11 #include <sys/socket.h>
17 #include <asm/byteorder.h>
18 #include <linux/cdrom.h>
19 #include <sys/ioctl.h>
22 #include "petitboot-paths.h"
24 /* Define below to operate without the frontend */
25 #undef USE_FAKE_SOCKET
27 /* Delay in seconds between polling of removable devices */
28 #define REMOVABLE_SLEEP_DELAY 2
33 void pb_log(const char *fmt, ...)
38 vfprintf(logf, fmt, ap);
42 static void print_boot_option(const struct boot_option *opt)
44 pb_log("\tname: %s\n", opt->name);
45 pb_log("\tdescription: %s\n", opt->description);
46 pb_log("\tboot_image: %s\n", opt->boot_image_file);
47 pb_log("\tinitrd: %s\n", opt->initrd_file);
48 pb_log("\tboot_args: %s\n", opt->boot_args);
52 static void print_device(const struct device *dev)
54 pb_log("\tid: %s\n", dev->id);
55 pb_log("\tname: %s\n", dev->name);
56 pb_log("\tdescription: %s\n", dev->description);
57 pb_log("\tboot_image: %s\n", dev->icon_file);
60 static int write_action(int fd, enum device_action action)
62 uint8_t action_buf = action;
63 return write(fd, &action_buf, sizeof(action_buf)) != sizeof(action_buf);
66 static int write_string(int fd, const char *str)
73 if (write(fd, &len_buf, sizeof(len_buf)) != sizeof(len_buf)) {
74 pb_log("write failed: %s\n", strerror(errno));
81 if (len > (1ull << (sizeof(len_buf) * 8 - 1))) {
82 pb_log("string too large\n");
86 len_buf = __cpu_to_be32(len);
87 if (write(fd, &len_buf, sizeof(len_buf)) != sizeof(len_buf)) {
88 pb_log("write failed: %s\n", strerror(errno));
93 int rc = write(fd, str, len - pos);
95 pb_log("write failed: %s\n", strerror(errno));
105 int add_device(const struct device *dev)
109 pb_log("device added:\n");
111 rc = write_action(sock, DEV_ACTION_ADD_DEVICE) ||
112 write_string(sock, dev->id) ||
113 write_string(sock, dev->name) ||
114 write_string(sock, dev->description) ||
115 write_string(sock, dev->icon_file);
118 pb_log("error writing device %s to socket\n", dev->name);
123 int add_boot_option(const struct boot_option *opt)
127 pb_log("boot option added:\n");
128 print_boot_option(opt);
130 rc = write_action(sock, DEV_ACTION_ADD_OPTION) ||
131 write_string(sock, opt->id) ||
132 write_string(sock, opt->name) ||
133 write_string(sock, opt->description) ||
134 write_string(sock, opt->icon_file) ||
135 write_string(sock, opt->boot_image_file) ||
136 write_string(sock, opt->initrd_file) ||
137 write_string(sock, opt->boot_args);
140 pb_log("error writing boot option %s to socket\n", opt->name);
145 int remove_device(const char *dev_path)
147 return write_action(sock, DEV_ACTION_REMOVE_DEVICE) ||
148 write_string(sock, dev_path);
151 int connect_to_socket()
153 #ifndef USE_FAKE_SOCKET
155 struct sockaddr_un addr;
157 fd = socket(PF_UNIX, SOCK_STREAM, 0);
159 pb_log("can't create socket: %s\n", strerror(errno));
163 addr.sun_family = AF_UNIX;
164 strcpy(addr.sun_path, PBOOT_DEVICE_SOCKET);
166 if (connect(fd, (struct sockaddr *)&addr, sizeof(addr))) {
167 pb_log("can't connect to %s: %s\n",
168 addr.sun_path, strerror(errno));
176 fd = open("./debug_socket", O_WRONLY | O_CREAT, 0640);
178 pb_log("can't create output file: %s\n", strerror(errno));
190 #define DEVICE_MAP_SIZE 32
191 static struct device_map device_map[DEVICE_MAP_SIZE];
193 const char *mountpoint_for_device(const char *dev_path)
196 const char *basename;
198 /* shorten '/dev/foo' to 'foo' */
199 basename = strrchr(dev_path, '/');
205 /* check existing entries in the map */
206 for (i = 0; (i < DEVICE_MAP_SIZE) && device_map[i].dev; i++)
207 if (!strcmp(device_map[i].dev, basename))
208 return device_map[i].mnt;
210 if (i == DEVICE_MAP_SIZE)
213 device_map[i].dev = strdup(dev_path);
214 asprintf(&device_map[i].mnt, "%s/%s", TMP_DIR, basename);
215 return device_map[i].mnt;
219 * Resolve a path given in a config file, to a path in the local filesystem.
220 * Paths may be of the form:
221 * device:path (eg /dev/sda:/boot/vmlinux)
225 * - in this case, the default mountpoint is used.
227 * Returns a newly-allocated string containing a full path to the file in path
229 char *resolve_path(const char *path, const char *default_mountpoint)
232 const char *devpath, *sep;
234 sep = strchr(path, ':');
236 devpath = default_mountpoint;
237 asprintf(&ret, "%s/%s", devpath, path);
239 /* copy just the device name into tmp */
240 char *dev = strndup(path, sep - path);
241 devpath = mountpoint_for_device(dev);
242 asprintf(&ret, "%s/%s", devpath, sep + 1);
249 int mount_device(const char *dev_path)
252 int pid, status, rc = -1;
255 dir = mountpoint_for_device(dev_path);
257 if (stat(dir, &statbuf)) {
258 if (mkdir(dir, 0755)) {
259 pb_log("couldn't create directory %s: %s\n",
260 dir, strerror(errno));
264 if (!S_ISDIR(statbuf.st_mode)) {
265 pb_log("mountpoint %s exists, "
266 "but isn't a directory\n", dir);
274 pb_log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
279 execl(MOUNT_BIN, MOUNT_BIN, dev_path, dir, "-o", "ro", NULL);
283 if (waitpid(pid, &status, 0) == -1) {
284 pb_log("%s: waitpid failed: %s\n", __FUNCTION__,
289 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
296 static int unmount_device(const char *dev_path)
303 pb_log("%s: fork failed: %s\n", __FUNCTION__, strerror(errno));
308 execl(UMOUNT_BIN, UMOUNT_BIN, dev_path, NULL);
312 if (waitpid(pid, &status, 0) == -1) {
313 pb_log("%s: waitpid failed: %s\n", __FUNCTION__,
318 rc = !WIFEXITED(status) || WEXITSTATUS(status) != 0;
323 static const struct device fake_boot_devices[] =
328 .icon_file = artwork_pathname("hdd.png"),
332 .name = "PinkCat Linux CD",
333 .icon_file = artwork_pathname("cdrom.png"),
337 static const struct boot_option fake_boot_options[] =
341 .name = "Bloobuntu Linux",
342 .description = "Boot Bloobuntu Linux",
343 .icon_file = artwork_pathname("hdd.png"),
347 .name = "Pendora Gore 6",
348 .description = "Boot Pendora Gora 6",
349 .icon_file = artwork_pathname("hdd.png"),
353 .name = "Genfoo Minux",
354 .description = "Boot Genfoo Minux",
355 .icon_file = artwork_pathname("hdd.png"),
359 .name = "PinkCat Linux",
360 .description = "Install PinkCat Linux - Graphical install",
361 .icon_file = artwork_pathname("cdrom.png"),
365 enum generic_icon_type guess_device_type(void)
367 const char *type = getenv("ID_TYPE");
368 const char *bus = getenv("ID_BUS");
369 if (type && streq(type, "cd"))
370 return ICON_TYPE_OPTICAL;
372 return ICON_TYPE_UNKNOWN;
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 pb_log(" -> 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 is_ignored_device(const char *devname)
402 static const char *ignored_devices[] = { "/dev/ram", NULL };
405 for (dev = ignored_devices; *dev; dev++)
406 if (!strncmp(devname, *dev, strlen(*dev)))
412 static int found_new_device(const char *dev_path)
414 const char *mountpoint = mountpoint_for_device(dev_path);
416 if (mount_device(dev_path)) {
417 pb_log("failed to mount %s\n", dev_path);
421 pb_log("mounted %s at %s\n", dev_path, mountpoint);
423 iterate_parsers(dev_path, mountpoint);
428 static void detach_and_sleep(int sec)
430 static int forked = 0;
437 pb_log("running in background...");
445 } else if (rc == -1) {
453 static int poll_device_plug(const char *dev_path,
458 /* Polling loop for optical drive */
459 for (; (*optical) != 0; ) {
460 pb_log("poll for optical drive insertion ...\n");
461 fd = open(dev_path, O_RDONLY|O_NONBLOCK);
464 rc = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
467 pb_log("not an optical drive, fallback...\n");
471 if (rc == CDS_DISC_OK)
474 pb_log("no... waiting\n");
475 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
478 /* Fall back to bare open() */
481 pb_log("poll for non-optical drive insertion ...\n");
482 fd = open(dev_path, O_RDONLY);
483 if (fd < 0 && errno != ENOMEDIUM)
488 pb_log("no... waiting\n");
489 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
493 static int poll_device_unplug(const char *dev_path, int optical)
498 pb_log("poll for optical drive removal ...\n");
499 fd = open(dev_path, O_RDONLY|O_NONBLOCK);
502 rc = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
504 if (rc != CDS_DISC_OK)
506 pb_log("no... waiting\n");
507 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
510 /* Fall back to bare open() */
512 pb_log("poll for non-optical drive removal ...\n");
513 fd = open(dev_path, O_RDONLY);
514 if (fd < 0 && errno != ENOMEDIUM)
519 pb_log("no... waiting\n");
520 detach_and_sleep(REMOVABLE_SLEEP_DELAY);
524 static int poll_removable_device(const char *sysfs_path,
525 const char *dev_path)
527 int rc, mounted, optical = -1;
530 rc = poll_device_plug(dev_path, &optical);
531 if (rc == EXIT_FAILURE)
533 rc = found_new_device(dev_path);
534 mounted = (rc == EXIT_SUCCESS);
536 poll_device_unplug(dev_path, optical);
538 remove_device(dev_path);
540 /* Unmount it repeatedly, if needs be */
541 while (mounted && !unmount_device(dev_path))
547 int main(int argc, char **argv)
549 char *dev_path, *action;
552 action = getenv("ACTION");
554 logf = fopen("/var/log/petitboot-udev-helpers.log", "a");
557 pb_log("%d started\n", getpid());
561 pb_log("missing environment?\n");
565 if (connect_to_socket())
568 if (streq(action, "fake")) {
571 add_device(&fake_boot_devices[0]);
572 add_boot_option(&fake_boot_options[0]);
573 add_boot_option(&fake_boot_options[1]);
574 add_boot_option(&fake_boot_options[2]);
575 add_device(&fake_boot_devices[1]);
576 add_boot_option(&fake_boot_options[3]);
581 dev_path = getenv("DEVNAME");
583 pb_log("missing environment?\n");
587 if (is_ignored_device(dev_path))
590 if (streq(action, "add")) {
591 char *sysfs_path = getenv("DEVPATH");
592 if (sysfs_path && is_removable_device(sysfs_path))
593 rc = poll_removable_device(sysfs_path, dev_path);
595 rc = found_new_device(dev_path);
596 } else if (streq(action, "remove")) {
597 pb_log("%s removed\n", dev_path);
599 remove_device(dev_path);
601 /* Unmount it repeatedly, if needs be */
602 while (!unmount_device(dev_path))
606 pb_log("invalid action '%s'\n", action);