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;
32 /* array of parsers, ordered by priority */
33 static struct parser *parsers[] = {
39 #define log(...) fprintf(logf, __VA_ARGS__)
41 static void iterate_parsers(const char *devpath, const char *mountpoint)
45 log("trying parsers for %s@%s\n", devpath, mountpoint);
47 for (i = 0; parsers[i]; i++) {
48 log("\ttrying parser '%s'\n", parsers[i]->name);
49 /* just use a dummy device path for now */
50 if (parsers[i]->parse(devpath, mountpoint))
53 log("\tno boot_options found\n");
56 static void print_boot_option(const struct boot_option *opt)
58 log("\tname: %s\n", opt->name);
59 log("\tdescription: %s\n", opt->description);
60 log("\tboot_image: %s\n", opt->boot_image_file);
61 log("\tinitrd: %s\n", opt->initrd_file);
62 log("\tboot_args: %s\n", opt->boot_args);
66 static void print_device(const struct device *dev)
68 log("\tid: %s\n", dev->id);
69 log("\tname: %s\n", dev->name);
70 log("\tdescription: %s\n", dev->description);
71 log("\tboot_image: %s\n", dev->icon_file);
75 void free_device(struct device *dev)
84 free(dev->description);
90 void free_boot_option(struct boot_option *opt)
97 free(opt->description);
100 if (opt->boot_image_file)
101 free(opt->boot_image_file);
102 if (opt->initrd_file)
103 free(opt->initrd_file);
105 free(opt->boot_args);
109 static int write_action(int fd, enum device_action action)
111 uint8_t action_buf = action;
112 return write(fd, &action_buf, sizeof(action_buf)) != sizeof(action_buf);
115 static int write_string(int fd, const char *str)
122 if (write(fd, &len_buf, sizeof(len_buf)) != sizeof(len_buf)) {
123 log("write failed: %s\n", strerror(errno));
130 if (len > (1ull << (sizeof(len_buf) * 8 - 1))) {
131 log("string too large\n");
135 len_buf = __cpu_to_be32(len);
136 if (write(fd, &len_buf, sizeof(len_buf)) != sizeof(len_buf)) {
137 log("write failed: %s\n", strerror(errno));
142 int rc = write(fd, str, len - pos);
144 log("write failed: %s\n", strerror(errno));
154 int add_device(const struct device *dev)
158 log("device added:\n");
160 rc = write_action(sock, DEV_ACTION_ADD_DEVICE) ||
161 write_string(sock, dev->id) ||
162 write_string(sock, dev->name) ||
163 write_string(sock, dev->description) ||
164 write_string(sock, dev->icon_file);
167 log("error writing device %s to socket\n", dev->name);
172 int add_boot_option(const struct boot_option *opt)
176 log("boot option added:\n");
177 print_boot_option(opt);
179 rc = write_action(sock, DEV_ACTION_ADD_OPTION) ||
180 write_string(sock, opt->id) ||
181 write_string(sock, opt->name) ||
182 write_string(sock, opt->description) ||
183 write_string(sock, opt->icon_file) ||
184 write_string(sock, opt->boot_image_file) ||
185 write_string(sock, opt->initrd_file) ||
186 write_string(sock, opt->boot_args);
189 log("error writing boot option %s to socket\n", opt->name);
194 int remove_device(const char *dev_path)
196 return write_action(sock, DEV_ACTION_REMOVE_DEVICE) ||
197 write_string(sock, dev_path);
200 int connect_to_socket()
202 #ifndef USE_FAKE_SOCKET
204 struct sockaddr_un addr;
206 fd = socket(PF_UNIX, SOCK_STREAM, 0);
208 log("can't create socket: %s\n", strerror(errno));
212 addr.sun_family = AF_UNIX;
213 strcpy(addr.sun_path, PBOOT_DEVICE_SOCKET);
215 if (connect(fd, (struct sockaddr *)&addr, sizeof(addr))) {
216 log("can't connect to %s: %s\n",
217 addr.sun_path, strerror(errno));
225 fd = open("./debug_socket", O_WRONLY | O_CREAT, 0640);
227 log("can't create output file: %s\n", strerror(errno));
235 int mount_device(const char *dev_path, char *mount_path)
238 const char *basename;
239 int pid, status, rc = -1;
241 basename = strrchr(dev_path, '/');
247 /* create a unique mountpoint */
248 dir = malloc(strlen(TMP_DIR) + 13 + strlen(basename));
249 sprintf(dir, "%s/mnt-%s-XXXXXX", TMP_DIR, basename);
252 log("failed to create temporary directory in %s: %s",
253 TMP_DIR, strerror(errno));
259 log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
264 execl(MOUNT_BIN, MOUNT_BIN, dev_path, dir, "-o", "ro", NULL);
268 if (waitpid(pid, &status, 0) == -1) {
269 log("%s: waitpid failed: %s\n", __FUNCTION__, strerror(errno));
273 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
274 strcpy(mount_path, dir);
283 static int unmount_device(const char *dev_path)
290 log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
295 execl(UMOUNT_BIN, UMOUNT_BIN, dev_path, NULL);
299 if (waitpid(pid, &status, 0) == -1) {
300 log("%s: waitpid failed: %s\n", __FUNCTION__, strerror(errno));
304 rc = !WIFEXITED(status) || WEXITSTATUS(status) != 0;
309 const char *generic_icon_file(enum generic_icon_type type)
313 return artwork_pathname("hdd.png");
315 return artwork_pathname("usbpen.png");
316 case ICON_TYPE_OPTICAL:
317 return artwork_pathname("cdrom.png");
318 case ICON_TYPE_NETWORK:
319 case ICON_TYPE_UNKNOWN:
322 return artwork_pathname("hdd.png");
325 static const struct device fake_boot_devices[] =
330 .icon_file = artwork_pathname("hdd.png"),
334 .name = "PinkCat Linux CD",
335 .icon_file = artwork_pathname("cdrom.png"),
339 static const struct boot_option fake_boot_options[] =
343 .name = "Bloobuntu Linux",
344 .description = "Boot Bloobuntu Linux",
345 .icon_file = artwork_pathname("hdd.png"),
349 .name = "Pendora Gore 6",
350 .description = "Boot Pendora Gora 6",
351 .icon_file = artwork_pathname("hdd.png"),
355 .name = "Genfoo Minux",
356 .description = "Boot Genfoo Minux",
357 .icon_file = artwork_pathname("hdd.png"),
361 .name = "PinkCat Linux",
362 .description = "Install PinkCat Linux - Graphical install",
363 .icon_file = artwork_pathname("cdrom.png"),
367 enum generic_icon_type guess_device_type(void)
369 const char *type = getenv("ID_TYPE");
370 const char *bus = getenv("ID_BUS");
371 if (type && streq(type, "cd"))
372 return ICON_TYPE_OPTICAL;
373 if (streq(bus, "usb"))
374 return ICON_TYPE_USB;
375 if (streq(bus, "ata") || streq(bus, "scsi"))
376 return ICON_TYPE_DISK;
377 return ICON_TYPE_UNKNOWN;
381 static int is_removable_device(const char *sysfs_path)
383 char full_path[PATH_MAX];
387 sprintf(full_path, "/sys/%s/removable", sysfs_path);
388 fd = open(full_path, O_RDONLY);
389 printf(" -> removable check on %s, fd=%d\n", full_path, fd);
392 buf_len = read(fd, buf, 79);
397 return strtol(buf, NULL, 10);
400 static int found_new_device(const char *dev_path)
402 char mountpoint[PATH_MAX];
404 if (mount_device(dev_path, mountpoint)) {
405 log("failed to mount %s\n", dev_path);
409 log("mounted %s at %s\n", dev_path, mountpoint);
411 iterate_parsers(dev_path, mountpoint);
416 static int poll_device_plug(const char *dev_path,
421 /* Polling loop for optical drive */
422 for (; (*optical) != 0; ) {
423 printf("poll for optical drive insertion ...\n");
424 fd = open(dev_path, O_RDONLY|O_NONBLOCK);
427 rc = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
430 printf("not an optical drive, fallback...\n");
434 if (rc == CDS_DISC_OK)
437 printf("no... waiting\n");
438 sleep(REMOVABLE_SLEEP_DELAY);
441 /* Fall back to bare open() */
444 printf("poll for non-optical drive insertion ...\n");
445 fd = open(dev_path, O_RDONLY);
446 if (fd < 0 && errno != ENOMEDIUM)
451 printf("no... waiting\n");
452 sleep(REMOVABLE_SLEEP_DELAY);
456 static int poll_device_unplug(const char *dev_path, int optical)
461 printf("poll for optical drive removal ...\n");
462 fd = open(dev_path, O_RDONLY|O_NONBLOCK);
465 rc = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
467 if (rc != CDS_DISC_OK)
469 printf("no... waiting\n");
470 sleep(REMOVABLE_SLEEP_DELAY);
473 /* Fall back to bare open() */
475 printf("poll for non-optical drive removal ...\n");
476 fd = open(dev_path, O_RDONLY);
477 if (fd < 0 && errno != ENOMEDIUM)
482 printf("no... waiting\n");
483 sleep(REMOVABLE_SLEEP_DELAY);
487 static int poll_removable_device(const char *sysfs_path,
488 const char *dev_path)
490 int rc, mounted, optical = -1;
493 rc = poll_device_plug(dev_path, &optical);
494 if (rc == EXIT_FAILURE)
496 rc = found_new_device(dev_path);
497 mounted = (rc == EXIT_SUCCESS);
499 poll_device_unplug(dev_path, optical);
501 remove_device(dev_path);
503 /* Unmount it repeatedly, if needs be */
504 while (mounted && !unmount_device(dev_path))
510 int main(int argc, char **argv)
512 char *dev_path, *action;
518 action = getenv("ACTION");
524 log("missing environment?\n");
528 if (connect_to_socket())
531 if (streq(action, "fake")) {
534 add_device(&fake_boot_devices[0]);
535 add_boot_option(&fake_boot_options[0]);
536 add_boot_option(&fake_boot_options[1]);
537 add_boot_option(&fake_boot_options[2]);
538 add_device(&fake_boot_devices[1]);
539 add_boot_option(&fake_boot_options[3]);
544 dev_path = getenv("DEVNAME");
546 log("missing environment?\n");
550 if (streq(action, "add")) {
551 char *sysfs_path = getenv("DEVPATH");
552 if (sysfs_path && is_removable_device(sysfs_path))
553 rc = poll_removable_device(sysfs_path, dev_path);
555 rc = found_new_device(dev_path);
556 } else if (streq(action, "remove")) {
557 log("%s removed\n", dev_path);
559 remove_device(dev_path);
561 /* Unmount it repeatedly, if needs be */
562 while (!unmount_device(dev_path))
566 log("invalid action '%s'\n", action);