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 #define REMOVABLE_SLEEP_DELAY 2
21 #include "petitboot-paths.h"
23 extern struct parser native_parser;
24 extern struct parser yaboot_parser;
28 /* array of parsers, ordered by priority */
29 static struct parser *parsers[] = {
35 #define log(...) fprintf(logf, __VA_ARGS__)
37 static void iterate_parsers(const char *devpath, const char *mountpoint)
41 log("trying parsers for %s@%s\n", devpath, mountpoint);
43 for (i = 0; parsers[i]; i++) {
44 log("\ttrying parser '%s'\n", parsers[i]->name);
45 /* just use a dummy device path for now */
46 if (parsers[i]->parse(devpath, mountpoint))
49 log("\tno boot_options found\n");
52 static void print_boot_option(const struct boot_option *opt)
54 log("\tname: %s\n", opt->name);
55 log("\tdescription: %s\n", opt->description);
56 log("\tboot_image: %s\n", opt->boot_image_file);
57 log("\tinitrd: %s\n", opt->initrd_file);
58 log("\tboot_args: %s\n", opt->boot_args);
62 static void print_device(const struct device *dev)
64 log("\tid: %s\n", dev->id);
65 log("\tname: %s\n", dev->name);
66 log("\tdescription: %s\n", dev->description);
67 log("\tboot_image: %s\n", dev->icon_file);
71 void free_device(struct device *dev)
80 free(dev->description);
86 void free_boot_option(struct boot_option *opt)
93 free(opt->description);
96 if (opt->boot_image_file)
97 free(opt->boot_image_file);
99 free(opt->initrd_file);
101 free(opt->boot_args);
105 static int write_action(int fd, enum device_action action)
107 uint8_t action_buf = action;
108 return write(fd, &action_buf, sizeof(action_buf)) != sizeof(action_buf);
111 static int write_string(int fd, const char *str)
118 if (write(fd, &len_buf, sizeof(len_buf)) != sizeof(len_buf)) {
119 log("write failed: %s\n", strerror(errno));
126 if (len > (1ull << (sizeof(len_buf) * 8 - 1))) {
127 log("string too large\n");
131 len_buf = __cpu_to_be32(len);
132 if (write(fd, &len_buf, sizeof(len_buf)) != sizeof(len_buf)) {
133 log("write failed: %s\n", strerror(errno));
138 int rc = write(fd, str, len - pos);
140 log("write failed: %s\n", strerror(errno));
150 int add_device(const struct device *dev)
154 log("device added:\n");
156 rc = write_action(sock, DEV_ACTION_ADD_DEVICE) ||
157 write_string(sock, dev->id) ||
158 write_string(sock, dev->name) ||
159 write_string(sock, dev->description) ||
160 write_string(sock, dev->icon_file);
163 log("error writing device %s to socket\n", dev->name);
168 int add_boot_option(const struct boot_option *opt)
172 log("boot option added:\n");
173 print_boot_option(opt);
175 rc = write_action(sock, DEV_ACTION_ADD_OPTION) ||
176 write_string(sock, opt->id) ||
177 write_string(sock, opt->name) ||
178 write_string(sock, opt->description) ||
179 write_string(sock, opt->icon_file) ||
180 write_string(sock, opt->boot_image_file) ||
181 write_string(sock, opt->initrd_file) ||
182 write_string(sock, opt->boot_args);
185 log("error writing boot option %s to socket\n", opt->name);
190 int remove_device(const char *dev_path)
192 return write_action(sock, DEV_ACTION_REMOVE_DEVICE) ||
193 write_string(sock, dev_path);
196 int connect_to_socket()
200 struct sockaddr_un addr;
202 fd = socket(PF_UNIX, SOCK_STREAM, 0);
204 log("can't create socket: %s\n", strerror(errno));
208 addr.sun_family = AF_UNIX;
209 strcpy(addr.sun_path, PBOOT_DEVICE_SOCKET);
211 if (connect(fd, (struct sockaddr *)&addr, sizeof(addr))) {
212 log("can't connect to %s: %s\n",
213 addr.sun_path, strerror(errno));
221 fd = open("./debug_socket", O_WRONLY | O_CREAT, 0640);
223 log("can't create output file: %s\n", strerror(errno));
231 int mount_device(const char *dev_path, char *mount_path)
234 const char *basename;
235 int pid, status, rc = -1;
237 basename = strrchr(dev_path, '/');
243 /* create a unique mountpoint */
244 dir = malloc(strlen(TMP_DIR) + 13 + strlen(basename));
245 sprintf(dir, "%s/mnt-%s-XXXXXX", TMP_DIR, basename);
248 log("failed to create temporary directory in %s: %s",
249 TMP_DIR, strerror(errno));
255 log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
260 execl(MOUNT_BIN, MOUNT_BIN, dev_path, dir, "-o", "ro", NULL);
264 if (waitpid(pid, &status, 0) == -1) {
265 log("%s: waitpid failed: %s\n", __FUNCTION__, strerror(errno));
269 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
270 strcpy(mount_path, dir);
279 static int unmount_device(const char *dev_path)
286 log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
291 execl(UMOUNT_BIN, UMOUNT_BIN, dev_path, NULL);
295 if (waitpid(pid, &status, 0) == -1) {
296 log("%s: waitpid failed: %s\n", __FUNCTION__, strerror(errno));
300 rc = !WIFEXITED(status) || WEXITSTATUS(status) != 0;
305 const char *generic_icon_file(enum generic_icon_type type)
309 return artwork_pathname("hdd.png");
311 return artwork_pathname("usbpen.png");
312 case ICON_TYPE_OPTICAL:
313 return artwork_pathname("cdrom.png");
314 case ICON_TYPE_NETWORK:
315 case ICON_TYPE_UNKNOWN:
318 return artwork_pathname("hdd.png");
321 static const struct device fake_boot_devices[] =
326 .icon_file = artwork_pathname("hdd.png"),
330 .name = "PinkCat Linux CD",
331 .icon_file = artwork_pathname("cdrom.png"),
335 static const struct boot_option fake_boot_options[] =
339 .name = "Bloobuntu Linux",
340 .description = "Boot Bloobuntu Linux",
341 .icon_file = artwork_pathname("hdd.png"),
345 .name = "Pendora Gore 6",
346 .description = "Boot Pendora Gora 6",
347 .icon_file = artwork_pathname("hdd.png"),
351 .name = "Genfoo Minux",
352 .description = "Boot Genfoo Minux",
353 .icon_file = artwork_pathname("hdd.png"),
357 .name = "PinkCat Linux",
358 .description = "Install PinkCat Linux - Graphical install",
359 .icon_file = artwork_pathname("cdrom.png"),
363 enum generic_icon_type guess_device_type(void)
365 const char *type = getenv("ID_TYPE");
366 const char *bus = getenv("ID_BUS");
367 if (type && streq(type, "cd"))
368 return ICON_TYPE_OPTICAL;
369 if (streq(bus, "usb"))
370 return ICON_TYPE_USB;
371 if (streq(bus, "ata") || streq(bus, "scsi"))
372 return ICON_TYPE_DISK;
373 return ICON_TYPE_UNKNOWN;
377 static int is_removable_device(const char *sysfs_path)
379 char full_path[PATH_MAX];
383 sprintf(full_path, "/sys/%s/removable", sysfs_path);
384 fd = open(full_path, O_RDONLY);
385 printf(" -> removable check on %s, fd=%d\n", full_path, fd);
388 buf_len = read(fd, buf, 79);
393 return strtol(buf, NULL, 10);
396 static int found_new_device(const char *dev_path)
398 char mountpoint[PATH_MAX];
400 if (mount_device(dev_path, mountpoint)) {
401 log("failed to mount %s\n", dev_path);
405 log("mounted %s at %s\n", dev_path, mountpoint);
407 iterate_parsers(dev_path, mountpoint);
412 static int poll_device_plug(const char *dev_path,
417 /* Polling loop for optical drive */
418 for (; (*optical) != 0; ) {
419 printf("poll for optical drive insertion ...\n");
420 fd = open(dev_path, O_RDONLY|O_NONBLOCK);
423 rc = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
426 printf("not an optical drive, fallback...\n");
430 if (rc == CDS_DISC_OK)
433 printf("no... waiting\n");
434 sleep(REMOVABLE_SLEEP_DELAY);
437 /* Fall back to bare open() */
440 printf("poll for non-optical drive insertion ...\n");
441 fd = open(dev_path, O_RDONLY);
442 if (fd < 0 && errno != ENOMEDIUM)
447 printf("no... waiting\n");
448 sleep(REMOVABLE_SLEEP_DELAY);
452 static int poll_device_unplug(const char *dev_path, int optical)
457 printf("poll for optical drive removal ...\n");
458 fd = open(dev_path, O_RDONLY|O_NONBLOCK);
461 rc = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
463 if (rc != CDS_DISC_OK)
465 printf("no... waiting\n");
466 sleep(REMOVABLE_SLEEP_DELAY);
469 /* Fall back to bare open() */
471 printf("poll for non-optical drive removal ...\n");
472 fd = open(dev_path, O_RDONLY);
473 if (fd < 0 && errno != ENOMEDIUM)
478 printf("no... waiting\n");
479 sleep(REMOVABLE_SLEEP_DELAY);
483 static int poll_removable_device(const char *sysfs_path,
484 const char *dev_path)
486 int rc, mounted, optical = -1;
489 rc = poll_device_plug(dev_path, &optical);
490 if (rc == EXIT_FAILURE)
492 rc = found_new_device(dev_path);
493 mounted = (rc == EXIT_SUCCESS);
495 poll_device_unplug(dev_path, optical);
497 remove_device(dev_path);
499 /* Unmount it repeatedly, if needs be */
500 while (mounted && !unmount_device(dev_path))
506 int main(int argc, char **argv)
508 char *dev_path, *action;
514 action = getenv("ACTION");
520 log("missing environment?\n");
524 if (connect_to_socket())
527 if (streq(action, "fake")) {
530 add_device(&fake_boot_devices[0]);
531 add_boot_option(&fake_boot_options[0]);
532 add_boot_option(&fake_boot_options[1]);
533 add_boot_option(&fake_boot_options[2]);
534 add_device(&fake_boot_devices[1]);
535 add_boot_option(&fake_boot_options[3]);
540 dev_path = getenv("DEVNAME");
542 log("missing environment?\n");
546 if (streq(action, "add")) {
547 char *sysfs_path = getenv("DEVPATH");
548 if (sysfs_path && is_removable_device(sysfs_path))
549 rc = poll_removable_device(sysfs_path, dev_path);
551 rc = found_new_device(dev_path);
552 } else if (streq(action, "remove")) {
553 log("%s removed\n", dev_path);
555 remove_device(dev_path);
557 /* Unmount it repeatedly, if needs be */
558 while (!unmount_device(dev_path))
562 log("invalid action '%s'\n", action);