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 list unresolved_boot_options;
36 void discover_context_add_boot_option(struct discover_context *ctx,
37 struct discover_boot_option *boot_option)
39 boot_option->source = ctx->parser;
40 list_add_tail(&ctx->boot_options, &boot_option->list);
41 talloc_steal(ctx, boot_option);
45 * device_handler_get_device_count - Get the count of current handler devices.
48 int device_handler_get_device_count(const struct device_handler *handler)
50 return handler->n_devices;
54 * device_handler_get_device - Get a handler device by index.
57 const struct discover_device *device_handler_get_device(
58 const struct device_handler *handler, unsigned int index)
60 if (index >= handler->n_devices) {
61 assert(0 && "bad index");
65 return handler->devices[index];
68 struct discover_boot_option *discover_boot_option_create(
69 struct discover_context *ctx,
70 struct discover_device *device)
72 struct discover_boot_option *opt;
74 opt = talloc_zero(ctx, struct discover_boot_option);
75 opt->option = talloc_zero(opt, struct boot_option);
81 static int device_match_path(struct discover_device *dev, const char *path)
83 return dev->device_path && !strcmp(dev->device_path, path);
86 static int device_match_uuid(struct discover_device *dev, const char *uuid)
88 return dev->uuid && !strcmp(dev->uuid, uuid);
91 static int device_match_label(struct discover_device *dev, const char *label)
93 return dev->label && !strcmp(dev->label, label);
96 static int device_match_id(struct discover_device *dev, const char *id)
98 return !strcmp(dev->device->id, id);
101 static struct discover_device *device_lookup(
102 struct device_handler *device_handler,
103 int (match_fn)(struct discover_device *, const char *),
106 struct discover_device *dev;
112 for (i = 0; i < device_handler->n_devices; i++) {
113 dev = device_handler->devices[i];
115 if (match_fn(dev, str))
122 struct discover_device *device_lookup_by_name(struct device_handler *handler,
125 struct discover_device *dev;
128 if (strncmp(name, "/dev/", strlen("/dev/")))
129 path = talloc_asprintf(NULL, "/dev/%s", name);
131 path = talloc_strdup(NULL, name);
133 dev = device_lookup_by_path(handler, path);
140 struct discover_device *device_lookup_by_path(
141 struct device_handler *device_handler,
144 return device_lookup(device_handler, device_match_path, path);
147 struct discover_device *device_lookup_by_uuid(
148 struct device_handler *device_handler,
151 return device_lookup(device_handler, device_match_uuid, uuid);
154 struct discover_device *device_lookup_by_label(
155 struct device_handler *device_handler,
158 return device_lookup(device_handler, device_match_label, label);
161 struct discover_device *device_lookup_by_id(
162 struct device_handler *device_handler,
165 return device_lookup(device_handler, device_match_id, id);
168 void device_handler_destroy(struct device_handler *handler)
170 talloc_free(handler);
173 #ifdef PETITBOOT_TEST
175 /* we have a simplified interface for petitboot testing, but still want
176 * to keep struct device_handler opaque. */
177 struct device_handler *device_handler_init(
178 struct discover_server *server __attribute__((unused)),
179 int dry_run __attribute__((unused)))
181 struct device_handler *handler;
183 handler = talloc_zero(NULL, struct device_handler);
184 list_init(&handler->unresolved_boot_options);
189 void device_handler_add_device(struct device_handler *handler,
190 struct discover_device *dev)
192 handler->n_devices++;
193 handler->devices = talloc_realloc(handler, handler->devices,
194 struct discover_device *, handler->n_devices);
195 handler->devices[handler->n_devices - 1] = dev;
200 static int mount_device(struct discover_device *dev)
204 if (!dev->device_path)
207 if (!dev->mount_path)
208 dev->mount_path = join_paths(dev, mount_base(),
211 if (pb_mkdir_recursive(dev->mount_path))
212 pb_log("couldn't create mount directory %s: %s\n",
213 dev->mount_path, strerror(errno));
215 argv[0] = pb_system_apps.mount;
216 argv[1] = dev->device_path;
217 argv[2] = dev->mount_path;
222 if (pb_run_cmd(argv, 1, 0)) {
224 /* Retry mount without ro option. */
226 argv[0] = pb_system_apps.mount;
227 argv[1] = dev->device_path;
228 argv[2] = dev->mount_path;
231 if (pb_run_cmd(argv, 1, 0))
238 pb_rmdir_recursive(mount_base(), dev->mount_path);
242 static int umount_device(struct discover_device *dev)
247 if (!dev->mount_path)
252 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
257 execl(pb_system_apps.umount, pb_system_apps.umount,
258 dev->mount_path, NULL);
262 if (waitpid(pid, &status, 0) == -1) {
263 pb_log("%s: waitpid failed: %s\n", __func__,
268 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
271 pb_rmdir_recursive(mount_base(), dev->mount_path);
276 struct device_handler *device_handler_init(struct discover_server *server,
279 struct device_handler *handler;
281 handler = talloc(NULL, struct device_handler);
282 handler->devices = NULL;
283 handler->n_devices = 0;
284 handler->server = server;
285 handler->dry_run = dry_run;
286 list_init(&handler->unresolved_boot_options);
288 /* set up our mount point base */
289 pb_mkdir_recursive(mount_base());
296 static int destroy_device(void *arg)
298 struct discover_device *dev = arg;
305 static struct discover_device *find_device(struct device_handler *handler,
308 struct discover_device *dev;
311 for (i = 0; i < handler->n_devices; i++) {
312 dev = handler->devices[i];
313 if (!strcmp(dev->device->id, id))
320 static struct discover_device *discover_device_create(
321 struct device_handler *handler,
322 struct discover_context *ctx,
325 struct discover_device *dev;
328 dev = find_device(handler, event->device);
332 dev = talloc_zero(ctx, struct discover_device);
333 dev->device = talloc_zero(dev, struct device);
334 list_init(&dev->boot_options);
336 devname = event_get_param(ctx->event, "DEVNAME");
338 dev->device_path = talloc_strdup(dev, devname);
340 dev->device->id = talloc_strdup(dev, event->device);
342 talloc_set_destructor(dev, destroy_device);
348 * device_handler_remove - Remove a device from the handler device array.
351 static void device_handler_remove(struct device_handler *handler,
352 struct discover_device *device)
356 for (i = 0; i < handler->n_devices; i++)
357 if (handler->devices[i] == device)
360 if (i == handler->n_devices) {
361 assert(0 && "unknown device");
365 handler->n_devices--;
366 memmove(&handler->devices[i], &handler->devices[i + 1],
367 (handler->n_devices - i) * sizeof(handler->devices[0]));
368 handler->devices = talloc_realloc(handler, handler->devices,
369 struct discover_device *, handler->n_devices);
371 discover_server_notify_device_remove(handler->server, device->device);
376 static bool resource_is_resolved(struct resource *res)
378 return !res || res->resolved;
381 /* We only use this in an assert, which will disappear if we're compiling
382 * with NDEBUG, so we need the 'used' attribute for these builds */
383 static bool __attribute__((used)) boot_option_is_resolved(
384 struct discover_boot_option *opt)
386 return resource_is_resolved(opt->boot_image) &&
387 resource_is_resolved(opt->initrd) &&
388 resource_is_resolved(opt->icon);
391 static bool resource_resolve(struct resource *res, const char *name,
392 struct discover_boot_option *opt,
393 struct device_handler *handler)
395 struct parser *parser = opt->source;
397 if (resource_is_resolved(res))
400 pb_log("Attempting to resolve resource %s->%s with parser %s\n",
401 opt->option->id, name, parser->name);
402 parser->resolve_resource(handler, res);
404 return res->resolved;
407 static bool boot_option_resolve(struct discover_boot_option *opt,
408 struct device_handler *handler)
410 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
411 resource_resolve(opt->initrd, "initrd", opt, handler) &&
412 resource_resolve(opt->icon, "icon", opt, handler);
415 static void boot_option_finalise(struct discover_boot_option *opt)
417 assert(boot_option_is_resolved(opt));
419 /* check that the parsers haven't set any of the final data */
420 assert(!opt->option->boot_image_file);
421 assert(!opt->option->initrd_file);
422 assert(!opt->option->icon_file);
423 assert(!opt->option->device_id);
426 opt->option->boot_image_file = opt->boot_image->url->full;
428 opt->option->initrd_file = opt->initrd->url->full;
430 opt->option->icon_file = opt->icon->url->full;
432 opt->option->device_id = opt->device->device->id;
435 static void process_boot_option_queue(struct device_handler *handler)
437 struct discover_boot_option *opt, *tmp;
439 list_for_each_entry_safe(&handler->unresolved_boot_options,
442 pb_log("queue: attempting resolution for %s\n",
445 if (!boot_option_resolve(opt, handler))
448 pb_log("\tresolved!\n");
450 list_remove(&opt->list);
451 list_add_tail(&opt->device->boot_options, &opt->list);
452 talloc_steal(opt->device, opt);
453 boot_option_finalise(opt);
454 discover_server_notify_boot_option_add(handler->server,
460 * context_commit - Commit a temporary discovery context to the handler,
461 * and notify the clients about any new options / devices
463 static void context_commit(struct device_handler *handler,
464 struct discover_context *ctx)
466 struct discover_device *dev = ctx->device;
467 struct discover_boot_option *opt, *tmp;
468 unsigned int i, existing_device = 0;
470 /* do we already have this device? */
471 for (i = 0; i < handler->n_devices; i++) {
472 if (ctx->device == handler->devices[i]) {
478 /* if not already present, add the device to the handler's array */
479 if (!existing_device) {
480 handler->n_devices++;
481 handler->devices = talloc_realloc(handler, handler->devices,
482 struct discover_device *, handler->n_devices);
483 handler->devices[handler->n_devices - 1] = dev;
484 talloc_steal(handler, dev);
486 discover_server_notify_device_add(handler->server, dev->device);
488 /* this new device might be able to resolve existing boot
490 pb_log("New device %s, processing queue\n", dev->device->id);
491 process_boot_option_queue(handler);
495 /* move boot options from the context to the device */
496 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
497 list_remove(&opt->list);
499 if (boot_option_resolve(opt, handler)) {
500 pb_log("boot option %s is resolved, "
501 "sending to clients\n",
503 list_add_tail(&dev->boot_options, &opt->list);
504 talloc_steal(dev, opt);
505 boot_option_finalise(opt);
506 discover_server_notify_boot_option_add(handler->server,
509 if (!opt->source->resolve_resource) {
510 pb_log("parser %s gave us an unresolved "
511 "resource (%s), but no way to "
513 opt->source->name, opt->option->id);
516 pb_log("boot option %s is unresolved, "
519 list_add(&handler->unresolved_boot_options,
521 talloc_steal(handler, opt);
527 static int handle_add_udev_event(struct device_handler *handler,
530 struct discover_context *ctx;
531 struct discover_device *dev;
535 /* create our context */
536 ctx = talloc(handler, struct discover_context);
538 list_init(&ctx->boot_options);
540 /* create our top-level device */
541 dev = discover_device_create(handler, ctx, event);
545 /* try to parse UUID and labels */
546 param = event_get_param(ctx->event, "ID_FS_UUID");
548 dev->uuid = talloc_strdup(dev, param);
550 param = event_get_param(ctx->event, "ID_FS_LABEL");
552 dev->label = talloc_strdup(dev, param);
554 rc = mount_device(dev);
560 /* run the parsers. This will populate the ctx's boot_option list. */
561 iterate_parsers(ctx, CONF_METHOD_LOCAL_FILE);
563 /* add discovered stuff to the handler */
564 context_commit(handler, ctx);
571 static int handle_remove_udev_event(struct device_handler *handler,
574 struct discover_device *dev;
576 dev = find_device(handler, event->device);
580 /* remove device from handler device array */
581 device_handler_remove(handler, dev);
586 static int handle_add_user_event(struct device_handler *handler,
589 struct discover_context *ctx;
590 struct discover_device *dev;
593 assert(event->device);
595 ctx = talloc(handler, struct discover_context);
597 list_init(&ctx->boot_options);
599 dev = discover_device_create(handler, ctx, event);
602 rc = parse_user_event(ctx, event);
605 context_commit(handler, ctx);
610 static int handle_remove_user_event(struct device_handler *handler,
613 struct discover_device *dev = find_device(handler, event->device);
618 /* remove device from handler device array */
619 device_handler_remove(handler, dev);
624 static enum conf_method parse_conf_method(const char *str)
627 if (!strcasecmp(str, "dhcp")) {
628 return CONF_METHOD_DHCP;
630 return CONF_METHOD_UNKNOWN;
633 static int handle_conf_user_event(struct device_handler *handler,
636 struct discover_context *ctx;
637 struct discover_device *dev;
638 enum conf_method method;
641 ctx = talloc(handler, struct discover_context);
643 list_init(&ctx->boot_options);
645 val = event_get_param(event, "url");
651 ctx->conf_url = pb_url_parse(ctx, val);
652 if (!ctx->conf_url) {
657 val = event_get_param(event, "method");
663 method = parse_conf_method(val);
664 if (method == CONF_METHOD_UNKNOWN) {
669 dev = discover_device_create(handler, ctx, event);
672 iterate_parsers(ctx, method);
674 context_commit(handler, ctx);
679 typedef int (*event_handler)(struct device_handler *, struct event *);
681 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
682 [EVENT_TYPE_UDEV] = {
683 [EVENT_ACTION_ADD] = handle_add_udev_event,
684 [EVENT_ACTION_REMOVE] = handle_remove_udev_event,
686 [EVENT_TYPE_USER] = {
687 [EVENT_ACTION_ADD] = handle_add_user_event,
688 [EVENT_ACTION_REMOVE] = handle_remove_user_event,
689 [EVENT_ACTION_CONF] = handle_conf_user_event,
693 int device_handler_event(struct device_handler *handler,
696 if (event->type >= EVENT_TYPE_MAX ||
697 event->action >= EVENT_ACTION_MAX ||
698 !handlers[event->type][event->action]) {
699 pb_log("%s unknown type/action: %d/%d\n", __func__,
700 event->type, event->action);
704 return handlers[event->type][event->action](handler, event);
707 static void boot_status(void *arg, struct boot_status *status)
709 struct device_handler *handler = arg;
711 discover_server_notify_boot_status(handler->server, status);
714 static struct discover_boot_option *find_boot_option_by_id(
715 struct device_handler *handler, const char *id)
719 for (i = 0; i < handler->n_devices; i++) {
720 struct discover_device *dev = handler->devices[i];
721 struct discover_boot_option *opt;
723 list_for_each_entry(&dev->boot_options, opt, list)
724 if (!strcmp(opt->option->id, id))
731 void device_handler_boot(struct device_handler *handler,
732 struct boot_command *cmd)
734 struct discover_boot_option *opt;
736 opt = find_boot_option_by_id(handler, cmd->option_id);
738 boot(handler, opt, cmd, handler->dry_run, boot_status, handler);