11 #include <pb-config/pb-config.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"
28 struct device_handler {
29 struct discover_server *server;
32 struct discover_device **devices;
33 unsigned int n_devices;
35 struct waitset *waitset;
36 struct waiter *timeout_waiter;
37 bool autoboot_enabled;
38 unsigned int sec_to_boot;
40 struct discover_boot_option *default_boot_option;
41 struct list unresolved_boot_options;
43 struct boot_task *pending_boot;
44 bool pending_boot_is_default;
47 static int mount_device(struct discover_device *dev);
48 static int umount_device(struct discover_device *dev);
50 void discover_context_add_boot_option(struct discover_context *ctx,
51 struct discover_boot_option *boot_option)
53 boot_option->source = ctx->parser;
54 list_add_tail(&ctx->boot_options, &boot_option->list);
55 talloc_steal(ctx, boot_option);
59 * device_handler_get_device_count - Get the count of current handler devices.
62 int device_handler_get_device_count(const struct device_handler *handler)
64 return handler->n_devices;
68 * device_handler_get_device - Get a handler device by index.
71 const struct discover_device *device_handler_get_device(
72 const struct device_handler *handler, unsigned int index)
74 if (index >= handler->n_devices) {
75 assert(0 && "bad index");
79 return handler->devices[index];
82 struct discover_boot_option *discover_boot_option_create(
83 struct discover_context *ctx,
84 struct discover_device *device)
86 struct discover_boot_option *opt;
88 opt = talloc_zero(ctx, struct discover_boot_option);
89 opt->option = talloc_zero(opt, struct boot_option);
95 static int device_match_uuid(struct discover_device *dev, const char *uuid)
97 return dev->uuid && !strcmp(dev->uuid, uuid);
100 static int device_match_label(struct discover_device *dev, const char *label)
102 return dev->label && !strcmp(dev->label, label);
105 static int device_match_id(struct discover_device *dev, const char *id)
107 return !strcmp(dev->device->id, id);
110 static int device_match_serial(struct discover_device *dev, const char *serial)
112 const char *val = discover_device_get_param(dev, "ID_SERIAL");
113 return val && !strcmp(val, serial);
116 static struct discover_device *device_lookup(
117 struct device_handler *device_handler,
118 int (match_fn)(struct discover_device *, const char *),
121 struct discover_device *dev;
127 for (i = 0; i < device_handler->n_devices; i++) {
128 dev = device_handler->devices[i];
130 if (match_fn(dev, str))
137 struct discover_device *device_lookup_by_name(struct device_handler *handler,
140 if (!strncmp(name, "/dev/", strlen("/dev/")))
141 name += strlen("/dev/");
143 return device_lookup_by_id(handler, name);
146 struct discover_device *device_lookup_by_uuid(
147 struct device_handler *device_handler,
150 return device_lookup(device_handler, device_match_uuid, uuid);
153 struct discover_device *device_lookup_by_label(
154 struct device_handler *device_handler,
157 return device_lookup(device_handler, device_match_label, label);
160 struct discover_device *device_lookup_by_id(
161 struct device_handler *device_handler,
164 return device_lookup(device_handler, device_match_id, id);
167 struct discover_device *device_lookup_by_serial(
168 struct device_handler *device_handler,
171 return device_lookup(device_handler, device_match_serial, serial);
174 void device_handler_destroy(struct device_handler *handler)
176 talloc_free(handler);
179 static int destroy_device(void *arg)
181 struct discover_device *dev = arg;
188 struct discover_device *discover_device_create(struct device_handler *handler,
191 struct discover_device *dev;
193 dev = device_lookup_by_id(handler, id);
197 dev = talloc_zero(handler, struct discover_device);
198 dev->device = talloc_zero(dev, struct device);
199 dev->device->id = talloc_strdup(dev->device, id);
200 list_init(&dev->params);
201 list_init(&dev->boot_options);
203 talloc_set_destructor(dev, destroy_device);
208 struct discover_device_param {
211 struct list_item list;
214 void discover_device_set_param(struct discover_device *device,
215 const char *name, const char *value)
217 struct discover_device_param *param;
220 list_for_each_entry(&device->params, param, list) {
221 if (!strcmp(param->name, name)) {
230 param = talloc(device, struct discover_device_param);
231 param->name = talloc_strdup(param, name);
232 list_add(&device->params, ¶m->list);
235 list_remove(¶m->list);
239 talloc_free(param->value);
242 param->value = talloc_strdup(param, value);
245 const char *discover_device_get_param(struct discover_device *device,
248 struct discover_device_param *param;
250 list_for_each_entry(&device->params, param, list) {
251 if (!strcmp(param->name, name))
257 struct device_handler *device_handler_init(struct discover_server *server,
258 struct waitset *waitset, int dry_run)
260 struct device_handler *handler;
262 handler = talloc_zero(NULL, struct device_handler);
263 handler->server = server;
264 handler->waitset = waitset;
265 handler->dry_run = dry_run;
266 handler->autoboot_enabled = config_get()->autoboot_enabled;
268 list_init(&handler->unresolved_boot_options);
270 /* set up our mount point base */
271 pb_mkdir_recursive(mount_base());
278 void device_handler_remove(struct device_handler *handler,
279 struct discover_device *device)
283 for (i = 0; i < handler->n_devices; i++)
284 if (handler->devices[i] == device)
287 if (i == handler->n_devices) {
292 handler->n_devices--;
293 memmove(&handler->devices[i], &handler->devices[i + 1],
294 (handler->n_devices - i) * sizeof(handler->devices[0]));
295 handler->devices = talloc_realloc(handler, handler->devices,
296 struct discover_device *, handler->n_devices);
298 if (device->notified)
299 discover_server_notify_device_remove(handler->server,
305 static void boot_status(void *arg, struct boot_status *status)
307 struct device_handler *handler = arg;
309 discover_server_notify_boot_status(handler->server, status);
312 static void countdown_status(struct device_handler *handler,
313 struct discover_boot_option *opt, unsigned int sec)
315 struct boot_status status;
317 status.type = BOOT_STATUS_INFO;
318 status.progress = -1;
319 status.detail = NULL;
320 status.message = talloc_asprintf(handler,
321 "Booting %s in %u sec", opt->option->name, sec);
323 discover_server_notify_boot_status(handler->server, &status);
325 talloc_free(status.message);
328 static int default_timeout(void *arg)
330 struct device_handler *handler = arg;
331 struct discover_boot_option *opt;
333 if (!handler->default_boot_option)
336 if (handler->pending_boot)
339 opt = handler->default_boot_option;
341 if (handler->sec_to_boot) {
342 countdown_status(handler, opt, handler->sec_to_boot);
343 handler->sec_to_boot--;
344 handler->timeout_waiter = waiter_register_timeout(
345 handler->waitset, 1000,
346 default_timeout, handler);
350 handler->timeout_waiter = NULL;
352 pb_log("Timeout expired, booting default option %s\n", opt->option->id);
354 handler->pending_boot = boot(handler, handler->default_boot_option,
355 NULL, handler->dry_run, boot_status, handler);
356 handler->pending_boot_is_default = true;
360 static bool priority_match(struct boot_priority *prio,
361 struct discover_boot_option *opt)
363 return prio->type == opt->device->device->type;
366 static int default_option_priority(struct discover_boot_option *opt)
368 const struct config *config;
369 struct boot_priority *prio;
372 config = config_get();
374 for (i = 0; i < config->n_boot_priorities; i++) {
375 prio = &config->boot_priorities[i];
376 if (priority_match(prio, opt))
383 static void set_default(struct device_handler *handler,
384 struct discover_boot_option *opt)
386 if (!handler->autoboot_enabled)
389 /* Resolve any conflicts: if we have a new default option, it only
390 * replaces the current if it has a higher priority. */
391 if (handler->default_boot_option) {
392 int new_prio, cur_prio;
394 new_prio = default_option_priority(opt);
395 cur_prio = default_option_priority(
396 handler->default_boot_option);
398 if (new_prio < cur_prio) {
399 handler->default_boot_option = opt;
400 /* extend the timeout a little, so the user sees some
401 * indication of the change */
402 handler->sec_to_boot += 2;
408 handler->sec_to_boot = config_get()->autoboot_timeout_sec;
409 handler->default_boot_option = opt;
411 pb_log("Boot option %s set as default, timeout %u sec.\n",
412 opt->option->id, handler->sec_to_boot);
414 default_timeout(handler);
417 static bool resource_is_resolved(struct resource *res)
419 return !res || res->resolved;
422 /* We only use this in an assert, which will disappear if we're compiling
423 * with NDEBUG, so we need the 'used' attribute for these builds */
424 static bool __attribute__((used)) boot_option_is_resolved(
425 struct discover_boot_option *opt)
427 return resource_is_resolved(opt->boot_image) &&
428 resource_is_resolved(opt->initrd) &&
429 resource_is_resolved(opt->dtb) &&
430 resource_is_resolved(opt->icon);
433 static bool resource_resolve(struct resource *res, const char *name,
434 struct discover_boot_option *opt,
435 struct device_handler *handler)
437 struct parser *parser = opt->source;
439 if (resource_is_resolved(res))
442 pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
443 opt->option->id, name, parser->name);
444 parser->resolve_resource(handler, res);
446 return res->resolved;
449 static bool boot_option_resolve(struct discover_boot_option *opt,
450 struct device_handler *handler)
452 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
453 resource_resolve(opt->initrd, "initrd", opt, handler) &&
454 resource_resolve(opt->dtb, "dtb", opt, handler) &&
455 resource_resolve(opt->icon, "icon", opt, handler);
458 static void boot_option_finalise(struct device_handler *handler,
459 struct discover_boot_option *opt)
461 assert(boot_option_is_resolved(opt));
463 /* check that the parsers haven't set any of the final data */
464 assert(!opt->option->boot_image_file);
465 assert(!opt->option->initrd_file);
466 assert(!opt->option->dtb_file);
467 assert(!opt->option->icon_file);
468 assert(!opt->option->device_id);
471 opt->option->boot_image_file = opt->boot_image->url->full;
473 opt->option->initrd_file = opt->initrd->url->full;
475 opt->option->dtb_file = opt->dtb->url->full;
477 opt->option->icon_file = opt->icon->url->full;
479 opt->option->device_id = opt->device->device->id;
481 if (opt->option->is_default)
482 set_default(handler, opt);
485 static void notify_boot_option(struct device_handler *handler,
486 struct discover_boot_option *opt)
488 struct discover_device *dev = opt->device;
491 discover_server_notify_device_add(handler->server,
492 opt->device->device);
493 dev->notified = true;
494 discover_server_notify_boot_option_add(handler->server, opt->option);
497 static void process_boot_option_queue(struct device_handler *handler)
499 struct discover_boot_option *opt, *tmp;
501 list_for_each_entry_safe(&handler->unresolved_boot_options,
504 pb_debug("queue: attempting resolution for %s\n",
507 if (!boot_option_resolve(opt, handler))
510 pb_debug("\tresolved!\n");
512 list_remove(&opt->list);
513 list_add_tail(&opt->device->boot_options, &opt->list);
514 talloc_steal(opt->device, opt);
515 boot_option_finalise(handler, opt);
516 notify_boot_option(handler, opt);
520 struct discover_context *device_handler_discover_context_create(
521 struct device_handler *handler,
522 struct discover_device *device)
524 struct discover_context *ctx;
526 ctx = talloc_zero(handler, struct discover_context);
527 ctx->device = device;
528 list_init(&ctx->boot_options);
534 * context_commit - Commit a temporary discovery context to the handler,
535 * and notify the clients about any new options / devices
537 void device_handler_discover_context_commit(struct device_handler *handler,
538 struct discover_context *ctx)
540 struct discover_device *dev = ctx->device;
541 struct discover_boot_option *opt, *tmp;
543 if (!device_lookup_by_id(handler, dev->device->id))
544 device_handler_add_device(handler, dev);
546 /* move boot options from the context to the device */
547 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
548 list_remove(&opt->list);
550 if (boot_option_resolve(opt, handler)) {
551 pb_log("boot option %s is resolved, "
552 "sending to clients\n",
554 list_add_tail(&dev->boot_options, &opt->list);
555 talloc_steal(dev, opt);
556 boot_option_finalise(handler, opt);
557 notify_boot_option(handler, opt);
559 if (!opt->source->resolve_resource) {
560 pb_log("parser %s gave us an unresolved "
561 "resource (%s), but no way to "
563 opt->source->name, opt->option->id);
566 pb_log("boot option %s is unresolved, "
569 list_add(&handler->unresolved_boot_options,
571 talloc_steal(handler, opt);
577 void device_handler_add_device(struct device_handler *handler,
578 struct discover_device *device)
580 handler->n_devices++;
581 handler->devices = talloc_realloc(handler, handler->devices,
582 struct discover_device *, handler->n_devices);
583 handler->devices[handler->n_devices - 1] = device;
587 /* Start discovery on a hotplugged device. The device will be in our devices
588 * array, but has only just been initialised by the hotplug source.
590 int device_handler_discover(struct device_handler *handler,
591 struct discover_device *dev)
593 struct discover_context *ctx;
596 process_boot_option_queue(handler);
598 /* create our context */
599 ctx = device_handler_discover_context_create(handler, dev);
601 rc = mount_device(dev);
605 /* run the parsers. This will populate the ctx's boot_option list. */
606 iterate_parsers(ctx);
608 /* add discovered stuff to the handler */
609 device_handler_discover_context_commit(handler, ctx);
617 /* Incoming dhcp event */
618 int device_handler_dhcp(struct device_handler *handler,
619 struct discover_device *dev, struct event *event)
621 struct discover_context *ctx;
623 /* create our context */
624 ctx = device_handler_discover_context_create(handler, dev);
627 iterate_parsers(ctx);
629 device_handler_discover_context_commit(handler, ctx);
636 /* incoming conf event */
637 int device_handler_conf(struct device_handler *handler,
638 struct discover_device *dev, struct pb_url *url)
640 struct discover_context *ctx;
642 /* create our context */
643 ctx = device_handler_discover_context_create(handler, dev);
646 iterate_parsers(ctx);
648 device_handler_discover_context_commit(handler, ctx);
655 static struct discover_boot_option *find_boot_option_by_id(
656 struct device_handler *handler, const char *id)
660 for (i = 0; i < handler->n_devices; i++) {
661 struct discover_device *dev = handler->devices[i];
662 struct discover_boot_option *opt;
664 list_for_each_entry(&dev->boot_options, opt, list)
665 if (!strcmp(opt->option->id, id))
672 void device_handler_boot(struct device_handler *handler,
673 struct boot_command *cmd)
675 struct discover_boot_option *opt = NULL;
677 if (cmd->option_id && strlen(cmd->option_id))
678 opt = find_boot_option_by_id(handler, cmd->option_id);
680 if (handler->pending_boot)
681 boot_cancel(handler->pending_boot);
682 handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
683 boot_status, handler);
684 handler->pending_boot_is_default = false;
687 void device_handler_cancel_default(struct device_handler *handler)
689 struct boot_status status;
691 if (handler->timeout_waiter)
692 waiter_remove(handler->timeout_waiter);
694 handler->timeout_waiter = NULL;
695 handler->autoboot_enabled = false;
697 /* we only send status if we had a default boot option queued */
698 if (!handler->default_boot_option)
701 pb_log("Cancelling default boot option\n");
703 if (handler->pending_boot && handler->pending_boot_is_default) {
704 boot_cancel(handler->pending_boot);
705 handler->pending_boot = NULL;
706 handler->pending_boot_is_default = false;
709 handler->default_boot_option = NULL;
711 status.type = BOOT_STATUS_INFO;
712 status.progress = -1;
713 status.detail = NULL;
714 status.message = "Default boot cancelled";
716 discover_server_notify_boot_status(handler->server, &status);
719 void device_handler_update_config(struct device_handler *handler,
720 struct config *config)
723 discover_server_notify_config(handler->server, config);
726 #ifndef PETITBOOT_TEST
727 static bool check_existing_mount(struct discover_device *dev)
729 struct stat devstat, mntstat;
734 rc = stat(dev->device_path, &devstat);
736 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
740 if (!S_ISBLK(devstat.st_mode)) {
741 pb_debug("%s: %s isn't a block device?\n", __func__,
746 fp = fopen("/proc/self/mounts", "r");
753 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
756 rc = stat(mnt->mnt_fsname, &mntstat);
760 if (!S_ISBLK(mntstat.st_mode))
763 if (mntstat.st_rdev == devstat.st_rdev) {
764 dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
765 dev->mounted_rw = !!hasmntopt(mnt, "rw");
767 dev->unmount = false;
769 pb_debug("%s: %s is already mounted (r%c) at %s\n",
770 __func__, dev->device_path,
771 dev->mounted_rw ? 'w' : 'o',
782 static int mount_device(struct discover_device *dev)
786 if (!dev->device_path)
792 if (check_existing_mount(dev))
795 dev->mount_path = join_paths(dev, mount_base(),
798 if (pb_mkdir_recursive(dev->mount_path)) {
799 pb_log("couldn't create mount directory %s: %s\n",
800 dev->mount_path, strerror(errno));
804 rc = process_run_simple(dev, pb_system_apps.mount,
805 dev->device_path, dev->mount_path,
809 dev->mounted_rw = false;
814 /* Retry mount without ro option. */
815 rc = process_run_simple(dev, pb_system_apps.mount,
816 dev->device_path, dev->mount_path, NULL);
820 dev->mounted_rw = true;
825 pb_rmdir_recursive(mount_base(), dev->mount_path);
827 talloc_free(dev->mount_path);
828 dev->mount_path = NULL;
832 static int umount_device(struct discover_device *dev)
836 if (!dev->mounted || !dev->unmount)
839 status = process_run_simple(dev, pb_system_apps.umount,
840 dev->mount_path, NULL);
842 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
845 dev->mounted = false;
847 pb_rmdir_recursive(mount_base(), dev->mount_path);
849 talloc_free(dev->mount_path);
850 dev->mount_path = NULL;
855 int device_request_write(struct discover_device *dev, bool *release)
867 rc = process_run_simple(dev, pb_system_apps.mount, dev->mount_path,
868 "-o", "remount,rw", NULL);
872 dev->mounted_rw = true;
877 void device_release_write(struct discover_device *dev, bool release)
882 process_run_simple(dev, pb_system_apps.mount, dev->mount_path,
883 "-o", "remount,ro", NULL);
884 dev->mounted_rw = false;
889 static int umount_device(struct discover_device *dev __attribute__((unused)))
894 static int __attribute__((unused)) mount_device(
895 struct discover_device *dev __attribute__((unused)))
900 int device_request_write(struct discover_device *dev __attribute__((unused)),
907 void device_release_write(struct discover_device *dev __attribute__((unused)),
908 bool release __attribute__((unused)))