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;
36 bool autoboot_enabled;
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->autoboot_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 %u 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->autoboot_enabled)
445 handler->default_boot_option = opt;
446 handler->sec_to_boot = DEFAULT_BOOT_TIMEOUT_SEC;
448 pb_log("Boot option %s set as default, timeout %u sec.\n",
449 opt->option->id, handler->sec_to_boot);
451 default_timeout(handler);
454 static bool resource_is_resolved(struct resource *res)
456 return !res || res->resolved;
459 /* We only use this in an assert, which will disappear if we're compiling
460 * with NDEBUG, so we need the 'used' attribute for these builds */
461 static bool __attribute__((used)) boot_option_is_resolved(
462 struct discover_boot_option *opt)
464 return resource_is_resolved(opt->boot_image) &&
465 resource_is_resolved(opt->initrd) &&
466 resource_is_resolved(opt->dtb) &&
467 resource_is_resolved(opt->icon);
470 static bool resource_resolve(struct resource *res, const char *name,
471 struct discover_boot_option *opt,
472 struct device_handler *handler)
474 struct parser *parser = opt->source;
476 if (resource_is_resolved(res))
479 pb_log("Attempting to resolve resource %s->%s with parser %s\n",
480 opt->option->id, name, parser->name);
481 parser->resolve_resource(handler, res);
483 return res->resolved;
486 static bool boot_option_resolve(struct discover_boot_option *opt,
487 struct device_handler *handler)
489 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
490 resource_resolve(opt->initrd, "initrd", opt, handler) &&
491 resource_resolve(opt->dtb, "dtb", opt, handler) &&
492 resource_resolve(opt->icon, "icon", opt, handler);
495 static void boot_option_finalise(struct device_handler *handler,
496 struct discover_boot_option *opt)
498 assert(boot_option_is_resolved(opt));
500 /* check that the parsers haven't set any of the final data */
501 assert(!opt->option->boot_image_file);
502 assert(!opt->option->initrd_file);
503 assert(!opt->option->dtb_file);
504 assert(!opt->option->icon_file);
505 assert(!opt->option->device_id);
508 opt->option->boot_image_file = opt->boot_image->url->full;
510 opt->option->initrd_file = opt->initrd->url->full;
512 opt->option->dtb_file = opt->dtb->url->full;
514 opt->option->icon_file = opt->icon->url->full;
516 opt->option->device_id = opt->device->device->id;
518 if (opt->option->is_default)
519 set_default(handler, opt);
522 static void process_boot_option_queue(struct device_handler *handler)
524 struct discover_boot_option *opt, *tmp;
526 list_for_each_entry_safe(&handler->unresolved_boot_options,
529 pb_log("queue: attempting resolution for %s\n",
532 if (!boot_option_resolve(opt, handler))
535 pb_log("\tresolved!\n");
537 list_remove(&opt->list);
538 list_add_tail(&opt->device->boot_options, &opt->list);
539 talloc_steal(opt->device, opt);
540 boot_option_finalise(handler, opt);
541 discover_server_notify_boot_option_add(handler->server,
547 * context_commit - Commit a temporary discovery context to the handler,
548 * and notify the clients about any new options / devices
550 static void context_commit(struct device_handler *handler,
551 struct discover_context *ctx)
553 struct discover_device *dev = ctx->device;
554 struct discover_boot_option *opt, *tmp;
555 unsigned int i, existing_device = 0;
557 /* do we already have this device? */
558 for (i = 0; i < handler->n_devices; i++) {
559 if (ctx->device == handler->devices[i]) {
565 /* if not already present, add the device to the handler's array */
566 if (!existing_device) {
567 handler->n_devices++;
568 handler->devices = talloc_realloc(handler, handler->devices,
569 struct discover_device *, handler->n_devices);
570 handler->devices[handler->n_devices - 1] = dev;
571 talloc_steal(handler, dev);
573 discover_server_notify_device_add(handler->server, dev->device);
575 /* this new device might be able to resolve existing boot
577 pb_log("New device %s, processing queue\n", dev->device->id);
578 process_boot_option_queue(handler);
582 /* move boot options from the context to the device */
583 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
584 list_remove(&opt->list);
586 if (boot_option_resolve(opt, handler)) {
587 pb_log("boot option %s is resolved, "
588 "sending to clients\n",
590 list_add_tail(&dev->boot_options, &opt->list);
591 talloc_steal(dev, opt);
592 boot_option_finalise(handler, opt);
593 discover_server_notify_boot_option_add(handler->server,
596 if (!opt->source->resolve_resource) {
597 pb_log("parser %s gave us an unresolved "
598 "resource (%s), but no way to "
600 opt->source->name, opt->option->id);
603 pb_log("boot option %s is unresolved, "
606 list_add(&handler->unresolved_boot_options,
608 talloc_steal(handler, opt);
614 static int handle_add_udev_event(struct device_handler *handler,
617 struct discover_context *ctx;
618 struct discover_device *dev;
622 /* create our context */
623 ctx = talloc(handler, struct discover_context);
625 list_init(&ctx->boot_options);
627 /* create our top-level device */
628 dev = discover_device_create(handler, ctx, event);
632 /* try to parse UUID and labels */
633 param = event_get_param(ctx->event, "ID_FS_UUID");
635 dev->uuid = talloc_strdup(dev, param);
637 param = event_get_param(ctx->event, "ID_FS_LABEL");
639 dev->label = talloc_strdup(dev, param);
641 rc = mount_device(dev);
647 /* run the parsers. This will populate the ctx's boot_option list. */
648 iterate_parsers(ctx, CONF_METHOD_LOCAL_FILE);
650 /* add discovered stuff to the handler */
651 context_commit(handler, ctx);
658 static int handle_remove_udev_event(struct device_handler *handler,
661 struct discover_device *dev;
663 dev = find_device(handler, event->device);
667 /* remove device from handler device array */
668 device_handler_remove(handler, dev);
673 static int handle_add_user_event(struct device_handler *handler,
676 struct discover_context *ctx;
677 struct discover_device *dev;
680 assert(event->device);
682 ctx = talloc(handler, struct discover_context);
684 list_init(&ctx->boot_options);
686 dev = discover_device_create(handler, ctx, event);
689 rc = parse_user_event(ctx, event);
692 context_commit(handler, ctx);
697 static int handle_remove_user_event(struct device_handler *handler,
700 struct discover_device *dev = find_device(handler, event->device);
705 /* remove device from handler device array */
706 device_handler_remove(handler, dev);
711 static enum conf_method parse_conf_method(const char *str)
714 if (!strcasecmp(str, "dhcp")) {
715 return CONF_METHOD_DHCP;
717 return CONF_METHOD_UNKNOWN;
720 static int handle_conf_user_event(struct device_handler *handler,
723 struct discover_context *ctx;
724 struct discover_device *dev;
725 enum conf_method method;
728 ctx = talloc(handler, struct discover_context);
730 list_init(&ctx->boot_options);
732 val = event_get_param(event, "url");
738 ctx->conf_url = pb_url_parse(ctx, val);
739 if (!ctx->conf_url) {
744 val = event_get_param(event, "method");
750 method = parse_conf_method(val);
751 if (method == CONF_METHOD_UNKNOWN) {
756 dev = discover_device_create(handler, ctx, event);
759 iterate_parsers(ctx, method);
761 context_commit(handler, ctx);
766 typedef int (*event_handler)(struct device_handler *, struct event *);
768 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
769 [EVENT_TYPE_UDEV] = {
770 [EVENT_ACTION_ADD] = handle_add_udev_event,
771 [EVENT_ACTION_REMOVE] = handle_remove_udev_event,
773 [EVENT_TYPE_USER] = {
774 [EVENT_ACTION_ADD] = handle_add_user_event,
775 [EVENT_ACTION_REMOVE] = handle_remove_user_event,
776 [EVENT_ACTION_CONF] = handle_conf_user_event,
780 int device_handler_event(struct device_handler *handler,
783 if (event->type >= EVENT_TYPE_MAX ||
784 event->action >= EVENT_ACTION_MAX ||
785 !handlers[event->type][event->action]) {
786 pb_log("%s unknown type/action: %d/%d\n", __func__,
787 event->type, event->action);
791 return handlers[event->type][event->action](handler, event);
794 static struct discover_boot_option *find_boot_option_by_id(
795 struct device_handler *handler, const char *id)
799 for (i = 0; i < handler->n_devices; i++) {
800 struct discover_device *dev = handler->devices[i];
801 struct discover_boot_option *opt;
803 list_for_each_entry(&dev->boot_options, opt, list)
804 if (!strcmp(opt->option->id, id))
811 void device_handler_boot(struct device_handler *handler,
812 struct boot_command *cmd)
814 struct discover_boot_option *opt;
816 opt = find_boot_option_by_id(handler, cmd->option_id);
818 boot(handler, opt, cmd, handler->dry_run, boot_status, handler);
821 void device_handler_cancel_default(struct device_handler *handler)
823 struct boot_status status;
825 if (handler->timeout_waiter)
826 waiter_remove(handler->timeout_waiter);
828 handler->timeout_waiter = NULL;
829 handler->autoboot_enabled = false;
831 /* we only send status if we had a default boot option queued */
832 if (!handler->default_boot_option)
835 pb_log("Cancelling default boot option\n");
837 handler->default_boot_option = NULL;
839 status.type = BOOT_STATUS_INFO;
840 status.progress = -1;
841 status.detail = NULL;
842 status.message = "Default boot cancelled";
844 discover_server_notify_boot_status(handler->server, &status);