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 list_add(&handler->unresolved_boot_options, &opt->list);
153 talloc_steal(handler, opt);
158 void discover_context_add_boot_option(struct discover_context *ctx,
159 struct discover_boot_option *boot_option)
161 boot_option->source = ctx->parser;
162 list_add(&ctx->boot_options, &boot_option->list);
163 talloc_steal(ctx, boot_option);
167 * device_handler_remove - Remove a device from the handler device array.
170 static void device_handler_remove(struct device_handler *handler,
171 struct discover_device *device)
175 for (i = 0; i < handler->n_devices; i++)
176 if (handler->devices[i] == device)
179 if (i == handler->n_devices) {
180 assert(0 && "unknown device");
184 handler->n_devices--;
185 memmove(&handler->devices[i], &handler->devices[i + 1],
186 (handler->n_devices - i) * sizeof(handler->devices[0]));
187 handler->devices = talloc_realloc(handler, handler->devices,
188 struct discover_device *, handler->n_devices);
190 discover_server_notify_device_remove(handler->server, device->device);
196 * device_handler_get_device_count - Get the count of current handler devices.
199 int device_handler_get_device_count(const struct device_handler *handler)
201 return handler->n_devices;
205 * device_handler_get_device - Get a handler device by index.
208 const struct discover_device *device_handler_get_device(
209 const struct device_handler *handler, unsigned int index)
211 if (index >= handler->n_devices) {
212 assert(0 && "bad index");
216 return handler->devices[index];
219 static int mount_device(struct discover_device *dev)
223 if (!dev->mount_path)
224 dev->mount_path = join_paths(dev, mount_base(),
227 if (pb_mkdir_recursive(dev->mount_path))
228 pb_log("couldn't create mount directory %s: %s\n",
229 dev->mount_path, strerror(errno));
231 argv[0] = pb_system_apps.mount;
232 argv[1] = dev->device_path;
233 argv[2] = dev->mount_path;
238 if (pb_run_cmd(argv, 1, 0)) {
240 /* Retry mount without ro option. */
242 argv[0] = pb_system_apps.mount;
243 argv[1] = dev->device_path;
244 argv[2] = dev->mount_path;
247 if (pb_run_cmd(argv, 1, 0))
254 pb_rmdir_recursive(mount_base(), dev->mount_path);
258 static int umount_device(struct discover_device *dev)
263 if (!dev->mount_path)
268 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
273 execl(pb_system_apps.umount, pb_system_apps.umount,
274 dev->mount_path, NULL);
278 if (waitpid(pid, &status, 0) == -1) {
279 pb_log("%s: waitpid failed: %s\n", __func__,
284 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
287 pb_rmdir_recursive(mount_base(), dev->mount_path);
292 static struct discover_device *find_device(struct device_handler *handler,
295 struct discover_device *dev;
298 for (i = 0; i < handler->n_devices; i++) {
299 dev = handler->devices[i];
300 if (!strcmp(dev->device->id, id))
307 static int destroy_device(void *arg)
309 struct discover_device *dev = arg;
316 static struct discover_device *discover_device_create(
317 struct device_handler *handler,
318 struct discover_context *ctx,
321 struct discover_device *dev;
324 dev = find_device(handler, event->device);
328 dev = talloc_zero(ctx, struct discover_device);
329 dev->device = talloc_zero(dev, struct device);
330 list_init(&dev->boot_options);
332 devname = event_get_param(ctx->event, "DEVNAME");
334 dev->device_path = talloc_strdup(dev, devname);
336 dev->device->id = talloc_strdup(dev, event->device);
338 talloc_set_destructor(dev, destroy_device);
343 struct discover_boot_option *discover_boot_option_create(
344 struct discover_context *ctx,
345 struct discover_device *device)
347 struct discover_boot_option *opt;
349 opt = talloc_zero(ctx, struct discover_boot_option);
350 opt->option = talloc_zero(opt, struct boot_option);
351 opt->device = device;
357 static int handle_add_udev_event(struct device_handler *handler,
360 struct discover_context *ctx;
361 struct discover_device *dev;
365 /* create our context */
366 ctx = talloc(handler, struct discover_context);
368 list_init(&ctx->boot_options);
370 /* create our top-level device */
371 dev = discover_device_create(handler, ctx, event);
375 /* try to parse UUID and labels */
376 param = event_get_param(ctx->event, "ID_FS_UUID");
378 dev->uuid = talloc_strdup(dev, param);
380 param = event_get_param(ctx->event, "ID_FS_LABEL");
382 dev->label = talloc_strdup(dev, param);
384 rc = mount_device(dev);
390 /* run the parsers. This will populate the ctx's boot_option list. */
391 iterate_parsers(ctx, CONF_METHOD_LOCAL_FILE);
393 /* add discovered stuff to the handler */
394 context_commit(handler, ctx);
401 static int handle_remove_udev_event(struct device_handler *handler,
404 struct discover_device *dev;
406 dev = find_device(handler, event->device);
410 /* remove device from handler device array */
411 device_handler_remove(handler, dev);
416 static int handle_add_user_event(struct device_handler *handler,
419 struct discover_context *ctx;
420 struct discover_device *dev;
423 assert(event->device);
425 ctx = talloc(handler, struct discover_context);
427 list_init(&ctx->boot_options);
429 dev = discover_device_create(handler, ctx, event);
432 rc = parse_user_event(ctx, event);
435 context_commit(handler, ctx);
440 static int handle_remove_user_event(struct device_handler *handler,
443 struct discover_device *dev = find_device(handler, event->device);
448 /* remove device from handler device array */
449 device_handler_remove(handler, dev);
454 static enum conf_method parse_conf_method(const char *str)
457 if (!strcasecmp(str, "dhcp")) {
458 return CONF_METHOD_DHCP;
460 return CONF_METHOD_UNKNOWN;
463 static int handle_conf_user_event(struct device_handler *handler,
466 struct discover_context *ctx;
467 struct discover_device *dev;
468 enum conf_method method;
471 ctx = talloc(handler, struct discover_context);
473 list_init(&ctx->boot_options);
475 val = event_get_param(event, "url");
481 ctx->conf_url = pb_url_parse(ctx, val);
482 if (!ctx->conf_url) {
487 val = event_get_param(event, "method");
493 method = parse_conf_method(val);
494 if (method == CONF_METHOD_UNKNOWN) {
499 dev = discover_device_create(handler, ctx, event);
502 iterate_parsers(ctx, method);
504 context_commit(handler, ctx);
509 typedef int (*event_handler)(struct device_handler *, struct event *);
511 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
512 [EVENT_TYPE_UDEV] = {
513 [EVENT_ACTION_ADD] = handle_add_udev_event,
514 [EVENT_ACTION_REMOVE] = handle_remove_udev_event,
516 [EVENT_TYPE_USER] = {
517 [EVENT_ACTION_ADD] = handle_add_user_event,
518 [EVENT_ACTION_REMOVE] = handle_remove_user_event,
519 [EVENT_ACTION_CONF] = handle_conf_user_event,
523 int device_handler_event(struct device_handler *handler,
526 if (event->type >= EVENT_TYPE_MAX ||
527 event->action >= EVENT_ACTION_MAX ||
528 !handlers[event->type][event->action]) {
529 pb_log("%s unknown type/action: %d/%d\n", __func__,
530 event->type, event->action);
534 return handlers[event->type][event->action](handler, event);
537 struct device_handler *device_handler_init(struct discover_server *server,
540 struct device_handler *handler;
542 handler = talloc(NULL, struct device_handler);
543 handler->devices = NULL;
544 handler->n_devices = 0;
545 handler->server = server;
546 handler->dry_run = dry_run;
547 list_init(&handler->unresolved_boot_options);
549 /* set up our mount point base */
550 pb_mkdir_recursive(mount_base());
557 void device_handler_destroy(struct device_handler *handler)
559 talloc_free(handler);
562 static int device_match_path(struct discover_device *dev, const char *path)
564 return !strcmp(dev->device_path, path);
567 static int device_match_uuid(struct discover_device *dev, const char *uuid)
569 return dev->uuid && !strcmp(dev->uuid, uuid);
572 static int device_match_label(struct discover_device *dev, const char *label)
574 return dev->label && !strcmp(dev->label, label);
577 static int device_match_id(struct discover_device *dev, const char *id)
579 return !strcmp(dev->device->id, id);
582 static struct discover_device *device_lookup(
583 struct device_handler *device_handler,
584 int (match_fn)(struct discover_device *, const char *),
587 struct discover_device *dev;
593 for (i = 0; i < device_handler->n_devices; i++) {
594 dev = device_handler->devices[i];
596 if (match_fn(dev, str))
603 struct discover_device *device_lookup_by_name(struct device_handler *handler,
606 struct discover_device *dev;
609 if (strncmp(name, "/dev/", strlen("/dev/")))
610 path = talloc_asprintf(NULL, "/dev/%s", name);
612 path = talloc_strdup(NULL, name);
614 dev = device_lookup_by_path(handler, path);
621 struct discover_device *device_lookup_by_path(
622 struct device_handler *device_handler,
625 return device_lookup(device_handler, device_match_path, path);
628 struct discover_device *device_lookup_by_uuid(
629 struct device_handler *device_handler,
632 return device_lookup(device_handler, device_match_uuid, uuid);
635 struct discover_device *device_lookup_by_label(
636 struct device_handler *device_handler,
639 return device_lookup(device_handler, device_match_label, label);
642 struct discover_device *device_lookup_by_id(
643 struct device_handler *device_handler,
646 return device_lookup(device_handler, device_match_id, id);
649 static struct discover_boot_option *find_boot_option_by_id(
650 struct device_handler *handler, const char *id)
654 for (i = 0; i < handler->n_devices; i++) {
655 struct discover_device *dev = handler->devices[i];
656 struct discover_boot_option *opt;
658 list_for_each_entry(&dev->boot_options, opt, list)
659 if (!strcmp(opt->option->id, id))
666 void device_handler_boot(struct device_handler *handler,
667 struct boot_command *cmd)
669 struct discover_boot_option *opt;
671 opt = find_boot_option_by_id(handler, cmd->option_id);
673 boot(handler, opt, cmd, handler->dry_run);