10 #include <talloc/talloc.h>
11 #include <list/list.h>
13 #include <types/types.h>
14 #include <system/system.h>
17 #include "device-handler.h"
18 #include "discover-server.h"
26 struct device_handler {
27 struct discover_server *server;
30 struct discover_device **devices;
31 unsigned int n_devices;
33 struct list unresolved_boot_options;
36 static bool resource_is_resolved(struct resource *res)
38 return !res || res->resolved;
41 /* We only use this in an assert, which will disappear if we're compiling
42 * with NDEBUG, so we need the 'used' attribute for these builds */
43 static bool __attribute__((used)) boot_option_is_resolved(
44 struct discover_boot_option *opt)
46 return resource_is_resolved(opt->boot_image) &&
47 resource_is_resolved(opt->initrd) &&
48 resource_is_resolved(opt->icon);
51 static bool resource_resolve(struct resource *res, struct parser *parser,
52 struct device_handler *handler)
54 if (resource_is_resolved(res))
57 parser->resolve_resource(handler, res);
62 static bool boot_option_resolve(struct discover_boot_option *opt,
63 struct device_handler *handler)
65 return resource_resolve(opt->boot_image, opt->source, handler) &&
66 resource_resolve(opt->initrd, opt->source, handler) &&
67 resource_resolve(opt->icon, opt->source, handler);
70 static void boot_option_finalise(struct discover_boot_option *opt)
72 assert(boot_option_is_resolved(opt));
74 /* check that the parsers haven't set any of the final data */
75 assert(!opt->option->boot_image_file);
76 assert(!opt->option->initrd_file);
77 assert(!opt->option->icon_file);
78 assert(!opt->option->device_id);
81 opt->option->boot_image_file = opt->boot_image->url->full;
83 opt->option->initrd_file = opt->initrd->url->full;
85 opt->option->icon_file = opt->icon->url->full;
87 opt->option->device_id = opt->device->device->id;
90 static void process_boot_option_queue(struct device_handler *handler)
92 struct discover_boot_option *opt, *tmp;
94 list_for_each_entry_safe(&handler->unresolved_boot_options,
97 if (!boot_option_resolve(opt, handler))
100 list_remove(&opt->list);
101 list_add(&opt->device->boot_options, &opt->list);
102 talloc_steal(opt->device, opt);
103 boot_option_finalise(opt);
104 discover_server_notify_boot_option_add(handler->server,
110 * context_commit - Commit a temporary discovery context to the handler,
111 * and notify the clients about any new options / devices
113 static void context_commit(struct device_handler *handler,
114 struct discover_context *ctx)
116 struct discover_device *dev = ctx->device;
117 struct discover_boot_option *opt, *tmp;
118 unsigned int i, existing_device = 0;
120 /* do we already have this device? */
121 for (i = 0; i < handler->n_devices; i++) {
122 if (ctx->device == handler->devices[i]) {
128 /* if not already present, add the device to the handler's array */
129 if (!existing_device) {
130 handler->n_devices++;
131 handler->devices = talloc_realloc(handler, handler->devices,
132 struct discover_device *, handler->n_devices);
133 handler->devices[handler->n_devices - 1] = dev;
134 talloc_steal(handler, dev);
136 discover_server_notify_device_add(handler->server, dev->device);
138 /* this new device might be able to resolve existing boot
140 process_boot_option_queue(handler);
144 /* move boot options from the context to the device */
145 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
146 list_remove(&opt->list);
148 if (boot_option_resolve(opt, handler)) {
149 list_add(&dev->boot_options, &opt->list);
150 talloc_steal(dev, opt);
151 boot_option_finalise(opt);
152 discover_server_notify_boot_option_add(handler->server,
155 if (!opt->source->resolve_resource) {
156 pb_log("parser %s gave us an unresolved "
157 "resource (%s), but no way to "
159 opt->source->name, opt->option->id);
162 pb_log("boot option %s is unresolved, "
165 list_add(&handler->unresolved_boot_options,
167 talloc_steal(handler, opt);
173 void discover_context_add_boot_option(struct discover_context *ctx,
174 struct discover_boot_option *boot_option)
176 boot_option->source = ctx->parser;
177 list_add_tail(&ctx->boot_options, &boot_option->list);
178 talloc_steal(ctx, boot_option);
182 * device_handler_remove - Remove a device from the handler device array.
185 static void device_handler_remove(struct device_handler *handler,
186 struct discover_device *device)
190 for (i = 0; i < handler->n_devices; i++)
191 if (handler->devices[i] == device)
194 if (i == handler->n_devices) {
195 assert(0 && "unknown device");
199 handler->n_devices--;
200 memmove(&handler->devices[i], &handler->devices[i + 1],
201 (handler->n_devices - i) * sizeof(handler->devices[0]));
202 handler->devices = talloc_realloc(handler, handler->devices,
203 struct discover_device *, handler->n_devices);
205 discover_server_notify_device_remove(handler->server, device->device);
211 * device_handler_get_device_count - Get the count of current handler devices.
214 int device_handler_get_device_count(const struct device_handler *handler)
216 return handler->n_devices;
220 * device_handler_get_device - Get a handler device by index.
223 const struct discover_device *device_handler_get_device(
224 const struct device_handler *handler, unsigned int index)
226 if (index >= handler->n_devices) {
227 assert(0 && "bad index");
231 return handler->devices[index];
234 static int mount_device(struct discover_device *dev)
238 if (!dev->device_path)
241 if (!dev->mount_path)
242 dev->mount_path = join_paths(dev, mount_base(),
245 if (pb_mkdir_recursive(dev->mount_path))
246 pb_log("couldn't create mount directory %s: %s\n",
247 dev->mount_path, strerror(errno));
249 argv[0] = pb_system_apps.mount;
250 argv[1] = dev->device_path;
251 argv[2] = dev->mount_path;
256 if (pb_run_cmd(argv, 1, 0)) {
258 /* Retry mount without ro option. */
260 argv[0] = pb_system_apps.mount;
261 argv[1] = dev->device_path;
262 argv[2] = dev->mount_path;
265 if (pb_run_cmd(argv, 1, 0))
272 pb_rmdir_recursive(mount_base(), dev->mount_path);
276 static int umount_device(struct discover_device *dev)
281 if (!dev->mount_path)
286 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
291 execl(pb_system_apps.umount, pb_system_apps.umount,
292 dev->mount_path, NULL);
296 if (waitpid(pid, &status, 0) == -1) {
297 pb_log("%s: waitpid failed: %s\n", __func__,
302 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
305 pb_rmdir_recursive(mount_base(), dev->mount_path);
310 static struct discover_device *find_device(struct device_handler *handler,
313 struct discover_device *dev;
316 for (i = 0; i < handler->n_devices; i++) {
317 dev = handler->devices[i];
318 if (!strcmp(dev->device->id, id))
325 static int destroy_device(void *arg)
327 struct discover_device *dev = arg;
334 static struct discover_device *discover_device_create(
335 struct device_handler *handler,
336 struct discover_context *ctx,
339 struct discover_device *dev;
342 dev = find_device(handler, event->device);
346 dev = talloc_zero(ctx, struct discover_device);
347 dev->device = talloc_zero(dev, struct device);
348 list_init(&dev->boot_options);
350 devname = event_get_param(ctx->event, "DEVNAME");
352 dev->device_path = talloc_strdup(dev, devname);
354 dev->device->id = talloc_strdup(dev, event->device);
356 talloc_set_destructor(dev, destroy_device);
361 struct discover_boot_option *discover_boot_option_create(
362 struct discover_context *ctx,
363 struct discover_device *device)
365 struct discover_boot_option *opt;
367 opt = talloc_zero(ctx, struct discover_boot_option);
368 opt->option = talloc_zero(opt, struct boot_option);
369 opt->device = device;
375 static int handle_add_udev_event(struct device_handler *handler,
378 struct discover_context *ctx;
379 struct discover_device *dev;
383 /* create our context */
384 ctx = talloc(handler, struct discover_context);
386 list_init(&ctx->boot_options);
388 /* create our top-level device */
389 dev = discover_device_create(handler, ctx, event);
393 /* try to parse UUID and labels */
394 param = event_get_param(ctx->event, "ID_FS_UUID");
396 dev->uuid = talloc_strdup(dev, param);
398 param = event_get_param(ctx->event, "ID_FS_LABEL");
400 dev->label = talloc_strdup(dev, param);
402 rc = mount_device(dev);
408 /* run the parsers. This will populate the ctx's boot_option list. */
409 iterate_parsers(ctx, CONF_METHOD_LOCAL_FILE);
411 /* add discovered stuff to the handler */
412 context_commit(handler, ctx);
419 static int handle_remove_udev_event(struct device_handler *handler,
422 struct discover_device *dev;
424 dev = find_device(handler, event->device);
428 /* remove device from handler device array */
429 device_handler_remove(handler, dev);
434 static int handle_add_user_event(struct device_handler *handler,
437 struct discover_context *ctx;
438 struct discover_device *dev;
441 assert(event->device);
443 ctx = talloc(handler, struct discover_context);
445 list_init(&ctx->boot_options);
447 dev = discover_device_create(handler, ctx, event);
450 rc = parse_user_event(ctx, event);
453 context_commit(handler, ctx);
458 static int handle_remove_user_event(struct device_handler *handler,
461 struct discover_device *dev = find_device(handler, event->device);
466 /* remove device from handler device array */
467 device_handler_remove(handler, dev);
472 static enum conf_method parse_conf_method(const char *str)
475 if (!strcasecmp(str, "dhcp")) {
476 return CONF_METHOD_DHCP;
478 return CONF_METHOD_UNKNOWN;
481 static int handle_conf_user_event(struct device_handler *handler,
484 struct discover_context *ctx;
485 struct discover_device *dev;
486 enum conf_method method;
489 ctx = talloc(handler, struct discover_context);
491 list_init(&ctx->boot_options);
493 val = event_get_param(event, "url");
499 ctx->conf_url = pb_url_parse(ctx, val);
500 if (!ctx->conf_url) {
505 val = event_get_param(event, "method");
511 method = parse_conf_method(val);
512 if (method == CONF_METHOD_UNKNOWN) {
517 dev = discover_device_create(handler, ctx, event);
520 iterate_parsers(ctx, method);
522 context_commit(handler, ctx);
527 typedef int (*event_handler)(struct device_handler *, struct event *);
529 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
530 [EVENT_TYPE_UDEV] = {
531 [EVENT_ACTION_ADD] = handle_add_udev_event,
532 [EVENT_ACTION_REMOVE] = handle_remove_udev_event,
534 [EVENT_TYPE_USER] = {
535 [EVENT_ACTION_ADD] = handle_add_user_event,
536 [EVENT_ACTION_REMOVE] = handle_remove_user_event,
537 [EVENT_ACTION_CONF] = handle_conf_user_event,
541 int device_handler_event(struct device_handler *handler,
544 if (event->type >= EVENT_TYPE_MAX ||
545 event->action >= EVENT_ACTION_MAX ||
546 !handlers[event->type][event->action]) {
547 pb_log("%s unknown type/action: %d/%d\n", __func__,
548 event->type, event->action);
552 return handlers[event->type][event->action](handler, event);
555 struct device_handler *device_handler_init(struct discover_server *server,
558 struct device_handler *handler;
560 handler = talloc(NULL, struct device_handler);
561 handler->devices = NULL;
562 handler->n_devices = 0;
563 handler->server = server;
564 handler->dry_run = dry_run;
565 list_init(&handler->unresolved_boot_options);
567 /* set up our mount point base */
568 pb_mkdir_recursive(mount_base());
575 void device_handler_destroy(struct device_handler *handler)
577 talloc_free(handler);
580 static int device_match_path(struct discover_device *dev, const char *path)
582 return dev->device_path && !strcmp(dev->device_path, path);
585 static int device_match_uuid(struct discover_device *dev, const char *uuid)
587 return dev->uuid && !strcmp(dev->uuid, uuid);
590 static int device_match_label(struct discover_device *dev, const char *label)
592 return dev->label && !strcmp(dev->label, label);
595 static int device_match_id(struct discover_device *dev, const char *id)
597 return !strcmp(dev->device->id, id);
600 static struct discover_device *device_lookup(
601 struct device_handler *device_handler,
602 int (match_fn)(struct discover_device *, const char *),
605 struct discover_device *dev;
611 for (i = 0; i < device_handler->n_devices; i++) {
612 dev = device_handler->devices[i];
614 if (match_fn(dev, str))
621 struct discover_device *device_lookup_by_name(struct device_handler *handler,
624 struct discover_device *dev;
627 if (strncmp(name, "/dev/", strlen("/dev/")))
628 path = talloc_asprintf(NULL, "/dev/%s", name);
630 path = talloc_strdup(NULL, name);
632 dev = device_lookup_by_path(handler, path);
639 struct discover_device *device_lookup_by_path(
640 struct device_handler *device_handler,
643 return device_lookup(device_handler, device_match_path, path);
646 struct discover_device *device_lookup_by_uuid(
647 struct device_handler *device_handler,
650 return device_lookup(device_handler, device_match_uuid, uuid);
653 struct discover_device *device_lookup_by_label(
654 struct device_handler *device_handler,
657 return device_lookup(device_handler, device_match_label, label);
660 struct discover_device *device_lookup_by_id(
661 struct device_handler *device_handler,
664 return device_lookup(device_handler, device_match_id, id);
667 static struct discover_boot_option *find_boot_option_by_id(
668 struct device_handler *handler, const char *id)
672 for (i = 0; i < handler->n_devices; i++) {
673 struct discover_device *dev = handler->devices[i];
674 struct discover_boot_option *opt;
676 list_for_each_entry(&dev->boot_options, opt, list)
677 if (!strcmp(opt->option->id, id))
684 static void boot_status(void *arg, struct boot_status *status)
686 struct device_handler *handler = arg;
688 discover_server_notify_boot_status(handler->server, status);
691 void device_handler_boot(struct device_handler *handler,
692 struct boot_command *cmd)
694 struct discover_boot_option *opt;
696 opt = find_boot_option_by_id(handler, cmd->option_id);
698 boot(handler, opt, cmd, handler->dry_run, boot_status, handler);