10 #include <talloc/talloc.h>
11 #include <list/list.h>
13 #include <types/types.h>
14 #include <system/system.h>
17 #include "device-handler.h"
18 #include "discover-server.h"
26 struct device_handler {
27 struct discover_server *server;
30 struct discover_device **devices;
31 unsigned int n_devices;
33 struct waitset *waitset;
34 struct waiter *timeout_waiter;
36 unsigned int sec_to_boot;
38 struct discover_boot_option *default_boot_option;
39 struct list unresolved_boot_options;
42 void discover_context_add_boot_option(struct discover_context *ctx,
43 struct discover_boot_option *boot_option)
45 boot_option->source = ctx->parser;
46 list_add_tail(&ctx->boot_options, &boot_option->list);
47 talloc_steal(ctx, boot_option);
51 * device_handler_get_device_count - Get the count of current handler devices.
54 int device_handler_get_device_count(const struct device_handler *handler)
56 return handler->n_devices;
60 * device_handler_get_device - Get a handler device by index.
63 const struct discover_device *device_handler_get_device(
64 const struct device_handler *handler, unsigned int index)
66 if (index >= handler->n_devices) {
67 assert(0 && "bad index");
71 return handler->devices[index];
74 struct discover_boot_option *discover_boot_option_create(
75 struct discover_context *ctx,
76 struct discover_device *device)
78 struct discover_boot_option *opt;
80 opt = talloc_zero(ctx, struct discover_boot_option);
81 opt->option = talloc_zero(opt, struct boot_option);
87 static int device_match_path(struct discover_device *dev, const char *path)
89 return dev->device_path && !strcmp(dev->device_path, path);
92 static int device_match_uuid(struct discover_device *dev, const char *uuid)
94 return dev->uuid && !strcmp(dev->uuid, uuid);
97 static int device_match_label(struct discover_device *dev, const char *label)
99 return dev->label && !strcmp(dev->label, label);
102 static int device_match_id(struct discover_device *dev, const char *id)
104 return !strcmp(dev->device->id, id);
107 static struct discover_device *device_lookup(
108 struct device_handler *device_handler,
109 int (match_fn)(struct discover_device *, const char *),
112 struct discover_device *dev;
118 for (i = 0; i < device_handler->n_devices; i++) {
119 dev = device_handler->devices[i];
121 if (match_fn(dev, str))
128 struct discover_device *device_lookup_by_name(struct device_handler *handler,
131 struct discover_device *dev;
134 if (strncmp(name, "/dev/", strlen("/dev/")))
135 path = talloc_asprintf(NULL, "/dev/%s", name);
137 path = talloc_strdup(NULL, name);
139 dev = device_lookup_by_path(handler, path);
146 struct discover_device *device_lookup_by_path(
147 struct device_handler *device_handler,
150 return device_lookup(device_handler, device_match_path, path);
153 struct discover_device *device_lookup_by_uuid(
154 struct device_handler *device_handler,
157 return device_lookup(device_handler, device_match_uuid, uuid);
160 struct discover_device *device_lookup_by_label(
161 struct device_handler *device_handler,
164 return device_lookup(device_handler, device_match_label, label);
167 struct discover_device *device_lookup_by_id(
168 struct device_handler *device_handler,
171 return device_lookup(device_handler, device_match_id, id);
174 void device_handler_destroy(struct device_handler *handler)
176 talloc_free(handler);
179 #ifdef PETITBOOT_TEST
181 /* we have a simplified interface for petitboot testing, but still want
182 * to keep struct device_handler opaque. */
183 struct device_handler *device_handler_init(
184 struct discover_server *server __attribute__((unused)),
185 struct waitset *waitset __attribute__((unused)),
186 int dry_run __attribute__((unused)))
188 struct device_handler *handler;
190 handler = talloc_zero(NULL, struct device_handler);
191 list_init(&handler->unresolved_boot_options);
196 void device_handler_add_device(struct device_handler *handler,
197 struct discover_device *dev)
199 handler->n_devices++;
200 handler->devices = talloc_realloc(handler, handler->devices,
201 struct discover_device *, handler->n_devices);
202 handler->devices[handler->n_devices - 1] = dev;
207 static int mount_device(struct discover_device *dev)
211 if (!dev->device_path)
214 if (!dev->mount_path)
215 dev->mount_path = join_paths(dev, mount_base(),
218 if (pb_mkdir_recursive(dev->mount_path))
219 pb_log("couldn't create mount directory %s: %s\n",
220 dev->mount_path, strerror(errno));
222 argv[0] = pb_system_apps.mount;
223 argv[1] = dev->device_path;
224 argv[2] = dev->mount_path;
229 if (pb_run_cmd(argv, 1, 0)) {
231 /* Retry mount without ro option. */
233 argv[0] = pb_system_apps.mount;
234 argv[1] = dev->device_path;
235 argv[2] = dev->mount_path;
238 if (pb_run_cmd(argv, 1, 0))
245 pb_rmdir_recursive(mount_base(), dev->mount_path);
249 static int umount_device(struct discover_device *dev)
254 if (!dev->mount_path)
259 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
264 execl(pb_system_apps.umount, pb_system_apps.umount,
265 dev->mount_path, NULL);
269 if (waitpid(pid, &status, 0) == -1) {
270 pb_log("%s: waitpid failed: %s\n", __func__,
275 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
278 pb_rmdir_recursive(mount_base(), dev->mount_path);
283 struct device_handler *device_handler_init(struct discover_server *server,
284 struct waitset *waitset, int dry_run)
286 struct device_handler *handler;
288 handler = talloc(NULL, struct device_handler);
289 handler->devices = NULL;
290 handler->n_devices = 0;
291 handler->server = server;
292 handler->waitset = waitset;
293 handler->dry_run = dry_run;
294 handler->default_boot_option = NULL;
295 handler->default_enabled = true;
296 list_init(&handler->unresolved_boot_options);
298 /* set up our mount point base */
299 pb_mkdir_recursive(mount_base());
306 static int destroy_device(void *arg)
308 struct discover_device *dev = arg;
315 static struct discover_device *find_device(struct device_handler *handler,
318 struct discover_device *dev;
321 for (i = 0; i < handler->n_devices; i++) {
322 dev = handler->devices[i];
323 if (!strcmp(dev->device->id, id))
330 static struct discover_device *discover_device_create(
331 struct device_handler *handler,
332 struct discover_context *ctx,
335 struct discover_device *dev;
338 dev = find_device(handler, event->device);
342 dev = talloc_zero(ctx, struct discover_device);
343 dev->device = talloc_zero(dev, struct device);
344 list_init(&dev->boot_options);
346 devname = event_get_param(ctx->event, "DEVNAME");
348 dev->device_path = talloc_strdup(dev, devname);
350 dev->device->id = talloc_strdup(dev, event->device);
352 talloc_set_destructor(dev, destroy_device);
358 * device_handler_remove - Remove a device from the handler device array.
361 static void device_handler_remove(struct device_handler *handler,
362 struct discover_device *device)
366 for (i = 0; i < handler->n_devices; i++)
367 if (handler->devices[i] == device)
370 if (i == handler->n_devices) {
371 assert(0 && "unknown device");
375 handler->n_devices--;
376 memmove(&handler->devices[i], &handler->devices[i + 1],
377 (handler->n_devices - i) * sizeof(handler->devices[0]));
378 handler->devices = talloc_realloc(handler, handler->devices,
379 struct discover_device *, handler->n_devices);
381 discover_server_notify_device_remove(handler->server, device->device);
386 static void boot_status(void *arg, struct boot_status *status)
388 struct device_handler *handler = arg;
390 discover_server_notify_boot_status(handler->server, status);
393 static void countdown_status(struct device_handler *handler,
394 struct discover_boot_option *opt, unsigned int sec)
396 struct boot_status status;
398 status.type = BOOT_STATUS_INFO;
399 status.progress = -1;
400 status.detail = NULL;
401 status.message = talloc_asprintf(handler,
402 "Booting %s in %d sec", opt->option->name, sec);
404 discover_server_notify_boot_status(handler->server, &status);
406 talloc_free(status.message);
409 static int default_timeout(void *arg)
411 struct device_handler *handler = arg;
412 struct discover_boot_option *opt;
414 if (!handler->default_boot_option)
417 opt = handler->default_boot_option;
419 if (handler->sec_to_boot) {
420 countdown_status(handler, opt, handler->sec_to_boot);
421 handler->sec_to_boot--;
422 handler->timeout_waiter = waiter_register_timeout(
423 handler->waitset, 1000,
424 default_timeout, handler);
428 pb_log("Timeout expired, booting default option %s\n", opt->option->id);
430 boot(handler, handler->default_boot_option, NULL,
431 handler->dry_run, boot_status, handler);
435 static void set_default(struct device_handler *handler,
436 struct discover_boot_option *opt)
438 if (handler->default_boot_option)
441 if (!handler->default_enabled)
444 pb_log("Boot option %s set as default\n", opt->option->id);
446 handler->default_boot_option = opt;
447 handler->sec_to_boot = DEFAULT_BOOT_TIMEOUT_SEC;
448 default_timeout(handler);
451 static bool resource_is_resolved(struct resource *res)
453 return !res || res->resolved;
456 /* We only use this in an assert, which will disappear if we're compiling
457 * with NDEBUG, so we need the 'used' attribute for these builds */
458 static bool __attribute__((used)) boot_option_is_resolved(
459 struct discover_boot_option *opt)
461 return resource_is_resolved(opt->boot_image) &&
462 resource_is_resolved(opt->initrd) &&
463 resource_is_resolved(opt->icon);
466 static bool resource_resolve(struct resource *res, const char *name,
467 struct discover_boot_option *opt,
468 struct device_handler *handler)
470 struct parser *parser = opt->source;
472 if (resource_is_resolved(res))
475 pb_log("Attempting to resolve resource %s->%s with parser %s\n",
476 opt->option->id, name, parser->name);
477 parser->resolve_resource(handler, res);
479 return res->resolved;
482 static bool boot_option_resolve(struct discover_boot_option *opt,
483 struct device_handler *handler)
485 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
486 resource_resolve(opt->initrd, "initrd", opt, handler) &&
487 resource_resolve(opt->icon, "icon", opt, handler);
490 static void boot_option_finalise(struct device_handler *handler,
491 struct discover_boot_option *opt)
493 assert(boot_option_is_resolved(opt));
495 /* check that the parsers haven't set any of the final data */
496 assert(!opt->option->boot_image_file);
497 assert(!opt->option->initrd_file);
498 assert(!opt->option->icon_file);
499 assert(!opt->option->device_id);
502 opt->option->boot_image_file = opt->boot_image->url->full;
504 opt->option->initrd_file = opt->initrd->url->full;
506 opt->option->icon_file = opt->icon->url->full;
508 opt->option->device_id = opt->device->device->id;
510 if (opt->option->is_default)
511 set_default(handler, opt);
514 static void process_boot_option_queue(struct device_handler *handler)
516 struct discover_boot_option *opt, *tmp;
518 list_for_each_entry_safe(&handler->unresolved_boot_options,
521 pb_log("queue: attempting resolution for %s\n",
524 if (!boot_option_resolve(opt, handler))
527 pb_log("\tresolved!\n");
529 list_remove(&opt->list);
530 list_add_tail(&opt->device->boot_options, &opt->list);
531 talloc_steal(opt->device, opt);
532 boot_option_finalise(handler, opt);
533 discover_server_notify_boot_option_add(handler->server,
539 * context_commit - Commit a temporary discovery context to the handler,
540 * and notify the clients about any new options / devices
542 static void context_commit(struct device_handler *handler,
543 struct discover_context *ctx)
545 struct discover_device *dev = ctx->device;
546 struct discover_boot_option *opt, *tmp;
547 unsigned int i, existing_device = 0;
549 /* do we already have this device? */
550 for (i = 0; i < handler->n_devices; i++) {
551 if (ctx->device == handler->devices[i]) {
557 /* if not already present, add the device to the handler's array */
558 if (!existing_device) {
559 handler->n_devices++;
560 handler->devices = talloc_realloc(handler, handler->devices,
561 struct discover_device *, handler->n_devices);
562 handler->devices[handler->n_devices - 1] = dev;
563 talloc_steal(handler, dev);
565 discover_server_notify_device_add(handler->server, dev->device);
567 /* this new device might be able to resolve existing boot
569 pb_log("New device %s, processing queue\n", dev->device->id);
570 process_boot_option_queue(handler);
574 /* move boot options from the context to the device */
575 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
576 list_remove(&opt->list);
578 if (boot_option_resolve(opt, handler)) {
579 pb_log("boot option %s is resolved, "
580 "sending to clients\n",
582 list_add_tail(&dev->boot_options, &opt->list);
583 talloc_steal(dev, opt);
584 boot_option_finalise(handler, opt);
585 discover_server_notify_boot_option_add(handler->server,
588 if (!opt->source->resolve_resource) {
589 pb_log("parser %s gave us an unresolved "
590 "resource (%s), but no way to "
592 opt->source->name, opt->option->id);
595 pb_log("boot option %s is unresolved, "
598 list_add(&handler->unresolved_boot_options,
600 talloc_steal(handler, opt);
606 static int handle_add_udev_event(struct device_handler *handler,
609 struct discover_context *ctx;
610 struct discover_device *dev;
614 /* create our context */
615 ctx = talloc(handler, struct discover_context);
617 list_init(&ctx->boot_options);
619 /* create our top-level device */
620 dev = discover_device_create(handler, ctx, event);
624 /* try to parse UUID and labels */
625 param = event_get_param(ctx->event, "ID_FS_UUID");
627 dev->uuid = talloc_strdup(dev, param);
629 param = event_get_param(ctx->event, "ID_FS_LABEL");
631 dev->label = talloc_strdup(dev, param);
633 rc = mount_device(dev);
639 /* run the parsers. This will populate the ctx's boot_option list. */
640 iterate_parsers(ctx, CONF_METHOD_LOCAL_FILE);
642 /* add discovered stuff to the handler */
643 context_commit(handler, ctx);
650 static int handle_remove_udev_event(struct device_handler *handler,
653 struct discover_device *dev;
655 dev = find_device(handler, event->device);
659 /* remove device from handler device array */
660 device_handler_remove(handler, dev);
665 static int handle_add_user_event(struct device_handler *handler,
668 struct discover_context *ctx;
669 struct discover_device *dev;
672 assert(event->device);
674 ctx = talloc(handler, struct discover_context);
676 list_init(&ctx->boot_options);
678 dev = discover_device_create(handler, ctx, event);
681 rc = parse_user_event(ctx, event);
684 context_commit(handler, ctx);
689 static int handle_remove_user_event(struct device_handler *handler,
692 struct discover_device *dev = find_device(handler, event->device);
697 /* remove device from handler device array */
698 device_handler_remove(handler, dev);
703 static enum conf_method parse_conf_method(const char *str)
706 if (!strcasecmp(str, "dhcp")) {
707 return CONF_METHOD_DHCP;
709 return CONF_METHOD_UNKNOWN;
712 static int handle_conf_user_event(struct device_handler *handler,
715 struct discover_context *ctx;
716 struct discover_device *dev;
717 enum conf_method method;
720 ctx = talloc(handler, struct discover_context);
722 list_init(&ctx->boot_options);
724 val = event_get_param(event, "url");
730 ctx->conf_url = pb_url_parse(ctx, val);
731 if (!ctx->conf_url) {
736 val = event_get_param(event, "method");
742 method = parse_conf_method(val);
743 if (method == CONF_METHOD_UNKNOWN) {
748 dev = discover_device_create(handler, ctx, event);
751 iterate_parsers(ctx, method);
753 context_commit(handler, ctx);
758 typedef int (*event_handler)(struct device_handler *, struct event *);
760 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
761 [EVENT_TYPE_UDEV] = {
762 [EVENT_ACTION_ADD] = handle_add_udev_event,
763 [EVENT_ACTION_REMOVE] = handle_remove_udev_event,
765 [EVENT_TYPE_USER] = {
766 [EVENT_ACTION_ADD] = handle_add_user_event,
767 [EVENT_ACTION_REMOVE] = handle_remove_user_event,
768 [EVENT_ACTION_CONF] = handle_conf_user_event,
772 int device_handler_event(struct device_handler *handler,
775 if (event->type >= EVENT_TYPE_MAX ||
776 event->action >= EVENT_ACTION_MAX ||
777 !handlers[event->type][event->action]) {
778 pb_log("%s unknown type/action: %d/%d\n", __func__,
779 event->type, event->action);
783 return handlers[event->type][event->action](handler, event);
786 static struct discover_boot_option *find_boot_option_by_id(
787 struct device_handler *handler, const char *id)
791 for (i = 0; i < handler->n_devices; i++) {
792 struct discover_device *dev = handler->devices[i];
793 struct discover_boot_option *opt;
795 list_for_each_entry(&dev->boot_options, opt, list)
796 if (!strcmp(opt->option->id, id))
803 void device_handler_boot(struct device_handler *handler,
804 struct boot_command *cmd)
806 struct discover_boot_option *opt;
808 opt = find_boot_option_by_id(handler, cmd->option_id);
810 boot(handler, opt, cmd, handler->dry_run, boot_status, handler);
813 void device_handler_cancel_default(struct device_handler *handler)
815 struct boot_status status;
817 if (handler->timeout_waiter)
818 waiter_remove(handler->timeout_waiter);
820 handler->timeout_waiter = NULL;
821 handler->default_enabled = false;
823 /* we only send status if we had a default boot option queued */
824 if (!handler->default_boot_option)
827 pb_log("Cancelling default boot option\n");
829 handler->default_boot_option = NULL;
831 status.type = BOOT_STATUS_INFO;
832 status.progress = -1;
833 status.detail = NULL;
834 status.message = "Default boot cancelled";
836 discover_server_notify_boot_status(handler->server, &status);