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_uuid(struct discover_device *dev, const char *uuid)
91 return dev->uuid && !strcmp(dev->uuid, uuid);
94 static int device_match_label(struct discover_device *dev, const char *label)
96 return dev->label && !strcmp(dev->label, label);
99 static int device_match_id(struct discover_device *dev, const char *id)
101 return !strcmp(dev->device->id, id);
104 static struct discover_device *device_lookup(
105 struct device_handler *device_handler,
106 int (match_fn)(struct discover_device *, const char *),
109 struct discover_device *dev;
115 for (i = 0; i < device_handler->n_devices; i++) {
116 dev = device_handler->devices[i];
118 if (match_fn(dev, str))
125 struct discover_device *device_lookup_by_name(struct device_handler *handler,
128 if (!strncmp(name, "/dev/", strlen("/dev/")))
129 name += strlen("/dev/");
131 return device_lookup_by_id(handler, name);
134 struct discover_device *device_lookup_by_uuid(
135 struct device_handler *device_handler,
138 return device_lookup(device_handler, device_match_uuid, uuid);
141 struct discover_device *device_lookup_by_label(
142 struct device_handler *device_handler,
145 return device_lookup(device_handler, device_match_label, label);
148 struct discover_device *device_lookup_by_id(
149 struct device_handler *device_handler,
152 return device_lookup(device_handler, device_match_id, id);
155 void device_handler_destroy(struct device_handler *handler)
157 talloc_free(handler);
160 #ifdef PETITBOOT_TEST
162 /* we have a simplified interface for petitboot testing, but still want
163 * to keep struct device_handler opaque. */
164 struct device_handler *device_handler_init(
165 struct discover_server *server __attribute__((unused)),
166 struct waitset *waitset __attribute__((unused)),
167 int dry_run __attribute__((unused)))
169 struct device_handler *handler;
171 handler = talloc_zero(NULL, struct device_handler);
172 list_init(&handler->unresolved_boot_options);
177 void device_handler_add_device(struct device_handler *handler,
178 struct discover_device *dev)
180 handler->n_devices++;
181 handler->devices = talloc_realloc(handler, handler->devices,
182 struct discover_device *, handler->n_devices);
183 handler->devices[handler->n_devices - 1] = dev;
188 static int mount_device(struct discover_device *dev)
192 if (!dev->device_path)
195 if (!dev->mount_path)
196 dev->mount_path = join_paths(dev, mount_base(),
199 if (pb_mkdir_recursive(dev->mount_path))
200 pb_log("couldn't create mount directory %s: %s\n",
201 dev->mount_path, strerror(errno));
203 rc = process_run_simple(dev, pb_system_apps.mount,
204 dev->device_path, dev->mount_path,
210 /* Retry mount without ro option. */
211 rc = process_run_simple(dev, pb_system_apps.mount,
212 dev->device_path, dev->mount_path, NULL);
217 pb_rmdir_recursive(mount_base(), dev->mount_path);
221 static int umount_device(struct discover_device *dev)
225 if (!dev->mount_path)
228 status = process_run_simple(dev, pb_system_apps.umount,
229 dev->mount_path, NULL);
231 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
234 pb_rmdir_recursive(mount_base(), dev->mount_path);
239 struct device_handler *device_handler_init(struct discover_server *server,
240 struct waitset *waitset, int dry_run)
242 struct device_handler *handler;
244 handler = talloc(NULL, struct device_handler);
245 handler->devices = NULL;
246 handler->n_devices = 0;
247 handler->server = server;
248 handler->waitset = waitset;
249 handler->dry_run = dry_run;
250 handler->default_boot_option = NULL;
251 handler->autoboot_enabled = config_get()->autoboot_enabled;
252 list_init(&handler->unresolved_boot_options);
254 /* set up our mount point base */
255 pb_mkdir_recursive(mount_base());
262 static int destroy_device(void *arg)
264 struct discover_device *dev = arg;
271 static struct discover_device *find_device(struct device_handler *handler,
274 struct discover_device *dev;
277 for (i = 0; i < handler->n_devices; i++) {
278 dev = handler->devices[i];
279 if (!strcmp(dev->device->id, id))
286 static enum device_type event_device_type(struct device *device,
291 param = event_get_param(event, "type");
293 pb_log("%s: empty type\n", device->id);
294 return DEVICE_TYPE_UNKNOWN;
297 if (!strcmp(param, "disk") || !strcmp(param, "partition"))
298 return DEVICE_TYPE_DISK;
300 if (!strcmp(param, "net"))
301 return DEVICE_TYPE_NETWORK;
303 pb_log("%s: unknown type '%s'\n", device->id, param);
304 return DEVICE_TYPE_UNKNOWN;
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 devnode = event_get_param(ctx->event, "node");
325 dev->device_path = talloc_strdup(dev, devnode);
327 dev->device->id = talloc_strdup(dev, event->device);
328 dev->device->type = event_device_type(dev->device, event);
330 talloc_set_destructor(dev, destroy_device);
336 * device_handler_remove - Remove a device from the handler device array.
339 static void device_handler_remove(struct device_handler *handler,
340 struct discover_device *device)
344 for (i = 0; i < handler->n_devices; i++)
345 if (handler->devices[i] == device)
348 if (i == handler->n_devices) {
349 assert(0 && "unknown device");
353 handler->n_devices--;
354 memmove(&handler->devices[i], &handler->devices[i + 1],
355 (handler->n_devices - i) * sizeof(handler->devices[0]));
356 handler->devices = talloc_realloc(handler, handler->devices,
357 struct discover_device *, handler->n_devices);
359 discover_server_notify_device_remove(handler->server, device->device);
364 static void boot_status(void *arg, struct boot_status *status)
366 struct device_handler *handler = arg;
368 discover_server_notify_boot_status(handler->server, status);
371 static void countdown_status(struct device_handler *handler,
372 struct discover_boot_option *opt, unsigned int sec)
374 struct boot_status status;
376 status.type = BOOT_STATUS_INFO;
377 status.progress = -1;
378 status.detail = NULL;
379 status.message = talloc_asprintf(handler,
380 "Booting %s in %u sec", opt->option->name, sec);
382 discover_server_notify_boot_status(handler->server, &status);
384 talloc_free(status.message);
387 static int default_timeout(void *arg)
389 struct device_handler *handler = arg;
390 struct discover_boot_option *opt;
392 if (!handler->default_boot_option)
395 opt = handler->default_boot_option;
397 if (handler->sec_to_boot) {
398 countdown_status(handler, opt, handler->sec_to_boot);
399 handler->sec_to_boot--;
400 handler->timeout_waiter = waiter_register_timeout(
401 handler->waitset, 1000,
402 default_timeout, handler);
406 pb_log("Timeout expired, booting default option %s\n", opt->option->id);
408 boot(handler, handler->default_boot_option, NULL,
409 handler->dry_run, boot_status, handler);
413 static void set_default(struct device_handler *handler,
414 struct discover_boot_option *opt)
416 if (handler->default_boot_option)
419 if (!handler->autoboot_enabled)
422 handler->default_boot_option = opt;
423 handler->sec_to_boot = config_get()->autoboot_timeout_sec;
425 pb_log("Boot option %s set as default, timeout %u sec.\n",
426 opt->option->id, handler->sec_to_boot);
428 default_timeout(handler);
431 static bool resource_is_resolved(struct resource *res)
433 return !res || res->resolved;
436 /* We only use this in an assert, which will disappear if we're compiling
437 * with NDEBUG, so we need the 'used' attribute for these builds */
438 static bool __attribute__((used)) boot_option_is_resolved(
439 struct discover_boot_option *opt)
441 return resource_is_resolved(opt->boot_image) &&
442 resource_is_resolved(opt->initrd) &&
443 resource_is_resolved(opt->dtb) &&
444 resource_is_resolved(opt->icon);
447 static bool resource_resolve(struct resource *res, const char *name,
448 struct discover_boot_option *opt,
449 struct device_handler *handler)
451 struct parser *parser = opt->source;
453 if (resource_is_resolved(res))
456 pb_log("Attempting to resolve resource %s->%s with parser %s\n",
457 opt->option->id, name, parser->name);
458 parser->resolve_resource(handler, res);
460 return res->resolved;
463 static bool boot_option_resolve(struct discover_boot_option *opt,
464 struct device_handler *handler)
466 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
467 resource_resolve(opt->initrd, "initrd", opt, handler) &&
468 resource_resolve(opt->dtb, "dtb", opt, handler) &&
469 resource_resolve(opt->icon, "icon", opt, handler);
472 static void boot_option_finalise(struct device_handler *handler,
473 struct discover_boot_option *opt)
475 assert(boot_option_is_resolved(opt));
477 /* check that the parsers haven't set any of the final data */
478 assert(!opt->option->boot_image_file);
479 assert(!opt->option->initrd_file);
480 assert(!opt->option->dtb_file);
481 assert(!opt->option->icon_file);
482 assert(!opt->option->device_id);
485 opt->option->boot_image_file = opt->boot_image->url->full;
487 opt->option->initrd_file = opt->initrd->url->full;
489 opt->option->dtb_file = opt->dtb->url->full;
491 opt->option->icon_file = opt->icon->url->full;
493 opt->option->device_id = opt->device->device->id;
495 if (opt->option->is_default)
496 set_default(handler, opt);
499 static void process_boot_option_queue(struct device_handler *handler)
501 struct discover_boot_option *opt, *tmp;
503 list_for_each_entry_safe(&handler->unresolved_boot_options,
506 pb_log("queue: attempting resolution for %s\n",
509 if (!boot_option_resolve(opt, handler))
512 pb_log("\tresolved!\n");
514 list_remove(&opt->list);
515 list_add_tail(&opt->device->boot_options, &opt->list);
516 talloc_steal(opt->device, opt);
517 boot_option_finalise(handler, opt);
518 discover_server_notify_boot_option_add(handler->server,
524 * context_commit - Commit a temporary discovery context to the handler,
525 * and notify the clients about any new options / devices
527 static void context_commit(struct device_handler *handler,
528 struct discover_context *ctx)
530 struct discover_device *dev = ctx->device;
531 struct discover_boot_option *opt, *tmp;
532 unsigned int i, existing_device = 0;
534 /* do we already have this device? */
535 for (i = 0; i < handler->n_devices; i++) {
536 if (ctx->device == handler->devices[i]) {
542 /* if not already present, add the device to the handler's array */
543 if (!existing_device) {
544 handler->n_devices++;
545 handler->devices = talloc_realloc(handler, handler->devices,
546 struct discover_device *, handler->n_devices);
547 handler->devices[handler->n_devices - 1] = dev;
548 talloc_steal(handler, dev);
550 discover_server_notify_device_add(handler->server, dev->device);
552 /* this new device might be able to resolve existing boot
554 pb_log("New device %s, processing queue\n", dev->device->id);
555 process_boot_option_queue(handler);
559 /* move boot options from the context to the device */
560 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
561 list_remove(&opt->list);
563 if (boot_option_resolve(opt, handler)) {
564 pb_log("boot option %s is resolved, "
565 "sending to clients\n",
567 list_add_tail(&dev->boot_options, &opt->list);
568 talloc_steal(dev, opt);
569 boot_option_finalise(handler, opt);
570 discover_server_notify_boot_option_add(handler->server,
573 if (!opt->source->resolve_resource) {
574 pb_log("parser %s gave us an unresolved "
575 "resource (%s), but no way to "
577 opt->source->name, opt->option->id);
580 pb_log("boot option %s is unresolved, "
583 list_add(&handler->unresolved_boot_options,
585 talloc_steal(handler, opt);
591 static int handle_add_udev_event(struct device_handler *handler,
594 struct discover_context *ctx;
595 struct discover_device *dev;
599 /* create our context */
600 ctx = talloc(handler, struct discover_context);
602 list_init(&ctx->boot_options);
604 /* create our top-level device */
605 dev = discover_device_create(handler, ctx, event);
609 /* try to parse UUID and labels */
610 param = event_get_param(ctx->event, "ID_FS_UUID");
612 dev->uuid = talloc_strdup(dev, param);
614 param = event_get_param(ctx->event, "ID_FS_LABEL");
616 dev->label = talloc_strdup(dev, param);
618 rc = mount_device(dev);
624 /* run the parsers. This will populate the ctx's boot_option list. */
625 iterate_parsers(ctx, CONF_METHOD_LOCAL_FILE);
627 /* add discovered stuff to the handler */
628 context_commit(handler, ctx);
635 static int handle_remove_udev_event(struct device_handler *handler,
638 struct discover_device *dev;
640 dev = find_device(handler, event->device);
644 /* remove device from handler device array */
645 device_handler_remove(handler, dev);
650 static int handle_add_user_event(struct device_handler *handler,
653 struct discover_context *ctx;
654 struct discover_device *dev;
657 assert(event->device);
659 ctx = talloc(handler, struct discover_context);
661 list_init(&ctx->boot_options);
663 dev = discover_device_create(handler, ctx, event);
666 rc = parse_user_event(ctx, event);
669 context_commit(handler, ctx);
674 static int handle_remove_user_event(struct device_handler *handler,
677 struct discover_device *dev = find_device(handler, event->device);
682 /* remove device from handler device array */
683 device_handler_remove(handler, dev);
688 static enum conf_method parse_conf_method(const char *str)
691 if (!strcasecmp(str, "dhcp")) {
692 return CONF_METHOD_DHCP;
694 return CONF_METHOD_UNKNOWN;
697 static int handle_conf_user_event(struct device_handler *handler,
700 struct discover_context *ctx;
701 struct discover_device *dev;
702 enum conf_method method;
705 ctx = talloc(handler, struct discover_context);
707 list_init(&ctx->boot_options);
709 val = event_get_param(event, "url");
715 ctx->conf_url = pb_url_parse(ctx, val);
716 if (!ctx->conf_url) {
721 val = event_get_param(event, "method");
727 method = parse_conf_method(val);
728 if (method == CONF_METHOD_UNKNOWN) {
733 dev = discover_device_create(handler, ctx, event);
736 iterate_parsers(ctx, method);
738 context_commit(handler, ctx);
743 typedef int (*event_handler)(struct device_handler *, struct event *);
745 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
746 [EVENT_TYPE_UDEV] = {
747 [EVENT_ACTION_ADD] = handle_add_udev_event,
748 [EVENT_ACTION_REMOVE] = handle_remove_udev_event,
750 [EVENT_TYPE_USER] = {
751 [EVENT_ACTION_ADD] = handle_add_user_event,
752 [EVENT_ACTION_REMOVE] = handle_remove_user_event,
753 [EVENT_ACTION_CONF] = handle_conf_user_event,
757 int device_handler_event(struct device_handler *handler,
760 if (event->type >= EVENT_TYPE_MAX ||
761 event->action >= EVENT_ACTION_MAX ||
762 !handlers[event->type][event->action]) {
763 pb_log("%s unknown type/action: %d/%d\n", __func__,
764 event->type, event->action);
768 return handlers[event->type][event->action](handler, event);
771 static struct discover_boot_option *find_boot_option_by_id(
772 struct device_handler *handler, const char *id)
776 for (i = 0; i < handler->n_devices; i++) {
777 struct discover_device *dev = handler->devices[i];
778 struct discover_boot_option *opt;
780 list_for_each_entry(&dev->boot_options, opt, list)
781 if (!strcmp(opt->option->id, id))
788 void device_handler_boot(struct device_handler *handler,
789 struct boot_command *cmd)
791 struct discover_boot_option *opt;
793 opt = find_boot_option_by_id(handler, cmd->option_id);
795 boot(handler, opt, cmd, handler->dry_run, boot_status, handler);
798 void device_handler_cancel_default(struct device_handler *handler)
800 struct boot_status status;
802 if (handler->timeout_waiter)
803 waiter_remove(handler->timeout_waiter);
805 handler->timeout_waiter = NULL;
806 handler->autoboot_enabled = false;
808 /* we only send status if we had a default boot option queued */
809 if (!handler->default_boot_option)
812 pb_log("Cancelling default boot option\n");
814 handler->default_boot_option = NULL;
816 status.type = BOOT_STATUS_INFO;
817 status.progress = -1;
818 status.detail = NULL;
819 status.message = "Default boot cancelled";
821 discover_server_notify_boot_status(handler->server, &status);