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);
60 static void device_handler_reinit_sources(struct device_handler *handler);
62 void discover_context_add_boot_option(struct discover_context *ctx,
63 struct discover_boot_option *boot_option)
65 boot_option->source = ctx->parser;
66 list_add_tail(&ctx->boot_options, &boot_option->list);
67 talloc_steal(ctx, boot_option);
71 * device_handler_get_device_count - Get the count of current handler devices.
74 int device_handler_get_device_count(const struct device_handler *handler)
76 return handler->n_devices;
80 * device_handler_get_device - Get a handler device by index.
83 const struct discover_device *device_handler_get_device(
84 const struct device_handler *handler, unsigned int index)
86 if (index >= handler->n_devices) {
87 assert(0 && "bad index");
91 return handler->devices[index];
94 struct discover_boot_option *discover_boot_option_create(
95 struct discover_context *ctx,
96 struct discover_device *device)
98 struct discover_boot_option *opt;
100 opt = talloc_zero(ctx, struct discover_boot_option);
101 opt->option = talloc_zero(opt, struct boot_option);
102 opt->device = device;
107 static int device_match_uuid(struct discover_device *dev, const char *uuid)
109 return dev->uuid && !strcmp(dev->uuid, uuid);
112 static int device_match_label(struct discover_device *dev, const char *label)
114 return dev->label && !strcmp(dev->label, label);
117 static int device_match_id(struct discover_device *dev, const char *id)
119 return !strcmp(dev->device->id, id);
122 static int device_match_serial(struct discover_device *dev, const char *serial)
124 const char *val = discover_device_get_param(dev, "ID_SERIAL");
125 return val && !strcmp(val, serial);
128 static struct discover_device *device_lookup(
129 struct device_handler *device_handler,
130 int (match_fn)(struct discover_device *, const char *),
133 struct discover_device *dev;
139 for (i = 0; i < device_handler->n_devices; i++) {
140 dev = device_handler->devices[i];
142 if (match_fn(dev, str))
149 struct discover_device *device_lookup_by_name(struct device_handler *handler,
152 if (!strncmp(name, "/dev/", strlen("/dev/")))
153 name += strlen("/dev/");
155 return device_lookup_by_id(handler, name);
158 struct discover_device *device_lookup_by_uuid(
159 struct device_handler *device_handler,
162 return device_lookup(device_handler, device_match_uuid, uuid);
165 struct discover_device *device_lookup_by_label(
166 struct device_handler *device_handler,
169 return device_lookup(device_handler, device_match_label, label);
172 struct discover_device *device_lookup_by_id(
173 struct device_handler *device_handler,
176 return device_lookup(device_handler, device_match_id, id);
179 struct discover_device *device_lookup_by_serial(
180 struct device_handler *device_handler,
183 return device_lookup(device_handler, device_match_serial, serial);
186 void device_handler_destroy(struct device_handler *handler)
188 talloc_free(handler);
191 static int destroy_device(void *arg)
193 struct discover_device *dev = arg;
200 struct discover_device *discover_device_create(struct device_handler *handler,
203 struct discover_device *dev;
205 dev = device_lookup_by_id(handler, id);
209 dev = talloc_zero(handler, struct discover_device);
210 dev->device = talloc_zero(dev, struct device);
211 dev->device->id = talloc_strdup(dev->device, id);
212 list_init(&dev->params);
213 list_init(&dev->boot_options);
215 talloc_set_destructor(dev, destroy_device);
220 struct discover_device_param {
223 struct list_item list;
226 void discover_device_set_param(struct discover_device *device,
227 const char *name, const char *value)
229 struct discover_device_param *param;
232 list_for_each_entry(&device->params, param, list) {
233 if (!strcmp(param->name, name)) {
242 param = talloc(device, struct discover_device_param);
243 param->name = talloc_strdup(param, name);
244 list_add(&device->params, ¶m->list);
247 list_remove(¶m->list);
251 talloc_free(param->value);
254 param->value = talloc_strdup(param, value);
257 const char *discover_device_get_param(struct discover_device *device,
260 struct discover_device_param *param;
262 list_for_each_entry(&device->params, param, list) {
263 if (!strcmp(param->name, name))
269 struct device_handler *device_handler_init(struct discover_server *server,
270 struct waitset *waitset, int dry_run)
272 struct device_handler *handler;
275 handler = talloc_zero(NULL, struct device_handler);
276 handler->server = server;
277 handler->waitset = waitset;
278 handler->dry_run = dry_run;
279 handler->autoboot_enabled = config_get()->autoboot_enabled;
281 list_init(&handler->unresolved_boot_options);
283 /* set up our mount point base */
284 pb_mkdir_recursive(mount_base());
288 if (config_get()->safe_mode)
291 rc = device_handler_init_sources(handler);
293 talloc_free(handler);
300 void device_handler_reinit(struct device_handler *handler)
302 struct discover_boot_option *opt, *tmp;
305 device_handler_cancel_default(handler);
307 /* free unresolved boot options */
308 list_for_each_entry_safe(&handler->unresolved_boot_options,
311 list_init(&handler->unresolved_boot_options);
313 /* drop all devices */
314 for (i = 0; i < handler->n_devices; i++)
315 discover_server_notify_device_remove(handler->server,
316 handler->devices[i]->device);
318 talloc_free(handler->devices);
319 handler->devices = NULL;
320 handler->n_devices = 0;
322 device_handler_reinit_sources(handler);
325 void device_handler_remove(struct device_handler *handler,
326 struct discover_device *device)
328 struct discover_boot_option *opt, *tmp;
331 for (i = 0; i < handler->n_devices; i++)
332 if (handler->devices[i] == device)
335 if (i == handler->n_devices) {
340 /* Free any unresolved options, as they're currently allocated
341 * against the handler */
342 list_for_each_entry_safe(&handler->unresolved_boot_options,
344 if (opt->device != device)
346 list_remove(&opt->list);
350 /* if this is a network device, we have to unregister it from the
352 if (device->device->type == DEVICE_TYPE_NETWORK)
353 network_unregister_device(handler->network, device);
355 handler->n_devices--;
356 memmove(&handler->devices[i], &handler->devices[i + 1],
357 (handler->n_devices - i) * sizeof(handler->devices[0]));
358 handler->devices = talloc_realloc(handler, handler->devices,
359 struct discover_device *, handler->n_devices);
361 if (device->notified)
362 discover_server_notify_device_remove(handler->server,
368 static void boot_status(void *arg, struct boot_status *status)
370 struct device_handler *handler = arg;
372 discover_server_notify_boot_status(handler->server, status);
375 static void countdown_status(struct device_handler *handler,
376 struct discover_boot_option *opt, unsigned int sec)
378 struct boot_status status;
380 status.type = BOOT_STATUS_INFO;
381 status.progress = -1;
382 status.detail = NULL;
383 status.message = talloc_asprintf(handler,
384 "Booting in %d sec: %s", sec, opt->option->name);
386 discover_server_notify_boot_status(handler->server, &status);
388 talloc_free(status.message);
391 static int default_timeout(void *arg)
393 struct device_handler *handler = arg;
394 struct discover_boot_option *opt;
396 if (!handler->default_boot_option)
399 if (handler->pending_boot)
402 opt = handler->default_boot_option;
404 if (handler->sec_to_boot) {
405 countdown_status(handler, opt, handler->sec_to_boot);
406 handler->sec_to_boot--;
407 handler->timeout_waiter = waiter_register_timeout(
408 handler->waitset, 1000,
409 default_timeout, handler);
413 handler->timeout_waiter = NULL;
415 pb_log("Timeout expired, booting default option %s\n", opt->option->id);
417 handler->pending_boot = boot(handler, handler->default_boot_option,
418 NULL, handler->dry_run, boot_status, handler);
419 handler->pending_boot_is_default = true;
423 static bool priority_match(struct boot_priority *prio,
424 struct discover_boot_option *opt)
426 return prio->type == opt->device->device->type ||
427 prio->type == DEVICE_TYPE_ANY;
430 static int default_option_priority(struct discover_boot_option *opt)
432 const struct config *config;
433 struct boot_priority *prio;
436 config = config_get();
438 for (i = 0; i < config->n_boot_priorities; i++) {
439 prio = &config->boot_priorities[i];
440 if (priority_match(prio, opt))
441 return prio->priority;
447 static bool device_allows_default(struct discover_device *dev)
451 dev_str = config_get()->boot_device;
453 if (!dev_str || !strlen(dev_str))
456 /* default devices are specified by UUIDs at present */
457 if (strcmp(dev->uuid, dev_str))
463 static void set_default(struct device_handler *handler,
464 struct discover_boot_option *opt)
468 if (!handler->autoboot_enabled)
471 /* do we allow default-booting from this device? */
472 if (!device_allows_default(opt->device))
475 new_prio = default_option_priority(opt);
477 /* A negative priority indicates that we don't want to boot this device
482 /* Resolve any conflicts: if we have a new default option, it only
483 * replaces the current if it has a higher priority. */
484 if (handler->default_boot_option) {
487 cur_prio = default_option_priority(
488 handler->default_boot_option);
490 if (new_prio > cur_prio) {
491 handler->default_boot_option = opt;
492 /* extend the timeout a little, so the user sees some
493 * indication of the change */
494 handler->sec_to_boot += 2;
500 handler->sec_to_boot = config_get()->autoboot_timeout_sec;
501 handler->default_boot_option = opt;
503 pb_log("Boot option %s set as default, timeout %u sec.\n",
504 opt->option->id, handler->sec_to_boot);
506 default_timeout(handler);
509 static bool resource_is_resolved(struct resource *res)
511 return !res || res->resolved;
514 /* We only use this in an assert, which will disappear if we're compiling
515 * with NDEBUG, so we need the 'used' attribute for these builds */
516 static bool __attribute__((used)) boot_option_is_resolved(
517 struct discover_boot_option *opt)
519 return resource_is_resolved(opt->boot_image) &&
520 resource_is_resolved(opt->initrd) &&
521 resource_is_resolved(opt->dtb) &&
522 resource_is_resolved(opt->icon);
525 static bool resource_resolve(struct resource *res, const char *name,
526 struct discover_boot_option *opt,
527 struct device_handler *handler)
529 struct parser *parser = opt->source;
531 if (resource_is_resolved(res))
534 pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
535 opt->option->id, name, parser->name);
536 parser->resolve_resource(handler, res);
538 return res->resolved;
541 static bool boot_option_resolve(struct discover_boot_option *opt,
542 struct device_handler *handler)
544 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
545 resource_resolve(opt->initrd, "initrd", opt, handler) &&
546 resource_resolve(opt->dtb, "dtb", opt, handler) &&
547 resource_resolve(opt->icon, "icon", opt, handler);
550 static void boot_option_finalise(struct device_handler *handler,
551 struct discover_boot_option *opt)
553 assert(boot_option_is_resolved(opt));
555 /* check that the parsers haven't set any of the final data */
556 assert(!opt->option->boot_image_file);
557 assert(!opt->option->initrd_file);
558 assert(!opt->option->dtb_file);
559 assert(!opt->option->icon_file);
560 assert(!opt->option->device_id);
563 opt->option->boot_image_file = opt->boot_image->url->full;
565 opt->option->initrd_file = opt->initrd->url->full;
567 opt->option->dtb_file = opt->dtb->url->full;
569 opt->option->icon_file = opt->icon->url->full;
571 opt->option->device_id = opt->device->device->id;
573 if (opt->option->is_default)
574 set_default(handler, opt);
577 static void notify_boot_option(struct device_handler *handler,
578 struct discover_boot_option *opt)
580 struct discover_device *dev = opt->device;
583 discover_server_notify_device_add(handler->server,
584 opt->device->device);
585 dev->notified = true;
586 discover_server_notify_boot_option_add(handler->server, opt->option);
589 static void process_boot_option_queue(struct device_handler *handler)
591 struct discover_boot_option *opt, *tmp;
593 list_for_each_entry_safe(&handler->unresolved_boot_options,
596 pb_debug("queue: attempting resolution for %s\n",
599 if (!boot_option_resolve(opt, handler))
602 pb_debug("\tresolved!\n");
604 list_remove(&opt->list);
605 list_add_tail(&opt->device->boot_options, &opt->list);
606 talloc_steal(opt->device, opt);
607 boot_option_finalise(handler, opt);
608 notify_boot_option(handler, opt);
612 struct discover_context *device_handler_discover_context_create(
613 struct device_handler *handler,
614 struct discover_device *device)
616 struct discover_context *ctx;
618 ctx = talloc_zero(handler, struct discover_context);
619 ctx->device = device;
620 list_init(&ctx->boot_options);
626 * context_commit - Commit a temporary discovery context to the handler,
627 * and notify the clients about any new options / devices
629 void device_handler_discover_context_commit(struct device_handler *handler,
630 struct discover_context *ctx)
632 struct discover_device *dev = ctx->device;
633 struct discover_boot_option *opt, *tmp;
635 if (!device_lookup_by_id(handler, dev->device->id))
636 device_handler_add_device(handler, dev);
638 /* move boot options from the context to the device */
639 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
640 list_remove(&opt->list);
642 if (boot_option_resolve(opt, handler)) {
643 pb_log("boot option %s is resolved, "
644 "sending to clients\n",
646 list_add_tail(&dev->boot_options, &opt->list);
647 talloc_steal(dev, opt);
648 boot_option_finalise(handler, opt);
649 notify_boot_option(handler, opt);
651 if (!opt->source->resolve_resource) {
652 pb_log("parser %s gave us an unresolved "
653 "resource (%s), but no way to "
655 opt->source->name, opt->option->id);
658 pb_log("boot option %s is unresolved, "
661 list_add(&handler->unresolved_boot_options,
663 talloc_steal(handler, opt);
669 void device_handler_add_device(struct device_handler *handler,
670 struct discover_device *device)
672 handler->n_devices++;
673 handler->devices = talloc_realloc(handler, handler->devices,
674 struct discover_device *, handler->n_devices);
675 handler->devices[handler->n_devices - 1] = device;
677 if (device->device->type == DEVICE_TYPE_NETWORK)
678 network_register_device(handler->network, device);
681 /* Start discovery on a hotplugged device. The device will be in our devices
682 * array, but has only just been initialised by the hotplug source.
684 int device_handler_discover(struct device_handler *handler,
685 struct discover_device *dev)
687 struct discover_context *ctx;
690 process_boot_option_queue(handler);
692 /* create our context */
693 ctx = device_handler_discover_context_create(handler, dev);
695 rc = mount_device(dev);
699 /* add this device to our system info */
700 system_info_register_blockdev(dev->device->id, dev->uuid,
703 /* run the parsers. This will populate the ctx's boot_option list. */
704 iterate_parsers(ctx);
706 /* add discovered stuff to the handler */
707 device_handler_discover_context_commit(handler, ctx);
715 /* Incoming dhcp event */
716 int device_handler_dhcp(struct device_handler *handler,
717 struct discover_device *dev, struct event *event)
719 struct discover_context *ctx;
721 /* create our context */
722 ctx = device_handler_discover_context_create(handler, dev);
725 iterate_parsers(ctx);
727 device_handler_discover_context_commit(handler, ctx);
734 /* incoming conf event */
735 int device_handler_conf(struct device_handler *handler,
736 struct discover_device *dev, struct pb_url *url)
738 struct discover_context *ctx;
740 /* create our context */
741 ctx = device_handler_discover_context_create(handler, dev);
744 iterate_parsers(ctx);
746 device_handler_discover_context_commit(handler, ctx);
753 static struct discover_boot_option *find_boot_option_by_id(
754 struct device_handler *handler, const char *id)
758 for (i = 0; i < handler->n_devices; i++) {
759 struct discover_device *dev = handler->devices[i];
760 struct discover_boot_option *opt;
762 list_for_each_entry(&dev->boot_options, opt, list)
763 if (!strcmp(opt->option->id, id))
770 void device_handler_boot(struct device_handler *handler,
771 struct boot_command *cmd)
773 struct discover_boot_option *opt = NULL;
775 if (cmd->option_id && strlen(cmd->option_id))
776 opt = find_boot_option_by_id(handler, cmd->option_id);
778 if (handler->pending_boot)
779 boot_cancel(handler->pending_boot);
780 handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
781 boot_status, handler);
782 handler->pending_boot_is_default = false;
785 void device_handler_cancel_default(struct device_handler *handler)
787 struct boot_status status;
789 if (handler->timeout_waiter)
790 waiter_remove(handler->timeout_waiter);
792 handler->timeout_waiter = NULL;
793 handler->autoboot_enabled = false;
795 /* we only send status if we had a default boot option queued */
796 if (!handler->default_boot_option)
799 pb_log("Cancelling default boot option\n");
801 if (handler->pending_boot && handler->pending_boot_is_default) {
802 boot_cancel(handler->pending_boot);
803 handler->pending_boot = NULL;
804 handler->pending_boot_is_default = false;
807 handler->default_boot_option = NULL;
809 status.type = BOOT_STATUS_INFO;
810 status.progress = -1;
811 status.detail = NULL;
812 status.message = "Default boot cancelled";
814 discover_server_notify_boot_status(handler->server, &status);
817 void device_handler_update_config(struct device_handler *handler,
818 struct config *config)
822 rc = config_set(config);
826 discover_server_notify_config(handler->server, config);
827 device_handler_reinit(handler);
830 #ifndef PETITBOOT_TEST
832 static int device_handler_init_sources(struct device_handler *handler)
834 /* init our device sources: udev, network and user events */
835 handler->udev = udev_init(handler, handler->waitset);
839 handler->network = network_init(handler, handler->waitset,
841 if (!handler->network)
844 handler->user_event = user_event_init(handler, handler->waitset);
845 if (!handler->user_event)
851 static void device_handler_reinit_sources(struct device_handler *handler)
853 /* if we haven't initialised sources previously (becuase we started in
854 * safe mode), then init once here. */
855 if (!(handler->udev || handler->network || handler->user_event)) {
856 device_handler_init_sources(handler);
860 udev_reinit(handler->udev);
862 network_shutdown(handler->network);
863 handler->network = network_init(handler, handler->waitset,
867 static bool check_existing_mount(struct discover_device *dev)
869 struct stat devstat, mntstat;
874 rc = stat(dev->device_path, &devstat);
876 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
880 if (!S_ISBLK(devstat.st_mode)) {
881 pb_debug("%s: %s isn't a block device?\n", __func__,
886 fp = fopen("/proc/self/mounts", "r");
893 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
896 rc = stat(mnt->mnt_fsname, &mntstat);
900 if (!S_ISBLK(mntstat.st_mode))
903 if (mntstat.st_rdev == devstat.st_rdev) {
904 dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
905 dev->mounted_rw = !!hasmntopt(mnt, "rw");
907 dev->unmount = false;
909 pb_debug("%s: %s is already mounted (r%c) at %s\n",
910 __func__, dev->device_path,
911 dev->mounted_rw ? 'w' : 'o',
922 static int mount_device(struct discover_device *dev)
927 if (!dev->device_path)
933 if (check_existing_mount(dev))
936 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
940 dev->mount_path = join_paths(dev, mount_base(),
943 if (pb_mkdir_recursive(dev->mount_path)) {
944 pb_log("couldn't create mount directory %s: %s\n",
945 dev->mount_path, strerror(errno));
949 pb_log("mounting device %s read-only\n", dev->device_path);
951 rc = mount(dev->device_path, dev->mount_path, fstype,
952 MS_RDONLY | MS_SILENT, "");
955 dev->mounted_rw = false;
960 pb_log("couldn't mount device %s: mount failed: %s\n",
961 dev->device_path, strerror(errno));
963 pb_rmdir_recursive(mount_base(), dev->mount_path);
965 talloc_free(dev->mount_path);
966 dev->mount_path = NULL;
970 static int umount_device(struct discover_device *dev)
974 if (!dev->mounted || !dev->unmount)
977 pb_log("unmounting device %s\n", dev->device_path);
978 rc = umount(dev->mount_path);
982 dev->mounted = false;
984 pb_rmdir_recursive(mount_base(), dev->mount_path);
986 talloc_free(dev->mount_path);
987 dev->mount_path = NULL;
992 int device_request_write(struct discover_device *dev, bool *release)
1001 if (dev->mounted_rw)
1004 pb_log("remounting device %s read-write\n", dev->device_path);
1005 rc = mount(dev->device_path, dev->mount_path, "",
1006 MS_REMOUNT | MS_SILENT, "");
1010 dev->mounted_rw = true;
1015 void device_release_write(struct discover_device *dev, bool release)
1020 pb_log("remounting device %s read-only\n", dev->device_path);
1021 mount(dev->device_path, dev->mount_path, "",
1022 MS_REMOUNT | MS_RDONLY | MS_SILENT, "");
1023 dev->mounted_rw = false;
1028 static int device_handler_init_sources(
1029 struct device_handler *handler __attribute__((unused)))
1034 static void device_handler_reinit_sources(
1035 struct device_handler *handler __attribute__((unused)))
1039 static int umount_device(struct discover_device *dev __attribute__((unused)))
1044 static int __attribute__((unused)) mount_device(
1045 struct discover_device *dev __attribute__((unused)))
1050 int device_request_write(struct discover_device *dev __attribute__((unused)),
1057 void device_release_write(struct discover_device *dev __attribute__((unused)),
1058 bool release __attribute__((unused)))