discover: Remove unnecessary event passing
authorJeremy Kerr <jk@ozlabs.org>
Thu, 19 Sep 2013 09:16:53 +0000 (17:16 +0800)
committerJeremy Kerr <jk@ozlabs.org>
Thu, 19 Sep 2013 13:36:33 +0000 (21:36 +0800)
Currently, we pass "events" between the udev, user-event and
device-handler layers. These events all get sent through
device_handler_event, then de-multiplexed to an appropriate handler,
depending on their source.

Instead, just export relevant device_handler functions, and have the
(old) event sources call these functions directly.

This also means we can include a lot more of the device hander code in
the parser tests.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
18 files changed:
discover/Makefile.am
discover/device-handler.c
discover/device-handler.h
discover/event-parser.c [deleted file]
discover/pb-discover.c
discover/udev.c
discover/user-event.c
lib/pb-config/pb-config.c
test/parser/handler.c
test/parser/parser-test.h
test/parser/test-grub2-f18-ppc64.c
test/parser/test-grub2-multiple-resolve.c
test/parser/test-grub2-ubuntu-13_04-x86.c
test/parser/test-yaboot-device-override.c
test/parser/test-yaboot-external.c
test/parser/test-yaboot-partition-override.c
test/parser/test-yaboot-partition.c
test/parser/utils.c

index 45b168f9b726c919eb15bff6caa29c656f147ff4..1dd63f1e088d76abf5b9681adeb922d51844abb5 100644 (file)
@@ -35,7 +35,6 @@ pb_discover_SOURCES = \
        discover-server.h \
        event.c \
        event.h \
-       event-parser.c \
        file.c \
        file.h \
        params.c \
index 7a84302b71314104b74309c3167abe5314d017b2..cdfee483767e449c885d0ebf3db3671040a4c78f 100644 (file)
@@ -21,7 +21,6 @@
 #include "event.h"
 #include "parser.h"
 #include "resource.h"
-#include "udev.h"
 #include "paths.h"
 #include "boot.h"
 
@@ -41,6 +40,9 @@ struct device_handler {
        struct list             unresolved_boot_options;
 };
 
+static int mount_device(struct discover_device *dev);
+static int umount_device(struct discover_device *dev);
+
 void discover_context_add_boot_option(struct discover_context *ctx,
                struct discover_boot_option *boot_option)
 {
@@ -157,83 +159,82 @@ void device_handler_destroy(struct device_handler *handler)
        talloc_free(handler);
 }
 
-#ifdef PETITBOOT_TEST
-
-/* we have a simplified interface for petitboot testing, but still want
- * to keep struct device_handler opaque. */
-struct device_handler *device_handler_init(
-               struct discover_server *server __attribute__((unused)),
-               struct waitset *waitset __attribute__((unused)),
-               int dry_run __attribute__((unused)))
+static int destroy_device(void *arg)
 {
-       struct device_handler *handler;
+       struct discover_device *dev = arg;
 
-       handler = talloc_zero(NULL, struct device_handler);
-       list_init(&handler->unresolved_boot_options);
+       umount_device(dev);
 
-       return handler;
+       return 0;
 }
 
-void device_handler_add_device(struct device_handler *handler,
-               struct discover_device *dev)
+struct discover_device *discover_device_create(struct device_handler *handler,
+               const char *id)
 {
-       handler->n_devices++;
-       handler->devices = talloc_realloc(handler, handler->devices,
-               struct discover_device *, handler->n_devices);
-       handler->devices[handler->n_devices - 1] = dev;
-}
-
-#else
+       struct discover_device *dev;
 
-static int mount_device(struct discover_device *dev)
-{
-       int rc;
+       dev = device_lookup_by_id(handler, id);
+       if (dev)
+               return dev;
 
-       if (!dev->device_path)
-               return -1;
+       dev = talloc_zero(handler, struct discover_device);
+       dev->device = talloc_zero(dev, struct device);
+       dev->device->id = talloc_strdup(dev->device, id);
+       list_init(&dev->params);
+       list_init(&dev->boot_options);
 
-       if (!dev->mount_path)
-               dev->mount_path = join_paths(dev, mount_base(),
-                                               dev->device_path);
+       talloc_set_destructor(dev, destroy_device);
 
-       if (pb_mkdir_recursive(dev->mount_path))
-               pb_log("couldn't create mount directory %s: %s\n",
-                               dev->mount_path, strerror(errno));
+       return dev;
+}
 
-       rc = process_run_simple(dev, pb_system_apps.mount,
-                       dev->device_path, dev->mount_path,
-                       "-o", "ro", NULL);
+struct discover_device_param {
+       char                    *name;
+       char                    *value;
+       struct list_item        list;
+};
 
-       if (!rc)
-               return 0;
+void discover_device_set_param(struct discover_device *device,
+               const char *name, const char *value)
+{
+       struct discover_device_param *param;
+       bool found = false;
 
-       /* Retry mount without ro option. */
-       rc = process_run_simple(dev, pb_system_apps.mount,
-                       dev->device_path, dev->mount_path, NULL);
+       list_for_each_entry(&device->params, param, list) {
+               if (!strcmp(param->name, name)) {
+                       found = true;
+                       break;
+               }
+       }
 
-       if (!rc)
-               return 0;
+       if (!found) {
+               if (!value)
+                       return;
+               param = talloc(device, struct discover_device_param);
+               param->name = talloc_strdup(param, name);
+               list_add(&device->params, &param->list);
+       } else {
+               if (!value) {
+                       list_remove(&param->list);
+                       talloc_free(param);
+                       return;
+               }
+               talloc_free(param->value);
+       }
 
-       pb_rmdir_recursive(mount_base(), dev->mount_path);
-       return -1;
+       param->value = talloc_strdup(param, value);
 }
 
-static int umount_device(struct discover_device *dev)
+const char *discover_device_get_param(struct discover_device *device,
+               const char *name)
 {
-       int status;
-
-       if (!dev->mount_path)
-               return 0;
-
-       status = process_run_simple(dev, pb_system_apps.umount,
-                       dev->mount_path, NULL);
-
-       if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
-               return -1;
+       struct discover_device_param *param;
 
-       pb_rmdir_recursive(mount_base(), dev->mount_path);
-
-       return 0;
+       list_for_each_entry(&device->params, param, list) {
+               if (!strcmp(param->name, name))
+                       return param->name;
+       }
+       return NULL;
 }
 
 struct device_handler *device_handler_init(struct discover_server *server,
@@ -241,14 +242,12 @@ struct device_handler *device_handler_init(struct discover_server *server,
 {
        struct device_handler *handler;
 
-       handler = talloc(NULL, struct device_handler);
-       handler->devices = NULL;
-       handler->n_devices = 0;
+       handler = talloc_zero(NULL, struct device_handler);
        handler->server = server;
        handler->waitset = waitset;
        handler->dry_run = dry_run;
-       handler->default_boot_option = NULL;
        handler->autoboot_enabled = config_get()->autoboot_enabled;
+
        list_init(&handler->unresolved_boot_options);
 
        /* set up our mount point base */
@@ -259,85 +258,8 @@ struct device_handler *device_handler_init(struct discover_server *server,
        return handler;
 }
 
-static int destroy_device(void *arg)
-{
-       struct discover_device *dev = arg;
-
-       umount_device(dev);
-
-       return 0;
-}
-
-static struct discover_device *find_device(struct device_handler *handler,
-               const char *id)
-{
-       struct discover_device *dev;
-       unsigned int i;
-
-       for (i = 0; i < handler->n_devices; i++) {
-               dev = handler->devices[i];
-               if (!strcmp(dev->device->id, id))
-                       return dev;
-       }
-
-       return NULL;
-}
-
-static enum device_type event_device_type(struct device *device,
-               struct event *event)
-{
-       const char *param;
-
-       param = event_get_param(event, "type");
-       if (!param) {
-               pb_log("%s: empty type\n", device->id);
-               return DEVICE_TYPE_UNKNOWN;
-       }
-
-       if (!strcmp(param, "disk") || !strcmp(param, "partition"))
-               return DEVICE_TYPE_DISK;
-
-       if (!strcmp(param, "net"))
-               return DEVICE_TYPE_NETWORK;
-
-       pb_log("%s: unknown type '%s'\n", device->id, param);
-       return DEVICE_TYPE_UNKNOWN;
-}
-
-static struct discover_device *discover_device_create(
-               struct device_handler *handler,
-               struct discover_context *ctx,
-               struct event *event)
-{
-       struct discover_device *dev;
-       const char *devnode;
-
-       dev = find_device(handler, event->device);
-       if (dev)
-               return dev;
-
-       dev = talloc_zero(ctx, struct discover_device);
-       dev->device = talloc_zero(dev, struct device);
-       list_init(&dev->boot_options);
-
-       devnode = event_get_param(ctx->event, "node");
-       if (devnode)
-               dev->device_path = talloc_strdup(dev, devnode);
-
-       dev->device->id = talloc_strdup(dev, event->device);
-       dev->device->type = event_device_type(dev->device, event);
-
-       talloc_set_destructor(dev, destroy_device);
-
-       return dev;
-}
-
-/**
- * device_handler_remove - Remove a device from the handler device array.
- */
-
-static void device_handler_remove(struct device_handler *handler,
-       struct discover_device *device)
+void device_handler_remove(struct device_handler *handler,
+               struct discover_device *device)
 {
        unsigned int i;
 
@@ -346,7 +268,7 @@ static void device_handler_remove(struct device_handler *handler,
                        break;
 
        if (i == handler->n_devices) {
-               assert(0 && "unknown device");
+               talloc_free(device);
                return;
        }
 
@@ -356,7 +278,9 @@ static void device_handler_remove(struct device_handler *handler,
        handler->devices = talloc_realloc(handler, handler->devices,
                struct discover_device *, handler->n_devices);
 
-       discover_server_notify_device_remove(handler->server, device->device);
+       if (device->notified)
+               discover_server_notify_device_remove(handler->server,
+                                                       device->device);
 
        talloc_free(device);
 }
@@ -496,6 +420,18 @@ static void boot_option_finalise(struct device_handler *handler,
                set_default(handler, opt);
 }
 
+static void notify_boot_option(struct device_handler *handler,
+               struct discover_boot_option *opt)
+{
+       struct discover_device *dev = opt->device;
+
+       if (!dev->notified)
+               discover_server_notify_device_add(handler->server,
+                                                 opt->device->device);
+       dev->notified = true;
+       discover_server_notify_boot_option_add(handler->server, opt->option);
+}
+
 static void process_boot_option_queue(struct device_handler *handler)
 {
        struct discover_boot_option *opt, *tmp;
@@ -515,46 +451,36 @@ static void process_boot_option_queue(struct device_handler *handler)
                list_add_tail(&opt->device->boot_options, &opt->list);
                talloc_steal(opt->device, opt);
                boot_option_finalise(handler, opt);
-               discover_server_notify_boot_option_add(handler->server,
-                                                       opt->option);
+               notify_boot_option(handler, opt);
        }
 }
 
+struct discover_context *device_handler_discover_context_create(
+               struct device_handler *handler,
+               struct discover_device *device)
+{
+       struct discover_context *ctx;
+
+       ctx = talloc(handler, struct discover_context);
+       ctx->device = device;
+       ctx->conf_url = NULL;
+       list_init(&ctx->boot_options);
+
+       return ctx;
+}
+
 /**
  * context_commit - Commit a temporary discovery context to the handler,
  * and notify the clients about any new options / devices
  */
-static void context_commit(struct device_handler *handler,
+void device_handler_discover_context_commit(struct device_handler *handler,
                struct discover_context *ctx)
 {
        struct discover_device *dev = ctx->device;
        struct discover_boot_option *opt, *tmp;
-       unsigned int i, existing_device = 0;
-
-       /* do we already have this device? */
-       for (i = 0; i < handler->n_devices; i++) {
-               if (ctx->device == handler->devices[i]) {
-                       existing_device = 1;
-                       break;
-               }
-       }
-
-       /* if not already present, add the device to the handler's array */
-       if (!existing_device) {
-               handler->n_devices++;
-               handler->devices = talloc_realloc(handler, handler->devices,
-                       struct discover_device *, handler->n_devices);
-               handler->devices[handler->n_devices - 1] = dev;
-               talloc_steal(handler, dev);
-
-               discover_server_notify_device_add(handler->server, dev->device);
-
-               /* this new device might be able to resolve existing boot
-                * options */
-               pb_log("New device %s, processing queue\n", dev->device->id);
-               process_boot_option_queue(handler);
-       }
 
+       if (!device_lookup_by_id(handler, dev->device->id))
+               device_handler_add_device(handler, dev);
 
        /* move boot options from the context to the device */
        list_for_each_entry_safe(&ctx->boot_options, opt, tmp, list) {
@@ -567,8 +493,7 @@ static void context_commit(struct device_handler *handler,
                        list_add_tail(&dev->boot_options, &opt->list);
                        talloc_steal(dev, opt);
                        boot_option_finalise(handler, opt);
-                       discover_server_notify_boot_option_add(handler->server,
-                                                               opt->option);
+                       notify_boot_option(handler, opt);
                } else {
                        if (!opt->source->resolve_resource) {
                                pb_log("parser %s gave us an unresolved "
@@ -588,184 +513,60 @@ static void context_commit(struct device_handler *handler,
        }
 }
 
-static int handle_add_udev_event(struct device_handler *handler,
-               struct event *event)
-{
-       struct discover_context *ctx;
-       struct discover_device *dev;
-       const char *param;
-       int rc;
-
-       /* create our context */
-       ctx = talloc(handler, struct discover_context);
-       ctx->event = event;
-       list_init(&ctx->boot_options);
-
-       /* create our top-level device */
-       dev = discover_device_create(handler, ctx, event);
-
-       ctx->device = dev;
-
-       /* try to parse UUID and labels */
-       param = event_get_param(ctx->event, "ID_FS_UUID");
-       if (param)
-               dev->uuid = talloc_strdup(dev, param);
-
-       param = event_get_param(ctx->event, "ID_FS_LABEL");
-       if (param)
-               dev->label = talloc_strdup(dev, param);
-
-       rc = mount_device(dev);
-       if (rc) {
-               talloc_free(ctx);
-               return 0;
-       }
-
-       /* run the parsers. This will populate the ctx's boot_option list. */
-       iterate_parsers(ctx, CONF_METHOD_LOCAL_FILE);
-
-       /* add discovered stuff to the handler */
-       context_commit(handler, ctx);
-
-       talloc_free(ctx);
-
-       return 0;
-}
-
-static int handle_remove_udev_event(struct device_handler *handler,
-               struct event *event)
+void device_handler_add_device(struct device_handler *handler,
+               struct discover_device *device)
 {
-       struct discover_device *dev;
-
-       dev = find_device(handler, event->device);
-       if (!dev)
-               return 0;
-
-       /* remove device from handler device array */
-       device_handler_remove(handler, dev);
+       handler->n_devices++;
+       handler->devices = talloc_realloc(handler, handler->devices,
+                               struct discover_device *, handler->n_devices);
+       handler->devices[handler->n_devices - 1] = device;
 
-       return 0;
 }
 
-static int handle_add_user_event(struct device_handler *handler,
-               struct event *event)
+/* Start discovery on a hotplugged device. The device will be in our devices
+ * array, but has only just been initialised by the hotplug source.
+ */
+int device_handler_discover(struct device_handler *handler,
+               struct discover_device *dev, enum conf_method method)
 {
        struct discover_context *ctx;
-       struct discover_device *dev;
-       int rc;
-
-       assert(event->device);
-
-       ctx = talloc(handler, struct discover_context);
-       ctx->event = event;
-       list_init(&ctx->boot_options);
 
-       dev = discover_device_create(handler, ctx, event);
-       ctx->device = dev;
+       process_boot_option_queue(handler);
 
-       rc = parse_user_event(ctx, event);
+       /* create our context */
+       ctx = device_handler_discover_context_create(handler, dev);
 
-       if (!rc)
-               context_commit(handler, ctx);
+       mount_device(dev);
 
-       return rc;
-}
-
-static int handle_remove_user_event(struct device_handler *handler,
-               struct event *event)
-{
-       struct discover_device *dev = find_device(handler, event->device);
+       /* run the parsers. This will populate the ctx's boot_option list. */
+       iterate_parsers(ctx, method);
 
-       if (!dev)
-               return 0;
+       /* add discovered stuff to the handler */
+       device_handler_discover_context_commit(handler, ctx);
 
-       /* remove device from handler device array */
-       device_handler_remove(handler, dev);
+       talloc_free(ctx);
 
        return 0;
 }
 
-static enum conf_method parse_conf_method(const char *str)
-{
-
-       if (!strcasecmp(str, "dhcp")) {
-               return CONF_METHOD_DHCP;
-       }
-       return CONF_METHOD_UNKNOWN;
-}
-
-static int handle_conf_user_event(struct device_handler *handler,
-               struct event *event)
+/* incoming conf event */
+int device_handler_conf(struct device_handler *handler,
+               struct discover_device *dev, struct pb_url *url,
+               enum conf_method method)
 {
        struct discover_context *ctx;
-       struct discover_device *dev;
-       enum conf_method method;
-       const char *val;
-
-       ctx = talloc(handler, struct discover_context);
-       ctx->event = event;
-       list_init(&ctx->boot_options);
-
-       val = event_get_param(event, "url");
-       if (!val) {
-               talloc_free(ctx);
-               return 0;
-       }
-
-       ctx->conf_url = pb_url_parse(ctx, val);
-       if (!ctx->conf_url) {
-               talloc_free(ctx);
-               return 0;
-       }
-
-       val = event_get_param(event, "method");
-       if (!val) {
-               talloc_free(ctx);
-               return 0;
-       }
 
-       method = parse_conf_method(val);
-       if (method == CONF_METHOD_UNKNOWN) {
-               talloc_free(ctx);
-               return 0;
-       }
-
-       dev = discover_device_create(handler, ctx, event);
-       ctx->device = dev;
+       /* create our context */
+       ctx = device_handler_discover_context_create(handler, dev);
+       ctx->conf_url = url;
 
        iterate_parsers(ctx, method);
 
-       context_commit(handler, ctx);
+       device_handler_discover_context_commit(handler, ctx);
 
-       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,
-               [EVENT_ACTION_CONF]     = handle_conf_user_event,
-       }
-};
-
-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;
-       }
+       talloc_free(ctx);
 
-       return handlers[event->type][event->action](handler, event);
+       return 0;
 }
 
 static struct discover_boot_option *find_boot_option_by_id(
@@ -820,4 +621,70 @@ void device_handler_cancel_default(struct device_handler *handler)
 
        discover_server_notify_boot_status(handler->server, &status);
 }
+
+#ifndef PETITBOOT_TEST
+static int mount_device(struct discover_device *dev)
+{
+       int rc;
+
+       if (!dev->device_path)
+               return -1;
+
+       if (!dev->mount_path)
+               dev->mount_path = join_paths(dev, mount_base(),
+                                               dev->device_path);
+
+       if (pb_mkdir_recursive(dev->mount_path))
+               pb_log("couldn't create mount directory %s: %s\n",
+                               dev->mount_path, strerror(errno));
+
+       rc = process_run_simple(dev, pb_system_apps.mount,
+                       dev->device_path, dev->mount_path,
+                       "-o", "ro", NULL);
+
+       if (!rc)
+               return 0;
+
+       /* Retry mount without ro option. */
+       rc = process_run_simple(dev, pb_system_apps.mount,
+                       dev->device_path, dev->mount_path, NULL);
+
+       if (!rc)
+               return 0;
+
+       pb_rmdir_recursive(mount_base(), dev->mount_path);
+       return -1;
+}
+
+static int umount_device(struct discover_device *dev)
+{
+       int status;
+
+       if (!dev->mount_path)
+               return 0;
+
+       status = process_run_simple(dev, pb_system_apps.umount,
+                       dev->mount_path, NULL);
+
+       if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
+               return -1;
+
+       pb_rmdir_recursive(mount_base(), dev->mount_path);
+
+       return 0;
+}
+#else
+
+static int umount_device(struct discover_device *dev __attribute__((unused)))
+{
+       return 0;
+}
+
+static int __attribute__((unused)) mount_device(
+               struct discover_device *dev __attribute__((unused)))
+{
+       return 0;
+}
+
 #endif
+
index e71212ceb69be512874d3c958eb42488a4a1aa95..1d78a499f706102d6606d7077e8615523b6307d9 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef _DEVICE_HANDLER_H
 #define _DEVICE_HANDLER_H
 
+#include <stdbool.h>
+
 #include <list/list.h>
 
 struct device_handler;
@@ -21,19 +23,23 @@ enum conf_method {
        CONF_METHOD_UNKNOWN = -1,
 };
 
+
 struct discover_device {
        struct device           *device;
 
        char                    **links;
        int                     n_links;
 
-       char                    *uuid;
-       char                    *label;
+       const char              *uuid;
+       const char              *label;
 
-       char                    *mount_path;
-       char                    *device_path;
+       const char              *mount_path;
+       const char              *device_path;
+
+       bool                    notified;
 
        struct list             boot_options;
+       struct list             params;
 };
 
 struct discover_boot_option {
@@ -67,14 +73,32 @@ int device_handler_get_device_count(const struct device_handler *handler);
 const struct discover_device *device_handler_get_device(
        const struct device_handler *handler, unsigned int index);
 
-struct device *discover_context_device(struct discover_context *ctx);
+struct discover_device *discover_device_create(struct device_handler *handler,
+               const char *id);
+void device_handler_add_device(struct device_handler *handler,
+               struct discover_device *device);
+int device_handler_discover(struct device_handler *handler,
+               struct discover_device *dev, enum conf_method method);
+int device_handler_conf(struct device_handler *handler,
+               struct discover_device *dev, struct pb_url *url,
+               enum conf_method method);
+void device_handler_remove(struct device_handler *handler,
+               struct discover_device *device);
+
+struct discover_context *device_handler_discover_context_create(
+               struct device_handler *handler,
+               struct discover_device *device);
+void device_handler_discover_context_commit(struct device_handler *handler,
+               struct discover_context *ctx);
+
 struct discover_boot_option *discover_boot_option_create(
                struct discover_context *ctx,
                struct discover_device *dev);
 void discover_context_add_boot_option(struct discover_context *ctx,
                struct discover_boot_option *opt);
 
-int device_handler_event(struct device_handler *handler, struct event *event);
+int device_handler_user_event(struct device_handler *handler,
+                               struct event *event);
 
 struct discover_device *device_lookup_by_name(struct device_handler *handler,
                const char *name);
@@ -85,6 +109,11 @@ struct discover_device *device_lookup_by_label(struct device_handler *handler,
 struct discover_device *device_lookup_by_id(struct device_handler *handler,
                const char *id);
 
+void discover_device_set_param(struct discover_device *device,
+               const char *name, const char *value);
+const char *discover_device_get_param(struct discover_device *device,
+               const char *name);
+
 void device_handler_boot(struct device_handler *handler,
                struct boot_command *cmd);
 void device_handler_cancel_default(struct device_handler *handler);
diff --git a/discover/event-parser.c b/discover/event-parser.c
deleted file mode 100644 (file)
index 99e2654..0000000
+++ /dev/null
@@ -1,96 +0,0 @@
-#define _GNU_SOURCE
-
-#include <assert.h>
-
-#include "log/log.h"
-#include "talloc/talloc.h"
-#include "url/url.h"
-
-#include "resource.h"
-#include "event.h"
-#include "parser-utils.h"
-#include "device-handler.h"
-
-static struct resource *user_event_resource(struct discover_boot_option *opt,
-               struct event *event, const char *param_name)
-{
-       struct resource *res;
-       struct pb_url *url;
-       const char *val;
-
-       val = event_get_param(event, param_name);
-       if (!val)
-               return NULL;
-
-       url = pb_url_parse(opt, val);
-       if (!url)
-               return NULL;
-
-       res = create_url_resource(opt, url);
-       if (!res) {
-               talloc_free(url);
-               return NULL;
-       }
-
-       return res;
-}
-
-/**
- * parse_user_event - Parse a user event.
- *
- * Understands params: name, image, args.
- */
-
-int parse_user_event(struct discover_context *ctx, struct event *event)
-{
-       struct discover_boot_option *d_opt;
-       struct boot_option *opt;
-       struct device *dev;
-       const char *p;
-
-       dev = ctx->device->device;
-
-       d_opt = discover_boot_option_create(ctx, ctx->device);
-       opt = d_opt->option;
-
-       if (!d_opt)
-               goto fail;
-
-       p = event_get_param(event, "name");
-
-       if (!p) {
-               pb_log("%s: no name found\n", __func__);
-               goto fail;
-       }
-
-       opt->id = talloc_asprintf(opt, "%s#%s", dev->id, p);
-       opt->name = talloc_strdup(opt, p);
-
-       d_opt->boot_image = user_event_resource(d_opt, event, "image");
-       if (!d_opt->boot_image) {
-               pb_log("%s: no boot image found for %s!\n", __func__,
-                               opt->name);
-               goto fail;
-       }
-
-       d_opt->initrd = user_event_resource(d_opt, event, "initrd");
-
-       p = event_get_param(event, "args");
-
-       if (p)
-               opt->boot_args = talloc_strdup(opt, p);
-
-       opt->description = talloc_asprintf(opt, "%s %s", opt->boot_image_file,
-               opt->boot_args ? : "");
-
-       if (event_get_param(event, "default"))
-               opt->is_default = true;
-
-       discover_context_add_boot_option(ctx, d_opt);
-
-       return 0;
-
-fail:
-       talloc_free(d_opt);
-       return -1;
-}
index c16d6903ed5c1da180dfd615beed1950c824b20b..26df9b3ff3f6062f9bdc85c85ae1dde068cc8e92 100644 (file)
@@ -158,8 +158,6 @@ int main(int argc, char *argv[])
 
        signal(SIGINT, sigint_handler);
 
-       config_init(NULL);
-
        if (opts.no_autoboot == opt_yes)
                config_set_autoboot(false);
 
@@ -173,6 +171,8 @@ int main(int argc, char *argv[])
        if (!procset)
                return EXIT_FAILURE;
 
+       config_init(NULL);
+
        network = network_init(server, waitset, opts.dry_run == opt_yes);
        if (!network)
                return EXIT_FAILURE;
index f9eb26d078213a9a323d74ff4e7447ca652a75bf..1c5cf71ff4fee7caf0949211ba4c0f7ee48afd37 100644 (file)
@@ -13,6 +13,7 @@
 #include <sys/un.h>
 
 #include <log/log.h>
+#include <types/types.h>
 #include <talloc/talloc.h>
 #include <waiter/waiter.h>
 #include <system/system.h>
@@ -50,93 +51,102 @@ static int udev_destructor(void *p)
        return 0;
 }
 
-static void udev_setup_event_params(struct udev_device *dev,
-               struct event *event)
+static void udev_setup_device_params(struct udev_device *udev,
+               struct discover_device *dev)
 {
        struct udev_list_entry *list, *entry;
 
-       list = udev_device_get_properties_list_entry(dev);
+       list = udev_device_get_properties_list_entry(udev);
        if (!list)
                return;
 
        udev_list_entry_foreach(entry, list)
-               event_set_param(event,udev_list_entry_get_name(entry),
+               discover_device_set_param(dev,
+                               udev_list_entry_get_name(entry),
                                udev_list_entry_get_value(entry));
 }
 
-static int udev_handle_dev_action(struct udev_device *dev, const char *action)
+static int udev_handle_dev_add(struct pb_udev *udev, struct udev_device *dev)
 {
-       const char *devtype;
-       const char *devpath;
-       const char *devnode;
-       struct pb_udev *udev;
-       struct event *event;
-       enum event_action eva = 0;
-
-       assert(dev);
-       assert(action);
-
-       devtype = udev_device_get_devtype(dev); /* DEVTYPE */
-
-       if (!devtype) {
-               pb_log("udev_device_get_devtype failed\n");
+       struct discover_device *ddev;
+       const char *typestr;
+       const char *path;
+       const char *name;
+
+       name = udev_device_get_sysname(dev);
+       if (!name) {
+               pb_debug("udev_device_get_sysname failed\n");
                return -1;
        }
 
-       devpath = udev_device_get_devpath(dev); /* DEVPATH */
-
-       if (!devpath) {
-               pb_log("udev_device_get_devpath failed\n");
+       typestr = udev_device_get_devtype(dev);
+       if (!typestr) {
+               pb_debug("udev_device_get_devtype failed\n");
                return -1;
        }
 
-       devnode = udev_device_get_devnode(dev); /* DEVNAME */
-
-       if (!devnode) {
-               pb_log("udev_device_get_devnode failed\n");
-               return -1;
+       if (!(!strcmp(typestr, "disk") || !strcmp(typestr, "partition"))) {
+               pb_debug("SKIP %s: invalid type %s\n", name, typestr);
+               return 0;
        }
 
-       /* Ignore non disk or partition, ram, loop. */
-
-       if (!(strstr(devtype, "disk") || strstr(devtype, "partition"))
-               || strstr(devpath, "virtual/block/loop")
-               || strstr(devpath, "virtual/block/ram")) {
-               pb_log("SKIP: %s - %s\n", devtype, devnode);
+       path = udev_device_get_devpath(dev);
+       if (path && (strstr(path, "virtual/block/loop")
+                       || strstr(path, "virtual/block/ram"))) {
+               pb_debug("SKIP: %s: ignored (path=%s)\n", name, path);
                return 0;
        }
 
-       if (!strcmp(action, "add")) {
-               pb_log("ADD: %s - %s\n", devtype, devnode);
-               eva = EVENT_ACTION_ADD;
-       } else if (!strcmp(action, "remove")) {
-               pb_log("REMOVE: %s - %s\n", devtype, devnode);
-               eva = EVENT_ACTION_REMOVE;
-       } else {
-               pb_log("SKIP: %s: %s - %s\n", action, devtype, devnode);
-               return 0;
+       /* We have enough info to create the device and start discovery */
+       ddev = device_lookup_by_id(udev->handler, name);
+       if (ddev) {
+               pb_debug("device %s is already present?\n", name);
+               return -1;
        }
 
-       event = talloc(NULL, struct event);
+       ddev = discover_device_create(udev->handler, name);
 
-       event->type = EVENT_TYPE_UDEV;
-       event->action = eva;
-       event->device = devnode;
+       ddev->device_path = udev_device_get_devnode(dev);
+       ddev->uuid = udev_device_get_property_value(dev, "ID_FS_UUID");
+       ddev->label = udev_device_get_property_value(dev, "ID_FS_LABEL");
+       ddev->device->type = DEVICE_TYPE_DISK;
 
-       event->n_params = 0;
-       event->params = NULL;
-       event_set_param(event, "path", devpath);
-       event_set_param(event, "node", devnode);
-       event_set_param(event, "type", devtype);
+       udev_setup_device_params(dev, ddev);
 
-       udev_setup_event_params(dev, event);
+       device_handler_discover(udev->handler, ddev, CONF_METHOD_LOCAL_FILE);
+
+       return 0;
+}
+
+static int udev_handle_dev_remove(struct pb_udev *udev, struct udev_device *dev)
+{
+       struct discover_device *ddev;
+       const char *name;
+
+       name = udev_device_get_sysname(dev);
+       if (!name) {
+               pb_debug("udev_device_get_sysname failed\n");
+               return -1;
+       }
+
+       ddev = device_lookup_by_id(udev->handler, name);
+       if (!ddev)
+               return 0;
+
+       device_handler_remove(udev->handler, ddev);
+
+       return 0;
+}
+static int udev_handle_dev_action(struct udev_device *dev, const char *action)
+{
+       struct pb_udev *udev = udev_get_userdata(udev_device_get_udev(dev));
 
-       udev = udev_get_userdata(udev_device_get_udev(dev));
-       assert(udev);
+       if (!strcmp(action, "add"))
+               return udev_handle_dev_add(udev, dev);
 
-       device_handler_event(udev->handler, event);
+       else if (!strcmp(action, "remove"))
+               return udev_handle_dev_remove(udev, dev);
 
-       talloc_free(event);
        return 0;
 }
 
index ae4e9f0a4723913e67fbbe4128356b5fa8465017..275d9e24244a430b46201e0a55c7cce86877aa1e 100644 (file)
 #include <sys/un.h>
 
 #include <log/log.h>
+#include <url/url.h>
+#include <types/types.h>
 #include <talloc/talloc.h>
 #include <waiter/waiter.h>
 
 #include "device-handler.h"
+#include "resource.h"
 #include "event.h"
 #include "user-event.h"
 
@@ -70,6 +73,156 @@ static void user_event_print_event(struct event __attribute__((unused)) *event)
                        event->params[i].name, event->params[i].value);
 }
 
+static enum conf_method parse_conf_method(const char *str)
+{
+
+       if (!strcasecmp(str, "dhcp")) {
+               return CONF_METHOD_DHCP;
+       }
+       return CONF_METHOD_UNKNOWN;
+}
+
+static struct resource *user_event_resource(struct discover_boot_option *opt,
+               struct event *event, const char *param_name)
+{
+       struct resource *res;
+       struct pb_url *url;
+       const char *val;
+
+       val = event_get_param(event, param_name);
+       if (!val)
+               return NULL;
+
+       url = pb_url_parse(opt, val);
+       if (!url)
+               return NULL;
+
+       res = create_url_resource(opt, url);
+       if (!res) {
+               talloc_free(url);
+               return NULL;
+       }
+
+       return res;
+}
+
+static int parse_user_event(struct discover_context *ctx, struct event *event)
+{
+       struct discover_boot_option *d_opt;
+       struct boot_option *opt;
+       struct device *dev;
+       const char *p;
+
+       dev = ctx->device->device;
+
+       d_opt = discover_boot_option_create(ctx, ctx->device);
+       opt = d_opt->option;
+
+       if (!d_opt)
+               goto fail;
+
+       p = event_get_param(event, "name");
+
+       if (!p) {
+               pb_log("%s: no name found\n", __func__);
+               goto fail;
+       }
+
+       opt->id = talloc_asprintf(opt, "%s#%s", dev->id, p);
+       opt->name = talloc_strdup(opt, p);
+
+       d_opt->boot_image = user_event_resource(d_opt, event, "image");
+       if (!d_opt->boot_image) {
+               pb_log("%s: no boot image found for %s!\n", __func__,
+                               opt->name);
+               goto fail;
+       }
+
+       d_opt->initrd = user_event_resource(d_opt, event, "initrd");
+
+       p = event_get_param(event, "args");
+
+       if (p)
+               opt->boot_args = talloc_strdup(opt, p);
+
+       opt->description = talloc_asprintf(opt, "%s %s", opt->boot_image_file,
+               opt->boot_args ? : "");
+
+       if (event_get_param(event, "default"))
+               opt->is_default = true;
+
+       discover_context_add_boot_option(ctx, d_opt);
+
+       return 0;
+
+fail:
+       talloc_free(d_opt);
+       return -1;
+}
+
+static int user_event_conf(struct user_event *uev, struct event *event)
+{
+       struct device_handler *handler = uev->handler;
+       struct discover_device *dev;
+       enum conf_method method;
+       struct pb_url *url;
+       const char *val;
+
+       val = event_get_param(event, "url");
+       if (!val)
+               return 0;
+
+       url = pb_url_parse(event, val);
+       if (!url)
+               return 0;
+
+       val = event_get_param(event, "method");
+       if (!val)
+               return 0;
+
+       method = parse_conf_method(val);
+       if (method == CONF_METHOD_UNKNOWN)
+               return 0;
+
+       dev = discover_device_create(handler, event->device);
+
+       device_handler_conf(handler, dev, url, method);
+
+       return 0;
+}
+
+static int user_event_add(struct user_event *uev, struct event *event)
+{
+       struct device_handler *handler = uev->handler;
+       struct discover_context *ctx;
+       struct discover_device *dev;
+
+       dev = discover_device_create(handler, event->device);
+       ctx = device_handler_discover_context_create(handler, dev);
+
+       parse_user_event(ctx, event);
+
+       device_handler_discover_context_commit(handler, ctx);
+
+       talloc_free(ctx);
+
+       return 0;
+}
+
+static int user_event_remove(struct user_event *uev, struct event *event)
+{
+       struct device_handler *handler = uev->handler;
+       struct discover_device *dev;
+
+       dev = device_lookup_by_id(handler, event->device);
+       if (!dev)
+               return 0;
+
+       device_handler_remove(handler, dev);
+
+       return 0;
+}
+
 static void user_event_handle_message(struct user_event *uev, char *buf,
        int len)
 {
@@ -85,7 +238,21 @@ static void user_event_handle_message(struct user_event *uev, char *buf,
                return;
 
        user_event_print_event(event);
-       device_handler_event(uev->handler, event);
+
+       switch (event->action) {
+       case EVENT_ACTION_ADD:
+               result = user_event_add(uev, event);
+               break;
+       case EVENT_ACTION_REMOVE:
+               result = user_event_remove(uev, event);
+               break;
+       case EVENT_ACTION_CONF:
+               result = user_event_conf(uev, event);
+               break;
+       default:
+               break;
+       }
+
        talloc_free(event);
 
        return;
index e43ddf885bfdee497df6757cf0e6f56b71ffb584..aad3b9e5a44cf3f7838fe252e36a2b614a1d007f 100644 (file)
@@ -77,6 +77,18 @@ int config_init(void *ctx)
        return 0;
 }
 
+/* A non-exported function to allow the test infrastructure to initialise
+ * (and change) the configuration variables */
+struct parser_test;
+struct config __attribute__((unused)) *test_config_init(
+               struct parser_test *test);
+struct config *test_config_init(struct parser_test *test)
+{
+       config = talloc(test, struct config);
+       config_set_defaults(config);
+       return config;
+}
+
 const struct config *config_get(void)
 {
        return config;
index f585c318c5c0dbdcf80a6bafeae74fb5e8bec742..437f76591b9b65b37e629b0775ce2df4a29f249c 100644 (file)
@@ -1,9 +1,12 @@
 
+#include <assert.h>
+
 #include <talloc/talloc.h>
 #include <types/types.h>
 
 #include "device-handler.h"
 
+typedef void (*boot_status_fn)(void *arg, struct boot_status *);
 
 void discover_server_notify_device_add(struct discover_server *server,
                struct device *device)
@@ -26,3 +29,32 @@ void discover_server_notify_device_remove(struct discover_server *server,
        (void)device;
 }
 
+void discover_server_notify_boot_status(struct discover_server *server,
+               struct boot_status *status)
+{
+       (void)server;
+       (void)status;
+}
+
+void parser_init(void)
+{
+}
+
+void iterate_parsers(struct discover_context *ctx, enum conf_method method)
+{
+       (void)ctx;
+       (void)method;
+       assert(false);
+}
+
+int boot(void *ctx, struct discover_boot_option *opt, struct boot_command *cmd,
+               int dry_run, boot_status_fn status_fn, void *status_arg)
+{
+       (void)ctx;
+       (void)opt;
+       (void)cmd;
+       (void)dry_run;
+       (void)status_fn;
+       (void)status_arg;
+       assert(false);
+}
index df9670fed8c0188657a9e8577a5cbe07c4e4ffa9..7e43a6843b8386eb0ad3586251292c80fefec41a 100644 (file)
@@ -9,6 +9,7 @@
 struct parser_test {
        struct device_handler *handler;
        struct discover_context *ctx;
+       struct config *config;
        struct {
                void    *buf;
                size_t  size;
@@ -19,7 +20,7 @@ struct parser_test {
 void __register_parser(struct parser *parser);
 
 /* test functions */
-struct discover_device *test_create_device(struct discover_context *ctx,
+struct discover_device *test_create_device(struct parser_test *test,
                const char *name);
 
 #define test_read_conf_data(t, d) \
index 94eb6a48b694cad56fcdb79063c527370e844a99..ba3515d239692b6840fea08312aa34349505e143 100644 (file)
@@ -38,7 +38,7 @@ void run_test(struct parser_test *test)
 
        /* hotplug a device with a maching UUID, and check that our
         * resources become resolved */
-       dev = test_create_device(ctx, "external");
+       dev = test_create_device(test, "external");
        dev->uuid = "773653a7-660e-490e-9a74-d9fdfc9bbbf6";
        test_hotplug_device(test, dev);
 
index dd7869512855776be4a2f07d3a0930cad4167859..4c4a7e9679accd0757016477272b5f755dba03a9 100644 (file)
@@ -34,7 +34,7 @@ void run_test(struct parser_test *test)
        check_unresolved_resource(opt[1]->boot_image);
        check_not_present_resource(opt[1]->initrd);
 
-       dev = test_create_device(ctx, "external");
+       dev = test_create_device(test, "external");
        dev->uuid = "48c1b787-20ad-47ce-b9eb-b108dddc3535";
        test_hotplug_device(test, dev);
 
index 45da55ff8b1686fec6b02b17d1e307cc268a5e10..145e4c0cfee1a919b0f911d86d8e387ff3567da3 100644 (file)
@@ -44,7 +44,7 @@ void run_test(struct parser_test *test)
 
        /* hotplug a device with a maching UUID, and check that our
         * resources become resolved */
-       dev = test_create_device(ctx, "external");
+       dev = test_create_device(test, "external");
        dev->uuid = "29beca39-9181-4780-bbb2-ab5d4be59aaf";
        test_hotplug_device(test, dev);
 
index 5db578835323d7c3f11e06dead3767756019bb3b..ddbe4f40405291674dd35d98d4dec19e154becf3 100644 (file)
@@ -63,7 +63,7 @@ void run_test(struct parser_test *test)
        /* hotplug all dependent devices */
        for (i = 0; i < 4; i++) {
                devname = talloc_asprintf(test, "sda%d", i + 1);
-               dev[i] = test_create_device(ctx, devname);
+               dev[i] = test_create_device(test, devname);
                test_hotplug_device(test, dev[i]);
        }
 
index 6d48b27a0bb5f6a66f159b0cef4d7155fe7d8c79..bd09b44e8a7999e4c3245293efd5926023f02110 100644 (file)
@@ -28,7 +28,7 @@ void run_test(struct parser_test *test)
        check_unresolved_resource(opt->boot_image);
        check_unresolved_resource(opt->initrd);
 
-       dev = test_create_device(ctx, "external");
+       dev = test_create_device(test, "external");
        test_hotplug_device(test, dev);
 
        check_resolved_local_resource(opt->boot_image, dev, "/vmlinux");
index a29c852fb896241f5a93c798669d39ea44858888..fc23ba0c65731843d89ede8b5135db4bb116e9d3 100644 (file)
@@ -26,7 +26,7 @@ void run_test(struct parser_test *test)
        check_name(opt, "linux");
        check_unresolved_resource(opt->boot_image);
 
-       dev = test_create_device(ctx, "sda2");
+       dev = test_create_device(test, "sda2");
        test_hotplug_device(test, dev);
 
        check_resolved_local_resource(opt->boot_image, dev, "/vmlinux");
index 060698240aae30eb9c288ec36e3428d120cd9cda..25aa98f0c8c6642df007340d07ae34f5f1e95cc8 100644 (file)
@@ -26,7 +26,7 @@ void run_test(struct parser_test *test)
        check_name(opt, "linux");
        check_unresolved_resource(opt->boot_image);
 
-       dev = test_create_device(ctx, "sda2");
+       dev = test_create_device(test, "sda2");
        test_hotplug_device(test, dev);
 
        check_resolved_local_resource(opt->boot_image, dev, "/vmlinux");
index 407ac80ba40e1184658f289e2f11df5801605118..de1dc13e8da60cfd789fb1b28ab62dd4d977e49d 100644 (file)
@@ -41,25 +41,22 @@ static void __attribute__((destructor)) __cleanup_parsers(void)
 }
 
 static struct discover_device *test_create_device_simple(
-               struct discover_context *ctx)
+               struct parser_test *test)
 {
        static int dev_idx;
        char name[10];
 
        sprintf(name, "__test%d", dev_idx++);
 
-       return test_create_device(ctx, name);
+       return test_create_device(test, name);
 }
 
-struct discover_device *test_create_device(struct discover_context *ctx,
+struct discover_device *test_create_device(struct parser_test *test,
                const char *name)
 {
        struct discover_device *dev;
 
-       dev = talloc_zero(ctx, struct discover_device);
-       dev->device = talloc_zero(dev, struct device);
-
-       list_init(&dev->boot_options);
+       dev = discover_device_create(test->handler, name);
 
        dev->device->id = talloc_strdup(dev, name);
        dev->device_path = talloc_asprintf(dev, "/dev/%s", name);
@@ -76,16 +73,20 @@ static struct discover_context *test_create_context(struct parser_test *test)
        assert(ctx);
 
        list_init(&ctx->boot_options);
-       ctx->device = test_create_device_simple(ctx);
+       ctx->device = test_create_device_simple(test);
+       device_handler_add_device(test->handler, ctx->device);
 
        return ctx;
 }
 
+extern struct config *test_config_init(struct parser_test *test);
+
 struct parser_test *test_init(void)
 {
        struct parser_test *test;
 
        test = talloc_zero(NULL, struct parser_test);
+       test->config = test_config_init(test);
        test->handler = device_handler_init(NULL, NULL, 0);
        test->ctx = test_create_context(test);
 
@@ -175,9 +176,6 @@ void boot_option_resolve(struct device_handler *handler,
        resource_resolve(handler, opt->source, opt->icon);
 }
 
-extern void device_handler_add_device(struct device_handler *handler,
-               struct discover_device *dev);
-
 void test_hotplug_device(struct parser_test *test, struct discover_device *dev)
 {
        struct discover_boot_option *opt;