10 #include <talloc/talloc.h>
11 #include <list/list.h>
13 #include <pb-protocol/pb-protocol.h>
14 #include <system/system.h>
16 #include "device-handler.h"
17 #include "discover-server.h"
23 #define MOUNT_BIN "/bin/mount"
25 #define UMOUNT_BIN "/bin/umount"
27 struct device_handler {
28 struct discover_server *server;
30 struct device **devices;
31 unsigned int n_devices;
42 * device_handler_add - Add a device to the handler device array.
45 static void device_handler_add(struct device_handler *handler,
46 struct device *device)
49 handler->devices = talloc_realloc(handler, handler->devices,
50 struct device *, handler->n_devices);
51 handler->devices[handler->n_devices - 1] = device;
55 * device_handler_remove - Remove a device from the handler device array.
58 static void device_handler_remove(struct device_handler *handler,
59 struct device *device)
63 for (i = 0; i < handler->n_devices; i++)
64 if (handler->devices[i] == device)
67 if (i == handler->n_devices) {
68 assert(0 && "unknown device");
73 memmove(&handler->devices[i], &handler->devices[i + 1],
74 (handler->n_devices - i) * sizeof(handler->devices[0]));
75 handler->devices = talloc_realloc(handler, handler->devices,
76 struct device *, handler->n_devices);
80 * device_handler_find - Find a handler device by id.
83 static struct device *device_handler_find(struct device_handler *handler,
90 for (i = 0; i < handler->n_devices; i++)
91 if (handler->devices[i]->id
92 && streq(handler->devices[i]->id, id))
93 return handler->devices[i];
95 pb_log("%s: unknown device: %s\n", __func__, id);
100 * device_handler_get_device_count - Get the count of current handler devices.
103 int device_handler_get_device_count(const struct device_handler *handler)
105 return handler->n_devices;
109 * device_handler_get_device - Get a handler device by index.
112 const struct device *device_handler_get_device(
113 const struct device_handler *handler, unsigned int index)
115 if (index >= handler->n_devices) {
116 assert(0 && "bad index");
120 return handler->devices[index];
123 static void setup_device_links(struct discover_context *ctx)
130 .dir = "disk/by-uuid"
133 .env = "ID_FS_LABEL",
134 .dir = "disk/by-label"
141 for (link = links; link->env; link++) {
142 char *enc, *dir, *path;
145 value = event_get_param(ctx->event, link->env);
146 if (!value || !*value)
149 enc = encode_label(ctx, value);
150 dir = join_paths(ctx, mount_base(), link->dir);
151 path = join_paths(ctx, dir, value);
153 if (!pb_mkdir_recursive(dir)) {
155 if (symlink(ctx->mount_path, path)) {
156 pb_log("symlink(%s,%s): %s\n",
157 ctx->mount_path, path,
161 int i = ctx->n_links++;
162 ctx->links = talloc_realloc(ctx,
165 ctx->links[i] = path;
175 static void remove_device_links(struct discover_context *ctx)
179 for (i = 0; i < ctx->n_links; i++)
180 unlink(ctx->links[i]);
183 static int mount_device(struct discover_context *ctx)
185 const char *mountpoint;
188 if (!ctx->mount_path) {
189 mountpoint = mountpoint_for_device(ctx->device_path);
190 ctx->mount_path = talloc_strdup(ctx, mountpoint);
193 if (pb_mkdir_recursive(ctx->mount_path))
194 pb_log("couldn't create mount directory %s: %s\n",
195 ctx->mount_path, strerror(errno));
198 argv[1] = ctx->device_path;
199 argv[2] = ctx->mount_path;
204 if (pb_run_cmd(argv, 1))
205 argv[3] = NULL; /* try without ro */
207 if (pb_run_cmd(argv, 1))
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(UMOUNT_BIN, UMOUNT_BIN, 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 pb_log("no devname for %s?\n", event->device);
295 ctx->device_path = talloc_strdup(ctx, devname);
297 rc = mount_device(ctx);
303 list_add(&handler->contexts, &ctx->list);
304 talloc_set_destructor(ctx, destroy_context);
306 /* set up the top-level device */
307 ctx->device = talloc_zero(ctx, struct device);
308 ctx->device->id = talloc_strdup(ctx->device, ctx->id);
309 list_init(&ctx->device->boot_options);
311 /* run the parsers */
312 iterate_parsers(ctx);
314 /* add device to handler device array */
315 device_handler_add(handler, ctx->device);
317 discover_server_notify_add(handler->server, ctx->device);
322 static int handle_remove_udev_event(struct device_handler *handler,
325 struct discover_context *ctx;
327 ctx = find_context(handler, event->device);
331 discover_server_notify_remove(handler->server, ctx->device);
333 /* remove device from handler device array */
334 device_handler_remove(handler, ctx->device);
341 static int handle_add_user_event(struct device_handler *handler,
344 struct device *device;
346 assert(event->device);
348 device = talloc_zero(handler, struct device);
353 device->id = talloc_strdup(device, event->device);
354 list_init(&device->boot_options);
356 parse_user_event(device, event);
358 discover_server_notify_add(handler->server, device);
360 /* add device to handler device array */
361 device_handler_add(handler, device);
370 static int handle_remove_user_event(struct device_handler *handler,
373 struct device *device = device_handler_find(handler, event->device);
378 discover_server_notify_remove(handler->server, device);
380 /* remove device from handler device array */
381 device_handler_remove(handler, device);
387 int device_handler_event(struct device_handler *handler,
392 switch (event->type) {
393 case EVENT_TYPE_UDEV:
394 switch (event->action) {
395 case EVENT_ACTION_ADD:
396 rc = handle_add_udev_event(handler, event);
398 case EVENT_ACTION_REMOVE:
399 rc = handle_remove_udev_event(handler, event);
402 pb_log("%s unknown action: %d\n", __func__,
407 case EVENT_TYPE_USER:
408 switch (event->action) {
409 case EVENT_ACTION_ADD:
410 rc = handle_add_user_event(handler, event);
412 case EVENT_ACTION_REMOVE:
413 rc = handle_remove_user_event(handler, event);
416 pb_log("%s unknown action: %d\n", __func__,
422 pb_log("%s unknown type: %d\n", __func__, event->type);
429 struct device_handler *device_handler_init(struct discover_server *server)
431 struct device_handler *handler;
433 handler = talloc(NULL, struct device_handler);
434 handler->devices = NULL;
435 handler->n_devices = 0;
436 handler->server = server;
438 list_init(&handler->contexts);
440 /* set up our mount point base */
441 pb_mkdir_recursive(mount_base());
448 void device_handler_destroy(struct device_handler *handler)
450 talloc_free(handler);