+ if (access(path, X_OK)) {
+ talloc_free(path);
+ continue;
+ }
+
+ process = process_create(task);
+
+ argv[0] = path;
+ process->path = path;
+ process->argv = argv;
+ process->keep_stdout = true;
+
+ pb_log("running boot hook %s\n", hooks[i]->d_name);
+
+ rc = process_run_sync(process);
+ if (rc) {
+ pb_log("boot hook exec failed!\n");
+
+ } else if (WIFEXITED(process->exit_status) &&
+ WEXITSTATUS(process->exit_status)
+ == BOOT_HOOK_EXIT_UPDATE) {
+ /* if the hook returned with BOOT_HOOK_EXIT_UPDATE,
+ * then we process stdout to look for updated params
+ */
+ boot_hook_update(task, hooks[i]->d_name,
+ process->stdout_buf);
+ boot_hook_setenv(task);
+ }
+
+ process_release(process);
+ talloc_free(path);
+ }
+
+ free(hooks);
+}
+
+static bool load_pending(struct load_url_result *result)
+{
+ return result && result->status == LOAD_ASYNC;
+}
+
+static int check_load(struct boot_task *task, const char *name,
+ struct load_url_result *result)
+{
+ if (!result)
+ return 0;
+ if (result->status != LOAD_ERROR)
+ return 0;
+
+ update_status(task->status_fn, task->status_arg,
+ BOOT_STATUS_ERROR,
+ _("Couldn't load %s"), name);
+ return -1;
+}
+
+static void cleanup_load(struct load_url_result *result)
+{
+ if (!result)
+ return;
+ if (result->status != LOAD_OK)
+ return;
+ if (!result->cleanup_local)
+ return;
+ unlink(result->local);
+}
+
+static void cleanup_cancellations(struct boot_task *task,
+ struct load_url_result *cur_result)
+{
+ struct load_url_result *result, **results[] = {
+ &task->image, &task->initrd, &task->dtb,
+ };
+ bool pending = false;
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(results); i++) {
+ result = *results[i];
+
+ if (!result)
+ continue;
+
+ /* We need to cleanup and free any completed loads */
+ if (result == cur_result || result->status == LOAD_OK
+ || result->status == LOAD_ERROR) {
+ cleanup_load(result);
+ talloc_free(result);
+ *results[i] = NULL;
+
+ /* ... and cancel any pending loads, which we'll free in
+ * the completion callback */
+ } else if (result->status == LOAD_ASYNC) {
+ load_url_async_cancel(result);
+ pending = true;
+
+ /* if we're waiting for a cancellation, we still need to
+ * wait for the completion before freeing the boot task */
+ } else if (result->status == LOAD_CANCELLED) {
+ pending = true;
+ }
+ }
+
+ if (!pending)
+ talloc_free(task);
+}
+
+static void boot_process(struct load_url_result *result, void *data)
+{
+ struct boot_task *task = data;
+ int rc = -1;
+
+ if (task->cancelled) {
+ cleanup_cancellations(task, result);
+ return;
+ }
+
+ if (load_pending(task->image) ||
+ load_pending(task->initrd) ||
+ load_pending(task->dtb))
+ return;
+
+ if (check_load(task, "kernel image", task->image) ||
+ check_load(task, "initrd", task->initrd) ||
+ check_load(task, "dtb", task->dtb))
+ goto no_load;
+
+ /* we make a copy of the local paths, as the boot hooks might update
+ * and/or create these */
+ task->local_image = task->image ? task->image->local : NULL;
+ task->local_initrd = task->initrd ? task->initrd->local : NULL;
+ task->local_dtb = task->dtb ? task->dtb->local : NULL;
+
+ run_boot_hooks(task);
+
+ update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
+ _("performing kexec_load"));
+
+ rc = kexec_load(task);
+ if (rc) {
+ update_status(task->status_fn, task->status_arg,
+ BOOT_STATUS_ERROR, _("kexec load failed"));
+ }
+
+no_load:
+ cleanup_load(task->image);
+ cleanup_load(task->initrd);
+ cleanup_load(task->dtb);
+
+ if (!rc) {
+ update_status(task->status_fn, task->status_arg,
+ BOOT_STATUS_INFO,
+ _("performing kexec reboot"));
+
+ rc = kexec_reboot(task);
+ if (rc) {
+ update_status(task->status_fn, task->status_arg,
+ BOOT_STATUS_ERROR,
+ _("kexec reboot failed"));
+ }
+ }
+}
+
+static int start_url_load(struct boot_task *task, const char *name,
+ struct pb_url *url, struct load_url_result **result)
+{
+ if (!url)
+ return 0;
+
+ *result = load_url_async(task, url, boot_process, task);
+ if (!*result) {
+ update_status(task->status_fn, task->status_arg,
+ BOOT_STATUS_ERROR,
+ _("Error loading %s"), name);
+ return -1;
+ }
+ return 0;
+}
+
+struct boot_task *boot(void *ctx, struct discover_boot_option *opt,
+ struct boot_command *cmd, int dry_run,
+ boot_status_fn status_fn, void *status_arg)
+{
+ struct pb_url *image = NULL, *initrd = NULL, *dtb = NULL;
+ const struct config *config;
+ struct boot_task *boot_task;
+ const char *boot_desc;
+ int rc;
+
+ if (opt && opt->option->name)
+ boot_desc = opt->option->name;
+ else if (cmd && cmd->boot_image_file)
+ boot_desc = cmd->boot_image_file;
+ else
+ boot_desc = _("(unknown)");
+
+ update_status(status_fn, status_arg, BOOT_STATUS_INFO,
+ _("Booting %s."), boot_desc);