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.
This commit is contained in:
Valentine Krasnobaeva 2024-06-26 16:21:50 +02:00 committed by William Lallemand
parent 6f613faa71
commit 5e06d45df7
3 changed files with 61 additions and 55 deletions

View File

@ -18,6 +18,8 @@
#include <haproxy/signal-t.h> #include <haproxy/signal-t.h>
extern struct mworker_proc *proc_self; extern struct mworker_proc *proc_self;
/* master CLI configuration (-S flag) */
extern struct list mworker_cli_conf;
void mworker_proc_list_to_env(void); void mworker_proc_list_to_env(void);
int mworker_env_to_proc_list(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_free_child(struct mworker_proc *);
void mworker_cleanup_proc(); void mworker_cleanup_proc();
void mworker_create_master_cli(void);
#endif /* _HAPROXY_MWORKER_H_ */ #endif /* _HAPROXY_MWORKER_H_ */

View File

@ -287,9 +287,6 @@ int check_kw_experimental(struct cfg_keyword *kw, const char *file, int linenum,
return 0; 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 /* 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 * 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 * 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; global.nbthread = 1;
} }
if (global.mode & (MODE_MWORKER|MODE_MWORKER_WAIT)) { if (global.mode & (MODE_MWORKER|MODE_MWORKER_WAIT))
struct wordlist *it, *c; mworker_create_master_cli();
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 (!LIST_ISEMPTY(&mworker_cli_conf) && !(arg_mode & MODE_MWORKER)) { 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"); ha_alert("a master CLI socket was defined, but master-worker mode (-W) is not enabled.\n");

View File

@ -48,6 +48,7 @@
static int exitcode = -1; static int exitcode = -1;
static int max_reloads = -1; /* number max of reloads a worker can have until they are killed */ 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 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 ----- */ /* ----- children processes handling ----- */
@ -787,6 +788,60 @@ void mworker_free_child(struct mworker_proc *child)
free(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 = {{ }, { static struct cfg_kw_list mworker_kws = {{ }, {
{ CFG_GLOBAL, "mworker-max-reloads", mworker_parse_global_max_reloads }, { CFG_GLOBAL, "mworker-max-reloads", mworker_parse_global_max_reloads },
{ 0, NULL, NULL }, { 0, NULL, NULL },