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);
337 talloc_free(handler->devices);
338 handler->devices = NULL;
339 handler->n_devices = 0;
341 device_handler_reinit_sources(handler);
344 void device_handler_remove(struct device_handler *handler,
345 struct discover_device *device)
347 struct discover_boot_option *opt, *tmp;
350 for (i = 0; i < handler->n_devices; i++)
351 if (handler->devices[i] == device)
354 if (i == handler->n_devices) {
359 /* Free any unresolved options, as they're currently allocated
360 * against the handler */
361 list_for_each_entry_safe(&handler->unresolved_boot_options,
363 if (opt->device != device)
365 list_remove(&opt->list);
369 /* if this is a network device, we have to unregister it from the
371 if (device->device->type == DEVICE_TYPE_NETWORK)
372 network_unregister_device(handler->network, device);
374 handler->n_devices--;
375 memmove(&handler->devices[i], &handler->devices[i + 1],
376 (handler->n_devices - i) * sizeof(handler->devices[0]));
377 handler->devices = talloc_realloc(handler, handler->devices,
378 struct discover_device *, handler->n_devices);
380 if (device->notified)
381 discover_server_notify_device_remove(handler->server,
387 static void boot_status(void *arg, struct boot_status *status)
389 struct device_handler *handler = arg;
391 discover_server_notify_boot_status(handler->server, status);
394 static void countdown_status(struct device_handler *handler,
395 struct discover_boot_option *opt, unsigned int sec)
397 struct boot_status status;
399 status.type = BOOT_STATUS_INFO;
400 status.progress = -1;
401 status.detail = NULL;
402 status.message = talloc_asprintf(handler,
403 _("Booting in %d sec: %s"), sec, opt->option->name);
405 discover_server_notify_boot_status(handler->server, &status);
407 talloc_free(status.message);
410 static int default_timeout(void *arg)
412 struct device_handler *handler = arg;
413 struct discover_boot_option *opt;
415 if (!handler->default_boot_option)
418 if (handler->pending_boot)
421 opt = handler->default_boot_option;
423 if (handler->sec_to_boot) {
424 countdown_status(handler, opt, handler->sec_to_boot);
425 handler->sec_to_boot--;
426 handler->timeout_waiter = waiter_register_timeout(
427 handler->waitset, 1000,
428 default_timeout, handler);
432 handler->timeout_waiter = NULL;
434 pb_log("Timeout expired, booting default option %s\n", opt->option->id);
436 handler->pending_boot = boot(handler, handler->default_boot_option,
437 NULL, handler->dry_run, boot_status, handler);
438 handler->pending_boot_is_default = true;
443 enum ipmi_bootdev ipmi_type;
444 enum device_type device_type;
445 } device_type_map[] = {
446 { IPMI_BOOTDEV_NETWORK, DEVICE_TYPE_NETWORK },
447 { IPMI_BOOTDEV_DISK, DEVICE_TYPE_DISK },
448 { IPMI_BOOTDEV_CDROM, DEVICE_TYPE_OPTICAL },
451 static bool ipmi_device_type_matches(enum ipmi_bootdev ipmi_type,
452 enum device_type device_type)
456 for (i = 0; i < ARRAY_SIZE(device_type_map); i++) {
457 if (device_type_map[i].device_type == device_type)
458 return device_type_map[i].ipmi_type == ipmi_type;
464 static int autoboot_option_priority(const struct config *config,
465 struct discover_boot_option *opt)
467 enum device_type type = opt->device->device->type;
468 const char *uuid = opt->device->uuid;
469 struct autoboot_option *auto_opt;
472 for (i = 0; i < config->n_autoboot_opts; i++) {
473 auto_opt = &config->autoboot_opts[i];
474 if (auto_opt->boot_type == BOOT_DEVICE_UUID)
475 if (!strcmp(auto_opt->uuid, uuid))
476 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
478 if (auto_opt->boot_type == BOOT_DEVICE_TYPE)
479 if (auto_opt->type == type ||
480 auto_opt->type == DEVICE_TYPE_ANY)
481 return DEFAULT_PRIORITY_LOCAL_FIRST + i;
488 * We have different priorities to resolve conflicts between boot options that
489 * report to be the default for their device. This function assigns a priority
492 static enum default_priority default_option_priority(
493 struct discover_boot_option *opt)
495 const struct config *config;
497 config = config_get();
499 /* We give highest priority to IPMI-configured boot options. If
500 * we have an IPMI bootdev configuration set, then we don't allow
501 * any other defaults */
502 if (config->ipmi_bootdev) {
503 bool ipmi_match = ipmi_device_type_matches(config->ipmi_bootdev,
504 opt->device->device->type);
506 return DEFAULT_PRIORITY_REMOTE;
508 pb_debug("handler: disabled default priority due to "
509 "non-matching IPMI type %x\n",
510 config->ipmi_bootdev);
511 return DEFAULT_PRIORITY_DISABLED;
514 /* Next, try to match the option against the user-defined autoboot
515 * options, either by device UUID or type. */
516 if (config->n_autoboot_opts) {
517 int boot_match = autoboot_option_priority(config, opt);
522 /* If the option didn't match any entry in the array, it is disabled */
523 pb_debug("handler: disabled default priority due to "
524 "non-matching UUID or type\n");
525 return DEFAULT_PRIORITY_DISABLED;
528 static void set_default(struct device_handler *handler,
529 struct discover_boot_option *opt)
531 enum default_priority cur_prio, new_prio;
533 if (!handler->autoboot_enabled)
536 pb_debug("handler: new default option: %s\n", opt->option->id);
538 new_prio = default_option_priority(opt);
540 /* Anything outside our range prevents a default boot */
541 if (new_prio >= DEFAULT_PRIORITY_DISABLED)
544 pb_debug("handler: calculated priority %d\n", new_prio);
546 /* Resolve any conflicts: if we have a new default option, it only
547 * replaces the current if it has a higher priority. */
548 if (handler->default_boot_option) {
550 cur_prio = handler->default_boot_option_priority;
552 if (new_prio < cur_prio) {
553 pb_log("handler: new prio %d beats "
554 "old prio %d for %s\n",
556 handler->default_boot_option
558 handler->default_boot_option = opt;
559 handler->default_boot_option_priority = new_prio;
560 /* extend the timeout a little, so the user sees some
561 * indication of the change */
562 handler->sec_to_boot += 2;
568 handler->sec_to_boot = config_get()->autoboot_timeout_sec;
569 handler->default_boot_option = opt;
570 handler->default_boot_option_priority = new_prio;
572 pb_log("handler: boot option %s set as default, timeout %u sec.\n",
573 opt->option->id, handler->sec_to_boot);
575 default_timeout(handler);
578 static bool resource_is_resolved(struct resource *res)
580 return !res || res->resolved;
583 /* We only use this in an assert, which will disappear if we're compiling
584 * with NDEBUG, so we need the 'used' attribute for these builds */
585 static bool __attribute__((used)) boot_option_is_resolved(
586 struct discover_boot_option *opt)
588 return resource_is_resolved(opt->boot_image) &&
589 resource_is_resolved(opt->initrd) &&
590 resource_is_resolved(opt->dtb) &&
591 resource_is_resolved(opt->icon);
594 static bool resource_resolve(struct resource *res, const char *name,
595 struct discover_boot_option *opt,
596 struct device_handler *handler)
598 struct parser *parser = opt->source;
600 if (resource_is_resolved(res))
603 pb_debug("Attempting to resolve resource %s->%s with parser %s\n",
604 opt->option->id, name, parser->name);
605 parser->resolve_resource(handler, res);
607 return res->resolved;
610 static bool boot_option_resolve(struct discover_boot_option *opt,
611 struct device_handler *handler)
613 return resource_resolve(opt->boot_image, "boot_image", opt, handler) &&
614 resource_resolve(opt->initrd, "initrd", opt, handler) &&
615 resource_resolve(opt->dtb, "dtb", opt, handler) &&
616 resource_resolve(opt->icon, "icon", opt, handler);
619 static void boot_option_finalise(struct device_handler *handler,
620 struct discover_boot_option *opt)
622 assert(boot_option_is_resolved(opt));
624 /* check that the parsers haven't set any of the final data */
625 assert(!opt->option->boot_image_file);
626 assert(!opt->option->initrd_file);
627 assert(!opt->option->dtb_file);
628 assert(!opt->option->icon_file);
629 assert(!opt->option->device_id);
632 opt->option->boot_image_file = opt->boot_image->url->full;
634 opt->option->initrd_file = opt->initrd->url->full;
636 opt->option->dtb_file = opt->dtb->url->full;
638 opt->option->icon_file = opt->icon->url->full;
640 opt->option->device_id = opt->device->device->id;
642 if (opt->option->is_default)
643 set_default(handler, opt);
646 static void notify_boot_option(struct device_handler *handler,
647 struct discover_boot_option *opt)
649 struct discover_device *dev = opt->device;
652 discover_server_notify_device_add(handler->server,
653 opt->device->device);
654 dev->notified = true;
655 discover_server_notify_boot_option_add(handler->server, opt->option);
658 static void process_boot_option_queue(struct device_handler *handler)
660 struct discover_boot_option *opt, *tmp;
662 list_for_each_entry_safe(&handler->unresolved_boot_options,
665 pb_debug("queue: attempting resolution for %s\n",
668 if (!boot_option_resolve(opt, handler))
671 pb_debug("\tresolved!\n");
673 list_remove(&opt->list);
674 list_add_tail(&opt->device->boot_options, &opt->list);
675 talloc_steal(opt->device, opt);
676 boot_option_finalise(handler, opt);
677 notify_boot_option(handler, opt);
681 struct discover_context *device_handler_discover_context_create(
682 struct device_handler *handler,
683 struct discover_device *device)
685 struct discover_context *ctx;
687 ctx = talloc_zero(handler, struct discover_context);
688 ctx->device = device;
689 ctx->network = handler->network;
690 list_init(&ctx->boot_options);
696 * context_commit - Commit a temporary discovery context to the handler,
697 * and notify the clients about any new options / devices
699 void device_handler_discover_context_commit(struct device_handler *handler,
700 struct discover_context *ctx)
702 struct discover_device *dev = ctx->device;
703 struct discover_boot_option *opt, *tmp;
705 if (!device_lookup_by_id(handler, dev->device->id))
706 device_handler_add_device(handler, dev);
708 /* move boot options from the context to the device */
709 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
710 list_remove(&opt->list);
712 if (boot_option_resolve(opt, handler)) {
713 pb_log("boot option %s is resolved, "
714 "sending to clients\n",
716 list_add_tail(&dev->boot_options, &opt->list);
717 talloc_steal(dev, opt);
718 boot_option_finalise(handler, opt);
719 notify_boot_option(handler, opt);
721 if (!opt->source->resolve_resource) {
722 pb_log("parser %s gave us an unresolved "
723 "resource (%s), but no way to "
725 opt->source->name, opt->option->id);
728 pb_log("boot option %s is unresolved, "
731 list_add(&handler->unresolved_boot_options,
733 talloc_steal(handler, opt);
739 void device_handler_add_device(struct device_handler *handler,
740 struct discover_device *device)
742 handler->n_devices++;
743 handler->devices = talloc_realloc(handler, handler->devices,
744 struct discover_device *, handler->n_devices);
745 handler->devices[handler->n_devices - 1] = device;
747 if (device->device->type == DEVICE_TYPE_NETWORK)
748 network_register_device(handler->network, device);
751 /* Start discovery on a hotplugged device. The device will be in our devices
752 * array, but has only just been initialised by the hotplug source.
754 int device_handler_discover(struct device_handler *handler,
755 struct discover_device *dev)
757 struct discover_context *ctx;
758 struct boot_status *status;
761 status = talloc_zero(handler, struct boot_status);
762 status->type = BOOT_STATUS_INFO;
763 status->message = talloc_asprintf(status, "Processing %s device %s",
764 device_type_display_name(dev->device->type),
766 boot_status(handler, status);
768 process_boot_option_queue(handler);
770 /* create our context */
771 ctx = device_handler_discover_context_create(handler, dev);
773 rc = mount_device(dev);
777 /* add this device to our system info */
778 system_info_register_blockdev(dev->device->id, dev->uuid,
781 /* run the parsers. This will populate the ctx's boot_option list. */
782 iterate_parsers(ctx);
784 /* add discovered stuff to the handler */
785 device_handler_discover_context_commit(handler, ctx);
788 status->message = talloc_asprintf(status,"Processing %s complete\n",
790 boot_status(handler, status);
798 /* Incoming dhcp event */
799 int device_handler_dhcp(struct device_handler *handler,
800 struct discover_device *dev, struct event *event)
802 struct discover_context *ctx;
803 struct boot_status *status;
805 status = talloc_zero(handler, struct boot_status);
806 status->type = BOOT_STATUS_INFO;
807 status->message = talloc_asprintf(status, "Processing dhcp event on %s",
809 boot_status(handler, status);
811 /* create our context */
812 ctx = device_handler_discover_context_create(handler, dev);
815 iterate_parsers(ctx);
817 device_handler_discover_context_commit(handler, ctx);
819 status->message = talloc_asprintf(status,"Processing %s complete\n",
821 boot_status(handler, status);
829 /* incoming conf event */
830 int device_handler_conf(struct device_handler *handler,
831 struct discover_device *dev, struct pb_url *url)
833 struct discover_context *ctx;
834 struct boot_status *status;
836 status = talloc_zero(handler, struct boot_status);
837 status->type = BOOT_STATUS_INFO;
838 status->message = talloc_asprintf(status, "Processing user config");
839 boot_status(handler, status);
841 /* create our context */
842 ctx = device_handler_discover_context_create(handler, dev);
845 iterate_parsers(ctx);
847 device_handler_discover_context_commit(handler, ctx);
849 status->message = talloc_asprintf(status,
850 "Processing user config complete");
851 boot_status(handler, status);
859 static struct discover_boot_option *find_boot_option_by_id(
860 struct device_handler *handler, const char *id)
864 for (i = 0; i < handler->n_devices; i++) {
865 struct discover_device *dev = handler->devices[i];
866 struct discover_boot_option *opt;
868 list_for_each_entry(&dev->boot_options, opt, list)
869 if (!strcmp(opt->option->id, id))
876 void device_handler_boot(struct device_handler *handler,
877 struct boot_command *cmd)
879 struct discover_boot_option *opt = NULL;
881 if (cmd->option_id && strlen(cmd->option_id))
882 opt = find_boot_option_by_id(handler, cmd->option_id);
884 if (handler->pending_boot)
885 boot_cancel(handler->pending_boot);
889 handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
890 boot_status, handler);
891 handler->pending_boot_is_default = false;
894 void device_handler_cancel_default(struct device_handler *handler)
896 struct boot_status status;
898 if (handler->timeout_waiter)
899 waiter_remove(handler->timeout_waiter);
901 handler->timeout_waiter = NULL;
902 handler->autoboot_enabled = false;
904 /* we only send status if we had a default boot option queued */
905 if (!handler->default_boot_option)
908 pb_log("Cancelling default boot option\n");
910 if (handler->pending_boot && handler->pending_boot_is_default) {
911 boot_cancel(handler->pending_boot);
912 handler->pending_boot = NULL;
913 handler->pending_boot_is_default = false;
916 handler->default_boot_option = NULL;
918 status.type = BOOT_STATUS_INFO;
919 status.progress = -1;
920 status.detail = NULL;
921 status.message = _("Default boot cancelled");
923 discover_server_notify_boot_status(handler->server, &status);
926 void device_handler_update_config(struct device_handler *handler,
927 struct config *config)
931 rc = config_set(config);
935 discover_server_notify_config(handler->server, config);
936 device_handler_update_lang(config->lang);
937 device_handler_reinit(handler);
940 static char *device_from_addr(void *ctx, struct pb_url *url)
942 char *ipaddr, *buf, *tok, *dev = NULL;
943 const char *delim = " ";
944 struct sockaddr_in *ip;
945 struct sockaddr_in si;
946 struct addrinfo *res;
950 /* Note: IPv4 only */
951 rc = inet_pton(AF_INET, url->host, &(si.sin_addr));
955 /* need to turn hostname into a valid IP */
956 rc = getaddrinfo(url->host, NULL, NULL, &res);
958 pb_debug("%s: Invalid URL\n",__func__);
961 ipaddr = talloc_array(ctx,char,INET_ADDRSTRLEN);
962 ip = (struct sockaddr_in *) res->ai_addr;
963 inet_ntop(AF_INET, &(ip->sin_addr), ipaddr, INET_ADDRSTRLEN);
967 const char *argv[] = {
969 "route", "show", "to", "match",
974 p = process_create(ctx);
976 p->path = pb_system_apps.ip;
978 p->keep_stdout = true;
980 rc = process_run_sync(p);
983 /* ip has complained for some reason; most likely
984 * there is no route to the host - bail out */
985 pb_debug("%s: No route to %s\n",__func__,url->host);
990 /* If a route is found, ip-route output will be of the form
991 * "... dev DEVNAME ... " */
992 tok = strtok(buf, delim);
994 if (!strcmp(tok, "dev")) {
995 tok = strtok(NULL, delim);
996 dev = talloc_strdup(ctx, tok);
999 tok = strtok(NULL, delim);
1004 pb_debug("%s: Found interface '%s'\n", __func__,dev);
1009 void device_handler_process_url(struct device_handler *handler,
1012 struct discover_context *ctx;
1013 struct discover_device *dev;
1014 struct boot_status *status;
1015 struct pb_url *pb_url;
1016 struct event *event;
1017 struct param *param;
1019 status = talloc(handler, struct boot_status);
1021 status->type = BOOT_STATUS_ERROR;
1022 status->progress = 0;
1023 status->detail = talloc_asprintf(status,
1024 _("Received config URL %s"), url);
1026 if (!handler->network) {
1027 status->message = talloc_asprintf(handler,
1028 _("No network configured"));
1032 event = talloc(handler, struct event);
1033 event->type = EVENT_TYPE_USER;
1034 event->action = EVENT_ACTION_CONF;
1036 event->params = talloc_array(event, struct param, 1);
1037 param = &event->params[0];
1038 param->name = talloc_strdup(event, "pxeconffile");
1039 param->value = talloc_strdup(event, url);
1040 event->n_params = 1;
1042 pb_url = pb_url_parse(event, event->params->value);
1043 if (!pb_url || !pb_url->host) {
1044 status->message = talloc_asprintf(handler,
1045 _("Invalid config URL!"));
1049 event->device = device_from_addr(event, pb_url);
1050 if (!event->device) {
1051 status->message = talloc_asprintf(status,
1052 _("Unable to route to host %s"),
1057 dev = discover_device_create(handler, event->device);
1058 ctx = device_handler_discover_context_create(handler, dev);
1061 iterate_parsers(ctx);
1063 device_handler_discover_context_commit(handler, ctx);
1067 status->type = BOOT_STATUS_INFO;
1068 status->message = talloc_asprintf(status, _("Config file %s parsed"),
1071 boot_status(handler, status);
1072 talloc_free(status);
1075 #ifndef PETITBOOT_TEST
1077 static void device_handler_update_lang(const char *lang)
1079 const char *cur_lang;
1084 cur_lang = setlocale(LC_ALL, NULL);
1085 if (cur_lang && !strcmp(cur_lang, lang))
1088 setlocale(LC_ALL, lang);
1091 static int device_handler_init_sources(struct device_handler *handler)
1093 /* init our device sources: udev, network and user events */
1094 handler->udev = udev_init(handler, handler->waitset);
1098 handler->network = network_init(handler, handler->waitset,
1100 if (!handler->network)
1103 handler->user_event = user_event_init(handler, handler->waitset);
1104 if (!handler->user_event)
1110 static void device_handler_reinit_sources(struct device_handler *handler)
1112 /* if we haven't initialised sources previously (becuase we started in
1113 * safe mode), then init once here. */
1114 if (!(handler->udev || handler->network || handler->user_event)) {
1115 device_handler_init_sources(handler);
1119 udev_reinit(handler->udev);
1121 network_shutdown(handler->network);
1122 handler->network = network_init(handler, handler->waitset,
1126 static const char *fs_parameters(unsigned int rw_flags, const char *fstype)
1128 if ((rw_flags | MS_RDONLY) != MS_RDONLY)
1131 /* Avoid writing back to the disk on journaled filesystems */
1132 if (!strncmp(fstype, "ext4", strlen("ext4")))
1133 return "norecovery";
1134 if (!strncmp(fstype, "xfs", strlen("xfs")))
1135 return "norecovery";
1140 static bool check_existing_mount(struct discover_device *dev)
1142 struct stat devstat, mntstat;
1147 rc = stat(dev->device_path, &devstat);
1149 pb_debug("%s: stat failed: %s\n", __func__, strerror(errno));
1153 if (!S_ISBLK(devstat.st_mode)) {
1154 pb_debug("%s: %s isn't a block device?\n", __func__,
1159 fp = fopen("/proc/self/mounts", "r");
1162 mnt = getmntent(fp);
1166 if (!mnt->mnt_fsname || mnt->mnt_fsname[0] != '/')
1169 rc = stat(mnt->mnt_fsname, &mntstat);
1173 if (!S_ISBLK(mntstat.st_mode))
1176 if (mntstat.st_rdev == devstat.st_rdev) {
1177 dev->mount_path = talloc_strdup(dev, mnt->mnt_dir);
1178 dev->mounted_rw = !!hasmntopt(mnt, "rw");
1179 dev->mounted = true;
1180 dev->unmount = false;
1182 pb_debug("%s: %s is already mounted (r%c) at %s\n",
1183 __func__, dev->device_path,
1184 dev->mounted_rw ? 'w' : 'o',
1195 static int mount_device(struct discover_device *dev)
1200 if (!dev->device_path)
1206 if (check_existing_mount(dev))
1209 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1213 /* ext3 treats the norecovery option as an error, so mount the device
1214 * as an ext4 filesystem instead */
1215 if (!strncmp(fstype, "ext3", strlen("ext3"))) {
1216 pb_debug("Mounting ext3 filesystem as ext4\n");
1217 fstype = talloc_asprintf(dev, "ext4");
1220 dev->mount_path = join_paths(dev, mount_base(),
1223 if (pb_mkdir_recursive(dev->mount_path)) {
1224 pb_log("couldn't create mount directory %s: %s\n",
1225 dev->mount_path, strerror(errno));
1229 pb_log("mounting device %s read-only\n", dev->device_path);
1231 rc = mount(dev->device_path, dev->mount_path, fstype,
1232 MS_RDONLY | MS_SILENT,
1233 fs_parameters(MS_RDONLY, fstype));
1235 dev->mounted = true;
1236 dev->mounted_rw = false;
1237 dev->unmount = true;
1241 pb_log("couldn't mount device %s: mount failed: %s\n",
1242 dev->device_path, strerror(errno));
1244 pb_rmdir_recursive(mount_base(), dev->mount_path);
1246 talloc_free(dev->mount_path);
1247 dev->mount_path = NULL;
1251 static int umount_device(struct discover_device *dev)
1255 if (!dev->mounted || !dev->unmount)
1258 pb_log("unmounting device %s\n", dev->device_path);
1259 rc = umount(dev->mount_path);
1263 dev->mounted = false;
1265 pb_rmdir_recursive(mount_base(), dev->mount_path);
1267 talloc_free(dev->mount_path);
1268 dev->mount_path = NULL;
1273 int device_request_write(struct discover_device *dev, bool *release)
1283 if (dev->mounted_rw)
1286 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1288 pb_log("remounting device %s read-write\n", dev->device_path);
1290 rc = umount(dev->mount_path);
1292 pb_log("Failed to unmount %s\n", dev->mount_path);
1295 rc = mount(dev->device_path, dev->mount_path, fstype,
1297 fs_parameters(MS_REMOUNT, fstype));
1301 dev->mounted_rw = true;
1306 pb_log("Unable to remount device %s read-write\n", dev->device_path);
1307 rc = mount(dev->device_path, dev->mount_path, fstype,
1308 MS_RDONLY | MS_SILENT,
1309 fs_parameters(MS_RDONLY, fstype));
1311 pb_log("Unable to recover mount for %s\n", dev->device_path);
1315 void device_release_write(struct discover_device *dev, bool release)
1322 fstype = discover_device_get_param(dev, "ID_FS_TYPE");
1324 pb_log("remounting device %s read-only\n", dev->device_path);
1325 mount(dev->device_path, dev->mount_path, "",
1326 MS_REMOUNT | MS_RDONLY | MS_SILENT,
1327 fs_parameters(MS_RDONLY, fstype));
1328 dev->mounted_rw = false;
1333 static void device_handler_update_lang(const char *lang __attribute__((unused)))
1337 static int device_handler_init_sources(
1338 struct device_handler *handler __attribute__((unused)))
1343 static void device_handler_reinit_sources(
1344 struct device_handler *handler __attribute__((unused)))
1348 static int umount_device(struct discover_device *dev __attribute__((unused)))
1353 static int __attribute__((unused)) mount_device(
1354 struct discover_device *dev __attribute__((unused)))
1359 int device_request_write(struct discover_device *dev __attribute__((unused)),
1366 void device_release_write(struct discover_device *dev __attribute__((unused)),
1367 bool release __attribute__((unused)))