12 #include <pb-config/pb-config.h>
13 #include <talloc/talloc.h>
14 #include <list/list.h>
16 #include <types/types.h>
17 #include <system/system.h>
18 #include <process/process.h>
21 #include "device-handler.h"
22 #include "discover-server.h"
29 struct device_handler {
30 struct discover_server *server;
33 struct discover_device **devices;
34 unsigned int n_devices;
36 struct waitset *waitset;
37 struct waiter *timeout_waiter;
38 bool autoboot_enabled;
39 unsigned int sec_to_boot;
41 struct discover_boot_option *default_boot_option;
42 struct list unresolved_boot_options;
44 struct boot_task *pending_boot;
45 bool pending_boot_is_default;
48 static int mount_device(struct discover_device *dev);
49 static int umount_device(struct discover_device *dev);
51 void discover_context_add_boot_option(struct discover_context *ctx,
52 struct discover_boot_option *boot_option)
54 boot_option->source = ctx->parser;
55 list_add_tail(&ctx->boot_options, &boot_option->list);
56 talloc_steal(ctx, boot_option);
60 * device_handler_get_device_count - Get the count of current handler devices.
63 int device_handler_get_device_count(const struct device_handler *handler)
65 return handler->n_devices;
69 * device_handler_get_device - Get a handler device by index.
72 const struct discover_device *device_handler_get_device(
73 const struct device_handler *handler, unsigned int index)
75 if (index >= handler->n_devices) {
76 assert(0 && "bad index");
80 return handler->devices[index];
83 struct discover_boot_option *discover_boot_option_create(
84 struct discover_context *ctx,
85 struct discover_device *device)
87 struct discover_boot_option *opt;
89 opt = talloc_zero(ctx, struct discover_boot_option);
90 opt->option = talloc_zero(opt, struct boot_option);
96 static int device_match_uuid(struct discover_device *dev, const char *uuid)
98 return dev->uuid && !strcmp(dev->uuid, uuid);
101 static int device_match_label(struct discover_device *dev, const char *label)
103 return dev->label && !strcmp(dev->label, label);
106 static int device_match_id(struct discover_device *dev, const char *id)
108 return !strcmp(dev->device->id, id);
111 static int device_match_serial(struct discover_device *dev, const char *serial)
113 const char *val = discover_device_get_param(dev, "ID_SERIAL");
114 return val && !strcmp(val, serial);
117 static struct discover_device *device_lookup(
118 struct device_handler *device_handler,
119 int (match_fn)(struct discover_device *, const char *),
122 struct discover_device *dev;
128 for (i = 0; i < device_handler->n_devices; i++) {
129 dev = device_handler->devices[i];
131 if (match_fn(dev, str))
138 struct discover_device *device_lookup_by_name(struct device_handler *handler,
141 if (!strncmp(name, "/dev/", strlen("/dev/")))
142 name += strlen("/dev/");
144 return device_lookup_by_id(handler, name);
147 struct discover_device *device_lookup_by_uuid(
148 struct device_handler *device_handler,
151 return device_lookup(device_handler, device_match_uuid, uuid);
154 struct discover_device *device_lookup_by_label(
155 struct device_handler *device_handler,
158 return device_lookup(device_handler, device_match_label, label);
161 struct discover_device *device_lookup_by_id(
162 struct device_handler *device_handler,
165 return device_lookup(device_handler, device_match_id, id);
168 struct discover_device *device_lookup_by_serial(
169 struct device_handler *device_handler,
172 return device_lookup(device_handler, device_match_serial, serial);
175 void device_handler_destroy(struct device_handler *handler)
177 talloc_free(handler);
180 static int destroy_device(void *arg)
182 struct discover_device *dev = arg;
189 struct discover_device *discover_device_create(struct device_handler *handler,
192 struct discover_device *dev;
194 dev = device_lookup_by_id(handler, id);
198 dev = talloc_zero(handler, struct discover_device);
199 dev->device = talloc_zero(dev, struct device);
200 dev->device->id = talloc_strdup(dev->device, id);
201 list_init(&dev->params);
202 list_init(&dev->boot_options);
204 talloc_set_destructor(dev, destroy_device);
209 struct discover_device_param {
212 struct list_item list;
215 void discover_device_set_param(struct discover_device *device,
216 const char *name, const char *value)
218 struct discover_device_param *param;
221 list_for_each_entry(&device->params, param, list) {
222 if (!strcmp(param->name, name)) {
231 param = talloc(device, struct discover_device_param);
232 param->name = talloc_strdup(param, name);
233 list_add(&device->params, ¶m->list);
236 list_remove(¶m->list);
240 talloc_free(param->value);
243 param->value = talloc_strdup(param, value);
246 const char *discover_device_get_param(struct discover_device *device,
249 struct discover_device_param *param;
251 list_for_each_entry(&device->params, param, list) {
252 if (!strcmp(param->name, name))
258 struct device_handler *device_handler_init(struct discover_server *server,
259 struct waitset *waitset, int dry_run)
261 struct device_handler *handler;
263 handler = talloc_zero(NULL, struct device_handler);
264 handler->server = server;
265 handler->waitset = waitset;
266 handler->dry_run = dry_run;
267 handler->autoboot_enabled = config_get()->autoboot_enabled;
269 list_init(&handler->unresolved_boot_options);
271 /* set up our mount point base */
272 pb_mkdir_recursive(mount_base());
279 void device_handler_remove(struct device_handler *handler,
280 struct discover_device *device)
284 for (i = 0; i < handler->n_devices; i++)
285 if (handler->devices[i] == device)
288 if (i == handler->n_devices) {
293 handler->n_devices--;
294 memmove(&handler->devices[i], &handler->devices[i + 1],
295 (handler->n_devices - i) * sizeof(handler->devices[0]));
296 handler->devices = talloc_realloc(handler, handler->devices,
297 struct discover_device *, handler->n_devices);
299 if (device->notified)
300 discover_server_notify_device_remove(handler->server,
306 static void boot_status(void *arg, struct boot_status *status)
308 struct device_handler *handler = arg;
310 discover_server_notify_boot_status(handler->server, status);
313 static void countdown_status(struct device_handler *handler,
314 struct discover_boot_option *opt, unsigned int sec)
316 struct boot_status status;
318 status.type = BOOT_STATUS_INFO;
319 status.progress = -1;
320 status.detail = NULL;
321 status.message = talloc_asprintf(handler,
322 "Booting %s in %u sec", opt->option->name, sec);
324 discover_server_notify_boot_status(handler->server, &status);
326 talloc_free(status.message);
329 static int default_timeout(void *arg)
331 struct device_handler *handler = arg;
332 struct discover_boot_option *opt;
334 if (!handler->default_boot_option)
337 if (handler->pending_boot)
340 opt = handler->default_boot_option;
342 if (handler->sec_to_boot) {
343 countdown_status(handler, opt, handler->sec_to_boot);
344 handler->sec_to_boot--;
345 handler->timeout_waiter = waiter_register_timeout(
346 handler->waitset, 1000,
347 default_timeout, handler);
351 handler->timeout_waiter = NULL;
353 pb_log("Timeout expired, booting default option %s\n", opt->option->id);
355 handler->pending_boot = boot(handler, handler->default_boot_option,
356 NULL, handler->dry_run, boot_status, handler);
357 handler->pending_boot_is_default = true;
361 static bool priority_match(struct boot_priority *prio,
362 struct discover_boot_option *opt)
364 return prio->type == opt->device->device->type;
367 static int default_option_priority(struct discover_boot_option *opt)
369 const struct config *config;
370 struct boot_priority *prio;
373 config = config_get();
375 for (i = 0; i < config->n_boot_priorities; i++) {
376 prio = &config->boot_priorities[i];
377 if (priority_match(prio, opt))
384 static void set_default(struct device_handler *handler,
385 struct discover_boot_option *opt)
387 if (!handler->autoboot_enabled)
390 /* Resolve any conflicts: if we have a new default option, it only
391 * replaces the current if it has a higher priority. */
392 if (handler->default_boot_option) {
393 int new_prio, cur_prio;
395 new_prio = default_option_priority(opt);
396 cur_prio = default_option_priority(
397 handler->default_boot_option);
399 if (new_prio < cur_prio) {
400 handler->default_boot_option = opt;
401 /* extend the timeout a little, so the user sees some
402 * indication of the change */
403 handler->sec_to_boot += 2;
409 handler->sec_to_boot = config_get()->autoboot_timeout_sec;
410 handler->default_boot_option = opt;
412 pb_log("Boot option %s set as default, timeout %u sec.\n",
413 opt->option->id, handler->sec_to_boot);
415 default_timeout(handler);
418 static bool resource_is_resolved(struct resource *res)
420 return !res || res->resolved;
423 /* We only use this in an assert, which will disappear if we're compiling
424 * with NDEBUG, so we need the 'used' attribute for these builds */
425 static bool __attribute__((used)) boot_option_is_resolved(
426 struct discover_boot_option *opt)
428 return resource_is_resolved(opt->boot_image) &&
429 resource_is_resolved(opt->initrd) &&
430 resource_is_resolved(opt->dtb) &&
431 resource_is_resolved(opt->icon);
434 static bool resource_resolve(struct resource *res, const char *name,
435 struct discover_boot_option *opt,
436 struct device_handler *handler)
438 struct parser *parser = opt->source;
440 if (resource_is_resolved(res))
443 pb_log("Attempting to resolve resource %s->%s with parser %s\n",
444 opt->option->id, name, parser->name);
445 parser->resolve_resource(handler, res);
447 return res->resolved;
450 static bool boot_option_resolve(struct discover_boot_option *opt,
451 struct device_handler *handler)
453 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
454 resource_resolve(opt->initrd, "initrd", opt, handler) &&
455 resource_resolve(opt->dtb, "dtb", opt, handler) &&
456 resource_resolve(opt->icon, "icon", opt, handler);
459 static void boot_option_finalise(struct device_handler *handler,
460 struct discover_boot_option *opt)
462 assert(boot_option_is_resolved(opt));
464 /* check that the parsers haven't set any of the final data */
465 assert(!opt->option->boot_image_file);
466 assert(!opt->option->initrd_file);
467 assert(!opt->option->dtb_file);
468 assert(!opt->option->icon_file);
469 assert(!opt->option->device_id);
472 opt->option->boot_image_file = opt->boot_image->url->full;
474 opt->option->initrd_file = opt->initrd->url->full;
476 opt->option->dtb_file = opt->dtb->url->full;
478 opt->option->icon_file = opt->icon->url->full;
480 opt->option->device_id = opt->device->device->id;
482 if (opt->option->is_default)
483 set_default(handler, opt);
486 static void notify_boot_option(struct device_handler *handler,
487 struct discover_boot_option *opt)
489 struct discover_device *dev = opt->device;
492 discover_server_notify_device_add(handler->server,
493 opt->device->device);
494 dev->notified = true;
495 discover_server_notify_boot_option_add(handler->server, opt->option);
498 static void process_boot_option_queue(struct device_handler *handler)
500 struct discover_boot_option *opt, *tmp;
502 list_for_each_entry_safe(&handler->unresolved_boot_options,
505 pb_log("queue: attempting resolution for %s\n",
508 if (!boot_option_resolve(opt, handler))
511 pb_log("\tresolved!\n");
513 list_remove(&opt->list);
514 list_add_tail(&opt->device->boot_options, &opt->list);
515 talloc_steal(opt->device, opt);
516 boot_option_finalise(handler, opt);
517 notify_boot_option(handler, opt);
521 struct discover_context *device_handler_discover_context_create(
522 struct device_handler *handler,
523 struct discover_device *device)
525 struct discover_context *ctx;
527 ctx = talloc(handler, struct discover_context);
528 ctx->device = device;
529 ctx->conf_url = NULL;
530 ctx->test_data = NULL;
531 list_init(&ctx->boot_options);
537 * context_commit - Commit a temporary discovery context to the handler,
538 * and notify the clients about any new options / devices
540 void device_handler_discover_context_commit(struct device_handler *handler,
541 struct discover_context *ctx)
543 struct discover_device *dev = ctx->device;
544 struct discover_boot_option *opt, *tmp;
546 if (!device_lookup_by_id(handler, dev->device->id))
547 device_handler_add_device(handler, dev);
549 /* move boot options from the context to the device */
550 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
551 list_remove(&opt->list);
553 if (boot_option_resolve(opt, handler)) {
554 pb_log("boot option %s is resolved, "
555 "sending to clients\n",
557 list_add_tail(&dev->boot_options, &opt->list);
558 talloc_steal(dev, opt);
559 boot_option_finalise(handler, opt);
560 notify_boot_option(handler, opt);
562 if (!opt->source->resolve_resource) {
563 pb_log("parser %s gave us an unresolved "
564 "resource (%s), but no way to "
566 opt->source->name, opt->option->id);
569 pb_log("boot option %s is unresolved, "
572 list_add(&handler->unresolved_boot_options,
574 talloc_steal(handler, opt);
580 void device_handler_add_device(struct device_handler *handler,
581 struct discover_device *device)
583 handler->n_devices++;
584 handler->devices = talloc_realloc(handler, handler->devices,
585 struct discover_device *, handler->n_devices);
586 handler->devices[handler->n_devices - 1] = device;
590 /* Start discovery on a hotplugged device. The device will be in our devices
591 * array, but has only just been initialised by the hotplug source.
593 int device_handler_discover(struct device_handler *handler,
594 struct discover_device *dev, enum conf_method method)
596 struct discover_context *ctx;
599 process_boot_option_queue(handler);
601 /* create our context */
602 ctx = device_handler_discover_context_create(handler, dev);
604 rc = mount_device(dev);
608 /* run the parsers. This will populate the ctx's boot_option list. */
609 iterate_parsers(ctx, method);
611 /* add discovered stuff to the handler */
612 device_handler_discover_context_commit(handler, ctx);
620 /* incoming conf event */
621 int device_handler_conf(struct device_handler *handler,
622 struct discover_device *dev, struct pb_url *url,
623 enum conf_method method)
625 struct discover_context *ctx;
627 /* create our context */
628 ctx = device_handler_discover_context_create(handler, dev);
631 iterate_parsers(ctx, method);
633 device_handler_discover_context_commit(handler, ctx);
640 static struct discover_boot_option *find_boot_option_by_id(
641 struct device_handler *handler, const char *id)
645 for (i = 0; i < handler->n_devices; i++) {
646 struct discover_device *dev = handler->devices[i];
647 struct discover_boot_option *opt;
649 list_for_each_entry(&dev->boot_options, opt, list)
650 if (!strcmp(opt->option->id, id))
657 void device_handler_boot(struct device_handler *handler,
658 struct boot_command *cmd)
660 struct discover_boot_option *opt = NULL;
662 if (cmd->option_id && strlen(cmd->option_id))
663 opt = find_boot_option_by_id(handler, cmd->option_id);
665 if (handler->pending_boot)
666 boot_cancel(handler->pending_boot);
667 handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
668 boot_status, handler);
669 handler->pending_boot_is_default = false;
672 void device_handler_cancel_default(struct device_handler *handler)
674 struct boot_status status;
676 if (handler->timeout_waiter)
677 waiter_remove(handler->timeout_waiter);
679 handler->timeout_waiter = NULL;
680 handler->autoboot_enabled = false;
682 /* we only send status if we had a default boot option queued */
683 if (!handler->default_boot_option)
686 pb_log("Cancelling default boot option\n");
688 if (handler->pending_boot && handler->pending_boot_is_default) {
689 boot_cancel(handler->pending_boot);
690 handler->pending_boot = NULL;
691 handler->pending_boot_is_default = false;
694 handler->default_boot_option = NULL;
696 status.type = BOOT_STATUS_INFO;
697 status.progress = -1;
698 status.detail = NULL;
699 status.message = "Default boot cancelled";
701 discover_server_notify_boot_status(handler->server, &status);
704 #ifndef PETITBOOT_TEST
705 static bool check_existing_mount(struct discover_device *dev)
707 struct stat devstat, mntstat;
712 rc = stat(dev->device_path, &devstat);
714 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
718 if (!S_ISBLK(devstat.st_mode)) {
719 pb_debug("%s: %s isn't a block device?\n", __func__,
724 fp = fopen("/proc/self/mounts", "r");
731 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
734 rc = stat(mnt->mnt_fsname, &mntstat);
738 if (!S_ISBLK(mntstat.st_mode))
741 if (mntstat.st_rdev == devstat.st_rdev) {
742 dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
743 dev->mounted_rw = !!hasmntopt(mnt, "rw");
745 dev->unmount = false;
747 pb_debug("%s: %s is already mounted (r%c) at %s\n",
748 __func__, dev->device_path,
749 dev->mounted_rw ? 'w' : 'o',
760 static int mount_device(struct discover_device *dev)
764 if (!dev->device_path)
770 if (check_existing_mount(dev))
773 dev->mount_path = join_paths(dev, mount_base(),
776 if (pb_mkdir_recursive(dev->mount_path)) {
777 pb_log("couldn't create mount directory %s: %s\n",
778 dev->mount_path, strerror(errno));
782 rc = process_run_simple(dev, pb_system_apps.mount,
783 dev->device_path, dev->mount_path,
787 dev->mounted_rw = false;
792 /* Retry mount without ro option. */
793 rc = process_run_simple(dev, pb_system_apps.mount,
794 dev->device_path, dev->mount_path, NULL);
798 dev->mounted_rw = true;
803 pb_rmdir_recursive(mount_base(), dev->mount_path);
805 talloc_free(dev->mount_path);
806 dev->mount_path = NULL;
810 static int umount_device(struct discover_device *dev)
814 if (!dev->mounted || !dev->unmount)
817 status = process_run_simple(dev, pb_system_apps.umount,
818 dev->mount_path, NULL);
820 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
823 dev->mounted = false;
825 pb_rmdir_recursive(mount_base(), dev->mount_path);
827 talloc_free(dev->mount_path);
828 dev->mount_path = NULL;
833 int device_request_write(struct discover_device *dev, bool *release)
845 rc = process_run_simple(dev, pb_system_apps.mount, dev->mount_path,
846 "-o", "remount,rw", NULL);
850 dev->mounted_rw = true;
855 void device_release_write(struct discover_device *dev, bool release)
860 process_run_simple(dev, pb_system_apps.mount, dev->mount_path,
861 "-o", "remount,ro", NULL);
862 dev->mounted_rw = false;
867 static int umount_device(struct discover_device *dev __attribute__((unused)))
872 static int __attribute__((unused)) mount_device(
873 struct discover_device *dev __attribute__((unused)))
878 int device_request_write(struct discover_device *dev __attribute__((unused)),
885 void device_release_write(struct discover_device *dev __attribute__((unused)),
886 bool release __attribute__((unused)))