From: Amaury Denoyelle Date: Thu, 22 May 2025 15:07:59 +0000 (+0200) Subject: MINOR: server: define CLI I/O handler for "add server" X-Git-Tag: v3.2.0~34 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2570892c41e0acd71a82278ff01e145376aed90b;p=thirdparty%2Fhaproxy.git MINOR: server: define CLI I/O handler for "add server" Extend "add server" to support an IO handler function named cli_io_handler_add_server(). A context object is also defined whose usage will depend on IO handler capabilities. IO handler is skipped when "add server" is run in default mode, i.e. on a dynamic server creation. Thus, currently IO handler is unneeded. However, it will become useful to support sub-commands for "add server". Note that return value of "add server" parser has been changed on server creation success. Previously, it was used incorrectly to report if server was inserted or not. In fact, parser return value is used by CLI generic code to detect if command processing has been completed, or should continue to the IO handler. Now, "add server" always returns 1 to signal that CLI processing is completed. This is necessary to preserve CLI output emitted by parser, even now that IO handler is defined for the command. Previously, output was emitted in every situations due to IO handler not defined. See below code snippet from cli.c for a better overview : if (kw->parse && kw->parse(args, payload, appctx, kw->private) != 0) { ret = 1; goto fail; } /* kw->parse could set its own io_handler or io_release handler */ if (!appctx->cli_ctx.io_handler) { ret = 1; goto fail; } appctx->st0 = CLI_ST_CALLBACK; ret = 1; goto end; --- diff --git a/src/server.c b/src/server.c index 526abb623..27c5b44bf 100644 --- a/src/server.c +++ b/src/server.c @@ -5813,11 +5813,40 @@ int srv_init_per_thr(struct server *srv) return 0; } -/* Parse a "add server" command - * Returns 0 if the server has been successfully initialized, 1 on failure. +/* Distinguish between "add server" default usage or one of its sub-commands. */ +enum add_srv_mode { + ADD_SRV_MODE_DEF, /* default mode, IO handler should be skipped by parser. */ +}; + +/* Context for "add server" CLI. */ +struct add_srv_ctx { + enum add_srv_mode mode; + void *obj1; + void *obj2; +}; + +/* Handler for "add server" command. Should be reserved to extra sub-commands. */ +int cli_io_handler_add_server(struct appctx *appctx) +{ + struct add_srv_ctx *ctx = appctx->svcctx; + + switch (ctx->mode) { + case ADD_SRV_MODE_DEF: + /* Add srv parser must return 1 to prevent I/O handler execution in default mode. */ + ABORT_NOW(); + break; + } + + return 1; +} + +/* Parse a "add server" command. + * + * Returns 1 to skip I/O handler processing, unless a sub-command is executed. */ static int cli_parse_add_server(char **args, char *payload, struct appctx *appctx, void *private) { + struct add_srv_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); struct proxy *be; struct server *srv; char *be_name, *sv_name; @@ -5832,6 +5861,7 @@ static int cli_parse_add_server(char **args, char *payload, struct appctx *appct ++args; + ctx->mode = ADD_SRV_MODE_DEF; sv_name = be_name = args[1]; /* split backend/server arg */ while (*sv_name && *(++sv_name)) { @@ -6063,9 +6093,7 @@ static int cli_parse_add_server(char **args, char *payload, struct appctx *appct ha_warning("Ignoring cookie as HTTP mode is disabled.\n"); ha_notice("New server registered.\n"); - cli_umsg(appctx, LOG_INFO); - - return 0; + return cli_umsg(appctx, LOG_INFO); out: if (srv) { @@ -6336,7 +6364,7 @@ static struct cli_kw_list cli_kws = {{ },{ { { "set", "server", NULL }, "set server / [opts] : change a server's state, weight, address or ssl", cli_parse_set_server }, { { "get", "weight", NULL }, "get weight / : report a server's current weight", cli_parse_get_weight }, { { "set", "weight", NULL }, "set weight / (DEPRECATED) : change a server's weight (use 'set server' instead)", cli_parse_set_weight }, - { { "add", "server", NULL }, "add server / : create a new server", cli_parse_add_server, NULL }, + { { "add", "server", NULL }, "add server / : create a new server", cli_parse_add_server, cli_io_handler_add_server }, { { "del", "server", NULL }, "del server / : remove a dynamically added server", cli_parse_delete_server, NULL }, {{},} }};