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)
233 setup_device_links(ctx);
237 rmdir_recursive(mount_base(), ctx->mount_path);
241 static int umount_device(struct discover_context *ctx)
246 remove_device_links(ctx);
250 pb_log("%s: fork failed: %s\n", __func__, strerror(errno));
255 execl(UMOUNT_BIN, UMOUNT_BIN, ctx->mount_path, NULL);
259 if (waitpid(pid, &status, 0) == -1) {
260 pb_log("%s: waitpid failed: %s\n", __func__,
265 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
268 rmdir_recursive(mount_base(), ctx->mount_path);
273 static struct discover_context *find_context(struct device_handler *handler,
276 struct discover_context *ctx;
278 list_for_each_entry(&handler->contexts, ctx, list) {
279 if (!strcmp(ctx->id, id))
287 static int destroy_context(void *arg)
289 struct discover_context *ctx = arg;
291 list_remove(&ctx->list);
297 static int handle_add_event(struct device_handler *handler,
298 struct udev_event *event)
300 struct discover_context *ctx;
304 /* create our context */
305 ctx = talloc(handler, struct discover_context);
307 ctx->mount_path = NULL;
311 ctx->id = talloc_strdup(ctx, event->device);
313 devname = udev_event_param(ctx->event, "DEVNAME");
315 pb_log("no devname for %s?\n", event->device);
319 ctx->device_path = talloc_strdup(ctx, devname);
321 rc = mount_device(ctx);
323 pb_log("mount_device failed for %s\n", event->device);
328 list_add(&handler->contexts, &ctx->list);
329 talloc_set_destructor(ctx, destroy_context);
331 /* set up the top-level device */
332 ctx->device = talloc_zero(ctx, struct device);
333 ctx->device->id = talloc_strdup(ctx->device, ctx->id);
334 list_init(&ctx->device->boot_options);
336 /* run the parsers */
337 iterate_parsers(ctx);
339 discover_server_notify_add(handler->server, ctx->device);
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);
353 discover_server_notify_remove(handler->server, ctx->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;
383 handler = talloc(NULL, struct device_handler);
384 handler->devices = NULL;
385 handler->n_devices = 0;
386 handler->server = server;
388 list_init(&handler->contexts);
390 /* set up our mount point base */
391 mkdir_recursive(mount_base());
393 /* setup out test objects */
394 list_init(&device.boot_options);
396 for (i = 0; i < sizeof(options) / sizeof(options[0]); i++)
397 list_add(&device.boot_options, &options[i].list);
404 void device_handler_destroy(struct device_handler *handler)
406 talloc_free(handler);