10 #include <pb-config/pb-config.h>
11 #include <talloc/talloc.h>
12 #include <list/list.h>
14 #include <types/types.h>
15 #include <system/system.h>
18 #include "device-handler.h"
19 #include "discover-server.h"
27 struct device_handler {
28 struct discover_server *server;
31 struct discover_device **devices;
32 unsigned int n_devices;
34 struct waitset *waitset;
35 struct waiter *timeout_waiter;
37 unsigned int sec_to_boot;
39 struct discover_boot_option *default_boot_option;
40 struct list unresolved_boot_options;
43 void discover_context_add_boot_option(struct discover_context *ctx,
44 struct discover_boot_option *boot_option)
46 boot_option->source = ctx->parser;
47 list_add_tail(&ctx->boot_options, &boot_option->list);
48 talloc_steal(ctx, boot_option);
52 * device_handler_get_device_count - Get the count of current handler devices.
55 int device_handler_get_device_count(const struct device_handler *handler)
57 return handler->n_devices;
61 * device_handler_get_device - Get a handler device by index.
64 const struct discover_device *device_handler_get_device(
65 const struct device_handler *handler, unsigned int index)
67 if (index >= handler->n_devices) {
68 assert(0 && "bad index");
72 return handler->devices[index];
75 struct discover_boot_option *discover_boot_option_create(
76 struct discover_context *ctx,
77 struct discover_device *device)
79 struct discover_boot_option *opt;
81 opt = talloc_zero(ctx, struct discover_boot_option);
82 opt->option = talloc_zero(opt, struct boot_option);
88 static int device_match_path(struct discover_device *dev, const char *path)
90 return dev->device_path && !strcmp(dev->device_path, path);
93 static int device_match_uuid(struct discover_device *dev, const char *uuid)
95 return dev->uuid && !strcmp(dev->uuid, uuid);
98 static int device_match_label(struct discover_device *dev, const char *label)
100 return dev->label && !strcmp(dev->label, label);
103 static int device_match_id(struct discover_device *dev, const char *id)
105 return !strcmp(dev->device->id, id);
108 static struct discover_device *device_lookup(
109 struct device_handler *device_handler,
110 int (match_fn)(struct discover_device *, const char *),
113 struct discover_device *dev;
119 for (i = 0; i < device_handler->n_devices; i++) {
120 dev = device_handler->devices[i];
122 if (match_fn(dev, str))
129 struct discover_device *device_lookup_by_name(struct device_handler *handler,
132 struct discover_device *dev;
135 if (strncmp(name, "/dev/", strlen("/dev/")))
136 path = talloc_asprintf(NULL, "/dev/%s", name);
138 path = talloc_strdup(NULL, name);
140 dev = device_lookup_by_path(handler, path);
147 struct discover_device *device_lookup_by_path(
148 struct device_handler *device_handler,
151 return device_lookup(device_handler, device_match_path, path);
154 struct discover_device *device_lookup_by_uuid(
155 struct device_handler *device_handler,
158 return device_lookup(device_handler, device_match_uuid, uuid);
161 struct discover_device *device_lookup_by_label(
162 struct device_handler *device_handler,
165 return device_lookup(device_handler, device_match_label, label);
168 struct discover_device *device_lookup_by_id(
169 struct device_handler *device_handler,
172 return device_lookup(device_handler, device_match_id, id);
175 void device_handler_destroy(struct device_handler *handler)
177 talloc_free(handler);
180 #ifdef PETITBOOT_TEST
182 /* we have a simplified interface for petitboot testing, but still want
183 * to keep struct device_handler opaque. */
184 struct device_handler *device_handler_init(
185 struct discover_server *server __attribute__((unused)),
186 struct waitset *waitset __attribute__((unused)),
187 int dry_run __attribute__((unused)))
189 struct device_handler *handler;
191 handler = talloc_zero(NULL, struct device_handler);
192 list_init(&handler->unresolved_boot_options);
197 void device_handler_add_device(struct device_handler *handler,
198 struct discover_device *dev)
200 handler->n_devices++;
201 handler->devices = talloc_realloc(handler, handler->devices,
202 struct discover_device *, handler->n_devices);
203 handler->devices[handler->n_devices - 1] = dev;
208 static int mount_device(struct discover_device *dev)
212 if (!dev->device_path)
215 if (!dev->mount_path)
216 dev->mount_path = join_paths(dev, mount_base(),
219 if (pb_mkdir_recursive(dev->mount_path))
220 pb_log("couldn't create mount directory %s: %s\n",
221 dev->mount_path, strerror(errno));
223 argv[0] = pb_system_apps.mount;
224 argv[1] = dev->device_path;
225 argv[2] = dev->mount_path;
230 if (pb_run_cmd(argv, 1, 0)) {
232 /* Retry mount without ro option. */
234 argv[0] = pb_system_apps.mount;
235 argv[1] = dev->device_path;
236 argv[2] = dev->mount_path;
239 if (pb_run_cmd(argv, 1, 0))
246 pb_rmdir_recursive(mount_base(), dev->mount_path);
250 static int umount_device(struct discover_device *dev)
255 if (!dev->mount_path)
260 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
265 execl(pb_system_apps.umount, pb_system_apps.umount,
266 dev->mount_path, NULL);
270 if (waitpid(pid, &status, 0) == -1) {
271 pb_log("%s: waitpid failed: %s\n", __func__,
276 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
279 pb_rmdir_recursive(mount_base(), dev->mount_path);
284 struct device_handler *device_handler_init(struct discover_server *server,
285 struct waitset *waitset, int dry_run)
287 struct device_handler *handler;
289 handler = talloc(NULL, struct device_handler);
290 handler->devices = NULL;
291 handler->n_devices = 0;
292 handler->server = server;
293 handler->waitset = waitset;
294 handler->dry_run = dry_run;
295 handler->default_boot_option = NULL;
296 handler->default_enabled = config_get()->autoboot_enabled;
297 list_init(&handler->unresolved_boot_options);
299 /* set up our mount point base */
300 pb_mkdir_recursive(mount_base());
307 static int destroy_device(void *arg)
309 struct discover_device *dev = arg;
316 static struct discover_device *find_device(struct device_handler *handler,
319 struct discover_device *dev;
322 for (i = 0; i < handler->n_devices; i++) {
323 dev = handler->devices[i];
324 if (!strcmp(dev->device->id, id))
331 static struct discover_device *discover_device_create(
332 struct device_handler *handler,
333 struct discover_context *ctx,
336 struct discover_device *dev;
339 dev = find_device(handler, event->device);
343 dev = talloc_zero(ctx, struct discover_device);
344 dev->device = talloc_zero(dev, struct device);
345 list_init(&dev->boot_options);
347 devname = event_get_param(ctx->event, "DEVNAME");
349 dev->device_path = talloc_strdup(dev, devname);
351 dev->device->id = talloc_strdup(dev, event->device);
353 talloc_set_destructor(dev, destroy_device);
359 * device_handler_remove - Remove a device from the handler device array.
362 static void device_handler_remove(struct device_handler *handler,
363 struct discover_device *device)
367 for (i = 0; i < handler->n_devices; i++)
368 if (handler->devices[i] == device)
371 if (i == handler->n_devices) {
372 assert(0 && "unknown device");
376 handler->n_devices--;
377 memmove(&handler->devices[i], &handler->devices[i + 1],
378 (handler->n_devices - i) * sizeof(handler->devices[0]));
379 handler->devices = talloc_realloc(handler, handler->devices,
380 struct discover_device *, handler->n_devices);
382 discover_server_notify_device_remove(handler->server, device->device);
387 static void boot_status(void *arg, struct boot_status *status)
389 struct device_handler *handler = arg;
391 discover_server_notify_boot_status(handler->server, status);
394 static void countdown_status(struct device_handler *handler,
395 struct discover_boot_option *opt, unsigned int sec)
397 struct boot_status status;
399 status.type = BOOT_STATUS_INFO;
400 status.progress = -1;
401 status.detail = NULL;
402 status.message = talloc_asprintf(handler,
403 "Booting %s in %d sec", opt->option->name, sec);
405 discover_server_notify_boot_status(handler->server, &status);
407 talloc_free(status.message);
410 static int default_timeout(void *arg)
412 struct device_handler *handler = arg;
413 struct discover_boot_option *opt;
415 if (!handler->default_boot_option)
418 opt = handler->default_boot_option;
420 if (handler->sec_to_boot) {
421 countdown_status(handler, opt, handler->sec_to_boot);
422 handler->sec_to_boot--;
423 handler->timeout_waiter = waiter_register_timeout(
424 handler->waitset, 1000,
425 default_timeout, handler);
429 pb_log("Timeout expired, booting default option %s\n", opt->option->id);
431 boot(handler, handler->default_boot_option, NULL,
432 handler->dry_run, boot_status, handler);
436 static void set_default(struct device_handler *handler,
437 struct discover_boot_option *opt)
439 if (handler->default_boot_option)
442 if (!handler->default_enabled)
445 pb_log("Boot option %s set as default\n", opt->option->id);
447 handler->default_boot_option = opt;
448 handler->sec_to_boot = DEFAULT_BOOT_TIMEOUT_SEC;
449 default_timeout(handler);
452 static bool resource_is_resolved(struct resource *res)
454 return !res || res->resolved;
457 /* We only use this in an assert, which will disappear if we're compiling
458 * with NDEBUG, so we need the 'used' attribute for these builds */
459 static bool __attribute__((used)) boot_option_is_resolved(
460 struct discover_boot_option *opt)
462 return resource_is_resolved(opt->boot_image) &&
463 resource_is_resolved(opt->initrd) &&
464 resource_is_resolved(opt->dtb) &&
465 resource_is_resolved(opt->icon);
468 static bool resource_resolve(struct resource *res, const char *name,
469 struct discover_boot_option *opt,
470 struct device_handler *handler)
472 struct parser *parser = opt->source;
474 if (resource_is_resolved(res))
477 pb_log("Attempting to resolve resource %s->%s with parser %s\n",
478 opt->option->id, name, parser->name);
479 parser->resolve_resource(handler, res);
481 return res->resolved;
484 static bool boot_option_resolve(struct discover_boot_option *opt,
485 struct device_handler *handler)
487 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
488 resource_resolve(opt->initrd, "initrd", opt, handler) &&
489 resource_resolve(opt->dtb, "dtb", opt, handler) &&
490 resource_resolve(opt->icon, "icon", opt, handler);
493 static void boot_option_finalise(struct device_handler *handler,
494 struct discover_boot_option *opt)
496 assert(boot_option_is_resolved(opt));
498 /* check that the parsers haven't set any of the final data */
499 assert(!opt->option->boot_image_file);
500 assert(!opt->option->initrd_file);
501 assert(!opt->option->dtb_file);
502 assert(!opt->option->icon_file);
503 assert(!opt->option->device_id);
506 opt->option->boot_image_file = opt->boot_image->url->full;
508 opt->option->initrd_file = opt->initrd->url->full;
510 opt->option->dtb_file = opt->dtb->url->full;
512 opt->option->icon_file = opt->icon->url->full;
514 opt->option->device_id = opt->device->device->id;
516 if (opt->option->is_default)
517 set_default(handler, opt);
520 static void process_boot_option_queue(struct device_handler *handler)
522 struct discover_boot_option *opt, *tmp;
524 list_for_each_entry_safe(&handler->unresolved_boot_options,
527 pb_log("queue: attempting resolution for %s\n",
530 if (!boot_option_resolve(opt, handler))
533 pb_log("\tresolved!\n");
535 list_remove(&opt->list);
536 list_add_tail(&opt->device->boot_options, &opt->list);
537 talloc_steal(opt->device, opt);
538 boot_option_finalise(handler, opt);
539 discover_server_notify_boot_option_add(handler->server,
545 * context_commit - Commit a temporary discovery context to the handler,
546 * and notify the clients about any new options / devices
548 static void context_commit(struct device_handler *handler,
549 struct discover_context *ctx)
551 struct discover_device *dev = ctx->device;
552 struct discover_boot_option *opt, *tmp;
553 unsigned int i, existing_device = 0;
555 /* do we already have this device? */
556 for (i = 0; i < handler->n_devices; i++) {
557 if (ctx->device == handler->devices[i]) {
563 /* if not already present, add the device to the handler's array */
564 if (!existing_device) {
565 handler->n_devices++;
566 handler->devices = talloc_realloc(handler, handler->devices,
567 struct discover_device *, handler->n_devices);
568 handler->devices[handler->n_devices - 1] = dev;
569 talloc_steal(handler, dev);
571 discover_server_notify_device_add(handler->server, dev->device);
573 /* this new device might be able to resolve existing boot
575 pb_log("New device %s, processing queue\n", dev->device->id);
576 process_boot_option_queue(handler);
580 /* move boot options from the context to the device */
581 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
582 list_remove(&opt->list);
584 if (boot_option_resolve(opt, handler)) {
585 pb_log("boot option %s is resolved, "
586 "sending to clients\n",
588 list_add_tail(&dev->boot_options, &opt->list);
589 talloc_steal(dev, opt);
590 boot_option_finalise(handler, opt);
591 discover_server_notify_boot_option_add(handler->server,
594 if (!opt->source->resolve_resource) {
595 pb_log("parser %s gave us an unresolved "
596 "resource (%s), but no way to "
598 opt->source->name, opt->option->id);
601 pb_log("boot option %s is unresolved, "
604 list_add(&handler->unresolved_boot_options,
606 talloc_steal(handler, opt);
612 static int handle_add_udev_event(struct device_handler *handler,
615 struct discover_context *ctx;
616 struct discover_device *dev;
620 /* create our context */
621 ctx = talloc(handler, struct discover_context);
623 list_init(&ctx->boot_options);
625 /* create our top-level device */
626 dev = discover_device_create(handler, ctx, event);
630 /* try to parse UUID and labels */
631 param = event_get_param(ctx->event, "ID_FS_UUID");
633 dev->uuid = talloc_strdup(dev, param);
635 param = event_get_param(ctx->event, "ID_FS_LABEL");
637 dev->label = talloc_strdup(dev, param);
639 rc = mount_device(dev);
645 /* run the parsers. This will populate the ctx's boot_option list. */
646 iterate_parsers(ctx, CONF_METHOD_LOCAL_FILE);
648 /* add discovered stuff to the handler */
649 context_commit(handler, ctx);
656 static int handle_remove_udev_event(struct device_handler *handler,
659 struct discover_device *dev;
661 dev = find_device(handler, event->device);
665 /* remove device from handler device array */
666 device_handler_remove(handler, dev);
671 static int handle_add_user_event(struct device_handler *handler,
674 struct discover_context *ctx;
675 struct discover_device *dev;
678 assert(event->device);
680 ctx = talloc(handler, struct discover_context);
682 list_init(&ctx->boot_options);
684 dev = discover_device_create(handler, ctx, event);
687 rc = parse_user_event(ctx, event);
690 context_commit(handler, ctx);
695 static int handle_remove_user_event(struct device_handler *handler,
698 struct discover_device *dev = find_device(handler, event->device);
703 /* remove device from handler device array */
704 device_handler_remove(handler, dev);
709 static enum conf_method parse_conf_method(const char *str)
712 if (!strcasecmp(str, "dhcp")) {
713 return CONF_METHOD_DHCP;
715 return CONF_METHOD_UNKNOWN;
718 static int handle_conf_user_event(struct device_handler *handler,
721 struct discover_context *ctx;
722 struct discover_device *dev;
723 enum conf_method method;
726 ctx = talloc(handler, struct discover_context);
728 list_init(&ctx->boot_options);
730 val = event_get_param(event, "url");
736 ctx->conf_url = pb_url_parse(ctx, val);
737 if (!ctx->conf_url) {
742 val = event_get_param(event, "method");
748 method = parse_conf_method(val);
749 if (method == CONF_METHOD_UNKNOWN) {
754 dev = discover_device_create(handler, ctx, event);
757 iterate_parsers(ctx, method);
759 context_commit(handler, ctx);
764 typedef int (*event_handler)(struct device_handler *, struct event *);
766 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
767 [EVENT_TYPE_UDEV] = {
768 [EVENT_ACTION_ADD] = handle_add_udev_event,
769 [EVENT_ACTION_REMOVE] = handle_remove_udev_event,
771 [EVENT_TYPE_USER] = {
772 [EVENT_ACTION_ADD] = handle_add_user_event,
773 [EVENT_ACTION_REMOVE] = handle_remove_user_event,
774 [EVENT_ACTION_CONF] = handle_conf_user_event,
778 int device_handler_event(struct device_handler *handler,
781 if (event->type >= EVENT_TYPE_MAX ||
782 event->action >= EVENT_ACTION_MAX ||
783 !handlers[event->type][event->action]) {
784 pb_log("%s unknown type/action: %d/%d\n", __func__,
785 event->type, event->action);
789 return handlers[event->type][event->action](handler, event);
792 static struct discover_boot_option *find_boot_option_by_id(
793 struct device_handler *handler, const char *id)
797 for (i = 0; i < handler->n_devices; i++) {
798 struct discover_device *dev = handler->devices[i];
799 struct discover_boot_option *opt;
801 list_for_each_entry(&dev->boot_options, opt, list)
802 if (!strcmp(opt->option->id, id))
809 void device_handler_boot(struct device_handler *handler,
810 struct boot_command *cmd)
812 struct discover_boot_option *opt;
814 opt = find_boot_option_by_id(handler, cmd->option_id);
816 boot(handler, opt, cmd, handler->dry_run, boot_status, handler);
819 void device_handler_cancel_default(struct device_handler *handler)
821 struct boot_status status;
823 if (handler->timeout_waiter)
824 waiter_remove(handler->timeout_waiter);
826 handler->timeout_waiter = NULL;
827 handler->default_enabled = false;
829 /* we only send status if we had a default boot option queued */
830 if (!handler->default_boot_option)
833 pb_log("Cancelling default boot option\n");
835 handler->default_boot_option = NULL;
837 status.type = BOOT_STATUS_INFO;
838 status.progress = -1;
839 status.detail = NULL;
840 status.message = "Default boot cancelled";
842 discover_server_notify_boot_status(handler->server, &status);