10 #include <sys/types.h>
12 #include <sys/socket.h>
18 #include <asm/byteorder.h>
19 #include <linux/cdrom.h>
20 #include <sys/ioctl.h>
24 #include "petitboot-paths.h"
26 /* Define below to operate without the frontend */
27 #undef USE_FAKE_SOCKET
29 /* Delay in seconds between polling of removable devices */
30 #define REMOVABLE_SLEEP_DELAY 2
35 void pb_log(const char *fmt, ...)
40 vfprintf(logf, fmt, ap);
44 static void print_boot_option(const struct boot_option *opt)
46 pb_log("\tname: %s\n", opt->name);
47 pb_log("\tdescription: %s\n", opt->description);
48 pb_log("\tboot_image: %s\n", opt->boot_image_file);
49 pb_log("\tinitrd: %s\n", opt->initrd_file);
50 pb_log("\tboot_args: %s\n", opt->boot_args);
54 static void print_device(const struct device *dev)
56 pb_log("\tid: %s\n", dev->id);
57 pb_log("\tname: %s\n", dev->name);
58 pb_log("\tdescription: %s\n", dev->description);
59 pb_log("\tboot_image: %s\n", dev->icon_file);
62 static int write_action(int fd, enum device_action action)
64 uint8_t action_buf = action;
65 return write(fd, &action_buf, sizeof(action_buf)) != sizeof(action_buf);
68 static int write_string(int fd, const char *str)
75 if (write(fd, &len_buf, sizeof(len_buf)) != sizeof(len_buf)) {
76 pb_log("write failed: %s\n", strerror(errno));
83 if (len > (1ull << (sizeof(len_buf) * 8 - 1))) {
84 pb_log("string too large\n");
88 len_buf = __cpu_to_be32(len);
89 if (write(fd, &len_buf, sizeof(len_buf)) != sizeof(len_buf)) {
90 pb_log("write failed: %s\n", strerror(errno));
95 int rc = write(fd, str, len - pos);
97 pb_log("write failed: %s\n", strerror(errno));
107 int add_device(const struct device *dev)
111 pb_log("device added:\n");
113 rc = write_action(sock, DEV_ACTION_ADD_DEVICE) ||
114 write_string(sock, dev->id) ||
115 write_string(sock, dev->name) ||
116 write_string(sock, dev->description) ||
117 write_string(sock, dev->icon_file);
120 pb_log("error writing device %s to socket\n", dev->name);
125 int add_boot_option(const struct boot_option *opt)
129 pb_log("boot option added:\n");
130 print_boot_option(opt);
132 rc = write_action(sock, DEV_ACTION_ADD_OPTION) ||
133 write_string(sock, opt->id) ||
134 write_string(sock, opt->name) ||
135 write_string(sock, opt->description) ||
136 write_string(sock, opt->icon_file) ||
137 write_string(sock, opt->boot_image_file) ||
138 write_string(sock, opt->initrd_file) ||
139 write_string(sock, opt->boot_args);
142 pb_log("error writing boot option %s to socket\n", opt->name);
147 int remove_device(const char *dev_path)
149 return write_action(sock, DEV_ACTION_REMOVE_DEVICE) ||
150 write_string(sock, dev_path);
153 int connect_to_socket()
155 #ifndef USE_FAKE_SOCKET
157 struct sockaddr_un addr;
159 fd = socket(PF_UNIX, SOCK_STREAM, 0);
161 pb_log("can't create socket: %s\n", strerror(errno));
165 addr.sun_family = AF_UNIX;
166 strcpy(addr.sun_path, PBOOT_DEVICE_SOCKET);
168 if (connect(fd, (struct sockaddr *)&addr, sizeof(addr))) {
169 pb_log("can't connect to %s: %s\n",
170 addr.sun_path, strerror(errno));
178 fd = open("./debug_socket", O_WRONLY | O_CREAT, 0640);
180 pb_log("can't create output file: %s\n", strerror(errno));
188 static int mkdir_recursive(const char *dir)
194 pb_log("mkdir_recursive(%s)\n", dir);
199 if (!stat(dir, &statbuf)) {
200 if (!S_ISDIR(statbuf.st_mode)) {
201 pb_log("%s: %s exists, but isn't a directory\n",
209 sep = strchr(*str == '/' ? str + 1 : str, '/');
213 /* terminate the path at sep */
216 pb_log("mkdir(%s)\n", str);
218 if (mkdir(str, mode) && errno != EEXIST) {
219 pb_log("mkdir(%s): %s\n", str, strerror(errno));
226 /* reset dir to the full path */
228 sep = strchr(sep + 1, '/');
236 static void setup_device_links(const char *device)
243 .dir = "disk/by-uuid"
246 .env = "ID_FS_LABEL",
247 .dir = "disk/by-label"
254 for (link = links; link->env; link++) {
255 char *value, *dir, *path;
257 value = getenv(link->env);
261 value = encode_label(value);
262 dir = join_paths(TMP_DIR, link->dir);
263 path = join_paths(dir, value);
265 if (!mkdir_recursive(dir)) {
267 if (symlink(mountpoint_for_device(device), path)) {
268 pb_log("symlink(%s): %s\n",
269 path, strerror(errno));
279 int mount_device(const char *dev_path)
282 int pid, status, rc = -1;
285 dir = mountpoint_for_device(dev_path);
287 if (stat(dir, &statbuf)) {
288 if (mkdir(dir, 0755)) {
289 pb_log("couldn't create directory %s: %s\n",
290 dir, strerror(errno));
294 if (!S_ISDIR(statbuf.st_mode)) {
295 pb_log("mountpoint %s exists, "
296 "but isn't a directory\n", dir);
304 pb_log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
309 execl(MOUNT_BIN, MOUNT_BIN, dev_path, dir, "-o", "ro", NULL);
313 if (waitpid(pid, &status, 0) == -1) {
314 pb_log("%s: waitpid failed: %s\n", __FUNCTION__,
319 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
320 setup_device_links(dev_path);
328 static int unmount_device(const char *dev_path)
335 pb_log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
340 execl(UMOUNT_BIN, UMOUNT_BIN, dev_path, NULL);
344 if (waitpid(pid, &status, 0) == -1) {
345 pb_log("%s: waitpid failed: %s\n", __FUNCTION__,
350 rc = !WIFEXITED(status) || WEXITSTATUS(status) != 0;
355 static const struct device fake_boot_devices[] =
360 .icon_file = artwork_pathname("hdd.png"),
364 .name = "PinkCat Linux CD",
365 .icon_file = artwork_pathname("cdrom.png"),
369 static const struct boot_option fake_boot_options[] =
373 .name = "Bloobuntu Linux",
374 .description = "Boot Bloobuntu Linux",
375 .icon_file = artwork_pathname("hdd.png"),
379 .name = "Pendora Gore 6",
380 .description = "Boot Pendora Gora 6",
381 .icon_file = artwork_pathname("hdd.png"),
385 .name = "Genfoo Minux",
386 .description = "Boot Genfoo Minux",
387 .icon_file = artwork_pathname("hdd.png"),
391 .name = "PinkCat Linux",
392 .description = "Install PinkCat Linux - Graphical install",
393 .icon_file = artwork_pathname("cdrom.png"),
397 enum generic_icon_type guess_device_type(void)
399 const char *type = getenv("ID_TYPE");
400 const char *bus = getenv("ID_BUS");
401 if (type && streq(type, "cd"))
402 return ICON_TYPE_OPTICAL;
404 return ICON_TYPE_UNKNOWN;
405 if (streq(bus, "usb"))
406 return ICON_TYPE_USB;
407 if (streq(bus, "ata") || streq(bus, "scsi"))
408 return ICON_TYPE_DISK;
409 return ICON_TYPE_UNKNOWN;
413 static int is_removable_device(const char *sysfs_path)
415 char full_path[PATH_MAX];
419 sprintf(full_path, "/sys/%s/removable", sysfs_path);
420 fd = open(full_path, O_RDONLY);
421 pb_log(" -> removable check on %s, fd=%d\n", full_path, fd);
424 buf_len = read(fd, buf, 79);
429 return strtol(buf, NULL, 10);
432 static int is_ignored_device(const char *devname)
434 static const char *ignored_devices[] =
435 { "/dev/ram", "/dev/loop", NULL };
438 for (dev = ignored_devices; *dev; dev++)
439 if (!strncmp(devname, *dev, strlen(*dev)))
445 static int found_new_device(const char *dev_path)
447 const char *mountpoint = mountpoint_for_device(dev_path);
449 if (mount_device(dev_path)) {
450 pb_log("failed to mount %s\n", dev_path);
454 pb_log("mounted %s at %s\n", dev_path, mountpoint);
456 iterate_parsers(dev_path, mountpoint);
461 static void detach_and_sleep(int sec)
463 static int forked = 0;
470 pb_log("running in background...");
478 } else if (rc == -1) {
486 static int poll_device_plug(const char *dev_path,
491 /* Polling loop for optical drive */
492 for (; (*optical) != 0; ) {
493 fd = open(dev_path, O_RDONLY|O_NONBLOCK);
496 rc = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
502 if (rc == CDS_DISC_OK)
505 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
508 /* Fall back to bare open() */
511 fd = open(dev_path, O_RDONLY);
512 if (fd < 0 && errno != ENOMEDIUM)
517 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
521 static int poll_device_unplug(const char *dev_path, int optical)
526 fd = open(dev_path, O_RDONLY|O_NONBLOCK);
529 rc = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
531 if (rc != CDS_DISC_OK)
533 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
536 /* Fall back to bare open() */
538 fd = open(dev_path, O_RDONLY);
539 if (fd < 0 && errno != ENOMEDIUM)
544 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
548 static int poll_removable_device(const char *sysfs_path,
549 const char *dev_path)
551 int rc, mounted, optical = -1;
554 rc = poll_device_plug(dev_path, &optical);
555 if (rc == EXIT_FAILURE)
557 rc = found_new_device(dev_path);
558 mounted = (rc == EXIT_SUCCESS);
560 poll_device_unplug(dev_path, optical);
562 remove_device(dev_path);
564 /* Unmount it repeatedly, if needs be */
565 while (mounted && !unmount_device(dev_path))
571 int main(int argc, char **argv)
573 char *dev_path, *action;
576 action = getenv("ACTION");
578 logf = fopen("/var/log/petitboot-udev-helpers.log", "a");
581 pb_log("%d started\n", getpid());
585 pb_log("missing environment?\n");
589 set_mount_base(TMP_DIR);
591 if (connect_to_socket())
594 if (streq(action, "fake")) {
597 add_device(&fake_boot_devices[0]);
598 add_boot_option(&fake_boot_options[0]);
599 add_boot_option(&fake_boot_options[1]);
600 add_boot_option(&fake_boot_options[2]);
601 add_device(&fake_boot_devices[1]);
602 add_boot_option(&fake_boot_options[3]);
607 dev_path = getenv("DEVNAME");
609 pb_log("missing environment?\n");
613 if (is_ignored_device(dev_path))
616 if (streq(action, "add")) {
617 char *sysfs_path = getenv("DEVPATH");
618 if (sysfs_path && is_removable_device(sysfs_path))
619 rc = poll_removable_device(sysfs_path, dev_path);
621 rc = found_new_device(dev_path);
622 } else if (streq(action, "remove")) {
623 pb_log("%s removed\n", dev_path);
625 remove_device(dev_path);
627 /* Unmount it repeatedly, if needs be */
628 while (!unmount_device(dev_path))
632 pb_log("invalid action '%s'\n", action);