9 #include <talloc/talloc.h>
10 #include <list/list.h>
11 #include <pb-protocol/pb-protocol.h>
13 #include "device-handler.h"
18 #define MOUNT_BIN "/bin/mount"
20 #define UMOUNT_BIN "/bin/umount"
22 struct device_handler {
23 struct discover_server *server;
25 struct device *devices;
31 struct discover_context {
35 struct udev_event *event;
36 struct device *device;
40 struct list_item list;
49 static struct boot_option options[] = {
53 .description = "meep description one",
54 .icon_file = "meep.one.png",
55 .boot_args = "root=/dev/sda1",
59 static struct device device = {
62 .description = "meep description",
63 .icon_file = "meep.png",
66 int device_handler_get_current_devices(struct device_handler *handler,
67 struct device **devices)
74 static int mkdir_recursive(const char *dir)
83 if (!stat(dir, &statbuf)) {
84 if (!S_ISDIR(statbuf.st_mode)) {
85 pb_log("%s: %s exists, but isn't a directory\n",
92 str = talloc_strdup(NULL, dir);
93 sep = strchr(*str == '/' ? str + 1 : str, '/');
97 /* terminate the path at sep */
101 if (mkdir(str, mode) && errno != EEXIST) {
102 pb_log("mkdir(%s): %s\n", str, strerror(errno));
109 /* reset dir to the full path */
111 sep = strchr(sep + 1, '/');
119 static int rmdir_recursive(const char *base, const char *dir)
123 /* sanity check: make sure that dir is within base */
124 if (strncmp(base, dir, strlen(base)))
127 cur = talloc_strdup(NULL, dir);
129 while (strcmp(base, dir)) {
133 /* null-terminate at the last slash */
134 pos = strrchr(dir, '/');
146 static void setup_device_links(struct discover_context *ctx)
153 .dir = "disk/by-uuid"
156 .env = "ID_FS_LABEL",
157 .dir = "disk/by-label"
164 for (link = links; link->env; link++) {
165 char *enc, *dir, *path;
168 value = udev_event_param(ctx->event, link->env);
169 if (!value || !*value)
172 enc = encode_label(ctx, value);
173 dir = join_paths(ctx, mount_base(), link->dir);
174 path = join_paths(ctx, dir, value);
176 if (!mkdir_recursive(dir)) {
178 if (symlink(ctx->mount_path, path)) {
179 pb_log("symlink(%s,%s): %s\n",
180 ctx->mount_path, path,
184 int i = ctx->n_links++;
185 ctx->links = talloc_realloc(ctx,
188 ctx->links[i] = path;
198 static void remove_device_links(struct discover_context *ctx)
202 for (i = 0; i < ctx->n_links; i++)
203 unlink(ctx->links[i]);
206 static int mount_device(struct discover_context *ctx)
208 const char *mountpoint;
212 if (!ctx->mount_path) {
213 mountpoint = mountpoint_for_device(ctx->device_path);
214 ctx->mount_path = talloc_strdup(ctx, mountpoint);
217 if (mkdir_recursive(ctx->mount_path))
218 pb_log("couldn't create mount directory %s: %s\n",
219 ctx->mount_path, strerror(errno));
223 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
228 execl(MOUNT_BIN, MOUNT_BIN, ctx->device_path, ctx->mount_path,
233 if (waitpid(pid, &status, 0) == -1) {
234 pb_log("%s: waitpid failed: %s\n", __func__,
239 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
242 setup_device_links(ctx);
246 rmdir_recursive(mount_base(), ctx->mount_path);
250 static int umount_device(struct discover_context *ctx)
255 remove_device_links(ctx);
259 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
264 execl(UMOUNT_BIN, UMOUNT_BIN, ctx->mount_path, NULL);
268 if (waitpid(pid, &status, 0) == -1) {
269 pb_log("%s: waitpid failed: %s\n", __func__,
274 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
277 rmdir_recursive(mount_base(), ctx->mount_path);
282 static struct discover_context *find_context(struct device_handler *handler,
285 struct discover_context *ctx;
287 list_for_each_entry(&handler->contexts, ctx, list) {
288 if (!strcmp(ctx->id, id))
296 static int destroy_context(void *arg)
298 struct discover_context *ctx = arg;
300 list_remove(&ctx->list);
306 static int handle_add_event(struct device_handler *handler,
307 struct udev_event *event)
309 struct discover_context *ctx;
313 /* create our context */
314 ctx = talloc(handler, struct discover_context);
316 ctx->mount_path = NULL;
320 ctx->id = talloc_strdup(ctx, event->device);
322 devname = udev_event_param(ctx->event, "DEVNAME");
324 pb_log("no devname for %s?\n", event->device);
328 ctx->device_path = talloc_strdup(ctx, devname);
330 rc = mount_device(ctx);
332 pb_log("mount_device failed for %s\n", event->device);
337 list_add(&handler->contexts, &ctx->list);
339 talloc_set_destructor(ctx, destroy_context);
344 static int handle_remove_event(struct device_handler *handler,
345 struct udev_event *event)
347 struct discover_context *ctx;
349 ctx = find_context(handler, event->device);
358 int device_handler_event(struct device_handler *handler,
359 struct udev_event *event)
363 switch (event->action) {
364 case UDEV_ACTION_ADD:
365 rc = handle_add_event(handler, event);
368 case UDEV_ACTION_REMOVE:
369 rc = handle_remove_event(handler, event);
376 struct device_handler *device_handler_init(struct discover_server *server)
378 struct device_handler *handler;
381 handler = talloc(NULL, struct device_handler);
382 handler->devices = NULL;
383 handler->n_devices = 0;
385 list_init(&handler->contexts);
387 /* set up our mount point base */
388 mkdir_recursive(mount_base());
390 /* setup out test objects */
391 list_init(&device.boot_options);
393 for (i = 0; i < sizeof(options) / sizeof(options[0]); i++)
394 list_add(&device.boot_options, &options[i].list);
399 void device_handler_destroy(struct device_handler *handler)
401 talloc_free(handler);