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_context *ctx)
134 struct discover_device *dev = ctx->device;
140 .dir = "disk/by-uuid"
143 .env = "ID_FS_LABEL",
144 .dir = "disk/by-label"
151 for (link = links; link->env; link++) {
152 char *enc, *dir, *path;
155 value = event_get_param(ctx->event, link->env);
156 if (!value || !*value)
159 enc = encode_label(ctx, value);
160 dir = join_paths(ctx, mount_base(), link->dir);
161 path = join_paths(dev, dir, value);
163 if (!pb_mkdir_recursive(dir)) {
165 if (symlink(dev->mount_path, path)) {
166 pb_log("symlink(%s,%s): %s\n",
167 dev->mount_path, path,
171 int i = dev->n_links++;
172 dev->links = talloc_realloc(dev,
175 dev->links[i] = path;
185 static void remove_device_links(struct discover_device *dev)
189 for (i = 0; i < dev->n_links; i++)
190 unlink(dev->links[i]);
193 static int mount_device(struct discover_context *ctx)
195 struct discover_device *dev = ctx->device;
196 const char *mountpoint;
199 if (!dev->mount_path) {
200 mountpoint = mountpoint_for_device(dev->device_path);
201 dev->mount_path = talloc_strdup(dev, mountpoint);
204 if (pb_mkdir_recursive(dev->mount_path))
205 pb_log("couldn't create mount directory %s: %s\n",
206 dev->mount_path, strerror(errno));
208 argv[0] = pb_system_apps.mount;
209 argv[1] = dev->device_path;
210 argv[2] = dev->mount_path;
215 if (pb_run_cmd(argv, 1, 0)) {
217 /* Retry mount without ro option. */
219 argv[0] = pb_system_apps.mount;
220 argv[1] = dev->device_path;
221 argv[2] = dev->mount_path;
224 if (pb_run_cmd(argv, 1, 0))
228 setup_device_links(ctx);
232 pb_rmdir_recursive(mount_base(), dev->mount_path);
236 static int umount_device(struct discover_device *dev)
241 remove_device_links(dev);
245 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
250 execl(pb_system_apps.umount, pb_system_apps.umount,
251 dev->mount_path, NULL);
255 if (waitpid(pid, &status, 0) == -1) {
256 pb_log("%s: waitpid failed: %s\n", __func__,
261 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
264 pb_rmdir_recursive(mount_base(), dev->mount_path);
269 static struct discover_device *find_device(struct device_handler *handler,
272 struct discover_device *dev;
275 for (i = 0; i < handler->n_devices; i++) {
276 dev = handler->devices[i];
277 if (!strcmp(dev->device->id, id))
284 static int destroy_device(void *arg)
286 struct discover_device *dev = arg;
293 static struct discover_device *discover_device_create(
294 struct device_handler *handler,
295 struct discover_context *ctx,
298 struct discover_device *dev;
301 dev = find_device(handler, event->device);
305 dev = talloc_zero(ctx, struct discover_device);
306 dev->device = talloc_zero(dev, struct device);
307 list_init(&dev->device->boot_options);
309 devname = event_get_param(ctx->event, "DEVNAME");
311 dev->device_path = talloc_strdup(dev, devname);
313 dev->device->id = talloc_strdup(dev, event->device);
315 talloc_set_destructor(dev, destroy_device);
320 static int handle_add_udev_event(struct device_handler *handler,
323 struct discover_context *ctx;
324 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 rc = mount_device(ctx);
343 /* run the parsers. This will populate the ctx's boot_option list. */
344 iterate_parsers(ctx);
346 /* add discovered stuff to the handler */
347 context_commit(handler, ctx);
354 static int handle_remove_udev_event(struct device_handler *handler,
357 struct discover_device *dev;
359 dev = find_device(handler, event->device);
363 /* remove device from handler device array */
364 device_handler_remove(handler, dev);
369 static int handle_add_user_event(struct device_handler *handler,
372 struct discover_context *ctx;
373 struct discover_device *dev;
376 assert(event->device);
378 ctx = talloc(handler, struct discover_context);
380 list_init(&ctx->boot_options);
382 dev = discover_device_create(handler, ctx, event);
385 rc = parse_user_event(ctx, event);
388 context_commit(handler, ctx);
393 static int handle_remove_user_event(struct device_handler *handler,
396 struct discover_device *dev = find_device(handler, event->device);
401 /* remove device from handler device array */
402 device_handler_remove(handler, dev);
407 typedef int (*event_handler)(struct device_handler *, struct event *);
409 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
410 [EVENT_TYPE_UDEV] = {
411 [EVENT_ACTION_ADD] = handle_add_udev_event,
412 [EVENT_ACTION_REMOVE] = handle_remove_udev_event,
414 [EVENT_TYPE_USER] = {
415 [EVENT_ACTION_ADD] = handle_add_user_event,
416 [EVENT_ACTION_REMOVE] = handle_remove_user_event,
420 int device_handler_event(struct device_handler *handler,
423 if (event->type >= EVENT_TYPE_MAX ||
424 event->action >= EVENT_ACTION_MAX ||
425 !handlers[event->type][event->action]) {
426 pb_log("%s unknown type/action: %d/%d\n", __func__,
427 event->type, event->action);
431 return handlers[event->type][event->action](handler, event);
434 struct device_handler *device_handler_init(struct discover_server *server,
437 struct device_handler *handler;
439 handler = talloc(NULL, struct device_handler);
440 handler->devices = NULL;
441 handler->n_devices = 0;
442 handler->server = server;
443 handler->dry_run = dry_run;
445 /* set up our mount point base */
446 pb_mkdir_recursive(mount_base());
453 void device_handler_destroy(struct device_handler *handler)
455 talloc_free(handler);
458 static struct boot_option *find_boot_option_by_id(
459 struct device_handler *handler, const char *id)
463 for (i = 0; i < handler->n_devices; i++) {
464 struct discover_device *dev = handler->devices[i];
465 struct boot_option *opt;
467 list_for_each_entry(&dev->device->boot_options, opt, list)
468 if (!strcmp(opt->id, id))
475 void device_handler_boot(struct device_handler *handler,
476 struct boot_command *cmd)
478 struct boot_option *opt;
480 opt = find_boot_option_by_id(handler, cmd->option_id);
482 boot(handler, opt, cmd, handler->dry_run);