10 #include <talloc/talloc.h>
11 #include <list/list.h>
13 #include <types/types.h>
14 #include <system/system.h>
16 #include "device-handler.h"
17 #include "discover-server.h"
24 struct device_handler {
25 struct discover_server *server;
28 struct discover_device **devices;
29 unsigned int n_devices;
33 * context_commit - Commit a temporary discovery context to the handler,
34 * and notify the clients about any new options / devices
36 static void context_commit(struct device_handler *handler,
37 struct discover_context *ctx)
39 struct discover_device *dev = ctx->device;
40 struct discover_boot_option *opt, *tmp;
41 unsigned int i, existing_device = 0;
43 /* do we already have this device? */
44 for (i = 0; i < handler->n_devices; i++) {
45 if (ctx->device == handler->devices[i]) {
51 /* if not already present, add the device to the handler's array */
52 if (!existing_device) {
54 handler->devices = talloc_realloc(handler, handler->devices,
55 struct discover_device *, handler->n_devices);
56 handler->devices[handler->n_devices - 1] = dev;
57 talloc_steal(handler, dev);
59 discover_server_notify_device_add(handler->server, dev->device);
63 /* move boot options from the context to the device */
64 list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
65 list_remove(&opt->list);
66 list_add(&dev->boot_options, &opt->list);
67 talloc_steal(dev, opt);
68 discover_server_notify_boot_option_add(handler->server,
73 void discover_context_add_boot_option(struct discover_context *ctx,
74 struct discover_boot_option *boot_option)
76 list_add(&ctx->boot_options, &boot_option->list);
77 talloc_steal(ctx, boot_option);
81 * device_handler_remove - Remove a device from the handler device array.
84 static void device_handler_remove(struct device_handler *handler,
85 struct discover_device *device)
89 for (i = 0; i < handler->n_devices; i++)
90 if (handler->devices[i] == device)
93 if (i == handler->n_devices) {
94 assert(0 && "unknown device");
99 memmove(&handler->devices[i], &handler->devices[i + 1],
100 (handler->n_devices - i) * sizeof(handler->devices[0]));
101 handler->devices = talloc_realloc(handler, handler->devices,
102 struct discover_device *, handler->n_devices);
104 discover_server_notify_device_remove(handler->server, device->device);
110 * device_handler_get_device_count - Get the count of current handler devices.
113 int device_handler_get_device_count(const struct device_handler *handler)
115 return handler->n_devices;
119 * device_handler_get_device - Get a handler device by index.
122 const struct discover_device *device_handler_get_device(
123 const struct device_handler *handler, unsigned int index)
125 if (index >= handler->n_devices) {
126 assert(0 && "bad index");
130 return handler->devices[index];
133 static int mount_device(struct discover_device *dev)
137 if (!dev->mount_path)
138 dev->mount_path = join_paths(dev, mount_base(),
141 if (pb_mkdir_recursive(dev->mount_path))
142 pb_log("couldn't create mount directory %s: %s\n",
143 dev->mount_path, strerror(errno));
145 argv[0] = pb_system_apps.mount;
146 argv[1] = dev->device_path;
147 argv[2] = dev->mount_path;
152 if (pb_run_cmd(argv, 1, 0)) {
154 /* Retry mount without ro option. */
156 argv[0] = pb_system_apps.mount;
157 argv[1] = dev->device_path;
158 argv[2] = dev->mount_path;
161 if (pb_run_cmd(argv, 1, 0))
168 pb_rmdir_recursive(mount_base(), dev->mount_path);
172 static int umount_device(struct discover_device *dev)
177 if (!dev->mount_path)
182 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
187 execl(pb_system_apps.umount, pb_system_apps.umount,
188 dev->mount_path, NULL);
192 if (waitpid(pid, &status, 0) == -1) {
193 pb_log("%s: waitpid failed: %s\n", __func__,
198 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
201 pb_rmdir_recursive(mount_base(), dev->mount_path);
206 static struct discover_device *find_device(struct device_handler *handler,
209 struct discover_device *dev;
212 for (i = 0; i < handler->n_devices; i++) {
213 dev = handler->devices[i];
214 if (!strcmp(dev->device->id, id))
221 static int destroy_device(void *arg)
223 struct discover_device *dev = arg;
230 static struct discover_device *discover_device_create(
231 struct device_handler *handler,
232 struct discover_context *ctx,
235 struct discover_device *dev;
238 dev = find_device(handler, event->device);
242 dev = talloc_zero(ctx, struct discover_device);
243 dev->device = talloc_zero(dev, struct device);
244 list_init(&dev->boot_options);
246 devname = event_get_param(ctx->event, "DEVNAME");
248 dev->device_path = talloc_strdup(dev, devname);
250 dev->device->id = talloc_strdup(dev, event->device);
252 talloc_set_destructor(dev, destroy_device);
257 struct discover_boot_option *discover_boot_option_create(
258 struct discover_context *ctx,
259 struct discover_device *device)
261 struct discover_boot_option *opt;
263 opt = talloc_zero(ctx, struct discover_boot_option);
264 opt->option = talloc_zero(opt, struct boot_option);
265 opt->device = device;
271 static int handle_add_udev_event(struct device_handler *handler,
274 struct discover_context *ctx;
275 struct discover_device *dev;
279 /* create our context */
280 ctx = talloc(handler, struct discover_context);
282 list_init(&ctx->boot_options);
284 /* create our top-level device */
285 dev = discover_device_create(handler, ctx, event);
289 /* try to parse UUID and labels */
290 param = event_get_param(ctx->event, "ID_FS_UUID");
292 dev->uuid = talloc_strdup(dev, param);
294 param = event_get_param(ctx->event, "ID_FS_LABEL");
296 dev->label = talloc_strdup(dev, param);
298 rc = mount_device(dev);
304 /* run the parsers. This will populate the ctx's boot_option list. */
305 iterate_parsers(ctx);
307 /* add discovered stuff to the handler */
308 context_commit(handler, ctx);
315 static int handle_remove_udev_event(struct device_handler *handler,
318 struct discover_device *dev;
320 dev = find_device(handler, event->device);
324 /* remove device from handler device array */
325 device_handler_remove(handler, dev);
330 static int handle_add_user_event(struct device_handler *handler,
333 struct discover_context *ctx;
334 struct discover_device *dev;
337 assert(event->device);
339 ctx = talloc(handler, struct discover_context);
341 list_init(&ctx->boot_options);
343 dev = discover_device_create(handler, ctx, event);
346 rc = parse_user_event(ctx, event);
349 context_commit(handler, ctx);
354 static int handle_remove_user_event(struct device_handler *handler,
357 struct discover_device *dev = find_device(handler, event->device);
362 /* remove device from handler device array */
363 device_handler_remove(handler, dev);
368 typedef int (*event_handler)(struct device_handler *, struct event *);
370 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
371 [EVENT_TYPE_UDEV] = {
372 [EVENT_ACTION_ADD] = handle_add_udev_event,
373 [EVENT_ACTION_REMOVE] = handle_remove_udev_event,
375 [EVENT_TYPE_USER] = {
376 [EVENT_ACTION_ADD] = handle_add_user_event,
377 [EVENT_ACTION_REMOVE] = handle_remove_user_event,
381 int device_handler_event(struct device_handler *handler,
384 if (event->type >= EVENT_TYPE_MAX ||
385 event->action >= EVENT_ACTION_MAX ||
386 !handlers[event->type][event->action]) {
387 pb_log("%s unknown type/action: %d/%d\n", __func__,
388 event->type, event->action);
392 return handlers[event->type][event->action](handler, event);
395 struct device_handler *device_handler_init(struct discover_server *server,
398 struct device_handler *handler;
400 handler = talloc(NULL, struct device_handler);
401 handler->devices = NULL;
402 handler->n_devices = 0;
403 handler->server = server;
404 handler->dry_run = dry_run;
406 /* set up our mount point base */
407 pb_mkdir_recursive(mount_base());
414 void device_handler_destroy(struct device_handler *handler)
416 talloc_free(handler);
419 static int device_match_path(struct discover_device *dev, const char *path)
421 return !strcmp(dev->device_path, path);
424 static int device_match_uuid(struct discover_device *dev, const char *uuid)
426 return dev->uuid && !strcmp(dev->uuid, uuid);
429 static int device_match_label(struct discover_device *dev, const char *label)
431 return dev->label && !strcmp(dev->label, label);
434 static int device_match_id(struct discover_device *dev, const char *id)
436 return !strcmp(dev->device->id, id);
439 static struct discover_device *device_lookup(
440 struct device_handler *device_handler,
441 int (match_fn)(struct discover_device *, const char *),
444 struct discover_device *dev;
450 for (i = 0; i < device_handler->n_devices; i++) {
451 dev = device_handler->devices[i];
453 if (match_fn(dev, str))
460 struct discover_device *device_lookup_by_name(struct device_handler *handler,
463 struct discover_device *dev;
466 if (strncmp(name, "/dev/", strlen("/dev/")))
467 path = talloc_asprintf(NULL, "/dev/%s", name);
469 path = talloc_strdup(NULL, name);
471 dev = device_lookup_by_path(handler, path);
478 struct discover_device *device_lookup_by_path(
479 struct device_handler *device_handler,
482 return device_lookup(device_handler, device_match_path, path);
485 struct discover_device *device_lookup_by_uuid(
486 struct device_handler *device_handler,
489 return device_lookup(device_handler, device_match_uuid, uuid);
492 struct discover_device *device_lookup_by_label(
493 struct device_handler *device_handler,
496 return device_lookup(device_handler, device_match_label, label);
499 struct discover_device *device_lookup_by_id(
500 struct device_handler *device_handler,
503 return device_lookup(device_handler, device_match_id, id);
506 static struct discover_boot_option *find_boot_option_by_id(
507 struct device_handler *handler, const char *id)
511 for (i = 0; i < handler->n_devices; i++) {
512 struct discover_device *dev = handler->devices[i];
513 struct discover_boot_option *opt;
515 list_for_each_entry(&dev->boot_options, opt, list)
516 if (!strcmp(opt->option->id, id))
523 void device_handler_boot(struct device_handler *handler,
524 struct boot_command *cmd)
526 struct discover_boot_option *opt;
528 opt = find_boot_option_by_id(handler, cmd->option_id);
530 boot(handler, opt, cmd, handler->dry_run);