11 #include <sys/socket.h>
17 #include <asm/byteorder.h>
18 #include <linux/cdrom.h>
19 #include <sys/ioctl.h>
23 #include "petitboot-paths.h"
25 /* Define below to operate without the frontend */
26 #undef USE_FAKE_SOCKET
28 /* Delay in seconds between polling of removable devices */
29 #define REMOVABLE_SLEEP_DELAY 2
34 void pb_log(const char *fmt, ...)
39 vfprintf(logf, fmt, ap);
43 static void print_boot_option(const struct boot_option *opt)
45 pb_log("\tname: %s\n", opt->name);
46 pb_log("\tdescription: %s\n", opt->description);
47 pb_log("\tboot_image: %s\n", opt->boot_image_file);
48 pb_log("\tinitrd: %s\n", opt->initrd_file);
49 pb_log("\tboot_args: %s\n", opt->boot_args);
53 static void print_device(const struct device *dev)
55 pb_log("\tid: %s\n", dev->id);
56 pb_log("\tname: %s\n", dev->name);
57 pb_log("\tdescription: %s\n", dev->description);
58 pb_log("\tboot_image: %s\n", dev->icon_file);
61 static int write_action(int fd, enum device_action action)
63 uint8_t action_buf = action;
64 return write(fd, &action_buf, sizeof(action_buf)) != sizeof(action_buf);
67 static int write_string(int fd, const char *str)
74 if (write(fd, &len_buf, sizeof(len_buf)) != sizeof(len_buf)) {
75 pb_log("write failed: %s\n", strerror(errno));
82 if (len > (1ull << (sizeof(len_buf) * 8 - 1))) {
83 pb_log("string too large\n");
87 len_buf = __cpu_to_be32(len);
88 if (write(fd, &len_buf, sizeof(len_buf)) != sizeof(len_buf)) {
89 pb_log("write failed: %s\n", strerror(errno));
94 int rc = write(fd, str, len - pos);
96 pb_log("write failed: %s\n", strerror(errno));
106 int add_device(const struct device *dev)
110 pb_log("device added:\n");
112 rc = write_action(sock, DEV_ACTION_ADD_DEVICE) ||
113 write_string(sock, dev->id) ||
114 write_string(sock, dev->name) ||
115 write_string(sock, dev->description) ||
116 write_string(sock, dev->icon_file);
119 pb_log("error writing device %s to socket\n", dev->name);
124 int add_boot_option(const struct boot_option *opt)
128 pb_log("boot option added:\n");
129 print_boot_option(opt);
131 rc = write_action(sock, DEV_ACTION_ADD_OPTION) ||
132 write_string(sock, opt->id) ||
133 write_string(sock, opt->name) ||
134 write_string(sock, opt->description) ||
135 write_string(sock, opt->icon_file) ||
136 write_string(sock, opt->boot_image_file) ||
137 write_string(sock, opt->initrd_file) ||
138 write_string(sock, opt->boot_args);
141 pb_log("error writing boot option %s to socket\n", opt->name);
146 int remove_device(const char *dev_path)
148 return write_action(sock, DEV_ACTION_REMOVE_DEVICE) ||
149 write_string(sock, dev_path);
152 int connect_to_socket()
154 #ifndef USE_FAKE_SOCKET
156 struct sockaddr_un addr;
158 fd = socket(PF_UNIX, SOCK_STREAM, 0);
160 pb_log("can't create socket: %s\n", strerror(errno));
164 addr.sun_family = AF_UNIX;
165 strcpy(addr.sun_path, PBOOT_DEVICE_SOCKET);
167 if (connect(fd, (struct sockaddr *)&addr, sizeof(addr))) {
168 pb_log("can't connect to %s: %s\n",
169 addr.sun_path, strerror(errno));
177 fd = open("./debug_socket", O_WRONLY | O_CREAT, 0640);
179 pb_log("can't create output file: %s\n", strerror(errno));
187 static int mkdir_recursive(const char *dir)
193 pb_log("mkdir_recursive(%s)\n", dir);
198 if (!stat(dir, &statbuf)) {
199 if (!S_ISDIR(statbuf.st_mode)) {
200 pb_log("%s: %s exists, but isn't a directory\n",
208 sep = strchr(*str == '/' ? str + 1 : str, '/');
212 /* terminate the path at sep */
215 pb_log("mkdir(%s)\n", str);
217 if (mkdir(str, mode) && errno != EEXIST) {
218 pb_log("mkdir(%s): %s\n", str, strerror(errno));
225 /* reset dir to the full path */
227 sep = strchr(sep + 1, '/');
235 static void setup_device_links(const char *device)
242 .dir = "disk/by-uuid"
245 .env = "ID_FS_LABEL",
246 .dir = "disk/by-label"
253 for (link = links; link->env; link++) {
254 char *value, *dir, *path;
256 value = getenv(link->env);
260 value = encode_label(value);
261 dir = join_paths(TMP_DIR, link->dir);
262 path = join_paths(dir, value);
264 if (!mkdir_recursive(dir)) {
266 if (symlink(mountpoint_for_device(device), path)) {
267 pb_log("symlink(%s): %s\n",
268 path, strerror(errno));
278 int mount_device(const char *dev_path)
281 int pid, status, rc = -1;
284 dir = mountpoint_for_device(dev_path);
286 if (stat(dir, &statbuf)) {
287 if (mkdir(dir, 0755)) {
288 pb_log("couldn't create directory %s: %s\n",
289 dir, strerror(errno));
293 if (!S_ISDIR(statbuf.st_mode)) {
294 pb_log("mountpoint %s exists, "
295 "but isn't a directory\n", dir);
303 pb_log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
308 execl(MOUNT_BIN, MOUNT_BIN, dev_path, dir, "-o", "ro", NULL);
312 if (waitpid(pid, &status, 0) == -1) {
313 pb_log("%s: waitpid failed: %s\n", __FUNCTION__,
318 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
319 setup_device_links(dev_path);
327 static int unmount_device(const char *dev_path)
334 pb_log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
339 execl(UMOUNT_BIN, UMOUNT_BIN, dev_path, NULL);
343 if (waitpid(pid, &status, 0) == -1) {
344 pb_log("%s: waitpid failed: %s\n", __FUNCTION__,
349 rc = !WIFEXITED(status) || WEXITSTATUS(status) != 0;
354 static const struct device fake_boot_devices[] =
359 .icon_file = artwork_pathname("hdd.png"),
363 .name = "PinkCat Linux CD",
364 .icon_file = artwork_pathname("cdrom.png"),
368 static const struct boot_option fake_boot_options[] =
372 .name = "Bloobuntu Linux",
373 .description = "Boot Bloobuntu Linux",
374 .icon_file = artwork_pathname("hdd.png"),
378 .name = "Pendora Gore 6",
379 .description = "Boot Pendora Gora 6",
380 .icon_file = artwork_pathname("hdd.png"),
384 .name = "Genfoo Minux",
385 .description = "Boot Genfoo Minux",
386 .icon_file = artwork_pathname("hdd.png"),
390 .name = "PinkCat Linux",
391 .description = "Install PinkCat Linux - Graphical install",
392 .icon_file = artwork_pathname("cdrom.png"),
396 enum generic_icon_type guess_device_type(void)
398 const char *type = getenv("ID_TYPE");
399 const char *bus = getenv("ID_BUS");
400 if (type && streq(type, "cd"))
401 return ICON_TYPE_OPTICAL;
403 return ICON_TYPE_UNKNOWN;
404 if (streq(bus, "usb"))
405 return ICON_TYPE_USB;
406 if (streq(bus, "ata") || streq(bus, "scsi"))
407 return ICON_TYPE_DISK;
408 return ICON_TYPE_UNKNOWN;
412 static int is_removable_device(const char *sysfs_path)
414 char full_path[PATH_MAX];
418 sprintf(full_path, "/sys/%s/removable", sysfs_path);
419 fd = open(full_path, O_RDONLY);
420 pb_log(" -> removable check on %s, fd=%d\n", full_path, fd);
423 buf_len = read(fd, buf, 79);
428 return strtol(buf, NULL, 10);
431 static int is_ignored_device(const char *devname)
433 static const char *ignored_devices[] =
434 { "/dev/ram", "/dev/loop", NULL };
437 for (dev = ignored_devices; *dev; dev++)
438 if (!strncmp(devname, *dev, strlen(*dev)))
444 static int found_new_device(const char *dev_path)
446 const char *mountpoint = mountpoint_for_device(dev_path);
448 if (mount_device(dev_path)) {
449 pb_log("failed to mount %s\n", dev_path);
453 pb_log("mounted %s at %s\n", dev_path, mountpoint);
455 iterate_parsers(dev_path, mountpoint);
460 static void detach_and_sleep(int sec)
462 static int forked = 0;
469 pb_log("running in background...");
477 } else if (rc == -1) {
485 static int poll_device_plug(const char *dev_path,
490 /* Polling loop for optical drive */
491 for (; (*optical) != 0; ) {
492 fd = open(dev_path, O_RDONLY|O_NONBLOCK);
495 rc = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
501 if (rc == CDS_DISC_OK)
504 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
507 /* Fall back to bare open() */
510 fd = open(dev_path, O_RDONLY);
511 if (fd < 0 && errno != ENOMEDIUM)
516 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
520 static int poll_device_unplug(const char *dev_path, int optical)
525 fd = open(dev_path, O_RDONLY|O_NONBLOCK);
528 rc = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
530 if (rc != CDS_DISC_OK)
532 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
535 /* Fall back to bare open() */
537 fd = open(dev_path, O_RDONLY);
538 if (fd < 0 && errno != ENOMEDIUM)
543 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
547 static int poll_removable_device(const char *sysfs_path,
548 const char *dev_path)
550 int rc, mounted, optical = -1;
553 rc = poll_device_plug(dev_path, &optical);
554 if (rc == EXIT_FAILURE)
556 rc = found_new_device(dev_path);
557 mounted = (rc == EXIT_SUCCESS);
559 poll_device_unplug(dev_path, optical);
561 remove_device(dev_path);
563 /* Unmount it repeatedly, if needs be */
564 while (mounted && !unmount_device(dev_path))
570 int main(int argc, char **argv)
572 char *dev_path, *action;
575 action = getenv("ACTION");
577 logf = fopen("/var/log/petitboot-udev-helpers.log", "a");
580 pb_log("%d started\n", getpid());
584 pb_log("missing environment?\n");
588 set_mount_base(TMP_DIR);
590 if (connect_to_socket())
593 if (streq(action, "fake")) {
596 add_device(&fake_boot_devices[0]);
597 add_boot_option(&fake_boot_options[0]);
598 add_boot_option(&fake_boot_options[1]);
599 add_boot_option(&fake_boot_options[2]);
600 add_device(&fake_boot_devices[1]);
601 add_boot_option(&fake_boot_options[3]);
606 dev_path = getenv("DEVNAME");
608 pb_log("missing environment?\n");
612 if (is_ignored_device(dev_path))
615 if (streq(action, "add")) {
616 char *sysfs_path = getenv("DEVPATH");
617 if (sysfs_path && is_removable_device(sysfs_path))
618 rc = poll_removable_device(sysfs_path, dev_path);
620 rc = found_new_device(dev_path);
621 } else if (streq(action, "remove")) {
622 pb_log("%s removed\n", dev_path);
624 remove_device(dev_path);
626 /* Unmount it repeatedly, if needs be */
627 while (!unmount_device(dev_path))
631 pb_log("invalid action '%s'\n", action);