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;
35 unsigned int sec_to_boot;
37 struct discover_boot_option *default_boot_option;
38 struct list unresolved_boot_options;
41 void discover_context_add_boot_option(struct discover_context *ctx,
42 struct discover_boot_option *boot_option)
44 boot_option->source = ctx->parser;
45 list_add_tail(&ctx->boot_options, &boot_option->list);
46 talloc_steal(ctx, boot_option);
50 * device_handler_get_device_count - Get the count of current handler devices.
53 int device_handler_get_device_count(const struct device_handler *handler)
55 return handler->n_devices;
59 * device_handler_get_device - Get a handler device by index.
62 const struct discover_device *device_handler_get_device(
63 const struct device_handler *handler, unsigned int index)
65 if (index >= handler->n_devices) {
66 assert(0 && "bad index");
70 return handler->devices[index];
73 struct discover_boot_option *discover_boot_option_create(
74 struct discover_context *ctx,
75 struct discover_device *device)
77 struct discover_boot_option *opt;
79 opt = talloc_zero(ctx, struct discover_boot_option);
80 opt->option = talloc_zero(opt, struct boot_option);
86 static int device_match_path(struct discover_device *dev, const char *path)
88 return dev->device_path && !strcmp(dev->device_path, path);
91 static int device_match_uuid(struct discover_device *dev, const char *uuid)
93 return dev->uuid && !strcmp(dev->uuid, uuid);
96 static int device_match_label(struct discover_device *dev, const char *label)
98 return dev->label && !strcmp(dev->label, label);
101 static int device_match_id(struct discover_device *dev, const char *id)
103 return !strcmp(dev->device->id, id);
106 static struct discover_device *device_lookup(
107 struct device_handler *device_handler,
108 int (match_fn)(struct discover_device *, const char *),
111 struct discover_device *dev;
117 for (i = 0; i < device_handler->n_devices; i++) {
118 dev = device_handler->devices[i];
120 if (match_fn(dev, str))
127 struct discover_device *device_lookup_by_name(struct device_handler *handler,
130 struct discover_device *dev;
133 if (strncmp(name, "/dev/", strlen("/dev/")))
134 path = talloc_asprintf(NULL, "/dev/%s", name);
136 path = talloc_strdup(NULL, name);
138 dev = device_lookup_by_path(handler, path);
145 struct discover_device *device_lookup_by_path(
146 struct device_handler *device_handler,
149 return device_lookup(device_handler, device_match_path, path);
152 struct discover_device *device_lookup_by_uuid(
153 struct device_handler *device_handler,
156 return device_lookup(device_handler, device_match_uuid, uuid);
159 struct discover_device *device_lookup_by_label(
160 struct device_handler *device_handler,
163 return device_lookup(device_handler, device_match_label, label);
166 struct discover_device *device_lookup_by_id(
167 struct device_handler *device_handler,
170 return device_lookup(device_handler, device_match_id, id);
173 void device_handler_destroy(struct device_handler *handler)
175 talloc_free(handler);
178 #ifdef PETITBOOT_TEST
180 /* we have a simplified interface for petitboot testing, but still want
181 * to keep struct device_handler opaque. */
182 struct device_handler *device_handler_init(
183 struct discover_server *server __attribute__((unused)),
184 struct waitset *waitset __attribute__((unused)),
185 int dry_run __attribute__((unused)))
187 struct device_handler *handler;
189 handler = talloc_zero(NULL, struct device_handler);
190 list_init(&handler->unresolved_boot_options);
195 void device_handler_add_device(struct device_handler *handler,
196 struct discover_device *dev)
198 handler->n_devices++;
199 handler->devices = talloc_realloc(handler, handler->devices,
200 struct discover_device *, handler->n_devices);
201 handler->devices[handler->n_devices - 1] = dev;
206 static int mount_device(struct discover_device *dev)
210 if (!dev->device_path)
213 if (!dev->mount_path)
214 dev->mount_path = join_paths(dev, mount_base(),
217 if (pb_mkdir_recursive(dev->mount_path))
218 pb_log("couldn't create mount directory %s: %s\n",
219 dev->mount_path, strerror(errno));
221 argv[0] = pb_system_apps.mount;
222 argv[1] = dev->device_path;
223 argv[2] = dev->mount_path;
228 if (pb_run_cmd(argv, 1, 0)) {
230 /* Retry mount without ro option. */
232 argv[0] = pb_system_apps.mount;
233 argv[1] = dev->device_path;
234 argv[2] = dev->mount_path;
237 if (pb_run_cmd(argv, 1, 0))
244 pb_rmdir_recursive(mount_base(), dev->mount_path);
248 static int umount_device(struct discover_device *dev)
253 if (!dev->mount_path)
258 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
263 execl(pb_system_apps.umount, pb_system_apps.umount,
264 dev->mount_path, NULL);
268 if (waitpid(pid, &status, 0) == -1) {
269 pb_log("%s: waitpid failed: %s\n", __func__,
274 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
277 pb_rmdir_recursive(mount_base(), dev->mount_path);
282 struct device_handler *device_handler_init(struct discover_server *server,
283 struct waitset *waitset, int dry_run)
285 struct device_handler *handler;
287 handler = talloc(NULL, struct device_handler);
288 handler->devices = NULL;
289 handler->n_devices = 0;
290 handler->server = server;
291 handler->waitset = waitset;
292 handler->dry_run = dry_run;
293 handler->default_boot_option = NULL;
294 list_init(&handler->unresolved_boot_options);
296 /* set up our mount point base */
297 pb_mkdir_recursive(mount_base());
304 static int destroy_device(void *arg)
306 struct discover_device *dev = arg;
313 static struct discover_device *find_device(struct device_handler *handler,
316 struct discover_device *dev;
319 for (i = 0; i < handler->n_devices; i++) {
320 dev = handler->devices[i];
321 if (!strcmp(dev->device->id, id))
328 static struct discover_device *discover_device_create(
329 struct device_handler *handler,
330 struct discover_context *ctx,
333 struct discover_device *dev;
336 dev = find_device(handler, event->device);
340 dev = talloc_zero(ctx, struct discover_device);
341 dev->device = talloc_zero(dev, struct device);
342 list_init(&dev->boot_options);
344 devname = event_get_param(ctx->event, "DEVNAME");
346 dev->device_path = talloc_strdup(dev, devname);
348 dev->device->id = talloc_strdup(dev, event->device);
350 talloc_set_destructor(dev, destroy_device);
356 * device_handler_remove - Remove a device from the handler device array.
359 static void device_handler_remove(struct device_handler *handler,
360 struct discover_device *device)
364 for (i = 0; i < handler->n_devices; i++)
365 if (handler->devices[i] == device)
368 if (i == handler->n_devices) {
369 assert(0 && "unknown device");
373 handler->n_devices--;
374 memmove(&handler->devices[i], &handler->devices[i + 1],
375 (handler->n_devices - i) * sizeof(handler->devices[0]));
376 handler->devices = talloc_realloc(handler, handler->devices,
377 struct discover_device *, handler->n_devices);
379 discover_server_notify_device_remove(handler->server, device->device);
384 static void boot_status(void *arg, struct boot_status *status)
386 struct device_handler *handler = arg;
388 discover_server_notify_boot_status(handler->server, status);
391 static void countdown_status(struct device_handler *handler,
392 struct discover_boot_option *opt, unsigned int sec)
394 struct boot_status status;
396 status.type = BOOT_STATUS_INFO;
397 status.progress = -1;
398 status.detail = NULL;
399 status.message = talloc_asprintf(handler,
400 "Booting %s in %d sec", opt->option->name, sec);
402 discover_server_notify_boot_status(handler->server, &status);
404 talloc_free(status.message);
407 static int default_timeout(void *arg)
409 struct device_handler *handler = arg;
410 struct discover_boot_option *opt;
412 if (!handler->default_boot_option)
415 opt = handler->default_boot_option;
417 if (handler->sec_to_boot) {
418 countdown_status(handler, opt, handler->sec_to_boot);
419 handler->sec_to_boot--;
420 handler->timeout_waiter = waiter_register_timeout(
421 handler->waitset, 1000,
422 default_timeout, handler);
426 boot(handler, handler->default_boot_option, NULL,
427 handler->dry_run, boot_status, handler);
431 static void set_default(struct device_handler *handler,
432 struct discover_boot_option *opt)
434 if (handler->default_boot_option)
437 handler->default_boot_option = opt;
438 handler->sec_to_boot = DEFAULT_BOOT_TIMEOUT_SEC;
439 default_timeout(handler);
442 static bool resource_is_resolved(struct resource *res)
444 return !res || res->resolved;
447 /* We only use this in an assert, which will disappear if we're compiling
448 * with NDEBUG, so we need the 'used' attribute for these builds */
449 static bool __attribute__((used)) boot_option_is_resolved(
450 struct discover_boot_option *opt)
452 return resource_is_resolved(opt->boot_image) &&
453 resource_is_resolved(opt->initrd) &&
454 resource_is_resolved(opt->icon);
457 static bool resource_resolve(struct resource *res, const char *name,
458 struct discover_boot_option *opt,
459 struct device_handler *handler)
461 struct parser *parser = opt->source;
463 if (resource_is_resolved(res))
466 pb_log("Attempting to resolve resource %s->%s with parser %s\n",
467 opt->option->id, name, parser->name);
468 parser->resolve_resource(handler, res);
470 return res->resolved;
473 static bool boot_option_resolve(struct discover_boot_option *opt,
474 struct device_handler *handler)
476 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
477 resource_resolve(opt->initrd, "initrd", opt, handler) &&
478 resource_resolve(opt->icon, "icon", opt, handler);
481 static void boot_option_finalise(struct device_handler *handler,
482 struct discover_boot_option *opt)
484 assert(boot_option_is_resolved(opt));
486 /* check that the parsers haven't set any of the final data */
487 assert(!opt->option->boot_image_file);
488 assert(!opt->option->initrd_file);
489 assert(!opt->option->icon_file);
490 assert(!opt->option->device_id);
493 opt->option->boot_image_file = opt->boot_image->url->full;
495 opt->option->initrd_file = opt->initrd->url->full;
497 opt->option->icon_file = opt->icon->url->full;
499 opt->option->device_id = opt->device->device->id;
501 if (opt->option->is_default)
502 set_default(handler, opt);
505 static void process_boot_option_queue(struct device_handler *handler)
507 struct discover_boot_option *opt, *tmp;
509 list_for_each_entry_safe(&handler->unresolved_boot_options,
512 pb_log("queue: attempting resolution for %s\n",
515 if (!boot_option_resolve(opt, handler))
518 pb_log("\tresolved!\n");
520 list_remove(&opt->list);
521 list_add_tail(&opt->device->boot_options, &opt->list);
522 talloc_steal(opt->device, opt);
523 boot_option_finalise(handler, opt);
524 discover_server_notify_boot_option_add(handler->server,
530 * context_commit - Commit a temporary discovery context to the handler,
531 * and notify the clients about any new options / devices
533 static void context_commit(struct device_handler *handler,
534 struct discover_context *ctx)
536 struct discover_device *dev = ctx->device;
537 struct discover_boot_option *opt, *tmp;
538 unsigned int i, existing_device = 0;
540 /* do we already have this device? */
541 for (i = 0; i < handler->n_devices; i++) {
542 if (ctx->device == handler->devices[i]) {
548 /* if not already present, add the device to the handler's array */
549 if (!existing_device) {
550 handler->n_devices++;
551 handler->devices = talloc_realloc(handler, handler->devices,
552 struct discover_device *, handler->n_devices);
553 handler->devices[handler->n_devices - 1] = dev;
554 talloc_steal(handler, dev);
556 discover_server_notify_device_add(handler->server, dev->device);
558 /* this new device might be able to resolve existing boot
560 pb_log("New device %s, processing queue\n", dev->device->id);
561 process_boot_option_queue(handler);
565 /* move boot options from the context to the device */
566 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
567 list_remove(&opt->list);
569 if (boot_option_resolve(opt, handler)) {
570 pb_log("boot option %s is resolved, "
571 "sending to clients\n",
573 list_add_tail(&dev->boot_options, &opt->list);
574 talloc_steal(dev, opt);
575 boot_option_finalise(handler, opt);
576 discover_server_notify_boot_option_add(handler->server,
579 if (!opt->source->resolve_resource) {
580 pb_log("parser %s gave us an unresolved "
581 "resource (%s), but no way to "
583 opt->source->name, opt->option->id);
586 pb_log("boot option %s is unresolved, "
589 list_add(&handler->unresolved_boot_options,
591 talloc_steal(handler, opt);
597 static int handle_add_udev_event(struct device_handler *handler,
600 struct discover_context *ctx;
601 struct discover_device *dev;
605 /* create our context */
606 ctx = talloc(handler, struct discover_context);
608 list_init(&ctx->boot_options);
610 /* create our top-level device */
611 dev = discover_device_create(handler, ctx, event);
615 /* try to parse UUID and labels */
616 param = event_get_param(ctx->event, "ID_FS_UUID");
618 dev->uuid = talloc_strdup(dev, param);
620 param = event_get_param(ctx->event, "ID_FS_LABEL");
622 dev->label = talloc_strdup(dev, param);
624 rc = mount_device(dev);
630 /* run the parsers. This will populate the ctx's boot_option list. */
631 iterate_parsers(ctx, CONF_METHOD_LOCAL_FILE);
633 /* add discovered stuff to the handler */
634 context_commit(handler, ctx);
641 static int handle_remove_udev_event(struct device_handler *handler,
644 struct discover_device *dev;
646 dev = find_device(handler, event->device);
650 /* remove device from handler device array */
651 device_handler_remove(handler, dev);
656 static int handle_add_user_event(struct device_handler *handler,
659 struct discover_context *ctx;
660 struct discover_device *dev;
663 assert(event->device);
665 ctx = talloc(handler, struct discover_context);
667 list_init(&ctx->boot_options);
669 dev = discover_device_create(handler, ctx, event);
672 rc = parse_user_event(ctx, event);
675 context_commit(handler, ctx);
680 static int handle_remove_user_event(struct device_handler *handler,
683 struct discover_device *dev = find_device(handler, event->device);
688 /* remove device from handler device array */
689 device_handler_remove(handler, dev);
694 static enum conf_method parse_conf_method(const char *str)
697 if (!strcasecmp(str, "dhcp")) {
698 return CONF_METHOD_DHCP;
700 return CONF_METHOD_UNKNOWN;
703 static int handle_conf_user_event(struct device_handler *handler,
706 struct discover_context *ctx;
707 struct discover_device *dev;
708 enum conf_method method;
711 ctx = talloc(handler, struct discover_context);
713 list_init(&ctx->boot_options);
715 val = event_get_param(event, "url");
721 ctx->conf_url = pb_url_parse(ctx, val);
722 if (!ctx->conf_url) {
727 val = event_get_param(event, "method");
733 method = parse_conf_method(val);
734 if (method == CONF_METHOD_UNKNOWN) {
739 dev = discover_device_create(handler, ctx, event);
742 iterate_parsers(ctx, method);
744 context_commit(handler, ctx);
749 typedef int (*event_handler)(struct device_handler *, struct event *);
751 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
752 [EVENT_TYPE_UDEV] = {
753 [EVENT_ACTION_ADD] = handle_add_udev_event,
754 [EVENT_ACTION_REMOVE] = handle_remove_udev_event,
756 [EVENT_TYPE_USER] = {
757 [EVENT_ACTION_ADD] = handle_add_user_event,
758 [EVENT_ACTION_REMOVE] = handle_remove_user_event,
759 [EVENT_ACTION_CONF] = handle_conf_user_event,
763 int device_handler_event(struct device_handler *handler,
766 if (event->type >= EVENT_TYPE_MAX ||
767 event->action >= EVENT_ACTION_MAX ||
768 !handlers[event->type][event->action]) {
769 pb_log("%s unknown type/action: %d/%d\n", __func__,
770 event->type, event->action);
774 return handlers[event->type][event->action](handler, event);
777 static struct discover_boot_option *find_boot_option_by_id(
778 struct device_handler *handler, const char *id)
782 for (i = 0; i < handler->n_devices; i++) {
783 struct discover_device *dev = handler->devices[i];
784 struct discover_boot_option *opt;
786 list_for_each_entry(&dev->boot_options, opt, list)
787 if (!strcmp(opt->option->id, id))
794 void device_handler_boot(struct device_handler *handler,
795 struct boot_command *cmd)
797 struct discover_boot_option *opt;
799 opt = find_boot_option_by_id(handler, cmd->option_id);
801 boot(handler, opt, cmd, handler->dry_run, boot_status, handler);