11 #include <sys/mount.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>
20 #include <i18n/i18n.h>
22 #include <sys/types.h>
23 #include <sys/socket.h>
25 #include <arpa/inet.h>
27 #include "device-handler.h"
28 #include "discover-server.h"
29 #include "devmapper.h"
30 #include "user-event.h"
42 enum default_priority {
43 DEFAULT_PRIORITY_REMOTE = 1,
44 DEFAULT_PRIORITY_LOCAL_FIRST = 2,
45 DEFAULT_PRIORITY_LOCAL_LAST = 0xfe,
46 DEFAULT_PRIORITY_DISABLED = 0xff,
49 struct device_handler {
50 struct discover_server *server;
54 struct network *network;
55 struct user_event *user_event;
57 struct discover_device **devices;
58 unsigned int n_devices;
60 struct ramdisk_device **ramdisks;
61 unsigned int n_ramdisks;
63 struct waitset *waitset;
64 struct waiter *timeout_waiter;
65 bool autoboot_enabled;
66 unsigned int sec_to_boot;
68 struct discover_boot_option *default_boot_option;
69 int default_boot_option_priority;
71 struct list unresolved_boot_options;
73 struct boot_task *pending_boot;
74 bool pending_boot_is_default;
77 static int mount_device(struct discover_device *dev);
78 static int umount_device(struct discover_device *dev);
80 static int device_handler_init_sources(struct device_handler *handler);
81 static void device_handler_reinit_sources(struct device_handler *handler);
83 static void device_handler_update_lang(const char *lang);
85 void discover_context_add_boot_option(struct discover_context *ctx,
86 struct discover_boot_option *boot_option)
88 boot_option->source = ctx->parser;
89 list_add_tail(&ctx->boot_options, &boot_option->list);
90 talloc_steal(ctx, boot_option);
94 * device_handler_get_device_count - Get the count of current handler devices.
97 int device_handler_get_device_count(const struct device_handler *handler)
99 return handler->n_devices;
103 * device_handler_get_device - Get a handler device by index.
106 const struct discover_device *device_handler_get_device(
107 const struct device_handler *handler, unsigned int index)
109 if (index >= handler->n_devices) {
110 assert(0 && "bad index");
114 return handler->devices[index];
117 struct discover_boot_option *discover_boot_option_create(
118 struct discover_context *ctx,
119 struct discover_device *device)
121 struct discover_boot_option *opt;
123 opt = talloc_zero(ctx, struct discover_boot_option);
124 opt->option = talloc_zero(opt, struct boot_option);
125 opt->device = device;
130 static int device_match_uuid(struct discover_device *dev, const char *uuid)
132 return dev->uuid && !strcmp(dev->uuid, uuid);
135 static int device_match_label(struct discover_device *dev, const char *label)
137 return dev->label && !strcmp(dev->label, label);
140 static int device_match_id(struct discover_device *dev, const char *id)
142 return !strcmp(dev->device->id, id);
145 static int device_match_serial(struct discover_device *dev, const char *serial)
147 const char *val = discover_device_get_param(dev, "ID_SERIAL");
148 return val && !strcmp(val, serial);
151 static struct discover_device *device_lookup(
152 struct device_handler *device_handler,
153 int (match_fn)(struct discover_device *, const char *),
156 struct discover_device *dev;
162 for (i = 0; i < device_handler->n_devices; i++) {
163 dev = device_handler->devices[i];
165 if (match_fn(dev, str))
172 struct discover_device *device_lookup_by_name(struct device_handler *handler,
175 if (!strncmp(name, "/dev/", strlen("/dev/")))
176 name += strlen("/dev/");
178 return device_lookup_by_id(handler, name);
181 struct discover_device *device_lookup_by_uuid(
182 struct device_handler *device_handler,
185 return device_lookup(device_handler, device_match_uuid, uuid);
188 struct discover_device *device_lookup_by_label(
189 struct device_handler *device_handler,
192 return device_lookup(device_handler, device_match_label, label);
195 struct discover_device *device_lookup_by_id(
196 struct device_handler *device_handler,
199 return device_lookup(device_handler, device_match_id, id);
202 struct discover_device *device_lookup_by_serial(
203 struct device_handler *device_handler,
206 return device_lookup(device_handler, device_match_serial, serial);
209 void device_handler_destroy(struct device_handler *handler)
211 talloc_free(handler);
214 static int destroy_device(void *arg)
216 struct discover_device *dev = arg;
223 struct discover_device *discover_device_create(struct device_handler *handler,
226 struct discover_device *dev;
228 dev = device_lookup_by_id(handler, id);
232 dev = talloc_zero(handler, struct discover_device);
233 dev->device = talloc_zero(dev, struct device);
234 dev->device->id = talloc_strdup(dev->device, id);
235 list_init(&dev->params);
236 list_init(&dev->boot_options);
238 talloc_set_destructor(dev, destroy_device);
243 struct discover_device_param {
246 struct list_item list;
249 void discover_device_set_param(struct discover_device *device,
250 const char *name, const char *value)
252 struct discover_device_param *param;
255 list_for_each_entry(&device->params, param, list) {
256 if (!strcmp(param->name, name)) {
265 param = talloc(device, struct discover_device_param);
266 param->name = talloc_strdup(param, name);
267 list_add(&device->params, ¶m->list);
270 list_remove(¶m->list);
274 talloc_free(param->value);
277 param->value = talloc_strdup(param, value);
280 const char *discover_device_get_param(struct discover_device *device,
283 struct discover_device_param *param;
285 list_for_each_entry(&device->params, param, list) {
286 if (!strcmp(param->name, name))
292 struct device_handler *device_handler_init(struct discover_server *server,
293 struct waitset *waitset, int dry_run)
295 struct device_handler *handler;
298 handler = talloc_zero(NULL, struct device_handler);
299 handler->server = server;
300 handler->waitset = waitset;
301 handler->dry_run = dry_run;
302 handler->autoboot_enabled = config_get()->autoboot_enabled;
304 list_init(&handler->unresolved_boot_options);
306 /* set up our mount point base */
307 pb_mkdir_recursive(mount_base());
311 if (config_get()->safe_mode)
314 rc = device_handler_init_sources(handler);
316 talloc_free(handler);
323 void device_handler_reinit(struct device_handler *handler)
325 struct discover_boot_option *opt, *tmp;
326 struct ramdisk_device *ramdisk;
329 device_handler_cancel_default(handler);
331 /* free unresolved boot options */
332 list_for_each_entry_safe(&handler->unresolved_boot_options,
335 list_init(&handler->unresolved_boot_options);
337 /* drop all devices */
338 for (i = 0; i < handler->n_devices; i++) {
339 discover_server_notify_device_remove(handler->server,
340 handler->devices[i]->device);
341 ramdisk = handler->devices[i]->ramdisk;
342 talloc_free(handler->devices[i]);
343 talloc_free(ramdisk);
346 talloc_free(handler->devices);
347 handler->devices = NULL;
348 handler->n_devices = 0;
349 talloc_free(handler->ramdisks);
350 handler->ramdisks = NULL;
351 handler->n_ramdisks = 0;
353 device_handler_reinit_sources(handler);
356 void device_handler_remove(struct device_handler *handler,
357 struct discover_device *device)
359 struct discover_boot_option *opt, *tmp;
362 for (i = 0; i < handler->n_devices; i++)
363 if (handler->devices[i] == device)
366 if (i == handler->n_devices) {
371 /* Free any unresolved options, as they're currently allocated
372 * against the handler */
373 list_for_each_entry_safe(&handler->unresolved_boot_options,
375 if (opt->device != device)
377 list_remove(&opt->list);
381 /* if this is a network device, we have to unregister it from the
383 if (device->device->type == DEVICE_TYPE_NETWORK)
384 network_unregister_device(handler->network, device);
386 handler->n_devices--;
387 memmove(&handler->devices[i], &handler->devices[i + 1],
388 (handler->n_devices - i) * sizeof(handler->devices[0]));
389 handler->devices = talloc_realloc(handler, handler->devices,
390 struct discover_device *, handler->n_devices);
392 if (device->notified)
393 discover_server_notify_device_remove(handler->server,
399 static void boot_status(void *arg, struct boot_status *status)
401 struct device_handler *handler = arg;
403 discover_server_notify_boot_status(handler->server, status);
406 static void countdown_status(struct device_handler *handler,
407 struct discover_boot_option *opt, unsigned int sec)
409 struct boot_status status;
411 status.type = BOOT_STATUS_INFO;
412 status.progress = -1;
413 status.detail = NULL;
414 status.message = talloc_asprintf(handler,
415 _("Booting in %d sec: %s"), sec, opt->option->name);
417 discover_server_notify_boot_status(handler->server, &status);
419 talloc_free(status.message);
422 static int default_timeout(void *arg)
424 struct device_handler *handler = arg;
425 struct discover_boot_option *opt;
427 if (!handler->default_boot_option)
430 if (handler->pending_boot)
433 opt = handler->default_boot_option;
435 if (handler->sec_to_boot) {
436 countdown_status(handler, opt, handler->sec_to_boot);
437 handler->sec_to_boot--;
438 handler->timeout_waiter = waiter_register_timeout(
439 handler->waitset, 1000,
440 default_timeout, handler);
444 handler->timeout_waiter = NULL;
446 pb_log("Timeout expired, booting default option %s\n", opt->option->id);
448 handler->pending_boot = boot(handler, handler->default_boot_option,
449 NULL, handler->dry_run, boot_status, handler);
450 handler->pending_boot_is_default = true;
455 enum ipmi_bootdev ipmi_type;
456 enum device_type device_type;
457 } device_type_map[] = {
458 { IPMI_BOOTDEV_NETWORK, DEVICE_TYPE_NETWORK },
459 { IPMI_BOOTDEV_DISK, DEVICE_TYPE_DISK },
460 { IPMI_BOOTDEV_CDROM, DEVICE_TYPE_OPTICAL },
463 static bool ipmi_device_type_matches(enum ipmi_bootdev ipmi_type,
464 enum device_type device_type)
468 for (i = 0; i < ARRAY_SIZE(device_type_map); i++) {
469 if (device_type_map[i].device_type == device_type)
470 return device_type_map[i].ipmi_type == ipmi_type;
476 static int autoboot_option_priority(const struct config *config,
477 struct discover_boot_option *opt)
479 enum device_type type = opt->device->device->type;
480 const char *uuid = opt->device->uuid;
481 struct autoboot_option *auto_opt;
484 for (i = 0; i < config->n_autoboot_opts; i++) {
485 auto_opt = &config->autoboot_opts[i];
486 if (auto_opt->boot_type == BOOT_DEVICE_UUID)
487 if (!strcmp(auto_opt->uuid, uuid))
488 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
490 if (auto_opt->boot_type == BOOT_DEVICE_TYPE)
491 if (auto_opt->type == type ||
492 auto_opt->type == DEVICE_TYPE_ANY)
493 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
500 * We have different priorities to resolve conflicts between boot options that
501 * report to be the default for their device. This function assigns a priority
504 static enum default_priority default_option_priority(
505 struct discover_boot_option *opt)
507 const struct config *config;
509 config = config_get();
511 /* We give highest priority to IPMI-configured boot options. If
512 * we have an IPMI bootdev configuration set, then we don't allow
513 * any other defaults */
514 if (config->ipmi_bootdev) {
515 bool ipmi_match = ipmi_device_type_matches(config->ipmi_bootdev,
516 opt->device->device->type);
518 return DEFAULT_PRIORITY_REMOTE;
520 pb_debug("handler: disabled default priority due to "
521 "non-matching IPMI type %x\n",
522 config->ipmi_bootdev);
523 return DEFAULT_PRIORITY_DISABLED;
526 /* Next, try to match the option against the user-defined autoboot
527 * options, either by device UUID or type. */
528 if (config->n_autoboot_opts) {
529 int boot_match = autoboot_option_priority(config, opt);
534 /* If the option didn't match any entry in the array, it is disabled */
535 pb_debug("handler: disabled default priority due to "
536 "non-matching UUID or type\n");
537 return DEFAULT_PRIORITY_DISABLED;
540 static void set_default(struct device_handler *handler,
541 struct discover_boot_option *opt)
543 enum default_priority cur_prio, new_prio;
545 if (!handler->autoboot_enabled)
548 pb_debug("handler: new default option: %s\n", opt->option->id);
550 new_prio = default_option_priority(opt);
552 /* Anything outside our range prevents a default boot */
553 if (new_prio >= DEFAULT_PRIORITY_DISABLED)
556 pb_debug("handler: calculated priority %d\n", new_prio);
558 /* Resolve any conflicts: if we have a new default option, it only
559 * replaces the current if it has a higher priority. */
560 if (handler->default_boot_option) {
562 cur_prio = handler->default_boot_option_priority;
564 if (new_prio < cur_prio) {
565 pb_log("handler: new prio %d beats "
566 "old prio %d for %s\n",
568 handler->default_boot_option
570 handler->default_boot_option = opt;
571 handler->default_boot_option_priority = new_prio;
572 /* extend the timeout a little, so the user sees some
573 * indication of the change */
574 handler->sec_to_boot += 2;
580 handler->sec_to_boot = config_get()->autoboot_timeout_sec;
581 handler->default_boot_option = opt;
582 handler->default_boot_option_priority = new_prio;
584 pb_log("handler: boot option %s set as default, timeout %u sec.\n",
585 opt->option->id, handler->sec_to_boot);
587 default_timeout(handler);
590 static bool resource_is_resolved(struct resource *res)
592 return !res || res->resolved;
595 /* We only use this in an assert, which will disappear if we're compiling
596 * with NDEBUG, so we need the 'used' attribute for these builds */
597 static bool __attribute__((used)) boot_option_is_resolved(
598 struct discover_boot_option *opt)
600 return resource_is_resolved(opt->boot_image) &&
601 resource_is_resolved(opt->initrd) &&
602 resource_is_resolved(opt->dtb) &&
603 resource_is_resolved(opt->icon);
606 static bool resource_resolve(struct resource *res, const char *name,
607 struct discover_boot_option *opt,
608 struct device_handler *handler)
610 struct parser *parser = opt->source;
612 if (resource_is_resolved(res))
615 pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
616 opt->option->id, name, parser->name);
617 parser->resolve_resource(handler, res);
619 return res->resolved;
622 static bool boot_option_resolve(struct discover_boot_option *opt,
623 struct device_handler *handler)
625 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
626 resource_resolve(opt->initrd, "initrd", opt, handler) &&
627 resource_resolve(opt->dtb, "dtb", opt, handler) &&
628 resource_resolve(opt->icon, "icon", opt, handler);
631 static void boot_option_finalise(struct device_handler *handler,
632 struct discover_boot_option *opt)
634 assert(boot_option_is_resolved(opt));
636 /* check that the parsers haven't set any of the final data */
637 assert(!opt->option->boot_image_file);
638 assert(!opt->option->initrd_file);
639 assert(!opt->option->dtb_file);
640 assert(!opt->option->icon_file);
641 assert(!opt->option->device_id);
644 opt->option->boot_image_file = opt->boot_image->url->full;
646 opt->option->initrd_file = opt->initrd->url->full;
648 opt->option->dtb_file = opt->dtb->url->full;
650 opt->option->icon_file = opt->icon->url->full;
652 opt->option->device_id = opt->device->device->id;
654 if (opt->option->is_default)
655 set_default(handler, opt);
658 static void notify_boot_option(struct device_handler *handler,
659 struct discover_boot_option *opt)
661 struct discover_device *dev = opt->device;
664 discover_server_notify_device_add(handler->server,
665 opt->device->device);
666 dev->notified = true;
667 discover_server_notify_boot_option_add(handler->server, opt->option);
670 static void process_boot_option_queue(struct device_handler *handler)
672 struct discover_boot_option *opt, *tmp;
674 list_for_each_entry_safe(&handler->unresolved_boot_options,
677 pb_debug("queue: attempting resolution for %s\n",
680 if (!boot_option_resolve(opt, handler))
683 pb_debug("\tresolved!\n");
685 list_remove(&opt->list);
686 list_add_tail(&opt->device->boot_options, &opt->list);
687 talloc_steal(opt->device, opt);
688 boot_option_finalise(handler, opt);
689 notify_boot_option(handler, opt);
693 struct discover_context *device_handler_discover_context_create(
694 struct device_handler *handler,
695 struct discover_device *device)
697 struct discover_context *ctx;
699 ctx = talloc_zero(handler, struct discover_context);
700 ctx->device = device;
701 ctx->network = handler->network;
702 list_init(&ctx->boot_options);
708 * context_commit - Commit a temporary discovery context to the handler,
709 * and notify the clients about any new options / devices
711 void device_handler_discover_context_commit(struct device_handler *handler,
712 struct discover_context *ctx)
714 struct discover_device *dev = ctx->device;
715 struct discover_boot_option *opt, *tmp;
717 if (!device_lookup_by_id(handler, dev->device->id))
718 device_handler_add_device(handler, dev);
720 /* move boot options from the context to the device */
721 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
722 list_remove(&opt->list);
724 if (boot_option_resolve(opt, handler)) {
725 pb_log("boot option %s is resolved, "
726 "sending to clients\n",
728 list_add_tail(&dev->boot_options, &opt->list);
729 talloc_steal(dev, opt);
730 boot_option_finalise(handler, opt);
731 notify_boot_option(handler, opt);
733 if (!opt->source->resolve_resource) {
734 pb_log("parser %s gave us an unresolved "
735 "resource (%s), but no way to "
737 opt->source->name, opt->option->id);
740 pb_log("boot option %s is unresolved, "
743 list_add(&handler->unresolved_boot_options,
745 talloc_steal(handler, opt);
751 void device_handler_add_device(struct device_handler *handler,
752 struct discover_device *device)
754 handler->n_devices++;
755 handler->devices = talloc_realloc(handler, handler->devices,
756 struct discover_device *, handler->n_devices);
757 handler->devices[handler->n_devices - 1] = device;
759 if (device->device->type == DEVICE_TYPE_NETWORK)
760 network_register_device(handler->network, device);
763 void device_handler_add_ramdisk(struct device_handler *handler,
766 struct ramdisk_device *dev;
772 for (i = 0; i < handler->n_ramdisks; i++)
773 if (!strcmp(handler->ramdisks[i]->path, path))
776 dev = talloc_zero(handler, struct ramdisk_device);
778 pb_log("Failed to allocate memory to track %s\n", path);
782 dev->path = talloc_strdup(handler, path);
784 handler->ramdisks = talloc_realloc(handler, handler->ramdisks,
785 struct ramdisk_device *,
786 handler->n_ramdisks + 1);
787 if (!handler->ramdisks) {
788 pb_log("Failed to reallocate memory"
789 "- ramdisk tracking inconsistent!\n");
793 handler->ramdisks[i] = dev;
794 i = handler->n_ramdisks++;
797 struct ramdisk_device *device_handler_get_ramdisk(
798 struct device_handler *handler)
804 /* Check if free ramdisk exists */
805 for (i = 0; i < handler->n_ramdisks; i++)
806 if (!handler->ramdisks[i]->snapshot &&
807 !handler->ramdisks[i]->origin &&
808 !handler->ramdisks[i]->base)
809 return handler->ramdisks[i];
811 /* Otherwise create a new one */
812 name = talloc_asprintf(handler, "/dev/ram%d",
813 handler->n_ramdisks);
815 pb_debug("Failed to allocate memory to name /dev/ram%d",
816 handler->n_ramdisks);
820 id = makedev(1, handler->n_ramdisks);
821 if (mknod(name, S_IFBLK, id)) {
822 if (errno == EEXIST) {
823 /* We haven't yet received updates for existing
824 * ramdisks - add and use this one */
825 pb_debug("Using untracked ramdisk %s\n", name);
827 pb_log("Failed to create new ramdisk %s: %s\n",
828 name, strerror(errno));
832 device_handler_add_ramdisk(handler, name);
835 return handler->ramdisks[i];
838 void device_handler_release_ramdisk(struct discover_device *device)
840 struct ramdisk_device *ramdisk = device->ramdisk;
842 talloc_free(ramdisk->snapshot);
843 talloc_free(ramdisk->origin);
844 talloc_free(ramdisk->base);
846 ramdisk->snapshot = ramdisk->origin = ramdisk->base = NULL;
847 ramdisk->sectors = 0;
849 device->ramdisk = NULL;
852 /* Start discovery on a hotplugged device. The device will be in our devices
853 * array, but has only just been initialised by the hotplug source.
855 int device_handler_discover(struct device_handler *handler,
856 struct discover_device *dev)
858 struct discover_context *ctx;
859 struct boot_status *status;
862 status = talloc_zero(handler, struct boot_status);
863 status->type = BOOT_STATUS_INFO;
864 status->message = talloc_asprintf(status, "Processing %s device %s",
865 device_type_display_name(dev->device->type),
867 boot_status(handler, status);
869 process_boot_option_queue(handler);
871 /* create our context */
872 ctx = device_handler_discover_context_create(handler, dev);
874 rc = mount_device(dev);
878 /* add this device to our system info */
879 system_info_register_blockdev(dev->device->id, dev->uuid,
882 /* run the parsers. This will populate the ctx's boot_option list. */
883 iterate_parsers(ctx);
885 /* add discovered stuff to the handler */
886 device_handler_discover_context_commit(handler, ctx);
889 status->message = talloc_asprintf(status,"Processing %s complete\n",
891 boot_status(handler, status);
899 /* Incoming dhcp event */
900 int device_handler_dhcp(struct device_handler *handler,
901 struct discover_device *dev, struct event *event)
903 struct discover_context *ctx;
904 struct boot_status *status;
906 status = talloc_zero(handler, struct boot_status);
907 status->type = BOOT_STATUS_INFO;
908 status->message = talloc_asprintf(status, "Processing dhcp event on %s",
910 boot_status(handler, status);
912 /* create our context */
913 ctx = device_handler_discover_context_create(handler, dev);
916 iterate_parsers(ctx);
918 device_handler_discover_context_commit(handler, ctx);
920 status->message = talloc_asprintf(status,"Processing %s complete\n",
922 boot_status(handler, status);
930 /* incoming conf event */
931 int device_handler_conf(struct device_handler *handler,
932 struct discover_device *dev, struct pb_url *url)
934 struct discover_context *ctx;
935 struct boot_status *status;
937 status = talloc_zero(handler, struct boot_status);
938 status->type = BOOT_STATUS_INFO;
939 status->message = talloc_asprintf(status, "Processing user config");
940 boot_status(handler, status);
942 /* create our context */
943 ctx = device_handler_discover_context_create(handler, dev);
946 iterate_parsers(ctx);
948 device_handler_discover_context_commit(handler, ctx);
950 status->message = talloc_asprintf(status,
951 "Processing user config complete");
952 boot_status(handler, status);
960 static struct discover_boot_option *find_boot_option_by_id(
961 struct device_handler *handler, const char *id)
965 for (i = 0; i < handler->n_devices; i++) {
966 struct discover_device *dev = handler->devices[i];
967 struct discover_boot_option *opt;
969 list_for_each_entry(&dev->boot_options, opt, list)
970 if (!strcmp(opt->option->id, id))
977 void device_handler_boot(struct device_handler *handler,
978 struct boot_command *cmd)
980 struct discover_boot_option *opt = NULL;
982 if (cmd->option_id && strlen(cmd->option_id))
983 opt = find_boot_option_by_id(handler, cmd->option_id);
985 if (handler->pending_boot)
986 boot_cancel(handler->pending_boot);
990 handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
991 boot_status, handler);
992 handler->pending_boot_is_default = false;
995 void device_handler_cancel_default(struct device_handler *handler)
997 struct boot_status status;
999 if (handler->timeout_waiter)
1000 waiter_remove(handler->timeout_waiter);
1002 handler->timeout_waiter = NULL;
1003 handler->autoboot_enabled = false;
1005 /* we only send status if we had a default boot option queued */
1006 if (!handler->default_boot_option)
1009 pb_log("Cancelling default boot option\n");
1011 if (handler->pending_boot && handler->pending_boot_is_default) {
1012 boot_cancel(handler->pending_boot);
1013 handler->pending_boot = NULL;
1014 handler->pending_boot_is_default = false;
1017 handler->default_boot_option = NULL;
1019 status.type = BOOT_STATUS_INFO;
1020 status.progress = -1;
1021 status.detail = NULL;
1022 status.message = _("Default boot cancelled");
1024 discover_server_notify_boot_status(handler->server, &status);
1027 void device_handler_update_config(struct device_handler *handler,
1028 struct config *config)
1032 rc = config_set(config);
1036 discover_server_notify_config(handler->server, config);
1037 device_handler_update_lang(config->lang);
1038 device_handler_reinit(handler);
1041 static char *device_from_addr(void *ctx, struct pb_url *url)
1043 char *ipaddr, *buf, *tok, *dev = NULL;
1044 const char *delim = " ";
1045 struct sockaddr_in *ip;
1046 struct sockaddr_in si;
1047 struct addrinfo *res;
1051 /* Note: IPv4 only */
1052 rc = inet_pton(AF_INET, url->host, &(si.sin_addr));
1056 /* need to turn hostname into a valid IP */
1057 rc = getaddrinfo(url->host, NULL, NULL, &res);
1059 pb_debug("%s: Invalid URL\n",__func__);
1062 ipaddr = talloc_array(ctx,char,INET_ADDRSTRLEN);
1063 ip = (struct sockaddr_in *) res->ai_addr;
1064 inet_ntop(AF_INET, &(ip->sin_addr), ipaddr, INET_ADDRSTRLEN);
1068 const char *argv[] = {
1070 "route", "show", "to", "match",
1075 p = process_create(ctx);
1077 p->path = pb_system_apps.ip;
1079 p->keep_stdout = true;
1081 rc = process_run_sync(p);
1084 /* ip has complained for some reason; most likely
1085 * there is no route to the host - bail out */
1086 pb_debug("%s: No route to %s\n",__func__,url->host);
1090 buf = p->stdout_buf;
1091 /* If a route is found, ip-route output will be of the form
1092 * "... dev DEVNAME ... " */
1093 tok = strtok(buf, delim);
1095 if (!strcmp(tok, "dev")) {
1096 tok = strtok(NULL, delim);
1097 dev = talloc_strdup(ctx, tok);
1100 tok = strtok(NULL, delim);
1105 pb_debug("%s: Found interface '%s'\n", __func__,dev);
1110 void device_handler_process_url(struct device_handler *handler,
1113 struct discover_context *ctx;
1114 struct discover_device *dev;
1115 struct boot_status *status;
1116 struct pb_url *pb_url;
1117 struct event *event;
1118 struct param *param;
1120 status = talloc(handler, struct boot_status);
1122 status->type = BOOT_STATUS_ERROR;
1123 status->progress = 0;
1124 status->detail = talloc_asprintf(status,
1125 _("Received config URL %s"), url);
1127 if (!handler->network) {
1128 status->message = talloc_asprintf(handler,
1129 _("No network configured"));
1133 event = talloc(handler, struct event);
1134 event->type = EVENT_TYPE_USER;
1135 event->action = EVENT_ACTION_CONF;
1137 event->params = talloc_array(event, struct param, 1);
1138 param = &event->params[0];
1139 param->name = talloc_strdup(event, "pxeconffile");
1140 param->value = talloc_strdup(event, url);
1141 event->n_params = 1;
1143 pb_url = pb_url_parse(event, event->params->value);
1144 if (!pb_url || !pb_url->host) {
1145 status->message = talloc_asprintf(handler,
1146 _("Invalid config URL!"));
1150 event->device = device_from_addr(event, pb_url);
1151 if (!event->device) {
1152 status->message = talloc_asprintf(status,
1153 _("Unable to route to host %s"),
1158 dev = discover_device_create(handler, event->device);
1159 ctx = device_handler_discover_context_create(handler, dev);
1162 iterate_parsers(ctx);
1164 device_handler_discover_context_commit(handler, ctx);
1168 status->type = BOOT_STATUS_INFO;
1169 status->message = talloc_asprintf(status, _("Config file %s parsed"),
1172 boot_status(handler, status);
1173 talloc_free(status);
1176 #ifndef PETITBOOT_TEST
1178 static void device_handler_update_lang(const char *lang)
1180 const char *cur_lang;
1185 cur_lang = setlocale(LC_ALL, NULL);
1186 if (cur_lang && !strcmp(cur_lang, lang))
1189 setlocale(LC_ALL, lang);
1192 static int device_handler_init_sources(struct device_handler *handler)
1194 /* init our device sources: udev, network and user events */
1195 handler->udev = udev_init(handler, handler->waitset);
1199 handler->network = network_init(handler, handler->waitset,
1201 if (!handler->network)
1204 handler->user_event = user_event_init(handler, handler->waitset);
1205 if (!handler->user_event)
1211 static void device_handler_reinit_sources(struct device_handler *handler)
1213 /* if we haven't initialised sources previously (becuase we started in
1214 * safe mode), then init once here. */
1215 if (!(handler->udev || handler->network || handler->user_event)) {
1216 device_handler_init_sources(handler);
1220 udev_reinit(handler->udev);
1222 network_shutdown(handler->network);
1223 handler->network = network_init(handler, handler->waitset,
1227 static const char *fs_parameters(unsigned int rw_flags, const char *fstype)
1229 if ((rw_flags | MS_RDONLY) != MS_RDONLY)
1232 /* Avoid writing back to the disk on journaled filesystems */
1233 if (!strncmp(fstype, "ext4", strlen("ext4")))
1234 return "norecovery";
1235 if (!strncmp(fstype, "xfs", strlen("xfs")))
1236 return "norecovery";
1241 static inline const char *get_device_path(struct discover_device *dev)
1243 return dev->ramdisk ? dev->ramdisk->snapshot : dev->device_path;
1246 static bool check_existing_mount(struct discover_device *dev)
1248 struct stat devstat, mntstat;
1249 const char *device_path;
1254 device_path = get_device_path(dev);
1256 rc = stat(device_path, &devstat);
1258 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
1262 if (!S_ISBLK(devstat.st_mode)) {
1263 pb_debug("%s: %s isn't a block device?\n", __func__,
1268 fp = fopen("/proc/self/mounts", "r");
1271 mnt = getmntent(fp);
1275 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
1278 rc = stat(mnt->mnt_fsname, &mntstat);
1282 if (!S_ISBLK(mntstat.st_mode))
1285 if (mntstat.st_rdev == devstat.st_rdev) {
1286 dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
1287 dev->mounted_rw = !!hasmntopt(mnt, "rw");
1288 dev->mounted = true;
1289 dev->unmount = false;
1291 pb_debug("%s: %s is already mounted (r%c) at %s\n",
1292 __func__, dev->device_path,
1293 dev->mounted_rw ? 'w' : 'o',
1304 static int mount_device(struct discover_device *dev)
1306 const char *fstype, *device_path;
1309 if (!dev->device_path)
1315 if (check_existing_mount(dev))
1318 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1322 /* ext3 treats the norecovery option as an error, so mount the device
1323 * as an ext4 filesystem instead */
1324 if (!strncmp(fstype, "ext3", strlen("ext3"))) {
1325 pb_debug("Mounting ext3 filesystem as ext4\n");
1326 fstype = talloc_asprintf(dev, "ext4");
1329 dev->mount_path = join_paths(dev, mount_base(),
1332 if (pb_mkdir_recursive(dev->mount_path)) {
1333 pb_log("couldn't create mount directory %s: %s\n",
1334 dev->mount_path, strerror(errno));
1338 device_path = get_device_path(dev);
1340 pb_log("mounting device %s read-only\n", dev->device_path);
1342 rc = mount(device_path, dev->mount_path, fstype,
1343 MS_RDONLY | MS_SILENT,
1344 fs_parameters(MS_RDONLY, fstype));
1346 dev->mounted = true;
1347 dev->mounted_rw = false;
1348 dev->unmount = true;
1352 pb_log("couldn't mount device %s: mount failed: %s\n",
1353 device_path, strerror(errno));
1355 /* If mount fails clean up any snapshot */
1356 devmapper_destroy_snapshot(dev);
1358 pb_rmdir_recursive(mount_base(), dev->mount_path);
1360 talloc_free(dev->mount_path);
1361 dev->mount_path = NULL;
1365 static int umount_device(struct discover_device *dev)
1367 const char *device_path;
1370 if (!dev->mounted || !dev->unmount)
1373 device_path = get_device_path(dev);
1375 pb_log("unmounting device %s\n", device_path);
1376 rc = umount(dev->mount_path);
1380 dev->mounted = false;
1381 devmapper_destroy_snapshot(dev);
1383 pb_rmdir_recursive(mount_base(), dev->mount_path);
1385 talloc_free(dev->mount_path);
1386 dev->mount_path = NULL;
1391 int device_request_write(struct discover_device *dev, bool *release)
1393 const char *fstype, *device_path;
1394 const struct config *config;
1399 config = config_get();
1400 if (!config->allow_writes)
1406 if (dev->mounted_rw)
1409 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1411 device_path = get_device_path(dev);
1413 pb_log("remounting device %s read-write\n", device_path);
1415 rc = umount(dev->mount_path);
1417 pb_log("Failed to unmount %s: %s\n",
1418 dev->mount_path, strerror(errno));
1422 rc = mount(device_path, dev->mount_path, fstype,
1424 fs_parameters(MS_REMOUNT, fstype));
1428 dev->mounted_rw = true;
1433 pb_log("Unable to remount device %s read-write: %s\n",
1434 device_path, strerror(errno));
1435 if (mount(device_path, dev->mount_path, fstype,
1436 MS_RDONLY | MS_SILENT,
1437 fs_parameters(MS_RDONLY, fstype)))
1438 pb_log("Unable to recover mount for %s: %s\n",
1439 device_path, strerror(errno));
1443 void device_release_write(struct discover_device *dev, bool release)
1445 const char *fstype, *device_path;
1451 device_path = get_device_path(dev);
1453 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1455 pb_log("remounting device %s read-only\n", device_path);
1457 if (umount(dev->mount_path)) {
1458 pb_log("Failed to unmount %s\n", dev->mount_path);
1461 dev->mounted_rw = dev->mounted = false;
1464 devmapper_merge_snapshot(dev);
1465 /* device_path becomes stale after merge */
1466 device_path = get_device_path(dev);
1469 mount(device_path, dev->mount_path, fstype,
1470 MS_RDONLY | MS_SILENT,
1471 fs_parameters(MS_RDONLY, fstype));
1473 pb_log("Failed to remount %s read-only: %s\n",
1474 device_path, strerror(errno));
1476 dev->mounted = true;
1481 static void device_handler_update_lang(const char *lang __attribute__((unused)))
1485 static int device_handler_init_sources(
1486 struct device_handler *handler __attribute__((unused)))
1491 static void device_handler_reinit_sources(
1492 struct device_handler *handler __attribute__((unused)))
1496 static int umount_device(struct discover_device *dev __attribute__((unused)))
1501 static int __attribute__((unused)) mount_device(
1502 struct discover_device *dev __attribute__((unused)))
1507 int device_request_write(struct discover_device *dev __attribute__((unused)),
1514 void device_release_write(struct discover_device *dev __attribute__((unused)),
1515 bool release __attribute__((unused)))