+ struct discover_boot_option *opt;
+
+ opt = talloc_zero(ctx, struct discover_boot_option);
+ opt->option = talloc_zero(opt, struct boot_option);
+ opt->device = device;
+
+ return opt;
+}
+
+static int device_match_uuid(struct discover_device *dev, const char *uuid)
+{
+ return dev->uuid && !strcmp(dev->uuid, uuid);
+}
+
+static int device_match_label(struct discover_device *dev, const char *label)
+{
+ return dev->label && !strcmp(dev->label, label);
+}
+
+static int device_match_id(struct discover_device *dev, const char *id)
+{
+ return !strcmp(dev->device->id, id);
+}
+
+static int device_match_serial(struct discover_device *dev, const char *serial)
+{
+ const char *val = discover_device_get_param(dev, "ID_SERIAL");
+ return val && !strcmp(val, serial);
+}
+
+static struct discover_device *device_lookup(
+ struct device_handler *device_handler,
+ int (match_fn)(struct discover_device *, const char *),
+ const char *str)
+{
+ struct discover_device *dev;
+ unsigned int i;
+
+ if (!str)
+ return NULL;
+
+ for (i = 0; i < device_handler->n_devices; i++) {
+ dev = device_handler->devices[i];
+
+ if (match_fn(dev, str))
+ return dev;
+ }
+
+ return NULL;
+}
+
+struct discover_device *device_lookup_by_name(struct device_handler *handler,
+ const char *name)
+{
+ if (!strncmp(name, "/dev/", strlen("/dev/")))
+ name += strlen("/dev/");
+
+ return device_lookup_by_id(handler, name);
+}
+
+struct discover_device *device_lookup_by_uuid(
+ struct device_handler *device_handler,
+ const char *uuid)
+{
+ return device_lookup(device_handler, device_match_uuid, uuid);
+}
+
+struct discover_device *device_lookup_by_label(
+ struct device_handler *device_handler,
+ const char *label)
+{
+ return device_lookup(device_handler, device_match_label, label);
+}
+
+struct discover_device *device_lookup_by_id(
+ struct device_handler *device_handler,
+ const char *id)
+{
+ return device_lookup(device_handler, device_match_id, id);
+}
+
+struct discover_device *device_lookup_by_serial(
+ struct device_handler *device_handler,
+ const char *serial)
+{
+ return device_lookup(device_handler, device_match_serial, serial);
+}
+
+void device_handler_destroy(struct device_handler *handler)
+{
+ talloc_free(handler);
+}
+
+static int destroy_device(void *arg)
+{
+ struct discover_device *dev = arg;
+
+ umount_device(dev);
+
+ return 0;
+}
+
+struct discover_device *discover_device_create(struct device_handler *handler,
+ const char *id)
+{
+ struct discover_device *dev;
+
+ dev = device_lookup_by_id(handler, id);
+ if (dev)
+ return dev;
+
+ 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);
+
+ talloc_set_destructor(dev, destroy_device);
+
+ return dev;
+}
+
+struct discover_device_param {
+ char *name;
+ char *value;
+ struct list_item list;
+};
+
+void discover_device_set_param(struct discover_device *device,
+ const char *name, const char *value)
+{
+ struct discover_device_param *param;
+ bool found = false;
+
+ list_for_each_entry(&device->params, param, list) {
+ if (!strcmp(param->name, name)) {
+ found = true;
+ break;
+ }
+ }
+
+ if (!found) {
+ if (!value)
+ return;
+ param = talloc(device, struct discover_device_param);
+ param->name = talloc_strdup(param, name);
+ list_add(&device->params, ¶m->list);
+ } else {
+ if (!value) {
+ list_remove(¶m->list);
+ talloc_free(param);
+ return;
+ }
+ talloc_free(param->value);
+ }
+
+ param->value = talloc_strdup(param, value);
+}
+
+const char *discover_device_get_param(struct discover_device *device,
+ const char *name)
+{
+ struct discover_device_param *param;
+
+ list_for_each_entry(&device->params, param, list) {
+ if (!strcmp(param->name, name))
+ return param->value;
+ }
+ return NULL;
+}
+
+struct device_handler *device_handler_init(struct discover_server *server,
+ struct waitset *waitset, int dry_run)
+{
+ struct device_handler *handler;
+ int rc;
+
+ handler = talloc_zero(NULL, struct device_handler);
+ handler->server = server;
+ handler->waitset = waitset;
+ handler->dry_run = dry_run;
+ handler->autoboot_enabled = config_get()->autoboot_enabled;
+
+ list_init(&handler->unresolved_boot_options);
+
+ /* set up our mount point base */
+ pb_mkdir_recursive(mount_base());
+
+ parser_init();
+
+ rc = device_handler_init_sources(handler);
+ if (rc) {
+ talloc_free(handler);
+ return NULL;
+ }
+
+ return handler;
+}
+
+void device_handler_reinit(struct device_handler *handler)
+{
+ struct discover_boot_option *opt, *tmp;
+ unsigned int i;
+
+ device_handler_cancel_default(handler);
+
+ /* free unresolved boot options */
+ list_for_each_entry_safe(&handler->unresolved_boot_options,
+ opt, tmp, list)
+ talloc_free(opt);
+ list_init(&handler->unresolved_boot_options);
+
+ /* drop all devices */
+ for (i = 0; i < handler->n_devices; i++)
+ discover_server_notify_device_remove(handler->server,
+ handler->devices[i]->device);
+
+ talloc_free(handler->devices);
+ handler->devices = NULL;
+ handler->n_devices = 0;
+
+ device_handler_reinit_sources(handler);
+}
+
+void device_handler_remove(struct device_handler *handler,
+ struct discover_device *device)
+{
+ struct discover_boot_option *opt, *tmp;