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>
16 #include <process/process.h>
19 #include "device-handler.h"
20 #include "discover-server.h"
28 struct device_handler {
29 struct discover_server *server;
32 struct discover_device **devices;
33 unsigned int n_devices;
35 struct waitset *waitset;
36 struct waiter *timeout_waiter;
37 bool autoboot_enabled;
38 unsigned int sec_to_boot;
40 struct discover_boot_option *default_boot_option;
41 struct list unresolved_boot_options;
44 void discover_context_add_boot_option(struct discover_context *ctx,
45 struct discover_boot_option *boot_option)
47 boot_option->source = ctx->parser;
48 list_add_tail(&ctx->boot_options, &boot_option->list);
49 talloc_steal(ctx, boot_option);
53 * device_handler_get_device_count - Get the count of current handler devices.
56 int device_handler_get_device_count(const struct device_handler *handler)
58 return handler->n_devices;
62 * device_handler_get_device - Get a handler device by index.
65 const struct discover_device *device_handler_get_device(
66 const struct device_handler *handler, unsigned int index)
68 if (index >= handler->n_devices) {
69 assert(0 && "bad index");
73 return handler->devices[index];
76 struct discover_boot_option *discover_boot_option_create(
77 struct discover_context *ctx,
78 struct discover_device *device)
80 struct discover_boot_option *opt;
82 opt = talloc_zero(ctx, struct discover_boot_option);
83 opt->option = talloc_zero(opt, struct boot_option);
89 static int device_match_path(struct discover_device *dev, const char *path)
91 return dev->device_path && !strcmp(dev->device_path, path);
94 static int device_match_uuid(struct discover_device *dev, const char *uuid)
96 return dev->uuid && !strcmp(dev->uuid, uuid);
99 static int device_match_label(struct discover_device *dev, const char *label)
101 return dev->label && !strcmp(dev->label, label);
104 static int device_match_id(struct discover_device *dev, const char *id)
106 return !strcmp(dev->device->id, id);
109 static struct discover_device *device_lookup(
110 struct device_handler *device_handler,
111 int (match_fn)(struct discover_device *, const char *),
114 struct discover_device *dev;
120 for (i = 0; i < device_handler->n_devices; i++) {
121 dev = device_handler->devices[i];
123 if (match_fn(dev, str))
130 struct discover_device *device_lookup_by_name(struct device_handler *handler,
133 struct discover_device *dev;
136 if (strncmp(name, "/dev/", strlen("/dev/")))
137 path = talloc_asprintf(NULL, "/dev/%s", name);
139 path = talloc_strdup(NULL, name);
141 dev = device_lookup_by_path(handler, path);
148 struct discover_device *device_lookup_by_path(
149 struct device_handler *device_handler,
152 return device_lookup(device_handler, device_match_path, path);
155 struct discover_device *device_lookup_by_uuid(
156 struct device_handler *device_handler,
159 return device_lookup(device_handler, device_match_uuid, uuid);
162 struct discover_device *device_lookup_by_label(
163 struct device_handler *device_handler,
166 return device_lookup(device_handler, device_match_label, label);
169 struct discover_device *device_lookup_by_id(
170 struct device_handler *device_handler,
173 return device_lookup(device_handler, device_match_id, id);
176 void device_handler_destroy(struct device_handler *handler)
178 talloc_free(handler);
181 #ifdef PETITBOOT_TEST
183 /* we have a simplified interface for petitboot testing, but still want
184 * to keep struct device_handler opaque. */
185 struct device_handler *device_handler_init(
186 struct discover_server *server __attribute__((unused)),
187 struct waitset *waitset __attribute__((unused)),
188 int dry_run __attribute__((unused)))
190 struct device_handler *handler;
192 handler = talloc_zero(NULL, struct device_handler);
193 list_init(&handler->unresolved_boot_options);
198 void device_handler_add_device(struct device_handler *handler,
199 struct discover_device *dev)
201 handler->n_devices++;
202 handler->devices = talloc_realloc(handler, handler->devices,
203 struct discover_device *, handler->n_devices);
204 handler->devices[handler->n_devices - 1] = dev;
209 static int mount_device(struct discover_device *dev)
213 if (!dev->device_path)
216 if (!dev->mount_path)
217 dev->mount_path = join_paths(dev, mount_base(),
220 if (pb_mkdir_recursive(dev->mount_path))
221 pb_log("couldn't create mount directory %s: %s\n",
222 dev->mount_path, strerror(errno));
224 rc = process_run_simple(dev, pb_system_apps.mount,
225 dev->device_path, dev->mount_path,
231 /* Retry mount without ro option. */
232 rc = process_run_simple(dev, pb_system_apps.mount,
233 dev->device_path, dev->mount_path, NULL);
238 pb_rmdir_recursive(mount_base(), dev->mount_path);
242 static int umount_device(struct discover_device *dev)
246 if (!dev->mount_path)
249 status = process_run_simple(dev, pb_system_apps.umount,
250 dev->mount_path, NULL);
252 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
255 pb_rmdir_recursive(mount_base(), dev->mount_path);
260 struct device_handler *device_handler_init(struct discover_server *server,
261 struct waitset *waitset, int dry_run)
263 struct device_handler *handler;
265 handler = talloc(NULL, struct device_handler);
266 handler->devices = NULL;
267 handler->n_devices = 0;
268 handler->server = server;
269 handler->waitset = waitset;
270 handler->dry_run = dry_run;
271 handler->default_boot_option = NULL;
272 handler->autoboot_enabled = config_get()->autoboot_enabled;
273 list_init(&handler->unresolved_boot_options);
275 /* set up our mount point base */
276 pb_mkdir_recursive(mount_base());
283 static int destroy_device(void *arg)
285 struct discover_device *dev = arg;
292 static struct discover_device *find_device(struct device_handler *handler,
295 struct discover_device *dev;
298 for (i = 0; i < handler->n_devices; i++) {
299 dev = handler->devices[i];
300 if (!strcmp(dev->device->id, id))
307 static struct discover_device *discover_device_create(
308 struct device_handler *handler,
309 struct discover_context *ctx,
312 struct discover_device *dev;
315 dev = find_device(handler, event->device);
319 dev = talloc_zero(ctx, struct discover_device);
320 dev->device = talloc_zero(dev, struct device);
321 list_init(&dev->boot_options);
323 devname = event_get_param(ctx->event, "DEVNAME");
325 dev->device_path = talloc_strdup(dev, devname);
327 dev->device->id = talloc_strdup(dev, event->device);
329 talloc_set_destructor(dev, destroy_device);
335 * device_handler_remove - Remove a device from the handler device array.
338 static void device_handler_remove(struct device_handler *handler,
339 struct discover_device *device)
343 for (i = 0; i < handler->n_devices; i++)
344 if (handler->devices[i] == device)
347 if (i == handler->n_devices) {
348 assert(0 && "unknown device");
352 handler->n_devices--;
353 memmove(&handler->devices[i], &handler->devices[i + 1],
354 (handler->n_devices - i) * sizeof(handler->devices[0]));
355 handler->devices = talloc_realloc(handler, handler->devices,
356 struct discover_device *, handler->n_devices);
358 discover_server_notify_device_remove(handler->server, device->device);
363 static void boot_status(void *arg, struct boot_status *status)
365 struct device_handler *handler = arg;
367 discover_server_notify_boot_status(handler->server, status);
370 static void countdown_status(struct device_handler *handler,
371 struct discover_boot_option *opt, unsigned int sec)
373 struct boot_status status;
375 status.type = BOOT_STATUS_INFO;
376 status.progress = -1;
377 status.detail = NULL;
378 status.message = talloc_asprintf(handler,
379 "Booting %s in %u sec", opt->option->name, sec);
381 discover_server_notify_boot_status(handler->server, &status);
383 talloc_free(status.message);
386 static int default_timeout(void *arg)
388 struct device_handler *handler = arg;
389 struct discover_boot_option *opt;
391 if (!handler->default_boot_option)
394 opt = handler->default_boot_option;
396 if (handler->sec_to_boot) {
397 countdown_status(handler, opt, handler->sec_to_boot);
398 handler->sec_to_boot--;
399 handler->timeout_waiter = waiter_register_timeout(
400 handler->waitset, 1000,
401 default_timeout, handler);
405 pb_log("Timeout expired, booting default option %s\n", opt->option->id);
407 boot(handler, handler->default_boot_option, NULL,
408 handler->dry_run, boot_status, handler);
412 static void set_default(struct device_handler *handler,
413 struct discover_boot_option *opt)
415 if (handler->default_boot_option)
418 if (!handler->autoboot_enabled)
421 handler->default_boot_option = opt;
422 handler->sec_to_boot = config_get()->autoboot_timeout_sec;
424 pb_log("Boot option %s set as default, timeout %u sec.\n",
425 opt->option->id, handler->sec_to_boot);
427 default_timeout(handler);
430 static bool resource_is_resolved(struct resource *res)
432 return !res || res->resolved;
435 /* We only use this in an assert, which will disappear if we're compiling
436 * with NDEBUG, so we need the 'used' attribute for these builds */
437 static bool __attribute__((used)) boot_option_is_resolved(
438 struct discover_boot_option *opt)
440 return resource_is_resolved(opt->boot_image) &&
441 resource_is_resolved(opt->initrd) &&
442 resource_is_resolved(opt->dtb) &&
443 resource_is_resolved(opt->icon);
446 static bool resource_resolve(struct resource *res, const char *name,
447 struct discover_boot_option *opt,
448 struct device_handler *handler)
450 struct parser *parser = opt->source;
452 if (resource_is_resolved(res))
455 pb_log("Attempting to resolve resource %s->%s with parser %s\n",
456 opt->option->id, name, parser->name);
457 parser->resolve_resource(handler, res);
459 return res->resolved;
462 static bool boot_option_resolve(struct discover_boot_option *opt,
463 struct device_handler *handler)
465 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
466 resource_resolve(opt->initrd, "initrd", opt, handler) &&
467 resource_resolve(opt->dtb, "dtb", opt, handler) &&
468 resource_resolve(opt->icon, "icon", opt, handler);
471 static void boot_option_finalise(struct device_handler *handler,
472 struct discover_boot_option *opt)
474 assert(boot_option_is_resolved(opt));
476 /* check that the parsers haven't set any of the final data */
477 assert(!opt->option->boot_image_file);
478 assert(!opt->option->initrd_file);
479 assert(!opt->option->dtb_file);
480 assert(!opt->option->icon_file);
481 assert(!opt->option->device_id);
484 opt->option->boot_image_file = opt->boot_image->url->full;
486 opt->option->initrd_file = opt->initrd->url->full;
488 opt->option->dtb_file = opt->dtb->url->full;
490 opt->option->icon_file = opt->icon->url->full;
492 opt->option->device_id = opt->device->device->id;
494 if (opt->option->is_default)
495 set_default(handler, opt);
498 static void process_boot_option_queue(struct device_handler *handler)
500 struct discover_boot_option *opt, *tmp;
502 list_for_each_entry_safe(&handler->unresolved_boot_options,
505 pb_log("queue: attempting resolution for %s\n",
508 if (!boot_option_resolve(opt, handler))
511 pb_log("\tresolved!\n");
513 list_remove(&opt->list);
514 list_add_tail(&opt->device->boot_options, &opt->list);
515 talloc_steal(opt->device, opt);
516 boot_option_finalise(handler, opt);
517 discover_server_notify_boot_option_add(handler->server,
523 * context_commit - Commit a temporary discovery context to the handler,
524 * and notify the clients about any new options / devices
526 static void context_commit(struct device_handler *handler,
527 struct discover_context *ctx)
529 struct discover_device *dev = ctx->device;
530 struct discover_boot_option *opt, *tmp;
531 unsigned int i, existing_device = 0;
533 /* do we already have this device? */
534 for (i = 0; i < handler->n_devices; i++) {
535 if (ctx->device == handler->devices[i]) {
541 /* if not already present, add the device to the handler's array */
542 if (!existing_device) {
543 handler->n_devices++;
544 handler->devices = talloc_realloc(handler, handler->devices,
545 struct discover_device *, handler->n_devices);
546 handler->devices[handler->n_devices - 1] = dev;
547 talloc_steal(handler, dev);
549 discover_server_notify_device_add(handler->server, dev->device);
551 /* this new device might be able to resolve existing boot
553 pb_log("New device %s, processing queue\n", dev->device->id);
554 process_boot_option_queue(handler);
558 /* move boot options from the context to the device */
559 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
560 list_remove(&opt->list);
562 if (boot_option_resolve(opt, handler)) {
563 pb_log("boot option %s is resolved, "
564 "sending to clients\n",
566 list_add_tail(&dev->boot_options, &opt->list);
567 talloc_steal(dev, opt);
568 boot_option_finalise(handler, opt);
569 discover_server_notify_boot_option_add(handler->server,
572 if (!opt->source->resolve_resource) {
573 pb_log("parser %s gave us an unresolved "
574 "resource (%s), but no way to "
576 opt->source->name, opt->option->id);
579 pb_log("boot option %s is unresolved, "
582 list_add(&handler->unresolved_boot_options,
584 talloc_steal(handler, opt);
590 static int handle_add_udev_event(struct device_handler *handler,
593 struct discover_context *ctx;
594 struct discover_device *dev;
598 /* create our context */
599 ctx = talloc(handler, struct discover_context);
601 list_init(&ctx->boot_options);
603 /* create our top-level device */
604 dev = discover_device_create(handler, ctx, event);
608 /* try to parse UUID and labels */
609 param = event_get_param(ctx->event, "ID_FS_UUID");
611 dev->uuid = talloc_strdup(dev, param);
613 param = event_get_param(ctx->event, "ID_FS_LABEL");
615 dev->label = talloc_strdup(dev, param);
617 rc = mount_device(dev);
623 /* run the parsers. This will populate the ctx's boot_option list. */
624 iterate_parsers(ctx, CONF_METHOD_LOCAL_FILE);
626 /* add discovered stuff to the handler */
627 context_commit(handler, ctx);
634 static int handle_remove_udev_event(struct device_handler *handler,
637 struct discover_device *dev;
639 dev = find_device(handler, event->device);
643 /* remove device from handler device array */
644 device_handler_remove(handler, dev);
649 static int handle_add_user_event(struct device_handler *handler,
652 struct discover_context *ctx;
653 struct discover_device *dev;
656 assert(event->device);
658 ctx = talloc(handler, struct discover_context);
660 list_init(&ctx->boot_options);
662 dev = discover_device_create(handler, ctx, event);
665 rc = parse_user_event(ctx, event);
668 context_commit(handler, ctx);
673 static int handle_remove_user_event(struct device_handler *handler,
676 struct discover_device *dev = find_device(handler, event->device);
681 /* remove device from handler device array */
682 device_handler_remove(handler, dev);
687 static enum conf_method parse_conf_method(const char *str)
690 if (!strcasecmp(str, "dhcp")) {
691 return CONF_METHOD_DHCP;
693 return CONF_METHOD_UNKNOWN;
696 static int handle_conf_user_event(struct device_handler *handler,
699 struct discover_context *ctx;
700 struct discover_device *dev;
701 enum conf_method method;
704 ctx = talloc(handler, struct discover_context);
706 list_init(&ctx->boot_options);
708 val = event_get_param(event, "url");
714 ctx->conf_url = pb_url_parse(ctx, val);
715 if (!ctx->conf_url) {
720 val = event_get_param(event, "method");
726 method = parse_conf_method(val);
727 if (method == CONF_METHOD_UNKNOWN) {
732 dev = discover_device_create(handler, ctx, event);
735 iterate_parsers(ctx, method);
737 context_commit(handler, ctx);
742 typedef int (*event_handler)(struct device_handler *, struct event *);
744 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
745 [EVENT_TYPE_UDEV] = {
746 [EVENT_ACTION_ADD] = handle_add_udev_event,
747 [EVENT_ACTION_REMOVE] = handle_remove_udev_event,
749 [EVENT_TYPE_USER] = {
750 [EVENT_ACTION_ADD] = handle_add_user_event,
751 [EVENT_ACTION_REMOVE] = handle_remove_user_event,
752 [EVENT_ACTION_CONF] = handle_conf_user_event,
756 int device_handler_event(struct device_handler *handler,
759 if (event->type >= EVENT_TYPE_MAX ||
760 event->action >= EVENT_ACTION_MAX ||
761 !handlers[event->type][event->action]) {
762 pb_log("%s unknown type/action: %d/%d\n", __func__,
763 event->type, event->action);
767 return handlers[event->type][event->action](handler, event);
770 static struct discover_boot_option *find_boot_option_by_id(
771 struct device_handler *handler, const char *id)
775 for (i = 0; i < handler->n_devices; i++) {
776 struct discover_device *dev = handler->devices[i];
777 struct discover_boot_option *opt;
779 list_for_each_entry(&dev->boot_options, opt, list)
780 if (!strcmp(opt->option->id, id))
787 void device_handler_boot(struct device_handler *handler,
788 struct boot_command *cmd)
790 struct discover_boot_option *opt;
792 opt = find_boot_option_by_id(handler, cmd->option_id);
794 boot(handler, opt, cmd, handler->dry_run, boot_status, handler);
797 void device_handler_cancel_default(struct device_handler *handler)
799 struct boot_status status;
801 if (handler->timeout_waiter)
802 waiter_remove(handler->timeout_waiter);
804 handler->timeout_waiter = NULL;
805 handler->autoboot_enabled = false;
807 /* we only send status if we had a default boot option queued */
808 if (!handler->default_boot_option)
811 pb_log("Cancelling default boot option\n");
813 handler->default_boot_option = NULL;
815 status.type = BOOT_STATUS_INFO;
816 status.progress = -1;
817 status.detail = NULL;
818 status.message = "Default boot cancelled";
820 discover_server_notify_boot_status(handler->server, &status);