9 #include <talloc/talloc.h>
10 #include <list/list.h>
11 #include <pb-protocol/pb-protocol.h>
13 #include "device-handler.h"
14 #include "discover-server.h"
20 #define MOUNT_BIN "/bin/mount"
22 #define UMOUNT_BIN "/bin/umount"
24 struct device_handler {
25 struct discover_server *server;
27 struct device *devices;
39 static struct boot_option options[] = {
43 .description = "meep description one",
44 .icon_file = "meep.one.png",
45 .boot_args = "root=/dev/sda1",
49 static struct device device = {
52 .description = "meep description",
53 .icon_file = "meep.png",
56 int device_handler_get_current_devices(struct device_handler *handler,
57 struct device **devices)
64 static int mkdir_recursive(const char *dir)
73 if (!stat(dir, &statbuf)) {
74 if (!S_ISDIR(statbuf.st_mode)) {
75 pb_log("%s: %s exists, but isn't a directory\n",
82 str = talloc_strdup(NULL, dir);
83 sep = strchr(*str == '/' ? str + 1 : str, '/');
87 /* terminate the path at sep */
91 if (mkdir(str, mode) && errno != EEXIST) {
92 pb_log("mkdir(%s): %s\n", str, strerror(errno));
99 /* reset dir to the full path */
101 sep = strchr(sep + 1, '/');
109 static int rmdir_recursive(const char *base, const char *dir)
113 /* sanity check: make sure that dir is within base */
114 if (strncmp(base, dir, strlen(base)))
117 cur = talloc_strdup(NULL, dir);
119 while (strcmp(base, dir)) {
123 /* null-terminate at the last slash */
124 pos = strrchr(dir, '/');
136 static void setup_device_links(struct discover_context *ctx)
143 .dir = "disk/by-uuid"
146 .env = "ID_FS_LABEL",
147 .dir = "disk/by-label"
154 for (link = links; link->env; link++) {
155 char *enc, *dir, *path;
158 value = udev_event_param(ctx->event, link->env);
159 if (!value || !*value)
162 enc = encode_label(ctx, value);
163 dir = join_paths(ctx, mount_base(), link->dir);
164 path = join_paths(ctx, dir, value);
166 if (!mkdir_recursive(dir)) {
168 if (symlink(ctx->mount_path, path)) {
169 pb_log("symlink(%s,%s): %s\n",
170 ctx->mount_path, path,
174 int i = ctx->n_links++;
175 ctx->links = talloc_realloc(ctx,
178 ctx->links[i] = path;
188 static void remove_device_links(struct discover_context *ctx)
192 for (i = 0; i < ctx->n_links; i++)
193 unlink(ctx->links[i]);
196 static int mount_device(struct discover_context *ctx)
198 const char *mountpoint;
202 if (!ctx->mount_path) {
203 mountpoint = mountpoint_for_device(ctx->device_path);
204 ctx->mount_path = talloc_strdup(ctx, mountpoint);
207 if (mkdir_recursive(ctx->mount_path))
208 pb_log("couldn't create mount directory %s: %s\n",
209 ctx->mount_path, strerror(errno));
213 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
218 execl(MOUNT_BIN, MOUNT_BIN, ctx->device_path, ctx->mount_path,
223 if (waitpid(pid, &status, 0) == -1) {
224 pb_log("%s: waitpid failed: %s\n", __func__,
229 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
232 setup_device_links(ctx);
236 rmdir_recursive(mount_base(), ctx->mount_path);
240 static int umount_device(struct discover_context *ctx)
245 remove_device_links(ctx);
249 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
254 execl(UMOUNT_BIN, UMOUNT_BIN, ctx->mount_path, NULL);
258 if (waitpid(pid, &status, 0) == -1) {
259 pb_log("%s: waitpid failed: %s\n", __func__,
264 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
267 rmdir_recursive(mount_base(), ctx->mount_path);
272 static struct discover_context *find_context(struct device_handler *handler,
275 struct discover_context *ctx;
277 list_for_each_entry(&handler->contexts, ctx, list) {
278 if (!strcmp(ctx->id, id))
286 static int destroy_context(void *arg)
288 struct discover_context *ctx = arg;
290 list_remove(&ctx->list);
296 static int handle_add_event(struct device_handler *handler,
297 struct udev_event *event)
299 struct discover_context *ctx;
303 /* create our context */
304 ctx = talloc(handler, struct discover_context);
306 ctx->mount_path = NULL;
310 ctx->id = talloc_strdup(ctx, event->device);
312 devname = udev_event_param(ctx->event, "DEVNAME");
314 pb_log("no devname for %s?\n", event->device);
318 ctx->device_path = talloc_strdup(ctx, devname);
320 rc = mount_device(ctx);
322 pb_log("mount_device failed for %s\n", event->device);
327 list_add(&handler->contexts, &ctx->list);
328 talloc_set_destructor(ctx, destroy_context);
330 /* set up the top-level device */
331 ctx->device = talloc_zero(ctx, struct device);
332 ctx->device->id = talloc_strdup(ctx->device, ctx->id);
333 list_init(&ctx->device->boot_options);
335 /* run the parsers */
336 iterate_parsers(ctx);
338 discover_server_notify_add(handler->server, ctx->device);
343 static int handle_remove_event(struct device_handler *handler,
344 struct udev_event *event)
346 struct discover_context *ctx;
348 ctx = find_context(handler, event->device);
352 discover_server_notify_remove(handler->server, ctx->device);
359 int device_handler_event(struct device_handler *handler,
360 struct udev_event *event)
364 switch (event->action) {
365 case UDEV_ACTION_ADD:
366 rc = handle_add_event(handler, event);
369 case UDEV_ACTION_REMOVE:
370 rc = handle_remove_event(handler, event);
377 struct device_handler *device_handler_init(struct discover_server *server)
379 struct device_handler *handler;
382 handler = talloc(NULL, struct device_handler);
383 handler->devices = NULL;
384 handler->n_devices = 0;
385 handler->server = server;
387 list_init(&handler->contexts);
389 /* set up our mount point base */
390 mkdir_recursive(mount_base());
392 /* setup out test objects */
393 list_init(&device.boot_options);
395 for (i = 0; i < sizeof(options) / sizeof(options[0]); i++)
396 list_add(&device.boot_options, &options[i].list);
403 void device_handler_destroy(struct device_handler *handler)
405 talloc_free(handler);