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"
22 #define MOUNT_BIN "/bin/mount"
24 #define UMOUNT_BIN "/bin/umount"
26 struct device_handler {
27 struct discover_server *server;
29 struct device **devices;
30 unsigned int n_devices;
41 * device_handler_add - Add a device to the handler device array.
44 static void device_handler_add(struct device_handler *handler,
45 struct device *device)
48 handler->devices = talloc_realloc(handler, handler->devices,
49 struct device *, handler->n_devices);
50 handler->devices[handler->n_devices - 1] = device;
54 * device_handler_remove - Remove a device from the handler device array.
57 static void device_handler_remove(struct device_handler *handler,
58 struct device *device)
62 for (i = 0; i < handler->n_devices; i++)
63 if (handler->devices[i] == device)
66 if (i < handler->n_devices) {
67 assert(0 && "unknown device");
72 memmove(&handler->devices[i], &handler->devices[i + 1],
73 (handler->n_devices - i) * sizeof(handler->devices[0]));
74 handler->devices = talloc_realloc(handler, handler->devices,
75 struct device *, handler->n_devices);
79 * device_handler_find - Find a handler device by id.
82 static struct device *device_handler_find(struct device_handler *handler,
89 for (i = 0; i < handler->n_devices; i++)
90 if (handler->devices[i]->id
91 && streq(handler->devices[i]->id, id))
92 return handler->devices[i];
94 assert(0 && "unknown device");
99 * device_handler_get_device_count - Get the count of current handler devices.
102 int device_handler_get_device_count(const struct device_handler *handler)
104 return handler->n_devices;
108 * device_handler_get_device - Get a handler device by index.
111 const struct device *device_handler_get_device(
112 const struct device_handler *handler, unsigned int index)
114 if (index >= handler->n_devices) {
115 assert(0 && "bad index");
119 return handler->devices[index];
122 static void setup_device_links(struct discover_context *ctx)
129 .dir = "disk/by-uuid"
132 .env = "ID_FS_LABEL",
133 .dir = "disk/by-label"
140 for (link = links; link->env; link++) {
141 char *enc, *dir, *path;
144 value = udev_event_param(ctx->event, link->env);
145 if (!value || !*value)
148 enc = encode_label(ctx, value);
149 dir = join_paths(ctx, mount_base(), link->dir);
150 path = join_paths(ctx, dir, value);
152 if (!pb_mkdir_recursive(dir)) {
154 if (symlink(ctx->mount_path, path)) {
155 pb_log("symlink(%s,%s): %s\n",
156 ctx->mount_path, path,
160 int i = ctx->n_links++;
161 ctx->links = talloc_realloc(ctx,
164 ctx->links[i] = path;
174 static void remove_device_links(struct discover_context *ctx)
178 for (i = 0; i < ctx->n_links; i++)
179 unlink(ctx->links[i]);
182 static int mount_device(struct discover_context *ctx)
184 const char *mountpoint;
187 if (!ctx->mount_path) {
188 mountpoint = mountpoint_for_device(ctx->device_path);
189 ctx->mount_path = talloc_strdup(ctx, mountpoint);
192 if (pb_mkdir_recursive(ctx->mount_path))
193 pb_log("couldn't create mount directory %s: %s\n",
194 ctx->mount_path, strerror(errno));
197 argv[1] = ctx->device_path;
198 argv[2] = ctx->mount_path;
203 if (pb_run_cmd(argv))
206 setup_device_links(ctx);
210 pb_rmdir_recursive(mount_base(), ctx->mount_path);
214 static int umount_device(struct discover_context *ctx)
219 remove_device_links(ctx);
223 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
228 execl(UMOUNT_BIN, UMOUNT_BIN, ctx->mount_path, NULL);
232 if (waitpid(pid, &status, 0) == -1) {
233 pb_log("%s: waitpid failed: %s\n", __func__,
238 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
241 pb_rmdir_recursive(mount_base(), ctx->mount_path);
246 static struct discover_context *find_context(struct device_handler *handler,
249 struct discover_context *ctx;
251 list_for_each_entry(&handler->contexts, ctx, list) {
252 if (!strcmp(ctx->id, id))
259 static int destroy_context(void *arg)
261 struct discover_context *ctx = arg;
263 list_remove(&ctx->list);
269 static int handle_add_event(struct device_handler *handler,
270 struct udev_event *event)
272 struct discover_context *ctx;
276 /* create our context */
277 ctx = talloc(handler, struct discover_context);
279 ctx->mount_path = NULL;
283 ctx->id = talloc_strdup(ctx, event->device);
285 devname = udev_event_param(ctx->event, "DEVNAME");
287 pb_log("no devname for %s?\n", event->device);
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_event(struct device_handler *handler,
319 struct udev_event *event)
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 int device_handler_event(struct device_handler *handler,
338 struct udev_event *event)
342 switch (event->action) {
343 case UDEV_ACTION_ADD:
344 rc = handle_add_event(handler, event);
347 case UDEV_ACTION_REMOVE:
348 rc = handle_remove_event(handler, event);
355 struct device_handler *device_handler_init(struct discover_server *server)
357 struct device_handler *handler;
359 handler = talloc(NULL, struct device_handler);
360 handler->devices = NULL;
361 handler->n_devices = 0;
362 handler->server = server;
364 list_init(&handler->contexts);
366 /* set up our mount point base */
367 pb_mkdir_recursive(mount_base());
374 void device_handler_destroy(struct device_handler *handler)
376 talloc_free(handler);