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 device **devices;
29 unsigned int n_devices;
35 * device_handler_add - Add a device to the handler device array.
38 static void device_handler_add(struct device_handler *handler,
39 struct device *device)
42 handler->devices = talloc_realloc(handler, handler->devices,
43 struct device *, handler->n_devices);
44 handler->devices[handler->n_devices - 1] = device;
48 * device_handler_remove - Remove a device from the handler device array.
51 static void device_handler_remove(struct device_handler *handler,
52 struct device *device)
56 for (i = 0; i < handler->n_devices; i++)
57 if (handler->devices[i] == device)
60 if (i == handler->n_devices) {
61 assert(0 && "unknown device");
66 memmove(&handler->devices[i], &handler->devices[i + 1],
67 (handler->n_devices - i) * sizeof(handler->devices[0]));
68 handler->devices = talloc_realloc(handler, handler->devices,
69 struct device *, handler->n_devices);
73 * device_handler_find - Find a handler device by id.
76 static struct device *device_handler_find(struct device_handler *handler,
83 for (i = 0; i < handler->n_devices; i++)
84 if (handler->devices[i]->id
85 && streq(handler->devices[i]->id, id))
86 return handler->devices[i];
88 pb_log("%s: unknown device: %s\n", __func__, id);
93 * device_handler_get_device_count - Get the count of current handler devices.
96 int device_handler_get_device_count(const struct device_handler *handler)
98 return handler->n_devices;
102 * device_handler_get_device - Get a handler device by index.
105 const struct device *device_handler_get_device(
106 const struct device_handler *handler, unsigned int index)
108 if (index >= handler->n_devices) {
109 assert(0 && "bad index");
113 return handler->devices[index];
116 static void setup_device_links(struct discover_context *ctx)
123 .dir = "disk/by-uuid"
126 .env = "ID_FS_LABEL",
127 .dir = "disk/by-label"
134 for (link = links; link->env; link++) {
135 char *enc, *dir, *path;
138 value = event_get_param(ctx->event, link->env);
139 if (!value || !*value)
142 enc = encode_label(ctx, value);
143 dir = join_paths(ctx, mount_base(), link->dir);
144 path = join_paths(ctx, dir, value);
146 if (!pb_mkdir_recursive(dir)) {
148 if (symlink(ctx->mount_path, path)) {
149 pb_log("symlink(%s,%s): %s\n",
150 ctx->mount_path, path,
154 int i = ctx->n_links++;
155 ctx->links = talloc_realloc(ctx,
158 ctx->links[i] = path;
168 static void remove_device_links(struct discover_context *ctx)
172 for (i = 0; i < ctx->n_links; i++)
173 unlink(ctx->links[i]);
176 static int mount_device(struct discover_context *ctx)
178 const char *mountpoint;
181 if (!ctx->mount_path) {
182 mountpoint = mountpoint_for_device(ctx->device_path);
183 ctx->mount_path = talloc_strdup(ctx, mountpoint);
186 if (pb_mkdir_recursive(ctx->mount_path))
187 pb_log("couldn't create mount directory %s: %s\n",
188 ctx->mount_path, strerror(errno));
190 argv[0] = pb_system_apps.mount;
191 argv[1] = ctx->device_path;
192 argv[2] = ctx->mount_path;
197 if (pb_run_cmd(argv, 1, 0)) {
199 /* Retry mount without ro option. */
201 argv[0] = pb_system_apps.mount;
202 argv[1] = ctx->device_path;
203 argv[2] = ctx->mount_path;
206 if (pb_run_cmd(argv, 1, 0))
210 setup_device_links(ctx);
214 pb_rmdir_recursive(mount_base(), ctx->mount_path);
218 static int umount_device(struct discover_context *ctx)
223 remove_device_links(ctx);
227 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
232 execl(pb_system_apps.umount, pb_system_apps.umount,
233 ctx->mount_path, NULL);
237 if (waitpid(pid, &status, 0) == -1) {
238 pb_log("%s: waitpid failed: %s\n", __func__,
243 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
246 pb_rmdir_recursive(mount_base(), ctx->mount_path);
251 static struct discover_context *find_context(struct device_handler *handler,
254 struct discover_context *ctx;
256 list_for_each_entry(&handler->contexts, ctx, list) {
257 if (!strcmp(ctx->id, id))
264 static int destroy_context(void *arg)
266 struct discover_context *ctx = arg;
268 list_remove(&ctx->list);
274 static int handle_add_udev_event(struct device_handler *handler,
277 struct discover_context *ctx;
281 /* create our context */
282 ctx = talloc(handler, struct discover_context);
284 ctx->mount_path = NULL;
288 ctx->id = talloc_strdup(ctx, event->device);
290 devname = event_get_param(ctx->event, "DEVNAME");
292 ctx->device_path = talloc_strdup(ctx, devname);
294 rc = mount_device(ctx);
300 list_add(&handler->contexts, &ctx->list);
301 talloc_set_destructor(ctx, destroy_context);
303 /* set up the top-level device */
304 ctx->device = talloc_zero(ctx, struct device);
305 ctx->device->id = talloc_strdup(ctx->device, ctx->id);
306 list_init(&ctx->device->boot_options);
308 /* run the parsers */
309 iterate_parsers(ctx);
311 /* add device to handler device array */
312 device_handler_add(handler, ctx->device);
314 discover_server_notify_add(handler->server, ctx->device);
319 static int handle_remove_udev_event(struct device_handler *handler,
322 struct discover_context *ctx;
324 ctx = find_context(handler, event->device);
328 discover_server_notify_remove(handler->server, ctx->device);
330 /* remove device from handler device array */
331 device_handler_remove(handler, ctx->device);
338 static int handle_add_user_event(struct device_handler *handler,
341 struct device *device;
343 assert(event->device);
345 device = talloc_zero(handler, struct device);
350 device->id = talloc_strdup(device, event->device);
351 list_init(&device->boot_options);
353 parse_user_event(device, event);
355 discover_server_notify_add(handler->server, device);
357 /* add device to handler device array */
358 device_handler_add(handler, device);
367 static int handle_remove_user_event(struct device_handler *handler,
370 struct device *device = device_handler_find(handler, event->device);
375 discover_server_notify_remove(handler->server, device);
377 /* remove device from handler device array */
378 device_handler_remove(handler, device);
384 typedef int (*event_handler)(struct device_handler *, struct event *);
386 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
387 [EVENT_TYPE_UDEV] = {
388 [EVENT_ACTION_ADD] = handle_add_udev_event,
389 [EVENT_ACTION_REMOVE] = handle_remove_udev_event,
391 [EVENT_TYPE_USER] = {
392 [EVENT_ACTION_ADD] = handle_add_user_event,
393 [EVENT_ACTION_REMOVE] = handle_remove_user_event,
397 int device_handler_event(struct device_handler *handler,
400 if (event->type >= EVENT_TYPE_MAX ||
401 event->action >= EVENT_ACTION_MAX ||
402 !handlers[event->type][event->action]) {
403 pb_log("%s unknown type/action: %d/%d\n", __func__,
404 event->type, event->action);
408 return handlers[event->type][event->action](handler, event);
411 struct device_handler *device_handler_init(struct discover_server *server,
414 struct device_handler *handler;
416 handler = talloc(NULL, struct device_handler);
417 handler->devices = NULL;
418 handler->n_devices = 0;
419 handler->server = server;
420 handler->dry_run = dry_run;
422 list_init(&handler->contexts);
424 /* set up our mount point base */
425 pb_mkdir_recursive(mount_base());
432 void device_handler_destroy(struct device_handler *handler)
434 talloc_free(handler);
437 static struct boot_option *find_boot_option_by_id(
438 struct device_handler *handler, const char *id)
442 for (i = 0; i < handler->n_devices; i++) {
443 struct device *dev = handler->devices[i];
444 struct boot_option *opt;
446 list_for_each_entry(&dev->boot_options, opt, list)
447 if (!strcmp(opt->id, id))
454 void device_handler_boot(struct device_handler *handler,
455 struct boot_command *cmd)
457 struct boot_option *opt;
459 opt = find_boot_option_by_id(handler, cmd->option_id);
461 boot(handler, opt, cmd, handler->dry_run);