From: Valentine Krasnobaeva Date: Fri, 28 Jun 2024 16:15:44 +0000 (+0200) Subject: MEDIUM: startup: move PID handling in init() X-Git-Tag: v3.1-dev10~56 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4cbfcc60f499328a6720b6b98b3be984a7fa5bb3;p=thirdparty%2Fhaproxy.git MEDIUM: startup: move PID handling in init() Let's move PID handling in init() from the main() code. It is more appropriate to open and to write the PID of the process just after daemonization fork. In case of daemon monoprocess mode, we will simply write a PID of the process, which is already in the background. In case of 'master-worker' mode, we keep the previous behaviour and we write only a PID of the master process. This allows to remove redundant tests of the process execution mode, tests of the pidfd value and consequent writes to this pidfd. This patch prepares the refactoring of master-worker fork by moving it in init() function as well. --- diff --git a/src/haproxy.c b/src/haproxy.c index 60c6fc6e92..11d580e2af 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;