In the master/worker model the master may not have
permission to re-open the pidfile. So just keep its
file descriptor open.

This has a few side effects:

* Changes to the pid file will not be honoured on reinitialisation.
  - In practice this probably isn't possible in a number of situations.
    + If privileges have been dropped
    + If chrooted the new pidfile would end up inside the chroot

* If the pid file is unlinked by some other process then
  it won't be replaced on reinitialisation
  - I don't really think this is a case we ought to worry about

* There is a rave if haproxy truncates the pidfile while
  another process was reading it
  - There were races anyway.
     e.g. if a process opens the pid file just after haproxy creates it
          and is reading it while haproxy is writing to it.
  - I don't think we need to worry about this either.
---
 src/haproxy.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/haproxy.c b/src/haproxy.c
index 8c9053f..8a50f8c 100644
--- a/src/haproxy.c
+++ b/src/haproxy.c
@@ -1056,7 +1056,7 @@ void run(int argc, char **argv)
 {
        int err, retry;
        struct rlimit limit;
-       FILE *pidfile = NULL;
+       static FILE *pidfile = NULL;
        char errmsg[100];
 
        socket_cache_make_all_available();
@@ -1075,7 +1075,7 @@ void run(int argc, char **argv)
        signal_register_fct(SIGPIPE, NULL, 0);
 
        /* open log & pid files before the chroot */
-       if (global.mode & MODE_DAEMON && global.pidfile != NULL) {
+       if (!pidfile && global.mode & MODE_DAEMON && global.pidfile != NULL) {
                int pidfd;
                unlink(global.pidfile);
                pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 
0644);
@@ -1088,6 +1088,7 @@ void run(int argc, char **argv)
                }
                pidfile = fdopen(pidfd, "w");
        }
+       free(global.pidfile); global.pidfile = NULL;
 
 #ifdef CONFIG_HAP_CTTPROXY
        if (global.last_checks & LSTCHK_CTTPROXY) {
@@ -1281,7 +1282,6 @@ void run(int argc, char **argv)
 
                /* We won't ever use this anymore */
                free(oldpids);        oldpids = NULL;
-               free(global.pidfile); global.pidfile = NULL;
 
                /* we might have to unbind some proxies from some processes */
                px = proxy;
-- 
1.7.2.3


Reply via email to