From 5e06d45df72a20674edc7b7c3c41ee661a5b33b6 Mon Sep 17 00:00:00 2001 From: Valentine Krasnobaeva Date: Wed, 26 Jun 2024 16:21:50 +0200 Subject: [PATCH] REORG: init: encapsulate 'reload' sockpair and master CLI listeners creation 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 | 4 +++ src/haproxy.c | 57 ++------------------------------------- src/mworker.c | 55 +++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 55 deletions(-) diff --git a/include/haproxy/mworker.h b/include/haproxy/mworker.h index c9dd8406f..0251a9de2 100644 --- a/include/haproxy/mworker.h +++ b/include/haproxy/mworker.h @@ -18,6 +18,8 @@ #include 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_ */ diff --git a/src/haproxy.c b/src/haproxy.c index e6712576b..5443ed382 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -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"); diff --git a/src/mworker.c b/src/mworker.c index c4461cc10..a5b55d24d 100644 --- a/src/mworker.c +++ b/src/mworker.c @@ -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 },