summaryrefslogtreecommitdiffstats
path: root/discover
diff options
context:
space:
mode:
authorJeremy Kerr <jk@ozlabs.org>2013-09-19 17:16:53 +0800
committerJeremy Kerr <jk@ozlabs.org>2013-09-19 21:36:33 +0800
commitf611bde3f182e9a4befb48a0160d1831708aca67 (patch)
treecacf467246c85c491bf91b3fe46c448c5fdbab94 /discover
parent4926cde5c97d09794ec33cca1321bb05a8d43304 (diff)
downloadpetitboot-f611bde3f182e9a4befb48a0160d1831708aca67.zip
petitboot-f611bde3f182e9a4befb48a0160d1831708aca67.tar.gz
discover: Remove unnecessary event passing
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>
Diffstat (limited to 'discover')
-rw-r--r--discover/Makefile.am1
-rw-r--r--discover/device-handler.c523
-rw-r--r--discover/device-handler.h41
-rw-r--r--discover/event-parser.c96
-rw-r--r--discover/pb-discover.c4
-rw-r--r--discover/udev.c124
-rw-r--r--discover/user-event.c169
7 files changed, 467 insertions, 491 deletions
diff --git a/discover/Makefile.am b/discover/Makefile.am
index 45b168f..1dd63f1 100644
--- a/discover/Makefile.am
+++ b/discover/Makefile.am
@@ -35,7 +35,6 @@ pb_discover_SOURCES = \
discover-server.h \
event.c \
event.h \
- event-parser.c \
file.c \
file.h \
params.c \
diff --git a/discover/device-handler.c b/discover/device-handler.c
index 7a84302..cdfee48 100644
--- a/discover/device-handler.c
+++ b/discover/device-handler.c
@@ -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
+
diff --git a/discover/device-handler.h b/discover/device-handler.h
index e71212c..1d78a49 100644
--- a/discover/device-handler.h
+++ b/discover/device-handler.h
@@ -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
index 99e2654..0000000
--- a/discover/event-parser.c
+++ /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;
-}
diff --git a/discover/pb-discover.c b/discover/pb-discover.c
index c16d690..26df9b3 100644
--- a/discover/pb-discover.c
+++ b/discover/pb-discover.c
@@ -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;
diff --git a/discover/udev.c b/discover/udev.c
index f9eb26d..1c5cf71 100644
--- a/discover/udev.c
+++ b/discover/udev.c
@@ -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;
}
diff --git a/discover/user-event.c b/discover/user-event.c
index ae4e9f0..275d9e2 100644
--- a/discover/user-event.c
+++ b/discover/user-event.c
@@ -29,10 +29,13 @@
#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;
OpenPOWER on IntegriCloud