]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: mworker/cli: split mworker_cli_proxy_create
authorValentine Krasnobaeva <vkrasnobaeva@haproxy.com>
Wed, 23 Oct 2024 15:29:10 +0000 (17:29 +0200)
committerWilliam Lallemand <wlallemand@haproxy.com>
Thu, 24 Oct 2024 09:32:20 +0000 (11:32 +0200)
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.

include/haproxy/cli.h
src/cli.c
src/haproxy.c

index 17a8c6557c70bc715af05db1e7cf1e0cd2502b15..383a531825aecc5769b16d6f57b5e7fa0de192d9 100644 (file)
@@ -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);
index 68ce8176def5ad320aace4b980fd863740af73d6..10456382d167cf0e756fcf3bedd8030ecfedb3a8 100644 (file)
--- 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;
 }
 
index 2058d7ca63e77f07e8a6228be35e88cb0081b957..2a75ecc98a5686c9914247d34ac3b759d5e8d6fc 100644 (file)
@@ -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);
        }