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;
278 struct boot_option *opt;
282 /* create our context */
283 ctx = talloc(handler, struct discover_context);
285 ctx->mount_path = NULL;
289 ctx->id = talloc_strdup(ctx, event->device);
291 devname = event_get_param(ctx->event, "DEVNAME");
293 ctx->device_path = talloc_strdup(ctx, devname);
295 rc = mount_device(ctx);
301 list_add(&handler->contexts, &ctx->list);
302 talloc_set_destructor(ctx, destroy_context);
304 /* set up the top-level device */
305 ctx->device = talloc_zero(ctx, struct device);
306 ctx->device->id = talloc_strdup(ctx->device, ctx->id);
307 list_init(&ctx->device->boot_options);
309 /* run the parsers */
310 iterate_parsers(ctx);
312 /* add device to handler device array */
313 device_handler_add(handler, ctx->device);
315 discover_server_notify_device_add(handler->server, ctx->device);
317 list_for_each_entry(&ctx->device->boot_options, opt, list)
318 discover_server_notify_boot_option_add(handler->server, opt);
323 static int handle_remove_udev_event(struct device_handler *handler,
326 struct discover_context *ctx;
328 ctx = find_context(handler, event->device);
332 discover_server_notify_device_remove(handler->server, ctx->device);
334 /* remove device from handler device array */
335 device_handler_remove(handler, ctx->device);
342 static int handle_add_user_event(struct device_handler *handler,
345 struct boot_option *opt;
346 struct device *device;
348 assert(event->device);
350 device = device_handler_find(handler, event->device);
353 device = talloc_zero(handler, struct device);
358 device->id = talloc_strdup(device, event->device);
359 list_init(&device->boot_options);
361 /* add device to handler device array */
362 device_handler_add(handler, device);
364 discover_server_notify_device_add(handler->server, device);
367 opt = parse_user_event(device, event);
368 discover_server_notify_boot_option_add(handler->server, opt);
377 static int handle_remove_user_event(struct device_handler *handler,
380 struct device *device = device_handler_find(handler, event->device);
385 discover_server_notify_device_remove(handler->server, device);
387 /* remove device from handler device array */
388 device_handler_remove(handler, device);
394 typedef int (*event_handler)(struct device_handler *, struct event *);
396 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
397 [EVENT_TYPE_UDEV] = {
398 [EVENT_ACTION_ADD] = handle_add_udev_event,
399 [EVENT_ACTION_REMOVE] = handle_remove_udev_event,
401 [EVENT_TYPE_USER] = {
402 [EVENT_ACTION_ADD] = handle_add_user_event,
403 [EVENT_ACTION_REMOVE] = handle_remove_user_event,
407 int device_handler_event(struct device_handler *handler,
410 if (event->type >= EVENT_TYPE_MAX ||
411 event->action >= EVENT_ACTION_MAX ||
412 !handlers[event->type][event->action]) {
413 pb_log("%s unknown type/action: %d/%d\n", __func__,
414 event->type, event->action);
418 return handlers[event->type][event->action](handler, event);
421 struct device_handler *device_handler_init(struct discover_server *server,
424 struct device_handler *handler;
426 handler = talloc(NULL, struct device_handler);
427 handler->devices = NULL;
428 handler->n_devices = 0;
429 handler->server = server;
430 handler->dry_run = dry_run;
432 list_init(&handler->contexts);
434 /* set up our mount point base */
435 pb_mkdir_recursive(mount_base());
442 void device_handler_destroy(struct device_handler *handler)
444 talloc_free(handler);
447 static struct boot_option *find_boot_option_by_id(
448 struct device_handler *handler, const char *id)
452 for (i = 0; i < handler->n_devices; i++) {
453 struct device *dev = handler->devices[i];
454 struct boot_option *opt;
456 list_for_each_entry(&dev->boot_options, opt, list)
457 if (!strcmp(opt->id, id))
464 void device_handler_boot(struct device_handler *handler,
465 struct boot_command *cmd)
467 struct boot_option *opt;
469 opt = find_boot_option_by_id(handler, cmd->option_id);
471 boot(handler, opt, cmd, handler->dry_run);