9 #include <sys/socket.h>
15 #include <asm/byteorder.h>
16 #include <linux/cdrom.h>
17 #include <sys/ioctl.h>
20 #include "petitboot-paths.h"
22 /* Define below to operate without the frontend */
23 #undef USE_FAKE_SOCKET
25 /* Delay in seconds between polling of removable devices */
26 #define REMOVABLE_SLEEP_DELAY 2
31 void pb_log(const char *fmt, ...)
36 vfprintf(logf, fmt, ap);
40 static void print_boot_option(const struct boot_option *opt)
42 pb_log("\tname: %s\n", opt->name);
43 pb_log("\tdescription: %s\n", opt->description);
44 pb_log("\tboot_image: %s\n", opt->boot_image_file);
45 pb_log("\tinitrd: %s\n", opt->initrd_file);
46 pb_log("\tboot_args: %s\n", opt->boot_args);
50 static void print_device(const struct device *dev)
52 pb_log("\tid: %s\n", dev->id);
53 pb_log("\tname: %s\n", dev->name);
54 pb_log("\tdescription: %s\n", dev->description);
55 pb_log("\tboot_image: %s\n", dev->icon_file);
58 static int write_action(int fd, enum device_action action)
60 uint8_t action_buf = action;
61 return write(fd, &action_buf, sizeof(action_buf)) != sizeof(action_buf);
64 static int write_string(int fd, const char *str)
71 if (write(fd, &len_buf, sizeof(len_buf)) != sizeof(len_buf)) {
72 pb_log("write failed: %s\n", strerror(errno));
79 if (len > (1ull << (sizeof(len_buf) * 8 - 1))) {
80 pb_log("string too large\n");
84 len_buf = __cpu_to_be32(len);
85 if (write(fd, &len_buf, sizeof(len_buf)) != sizeof(len_buf)) {
86 pb_log("write failed: %s\n", strerror(errno));
91 int rc = write(fd, str, len - pos);
93 pb_log("write failed: %s\n", strerror(errno));
103 int add_device(const struct device *dev)
107 pb_log("device added:\n");
109 rc = write_action(sock, DEV_ACTION_ADD_DEVICE) ||
110 write_string(sock, dev->id) ||
111 write_string(sock, dev->name) ||
112 write_string(sock, dev->description) ||
113 write_string(sock, dev->icon_file);
116 pb_log("error writing device %s to socket\n", dev->name);
121 int add_boot_option(const struct boot_option *opt)
125 pb_log("boot option added:\n");
126 print_boot_option(opt);
128 rc = write_action(sock, DEV_ACTION_ADD_OPTION) ||
129 write_string(sock, opt->id) ||
130 write_string(sock, opt->name) ||
131 write_string(sock, opt->description) ||
132 write_string(sock, opt->icon_file) ||
133 write_string(sock, opt->boot_image_file) ||
134 write_string(sock, opt->initrd_file) ||
135 write_string(sock, opt->boot_args);
138 pb_log("error writing boot option %s to socket\n", opt->name);
143 int remove_device(const char *dev_path)
145 return write_action(sock, DEV_ACTION_REMOVE_DEVICE) ||
146 write_string(sock, dev_path);
149 int connect_to_socket()
151 #ifndef USE_FAKE_SOCKET
153 struct sockaddr_un addr;
155 fd = socket(PF_UNIX, SOCK_STREAM, 0);
157 pb_log("can't create socket: %s\n", strerror(errno));
161 addr.sun_family = AF_UNIX;
162 strcpy(addr.sun_path, PBOOT_DEVICE_SOCKET);
164 if (connect(fd, (struct sockaddr *)&addr, sizeof(addr))) {
165 pb_log("can't connect to %s: %s\n",
166 addr.sun_path, strerror(errno));
174 fd = open("./debug_socket", O_WRONLY | O_CREAT, 0640);
176 pb_log("can't create output file: %s\n", strerror(errno));
184 int mount_device(const char *dev_path, char *mount_path)
187 const char *basename;
188 int pid, status, rc = -1;
190 basename = strrchr(dev_path, '/');
196 /* create a unique mountpoint */
197 dir = malloc(strlen(TMP_DIR) + 13 + strlen(basename));
198 sprintf(dir, "%s/mnt-%s-XXXXXX", TMP_DIR, basename);
201 pb_log("failed to create temporary directory in %s: %s",
202 TMP_DIR, strerror(errno));
208 pb_log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
213 execl(MOUNT_BIN, MOUNT_BIN, dev_path, dir, "-o", "ro", NULL);
217 if (waitpid(pid, &status, 0) == -1) {
218 pb_log("%s: waitpid failed: %s\n", __FUNCTION__,
223 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
224 strcpy(mount_path, dir);
233 static int unmount_device(const char *dev_path)
240 pb_log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
245 execl(UMOUNT_BIN, UMOUNT_BIN, dev_path, NULL);
249 if (waitpid(pid, &status, 0) == -1) {
250 pb_log("%s: waitpid failed: %s\n", __FUNCTION__,
255 rc = !WIFEXITED(status) || WEXITSTATUS(status) != 0;
260 static const struct device fake_boot_devices[] =
265 .icon_file = artwork_pathname("hdd.png"),
269 .name = "PinkCat Linux CD",
270 .icon_file = artwork_pathname("cdrom.png"),
274 static const struct boot_option fake_boot_options[] =
278 .name = "Bloobuntu Linux",
279 .description = "Boot Bloobuntu Linux",
280 .icon_file = artwork_pathname("hdd.png"),
284 .name = "Pendora Gore 6",
285 .description = "Boot Pendora Gora 6",
286 .icon_file = artwork_pathname("hdd.png"),
290 .name = "Genfoo Minux",
291 .description = "Boot Genfoo Minux",
292 .icon_file = artwork_pathname("hdd.png"),
296 .name = "PinkCat Linux",
297 .description = "Install PinkCat Linux - Graphical install",
298 .icon_file = artwork_pathname("cdrom.png"),
302 enum generic_icon_type guess_device_type(void)
304 const char *type = getenv("ID_TYPE");
305 const char *bus = getenv("ID_BUS");
306 if (type && streq(type, "cd"))
307 return ICON_TYPE_OPTICAL;
309 return ICON_TYPE_UNKNOWN;
310 if (streq(bus, "usb"))
311 return ICON_TYPE_USB;
312 if (streq(bus, "ata") || streq(bus, "scsi"))
313 return ICON_TYPE_DISK;
314 return ICON_TYPE_UNKNOWN;
318 static int is_removable_device(const char *sysfs_path)
320 char full_path[PATH_MAX];
324 sprintf(full_path, "/sys/%s/removable", sysfs_path);
325 fd = open(full_path, O_RDONLY);
326 pb_log(" -> removable check on %s, fd=%d\n", full_path, fd);
329 buf_len = read(fd, buf, 79);
334 return strtol(buf, NULL, 10);
337 static int is_ignored_device(const char *devname)
339 static const char *ignored_devices[] = { "/dev/ram", NULL };
342 for (dev = ignored_devices; *dev; dev++)
343 if (!strncmp(devname, *dev, strlen(*dev)))
349 static int found_new_device(const char *dev_path)
351 char mountpoint[PATH_MAX];
353 if (mount_device(dev_path, mountpoint)) {
354 pb_log("failed to mount %s\n", dev_path);
358 pb_log("mounted %s at %s\n", dev_path, mountpoint);
360 iterate_parsers(dev_path, mountpoint);
365 static void detach_and_sleep(int sec)
367 static int forked = 0;
374 pb_log("running in background...");
382 } else if (rc == -1) {
390 static int poll_device_plug(const char *dev_path,
395 /* Polling loop for optical drive */
396 for (; (*optical) != 0; ) {
397 pb_log("poll for optical drive insertion ...\n");
398 fd = open(dev_path, O_RDONLY|O_NONBLOCK);
401 rc = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
404 pb_log("not an optical drive, fallback...\n");
408 if (rc == CDS_DISC_OK)
411 pb_log("no... waiting\n");
412 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
415 /* Fall back to bare open() */
418 pb_log("poll for non-optical drive insertion ...\n");
419 fd = open(dev_path, O_RDONLY);
420 if (fd < 0 && errno != ENOMEDIUM)
425 pb_log("no... waiting\n");
426 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
430 static int poll_device_unplug(const char *dev_path, int optical)
435 pb_log("poll for optical drive removal ...\n");
436 fd = open(dev_path, O_RDONLY|O_NONBLOCK);
439 rc = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
441 if (rc != CDS_DISC_OK)
443 pb_log("no... waiting\n");
444 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
447 /* Fall back to bare open() */
449 pb_log("poll for non-optical drive removal ...\n");
450 fd = open(dev_path, O_RDONLY);
451 if (fd < 0 && errno != ENOMEDIUM)
456 pb_log("no... waiting\n");
457 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
461 static int poll_removable_device(const char *sysfs_path,
462 const char *dev_path)
464 int rc, mounted, optical = -1;
467 rc = poll_device_plug(dev_path, &optical);
468 if (rc == EXIT_FAILURE)
470 rc = found_new_device(dev_path);
471 mounted = (rc == EXIT_SUCCESS);
473 poll_device_unplug(dev_path, optical);
475 remove_device(dev_path);
477 /* Unmount it repeatedly, if needs be */
478 while (mounted && !unmount_device(dev_path))
484 int main(int argc, char **argv)
486 char *dev_path, *action;
489 action = getenv("ACTION");
491 logf = fopen("/var/tmp/petitboot-udev-helpers.log", "a");
494 pb_log("%d started\n", getpid());
498 pb_log("missing environment?\n");
502 if (connect_to_socket())
505 if (streq(action, "fake")) {
508 add_device(&fake_boot_devices[0]);
509 add_boot_option(&fake_boot_options[0]);
510 add_boot_option(&fake_boot_options[1]);
511 add_boot_option(&fake_boot_options[2]);
512 add_device(&fake_boot_devices[1]);
513 add_boot_option(&fake_boot_options[3]);
518 dev_path = getenv("DEVNAME");
520 pb_log("missing environment?\n");
524 if (is_ignored_device(dev_path))
527 if (streq(action, "add")) {
528 char *sysfs_path = getenv("DEVPATH");
529 if (sysfs_path && is_removable_device(sysfs_path))
530 rc = poll_removable_device(sysfs_path, dev_path);
532 rc = found_new_device(dev_path);
533 } else if (streq(action, "remove")) {
534 pb_log("%s removed\n", dev_path);
536 remove_device(dev_path);
538 /* Unmount it repeatedly, if needs be */
539 while (!unmount_device(dev_path))
543 pb_log("invalid action '%s'\n", action);