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);
80 opt->option->boot_image_file = opt->boot_image->url->full;
82 opt->option->initrd_file = opt->initrd->url->full;
84 opt->option->icon_file = opt->icon->url->full;
87 static void process_boot_option_queue(struct device_handler *handler)
89 struct discover_boot_option *opt, *tmp;
91 list_for_each_entry_safe(&handler->unresolved_boot_options,
94 if (!boot_option_resolve(opt, handler))
97 list_remove(&opt->list);
98 list_add(&opt->device->boot_options, &opt->list);
99 talloc_steal(opt->device, opt);
100 boot_option_finalise(opt);
101 discover_server_notify_boot_option_add(handler->server,
107 * context_commit - Commit a temporary discovery context to the handler,
108 * and notify the clients about any new options / devices
110 static void context_commit(struct device_handler *handler,
111 struct discover_context *ctx)
113 struct discover_device *dev = ctx->device;
114 struct discover_boot_option *opt, *tmp;
115 unsigned int i, existing_device = 0;
117 /* do we already have this device? */
118 for (i = 0; i < handler->n_devices; i++) {
119 if (ctx->device == handler->devices[i]) {
125 /* if not already present, add the device to the handler's array */
126 if (!existing_device) {
127 handler->n_devices++;
128 handler->devices = talloc_realloc(handler, handler->devices,
129 struct discover_device *, handler->n_devices);
130 handler->devices[handler->n_devices - 1] = dev;
131 talloc_steal(handler, dev);
133 discover_server_notify_device_add(handler->server, dev->device);
135 /* this new device might be able to resolve existing boot
137 process_boot_option_queue(handler);
141 /* move boot options from the context to the device */
142 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
143 list_remove(&opt->list);
145 if (boot_option_resolve(opt, handler)) {
146 list_add(&dev->boot_options, &opt->list);
147 talloc_steal(dev, opt);
148 boot_option_finalise(opt);
149 discover_server_notify_boot_option_add(handler->server,
152 if (!opt->source->resolve_resource) {
153 pb_log("parser %s gave us an unresolved "
154 "resource (%s), but no way to "
156 opt->source->name, opt->option->id);
159 pb_log("boot option %s is unresolved, "
162 list_add(&handler->unresolved_boot_options,
164 talloc_steal(handler, opt);
170 void discover_context_add_boot_option(struct discover_context *ctx,
171 struct discover_boot_option *boot_option)
173 boot_option->source = ctx->parser;
174 list_add(&ctx->boot_options, &boot_option->list);
175 talloc_steal(ctx, boot_option);
179 * device_handler_remove - Remove a device from the handler device array.
182 static void device_handler_remove(struct device_handler *handler,
183 struct discover_device *device)
187 for (i = 0; i < handler->n_devices; i++)
188 if (handler->devices[i] == device)
191 if (i == handler->n_devices) {
192 assert(0 && "unknown device");
196 handler->n_devices--;
197 memmove(&handler->devices[i], &handler->devices[i + 1],
198 (handler->n_devices - i) * sizeof(handler->devices[0]));
199 handler->devices = talloc_realloc(handler, handler->devices,
200 struct discover_device *, handler->n_devices);
202 discover_server_notify_device_remove(handler->server, device->device);
208 * device_handler_get_device_count - Get the count of current handler devices.
211 int device_handler_get_device_count(const struct device_handler *handler)
213 return handler->n_devices;
217 * device_handler_get_device - Get a handler device by index.
220 const struct discover_device *device_handler_get_device(
221 const struct device_handler *handler, unsigned int index)
223 if (index >= handler->n_devices) {
224 assert(0 && "bad index");
228 return handler->devices[index];
231 static int mount_device(struct discover_device *dev)
235 if (!dev->mount_path)
236 dev->mount_path = join_paths(dev, mount_base(),
239 if (pb_mkdir_recursive(dev->mount_path))
240 pb_log("couldn't create mount directory %s: %s\n",
241 dev->mount_path, strerror(errno));
243 argv[0] = pb_system_apps.mount;
244 argv[1] = dev->device_path;
245 argv[2] = dev->mount_path;
250 if (pb_run_cmd(argv, 1, 0)) {
252 /* Retry mount without ro option. */
254 argv[0] = pb_system_apps.mount;
255 argv[1] = dev->device_path;
256 argv[2] = dev->mount_path;
259 if (pb_run_cmd(argv, 1, 0))
266 pb_rmdir_recursive(mount_base(), dev->mount_path);
270 static int umount_device(struct discover_device *dev)
275 if (!dev->mount_path)
280 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
285 execl(pb_system_apps.umount, pb_system_apps.umount,
286 dev->mount_path, NULL);
290 if (waitpid(pid, &status, 0) == -1) {
291 pb_log("%s: waitpid failed: %s\n", __func__,
296 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
299 pb_rmdir_recursive(mount_base(), dev->mount_path);
304 static struct discover_device *find_device(struct device_handler *handler,
307 struct discover_device *dev;
310 for (i = 0; i < handler->n_devices; i++) {
311 dev = handler->devices[i];
312 if (!strcmp(dev->device->id, id))
319 static int destroy_device(void *arg)
321 struct discover_device *dev = arg;
328 static struct discover_device *discover_device_create(
329 struct device_handler *handler,
330 struct discover_context *ctx,
333 struct discover_device *dev;
336 dev = find_device(handler, event->device);
340 dev = talloc_zero(ctx, struct discover_device);
341 dev->device = talloc_zero(dev, struct device);
342 list_init(&dev->boot_options);
344 devname = event_get_param(ctx->event, "DEVNAME");
346 dev->device_path = talloc_strdup(dev, devname);
348 dev->device->id = talloc_strdup(dev, event->device);
350 talloc_set_destructor(dev, destroy_device);
355 struct discover_boot_option *discover_boot_option_create(
356 struct discover_context *ctx,
357 struct discover_device *device)
359 struct discover_boot_option *opt;
361 opt = talloc_zero(ctx, struct discover_boot_option);
362 opt->option = talloc_zero(opt, struct boot_option);
363 opt->device = device;
369 static int handle_add_udev_event(struct device_handler *handler,
372 struct discover_context *ctx;
373 struct discover_device *dev;
377 /* create our context */
378 ctx = talloc(handler, struct discover_context);
380 list_init(&ctx->boot_options);
382 /* create our top-level device */
383 dev = discover_device_create(handler, ctx, event);
387 /* try to parse UUID and labels */
388 param = event_get_param(ctx->event, "ID_FS_UUID");
390 dev->uuid = talloc_strdup(dev, param);
392 param = event_get_param(ctx->event, "ID_FS_LABEL");
394 dev->label = talloc_strdup(dev, param);
396 rc = mount_device(dev);
402 /* run the parsers. This will populate the ctx's boot_option list. */
403 iterate_parsers(ctx, CONF_METHOD_LOCAL_FILE);
405 /* add discovered stuff to the handler */
406 context_commit(handler, ctx);
413 static int handle_remove_udev_event(struct device_handler *handler,
416 struct discover_device *dev;
418 dev = find_device(handler, event->device);
422 /* remove device from handler device array */
423 device_handler_remove(handler, dev);
428 static int handle_add_user_event(struct device_handler *handler,
431 struct discover_context *ctx;
432 struct discover_device *dev;
435 assert(event->device);
437 ctx = talloc(handler, struct discover_context);
439 list_init(&ctx->boot_options);
441 dev = discover_device_create(handler, ctx, event);
444 rc = parse_user_event(ctx, event);
447 context_commit(handler, ctx);
452 static int handle_remove_user_event(struct device_handler *handler,
455 struct discover_device *dev = find_device(handler, event->device);
460 /* remove device from handler device array */
461 device_handler_remove(handler, dev);
466 static enum conf_method parse_conf_method(const char *str)
469 if (!strcasecmp(str, "dhcp")) {
470 return CONF_METHOD_DHCP;
472 return CONF_METHOD_UNKNOWN;
475 static int handle_conf_user_event(struct device_handler *handler,
478 struct discover_context *ctx;
479 struct discover_device *dev;
480 enum conf_method method;
483 ctx = talloc(handler, struct discover_context);
485 list_init(&ctx->boot_options);
487 val = event_get_param(event, "url");
493 ctx->conf_url = pb_url_parse(ctx, val);
494 if (!ctx->conf_url) {
499 val = event_get_param(event, "method");
505 method = parse_conf_method(val);
506 if (method == CONF_METHOD_UNKNOWN) {
511 dev = discover_device_create(handler, ctx, event);
514 iterate_parsers(ctx, method);
516 context_commit(handler, ctx);
521 typedef int (*event_handler)(struct device_handler *, struct event *);
523 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
524 [EVENT_TYPE_UDEV] = {
525 [EVENT_ACTION_ADD] = handle_add_udev_event,
526 [EVENT_ACTION_REMOVE] = handle_remove_udev_event,
528 [EVENT_TYPE_USER] = {
529 [EVENT_ACTION_ADD] = handle_add_user_event,
530 [EVENT_ACTION_REMOVE] = handle_remove_user_event,
531 [EVENT_ACTION_CONF] = handle_conf_user_event,
535 int device_handler_event(struct device_handler *handler,
538 if (event->type >= EVENT_TYPE_MAX ||
539 event->action >= EVENT_ACTION_MAX ||
540 !handlers[event->type][event->action]) {
541 pb_log("%s unknown type/action: %d/%d\n", __func__,
542 event->type, event->action);
546 return handlers[event->type][event->action](handler, event);
549 struct device_handler *device_handler_init(struct discover_server *server,
552 struct device_handler *handler;
554 handler = talloc(NULL, struct device_handler);
555 handler->devices = NULL;
556 handler->n_devices = 0;
557 handler->server = server;
558 handler->dry_run = dry_run;
559 list_init(&handler->unresolved_boot_options);
561 /* set up our mount point base */
562 pb_mkdir_recursive(mount_base());
569 void device_handler_destroy(struct device_handler *handler)
571 talloc_free(handler);
574 static int device_match_path(struct discover_device *dev, const char *path)
576 return !strcmp(dev->device_path, path);
579 static int device_match_uuid(struct discover_device *dev, const char *uuid)
581 return dev->uuid && !strcmp(dev->uuid, uuid);
584 static int device_match_label(struct discover_device *dev, const char *label)
586 return dev->label && !strcmp(dev->label, label);
589 static int device_match_id(struct discover_device *dev, const char *id)
591 return !strcmp(dev->device->id, id);
594 static struct discover_device *device_lookup(
595 struct device_handler *device_handler,
596 int (match_fn)(struct discover_device *, const char *),
599 struct discover_device *dev;
605 for (i = 0; i < device_handler->n_devices; i++) {
606 dev = device_handler->devices[i];
608 if (match_fn(dev, str))
615 struct discover_device *device_lookup_by_name(struct device_handler *handler,
618 struct discover_device *dev;
621 if (strncmp(name, "/dev/", strlen("/dev/")))
622 path = talloc_asprintf(NULL, "/dev/%s", name);
624 path = talloc_strdup(NULL, name);
626 dev = device_lookup_by_path(handler, path);
633 struct discover_device *device_lookup_by_path(
634 struct device_handler *device_handler,
637 return device_lookup(device_handler, device_match_path, path);
640 struct discover_device *device_lookup_by_uuid(
641 struct device_handler *device_handler,
644 return device_lookup(device_handler, device_match_uuid, uuid);
647 struct discover_device *device_lookup_by_label(
648 struct device_handler *device_handler,
651 return device_lookup(device_handler, device_match_label, label);
654 struct discover_device *device_lookup_by_id(
655 struct device_handler *device_handler,
658 return device_lookup(device_handler, device_match_id, id);
661 static struct discover_boot_option *find_boot_option_by_id(
662 struct device_handler *handler, const char *id)
666 for (i = 0; i < handler->n_devices; i++) {
667 struct discover_device *dev = handler->devices[i];
668 struct discover_boot_option *opt;
670 list_for_each_entry(&dev->boot_options, opt, list)
671 if (!strcmp(opt->option->id, id))
678 static void boot_status(void *arg, struct boot_status *status)
680 struct device_handler *handler = arg;
682 discover_server_notify_boot_status(handler->server, status);
685 void device_handler_boot(struct device_handler *handler,
686 struct boot_command *cmd)
688 struct discover_boot_option *opt;
690 opt = find_boot_option_by_id(handler, cmd->option_id);
692 boot(handler, opt, cmd, handler->dry_run, boot_status, handler);