If master/worker mode is active then there will be multiple processes
present and as such it is useful to know the relevant pids.

This also splits run() up into two parts which will aid later
refactoring.
---
 src/haproxy.c |   43 +++++++++++++++++++++++++++----------------
 1 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/src/haproxy.c b/src/haproxy.c
index 998c4ff..caa2a29 100644
--- a/src/haproxy.c
+++ b/src/haproxy.c
@@ -404,6 +404,8 @@ void init(int argc, char **argv)
        error_snapshot_id = 0;
        jobs = 0;
 
+       socket_cache_make_all_available();
+
        /* NB: POSIX does not make it mandatory for gethostname() to 
NULL-terminate
         * the string in case of truncation, and at least FreeBSD appears not 
to do
         * it.
@@ -1100,12 +1102,12 @@ static void setid_late(const char *name)
 #endif
 }
 
-void run(int argc, char **argv)
+static FILE *prepare(int argc, char **argv)
 {
        int err, retry;
        int mode = global.mode & (MODE_DAEMON|MODE_MASTER_WORKER);
        struct rlimit limit;
-       static FILE *pidfile = NULL;
+       FILE *pidfile = NULL;
        char errmsg[100];
 
        socket_cache_make_all_available();
@@ -1120,7 +1122,7 @@ void run(int argc, char **argv)
        /* Signalling a reset can be handled here */
        if (oldpids_sig == SIGUSR2) {
                tell_old_pids(SIGUSR2);
-               return;
+               exit(0);
        }
 
        signal_register_fct(SIGQUIT, dump, SIGQUIT);
@@ -1138,7 +1140,8 @@ void run(int argc, char **argv)
        signal_register_fct(SIGPIPE, NULL, 0);
 
        /* open log & pid files before the chroot */
-       if (!pidfile && global.mode & MODE_DAEMON && global.pidfile != NULL) {
+       if (!is_master && global.mode & (MODE_DAEMON|MODE_MASTER_WORKER) &&
+           global.pidfile != NULL) {
                int pidfd;
                unlink(global.pidfile);
                pidfd = open(global.pidfile, O_CREAT | O_WRONLY | O_TRUNC, 
0644);
@@ -1319,6 +1322,12 @@ void run(int argc, char **argv)
 
        socket_cache_gc();
 
+       return pidfile;
+}
+
+static void run(int argc, char **argv, FILE *pidfile)
+{
+
        if (global.mode & (MODE_DAEMON|MODE_MASTER_WORKER)) {
                struct proxy *px;
                int ret = 0;
@@ -1348,13 +1357,11 @@ void run(int argc, char **argv)
                                send_log(NULL, LOG_INFO, "Master started\n");
                }
 
-               if (pidfile != NULL) {
-                       rewind(pidfile);
-                       ftruncate(fileno(pidfile), 0);
-                       if (global.mode & MODE_MASTER_WORKER) {
-                               fprintf(pidfile, "%d\n", pid);
-                               fflush(pidfile);
-                       }
+               rewind(pidfile);
+               ftruncate(fileno(pidfile), 0);
+               if (global.mode & MODE_MASTER_WORKER) {
+                       fprintf(pidfile, "%d\n", pid);
+                       fflush(pidfile);
                }
 
                /* Store PIDs of worker processes in oldpid so
@@ -1388,10 +1395,8 @@ void run(int argc, char **argv)
                                                 "Worker #%d started\n", proc);
                                break;
                        }
-                       if (pidfile != NULL) {
-                               fprintf(pidfile, "%d\n", ret);
-                               fflush(pidfile);
-                       }
+                       fprintf(pidfile, "%d\n", ret);
+                       fflush(pidfile);
                        oldpids[proc] = ret;
                        relative_pid++; /* each child will get a different one 
*/
                }
@@ -1452,8 +1457,14 @@ void run(int argc, char **argv)
 
 int main(int argc, char **argv)
 {
+       FILE *pidfile = NULL;
+
        while (1) {
-               run(argc, argv);
+               FILE *newpidfile = prepare(argc, argv);
+               if (!is_master)
+                       pidfile = newpidfile;
+
+               run(argc, argv, pidfile);
                if (!restarting)
                        break;
        }
-- 
1.7.2.3


Reply via email to