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 int mount_device(const char *dev_path)
190 int pid, status, rc = -1;
193 dir = mountpoint_for_device(dev_path);
195 if (stat(dir, &statbuf)) {
196 if (mkdir(dir, 0755)) {
197 pb_log("couldn't create directory %s: %s\n",
198 dir, strerror(errno));
202 if (!S_ISDIR(statbuf.st_mode)) {
203 pb_log("mountpoint %s exists, "
204 "but isn't a directory\n", dir);
212 pb_log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
217 execl(MOUNT_BIN, MOUNT_BIN, dev_path, dir, "-o", "ro", NULL);
221 if (waitpid(pid, &status, 0) == -1) {
222 pb_log("%s: waitpid failed: %s\n", __FUNCTION__,
227 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
234 static int unmount_device(const char *dev_path)
241 pb_log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
246 execl(UMOUNT_BIN, UMOUNT_BIN, dev_path, NULL);
250 if (waitpid(pid, &status, 0) == -1) {
251 pb_log("%s: waitpid failed: %s\n", __FUNCTION__,
256 rc = !WIFEXITED(status) || WEXITSTATUS(status) != 0;
261 static const struct device fake_boot_devices[] =
266 .icon_file = artwork_pathname("hdd.png"),
270 .name = "PinkCat Linux CD",
271 .icon_file = artwork_pathname("cdrom.png"),
275 static const struct boot_option fake_boot_options[] =
279 .name = "Bloobuntu Linux",
280 .description = "Boot Bloobuntu Linux",
281 .icon_file = artwork_pathname("hdd.png"),
285 .name = "Pendora Gore 6",
286 .description = "Boot Pendora Gora 6",
287 .icon_file = artwork_pathname("hdd.png"),
291 .name = "Genfoo Minux",
292 .description = "Boot Genfoo Minux",
293 .icon_file = artwork_pathname("hdd.png"),
297 .name = "PinkCat Linux",
298 .description = "Install PinkCat Linux - Graphical install",
299 .icon_file = artwork_pathname("cdrom.png"),
303 enum generic_icon_type guess_device_type(void)
305 const char *type = getenv("ID_TYPE");
306 const char *bus = getenv("ID_BUS");
307 if (type && streq(type, "cd"))
308 return ICON_TYPE_OPTICAL;
310 return ICON_TYPE_UNKNOWN;
311 if (streq(bus, "usb"))
312 return ICON_TYPE_USB;
313 if (streq(bus, "ata") || streq(bus, "scsi"))
314 return ICON_TYPE_DISK;
315 return ICON_TYPE_UNKNOWN;
319 static int is_removable_device(const char *sysfs_path)
321 char full_path[PATH_MAX];
325 sprintf(full_path, "/sys/%s/removable", sysfs_path);
326 fd = open(full_path, O_RDONLY);
327 pb_log(" -> removable check on %s, fd=%d\n", full_path, fd);
330 buf_len = read(fd, buf, 79);
335 return strtol(buf, NULL, 10);
338 static int is_ignored_device(const char *devname)
340 static const char *ignored_devices[] = { "/dev/ram", NULL };
343 for (dev = ignored_devices; *dev; dev++)
344 if (!strncmp(devname, *dev, strlen(*dev)))
350 static int found_new_device(const char *dev_path)
352 const char *mountpoint = mountpoint_for_device(dev_path);
354 if (mount_device(dev_path)) {
355 pb_log("failed to mount %s\n", dev_path);
359 pb_log("mounted %s at %s\n", dev_path, mountpoint);
361 iterate_parsers(dev_path, mountpoint);
366 static void detach_and_sleep(int sec)
368 static int forked = 0;
375 pb_log("running in background...");
383 } else if (rc == -1) {
391 static int poll_device_plug(const char *dev_path,
396 /* Polling loop for optical drive */
397 for (; (*optical) != 0; ) {
398 pb_log("poll for optical drive insertion ...\n");
399 fd = open(dev_path, O_RDONLY|O_NONBLOCK);
402 rc = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
405 pb_log("not an optical drive, fallback...\n");
409 if (rc == CDS_DISC_OK)
412 pb_log("no... waiting\n");
413 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
416 /* Fall back to bare open() */
419 pb_log("poll for non-optical drive insertion ...\n");
420 fd = open(dev_path, O_RDONLY);
421 if (fd < 0 && errno != ENOMEDIUM)
426 pb_log("no... waiting\n");
427 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
431 static int poll_device_unplug(const char *dev_path, int optical)
436 pb_log("poll for optical drive removal ...\n");
437 fd = open(dev_path, O_RDONLY|O_NONBLOCK);
440 rc = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
442 if (rc != CDS_DISC_OK)
444 pb_log("no... waiting\n");
445 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
448 /* Fall back to bare open() */
450 pb_log("poll for non-optical drive removal ...\n");
451 fd = open(dev_path, O_RDONLY);
452 if (fd < 0 && errno != ENOMEDIUM)
457 pb_log("no... waiting\n");
458 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
462 static int poll_removable_device(const char *sysfs_path,
463 const char *dev_path)
465 int rc, mounted, optical = -1;
468 rc = poll_device_plug(dev_path, &optical);
469 if (rc == EXIT_FAILURE)
471 rc = found_new_device(dev_path);
472 mounted = (rc == EXIT_SUCCESS);
474 poll_device_unplug(dev_path, optical);
476 remove_device(dev_path);
478 /* Unmount it repeatedly, if needs be */
479 while (mounted && !unmount_device(dev_path))
485 int main(int argc, char **argv)
487 char *dev_path, *action;
490 action = getenv("ACTION");
492 logf = fopen("/var/log/petitboot-udev-helpers.log", "a");
495 pb_log("%d started\n", getpid());
499 pb_log("missing environment?\n");
503 set_mount_base(TMP_DIR);
505 if (connect_to_socket())
508 if (streq(action, "fake")) {
511 add_device(&fake_boot_devices[0]);
512 add_boot_option(&fake_boot_options[0]);
513 add_boot_option(&fake_boot_options[1]);
514 add_boot_option(&fake_boot_options[2]);
515 add_device(&fake_boot_devices[1]);
516 add_boot_option(&fake_boot_options[3]);
521 dev_path = getenv("DEVNAME");
523 pb_log("missing environment?\n");
527 if (is_ignored_device(dev_path))
530 if (streq(action, "add")) {
531 char *sysfs_path = getenv("DEVPATH");
532 if (sysfs_path && is_removable_device(sysfs_path))
533 rc = poll_removable_device(sysfs_path, dev_path);
535 rc = found_new_device(dev_path);
536 } else if (streq(action, "remove")) {
537 pb_log("%s removed\n", dev_path);
539 remove_device(dev_path);
541 /* Unmount it repeatedly, if needs be */
542 while (!unmount_device(dev_path))
546 pb_log("invalid action '%s'\n", action);