diff --git a/src/haproxy.c b/src/haproxy.c index 60c6fc6e9..11d580e2a 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -155,6 +155,7 @@ char *build_features = ""; static struct list cfg_cfgfiles = LIST_HEAD_INIT(cfg_cfgfiles); int pid; /* current process id */ char **init_env; +int pidfd = -1; /* FD to keep PID */ static unsigned long stopping_tgroup_mask; /* Thread groups acknowledging stopping */ @@ -1996,6 +1997,7 @@ static void init(int argc, char **argv) struct cfgfile *cfg, *cfg_tmp; int ret, ideal_maxconn; const char *cc, *cflags, *opts; + char pidstr[100]; #ifdef USE_OPENSSL #ifdef USE_OPENSSL_WOLFSSL @@ -2139,6 +2141,22 @@ static void init(int argc, char **argv) } } + /* open log & pid files before the chroot */ + if ((global.mode & MODE_DAEMON || global.mode & MODE_MWORKER) && + !(global.mode & MODE_MWORKER_WAIT) && global.pidfile != NULL) { + unlink(global.pidfile); + pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644); + if (pidfd < 0) { + ha_alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile); + if (nb_oldpids) + tell_old_pids(SIGTTIN); + protocol_unbind_all(); + exit(1); + } + snprintf(pidstr, sizeof(pidstr), "%d\n", (int)getpid()); + DISGUISE(write(pidfd, pidstr, strlen(pidstr))); + } + if (global.mode & (MODE_MWORKER|MODE_MWORKER_WAIT)) mworker_create_master_cli(); @@ -3201,7 +3219,6 @@ int main(int argc, char **argv) { int err, retry; struct rlimit limit; - int pidfd = -1; int intovf = (unsigned char)argc + 1; /* let the compiler know it's strictly positive */ /* Catch broken toolchains */ @@ -3438,19 +3455,6 @@ int main(int argc, char **argv) } } - /* open log & pid files before the chroot */ - if ((global.mode & MODE_DAEMON || global.mode & MODE_MWORKER) && - !(global.mode & MODE_MWORKER_WAIT) && global.pidfile != NULL) { - unlink(global.pidfile); - pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 0644); - if (pidfd < 0) { - ha_alert("[%s.main()] Cannot create pidfile %s\n", argv[0], global.pidfile); - if (nb_oldpids) - tell_old_pids(SIGTTIN); - protocol_unbind_all(); - exit(1); - } - } if ((global.mode & (MODE_MWORKER|MODE_DAEMON)) == 0) { @@ -3552,14 +3556,6 @@ int main(int argc, char **argv) int in_parent = 0; int devnullfd = -1; - /* if in master-worker mode, write the PID of the father */ - if (global.mode & MODE_MWORKER) { - char pidstr[100]; - snprintf(pidstr, sizeof(pidstr), "%d\n", (int)getpid()); - if (pidfd >= 0) - DISGUISE(write(pidfd, pidstr, strlen(pidstr))); - } - /* the father launches the required number of processes */ if (!(global.mode & MODE_MWORKER_WAIT)) { struct ring *tmp_startup_logs = NULL; @@ -3585,11 +3581,6 @@ int main(int argc, char **argv) else { /* parent here */ in_parent = 1; - if (pidfd >= 0 && !(global.mode & MODE_MWORKER)) { - char pidstr[100]; - snprintf(pidstr, sizeof(pidstr), "%d\n", ret); - DISGUISE(write(pidfd, pidstr, strlen(pidstr))); - } if (global.mode & MODE_MWORKER) { struct mworker_proc *child;