]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
REORG: init: encapsulate 'reload' sockpair and master CLI listeners creation
authorValentine Krasnobaeva <vkrasnobaeva@haproxy.com>
Wed, 26 Jun 2024 14:21:50 +0000 (16:21 +0200)
committerWilliam Lallemand <wlallemand@haproxy.com>
Thu, 27 Jun 2024 14:08:42 +0000 (16:08 +0200)
Let's encapsulate the logic of 'reload' sockpair and master CLI listeners
creation, used by master CLI into a separate function, as we needed this
only in master-worker runtime  mode. This makes the code of init() more
readable.

include/haproxy/mworker.h
src/haproxy.c
src/mworker.c

index c9dd8406ff763ded231861201706b4378a76f473..0251a9de27795e39534c23afcf745382da550819 100644 (file)
@@ -18,6 +18,8 @@
 #include <haproxy/signal-t.h>
 
 extern struct mworker_proc *proc_self;
+/* master CLI configuration (-S flag) */
+extern struct list mworker_cli_conf;
 
 void mworker_proc_list_to_env(void);
 int mworker_env_to_proc_list(void);
@@ -45,4 +47,6 @@ struct mworker_proc *mworker_proc_new();
 void mworker_free_child(struct mworker_proc *);
 void mworker_cleanup_proc();
 
+void mworker_create_master_cli(void);
+
 #endif /* _HAPROXY_MWORKER_H_ */
index e6712576bc30e48d985294d8e6eb8f5b91a68d61..5443ed382f0b18d3f80a9f5f3deb7e17bda7fac7 100644 (file)
@@ -287,9 +287,6 @@ int check_kw_experimental(struct cfg_keyword *kw, const char *file, int linenum,
        return 0;
 }
 
-/* master CLI configuration (-S flag) */
-struct list mworker_cli_conf = LIST_HEAD_INIT(mworker_cli_conf);
-
 /* These are strings to be reported in the output of "haproxy -vv". They may
  * either be constants (in which case must_free must be zero) or dynamically
  * allocated strings to pass to free() on exit, and in this case must_free
@@ -2261,58 +2258,8 @@ static void init(int argc, char **argv)
                global.nbthread = 1;
        }
 
-       if (global.mode & (MODE_MWORKER|MODE_MWORKER_WAIT)) {
-               struct wordlist *it, *c;
-
-               master = 1;
-               /* get the info of the children in the env */
-               if (mworker_env_to_proc_list() < 0) {
-                       exit(EXIT_FAILURE);
-               }
-
-               if (!LIST_ISEMPTY(&mworker_cli_conf)) {
-                       char *path = NULL;
-
-                       if (mworker_cli_proxy_create() < 0) {
-                               ha_alert("Can't create the master's CLI.\n");
-                               exit(EXIT_FAILURE);
-                       }
-
-                       list_for_each_entry_safe(c, it, &mworker_cli_conf, list) {
-
-                               if (mworker_cli_proxy_new_listener(c->s) == NULL) {
-                                       ha_alert("Can't create the master's CLI.\n");
-                                       exit(EXIT_FAILURE);
-                               }
-                               LIST_DELETE(&c->list);
-                               free(c->s);
-                               free(c);
-                       }
-                       /* Creates the mcli_reload listener, which is the listener used
-                        * to retrieve the master CLI session which asked for the reload.
-                        *
-                        * ipc_fd[1] will be used as a listener, and ipc_fd[0]
-                        * will be used to send the FD of the session.
-                        *
-                        * Both FDs will be kept in the master. The sockets are
-                        * created only if they weren't inherited.
-                        */
-                       if ((proc_self->ipc_fd[1] == -1) &&
-                            socketpair(AF_UNIX, SOCK_STREAM, 0, proc_self->ipc_fd) < 0) {
-                               ha_alert("cannot create the mcli_reload socketpair.\n");
-                               exit(EXIT_FAILURE);
-                       }
-
-                       /* Create the mcli_reload listener from the proc_self struct */
-                       memprintf(&path, "sockpair@%d", proc_self->ipc_fd[1]);
-                       mcli_reload_bind_conf = mworker_cli_proxy_new_listener(path);
-                       if (mcli_reload_bind_conf == NULL) {
-                               ha_alert("Cannot create the mcli_reload listener.\n");
-                               exit(EXIT_FAILURE);
-                       }
-                       ha_free(&path);
-               }
-       }
+       if (global.mode & (MODE_MWORKER|MODE_MWORKER_WAIT))
+               mworker_create_master_cli();
 
        if (!LIST_ISEMPTY(&mworker_cli_conf) && !(arg_mode & MODE_MWORKER)) {
                ha_alert("a master CLI socket was defined, but master-worker mode (-W) is not enabled.\n");
index c4461cc10e8398069a6f04475438e28084c798ec..a5b55d24d15c21aaf3b8c0c25927023b16797b86 100644 (file)
@@ -48,6 +48,7 @@
 static int exitcode = -1;
 static int max_reloads = -1; /* number max of reloads a worker can have until they are killed */
 struct mworker_proc *proc_self = NULL; /* process structure of current process */
+struct list mworker_cli_conf = LIST_HEAD_INIT(mworker_cli_conf); /* master CLI configuration (-S flag) */
 
 /* ----- children processes handling ----- */
 
@@ -787,6 +788,60 @@ void mworker_free_child(struct mworker_proc *child)
        free(child);
 }
 
+/* Creates and binds dedicated master CLI 'reload' sockpair and listeners */
+void mworker_create_master_cli(void)
+{
+       struct wordlist *it, *c;
+
+       /* get the info of the children in the env */
+       if (mworker_env_to_proc_list() < 0) {
+               exit(EXIT_FAILURE);
+       }
+
+       if (!LIST_ISEMPTY(&mworker_cli_conf)) {
+               char *path = NULL;
+
+               if (mworker_cli_proxy_create() < 0) {
+                       ha_alert("Can't create the master's CLI.\n");
+                       exit(EXIT_FAILURE);
+               }
+
+               list_for_each_entry_safe(c, it, &mworker_cli_conf, list) {
+
+                       if (mworker_cli_proxy_new_listener(c->s) == NULL) {
+                               ha_alert("Can't create the master's CLI.\n");
+                               exit(EXIT_FAILURE);
+                       }
+                       LIST_DELETE(&c->list);
+                       free(c->s);
+                       free(c);
+               }
+               /* Creates the mcli_reload listener, which is the listener used
+                * to retrieve the master CLI session which asked for the reload.
+                *
+                * ipc_fd[1] will be used as a listener, and ipc_fd[0]
+                * will be used to send the FD of the session.
+                *
+                * Both FDs will be kept in the master. The sockets are
+                * created only if they weren't inherited.
+                */
+               if ((proc_self->ipc_fd[1] == -1) &&
+                    socketpair(AF_UNIX, SOCK_STREAM, 0, proc_self->ipc_fd) < 0) {
+                       ha_alert("Can't create the mcli_reload socketpair.\n");
+                       exit(EXIT_FAILURE);
+               }
+
+               /* Create the mcli_reload listener from the proc_self struct */
+               memprintf(&path, "sockpair@%d", proc_self->ipc_fd[1]);
+               mcli_reload_bind_conf = mworker_cli_proxy_new_listener(path);
+               if (mcli_reload_bind_conf == NULL) {
+                       ha_alert("Can't create the mcli_reload listener.\n");
+                       exit(EXIT_FAILURE);
+               }
+               ha_free(&path);
+       }
+}
+
 static struct cfg_kw_list mworker_kws = {{ }, {
        { CFG_GLOBAL, "mworker-max-reloads", mworker_parse_global_max_reloads },
        { 0, NULL, NULL },