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 waitset *waitset;
60 struct waiter *timeout_waiter;
61 bool autoboot_enabled;
62 unsigned int sec_to_boot;
64 struct discover_boot_option *default_boot_option;
65 int default_boot_option_priority;
67 struct list unresolved_boot_options;
69 struct boot_task *pending_boot;
70 bool pending_boot_is_default;
73 static int mount_device(struct discover_device *dev);
74 static int umount_device(struct discover_device *dev);
76 static int device_handler_init_sources(struct device_handler *handler);
77 static void device_handler_reinit_sources(struct device_handler *handler);
79 static void device_handler_update_lang(const char *lang);
81 void discover_context_add_boot_option(struct discover_context *ctx,
82 struct discover_boot_option *boot_option)
84 boot_option->source = ctx->parser;
85 list_add_tail(&ctx->boot_options, &boot_option->list);
86 talloc_steal(ctx, boot_option);
90 * device_handler_get_device_count - Get the count of current handler devices.
93 int device_handler_get_device_count(const struct device_handler *handler)
95 return handler->n_devices;
99 * device_handler_get_device - Get a handler device by index.
102 const struct discover_device *device_handler_get_device(
103 const struct device_handler *handler, unsigned int index)
105 if (index >= handler->n_devices) {
106 assert(0 && "bad index");
110 return handler->devices[index];
113 struct discover_boot_option *discover_boot_option_create(
114 struct discover_context *ctx,
115 struct discover_device *device)
117 struct discover_boot_option *opt;
119 opt = talloc_zero(ctx, struct discover_boot_option);
120 opt->option = talloc_zero(opt, struct boot_option);
121 opt->device = device;
126 static int device_match_uuid(struct discover_device *dev, const char *uuid)
128 return dev->uuid && !strcmp(dev->uuid, uuid);
131 static int device_match_label(struct discover_device *dev, const char *label)
133 return dev->label && !strcmp(dev->label, label);
136 static int device_match_id(struct discover_device *dev, const char *id)
138 return !strcmp(dev->device->id, id);
141 static int device_match_serial(struct discover_device *dev, const char *serial)
143 const char *val = discover_device_get_param(dev, "ID_SERIAL");
144 return val && !strcmp(val, serial);
147 static struct discover_device *device_lookup(
148 struct device_handler *device_handler,
149 int (match_fn)(struct discover_device *, const char *),
152 struct discover_device *dev;
158 for (i = 0; i < device_handler->n_devices; i++) {
159 dev = device_handler->devices[i];
161 if (match_fn(dev, str))
168 struct discover_device *device_lookup_by_name(struct device_handler *handler,
171 if (!strncmp(name, "/dev/", strlen("/dev/")))
172 name += strlen("/dev/");
174 return device_lookup_by_id(handler, name);
177 struct discover_device *device_lookup_by_uuid(
178 struct device_handler *device_handler,
181 return device_lookup(device_handler, device_match_uuid, uuid);
184 struct discover_device *device_lookup_by_label(
185 struct device_handler *device_handler,
188 return device_lookup(device_handler, device_match_label, label);
191 struct discover_device *device_lookup_by_id(
192 struct device_handler *device_handler,
195 return device_lookup(device_handler, device_match_id, id);
198 struct discover_device *device_lookup_by_serial(
199 struct device_handler *device_handler,
202 return device_lookup(device_handler, device_match_serial, serial);
205 void device_handler_destroy(struct device_handler *handler)
207 talloc_free(handler);
210 static int destroy_device(void *arg)
212 struct discover_device *dev = arg;
219 struct discover_device *discover_device_create(struct device_handler *handler,
222 struct discover_device *dev;
224 dev = device_lookup_by_id(handler, id);
228 dev = talloc_zero(handler, struct discover_device);
229 dev->device = talloc_zero(dev, struct device);
230 dev->device->id = talloc_strdup(dev->device, id);
231 list_init(&dev->params);
232 list_init(&dev->boot_options);
234 talloc_set_destructor(dev, destroy_device);
239 struct discover_device_param {
242 struct list_item list;
245 void discover_device_set_param(struct discover_device *device,
246 const char *name, const char *value)
248 struct discover_device_param *param;
251 list_for_each_entry(&device->params, param, list) {
252 if (!strcmp(param->name, name)) {
261 param = talloc(device, struct discover_device_param);
262 param->name = talloc_strdup(param, name);
263 list_add(&device->params, ¶m->list);
266 list_remove(¶m->list);
270 talloc_free(param->value);
273 param->value = talloc_strdup(param, value);
276 const char *discover_device_get_param(struct discover_device *device,
279 struct discover_device_param *param;
281 list_for_each_entry(&device->params, param, list) {
282 if (!strcmp(param->name, name))
288 struct device_handler *device_handler_init(struct discover_server *server,
289 struct waitset *waitset, int dry_run)
291 struct device_handler *handler;
294 handler = talloc_zero(NULL, struct device_handler);
295 handler->server = server;
296 handler->waitset = waitset;
297 handler->dry_run = dry_run;
298 handler->autoboot_enabled = config_get()->autoboot_enabled;
300 list_init(&handler->unresolved_boot_options);
302 /* set up our mount point base */
303 pb_mkdir_recursive(mount_base());
307 if (config_get()->safe_mode)
310 rc = device_handler_init_sources(handler);
312 talloc_free(handler);
319 void device_handler_reinit(struct device_handler *handler)
321 struct discover_boot_option *opt, *tmp;
324 device_handler_cancel_default(handler);
326 /* free unresolved boot options */
327 list_for_each_entry_safe(&handler->unresolved_boot_options,
330 list_init(&handler->unresolved_boot_options);
332 /* drop all devices */
333 for (i = 0; i < handler->n_devices; i++) {
334 discover_server_notify_device_remove(handler->server,
335 handler->devices[i]->device);
336 talloc_free(handler->devices[i]);
339 talloc_free(handler->devices);
340 handler->devices = NULL;
341 handler->n_devices = 0;
343 device_handler_reinit_sources(handler);
346 void device_handler_remove(struct device_handler *handler,
347 struct discover_device *device)
349 struct discover_boot_option *opt, *tmp;
352 for (i = 0; i < handler->n_devices; i++)
353 if (handler->devices[i] == device)
356 if (i == handler->n_devices) {
361 /* Free any unresolved options, as they're currently allocated
362 * against the handler */
363 list_for_each_entry_safe(&handler->unresolved_boot_options,
365 if (opt->device != device)
367 list_remove(&opt->list);
371 /* if this is a network device, we have to unregister it from the
373 if (device->device->type == DEVICE_TYPE_NETWORK)
374 network_unregister_device(handler->network, device);
376 handler->n_devices--;
377 memmove(&handler->devices[i], &handler->devices[i + 1],
378 (handler->n_devices - i) * sizeof(handler->devices[0]));
379 handler->devices = talloc_realloc(handler, handler->devices,
380 struct discover_device *, handler->n_devices);
382 if (device->notified)
383 discover_server_notify_device_remove(handler->server,
389 static void boot_status(void *arg, struct boot_status *status)
391 struct device_handler *handler = arg;
393 discover_server_notify_boot_status(handler->server, status);
396 static void countdown_status(struct device_handler *handler,
397 struct discover_boot_option *opt, unsigned int sec)
399 struct boot_status status;
401 status.type = BOOT_STATUS_INFO;
402 status.progress = -1;
403 status.detail = NULL;
404 status.message = talloc_asprintf(handler,
405 _("Booting in %d sec: %s"), sec, opt->option->name);
407 discover_server_notify_boot_status(handler->server, &status);
409 talloc_free(status.message);
412 static int default_timeout(void *arg)
414 struct device_handler *handler = arg;
415 struct discover_boot_option *opt;
417 if (!handler->default_boot_option)
420 if (handler->pending_boot)
423 opt = handler->default_boot_option;
425 if (handler->sec_to_boot) {
426 countdown_status(handler, opt, handler->sec_to_boot);
427 handler->sec_to_boot--;
428 handler->timeout_waiter = waiter_register_timeout(
429 handler->waitset, 1000,
430 default_timeout, handler);
434 handler->timeout_waiter = NULL;
436 pb_log("Timeout expired, booting default option %s\n", opt->option->id);
438 handler->pending_boot = boot(handler, handler->default_boot_option,
439 NULL, handler->dry_run, boot_status, handler);
440 handler->pending_boot_is_default = true;
445 enum ipmi_bootdev ipmi_type;
446 enum device_type device_type;
447 } device_type_map[] = {
448 { IPMI_BOOTDEV_NETWORK, DEVICE_TYPE_NETWORK },
449 { IPMI_BOOTDEV_DISK, DEVICE_TYPE_DISK },
450 { IPMI_BOOTDEV_CDROM, DEVICE_TYPE_OPTICAL },
453 static bool ipmi_device_type_matches(enum ipmi_bootdev ipmi_type,
454 enum device_type device_type)
458 for (i = 0; i < ARRAY_SIZE(device_type_map); i++) {
459 if (device_type_map[i].device_type == device_type)
460 return device_type_map[i].ipmi_type == ipmi_type;
466 static int autoboot_option_priority(const struct config *config,
467 struct discover_boot_option *opt)
469 enum device_type type = opt->device->device->type;
470 const char *uuid = opt->device->uuid;
471 struct autoboot_option *auto_opt;
474 for (i = 0; i < config->n_autoboot_opts; i++) {
475 auto_opt = &config->autoboot_opts[i];
476 if (auto_opt->boot_type == BOOT_DEVICE_UUID)
477 if (!strcmp(auto_opt->uuid, uuid))
478 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
480 if (auto_opt->boot_type == BOOT_DEVICE_TYPE)
481 if (auto_opt->type == type ||
482 auto_opt->type == DEVICE_TYPE_ANY)
483 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
490 * We have different priorities to resolve conflicts between boot options that
491 * report to be the default for their device. This function assigns a priority
494 static enum default_priority default_option_priority(
495 struct discover_boot_option *opt)
497 const struct config *config;
499 config = config_get();
501 /* We give highest priority to IPMI-configured boot options. If
502 * we have an IPMI bootdev configuration set, then we don't allow
503 * any other defaults */
504 if (config->ipmi_bootdev) {
505 bool ipmi_match = ipmi_device_type_matches(config->ipmi_bootdev,
506 opt->device->device->type);
508 return DEFAULT_PRIORITY_REMOTE;
510 pb_debug("handler: disabled default priority due to "
511 "non-matching IPMI type %x\n",
512 config->ipmi_bootdev);
513 return DEFAULT_PRIORITY_DISABLED;
516 /* Next, try to match the option against the user-defined autoboot
517 * options, either by device UUID or type. */
518 if (config->n_autoboot_opts) {
519 int boot_match = autoboot_option_priority(config, opt);
524 /* If the option didn't match any entry in the array, it is disabled */
525 pb_debug("handler: disabled default priority due to "
526 "non-matching UUID or type\n");
527 return DEFAULT_PRIORITY_DISABLED;
530 static void set_default(struct device_handler *handler,
531 struct discover_boot_option *opt)
533 enum default_priority cur_prio, new_prio;
535 if (!handler->autoboot_enabled)
538 pb_debug("handler: new default option: %s\n", opt->option->id);
540 new_prio = default_option_priority(opt);
542 /* Anything outside our range prevents a default boot */
543 if (new_prio >= DEFAULT_PRIORITY_DISABLED)
546 pb_debug("handler: calculated priority %d\n", new_prio);
548 /* Resolve any conflicts: if we have a new default option, it only
549 * replaces the current if it has a higher priority. */
550 if (handler->default_boot_option) {
552 cur_prio = handler->default_boot_option_priority;
554 if (new_prio < cur_prio) {
555 pb_log("handler: new prio %d beats "
556 "old prio %d for %s\n",
558 handler->default_boot_option
560 handler->default_boot_option = opt;
561 handler->default_boot_option_priority = new_prio;
562 /* extend the timeout a little, so the user sees some
563 * indication of the change */
564 handler->sec_to_boot += 2;
570 handler->sec_to_boot = config_get()->autoboot_timeout_sec;
571 handler->default_boot_option = opt;
572 handler->default_boot_option_priority = new_prio;
574 pb_log("handler: boot option %s set as default, timeout %u sec.\n",
575 opt->option->id, handler->sec_to_boot);
577 default_timeout(handler);
580 static bool resource_is_resolved(struct resource *res)
582 return !res || res->resolved;
585 /* We only use this in an assert, which will disappear if we're compiling
586 * with NDEBUG, so we need the 'used' attribute for these builds */
587 static bool __attribute__((used)) boot_option_is_resolved(
588 struct discover_boot_option *opt)
590 return resource_is_resolved(opt->boot_image) &&
591 resource_is_resolved(opt->initrd) &&
592 resource_is_resolved(opt->dtb) &&
593 resource_is_resolved(opt->icon);
596 static bool resource_resolve(struct resource *res, const char *name,
597 struct discover_boot_option *opt,
598 struct device_handler *handler)
600 struct parser *parser = opt->source;
602 if (resource_is_resolved(res))
605 pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
606 opt->option->id, name, parser->name);
607 parser->resolve_resource(handler, res);
609 return res->resolved;
612 static bool boot_option_resolve(struct discover_boot_option *opt,
613 struct device_handler *handler)
615 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
616 resource_resolve(opt->initrd, "initrd", opt, handler) &&
617 resource_resolve(opt->dtb, "dtb", opt, handler) &&
618 resource_resolve(opt->icon, "icon", opt, handler);
621 static void boot_option_finalise(struct device_handler *handler,
622 struct discover_boot_option *opt)
624 assert(boot_option_is_resolved(opt));
626 /* check that the parsers haven't set any of the final data */
627 assert(!opt->option->boot_image_file);
628 assert(!opt->option->initrd_file);
629 assert(!opt->option->dtb_file);
630 assert(!opt->option->icon_file);
631 assert(!opt->option->device_id);
634 opt->option->boot_image_file = opt->boot_image->url->full;
636 opt->option->initrd_file = opt->initrd->url->full;
638 opt->option->dtb_file = opt->dtb->url->full;
640 opt->option->icon_file = opt->icon->url->full;
642 opt->option->device_id = opt->device->device->id;
644 if (opt->option->is_default)
645 set_default(handler, opt);
648 static void notify_boot_option(struct device_handler *handler,
649 struct discover_boot_option *opt)
651 struct discover_device *dev = opt->device;
654 discover_server_notify_device_add(handler->server,
655 opt->device->device);
656 dev->notified = true;
657 discover_server_notify_boot_option_add(handler->server, opt->option);
660 static void process_boot_option_queue(struct device_handler *handler)
662 struct discover_boot_option *opt, *tmp;
664 list_for_each_entry_safe(&handler->unresolved_boot_options,
667 pb_debug("queue: attempting resolution for %s\n",
670 if (!boot_option_resolve(opt, handler))
673 pb_debug("\tresolved!\n");
675 list_remove(&opt->list);
676 list_add_tail(&opt->device->boot_options, &opt->list);
677 talloc_steal(opt->device, opt);
678 boot_option_finalise(handler, opt);
679 notify_boot_option(handler, opt);
683 struct discover_context *device_handler_discover_context_create(
684 struct device_handler *handler,
685 struct discover_device *device)
687 struct discover_context *ctx;
689 ctx = talloc_zero(handler, struct discover_context);
690 ctx->device = device;
691 ctx->network = handler->network;
692 list_init(&ctx->boot_options);
698 * context_commit - Commit a temporary discovery context to the handler,
699 * and notify the clients about any new options / devices
701 void device_handler_discover_context_commit(struct device_handler *handler,
702 struct discover_context *ctx)
704 struct discover_device *dev = ctx->device;
705 struct discover_boot_option *opt, *tmp;
707 if (!device_lookup_by_id(handler, dev->device->id))
708 device_handler_add_device(handler, dev);
710 /* move boot options from the context to the device */
711 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
712 list_remove(&opt->list);
714 if (boot_option_resolve(opt, handler)) {
715 pb_log("boot option %s is resolved, "
716 "sending to clients\n",
718 list_add_tail(&dev->boot_options, &opt->list);
719 talloc_steal(dev, opt);
720 boot_option_finalise(handler, opt);
721 notify_boot_option(handler, opt);
723 if (!opt->source->resolve_resource) {
724 pb_log("parser %s gave us an unresolved "
725 "resource (%s), but no way to "
727 opt->source->name, opt->option->id);
730 pb_log("boot option %s is unresolved, "
733 list_add(&handler->unresolved_boot_options,
735 talloc_steal(handler, opt);
741 void device_handler_add_device(struct device_handler *handler,
742 struct discover_device *device)
744 handler->n_devices++;
745 handler->devices = talloc_realloc(handler, handler->devices,
746 struct discover_device *, handler->n_devices);
747 handler->devices[handler->n_devices - 1] = device;
749 if (device->device->type == DEVICE_TYPE_NETWORK)
750 network_register_device(handler->network, device);
753 /* Start discovery on a hotplugged device. The device will be in our devices
754 * array, but has only just been initialised by the hotplug source.
756 int device_handler_discover(struct device_handler *handler,
757 struct discover_device *dev)
759 struct discover_context *ctx;
760 struct boot_status *status;
763 status = talloc_zero(handler, struct boot_status);
764 status->type = BOOT_STATUS_INFO;
765 status->message = talloc_asprintf(status, "Processing %s device %s",
766 device_type_display_name(dev->device->type),
768 boot_status(handler, status);
770 process_boot_option_queue(handler);
772 /* create our context */
773 ctx = device_handler_discover_context_create(handler, dev);
775 rc = mount_device(dev);
779 /* add this device to our system info */
780 system_info_register_blockdev(dev->device->id, dev->uuid,
783 /* run the parsers. This will populate the ctx's boot_option list. */
784 iterate_parsers(ctx);
786 /* add discovered stuff to the handler */
787 device_handler_discover_context_commit(handler, ctx);
790 status->message = talloc_asprintf(status,"Processing %s complete\n",
792 boot_status(handler, status);
800 /* Incoming dhcp event */
801 int device_handler_dhcp(struct device_handler *handler,
802 struct discover_device *dev, struct event *event)
804 struct discover_context *ctx;
805 struct boot_status *status;
807 status = talloc_zero(handler, struct boot_status);
808 status->type = BOOT_STATUS_INFO;
809 status->message = talloc_asprintf(status, "Processing dhcp event on %s",
811 boot_status(handler, status);
813 /* create our context */
814 ctx = device_handler_discover_context_create(handler, dev);
817 iterate_parsers(ctx);
819 device_handler_discover_context_commit(handler, ctx);
821 status->message = talloc_asprintf(status,"Processing %s complete\n",
823 boot_status(handler, status);
831 /* incoming conf event */
832 int device_handler_conf(struct device_handler *handler,
833 struct discover_device *dev, struct pb_url *url)
835 struct discover_context *ctx;
836 struct boot_status *status;
838 status = talloc_zero(handler, struct boot_status);
839 status->type = BOOT_STATUS_INFO;
840 status->message = talloc_asprintf(status, "Processing user config");
841 boot_status(handler, status);
843 /* create our context */
844 ctx = device_handler_discover_context_create(handler, dev);
847 iterate_parsers(ctx);
849 device_handler_discover_context_commit(handler, ctx);
851 status->message = talloc_asprintf(status,
852 "Processing user config complete");
853 boot_status(handler, status);
861 static struct discover_boot_option *find_boot_option_by_id(
862 struct device_handler *handler, const char *id)
866 for (i = 0; i < handler->n_devices; i++) {
867 struct discover_device *dev = handler->devices[i];
868 struct discover_boot_option *opt;
870 list_for_each_entry(&dev->boot_options, opt, list)
871 if (!strcmp(opt->option->id, id))
878 void device_handler_boot(struct device_handler *handler,
879 struct boot_command *cmd)
881 struct discover_boot_option *opt = NULL;
883 if (cmd->option_id && strlen(cmd->option_id))
884 opt = find_boot_option_by_id(handler, cmd->option_id);
886 if (handler->pending_boot)
887 boot_cancel(handler->pending_boot);
891 handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
892 boot_status, handler);
893 handler->pending_boot_is_default = false;
896 void device_handler_cancel_default(struct device_handler *handler)
898 struct boot_status status;
900 if (handler->timeout_waiter)
901 waiter_remove(handler->timeout_waiter);
903 handler->timeout_waiter = NULL;
904 handler->autoboot_enabled = false;
906 /* we only send status if we had a default boot option queued */
907 if (!handler->default_boot_option)
910 pb_log("Cancelling default boot option\n");
912 if (handler->pending_boot && handler->pending_boot_is_default) {
913 boot_cancel(handler->pending_boot);
914 handler->pending_boot = NULL;
915 handler->pending_boot_is_default = false;
918 handler->default_boot_option = NULL;
920 status.type = BOOT_STATUS_INFO;
921 status.progress = -1;
922 status.detail = NULL;
923 status.message = _("Default boot cancelled");
925 discover_server_notify_boot_status(handler->server, &status);
928 void device_handler_update_config(struct device_handler *handler,
929 struct config *config)
933 rc = config_set(config);
937 discover_server_notify_config(handler->server, config);
938 device_handler_update_lang(config->lang);
939 device_handler_reinit(handler);
942 static char *device_from_addr(void *ctx, struct pb_url *url)
944 char *ipaddr, *buf, *tok, *dev = NULL;
945 const char *delim = " ";
946 struct sockaddr_in *ip;
947 struct sockaddr_in si;
948 struct addrinfo *res;
952 /* Note: IPv4 only */
953 rc = inet_pton(AF_INET, url->host, &(si.sin_addr));
957 /* need to turn hostname into a valid IP */
958 rc = getaddrinfo(url->host, NULL, NULL, &res);
960 pb_debug("%s: Invalid URL\n",__func__);
963 ipaddr = talloc_array(ctx,char,INET_ADDRSTRLEN);
964 ip = (struct sockaddr_in *) res->ai_addr;
965 inet_ntop(AF_INET, &(ip->sin_addr), ipaddr, INET_ADDRSTRLEN);
969 const char *argv[] = {
971 "route", "show", "to", "match",
976 p = process_create(ctx);
978 p->path = pb_system_apps.ip;
980 p->keep_stdout = true;
982 rc = process_run_sync(p);
985 /* ip has complained for some reason; most likely
986 * there is no route to the host - bail out */
987 pb_debug("%s: No route to %s\n",__func__,url->host);
992 /* If a route is found, ip-route output will be of the form
993 * "... dev DEVNAME ... " */
994 tok = strtok(buf, delim);
996 if (!strcmp(tok, "dev")) {
997 tok = strtok(NULL, delim);
998 dev = talloc_strdup(ctx, tok);
1001 tok = strtok(NULL, delim);
1006 pb_debug("%s: Found interface '%s'\n", __func__,dev);
1011 void device_handler_process_url(struct device_handler *handler,
1014 struct discover_context *ctx;
1015 struct discover_device *dev;
1016 struct boot_status *status;
1017 struct pb_url *pb_url;
1018 struct event *event;
1019 struct param *param;
1021 status = talloc(handler, struct boot_status);
1023 status->type = BOOT_STATUS_ERROR;
1024 status->progress = 0;
1025 status->detail = talloc_asprintf(status,
1026 _("Received config URL %s"), url);
1028 if (!handler->network) {
1029 status->message = talloc_asprintf(handler,
1030 _("No network configured"));
1034 event = talloc(handler, struct event);
1035 event->type = EVENT_TYPE_USER;
1036 event->action = EVENT_ACTION_CONF;
1038 event->params = talloc_array(event, struct param, 1);
1039 param = &event->params[0];
1040 param->name = talloc_strdup(event, "pxeconffile");
1041 param->value = talloc_strdup(event, url);
1042 event->n_params = 1;
1044 pb_url = pb_url_parse(event, event->params->value);
1045 if (!pb_url || !pb_url->host) {
1046 status->message = talloc_asprintf(handler,
1047 _("Invalid config URL!"));
1051 event->device = device_from_addr(event, pb_url);
1052 if (!event->device) {
1053 status->message = talloc_asprintf(status,
1054 _("Unable to route to host %s"),
1059 dev = discover_device_create(handler, event->device);
1060 ctx = device_handler_discover_context_create(handler, dev);
1063 iterate_parsers(ctx);
1065 device_handler_discover_context_commit(handler, ctx);
1069 status->type = BOOT_STATUS_INFO;
1070 status->message = talloc_asprintf(status, _("Config file %s parsed"),
1073 boot_status(handler, status);
1074 talloc_free(status);
1077 #ifndef PETITBOOT_TEST
1079 static void device_handler_update_lang(const char *lang)
1081 const char *cur_lang;
1086 cur_lang = setlocale(LC_ALL, NULL);
1087 if (cur_lang && !strcmp(cur_lang, lang))
1090 setlocale(LC_ALL, lang);
1093 static int device_handler_init_sources(struct device_handler *handler)
1095 /* init our device sources: udev, network and user events */
1096 handler->udev = udev_init(handler, handler->waitset);
1100 handler->network = network_init(handler, handler->waitset,
1102 if (!handler->network)
1105 handler->user_event = user_event_init(handler, handler->waitset);
1106 if (!handler->user_event)
1112 static void device_handler_reinit_sources(struct device_handler *handler)
1114 /* if we haven't initialised sources previously (becuase we started in
1115 * safe mode), then init once here. */
1116 if (!(handler->udev || handler->network || handler->user_event)) {
1117 device_handler_init_sources(handler);
1121 udev_reinit(handler->udev);
1123 network_shutdown(handler->network);
1124 handler->network = network_init(handler, handler->waitset,
1128 static const char *fs_parameters(unsigned int rw_flags, const char *fstype)
1130 if ((rw_flags | MS_RDONLY) != MS_RDONLY)
1133 /* Avoid writing back to the disk on journaled filesystems */
1134 if (!strncmp(fstype, "ext4", strlen("ext4")))
1135 return "norecovery";
1136 if (!strncmp(fstype, "xfs", strlen("xfs")))
1137 return "norecovery";
1142 static bool check_existing_mount(struct discover_device *dev)
1144 struct stat devstat, mntstat;
1149 rc = stat(dev->device_path, &devstat);
1151 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
1155 if (!S_ISBLK(devstat.st_mode)) {
1156 pb_debug("%s: %s isn't a block device?\n", __func__,
1161 fp = fopen("/proc/self/mounts", "r");
1164 mnt = getmntent(fp);
1168 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
1171 rc = stat(mnt->mnt_fsname, &mntstat);
1175 if (!S_ISBLK(mntstat.st_mode))
1178 if (mntstat.st_rdev == devstat.st_rdev) {
1179 dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
1180 dev->mounted_rw = !!hasmntopt(mnt, "rw");
1181 dev->mounted = true;
1182 dev->unmount = false;
1184 pb_debug("%s: %s is already mounted (r%c) at %s\n",
1185 __func__, dev->device_path,
1186 dev->mounted_rw ? 'w' : 'o',
1197 static int mount_device(struct discover_device *dev)
1202 if (!dev->device_path)
1208 if (check_existing_mount(dev))
1211 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1215 /* ext3 treats the norecovery option as an error, so mount the device
1216 * as an ext4 filesystem instead */
1217 if (!strncmp(fstype, "ext3", strlen("ext3"))) {
1218 pb_debug("Mounting ext3 filesystem as ext4\n");
1219 fstype = talloc_asprintf(dev, "ext4");
1222 dev->mount_path = join_paths(dev, mount_base(),
1225 if (pb_mkdir_recursive(dev->mount_path)) {
1226 pb_log("couldn't create mount directory %s: %s\n",
1227 dev->mount_path, strerror(errno));
1231 pb_log("mounting device %s read-only\n", dev->device_path);
1233 rc = mount(dev->device_path, dev->mount_path, fstype,
1234 MS_RDONLY | MS_SILENT,
1235 fs_parameters(MS_RDONLY, fstype));
1237 dev->mounted = true;
1238 dev->mounted_rw = false;
1239 dev->unmount = true;
1243 pb_log("couldn't mount device %s: mount failed: %s\n",
1244 dev->device_path, strerror(errno));
1246 pb_rmdir_recursive(mount_base(), dev->mount_path);
1248 talloc_free(dev->mount_path);
1249 dev->mount_path = NULL;
1253 static int umount_device(struct discover_device *dev)
1257 if (!dev->mounted || !dev->unmount)
1260 pb_log("unmounting device %s\n", dev->device_path);
1261 rc = umount(dev->mount_path);
1265 dev->mounted = false;
1267 pb_rmdir_recursive(mount_base(), dev->mount_path);
1269 talloc_free(dev->mount_path);
1270 dev->mount_path = NULL;
1275 int device_request_write(struct discover_device *dev, bool *release)
1285 if (dev->mounted_rw)
1288 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1290 pb_log("remounting device %s read-write\n", dev->device_path);
1292 rc = umount(dev->mount_path);
1294 pb_log("Failed to unmount %s\n", dev->mount_path);
1297 rc = mount(dev->device_path, dev->mount_path, fstype,
1299 fs_parameters(MS_REMOUNT, fstype));
1303 dev->mounted_rw = true;
1308 pb_log("Unable to remount device %s read-write\n", dev->device_path);
1309 rc = mount(dev->device_path, dev->mount_path, fstype,
1310 MS_RDONLY | MS_SILENT,
1311 fs_parameters(MS_RDONLY, fstype));
1313 pb_log("Unable to recover mount for %s\n", dev->device_path);
1317 void device_release_write(struct discover_device *dev, bool release)
1324 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1326 pb_log("remounting device %s read-only\n", dev->device_path);
1327 mount(dev->device_path, dev->mount_path, "",
1328 MS_REMOUNT | MS_RDONLY | MS_SILENT,
1329 fs_parameters(MS_RDONLY, fstype));
1330 dev->mounted_rw = false;
1335 static void device_handler_update_lang(const char *lang __attribute__((unused)))
1339 static int device_handler_init_sources(
1340 struct device_handler *handler __attribute__((unused)))
1345 static void device_handler_reinit_sources(
1346 struct device_handler *handler __attribute__((unused)))
1350 static int umount_device(struct discover_device *dev __attribute__((unused)))
1355 static int __attribute__((unused)) mount_device(
1356 struct discover_device *dev __attribute__((unused)))
1361 int device_request_write(struct discover_device *dev __attribute__((unused)),
1368 void device_release_write(struct discover_device *dev __attribute__((unused)),
1369 bool release __attribute__((unused)))