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"
30 struct device_handler {
31 struct discover_server *server;
34 struct discover_device **devices;
35 unsigned int n_devices;
37 struct waitset *waitset;
38 struct waiter *timeout_waiter;
39 bool autoboot_enabled;
40 unsigned int sec_to_boot;
42 struct discover_boot_option *default_boot_option;
43 struct list unresolved_boot_options;
45 struct boot_task *pending_boot;
46 bool pending_boot_is_default;
49 static int mount_device(struct discover_device *dev);
50 static int umount_device(struct discover_device *dev);
52 void discover_context_add_boot_option(struct discover_context *ctx,
53 struct discover_boot_option *boot_option)
55 boot_option->source = ctx->parser;
56 list_add_tail(&ctx->boot_options, &boot_option->list);
57 talloc_steal(ctx, boot_option);
61 * device_handler_get_device_count - Get the count of current handler devices.
64 int device_handler_get_device_count(const struct device_handler *handler)
66 return handler->n_devices;
70 * device_handler_get_device - Get a handler device by index.
73 const struct discover_device *device_handler_get_device(
74 const struct device_handler *handler, unsigned int index)
76 if (index >= handler->n_devices) {
77 assert(0 && "bad index");
81 return handler->devices[index];
84 struct discover_boot_option *discover_boot_option_create(
85 struct discover_context *ctx,
86 struct discover_device *device)
88 struct discover_boot_option *opt;
90 opt = talloc_zero(ctx, struct discover_boot_option);
91 opt->option = talloc_zero(opt, struct boot_option);
97 static int device_match_uuid(struct discover_device *dev, const char *uuid)
99 return dev->uuid && !strcmp(dev->uuid, uuid);
102 static int device_match_label(struct discover_device *dev, const char *label)
104 return dev->label && !strcmp(dev->label, label);
107 static int device_match_id(struct discover_device *dev, const char *id)
109 return !strcmp(dev->device->id, id);
112 static int device_match_serial(struct discover_device *dev, const char *serial)
114 const char *val = discover_device_get_param(dev, "ID_SERIAL");
115 return val && !strcmp(val, serial);
118 static struct discover_device *device_lookup(
119 struct device_handler *device_handler,
120 int (match_fn)(struct discover_device *, const char *),
123 struct discover_device *dev;
129 for (i = 0; i < device_handler->n_devices; i++) {
130 dev = device_handler->devices[i];
132 if (match_fn(dev, str))
139 struct discover_device *device_lookup_by_name(struct device_handler *handler,
142 if (!strncmp(name, "/dev/", strlen("/dev/")))
143 name += strlen("/dev/");
145 return device_lookup_by_id(handler, name);
148 struct discover_device *device_lookup_by_uuid(
149 struct device_handler *device_handler,
152 return device_lookup(device_handler, device_match_uuid, uuid);
155 struct discover_device *device_lookup_by_label(
156 struct device_handler *device_handler,
159 return device_lookup(device_handler, device_match_label, label);
162 struct discover_device *device_lookup_by_id(
163 struct device_handler *device_handler,
166 return device_lookup(device_handler, device_match_id, id);
169 struct discover_device *device_lookup_by_serial(
170 struct device_handler *device_handler,
173 return device_lookup(device_handler, device_match_serial, serial);
176 void device_handler_destroy(struct device_handler *handler)
178 talloc_free(handler);
181 static int destroy_device(void *arg)
183 struct discover_device *dev = arg;
190 struct discover_device *discover_device_create(struct device_handler *handler,
193 struct discover_device *dev;
195 dev = device_lookup_by_id(handler, id);
199 dev = talloc_zero(handler, struct discover_device);
200 dev->device = talloc_zero(dev, struct device);
201 dev->device->id = talloc_strdup(dev->device, id);
202 list_init(&dev->params);
203 list_init(&dev->boot_options);
205 talloc_set_destructor(dev, destroy_device);
210 struct discover_device_param {
213 struct list_item list;
216 void discover_device_set_param(struct discover_device *device,
217 const char *name, const char *value)
219 struct discover_device_param *param;
222 list_for_each_entry(&device->params, param, list) {
223 if (!strcmp(param->name, name)) {
232 param = talloc(device, struct discover_device_param);
233 param->name = talloc_strdup(param, name);
234 list_add(&device->params, ¶m->list);
237 list_remove(¶m->list);
241 talloc_free(param->value);
244 param->value = talloc_strdup(param, value);
247 const char *discover_device_get_param(struct discover_device *device,
250 struct discover_device_param *param;
252 list_for_each_entry(&device->params, param, list) {
253 if (!strcmp(param->name, name))
259 struct device_handler *device_handler_init(struct discover_server *server,
260 struct waitset *waitset, int dry_run)
262 struct device_handler *handler;
264 handler = talloc_zero(NULL, struct device_handler);
265 handler->server = server;
266 handler->waitset = waitset;
267 handler->dry_run = dry_run;
268 handler->autoboot_enabled = config_get()->autoboot_enabled;
270 list_init(&handler->unresolved_boot_options);
272 /* set up our mount point base */
273 pb_mkdir_recursive(mount_base());
280 void device_handler_remove(struct device_handler *handler,
281 struct discover_device *device)
283 struct discover_boot_option *opt, *tmp;
286 for (i = 0; i < handler->n_devices; i++)
287 if (handler->devices[i] == device)
290 if (i == handler->n_devices) {
295 /* Free any unresolved options, as they're currently allocated
296 * against the handler */
297 list_for_each_entry_safe(&handler->unresolved_boot_options,
299 if (opt->device != device)
301 list_remove(&opt->list);
305 handler->n_devices--;
306 memmove(&handler->devices[i], &handler->devices[i + 1],
307 (handler->n_devices - i) * sizeof(handler->devices[0]));
308 handler->devices = talloc_realloc(handler, handler->devices,
309 struct discover_device *, handler->n_devices);
311 if (device->notified)
312 discover_server_notify_device_remove(handler->server,
318 static void boot_status(void *arg, struct boot_status *status)
320 struct device_handler *handler = arg;
322 discover_server_notify_boot_status(handler->server, status);
325 static void countdown_status(struct device_handler *handler,
326 struct discover_boot_option *opt, unsigned int sec)
328 struct boot_status status;
330 status.type = BOOT_STATUS_INFO;
331 status.progress = -1;
332 status.detail = NULL;
333 status.message = talloc_asprintf(handler,
334 "Booting %s in %u sec", opt->option->name, sec);
336 discover_server_notify_boot_status(handler->server, &status);
338 talloc_free(status.message);
341 static int default_timeout(void *arg)
343 struct device_handler *handler = arg;
344 struct discover_boot_option *opt;
346 if (!handler->default_boot_option)
349 if (handler->pending_boot)
352 opt = handler->default_boot_option;
354 if (handler->sec_to_boot) {
355 countdown_status(handler, opt, handler->sec_to_boot);
356 handler->sec_to_boot--;
357 handler->timeout_waiter = waiter_register_timeout(
358 handler->waitset, 1000,
359 default_timeout, handler);
363 handler->timeout_waiter = NULL;
365 pb_log("Timeout expired, booting default option %s\n", opt->option->id);
367 handler->pending_boot = boot(handler, handler->default_boot_option,
368 NULL, handler->dry_run, boot_status, handler);
369 handler->pending_boot_is_default = true;
373 static bool priority_match(struct boot_priority *prio,
374 struct discover_boot_option *opt)
376 return prio->type == opt->device->device->type ||
377 prio->type == DEVICE_TYPE_ANY;
380 static int default_option_priority(struct discover_boot_option *opt)
382 const struct config *config;
383 struct boot_priority *prio;
386 config = config_get();
388 for (i = 0; i < config->n_boot_priorities; i++) {
389 prio = &config->boot_priorities[i];
390 if (priority_match(prio, opt))
391 return prio->priority;
397 static void set_default(struct device_handler *handler,
398 struct discover_boot_option *opt)
402 if (!handler->autoboot_enabled)
405 new_prio = default_option_priority(opt);
407 /* A negative priority indicates that we don't want to boot this device
412 /* Resolve any conflicts: if we have a new default option, it only
413 * replaces the current if it has a higher priority. */
414 if (handler->default_boot_option) {
417 cur_prio = default_option_priority(
418 handler->default_boot_option);
420 if (new_prio > cur_prio) {
421 handler->default_boot_option = opt;
422 /* extend the timeout a little, so the user sees some
423 * indication of the change */
424 handler->sec_to_boot += 2;
430 handler->sec_to_boot = config_get()->autoboot_timeout_sec;
431 handler->default_boot_option = opt;
433 pb_log("Boot option %s set as default, timeout %u sec.\n",
434 opt->option->id, handler->sec_to_boot);
436 default_timeout(handler);
439 static bool resource_is_resolved(struct resource *res)
441 return !res || res->resolved;
444 /* We only use this in an assert, which will disappear if we're compiling
445 * with NDEBUG, so we need the 'used' attribute for these builds */
446 static bool __attribute__((used)) boot_option_is_resolved(
447 struct discover_boot_option *opt)
449 return resource_is_resolved(opt->boot_image) &&
450 resource_is_resolved(opt->initrd) &&
451 resource_is_resolved(opt->dtb) &&
452 resource_is_resolved(opt->icon);
455 static bool resource_resolve(struct resource *res, const char *name,
456 struct discover_boot_option *opt,
457 struct device_handler *handler)
459 struct parser *parser = opt->source;
461 if (resource_is_resolved(res))
464 pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
465 opt->option->id, name, parser->name);
466 parser->resolve_resource(handler, res);
468 return res->resolved;
471 static bool boot_option_resolve(struct discover_boot_option *opt,
472 struct device_handler *handler)
474 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
475 resource_resolve(opt->initrd, "initrd", opt, handler) &&
476 resource_resolve(opt->dtb, "dtb", opt, handler) &&
477 resource_resolve(opt->icon, "icon", opt, handler);
480 static void boot_option_finalise(struct device_handler *handler,
481 struct discover_boot_option *opt)
483 assert(boot_option_is_resolved(opt));
485 /* check that the parsers haven't set any of the final data */
486 assert(!opt->option->boot_image_file);
487 assert(!opt->option->initrd_file);
488 assert(!opt->option->dtb_file);
489 assert(!opt->option->icon_file);
490 assert(!opt->option->device_id);
493 opt->option->boot_image_file = opt->boot_image->url->full;
495 opt->option->initrd_file = opt->initrd->url->full;
497 opt->option->dtb_file = opt->dtb->url->full;
499 opt->option->icon_file = opt->icon->url->full;
501 opt->option->device_id = opt->device->device->id;
503 if (opt->option->is_default)
504 set_default(handler, opt);
507 static void notify_boot_option(struct device_handler *handler,
508 struct discover_boot_option *opt)
510 struct discover_device *dev = opt->device;
513 discover_server_notify_device_add(handler->server,
514 opt->device->device);
515 dev->notified = true;
516 discover_server_notify_boot_option_add(handler->server, opt->option);
519 static void process_boot_option_queue(struct device_handler *handler)
521 struct discover_boot_option *opt, *tmp;
523 list_for_each_entry_safe(&handler->unresolved_boot_options,
526 pb_debug("queue: attempting resolution for %s\n",
529 if (!boot_option_resolve(opt, handler))
532 pb_debug("\tresolved!\n");
534 list_remove(&opt->list);
535 list_add_tail(&opt->device->boot_options, &opt->list);
536 talloc_steal(opt->device, opt);
537 boot_option_finalise(handler, opt);
538 notify_boot_option(handler, opt);
542 struct discover_context *device_handler_discover_context_create(
543 struct device_handler *handler,
544 struct discover_device *device)
546 struct discover_context *ctx;
548 ctx = talloc_zero(handler, struct discover_context);
549 ctx->device = device;
550 list_init(&ctx->boot_options);
556 * context_commit - Commit a temporary discovery context to the handler,
557 * and notify the clients about any new options / devices
559 void device_handler_discover_context_commit(struct device_handler *handler,
560 struct discover_context *ctx)
562 struct discover_device *dev = ctx->device;
563 struct discover_boot_option *opt, *tmp;
565 if (!device_lookup_by_id(handler, dev->device->id))
566 device_handler_add_device(handler, dev);
568 /* move boot options from the context to the device */
569 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
570 list_remove(&opt->list);
572 if (boot_option_resolve(opt, handler)) {
573 pb_log("boot option %s is resolved, "
574 "sending to clients\n",
576 list_add_tail(&dev->boot_options, &opt->list);
577 talloc_steal(dev, opt);
578 boot_option_finalise(handler, opt);
579 notify_boot_option(handler, opt);
581 if (!opt->source->resolve_resource) {
582 pb_log("parser %s gave us an unresolved "
583 "resource (%s), but no way to "
585 opt->source->name, opt->option->id);
588 pb_log("boot option %s is unresolved, "
591 list_add(&handler->unresolved_boot_options,
593 talloc_steal(handler, opt);
599 void device_handler_add_device(struct device_handler *handler,
600 struct discover_device *device)
602 handler->n_devices++;
603 handler->devices = talloc_realloc(handler, handler->devices,
604 struct discover_device *, handler->n_devices);
605 handler->devices[handler->n_devices - 1] = device;
609 /* Start discovery on a hotplugged device. The device will be in our devices
610 * array, but has only just been initialised by the hotplug source.
612 int device_handler_discover(struct device_handler *handler,
613 struct discover_device *dev)
615 struct discover_context *ctx;
618 process_boot_option_queue(handler);
620 /* create our context */
621 ctx = device_handler_discover_context_create(handler, dev);
623 rc = mount_device(dev);
627 /* add this device to our system info */
628 system_info_register_blockdev(dev->device->id, dev->uuid,
631 /* run the parsers. This will populate the ctx's boot_option list. */
632 iterate_parsers(ctx);
634 /* add discovered stuff to the handler */
635 device_handler_discover_context_commit(handler, ctx);
643 /* Incoming dhcp event */
644 int device_handler_dhcp(struct device_handler *handler,
645 struct discover_device *dev, struct event *event)
647 struct discover_context *ctx;
649 /* create our context */
650 ctx = device_handler_discover_context_create(handler, dev);
653 iterate_parsers(ctx);
655 device_handler_discover_context_commit(handler, ctx);
662 /* incoming conf event */
663 int device_handler_conf(struct device_handler *handler,
664 struct discover_device *dev, struct pb_url *url)
666 struct discover_context *ctx;
668 /* create our context */
669 ctx = device_handler_discover_context_create(handler, dev);
672 iterate_parsers(ctx);
674 device_handler_discover_context_commit(handler, ctx);
681 static struct discover_boot_option *find_boot_option_by_id(
682 struct device_handler *handler, const char *id)
686 for (i = 0; i < handler->n_devices; i++) {
687 struct discover_device *dev = handler->devices[i];
688 struct discover_boot_option *opt;
690 list_for_each_entry(&dev->boot_options, opt, list)
691 if (!strcmp(opt->option->id, id))
698 void device_handler_boot(struct device_handler *handler,
699 struct boot_command *cmd)
701 struct discover_boot_option *opt = NULL;
703 if (cmd->option_id && strlen(cmd->option_id))
704 opt = find_boot_option_by_id(handler, cmd->option_id);
706 if (handler->pending_boot)
707 boot_cancel(handler->pending_boot);
708 handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
709 boot_status, handler);
710 handler->pending_boot_is_default = false;
713 void device_handler_cancel_default(struct device_handler *handler)
715 struct boot_status status;
717 if (handler->timeout_waiter)
718 waiter_remove(handler->timeout_waiter);
720 handler->timeout_waiter = NULL;
721 handler->autoboot_enabled = false;
723 /* we only send status if we had a default boot option queued */
724 if (!handler->default_boot_option)
727 pb_log("Cancelling default boot option\n");
729 if (handler->pending_boot && handler->pending_boot_is_default) {
730 boot_cancel(handler->pending_boot);
731 handler->pending_boot = NULL;
732 handler->pending_boot_is_default = false;
735 handler->default_boot_option = NULL;
737 status.type = BOOT_STATUS_INFO;
738 status.progress = -1;
739 status.detail = NULL;
740 status.message = "Default boot cancelled";
742 discover_server_notify_boot_status(handler->server, &status);
745 void device_handler_update_config(struct device_handler *handler,
746 struct config *config)
749 discover_server_notify_config(handler->server, config);
752 #ifndef PETITBOOT_TEST
753 static bool check_existing_mount(struct discover_device *dev)
755 struct stat devstat, mntstat;
760 rc = stat(dev->device_path, &devstat);
762 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
766 if (!S_ISBLK(devstat.st_mode)) {
767 pb_debug("%s: %s isn't a block device?\n", __func__,
772 fp = fopen("/proc/self/mounts", "r");
779 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
782 rc = stat(mnt->mnt_fsname, &mntstat);
786 if (!S_ISBLK(mntstat.st_mode))
789 if (mntstat.st_rdev == devstat.st_rdev) {
790 dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
791 dev->mounted_rw = !!hasmntopt(mnt, "rw");
793 dev->unmount = false;
795 pb_debug("%s: %s is already mounted (r%c) at %s\n",
796 __func__, dev->device_path,
797 dev->mounted_rw ? 'w' : 'o',
808 static int mount_device(struct discover_device *dev)
813 if (!dev->device_path)
819 if (check_existing_mount(dev))
822 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
826 dev->mount_path = join_paths(dev, mount_base(),
829 if (pb_mkdir_recursive(dev->mount_path)) {
830 pb_log("couldn't create mount directory %s: %s\n",
831 dev->mount_path, strerror(errno));
835 pb_log("mounting device %s read-only\n", dev->device_path);
837 rc = mount(dev->device_path, dev->mount_path, fstype,
838 MS_RDONLY | MS_SILENT, "");
841 dev->mounted_rw = false;
846 pb_log("couldn't mount device %s: mount failed: %s\n",
847 dev->device_path, strerror(errno));
849 pb_rmdir_recursive(mount_base(), dev->mount_path);
851 talloc_free(dev->mount_path);
852 dev->mount_path = NULL;
856 static int umount_device(struct discover_device *dev)
860 if (!dev->mounted || !dev->unmount)
863 pb_log("unmounting device %s\n", dev->device_path);
864 rc = umount(dev->mount_path);
868 dev->mounted = false;
870 pb_rmdir_recursive(mount_base(), dev->mount_path);
872 talloc_free(dev->mount_path);
873 dev->mount_path = NULL;
878 int device_request_write(struct discover_device *dev, bool *release)
890 pb_log("remounting device %s read-write\n", dev->device_path);
891 rc = mount(dev->device_path, dev->mount_path, "",
892 MS_REMOUNT | MS_SILENT, "");
896 dev->mounted_rw = true;
901 void device_release_write(struct discover_device *dev, bool release)
906 pb_log("remounting device %s read-only\n", dev->device_path);
907 mount(dev->device_path, dev->mount_path, "",
908 MS_REMOUNT | MS_RDONLY | MS_SILENT, "");
909 dev->mounted_rw = false;
914 static int umount_device(struct discover_device *dev __attribute__((unused)))
919 static int __attribute__((unused)) mount_device(
920 struct discover_device *dev __attribute__((unused)))
925 int device_request_write(struct discover_device *dev __attribute__((unused)),
932 void device_release_write(struct discover_device *dev __attribute__((unused)),
933 bool release __attribute__((unused)))