+ }
+ device_handler_add_ramdisk(handler, name);
+ talloc_free(name);
+
+ return handler->ramdisks[i];
+}
+
+void device_handler_release_ramdisk(struct discover_device *device)
+{
+ struct ramdisk_device *ramdisk = device->ramdisk;
+
+ talloc_free(ramdisk->snapshot);
+ talloc_free(ramdisk->origin);
+ talloc_free(ramdisk->base);
+
+ ramdisk->snapshot = ramdisk->origin = ramdisk->base = NULL;
+ ramdisk->sectors = 0;
+
+ device->ramdisk = NULL;
+}
+
+/* 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)
+{
+ struct discover_context *ctx;
+ struct boot_status *status;
+ int rc;
+
+ status = talloc_zero(handler, struct boot_status);
+ status->type = BOOT_STATUS_INFO;
+ /*
+ * TRANSLATORS: this string will be passed the type and identifier
+ * of the device. For example, the first parameter could be "Disk",
+ * (which will be translated accordingly) and the second a Linux device
+ * identifier like 'sda1' (which will not be translated)
+ */
+ status->message = talloc_asprintf(status, _("Processing %s device %s"),
+ device_type_display_name(dev->device->type),
+ dev->device->id);
+ boot_status(handler, status);
+
+ process_boot_option_queue(handler);
+
+ /* create our context */
+ ctx = device_handler_discover_context_create(handler, dev);
+
+ rc = mount_device(dev);
+ if (rc)
+ goto out;
+
+ /* add this device to our system info */
+ system_info_register_blockdev(dev->device->id, dev->uuid,
+ dev->mount_path);
+
+ /* run the parsers. This will populate the ctx's boot_option list. */
+ iterate_parsers(ctx);
+
+ /* add discovered stuff to the handler */
+ device_handler_discover_context_commit(handler, ctx);
+
+out:
+ /*
+ * TRANSLATORS: the format specifier in this string is a Linux
+ * device identifier, like 'sda1'
+ */
+ status->message = talloc_asprintf(status,_("Processing %s complete"),
+ dev->device->id);
+ boot_status(handler, status);
+
+ talloc_free(status);
+ talloc_free(ctx);
+
+ return 0;
+}
+
+/* Incoming dhcp event */
+int device_handler_dhcp(struct device_handler *handler,
+ struct discover_device *dev, struct event *event)
+{
+ struct discover_context *ctx;
+ struct boot_status *status;
+
+ status = talloc_zero(handler, struct boot_status);
+ status->type = BOOT_STATUS_INFO;
+ /*
+ * TRANSLATORS: this format specifier will be the name of a network
+ * device, like 'eth0'.
+ */
+ status->message = talloc_asprintf(status, _("Processing dhcp event on %s"),
+ dev->device->id);
+ boot_status(handler, status);
+
+ /* create our context */
+ ctx = device_handler_discover_context_create(handler, dev);
+ ctx->event = event;
+
+ iterate_parsers(ctx);
+
+ device_handler_discover_context_commit(handler, ctx);
+
+ /*
+ * TRANSLATORS: this format specifier will be the name of a network
+ * device, like 'eth0'.
+ */
+ status->message = talloc_asprintf(status,_("Processing %s complete"),
+ dev->device->id);
+ boot_status(handler, status);
+
+ talloc_free(status);
+ talloc_free(ctx);
+
+ return 0;
+}
+
+/* incoming conf event */
+int device_handler_conf(struct device_handler *handler,
+ struct discover_device *dev, struct pb_url *url)
+{
+ struct discover_context *ctx;
+ struct boot_status *status;
+
+ status = talloc_zero(handler, struct boot_status);
+ status->type = BOOT_STATUS_INFO;
+ status->message = talloc_asprintf(status, _("Processing user config"));
+ boot_status(handler, status);
+
+ /* create our context */
+ ctx = device_handler_discover_context_create(handler, dev);
+ ctx->conf_url = url;
+
+ iterate_parsers(ctx);
+
+ device_handler_discover_context_commit(handler, ctx);
+
+ status->message = talloc_asprintf(status,
+ _("Processing user config complete"));
+ boot_status(handler, status);
+
+ talloc_free(status);
+ talloc_free(ctx);
+
+ return 0;
+}
+
+static struct discover_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 discover_device *dev = handler->devices[i];
+ struct discover_boot_option *opt;
+
+ list_for_each_entry(&dev->boot_options, opt, list)
+ if (!strcmp(opt->option->id, id))
+ return opt;
+ }
+
+ return NULL;
+}
+
+void device_handler_boot(struct device_handler *handler,
+ struct boot_command *cmd)
+{
+ struct discover_boot_option *opt = NULL;
+
+ if (cmd->option_id && strlen(cmd->option_id))
+ opt = find_boot_option_by_id(handler, cmd->option_id);
+
+ if (handler->pending_boot)
+ boot_cancel(handler->pending_boot);
+
+ platform_pre_boot();
+
+ handler->pending_boot = boot(handler, opt, cmd, handler->dry_run,
+ boot_status, handler);
+ handler->pending_boot_is_default = false;
+}
+
+void device_handler_cancel_default(struct device_handler *handler)
+{
+ struct boot_status status;
+
+ if (handler->timeout_waiter)
+ waiter_remove(handler->timeout_waiter);
+
+ handler->timeout_waiter = NULL;
+ handler->autoboot_enabled = false;
+
+ /* we only send status if we had a default boot option queued */
+ if (!handler->default_boot_option)
+ return;
+
+ pb_log("Cancelling default boot option\n");
+
+ if (handler->pending_boot && handler->pending_boot_is_default) {
+ boot_cancel(handler->pending_boot);
+ handler->pending_boot = NULL;
+ handler->pending_boot_is_default = false;
+ }
+
+ handler->default_boot_option = NULL;
+
+ status.type = BOOT_STATUS_INFO;
+ status.progress = -1;
+ status.detail = NULL;
+ status.message = _("Default boot cancelled");
+
+ discover_server_notify_boot_status(handler->server, &status);
+}
+
+void device_handler_update_config(struct device_handler *handler,
+ struct config *config)
+{
+ int rc;
+
+ rc = config_set(config);
+ if (rc)
+ return;
+
+ discover_server_notify_config(handler->server, config);
+ device_handler_update_lang(config->lang);
+ device_handler_reinit(handler);
+}
+
+static char *device_from_addr(void *ctx, struct pb_url *url)
+{
+ char *ipaddr, *buf, *tok, *dev = NULL;
+ const char *delim = " ";
+ struct sockaddr_in *ip;
+ struct sockaddr_in si;
+ struct addrinfo *res;
+ struct process *p;
+ int rc;
+
+ /* Note: IPv4 only */
+ rc = inet_pton(AF_INET, url->host, &(si.sin_addr));
+ if (rc > 0) {
+ ipaddr = url->host;