CLEANUP: peers/cli: take the "show peers" context definition out of the appctx

This makes use of the generic command context allocation so that the
appctx doesn't have to declare a specific one anymore. The context is
created during parsing. The code also uses st2 which deserves being
addressed in separate commit.
This commit is contained in:
Willy Tarreau 2022-05-03 14:26:31 +02:00
parent c7e9706e0f
commit cb8bf17900
2 changed files with 22 additions and 19 deletions

View File

@ -154,12 +154,6 @@ struct appctx {
struct task *task; struct task *task;
struct hlua_function *fcn; struct hlua_function *fcn;
} hlua_cli; } hlua_cli;
struct {
void *target;
struct peers *peers; /* "peers" section being currently dumped. */
struct peer *peer; /* "peer" being currently dumped. */
int flags; /* non-zero if "dict" dump requested */
} cfgpeers;
struct { struct {
char *path; char *path;
struct ckch_store *old_ckchs; struct ckch_store *old_ckchs;

View File

@ -3699,6 +3699,14 @@ int peers_register_table(struct peers *peers, struct stktable *table)
return retval; return retval;
} }
/* context used by a "show peers" command */
struct show_peers_ctx {
void *target;
struct peers *peers; /* "peers" section being currently dumped. */
struct peer *peer; /* "peer" being currently dumped. */
int flags; /* non-zero if "dict" dump requested */
};
/* /*
* Parse the "show peers" command arguments. * Parse the "show peers" command arguments.
* Returns 0 if succeeded, 1 if not with the ->msg of the appctx set as * Returns 0 if succeeded, 1 if not with the ->msg of the appctx set as
@ -3706,11 +3714,11 @@ int peers_register_table(struct peers *peers, struct stktable *table)
*/ */
static int cli_parse_show_peers(char **args, char *payload, struct appctx *appctx, void *private) static int cli_parse_show_peers(char **args, char *payload, struct appctx *appctx, void *private)
{ {
appctx->ctx.cfgpeers.target = NULL; struct show_peers_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
if (strcmp(args[2], "dict") == 0) { if (strcmp(args[2], "dict") == 0) {
/* show the dictionaries (large dump) */ /* show the dictionaries (large dump) */
appctx->ctx.cfgpeers.flags |= PEERS_SHOW_F_DICT; ctx->flags |= PEERS_SHOW_F_DICT;
args++; args++;
} else if (strcmp(args[2], "-") == 0) } else if (strcmp(args[2], "-") == 0)
args++; // allows to show a section called "dict" args++; // allows to show a section called "dict"
@ -3720,7 +3728,7 @@ static int cli_parse_show_peers(char **args, char *payload, struct appctx *appct
for (p = cfg_peers; p; p = p->next) { for (p = cfg_peers; p; p = p->next) {
if (strcmp(p->id, args[2]) == 0) { if (strcmp(p->id, args[2]) == 0) {
appctx->ctx.cfgpeers.target = p; ctx->target = p;
break; break;
} }
} }
@ -3928,12 +3936,13 @@ static int peers_dump_peer(struct buffer *msg, struct conn_stream *cs, struct pe
*/ */
static int cli_io_handler_show_peers(struct appctx *appctx) static int cli_io_handler_show_peers(struct appctx *appctx)
{ {
struct show_peers_ctx *ctx = appctx->svcctx;
int show_all; int show_all;
int ret = 0, first_peers = 1; int ret = 0, first_peers = 1;
thread_isolate(); thread_isolate();
show_all = !appctx->ctx.cfgpeers.target; show_all = !ctx->target;
chunk_reset(&trash); chunk_reset(&trash);
@ -3941,15 +3950,15 @@ static int cli_io_handler_show_peers(struct appctx *appctx)
switch (appctx->st2) { switch (appctx->st2) {
case STAT_ST_INIT: case STAT_ST_INIT:
if (show_all) if (show_all)
appctx->ctx.cfgpeers.peers = cfg_peers; ctx->peers = cfg_peers;
else else
appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.target; ctx->peers = ctx->target;
appctx->st2 = STAT_ST_LIST; appctx->st2 = STAT_ST_LIST;
/* fall through */ /* fall through */
case STAT_ST_LIST: case STAT_ST_LIST:
if (!appctx->ctx.cfgpeers.peers) { if (!ctx->peers) {
/* No more peers list. */ /* No more peers list. */
appctx->st2 = STAT_ST_END; appctx->st2 = STAT_ST_END;
} }
@ -3958,17 +3967,17 @@ static int cli_io_handler_show_peers(struct appctx *appctx)
chunk_appendf(&trash, "\n"); chunk_appendf(&trash, "\n");
else else
first_peers = 0; first_peers = 0;
if (!peers_dump_head(&trash, appctx->owner, appctx->ctx.cfgpeers.peers)) if (!peers_dump_head(&trash, appctx->owner, ctx->peers))
goto out; goto out;
appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peers->remote; ctx->peer = ctx->peers->remote;
appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.peers->next; ctx->peers = ctx->peers->next;
appctx->st2 = STAT_ST_INFO; appctx->st2 = STAT_ST_INFO;
} }
break; break;
case STAT_ST_INFO: case STAT_ST_INFO:
if (!appctx->ctx.cfgpeers.peer) { if (!ctx->peer) {
/* End of peer list */ /* End of peer list */
if (show_all) if (show_all)
appctx->st2 = STAT_ST_LIST; appctx->st2 = STAT_ST_LIST;
@ -3976,10 +3985,10 @@ static int cli_io_handler_show_peers(struct appctx *appctx)
appctx->st2 = STAT_ST_END; appctx->st2 = STAT_ST_END;
} }
else { else {
if (!peers_dump_peer(&trash, appctx->owner, appctx->ctx.cfgpeers.peer, appctx->ctx.cfgpeers.flags)) if (!peers_dump_peer(&trash, appctx->owner, ctx->peer, ctx->flags))
goto out; goto out;
appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peer->next; ctx->peer = ctx->peer->next;
} }
break; break;