]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: startup: move daemonization fork in init
authorValentine Krasnobaeva <vkrasnobaeva@haproxy.com>
Fri, 28 Jun 2024 16:06:52 +0000 (18:06 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 16 Oct 2024 20:00:58 +0000 (22:00 +0200)
Let's move daemonization fork in init(). We need to perform this fork always
before forking a worker process, in order to be able to launch master and then
its worker in daemon, i.e. background mode, if haproxy was started with '-D'
option.

This refactoring is a preparation step, needed for replacing then master-worker
fork in init() as well. This allows the master process not to read the whole
configuration file and not to do re-execution in order to free additional
memory, when worker was forked. In the new refactored design only the worker
process will read and apply a new configuration, while the master will arrive
very fast in its polling loop to wait worker's termination and to handle
signals. See more details in the following commits.

src/haproxy.c

index 50ec6fcb14064cfaec473ea508f0103d01e1778d..c39621eb80ad5a66924b85525fa88baeb966d4bf 100644 (file)
@@ -2115,6 +2115,22 @@ static void init(int argc, char **argv)
                global.nbthread = 1;
        }
 
+       /* if daemon + mworker: must fork here to let a master process live in
+        * background before forking children.
+        */
+       if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL) &&
+               (global.mode & MODE_DAEMON)) {
+               ret = fork();
+               if (ret < 0) {
+                       ha_alert("[%s.main()] Cannot fork.\n", argv[0]);
+                       protocol_unbind_all();
+                       exit(1); /* there has been an error */
+               } else if (ret > 0) { /* parent leave to daemonize */
+                       exit(0);
+               } else /* change the process group ID in the child (master process) */
+                       setsid();
+       }
+
        if (global.mode & (MODE_MWORKER|MODE_MWORKER_WAIT))
                mworker_create_master_cli();
 
@@ -3528,26 +3544,6 @@ int main(int argc, char **argv)
                int in_parent = 0;
                int devnullfd = -1;
 
-               /*
-                * if daemon + mworker: must fork here to let a master
-                * process live in background before forking children
-                */
-
-               if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL)
-                   && (global.mode & MODE_MWORKER)
-                   && (global.mode & MODE_DAEMON)) {
-                       ret = fork();
-                       if (ret < 0) {
-                               ha_alert("[%s.main()] Cannot fork.\n", argv[0]);
-                               protocol_unbind_all();
-                               exit(1); /* there has been an error */
-                       } else if (ret > 0) { /* parent leave to daemonize */
-                               exit(0);
-                       } else /* change the process group ID in the child (master process) */
-                               setsid();
-               }
-
-
                /* if in master-worker mode, write the PID of the father */
                if (global.mode & MODE_MWORKER) {
                        char pidstr[100];