]> git.ozlabs.org Git - petitboot/blobdiff - discover/device-handler.c
Move --dry-run option to discover server
[petitboot] / discover / device-handler.c
index 0f876658b5a2331750ceeced591bfca53e81f042..12bd5aed63a20ee56b45cd12e19f7d98a12c1901 100644 (file)
@@ -1,4 +1,5 @@
 
+#include <assert.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 #include <talloc/talloc.h>
 #include <list/list.h>
 #include <log/log.h>
-#include <pb-protocol/pb-protocol.h>
+#include <types/types.h>
+#include <system/system.h>
 
 #include "device-handler.h"
 #include "discover-server.h"
+#include "event.h"
 #include "parser.h"
 #include "udev.h"
 #include "paths.h"
-
-#define MOUNT_BIN "/bin/mount"
-
-#define UMOUNT_BIN "/bin/umount"
+#include "boot.h"
 
 struct device_handler {
        struct discover_server *server;
+       int dry_run;
 
-       struct device *devices;
-       int n_devices;
+       struct device **devices;
+       unsigned int n_devices;
 
        struct list contexts;
 };
 
-struct mount_map {
-       char *device_path;
-       char *mount_point;
-};
-
-
-static struct boot_option options[] = {
-       {
-               .id = "1.1",
-               .name = "meep one",
-               .description = "meep description one",
-               .icon_file = "meep.one.png",
-               .boot_args = "root=/dev/sda1",
-       },
-};
-
-static struct device device = {
-       .id = "1",
-       .name = "meep",
-       .description = "meep description",
-       .icon_file = "meep.png",
-};
-
-int device_handler_get_current_devices(
-               struct device_handler *handler __attribute__((unused)),
-               const struct device **devices)
+/**
+ * device_handler_add - Add a device to the handler device array.
+ */
 
+static void device_handler_add(struct device_handler *handler,
+       struct device *device)
 {
-       *devices = &device;
-       return 1;
+       handler->n_devices++;
+       handler->devices = talloc_realloc(handler, handler->devices,
+               struct device *, handler->n_devices);
+       handler->devices[handler->n_devices - 1] = device;
 }
 
-static int mkdir_recursive(const char *dir)
-{
-       struct stat statbuf;
-       char *str, *sep;
-       int mode = 0755;
-
-       if (!*dir)
-               return 0;
-
-       if (!stat(dir, &statbuf)) {
-               if (!S_ISDIR(statbuf.st_mode)) {
-                       pb_log("%s: %s exists, but isn't a directory\n",
-                                       __func__, dir);
-                       return -1;
-               }
-               return 0;
-       }
-
-       str = talloc_strdup(NULL, dir);
-       sep = strchr(*str == '/' ? str + 1 : str, '/');
-
-       while (1) {
-
-               /* terminate the path at sep */
-               if (sep)
-                       *sep = '\0';
+/**
+ * device_handler_remove - Remove a device from the handler device array.
+ */
 
-               if (mkdir(str, mode) && errno != EEXIST) {
-                       pb_log("mkdir(%s): %s\n", str, strerror(errno));
-                       return -1;
-               }
+static void device_handler_remove(struct device_handler *handler,
+       struct device *device)
+{
+       unsigned int i;
 
-               if (!sep)
+       for (i = 0; i < handler->n_devices; i++)
+               if (handler->devices[i] == device)
                        break;
 
-               /* reset dir to the full path */
-               strcpy(str, dir);
-               sep = strchr(sep + 1, '/');
+       if (i == handler->n_devices) {
+               assert(0 && "unknown device");
+               return;
        }
 
-       talloc_free(str);
-
-       return 0;
+       handler->n_devices--;
+       memmove(&handler->devices[i], &handler->devices[i + 1],
+               (handler->n_devices - i) * sizeof(handler->devices[0]));
+       handler->devices = talloc_realloc(handler, handler->devices,
+               struct device *, handler->n_devices);
 }
 
-static int rmdir_recursive(const char *base, const char *dir)
+/**
+ * device_handler_find - Find a handler device by id.
+ */
+
+static struct device *device_handler_find(struct device_handler *handler,
+       const char *id)
 {
-       char *cur, *pos;
+       unsigned int i;
 
-       /* sanity check: make sure that dir is within base */
-       if (strncmp(base, dir, strlen(base)))
-               return -1;
+       assert(id);
 
-       cur = talloc_strdup(NULL, dir);
+       for (i = 0; i < handler->n_devices; i++)
+               if (handler->devices[i]->id
+                       && streq(handler->devices[i]->id, id))
+                       return handler->devices[i];
 
-       while (strcmp(base, dir)) {
+       pb_log("%s: unknown device: %s\n", __func__, id);
+       return NULL;
+}
 
-               rmdir(dir);
+/**
+ * device_handler_get_device_count - Get the count of current handler devices.
+ */
 
-               /* null-terminate at the last slash */
-               pos = strrchr(dir, '/');
-               if (!pos)
-                       break;
+int device_handler_get_device_count(const struct device_handler *handler)
+{
+       return handler->n_devices;
+}
 
-               *pos = '\0';
-       }
+/**
+ * device_handler_get_device - Get a handler device by index.
+ */
 
-       talloc_free(cur);
+const struct device *device_handler_get_device(
+       const struct device_handler *handler, unsigned int index)
+{
+       if (index >= handler->n_devices) {
+               assert(0 && "bad index");
+               return NULL;
+       }
 
-       return 0;
+       return handler->devices[index];
 }
 
 static void setup_device_links(struct discover_context *ctx)
@@ -156,7 +135,7 @@ static void setup_device_links(struct discover_context *ctx)
                char *enc, *dir, *path;
                const char *value;
 
-               value = udev_event_param(ctx->event, link->env);
+               value = event_get_param(ctx->event, link->env);
                if (!value || !*value)
                        continue;
 
@@ -164,7 +143,7 @@ static void setup_device_links(struct discover_context *ctx)
                dir = join_paths(ctx, mount_base(), link->dir);
                path = join_paths(ctx, dir, value);
 
-               if (!mkdir_recursive(dir)) {
+               if (!pb_mkdir_recursive(dir)) {
                        unlink(path);
                        if (symlink(ctx->mount_path, path)) {
                                pb_log("symlink(%s,%s): %s\n",
@@ -197,47 +176,42 @@ static void remove_device_links(struct discover_context *ctx)
 static int mount_device(struct discover_context *ctx)
 {
        const char *mountpoint;
-       int status;
-       pid_t pid;
+       const char *argv[6];
 
        if (!ctx->mount_path) {
                mountpoint = mountpoint_for_device(ctx->device_path);
                ctx->mount_path = talloc_strdup(ctx, mountpoint);
        }
 
-       if (mkdir_recursive(ctx->mount_path))
+       if (pb_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));
-               goto out_rmdir;
-       }
+       argv[0] = pb_system_apps.mount;
+       argv[1] = ctx->device_path;
+       argv[2] = ctx->mount_path;
+       argv[3] = "-o";
+       argv[4] = "ro";
+       argv[5] = NULL;
 
-       if (pid == 0) {
-               execl(MOUNT_BIN, MOUNT_BIN, ctx->device_path, ctx->mount_path,
-                               "-o", "ro", NULL);
-               exit(EXIT_FAILURE);
-       }
+       if (pb_run_cmd(argv, 1, 0)) {
 
-       if (waitpid(pid, &status, 0) == -1) {
-               pb_log("%s: waitpid failed: %s\n", __func__,
-                               strerror(errno));
-               goto out_rmdir;
-       }
+               /* Retry mount without ro option. */
+
+               argv[0] = pb_system_apps.mount;
+               argv[1] = ctx->device_path;
+               argv[2] = ctx->mount_path;
+               argv[3] = NULL;
 
-       if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
-               pb_log("%s: mount failed (%d): %s\n", __func__,
-                       WEXITSTATUS(status), ctx->event->device);
-               goto out_rmdir;
+               if (pb_run_cmd(argv, 1, 0))
+                       goto out_rmdir;
        }
 
        setup_device_links(ctx);
        return 0;
 
 out_rmdir:
-       rmdir_recursive(mount_base(), ctx->mount_path);
+       pb_rmdir_recursive(mount_base(), ctx->mount_path);
        return -1;
 }
 
@@ -255,7 +229,8 @@ static int umount_device(struct discover_context *ctx)
        }
 
        if (pid == 0) {
-               execl(UMOUNT_BIN, UMOUNT_BIN, ctx->mount_path, NULL);
+               execl(pb_system_apps.umount, pb_system_apps.umount,
+                                               ctx->mount_path, NULL);
                exit(EXIT_FAILURE);
        }
 
@@ -268,7 +243,7 @@ static int umount_device(struct discover_context *ctx)
        if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
                return -1;
 
-       rmdir_recursive(mount_base(), ctx->mount_path);
+       pb_rmdir_recursive(mount_base(), ctx->mount_path);
 
        return 0;
 }
@@ -286,7 +261,6 @@ static struct discover_context *find_context(struct device_handler *handler,
        return NULL;
 }
 
-
 static int destroy_context(void *arg)
 {
        struct discover_context *ctx = arg;
@@ -297,8 +271,8 @@ static int destroy_context(void *arg)
        return 0;
 }
 
-static int handle_add_event(struct device_handler *handler,
-               struct udev_event *event)
+static int handle_add_udev_event(struct device_handler *handler,
+               struct event *event)
 {
        struct discover_context *ctx;
        const char *devname;
@@ -313,12 +287,8 @@ static int handle_add_event(struct device_handler *handler,
 
        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);
-               return 0;
-       }
-
+       devname = event_get_param(ctx->event, "DEVNAME");
+       assert(devname);
        ctx->device_path = talloc_strdup(ctx, devname);
 
        rc = mount_device(ctx);
@@ -338,13 +308,16 @@ static int handle_add_event(struct device_handler *handler,
        /* run the parsers */
        iterate_parsers(ctx);
 
+       /* add device to handler device array */
+       device_handler_add(handler, ctx->device);
+
        discover_server_notify_add(handler->server, ctx->device);
 
        return 0;
 }
 
-static int handle_remove_event(struct device_handler *handler,
-               struct udev_event *event)
+static int handle_remove_udev_event(struct device_handler *handler,
+               struct event *event)
 {
        struct discover_context *ctx;
 
@@ -354,49 +327,102 @@ static int handle_remove_event(struct device_handler *handler,
 
        discover_server_notify_remove(handler->server, ctx->device);
 
+       /* remove device from handler device array */
+       device_handler_remove(handler, ctx->device);
+
        talloc_free(ctx);
 
        return 0;
 }
 
-int device_handler_event(struct device_handler *handler,
-               struct udev_event *event)
+static int handle_add_user_event(struct device_handler *handler,
+               struct event *event)
 {
-       int rc;
+       struct device *device;
+
+       assert(event->device);
+
+       device = talloc_zero(handler, struct device);
+
+       if (!device)
+               goto fail;
+
+       device->id = talloc_strdup(device, event->device);
+       list_init(&device->boot_options);
+
+       parse_user_event(device, event);
+
+       discover_server_notify_add(handler->server, device);
 
-       switch (event->action) {
-       case UDEV_ACTION_ADD:
-               rc = handle_add_event(handler, event);
-               break;
+       /* add device to handler device array */
+       device_handler_add(handler, device);
 
-       case UDEV_ACTION_REMOVE:
-               rc = handle_remove_event(handler, event);
-               break;
+       return 0;
+
+fail:
+       talloc_free(device);
+       return 0;
+}
+
+static int handle_remove_user_event(struct device_handler *handler,
+               struct event *event)
+{
+       struct device *device = device_handler_find(handler, event->device);
+
+       if (!device)
+               return 0;
+
+       discover_server_notify_remove(handler->server, device);
+
+       /* remove device from handler device array */
+       device_handler_remove(handler, device);
+
+       talloc_free(device);
+       return 0;
+}
+
+typedef int (*event_handler)(struct device_handler *, struct event *);
+
+static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = {
+       [EVENT_TYPE_UDEV] = {
+               [EVENT_ACTION_ADD]      = handle_add_udev_event,
+               [EVENT_ACTION_REMOVE]   = handle_remove_udev_event,
+       },
+       [EVENT_TYPE_USER] = {
+               [EVENT_ACTION_ADD]      = handle_add_user_event,
+               [EVENT_ACTION_REMOVE]   = handle_remove_user_event,
        }
+};
 
-       return rc;
+int device_handler_event(struct device_handler *handler,
+               struct event *event)
+{
+       if (event->type >= EVENT_TYPE_MAX ||
+                       event->action >= EVENT_ACTION_MAX ||
+                       !handlers[event->type][event->action]) {
+               pb_log("%s unknown type/action: %d/%d\n", __func__,
+                               event->type, event->action);
+               return 0;
+       }
+
+       return handlers[event->type][event->action](handler, event);
 }
 
-struct device_handler *device_handler_init(struct discover_server *server)
+struct device_handler *device_handler_init(struct discover_server *server,
+               int dry_run)
 {
        struct device_handler *handler;
-       unsigned int i;
 
        handler = talloc(NULL, struct device_handler);
        handler->devices = NULL;
        handler->n_devices = 0;
        handler->server = server;
+       handler->dry_run = dry_run;
 
        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);
+       pb_mkdir_recursive(mount_base());
 
        parser_init();
 
@@ -408,3 +434,29 @@ void device_handler_destroy(struct device_handler *handler)
        talloc_free(handler);
 }
 
+static struct boot_option *find_boot_option_by_id(
+               struct device_handler *handler, const char *id)
+{
+       unsigned int i;
+
+       for (i = 0; i < handler->n_devices; i++) {
+               struct device *dev = handler->devices[i];
+               struct boot_option *opt;
+
+               list_for_each_entry(&dev->boot_options, opt, list)
+                       if (!strcmp(opt->id, id))
+                               return opt;
+       }
+
+       return NULL;
+}
+
+void device_handler_boot(struct device_handler *handler,
+               struct boot_command *cmd)
+{
+       struct boot_option *opt;
+
+       opt = find_boot_option_by_id(handler, cmd->option_id);
+
+       boot(handler, opt, cmd, handler->dry_run);
+}