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;
34 static bool resource_is_resolved(struct resource *res)
36 return !res || res->resolved;
39 /* We only use this in an assert, which will disappear if we're compiling
40 * with NDEBUG, so we need the 'used' attribute for these builds */
41 static bool __attribute__((used)) boot_option_is_resolved(
42 struct discover_boot_option *opt)
44 return resource_is_resolved(opt->boot_image) &&
45 resource_is_resolved(opt->initrd) &&
46 resource_is_resolved(opt->icon);
49 static void boot_option_finalise(struct discover_boot_option *opt)
51 assert(boot_option_is_resolved(opt));
53 /* check that the parsers haven't set any of the final data */
54 assert(!opt->option->boot_image_file);
55 assert(!opt->option->initrd_file);
56 assert(!opt->option->icon_file);
59 opt->option->boot_image_file = opt->boot_image->url->full;
61 opt->option->initrd_file = opt->initrd->url->full;
63 opt->option->icon_file = opt->icon->url->full;
67 * context_commit - Commit a temporary discovery context to the handler,
68 * and notify the clients about any new options / devices
70 static void context_commit(struct device_handler *handler,
71 struct discover_context *ctx)
73 struct discover_device *dev = ctx->device;
74 struct discover_boot_option *opt, *tmp;
75 unsigned int i, existing_device = 0;
77 /* do we already have this device? */
78 for (i = 0; i < handler->n_devices; i++) {
79 if (ctx->device == handler->devices[i]) {
85 /* if not already present, add the device to the handler's array */
86 if (!existing_device) {
88 handler->devices = talloc_realloc(handler, handler->devices,
89 struct discover_device *, handler->n_devices);
90 handler->devices[handler->n_devices - 1] = dev;
91 talloc_steal(handler, dev);
93 discover_server_notify_device_add(handler->server, dev->device);
97 /* move boot options from the context to the device */
98 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
99 list_remove(&opt->list);
100 list_add(&dev->boot_options, &opt->list);
101 talloc_steal(dev, opt);
102 boot_option_finalise(opt);
103 discover_server_notify_boot_option_add(handler->server,
108 void discover_context_add_boot_option(struct discover_context *ctx,
109 struct discover_boot_option *boot_option)
111 list_add(&ctx->boot_options, &boot_option->list);
112 talloc_steal(ctx, boot_option);
116 * device_handler_remove - Remove a device from the handler device array.
119 static void device_handler_remove(struct device_handler *handler,
120 struct discover_device *device)
124 for (i = 0; i < handler->n_devices; i++)
125 if (handler->devices[i] == device)
128 if (i == handler->n_devices) {
129 assert(0 && "unknown device");
133 handler->n_devices--;
134 memmove(&handler->devices[i], &handler->devices[i + 1],
135 (handler->n_devices - i) * sizeof(handler->devices[0]));
136 handler->devices = talloc_realloc(handler, handler->devices,
137 struct discover_device *, handler->n_devices);
139 discover_server_notify_device_remove(handler->server, device->device);
145 * device_handler_get_device_count - Get the count of current handler devices.
148 int device_handler_get_device_count(const struct device_handler *handler)
150 return handler->n_devices;
154 * device_handler_get_device - Get a handler device by index.
157 const struct discover_device *device_handler_get_device(
158 const struct device_handler *handler, unsigned int index)
160 if (index >= handler->n_devices) {
161 assert(0 && "bad index");
165 return handler->devices[index];
168 static int mount_device(struct discover_device *dev)
172 if (!dev->mount_path)
173 dev->mount_path = join_paths(dev, mount_base(),
176 if (pb_mkdir_recursive(dev->mount_path))
177 pb_log("couldn't create mount directory %s: %s\n",
178 dev->mount_path, strerror(errno));
180 argv[0] = pb_system_apps.mount;
181 argv[1] = dev->device_path;
182 argv[2] = dev->mount_path;
187 if (pb_run_cmd(argv, 1, 0)) {
189 /* Retry mount without ro option. */
191 argv[0] = pb_system_apps.mount;
192 argv[1] = dev->device_path;
193 argv[2] = dev->mount_path;
196 if (pb_run_cmd(argv, 1, 0))
203 pb_rmdir_recursive(mount_base(), dev->mount_path);
207 static int umount_device(struct discover_device *dev)
212 if (!dev->mount_path)
217 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
222 execl(pb_system_apps.umount, pb_system_apps.umount,
223 dev->mount_path, NULL);
227 if (waitpid(pid, &status, 0) == -1) {
228 pb_log("%s: waitpid failed: %s\n", __func__,
233 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
236 pb_rmdir_recursive(mount_base(), dev->mount_path);
241 static struct discover_device *find_device(struct device_handler *handler,
244 struct discover_device *dev;
247 for (i = 0; i < handler->n_devices; i++) {
248 dev = handler->devices[i];
249 if (!strcmp(dev->device->id, id))
256 static int destroy_device(void *arg)
258 struct discover_device *dev = arg;
265 static struct discover_device *discover_device_create(
266 struct device_handler *handler,
267 struct discover_context *ctx,
270 struct discover_device *dev;
273 dev = find_device(handler, event->device);
277 dev = talloc_zero(ctx, struct discover_device);
278 dev->device = talloc_zero(dev, struct device);
279 list_init(&dev->boot_options);
281 devname = event_get_param(ctx->event, "DEVNAME");
283 dev->device_path = talloc_strdup(dev, devname);
285 dev->device->id = talloc_strdup(dev, event->device);
287 talloc_set_destructor(dev, destroy_device);
292 struct discover_boot_option *discover_boot_option_create(
293 struct discover_context *ctx,
294 struct discover_device *device)
296 struct discover_boot_option *opt;
298 opt = talloc_zero(ctx, struct discover_boot_option);
299 opt->option = talloc_zero(opt, struct boot_option);
300 opt->device = device;
306 static int handle_add_udev_event(struct device_handler *handler,
309 struct discover_context *ctx;
310 struct discover_device *dev;
314 /* create our context */
315 ctx = talloc(handler, struct discover_context);
317 list_init(&ctx->boot_options);
319 /* create our top-level device */
320 dev = discover_device_create(handler, ctx, event);
324 /* try to parse UUID and labels */
325 param = event_get_param(ctx->event, "ID_FS_UUID");
327 dev->uuid = talloc_strdup(dev, param);
329 param = event_get_param(ctx->event, "ID_FS_LABEL");
331 dev->label = talloc_strdup(dev, param);
333 rc = mount_device(dev);
339 /* run the parsers. This will populate the ctx's boot_option list. */
340 iterate_parsers(ctx);
342 /* add discovered stuff to the handler */
343 context_commit(handler, ctx);
350 static int handle_remove_udev_event(struct device_handler *handler,
353 struct discover_device *dev;
355 dev = find_device(handler, event->device);
359 /* remove device from handler device array */
360 device_handler_remove(handler, dev);
365 static int handle_add_user_event(struct device_handler *handler,
368 struct discover_context *ctx;
369 struct discover_device *dev;
372 assert(event->device);
374 ctx = talloc(handler, struct discover_context);
376 list_init(&ctx->boot_options);
378 dev = discover_device_create(handler, ctx, event);
381 rc = parse_user_event(ctx, event);
384 context_commit(handler, ctx);
389 static int handle_remove_user_event(struct device_handler *handler,
392 struct discover_device *dev = find_device(handler, event->device);
397 /* remove device from handler device array */
398 device_handler_remove(handler, dev);
403 typedef int (*event_handler)(struct device_handler *, struct event *);
405 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
406 [EVENT_TYPE_UDEV] = {
407 [EVENT_ACTION_ADD] = handle_add_udev_event,
408 [EVENT_ACTION_REMOVE] = handle_remove_udev_event,
410 [EVENT_TYPE_USER] = {
411 [EVENT_ACTION_ADD] = handle_add_user_event,
412 [EVENT_ACTION_REMOVE] = handle_remove_user_event,
416 int device_handler_event(struct device_handler *handler,
419 if (event->type >= EVENT_TYPE_MAX ||
420 event->action >= EVENT_ACTION_MAX ||
421 !handlers[event->type][event->action]) {
422 pb_log("%s unknown type/action: %d/%d\n", __func__,
423 event->type, event->action);
427 return handlers[event->type][event->action](handler, event);
430 struct device_handler *device_handler_init(struct discover_server *server,
433 struct device_handler *handler;
435 handler = talloc(NULL, struct device_handler);
436 handler->devices = NULL;
437 handler->n_devices = 0;
438 handler->server = server;
439 handler->dry_run = dry_run;
441 /* set up our mount point base */
442 pb_mkdir_recursive(mount_base());
449 void device_handler_destroy(struct device_handler *handler)
451 talloc_free(handler);
454 static int device_match_path(struct discover_device *dev, const char *path)
456 return !strcmp(dev->device_path, path);
459 static int device_match_uuid(struct discover_device *dev, const char *uuid)
461 return dev->uuid && !strcmp(dev->uuid, uuid);
464 static int device_match_label(struct discover_device *dev, const char *label)
466 return dev->label && !strcmp(dev->label, label);
469 static int device_match_id(struct discover_device *dev, const char *id)
471 return !strcmp(dev->device->id, id);
474 static struct discover_device *device_lookup(
475 struct device_handler *device_handler,
476 int (match_fn)(struct discover_device *, const char *),
479 struct discover_device *dev;
485 for (i = 0; i < device_handler->n_devices; i++) {
486 dev = device_handler->devices[i];
488 if (match_fn(dev, str))
495 struct discover_device *device_lookup_by_name(struct device_handler *handler,
498 struct discover_device *dev;
501 if (strncmp(name, "/dev/", strlen("/dev/")))
502 path = talloc_asprintf(NULL, "/dev/%s", name);
504 path = talloc_strdup(NULL, name);
506 dev = device_lookup_by_path(handler, path);
513 struct discover_device *device_lookup_by_path(
514 struct device_handler *device_handler,
517 return device_lookup(device_handler, device_match_path, path);
520 struct discover_device *device_lookup_by_uuid(
521 struct device_handler *device_handler,
524 return device_lookup(device_handler, device_match_uuid, uuid);
527 struct discover_device *device_lookup_by_label(
528 struct device_handler *device_handler,
531 return device_lookup(device_handler, device_match_label, label);
534 struct discover_device *device_lookup_by_id(
535 struct device_handler *device_handler,
538 return device_lookup(device_handler, device_match_id, id);
541 static struct discover_boot_option *find_boot_option_by_id(
542 struct device_handler *handler, const char *id)
546 for (i = 0; i < handler->n_devices; i++) {
547 struct discover_device *dev = handler->devices[i];
548 struct discover_boot_option *opt;
550 list_for_each_entry(&dev->boot_options, opt, list)
551 if (!strcmp(opt->option->id, id))
558 void device_handler_boot(struct device_handler *handler,
559 struct boot_command *cmd)
561 struct discover_boot_option *opt;
563 opt = find_boot_option_by_id(handler, cmd->option_id);
565 boot(handler, opt, cmd, handler->dry_run);