Displays the master information in show proc.
---
 include/types/global.h |  2 ++
 src/cli.c              | 18 +++++++++++++++---
 src/haproxy.c          | 42 ++++++++++++++++++++++++++++++++----------
 3 files changed, 49 insertions(+), 13 deletions(-)

diff --git a/include/types/global.h b/include/types/global.h
index be4d7c533..ccfc7a96a 100644
--- a/include/types/global.h
+++ b/include/types/global.h
@@ -209,6 +209,7 @@ struct activity {
  */
 struct mworker_proc {
        int pid;
+       char type;  /* m(aster), w(orker)  */
        int ipc_fd[2]; /* 0 is master side, 1 is worker side */
        int relative_pid;
        int reloads;
@@ -242,6 +243,7 @@ extern struct task *global_listener_queue_task;
 extern unsigned int warned;     /* bitfield of a few warnings to emit just 
once */
 extern volatile unsigned long sleeping_thread_mask;
 extern struct list proc_list; /* list of process in mworker mode */
+extern struct mworker_proc *proc_self; /* process structure of current process 
*/
 
 /* bit values to go with "warned" above */
 #define WARN_BLOCK_DEPRECATED       0x00000001
diff --git a/src/cli.c b/src/cli.c
index 24cb2a48f..4356586bf 100644
--- a/src/cli.c
+++ b/src/cli.c
@@ -1399,6 +1399,7 @@ static int cli_io_handler_show_proc(struct appctx *appctx)
        struct stream_interface *si = appctx->owner;
        struct mworker_proc *child;
        int old = 0;
+       int up = now.tv_sec - proc_self->timestamp;
 
        if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW)))
                return 1;
@@ -1406,13 +1407,16 @@ static int cli_io_handler_show_proc(struct appctx 
*appctx)
        chunk_reset(&trash);
 
        chunk_printf(&trash, "#%-14s %-15s %-15s %-15s %s\n", "<PID>", 
"<type>", "<relative PID>", "<reloads>", "<uptime>");
-       chunk_appendf(&trash, "%-15u %-15s %-15u %-15s %s\n", getpid(), 
"master", 0, "-", "-");
+       chunk_appendf(&trash, "%-15u %-15s %-15u %-15d %dd %02dh%02dm%02ds\n", 
getpid(), "master", 0, proc_self->reloads, up / 86400, (up % 86400) / 3600, (up 
% 3600) / 60, (up % 60));
 
        /* displays current processes */
 
        chunk_appendf(&trash, "# workers\n");
        list_for_each_entry(child, &proc_list, list) {
-               int up = now.tv_sec - child->timestamp;
+               up = now.tv_sec - child->timestamp;
+
+               if (child->type != 'w')
+                       continue;
 
                if (child->reloads > 0) {
                        old++;
@@ -1426,7 +1430,11 @@ static int cli_io_handler_show_proc(struct appctx 
*appctx)
        if (old) {
                chunk_appendf(&trash, "# old workers\n");
                list_for_each_entry(child, &proc_list, list) {
-                       int up = now.tv_sec - child->timestamp;
+                       up = now.tv_sec - child->timestamp;
+
+                       if (child->type != 'w')
+                               continue;
+
                        if (child->reloads > 0)
                                chunk_appendf(&trash, "%-15u %-15s %-15u %-15d 
%dd %02dh%02dm%02ds\n", child->pid, "worker", child->relative_pid, 
child->reloads, up / 86400, (up % 86400) / 3600, (up % 3600) / 60, (up % 60));
                }
@@ -1707,6 +1715,8 @@ static int pcli_prefix_to_pid(const char *prefix)
                if (*errtol != '\0')
                        return -1;
                list_for_each_entry(child, &proc_list, list) {
+                       if (child->type != 'w')
+                               continue;
                        if (child->pid == proc_pid){
                                return child->pid;
                        }
@@ -1725,6 +1735,8 @@ static int pcli_prefix_to_pid(const char *prefix)
                /* chose the right process, the current one is the one with the
                 least number of reloads */
                list_for_each_entry(child, &proc_list, list) {
+                       if (child->type != 'w')
+                               continue;
                        if (child->relative_pid == proc_pid){
                                if (child->reloads == 0)
                                        return child->pid;
diff --git a/src/haproxy.c b/src/haproxy.c
index 276652b99..225dc6933 100644
--- a/src/haproxy.c
+++ b/src/haproxy.c
@@ -219,7 +219,7 @@ struct list proc_list = LIST_HEAD_INIT(proc_list);
 
 int master = 0; /* 1 if in master, 0 if in child */
 
-struct mworker_proc *proc_self;
+struct mworker_proc *proc_self = NULL;
 
 /* list of the temporarily limited listeners because of lack of resource */
 struct list global_listener_queue = LIST_HEAD_INIT(global_listener_queue);
@@ -540,13 +540,10 @@ static void mworker_proc_list_to_env()
        struct mworker_proc *child;
 
        list_for_each_entry(child, &proc_list, list) {
-               if (msg)
-                       memprintf(&msg, 
"%s|type=worker;fd=%d;pid=%d;rpid=%d;reloads=%d;timestamp=%d", msg, 
child->ipc_fd[0], child->pid, child->relative_pid, child->reloads, 
child->timestamp);
-               else
-                       memprintf(&msg, 
"type=worker;fd=%d;pid=%d;rpid=%d;reloads=%d;timestamp=%d", child->ipc_fd[0], 
child->pid, child->relative_pid, child->reloads, child->timestamp);
+               memprintf(&msg, 
"%s|type=%c;fd=%d;pid=%d;rpid=%d;reloads=%d;timestamp=%d", msg ? msg : "", 
child->type, child->ipc_fd[0], child->pid, child->relative_pid, child->reloads, 
child->timestamp);
        }
        if (msg)
-               setenv("HAPROXY_CHILDREN", msg, 1);
+               setenv("HAPROXY_PROCESSES", msg, 1);
 }
 
 /*
@@ -556,7 +553,7 @@ static void mworker_env_to_proc_list()
 {
        char *msg, *token = NULL, *s1;
 
-       msg = getenv("HAPROXY_CHILDREN");
+       msg = getenv("HAPROXY_PROCESSES");
        if (!msg)
                return;
 
@@ -573,7 +570,11 @@ static void mworker_env_to_proc_list()
 
                        token = NULL;
 
-                       if (strncmp(subtoken, "fd=", 3) == 0) {
+                       if (strncmp(subtoken, "type=", 5) == 0) {
+                               child->type = *(subtoken+5);
+                               if (child->type == 'm') /* we are in the 
master, assign it */
+                                       proc_self = child;
+                       } else if (strncmp(subtoken, "fd=", 3) == 0) {
                                child->ipc_fd[0] = atoi(subtoken+3);
                        } else if (strncmp(subtoken, "pid=", 4) == 0) {
                                child->pid = atoi(subtoken+4);
@@ -592,7 +593,7 @@ static void mworker_env_to_proc_list()
                        free(child);
        }
 
-       unsetenv("HAPROXY_CHILDREN");
+       unsetenv("HAPROXY_PROCESSES");
 }
 
 /*
@@ -1735,9 +1736,29 @@ static void init(int argc, char **argv)
        if (global.mode & MODE_MWORKER) {
                int proc;
                struct wordlist *it, *c;
+               struct mworker_proc *tmproc;
+
+               if (getenv("HAPROXY_MWORKER_REEXEC") == NULL) {
+
+                       tmproc = malloc(sizeof(*tmproc));
+                       if (!tmproc) {
+                               ha_alert("Cannot allocate process 
structures.\n");
+                               exit(EXIT_FAILURE);
+                       }
+                       tmproc->type = 'm'; /* master */
+                       tmproc->reloads = 0;
+                       tmproc->relative_pid = 0;
+                       tmproc->pid = pid;
+                       tmproc->timestamp = start_date.tv_sec;
+                       tmproc->ipc_fd[0] = -1;
+                       tmproc->ipc_fd[1] = -1;
+
+                       proc_self = tmproc;
+
+                       LIST_ADDQ(&proc_list, &tmproc->list);
+               }
 
                for (proc = 0; proc < global.nbproc; proc++) {
-                       struct mworker_proc *tmproc;
 
                        tmproc = malloc(sizeof(*tmproc));
                        if (!tmproc) {
@@ -1745,6 +1766,7 @@ static void init(int argc, char **argv)
                                exit(EXIT_FAILURE);
                        }
 
+                       tmproc->type = 'w'; /* worker */
                        tmproc->pid = -1;
                        tmproc->reloads = 0;
                        tmproc->timestamp = -1;
-- 
2.18.1


Reply via email to