]> git.ozlabs.org Git - petitboot/blob - devices/udev-helper.c
84b2bef95947cd16ea70ce5d311998f250ff5bd9
[petitboot] / devices / udev-helper.c
1
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <stdint.h>
5 #include <unistd.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <sys/socket.h>
9 #include <sys/wait.h>
10 #include <sys/un.h>
11 #include <fcntl.h>
12 #include <errno.h>
13 #include <string.h>
14 #include <asm/byteorder.h>
15 #include <linux/cdrom.h>
16 #include <sys/ioctl.h>
17
18 #include "udev-helper.h"
19 #include "petitboot-paths.h"
20
21 /* Define below to operate without the frontend */
22 #undef USE_FAKE_SOCKET
23
24 /* Delay in seconds between polling of removable devices */
25 #define REMOVABLE_SLEEP_DELAY   2
26
27 extern struct parser native_parser;
28 extern struct parser yaboot_parser;
29 extern struct parser kboot_parser;
30 static FILE *logf;
31 static int sock;
32
33 /* array of parsers, ordered by priority */
34 static struct parser *parsers[] = {
35         &native_parser,
36         &yaboot_parser,
37         &kboot_parser,
38         NULL
39 };
40
41 #define log(...) fprintf(logf, __VA_ARGS__)
42
43 static void iterate_parsers(const char *devpath, const char *mountpoint)
44 {
45         int i;
46
47         log("trying parsers for %s@%s\n", devpath, mountpoint);
48
49         for (i = 0; parsers[i]; i++) {
50                 log("\ttrying parser '%s'\n", parsers[i]->name);
51                 /* just use a dummy device path for now */
52                 if (parsers[i]->parse(devpath, mountpoint))
53                         /*return*/;
54         }
55         log("\tno boot_options found\n");
56 }
57
58 static void print_boot_option(const struct boot_option *opt)
59 {
60         log("\tname: %s\n", opt->name);
61         log("\tdescription: %s\n", opt->description);
62         log("\tboot_image: %s\n", opt->boot_image_file);
63         log("\tinitrd: %s\n", opt->initrd_file);
64         log("\tboot_args: %s\n", opt->boot_args);
65
66 }
67
68 static void print_device(const struct device *dev)
69 {
70         log("\tid: %s\n", dev->id);
71         log("\tname: %s\n", dev->name);
72         log("\tdescription: %s\n", dev->description);
73         log("\tboot_image: %s\n", dev->icon_file);
74 }
75
76
77 void free_device(struct device *dev)
78 {
79         if (!dev)
80                 return;
81         if (dev->id)
82                 free(dev->id);
83         if (dev->name)
84                 free(dev->name);
85         if (dev->description)
86                 free(dev->description);
87         if (dev->icon_file)
88                 free(dev->icon_file);
89         free(dev);
90 }
91
92 void free_boot_option(struct boot_option *opt)
93 {
94         if (!opt)
95                 return;
96         if (opt->name)
97                 free(opt->name);
98         if (opt->description)
99                 free(opt->description);
100         if (opt->icon_file)
101                 free(opt->icon_file);
102         if (opt->boot_image_file)
103                 free(opt->boot_image_file);
104         if (opt->initrd_file)
105                 free(opt->initrd_file);
106         if (opt->boot_args)
107                 free(opt->boot_args);
108         free(opt);
109 }
110
111 static int write_action(int fd, enum device_action action)
112 {
113         uint8_t action_buf = action;
114         return write(fd, &action_buf, sizeof(action_buf)) != sizeof(action_buf);
115 }
116
117 static int write_string(int fd, const char *str)
118 {
119         int len, pos = 0;
120         uint32_t len_buf;
121
122         if (!str) {
123                 len_buf = 0;
124                 if (write(fd, &len_buf, sizeof(len_buf)) != sizeof(len_buf)) {
125                         log("write failed: %s\n", strerror(errno));
126                         return -1;
127                 }
128                 return 0;
129         }
130
131         len = strlen(str);
132         if (len > (1ull << (sizeof(len_buf) * 8 - 1))) {
133                 log("string too large\n");
134                 return -1;
135         }
136
137         len_buf = __cpu_to_be32(len);
138         if (write(fd, &len_buf, sizeof(len_buf)) != sizeof(len_buf)) {
139                 log("write failed: %s\n", strerror(errno));
140                 return -1;
141         }
142
143         while (pos < len) {
144                 int rc = write(fd, str, len - pos);
145                 if (rc <= 0) {
146                         log("write failed: %s\n", strerror(errno));
147                         return -1;
148                 }
149                 pos += rc;
150                 str += rc;
151         }
152
153         return 0;
154 }
155
156 int add_device(const struct device *dev)
157 {
158         int rc;
159
160         log("device added:\n");
161         print_device(dev);
162         rc = write_action(sock, DEV_ACTION_ADD_DEVICE) ||
163                 write_string(sock, dev->id) ||
164                 write_string(sock, dev->name) ||
165                 write_string(sock, dev->description) ||
166                 write_string(sock, dev->icon_file);
167
168         if (rc)
169                 log("error writing device %s to socket\n", dev->name);
170
171         return rc;
172 }
173
174 int add_boot_option(const struct boot_option *opt)
175 {
176         int rc;
177
178         log("boot option added:\n");
179         print_boot_option(opt);
180
181         rc = write_action(sock, DEV_ACTION_ADD_OPTION) ||
182                 write_string(sock, opt->id) ||
183                 write_string(sock, opt->name) ||
184                 write_string(sock, opt->description) ||
185                 write_string(sock, opt->icon_file) ||
186                 write_string(sock, opt->boot_image_file) ||
187                 write_string(sock, opt->initrd_file) ||
188                 write_string(sock, opt->boot_args);
189
190         if (rc)
191                 log("error writing boot option %s to socket\n", opt->name);
192
193         return rc;
194 }
195
196 int remove_device(const char *dev_path)
197 {
198         return write_action(sock, DEV_ACTION_REMOVE_DEVICE) ||
199                 write_string(sock, dev_path);
200 }
201
202 int connect_to_socket()
203 {
204 #ifndef USE_FAKE_SOCKET
205         int fd;
206         struct sockaddr_un addr;
207
208         fd = socket(PF_UNIX, SOCK_STREAM, 0);
209         if (fd == -1) {
210                 log("can't create socket: %s\n", strerror(errno));
211                 return -1;
212         }
213
214         addr.sun_family = AF_UNIX;
215         strcpy(addr.sun_path, PBOOT_DEVICE_SOCKET);
216
217         if (connect(fd, (struct sockaddr *)&addr, sizeof(addr))) {
218                 log("can't connect to %s: %s\n",
219                                 addr.sun_path, strerror(errno));
220                 return -1;
221         }
222         sock = fd;
223
224         return 0;
225 #else
226         int fd;
227         fd = open("./debug_socket", O_WRONLY | O_CREAT, 0640);
228         if (fd < 0) {
229                 log("can't create output file: %s\n", strerror(errno));
230                 return -1;
231         }
232         sock = fd;
233         return 0;
234 #endif
235 }
236
237 int mount_device(const char *dev_path, char *mount_path)
238 {
239         char *dir;
240         const char *basename;
241         int pid, status, rc = -1;
242
243         basename = strrchr(dev_path, '/');
244         if (basename)
245                 basename++;
246         else
247                 basename = dev_path;
248  
249         /* create a unique mountpoint */
250         dir = malloc(strlen(TMP_DIR) + 13 + strlen(basename));
251         sprintf(dir, "%s/mnt-%s-XXXXXX", TMP_DIR, basename);
252
253         if (!mkdtemp(dir)) {
254                 log("failed to create temporary directory in %s: %s",
255                                 TMP_DIR, strerror(errno));
256                 goto out;
257         }
258
259         pid = fork();
260         if (pid == -1) {
261                 log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
262                 goto out;
263         }
264
265         if (pid == 0) {
266                 execl(MOUNT_BIN, MOUNT_BIN, dev_path, dir, "-o", "ro", NULL);
267                 exit(EXIT_FAILURE);
268         }
269
270         if (waitpid(pid, &status, 0) == -1) {
271                 log("%s: waitpid failed: %s\n", __FUNCTION__, strerror(errno));
272                 goto out;
273         }
274
275         if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
276                 strcpy(mount_path, dir);
277                 rc = 0;
278         }
279
280 out:
281         free(dir);
282         return rc;
283 }
284
285 static int unmount_device(const char *dev_path)
286 {
287         int pid, status, rc;
288
289         pid = fork();
290
291         if (pid == -1) {
292                 log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
293                 return -1;
294         }
295
296         if (pid == 0) {
297                 execl(UMOUNT_BIN, UMOUNT_BIN, dev_path, NULL);
298                 exit(EXIT_FAILURE);
299         }
300
301         if (waitpid(pid, &status, 0) == -1) {
302                 log("%s: waitpid failed: %s\n", __FUNCTION__, strerror(errno));
303                 return -1;
304         }
305
306         rc = !WIFEXITED(status) || WEXITSTATUS(status) != 0;
307
308         return rc;
309 }
310
311 const char *generic_icon_file(enum generic_icon_type type)
312 {
313         switch (type) {
314         case ICON_TYPE_DISK:
315                 return artwork_pathname("hdd.png");
316         case ICON_TYPE_USB:
317                 return artwork_pathname("usbpen.png");
318         case ICON_TYPE_OPTICAL:
319                 return artwork_pathname("cdrom.png");
320         case ICON_TYPE_NETWORK:
321         case ICON_TYPE_UNKNOWN:
322                 break;
323         }
324         return artwork_pathname("hdd.png");
325 }
326
327 static const struct device fake_boot_devices[] =
328 {
329         {
330                 .id             = "fakeDisk0",
331                 .name           = "Hard Disk",
332                 .icon_file      = artwork_pathname("hdd.png"),
333         },
334         {
335                 .id             = "fakeDisk1",
336                 .name           = "PinkCat Linux CD",
337                 .icon_file      = artwork_pathname("cdrom.png"),
338         }
339 };
340
341 static const struct boot_option fake_boot_options[] =
342 {
343         {
344                 .id             = "fakeBoot0",
345                 .name           = "Bloobuntu Linux",
346                 .description    = "Boot Bloobuntu Linux",
347                 .icon_file      = artwork_pathname("hdd.png"),
348         },
349         {
350                 .id             = "fakeBoot1",
351                 .name           = "Pendora Gore 6",
352                 .description    = "Boot Pendora Gora 6",
353                 .icon_file      = artwork_pathname("hdd.png"),
354         },
355         {
356                 .id             = "fakeBoot2",
357                 .name           = "Genfoo Minux",
358                 .description    = "Boot Genfoo Minux",
359                 .icon_file      = artwork_pathname("hdd.png"),
360         },
361         {
362                 .id             = "fakeBoot3",
363                 .name           = "PinkCat Linux",
364                 .description    = "Install PinkCat Linux - Graphical install",
365                 .icon_file      = artwork_pathname("cdrom.png"),
366         },
367 };
368
369 enum generic_icon_type guess_device_type(void)
370 {
371         const char *type = getenv("ID_TYPE");
372         const char *bus = getenv("ID_BUS");
373         if (type && streq(type, "cd"))
374                 return ICON_TYPE_OPTICAL;
375         if (!bus)
376                 return ICON_TYPE_UNKNOWN;
377         if (streq(bus, "usb"))
378                 return ICON_TYPE_USB;
379         if (streq(bus, "ata") || streq(bus, "scsi"))
380                 return ICON_TYPE_DISK;
381         return ICON_TYPE_UNKNOWN;
382 }
383
384
385 static int is_removable_device(const char *sysfs_path) 
386 {
387         char full_path[PATH_MAX];
388         char buf[80];
389         int fd, buf_len;
390
391         sprintf(full_path, "/sys/%s/removable", sysfs_path);    
392         fd = open(full_path, O_RDONLY);
393         printf(" -> removable check on %s, fd=%d\n", full_path, fd);
394         if (fd < 0)
395                 return 0;
396         buf_len = read(fd, buf, 79);
397         close(fd);
398         if (buf_len < 0)
399                 return 0;
400         buf[buf_len] = 0;
401         return strtol(buf, NULL, 10);
402 }
403
404 static int is_ignored_device(const char *devname)
405 {
406         static const char *ignored_devices[] = { "/dev/ram", NULL };
407         const char **dev;
408
409         for (dev = ignored_devices; *dev; dev++)
410                 if (!strncmp(devname, *dev, strlen(*dev)))
411                         return 1;
412
413         return 0;
414 }
415
416 static int found_new_device(const char *dev_path)
417 {
418         char mountpoint[PATH_MAX];
419
420         if (mount_device(dev_path, mountpoint)) {
421                 log("failed to mount %s\n", dev_path);
422                 return EXIT_FAILURE;
423         }
424
425         log("mounted %s at %s\n", dev_path, mountpoint);
426
427         iterate_parsers(dev_path, mountpoint);
428
429         return EXIT_SUCCESS;
430 }
431
432 static void detach_and_sleep(int sec)
433 {
434         static int forked = 0;
435         int rc = 0;
436
437         if (sec <= 0)
438                 return;
439
440         if (!forked) {
441                 log("running in background...");
442                 rc = fork();
443                 forked = 1;
444         }
445
446         if (rc == 0) {
447                 sleep(sec);
448
449         } else if (rc == -1) {
450                 perror("fork()");
451                 exit(EXIT_FAILURE);
452         } else {
453                 exit(EXIT_SUCCESS);
454         }
455 }
456
457 static int poll_device_plug(const char *dev_path,
458                             int *optical)
459 {
460         int rc, fd;
461
462         /* Polling loop for optical drive */
463         for (; (*optical) != 0; ) {
464                 printf("poll for optical drive insertion ...\n");
465                 fd = open(dev_path, O_RDONLY|O_NONBLOCK);
466                 if (fd < 0)
467                         return EXIT_FAILURE;
468                 rc = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
469                 close(fd);
470                 if (rc == -1) {
471                         printf("not an optical drive, fallback...\n");
472                         break;
473                 }
474                 *optical = 1;
475                 if (rc == CDS_DISC_OK)
476                         return EXIT_SUCCESS;
477
478                 printf("no... waiting\n");
479                 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
480         }
481
482         /* Fall back to bare open() */
483         *optical = 0;
484         for (;;) {
485                 printf("poll for non-optical drive insertion ...\n");
486                 fd = open(dev_path, O_RDONLY);
487                 if (fd < 0 && errno != ENOMEDIUM)
488                         return EXIT_FAILURE;
489                 close(fd);
490                 if (fd >= 0)
491                         return EXIT_SUCCESS;
492                 printf("no... waiting\n");
493                 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
494         }
495 }
496
497 static int poll_device_unplug(const char *dev_path, int optical)
498 {
499         int rc, fd;
500
501         for (;optical;) {
502                 printf("poll for optical drive removal ...\n");
503                 fd = open(dev_path, O_RDONLY|O_NONBLOCK);
504                 if (fd < 0)
505                         return EXIT_FAILURE;
506                 rc = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
507                 close(fd);
508                 if (rc != CDS_DISC_OK)
509                         return EXIT_SUCCESS;
510                 printf("no... waiting\n");
511                 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
512         }
513
514         /* Fall back to bare open() */
515         for (;;) {
516                 printf("poll for non-optical drive removal ...\n");
517                 fd = open(dev_path, O_RDONLY);
518                 if (fd < 0 && errno != ENOMEDIUM)
519                         return EXIT_FAILURE;
520                 close(fd);
521                 if (fd < 0)
522                         return EXIT_SUCCESS;
523                 printf("no... waiting\n");
524                 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
525         }
526 }
527
528 static int poll_removable_device(const char *sysfs_path,
529                                  const char *dev_path)
530 {
531         int rc, mounted, optical = -1;
532        
533         for (;;) {
534                 rc = poll_device_plug(dev_path, &optical);
535                 if (rc == EXIT_FAILURE)
536                         return rc;
537                 rc = found_new_device(dev_path);
538                 mounted = (rc == EXIT_SUCCESS);
539
540                 poll_device_unplug(dev_path, optical);
541
542                 remove_device(dev_path);
543
544                 /* Unmount it repeatedly, if needs be */
545                 while (mounted && !unmount_device(dev_path))
546                         ;
547                 detach_and_sleep(1);
548         }
549 }
550
551 int main(int argc, char **argv)
552 {
553         char *dev_path, *action;
554         int rc;
555
556         action = getenv("ACTION");
557
558         logf = stdout;
559         rc = EXIT_SUCCESS;
560
561         if (!action) {
562                 log("missing environment?\n");
563                 return EXIT_FAILURE;
564         }
565
566         if (connect_to_socket())
567                 return EXIT_FAILURE;
568
569         if (streq(action, "fake")) {
570                 log("fake mode");
571
572                 add_device(&fake_boot_devices[0]);
573                 add_boot_option(&fake_boot_options[0]);
574                 add_boot_option(&fake_boot_options[1]);
575                 add_boot_option(&fake_boot_options[2]);
576                 add_device(&fake_boot_devices[1]);
577                 add_boot_option(&fake_boot_options[3]);
578
579                 return EXIT_SUCCESS;
580         }
581
582         dev_path = getenv("DEVNAME");
583         if (!dev_path) {
584                 log("missing environment?\n");
585                 return EXIT_FAILURE;
586         }
587
588         if (is_ignored_device(dev_path))
589                 return EXIT_SUCCESS;
590
591         if (streq(action, "add")) {
592                 char *sysfs_path = getenv("DEVPATH");
593                 if (sysfs_path && is_removable_device(sysfs_path))
594                         rc = poll_removable_device(sysfs_path, dev_path);
595                 else
596                         rc = found_new_device(dev_path);
597         } else if (streq(action, "remove")) {
598                 log("%s removed\n", dev_path);
599
600                 remove_device(dev_path);
601
602                 /* Unmount it repeatedly, if needs be */
603                 while (!unmount_device(dev_path))
604                         ;
605
606         } else {
607                 log("invalid action '%s'\n", action);
608                 rc = EXIT_FAILURE;
609         }
610         return rc;
611 }
612
613 /* convenience function for parsers */
614 char *join_paths(const char *a, const char *b)
615 {
616         char *full_path;
617
618         full_path = malloc(strlen(a) + strlen(b) + 2);
619
620         strcpy(full_path, a);
621         if (b[0] != '/')
622                 strcat(full_path, "/");
623         strcat(full_path, b);
624
625         return full_path;
626 }
627