]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: startup: refactor "daemonization" fork
authorValentine Krasnobaeva <vkrasnobaeva@haproxy.com>
Thu, 27 Jun 2024 16:16:40 +0000 (18:16 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 16 Oct 2024 20:00:58 +0000 (22:00 +0200)
Let's put "daemonization" fork into a switch-case. This is more readable and we
don't need to allocate memory for the fork() return value here.

src/haproxy.c

index c39621eb80ad5a66924b85525fa88baeb966d4bf..60c6fc6e922360ae36c94d6006166d62ea9bad35 100644 (file)
@@ -2121,14 +2121,22 @@ static void init(int argc, char **argv)
        if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL) &&
                (global.mode & MODE_DAEMON)) {
                ret = fork();
-               if (ret < 0) {
+               switch(ret) {
+               case -1:
                        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) */
+               case 0:
+                       /* in child, change the process group ID, in the master-worker
+                        * mode, this will be the master process
+                        */
                        setsid();
+
+                       break;
+               default:
+                       /* in parent, which leaves to daemonize */
+                       exit(0);
+               }
        }
 
        if (global.mode & (MODE_MWORKER|MODE_MWORKER_WAIT))