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 unsigned int i, existing_device;
41 struct boot_option *opt, *tmp;
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->device->boot_options, &opt->list);
67 dev->device->n_options++;
68 discover_server_notify_boot_option_add(handler->server, opt);
72 void discover_context_add_boot_option(struct discover_context *ctx,
73 struct boot_option *boot_option)
75 list_add(&ctx->boot_options, &boot_option->list);
76 talloc_steal(ctx, boot_option);
80 * device_handler_remove - Remove a device from the handler device array.
83 static void device_handler_remove(struct device_handler *handler,
84 struct discover_device *device)
88 for (i = 0; i < handler->n_devices; i++)
89 if (handler->devices[i] == device)
92 if (i == handler->n_devices) {
93 assert(0 && "unknown device");
98 memmove(&handler->devices[i], &handler->devices[i + 1],
99 (handler->n_devices - i) * sizeof(handler->devices[0]));
100 handler->devices = talloc_realloc(handler, handler->devices,
101 struct discover_device *, handler->n_devices);
103 discover_server_notify_device_remove(handler->server, device->device);
109 * device_handler_get_device_count - Get the count of current handler devices.
112 int device_handler_get_device_count(const struct device_handler *handler)
114 return handler->n_devices;
118 * device_handler_get_device - Get a handler device by index.
121 const struct device *device_handler_get_device(
122 const struct device_handler *handler, unsigned int index)
124 if (index >= handler->n_devices) {
125 assert(0 && "bad index");
129 return handler->devices[index]->device;
132 static void setup_device_links(struct discover_device *dev)
135 const char *dir, *val;
138 .dir = "disk/by-uuid",
142 .dir = "disk/by-label",
150 for (link = links; link->dir; link++) {
151 char *enc, *dir, *path;
153 if (!link->val || !*link->val)
156 enc = encode_label(dev, link->val);
157 dir = join_paths(dev, mount_base(), link->dir);
158 path = join_paths(dev, dir, enc);
160 if (!pb_mkdir_recursive(dir)) {
162 if (symlink(dev->mount_path, path)) {
163 pb_log("symlink(%s,%s): %s\n",
164 dev->mount_path, path,
168 int i = dev->n_links++;
169 dev->links = talloc_realloc(dev,
172 dev->links[i] = path;
182 static void remove_device_links(struct discover_device *dev)
186 for (i = 0; i < dev->n_links; i++)
187 unlink(dev->links[i]);
190 static int mount_device(struct discover_device *dev)
192 const char *mountpoint;
195 if (!dev->mount_path) {
196 mountpoint = mountpoint_for_device(dev->device_path);
197 dev->mount_path = talloc_strdup(dev, mountpoint);
200 if (pb_mkdir_recursive(dev->mount_path))
201 pb_log("couldn't create mount directory %s: %s\n",
202 dev->mount_path, strerror(errno));
204 argv[0] = pb_system_apps.mount;
205 argv[1] = dev->device_path;
206 argv[2] = dev->mount_path;
211 if (pb_run_cmd(argv, 1, 0)) {
213 /* Retry mount without ro option. */
215 argv[0] = pb_system_apps.mount;
216 argv[1] = dev->device_path;
217 argv[2] = dev->mount_path;
220 if (pb_run_cmd(argv, 1, 0))
224 setup_device_links(dev);
228 pb_rmdir_recursive(mount_base(), dev->mount_path);
232 static int umount_device(struct discover_device *dev)
237 remove_device_links(dev);
239 if (!dev->mount_path)
244 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
249 execl(pb_system_apps.umount, pb_system_apps.umount,
250 dev->mount_path, NULL);
254 if (waitpid(pid, &status, 0) == -1) {
255 pb_log("%s: waitpid failed: %s\n", __func__,
260 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
263 pb_rmdir_recursive(mount_base(), dev->mount_path);
268 static struct discover_device *find_device(struct device_handler *handler,
271 struct discover_device *dev;
274 for (i = 0; i < handler->n_devices; i++) {
275 dev = handler->devices[i];
276 if (!strcmp(dev->device->id, id))
283 static int destroy_device(void *arg)
285 struct discover_device *dev = arg;
292 static struct discover_device *discover_device_create(
293 struct device_handler *handler,
294 struct discover_context *ctx,
297 struct discover_device *dev;
300 dev = find_device(handler, event->device);
304 dev = talloc_zero(ctx, struct discover_device);
305 dev->device = talloc_zero(dev, struct device);
306 list_init(&dev->device->boot_options);
308 devname = event_get_param(ctx->event, "DEVNAME");
310 dev->device_path = talloc_strdup(dev, devname);
312 dev->device->id = talloc_strdup(dev, event->device);
314 talloc_set_destructor(dev, destroy_device);
319 static int handle_add_udev_event(struct device_handler *handler,
322 struct discover_context *ctx;
323 struct discover_device *dev;
327 /* create our context */
328 ctx = talloc(handler, struct discover_context);
330 list_init(&ctx->boot_options);
332 /* create our top-level device */
333 dev = discover_device_create(handler, ctx, event);
337 /* try to parse UUID and labels */
338 param = event_get_param(ctx->event, "ID_FS_UUID");
340 dev->uuid = talloc_strdup(dev, param);
342 param = event_get_param(ctx->event, "ID_FS_LABEL");
344 dev->label = talloc_strdup(dev, param);
346 rc = mount_device(dev);
352 /* run the parsers. This will populate the ctx's boot_option list. */
353 iterate_parsers(ctx);
355 /* add discovered stuff to the handler */
356 context_commit(handler, ctx);
363 static int handle_remove_udev_event(struct device_handler *handler,
366 struct discover_device *dev;
368 dev = find_device(handler, event->device);
372 /* remove device from handler device array */
373 device_handler_remove(handler, dev);
378 static int handle_add_user_event(struct device_handler *handler,
381 struct discover_context *ctx;
382 struct discover_device *dev;
385 assert(event->device);
387 ctx = talloc(handler, struct discover_context);
389 list_init(&ctx->boot_options);
391 dev = discover_device_create(handler, ctx, event);
394 rc = parse_user_event(ctx, event);
397 context_commit(handler, ctx);
402 static int handle_remove_user_event(struct device_handler *handler,
405 struct discover_device *dev = find_device(handler, event->device);
410 /* remove device from handler device array */
411 device_handler_remove(handler, dev);
416 typedef int (*event_handler)(struct device_handler *, struct event *);
418 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
419 [EVENT_TYPE_UDEV] = {
420 [EVENT_ACTION_ADD] = handle_add_udev_event,
421 [EVENT_ACTION_REMOVE] = handle_remove_udev_event,
423 [EVENT_TYPE_USER] = {
424 [EVENT_ACTION_ADD] = handle_add_user_event,
425 [EVENT_ACTION_REMOVE] = handle_remove_user_event,
429 int device_handler_event(struct device_handler *handler,
432 if (event->type >= EVENT_TYPE_MAX ||
433 event->action >= EVENT_ACTION_MAX ||
434 !handlers[event->type][event->action]) {
435 pb_log("%s unknown type/action: %d/%d\n", __func__,
436 event->type, event->action);
440 return handlers[event->type][event->action](handler, event);
443 struct device_handler *device_handler_init(struct discover_server *server,
446 struct device_handler *handler;
448 handler = talloc(NULL, struct device_handler);
449 handler->devices = NULL;
450 handler->n_devices = 0;
451 handler->server = server;
452 handler->dry_run = dry_run;
454 /* set up our mount point base */
455 pb_mkdir_recursive(mount_base());
462 void device_handler_destroy(struct device_handler *handler)
464 talloc_free(handler);
467 static int device_match_path(struct discover_device *dev, const char *path)
469 return !strcmp(dev->device_path, path);
472 static int device_match_uuid(struct discover_device *dev, const char *uuid)
474 return dev->uuid && !strcmp(dev->uuid, uuid);
477 static int device_match_label(struct discover_device *dev, const char *label)
479 return dev->label && !strcmp(dev->label, label);
482 static int device_match_id(struct discover_device *dev, const char *id)
484 return !strcmp(dev->device->id, id);
487 static struct discover_device *device_lookup(
488 struct device_handler *device_handler,
489 int (match_fn)(struct discover_device *, const char *),
492 struct discover_device *dev;
498 for (i = 0; i < device_handler->n_devices; i++) {
499 dev = device_handler->devices[i];
501 if (match_fn(dev, str))
508 struct discover_device *device_lookup_by_name(struct device_handler *handler,
511 struct discover_device *dev;
514 if (strncmp(name, "/dev/", strlen("/dev/")))
515 path = talloc_asprintf(NULL, "/dev/%s", name);
517 path = talloc_strdup(NULL, name);
519 dev = device_lookup_by_path(handler, path);
526 struct discover_device *device_lookup_by_path(
527 struct device_handler *device_handler,
530 return device_lookup(device_handler, device_match_path, path);
533 struct discover_device *device_lookup_by_uuid(
534 struct device_handler *device_handler,
537 return device_lookup(device_handler, device_match_uuid, uuid);
540 struct discover_device *device_lookup_by_label(
541 struct device_handler *device_handler,
544 return device_lookup(device_handler, device_match_label, label);
547 struct discover_device *device_lookup_by_id(
548 struct device_handler *device_handler,
551 return device_lookup(device_handler, device_match_id, id);
554 static struct boot_option *find_boot_option_by_id(
555 struct device_handler *handler, const char *id)
559 for (i = 0; i < handler->n_devices; i++) {
560 struct discover_device *dev = handler->devices[i];
561 struct boot_option *opt;
563 list_for_each_entry(&dev->device->boot_options, opt, list)
564 if (!strcmp(opt->id, id))
571 void device_handler_boot(struct device_handler *handler,
572 struct boot_command *cmd)
574 struct boot_option *opt;
576 opt = find_boot_option_by_id(handler, cmd->option_id);
578 boot(handler, opt, cmd, handler->dry_run);