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 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 status status;
425 status.type = STATUS_INFO;
426 status.message = talloc_asprintf(handler,
427 _("Booting in %d sec: %s"), sec, opt->option->name);
429 discover_server_notify_boot_status(handler->server, &status);
431 talloc_free(status.message);
434 static int default_timeout(void *arg)
436 struct device_handler *handler = arg;
437 struct discover_boot_option *opt;
439 if (!handler->default_boot_option)
442 if (handler->pending_boot)
445 opt = handler->default_boot_option;
447 if (handler->sec_to_boot) {
448 countdown_status(handler, opt, handler->sec_to_boot);
449 handler->sec_to_boot--;
450 handler->timeout_waiter = waiter_register_timeout(
451 handler->waitset, 1000,
452 default_timeout, handler);
456 handler->timeout_waiter = NULL;
458 pb_log("Timeout expired, booting default option %s\n", opt->option->id);
462 handler->pending_boot = boot(handler, handler->default_boot_option,
463 NULL, handler->dry_run, device_handler_boot_status,
465 handler->pending_boot_is_default = true;
470 enum ipmi_bootdev ipmi_type;
471 enum device_type device_type;
472 } device_type_map[] = {
473 { IPMI_BOOTDEV_NETWORK, DEVICE_TYPE_NETWORK },
474 { IPMI_BOOTDEV_DISK, DEVICE_TYPE_DISK },
475 { IPMI_BOOTDEV_DISK, DEVICE_TYPE_USB },
476 { IPMI_BOOTDEV_CDROM, DEVICE_TYPE_OPTICAL },
479 static bool ipmi_device_type_matches(enum ipmi_bootdev ipmi_type,
480 enum device_type device_type)
484 for (i = 0; i < ARRAY_SIZE(device_type_map); i++) {
485 if (device_type_map[i].device_type == device_type)
486 return device_type_map[i].ipmi_type == ipmi_type;
492 static int autoboot_option_priority(const struct config *config,
493 struct discover_boot_option *opt)
495 enum device_type type = opt->device->device->type;
496 const char *uuid = opt->device->uuid;
497 struct autoboot_option *auto_opt;
500 for (i = 0; i < config->n_autoboot_opts; i++) {
501 auto_opt = &config->autoboot_opts[i];
502 if (auto_opt->boot_type == BOOT_DEVICE_UUID)
503 if (!strcmp(auto_opt->uuid, uuid))
504 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
506 if (auto_opt->boot_type == BOOT_DEVICE_TYPE)
507 if (auto_opt->type == type ||
508 auto_opt->type == DEVICE_TYPE_ANY)
509 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
516 * We have different priorities to resolve conflicts between boot options that
517 * report to be the default for their device. This function assigns a priority
520 static enum default_priority default_option_priority(
521 struct discover_boot_option *opt)
523 const struct config *config;
525 config = config_get();
527 /* We give highest priority to IPMI-configured boot options. If
528 * we have an IPMI bootdev configuration set, then we don't allow
529 * any other defaults */
530 if (config->ipmi_bootdev) {
531 bool ipmi_match = ipmi_device_type_matches(config->ipmi_bootdev,
532 opt->device->device->type);
534 return DEFAULT_PRIORITY_REMOTE;
536 pb_debug("handler: disabled default priority due to "
537 "non-matching IPMI type %x\n",
538 config->ipmi_bootdev);
539 return DEFAULT_PRIORITY_DISABLED;
542 /* Next, try to match the option against the user-defined autoboot
543 * options, either by device UUID or type. */
544 if (config->n_autoboot_opts) {
545 int boot_match = autoboot_option_priority(config, opt);
550 /* If the option didn't match any entry in the array, it is disabled */
551 pb_debug("handler: disabled default priority due to "
552 "non-matching UUID or type\n");
553 return DEFAULT_PRIORITY_DISABLED;
556 static void set_default(struct device_handler *handler,
557 struct discover_boot_option *opt)
559 enum default_priority cur_prio, new_prio;
561 if (!handler->autoboot_enabled)
564 pb_debug("handler: new default option: %s\n", opt->option->id);
566 new_prio = default_option_priority(opt);
568 /* Anything outside our range prevents a default boot */
569 if (new_prio >= DEFAULT_PRIORITY_DISABLED)
572 pb_debug("handler: calculated priority %d\n", new_prio);
574 /* Resolve any conflicts: if we have a new default option, it only
575 * replaces the current if it has a higher priority. */
576 if (handler->default_boot_option) {
578 cur_prio = handler->default_boot_option_priority;
580 if (new_prio < cur_prio) {
581 pb_log("handler: new prio %d beats "
582 "old prio %d for %s\n",
584 handler->default_boot_option
586 handler->default_boot_option = opt;
587 handler->default_boot_option_priority = new_prio;
588 /* extend the timeout a little, so the user sees some
589 * indication of the change */
590 handler->sec_to_boot += 2;
596 handler->sec_to_boot = config_get()->autoboot_timeout_sec;
597 handler->default_boot_option = opt;
598 handler->default_boot_option_priority = new_prio;
600 pb_log("handler: boot option %s set as default, timeout %u sec.\n",
601 opt->option->id, handler->sec_to_boot);
603 default_timeout(handler);
606 static bool resource_is_resolved(struct resource *res)
608 return !res || res->resolved;
611 /* We only use this in an assert, which will disappear if we're compiling
612 * with NDEBUG, so we need the 'used' attribute for these builds */
613 static bool __attribute__((used)) boot_option_is_resolved(
614 struct discover_boot_option *opt)
616 return resource_is_resolved(opt->boot_image) &&
617 resource_is_resolved(opt->initrd) &&
618 resource_is_resolved(opt->dtb) &&
619 resource_is_resolved(opt->args_sig_file) &&
620 resource_is_resolved(opt->icon);
623 static bool resource_resolve(struct resource *res, const char *name,
624 struct discover_boot_option *opt,
625 struct device_handler *handler)
627 struct parser *parser = opt->source;
629 if (resource_is_resolved(res))
632 pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
633 opt->option->id, name, parser->name);
634 parser->resolve_resource(handler, res);
636 return res->resolved;
639 static bool boot_option_resolve(struct discover_boot_option *opt,
640 struct device_handler *handler)
642 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
643 resource_resolve(opt->initrd, "initrd", opt, handler) &&
644 resource_resolve(opt->dtb, "dtb", opt, handler) &&
645 resource_resolve(opt->args_sig_file, "args_sig_file", opt,
647 resource_resolve(opt->icon, "icon", opt, handler);
650 static void boot_option_finalise(struct device_handler *handler,
651 struct discover_boot_option *opt)
653 assert(boot_option_is_resolved(opt));
655 /* check that the parsers haven't set any of the final data */
656 assert(!opt->option->boot_image_file);
657 assert(!opt->option->initrd_file);
658 assert(!opt->option->dtb_file);
659 assert(!opt->option->icon_file);
660 assert(!opt->option->device_id);
661 assert(!opt->option->args_sig_file);
664 opt->option->boot_image_file = opt->boot_image->url->full;
666 opt->option->initrd_file = opt->initrd->url->full;
668 opt->option->dtb_file = opt->dtb->url->full;
670 opt->option->icon_file = opt->icon->url->full;
671 if (opt->args_sig_file)
672 opt->option->args_sig_file = opt->args_sig_file->url->full;
674 opt->option->device_id = opt->device->device->id;
676 if (opt->option->is_default)
677 set_default(handler, opt);
680 static void notify_boot_option(struct device_handler *handler,
681 struct discover_boot_option *opt)
683 struct discover_device *dev = opt->device;
686 discover_server_notify_device_add(handler->server,
687 opt->device->device);
688 dev->notified = true;
689 discover_server_notify_boot_option_add(handler->server, opt->option);
692 static void process_boot_option_queue(struct device_handler *handler)
694 struct discover_boot_option *opt, *tmp;
696 list_for_each_entry_safe(&handler->unresolved_boot_options,
699 pb_debug("queue: attempting resolution for %s\n",
702 if (!boot_option_resolve(opt, handler))
705 pb_debug("\tresolved!\n");
707 list_remove(&opt->list);
708 list_add_tail(&opt->device->boot_options, &opt->list);
709 talloc_steal(opt->device, opt);
710 boot_option_finalise(handler, opt);
711 notify_boot_option(handler, opt);
715 struct discover_context *device_handler_discover_context_create(
716 struct device_handler *handler,
717 struct discover_device *device)
719 struct discover_context *ctx;
721 ctx = talloc_zero(handler, struct discover_context);
722 ctx->device = device;
723 ctx->network = handler->network;
724 list_init(&ctx->boot_options);
729 void device_handler_add_device(struct device_handler *handler,
730 struct discover_device *device)
732 handler->n_devices++;
733 handler->devices = talloc_realloc(handler, handler->devices,
734 struct discover_device *, handler->n_devices);
735 handler->devices[handler->n_devices - 1] = device;
737 if (device->device->type == DEVICE_TYPE_NETWORK)
738 network_register_device(handler->network, device);
741 void device_handler_add_ramdisk(struct device_handler *handler,
744 struct ramdisk_device *dev;
750 for (i = 0; i < handler->n_ramdisks; i++)
751 if (!strcmp(handler->ramdisks[i]->path, path))
754 dev = talloc_zero(handler, struct ramdisk_device);
756 pb_log("Failed to allocate memory to track %s\n", path);
760 dev->path = talloc_strdup(handler, path);
762 handler->ramdisks = talloc_realloc(handler, handler->ramdisks,
763 struct ramdisk_device *,
764 handler->n_ramdisks + 1);
765 if (!handler->ramdisks) {
766 pb_log("Failed to reallocate memory"
767 "- ramdisk tracking inconsistent!\n");
771 handler->ramdisks[i] = dev;
772 i = handler->n_ramdisks++;
775 struct ramdisk_device *device_handler_get_ramdisk(
776 struct device_handler *handler)
782 /* Check if free ramdisk exists */
783 for (i = 0; i < handler->n_ramdisks; i++)
784 if (!handler->ramdisks[i]->snapshot &&
785 !handler->ramdisks[i]->origin &&
786 !handler->ramdisks[i]->base)
787 return handler->ramdisks[i];
789 /* Otherwise create a new one */
790 name = talloc_asprintf(handler, "/dev/ram%d",
791 handler->n_ramdisks);
793 pb_debug("Failed to allocate memory to name /dev/ram%d",
794 handler->n_ramdisks);
798 id = makedev(1, handler->n_ramdisks);
799 if (mknod(name, S_IFBLK, id)) {
800 if (errno == EEXIST) {
801 /* We haven't yet received updates for existing
802 * ramdisks - add and use this one */
803 pb_debug("Using untracked ramdisk %s\n", name);
805 pb_log("Failed to create new ramdisk %s: %s\n",
806 name, strerror(errno));
810 device_handler_add_ramdisk(handler, name);
813 return handler->ramdisks[i];
816 void device_handler_release_ramdisk(struct discover_device *device)
818 struct ramdisk_device *ramdisk = device->ramdisk;
820 talloc_free(ramdisk->snapshot);
821 talloc_free(ramdisk->origin);
822 talloc_free(ramdisk->base);
824 ramdisk->snapshot = ramdisk->origin = ramdisk->base = NULL;
825 ramdisk->sectors = 0;
827 device->ramdisk = NULL;
830 /* Start discovery on a hotplugged device. The device will be in our devices
831 * array, but has only just been initialised by the hotplug source.
833 int device_handler_discover(struct device_handler *handler,
834 struct discover_device *dev)
836 struct discover_context *ctx;
837 struct status *status;
840 status = talloc_zero(handler, struct status);
841 status->type = STATUS_INFO;
843 * TRANSLATORS: this string will be passed the type and identifier
844 * of the device. For example, the first parameter could be "Disk",
845 * (which will be translated accordingly) and the second a Linux device
846 * identifier like 'sda1' (which will not be translated)
848 status->message = talloc_asprintf(status, _("Processing %s device %s"),
849 device_type_display_name(dev->device->type),
851 device_handler_boot_status(handler, status);
853 process_boot_option_queue(handler);
855 /* create our context */
856 ctx = device_handler_discover_context_create(handler, dev);
858 rc = mount_device(dev);
862 /* add this device to our system info */
863 system_info_register_blockdev(dev->device->id, dev->uuid,
866 /* run the parsers. This will populate the ctx's boot_option list. */
867 iterate_parsers(ctx);
869 /* add discovered stuff to the handler */
870 device_handler_discover_context_commit(handler, ctx);
874 * TRANSLATORS: the format specifier in this string is a Linux
875 * device identifier, like 'sda1'
877 status->message = talloc_asprintf(status,_("Processing %s complete"),
879 device_handler_boot_status(handler, status);
882 talloc_unlink(handler, ctx);
887 /* Incoming dhcp event */
888 int device_handler_dhcp(struct device_handler *handler,
889 struct discover_device *dev, struct event *event)
891 struct discover_context *ctx;
892 struct status *status;
894 status = talloc_zero(handler, struct status);
895 status->type = STATUS_INFO;
897 * TRANSLATORS: this format specifier will be the name of a network
898 * device, like 'eth0'.
900 status->message = talloc_asprintf(status, _("Processing dhcp event on %s"),
902 device_handler_boot_status(handler, status);
904 /* create our context */
905 ctx = device_handler_discover_context_create(handler, dev);
906 talloc_steal(ctx, event);
909 iterate_parsers(ctx);
911 device_handler_discover_context_commit(handler, ctx);
914 * TRANSLATORS: this format specifier will be the name of a network
915 * device, like 'eth0'.
917 status->message = talloc_asprintf(status,_("Processing %s complete"),
919 device_handler_boot_status(handler, status);
922 talloc_unlink(handler, ctx);
927 static struct discover_boot_option *find_boot_option_by_id(
928 struct device_handler *handler, const char *id)
932 for (i = 0; i < handler->n_devices; i++) {
933 struct discover_device *dev = handler->devices[i];
934 struct discover_boot_option *opt;
936 list_for_each_entry(&dev->boot_options, opt, list)
937 if (!strcmp(opt->option->id, id))
944 void device_handler_boot(struct device_handler *handler,
945 struct boot_command *cmd)
947 struct discover_boot_option *opt = NULL;
949 if (cmd->option_id && strlen(cmd->option_id))
950 opt = find_boot_option_by_id(handler, cmd->option_id);
952 if (handler->pending_boot)
953 boot_cancel(handler->pending_boot);
957 handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
958 device_handler_boot_status, handler);
959 handler->pending_boot_is_default = false;
962 void device_handler_cancel_default(struct device_handler *handler)
964 struct status status;
966 if (handler->timeout_waiter)
967 waiter_remove(handler->timeout_waiter);
969 handler->timeout_waiter = NULL;
970 handler->autoboot_enabled = false;
972 /* we only send status if we had a default boot option queued */
973 if (!handler->default_boot_option)
976 pb_log("Cancelling default boot option\n");
978 if (handler->pending_boot && handler->pending_boot_is_default) {
979 boot_cancel(handler->pending_boot);
980 handler->pending_boot = NULL;
981 handler->pending_boot_is_default = false;
984 handler->default_boot_option = NULL;
986 status.type = STATUS_INFO;
987 status.message = _("Default boot cancelled");
989 discover_server_notify_boot_status(handler->server, &status);
992 void device_handler_update_config(struct device_handler *handler,
993 struct config *config)
997 rc = config_set(config);
1001 discover_server_notify_config(handler->server, config);
1002 device_handler_update_lang(config->lang);
1003 device_handler_reinit(handler);
1006 static char *device_from_addr(void *ctx, struct pb_url *url)
1008 char *ipaddr, *buf, *tok, *dev = NULL;
1009 const char *delim = " ";
1010 struct sockaddr_in *ip;
1011 struct sockaddr_in si;
1012 struct addrinfo *res;
1016 /* Note: IPv4 only */
1017 rc = inet_pton(AF_INET, url->host, &(si.sin_addr));
1021 /* need to turn hostname into a valid IP */
1022 rc = getaddrinfo(url->host, NULL, NULL, &res);
1024 pb_debug("%s: Invalid URL\n",__func__);
1027 ipaddr = talloc_array(ctx,char,INET_ADDRSTRLEN);
1028 ip = (struct sockaddr_in *) res->ai_addr;
1029 inet_ntop(AF_INET, &(ip->sin_addr), ipaddr, INET_ADDRSTRLEN);
1033 const char *argv[] = {
1035 "route", "show", "to", "match",
1040 p = process_create(ctx);
1042 p->path = pb_system_apps.ip;
1044 p->keep_stdout = true;
1046 rc = process_run_sync(p);
1048 if (rc || p->exit_status) {
1049 /* ip has complained for some reason; most likely
1050 * there is no route to the host - bail out */
1051 pb_debug("%s: `ip` returns non-zero exit status\n", __func__);
1052 pb_debug("ip buf: %s\n", p->stdout_buf);
1057 buf = p->stdout_buf;
1058 /* If a route is found, ip-route output will be of the form
1059 * "... dev DEVNAME ... " */
1060 tok = strtok(buf, delim);
1062 if (!strcmp(tok, "dev")) {
1063 tok = strtok(NULL, delim);
1064 dev = talloc_strdup(ctx, tok);
1067 tok = strtok(NULL, delim);
1072 pb_debug("%s: Found interface '%s'\n", __func__,dev);
1076 void device_handler_process_url(struct device_handler *handler,
1077 const char *url, const char *mac, const char *ip)
1079 struct discover_context *ctx;
1080 struct discover_device *dev;
1081 struct status *status;
1082 struct pb_url *pb_url;
1083 struct event *event;
1084 struct param *param;
1086 status = talloc(handler, struct status);
1087 status->type = STATUS_ERROR;
1089 if (!handler->network) {
1090 status->message = talloc_asprintf(handler,
1091 _("No network configured"));
1095 event = talloc(handler, struct event);
1096 event->type = EVENT_TYPE_USER;
1097 event->action = EVENT_ACTION_URL;
1099 if (url[strlen(url) - 1] == '/') {
1100 event->params = talloc_array(event, struct param, 3);
1101 param = &event->params[0];
1102 param->name = talloc_strdup(event, "pxepathprefix");
1103 param->value = talloc_strdup(event, url);
1104 param = &event->params[1];
1105 param->name = talloc_strdup(event, "mac");
1106 param->value = talloc_strdup(event, mac);
1107 param = &event->params[2];
1108 param->name = talloc_strdup(event, "ip");
1109 param->value = talloc_strdup(event, ip);
1110 event->n_params = 3;
1112 event->params = talloc_array(event, struct param, 1);
1113 param = &event->params[0];
1114 param->name = talloc_strdup(event, "pxeconffile");
1115 param->value = talloc_strdup(event, url);
1116 event->n_params = 1;
1119 pb_url = pb_url_parse(event, event->params->value);
1120 if (!pb_url || (pb_url->scheme != pb_url_file && !pb_url->host)) {
1121 status->message = talloc_asprintf(handler,
1122 _("Invalid config URL!"));
1126 if (pb_url->scheme == pb_url_file)
1127 event->device = talloc_asprintf(event, "local");
1129 event->device = device_from_addr(event, pb_url);
1131 if (!event->device) {
1132 status->message = talloc_asprintf(status,
1133 _("Unable to route to host %s"),
1138 dev = discover_device_create(handler, mac, event->device);
1139 if (pb_url->scheme == pb_url_file)
1140 dev->device->type = DEVICE_TYPE_ANY;
1141 ctx = device_handler_discover_context_create(handler, dev);
1142 talloc_steal(ctx, event);
1145 iterate_parsers(ctx);
1147 device_handler_discover_context_commit(handler, ctx);
1149 talloc_unlink(handler, ctx);
1151 status->type = STATUS_INFO;
1152 status->message = talloc_asprintf(status, _("Config file %s parsed"),
1155 device_handler_boot_status(handler, status);
1156 talloc_free(status);
1159 #ifndef PETITBOOT_TEST
1162 * context_commit - Commit a temporary discovery context to the handler,
1163 * and notify the clients about any new options / devices
1165 void device_handler_discover_context_commit(struct device_handler *handler,
1166 struct discover_context *ctx)
1168 struct discover_device *dev = ctx->device;
1169 struct discover_boot_option *opt, *tmp;
1171 if (!device_lookup_by_uuid(handler, dev->uuid))
1172 device_handler_add_device(handler, dev);
1174 /* move boot options from the context to the device */
1175 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
1176 list_remove(&opt->list);
1178 /* All boot options need at least a kernel image */
1179 if (!opt->boot_image || !opt->boot_image->url) {
1180 pb_log("boot option %s is missing boot image, ignoring\n",
1186 if (boot_option_resolve(opt, handler)) {
1187 pb_log("boot option %s is resolved, "
1188 "sending to clients\n",
1190 list_add_tail(&dev->boot_options, &opt->list);
1191 talloc_steal(dev, opt);
1192 boot_option_finalise(handler, opt);
1193 notify_boot_option(handler, opt);
1195 if (!opt->source->resolve_resource) {
1196 pb_log("parser %s gave us an unresolved "
1197 "resource (%s), but no way to "
1199 opt->source->name, opt->option->id);
1202 pb_log("boot option %s is unresolved, "
1203 "adding to queue\n",
1205 list_add(&handler->unresolved_boot_options,
1207 talloc_steal(handler, opt);
1213 static void device_handler_update_lang(const char *lang)
1215 const char *cur_lang;
1220 cur_lang = setlocale(LC_ALL, NULL);
1221 if (cur_lang && !strcmp(cur_lang, lang))
1224 setlocale(LC_ALL, lang);
1227 static int device_handler_init_sources(struct device_handler *handler)
1229 /* init our device sources: udev, network and user events */
1230 handler->udev = udev_init(handler, handler->waitset);
1234 handler->network = network_init(handler, handler->waitset,
1236 if (!handler->network)
1239 handler->user_event = user_event_init(handler, handler->waitset);
1240 if (!handler->user_event)
1246 static void device_handler_reinit_sources(struct device_handler *handler)
1248 /* if we haven't initialised sources previously (becuase we started in
1249 * safe mode), then init once here. */
1250 if (!(handler->udev || handler->network || handler->user_event)) {
1251 device_handler_init_sources(handler);
1255 udev_reinit(handler->udev);
1257 network_shutdown(handler->network);
1258 handler->network = network_init(handler, handler->waitset,
1262 static inline const char *get_device_path(struct discover_device *dev)
1264 return dev->ramdisk ? dev->ramdisk->snapshot : dev->device_path;
1267 static char *check_subvols(struct discover_device *dev)
1269 const char *fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1274 if (strncmp(fstype, "btrfs", strlen("btrfs")))
1275 return dev->mount_path;
1277 /* On btrfs a device's root may be under a subvolume path */
1278 path = join_paths(dev, dev->mount_path, "@");
1279 rc = stat(path, &sb);
1280 if (!rc && S_ISDIR(sb.st_mode)) {
1281 pb_debug("Using '%s' for btrfs root path\n", path);
1286 return dev->mount_path;
1289 static bool check_existing_mount(struct discover_device *dev)
1291 struct stat devstat, mntstat;
1292 const char *device_path;
1297 device_path = get_device_path(dev);
1299 rc = stat(device_path, &devstat);
1301 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
1305 if (!S_ISBLK(devstat.st_mode)) {
1306 pb_debug("%s: %s isn't a block device?\n", __func__,
1311 fp = fopen("/proc/self/mounts", "r");
1314 mnt = getmntent(fp);
1318 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
1321 rc = stat(mnt->mnt_fsname, &mntstat);
1325 if (!S_ISBLK(mntstat.st_mode))
1328 if (mntstat.st_rdev == devstat.st_rdev) {
1329 dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
1330 dev->root_path = check_subvols(dev);
1331 dev->mounted_rw = !!hasmntopt(mnt, "rw");
1332 dev->mounted = true;
1333 dev->unmount = false;
1335 pb_debug("%s: %s is already mounted (r%c) at %s\n",
1336 __func__, dev->device_path,
1337 dev->mounted_rw ? 'w' : 'o',
1349 * Attempt to mount a filesystem safely, while handling certain filesytem-
1352 static int try_mount(const char *device_path, const char *mount_path,
1353 const char *fstype, unsigned long flags,
1356 const char *fs, *safe_opts;
1359 /* Mount ext3 as ext4 instead so 'norecovery' can be used */
1360 if (strncmp(fstype, "ext3", strlen("ext3")) == 0) {
1361 pb_debug("Mounting ext3 filesystem as ext4\n");
1366 if (strncmp(fs, "xfs", strlen("xfs")) == 0 ||
1367 strncmp(fs, "ext4", strlen("ext4")) == 0)
1368 safe_opts = "norecovery";
1373 /* If no snapshot is available don't attempt recovery */
1375 return mount(device_path, mount_path, fs, flags, safe_opts);
1377 rc = mount(device_path, mount_path, fs, flags, NULL);
1382 /* Mounting failed; some filesystems will fail to mount if a recovery
1383 * journal exists (eg. cross-endian XFS), so try again with norecovery
1384 * where that option is available.
1385 * If mounting read-write just return the error as norecovery is not a
1387 if ((flags & MS_RDONLY) != MS_RDONLY || !safe_opts)
1391 return mount(device_path, mount_path, fs, flags, safe_opts);
1394 static int mount_device(struct discover_device *dev)
1396 const char *fstype, *device_path;
1399 if (!dev->device_path)
1405 if (check_existing_mount(dev))
1408 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1412 dev->mount_path = join_paths(dev, mount_base(),
1415 if (pb_mkdir_recursive(dev->mount_path)) {
1416 pb_log("couldn't create mount directory %s: %s\n",
1417 dev->mount_path, strerror(errno));
1421 device_path = get_device_path(dev);
1423 pb_log("mounting device %s read-only\n", dev->device_path);
1424 rc = try_mount(device_path, dev->mount_path, fstype,
1425 MS_RDONLY | MS_SILENT, dev->ramdisk);
1428 dev->mounted = true;
1429 dev->mounted_rw = false;
1430 dev->unmount = true;
1431 dev->root_path = check_subvols(dev);
1435 pb_log("couldn't mount device %s: mount failed: %s\n",
1436 device_path, strerror(errno));
1438 /* If mount fails clean up any snapshot */
1439 devmapper_destroy_snapshot(dev);
1441 pb_rmdir_recursive(mount_base(), dev->mount_path);
1443 talloc_free(dev->mount_path);
1444 dev->mount_path = NULL;
1448 static int umount_device(struct discover_device *dev)
1450 const char *device_path;
1453 if (!dev->mounted || !dev->unmount)
1456 device_path = get_device_path(dev);
1458 pb_log("unmounting device %s\n", device_path);
1459 rc = umount(dev->mount_path);
1463 dev->mounted = false;
1464 devmapper_destroy_snapshot(dev);
1466 pb_rmdir_recursive(mount_base(), dev->mount_path);
1468 talloc_free(dev->mount_path);
1469 dev->mount_path = NULL;
1470 dev->root_path = NULL;
1475 int device_request_write(struct discover_device *dev, bool *release)
1477 const char *fstype, *device_path;
1478 const struct config *config;
1483 config = config_get();
1484 if (!config->allow_writes)
1490 if (dev->mounted_rw)
1493 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1495 device_path = get_device_path(dev);
1497 pb_log("remounting device %s read-write\n", device_path);
1499 rc = umount(dev->mount_path);
1501 pb_log("Failed to unmount %s: %s\n",
1502 dev->mount_path, strerror(errno));
1506 rc = try_mount(device_path, dev->mount_path, fstype,
1507 MS_SILENT, dev->ramdisk);
1511 dev->mounted_rw = true;
1516 pb_log("Unable to remount device %s read-write: %s\n",
1517 device_path, strerror(errno));
1518 rc = try_mount(device_path, dev->mount_path, fstype,
1519 MS_RDONLY | MS_SILENT, dev->ramdisk);
1521 pb_log("Unable to recover mount for %s: %s\n",
1522 device_path, strerror(errno));
1526 void device_release_write(struct discover_device *dev, bool release)
1528 const char *fstype, *device_path;
1533 device_path = get_device_path(dev);
1535 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1537 pb_log("remounting device %s read-only\n", device_path);
1539 if (umount(dev->mount_path)) {
1540 pb_log("Failed to unmount %s\n", dev->mount_path);
1543 dev->mounted_rw = dev->mounted = false;
1546 devmapper_merge_snapshot(dev);
1547 /* device_path becomes stale after merge */
1548 device_path = get_device_path(dev);
1551 if (try_mount(device_path, dev->mount_path, fstype,
1552 MS_RDONLY | MS_SILENT, dev->ramdisk))
1553 pb_log("Failed to remount %s read-only: %s\n",
1554 device_path, strerror(errno));
1556 dev->mounted = true;
1559 void device_sync_snapshots(struct device_handler *handler, const char *device)
1561 struct discover_device *dev = NULL;
1565 /* Find matching device and sync */
1566 dev = device_lookup_by_name(handler, device);
1568 pb_log("%s: device name '%s' unrecognised\n",
1573 device_release_write(dev, true);
1575 pb_log("%s has no snapshot to merge, skipping\n",
1580 /* Otherwise sync all relevant devices */
1581 for (i = 0; i < handler->n_devices; i++) {
1582 dev = handler->devices[i];
1583 if (dev->device->type != DEVICE_TYPE_DISK &&
1584 dev->device->type != DEVICE_TYPE_USB)
1587 device_release_write(dev, true);
1589 pb_log("%s has no snapshot to merge, skipping\n",
1596 void device_handler_discover_context_commit(
1597 struct device_handler *handler __attribute__((unused)),
1598 struct discover_context *ctx __attribute__((unused)))
1600 pb_log("%s stubbed out for test cases\n", __func__);
1603 static void device_handler_update_lang(const char *lang __attribute__((unused)))
1607 static int device_handler_init_sources(
1608 struct device_handler *handler __attribute__((unused)))
1613 static void device_handler_reinit_sources(
1614 struct device_handler *handler __attribute__((unused)))
1618 static int umount_device(struct discover_device *dev __attribute__((unused)))
1623 static int __attribute__((unused)) mount_device(
1624 struct discover_device *dev __attribute__((unused)))
1629 int device_request_write(struct discover_device *dev __attribute__((unused)),
1636 void device_release_write(struct discover_device *dev __attribute__((unused)),
1637 bool release __attribute__((unused)))
1641 void device_sync_snapshots(
1642 struct device_handler *handler __attribute__((unused)),
1643 const char *device __attribute__((unused)))