+static void boot_hook_setenv(struct boot_task *task)
+{
+ unsetenv("boot_image");
+ unsetenv("boot_initrd");
+ unsetenv("boot_dtb");
+ unsetenv("boot_args");
+
+ setenv("boot_image", task->local_image, 1);
+ if (task->local_initrd)
+ setenv("boot_initrd", task->local_initrd, 1);
+ if (task->local_dtb)
+ setenv("boot_dtb", task->local_dtb, 1);
+ if (task->args)
+ setenv("boot_args", task->args, 1);
+}
+
+static int hook_filter(const struct dirent *dirent)
+{
+ return dirent->d_type == DT_REG || dirent->d_type == DT_LNK;
+}
+
+static int hook_cmp(const struct dirent **a, const struct dirent **b)
+{
+ return strcmp((*a)->d_name, (*b)->d_name);
+}
+
+static void run_boot_hooks(struct boot_task *task)
+{
+ struct dirent **hooks;
+ int i, n;
+
+ n = scandir(boot_hook_dir, &hooks, hook_filter, hook_cmp);
+ if (n < 1)
+ return;
+
+ update_status(task->status_fn, task->status_arg, BOOT_STATUS_INFO,
+ _("running boot hooks"));
+
+ boot_hook_setenv(task);
+
+ for (i = 0; i < n; i++) {
+ const char *argv[2] = { NULL, NULL };
+ struct process *process;
+ char *path;
+ int rc;
+
+ path = join_paths(task, boot_hook_dir, hooks[i]->d_name);
+
+ 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);