]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MINOR: startup: fix pidfile creation
authorValentine Krasnobaeva <vkrasnobaeva@haproxy.com>
Mon, 2 Dec 2024 15:05:16 +0000 (16:05 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 2 Dec 2024 16:28:04 +0000 (17:28 +0100)
Pidfile should be created at the latest initialization stage, when we are
sure, that process is able to start successfully, otherwise PID value, written
in this file is no longer valid.

So, for the standalone mode, let's move the block, which opens the pidfile and
let's put it just before applying "chroot". In master-worker mode, master
doesn't perform chroot. So it creates the pidfile, only when the "READY"
message from the newly forked worker is received.

This should be backported only in 3.1

src/cli.c
src/haproxy.c

index a64ce8aa2f3e0b8344f5c51fcf91126b985cd9ea..05baf85ee6ba6c432f19c10994498038ea67664b 100644 (file)
--- a/src/cli.c
+++ b/src/cli.c
@@ -2526,6 +2526,14 @@ static int _send_status(char **args, char *payload, struct appctx *appctx, void
                        kill(proc->pid, oldpids_sig);
                }
        }
+
+       /* At this point we are sure, that newly forked worker is started,
+        * so we can write our PID in a pidfile, if provided. Master doesn't
+        * perform chroot.
+        */
+       if (global.pidfile != NULL)
+               handle_pidfile();
+
        load_status = 1;
        ha_notice("Loading success.\n");
 
index fd77f35454f0bdb6f4c75a8c3b3325b9defe7b45..1efa4a8b3c7bf8111c2f41f0664dca671c721d5d 100644 (file)
@@ -3370,10 +3370,6 @@ int main(int argc, char **argv)
        if ((getenv("HAPROXY_MWORKER_REEXEC") == NULL) && (global.mode & MODE_DAEMON))
                apply_daemon_mode();
 
-       /* Open pid file before the chroot */
-       if ((global.mode & MODE_DAEMON || global.mode & MODE_MWORKER) && global.pidfile != NULL)
-               handle_pidfile();
-
        /* Master-worker and program forks */
        if (global.mode & MODE_MWORKER) {
                /* fork and run binary from command keyword in program section */
@@ -3528,6 +3524,15 @@ int main(int argc, char **argv)
                }
        }
 
+       /* Open PID file before the chroot. In master-worker mode, it's master
+        * who will create the pidfile, see _send_status().
+        */
+       if (!(global.mode & MODE_MWORKER)) {
+               if (global.mode & MODE_DAEMON && (global.pidfile != NULL)) {
+                       handle_pidfile();
+               }
+       }
+
        /* Must chroot and setgid/setuid in the children */
        /* chroot if needed */
        if (global.chroot != NULL) {