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)) {
206 /* Retry mount without ro option. */
209 argv[1] = ctx->device_path;
210 argv[2] = ctx->mount_path;
213 if (pb_run_cmd(argv, 1))
217 setup_device_links(ctx);
221 pb_rmdir_recursive(mount_base(), ctx->mount_path);
225 static int umount_device(struct discover_context *ctx)
230 remove_device_links(ctx);
234 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
239 execl(UMOUNT_BIN, UMOUNT_BIN, ctx->mount_path, NULL);
243 if (waitpid(pid, &status, 0) == -1) {
244 pb_log("%s: waitpid failed: %s\n", __func__,
249 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
252 pb_rmdir_recursive(mount_base(), ctx->mount_path);
257 static struct discover_context *find_context(struct device_handler *handler,
260 struct discover_context *ctx;
262 list_for_each_entry(&handler->contexts, ctx, list) {
263 if (!strcmp(ctx->id, id))
270 static int destroy_context(void *arg)
272 struct discover_context *ctx = arg;
274 list_remove(&ctx->list);
280 static int handle_add_udev_event(struct device_handler *handler,
283 struct discover_context *ctx;
287 /* create our context */
288 ctx = talloc(handler, struct discover_context);
290 ctx->mount_path = NULL;
294 ctx->id = talloc_strdup(ctx, event->device);
296 devname = event_get_param(ctx->event, "DEVNAME");
298 pb_log("no devname for %s?\n", event->device);
302 ctx->device_path = talloc_strdup(ctx, devname);
304 rc = mount_device(ctx);
310 list_add(&handler->contexts, &ctx->list);
311 talloc_set_destructor(ctx, destroy_context);
313 /* set up the top-level device */
314 ctx->device = talloc_zero(ctx, struct device);
315 ctx->device->id = talloc_strdup(ctx->device, ctx->id);
316 list_init(&ctx->device->boot_options);
318 /* run the parsers */
319 iterate_parsers(ctx);
321 /* add device to handler device array */
322 device_handler_add(handler, ctx->device);
324 discover_server_notify_add(handler->server, ctx->device);
329 static int handle_remove_udev_event(struct device_handler *handler,
332 struct discover_context *ctx;
334 ctx = find_context(handler, event->device);
338 discover_server_notify_remove(handler->server, ctx->device);
340 /* remove device from handler device array */
341 device_handler_remove(handler, ctx->device);
348 static int handle_add_user_event(struct device_handler *handler,
351 struct device *device;
353 assert(event->device);
355 device = talloc_zero(handler, struct device);
360 device->id = talloc_strdup(device, event->device);
361 list_init(&device->boot_options);
363 parse_user_event(device, event);
365 discover_server_notify_add(handler->server, device);
367 /* add device to handler device array */
368 device_handler_add(handler, device);
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_remove(handler->server, device);
387 /* remove device from handler device array */
388 device_handler_remove(handler, device);
394 int device_handler_event(struct device_handler *handler,
399 switch (event->type) {
400 case EVENT_TYPE_UDEV:
401 switch (event->action) {
402 case EVENT_ACTION_ADD:
403 rc = handle_add_udev_event(handler, event);
405 case EVENT_ACTION_REMOVE:
406 rc = handle_remove_udev_event(handler, event);
409 pb_log("%s unknown action: %d\n", __func__,
414 case EVENT_TYPE_USER:
415 switch (event->action) {
416 case EVENT_ACTION_ADD:
417 rc = handle_add_user_event(handler, event);
419 case EVENT_ACTION_REMOVE:
420 rc = handle_remove_user_event(handler, event);
423 pb_log("%s unknown action: %d\n", __func__,
429 pb_log("%s unknown type: %d\n", __func__, event->type);
436 struct device_handler *device_handler_init(struct discover_server *server)
438 struct device_handler *handler;
440 handler = talloc(NULL, struct device_handler);
441 handler->devices = NULL;
442 handler->n_devices = 0;
443 handler->server = server;
445 list_init(&handler->contexts);
447 /* set up our mount point base */
448 pb_mkdir_recursive(mount_base());
455 void device_handler_destroy(struct device_handler *handler)
457 talloc_free(handler);