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;
29 struct discover_context {
33 struct udev_event *event;
34 struct device *device;
38 struct list_item list;
46 static struct list contexts;
48 static struct boot_option options[] = {
52 .description = "meep description one",
53 .icon_file = "meep.one.png",
54 .boot_args = "root=/dev/sda1",
58 static struct device device = {
61 .description = "meep description",
62 .icon_file = "meep.png",
67 int device_handler_get_current_devices(struct device_handler *handler,
68 struct device **devices)
75 static int mkdir_recursive(const char *dir)
84 if (!stat(dir, &statbuf)) {
85 if (!S_ISDIR(statbuf.st_mode)) {
86 pb_log("%s: %s exists, but isn't a directory\n",
93 str = talloc_strdup(NULL, dir);
94 sep = strchr(*str == '/' ? str + 1 : str, '/');
98 /* terminate the path at sep */
102 if (mkdir(str, mode) && errno != EEXIST) {
103 pb_log("mkdir(%s): %s\n", str, strerror(errno));
110 /* reset dir to the full path */
112 sep = strchr(sep + 1, '/');
120 static int rmdir_recursive(const char *base, const char *dir)
124 /* sanity check: make sure that dir is within base */
125 if (strncmp(base, dir, strlen(base)))
128 cur = talloc_strdup(NULL, dir);
130 while (strcmp(base, dir)) {
134 /* null-terminate at the last slash */
135 pos = strrchr(dir, '/');
147 static void setup_device_links(struct discover_context *ctx)
154 .dir = "disk/by-uuid"
157 .env = "ID_FS_LABEL",
158 .dir = "disk/by-label"
165 for (link = links; link->env; link++) {
166 char *enc, *dir, *path;
169 value = udev_event_param(ctx->event, link->env);
170 if (!value || !*value)
173 enc = encode_label(ctx, value);
174 dir = join_paths(ctx, mount_base(), link->dir);
175 path = join_paths(ctx, dir, value);
177 if (!mkdir_recursive(dir)) {
179 if (symlink(ctx->mount_path, path)) {
180 pb_log("symlink(%s,%s): %s\n",
181 ctx->mount_path, path,
185 int i = ctx->n_links++;
186 ctx->links = talloc_realloc(ctx,
189 ctx->links[i] = path;
199 static void remove_device_links(struct discover_context *ctx)
203 for (i = 0; i < ctx->n_links; i++)
204 unlink(ctx->links[i]);
207 static int mount_device(struct discover_context *ctx)
209 const char *mountpoint;
213 if (!ctx->mount_path) {
214 mountpoint = mountpoint_for_device(ctx->device_path);
215 ctx->mount_path = talloc_strdup(ctx, mountpoint);
218 if (mkdir_recursive(ctx->mount_path))
219 pb_log("couldn't create mount directory %s: %s\n",
220 ctx->mount_path, strerror(errno));
224 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
229 execl(MOUNT_BIN, MOUNT_BIN, ctx->device_path, ctx->mount_path,
234 if (waitpid(pid, &status, 0) == -1) {
235 pb_log("%s: waitpid failed: %s\n", __func__,
240 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
243 setup_device_links(ctx);
247 rmdir_recursive(mount_base(), ctx->mount_path);
251 static int umount_device(struct discover_context *ctx)
256 remove_device_links(ctx);
260 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
265 execl(UMOUNT_BIN, UMOUNT_BIN, ctx->mount_path, NULL);
269 if (waitpid(pid, &status, 0) == -1) {
270 pb_log("%s: waitpid failed: %s\n", __func__,
275 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
278 rmdir_recursive(mount_base(), ctx->mount_path);
283 static struct discover_context *find_context(const char *id)
285 struct discover_context *ctx;
287 list_for_each_entry(&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(NULL, 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(&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(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;
380 handler = talloc(NULL, struct device_handler);
381 handler->devices = NULL;
382 handler->n_devices = 0;
384 list_init(&contexts);
386 /* set up our mount point base */
387 mkdir_recursive(mount_base());
392 void device_handler_destroy(struct device_handler *devices)
394 struct discover_context *ctx, *n;
396 talloc_free(devices);
398 list_for_each_entry_safe(&contexts, ctx, n, list)