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->mount_path)
239 dev->mount_path = join_paths(dev, mount_base(),
242 if (pb_mkdir_recursive(dev->mount_path))
243 pb_log("couldn't create mount directory %s: %s\n",
244 dev->mount_path, strerror(errno));
246 argv[0] = pb_system_apps.mount;
247 argv[1] = dev->device_path;
248 argv[2] = dev->mount_path;
253 if (pb_run_cmd(argv, 1, 0)) {
255 /* Retry mount without ro option. */
257 argv[0] = pb_system_apps.mount;
258 argv[1] = dev->device_path;
259 argv[2] = dev->mount_path;
262 if (pb_run_cmd(argv, 1, 0))
269 pb_rmdir_recursive(mount_base(), dev->mount_path);
273 static int umount_device(struct discover_device *dev)
278 if (!dev->mount_path)
283 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
288 execl(pb_system_apps.umount, pb_system_apps.umount,
289 dev->mount_path, NULL);
293 if (waitpid(pid, &status, 0) == -1) {
294 pb_log("%s: waitpid failed: %s\n", __func__,
299 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
302 pb_rmdir_recursive(mount_base(), dev->mount_path);
307 static struct discover_device *find_device(struct device_handler *handler,
310 struct discover_device *dev;
313 for (i = 0; i < handler->n_devices; i++) {
314 dev = handler->devices[i];
315 if (!strcmp(dev->device->id, id))
322 static int destroy_device(void *arg)
324 struct discover_device *dev = arg;
331 static struct discover_device *discover_device_create(
332 struct device_handler *handler,
333 struct discover_context *ctx,
336 struct discover_device *dev;
339 dev = find_device(handler, event->device);
343 dev = talloc_zero(ctx, struct discover_device);
344 dev->device = talloc_zero(dev, struct device);
345 list_init(&dev->boot_options);
347 devname = event_get_param(ctx->event, "DEVNAME");
349 dev->device_path = talloc_strdup(dev, devname);
351 dev->device->id = talloc_strdup(dev, event->device);
353 talloc_set_destructor(dev, destroy_device);
358 struct discover_boot_option *discover_boot_option_create(
359 struct discover_context *ctx,
360 struct discover_device *device)
362 struct discover_boot_option *opt;
364 opt = talloc_zero(ctx, struct discover_boot_option);
365 opt->option = talloc_zero(opt, struct boot_option);
366 opt->device = device;
372 static int handle_add_udev_event(struct device_handler *handler,
375 struct discover_context *ctx;
376 struct discover_device *dev;
380 /* create our context */
381 ctx = talloc(handler, struct discover_context);
383 list_init(&ctx->boot_options);
385 /* create our top-level device */
386 dev = discover_device_create(handler, ctx, event);
390 /* try to parse UUID and labels */
391 param = event_get_param(ctx->event, "ID_FS_UUID");
393 dev->uuid = talloc_strdup(dev, param);
395 param = event_get_param(ctx->event, "ID_FS_LABEL");
397 dev->label = talloc_strdup(dev, param);
399 rc = mount_device(dev);
405 /* run the parsers. This will populate the ctx's boot_option list. */
406 iterate_parsers(ctx, CONF_METHOD_LOCAL_FILE);
408 /* add discovered stuff to the handler */
409 context_commit(handler, ctx);
416 static int handle_remove_udev_event(struct device_handler *handler,
419 struct discover_device *dev;
421 dev = find_device(handler, event->device);
425 /* remove device from handler device array */
426 device_handler_remove(handler, dev);
431 static int handle_add_user_event(struct device_handler *handler,
434 struct discover_context *ctx;
435 struct discover_device *dev;
438 assert(event->device);
440 ctx = talloc(handler, struct discover_context);
442 list_init(&ctx->boot_options);
444 dev = discover_device_create(handler, ctx, event);
447 rc = parse_user_event(ctx, event);
450 context_commit(handler, ctx);
455 static int handle_remove_user_event(struct device_handler *handler,
458 struct discover_device *dev = find_device(handler, event->device);
463 /* remove device from handler device array */
464 device_handler_remove(handler, dev);
469 static enum conf_method parse_conf_method(const char *str)
472 if (!strcasecmp(str, "dhcp")) {
473 return CONF_METHOD_DHCP;
475 return CONF_METHOD_UNKNOWN;
478 static int handle_conf_user_event(struct device_handler *handler,
481 struct discover_context *ctx;
482 struct discover_device *dev;
483 enum conf_method method;
486 ctx = talloc(handler, struct discover_context);
488 list_init(&ctx->boot_options);
490 val = event_get_param(event, "url");
496 ctx->conf_url = pb_url_parse(ctx, val);
497 if (!ctx->conf_url) {
502 val = event_get_param(event, "method");
508 method = parse_conf_method(val);
509 if (method == CONF_METHOD_UNKNOWN) {
514 dev = discover_device_create(handler, ctx, event);
517 iterate_parsers(ctx, method);
519 context_commit(handler, ctx);
524 typedef int (*event_handler)(struct device_handler *, struct event *);
526 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
527 [EVENT_TYPE_UDEV] = {
528 [EVENT_ACTION_ADD] = handle_add_udev_event,
529 [EVENT_ACTION_REMOVE] = handle_remove_udev_event,
531 [EVENT_TYPE_USER] = {
532 [EVENT_ACTION_ADD] = handle_add_user_event,
533 [EVENT_ACTION_REMOVE] = handle_remove_user_event,
534 [EVENT_ACTION_CONF] = handle_conf_user_event,
538 int device_handler_event(struct device_handler *handler,
541 if (event->type >= EVENT_TYPE_MAX ||
542 event->action >= EVENT_ACTION_MAX ||
543 !handlers[event->type][event->action]) {
544 pb_log("%s unknown type/action: %d/%d\n", __func__,
545 event->type, event->action);
549 return handlers[event->type][event->action](handler, event);
552 struct device_handler *device_handler_init(struct discover_server *server,
555 struct device_handler *handler;
557 handler = talloc(NULL, struct device_handler);
558 handler->devices = NULL;
559 handler->n_devices = 0;
560 handler->server = server;
561 handler->dry_run = dry_run;
562 list_init(&handler->unresolved_boot_options);
564 /* set up our mount point base */
565 pb_mkdir_recursive(mount_base());
572 void device_handler_destroy(struct device_handler *handler)
574 talloc_free(handler);
577 static int device_match_path(struct discover_device *dev, const char *path)
579 return !strcmp(dev->device_path, path);
582 static int device_match_uuid(struct discover_device *dev, const char *uuid)
584 return dev->uuid && !strcmp(dev->uuid, uuid);
587 static int device_match_label(struct discover_device *dev, const char *label)
589 return dev->label && !strcmp(dev->label, label);
592 static int device_match_id(struct discover_device *dev, const char *id)
594 return !strcmp(dev->device->id, id);
597 static struct discover_device *device_lookup(
598 struct device_handler *device_handler,
599 int (match_fn)(struct discover_device *, const char *),
602 struct discover_device *dev;
608 for (i = 0; i < device_handler->n_devices; i++) {
609 dev = device_handler->devices[i];
611 if (match_fn(dev, str))
618 struct discover_device *device_lookup_by_name(struct device_handler *handler,
621 struct discover_device *dev;
624 if (strncmp(name, "/dev/", strlen("/dev/")))
625 path = talloc_asprintf(NULL, "/dev/%s", name);
627 path = talloc_strdup(NULL, name);
629 dev = device_lookup_by_path(handler, path);
636 struct discover_device *device_lookup_by_path(
637 struct device_handler *device_handler,
640 return device_lookup(device_handler, device_match_path, path);
643 struct discover_device *device_lookup_by_uuid(
644 struct device_handler *device_handler,
647 return device_lookup(device_handler, device_match_uuid, uuid);
650 struct discover_device *device_lookup_by_label(
651 struct device_handler *device_handler,
654 return device_lookup(device_handler, device_match_label, label);
657 struct discover_device *device_lookup_by_id(
658 struct device_handler *device_handler,
661 return device_lookup(device_handler, device_match_id, id);
664 static struct discover_boot_option *find_boot_option_by_id(
665 struct device_handler *handler, const char *id)
669 for (i = 0; i < handler->n_devices; i++) {
670 struct discover_device *dev = handler->devices[i];
671 struct discover_boot_option *opt;
673 list_for_each_entry(&dev->boot_options, opt, list)
674 if (!strcmp(opt->option->id, id))
681 static void boot_status(void *arg, struct boot_status *status)
683 struct device_handler *handler = arg;
685 discover_server_notify_boot_status(handler->server, status);
688 void device_handler_boot(struct device_handler *handler,
689 struct boot_command *cmd)
691 struct discover_boot_option *opt;
693 opt = find_boot_option_by_id(handler, cmd->option_id);
695 boot(handler, opt, cmd, handler->dry_run, boot_status, handler);