Dnia środa, 2 maja 2012 15:32:32 Łukasz Mierzwa pisze:
> Dnia środa, 2 maja 2012 11:41:04 Łukasz Mierzwa pisze:
> > Since there is now uWSGI option to run any command at worker exit, I had
> > to
> > write very simple patch that adds this. There is no way to run that
> > command
> > as root since this is done after privileges drop so the command is being
> > run as user (unless someone runs workers as root user).
> >
> > Does this patch qualifies for merging into uWSGI?
>
> It turns out that I need to make another patch, with this one exec-as-user-
> atexit would be executed at each worker shutdown, and I need to execute it
> only at master exit.
This patch works for me, I can execute command at app shutdown and emperor
reload.
Łukasz Mierzwa
diff -r c3cdecbf2bac uwsgi.c
--- a/uwsgi.c Mon Apr 30 11:05:08 2012 +0200
+++ b/uwsgi.c Fri May 04 14:14:11 2012 +0200
@@ -218,6 +218,7 @@
{"exec-in-jail", required_argument,0, "run the specified command in jail after initialization", uwsgi_opt_add_string_list, &uwsgi.exec_in_jail,0},
{"exec-as-root", required_argument,0, "run the specified command before privileges drop", uwsgi_opt_add_string_list, &uwsgi.exec_as_root,0},
{"exec-as-user", required_argument,0, "run the specified command after privileges drop", uwsgi_opt_add_string_list, &uwsgi.exec_as_user,0},
+ {"exec-as-user-atexit", required_argument,0, "run the specified command before app exit and reload", uwsgi_opt_add_string_list, &uwsgi.exec_as_user_atexit,0},
{"exec-pre-app", required_argument,0, "run the specified command before app loading", uwsgi_opt_add_string_list, &uwsgi.exec_pre_app,0},
#ifdef UWSGI_INI
{"ini", required_argument, 0, "load config from ini file", uwsgi_opt_load_ini, NULL, UWSGI_OPT_IMMEDIATE},
@@ -620,6 +621,27 @@
}
#endif
+pid_t masterpid;
+
+static void exec_atexit(void) {
+
+ if (uwsgi.exec_as_user_atexit) {
+ if (getpid() == masterpid) {
+ // now run exit scripts needed by the user
+ struct uwsgi_string_list *usl = uwsgi.exec_as_user_atexit;
+ while(usl) {
+ uwsgi_log("running \"%s\" (as uid: %d gid: %d) ...\n", usl->value, (int) getuid(), (int) getgid());
+ int ret = uwsgi_run_command_and_wait(NULL, usl->value);
+ if (ret != 0) {
+ uwsgi_log("command \"%s\" exited with non-zero code: %d\n", usl->value, ret);
+ }
+ usl = usl->next;
+ }
+ }
+ }
+
+}
+
void kill_them_all(int signum) {
int i;
@@ -724,6 +746,7 @@
kill(uwsgi.mules[i].pid, SIGKILL);
}
+ exec_atexit();
uwsgi_log("...gracefully killing workers...\n");
@@ -941,7 +964,6 @@
}
-pid_t masterpid;
struct timeval last_respawn;
@@ -1272,6 +1294,7 @@
init_magic_table(uwsgi.magic_table);
+ atexit(exec_atexit);
atexit(uwsgi_flush_logs);
atexit(vacuum);
atexit(uwsgi_plugins_atexit);
diff -r c3cdecbf2bac uwsgi.h
--- a/uwsgi.h Mon Apr 30 11:05:08 2012 +0200
+++ b/uwsgi.h Fri May 04 14:14:11 2012 +0200
@@ -1264,6 +1264,7 @@
struct uwsgi_string_list *exec_in_jail;
struct uwsgi_string_list *exec_as_root;
struct uwsgi_string_list *exec_as_user;
+ struct uwsgi_string_list *exec_as_user_atexit;
struct uwsgi_string_list *exec_pre_app;
struct uwsgi_logger *loggers;
@@ -1486,6 +1487,7 @@
char *chdir2;
int vacuum;
+ int exec_atexit;
int no_server;
int command_mode;
_______________________________________________
uWSGI mailing list
[email protected]
http://lists.unbit.it/cgi-bin/mailman/listinfo/uwsgi