9 #include <talloc/talloc.h>
10 #include <list/list.h>
12 #include <pb-protocol/pb-protocol.h>
14 #include "device-handler.h"
15 #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(
57 struct device_handler *handler __attribute__((unused)),
58 const struct device **devices)
65 static int mkdir_recursive(const char *dir)
74 if (!stat(dir, &statbuf)) {
75 if (!S_ISDIR(statbuf.st_mode)) {
76 pb_log("%s: %s exists, but isn't a directory\n",
83 str = talloc_strdup(NULL, dir);
84 sep = strchr(*str == '/' ? str + 1 : str, '/');
88 /* terminate the path at sep */
92 if (mkdir(str, mode) && errno != EEXIST) {
93 pb_log("mkdir(%s): %s\n", str, strerror(errno));
100 /* reset dir to the full path */
102 sep = strchr(sep + 1, '/');
110 static int rmdir_recursive(const char *base, const char *dir)
114 /* sanity check: make sure that dir is within base */
115 if (strncmp(base, dir, strlen(base)))
118 cur = talloc_strdup(NULL, dir);
120 while (strcmp(base, dir)) {
124 /* null-terminate at the last slash */
125 pos = strrchr(dir, '/');
137 static void setup_device_links(struct discover_context *ctx)
144 .dir = "disk/by-uuid"
147 .env = "ID_FS_LABEL",
148 .dir = "disk/by-label"
155 for (link = links; link->env; link++) {
156 char *enc, *dir, *path;
159 value = udev_event_param(ctx->event, link->env);
160 if (!value || !*value)
163 enc = encode_label(ctx, value);
164 dir = join_paths(ctx, mount_base(), link->dir);
165 path = join_paths(ctx, dir, value);
167 if (!mkdir_recursive(dir)) {
169 if (symlink(ctx->mount_path, path)) {
170 pb_log("symlink(%s,%s): %s\n",
171 ctx->mount_path, path,
175 int i = ctx->n_links++;
176 ctx->links = talloc_realloc(ctx,
179 ctx->links[i] = path;
189 static void remove_device_links(struct discover_context *ctx)
193 for (i = 0; i < ctx->n_links; i++)
194 unlink(ctx->links[i]);
197 static int mount_device(struct discover_context *ctx)
199 const char *mountpoint;
203 if (!ctx->mount_path) {
204 mountpoint = mountpoint_for_device(ctx->device_path);
205 ctx->mount_path = talloc_strdup(ctx, mountpoint);
208 if (mkdir_recursive(ctx->mount_path))
209 pb_log("couldn't create mount directory %s: %s\n",
210 ctx->mount_path, strerror(errno));
214 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
219 execl(MOUNT_BIN, MOUNT_BIN, ctx->device_path, ctx->mount_path,
224 if (waitpid(pid, &status, 0) == -1) {
225 pb_log("%s: waitpid failed: %s\n", __func__,
230 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
231 pb_log("%s: mount failed (%d): %s\n", __func__,
232 WEXITSTATUS(status), ctx->event->device);
236 setup_device_links(ctx);
240 rmdir_recursive(mount_base(), ctx->mount_path);
244 static int umount_device(struct discover_context *ctx)
249 remove_device_links(ctx);
253 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
258 execl(UMOUNT_BIN, UMOUNT_BIN, ctx->mount_path, NULL);
262 if (waitpid(pid, &status, 0) == -1) {
263 pb_log("%s: waitpid failed: %s\n", __func__,
268 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
271 rmdir_recursive(mount_base(), ctx->mount_path);
276 static struct discover_context *find_context(struct device_handler *handler,
279 struct discover_context *ctx;
281 list_for_each_entry(&handler->contexts, ctx, list) {
282 if (!strcmp(ctx->id, id))
290 static int destroy_context(void *arg)
292 struct discover_context *ctx = arg;
294 list_remove(&ctx->list);
300 static int handle_add_event(struct device_handler *handler,
301 struct udev_event *event)
303 struct discover_context *ctx;
307 /* create our context */
308 ctx = talloc(handler, struct discover_context);
310 ctx->mount_path = NULL;
314 ctx->id = talloc_strdup(ctx, event->device);
316 devname = udev_event_param(ctx->event, "DEVNAME");
318 pb_log("no devname for %s?\n", event->device);
322 ctx->device_path = talloc_strdup(ctx, devname);
324 rc = mount_device(ctx);
330 list_add(&handler->contexts, &ctx->list);
331 talloc_set_destructor(ctx, destroy_context);
333 /* set up the top-level device */
334 ctx->device = talloc_zero(ctx, struct device);
335 ctx->device->id = talloc_strdup(ctx->device, ctx->id);
336 list_init(&ctx->device->boot_options);
338 /* run the parsers */
339 iterate_parsers(ctx);
341 discover_server_notify_add(handler->server, ctx->device);
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);
355 discover_server_notify_remove(handler->server, ctx->device);
362 int device_handler_event(struct device_handler *handler,
363 struct udev_event *event)
367 switch (event->action) {
368 case UDEV_ACTION_ADD:
369 rc = handle_add_event(handler, event);
372 case UDEV_ACTION_REMOVE:
373 rc = handle_remove_event(handler, event);
380 struct device_handler *device_handler_init(struct discover_server *server)
382 struct device_handler *handler;
385 handler = talloc(NULL, struct device_handler);
386 handler->devices = NULL;
387 handler->n_devices = 0;
388 handler->server = server;
390 list_init(&handler->contexts);
392 /* set up our mount point base */
393 mkdir_recursive(mount_base());
395 /* setup out test objects */
396 list_init(&device.boot_options);
398 for (i = 0; i < sizeof(options) / sizeof(options[0]); i++)
399 list_add(&device.boot_options, &options[i].list);
406 void device_handler_destroy(struct device_handler *handler)
408 talloc_free(handler);