X-Git-Url: http://git.ozlabs.org/?p=petitboot;a=blobdiff_plain;f=discover%2Fdevice-handler.c;h=1f8938f295b322e68b2df19fb7eeadd8146b1ca1;hp=5d40ebe99642711e6f17d9f4b2d113e94f7bfc4c;hb=b118597194815910897ccf86d77b8b5a066adf5a;hpb=5eb7f7bcd3431cd8f634b02b71bd78f6162c2af3;ds=sidebyside diff --git a/discover/device-handler.c b/discover/device-handler.c index 5d40ebe..1f8938f 100644 --- a/discover/device-handler.c +++ b/discover/device-handler.c @@ -7,6 +7,7 @@ #include #include +#include #include #include "device-handler.h" @@ -16,20 +17,27 @@ #define MOUNT_BIN "/bin/mount" +#define UMOUNT_BIN "/bin/umount" + struct device_handler { struct discover_server *server; struct device *devices; int n_devices; + + struct list contexts; }; struct discover_context { + char *id; char *device_path; char *mount_path; struct udev_event *event; struct device *device; char **links; int n_links; + + struct list_item list; }; struct mount_map { @@ -53,8 +61,6 @@ static struct device device = { .name = "meep", .description = "meep description", .icon_file = "meep.png", - .n_options = 1, - .options = options, }; int device_handler_get_current_devices(struct device_handler *handler, @@ -110,6 +116,33 @@ static int mkdir_recursive(const char *dir) return 0; } +static int rmdir_recursive(const char *base, const char *dir) +{ + char *cur, *pos; + + /* sanity check: make sure that dir is within base */ + if (strncmp(base, dir, strlen(base))) + return -1; + + cur = talloc_strdup(NULL, dir); + + while (strcmp(base, dir)) { + + rmdir(dir); + + /* null-terminate at the last slash */ + pos = strrchr(dir, '/'); + if (!pos) + break; + + *pos = '\0'; + } + + talloc_free(cur); + + return 0; +} + static void setup_device_links(struct discover_context *ctx) { struct link { @@ -133,7 +166,7 @@ static void setup_device_links(struct discover_context *ctx) const char *value; value = udev_event_param(ctx->event, link->env); - if (!value) + if (!value || !*value) continue; enc = encode_label(ctx, value); @@ -162,10 +195,17 @@ static void setup_device_links(struct discover_context *ctx) } } +static void remove_device_links(struct discover_context *ctx) +{ + int i; + + for (i = 0; i < ctx->n_links; i++) + unlink(ctx->links[i]); +} + static int mount_device(struct discover_context *ctx) { const char *mountpoint; - struct stat statbuf; int status; pid_t pid; @@ -174,24 +214,14 @@ static int mount_device(struct discover_context *ctx) ctx->mount_path = talloc_strdup(ctx, mountpoint); } - if (stat(ctx->mount_path, &statbuf)) { - if (mkdir(ctx->mount_path, 0755)) { - pb_log("couldn't create mount directory %s: %s\n", - ctx->mount_path, strerror(errno)); - return -1; - } - } else { - if (!S_ISDIR(statbuf.st_mode)) { - pb_log("mountpoint %s exists, but isn't a directory\n", - ctx->mount_path); - return -1; - } - } + if (mkdir_recursive(ctx->mount_path)) + pb_log("couldn't create mount directory %s: %s\n", + ctx->mount_path, strerror(errno)); pid = fork(); if (pid == -1) { pb_log("%s: fork failed: %s\n", __func__, strerror(errno)); - return -1; + goto out_rmdir; } if (pid == 0) { @@ -200,6 +230,41 @@ static int mount_device(struct discover_context *ctx) exit(EXIT_FAILURE); } + if (waitpid(pid, &status, 0) == -1) { + pb_log("%s: waitpid failed: %s\n", __func__, + strerror(errno)); + goto out_rmdir; + } + + if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) + goto out_rmdir; + + setup_device_links(ctx); + return 0; + +out_rmdir: + rmdir_recursive(mount_base(), ctx->mount_path); + return -1; +} + +static int umount_device(struct discover_context *ctx) +{ + int status; + pid_t pid; + + remove_device_links(ctx); + + pid = fork(); + if (pid == -1) { + pb_log("%s: fork failed: %s\n", __func__, strerror(errno)); + return -1; + } + + if (pid == 0) { + execl(UMOUNT_BIN, UMOUNT_BIN, ctx->mount_path, NULL); + exit(EXIT_FAILURE); + } + if (waitpid(pid, &status, 0) == -1) { pb_log("%s: waitpid failed: %s\n", __func__, strerror(errno)); @@ -209,7 +274,32 @@ static int mount_device(struct discover_context *ctx) if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) return -1; - setup_device_links(ctx); + rmdir_recursive(mount_base(), ctx->mount_path); + + return 0; +} + +static struct discover_context *find_context(struct device_handler *handler, + const char *id) +{ + struct discover_context *ctx; + + list_for_each_entry(&handler->contexts, ctx, list) { + if (!strcmp(ctx->id, id)) + return ctx; + } + + return NULL; +} + + +static int destroy_context(void *arg) +{ + struct discover_context *ctx = arg; + + list_remove(&ctx->list); + umount_device(ctx); + return 0; } @@ -221,12 +311,14 @@ static int handle_add_event(struct device_handler *handler, int rc; /* create our context */ - ctx = talloc(NULL, struct discover_context); + ctx = talloc(handler, struct discover_context); ctx->event = event; ctx->mount_path = NULL; ctx->links = NULL; ctx->n_links = 0; + ctx->id = talloc_strdup(ctx, event->device); + devname = udev_event_param(ctx->event, "DEVNAME"); if (!devname) { pb_log("no devname for %s?\n", event->device); @@ -242,7 +334,9 @@ static int handle_add_event(struct device_handler *handler, return 0; } - talloc_free(ctx); + list_add(&handler->contexts, &ctx->list); + + talloc_set_destructor(ctx, destroy_context); return 0; } @@ -250,6 +344,14 @@ static int handle_add_event(struct device_handler *handler, static int handle_remove_event(struct device_handler *handler, struct udev_event *event) { + struct discover_context *ctx; + + ctx = find_context(handler, event->device); + if (!ctx) + return 0; + + talloc_free(ctx); + return 0; } @@ -274,19 +376,28 @@ int device_handler_event(struct device_handler *handler, struct device_handler *device_handler_init(struct discover_server *server) { struct device_handler *handler; + int i; handler = talloc(NULL, struct device_handler); handler->devices = NULL; handler->n_devices = 0; + list_init(&handler->contexts); + /* set up our mount point base */ mkdir_recursive(mount_base()); + /* setup out test objects */ + list_init(&device.boot_options); + + for (i = 0; i < sizeof(options) / sizeof(options[0]); i++) + list_add(&device.boot_options, &options[i].list); + return handler; } -void device_handler_destroy(struct device_handler *devices) +void device_handler_destroy(struct device_handler *handler) { - talloc_free(devices); + talloc_free(handler); }