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, struct parser *parser,
392 struct device_handler *handler)
394 if (resource_is_resolved(res))
397 parser->resolve_resource(handler, res);
399 return res->resolved;
402 static bool boot_option_resolve(struct discover_boot_option *opt,
403 struct device_handler *handler)
405 return resource_resolve(opt->boot_image, opt->source, handler) &&
406 resource_resolve(opt->initrd, opt->source, handler) &&
407 resource_resolve(opt->icon, opt->source, handler);
410 static void boot_option_finalise(struct discover_boot_option *opt)
412 assert(boot_option_is_resolved(opt));
414 /* check that the parsers haven't set any of the final data */
415 assert(!opt->option->boot_image_file);
416 assert(!opt->option->initrd_file);
417 assert(!opt->option->icon_file);
418 assert(!opt->option->device_id);
421 opt->option->boot_image_file = opt->boot_image->url->full;
423 opt->option->initrd_file = opt->initrd->url->full;
425 opt->option->icon_file = opt->icon->url->full;
427 opt->option->device_id = opt->device->device->id;
430 static void process_boot_option_queue(struct device_handler *handler)
432 struct discover_boot_option *opt, *tmp;
434 list_for_each_entry_safe(&handler->unresolved_boot_options,
437 if (!boot_option_resolve(opt, handler))
440 list_remove(&opt->list);
441 list_add(&opt->device->boot_options, &opt->list);
442 talloc_steal(opt->device, opt);
443 boot_option_finalise(opt);
444 discover_server_notify_boot_option_add(handler->server,
450 * context_commit - Commit a temporary discovery context to the handler,
451 * and notify the clients about any new options / devices
453 static void context_commit(struct device_handler *handler,
454 struct discover_context *ctx)
456 struct discover_device *dev = ctx->device;
457 struct discover_boot_option *opt, *tmp;
458 unsigned int i, existing_device = 0;
460 /* do we already have this device? */
461 for (i = 0; i < handler->n_devices; i++) {
462 if (ctx->device == handler->devices[i]) {
468 /* if not already present, add the device to the handler's array */
469 if (!existing_device) {
470 handler->n_devices++;
471 handler->devices = talloc_realloc(handler, handler->devices,
472 struct discover_device *, handler->n_devices);
473 handler->devices[handler->n_devices - 1] = dev;
474 talloc_steal(handler, dev);
476 discover_server_notify_device_add(handler->server, dev->device);
478 /* this new device might be able to resolve existing boot
480 process_boot_option_queue(handler);
484 /* move boot options from the context to the device */
485 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
486 list_remove(&opt->list);
488 if (boot_option_resolve(opt, handler)) {
489 list_add(&dev->boot_options, &opt->list);
490 talloc_steal(dev, opt);
491 boot_option_finalise(opt);
492 discover_server_notify_boot_option_add(handler->server,
495 if (!opt->source->resolve_resource) {
496 pb_log("parser %s gave us an unresolved "
497 "resource (%s), but no way to "
499 opt->source->name, opt->option->id);
502 pb_log("boot option %s is unresolved, "
505 list_add(&handler->unresolved_boot_options,
507 talloc_steal(handler, opt);
513 static int handle_add_udev_event(struct device_handler *handler,
516 struct discover_context *ctx;
517 struct discover_device *dev;
521 /* create our context */
522 ctx = talloc(handler, struct discover_context);
524 list_init(&ctx->boot_options);
526 /* create our top-level device */
527 dev = discover_device_create(handler, ctx, event);
531 /* try to parse UUID and labels */
532 param = event_get_param(ctx->event, "ID_FS_UUID");
534 dev->uuid = talloc_strdup(dev, param);
536 param = event_get_param(ctx->event, "ID_FS_LABEL");
538 dev->label = talloc_strdup(dev, param);
540 rc = mount_device(dev);
546 /* run the parsers. This will populate the ctx's boot_option list. */
547 iterate_parsers(ctx, CONF_METHOD_LOCAL_FILE);
549 /* add discovered stuff to the handler */
550 context_commit(handler, ctx);
557 static int handle_remove_udev_event(struct device_handler *handler,
560 struct discover_device *dev;
562 dev = find_device(handler, event->device);
566 /* remove device from handler device array */
567 device_handler_remove(handler, dev);
572 static int handle_add_user_event(struct device_handler *handler,
575 struct discover_context *ctx;
576 struct discover_device *dev;
579 assert(event->device);
581 ctx = talloc(handler, struct discover_context);
583 list_init(&ctx->boot_options);
585 dev = discover_device_create(handler, ctx, event);
588 rc = parse_user_event(ctx, event);
591 context_commit(handler, ctx);
596 static int handle_remove_user_event(struct device_handler *handler,
599 struct discover_device *dev = find_device(handler, event->device);
604 /* remove device from handler device array */
605 device_handler_remove(handler, dev);
610 static enum conf_method parse_conf_method(const char *str)
613 if (!strcasecmp(str, "dhcp")) {
614 return CONF_METHOD_DHCP;
616 return CONF_METHOD_UNKNOWN;
619 static int handle_conf_user_event(struct device_handler *handler,
622 struct discover_context *ctx;
623 struct discover_device *dev;
624 enum conf_method method;
627 ctx = talloc(handler, struct discover_context);
629 list_init(&ctx->boot_options);
631 val = event_get_param(event, "url");
637 ctx->conf_url = pb_url_parse(ctx, val);
638 if (!ctx->conf_url) {
643 val = event_get_param(event, "method");
649 method = parse_conf_method(val);
650 if (method == CONF_METHOD_UNKNOWN) {
655 dev = discover_device_create(handler, ctx, event);
658 iterate_parsers(ctx, method);
660 context_commit(handler, ctx);
665 typedef int (*event_handler)(struct device_handler *, struct event *);
667 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
668 [EVENT_TYPE_UDEV] = {
669 [EVENT_ACTION_ADD] = handle_add_udev_event,
670 [EVENT_ACTION_REMOVE] = handle_remove_udev_event,
672 [EVENT_TYPE_USER] = {
673 [EVENT_ACTION_ADD] = handle_add_user_event,
674 [EVENT_ACTION_REMOVE] = handle_remove_user_event,
675 [EVENT_ACTION_CONF] = handle_conf_user_event,
679 int device_handler_event(struct device_handler *handler,
682 if (event->type >= EVENT_TYPE_MAX ||
683 event->action >= EVENT_ACTION_MAX ||
684 !handlers[event->type][event->action]) {
685 pb_log("%s unknown type/action: %d/%d\n", __func__,
686 event->type, event->action);
690 return handlers[event->type][event->action](handler, event);
693 static void boot_status(void *arg, struct boot_status *status)
695 struct device_handler *handler = arg;
697 discover_server_notify_boot_status(handler->server, status);
700 static struct discover_boot_option *find_boot_option_by_id(
701 struct device_handler *handler, const char *id)
705 for (i = 0; i < handler->n_devices; i++) {
706 struct discover_device *dev = handler->devices[i];
707 struct discover_boot_option *opt;
709 list_for_each_entry(&dev->boot_options, opt, list)
710 if (!strcmp(opt->option->id, id))
717 void device_handler_boot(struct device_handler *handler,
718 struct boot_command *cmd)
720 struct discover_boot_option *opt;
722 opt = find_boot_option_by_id(handler, cmd->option_id);
724 boot(handler, opt, cmd, handler->dry_run, boot_status, handler);