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;
37 * device_handler_add - Add a device to the handler device array.
40 static void device_handler_add(struct device_handler *handler,
41 struct device *device)
44 handler->devices = talloc_realloc(handler, handler->devices,
45 struct device *, handler->n_devices);
46 handler->devices[handler->n_devices - 1] = device;
50 * device_handler_remove - Remove a device from the handler device array.
53 static void device_handler_remove(struct device_handler *handler,
54 struct device *device)
58 for (i = 0; i < handler->n_devices; i++)
59 if (handler->devices[i] == device)
62 if (i == handler->n_devices) {
63 assert(0 && "unknown device");
68 memmove(&handler->devices[i], &handler->devices[i + 1],
69 (handler->n_devices - i) * sizeof(handler->devices[0]));
70 handler->devices = talloc_realloc(handler, handler->devices,
71 struct device *, handler->n_devices);
75 * device_handler_find - Find a handler device by id.
78 static struct device *device_handler_find(struct device_handler *handler,
85 for (i = 0; i < handler->n_devices; i++)
86 if (handler->devices[i]->id
87 && streq(handler->devices[i]->id, id))
88 return handler->devices[i];
90 pb_log("%s: unknown device: %s\n", __func__, id);
95 * device_handler_get_device_count - Get the count of current handler devices.
98 int device_handler_get_device_count(const struct device_handler *handler)
100 return handler->n_devices;
104 * device_handler_get_device - Get a handler device by index.
107 const struct device *device_handler_get_device(
108 const struct device_handler *handler, unsigned int index)
110 if (index >= handler->n_devices) {
111 assert(0 && "bad index");
115 return handler->devices[index];
118 static void setup_device_links(struct discover_context *ctx)
125 .dir = "disk/by-uuid"
128 .env = "ID_FS_LABEL",
129 .dir = "disk/by-label"
136 for (link = links; link->env; link++) {
137 char *enc, *dir, *path;
140 value = event_get_param(ctx->event, link->env);
141 if (!value || !*value)
144 enc = encode_label(ctx, value);
145 dir = join_paths(ctx, mount_base(), link->dir);
146 path = join_paths(ctx, dir, value);
148 if (!pb_mkdir_recursive(dir)) {
150 if (symlink(ctx->mount_path, path)) {
151 pb_log("symlink(%s,%s): %s\n",
152 ctx->mount_path, path,
156 int i = ctx->n_links++;
157 ctx->links = talloc_realloc(ctx,
160 ctx->links[i] = path;
170 static void remove_device_links(struct discover_context *ctx)
174 for (i = 0; i < ctx->n_links; i++)
175 unlink(ctx->links[i]);
178 static int mount_device(struct discover_context *ctx)
180 const char *mountpoint;
183 if (!ctx->mount_path) {
184 mountpoint = mountpoint_for_device(ctx->device_path);
185 ctx->mount_path = talloc_strdup(ctx, mountpoint);
188 if (pb_mkdir_recursive(ctx->mount_path))
189 pb_log("couldn't create mount directory %s: %s\n",
190 ctx->mount_path, strerror(errno));
193 argv[1] = ctx->device_path;
194 argv[2] = ctx->mount_path;
199 if (pb_run_cmd(argv, 1, 0)) {
201 /* Retry mount without ro option. */
204 argv[1] = ctx->device_path;
205 argv[2] = ctx->mount_path;
208 if (pb_run_cmd(argv, 1, 0))
212 setup_device_links(ctx);
216 pb_rmdir_recursive(mount_base(), ctx->mount_path);
220 static int umount_device(struct discover_context *ctx)
225 remove_device_links(ctx);
229 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
234 execl(UMOUNT_BIN, UMOUNT_BIN, ctx->mount_path, NULL);
238 if (waitpid(pid, &status, 0) == -1) {
239 pb_log("%s: waitpid failed: %s\n", __func__,
244 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
247 pb_rmdir_recursive(mount_base(), ctx->mount_path);
252 static struct discover_context *find_context(struct device_handler *handler,
255 struct discover_context *ctx;
257 list_for_each_entry(&handler->contexts, ctx, list) {
258 if (!strcmp(ctx->id, id))
265 static int destroy_context(void *arg)
267 struct discover_context *ctx = arg;
269 list_remove(&ctx->list);
275 static int handle_add_udev_event(struct device_handler *handler,
278 struct discover_context *ctx;
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_add(handler->server, ctx->device);
320 static int handle_remove_udev_event(struct device_handler *handler,
323 struct discover_context *ctx;
325 ctx = find_context(handler, event->device);
329 discover_server_notify_remove(handler->server, ctx->device);
331 /* remove device from handler device array */
332 device_handler_remove(handler, ctx->device);
339 static int handle_add_user_event(struct device_handler *handler,
342 struct device *device;
344 assert(event->device);
346 device = talloc_zero(handler, struct device);
351 device->id = talloc_strdup(device, event->device);
352 list_init(&device->boot_options);
354 parse_user_event(device, event);
356 discover_server_notify_add(handler->server, device);
358 /* add device to handler device array */
359 device_handler_add(handler, device);
368 static int handle_remove_user_event(struct device_handler *handler,
371 struct device *device = device_handler_find(handler, event->device);
376 discover_server_notify_remove(handler->server, device);
378 /* remove device from handler device array */
379 device_handler_remove(handler, device);
385 typedef int (*event_handler)(struct device_handler *, struct event *);
387 static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
388 [EVENT_TYPE_UDEV] = {
389 [EVENT_ACTION_ADD] = handle_add_udev_event,
390 [EVENT_ACTION_REMOVE] = handle_remove_udev_event,
392 [EVENT_TYPE_USER] = {
393 [EVENT_ACTION_ADD] = handle_add_user_event,
394 [EVENT_ACTION_REMOVE] = handle_remove_user_event,
398 int device_handler_event(struct device_handler *handler,
401 if (event->type >= EVENT_TYPE_MAX ||
402 event->action >= EVENT_ACTION_MAX ||
403 !handlers[event->type][event->action]) {
404 pb_log("%s unknown type/action: %d/%d\n", __func__,
405 event->type, event->action);
409 return handlers[event->type][event->action](handler, event);
412 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;
421 list_init(&handler->contexts);
423 /* set up our mount point base */
424 pb_mkdir_recursive(mount_base());
431 void device_handler_destroy(struct device_handler *handler)
433 talloc_free(handler);