From 8baeb0af2f71fd0859417dcbb31ff207de88b24d Mon Sep 17 00:00:00 2001 From: Unbit Date: Wed, 28 Aug 2013 13:25:52 +0200 Subject: [PATCH] prepare for more emperor hooks --- core/emperor.c | 12 ++++++++++ core/utils.c | 64 ++++++++++++++++++++++++++++++++++++++------------ core/uwsgi.c | 6 +++++ uwsgi.h | 7 ++++++ 4 files changed, 74 insertions(+), 15 deletions(-) diff --git a/core/emperor.c b/core/emperor.c index aced98de..3fc736a5 100644 --- a/core/emperor.c +++ b/core/emperor.c @@ -859,6 +859,18 @@ int uwsgi_emperor_vassal_start(struct uwsgi_instance *n_ui) { } } + + // once the config is sent we can run hooks (they can fail) + // exec hooks have access to all of the currently defined vars + UWSGI_VASSAL_PID, UWSGI_VASSAL_UID, UWSGI_VASSAL_GID, UWSGI_VASSAL_CONFIG + struct uwsgi_string_list *usl; + uwsgi_foreach(usl, uwsgi.exec_as_emperor) { + uwsgi_log("running \"%s\" (as-emperor for vassal \"%s\" pid: %d uid: %d gid: %d)...\n", usl->value, n_ui->name, n_ui->pid, n_ui->uid, n_ui->gid); + int ret = uwsgi_run_command_putenv_and_wait(NULL, usl->value, NULL, 0); + uwsgi_log("command \"%s\" exited with code: %d\n", usl->value, ret); + } + // 4 call hooks + // config / config + pid / config + pid + uid + gid + // call return 0; } else { diff --git a/core/utils.c b/core/utils.c index 4471c435..09c609ca 100644 --- a/core/utils.c +++ b/core/utils.c @@ -2014,23 +2014,9 @@ void uwsgi_exec_command_with_args(char *cmdline) { exit(1); } -int uwsgi_run_command_and_wait(char *command, char *arg) { +static int uwsgi_run_command_do(char *command, char *arg) { char *argv[4]; - int waitpid_status = 0; - pid_t pid = fork(); - if (pid < 0) { - return -1; - } - - if (pid > 0) { - if (waitpid(pid, &waitpid_status, 0) < 0) { - uwsgi_error("waitpid()"); - return -1; - } - - return WEXITSTATUS(waitpid_status); - } #ifdef __linux__ if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0)) { @@ -2058,6 +2044,54 @@ int uwsgi_run_command_and_wait(char *command, char *arg) { exit(1); } +int uwsgi_run_command_and_wait(char *command, char *arg) { + + int waitpid_status = 0; + pid_t pid = fork(); + if (pid < 0) { + return -1; + } + + if (pid > 0) { + if (waitpid(pid, &waitpid_status, 0) < 0) { + uwsgi_error("uwsgi_run_command_and_wait()/waitpid()"); + return -1; + } + + return WEXITSTATUS(waitpid_status); + } + return uwsgi_run_command_do(command, arg); +} + +int uwsgi_run_command_putenv_and_wait(char *command, char *arg, char **envs, unsigned int nenvs) { + + int waitpid_status = 0; + pid_t pid = fork(); + if (pid < 0) { + return -1; + } + + if (pid > 0) { + if (waitpid(pid, &waitpid_status, 0) < 0) { + uwsgi_error("uwsgi_run_command_and_wait()/waitpid()"); + return -1; + } + + return WEXITSTATUS(waitpid_status); + } + + unsigned int i; + for(i=0;i