Skip to content

Commit

Permalink
BUG/MINOR: startup: fix pidfile creation
Browse files Browse the repository at this point in the history
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
  • Loading branch information
vkssv authored and wtarreau committed Dec 2, 2024
1 parent a33977d commit 2950710
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
13 changes: 9 additions & 4 deletions src/haproxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 2950710

Please sign in to comment.