From: Valentine Krasnobaeva Date: Wed, 23 Oct 2024 15:29:10 +0000 (+0200) Subject: MINOR: mworker/cli: split mworker_cli_proxy_create X-Git-Tag: v3.1-dev11~64 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ddb829bb51b5e53d7b9fe3439ac5c8d5163dde2e;p=thirdparty%2Fhaproxy.git MINOR: mworker/cli: split mworker_cli_proxy_create There are two parts in mworker_cli_proxy_create(): allocating and setting up MASTER proxy and allocating and setting up servers on ipc_fd[0] of the sockpairs shared with workers. So, let's split mworker_cli_proxy_create() into two functions respectively. Each of them takes **errmsg as an argument to write an error message, which may be triggered by some subcalls. The content of this errmsg will allow to extend the final alert message shown to user, if these new functions will fail. The main goals of this split is to allow to move these two parts independantly in future and makes the code of haproxy initialization in haproxy.c more transparent. --- diff --git a/include/haproxy/cli.h b/include/haproxy/cli.h index 17a8c6557c..383a531825 100644 --- a/include/haproxy/cli.h +++ b/include/haproxy/cli.h @@ -40,8 +40,8 @@ int cli_has_level(struct appctx *appctx, int level); int cli_parse_default(char **args, char *payload, struct appctx *appctx, void *private); /* mworker proxy functions */ - -int mworker_cli_proxy_create(void); +int mworker_cli_create_master_proxy(char **errmsg); +int mworker_cli_attach_server(char **errmsg); struct bind_conf *mworker_cli_master_proxy_new_listener(char *line); int mworker_cli_global_proxy_new_listener(struct mworker_proc *proc); void mworker_cli_proxy_stop(void); diff --git a/src/cli.c b/src/cli.c index 68ce8176de..10456382d1 100644 --- a/src/cli.c +++ b/src/cli.c @@ -3338,29 +3338,47 @@ void mworker_cli_proxy_stop() } /* - * Create the mworker CLI proxy + * Create the MASTER proxy */ -int mworker_cli_proxy_create() +int mworker_cli_create_master_proxy(char **errmsg) { - struct mworker_proc *child; - char *msg = NULL; - char *errmsg = NULL; - - mworker_proxy = alloc_new_proxy("MASTER", PR_CAP_LISTEN|PR_CAP_INT, &errmsg); - if (!mworker_proxy) - goto error_proxy; + mworker_proxy = alloc_new_proxy("MASTER", PR_CAP_LISTEN|PR_CAP_INT, errmsg); + if (!mworker_proxy) { + return -1; + } mworker_proxy->mode = PR_MODE_CLI; - mworker_proxy->maxconn = 10; /* default to 10 concurrent connections */ - mworker_proxy->timeout.client = 0; /* no timeout */ - mworker_proxy->conf.file = copy_file_name("MASTER"); + /* default to 10 concurrent connections */ + mworker_proxy->maxconn = 10; + /* no timeout */ + mworker_proxy->timeout.client = 0; + mworker_proxy->conf.file = strdup("MASTER"); mworker_proxy->conf.line = 0; mworker_proxy->accept = frontend_accept; - mworker_proxy-> lbprm.algo = BE_LB_ALGO_NONE; + mworker_proxy->lbprm.algo = BE_LB_ALGO_NONE; /* Does not init the default target the CLI applet, but must be done in * the request parsing code */ mworker_proxy->default_target = NULL; + mworker_proxy->next = proxies_list; + proxies_list = mworker_proxy; + + return 0; +} + +/* + * Attach servers to ipc_fd[0] of all presented in proc_list workers. Master and + * worker share MCLI sockpair (ipc_fd[0] and ipc_fd[1]). Servers are attached to + * ipc_fd[0], which is always opened at master side. ipc_fd[0] of worker, started + * before the reload, is inherited in master after the reload (execvp). + */ +int mworker_cli_attach_server(char **errmsg) +{ + char *msg = NULL; + struct mworker_proc *child; + + BUG_ON((mworker_proxy == NULL), "Triggered in mworker_cli_attach_server(), " + "mworker_proxy must be created before this call.\n"); /* create all servers using the mworker_proc list */ list_for_each_entry(child, &proc_list, list) { @@ -3377,8 +3395,7 @@ int mworker_cli_proxy_create() if (!newsrv) goto error; - /* we don't know the new pid yet */ - if (child->pid == -1) + if (child->options & PROC_O_INIT) memprintf(&msg, "cur-%d", 1); else memprintf(&msg, "old-%d", child->pid); @@ -3391,7 +3408,7 @@ int mworker_cli_proxy_create() memprintf(&msg, "sockpair@%d", child->ipc_fd[0]); if ((sk = str2sa_range(msg, &port, &port1, &port2, NULL, &proto, NULL, - &errmsg, NULL, NULL, NULL, PA_O_STREAM)) == 0) { + errmsg, NULL, NULL, NULL, PA_O_STREAM)) == 0) { goto error; } ha_free(&msg); @@ -3411,9 +3428,6 @@ int mworker_cli_proxy_create() child->srv = newsrv; } - mworker_proxy->next = proxies_list; - proxies_list = mworker_proxy; - return 0; error: @@ -3423,13 +3437,8 @@ error: free(child->srv->id); ha_free(&child->srv); } - free_proxy(mworker_proxy); free(msg); -error_proxy: - ha_alert("%s\n", errmsg); - free(errmsg); - return -1; } diff --git a/src/haproxy.c b/src/haproxy.c index 2058d7ca63..2a75ecc98a 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -2133,6 +2133,7 @@ static void apply_master_worker_mode() int worker_pid; struct mworker_proc *child; char *sock_name = NULL; + char *errmsg = NULL; worker_pid = fork(); switch (worker_pid) { @@ -2175,9 +2176,17 @@ static void apply_master_worker_mode() global.nbtgroups = 1; global.nbthread = 1; - /* creates MASTER proxy and attaches server to child->ipc_fd[0] */ - if (mworker_cli_proxy_create() < 0) { - ha_alert("Can't create the master's CLI.\n"); + /* creates MASTER proxy */ + if (mworker_cli_create_master_proxy(&errmsg) < 0) { + ha_alert("Can't create MASTER proxy: %s\n", errmsg); + free(errmsg); + exit(EXIT_FAILURE); + } + + /* attaches servers to all existed workers on its shared MCLI sockpair ends, ipc_fd[0] */ + if (mworker_cli_attach_server(&errmsg) < 0) { + ha_alert("Can't attach servers needed for master CLI %s\n", errmsg ? errmsg : ""); + free(errmsg); exit(EXIT_FAILURE); } @@ -3021,6 +3030,7 @@ static void set_verbosity(void) { static void run_master_in_recovery_mode(int argc, char **argv) { struct mworker_proc *proc; + char *errmsg = NULL; /* HAPROXY_LOAD_SUCCESS is checked in cli_io_handler_show_cli_sock() to * dump master startup logs with its alerts/warnings via master CLI sock. @@ -3044,9 +3054,17 @@ static void run_master_in_recovery_mode(int argc, char **argv) atexit(exit_on_failure); set_verbosity(); - /* creates MASTER proxy and attaches server to child->ipc_fd[0] */ - if (mworker_cli_proxy_create() < 0) { - ha_alert("Can't create the master's CLI.\n"); + /* creates MASTER proxy */ + if (mworker_cli_create_master_proxy(&errmsg) < 0) { + ha_alert("Can't create MASTER proxy: %s\n", errmsg); + free(errmsg); + exit(EXIT_FAILURE); + } + + /* attaches servers to all existed workers on its shared MCLI sockpair ends, ipc_fd[0] */ + if (mworker_cli_attach_server(&errmsg) < 0) { + ha_alert("Can't attach servers needed for master CLI %s\n", errmsg ? errmsg : ""); + free(errmsg); exit(EXIT_FAILURE); }