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 struct discover_boot_option *default_boot_option;
37 struct list unresolved_boot_options;
40 void discover_context_add_boot_option(struct discover_context *ctx,
41 struct discover_boot_option *boot_option)
43 boot_option->source = ctx->parser;
44 list_add_tail(&ctx->boot_options, &boot_option->list);
45 talloc_steal(ctx, boot_option);
49 * device_handler_get_device_count - Get the count of current handler devices.
52 int device_handler_get_device_count(const struct device_handler *handler)
54 return handler->n_devices;
58 * device_handler_get_device - Get a handler device by index.
61 const struct discover_device *device_handler_get_device(
62 const struct device_handler *handler, unsigned int index)
64 if (index >= handler->n_devices) {
65 assert(0 && "bad index");
69 return handler->devices[index];
72 struct discover_boot_option *discover_boot_option_create(
73 struct discover_context *ctx,
74 struct discover_device *device)
76 struct discover_boot_option *opt;
78 opt = talloc_zero(ctx, struct discover_boot_option);
79 opt->option = talloc_zero(opt, struct boot_option);
85 static int device_match_path(struct discover_device *dev, const char *path)
87 return dev->device_path && !strcmp(dev->device_path, path);
90 static int device_match_uuid(struct discover_device *dev, const char *uuid)
92 return dev->uuid && !strcmp(dev->uuid, uuid);
95 static int device_match_label(struct discover_device *dev, const char *label)
97 return dev->label && !strcmp(dev->label, label);
100 static int device_match_id(struct discover_device *dev, const char *id)
102 return !strcmp(dev->device->id, id);
105 static struct discover_device *device_lookup(
106 struct device_handler *device_handler,
107 int (match_fn)(struct discover_device *, const char *),
110 struct discover_device *dev;
116 for (i = 0; i < device_handler->n_devices; i++) {
117 dev = device_handler->devices[i];
119 if (match_fn(dev, str))
126 struct discover_device *device_lookup_by_name(struct device_handler *handler,
129 struct discover_device *dev;
132 if (strncmp(name, "/dev/", strlen("/dev/")))
133 path = talloc_asprintf(NULL, "/dev/%s", name);
135 path = talloc_strdup(NULL, name);
137 dev = device_lookup_by_path(handler, path);
144 struct discover_device *device_lookup_by_path(
145 struct device_handler *device_handler,
148 return device_lookup(device_handler, device_match_path, path);
151 struct discover_device *device_lookup_by_uuid(
152 struct device_handler *device_handler,
155 return device_lookup(device_handler, device_match_uuid, uuid);
158 struct discover_device *device_lookup_by_label(
159 struct device_handler *device_handler,
162 return device_lookup(device_handler, device_match_label, label);
165 struct discover_device *device_lookup_by_id(
166 struct device_handler *device_handler,
169 return device_lookup(device_handler, device_match_id, id);
172 void device_handler_destroy(struct device_handler *handler)
174 talloc_free(handler);
177 #ifdef PETITBOOT_TEST
179 /* we have a simplified interface for petitboot testing, but still want
180 * to keep struct device_handler opaque. */
181 struct device_handler *device_handler_init(
182 struct discover_server *server __attribute__((unused)),
183 struct waitset *waitset __attribute__((unused)),
184 int dry_run __attribute__((unused)))
186 struct device_handler *handler;
188 handler = talloc_zero(NULL, struct device_handler);
189 list_init(&handler->unresolved_boot_options);
194 void device_handler_add_device(struct device_handler *handler,
195 struct discover_device *dev)
197 handler->n_devices++;
198 handler->devices = talloc_realloc(handler, handler->devices,
199 struct discover_device *, handler->n_devices);
200 handler->devices[handler->n_devices - 1] = dev;
205 static int mount_device(struct discover_device *dev)
209 if (!dev->device_path)
212 if (!dev->mount_path)
213 dev->mount_path = join_paths(dev, mount_base(),
216 if (pb_mkdir_recursive(dev->mount_path))
217 pb_log("couldn't create mount directory %s: %s\n",
218 dev->mount_path, strerror(errno));
220 argv[0] = pb_system_apps.mount;
221 argv[1] = dev->device_path;
222 argv[2] = dev->mount_path;
227 if (pb_run_cmd(argv, 1, 0)) {
229 /* Retry mount without ro option. */
231 argv[0] = pb_system_apps.mount;
232 argv[1] = dev->device_path;
233 argv[2] = dev->mount_path;
236 if (pb_run_cmd(argv, 1, 0))
243 pb_rmdir_recursive(mount_base(), dev->mount_path);
247 static int umount_device(struct discover_device *dev)
252 if (!dev->mount_path)
257 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
262 execl(pb_system_apps.umount, pb_system_apps.umount,
263 dev->mount_path, NULL);
267 if (waitpid(pid, &status, 0) == -1) {
268 pb_log("%s: waitpid failed: %s\n", __func__,
273 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
276 pb_rmdir_recursive(mount_base(), dev->mount_path);
281 struct device_handler *device_handler_init(struct discover_server *server,
282 struct waitset *waitset, int dry_run)
284 struct device_handler *handler;
286 handler = talloc(NULL, struct device_handler);
287 handler->devices = NULL;
288 handler->n_devices = 0;
289 handler->server = server;
290 handler->waitset = waitset;
291 handler->dry_run = dry_run;
292 handler->default_boot_option = NULL;
293 list_init(&handler->unresolved_boot_options);
295 /* set up our mount point base */
296 pb_mkdir_recursive(mount_base());
303 static int destroy_device(void *arg)
305 struct discover_device *dev = arg;
312 static struct discover_device *find_device(struct device_handler *handler,
315 struct discover_device *dev;
318 for (i = 0; i < handler->n_devices; i++) {
319 dev = handler->devices[i];
320 if (!strcmp(dev->device->id, id))
327 static struct discover_device *discover_device_create(
328 struct device_handler *handler,
329 struct discover_context *ctx,
332 struct discover_device *dev;
335 dev = find_device(handler, event->device);
339 dev = talloc_zero(ctx, struct discover_device);
340 dev->device = talloc_zero(dev, struct device);
341 list_init(&dev->boot_options);
343 devname = event_get_param(ctx->event, "DEVNAME");
345 dev->device_path = talloc_strdup(dev, devname);
347 dev->device->id = talloc_strdup(dev, event->device);
349 talloc_set_destructor(dev, destroy_device);
355 * device_handler_remove - Remove a device from the handler device array.
358 static void device_handler_remove(struct device_handler *handler,
359 struct discover_device *device)
363 for (i = 0; i < handler->n_devices; i++)
364 if (handler->devices[i] == device)
367 if (i == handler->n_devices) {
368 assert(0 && "unknown device");
372 handler->n_devices--;
373 memmove(&handler->devices[i], &handler->devices[i + 1],
374 (handler->n_devices - i) * sizeof(handler->devices[0]));
375 handler->devices = talloc_realloc(handler, handler->devices,
376 struct discover_device *, handler->n_devices);
378 discover_server_notify_device_remove(handler->server, device->device);
383 static void boot_status(void *arg, struct boot_status *status)
385 struct device_handler *handler = arg;
387 discover_server_notify_boot_status(handler->server, status);
390 static int default_timeout(void *arg)
392 struct device_handler *handler = arg;
394 if (!handler->default_boot_option)
397 boot(handler, handler->default_boot_option, NULL,
398 handler->dry_run, boot_status, handler);
402 static void set_default(struct device_handler *handler,
403 struct discover_boot_option *opt)
405 if (handler->default_boot_option)
408 handler->default_boot_option = opt;
409 handler->timeout_waiter = waiter_register_timeout(handler->waitset,
410 DEFAULT_BOOT_TIMEOUT_SEC * 1000,
411 default_timeout, handler);
414 static bool resource_is_resolved(struct resource *res)
416 return !res || res->resolved;
419 /* We only use this in an assert, which will disappear if we're compiling
420 * with NDEBUG, so we need the 'used' attribute for these builds */
421 static bool __attribute__((used)) boot_option_is_resolved(
422 struct discover_boot_option *opt)
424 return resource_is_resolved(opt->boot_image) &&
425 resource_is_resolved(opt->initrd) &&
426 resource_is_resolved(opt->icon);
429 static bool resource_resolve(struct resource *res, const char *name,
430 struct discover_boot_option *opt,
431 struct device_handler *handler)
433 struct parser *parser = opt->source;
435 if (resource_is_resolved(res))
438 pb_log("Attempting to resolve resource %s->%s with parser %s\n",
439 opt->option->id, name, parser->name);
440 parser->resolve_resource(handler, res);
442 return res->resolved;
445 static bool boot_option_resolve(struct discover_boot_option *opt,
446 struct device_handler *handler)
448 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
449 resource_resolve(opt->initrd, "initrd", opt, handler) &&
450 resource_resolve(opt->icon, "icon", opt, handler);
453 static void boot_option_finalise(struct device_handler *handler,
454 struct discover_boot_option *opt)
456 assert(boot_option_is_resolved(opt));
458 /* check that the parsers haven't set any of the final data */
459 assert(!opt->option->boot_image_file);
460 assert(!opt->option->initrd_file);
461 assert(!opt->option->icon_file);
462 assert(!opt->option->device_id);
465 opt->option->boot_image_file = opt->boot_image->url->full;
467 opt->option->initrd_file = opt->initrd->url->full;
469 opt->option->icon_file = opt->icon->url->full;
471 opt->option->device_id = opt->device->device->id;
473 if (opt->option->is_default)
474 set_default(handler, opt);
477 static void process_boot_option_queue(struct device_handler *handler)
479 struct discover_boot_option *opt, *tmp;
481 list_for_each_entry_safe(&handler->unresolved_boot_options,
484 pb_log("queue: attempting resolution for %s\n",
487 if (!boot_option_resolve(opt, handler))
490 pb_log("\tresolved!\n");
492 list_remove(&opt->list);
493 list_add_tail(&opt->device->boot_options, &opt->list);
494 talloc_steal(opt->device, opt);
495 boot_option_finalise(handler, opt);
496 discover_server_notify_boot_option_add(handler->server,
502 * context_commit - Commit a temporary discovery context to the handler,
503 * and notify the clients about any new options / devices
505 static void context_commit(struct device_handler *handler,
506 struct discover_context *ctx)
508 struct discover_device *dev = ctx->device;
509 struct discover_boot_option *opt, *tmp;
510 unsigned int i, existing_device = 0;
512 /* do we already have this device? */
513 for (i = 0; i < handler->n_devices; i++) {
514 if (ctx->device == handler->devices[i]) {
520 /* if not already present, add the device to the handler's array */
521 if (!existing_device) {
522 handler->n_devices++;
523 handler->devices = talloc_realloc(handler, handler->devices,
524 struct discover_device *, handler->n_devices);
525 handler->devices[handler->n_devices - 1] = dev;
526 talloc_steal(handler, dev);
528 discover_server_notify_device_add(handler->server, dev->device);
530 /* this new device might be able to resolve existing boot
532 pb_log("New device %s, processing queue\n", dev->device->id);
533 process_boot_option_queue(handler);
537 /* move boot options from the context to the device */
538 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
539 list_remove(&opt->list);
541 if (boot_option_resolve(opt, handler)) {
542 pb_log("boot option %s is resolved, "
543 "sending to clients\n",
545 list_add_tail(&dev->boot_options, &opt->list);
546 talloc_steal(dev, opt);
547 boot_option_finalise(handler, opt);
548 discover_server_notify_boot_option_add(handler->server,
551 if (!opt->source->resolve_resource) {
552 pb_log("parser %s gave us an unresolved "
553 "resource (%s), but no way to "
555 opt->source->name, opt->option->id);
558 pb_log("boot option %s is unresolved, "
561 list_add(&handler->unresolved_boot_options,
563 talloc_steal(handler, opt);
569 static int handle_add_udev_event(struct device_handler *handler,
572 struct discover_context *ctx;
573 struct discover_device *dev;
577 /* create our context */
578 ctx = talloc(handler, struct discover_context);
580 list_init(&ctx->boot_options);
582 /* create our top-level device */
583 dev = discover_device_create(handler, ctx, event);
587 /* try to parse UUID and labels */
588 param = event_get_param(ctx->event, "ID_FS_UUID");
590 dev->uuid = talloc_strdup(dev, param);
592 param = event_get_param(ctx->event, "ID_FS_LABEL");
594 dev->label = talloc_strdup(dev, param);
596 rc = mount_device(dev);
602 /* run the parsers. This will populate the ctx's boot_option list. */
603 iterate_parsers(ctx, CONF_METHOD_LOCAL_FILE);
605 /* add discovered stuff to the handler */
606 context_commit(handler, ctx);
613 static int handle_remove_udev_event(struct device_handler *handler,
616 struct discover_device *dev;
618 dev = find_device(handler, event->device);
622 /* remove device from handler device array */
623 device_handler_remove(handler, dev);
628 static int handle_add_user_event(struct device_handler *handler,
631 struct discover_context *ctx;
632 struct discover_device *dev;
635 assert(event->device);
637 ctx = talloc(handler, struct discover_context);
639 list_init(&ctx->boot_options);
641 dev = discover_device_create(handler, ctx, event);
644 rc = parse_user_event(ctx, event);
647 context_commit(handler, ctx);
652 static int handle_remove_user_event(struct device_handler *handler,
655 struct discover_device *dev = find_device(handler, event->device);
660 /* remove device from handler device array */
661 device_handler_remove(handler, dev);
666 static enum conf_method parse_conf_method(const char *str)
669 if (!strcasecmp(str, "dhcp")) {
670 return CONF_METHOD_DHCP;
672 return CONF_METHOD_UNKNOWN;
675 static int handle_conf_user_event(struct device_handler *handler,
678 struct discover_context *ctx;
679 struct discover_device *dev;
680 enum conf_method method;
683 ctx = talloc(handler, struct discover_context);
685 list_init(&ctx->boot_options);
687 val = event_get_param(event, "url");
693 ctx->conf_url = pb_url_parse(ctx, val);
694 if (!ctx->conf_url) {
699 val = event_get_param(event, "method");
705 method = parse_conf_method(val);
706 if (method == CONF_METHOD_UNKNOWN) {
711 dev = discover_device_create(handler, ctx, event);
714 iterate_parsers(ctx, method);
716 context_commit(handler, ctx);
721 typedef int (*event_handler)(struct device_handler *, struct event *);
723 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
724 [EVENT_TYPE_UDEV] = {
725 [EVENT_ACTION_ADD] = handle_add_udev_event,
726 [EVENT_ACTION_REMOVE] = handle_remove_udev_event,
728 [EVENT_TYPE_USER] = {
729 [EVENT_ACTION_ADD] = handle_add_user_event,
730 [EVENT_ACTION_REMOVE] = handle_remove_user_event,
731 [EVENT_ACTION_CONF] = handle_conf_user_event,
735 int device_handler_event(struct device_handler *handler,
738 if (event->type >= EVENT_TYPE_MAX ||
739 event->action >= EVENT_ACTION_MAX ||
740 !handlers[event->type][event->action]) {
741 pb_log("%s unknown type/action: %d/%d\n", __func__,
742 event->type, event->action);
746 return handlers[event->type][event->action](handler, event);
749 static struct discover_boot_option *find_boot_option_by_id(
750 struct device_handler *handler, const char *id)
754 for (i = 0; i < handler->n_devices; i++) {
755 struct discover_device *dev = handler->devices[i];
756 struct discover_boot_option *opt;
758 list_for_each_entry(&dev->boot_options, opt, list)
759 if (!strcmp(opt->option->id, id))
766 void device_handler_boot(struct device_handler *handler,
767 struct boot_command *cmd)
769 struct discover_boot_option *opt;
771 opt = find_boot_option_by_id(handler, cmd->option_id);
773 boot(handler, opt, cmd, handler->dry_run, boot_status, handler);