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 list_for_each_entry_safe(&device->boot_options, opt, tmp, list) {
363 if (opt == handler->default_boot_option) {
364 pb_log("Default option %s cancelled since device removed",
366 device_handler_cancel_default(handler);
371 for (i = 0; i < handler->n_devices; i++)
372 if (handler->devices[i] == device)
375 if (i == handler->n_devices) {
380 /* Free any unresolved options, as they're currently allocated
381 * against the handler */
382 list_for_each_entry_safe(&handler->unresolved_boot_options,
384 if (opt->device != device)
386 list_remove(&opt->list);
390 /* if this is a network device, we have to unregister it from the
392 if (device->device->type == DEVICE_TYPE_NETWORK)
393 network_unregister_device(handler->network, device);
395 handler->n_devices--;
396 memmove(&handler->devices[i], &handler->devices[i + 1],
397 (handler->n_devices - i) * sizeof(handler->devices[0]));
398 handler->devices = talloc_realloc(handler, handler->devices,
399 struct discover_device *, handler->n_devices);
401 if (device->notified)
402 discover_server_notify_device_remove(handler->server,
408 void device_handler_boot_status(void *arg, struct boot_status *status)
410 struct device_handler *handler = arg;
412 discover_server_notify_boot_status(handler->server, status);
415 static void countdown_status(struct device_handler *handler,
416 struct discover_boot_option *opt, unsigned int sec)
418 struct boot_status status;
420 status.type = BOOT_STATUS_INFO;
421 status.progress = -1;
422 status.detail = NULL;
423 status.message = talloc_asprintf(handler,
424 _("Booting in %d sec: %s"), sec, opt->option->name);
426 discover_server_notify_boot_status(handler->server, &status);
428 talloc_free(status.message);
431 static int default_timeout(void *arg)
433 struct device_handler *handler = arg;
434 struct discover_boot_option *opt;
436 if (!handler->default_boot_option)
439 if (handler->pending_boot)
442 opt = handler->default_boot_option;
444 if (handler->sec_to_boot) {
445 countdown_status(handler, opt, handler->sec_to_boot);
446 handler->sec_to_boot--;
447 handler->timeout_waiter = waiter_register_timeout(
448 handler->waitset, 1000,
449 default_timeout, handler);
453 handler->timeout_waiter = NULL;
455 pb_log("Timeout expired, booting default option %s\n", opt->option->id);
459 handler->pending_boot = boot(handler, handler->default_boot_option,
460 NULL, handler->dry_run, device_handler_boot_status,
462 handler->pending_boot_is_default = true;
467 enum ipmi_bootdev ipmi_type;
468 enum device_type device_type;
469 } device_type_map[] = {
470 { IPMI_BOOTDEV_NETWORK, DEVICE_TYPE_NETWORK },
471 { IPMI_BOOTDEV_DISK, DEVICE_TYPE_DISK },
472 { IPMI_BOOTDEV_DISK, DEVICE_TYPE_USB },
473 { IPMI_BOOTDEV_CDROM, DEVICE_TYPE_OPTICAL },
476 static bool ipmi_device_type_matches(enum ipmi_bootdev ipmi_type,
477 enum device_type device_type)
481 for (i = 0; i < ARRAY_SIZE(device_type_map); i++) {
482 if (device_type_map[i].device_type == device_type)
483 return device_type_map[i].ipmi_type == ipmi_type;
489 static int autoboot_option_priority(const struct config *config,
490 struct discover_boot_option *opt)
492 enum device_type type = opt->device->device->type;
493 const char *uuid = opt->device->uuid;
494 struct autoboot_option *auto_opt;
497 for (i = 0; i < config->n_autoboot_opts; i++) {
498 auto_opt = &config->autoboot_opts[i];
499 if (auto_opt->boot_type == BOOT_DEVICE_UUID)
500 if (!strcmp(auto_opt->uuid, uuid))
501 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
503 if (auto_opt->boot_type == BOOT_DEVICE_TYPE)
504 if (auto_opt->type == type ||
505 auto_opt->type == DEVICE_TYPE_ANY)
506 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
513 * We have different priorities to resolve conflicts between boot options that
514 * report to be the default for their device. This function assigns a priority
517 static enum default_priority default_option_priority(
518 struct discover_boot_option *opt)
520 const struct config *config;
522 config = config_get();
524 /* We give highest priority to IPMI-configured boot options. If
525 * we have an IPMI bootdev configuration set, then we don't allow
526 * any other defaults */
527 if (config->ipmi_bootdev) {
528 bool ipmi_match = ipmi_device_type_matches(config->ipmi_bootdev,
529 opt->device->device->type);
531 return DEFAULT_PRIORITY_REMOTE;
533 pb_debug("handler: disabled default priority due to "
534 "non-matching IPMI type %x\n",
535 config->ipmi_bootdev);
536 return DEFAULT_PRIORITY_DISABLED;
539 /* Next, try to match the option against the user-defined autoboot
540 * options, either by device UUID or type. */
541 if (config->n_autoboot_opts) {
542 int boot_match = autoboot_option_priority(config, opt);
547 /* If the option didn't match any entry in the array, it is disabled */
548 pb_debug("handler: disabled default priority due to "
549 "non-matching UUID or type\n");
550 return DEFAULT_PRIORITY_DISABLED;
553 static void set_default(struct device_handler *handler,
554 struct discover_boot_option *opt)
556 enum default_priority cur_prio, new_prio;
558 if (!handler->autoboot_enabled)
561 pb_debug("handler: new default option: %s\n", opt->option->id);
563 new_prio = default_option_priority(opt);
565 /* Anything outside our range prevents a default boot */
566 if (new_prio >= DEFAULT_PRIORITY_DISABLED)
569 pb_debug("handler: calculated priority %d\n", new_prio);
571 /* Resolve any conflicts: if we have a new default option, it only
572 * replaces the current if it has a higher priority. */
573 if (handler->default_boot_option) {
575 cur_prio = handler->default_boot_option_priority;
577 if (new_prio < cur_prio) {
578 pb_log("handler: new prio %d beats "
579 "old prio %d for %s\n",
581 handler->default_boot_option
583 handler->default_boot_option = opt;
584 handler->default_boot_option_priority = new_prio;
585 /* extend the timeout a little, so the user sees some
586 * indication of the change */
587 handler->sec_to_boot += 2;
593 handler->sec_to_boot = config_get()->autoboot_timeout_sec;
594 handler->default_boot_option = opt;
595 handler->default_boot_option_priority = new_prio;
597 pb_log("handler: boot option %s set as default, timeout %u sec.\n",
598 opt->option->id, handler->sec_to_boot);
600 default_timeout(handler);
603 static bool resource_is_resolved(struct resource *res)
605 return !res || res->resolved;
608 /* We only use this in an assert, which will disappear if we're compiling
609 * with NDEBUG, so we need the 'used' attribute for these builds */
610 static bool __attribute__((used)) boot_option_is_resolved(
611 struct discover_boot_option *opt)
613 return resource_is_resolved(opt->boot_image) &&
614 resource_is_resolved(opt->initrd) &&
615 resource_is_resolved(opt->dtb) &&
616 resource_is_resolved(opt->icon);
619 static bool resource_resolve(struct resource *res, const char *name,
620 struct discover_boot_option *opt,
621 struct device_handler *handler)
623 struct parser *parser = opt->source;
625 if (resource_is_resolved(res))
628 pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
629 opt->option->id, name, parser->name);
630 parser->resolve_resource(handler, res);
632 return res->resolved;
635 static bool boot_option_resolve(struct discover_boot_option *opt,
636 struct device_handler *handler)
638 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
639 resource_resolve(opt->initrd, "initrd", opt, handler) &&
640 resource_resolve(opt->dtb, "dtb", opt, handler) &&
641 resource_resolve(opt->icon, "icon", opt, handler);
644 static void boot_option_finalise(struct device_handler *handler,
645 struct discover_boot_option *opt)
647 assert(boot_option_is_resolved(opt));
649 /* check that the parsers haven't set any of the final data */
650 assert(!opt->option->boot_image_file);
651 assert(!opt->option->initrd_file);
652 assert(!opt->option->dtb_file);
653 assert(!opt->option->icon_file);
654 assert(!opt->option->device_id);
657 opt->option->boot_image_file = opt->boot_image->url->full;
659 opt->option->initrd_file = opt->initrd->url->full;
661 opt->option->dtb_file = opt->dtb->url->full;
663 opt->option->icon_file = opt->icon->url->full;
665 opt->option->device_id = opt->device->device->id;
667 if (opt->option->is_default)
668 set_default(handler, opt);
671 static void notify_boot_option(struct device_handler *handler,
672 struct discover_boot_option *opt)
674 struct discover_device *dev = opt->device;
677 discover_server_notify_device_add(handler->server,
678 opt->device->device);
679 dev->notified = true;
680 discover_server_notify_boot_option_add(handler->server, opt->option);
683 static void process_boot_option_queue(struct device_handler *handler)
685 struct discover_boot_option *opt, *tmp;
687 list_for_each_entry_safe(&handler->unresolved_boot_options,
690 pb_debug("queue: attempting resolution for %s\n",
693 if (!boot_option_resolve(opt, handler))
696 pb_debug("\tresolved!\n");
698 list_remove(&opt->list);
699 list_add_tail(&opt->device->boot_options, &opt->list);
700 talloc_steal(opt->device, opt);
701 boot_option_finalise(handler, opt);
702 notify_boot_option(handler, opt);
706 struct discover_context *device_handler_discover_context_create(
707 struct device_handler *handler,
708 struct discover_device *device)
710 struct discover_context *ctx;
712 ctx = talloc_zero(handler, struct discover_context);
713 ctx->device = device;
714 ctx->network = handler->network;
715 list_init(&ctx->boot_options);
720 void device_handler_add_device(struct device_handler *handler,
721 struct discover_device *device)
723 handler->n_devices++;
724 handler->devices = talloc_realloc(handler, handler->devices,
725 struct discover_device *, handler->n_devices);
726 handler->devices[handler->n_devices - 1] = device;
728 if (device->device->type == DEVICE_TYPE_NETWORK)
729 network_register_device(handler->network, device);
732 void device_handler_add_ramdisk(struct device_handler *handler,
735 struct ramdisk_device *dev;
741 for (i = 0; i < handler->n_ramdisks; i++)
742 if (!strcmp(handler->ramdisks[i]->path, path))
745 dev = talloc_zero(handler, struct ramdisk_device);
747 pb_log("Failed to allocate memory to track %s\n", path);
751 dev->path = talloc_strdup(handler, path);
753 handler->ramdisks = talloc_realloc(handler, handler->ramdisks,
754 struct ramdisk_device *,
755 handler->n_ramdisks + 1);
756 if (!handler->ramdisks) {
757 pb_log("Failed to reallocate memory"
758 "- ramdisk tracking inconsistent!\n");
762 handler->ramdisks[i] = dev;
763 i = handler->n_ramdisks++;
766 struct ramdisk_device *device_handler_get_ramdisk(
767 struct device_handler *handler)
773 /* Check if free ramdisk exists */
774 for (i = 0; i < handler->n_ramdisks; i++)
775 if (!handler->ramdisks[i]->snapshot &&
776 !handler->ramdisks[i]->origin &&
777 !handler->ramdisks[i]->base)
778 return handler->ramdisks[i];
780 /* Otherwise create a new one */
781 name = talloc_asprintf(handler, "/dev/ram%d",
782 handler->n_ramdisks);
784 pb_debug("Failed to allocate memory to name /dev/ram%d",
785 handler->n_ramdisks);
789 id = makedev(1, handler->n_ramdisks);
790 if (mknod(name, S_IFBLK, id)) {
791 if (errno == EEXIST) {
792 /* We haven't yet received updates for existing
793 * ramdisks - add and use this one */
794 pb_debug("Using untracked ramdisk %s\n", name);
796 pb_log("Failed to create new ramdisk %s: %s\n",
797 name, strerror(errno));
801 device_handler_add_ramdisk(handler, name);
804 return handler->ramdisks[i];
807 void device_handler_release_ramdisk(struct discover_device *device)
809 struct ramdisk_device *ramdisk = device->ramdisk;
811 talloc_free(ramdisk->snapshot);
812 talloc_free(ramdisk->origin);
813 talloc_free(ramdisk->base);
815 ramdisk->snapshot = ramdisk->origin = ramdisk->base = NULL;
816 ramdisk->sectors = 0;
818 device->ramdisk = NULL;
821 /* Start discovery on a hotplugged device. The device will be in our devices
822 * array, but has only just been initialised by the hotplug source.
824 int device_handler_discover(struct device_handler *handler,
825 struct discover_device *dev)
827 struct discover_context *ctx;
828 struct boot_status *status;
831 status = talloc_zero(handler, struct boot_status);
832 status->type = BOOT_STATUS_INFO;
834 * TRANSLATORS: this string will be passed the type and identifier
835 * of the device. For example, the first parameter could be "Disk",
836 * (which will be translated accordingly) and the second a Linux device
837 * identifier like 'sda1' (which will not be translated)
839 status->message = talloc_asprintf(status, _("Processing %s device %s"),
840 device_type_display_name(dev->device->type),
842 device_handler_boot_status(handler, status);
844 process_boot_option_queue(handler);
846 /* create our context */
847 ctx = device_handler_discover_context_create(handler, dev);
849 rc = mount_device(dev);
853 /* add this device to our system info */
854 system_info_register_blockdev(dev->device->id, dev->uuid,
857 /* run the parsers. This will populate the ctx's boot_option list. */
858 iterate_parsers(ctx);
860 /* add discovered stuff to the handler */
861 device_handler_discover_context_commit(handler, ctx);
865 * TRANSLATORS: the format specifier in this string is a Linux
866 * device identifier, like 'sda1'
868 status->message = talloc_asprintf(status,_("Processing %s complete"),
870 device_handler_boot_status(handler, status);
873 talloc_unlink(handler, ctx);
878 /* Incoming dhcp event */
879 int device_handler_dhcp(struct device_handler *handler,
880 struct discover_device *dev, struct event *event)
882 struct discover_context *ctx;
883 struct boot_status *status;
885 status = talloc_zero(handler, struct boot_status);
886 status->type = BOOT_STATUS_INFO;
888 * TRANSLATORS: this format specifier will be the name of a network
889 * device, like 'eth0'.
891 status->message = talloc_asprintf(status, _("Processing dhcp event on %s"),
893 device_handler_boot_status(handler, status);
895 /* create our context */
896 ctx = device_handler_discover_context_create(handler, dev);
897 talloc_steal(ctx, event);
900 iterate_parsers(ctx);
902 device_handler_discover_context_commit(handler, ctx);
905 * TRANSLATORS: this format specifier will be the name of a network
906 * device, like 'eth0'.
908 status->message = talloc_asprintf(status,_("Processing %s complete"),
910 device_handler_boot_status(handler, status);
913 talloc_unlink(handler, ctx);
918 static struct discover_boot_option *find_boot_option_by_id(
919 struct device_handler *handler, const char *id)
923 for (i = 0; i < handler->n_devices; i++) {
924 struct discover_device *dev = handler->devices[i];
925 struct discover_boot_option *opt;
927 list_for_each_entry(&dev->boot_options, opt, list)
928 if (!strcmp(opt->option->id, id))
935 void device_handler_boot(struct device_handler *handler,
936 struct boot_command *cmd)
938 struct discover_boot_option *opt = NULL;
940 if (cmd->option_id && strlen(cmd->option_id))
941 opt = find_boot_option_by_id(handler, cmd->option_id);
943 if (handler->pending_boot)
944 boot_cancel(handler->pending_boot);
948 handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
949 device_handler_boot_status, handler);
950 handler->pending_boot_is_default = false;
953 void device_handler_cancel_default(struct device_handler *handler)
955 struct boot_status status;
957 if (handler->timeout_waiter)
958 waiter_remove(handler->timeout_waiter);
960 handler->timeout_waiter = NULL;
961 handler->autoboot_enabled = false;
963 /* we only send status if we had a default boot option queued */
964 if (!handler->default_boot_option)
967 pb_log("Cancelling default boot option\n");
969 if (handler->pending_boot && handler->pending_boot_is_default) {
970 boot_cancel(handler->pending_boot);
971 handler->pending_boot = NULL;
972 handler->pending_boot_is_default = false;
975 handler->default_boot_option = NULL;
977 status.type = BOOT_STATUS_INFO;
978 status.progress = -1;
979 status.detail = NULL;
980 status.message = _("Default boot cancelled");
982 discover_server_notify_boot_status(handler->server, &status);
985 void device_handler_update_config(struct device_handler *handler,
986 struct config *config)
990 rc = config_set(config);
994 discover_server_notify_config(handler->server, config);
995 device_handler_update_lang(config->lang);
996 device_handler_reinit(handler);
999 static char *device_from_addr(void *ctx, struct pb_url *url)
1001 char *ipaddr, *buf, *tok, *dev = NULL;
1002 const char *delim = " ";
1003 struct sockaddr_in *ip;
1004 struct sockaddr_in si;
1005 struct addrinfo *res;
1009 /* Note: IPv4 only */
1010 rc = inet_pton(AF_INET, url->host, &(si.sin_addr));
1014 /* need to turn hostname into a valid IP */
1015 rc = getaddrinfo(url->host, NULL, NULL, &res);
1017 pb_debug("%s: Invalid URL\n",__func__);
1020 ipaddr = talloc_array(ctx,char,INET_ADDRSTRLEN);
1021 ip = (struct sockaddr_in *) res->ai_addr;
1022 inet_ntop(AF_INET, &(ip->sin_addr), ipaddr, INET_ADDRSTRLEN);
1026 const char *argv[] = {
1028 "route", "show", "to", "match",
1033 p = process_create(ctx);
1035 p->path = pb_system_apps.ip;
1037 p->keep_stdout = true;
1039 rc = process_run_sync(p);
1041 if (rc || p->exit_status) {
1042 /* ip has complained for some reason; most likely
1043 * there is no route to the host - bail out */
1044 pb_debug("%s: `ip` returns non-zero exit status\n", __func__);
1045 pb_debug("ip buf: %s\n", p->stdout_buf);
1050 buf = p->stdout_buf;
1051 /* If a route is found, ip-route output will be of the form
1052 * "... dev DEVNAME ... " */
1053 tok = strtok(buf, delim);
1055 if (!strcmp(tok, "dev")) {
1056 tok = strtok(NULL, delim);
1057 dev = talloc_strdup(ctx, tok);
1060 tok = strtok(NULL, delim);
1065 pb_debug("%s: Found interface '%s'\n", __func__,dev);
1069 void device_handler_process_url(struct device_handler *handler,
1070 const char *url, const char *mac, const char *ip)
1072 struct discover_context *ctx;
1073 struct discover_device *dev;
1074 struct boot_status *status;
1075 struct pb_url *pb_url;
1076 struct event *event;
1077 struct param *param;
1079 status = talloc(handler, struct boot_status);
1081 status->type = BOOT_STATUS_ERROR;
1082 status->progress = 0;
1083 status->detail = talloc_asprintf(status,
1084 _("Received config URL %s"), url);
1086 if (!handler->network) {
1087 status->message = talloc_asprintf(handler,
1088 _("No network configured"));
1092 event = talloc(handler, struct event);
1093 event->type = EVENT_TYPE_USER;
1094 event->action = EVENT_ACTION_URL;
1096 if (url[strlen(url) - 1] == '/') {
1097 event->params = talloc_array(event, struct param, 3);
1098 param = &event->params[0];
1099 param->name = talloc_strdup(event, "pxepathprefix");
1100 param->value = talloc_strdup(event, url);
1101 param = &event->params[1];
1102 param->name = talloc_strdup(event, "mac");
1103 param->value = talloc_strdup(event, mac);
1104 param = &event->params[2];
1105 param->name = talloc_strdup(event, "ip");
1106 param->value = talloc_strdup(event, ip);
1107 event->n_params = 3;
1109 event->params = talloc_array(event, struct param, 1);
1110 param = &event->params[0];
1111 param->name = talloc_strdup(event, "pxeconffile");
1112 param->value = talloc_strdup(event, url);
1113 event->n_params = 1;
1116 pb_url = pb_url_parse(event, event->params->value);
1117 if (!pb_url || (pb_url->scheme != pb_url_file && !pb_url->host)) {
1118 status->message = talloc_asprintf(handler,
1119 _("Invalid config URL!"));
1123 if (pb_url->scheme == pb_url_file)
1124 event->device = talloc_asprintf(event, "local");
1126 event->device = device_from_addr(event, pb_url);
1128 if (!event->device) {
1129 status->message = talloc_asprintf(status,
1130 _("Unable to route to host %s"),
1135 dev = discover_device_create(handler, event->device);
1136 if (pb_url->scheme == pb_url_file)
1137 dev->device->type = DEVICE_TYPE_ANY;
1138 ctx = device_handler_discover_context_create(handler, dev);
1139 talloc_steal(ctx, event);
1142 iterate_parsers(ctx);
1144 device_handler_discover_context_commit(handler, ctx);
1146 talloc_unlink(handler, ctx);
1148 status->type = BOOT_STATUS_INFO;
1149 status->message = talloc_asprintf(status, _("Config file %s parsed"),
1152 device_handler_boot_status(handler, status);
1153 talloc_free(status);
1156 #ifndef PETITBOOT_TEST
1159 * context_commit - Commit a temporary discovery context to the handler,
1160 * and notify the clients about any new options / devices
1162 void device_handler_discover_context_commit(struct device_handler *handler,
1163 struct discover_context *ctx)
1165 struct discover_device *dev = ctx->device;
1166 struct discover_boot_option *opt, *tmp;
1168 if (!device_lookup_by_id(handler, dev->device->id))
1169 device_handler_add_device(handler, dev);
1171 /* move boot options from the context to the device */
1172 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
1173 list_remove(&opt->list);
1175 /* All boot options need at least a kernel image */
1176 if (!opt->boot_image || !opt->boot_image->url) {
1177 pb_log("boot option %s is missing boot image, ignoring\n",
1183 if (boot_option_resolve(opt, handler)) {
1184 pb_log("boot option %s is resolved, "
1185 "sending to clients\n",
1187 list_add_tail(&dev->boot_options, &opt->list);
1188 talloc_steal(dev, opt);
1189 boot_option_finalise(handler, opt);
1190 notify_boot_option(handler, opt);
1192 if (!opt->source->resolve_resource) {
1193 pb_log("parser %s gave us an unresolved "
1194 "resource (%s), but no way to "
1196 opt->source->name, opt->option->id);
1199 pb_log("boot option %s is unresolved, "
1200 "adding to queue\n",
1202 list_add(&handler->unresolved_boot_options,
1204 talloc_steal(handler, opt);
1210 static void device_handler_update_lang(const char *lang)
1212 const char *cur_lang;
1217 cur_lang = setlocale(LC_ALL, NULL);
1218 if (cur_lang && !strcmp(cur_lang, lang))
1221 setlocale(LC_ALL, lang);
1224 static int device_handler_init_sources(struct device_handler *handler)
1226 /* init our device sources: udev, network and user events */
1227 handler->udev = udev_init(handler, handler->waitset);
1231 handler->network = network_init(handler, handler->waitset,
1233 if (!handler->network)
1236 handler->user_event = user_event_init(handler, handler->waitset);
1237 if (!handler->user_event)
1243 static void device_handler_reinit_sources(struct device_handler *handler)
1245 /* if we haven't initialised sources previously (becuase we started in
1246 * safe mode), then init once here. */
1247 if (!(handler->udev || handler->network || handler->user_event)) {
1248 device_handler_init_sources(handler);
1252 udev_reinit(handler->udev);
1254 network_shutdown(handler->network);
1255 handler->network = network_init(handler, handler->waitset,
1259 static inline const char *get_device_path(struct discover_device *dev)
1261 return dev->ramdisk ? dev->ramdisk->snapshot : dev->device_path;
1264 static char *check_subvols(struct discover_device *dev)
1266 const char *fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1271 if (strncmp(fstype, "btrfs", strlen("btrfs")))
1272 return dev->mount_path;
1274 /* On btrfs a device's root may be under a subvolume path */
1275 path = join_paths(dev, dev->mount_path, "@");
1276 rc = stat(path, &sb);
1277 if (!rc && S_ISDIR(sb.st_mode)) {
1278 pb_debug("Using '%s' for btrfs root path\n", path);
1283 return dev->mount_path;
1286 static bool check_existing_mount(struct discover_device *dev)
1288 struct stat devstat, mntstat;
1289 const char *device_path;
1294 device_path = get_device_path(dev);
1296 rc = stat(device_path, &devstat);
1298 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
1302 if (!S_ISBLK(devstat.st_mode)) {
1303 pb_debug("%s: %s isn't a block device?\n", __func__,
1308 fp = fopen("/proc/self/mounts", "r");
1311 mnt = getmntent(fp);
1315 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
1318 rc = stat(mnt->mnt_fsname, &mntstat);
1322 if (!S_ISBLK(mntstat.st_mode))
1325 if (mntstat.st_rdev == devstat.st_rdev) {
1326 dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
1327 dev->root_path = check_subvols(dev);
1328 dev->mounted_rw = !!hasmntopt(mnt, "rw");
1329 dev->mounted = true;
1330 dev->unmount = false;
1332 pb_debug("%s: %s is already mounted (r%c) at %s\n",
1333 __func__, dev->device_path,
1334 dev->mounted_rw ? 'w' : 'o',
1346 * Attempt to mount a filesystem safely, while handling certain filesytem-
1349 static int try_mount(const char *device_path, const char *mount_path,
1350 const char *fstype, unsigned long flags,
1353 const char *fs, *safe_opts;
1356 /* Mount ext3 as ext4 instead so 'norecovery' can be used */
1357 if (strncmp(fstype, "ext3", strlen("ext3")) == 0) {
1358 pb_debug("Mounting ext3 filesystem as ext4\n");
1363 if (strncmp(fs, "xfs", strlen("xfs")) == 0 ||
1364 strncmp(fs, "ext4", strlen("ext4")) == 0)
1365 safe_opts = "norecovery";
1370 /* If no snapshot is available don't attempt recovery */
1372 return mount(device_path, mount_path, fs, flags, safe_opts);
1374 rc = mount(device_path, mount_path, fs, flags, NULL);
1379 /* Mounting failed; some filesystems will fail to mount if a recovery
1380 * journal exists (eg. cross-endian XFS), so try again with norecovery
1381 * where that option is available.
1382 * If mounting read-write just return the error as norecovery is not a
1384 if ((flags & MS_RDONLY) != MS_RDONLY || !safe_opts)
1388 return mount(device_path, mount_path, fs, flags, safe_opts);
1391 static int mount_device(struct discover_device *dev)
1393 const char *fstype, *device_path;
1396 if (!dev->device_path)
1402 if (check_existing_mount(dev))
1405 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1409 dev->mount_path = join_paths(dev, mount_base(),
1412 if (pb_mkdir_recursive(dev->mount_path)) {
1413 pb_log("couldn't create mount directory %s: %s\n",
1414 dev->mount_path, strerror(errno));
1418 device_path = get_device_path(dev);
1420 pb_log("mounting device %s read-only\n", dev->device_path);
1421 rc = try_mount(device_path, dev->mount_path, fstype,
1422 MS_RDONLY | MS_SILENT, dev->ramdisk);
1425 dev->mounted = true;
1426 dev->mounted_rw = false;
1427 dev->unmount = true;
1428 dev->root_path = check_subvols(dev);
1432 pb_log("couldn't mount device %s: mount failed: %s\n",
1433 device_path, strerror(errno));
1435 /* If mount fails clean up any snapshot */
1436 devmapper_destroy_snapshot(dev);
1438 pb_rmdir_recursive(mount_base(), dev->mount_path);
1440 talloc_free(dev->mount_path);
1441 dev->mount_path = NULL;
1445 static int umount_device(struct discover_device *dev)
1447 const char *device_path;
1450 if (!dev->mounted || !dev->unmount)
1453 device_path = get_device_path(dev);
1455 pb_log("unmounting device %s\n", device_path);
1456 rc = umount(dev->mount_path);
1460 dev->mounted = false;
1461 devmapper_destroy_snapshot(dev);
1463 pb_rmdir_recursive(mount_base(), dev->mount_path);
1465 talloc_free(dev->mount_path);
1466 dev->mount_path = NULL;
1467 dev->root_path = NULL;
1472 int device_request_write(struct discover_device *dev, bool *release)
1474 const char *fstype, *device_path;
1475 const struct config *config;
1480 config = config_get();
1481 if (!config->allow_writes)
1487 if (dev->mounted_rw)
1490 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1492 device_path = get_device_path(dev);
1494 pb_log("remounting device %s read-write\n", device_path);
1496 rc = umount(dev->mount_path);
1498 pb_log("Failed to unmount %s: %s\n",
1499 dev->mount_path, strerror(errno));
1503 rc = try_mount(device_path, dev->mount_path, fstype,
1504 MS_SILENT, dev->ramdisk);
1508 dev->mounted_rw = true;
1513 pb_log("Unable to remount device %s read-write: %s\n",
1514 device_path, strerror(errno));
1515 rc = try_mount(device_path, dev->mount_path, fstype,
1516 MS_RDONLY | MS_SILENT, dev->ramdisk);
1518 pb_log("Unable to recover mount for %s: %s\n",
1519 device_path, strerror(errno));
1523 void device_release_write(struct discover_device *dev, bool release)
1525 const char *fstype, *device_path;
1530 device_path = get_device_path(dev);
1532 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1534 pb_log("remounting device %s read-only\n", device_path);
1536 if (umount(dev->mount_path)) {
1537 pb_log("Failed to unmount %s\n", dev->mount_path);
1540 dev->mounted_rw = dev->mounted = false;
1543 devmapper_merge_snapshot(dev);
1544 /* device_path becomes stale after merge */
1545 device_path = get_device_path(dev);
1548 if (try_mount(device_path, dev->mount_path, fstype,
1549 MS_RDONLY | MS_SILENT, dev->ramdisk))
1550 pb_log("Failed to remount %s read-only: %s\n",
1551 device_path, strerror(errno));
1553 dev->mounted = true;
1556 void device_sync_snapshots(struct device_handler *handler, const char *device)
1558 struct discover_device *dev = NULL;
1562 /* Find matching device and sync */
1563 dev = device_lookup_by_name(handler, device);
1565 pb_log("%s: device name '%s' unrecognised\n",
1570 device_release_write(dev, true);
1572 pb_log("%s has no snapshot to merge, skipping\n",
1577 /* Otherwise sync all relevant devices */
1578 for (i = 0; i < handler->n_devices; i++) {
1579 dev = handler->devices[i];
1580 if (dev->device->type != DEVICE_TYPE_DISK &&
1581 dev->device->type != DEVICE_TYPE_USB)
1584 device_release_write(dev, true);
1586 pb_log("%s has no snapshot to merge, skipping\n",
1593 void device_handler_discover_context_commit(
1594 struct device_handler *handler __attribute__((unused)),
1595 struct discover_context *ctx __attribute__((unused)))
1597 pb_log("%s stubbed out for test cases\n", __func__);
1600 static void device_handler_update_lang(const char *lang __attribute__((unused)))
1604 static int device_handler_init_sources(
1605 struct device_handler *handler __attribute__((unused)))
1610 static void device_handler_reinit_sources(
1611 struct device_handler *handler __attribute__((unused)))
1615 static int umount_device(struct discover_device *dev __attribute__((unused)))
1620 static int __attribute__((unused)) mount_device(
1621 struct discover_device *dev __attribute__((unused)))
1626 int device_request_write(struct discover_device *dev __attribute__((unused)),
1633 void device_release_write(struct discover_device *dev __attribute__((unused)),
1634 bool release __attribute__((unused)))
1638 void device_sync_snapshots(
1639 struct device_handler *handler __attribute__((unused)),
1640 const char *device __attribute__((unused)))