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 "user-event.h"
41 enum default_priority {
42 DEFAULT_PRIORITY_REMOTE = 1,
43 DEFAULT_PRIORITY_LOCAL_FIRST = 2,
44 DEFAULT_PRIORITY_LOCAL_LAST = 0xfe,
45 DEFAULT_PRIORITY_DISABLED = 0xff,
48 struct device_handler {
49 struct discover_server *server;
53 struct network *network;
54 struct user_event *user_event;
56 struct discover_device **devices;
57 unsigned int n_devices;
59 struct ramdisk_device **ramdisks;
60 unsigned int n_ramdisks;
62 struct waitset *waitset;
63 struct waiter *timeout_waiter;
64 bool autoboot_enabled;
65 unsigned int sec_to_boot;
67 struct discover_boot_option *default_boot_option;
68 int default_boot_option_priority;
70 struct list unresolved_boot_options;
72 struct boot_task *pending_boot;
73 bool pending_boot_is_default;
76 static int mount_device(struct discover_device *dev);
77 static int umount_device(struct discover_device *dev);
79 static int device_handler_init_sources(struct device_handler *handler);
80 static void device_handler_reinit_sources(struct device_handler *handler);
82 static void device_handler_update_lang(const char *lang);
84 void discover_context_add_boot_option(struct discover_context *ctx,
85 struct discover_boot_option *boot_option)
87 boot_option->source = ctx->parser;
88 list_add_tail(&ctx->boot_options, &boot_option->list);
89 talloc_steal(ctx, boot_option);
93 * device_handler_get_device_count - Get the count of current handler devices.
96 int device_handler_get_device_count(const struct device_handler *handler)
98 return handler->n_devices;
102 * device_handler_get_device - Get a handler device by index.
105 const struct discover_device *device_handler_get_device(
106 const struct device_handler *handler, unsigned int index)
108 if (index >= handler->n_devices) {
109 assert(0 && "bad index");
113 return handler->devices[index];
116 struct discover_boot_option *discover_boot_option_create(
117 struct discover_context *ctx,
118 struct discover_device *device)
120 struct discover_boot_option *opt;
122 opt = talloc_zero(ctx, struct discover_boot_option);
123 opt->option = talloc_zero(opt, struct boot_option);
124 opt->device = device;
129 static int device_match_uuid(struct discover_device *dev, const char *uuid)
131 return dev->uuid && !strcmp(dev->uuid, uuid);
134 static int device_match_label(struct discover_device *dev, const char *label)
136 return dev->label && !strcmp(dev->label, label);
139 static int device_match_id(struct discover_device *dev, const char *id)
141 return !strcmp(dev->device->id, id);
144 static int device_match_serial(struct discover_device *dev, const char *serial)
146 const char *val = discover_device_get_param(dev, "ID_SERIAL");
147 return val && !strcmp(val, serial);
150 static struct discover_device *device_lookup(
151 struct device_handler *device_handler,
152 int (match_fn)(struct discover_device *, const char *),
155 struct discover_device *dev;
161 for (i = 0; i < device_handler->n_devices; i++) {
162 dev = device_handler->devices[i];
164 if (match_fn(dev, str))
171 struct discover_device *device_lookup_by_name(struct device_handler *handler,
174 if (!strncmp(name, "/dev/", strlen("/dev/")))
175 name += strlen("/dev/");
177 return device_lookup_by_id(handler, name);
180 struct discover_device *device_lookup_by_uuid(
181 struct device_handler *device_handler,
184 return device_lookup(device_handler, device_match_uuid, uuid);
187 struct discover_device *device_lookup_by_label(
188 struct device_handler *device_handler,
191 return device_lookup(device_handler, device_match_label, label);
194 struct discover_device *device_lookup_by_id(
195 struct device_handler *device_handler,
198 return device_lookup(device_handler, device_match_id, id);
201 struct discover_device *device_lookup_by_serial(
202 struct device_handler *device_handler,
205 return device_lookup(device_handler, device_match_serial, serial);
208 void device_handler_destroy(struct device_handler *handler)
210 talloc_free(handler);
213 static int destroy_device(void *arg)
215 struct discover_device *dev = arg;
222 struct discover_device *discover_device_create(struct device_handler *handler,
225 struct discover_device *dev;
227 dev = device_lookup_by_id(handler, id);
231 dev = talloc_zero(handler, struct discover_device);
232 dev->device = talloc_zero(dev, struct device);
233 dev->device->id = talloc_strdup(dev->device, id);
234 list_init(&dev->params);
235 list_init(&dev->boot_options);
237 talloc_set_destructor(dev, destroy_device);
242 struct discover_device_param {
245 struct list_item list;
248 void discover_device_set_param(struct discover_device *device,
249 const char *name, const char *value)
251 struct discover_device_param *param;
254 list_for_each_entry(&device->params, param, list) {
255 if (!strcmp(param->name, name)) {
264 param = talloc(device, struct discover_device_param);
265 param->name = talloc_strdup(param, name);
266 list_add(&device->params, ¶m->list);
269 list_remove(¶m->list);
273 talloc_free(param->value);
276 param->value = talloc_strdup(param, value);
279 const char *discover_device_get_param(struct discover_device *device,
282 struct discover_device_param *param;
284 list_for_each_entry(&device->params, param, list) {
285 if (!strcmp(param->name, name))
291 struct device_handler *device_handler_init(struct discover_server *server,
292 struct waitset *waitset, int dry_run)
294 struct device_handler *handler;
297 handler = talloc_zero(NULL, struct device_handler);
298 handler->server = server;
299 handler->waitset = waitset;
300 handler->dry_run = dry_run;
301 handler->autoboot_enabled = config_get()->autoboot_enabled;
303 list_init(&handler->unresolved_boot_options);
305 /* set up our mount point base */
306 pb_mkdir_recursive(mount_base());
310 if (config_get()->safe_mode)
313 rc = device_handler_init_sources(handler);
315 talloc_free(handler);
322 void device_handler_reinit(struct device_handler *handler)
324 struct discover_boot_option *opt, *tmp;
325 struct ramdisk_device *ramdisk;
328 device_handler_cancel_default(handler);
330 /* free unresolved boot options */
331 list_for_each_entry_safe(&handler->unresolved_boot_options,
334 list_init(&handler->unresolved_boot_options);
336 /* drop all devices */
337 for (i = 0; i < handler->n_devices; i++) {
338 discover_server_notify_device_remove(handler->server,
339 handler->devices[i]->device);
340 ramdisk = handler->devices[i]->ramdisk;
341 talloc_free(handler->devices[i]);
342 talloc_free(ramdisk);
345 talloc_free(handler->devices);
346 handler->devices = NULL;
347 handler->n_devices = 0;
348 talloc_free(handler->ramdisks);
349 handler->ramdisks = NULL;
350 handler->n_ramdisks = 0;
352 device_handler_reinit_sources(handler);
355 void device_handler_remove(struct device_handler *handler,
356 struct discover_device *device)
358 struct discover_boot_option *opt, *tmp;
361 for (i = 0; i < handler->n_devices; i++)
362 if (handler->devices[i] == device)
365 if (i == handler->n_devices) {
370 /* Free any unresolved options, as they're currently allocated
371 * against the handler */
372 list_for_each_entry_safe(&handler->unresolved_boot_options,
374 if (opt->device != device)
376 list_remove(&opt->list);
380 /* if this is a network device, we have to unregister it from the
382 if (device->device->type == DEVICE_TYPE_NETWORK)
383 network_unregister_device(handler->network, device);
385 handler->n_devices--;
386 memmove(&handler->devices[i], &handler->devices[i + 1],
387 (handler->n_devices - i) * sizeof(handler->devices[0]));
388 handler->devices = talloc_realloc(handler, handler->devices,
389 struct discover_device *, handler->n_devices);
391 if (device->notified)
392 discover_server_notify_device_remove(handler->server,
398 static void boot_status(void *arg, struct boot_status *status)
400 struct device_handler *handler = arg;
402 discover_server_notify_boot_status(handler->server, status);
405 static void countdown_status(struct device_handler *handler,
406 struct discover_boot_option *opt, unsigned int sec)
408 struct boot_status status;
410 status.type = BOOT_STATUS_INFO;
411 status.progress = -1;
412 status.detail = NULL;
413 status.message = talloc_asprintf(handler,
414 _("Booting in %d sec: %s"), sec, opt->option->name);
416 discover_server_notify_boot_status(handler->server, &status);
418 talloc_free(status.message);
421 static int default_timeout(void *arg)
423 struct device_handler *handler = arg;
424 struct discover_boot_option *opt;
426 if (!handler->default_boot_option)
429 if (handler->pending_boot)
432 opt = handler->default_boot_option;
434 if (handler->sec_to_boot) {
435 countdown_status(handler, opt, handler->sec_to_boot);
436 handler->sec_to_boot--;
437 handler->timeout_waiter = waiter_register_timeout(
438 handler->waitset, 1000,
439 default_timeout, handler);
443 handler->timeout_waiter = NULL;
445 pb_log("Timeout expired, booting default option %s\n", opt->option->id);
447 handler->pending_boot = boot(handler, handler->default_boot_option,
448 NULL, handler->dry_run, boot_status, handler);
449 handler->pending_boot_is_default = true;
454 enum ipmi_bootdev ipmi_type;
455 enum device_type device_type;
456 } device_type_map[] = {
457 { IPMI_BOOTDEV_NETWORK, DEVICE_TYPE_NETWORK },
458 { IPMI_BOOTDEV_DISK, DEVICE_TYPE_DISK },
459 { IPMI_BOOTDEV_CDROM, DEVICE_TYPE_OPTICAL },
462 static bool ipmi_device_type_matches(enum ipmi_bootdev ipmi_type,
463 enum device_type device_type)
467 for (i = 0; i < ARRAY_SIZE(device_type_map); i++) {
468 if (device_type_map[i].device_type == device_type)
469 return device_type_map[i].ipmi_type == ipmi_type;
475 static int autoboot_option_priority(const struct config *config,
476 struct discover_boot_option *opt)
478 enum device_type type = opt->device->device->type;
479 const char *uuid = opt->device->uuid;
480 struct autoboot_option *auto_opt;
483 for (i = 0; i < config->n_autoboot_opts; i++) {
484 auto_opt = &config->autoboot_opts[i];
485 if (auto_opt->boot_type == BOOT_DEVICE_UUID)
486 if (!strcmp(auto_opt->uuid, uuid))
487 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
489 if (auto_opt->boot_type == BOOT_DEVICE_TYPE)
490 if (auto_opt->type == type ||
491 auto_opt->type == DEVICE_TYPE_ANY)
492 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
499 * We have different priorities to resolve conflicts between boot options that
500 * report to be the default for their device. This function assigns a priority
503 static enum default_priority default_option_priority(
504 struct discover_boot_option *opt)
506 const struct config *config;
508 config = config_get();
510 /* We give highest priority to IPMI-configured boot options. If
511 * we have an IPMI bootdev configuration set, then we don't allow
512 * any other defaults */
513 if (config->ipmi_bootdev) {
514 bool ipmi_match = ipmi_device_type_matches(config->ipmi_bootdev,
515 opt->device->device->type);
517 return DEFAULT_PRIORITY_REMOTE;
519 pb_debug("handler: disabled default priority due to "
520 "non-matching IPMI type %x\n",
521 config->ipmi_bootdev);
522 return DEFAULT_PRIORITY_DISABLED;
525 /* Next, try to match the option against the user-defined autoboot
526 * options, either by device UUID or type. */
527 if (config->n_autoboot_opts) {
528 int boot_match = autoboot_option_priority(config, opt);
533 /* If the option didn't match any entry in the array, it is disabled */
534 pb_debug("handler: disabled default priority due to "
535 "non-matching UUID or type\n");
536 return DEFAULT_PRIORITY_DISABLED;
539 static void set_default(struct device_handler *handler,
540 struct discover_boot_option *opt)
542 enum default_priority cur_prio, new_prio;
544 if (!handler->autoboot_enabled)
547 pb_debug("handler: new default option: %s\n", opt->option->id);
549 new_prio = default_option_priority(opt);
551 /* Anything outside our range prevents a default boot */
552 if (new_prio >= DEFAULT_PRIORITY_DISABLED)
555 pb_debug("handler: calculated priority %d\n", new_prio);
557 /* Resolve any conflicts: if we have a new default option, it only
558 * replaces the current if it has a higher priority. */
559 if (handler->default_boot_option) {
561 cur_prio = handler->default_boot_option_priority;
563 if (new_prio < cur_prio) {
564 pb_log("handler: new prio %d beats "
565 "old prio %d for %s\n",
567 handler->default_boot_option
569 handler->default_boot_option = opt;
570 handler->default_boot_option_priority = new_prio;
571 /* extend the timeout a little, so the user sees some
572 * indication of the change */
573 handler->sec_to_boot += 2;
579 handler->sec_to_boot = config_get()->autoboot_timeout_sec;
580 handler->default_boot_option = opt;
581 handler->default_boot_option_priority = new_prio;
583 pb_log("handler: boot option %s set as default, timeout %u sec.\n",
584 opt->option->id, handler->sec_to_boot);
586 default_timeout(handler);
589 static bool resource_is_resolved(struct resource *res)
591 return !res || res->resolved;
594 /* We only use this in an assert, which will disappear if we're compiling
595 * with NDEBUG, so we need the 'used' attribute for these builds */
596 static bool __attribute__((used)) boot_option_is_resolved(
597 struct discover_boot_option *opt)
599 return resource_is_resolved(opt->boot_image) &&
600 resource_is_resolved(opt->initrd) &&
601 resource_is_resolved(opt->dtb) &&
602 resource_is_resolved(opt->icon);
605 static bool resource_resolve(struct resource *res, const char *name,
606 struct discover_boot_option *opt,
607 struct device_handler *handler)
609 struct parser *parser = opt->source;
611 if (resource_is_resolved(res))
614 pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
615 opt->option->id, name, parser->name);
616 parser->resolve_resource(handler, res);
618 return res->resolved;
621 static bool boot_option_resolve(struct discover_boot_option *opt,
622 struct device_handler *handler)
624 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
625 resource_resolve(opt->initrd, "initrd", opt, handler) &&
626 resource_resolve(opt->dtb, "dtb", opt, handler) &&
627 resource_resolve(opt->icon, "icon", opt, handler);
630 static void boot_option_finalise(struct device_handler *handler,
631 struct discover_boot_option *opt)
633 assert(boot_option_is_resolved(opt));
635 /* check that the parsers haven't set any of the final data */
636 assert(!opt->option->boot_image_file);
637 assert(!opt->option->initrd_file);
638 assert(!opt->option->dtb_file);
639 assert(!opt->option->icon_file);
640 assert(!opt->option->device_id);
643 opt->option->boot_image_file = opt->boot_image->url->full;
645 opt->option->initrd_file = opt->initrd->url->full;
647 opt->option->dtb_file = opt->dtb->url->full;
649 opt->option->icon_file = opt->icon->url->full;
651 opt->option->device_id = opt->device->device->id;
653 if (opt->option->is_default)
654 set_default(handler, opt);
657 static void notify_boot_option(struct device_handler *handler,
658 struct discover_boot_option *opt)
660 struct discover_device *dev = opt->device;
663 discover_server_notify_device_add(handler->server,
664 opt->device->device);
665 dev->notified = true;
666 discover_server_notify_boot_option_add(handler->server, opt->option);
669 static void process_boot_option_queue(struct device_handler *handler)
671 struct discover_boot_option *opt, *tmp;
673 list_for_each_entry_safe(&handler->unresolved_boot_options,
676 pb_debug("queue: attempting resolution for %s\n",
679 if (!boot_option_resolve(opt, handler))
682 pb_debug("\tresolved!\n");
684 list_remove(&opt->list);
685 list_add_tail(&opt->device->boot_options, &opt->list);
686 talloc_steal(opt->device, opt);
687 boot_option_finalise(handler, opt);
688 notify_boot_option(handler, opt);
692 struct discover_context *device_handler_discover_context_create(
693 struct device_handler *handler,
694 struct discover_device *device)
696 struct discover_context *ctx;
698 ctx = talloc_zero(handler, struct discover_context);
699 ctx->device = device;
700 ctx->network = handler->network;
701 list_init(&ctx->boot_options);
707 * context_commit - Commit a temporary discovery context to the handler,
708 * and notify the clients about any new options / devices
710 void device_handler_discover_context_commit(struct device_handler *handler,
711 struct discover_context *ctx)
713 struct discover_device *dev = ctx->device;
714 struct discover_boot_option *opt, *tmp;
716 if (!device_lookup_by_id(handler, dev->device->id))
717 device_handler_add_device(handler, dev);
719 /* move boot options from the context to the device */
720 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
721 list_remove(&opt->list);
723 if (boot_option_resolve(opt, handler)) {
724 pb_log("boot option %s is resolved, "
725 "sending to clients\n",
727 list_add_tail(&dev->boot_options, &opt->list);
728 talloc_steal(dev, opt);
729 boot_option_finalise(handler, opt);
730 notify_boot_option(handler, opt);
732 if (!opt->source->resolve_resource) {
733 pb_log("parser %s gave us an unresolved "
734 "resource (%s), but no way to "
736 opt->source->name, opt->option->id);
739 pb_log("boot option %s is unresolved, "
742 list_add(&handler->unresolved_boot_options,
744 talloc_steal(handler, opt);
750 void device_handler_add_device(struct device_handler *handler,
751 struct discover_device *device)
753 handler->n_devices++;
754 handler->devices = talloc_realloc(handler, handler->devices,
755 struct discover_device *, handler->n_devices);
756 handler->devices[handler->n_devices - 1] = device;
758 if (device->device->type == DEVICE_TYPE_NETWORK)
759 network_register_device(handler->network, device);
762 void device_handler_add_ramdisk(struct device_handler *handler,
765 struct ramdisk_device *dev;
771 for (i = 0; i < handler->n_ramdisks; i++)
772 if (!strcmp(handler->ramdisks[i]->path, path))
775 dev = talloc_zero(handler, struct ramdisk_device);
777 pb_log("Failed to allocate memory to track %s\n", path);
781 dev->path = talloc_strdup(handler, path);
783 handler->ramdisks = talloc_realloc(handler, handler->ramdisks,
784 struct ramdisk_device *,
785 handler->n_ramdisks + 1);
786 if (!handler->ramdisks) {
787 pb_log("Failed to reallocate memory"
788 "- ramdisk tracking inconsistent!\n");
792 handler->ramdisks[i] = dev;
793 i = handler->n_ramdisks++;
796 struct ramdisk_device *device_handler_get_ramdisk(
797 struct device_handler *handler)
803 /* Check if free ramdisk exists */
804 for (i = 0; i < handler->n_ramdisks; i++)
805 if (!handler->ramdisks[i]->snapshot &&
806 !handler->ramdisks[i]->origin &&
807 !handler->ramdisks[i]->base)
808 return handler->ramdisks[i];
810 /* Otherwise create a new one */
811 name = talloc_asprintf(handler, "/dev/ram%d",
812 handler->n_ramdisks);
814 pb_debug("Failed to allocate memory to name /dev/ram%d",
815 handler->n_ramdisks);
819 id = makedev(1, handler->n_ramdisks);
820 if (mknod(name, S_IFBLK, id)) {
821 if (errno == EEXIST) {
822 /* We haven't yet received updates for existing
823 * ramdisks - add and use this one */
824 pb_debug("Using untracked ramdisk %s\n", name);
826 pb_log("Failed to create new ramdisk %s: %s\n",
827 name, strerror(errno));
831 device_handler_add_ramdisk(handler, name);
834 return handler->ramdisks[i];
837 void device_handler_release_ramdisk(struct discover_device *device)
839 struct ramdisk_device *ramdisk = device->ramdisk;
841 talloc_free(ramdisk->snapshot);
842 talloc_free(ramdisk->origin);
843 talloc_free(ramdisk->base);
845 ramdisk->snapshot = ramdisk->origin = ramdisk->base = NULL;
846 ramdisk->sectors = 0;
848 device->ramdisk = NULL;
851 /* Start discovery on a hotplugged device. The device will be in our devices
852 * array, but has only just been initialised by the hotplug source.
854 int device_handler_discover(struct device_handler *handler,
855 struct discover_device *dev)
857 struct discover_context *ctx;
858 struct boot_status *status;
861 status = talloc_zero(handler, struct boot_status);
862 status->type = BOOT_STATUS_INFO;
863 status->message = talloc_asprintf(status, "Processing %s device %s",
864 device_type_display_name(dev->device->type),
866 boot_status(handler, status);
868 process_boot_option_queue(handler);
870 /* create our context */
871 ctx = device_handler_discover_context_create(handler, dev);
873 rc = mount_device(dev);
877 /* add this device to our system info */
878 system_info_register_blockdev(dev->device->id, dev->uuid,
881 /* run the parsers. This will populate the ctx's boot_option list. */
882 iterate_parsers(ctx);
884 /* add discovered stuff to the handler */
885 device_handler_discover_context_commit(handler, ctx);
888 status->message = talloc_asprintf(status,"Processing %s complete\n",
890 boot_status(handler, status);
898 /* Incoming dhcp event */
899 int device_handler_dhcp(struct device_handler *handler,
900 struct discover_device *dev, struct event *event)
902 struct discover_context *ctx;
903 struct boot_status *status;
905 status = talloc_zero(handler, struct boot_status);
906 status->type = BOOT_STATUS_INFO;
907 status->message = talloc_asprintf(status, "Processing dhcp event on %s",
909 boot_status(handler, status);
911 /* create our context */
912 ctx = device_handler_discover_context_create(handler, dev);
915 iterate_parsers(ctx);
917 device_handler_discover_context_commit(handler, ctx);
919 status->message = talloc_asprintf(status,"Processing %s complete\n",
921 boot_status(handler, status);
929 /* incoming conf event */
930 int device_handler_conf(struct device_handler *handler,
931 struct discover_device *dev, struct pb_url *url)
933 struct discover_context *ctx;
934 struct boot_status *status;
936 status = talloc_zero(handler, struct boot_status);
937 status->type = BOOT_STATUS_INFO;
938 status->message = talloc_asprintf(status, "Processing user config");
939 boot_status(handler, status);
941 /* create our context */
942 ctx = device_handler_discover_context_create(handler, dev);
945 iterate_parsers(ctx);
947 device_handler_discover_context_commit(handler, ctx);
949 status->message = talloc_asprintf(status,
950 "Processing user config complete");
951 boot_status(handler, status);
959 static struct discover_boot_option *find_boot_option_by_id(
960 struct device_handler *handler, const char *id)
964 for (i = 0; i < handler->n_devices; i++) {
965 struct discover_device *dev = handler->devices[i];
966 struct discover_boot_option *opt;
968 list_for_each_entry(&dev->boot_options, opt, list)
969 if (!strcmp(opt->option->id, id))
976 void device_handler_boot(struct device_handler *handler,
977 struct boot_command *cmd)
979 struct discover_boot_option *opt = NULL;
981 if (cmd->option_id && strlen(cmd->option_id))
982 opt = find_boot_option_by_id(handler, cmd->option_id);
984 if (handler->pending_boot)
985 boot_cancel(handler->pending_boot);
989 handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
990 boot_status, handler);
991 handler->pending_boot_is_default = false;
994 void device_handler_cancel_default(struct device_handler *handler)
996 struct boot_status status;
998 if (handler->timeout_waiter)
999 waiter_remove(handler->timeout_waiter);
1001 handler->timeout_waiter = NULL;
1002 handler->autoboot_enabled = false;
1004 /* we only send status if we had a default boot option queued */
1005 if (!handler->default_boot_option)
1008 pb_log("Cancelling default boot option\n");
1010 if (handler->pending_boot && handler->pending_boot_is_default) {
1011 boot_cancel(handler->pending_boot);
1012 handler->pending_boot = NULL;
1013 handler->pending_boot_is_default = false;
1016 handler->default_boot_option = NULL;
1018 status.type = BOOT_STATUS_INFO;
1019 status.progress = -1;
1020 status.detail = NULL;
1021 status.message = _("Default boot cancelled");
1023 discover_server_notify_boot_status(handler->server, &status);
1026 void device_handler_update_config(struct device_handler *handler,
1027 struct config *config)
1031 rc = config_set(config);
1035 discover_server_notify_config(handler->server, config);
1036 device_handler_update_lang(config->lang);
1037 device_handler_reinit(handler);
1040 static char *device_from_addr(void *ctx, struct pb_url *url)
1042 char *ipaddr, *buf, *tok, *dev = NULL;
1043 const char *delim = " ";
1044 struct sockaddr_in *ip;
1045 struct sockaddr_in si;
1046 struct addrinfo *res;
1050 /* Note: IPv4 only */
1051 rc = inet_pton(AF_INET, url->host, &(si.sin_addr));
1055 /* need to turn hostname into a valid IP */
1056 rc = getaddrinfo(url->host, NULL, NULL, &res);
1058 pb_debug("%s: Invalid URL\n",__func__);
1061 ipaddr = talloc_array(ctx,char,INET_ADDRSTRLEN);
1062 ip = (struct sockaddr_in *) res->ai_addr;
1063 inet_ntop(AF_INET, &(ip->sin_addr), ipaddr, INET_ADDRSTRLEN);
1067 const char *argv[] = {
1069 "route", "show", "to", "match",
1074 p = process_create(ctx);
1076 p->path = pb_system_apps.ip;
1078 p->keep_stdout = true;
1080 rc = process_run_sync(p);
1083 /* ip has complained for some reason; most likely
1084 * there is no route to the host - bail out */
1085 pb_debug("%s: No route to %s\n",__func__,url->host);
1089 buf = p->stdout_buf;
1090 /* If a route is found, ip-route output will be of the form
1091 * "... dev DEVNAME ... " */
1092 tok = strtok(buf, delim);
1094 if (!strcmp(tok, "dev")) {
1095 tok = strtok(NULL, delim);
1096 dev = talloc_strdup(ctx, tok);
1099 tok = strtok(NULL, delim);
1104 pb_debug("%s: Found interface '%s'\n", __func__,dev);
1109 void device_handler_process_url(struct device_handler *handler,
1112 struct discover_context *ctx;
1113 struct discover_device *dev;
1114 struct boot_status *status;
1115 struct pb_url *pb_url;
1116 struct event *event;
1117 struct param *param;
1119 status = talloc(handler, struct boot_status);
1121 status->type = BOOT_STATUS_ERROR;
1122 status->progress = 0;
1123 status->detail = talloc_asprintf(status,
1124 _("Received config URL %s"), url);
1126 if (!handler->network) {
1127 status->message = talloc_asprintf(handler,
1128 _("No network configured"));
1132 event = talloc(handler, struct event);
1133 event->type = EVENT_TYPE_USER;
1134 event->action = EVENT_ACTION_CONF;
1136 event->params = talloc_array(event, struct param, 1);
1137 param = &event->params[0];
1138 param->name = talloc_strdup(event, "pxeconffile");
1139 param->value = talloc_strdup(event, url);
1140 event->n_params = 1;
1142 pb_url = pb_url_parse(event, event->params->value);
1143 if (!pb_url || !pb_url->host) {
1144 status->message = talloc_asprintf(handler,
1145 _("Invalid config URL!"));
1149 event->device = device_from_addr(event, pb_url);
1150 if (!event->device) {
1151 status->message = talloc_asprintf(status,
1152 _("Unable to route to host %s"),
1157 dev = discover_device_create(handler, event->device);
1158 ctx = device_handler_discover_context_create(handler, dev);
1161 iterate_parsers(ctx);
1163 device_handler_discover_context_commit(handler, ctx);
1167 status->type = BOOT_STATUS_INFO;
1168 status->message = talloc_asprintf(status, _("Config file %s parsed"),
1171 boot_status(handler, status);
1172 talloc_free(status);
1175 #ifndef PETITBOOT_TEST
1177 static void device_handler_update_lang(const char *lang)
1179 const char *cur_lang;
1184 cur_lang = setlocale(LC_ALL, NULL);
1185 if (cur_lang && !strcmp(cur_lang, lang))
1188 setlocale(LC_ALL, lang);
1191 static int device_handler_init_sources(struct device_handler *handler)
1193 /* init our device sources: udev, network and user events */
1194 handler->udev = udev_init(handler, handler->waitset);
1198 handler->network = network_init(handler, handler->waitset,
1200 if (!handler->network)
1203 handler->user_event = user_event_init(handler, handler->waitset);
1204 if (!handler->user_event)
1210 static void device_handler_reinit_sources(struct device_handler *handler)
1212 /* if we haven't initialised sources previously (becuase we started in
1213 * safe mode), then init once here. */
1214 if (!(handler->udev || handler->network || handler->user_event)) {
1215 device_handler_init_sources(handler);
1219 udev_reinit(handler->udev);
1221 network_shutdown(handler->network);
1222 handler->network = network_init(handler, handler->waitset,
1226 static const char *fs_parameters(unsigned int rw_flags, const char *fstype)
1228 if ((rw_flags | MS_RDONLY) != MS_RDONLY)
1231 /* Avoid writing back to the disk on journaled filesystems */
1232 if (!strncmp(fstype, "ext4", strlen("ext4")))
1233 return "norecovery";
1234 if (!strncmp(fstype, "xfs", strlen("xfs")))
1235 return "norecovery";
1240 static bool check_existing_mount(struct discover_device *dev)
1242 struct stat devstat, mntstat;
1247 rc = stat(dev->device_path, &devstat);
1249 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
1253 if (!S_ISBLK(devstat.st_mode)) {
1254 pb_debug("%s: %s isn't a block device?\n", __func__,
1259 fp = fopen("/proc/self/mounts", "r");
1262 mnt = getmntent(fp);
1266 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
1269 rc = stat(mnt->mnt_fsname, &mntstat);
1273 if (!S_ISBLK(mntstat.st_mode))
1276 if (mntstat.st_rdev == devstat.st_rdev) {
1277 dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
1278 dev->mounted_rw = !!hasmntopt(mnt, "rw");
1279 dev->mounted = true;
1280 dev->unmount = false;
1282 pb_debug("%s: %s is already mounted (r%c) at %s\n",
1283 __func__, dev->device_path,
1284 dev->mounted_rw ? 'w' : 'o',
1295 static int mount_device(struct discover_device *dev)
1300 if (!dev->device_path)
1306 if (check_existing_mount(dev))
1309 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1313 /* ext3 treats the norecovery option as an error, so mount the device
1314 * as an ext4 filesystem instead */
1315 if (!strncmp(fstype, "ext3", strlen("ext3"))) {
1316 pb_debug("Mounting ext3 filesystem as ext4\n");
1317 fstype = talloc_asprintf(dev, "ext4");
1320 dev->mount_path = join_paths(dev, mount_base(),
1323 if (pb_mkdir_recursive(dev->mount_path)) {
1324 pb_log("couldn't create mount directory %s: %s\n",
1325 dev->mount_path, strerror(errno));
1329 pb_log("mounting device %s read-only\n", dev->device_path);
1331 rc = mount(dev->device_path, dev->mount_path, fstype,
1332 MS_RDONLY | MS_SILENT,
1333 fs_parameters(MS_RDONLY, fstype));
1335 dev->mounted = true;
1336 dev->mounted_rw = false;
1337 dev->unmount = true;
1341 pb_log("couldn't mount device %s: mount failed: %s\n",
1342 dev->device_path, strerror(errno));
1344 pb_rmdir_recursive(mount_base(), dev->mount_path);
1346 talloc_free(dev->mount_path);
1347 dev->mount_path = NULL;
1351 static int umount_device(struct discover_device *dev)
1355 if (!dev->mounted || !dev->unmount)
1358 pb_log("unmounting device %s\n", dev->device_path);
1359 rc = umount(dev->mount_path);
1363 dev->mounted = false;
1365 pb_rmdir_recursive(mount_base(), dev->mount_path);
1367 talloc_free(dev->mount_path);
1368 dev->mount_path = NULL;
1373 int device_request_write(struct discover_device *dev, bool *release)
1383 if (dev->mounted_rw)
1386 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1388 pb_log("remounting device %s read-write\n", dev->device_path);
1390 rc = umount(dev->mount_path);
1392 pb_log("Failed to unmount %s\n", dev->mount_path);
1395 rc = mount(dev->device_path, dev->mount_path, fstype,
1397 fs_parameters(MS_REMOUNT, fstype));
1401 dev->mounted_rw = true;
1406 pb_log("Unable to remount device %s read-write\n", dev->device_path);
1407 rc = mount(dev->device_path, dev->mount_path, fstype,
1408 MS_RDONLY | MS_SILENT,
1409 fs_parameters(MS_RDONLY, fstype));
1411 pb_log("Unable to recover mount for %s\n", dev->device_path);
1415 void device_release_write(struct discover_device *dev, bool release)
1422 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1424 pb_log("remounting device %s read-only\n", dev->device_path);
1425 mount(dev->device_path, dev->mount_path, "",
1426 MS_REMOUNT | MS_RDONLY | MS_SILENT,
1427 fs_parameters(MS_RDONLY, fstype));
1428 dev->mounted_rw = false;
1433 static void device_handler_update_lang(const char *lang __attribute__((unused)))
1437 static int device_handler_init_sources(
1438 struct device_handler *handler __attribute__((unused)))
1443 static void device_handler_reinit_sources(
1444 struct device_handler *handler __attribute__((unused)))
1448 static int umount_device(struct discover_device *dev __attribute__((unused)))
1453 static int __attribute__((unused)) mount_device(
1454 struct discover_device *dev __attribute__((unused)))
1459 int device_request_write(struct discover_device *dev __attribute__((unused)),
1466 void device_release_write(struct discover_device *dev __attribute__((unused)),
1467 bool release __attribute__((unused)))