Verified Commit 07035a4f authored by Karel Koci's avatar Karel Koci 🤘
Browse files

Use subprocess for hooks

parent 186882a1
......@@ -201,7 +201,7 @@ void log_subproc_open(struct log_subproc *lsp, enum log_subproc_type type, const
cls->lsp = lsp;
lsp->err = fopencookie(cls, "w", fncs);
// Print info
INFO("%s: %s", type_string[type], message);
INFO("%s", message);
}
void log_subproc_close(struct log_subproc *lsp, int exit_code, char **output) {
......@@ -212,6 +212,4 @@ void log_subproc_close(struct log_subproc *lsp, int exit_code, char **output) {
*output = lsp->buffer.buf;
else
free(lsp->buffer.buf);
if (exit_code)
WARN("%s exited with exit code: %d", type_string[lsp->type], exit_code);
}
......@@ -21,6 +21,7 @@
#include "util.h"
#include "logging.h"
#include "subprocess.h"
#include <stdio.h>
#include <string.h>
......@@ -40,27 +41,12 @@ bool dump2file (const char *file, const char *text) {
return true;
}
static void exec_dir_callback(struct wait_id id __attribute__((unused)), void *data, int status, enum command_kill_status killed __attribute__((unused)), size_t out_size, const char *out, size_t err_size, const char *err) {
if (out_size > 0) {
INFO("Subprogram output: %s:\n%s", (char *)data, out);
INFO("End of subprogram output");
}
if (err_size > 0) {
ERROR("Subprogram output: %s:\n%s", (char *)data, err);
ERROR("End of subprogram output");
}
if (out_size == 0 && err_size == 0)
INFO("Executed: %s", (char *)data);
if (status)
ERROR("Execution failed with status: %d, %s", status, (char *)data);
}
static int exec_dir_filter(const struct dirent *de) {
// ignore system paths and accept only files
return strcmp(de->d_name, ".") && strcmp(de->d_name, "..") && de->d_type == DT_REG;
}
void exec_dir(struct events *events, const char *dir) {
void exec_hook(const char *dir, const char *message) {
struct dirent **namelist;
int count = scandir(dir, &namelist, exec_dir_filter, alphasort);
if (count == -1) {
......@@ -69,10 +55,11 @@ void exec_dir(struct events *events, const char *dir) {
}
for (int i = 0; i < count; i++) {
char *fpath = aprintf("%s/%s", dir, namelist[i]->d_name);
if (!access(fpath, X_OK)) {
struct wait_id wid = run_command(events, exec_dir_callback, NULL, fpath, 0, NULL, -1, -1, fpath, (const char *)NULL);
events_wait(events, 1, &wid);
} else
char *msg = aprintf("%s: %s", message, namelist[i]->d_name);
// TODO do we want to have some timeout here?
if (!access(fpath, X_OK))
lsubprocv(LST_HOOK, msg, NULL, -1, fpath, NULL);
else
DBG("File not executed, not executable: %s", namelist[i]->d_name);
free(namelist[i]);
}
......
......@@ -31,7 +31,7 @@
bool dump2file (const char *file, const char *text) __attribute__((nonnull,nonnull));
// Executes all executable files in given directory
void exec_dir(struct events *events, const char *dir) __attribute__((nonnull));
void exec_hook(const char *dir, const char *message) __attribute__((nonnull));
// Using these functions you can register/unregister cleanup function. Note that
// they are called in reverse order of insertion. This is implemented using atexit
......
......@@ -277,10 +277,9 @@ int main(int argc, char *argv[]) {
GOTO_CLEANUP;
if (!replan) {
update_state(LS_PREUPD);
INFO("Executing preupdate hooks...");
const char *hook_path = aprintf("%s%s", root_dir, hook_preupdate);
setenv("ROOT_DIR", root_dir, true);
exec_dir(events, hook_path);
exec_hook(hook_path, "Executing preupdate hook");
}
if (task_log) {
FILE *log = fopen(task_log, "a");
......@@ -305,9 +304,8 @@ int main(int argc, char *argv[]) {
bool reboot_delayed;
ASSERT(interpreter_collect_results(interpreter, "bb", &reboot_delayed, &reboot_finished) == -1);
if (reboot_delayed) {
INFO("Executing reboot_required hooks...");
const char *hook_path = aprintf("%s%s", root_dir, hook_reboot_delayed);
exec_dir(events, hook_path);
exec_hook(hook_path, "Executing reboot_required hook");
}
err = interpreter_call(interpreter, "updater.cleanup", NULL, "bb", reboot_finished);
ASSERT_MSG(!err, "%s", err);
......@@ -321,10 +319,9 @@ int main(int argc, char *argv[]) {
}
REPLAN_CLEANUP:
update_state(LS_POSTUPD);
INFO("Executing postupdate hooks...");
const char *hook_path = aprintf("%s%s", root_dir, hook_postupdate);
setenv("SUCCESS", trans_ok ? "true" : "false", true); // ROOT_DIR is already set
exec_dir(events, hook_path);
exec_hook(hook_path, "Executing postupdate hook");
CLEANUP:
free(approvals);
interpreter_destroy(interpreter);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment