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",
68 int device_handler_get_current_devices(struct device_handler *handler,
69 struct device **devices)
76 static int mkdir_recursive(const char *dir)
85 if (!stat(dir, &statbuf)) {
86 if (!S_ISDIR(statbuf.st_mode)) {
87 pb_log("%s: %s exists, but isn't a directory\n",
94 str = talloc_strdup(NULL, dir);
95 sep = strchr(*str == '/' ? str + 1 : str, '/');
99 /* terminate the path at sep */
103 if (mkdir(str, mode) && errno != EEXIST) {
104 pb_log("mkdir(%s): %s\n", str, strerror(errno));
111 /* reset dir to the full path */
113 sep = strchr(sep + 1, '/');
121 static int rmdir_recursive(const char *base, const char *dir)
125 /* sanity check: make sure that dir is within base */
126 if (strncmp(base, dir, strlen(base)))
129 cur = talloc_strdup(NULL, dir);
131 while (strcmp(base, dir)) {
135 /* null-terminate at the last slash */
136 pos = strrchr(dir, '/');
148 static void setup_device_links(struct discover_context *ctx)
155 .dir = "disk/by-uuid"
158 .env = "ID_FS_LABEL",
159 .dir = "disk/by-label"
166 for (link = links; link->env; link++) {
167 char *enc, *dir, *path;
170 value = udev_event_param(ctx->event, link->env);
171 if (!value || !*value)
174 enc = encode_label(ctx, value);
175 dir = join_paths(ctx, mount_base(), link->dir);
176 path = join_paths(ctx, dir, value);
178 if (!mkdir_recursive(dir)) {
180 if (symlink(ctx->mount_path, path)) {
181 pb_log("symlink(%s,%s): %s\n",
182 ctx->mount_path, path,
186 int i = ctx->n_links++;
187 ctx->links = talloc_realloc(ctx,
190 ctx->links[i] = path;
200 static void remove_device_links(struct discover_context *ctx)
204 for (i = 0; i < ctx->n_links; i++)
205 unlink(ctx->links[i]);
208 static int mount_device(struct discover_context *ctx)
210 const char *mountpoint;
214 if (!ctx->mount_path) {
215 mountpoint = mountpoint_for_device(ctx->device_path);
216 ctx->mount_path = talloc_strdup(ctx, mountpoint);
219 if (mkdir_recursive(ctx->mount_path))
220 pb_log("couldn't create mount directory %s: %s\n",
221 ctx->mount_path, strerror(errno));
225 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
230 execl(MOUNT_BIN, MOUNT_BIN, ctx->device_path, ctx->mount_path,
235 if (waitpid(pid, &status, 0) == -1) {
236 pb_log("%s: waitpid failed: %s\n", __func__,
241 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
244 setup_device_links(ctx);
248 rmdir_recursive(mount_base(), ctx->mount_path);
252 static int umount_device(struct discover_context *ctx)
257 remove_device_links(ctx);
261 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
266 execl(UMOUNT_BIN, UMOUNT_BIN, ctx->mount_path, NULL);
270 if (waitpid(pid, &status, 0) == -1) {
271 pb_log("%s: waitpid failed: %s\n", __func__,
276 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
279 rmdir_recursive(mount_base(), ctx->mount_path);
284 static struct discover_context *find_context(struct device_handler *handler,
287 struct discover_context *ctx;
289 list_for_each_entry(&handler->contexts, ctx, list) {
290 if (!strcmp(ctx->id, id))
298 static int destroy_context(void *arg)
300 struct discover_context *ctx = arg;
302 list_remove(&ctx->list);
308 static int handle_add_event(struct device_handler *handler,
309 struct udev_event *event)
311 struct discover_context *ctx;
315 /* create our context */
316 ctx = talloc(handler, struct discover_context);
318 ctx->mount_path = NULL;
322 ctx->id = talloc_strdup(ctx, event->device);
324 devname = udev_event_param(ctx->event, "DEVNAME");
326 pb_log("no devname for %s?\n", event->device);
330 ctx->device_path = talloc_strdup(ctx, devname);
332 rc = mount_device(ctx);
334 pb_log("mount_device failed for %s\n", event->device);
339 list_add(&handler->contexts, &ctx->list);
341 talloc_set_destructor(ctx, destroy_context);
346 static int handle_remove_event(struct device_handler *handler,
347 struct udev_event *event)
349 struct discover_context *ctx;
351 ctx = find_context(handler, event->device);
360 int device_handler_event(struct device_handler *handler,
361 struct udev_event *event)
365 switch (event->action) {
366 case UDEV_ACTION_ADD:
367 rc = handle_add_event(handler, event);
370 case UDEV_ACTION_REMOVE:
371 rc = handle_remove_event(handler, event);
378 struct device_handler *device_handler_init(struct discover_server *server)
380 struct device_handler *handler;
382 handler = talloc(NULL, struct device_handler);
383 handler->devices = NULL;
384 handler->n_devices = 0;
386 list_init(&handler->contexts);
388 /* set up our mount point base */
389 mkdir_recursive(mount_base());
394 void device_handler_destroy(struct device_handler *handler)
396 talloc_free(handler);