procname initial management

This commit is contained in:
roberto@debian32
2011-10-16 09:45:41 +02:00
parent 02331904de
commit b39a10ff7c
12 changed files with 141 additions and 27 deletions
+5 -5
View File
@@ -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);
}
}
+4
View File
@@ -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);
+1 -1
View File
@@ -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);
+1 -1
View File
@@ -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
+1 -1
View File
@@ -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);
}
+2 -2
View File
@@ -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);
}
+1 -1
View File
@@ -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);
}
+1
View File
@@ -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()");
+1 -1
View File
@@ -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);
+46 -1
View File
@@ -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<uwsgi.argc;i++) {
strcpy(uwsgi.orig_argv[i],uwsgi.argv[i]);
}
if (uwsgi.auto_procname && name) {
uwsgi_set_processname(name);
}
}
return pid;
}
+62 -14
View File
@@ -78,6 +78,9 @@ static struct option long_base_options[] = {
#ifdef UWSGI_THREADING
{"enable-threads", no_argument, 0, 'T'},
#endif
{"auto-procname", no_argument, &uwsgi.auto_procname, 1},
{"procname-prefix", required_argument, 0, LONG_ARGS_PROCNAME_PREFIX},
{"procname-append", required_argument, 0, LONG_ARGS_PROCNAME_APPEND},
{"single-interpreter", no_argument, 0, 'i'},
{"master", no_argument, 0, 'M'},
{"emperor", required_argument, 0, LONG_ARGS_EMPEROR},
@@ -847,6 +850,38 @@ void signal_pidfile(int sig, char *filename) {
}
}
void fixup_argv_and_environ(int argc, char **argv, char **environ) {
int i;
int env_count = 0;
uwsgi.orig_argv = argv;
uwsgi.argv = uwsgi_malloc( sizeof(char *) * argc);
uwsgi.argc = argc;
for(i=0;i<argc;i++) {
if (i==0 || argv[0] + uwsgi.max_procname + 1 == argv[i]) {
uwsgi.max_procname += strlen(argv[i]) + 1;
}
uwsgi.argv[i] = strdup(argv[i]);
}
for (i = 0; environ[i] != NULL; i++) {
uwsgi.max_procname += strlen(environ[i]) + 1;
env_count++;
}
uwsgi.environ = uwsgi_malloc(sizeof(char *) * env_count);
for(i=0;i<env_count;i++) {
uwsgi.environ[i] = strdup(environ[i]);
}
#ifdef UWSGI_DEBUG
uwsgi_log("max space for custom process name = %d\n", uwsgi.max_procname);
#endif
environ = uwsgi.environ;
}
#ifdef UWSGI_AS_SHARED_LIBRARY
int uwsgi_init(int argc, char *argv[], char *envp[]) {
@@ -886,6 +921,7 @@ int main(int argc, char *argv[], char *envp[]) {
signal(SIGTERM, SIG_IGN);
signal(SIGPIPE, SIG_IGN);
//initialize masterpid with a default value
masterpid = getpid();
@@ -1024,6 +1060,9 @@ int main(int argc, char *argv[], char *envp[]) {
uwsgi.page_size = getpagesize();
uwsgi.binary_path = uwsgi_get_binary_path(argv[0]);
// ok we can now safely play with argv and environ
fixup_argv_and_environ(argc, argv, environ);
//initialize embedded plugins
UWSGI_LOAD_EMBEDDED_PLUGINS
// now a bit of magic, if the executable basename contains a 'uwsgi_' string,
@@ -1067,7 +1106,7 @@ int main(int argc, char *argv[], char *envp[]) {
#endif
uwsgi.option_index = -1;
while ((i = getopt_long(argc, argv, short_options, uwsgi.long_options, &uwsgi.option_index)) != -1) {
while ((i = getopt_long(uwsgi.argc, uwsgi.argv, short_options, uwsgi.long_options, &uwsgi.option_index)) != -1) {
if (uwsgi.option_index > -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;i<argc;i++) {
char *lazy = argv[i];
for(i=optind;i<uwsgi.argc;i++) {
char *lazy = uwsgi.argv[i];
if (lazy[0] != '[') {
if (0) {
}
@@ -1337,8 +1376,8 @@ int main(int argc, char *argv[], char *envp[]) {
//call after_opt hooks
if (uwsgi.binary_path == argv[0]) {
uwsgi.binary_path = uwsgi_str(argv[0]);
if (uwsgi.binary_path == uwsgi.argv[0]) {
uwsgi.binary_path = uwsgi_str(uwsgi.argv[0]);
}
if (!uwsgi.no_initial_output) {
@@ -1433,7 +1472,7 @@ int main(int argc, char *argv[], char *envp[]) {
exit(1);
}
uwsgi.emperor_pid = fork();
uwsgi.emperor_pid = uwsgi_fork("uWSGI Emperor");
if (uwsgi.emperor_pid < 0) {
uwsgi_error("pid()");
exit(1);
@@ -1454,7 +1493,7 @@ int main(int argc, char *argv[], char *envp[]) {
// call jail systems
for (i = 0; i < uwsgi.gp_cnt; i++) {
if (uwsgi.gp[i]->jail) {
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);
+16
View File
@@ -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