8 #include <sys/socket.h>
14 #include <asm/byteorder.h>
15 #include <linux/cdrom.h>
16 #include <sys/ioctl.h>
18 #include "udev-helper.h"
19 #include "petitboot-paths.h"
21 /* Define below to operate without the frontend */
22 #undef USE_FAKE_SOCKET
24 /* Delay in seconds between polling of removable devices */
25 #define REMOVABLE_SLEEP_DELAY 2
27 extern struct parser native_parser;
28 extern struct parser yaboot_parser;
29 extern struct parser kboot_parser;
33 /* array of parsers, ordered by priority */
34 static struct parser *parsers[] = {
41 #define log(...) fprintf(logf, __VA_ARGS__)
43 static void iterate_parsers(const char *devpath, const char *mountpoint)
47 log("trying parsers for %s@%s\n", devpath, mountpoint);
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))
55 log("\tno boot_options found\n");
58 static void print_boot_option(const struct boot_option *opt)
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);
68 static void print_device(const struct device *dev)
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);
77 void free_device(struct device *dev)
86 free(dev->description);
92 void free_boot_option(struct boot_option *opt)
99 free(opt->description);
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);
107 free(opt->boot_args);
111 static int write_action(int fd, enum device_action action)
113 uint8_t action_buf = action;
114 return write(fd, &action_buf, sizeof(action_buf)) != sizeof(action_buf);
117 static int write_string(int fd, const char *str)
124 if (write(fd, &len_buf, sizeof(len_buf)) != sizeof(len_buf)) {
125 log("write failed: %s\n", strerror(errno));
132 if (len > (1ull << (sizeof(len_buf) * 8 - 1))) {
133 log("string too large\n");
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));
144 int rc = write(fd, str, len - pos);
146 log("write failed: %s\n", strerror(errno));
156 int add_device(const struct device *dev)
160 log("device added:\n");
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);
169 log("error writing device %s to socket\n", dev->name);
174 int add_boot_option(const struct boot_option *opt)
178 log("boot option added:\n");
179 print_boot_option(opt);
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);
191 log("error writing boot option %s to socket\n", opt->name);
196 int remove_device(const char *dev_path)
198 return write_action(sock, DEV_ACTION_REMOVE_DEVICE) ||
199 write_string(sock, dev_path);
202 int connect_to_socket()
204 #ifndef USE_FAKE_SOCKET
206 struct sockaddr_un addr;
208 fd = socket(PF_UNIX, SOCK_STREAM, 0);
210 log("can't create socket: %s\n", strerror(errno));
214 addr.sun_family = AF_UNIX;
215 strcpy(addr.sun_path, PBOOT_DEVICE_SOCKET);
217 if (connect(fd, (struct sockaddr *)&addr, sizeof(addr))) {
218 log("can't connect to %s: %s\n",
219 addr.sun_path, strerror(errno));
227 fd = open("./debug_socket", O_WRONLY | O_CREAT, 0640);
229 log("can't create output file: %s\n", strerror(errno));
237 int mount_device(const char *dev_path, char *mount_path)
240 const char *basename;
241 int pid, status, rc = -1;
243 basename = strrchr(dev_path, '/');
249 /* create a unique mountpoint */
250 dir = malloc(strlen(TMP_DIR) + 13 + strlen(basename));
251 sprintf(dir, "%s/mnt-%s-XXXXXX", TMP_DIR, basename);
254 log("failed to create temporary directory in %s: %s",
255 TMP_DIR, strerror(errno));
261 log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
266 execl(MOUNT_BIN, MOUNT_BIN, dev_path, dir, "-o", "ro", NULL);
270 if (waitpid(pid, &status, 0) == -1) {
271 log("%s: waitpid failed: %s\n", __FUNCTION__, strerror(errno));
275 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
276 strcpy(mount_path, dir);
285 static int unmount_device(const char *dev_path)
292 log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
297 execl(UMOUNT_BIN, UMOUNT_BIN, dev_path, NULL);
301 if (waitpid(pid, &status, 0) == -1) {
302 log("%s: waitpid failed: %s\n", __FUNCTION__, strerror(errno));
306 rc = !WIFEXITED(status) || WEXITSTATUS(status) != 0;
311 const char *generic_icon_file(enum generic_icon_type type)
315 return artwork_pathname("hdd.png");
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:
324 return artwork_pathname("hdd.png");
327 static const struct device fake_boot_devices[] =
332 .icon_file = artwork_pathname("hdd.png"),
336 .name = "PinkCat Linux CD",
337 .icon_file = artwork_pathname("cdrom.png"),
341 static const struct boot_option fake_boot_options[] =
345 .name = "Bloobuntu Linux",
346 .description = "Boot Bloobuntu Linux",
347 .icon_file = artwork_pathname("hdd.png"),
351 .name = "Pendora Gore 6",
352 .description = "Boot Pendora Gora 6",
353 .icon_file = artwork_pathname("hdd.png"),
357 .name = "Genfoo Minux",
358 .description = "Boot Genfoo Minux",
359 .icon_file = artwork_pathname("hdd.png"),
363 .name = "PinkCat Linux",
364 .description = "Install PinkCat Linux - Graphical install",
365 .icon_file = artwork_pathname("cdrom.png"),
369 enum generic_icon_type guess_device_type(void)
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;
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;
385 static int is_removable_device(const char *sysfs_path)
387 char full_path[PATH_MAX];
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);
396 buf_len = read(fd, buf, 79);
401 return strtol(buf, NULL, 10);
404 static int is_ignored_device(const char *devname)
406 static const char *ignored_devices[] = { "/dev/ram", NULL };
409 for (dev = ignored_devices; *dev; dev++)
410 if (!strncmp(devname, *dev, strlen(*dev)))
416 static int found_new_device(const char *dev_path)
418 char mountpoint[PATH_MAX];
420 if (mount_device(dev_path, mountpoint)) {
421 log("failed to mount %s\n", dev_path);
425 log("mounted %s at %s\n", dev_path, mountpoint);
427 iterate_parsers(dev_path, mountpoint);
432 static void detach_and_sleep(int sec)
434 static int forked = 0;
441 log("running in background...");
449 } else if (rc == -1) {
457 static int poll_device_plug(const char *dev_path,
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);
468 rc = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
471 printf("not an optical drive, fallback...\n");
475 if (rc == CDS_DISC_OK)
478 printf("no... waiting\n");
479 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
482 /* Fall back to bare open() */
485 printf("poll for non-optical drive insertion ...\n");
486 fd = open(dev_path, O_RDONLY);
487 if (fd < 0 && errno != ENOMEDIUM)
492 printf("no... waiting\n");
493 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
497 static int poll_device_unplug(const char *dev_path, int optical)
502 printf("poll for optical drive removal ...\n");
503 fd = open(dev_path, O_RDONLY|O_NONBLOCK);
506 rc = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
508 if (rc != CDS_DISC_OK)
510 printf("no... waiting\n");
511 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
514 /* Fall back to bare open() */
516 printf("poll for non-optical drive removal ...\n");
517 fd = open(dev_path, O_RDONLY);
518 if (fd < 0 && errno != ENOMEDIUM)
523 printf("no... waiting\n");
524 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
528 static int poll_removable_device(const char *sysfs_path,
529 const char *dev_path)
531 int rc, mounted, optical = -1;
534 rc = poll_device_plug(dev_path, &optical);
535 if (rc == EXIT_FAILURE)
537 rc = found_new_device(dev_path);
538 mounted = (rc == EXIT_SUCCESS);
540 poll_device_unplug(dev_path, optical);
542 remove_device(dev_path);
544 /* Unmount it repeatedly, if needs be */
545 while (mounted && !unmount_device(dev_path))
551 int main(int argc, char **argv)
553 char *dev_path, *action;
556 action = getenv("ACTION");
562 log("missing environment?\n");
566 if (connect_to_socket())
569 if (streq(action, "fake")) {
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]);
582 dev_path = getenv("DEVNAME");
584 log("missing environment?\n");
588 if (is_ignored_device(dev_path))
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);
596 rc = found_new_device(dev_path);
597 } else if (streq(action, "remove")) {
598 log("%s removed\n", dev_path);
600 remove_device(dev_path);
602 /* Unmount it repeatedly, if needs be */
603 while (!unmount_device(dev_path))
607 log("invalid action '%s'\n", action);
613 /* convenience function for parsers */
614 char *join_paths(const char *a, const char *b)
618 full_path = malloc(strlen(a) + strlen(b) + 2);
620 strcpy(full_path, a);
622 strcat(full_path, "/");
623 strcat(full_path, b);