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 void device_handler_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);
450 handler->pending_boot = boot(handler, handler->default_boot_option,
451 NULL, handler->dry_run, device_handler_boot_status,
453 handler->pending_boot_is_default = true;
458 enum ipmi_bootdev ipmi_type;
459 enum device_type device_type;
460 } device_type_map[] = {
461 { IPMI_BOOTDEV_NETWORK, DEVICE_TYPE_NETWORK },
462 { IPMI_BOOTDEV_DISK, DEVICE_TYPE_DISK },
463 { IPMI_BOOTDEV_DISK, DEVICE_TYPE_USB },
464 { IPMI_BOOTDEV_CDROM, DEVICE_TYPE_OPTICAL },
467 static bool ipmi_device_type_matches(enum ipmi_bootdev ipmi_type,
468 enum device_type device_type)
472 for (i = 0; i < ARRAY_SIZE(device_type_map); i++) {
473 if (device_type_map[i].device_type == device_type)
474 return device_type_map[i].ipmi_type == ipmi_type;
480 static int autoboot_option_priority(const struct config *config,
481 struct discover_boot_option *opt)
483 enum device_type type = opt->device->device->type;
484 const char *uuid = opt->device->uuid;
485 struct autoboot_option *auto_opt;
488 for (i = 0; i < config->n_autoboot_opts; i++) {
489 auto_opt = &config->autoboot_opts[i];
490 if (auto_opt->boot_type == BOOT_DEVICE_UUID)
491 if (!strcmp(auto_opt->uuid, uuid))
492 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
494 if (auto_opt->boot_type == BOOT_DEVICE_TYPE)
495 if (auto_opt->type == type ||
496 auto_opt->type == DEVICE_TYPE_ANY)
497 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
504 * We have different priorities to resolve conflicts between boot options that
505 * report to be the default for their device. This function assigns a priority
508 static enum default_priority default_option_priority(
509 struct discover_boot_option *opt)
511 const struct config *config;
513 config = config_get();
515 /* We give highest priority to IPMI-configured boot options. If
516 * we have an IPMI bootdev configuration set, then we don't allow
517 * any other defaults */
518 if (config->ipmi_bootdev) {
519 bool ipmi_match = ipmi_device_type_matches(config->ipmi_bootdev,
520 opt->device->device->type);
522 return DEFAULT_PRIORITY_REMOTE;
524 pb_debug("handler: disabled default priority due to "
525 "non-matching IPMI type %x\n",
526 config->ipmi_bootdev);
527 return DEFAULT_PRIORITY_DISABLED;
530 /* Next, try to match the option against the user-defined autoboot
531 * options, either by device UUID or type. */
532 if (config->n_autoboot_opts) {
533 int boot_match = autoboot_option_priority(config, opt);
538 /* If the option didn't match any entry in the array, it is disabled */
539 pb_debug("handler: disabled default priority due to "
540 "non-matching UUID or type\n");
541 return DEFAULT_PRIORITY_DISABLED;
544 static void set_default(struct device_handler *handler,
545 struct discover_boot_option *opt)
547 enum default_priority cur_prio, new_prio;
549 if (!handler->autoboot_enabled)
552 pb_debug("handler: new default option: %s\n", opt->option->id);
554 new_prio = default_option_priority(opt);
556 /* Anything outside our range prevents a default boot */
557 if (new_prio >= DEFAULT_PRIORITY_DISABLED)
560 pb_debug("handler: calculated priority %d\n", new_prio);
562 /* Resolve any conflicts: if we have a new default option, it only
563 * replaces the current if it has a higher priority. */
564 if (handler->default_boot_option) {
566 cur_prio = handler->default_boot_option_priority;
568 if (new_prio < cur_prio) {
569 pb_log("handler: new prio %d beats "
570 "old prio %d for %s\n",
572 handler->default_boot_option
574 handler->default_boot_option = opt;
575 handler->default_boot_option_priority = new_prio;
576 /* extend the timeout a little, so the user sees some
577 * indication of the change */
578 handler->sec_to_boot += 2;
584 handler->sec_to_boot = config_get()->autoboot_timeout_sec;
585 handler->default_boot_option = opt;
586 handler->default_boot_option_priority = new_prio;
588 pb_log("handler: boot option %s set as default, timeout %u sec.\n",
589 opt->option->id, handler->sec_to_boot);
591 default_timeout(handler);
594 static bool resource_is_resolved(struct resource *res)
596 return !res || res->resolved;
599 /* We only use this in an assert, which will disappear if we're compiling
600 * with NDEBUG, so we need the 'used' attribute for these builds */
601 static bool __attribute__((used)) boot_option_is_resolved(
602 struct discover_boot_option *opt)
604 return resource_is_resolved(opt->boot_image) &&
605 resource_is_resolved(opt->initrd) &&
606 resource_is_resolved(opt->dtb) &&
607 resource_is_resolved(opt->icon);
610 static bool resource_resolve(struct resource *res, const char *name,
611 struct discover_boot_option *opt,
612 struct device_handler *handler)
614 struct parser *parser = opt->source;
616 if (resource_is_resolved(res))
619 pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
620 opt->option->id, name, parser->name);
621 parser->resolve_resource(handler, res);
623 return res->resolved;
626 static bool boot_option_resolve(struct discover_boot_option *opt,
627 struct device_handler *handler)
629 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
630 resource_resolve(opt->initrd, "initrd", opt, handler) &&
631 resource_resolve(opt->dtb, "dtb", opt, handler) &&
632 resource_resolve(opt->icon, "icon", opt, handler);
635 static void boot_option_finalise(struct device_handler *handler,
636 struct discover_boot_option *opt)
638 assert(boot_option_is_resolved(opt));
640 /* check that the parsers haven't set any of the final data */
641 assert(!opt->option->boot_image_file);
642 assert(!opt->option->initrd_file);
643 assert(!opt->option->dtb_file);
644 assert(!opt->option->icon_file);
645 assert(!opt->option->device_id);
648 opt->option->boot_image_file = opt->boot_image->url->full;
650 opt->option->initrd_file = opt->initrd->url->full;
652 opt->option->dtb_file = opt->dtb->url->full;
654 opt->option->icon_file = opt->icon->url->full;
656 opt->option->device_id = opt->device->device->id;
658 if (opt->option->is_default)
659 set_default(handler, opt);
662 static void notify_boot_option(struct device_handler *handler,
663 struct discover_boot_option *opt)
665 struct discover_device *dev = opt->device;
668 discover_server_notify_device_add(handler->server,
669 opt->device->device);
670 dev->notified = true;
671 discover_server_notify_boot_option_add(handler->server, opt->option);
674 static void process_boot_option_queue(struct device_handler *handler)
676 struct discover_boot_option *opt, *tmp;
678 list_for_each_entry_safe(&handler->unresolved_boot_options,
681 pb_debug("queue: attempting resolution for %s\n",
684 if (!boot_option_resolve(opt, handler))
687 pb_debug("\tresolved!\n");
689 list_remove(&opt->list);
690 list_add_tail(&opt->device->boot_options, &opt->list);
691 talloc_steal(opt->device, opt);
692 boot_option_finalise(handler, opt);
693 notify_boot_option(handler, opt);
697 struct discover_context *device_handler_discover_context_create(
698 struct device_handler *handler,
699 struct discover_device *device)
701 struct discover_context *ctx;
703 ctx = talloc_zero(handler, struct discover_context);
704 ctx->device = device;
705 ctx->network = handler->network;
706 list_init(&ctx->boot_options);
711 void device_handler_add_device(struct device_handler *handler,
712 struct discover_device *device)
714 handler->n_devices++;
715 handler->devices = talloc_realloc(handler, handler->devices,
716 struct discover_device *, handler->n_devices);
717 handler->devices[handler->n_devices - 1] = device;
719 if (device->device->type == DEVICE_TYPE_NETWORK)
720 network_register_device(handler->network, device);
723 void device_handler_add_ramdisk(struct device_handler *handler,
726 struct ramdisk_device *dev;
732 for (i = 0; i < handler->n_ramdisks; i++)
733 if (!strcmp(handler->ramdisks[i]->path, path))
736 dev = talloc_zero(handler, struct ramdisk_device);
738 pb_log("Failed to allocate memory to track %s\n", path);
742 dev->path = talloc_strdup(handler, path);
744 handler->ramdisks = talloc_realloc(handler, handler->ramdisks,
745 struct ramdisk_device *,
746 handler->n_ramdisks + 1);
747 if (!handler->ramdisks) {
748 pb_log("Failed to reallocate memory"
749 "- ramdisk tracking inconsistent!\n");
753 handler->ramdisks[i] = dev;
754 i = handler->n_ramdisks++;
757 struct ramdisk_device *device_handler_get_ramdisk(
758 struct device_handler *handler)
764 /* Check if free ramdisk exists */
765 for (i = 0; i < handler->n_ramdisks; i++)
766 if (!handler->ramdisks[i]->snapshot &&
767 !handler->ramdisks[i]->origin &&
768 !handler->ramdisks[i]->base)
769 return handler->ramdisks[i];
771 /* Otherwise create a new one */
772 name = talloc_asprintf(handler, "/dev/ram%d",
773 handler->n_ramdisks);
775 pb_debug("Failed to allocate memory to name /dev/ram%d",
776 handler->n_ramdisks);
780 id = makedev(1, handler->n_ramdisks);
781 if (mknod(name, S_IFBLK, id)) {
782 if (errno == EEXIST) {
783 /* We haven't yet received updates for existing
784 * ramdisks - add and use this one */
785 pb_debug("Using untracked ramdisk %s\n", name);
787 pb_log("Failed to create new ramdisk %s: %s\n",
788 name, strerror(errno));
792 device_handler_add_ramdisk(handler, name);
795 return handler->ramdisks[i];
798 void device_handler_release_ramdisk(struct discover_device *device)
800 struct ramdisk_device *ramdisk = device->ramdisk;
802 talloc_free(ramdisk->snapshot);
803 talloc_free(ramdisk->origin);
804 talloc_free(ramdisk->base);
806 ramdisk->snapshot = ramdisk->origin = ramdisk->base = NULL;
807 ramdisk->sectors = 0;
809 device->ramdisk = NULL;
812 /* Start discovery on a hotplugged device. The device will be in our devices
813 * array, but has only just been initialised by the hotplug source.
815 int device_handler_discover(struct device_handler *handler,
816 struct discover_device *dev)
818 struct discover_context *ctx;
819 struct boot_status *status;
822 status = talloc_zero(handler, struct boot_status);
823 status->type = BOOT_STATUS_INFO;
825 * TRANSLATORS: this string will be passed the type and identifier
826 * of the device. For example, the first parameter could be "Disk",
827 * (which will be translated accordingly) and the second a Linux device
828 * identifier like 'sda1' (which will not be translated)
830 status->message = talloc_asprintf(status, _("Processing %s device %s"),
831 device_type_display_name(dev->device->type),
833 device_handler_boot_status(handler, status);
835 process_boot_option_queue(handler);
837 /* create our context */
838 ctx = device_handler_discover_context_create(handler, dev);
840 rc = mount_device(dev);
844 /* add this device to our system info */
845 system_info_register_blockdev(dev->device->id, dev->uuid,
848 /* run the parsers. This will populate the ctx's boot_option list. */
849 iterate_parsers(ctx);
851 /* add discovered stuff to the handler */
852 device_handler_discover_context_commit(handler, ctx);
856 * TRANSLATORS: the format specifier in this string is a Linux
857 * device identifier, like 'sda1'
859 status->message = talloc_asprintf(status,_("Processing %s complete"),
861 device_handler_boot_status(handler, status);
864 talloc_unlink(handler, ctx);
869 /* Incoming dhcp event */
870 int device_handler_dhcp(struct device_handler *handler,
871 struct discover_device *dev, struct event *event)
873 struct discover_context *ctx;
874 struct boot_status *status;
876 status = talloc_zero(handler, struct boot_status);
877 status->type = BOOT_STATUS_INFO;
879 * TRANSLATORS: this format specifier will be the name of a network
880 * device, like 'eth0'.
882 status->message = talloc_asprintf(status, _("Processing dhcp event on %s"),
884 device_handler_boot_status(handler, status);
886 /* create our context */
887 ctx = device_handler_discover_context_create(handler, dev);
890 iterate_parsers(ctx);
892 device_handler_discover_context_commit(handler, ctx);
895 * TRANSLATORS: this format specifier will be the name of a network
896 * device, like 'eth0'.
898 status->message = talloc_asprintf(status,_("Processing %s complete"),
900 device_handler_boot_status(handler, status);
903 talloc_unlink(handler, ctx);
908 /* incoming conf event */
909 int device_handler_conf(struct device_handler *handler,
910 struct discover_device *dev, struct pb_url *url)
912 struct discover_context *ctx;
913 struct boot_status *status;
915 status = talloc_zero(handler, struct boot_status);
916 status->type = BOOT_STATUS_INFO;
917 status->message = talloc_asprintf(status, _("Processing user config"));
918 device_handler_boot_status(handler, status);
920 /* create our context */
921 ctx = device_handler_discover_context_create(handler, dev);
924 iterate_parsers(ctx);
926 device_handler_discover_context_commit(handler, ctx);
928 status->message = talloc_asprintf(status,
929 _("Processing user config complete"));
930 device_handler_boot_status(handler, status);
933 talloc_unlink(handler, ctx);
938 static struct discover_boot_option *find_boot_option_by_id(
939 struct device_handler *handler, const char *id)
943 for (i = 0; i < handler->n_devices; i++) {
944 struct discover_device *dev = handler->devices[i];
945 struct discover_boot_option *opt;
947 list_for_each_entry(&dev->boot_options, opt, list)
948 if (!strcmp(opt->option->id, id))
955 void device_handler_boot(struct device_handler *handler,
956 struct boot_command *cmd)
958 struct discover_boot_option *opt = NULL;
960 if (cmd->option_id && strlen(cmd->option_id))
961 opt = find_boot_option_by_id(handler, cmd->option_id);
963 if (handler->pending_boot)
964 boot_cancel(handler->pending_boot);
968 handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
969 device_handler_boot_status, handler);
970 handler->pending_boot_is_default = false;
973 void device_handler_cancel_default(struct device_handler *handler)
975 struct boot_status status;
977 if (handler->timeout_waiter)
978 waiter_remove(handler->timeout_waiter);
980 handler->timeout_waiter = NULL;
981 handler->autoboot_enabled = false;
983 /* we only send status if we had a default boot option queued */
984 if (!handler->default_boot_option)
987 pb_log("Cancelling default boot option\n");
989 if (handler->pending_boot && handler->pending_boot_is_default) {
990 boot_cancel(handler->pending_boot);
991 handler->pending_boot = NULL;
992 handler->pending_boot_is_default = false;
995 handler->default_boot_option = NULL;
997 status.type = BOOT_STATUS_INFO;
998 status.progress = -1;
999 status.detail = NULL;
1000 status.message = _("Default boot cancelled");
1002 discover_server_notify_boot_status(handler->server, &status);
1005 void device_handler_update_config(struct device_handler *handler,
1006 struct config *config)
1010 rc = config_set(config);
1014 discover_server_notify_config(handler->server, config);
1015 device_handler_update_lang(config->lang);
1016 device_handler_reinit(handler);
1019 static char *device_from_addr(void *ctx, struct pb_url *url)
1021 char *ipaddr, *buf, *tok, *dev = NULL;
1022 const char *delim = " ";
1023 struct sockaddr_in *ip;
1024 struct sockaddr_in si;
1025 struct addrinfo *res;
1029 /* Note: IPv4 only */
1030 rc = inet_pton(AF_INET, url->host, &(si.sin_addr));
1034 /* need to turn hostname into a valid IP */
1035 rc = getaddrinfo(url->host, NULL, NULL, &res);
1037 pb_debug("%s: Invalid URL\n",__func__);
1040 ipaddr = talloc_array(ctx,char,INET_ADDRSTRLEN);
1041 ip = (struct sockaddr_in *) res->ai_addr;
1042 inet_ntop(AF_INET, &(ip->sin_addr), ipaddr, INET_ADDRSTRLEN);
1046 const char *argv[] = {
1048 "route", "show", "to", "match",
1053 p = process_create(ctx);
1055 p->path = pb_system_apps.ip;
1057 p->keep_stdout = true;
1059 rc = process_run_sync(p);
1061 if (rc || p->exit_status) {
1062 /* ip has complained for some reason; most likely
1063 * there is no route to the host - bail out */
1064 pb_debug("%s: `ip` returns non-zero exit status\n", __func__);
1065 pb_debug("ip buf: %s\n", p->stdout_buf);
1070 buf = p->stdout_buf;
1071 /* If a route is found, ip-route output will be of the form
1072 * "... dev DEVNAME ... " */
1073 tok = strtok(buf, delim);
1075 if (!strcmp(tok, "dev")) {
1076 tok = strtok(NULL, delim);
1077 dev = talloc_strdup(ctx, tok);
1080 tok = strtok(NULL, delim);
1085 pb_debug("%s: Found interface '%s'\n", __func__,dev);
1089 void device_handler_process_url(struct device_handler *handler,
1090 const char *url, const char *mac, const char *ip)
1092 struct discover_context *ctx;
1093 struct discover_device *dev;
1094 struct boot_status *status;
1095 struct pb_url *pb_url;
1096 struct event *event;
1097 struct param *param;
1099 status = talloc(handler, struct boot_status);
1101 status->type = BOOT_STATUS_ERROR;
1102 status->progress = 0;
1103 status->detail = talloc_asprintf(status,
1104 _("Received config URL %s"), url);
1106 if (!handler->network) {
1107 status->message = talloc_asprintf(handler,
1108 _("No network configured"));
1112 event = talloc(handler, struct event);
1113 event->type = EVENT_TYPE_USER;
1114 event->action = EVENT_ACTION_CONF;
1116 if (url[strlen(url) - 1] == '/') {
1117 event->params = talloc_array(event, struct param, 3);
1118 param = &event->params[0];
1119 param->name = talloc_strdup(event, "pxepathprefix");
1120 param->value = talloc_strdup(event, url);
1121 param = &event->params[1];
1122 param->name = talloc_strdup(event, "mac");
1123 param->value = talloc_strdup(event, mac);
1124 param = &event->params[2];
1125 param->name = talloc_strdup(event, "ip");
1126 param->value = talloc_strdup(event, ip);
1127 event->n_params = 3;
1129 event->params = talloc_array(event, struct param, 1);
1130 param = &event->params[0];
1131 param->name = talloc_strdup(event, "pxeconffile");
1132 param->value = talloc_strdup(event, url);
1133 event->n_params = 1;
1136 pb_url = pb_url_parse(event, event->params->value);
1137 if (!pb_url || (pb_url->scheme != pb_url_file && !pb_url->host)) {
1138 status->message = talloc_asprintf(handler,
1139 _("Invalid config URL!"));
1143 if (pb_url->scheme == pb_url_file)
1144 event->device = talloc_asprintf(event, "local");
1146 event->device = device_from_addr(event, pb_url);
1148 if (!event->device) {
1149 status->message = talloc_asprintf(status,
1150 _("Unable to route to host %s"),
1155 dev = discover_device_create(handler, event->device);
1156 if (pb_url->scheme == pb_url_file)
1157 dev->device->type = DEVICE_TYPE_ANY;
1158 ctx = device_handler_discover_context_create(handler, dev);
1161 iterate_parsers(ctx);
1163 device_handler_discover_context_commit(handler, ctx);
1165 talloc_unlink(handler, ctx);
1167 status->type = BOOT_STATUS_INFO;
1168 status->message = talloc_asprintf(status, _("Config file %s parsed"),
1171 device_handler_boot_status(handler, status);
1172 talloc_free(status);
1175 #ifndef PETITBOOT_TEST
1178 * context_commit - Commit a temporary discovery context to the handler,
1179 * and notify the clients about any new options / devices
1181 void device_handler_discover_context_commit(struct device_handler *handler,
1182 struct discover_context *ctx)
1184 struct discover_device *dev = ctx->device;
1185 struct discover_boot_option *opt, *tmp;
1187 if (!device_lookup_by_id(handler, dev->device->id))
1188 device_handler_add_device(handler, dev);
1190 /* move boot options from the context to the device */
1191 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
1192 list_remove(&opt->list);
1194 /* All boot options need at least a kernel image */
1195 if (!opt->boot_image || !opt->boot_image->url) {
1196 pb_log("boot option %s is missing boot image, ignoring\n",
1202 if (boot_option_resolve(opt, handler)) {
1203 pb_log("boot option %s is resolved, "
1204 "sending to clients\n",
1206 list_add_tail(&dev->boot_options, &opt->list);
1207 talloc_steal(dev, opt);
1208 boot_option_finalise(handler, opt);
1209 notify_boot_option(handler, opt);
1211 if (!opt->source->resolve_resource) {
1212 pb_log("parser %s gave us an unresolved "
1213 "resource (%s), but no way to "
1215 opt->source->name, opt->option->id);
1218 pb_log("boot option %s is unresolved, "
1219 "adding to queue\n",
1221 list_add(&handler->unresolved_boot_options,
1223 talloc_steal(handler, opt);
1229 static void device_handler_update_lang(const char *lang)
1231 const char *cur_lang;
1236 cur_lang = setlocale(LC_ALL, NULL);
1237 if (cur_lang && !strcmp(cur_lang, lang))
1240 setlocale(LC_ALL, lang);
1243 static int device_handler_init_sources(struct device_handler *handler)
1245 /* init our device sources: udev, network and user events */
1246 handler->udev = udev_init(handler, handler->waitset);
1250 handler->network = network_init(handler, handler->waitset,
1252 if (!handler->network)
1255 handler->user_event = user_event_init(handler, handler->waitset);
1256 if (!handler->user_event)
1262 static void device_handler_reinit_sources(struct device_handler *handler)
1264 /* if we haven't initialised sources previously (becuase we started in
1265 * safe mode), then init once here. */
1266 if (!(handler->udev || handler->network || handler->user_event)) {
1267 device_handler_init_sources(handler);
1271 udev_reinit(handler->udev);
1273 network_shutdown(handler->network);
1274 handler->network = network_init(handler, handler->waitset,
1278 static inline const char *get_device_path(struct discover_device *dev)
1280 return dev->ramdisk ? dev->ramdisk->snapshot : dev->device_path;
1283 static char *check_subvols(struct discover_device *dev)
1285 const char *fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1290 if (strncmp(fstype, "btrfs", strlen("btrfs")))
1291 return dev->mount_path;
1293 /* On btrfs a device's root may be under a subvolume path */
1294 path = join_paths(dev, dev->mount_path, "@");
1295 rc = stat(path, &sb);
1296 if (!rc && S_ISDIR(sb.st_mode)) {
1297 pb_debug("Using '%s' for btrfs root path\n", path);
1302 return dev->mount_path;
1305 static bool check_existing_mount(struct discover_device *dev)
1307 struct stat devstat, mntstat;
1308 const char *device_path;
1313 device_path = get_device_path(dev);
1315 rc = stat(device_path, &devstat);
1317 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
1321 if (!S_ISBLK(devstat.st_mode)) {
1322 pb_debug("%s: %s isn't a block device?\n", __func__,
1327 fp = fopen("/proc/self/mounts", "r");
1330 mnt = getmntent(fp);
1334 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
1337 rc = stat(mnt->mnt_fsname, &mntstat);
1341 if (!S_ISBLK(mntstat.st_mode))
1344 if (mntstat.st_rdev == devstat.st_rdev) {
1345 dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
1346 dev->root_path = check_subvols(dev);
1347 dev->mounted_rw = !!hasmntopt(mnt, "rw");
1348 dev->mounted = true;
1349 dev->unmount = false;
1351 pb_debug("%s: %s is already mounted (r%c) at %s\n",
1352 __func__, dev->device_path,
1353 dev->mounted_rw ? 'w' : 'o',
1365 * Attempt to mount a filesystem safely, while handling certain filesytem-
1368 static int try_mount(const char *device_path, const char *mount_path,
1369 const char *fstype, unsigned long flags,
1372 const char *fs, *safe_opts;
1375 /* Mount ext3 as ext4 instead so 'norecovery' can be used */
1376 if (strncmp(fstype, "ext3", strlen("ext3")) == 0) {
1377 pb_debug("Mounting ext3 filesystem as ext4\n");
1382 if (strncmp(fs, "xfs", strlen("xfs")) == 0 ||
1383 strncmp(fs, "ext4", strlen("ext4")) == 0)
1384 safe_opts = "norecovery";
1389 /* If no snapshot is available don't attempt recovery */
1391 return mount(device_path, mount_path, fs, flags, safe_opts);
1393 rc = mount(device_path, mount_path, fs, flags, NULL);
1398 /* Mounting failed; some filesystems will fail to mount if a recovery
1399 * journal exists (eg. cross-endian XFS), so try again with norecovery
1400 * where that option is available.
1401 * If mounting read-write just return the error as norecovery is not a
1403 if ((flags & MS_RDONLY) != MS_RDONLY || !safe_opts)
1407 return mount(device_path, mount_path, fs, flags, safe_opts);
1410 static int mount_device(struct discover_device *dev)
1412 const char *fstype, *device_path;
1415 if (!dev->device_path)
1421 if (check_existing_mount(dev))
1424 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1428 dev->mount_path = join_paths(dev, mount_base(),
1431 if (pb_mkdir_recursive(dev->mount_path)) {
1432 pb_log("couldn't create mount directory %s: %s\n",
1433 dev->mount_path, strerror(errno));
1437 device_path = get_device_path(dev);
1439 pb_log("mounting device %s read-only\n", dev->device_path);
1440 rc = try_mount(device_path, dev->mount_path, fstype,
1441 MS_RDONLY | MS_SILENT, dev->ramdisk);
1444 dev->mounted = true;
1445 dev->mounted_rw = false;
1446 dev->unmount = true;
1447 dev->root_path = check_subvols(dev);
1451 pb_log("couldn't mount device %s: mount failed: %s\n",
1452 device_path, strerror(errno));
1454 /* If mount fails clean up any snapshot */
1455 devmapper_destroy_snapshot(dev);
1457 pb_rmdir_recursive(mount_base(), dev->mount_path);
1459 talloc_free(dev->mount_path);
1460 dev->mount_path = NULL;
1464 static int umount_device(struct discover_device *dev)
1466 const char *device_path;
1469 if (!dev->mounted || !dev->unmount)
1472 device_path = get_device_path(dev);
1474 pb_log("unmounting device %s\n", device_path);
1475 rc = umount(dev->mount_path);
1479 dev->mounted = false;
1480 devmapper_destroy_snapshot(dev);
1482 pb_rmdir_recursive(mount_base(), dev->mount_path);
1484 talloc_free(dev->mount_path);
1485 dev->mount_path = NULL;
1486 dev->root_path = NULL;
1491 int device_request_write(struct discover_device *dev, bool *release)
1493 const char *fstype, *device_path;
1494 const struct config *config;
1499 config = config_get();
1500 if (!config->allow_writes)
1506 if (dev->mounted_rw)
1509 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1511 device_path = get_device_path(dev);
1513 pb_log("remounting device %s read-write\n", device_path);
1515 rc = umount(dev->mount_path);
1517 pb_log("Failed to unmount %s: %s\n",
1518 dev->mount_path, strerror(errno));
1522 rc = try_mount(device_path, dev->mount_path, fstype,
1523 MS_SILENT, dev->ramdisk);
1527 dev->mounted_rw = true;
1532 pb_log("Unable to remount device %s read-write: %s\n",
1533 device_path, strerror(errno));
1534 rc = try_mount(device_path, dev->mount_path, fstype,
1535 MS_RDONLY | MS_SILENT, dev->ramdisk);
1537 pb_log("Unable to recover mount for %s: %s\n",
1538 device_path, strerror(errno));
1542 void device_release_write(struct discover_device *dev, bool release)
1544 const char *fstype, *device_path;
1549 device_path = get_device_path(dev);
1551 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1553 pb_log("remounting device %s read-only\n", device_path);
1555 if (umount(dev->mount_path)) {
1556 pb_log("Failed to unmount %s\n", dev->mount_path);
1559 dev->mounted_rw = dev->mounted = false;
1562 devmapper_merge_snapshot(dev);
1563 /* device_path becomes stale after merge */
1564 device_path = get_device_path(dev);
1567 if (try_mount(device_path, dev->mount_path, fstype,
1568 MS_RDONLY | MS_SILENT, dev->ramdisk))
1569 pb_log("Failed to remount %s read-only: %s\n",
1570 device_path, strerror(errno));
1572 dev->mounted = true;
1577 void device_handler_discover_context_commit(
1578 struct device_handler *handler __attribute__((unused)),
1579 struct discover_context *ctx __attribute__((unused)))
1581 pb_log("%s stubbed out for test cases\n", __func__);
1584 static void device_handler_update_lang(const char *lang __attribute__((unused)))
1588 static int device_handler_init_sources(
1589 struct device_handler *handler __attribute__((unused)))
1594 static void device_handler_reinit_sources(
1595 struct device_handler *handler __attribute__((unused)))
1599 static int umount_device(struct discover_device *dev __attribute__((unused)))
1604 static int __attribute__((unused)) mount_device(
1605 struct discover_device *dev __attribute__((unused)))
1610 int device_request_write(struct discover_device *dev __attribute__((unused)),
1617 void device_release_write(struct discover_device *dev __attribute__((unused)),
1618 bool release __attribute__((unused)))