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,
224 const char *uuid, const char *id)
226 struct discover_device *dev;
229 dev = device_lookup_by_uuid(handler, uuid);
231 dev = device_lookup_by_id(handler, id);
236 dev = talloc_zero(handler, struct discover_device);
237 dev->device = talloc_zero(dev, struct device);
238 dev->device->id = talloc_strdup(dev->device, id);
239 dev->uuid = talloc_strdup(dev, uuid);
240 list_init(&dev->params);
241 list_init(&dev->boot_options);
243 talloc_set_destructor(dev, destroy_device);
248 struct discover_device_param {
251 struct list_item list;
254 void discover_device_set_param(struct discover_device *device,
255 const char *name, const char *value)
257 struct discover_device_param *param;
260 list_for_each_entry(&device->params, param, list) {
261 if (!strcmp(param->name, name)) {
270 param = talloc(device, struct discover_device_param);
271 param->name = talloc_strdup(param, name);
272 list_add(&device->params, ¶m->list);
275 list_remove(¶m->list);
279 talloc_free(param->value);
282 param->value = talloc_strdup(param, value);
285 const char *discover_device_get_param(struct discover_device *device,
288 struct discover_device_param *param;
290 list_for_each_entry(&device->params, param, list) {
291 if (!strcmp(param->name, name))
297 struct device_handler *device_handler_init(struct discover_server *server,
298 struct waitset *waitset, int dry_run)
300 struct device_handler *handler;
303 handler = talloc_zero(NULL, struct device_handler);
304 handler->server = server;
305 handler->waitset = waitset;
306 handler->dry_run = dry_run;
307 handler->autoboot_enabled = config_get()->autoboot_enabled;
309 list_init(&handler->unresolved_boot_options);
311 /* set up our mount point base */
312 pb_mkdir_recursive(mount_base());
316 if (config_get()->safe_mode)
319 rc = device_handler_init_sources(handler);
321 talloc_free(handler);
328 void device_handler_reinit(struct device_handler *handler)
330 struct discover_boot_option *opt, *tmp;
331 struct ramdisk_device *ramdisk;
334 device_handler_cancel_default(handler);
336 /* free unresolved boot options */
337 list_for_each_entry_safe(&handler->unresolved_boot_options,
340 list_init(&handler->unresolved_boot_options);
342 /* drop all devices */
343 for (i = 0; i < handler->n_devices; i++) {
344 discover_server_notify_device_remove(handler->server,
345 handler->devices[i]->device);
346 ramdisk = handler->devices[i]->ramdisk;
347 talloc_free(handler->devices[i]);
348 talloc_free(ramdisk);
351 talloc_free(handler->devices);
352 handler->devices = NULL;
353 handler->n_devices = 0;
354 talloc_free(handler->ramdisks);
355 handler->ramdisks = NULL;
356 handler->n_ramdisks = 0;
358 device_handler_reinit_sources(handler);
361 void device_handler_remove(struct device_handler *handler,
362 struct discover_device *device)
364 struct discover_boot_option *opt, *tmp;
367 list_for_each_entry_safe(&device->boot_options, opt, tmp, list) {
368 if (opt == handler->default_boot_option) {
369 pb_log("Default option %s cancelled since device removed",
371 device_handler_cancel_default(handler);
376 for (i = 0; i < handler->n_devices; i++)
377 if (handler->devices[i] == device)
380 if (i == handler->n_devices) {
385 /* Free any unresolved options, as they're currently allocated
386 * against the handler */
387 list_for_each_entry_safe(&handler->unresolved_boot_options,
389 if (opt->device != device)
391 list_remove(&opt->list);
395 /* if this is a network device, we have to unregister it from the
397 if (device->device->type == DEVICE_TYPE_NETWORK)
398 network_unregister_device(handler->network, device);
400 handler->n_devices--;
401 memmove(&handler->devices[i], &handler->devices[i + 1],
402 (handler->n_devices - i) * sizeof(handler->devices[0]));
403 handler->devices = talloc_realloc(handler, handler->devices,
404 struct discover_device *, handler->n_devices);
406 if (device->notified)
407 discover_server_notify_device_remove(handler->server,
413 void device_handler_boot_status(void *arg, struct boot_status *status)
415 struct device_handler *handler = arg;
417 discover_server_notify_boot_status(handler->server, status);
420 static void countdown_status(struct device_handler *handler,
421 struct discover_boot_option *opt, unsigned int sec)
423 struct boot_status status;
425 status.type = BOOT_STATUS_INFO;
426 status.progress = -1;
427 status.detail = NULL;
428 status.message = talloc_asprintf(handler,
429 _("Booting in %d sec: %s"), sec, opt->option->name);
431 discover_server_notify_boot_status(handler->server, &status);
433 talloc_free(status.message);
436 static int default_timeout(void *arg)
438 struct device_handler *handler = arg;
439 struct discover_boot_option *opt;
441 if (!handler->default_boot_option)
444 if (handler->pending_boot)
447 opt = handler->default_boot_option;
449 if (handler->sec_to_boot) {
450 countdown_status(handler, opt, handler->sec_to_boot);
451 handler->sec_to_boot--;
452 handler->timeout_waiter = waiter_register_timeout(
453 handler->waitset, 1000,
454 default_timeout, handler);
458 handler->timeout_waiter = NULL;
460 pb_log("Timeout expired, booting default option %s\n", opt->option->id);
464 handler->pending_boot = boot(handler, handler->default_boot_option,
465 NULL, handler->dry_run, device_handler_boot_status,
467 handler->pending_boot_is_default = true;
472 enum ipmi_bootdev ipmi_type;
473 enum device_type device_type;
474 } device_type_map[] = {
475 { IPMI_BOOTDEV_NETWORK, DEVICE_TYPE_NETWORK },
476 { IPMI_BOOTDEV_DISK, DEVICE_TYPE_DISK },
477 { IPMI_BOOTDEV_DISK, DEVICE_TYPE_USB },
478 { IPMI_BOOTDEV_CDROM, DEVICE_TYPE_OPTICAL },
481 static bool ipmi_device_type_matches(enum ipmi_bootdev ipmi_type,
482 enum device_type device_type)
486 for (i = 0; i < ARRAY_SIZE(device_type_map); i++) {
487 if (device_type_map[i].device_type == device_type)
488 return device_type_map[i].ipmi_type == ipmi_type;
494 static int autoboot_option_priority(const struct config *config,
495 struct discover_boot_option *opt)
497 enum device_type type = opt->device->device->type;
498 const char *uuid = opt->device->uuid;
499 struct autoboot_option *auto_opt;
502 for (i = 0; i < config->n_autoboot_opts; i++) {
503 auto_opt = &config->autoboot_opts[i];
504 if (auto_opt->boot_type == BOOT_DEVICE_UUID)
505 if (!strcmp(auto_opt->uuid, uuid))
506 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
508 if (auto_opt->boot_type == BOOT_DEVICE_TYPE)
509 if (auto_opt->type == type ||
510 auto_opt->type == DEVICE_TYPE_ANY)
511 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
518 * We have different priorities to resolve conflicts between boot options that
519 * report to be the default for their device. This function assigns a priority
522 static enum default_priority default_option_priority(
523 struct discover_boot_option *opt)
525 const struct config *config;
527 config = config_get();
529 /* We give highest priority to IPMI-configured boot options. If
530 * we have an IPMI bootdev configuration set, then we don't allow
531 * any other defaults */
532 if (config->ipmi_bootdev) {
533 bool ipmi_match = ipmi_device_type_matches(config->ipmi_bootdev,
534 opt->device->device->type);
536 return DEFAULT_PRIORITY_REMOTE;
538 pb_debug("handler: disabled default priority due to "
539 "non-matching IPMI type %x\n",
540 config->ipmi_bootdev);
541 return DEFAULT_PRIORITY_DISABLED;
544 /* Next, try to match the option against the user-defined autoboot
545 * options, either by device UUID or type. */
546 if (config->n_autoboot_opts) {
547 int boot_match = autoboot_option_priority(config, opt);
552 /* If the option didn't match any entry in the array, it is disabled */
553 pb_debug("handler: disabled default priority due to "
554 "non-matching UUID or type\n");
555 return DEFAULT_PRIORITY_DISABLED;
558 static void set_default(struct device_handler *handler,
559 struct discover_boot_option *opt)
561 enum default_priority cur_prio, new_prio;
563 if (!handler->autoboot_enabled)
566 pb_debug("handler: new default option: %s\n", opt->option->id);
568 new_prio = default_option_priority(opt);
570 /* Anything outside our range prevents a default boot */
571 if (new_prio >= DEFAULT_PRIORITY_DISABLED)
574 pb_debug("handler: calculated priority %d\n", new_prio);
576 /* Resolve any conflicts: if we have a new default option, it only
577 * replaces the current if it has a higher priority. */
578 if (handler->default_boot_option) {
580 cur_prio = handler->default_boot_option_priority;
582 if (new_prio < cur_prio) {
583 pb_log("handler: new prio %d beats "
584 "old prio %d for %s\n",
586 handler->default_boot_option
588 handler->default_boot_option = opt;
589 handler->default_boot_option_priority = new_prio;
590 /* extend the timeout a little, so the user sees some
591 * indication of the change */
592 handler->sec_to_boot += 2;
598 handler->sec_to_boot = config_get()->autoboot_timeout_sec;
599 handler->default_boot_option = opt;
600 handler->default_boot_option_priority = new_prio;
602 pb_log("handler: boot option %s set as default, timeout %u sec.\n",
603 opt->option->id, handler->sec_to_boot);
605 default_timeout(handler);
608 static bool resource_is_resolved(struct resource *res)
610 return !res || res->resolved;
613 /* We only use this in an assert, which will disappear if we're compiling
614 * with NDEBUG, so we need the 'used' attribute for these builds */
615 static bool __attribute__((used)) boot_option_is_resolved(
616 struct discover_boot_option *opt)
618 return resource_is_resolved(opt->boot_image) &&
619 resource_is_resolved(opt->initrd) &&
620 resource_is_resolved(opt->dtb) &&
621 resource_is_resolved(opt->icon);
624 static bool resource_resolve(struct resource *res, const char *name,
625 struct discover_boot_option *opt,
626 struct device_handler *handler)
628 struct parser *parser = opt->source;
630 if (resource_is_resolved(res))
633 pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
634 opt->option->id, name, parser->name);
635 parser->resolve_resource(handler, res);
637 return res->resolved;
640 static bool boot_option_resolve(struct discover_boot_option *opt,
641 struct device_handler *handler)
643 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
644 resource_resolve(opt->initrd, "initrd", opt, handler) &&
645 resource_resolve(opt->dtb, "dtb", opt, handler) &&
646 resource_resolve(opt->icon, "icon", opt, handler);
649 static void boot_option_finalise(struct device_handler *handler,
650 struct discover_boot_option *opt)
652 assert(boot_option_is_resolved(opt));
654 /* check that the parsers haven't set any of the final data */
655 assert(!opt->option->boot_image_file);
656 assert(!opt->option->initrd_file);
657 assert(!opt->option->dtb_file);
658 assert(!opt->option->icon_file);
659 assert(!opt->option->device_id);
662 opt->option->boot_image_file = opt->boot_image->url->full;
664 opt->option->initrd_file = opt->initrd->url->full;
666 opt->option->dtb_file = opt->dtb->url->full;
668 opt->option->icon_file = opt->icon->url->full;
670 opt->option->device_id = opt->device->device->id;
672 if (opt->option->is_default)
673 set_default(handler, opt);
676 static void notify_boot_option(struct device_handler *handler,
677 struct discover_boot_option *opt)
679 struct discover_device *dev = opt->device;
682 discover_server_notify_device_add(handler->server,
683 opt->device->device);
684 dev->notified = true;
685 discover_server_notify_boot_option_add(handler->server, opt->option);
688 static void process_boot_option_queue(struct device_handler *handler)
690 struct discover_boot_option *opt, *tmp;
692 list_for_each_entry_safe(&handler->unresolved_boot_options,
695 pb_debug("queue: attempting resolution for %s\n",
698 if (!boot_option_resolve(opt, handler))
701 pb_debug("\tresolved!\n");
703 list_remove(&opt->list);
704 list_add_tail(&opt->device->boot_options, &opt->list);
705 talloc_steal(opt->device, opt);
706 boot_option_finalise(handler, opt);
707 notify_boot_option(handler, opt);
711 struct discover_context *device_handler_discover_context_create(
712 struct device_handler *handler,
713 struct discover_device *device)
715 struct discover_context *ctx;
717 ctx = talloc_zero(handler, struct discover_context);
718 ctx->device = device;
719 ctx->network = handler->network;
720 list_init(&ctx->boot_options);
725 void device_handler_add_device(struct device_handler *handler,
726 struct discover_device *device)
728 handler->n_devices++;
729 handler->devices = talloc_realloc(handler, handler->devices,
730 struct discover_device *, handler->n_devices);
731 handler->devices[handler->n_devices - 1] = device;
733 if (device->device->type == DEVICE_TYPE_NETWORK)
734 network_register_device(handler->network, device);
737 void device_handler_add_ramdisk(struct device_handler *handler,
740 struct ramdisk_device *dev;
746 for (i = 0; i < handler->n_ramdisks; i++)
747 if (!strcmp(handler->ramdisks[i]->path, path))
750 dev = talloc_zero(handler, struct ramdisk_device);
752 pb_log("Failed to allocate memory to track %s\n", path);
756 dev->path = talloc_strdup(handler, path);
758 handler->ramdisks = talloc_realloc(handler, handler->ramdisks,
759 struct ramdisk_device *,
760 handler->n_ramdisks + 1);
761 if (!handler->ramdisks) {
762 pb_log("Failed to reallocate memory"
763 "- ramdisk tracking inconsistent!\n");
767 handler->ramdisks[i] = dev;
768 i = handler->n_ramdisks++;
771 struct ramdisk_device *device_handler_get_ramdisk(
772 struct device_handler *handler)
778 /* Check if free ramdisk exists */
779 for (i = 0; i < handler->n_ramdisks; i++)
780 if (!handler->ramdisks[i]->snapshot &&
781 !handler->ramdisks[i]->origin &&
782 !handler->ramdisks[i]->base)
783 return handler->ramdisks[i];
785 /* Otherwise create a new one */
786 name = talloc_asprintf(handler, "/dev/ram%d",
787 handler->n_ramdisks);
789 pb_debug("Failed to allocate memory to name /dev/ram%d",
790 handler->n_ramdisks);
794 id = makedev(1, handler->n_ramdisks);
795 if (mknod(name, S_IFBLK, id)) {
796 if (errno == EEXIST) {
797 /* We haven't yet received updates for existing
798 * ramdisks - add and use this one */
799 pb_debug("Using untracked ramdisk %s\n", name);
801 pb_log("Failed to create new ramdisk %s: %s\n",
802 name, strerror(errno));
806 device_handler_add_ramdisk(handler, name);
809 return handler->ramdisks[i];
812 void device_handler_release_ramdisk(struct discover_device *device)
814 struct ramdisk_device *ramdisk = device->ramdisk;
816 talloc_free(ramdisk->snapshot);
817 talloc_free(ramdisk->origin);
818 talloc_free(ramdisk->base);
820 ramdisk->snapshot = ramdisk->origin = ramdisk->base = NULL;
821 ramdisk->sectors = 0;
823 device->ramdisk = NULL;
826 /* Start discovery on a hotplugged device. The device will be in our devices
827 * array, but has only just been initialised by the hotplug source.
829 int device_handler_discover(struct device_handler *handler,
830 struct discover_device *dev)
832 struct discover_context *ctx;
833 struct boot_status *status;
836 status = talloc_zero(handler, struct boot_status);
837 status->type = BOOT_STATUS_INFO;
839 * TRANSLATORS: this string will be passed the type and identifier
840 * of the device. For example, the first parameter could be "Disk",
841 * (which will be translated accordingly) and the second a Linux device
842 * identifier like 'sda1' (which will not be translated)
844 status->message = talloc_asprintf(status, _("Processing %s device %s"),
845 device_type_display_name(dev->device->type),
847 device_handler_boot_status(handler, status);
849 process_boot_option_queue(handler);
851 /* create our context */
852 ctx = device_handler_discover_context_create(handler, dev);
854 rc = mount_device(dev);
858 /* add this device to our system info */
859 system_info_register_blockdev(dev->device->id, dev->uuid,
862 /* run the parsers. This will populate the ctx's boot_option list. */
863 iterate_parsers(ctx);
865 /* add discovered stuff to the handler */
866 device_handler_discover_context_commit(handler, ctx);
870 * TRANSLATORS: the format specifier in this string is a Linux
871 * device identifier, like 'sda1'
873 status->message = talloc_asprintf(status,_("Processing %s complete"),
875 device_handler_boot_status(handler, status);
878 talloc_unlink(handler, ctx);
883 /* Incoming dhcp event */
884 int device_handler_dhcp(struct device_handler *handler,
885 struct discover_device *dev, struct event *event)
887 struct discover_context *ctx;
888 struct boot_status *status;
890 status = talloc_zero(handler, struct boot_status);
891 status->type = BOOT_STATUS_INFO;
893 * TRANSLATORS: this format specifier will be the name of a network
894 * device, like 'eth0'.
896 status->message = talloc_asprintf(status, _("Processing dhcp event on %s"),
898 device_handler_boot_status(handler, status);
900 /* create our context */
901 ctx = device_handler_discover_context_create(handler, dev);
902 talloc_steal(ctx, event);
905 iterate_parsers(ctx);
907 device_handler_discover_context_commit(handler, ctx);
910 * TRANSLATORS: this format specifier will be the name of a network
911 * device, like 'eth0'.
913 status->message = talloc_asprintf(status,_("Processing %s complete"),
915 device_handler_boot_status(handler, status);
918 talloc_unlink(handler, ctx);
923 static struct discover_boot_option *find_boot_option_by_id(
924 struct device_handler *handler, const char *id)
928 for (i = 0; i < handler->n_devices; i++) {
929 struct discover_device *dev = handler->devices[i];
930 struct discover_boot_option *opt;
932 list_for_each_entry(&dev->boot_options, opt, list)
933 if (!strcmp(opt->option->id, id))
940 void device_handler_boot(struct device_handler *handler,
941 struct boot_command *cmd)
943 struct discover_boot_option *opt = NULL;
945 if (cmd->option_id && strlen(cmd->option_id))
946 opt = find_boot_option_by_id(handler, cmd->option_id);
948 if (handler->pending_boot)
949 boot_cancel(handler->pending_boot);
953 handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
954 device_handler_boot_status, handler);
955 handler->pending_boot_is_default = false;
958 void device_handler_cancel_default(struct device_handler *handler)
960 struct boot_status status;
962 if (handler->timeout_waiter)
963 waiter_remove(handler->timeout_waiter);
965 handler->timeout_waiter = NULL;
966 handler->autoboot_enabled = false;
968 /* we only send status if we had a default boot option queued */
969 if (!handler->default_boot_option)
972 pb_log("Cancelling default boot option\n");
974 if (handler->pending_boot && handler->pending_boot_is_default) {
975 boot_cancel(handler->pending_boot);
976 handler->pending_boot = NULL;
977 handler->pending_boot_is_default = false;
980 handler->default_boot_option = NULL;
982 status.type = BOOT_STATUS_INFO;
983 status.progress = -1;
984 status.detail = NULL;
985 status.message = _("Default boot cancelled");
987 discover_server_notify_boot_status(handler->server, &status);
990 void device_handler_update_config(struct device_handler *handler,
991 struct config *config)
995 rc = config_set(config);
999 discover_server_notify_config(handler->server, config);
1000 device_handler_update_lang(config->lang);
1001 device_handler_reinit(handler);
1004 static char *device_from_addr(void *ctx, struct pb_url *url)
1006 char *ipaddr, *buf, *tok, *dev = NULL;
1007 const char *delim = " ";
1008 struct sockaddr_in *ip;
1009 struct sockaddr_in si;
1010 struct addrinfo *res;
1014 /* Note: IPv4 only */
1015 rc = inet_pton(AF_INET, url->host, &(si.sin_addr));
1019 /* need to turn hostname into a valid IP */
1020 rc = getaddrinfo(url->host, NULL, NULL, &res);
1022 pb_debug("%s: Invalid URL\n",__func__);
1025 ipaddr = talloc_array(ctx,char,INET_ADDRSTRLEN);
1026 ip = (struct sockaddr_in *) res->ai_addr;
1027 inet_ntop(AF_INET, &(ip->sin_addr), ipaddr, INET_ADDRSTRLEN);
1031 const char *argv[] = {
1033 "route", "show", "to", "match",
1038 p = process_create(ctx);
1040 p->path = pb_system_apps.ip;
1042 p->keep_stdout = true;
1044 rc = process_run_sync(p);
1046 if (rc || p->exit_status) {
1047 /* ip has complained for some reason; most likely
1048 * there is no route to the host - bail out */
1049 pb_debug("%s: `ip` returns non-zero exit status\n", __func__);
1050 pb_debug("ip buf: %s\n", p->stdout_buf);
1055 buf = p->stdout_buf;
1056 /* If a route is found, ip-route output will be of the form
1057 * "... dev DEVNAME ... " */
1058 tok = strtok(buf, delim);
1060 if (!strcmp(tok, "dev")) {
1061 tok = strtok(NULL, delim);
1062 dev = talloc_strdup(ctx, tok);
1065 tok = strtok(NULL, delim);
1070 pb_debug("%s: Found interface '%s'\n", __func__,dev);
1074 void device_handler_process_url(struct device_handler *handler,
1075 const char *url, const char *mac, const char *ip)
1077 struct discover_context *ctx;
1078 struct discover_device *dev;
1079 struct boot_status *status;
1080 struct pb_url *pb_url;
1081 struct event *event;
1082 struct param *param;
1084 status = talloc(handler, struct boot_status);
1086 status->type = BOOT_STATUS_ERROR;
1087 status->progress = 0;
1088 status->detail = talloc_asprintf(status,
1089 _("Received config URL %s"), url);
1091 if (!handler->network) {
1092 status->message = talloc_asprintf(handler,
1093 _("No network configured"));
1097 event = talloc(handler, struct event);
1098 event->type = EVENT_TYPE_USER;
1099 event->action = EVENT_ACTION_URL;
1101 if (url[strlen(url) - 1] == '/') {
1102 event->params = talloc_array(event, struct param, 3);
1103 param = &event->params[0];
1104 param->name = talloc_strdup(event, "pxepathprefix");
1105 param->value = talloc_strdup(event, url);
1106 param = &event->params[1];
1107 param->name = talloc_strdup(event, "mac");
1108 param->value = talloc_strdup(event, mac);
1109 param = &event->params[2];
1110 param->name = talloc_strdup(event, "ip");
1111 param->value = talloc_strdup(event, ip);
1112 event->n_params = 3;
1114 event->params = talloc_array(event, struct param, 1);
1115 param = &event->params[0];
1116 param->name = talloc_strdup(event, "pxeconffile");
1117 param->value = talloc_strdup(event, url);
1118 event->n_params = 1;
1121 pb_url = pb_url_parse(event, event->params->value);
1122 if (!pb_url || (pb_url->scheme != pb_url_file && !pb_url->host)) {
1123 status->message = talloc_asprintf(handler,
1124 _("Invalid config URL!"));
1128 if (pb_url->scheme == pb_url_file)
1129 event->device = talloc_asprintf(event, "local");
1131 event->device = device_from_addr(event, pb_url);
1133 if (!event->device) {
1134 status->message = talloc_asprintf(status,
1135 _("Unable to route to host %s"),
1140 dev = discover_device_create(handler, mac, event->device);
1141 if (pb_url->scheme == pb_url_file)
1142 dev->device->type = DEVICE_TYPE_ANY;
1143 ctx = device_handler_discover_context_create(handler, dev);
1144 talloc_steal(ctx, event);
1147 iterate_parsers(ctx);
1149 device_handler_discover_context_commit(handler, ctx);
1151 talloc_unlink(handler, ctx);
1153 status->type = BOOT_STATUS_INFO;
1154 status->message = talloc_asprintf(status, _("Config file %s parsed"),
1157 device_handler_boot_status(handler, status);
1158 talloc_free(status);
1161 #ifndef PETITBOOT_TEST
1164 * context_commit - Commit a temporary discovery context to the handler,
1165 * and notify the clients about any new options / devices
1167 void device_handler_discover_context_commit(struct device_handler *handler,
1168 struct discover_context *ctx)
1170 struct discover_device *dev = ctx->device;
1171 struct discover_boot_option *opt, *tmp;
1173 if (!device_lookup_by_uuid(handler, dev->uuid))
1174 device_handler_add_device(handler, dev);
1176 /* move boot options from the context to the device */
1177 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
1178 list_remove(&opt->list);
1180 /* All boot options need at least a kernel image */
1181 if (!opt->boot_image || !opt->boot_image->url) {
1182 pb_log("boot option %s is missing boot image, ignoring\n",
1188 if (boot_option_resolve(opt, handler)) {
1189 pb_log("boot option %s is resolved, "
1190 "sending to clients\n",
1192 list_add_tail(&dev->boot_options, &opt->list);
1193 talloc_steal(dev, opt);
1194 boot_option_finalise(handler, opt);
1195 notify_boot_option(handler, opt);
1197 if (!opt->source->resolve_resource) {
1198 pb_log("parser %s gave us an unresolved "
1199 "resource (%s), but no way to "
1201 opt->source->name, opt->option->id);
1204 pb_log("boot option %s is unresolved, "
1205 "adding to queue\n",
1207 list_add(&handler->unresolved_boot_options,
1209 talloc_steal(handler, opt);
1215 static void device_handler_update_lang(const char *lang)
1217 const char *cur_lang;
1222 cur_lang = setlocale(LC_ALL, NULL);
1223 if (cur_lang && !strcmp(cur_lang, lang))
1226 setlocale(LC_ALL, lang);
1229 static int device_handler_init_sources(struct device_handler *handler)
1231 /* init our device sources: udev, network and user events */
1232 handler->udev = udev_init(handler, handler->waitset);
1236 handler->network = network_init(handler, handler->waitset,
1238 if (!handler->network)
1241 handler->user_event = user_event_init(handler, handler->waitset);
1242 if (!handler->user_event)
1248 static void device_handler_reinit_sources(struct device_handler *handler)
1250 /* if we haven't initialised sources previously (becuase we started in
1251 * safe mode), then init once here. */
1252 if (!(handler->udev || handler->network || handler->user_event)) {
1253 device_handler_init_sources(handler);
1257 udev_reinit(handler->udev);
1259 network_shutdown(handler->network);
1260 handler->network = network_init(handler, handler->waitset,
1264 static inline const char *get_device_path(struct discover_device *dev)
1266 return dev->ramdisk ? dev->ramdisk->snapshot : dev->device_path;
1269 static char *check_subvols(struct discover_device *dev)
1271 const char *fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1276 if (strncmp(fstype, "btrfs", strlen("btrfs")))
1277 return dev->mount_path;
1279 /* On btrfs a device's root may be under a subvolume path */
1280 path = join_paths(dev, dev->mount_path, "@");
1281 rc = stat(path, &sb);
1282 if (!rc && S_ISDIR(sb.st_mode)) {
1283 pb_debug("Using '%s' for btrfs root path\n", path);
1288 return dev->mount_path;
1291 static bool check_existing_mount(struct discover_device *dev)
1293 struct stat devstat, mntstat;
1294 const char *device_path;
1299 device_path = get_device_path(dev);
1301 rc = stat(device_path, &devstat);
1303 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
1307 if (!S_ISBLK(devstat.st_mode)) {
1308 pb_debug("%s: %s isn't a block device?\n", __func__,
1313 fp = fopen("/proc/self/mounts", "r");
1316 mnt = getmntent(fp);
1320 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
1323 rc = stat(mnt->mnt_fsname, &mntstat);
1327 if (!S_ISBLK(mntstat.st_mode))
1330 if (mntstat.st_rdev == devstat.st_rdev) {
1331 dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
1332 dev->root_path = check_subvols(dev);
1333 dev->mounted_rw = !!hasmntopt(mnt, "rw");
1334 dev->mounted = true;
1335 dev->unmount = false;
1337 pb_debug("%s: %s is already mounted (r%c) at %s\n",
1338 __func__, dev->device_path,
1339 dev->mounted_rw ? 'w' : 'o',
1351 * Attempt to mount a filesystem safely, while handling certain filesytem-
1354 static int try_mount(const char *device_path, const char *mount_path,
1355 const char *fstype, unsigned long flags,
1358 const char *fs, *safe_opts;
1361 /* Mount ext3 as ext4 instead so 'norecovery' can be used */
1362 if (strncmp(fstype, "ext3", strlen("ext3")) == 0) {
1363 pb_debug("Mounting ext3 filesystem as ext4\n");
1368 if (strncmp(fs, "xfs", strlen("xfs")) == 0 ||
1369 strncmp(fs, "ext4", strlen("ext4")) == 0)
1370 safe_opts = "norecovery";
1375 /* If no snapshot is available don't attempt recovery */
1377 return mount(device_path, mount_path, fs, flags, safe_opts);
1379 rc = mount(device_path, mount_path, fs, flags, NULL);
1384 /* Mounting failed; some filesystems will fail to mount if a recovery
1385 * journal exists (eg. cross-endian XFS), so try again with norecovery
1386 * where that option is available.
1387 * If mounting read-write just return the error as norecovery is not a
1389 if ((flags & MS_RDONLY) != MS_RDONLY || !safe_opts)
1393 return mount(device_path, mount_path, fs, flags, safe_opts);
1396 static int mount_device(struct discover_device *dev)
1398 const char *fstype, *device_path;
1401 if (!dev->device_path)
1407 if (check_existing_mount(dev))
1410 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1414 dev->mount_path = join_paths(dev, mount_base(),
1417 if (pb_mkdir_recursive(dev->mount_path)) {
1418 pb_log("couldn't create mount directory %s: %s\n",
1419 dev->mount_path, strerror(errno));
1423 device_path = get_device_path(dev);
1425 pb_log("mounting device %s read-only\n", dev->device_path);
1426 rc = try_mount(device_path, dev->mount_path, fstype,
1427 MS_RDONLY | MS_SILENT, dev->ramdisk);
1430 dev->mounted = true;
1431 dev->mounted_rw = false;
1432 dev->unmount = true;
1433 dev->root_path = check_subvols(dev);
1437 pb_log("couldn't mount device %s: mount failed: %s\n",
1438 device_path, strerror(errno));
1440 /* If mount fails clean up any snapshot */
1441 devmapper_destroy_snapshot(dev);
1443 pb_rmdir_recursive(mount_base(), dev->mount_path);
1445 talloc_free(dev->mount_path);
1446 dev->mount_path = NULL;
1450 static int umount_device(struct discover_device *dev)
1452 const char *device_path;
1455 if (!dev->mounted || !dev->unmount)
1458 device_path = get_device_path(dev);
1460 pb_log("unmounting device %s\n", device_path);
1461 rc = umount(dev->mount_path);
1465 dev->mounted = false;
1466 devmapper_destroy_snapshot(dev);
1468 pb_rmdir_recursive(mount_base(), dev->mount_path);
1470 talloc_free(dev->mount_path);
1471 dev->mount_path = NULL;
1472 dev->root_path = NULL;
1477 int device_request_write(struct discover_device *dev, bool *release)
1479 const char *fstype, *device_path;
1480 const struct config *config;
1485 config = config_get();
1486 if (!config->allow_writes)
1492 if (dev->mounted_rw)
1495 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1497 device_path = get_device_path(dev);
1499 pb_log("remounting device %s read-write\n", device_path);
1501 rc = umount(dev->mount_path);
1503 pb_log("Failed to unmount %s: %s\n",
1504 dev->mount_path, strerror(errno));
1508 rc = try_mount(device_path, dev->mount_path, fstype,
1509 MS_SILENT, dev->ramdisk);
1513 dev->mounted_rw = true;
1518 pb_log("Unable to remount device %s read-write: %s\n",
1519 device_path, strerror(errno));
1520 rc = try_mount(device_path, dev->mount_path, fstype,
1521 MS_RDONLY | MS_SILENT, dev->ramdisk);
1523 pb_log("Unable to recover mount for %s: %s\n",
1524 device_path, strerror(errno));
1528 void device_release_write(struct discover_device *dev, bool release)
1530 const char *fstype, *device_path;
1535 device_path = get_device_path(dev);
1537 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1539 pb_log("remounting device %s read-only\n", device_path);
1541 if (umount(dev->mount_path)) {
1542 pb_log("Failed to unmount %s\n", dev->mount_path);
1545 dev->mounted_rw = dev->mounted = false;
1548 devmapper_merge_snapshot(dev);
1549 /* device_path becomes stale after merge */
1550 device_path = get_device_path(dev);
1553 if (try_mount(device_path, dev->mount_path, fstype,
1554 MS_RDONLY | MS_SILENT, dev->ramdisk))
1555 pb_log("Failed to remount %s read-only: %s\n",
1556 device_path, strerror(errno));
1558 dev->mounted = true;
1561 void device_sync_snapshots(struct device_handler *handler, const char *device)
1563 struct discover_device *dev = NULL;
1567 /* Find matching device and sync */
1568 dev = device_lookup_by_name(handler, device);
1570 pb_log("%s: device name '%s' unrecognised\n",
1575 device_release_write(dev, true);
1577 pb_log("%s has no snapshot to merge, skipping\n",
1582 /* Otherwise sync all relevant devices */
1583 for (i = 0; i < handler->n_devices; i++) {
1584 dev = handler->devices[i];
1585 if (dev->device->type != DEVICE_TYPE_DISK &&
1586 dev->device->type != DEVICE_TYPE_USB)
1589 device_release_write(dev, true);
1591 pb_log("%s has no snapshot to merge, skipping\n",
1598 void device_handler_discover_context_commit(
1599 struct device_handler *handler __attribute__((unused)),
1600 struct discover_context *ctx __attribute__((unused)))
1602 pb_log("%s stubbed out for test cases\n", __func__);
1605 static void device_handler_update_lang(const char *lang __attribute__((unused)))
1609 static int device_handler_init_sources(
1610 struct device_handler *handler __attribute__((unused)))
1615 static void device_handler_reinit_sources(
1616 struct device_handler *handler __attribute__((unused)))
1620 static int umount_device(struct discover_device *dev __attribute__((unused)))
1625 static int __attribute__((unused)) mount_device(
1626 struct discover_device *dev __attribute__((unused)))
1631 int device_request_write(struct discover_device *dev __attribute__((unused)),
1638 void device_release_write(struct discover_device *dev __attribute__((unused)),
1639 bool release __attribute__((unused)))
1643 void device_sync_snapshots(
1644 struct device_handler *handler __attribute__((unused)),
1645 const char *device __attribute__((unused)))