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;
27 struct device **devices;
28 unsigned int n_devices;
34 * device_handler_add - Add a device to the handler device array.
37 static void device_handler_add(struct device_handler *handler,
38 struct device *device)
41 handler->devices = talloc_realloc(handler, handler->devices,
42 struct device *, handler->n_devices);
43 handler->devices[handler->n_devices - 1] = device;
47 * device_handler_remove - Remove a device from the handler device array.
50 static void device_handler_remove(struct device_handler *handler,
51 struct device *device)
55 for (i = 0; i < handler->n_devices; i++)
56 if (handler->devices[i] == device)
59 if (i == handler->n_devices) {
60 assert(0 && "unknown device");
65 memmove(&handler->devices[i], &handler->devices[i + 1],
66 (handler->n_devices - i) * sizeof(handler->devices[0]));
67 handler->devices = talloc_realloc(handler, handler->devices,
68 struct device *, handler->n_devices);
72 * device_handler_find - Find a handler device by id.
75 static struct device *device_handler_find(struct device_handler *handler,
82 for (i = 0; i < handler->n_devices; i++)
83 if (handler->devices[i]->id
84 && streq(handler->devices[i]->id, id))
85 return handler->devices[i];
87 pb_log("%s: unknown device: %s\n", __func__, id);
92 * device_handler_get_device_count - Get the count of current handler devices.
95 int device_handler_get_device_count(const struct device_handler *handler)
97 return handler->n_devices;
101 * device_handler_get_device - Get a handler device by index.
104 const struct device *device_handler_get_device(
105 const struct device_handler *handler, unsigned int index)
107 if (index >= handler->n_devices) {
108 assert(0 && "bad index");
112 return handler->devices[index];
115 static void setup_device_links(struct discover_context *ctx)
122 .dir = "disk/by-uuid"
125 .env = "ID_FS_LABEL",
126 .dir = "disk/by-label"
133 for (link = links; link->env; link++) {
134 char *enc, *dir, *path;
137 value = event_get_param(ctx->event, link->env);
138 if (!value || !*value)
141 enc = encode_label(ctx, value);
142 dir = join_paths(ctx, mount_base(), link->dir);
143 path = join_paths(ctx, dir, value);
145 if (!pb_mkdir_recursive(dir)) {
147 if (symlink(ctx->mount_path, path)) {
148 pb_log("symlink(%s,%s): %s\n",
149 ctx->mount_path, path,
153 int i = ctx->n_links++;
154 ctx->links = talloc_realloc(ctx,
157 ctx->links[i] = path;
167 static void remove_device_links(struct discover_context *ctx)
171 for (i = 0; i < ctx->n_links; i++)
172 unlink(ctx->links[i]);
175 static int mount_device(struct discover_context *ctx)
177 const char *mountpoint;
180 if (!ctx->mount_path) {
181 mountpoint = mountpoint_for_device(ctx->device_path);
182 ctx->mount_path = talloc_strdup(ctx, mountpoint);
185 if (pb_mkdir_recursive(ctx->mount_path))
186 pb_log("couldn't create mount directory %s: %s\n",
187 ctx->mount_path, strerror(errno));
189 argv[0] = pb_system_apps.mount;
190 argv[1] = ctx->device_path;
191 argv[2] = ctx->mount_path;
196 if (pb_run_cmd(argv, 1, 0)) {
198 /* Retry mount without ro option. */
200 argv[0] = pb_system_apps.mount;
201 argv[1] = ctx->device_path;
202 argv[2] = ctx->mount_path;
205 if (pb_run_cmd(argv, 1, 0))
209 setup_device_links(ctx);
213 pb_rmdir_recursive(mount_base(), ctx->mount_path);
217 static int umount_device(struct discover_context *ctx)
222 remove_device_links(ctx);
226 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
231 execl(pb_system_apps.umount, pb_system_apps.umount,
232 ctx->mount_path, NULL);
236 if (waitpid(pid, &status, 0) == -1) {
237 pb_log("%s: waitpid failed: %s\n", __func__,
242 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
245 pb_rmdir_recursive(mount_base(), ctx->mount_path);
250 static struct discover_context *find_context(struct device_handler *handler,
253 struct discover_context *ctx;
255 list_for_each_entry(&handler->contexts, ctx, list) {
256 if (!strcmp(ctx->id, id))
263 static int destroy_context(void *arg)
265 struct discover_context *ctx = arg;
267 list_remove(&ctx->list);
273 static int handle_add_udev_event(struct device_handler *handler,
276 struct discover_context *ctx;
280 /* create our context */
281 ctx = talloc(handler, struct discover_context);
283 ctx->mount_path = NULL;
287 ctx->id = talloc_strdup(ctx, event->device);
289 devname = event_get_param(ctx->event, "DEVNAME");
291 ctx->device_path = talloc_strdup(ctx, devname);
293 rc = mount_device(ctx);
299 list_add(&handler->contexts, &ctx->list);
300 talloc_set_destructor(ctx, destroy_context);
302 /* set up the top-level device */
303 ctx->device = talloc_zero(ctx, struct device);
304 ctx->device->id = talloc_strdup(ctx->device, ctx->id);
305 list_init(&ctx->device->boot_options);
307 /* run the parsers */
308 iterate_parsers(ctx);
310 /* add device to handler device array */
311 device_handler_add(handler, ctx->device);
313 discover_server_notify_add(handler->server, ctx->device);
318 static int handle_remove_udev_event(struct device_handler *handler,
321 struct discover_context *ctx;
323 ctx = find_context(handler, event->device);
327 discover_server_notify_remove(handler->server, ctx->device);
329 /* remove device from handler device array */
330 device_handler_remove(handler, ctx->device);
337 static int handle_add_user_event(struct device_handler *handler,
340 struct device *device;
342 assert(event->device);
344 device = talloc_zero(handler, struct device);
349 device->id = talloc_strdup(device, event->device);
350 list_init(&device->boot_options);
352 parse_user_event(device, event);
354 discover_server_notify_add(handler->server, device);
356 /* add device to handler device array */
357 device_handler_add(handler, device);
366 static int handle_remove_user_event(struct device_handler *handler,
369 struct device *device = device_handler_find(handler, event->device);
374 discover_server_notify_remove(handler->server, device);
376 /* remove device from handler device array */
377 device_handler_remove(handler, device);
383 typedef int (*event_handler)(struct device_handler *, struct event *);
385 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
386 [EVENT_TYPE_UDEV] = {
387 [EVENT_ACTION_ADD] = handle_add_udev_event,
388 [EVENT_ACTION_REMOVE] = handle_remove_udev_event,
390 [EVENT_TYPE_USER] = {
391 [EVENT_ACTION_ADD] = handle_add_user_event,
392 [EVENT_ACTION_REMOVE] = handle_remove_user_event,
396 int device_handler_event(struct device_handler *handler,
399 if (event->type >= EVENT_TYPE_MAX ||
400 event->action >= EVENT_ACTION_MAX ||
401 !handlers[event->type][event->action]) {
402 pb_log("%s unknown type/action: %d/%d\n", __func__,
403 event->type, event->action);
407 return handlers[event->type][event->action](handler, event);
410 struct device_handler *device_handler_init(struct discover_server *server)
412 struct device_handler *handler;
414 handler = talloc(NULL, struct device_handler);
415 handler->devices = NULL;
416 handler->n_devices = 0;
417 handler->server = server;
419 list_init(&handler->contexts);
421 /* set up our mount point base */
422 pb_mkdir_recursive(mount_base());
429 void device_handler_destroy(struct device_handler *handler)
431 talloc_free(handler);
434 static struct boot_option *find_boot_option_by_id(
435 struct device_handler *handler, const char *id)
439 for (i = 0; i < handler->n_devices; i++) {
440 struct device *dev = handler->devices[i];
441 struct boot_option *opt;
443 list_for_each_entry(&dev->boot_options, opt, list)
444 if (!strcmp(opt->id, id))
451 void device_handler_boot(struct device_handler *handler,
452 struct boot_command *cmd)
454 struct boot_option *opt;
456 opt = find_boot_option_by_id(handler, cmd->option_id);
458 boot(handler, opt, cmd, 0);