]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: startup: move PID handling in init()
authorValentine Krasnobaeva <vkrasnobaeva@haproxy.com>
Fri, 28 Jun 2024 16:15:44 +0000 (18:15 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 16 Oct 2024 20:00:58 +0000 (22:00 +0200)
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.

src/haproxy.c

index 60c6fc6e922360ae36c94d6006166d62ea9bad35..11d580e2af0477f5af7c21263d4864677c4d7557 100644 (file)
@@ -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;