From b39a10ff7cb648bf30fe7a53f93a4da647484231 Mon Sep 17 00:00:00 2001 From: "roberto@debian32" Date: Sun, 16 Oct 2011 09:45:41 +0200 Subject: [PATCH] procname initial management --- gateway.c | 10 ++--- master.c | 4 ++ master_utils.c | 2 +- mule.c | 2 +- plugins/erlang/erlang.c | 2 +- plugins/fastrouter/fastrouter.c | 4 +- plugins/http/http.c | 2 +- plugins/python/uwsgi_pymodule.c | 1 + spooler.c | 2 +- utils.c | 47 +++++++++++++++++++- uwsgi.c | 76 +++++++++++++++++++++++++++------ uwsgi.h | 16 +++++++ 12 files changed, 141 insertions(+), 27 deletions(-) diff --git a/gateway.c b/gateway.c index 17be0808..4e9c83df 100644 --- a/gateway.c +++ b/gateway.c @@ -47,7 +47,7 @@ struct uwsgi_gateway *register_gateway(char *name, void (*loop)(void)) { } } - gw_pid = fork(); + gw_pid = uwsgi_fork(name); if (gw_pid < 0) { uwsgi_error("fork()"); return NULL; @@ -90,7 +90,7 @@ struct uwsgi_gateway *register_gateway(char *name, void (*loop)(void)) { ug->loop = loop; ug->num = num; - uwsgi_log( "spawned uWSGI %s %d (pid: %d)\n", ug->name, ug->num, (int) ug->pid); + uwsgi_log( "spawned %s %d (pid: %d)\n", ug->name, ug->num, (int) ug->pid); uwsgi.gateways_cnt++; @@ -103,7 +103,7 @@ void gateway_respawn(int id) { pid_t gw_pid; struct uwsgi_gateway *ug = &uwsgi.gateways[id]; - gw_pid = fork(); + gw_pid = uwsgi_fork(ug->name); if (gw_pid < 0) { uwsgi_error("fork()"); return; @@ -124,10 +124,10 @@ void gateway_respawn(int id) { ug->pid = gw_pid; ug->respawns++; if (ug->respawns == 1) { - uwsgi_log( "spawned uWSGI %s %d (pid: %d)\n", ug->name, ug->num, (int) gw_pid); + uwsgi_log( "spawned %s %d (pid: %d)\n", ug->name, ug->num, (int) gw_pid); } else { - uwsgi_log( "respawned uWSGI %s %d (pid: %d)\n", ug->name, ug->num, (int) gw_pid); + uwsgi_log( "respawned %s %d (pid: %d)\n", ug->name, ug->num, (int) gw_pid); } } diff --git a/master.c b/master.c index c4e20972..8e080f7d 100644 --- a/master.c +++ b/master.c @@ -294,6 +294,10 @@ int master_loop(char **argv, char **environ) { struct uwsgi_rb_timer *min_timeout; struct rb_root *rb_timers = uwsgi_init_rb_timer(); + if (uwsgi.auto_procname) { + uwsgi_set_processname("uWSGI master"); + } + uwsgi.current_time = time(NULL); uwsgi_unix_signal(SIGHUP, grace_them_all); diff --git a/master_utils.c b/master_utils.c index baddbcbd..01f8d9a0 100644 --- a/master_utils.c +++ b/master_utils.c @@ -81,7 +81,7 @@ int uwsgi_respawn_worker(int wid) { int respawns = uwsgi.workers[wid].respawn_count; int i; - pid_t pid = fork(); + pid_t pid = uwsgi_fork(uwsgi.workers[wid].name); if (pid == 0) { signal(SIGWINCH, worker_wakeup); diff --git a/mule.c b/mule.c index 552bbd4f..6452ba2c 100644 --- a/mule.c +++ b/mule.c @@ -16,7 +16,7 @@ void uwsgi_mule(int id) { int i; - pid_t pid = fork(); + pid_t pid = uwsgi_fork(uwsgi.mules[id-1].name); if (pid == 0) { uwsgi.muleid = id; // avoid race conditions diff --git a/plugins/erlang/erlang.c b/plugins/erlang/erlang.c index 2adbbedf..f555c912 100644 --- a/plugins/erlang/erlang.c +++ b/plugins/erlang/erlang.c @@ -422,7 +422,7 @@ int erlang_init() { uwsgi_log("Erlang C-Node %s registered on port %d\n", ei_thisnodename(&uerl.cnode), ntohs(sin.sin_port)); - if (register_fat_gateway("erlang", erlang_loop) == NULL) { + if (register_fat_gateway("uWSGI erlang c-node", erlang_loop) == NULL) { uwsgi_log("unable to register the erlang gateway\n"); exit(1); } diff --git a/plugins/fastrouter/fastrouter.c b/plugins/fastrouter/fastrouter.c index f349f078..8efa3eb0 100644 --- a/plugins/fastrouter/fastrouter.c +++ b/plugins/fastrouter/fastrouter.c @@ -701,13 +701,13 @@ int fastrouter_init() { if (!ufr.nevents) ufr.nevents = 64; if (ufr.code_string_code && ufr.code_string_function) { - if (register_fat_gateway("fastrouter", fastrouter_loop) == NULL) { + if (register_fat_gateway("uWSGI fastrouter", fastrouter_loop) == NULL) { uwsgi_log("unable to register the fastrouter gateway\n"); exit(1); } } else { - if (register_gateway("fastrouter", fastrouter_loop) == NULL) { + if (register_gateway("uWSGI fastrouter", fastrouter_loop) == NULL) { uwsgi_log("unable to register the fastrouter gateway\n"); exit(1); } diff --git a/plugins/http/http.c b/plugins/http/http.c index 197ba336..b8a9b244 100644 --- a/plugins/http/http.c +++ b/plugins/http/http.c @@ -883,7 +883,7 @@ int http_init() { uhttp.server = bind_to_tcp(uhttp.socket_name, uwsgi.listen_queue, strchr(uhttp.socket_name,':')); - if (register_gateway("http", http_loop) == NULL) { + if (register_gateway("uWSGI http", http_loop) == NULL) { uwsgi_log("unable to register the http gateway\n"); exit(1); } diff --git a/plugins/python/uwsgi_pymodule.c b/plugins/python/uwsgi_pymodule.c index 0e6c81a9..a001b2aa 100644 --- a/plugins/python/uwsgi_pymodule.c +++ b/plugins/python/uwsgi_pymodule.c @@ -2636,6 +2636,7 @@ PyObject *py_uwsgi_grunt(PyObject * self, PyObject * args) { goto clear; } + // use a normal fork here grunt_pid = fork(); if (grunt_pid < 0) { uwsgi_error("fork()"); diff --git a/spooler.c b/spooler.c index 0e419c8e..7f766492 100644 --- a/spooler.c +++ b/spooler.c @@ -16,7 +16,7 @@ pid_t spooler_start() { int i; - pid_t pid = fork(); + pid_t pid = uwsgi_fork("uWSGI spooler"); if (pid < 0) { uwsgi_error("fork()"); exit(1); diff --git a/utils.c b/utils.c index a5a25ac9..06b9a1ac 100644 --- a/utils.c +++ b/utils.c @@ -2226,7 +2226,7 @@ void spawn_daemon(struct uwsgi_daemon *ud) { int cnt = 1; int devnull = -1; - pid_t pid = fork(); + pid_t pid = uwsgi_fork("uWSGI external daemon"); if (pid < 0) { uwsgi_error("fork()"); return; @@ -3187,3 +3187,48 @@ void uwsgi_apply_config_pass(char symbol, char*(*hook)(char *) ) { } } + +void uwsgi_set_processname(char *name) { + + char *pos = uwsgi.orig_argv[0]; + size_t amount = 0; + + // prepare for strncat + *uwsgi.orig_argv[0] = 0; + + if (uwsgi.procname_prefix) { + amount += strlen(uwsgi.procname_prefix)+1; + strncat(uwsgi.orig_argv[0], uwsgi.procname_prefix, uwsgi.max_procname-amount); + pos+=amount; + } + + amount += strlen(name)+1; + strncat(uwsgi.orig_argv[0], name, uwsgi.max_procname-amount); + pos+=amount; + + if (uwsgi.procname_append) { + amount += strlen(uwsgi.procname_append)+1; + strncat(uwsgi.orig_argv[0], uwsgi.procname_append, uwsgi.max_procname-amount); + pos+=amount; + } + + memset(pos, ' ', uwsgi.max_procname-amount); +} + +// this is a wrapper for fork restoring original argv +pid_t uwsgi_fork(char *name) { + + int i; + + pid_t pid = fork(); + if (pid == 0) { + for(i=0;i -1) { optname = (char *) uwsgi.long_options[uwsgi.option_index].name; @@ -1091,8 +1130,8 @@ int main(int argc, char *argv[], char *envp[]) { #endif if (optind < argc) { - for(i=optind;ijail) { - uwsgi.gp[i]->jail(uwsgi_start, argv); + uwsgi.gp[i]->jail(uwsgi_start, uwsgi.argv); } } @@ -1462,12 +1501,12 @@ int main(int argc, char *argv[], char *envp[]) { // TODO pluginize basic Linux namespace support #ifdef __linux__ if (uwsgi.ns) { - linux_namespace_start((void *) argv); + linux_namespace_start((void *) uwsgi.argv); // never here } else { #endif - uwsgi_start((void *) argv); + uwsgi_start((void *) uwsgi.argv); #ifdef __linux__ } #endif @@ -1479,8 +1518,6 @@ int main(int argc, char *argv[], char *envp[]) { int uwsgi_start(void *v_argv) { - char **argv = v_argv; - #ifdef UWSGI_DEBUG int so_bufsize; socklen_t so_bufsize_len; @@ -1590,7 +1627,7 @@ int uwsgi_start(void *v_argv) { exit(1); } - uwsgi.emperor_pid = fork(); + uwsgi.emperor_pid = uwsgi_fork("uWSGI Emperor"); if (uwsgi.emperor_pid < 0) { uwsgi_error("pid()"); exit(1); @@ -2154,6 +2191,7 @@ skipzero: for(i=1;i<=uwsgi.numproc;i++) { uwsgi.workers[i].signal_pipe[0] = - 1; uwsgi.workers[i].signal_pipe[1] = - 1; + snprintf(uwsgi.workers[i].name, 0xff, "uWSGI worker %d", i); } if (uwsgi.master_process) { @@ -2193,6 +2231,8 @@ skipzero: uwsgi_error("socketpair()"); exit(1); } + + snprintf(uwsgi.mules[i].name, 0xff, "uWSGI mule %d", i); } } @@ -2373,12 +2413,12 @@ skipzero: if (getpid() == masterpid && uwsgi.master_process == 1) { #ifdef UWSGI_AS_SHARED_LIBRARY - int ml_ret = master_loop(argv, environ); + int ml_ret = master_loop(uwsgi.argv, uwsgi.environ); if (ml_ret == -1) { return 0; } #else - (void) master_loop(argv, environ); + (void) master_loop(uwsgi.argv, uwsgi.environ); #endif //from now on the process is a real worker } @@ -2799,6 +2839,14 @@ static int manage_base_opt(int i, char *optarg) { case 0: return 1; + case LONG_ARGS_PROCNAME_PREFIX: + uwsgi.auto_procname = 1; + uwsgi.procname_prefix = optarg; + return 1; + case LONG_ARGS_PROCNAME_APPEND: + uwsgi.auto_procname = 1; + uwsgi.procname_append = optarg; + return 1; #ifdef UWSGI_UDP case LONG_ARGS_CLUSTER_RELOAD: send_udp_message(98, optarg, "", 0); diff --git a/uwsgi.h b/uwsgi.h index 5f5becb2..b18b1b31 100644 --- a/uwsgi.h +++ b/uwsgi.h @@ -528,6 +528,8 @@ struct uwsgi_opt { #define LONG_ARGS_EMPEROR_STATS 17150 #define LONG_ARGS_SPOOLER_CHDIR 17151 #define LONG_ARGS_LOCKS 17152 +#define LONG_ARGS_PROCNAME_PREFIX 17153 +#define LONG_ARGS_PROCNAME_APPEND 17154 #define UWSGI_OK 0 @@ -966,6 +968,15 @@ struct uwsgi_server { char hostname[256]; int hostname_len; + char **orig_argv; + char **argv; + int argc; + int max_procname; + int auto_procname; + char **environ; + char *procname_prefix; + char *procname_append; + // quiet startup int no_initial_output; @@ -1712,6 +1723,7 @@ struct uwsgi_worker { uint64_t avg_response_time; + char name[0xff]; }; struct uwsgi_mule { @@ -2371,6 +2383,10 @@ char *uwsgi_string_get_list(struct uwsgi_string_list **, int, size_t *); void uwsgi_fixup_fds(int, int); +void uwsgi_set_processname(char *); + +pid_t uwsgi_fork(char *); + #ifdef UWSGI_CAP void uwsgi_build_cap(char *); #endif