10 #include <sys/mount.h>
12 #include <talloc/talloc.h>
13 #include <list/list.h>
15 #include <types/types.h>
16 #include <system/system.h>
17 #include <process/process.h>
20 #include "device-handler.h"
21 #include "discover-server.h"
22 #include "user-event.h"
33 struct device_handler {
34 struct discover_server *server;
38 struct network *network;
39 struct user_event *user_event;
41 struct discover_device **devices;
42 unsigned int n_devices;
44 struct waitset *waitset;
45 struct waiter *timeout_waiter;
46 bool autoboot_enabled;
47 unsigned int sec_to_boot;
49 struct discover_boot_option *default_boot_option;
50 struct list unresolved_boot_options;
52 struct boot_task *pending_boot;
53 bool pending_boot_is_default;
56 static int mount_device(struct discover_device *dev);
57 static int umount_device(struct discover_device *dev);
59 static int device_handler_init_sources(struct device_handler *handler);
61 void discover_context_add_boot_option(struct discover_context *ctx,
62 struct discover_boot_option *boot_option)
64 boot_option->source = ctx->parser;
65 list_add_tail(&ctx->boot_options, &boot_option->list);
66 talloc_steal(ctx, boot_option);
70 * device_handler_get_device_count - Get the count of current handler devices.
73 int device_handler_get_device_count(const struct device_handler *handler)
75 return handler->n_devices;
79 * device_handler_get_device - Get a handler device by index.
82 const struct discover_device *device_handler_get_device(
83 const struct device_handler *handler, unsigned int index)
85 if (index >= handler->n_devices) {
86 assert(0 && "bad index");
90 return handler->devices[index];
93 struct discover_boot_option *discover_boot_option_create(
94 struct discover_context *ctx,
95 struct discover_device *device)
97 struct discover_boot_option *opt;
99 opt = talloc_zero(ctx, struct discover_boot_option);
100 opt->option = talloc_zero(opt, struct boot_option);
101 opt->device = device;
106 static int device_match_uuid(struct discover_device *dev, const char *uuid)
108 return dev->uuid && !strcmp(dev->uuid, uuid);
111 static int device_match_label(struct discover_device *dev, const char *label)
113 return dev->label && !strcmp(dev->label, label);
116 static int device_match_id(struct discover_device *dev, const char *id)
118 return !strcmp(dev->device->id, id);
121 static int device_match_serial(struct discover_device *dev, const char *serial)
123 const char *val = discover_device_get_param(dev, "ID_SERIAL");
124 return val && !strcmp(val, serial);
127 static struct discover_device *device_lookup(
128 struct device_handler *device_handler,
129 int (match_fn)(struct discover_device *, const char *),
132 struct discover_device *dev;
138 for (i = 0; i < device_handler->n_devices; i++) {
139 dev = device_handler->devices[i];
141 if (match_fn(dev, str))
148 struct discover_device *device_lookup_by_name(struct device_handler *handler,
151 if (!strncmp(name, "/dev/", strlen("/dev/")))
152 name += strlen("/dev/");
154 return device_lookup_by_id(handler, name);
157 struct discover_device *device_lookup_by_uuid(
158 struct device_handler *device_handler,
161 return device_lookup(device_handler, device_match_uuid, uuid);
164 struct discover_device *device_lookup_by_label(
165 struct device_handler *device_handler,
168 return device_lookup(device_handler, device_match_label, label);
171 struct discover_device *device_lookup_by_id(
172 struct device_handler *device_handler,
175 return device_lookup(device_handler, device_match_id, id);
178 struct discover_device *device_lookup_by_serial(
179 struct device_handler *device_handler,
182 return device_lookup(device_handler, device_match_serial, serial);
185 void device_handler_destroy(struct device_handler *handler)
187 talloc_free(handler);
190 static int destroy_device(void *arg)
192 struct discover_device *dev = arg;
199 struct discover_device *discover_device_create(struct device_handler *handler,
202 struct discover_device *dev;
204 dev = device_lookup_by_id(handler, id);
208 dev = talloc_zero(handler, struct discover_device);
209 dev->device = talloc_zero(dev, struct device);
210 dev->device->id = talloc_strdup(dev->device, id);
211 list_init(&dev->params);
212 list_init(&dev->boot_options);
214 talloc_set_destructor(dev, destroy_device);
219 struct discover_device_param {
222 struct list_item list;
225 void discover_device_set_param(struct discover_device *device,
226 const char *name, const char *value)
228 struct discover_device_param *param;
231 list_for_each_entry(&device->params, param, list) {
232 if (!strcmp(param->name, name)) {
241 param = talloc(device, struct discover_device_param);
242 param->name = talloc_strdup(param, name);
243 list_add(&device->params, ¶m->list);
246 list_remove(¶m->list);
250 talloc_free(param->value);
253 param->value = talloc_strdup(param, value);
256 const char *discover_device_get_param(struct discover_device *device,
259 struct discover_device_param *param;
261 list_for_each_entry(&device->params, param, list) {
262 if (!strcmp(param->name, name))
268 struct device_handler *device_handler_init(struct discover_server *server,
269 struct waitset *waitset, int dry_run)
271 struct device_handler *handler;
274 handler = talloc_zero(NULL, struct device_handler);
275 handler->server = server;
276 handler->waitset = waitset;
277 handler->dry_run = dry_run;
278 handler->autoboot_enabled = config_get()->autoboot_enabled;
280 list_init(&handler->unresolved_boot_options);
282 /* set up our mount point base */
283 pb_mkdir_recursive(mount_base());
287 rc = device_handler_init_sources(handler);
289 talloc_free(handler);
296 void device_handler_remove(struct device_handler *handler,
297 struct discover_device *device)
299 struct discover_boot_option *opt, *tmp;
302 for (i = 0; i < handler->n_devices; i++)
303 if (handler->devices[i] == device)
306 if (i == handler->n_devices) {
311 /* Free any unresolved options, as they're currently allocated
312 * against the handler */
313 list_for_each_entry_safe(&handler->unresolved_boot_options,
315 if (opt->device != device)
317 list_remove(&opt->list);
321 handler->n_devices--;
322 memmove(&handler->devices[i], &handler->devices[i + 1],
323 (handler->n_devices - i) * sizeof(handler->devices[0]));
324 handler->devices = talloc_realloc(handler, handler->devices,
325 struct discover_device *, handler->n_devices);
327 if (device->notified)
328 discover_server_notify_device_remove(handler->server,
334 static void boot_status(void *arg, struct boot_status *status)
336 struct device_handler *handler = arg;
338 discover_server_notify_boot_status(handler->server, status);
341 static void countdown_status(struct device_handler *handler,
342 struct discover_boot_option *opt, unsigned int sec)
344 struct boot_status status;
346 status.type = BOOT_STATUS_INFO;
347 status.progress = -1;
348 status.detail = NULL;
349 status.message = talloc_asprintf(handler,
350 "Booting %s in %u sec", opt->option->name, sec);
352 discover_server_notify_boot_status(handler->server, &status);
354 talloc_free(status.message);
357 static int default_timeout(void *arg)
359 struct device_handler *handler = arg;
360 struct discover_boot_option *opt;
362 if (!handler->default_boot_option)
365 if (handler->pending_boot)
368 opt = handler->default_boot_option;
370 if (handler->sec_to_boot) {
371 countdown_status(handler, opt, handler->sec_to_boot);
372 handler->sec_to_boot--;
373 handler->timeout_waiter = waiter_register_timeout(
374 handler->waitset, 1000,
375 default_timeout, handler);
379 handler->timeout_waiter = NULL;
381 pb_log("Timeout expired, booting default option %s\n", opt->option->id);
383 handler->pending_boot = boot(handler, handler->default_boot_option,
384 NULL, handler->dry_run, boot_status, handler);
385 handler->pending_boot_is_default = true;
389 static bool priority_match(struct boot_priority *prio,
390 struct discover_boot_option *opt)
392 return prio->type == opt->device->device->type ||
393 prio->type == DEVICE_TYPE_ANY;
396 static int default_option_priority(struct discover_boot_option *opt)
398 const struct config *config;
399 struct boot_priority *prio;
402 config = config_get();
404 for (i = 0; i < config->n_boot_priorities; i++) {
405 prio = &config->boot_priorities[i];
406 if (priority_match(prio, opt))
407 return prio->priority;
413 static void set_default(struct device_handler *handler,
414 struct discover_boot_option *opt)
418 if (!handler->autoboot_enabled)
421 new_prio = default_option_priority(opt);
423 /* A negative priority indicates that we don't want to boot this device
428 /* Resolve any conflicts: if we have a new default option, it only
429 * replaces the current if it has a higher priority. */
430 if (handler->default_boot_option) {
433 cur_prio = default_option_priority(
434 handler->default_boot_option);
436 if (new_prio > cur_prio) {
437 handler->default_boot_option = opt;
438 /* extend the timeout a little, so the user sees some
439 * indication of the change */
440 handler->sec_to_boot += 2;
446 handler->sec_to_boot = config_get()->autoboot_timeout_sec;
447 handler->default_boot_option = opt;
449 pb_log("Boot option %s set as default, timeout %u sec.\n",
450 opt->option->id, handler->sec_to_boot);
452 default_timeout(handler);
455 static bool resource_is_resolved(struct resource *res)
457 return !res || res->resolved;
460 /* We only use this in an assert, which will disappear if we're compiling
461 * with NDEBUG, so we need the 'used' attribute for these builds */
462 static bool __attribute__((used)) boot_option_is_resolved(
463 struct discover_boot_option *opt)
465 return resource_is_resolved(opt->boot_image) &&
466 resource_is_resolved(opt->initrd) &&
467 resource_is_resolved(opt->dtb) &&
468 resource_is_resolved(opt->icon);
471 static bool resource_resolve(struct resource *res, const char *name,
472 struct discover_boot_option *opt,
473 struct device_handler *handler)
475 struct parser *parser = opt->source;
477 if (resource_is_resolved(res))
480 pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
481 opt->option->id, name, parser->name);
482 parser->resolve_resource(handler, res);
484 return res->resolved;
487 static bool boot_option_resolve(struct discover_boot_option *opt,
488 struct device_handler *handler)
490 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
491 resource_resolve(opt->initrd, "initrd", opt, handler) &&
492 resource_resolve(opt->dtb, "dtb", opt, handler) &&
493 resource_resolve(opt->icon, "icon", opt, handler);
496 static void boot_option_finalise(struct device_handler *handler,
497 struct discover_boot_option *opt)
499 assert(boot_option_is_resolved(opt));
501 /* check that the parsers haven't set any of the final data */
502 assert(!opt->option->boot_image_file);
503 assert(!opt->option->initrd_file);
504 assert(!opt->option->dtb_file);
505 assert(!opt->option->icon_file);
506 assert(!opt->option->device_id);
509 opt->option->boot_image_file = opt->boot_image->url->full;
511 opt->option->initrd_file = opt->initrd->url->full;
513 opt->option->dtb_file = opt->dtb->url->full;
515 opt->option->icon_file = opt->icon->url->full;
517 opt->option->device_id = opt->device->device->id;
519 if (opt->option->is_default)
520 set_default(handler, opt);
523 static void notify_boot_option(struct device_handler *handler,
524 struct discover_boot_option *opt)
526 struct discover_device *dev = opt->device;
529 discover_server_notify_device_add(handler->server,
530 opt->device->device);
531 dev->notified = true;
532 discover_server_notify_boot_option_add(handler->server, opt->option);
535 static void process_boot_option_queue(struct device_handler *handler)
537 struct discover_boot_option *opt, *tmp;
539 list_for_each_entry_safe(&handler->unresolved_boot_options,
542 pb_debug("queue: attempting resolution for %s\n",
545 if (!boot_option_resolve(opt, handler))
548 pb_debug("\tresolved!\n");
550 list_remove(&opt->list);
551 list_add_tail(&opt->device->boot_options, &opt->list);
552 talloc_steal(opt->device, opt);
553 boot_option_finalise(handler, opt);
554 notify_boot_option(handler, opt);
558 struct discover_context *device_handler_discover_context_create(
559 struct device_handler *handler,
560 struct discover_device *device)
562 struct discover_context *ctx;
564 ctx = talloc_zero(handler, struct discover_context);
565 ctx->device = device;
566 list_init(&ctx->boot_options);
572 * context_commit - Commit a temporary discovery context to the handler,
573 * and notify the clients about any new options / devices
575 void device_handler_discover_context_commit(struct device_handler *handler,
576 struct discover_context *ctx)
578 struct discover_device *dev = ctx->device;
579 struct discover_boot_option *opt, *tmp;
581 if (!device_lookup_by_id(handler, dev->device->id))
582 device_handler_add_device(handler, dev);
584 /* move boot options from the context to the device */
585 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
586 list_remove(&opt->list);
588 if (boot_option_resolve(opt, handler)) {
589 pb_log("boot option %s is resolved, "
590 "sending to clients\n",
592 list_add_tail(&dev->boot_options, &opt->list);
593 talloc_steal(dev, opt);
594 boot_option_finalise(handler, opt);
595 notify_boot_option(handler, opt);
597 if (!opt->source->resolve_resource) {
598 pb_log("parser %s gave us an unresolved "
599 "resource (%s), but no way to "
601 opt->source->name, opt->option->id);
604 pb_log("boot option %s is unresolved, "
607 list_add(&handler->unresolved_boot_options,
609 talloc_steal(handler, opt);
615 void device_handler_add_device(struct device_handler *handler,
616 struct discover_device *device)
618 handler->n_devices++;
619 handler->devices = talloc_realloc(handler, handler->devices,
620 struct discover_device *, handler->n_devices);
621 handler->devices[handler->n_devices - 1] = device;
625 /* Start discovery on a hotplugged device. The device will be in our devices
626 * array, but has only just been initialised by the hotplug source.
628 int device_handler_discover(struct device_handler *handler,
629 struct discover_device *dev)
631 struct discover_context *ctx;
634 process_boot_option_queue(handler);
636 /* create our context */
637 ctx = device_handler_discover_context_create(handler, dev);
639 rc = mount_device(dev);
643 /* add this device to our system info */
644 system_info_register_blockdev(dev->device->id, dev->uuid,
647 /* run the parsers. This will populate the ctx's boot_option list. */
648 iterate_parsers(ctx);
650 /* add discovered stuff to the handler */
651 device_handler_discover_context_commit(handler, ctx);
659 /* Incoming dhcp event */
660 int device_handler_dhcp(struct device_handler *handler,
661 struct discover_device *dev, struct event *event)
663 struct discover_context *ctx;
665 /* create our context */
666 ctx = device_handler_discover_context_create(handler, dev);
669 iterate_parsers(ctx);
671 device_handler_discover_context_commit(handler, ctx);
678 /* incoming conf event */
679 int device_handler_conf(struct device_handler *handler,
680 struct discover_device *dev, struct pb_url *url)
682 struct discover_context *ctx;
684 /* create our context */
685 ctx = device_handler_discover_context_create(handler, dev);
688 iterate_parsers(ctx);
690 device_handler_discover_context_commit(handler, ctx);
697 static struct discover_boot_option *find_boot_option_by_id(
698 struct device_handler *handler, const char *id)
702 for (i = 0; i < handler->n_devices; i++) {
703 struct discover_device *dev = handler->devices[i];
704 struct discover_boot_option *opt;
706 list_for_each_entry(&dev->boot_options, opt, list)
707 if (!strcmp(opt->option->id, id))
714 void device_handler_boot(struct device_handler *handler,
715 struct boot_command *cmd)
717 struct discover_boot_option *opt = NULL;
719 if (cmd->option_id && strlen(cmd->option_id))
720 opt = find_boot_option_by_id(handler, cmd->option_id);
722 if (handler->pending_boot)
723 boot_cancel(handler->pending_boot);
724 handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
725 boot_status, handler);
726 handler->pending_boot_is_default = false;
729 void device_handler_cancel_default(struct device_handler *handler)
731 struct boot_status status;
733 if (handler->timeout_waiter)
734 waiter_remove(handler->timeout_waiter);
736 handler->timeout_waiter = NULL;
737 handler->autoboot_enabled = false;
739 /* we only send status if we had a default boot option queued */
740 if (!handler->default_boot_option)
743 pb_log("Cancelling default boot option\n");
745 if (handler->pending_boot && handler->pending_boot_is_default) {
746 boot_cancel(handler->pending_boot);
747 handler->pending_boot = NULL;
748 handler->pending_boot_is_default = false;
751 handler->default_boot_option = NULL;
753 status.type = BOOT_STATUS_INFO;
754 status.progress = -1;
755 status.detail = NULL;
756 status.message = "Default boot cancelled";
758 discover_server_notify_boot_status(handler->server, &status);
761 void device_handler_update_config(struct device_handler *handler,
762 struct config *config)
765 discover_server_notify_config(handler->server, config);
768 #ifndef PETITBOOT_TEST
770 static int device_handler_init_sources(struct device_handler *handler)
772 /* init our device sources: udev, network and user events */
773 handler->udev = udev_init(handler, handler->waitset);
777 handler->network = network_init(handler, handler->waitset,
779 if (!handler->network)
782 handler->user_event = user_event_init(handler, handler->waitset);
783 if (!handler->user_event)
789 static bool check_existing_mount(struct discover_device *dev)
791 struct stat devstat, mntstat;
796 rc = stat(dev->device_path, &devstat);
798 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
802 if (!S_ISBLK(devstat.st_mode)) {
803 pb_debug("%s: %s isn't a block device?\n", __func__,
808 fp = fopen("/proc/self/mounts", "r");
815 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
818 rc = stat(mnt->mnt_fsname, &mntstat);
822 if (!S_ISBLK(mntstat.st_mode))
825 if (mntstat.st_rdev == devstat.st_rdev) {
826 dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
827 dev->mounted_rw = !!hasmntopt(mnt, "rw");
829 dev->unmount = false;
831 pb_debug("%s: %s is already mounted (r%c) at %s\n",
832 __func__, dev->device_path,
833 dev->mounted_rw ? 'w' : 'o',
844 static int mount_device(struct discover_device *dev)
849 if (!dev->device_path)
855 if (check_existing_mount(dev))
858 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
862 dev->mount_path = join_paths(dev, mount_base(),
865 if (pb_mkdir_recursive(dev->mount_path)) {
866 pb_log("couldn't create mount directory %s: %s\n",
867 dev->mount_path, strerror(errno));
871 pb_log("mounting device %s read-only\n", dev->device_path);
873 rc = mount(dev->device_path, dev->mount_path, fstype,
874 MS_RDONLY | MS_SILENT, "");
877 dev->mounted_rw = false;
882 pb_log("couldn't mount device %s: mount failed: %s\n",
883 dev->device_path, strerror(errno));
885 pb_rmdir_recursive(mount_base(), dev->mount_path);
887 talloc_free(dev->mount_path);
888 dev->mount_path = NULL;
892 static int umount_device(struct discover_device *dev)
896 if (!dev->mounted || !dev->unmount)
899 pb_log("unmounting device %s\n", dev->device_path);
900 rc = umount(dev->mount_path);
904 dev->mounted = false;
906 pb_rmdir_recursive(mount_base(), dev->mount_path);
908 talloc_free(dev->mount_path);
909 dev->mount_path = NULL;
914 int device_request_write(struct discover_device *dev, bool *release)
926 pb_log("remounting device %s read-write\n", dev->device_path);
927 rc = mount(dev->device_path, dev->mount_path, "",
928 MS_REMOUNT | MS_SILENT, "");
932 dev->mounted_rw = true;
937 void device_release_write(struct discover_device *dev, bool release)
942 pb_log("remounting device %s read-only\n", dev->device_path);
943 mount(dev->device_path, dev->mount_path, "",
944 MS_REMOUNT | MS_RDONLY | MS_SILENT, "");
945 dev->mounted_rw = false;
950 static int device_handler_init_sources(
951 struct device_handler *handler __attribute__((unused)))
956 static int umount_device(struct discover_device *dev __attribute__((unused)))
961 static int __attribute__((unused)) mount_device(
962 struct discover_device *dev __attribute__((unused)))
967 int device_request_write(struct discover_device *dev __attribute__((unused)),
974 void device_release_write(struct discover_device *dev __attribute__((unused)),
975 bool release __attribute__((unused)))