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_status(struct device_handler *handler,
414 struct status *status)
416 discover_server_notify_boot_status(handler->server, status);
419 static void device_handler_boot_status_cb(void *arg, struct status *status)
421 device_handler_status(arg, status);
424 static void countdown_status(struct device_handler *handler,
425 struct discover_boot_option *opt, unsigned int sec)
427 struct status status;
429 status.type = STATUS_INFO;
430 status.message = talloc_asprintf(handler,
431 _("Booting in %d sec: %s"), sec, opt->option->name);
433 device_handler_status(handler, &status);
435 talloc_free(status.message);
438 static int default_timeout(void *arg)
440 struct device_handler *handler = arg;
441 struct discover_boot_option *opt;
443 if (!handler->default_boot_option)
446 if (handler->pending_boot)
449 opt = handler->default_boot_option;
451 if (handler->sec_to_boot) {
452 countdown_status(handler, opt, handler->sec_to_boot);
453 handler->sec_to_boot--;
454 handler->timeout_waiter = waiter_register_timeout(
455 handler->waitset, 1000,
456 default_timeout, handler);
460 handler->timeout_waiter = NULL;
462 pb_log("Timeout expired, booting default option %s\n", opt->option->id);
466 handler->pending_boot = boot(handler, handler->default_boot_option,
467 NULL, handler->dry_run, device_handler_boot_status_cb,
469 handler->pending_boot_is_default = true;
474 enum ipmi_bootdev ipmi_type;
475 enum device_type device_type;
476 } device_type_map[] = {
477 { IPMI_BOOTDEV_NETWORK, DEVICE_TYPE_NETWORK },
478 { IPMI_BOOTDEV_DISK, DEVICE_TYPE_DISK },
479 { IPMI_BOOTDEV_DISK, DEVICE_TYPE_USB },
480 { IPMI_BOOTDEV_CDROM, DEVICE_TYPE_OPTICAL },
483 static bool ipmi_device_type_matches(enum ipmi_bootdev ipmi_type,
484 enum device_type device_type)
488 for (i = 0; i < ARRAY_SIZE(device_type_map); i++) {
489 if (device_type_map[i].device_type == device_type)
490 return device_type_map[i].ipmi_type == ipmi_type;
496 static int autoboot_option_priority(const struct config *config,
497 struct discover_boot_option *opt)
499 enum device_type type = opt->device->device->type;
500 const char *uuid = opt->device->uuid;
501 struct autoboot_option *auto_opt;
504 for (i = 0; i < config->n_autoboot_opts; i++) {
505 auto_opt = &config->autoboot_opts[i];
506 if (auto_opt->boot_type == BOOT_DEVICE_UUID)
507 if (!strcmp(auto_opt->uuid, uuid))
508 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
510 if (auto_opt->boot_type == BOOT_DEVICE_TYPE)
511 if (auto_opt->type == type ||
512 auto_opt->type == DEVICE_TYPE_ANY)
513 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
520 * We have different priorities to resolve conflicts between boot options that
521 * report to be the default for their device. This function assigns a priority
524 static enum default_priority default_option_priority(
525 struct discover_boot_option *opt)
527 const struct config *config;
529 config = config_get();
531 /* We give highest priority to IPMI-configured boot options. If
532 * we have an IPMI bootdev configuration set, then we don't allow
533 * any other defaults */
534 if (config->ipmi_bootdev) {
535 bool ipmi_match = ipmi_device_type_matches(config->ipmi_bootdev,
536 opt->device->device->type);
538 return DEFAULT_PRIORITY_REMOTE;
540 pb_debug("handler: disabled default priority due to "
541 "non-matching IPMI type %x\n",
542 config->ipmi_bootdev);
543 return DEFAULT_PRIORITY_DISABLED;
546 /* Next, try to match the option against the user-defined autoboot
547 * options, either by device UUID or type. */
548 if (config->n_autoboot_opts) {
549 int boot_match = autoboot_option_priority(config, opt);
554 /* If the option didn't match any entry in the array, it is disabled */
555 pb_debug("handler: disabled default priority due to "
556 "non-matching UUID or type\n");
557 return DEFAULT_PRIORITY_DISABLED;
560 static void set_default(struct device_handler *handler,
561 struct discover_boot_option *opt)
563 enum default_priority cur_prio, new_prio;
565 if (!handler->autoboot_enabled)
568 pb_debug("handler: new default option: %s\n", opt->option->id);
570 new_prio = default_option_priority(opt);
572 /* Anything outside our range prevents a default boot */
573 if (new_prio >= DEFAULT_PRIORITY_DISABLED)
576 pb_debug("handler: calculated priority %d\n", new_prio);
578 /* Resolve any conflicts: if we have a new default option, it only
579 * replaces the current if it has a higher priority. */
580 if (handler->default_boot_option) {
582 cur_prio = handler->default_boot_option_priority;
584 if (new_prio < cur_prio) {
585 pb_log("handler: new prio %d beats "
586 "old prio %d for %s\n",
588 handler->default_boot_option
590 handler->default_boot_option = opt;
591 handler->default_boot_option_priority = new_prio;
592 /* extend the timeout a little, so the user sees some
593 * indication of the change */
594 handler->sec_to_boot += 2;
600 handler->sec_to_boot = config_get()->autoboot_timeout_sec;
601 handler->default_boot_option = opt;
602 handler->default_boot_option_priority = new_prio;
604 pb_log("handler: boot option %s set as default, timeout %u sec.\n",
605 opt->option->id, handler->sec_to_boot);
607 default_timeout(handler);
610 static bool resource_is_resolved(struct resource *res)
612 return !res || res->resolved;
615 /* We only use this in an assert, which will disappear if we're compiling
616 * with NDEBUG, so we need the 'used' attribute for these builds */
617 static bool __attribute__((used)) boot_option_is_resolved(
618 struct discover_boot_option *opt)
620 return resource_is_resolved(opt->boot_image) &&
621 resource_is_resolved(opt->initrd) &&
622 resource_is_resolved(opt->dtb) &&
623 resource_is_resolved(opt->args_sig_file) &&
624 resource_is_resolved(opt->icon);
627 static bool resource_resolve(struct resource *res, const char *name,
628 struct discover_boot_option *opt,
629 struct device_handler *handler)
631 struct parser *parser = opt->source;
633 if (resource_is_resolved(res))
636 pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
637 opt->option->id, name, parser->name);
638 parser->resolve_resource(handler, res);
640 return res->resolved;
643 static bool boot_option_resolve(struct discover_boot_option *opt,
644 struct device_handler *handler)
646 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
647 resource_resolve(opt->initrd, "initrd", opt, handler) &&
648 resource_resolve(opt->dtb, "dtb", opt, handler) &&
649 resource_resolve(opt->args_sig_file, "args_sig_file", opt,
651 resource_resolve(opt->icon, "icon", opt, handler);
654 static void boot_option_finalise(struct device_handler *handler,
655 struct discover_boot_option *opt)
657 assert(boot_option_is_resolved(opt));
659 /* check that the parsers haven't set any of the final data */
660 assert(!opt->option->boot_image_file);
661 assert(!opt->option->initrd_file);
662 assert(!opt->option->dtb_file);
663 assert(!opt->option->icon_file);
664 assert(!opt->option->device_id);
665 assert(!opt->option->args_sig_file);
668 opt->option->boot_image_file = opt->boot_image->url->full;
670 opt->option->initrd_file = opt->initrd->url->full;
672 opt->option->dtb_file = opt->dtb->url->full;
674 opt->option->icon_file = opt->icon->url->full;
675 if (opt->args_sig_file)
676 opt->option->args_sig_file = opt->args_sig_file->url->full;
678 opt->option->device_id = opt->device->device->id;
680 if (opt->option->is_default)
681 set_default(handler, opt);
684 static void notify_boot_option(struct device_handler *handler,
685 struct discover_boot_option *opt)
687 struct discover_device *dev = opt->device;
690 discover_server_notify_device_add(handler->server,
691 opt->device->device);
692 dev->notified = true;
693 discover_server_notify_boot_option_add(handler->server, opt->option);
696 static void process_boot_option_queue(struct device_handler *handler)
698 struct discover_boot_option *opt, *tmp;
700 list_for_each_entry_safe(&handler->unresolved_boot_options,
703 pb_debug("queue: attempting resolution for %s\n",
706 if (!boot_option_resolve(opt, handler))
709 pb_debug("\tresolved!\n");
711 list_remove(&opt->list);
712 list_add_tail(&opt->device->boot_options, &opt->list);
713 talloc_steal(opt->device, opt);
714 boot_option_finalise(handler, opt);
715 notify_boot_option(handler, opt);
719 struct discover_context *device_handler_discover_context_create(
720 struct device_handler *handler,
721 struct discover_device *device)
723 struct discover_context *ctx;
725 ctx = talloc_zero(handler, struct discover_context);
726 ctx->device = device;
727 ctx->network = handler->network;
728 list_init(&ctx->boot_options);
733 void device_handler_add_device(struct device_handler *handler,
734 struct discover_device *device)
736 handler->n_devices++;
737 handler->devices = talloc_realloc(handler, handler->devices,
738 struct discover_device *, handler->n_devices);
739 handler->devices[handler->n_devices - 1] = device;
741 if (device->device->type == DEVICE_TYPE_NETWORK)
742 network_register_device(handler->network, device);
745 void device_handler_add_ramdisk(struct device_handler *handler,
748 struct ramdisk_device *dev;
754 for (i = 0; i < handler->n_ramdisks; i++)
755 if (!strcmp(handler->ramdisks[i]->path, path))
758 dev = talloc_zero(handler, struct ramdisk_device);
760 pb_log("Failed to allocate memory to track %s\n", path);
764 dev->path = talloc_strdup(handler, path);
766 handler->ramdisks = talloc_realloc(handler, handler->ramdisks,
767 struct ramdisk_device *,
768 handler->n_ramdisks + 1);
769 if (!handler->ramdisks) {
770 pb_log("Failed to reallocate memory"
771 "- ramdisk tracking inconsistent!\n");
775 handler->ramdisks[i] = dev;
776 i = handler->n_ramdisks++;
779 struct ramdisk_device *device_handler_get_ramdisk(
780 struct device_handler *handler)
786 /* Check if free ramdisk exists */
787 for (i = 0; i < handler->n_ramdisks; i++)
788 if (!handler->ramdisks[i]->snapshot &&
789 !handler->ramdisks[i]->origin &&
790 !handler->ramdisks[i]->base)
791 return handler->ramdisks[i];
793 /* Otherwise create a new one */
794 name = talloc_asprintf(handler, "/dev/ram%d",
795 handler->n_ramdisks);
797 pb_debug("Failed to allocate memory to name /dev/ram%d",
798 handler->n_ramdisks);
802 id = makedev(1, handler->n_ramdisks);
803 if (mknod(name, S_IFBLK, id)) {
804 if (errno == EEXIST) {
805 /* We haven't yet received updates for existing
806 * ramdisks - add and use this one */
807 pb_debug("Using untracked ramdisk %s\n", name);
809 pb_log("Failed to create new ramdisk %s: %s\n",
810 name, strerror(errno));
814 device_handler_add_ramdisk(handler, name);
817 return handler->ramdisks[i];
820 void device_handler_release_ramdisk(struct discover_device *device)
822 struct ramdisk_device *ramdisk = device->ramdisk;
824 talloc_free(ramdisk->snapshot);
825 talloc_free(ramdisk->origin);
826 talloc_free(ramdisk->base);
828 ramdisk->snapshot = ramdisk->origin = ramdisk->base = NULL;
829 ramdisk->sectors = 0;
831 device->ramdisk = NULL;
834 /* Start discovery on a hotplugged device. The device will be in our devices
835 * array, but has only just been initialised by the hotplug source.
837 int device_handler_discover(struct device_handler *handler,
838 struct discover_device *dev)
840 struct discover_context *ctx;
841 struct status *status;
844 status = talloc_zero(handler, struct status);
845 status->type = STATUS_INFO;
847 * TRANSLATORS: this string will be passed the type and identifier
848 * of the device. For example, the first parameter could be "Disk",
849 * (which will be translated accordingly) and the second a Linux device
850 * identifier like 'sda1' (which will not be translated)
852 status->message = talloc_asprintf(status, _("Processing %s device %s"),
853 device_type_display_name(dev->device->type),
855 device_handler_status(handler, status);
857 process_boot_option_queue(handler);
859 /* create our context */
860 ctx = device_handler_discover_context_create(handler, dev);
862 rc = mount_device(dev);
866 /* add this device to our system info */
867 system_info_register_blockdev(dev->device->id, dev->uuid,
870 /* run the parsers. This will populate the ctx's boot_option list. */
871 iterate_parsers(ctx);
873 /* add discovered stuff to the handler */
874 device_handler_discover_context_commit(handler, ctx);
878 * TRANSLATORS: the format specifier in this string is a Linux
879 * device identifier, like 'sda1'
881 status->message = talloc_asprintf(status,_("Processing %s complete"),
883 device_handler_status(handler, status);
886 talloc_unlink(handler, ctx);
891 /* Incoming dhcp event */
892 int device_handler_dhcp(struct device_handler *handler,
893 struct discover_device *dev, struct event *event)
895 struct discover_context *ctx;
896 struct status *status;
898 status = talloc_zero(handler, struct status);
899 status->type = STATUS_INFO;
901 * TRANSLATORS: this format specifier will be the name of a network
902 * device, like 'eth0'.
904 status->message = talloc_asprintf(status, _("Processing dhcp event on %s"),
906 device_handler_status(handler, status);
908 /* create our context */
909 ctx = device_handler_discover_context_create(handler, dev);
910 talloc_steal(ctx, event);
913 iterate_parsers(ctx);
915 device_handler_discover_context_commit(handler, ctx);
918 * TRANSLATORS: this format specifier will be the name of a network
919 * device, like 'eth0'.
921 status->message = talloc_asprintf(status,_("Processing %s complete"),
923 device_handler_status(handler, status);
926 talloc_unlink(handler, ctx);
931 static struct discover_boot_option *find_boot_option_by_id(
932 struct device_handler *handler, const char *id)
936 for (i = 0; i < handler->n_devices; i++) {
937 struct discover_device *dev = handler->devices[i];
938 struct discover_boot_option *opt;
940 list_for_each_entry(&dev->boot_options, opt, list)
941 if (!strcmp(opt->option->id, id))
948 void device_handler_boot(struct device_handler *handler,
949 struct boot_command *cmd)
951 struct discover_boot_option *opt = NULL;
953 if (cmd->option_id && strlen(cmd->option_id))
954 opt = find_boot_option_by_id(handler, cmd->option_id);
956 if (handler->pending_boot)
957 boot_cancel(handler->pending_boot);
961 handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
962 device_handler_boot_status_cb, handler);
963 handler->pending_boot_is_default = false;
966 void device_handler_cancel_default(struct device_handler *handler)
968 struct status status;
970 if (handler->timeout_waiter)
971 waiter_remove(handler->timeout_waiter);
973 handler->timeout_waiter = NULL;
974 handler->autoboot_enabled = false;
976 /* we only send status if we had a default boot option queued */
977 if (!handler->default_boot_option)
980 pb_log("Cancelling default boot option\n");
982 if (handler->pending_boot && handler->pending_boot_is_default) {
983 boot_cancel(handler->pending_boot);
984 handler->pending_boot = NULL;
985 handler->pending_boot_is_default = false;
988 handler->default_boot_option = NULL;
990 status.type = STATUS_INFO;
991 status.message = _("Default boot cancelled");
993 device_handler_status(handler, &status);
996 void device_handler_update_config(struct device_handler *handler,
997 struct config *config)
1001 rc = config_set(config);
1005 discover_server_notify_config(handler->server, config);
1006 device_handler_update_lang(config->lang);
1007 device_handler_reinit(handler);
1010 static char *device_from_addr(void *ctx, struct pb_url *url)
1012 char *ipaddr, *buf, *tok, *dev = NULL;
1013 const char *delim = " ";
1014 struct sockaddr_in *ip;
1015 struct sockaddr_in si;
1016 struct addrinfo *res;
1020 /* Note: IPv4 only */
1021 rc = inet_pton(AF_INET, url->host, &(si.sin_addr));
1025 /* need to turn hostname into a valid IP */
1026 rc = getaddrinfo(url->host, NULL, NULL, &res);
1028 pb_debug("%s: Invalid URL\n",__func__);
1031 ipaddr = talloc_array(ctx,char,INET_ADDRSTRLEN);
1032 ip = (struct sockaddr_in *) res->ai_addr;
1033 inet_ntop(AF_INET, &(ip->sin_addr), ipaddr, INET_ADDRSTRLEN);
1037 const char *argv[] = {
1039 "route", "show", "to", "match",
1044 p = process_create(ctx);
1046 p->path = pb_system_apps.ip;
1048 p->keep_stdout = true;
1050 rc = process_run_sync(p);
1052 if (rc || p->exit_status) {
1053 /* ip has complained for some reason; most likely
1054 * there is no route to the host - bail out */
1055 pb_debug("%s: `ip` returns non-zero exit status\n", __func__);
1056 pb_debug("ip buf: %s\n", p->stdout_buf);
1061 buf = p->stdout_buf;
1062 /* If a route is found, ip-route output will be of the form
1063 * "... dev DEVNAME ... " */
1064 tok = strtok(buf, delim);
1066 if (!strcmp(tok, "dev")) {
1067 tok = strtok(NULL, delim);
1068 dev = talloc_strdup(ctx, tok);
1071 tok = strtok(NULL, delim);
1076 pb_debug("%s: Found interface '%s'\n", __func__,dev);
1080 void device_handler_process_url(struct device_handler *handler,
1081 const char *url, const char *mac, const char *ip)
1083 struct discover_context *ctx;
1084 struct discover_device *dev;
1085 struct status *status;
1086 struct pb_url *pb_url;
1087 struct event *event;
1088 struct param *param;
1090 status = talloc(handler, struct status);
1091 status->type = STATUS_ERROR;
1093 if (!handler->network) {
1094 status->message = talloc_asprintf(handler,
1095 _("No network configured"));
1099 event = talloc(handler, struct event);
1100 event->type = EVENT_TYPE_USER;
1101 event->action = EVENT_ACTION_URL;
1103 if (url[strlen(url) - 1] == '/') {
1104 event->params = talloc_array(event, struct param, 3);
1105 param = &event->params[0];
1106 param->name = talloc_strdup(event, "pxepathprefix");
1107 param->value = talloc_strdup(event, url);
1108 param = &event->params[1];
1109 param->name = talloc_strdup(event, "mac");
1110 param->value = talloc_strdup(event, mac);
1111 param = &event->params[2];
1112 param->name = talloc_strdup(event, "ip");
1113 param->value = talloc_strdup(event, ip);
1114 event->n_params = 3;
1116 event->params = talloc_array(event, struct param, 1);
1117 param = &event->params[0];
1118 param->name = talloc_strdup(event, "pxeconffile");
1119 param->value = talloc_strdup(event, url);
1120 event->n_params = 1;
1123 pb_url = pb_url_parse(event, event->params->value);
1124 if (!pb_url || (pb_url->scheme != pb_url_file && !pb_url->host)) {
1125 status->message = talloc_asprintf(handler,
1126 _("Invalid config URL!"));
1130 if (pb_url->scheme == pb_url_file)
1131 event->device = talloc_asprintf(event, "local");
1133 event->device = device_from_addr(event, pb_url);
1135 if (!event->device) {
1136 status->message = talloc_asprintf(status,
1137 _("Unable to route to host %s"),
1142 dev = discover_device_create(handler, mac, event->device);
1143 if (pb_url->scheme == pb_url_file)
1144 dev->device->type = DEVICE_TYPE_ANY;
1145 ctx = device_handler_discover_context_create(handler, dev);
1146 talloc_steal(ctx, event);
1149 iterate_parsers(ctx);
1151 device_handler_discover_context_commit(handler, ctx);
1153 talloc_unlink(handler, ctx);
1155 status->type = STATUS_INFO;
1156 status->message = talloc_asprintf(status, _("Config file %s parsed"),
1159 device_handler_status(handler, status);
1160 talloc_free(status);
1163 #ifndef PETITBOOT_TEST
1166 * context_commit - Commit a temporary discovery context to the handler,
1167 * and notify the clients about any new options / devices
1169 void device_handler_discover_context_commit(struct device_handler *handler,
1170 struct discover_context *ctx)
1172 struct discover_device *dev = ctx->device;
1173 struct discover_boot_option *opt, *tmp;
1175 if (!device_lookup_by_uuid(handler, dev->uuid))
1176 device_handler_add_device(handler, dev);
1178 /* move boot options from the context to the device */
1179 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
1180 list_remove(&opt->list);
1182 /* All boot options need at least a kernel image */
1183 if (!opt->boot_image || !opt->boot_image->url) {
1184 pb_log("boot option %s is missing boot image, ignoring\n",
1190 if (boot_option_resolve(opt, handler)) {
1191 pb_log("boot option %s is resolved, "
1192 "sending to clients\n",
1194 list_add_tail(&dev->boot_options, &opt->list);
1195 talloc_steal(dev, opt);
1196 boot_option_finalise(handler, opt);
1197 notify_boot_option(handler, opt);
1199 if (!opt->source->resolve_resource) {
1200 pb_log("parser %s gave us an unresolved "
1201 "resource (%s), but no way to "
1203 opt->source->name, opt->option->id);
1206 pb_log("boot option %s is unresolved, "
1207 "adding to queue\n",
1209 list_add(&handler->unresolved_boot_options,
1211 talloc_steal(handler, opt);
1217 static void device_handler_update_lang(const char *lang)
1219 const char *cur_lang;
1224 cur_lang = setlocale(LC_ALL, NULL);
1225 if (cur_lang && !strcmp(cur_lang, lang))
1228 setlocale(LC_ALL, lang);
1231 static int device_handler_init_sources(struct device_handler *handler)
1233 /* init our device sources: udev, network and user events */
1234 handler->udev = udev_init(handler, handler->waitset);
1238 handler->network = network_init(handler, handler->waitset,
1240 if (!handler->network)
1243 handler->user_event = user_event_init(handler, handler->waitset);
1244 if (!handler->user_event)
1250 static void device_handler_reinit_sources(struct device_handler *handler)
1252 /* if we haven't initialised sources previously (becuase we started in
1253 * safe mode), then init once here. */
1254 if (!(handler->udev || handler->network || handler->user_event)) {
1255 device_handler_init_sources(handler);
1259 udev_reinit(handler->udev);
1261 network_shutdown(handler->network);
1262 handler->network = network_init(handler, handler->waitset,
1266 static inline const char *get_device_path(struct discover_device *dev)
1268 return dev->ramdisk ? dev->ramdisk->snapshot : dev->device_path;
1271 static char *check_subvols(struct discover_device *dev)
1273 const char *fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1278 if (strncmp(fstype, "btrfs", strlen("btrfs")))
1279 return dev->mount_path;
1281 /* On btrfs a device's root may be under a subvolume path */
1282 path = join_paths(dev, dev->mount_path, "@");
1283 rc = stat(path, &sb);
1284 if (!rc && S_ISDIR(sb.st_mode)) {
1285 pb_debug("Using '%s' for btrfs root path\n", path);
1290 return dev->mount_path;
1293 static bool check_existing_mount(struct discover_device *dev)
1295 struct stat devstat, mntstat;
1296 const char *device_path;
1301 device_path = get_device_path(dev);
1303 rc = stat(device_path, &devstat);
1305 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
1309 if (!S_ISBLK(devstat.st_mode)) {
1310 pb_debug("%s: %s isn't a block device?\n", __func__,
1315 fp = fopen("/proc/self/mounts", "r");
1318 mnt = getmntent(fp);
1322 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
1325 rc = stat(mnt->mnt_fsname, &mntstat);
1329 if (!S_ISBLK(mntstat.st_mode))
1332 if (mntstat.st_rdev == devstat.st_rdev) {
1333 dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
1334 dev->root_path = check_subvols(dev);
1335 dev->mounted_rw = !!hasmntopt(mnt, "rw");
1336 dev->mounted = true;
1337 dev->unmount = false;
1339 pb_debug("%s: %s is already mounted (r%c) at %s\n",
1340 __func__, dev->device_path,
1341 dev->mounted_rw ? 'w' : 'o',
1353 * Attempt to mount a filesystem safely, while handling certain filesytem-
1356 static int try_mount(const char *device_path, const char *mount_path,
1357 const char *fstype, unsigned long flags,
1360 const char *fs, *safe_opts;
1363 /* Mount ext3 as ext4 instead so 'norecovery' can be used */
1364 if (strncmp(fstype, "ext3", strlen("ext3")) == 0) {
1365 pb_debug("Mounting ext3 filesystem as ext4\n");
1370 if (strncmp(fs, "xfs", strlen("xfs")) == 0 ||
1371 strncmp(fs, "ext4", strlen("ext4")) == 0)
1372 safe_opts = "norecovery";
1377 /* If no snapshot is available don't attempt recovery */
1379 return mount(device_path, mount_path, fs, flags, safe_opts);
1381 rc = mount(device_path, mount_path, fs, flags, NULL);
1386 /* Mounting failed; some filesystems will fail to mount if a recovery
1387 * journal exists (eg. cross-endian XFS), so try again with norecovery
1388 * where that option is available.
1389 * If mounting read-write just return the error as norecovery is not a
1391 if ((flags & MS_RDONLY) != MS_RDONLY || !safe_opts)
1395 return mount(device_path, mount_path, fs, flags, safe_opts);
1398 static int mount_device(struct discover_device *dev)
1400 const char *fstype, *device_path;
1403 if (!dev->device_path)
1409 if (check_existing_mount(dev))
1412 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1416 dev->mount_path = join_paths(dev, mount_base(),
1419 if (pb_mkdir_recursive(dev->mount_path)) {
1420 pb_log("couldn't create mount directory %s: %s\n",
1421 dev->mount_path, strerror(errno));
1425 device_path = get_device_path(dev);
1427 pb_log("mounting device %s read-only\n", dev->device_path);
1428 rc = try_mount(device_path, dev->mount_path, fstype,
1429 MS_RDONLY | MS_SILENT, dev->ramdisk);
1432 dev->mounted = true;
1433 dev->mounted_rw = false;
1434 dev->unmount = true;
1435 dev->root_path = check_subvols(dev);
1439 pb_log("couldn't mount device %s: mount failed: %s\n",
1440 device_path, strerror(errno));
1442 /* If mount fails clean up any snapshot */
1443 devmapper_destroy_snapshot(dev);
1445 pb_rmdir_recursive(mount_base(), dev->mount_path);
1447 talloc_free(dev->mount_path);
1448 dev->mount_path = NULL;
1452 static int umount_device(struct discover_device *dev)
1454 const char *device_path;
1457 if (!dev->mounted || !dev->unmount)
1460 device_path = get_device_path(dev);
1462 pb_log("unmounting device %s\n", device_path);
1463 rc = umount(dev->mount_path);
1467 dev->mounted = false;
1468 devmapper_destroy_snapshot(dev);
1470 pb_rmdir_recursive(mount_base(), dev->mount_path);
1472 talloc_free(dev->mount_path);
1473 dev->mount_path = NULL;
1474 dev->root_path = NULL;
1479 int device_request_write(struct discover_device *dev, bool *release)
1481 const char *fstype, *device_path;
1482 const struct config *config;
1487 config = config_get();
1488 if (!config->allow_writes)
1494 if (dev->mounted_rw)
1497 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1499 device_path = get_device_path(dev);
1501 pb_log("remounting device %s read-write\n", device_path);
1503 rc = umount(dev->mount_path);
1505 pb_log("Failed to unmount %s: %s\n",
1506 dev->mount_path, strerror(errno));
1510 rc = try_mount(device_path, dev->mount_path, fstype,
1511 MS_SILENT, dev->ramdisk);
1515 dev->mounted_rw = true;
1520 pb_log("Unable to remount device %s read-write: %s\n",
1521 device_path, strerror(errno));
1522 rc = try_mount(device_path, dev->mount_path, fstype,
1523 MS_RDONLY | MS_SILENT, dev->ramdisk);
1525 pb_log("Unable to recover mount for %s: %s\n",
1526 device_path, strerror(errno));
1530 void device_release_write(struct discover_device *dev, bool release)
1532 const char *fstype, *device_path;
1537 device_path = get_device_path(dev);
1539 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1541 pb_log("remounting device %s read-only\n", device_path);
1543 if (umount(dev->mount_path)) {
1544 pb_log("Failed to unmount %s\n", dev->mount_path);
1547 dev->mounted_rw = dev->mounted = false;
1550 devmapper_merge_snapshot(dev);
1551 /* device_path becomes stale after merge */
1552 device_path = get_device_path(dev);
1555 if (try_mount(device_path, dev->mount_path, fstype,
1556 MS_RDONLY | MS_SILENT, dev->ramdisk))
1557 pb_log("Failed to remount %s read-only: %s\n",
1558 device_path, strerror(errno));
1560 dev->mounted = true;
1563 void device_sync_snapshots(struct device_handler *handler, const char *device)
1565 struct discover_device *dev = NULL;
1569 /* Find matching device and sync */
1570 dev = device_lookup_by_name(handler, device);
1572 pb_log("%s: device name '%s' unrecognised\n",
1577 device_release_write(dev, true);
1579 pb_log("%s has no snapshot to merge, skipping\n",
1584 /* Otherwise sync all relevant devices */
1585 for (i = 0; i < handler->n_devices; i++) {
1586 dev = handler->devices[i];
1587 if (dev->device->type != DEVICE_TYPE_DISK &&
1588 dev->device->type != DEVICE_TYPE_USB)
1591 device_release_write(dev, true);
1593 pb_log("%s has no snapshot to merge, skipping\n",
1600 void device_handler_discover_context_commit(
1601 struct device_handler *handler __attribute__((unused)),
1602 struct discover_context *ctx __attribute__((unused)))
1604 pb_log("%s stubbed out for test cases\n", __func__);
1607 static void device_handler_update_lang(const char *lang __attribute__((unused)))
1611 static int device_handler_init_sources(
1612 struct device_handler *handler __attribute__((unused)))
1617 static void device_handler_reinit_sources(
1618 struct device_handler *handler __attribute__((unused)))
1622 static int umount_device(struct discover_device *dev __attribute__((unused)))
1627 static int __attribute__((unused)) mount_device(
1628 struct discover_device *dev __attribute__((unused)))
1633 int device_request_write(struct discover_device *dev __attribute__((unused)),
1640 void device_release_write(struct discover_device *dev __attribute__((unused)),
1641 bool release __attribute__((unused)))
1645 void device_sync_snapshots(
1646 struct device_handler *handler __attribute__((unused)),
1647 const char *device __attribute__((unused)))