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>
19 #include <i18n/i18n.h>
21 #include "device-handler.h"
22 #include "discover-server.h"
23 #include "user-event.h"
34 struct device_handler {
35 struct discover_server *server;
39 struct network *network;
40 struct user_event *user_event;
42 struct discover_device **devices;
43 unsigned int n_devices;
45 struct waitset *waitset;
46 struct waiter *timeout_waiter;
47 bool autoboot_enabled;
48 unsigned int sec_to_boot;
50 struct discover_boot_option *default_boot_option;
51 struct list unresolved_boot_options;
53 struct boot_task *pending_boot;
54 bool pending_boot_is_default;
57 static int mount_device(struct discover_device *dev);
58 static int umount_device(struct discover_device *dev);
60 static int device_handler_init_sources(struct device_handler *handler);
61 static void device_handler_reinit_sources(struct device_handler *handler);
63 static void device_handler_update_lang(const char *lang);
65 void discover_context_add_boot_option(struct discover_context *ctx,
66 struct discover_boot_option *boot_option)
68 boot_option->source = ctx->parser;
69 list_add_tail(&ctx->boot_options, &boot_option->list);
70 talloc_steal(ctx, boot_option);
74 * device_handler_get_device_count - Get the count of current handler devices.
77 int device_handler_get_device_count(const struct device_handler *handler)
79 return handler->n_devices;
83 * device_handler_get_device - Get a handler device by index.
86 const struct discover_device *device_handler_get_device(
87 const struct device_handler *handler, unsigned int index)
89 if (index >= handler->n_devices) {
90 assert(0 && "bad index");
94 return handler->devices[index];
97 struct discover_boot_option *discover_boot_option_create(
98 struct discover_context *ctx,
99 struct discover_device *device)
101 struct discover_boot_option *opt;
103 opt = talloc_zero(ctx, struct discover_boot_option);
104 opt->option = talloc_zero(opt, struct boot_option);
105 opt->device = device;
110 static int device_match_uuid(struct discover_device *dev, const char *uuid)
112 return dev->uuid && !strcmp(dev->uuid, uuid);
115 static int device_match_label(struct discover_device *dev, const char *label)
117 return dev->label && !strcmp(dev->label, label);
120 static int device_match_id(struct discover_device *dev, const char *id)
122 return !strcmp(dev->device->id, id);
125 static int device_match_serial(struct discover_device *dev, const char *serial)
127 const char *val = discover_device_get_param(dev, "ID_SERIAL");
128 return val && !strcmp(val, serial);
131 static struct discover_device *device_lookup(
132 struct device_handler *device_handler,
133 int (match_fn)(struct discover_device *, const char *),
136 struct discover_device *dev;
142 for (i = 0; i < device_handler->n_devices; i++) {
143 dev = device_handler->devices[i];
145 if (match_fn(dev, str))
152 struct discover_device *device_lookup_by_name(struct device_handler *handler,
155 if (!strncmp(name, "/dev/", strlen("/dev/")))
156 name += strlen("/dev/");
158 return device_lookup_by_id(handler, name);
161 struct discover_device *device_lookup_by_uuid(
162 struct device_handler *device_handler,
165 return device_lookup(device_handler, device_match_uuid, uuid);
168 struct discover_device *device_lookup_by_label(
169 struct device_handler *device_handler,
172 return device_lookup(device_handler, device_match_label, label);
175 struct discover_device *device_lookup_by_id(
176 struct device_handler *device_handler,
179 return device_lookup(device_handler, device_match_id, id);
182 struct discover_device *device_lookup_by_serial(
183 struct device_handler *device_handler,
186 return device_lookup(device_handler, device_match_serial, serial);
189 void device_handler_destroy(struct device_handler *handler)
191 talloc_free(handler);
194 static int destroy_device(void *arg)
196 struct discover_device *dev = arg;
203 struct discover_device *discover_device_create(struct device_handler *handler,
206 struct discover_device *dev;
208 dev = device_lookup_by_id(handler, id);
212 dev = talloc_zero(handler, struct discover_device);
213 dev->device = talloc_zero(dev, struct device);
214 dev->device->id = talloc_strdup(dev->device, id);
215 list_init(&dev->params);
216 list_init(&dev->boot_options);
218 talloc_set_destructor(dev, destroy_device);
223 struct discover_device_param {
226 struct list_item list;
229 void discover_device_set_param(struct discover_device *device,
230 const char *name, const char *value)
232 struct discover_device_param *param;
235 list_for_each_entry(&device->params, param, list) {
236 if (!strcmp(param->name, name)) {
245 param = talloc(device, struct discover_device_param);
246 param->name = talloc_strdup(param, name);
247 list_add(&device->params, ¶m->list);
250 list_remove(¶m->list);
254 talloc_free(param->value);
257 param->value = talloc_strdup(param, value);
260 const char *discover_device_get_param(struct discover_device *device,
263 struct discover_device_param *param;
265 list_for_each_entry(&device->params, param, list) {
266 if (!strcmp(param->name, name))
272 struct device_handler *device_handler_init(struct discover_server *server,
273 struct waitset *waitset, int dry_run)
275 struct device_handler *handler;
278 handler = talloc_zero(NULL, struct device_handler);
279 handler->server = server;
280 handler->waitset = waitset;
281 handler->dry_run = dry_run;
282 handler->autoboot_enabled = config_get()->autoboot_enabled;
284 list_init(&handler->unresolved_boot_options);
286 /* set up our mount point base */
287 pb_mkdir_recursive(mount_base());
291 if (config_get()->safe_mode)
294 rc = device_handler_init_sources(handler);
296 talloc_free(handler);
303 void device_handler_reinit(struct device_handler *handler)
305 struct discover_boot_option *opt, *tmp;
308 device_handler_cancel_default(handler);
310 /* free unresolved boot options */
311 list_for_each_entry_safe(&handler->unresolved_boot_options,
314 list_init(&handler->unresolved_boot_options);
316 /* drop all devices */
317 for (i = 0; i < handler->n_devices; i++)
318 discover_server_notify_device_remove(handler->server,
319 handler->devices[i]->device);
321 talloc_free(handler->devices);
322 handler->devices = NULL;
323 handler->n_devices = 0;
325 device_handler_reinit_sources(handler);
328 void device_handler_remove(struct device_handler *handler,
329 struct discover_device *device)
331 struct discover_boot_option *opt, *tmp;
334 for (i = 0; i < handler->n_devices; i++)
335 if (handler->devices[i] == device)
338 if (i == handler->n_devices) {
343 /* Free any unresolved options, as they're currently allocated
344 * against the handler */
345 list_for_each_entry_safe(&handler->unresolved_boot_options,
347 if (opt->device != device)
349 list_remove(&opt->list);
353 /* if this is a network device, we have to unregister it from the
355 if (device->device->type == DEVICE_TYPE_NETWORK)
356 network_unregister_device(handler->network, device);
358 handler->n_devices--;
359 memmove(&handler->devices[i], &handler->devices[i + 1],
360 (handler->n_devices - i) * sizeof(handler->devices[0]));
361 handler->devices = talloc_realloc(handler, handler->devices,
362 struct discover_device *, handler->n_devices);
364 if (device->notified)
365 discover_server_notify_device_remove(handler->server,
371 static void boot_status(void *arg, struct boot_status *status)
373 struct device_handler *handler = arg;
375 discover_server_notify_boot_status(handler->server, status);
378 static void countdown_status(struct device_handler *handler,
379 struct discover_boot_option *opt, unsigned int sec)
381 struct boot_status status;
383 status.type = BOOT_STATUS_INFO;
384 status.progress = -1;
385 status.detail = NULL;
386 status.message = talloc_asprintf(handler,
387 "Booting in %d sec: %s", sec, opt->option->name);
389 discover_server_notify_boot_status(handler->server, &status);
391 talloc_free(status.message);
394 static int default_timeout(void *arg)
396 struct device_handler *handler = arg;
397 struct discover_boot_option *opt;
399 if (!handler->default_boot_option)
402 if (handler->pending_boot)
405 opt = handler->default_boot_option;
407 if (handler->sec_to_boot) {
408 countdown_status(handler, opt, handler->sec_to_boot);
409 handler->sec_to_boot--;
410 handler->timeout_waiter = waiter_register_timeout(
411 handler->waitset, 1000,
412 default_timeout, handler);
416 handler->timeout_waiter = NULL;
418 pb_log("Timeout expired, booting default option %s\n", opt->option->id);
420 handler->pending_boot = boot(handler, handler->default_boot_option,
421 NULL, handler->dry_run, boot_status, handler);
422 handler->pending_boot_is_default = true;
426 static bool priority_match(struct boot_priority *prio,
427 struct discover_boot_option *opt)
429 return prio->type == opt->device->device->type ||
430 prio->type == DEVICE_TYPE_ANY;
433 static int default_option_priority(struct discover_boot_option *opt)
435 const struct config *config;
436 struct boot_priority *prio;
439 config = config_get();
441 for (i = 0; i < config->n_boot_priorities; i++) {
442 prio = &config->boot_priorities[i];
443 if (priority_match(prio, opt))
444 return prio->priority;
450 static bool device_allows_default(struct discover_device *dev)
454 dev_str = config_get()->boot_device;
456 if (!dev_str || !strlen(dev_str))
459 /* default devices are specified by UUIDs at present */
460 if (strcmp(dev->uuid, dev_str))
466 static void set_default(struct device_handler *handler,
467 struct discover_boot_option *opt)
471 if (!handler->autoboot_enabled)
474 /* do we allow default-booting from this device? */
475 if (!device_allows_default(opt->device))
478 new_prio = default_option_priority(opt);
480 /* A negative priority indicates that we don't want to boot this device
485 /* Resolve any conflicts: if we have a new default option, it only
486 * replaces the current if it has a higher priority. */
487 if (handler->default_boot_option) {
490 cur_prio = default_option_priority(
491 handler->default_boot_option);
493 if (new_prio > cur_prio) {
494 handler->default_boot_option = opt;
495 /* extend the timeout a little, so the user sees some
496 * indication of the change */
497 handler->sec_to_boot += 2;
503 handler->sec_to_boot = config_get()->autoboot_timeout_sec;
504 handler->default_boot_option = opt;
506 pb_log("Boot option %s set as default, timeout %u sec.\n",
507 opt->option->id, handler->sec_to_boot);
509 default_timeout(handler);
512 static bool resource_is_resolved(struct resource *res)
514 return !res || res->resolved;
517 /* We only use this in an assert, which will disappear if we're compiling
518 * with NDEBUG, so we need the 'used' attribute for these builds */
519 static bool __attribute__((used)) boot_option_is_resolved(
520 struct discover_boot_option *opt)
522 return resource_is_resolved(opt->boot_image) &&
523 resource_is_resolved(opt->initrd) &&
524 resource_is_resolved(opt->dtb) &&
525 resource_is_resolved(opt->icon);
528 static bool resource_resolve(struct resource *res, const char *name,
529 struct discover_boot_option *opt,
530 struct device_handler *handler)
532 struct parser *parser = opt->source;
534 if (resource_is_resolved(res))
537 pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
538 opt->option->id, name, parser->name);
539 parser->resolve_resource(handler, res);
541 return res->resolved;
544 static bool boot_option_resolve(struct discover_boot_option *opt,
545 struct device_handler *handler)
547 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
548 resource_resolve(opt->initrd, "initrd", opt, handler) &&
549 resource_resolve(opt->dtb, "dtb", opt, handler) &&
550 resource_resolve(opt->icon, "icon", opt, handler);
553 static void boot_option_finalise(struct device_handler *handler,
554 struct discover_boot_option *opt)
556 assert(boot_option_is_resolved(opt));
558 /* check that the parsers haven't set any of the final data */
559 assert(!opt->option->boot_image_file);
560 assert(!opt->option->initrd_file);
561 assert(!opt->option->dtb_file);
562 assert(!opt->option->icon_file);
563 assert(!opt->option->device_id);
566 opt->option->boot_image_file = opt->boot_image->url->full;
568 opt->option->initrd_file = opt->initrd->url->full;
570 opt->option->dtb_file = opt->dtb->url->full;
572 opt->option->icon_file = opt->icon->url->full;
574 opt->option->device_id = opt->device->device->id;
576 if (opt->option->is_default)
577 set_default(handler, opt);
580 static void notify_boot_option(struct device_handler *handler,
581 struct discover_boot_option *opt)
583 struct discover_device *dev = opt->device;
586 discover_server_notify_device_add(handler->server,
587 opt->device->device);
588 dev->notified = true;
589 discover_server_notify_boot_option_add(handler->server, opt->option);
592 static void process_boot_option_queue(struct device_handler *handler)
594 struct discover_boot_option *opt, *tmp;
596 list_for_each_entry_safe(&handler->unresolved_boot_options,
599 pb_debug("queue: attempting resolution for %s\n",
602 if (!boot_option_resolve(opt, handler))
605 pb_debug("\tresolved!\n");
607 list_remove(&opt->list);
608 list_add_tail(&opt->device->boot_options, &opt->list);
609 talloc_steal(opt->device, opt);
610 boot_option_finalise(handler, opt);
611 notify_boot_option(handler, opt);
615 struct discover_context *device_handler_discover_context_create(
616 struct device_handler *handler,
617 struct discover_device *device)
619 struct discover_context *ctx;
621 ctx = talloc_zero(handler, struct discover_context);
622 ctx->device = device;
623 list_init(&ctx->boot_options);
629 * context_commit - Commit a temporary discovery context to the handler,
630 * and notify the clients about any new options / devices
632 void device_handler_discover_context_commit(struct device_handler *handler,
633 struct discover_context *ctx)
635 struct discover_device *dev = ctx->device;
636 struct discover_boot_option *opt, *tmp;
638 if (!device_lookup_by_id(handler, dev->device->id))
639 device_handler_add_device(handler, dev);
641 /* move boot options from the context to the device */
642 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
643 list_remove(&opt->list);
645 if (boot_option_resolve(opt, handler)) {
646 pb_log("boot option %s is resolved, "
647 "sending to clients\n",
649 list_add_tail(&dev->boot_options, &opt->list);
650 talloc_steal(dev, opt);
651 boot_option_finalise(handler, opt);
652 notify_boot_option(handler, opt);
654 if (!opt->source->resolve_resource) {
655 pb_log("parser %s gave us an unresolved "
656 "resource (%s), but no way to "
658 opt->source->name, opt->option->id);
661 pb_log("boot option %s is unresolved, "
664 list_add(&handler->unresolved_boot_options,
666 talloc_steal(handler, opt);
672 void device_handler_add_device(struct device_handler *handler,
673 struct discover_device *device)
675 handler->n_devices++;
676 handler->devices = talloc_realloc(handler, handler->devices,
677 struct discover_device *, handler->n_devices);
678 handler->devices[handler->n_devices - 1] = device;
680 if (device->device->type == DEVICE_TYPE_NETWORK)
681 network_register_device(handler->network, device);
684 /* Start discovery on a hotplugged device. The device will be in our devices
685 * array, but has only just been initialised by the hotplug source.
687 int device_handler_discover(struct device_handler *handler,
688 struct discover_device *dev)
690 struct discover_context *ctx;
693 process_boot_option_queue(handler);
695 /* create our context */
696 ctx = device_handler_discover_context_create(handler, dev);
698 rc = mount_device(dev);
702 /* add this device to our system info */
703 system_info_register_blockdev(dev->device->id, dev->uuid,
706 /* run the parsers. This will populate the ctx's boot_option list. */
707 iterate_parsers(ctx);
709 /* add discovered stuff to the handler */
710 device_handler_discover_context_commit(handler, ctx);
718 /* Incoming dhcp event */
719 int device_handler_dhcp(struct device_handler *handler,
720 struct discover_device *dev, struct event *event)
722 struct discover_context *ctx;
724 /* create our context */
725 ctx = device_handler_discover_context_create(handler, dev);
728 iterate_parsers(ctx);
730 device_handler_discover_context_commit(handler, ctx);
737 /* incoming conf event */
738 int device_handler_conf(struct device_handler *handler,
739 struct discover_device *dev, struct pb_url *url)
741 struct discover_context *ctx;
743 /* create our context */
744 ctx = device_handler_discover_context_create(handler, dev);
747 iterate_parsers(ctx);
749 device_handler_discover_context_commit(handler, ctx);
756 static struct discover_boot_option *find_boot_option_by_id(
757 struct device_handler *handler, const char *id)
761 for (i = 0; i < handler->n_devices; i++) {
762 struct discover_device *dev = handler->devices[i];
763 struct discover_boot_option *opt;
765 list_for_each_entry(&dev->boot_options, opt, list)
766 if (!strcmp(opt->option->id, id))
773 void device_handler_boot(struct device_handler *handler,
774 struct boot_command *cmd)
776 struct discover_boot_option *opt = NULL;
778 if (cmd->option_id && strlen(cmd->option_id))
779 opt = find_boot_option_by_id(handler, cmd->option_id);
781 if (handler->pending_boot)
782 boot_cancel(handler->pending_boot);
783 handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
784 boot_status, handler);
785 handler->pending_boot_is_default = false;
788 void device_handler_cancel_default(struct device_handler *handler)
790 struct boot_status status;
792 if (handler->timeout_waiter)
793 waiter_remove(handler->timeout_waiter);
795 handler->timeout_waiter = NULL;
796 handler->autoboot_enabled = false;
798 /* we only send status if we had a default boot option queued */
799 if (!handler->default_boot_option)
802 pb_log("Cancelling default boot option\n");
804 if (handler->pending_boot && handler->pending_boot_is_default) {
805 boot_cancel(handler->pending_boot);
806 handler->pending_boot = NULL;
807 handler->pending_boot_is_default = false;
810 handler->default_boot_option = NULL;
812 status.type = BOOT_STATUS_INFO;
813 status.progress = -1;
814 status.detail = NULL;
815 status.message = "Default boot cancelled";
817 discover_server_notify_boot_status(handler->server, &status);
820 void device_handler_update_config(struct device_handler *handler,
821 struct config *config)
825 rc = config_set(config);
829 discover_server_notify_config(handler->server, config);
830 device_handler_update_lang(config->lang);
831 device_handler_reinit(handler);
834 #ifndef PETITBOOT_TEST
836 static void device_handler_update_lang(const char *lang)
838 const char *cur_lang;
843 cur_lang = setlocale(LC_ALL, NULL);
844 if (cur_lang && !strcmp(cur_lang, lang))
847 setlocale(LC_ALL, lang);
850 static int device_handler_init_sources(struct device_handler *handler)
852 /* init our device sources: udev, network and user events */
853 handler->udev = udev_init(handler, handler->waitset);
857 handler->network = network_init(handler, handler->waitset,
859 if (!handler->network)
862 handler->user_event = user_event_init(handler, handler->waitset);
863 if (!handler->user_event)
869 static void device_handler_reinit_sources(struct device_handler *handler)
871 /* if we haven't initialised sources previously (becuase we started in
872 * safe mode), then init once here. */
873 if (!(handler->udev || handler->network || handler->user_event)) {
874 device_handler_init_sources(handler);
878 udev_reinit(handler->udev);
880 network_shutdown(handler->network);
881 handler->network = network_init(handler, handler->waitset,
885 static bool check_existing_mount(struct discover_device *dev)
887 struct stat devstat, mntstat;
892 rc = stat(dev->device_path, &devstat);
894 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
898 if (!S_ISBLK(devstat.st_mode)) {
899 pb_debug("%s: %s isn't a block device?\n", __func__,
904 fp = fopen("/proc/self/mounts", "r");
911 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
914 rc = stat(mnt->mnt_fsname, &mntstat);
918 if (!S_ISBLK(mntstat.st_mode))
921 if (mntstat.st_rdev == devstat.st_rdev) {
922 dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
923 dev->mounted_rw = !!hasmntopt(mnt, "rw");
925 dev->unmount = false;
927 pb_debug("%s: %s is already mounted (r%c) at %s\n",
928 __func__, dev->device_path,
929 dev->mounted_rw ? 'w' : 'o',
940 static int mount_device(struct discover_device *dev)
945 if (!dev->device_path)
951 if (check_existing_mount(dev))
954 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
958 dev->mount_path = join_paths(dev, mount_base(),
961 if (pb_mkdir_recursive(dev->mount_path)) {
962 pb_log("couldn't create mount directory %s: %s\n",
963 dev->mount_path, strerror(errno));
967 pb_log("mounting device %s read-only\n", dev->device_path);
969 rc = mount(dev->device_path, dev->mount_path, fstype,
970 MS_RDONLY | MS_SILENT, "");
973 dev->mounted_rw = false;
978 pb_log("couldn't mount device %s: mount failed: %s\n",
979 dev->device_path, strerror(errno));
981 pb_rmdir_recursive(mount_base(), dev->mount_path);
983 talloc_free(dev->mount_path);
984 dev->mount_path = NULL;
988 static int umount_device(struct discover_device *dev)
992 if (!dev->mounted || !dev->unmount)
995 pb_log("unmounting device %s\n", dev->device_path);
996 rc = umount(dev->mount_path);
1000 dev->mounted = false;
1002 pb_rmdir_recursive(mount_base(), dev->mount_path);
1004 talloc_free(dev->mount_path);
1005 dev->mount_path = NULL;
1010 int device_request_write(struct discover_device *dev, bool *release)
1019 if (dev->mounted_rw)
1022 pb_log("remounting device %s read-write\n", dev->device_path);
1023 rc = mount(dev->device_path, dev->mount_path, "",
1024 MS_REMOUNT | MS_SILENT, "");
1028 dev->mounted_rw = true;
1033 void device_release_write(struct discover_device *dev, bool release)
1038 pb_log("remounting device %s read-only\n", dev->device_path);
1039 mount(dev->device_path, dev->mount_path, "",
1040 MS_REMOUNT | MS_RDONLY | MS_SILENT, "");
1041 dev->mounted_rw = false;
1046 static void device_handler_update_lang(const char *lang __attribute__((unused)))
1050 static int device_handler_init_sources(
1051 struct device_handler *handler __attribute__((unused)))
1056 static void device_handler_reinit_sources(
1057 struct device_handler *handler __attribute__((unused)))
1061 static int umount_device(struct discover_device *dev __attribute__((unused)))
1066 static int __attribute__((unused)) mount_device(
1067 struct discover_device *dev __attribute__((unused)))
1072 int device_request_write(struct discover_device *dev __attribute__((unused)),
1079 void device_release_write(struct discover_device *dev __attribute__((unused)),
1080 bool release __attribute__((unused)))