From cb8bf17900af14ff8b3e414dd274010663d51a56 Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Tue, 3 May 2022 14:26:31 +0200 Subject: [PATCH] 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. --- include/haproxy/applet-t.h | 6 ------ src/peers.c | 35 ++++++++++++++++++++++------------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/include/haproxy/applet-t.h b/include/haproxy/applet-t.h index 39338f858..8fe2bc2cd 100644 --- a/include/haproxy/applet-t.h +++ b/include/haproxy/applet-t.h @@ -154,12 +154,6 @@ struct appctx { struct task *task; struct hlua_function *fcn; } 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 { char *path; struct ckch_store *old_ckchs; diff --git a/src/peers.c b/src/peers.c index 522f2dbd8..a5180ed29 100644 --- a/src/peers.c +++ b/src/peers.c @@ -3699,6 +3699,14 @@ int peers_register_table(struct peers *peers, struct stktable *table) 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. * 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) { - appctx->ctx.cfgpeers.target = NULL; + struct show_peers_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); if (strcmp(args[2], "dict") == 0) { /* show the dictionaries (large dump) */ - appctx->ctx.cfgpeers.flags |= PEERS_SHOW_F_DICT; + ctx->flags |= PEERS_SHOW_F_DICT; args++; } else if (strcmp(args[2], "-") == 0) 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) { if (strcmp(p->id, args[2]) == 0) { - appctx->ctx.cfgpeers.target = p; + ctx->target = p; 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) { + struct show_peers_ctx *ctx = appctx->svcctx; int show_all; int ret = 0, first_peers = 1; thread_isolate(); - show_all = !appctx->ctx.cfgpeers.target; + show_all = !ctx->target; chunk_reset(&trash); @@ -3941,15 +3950,15 @@ static int cli_io_handler_show_peers(struct appctx *appctx) switch (appctx->st2) { case STAT_ST_INIT: if (show_all) - appctx->ctx.cfgpeers.peers = cfg_peers; + ctx->peers = cfg_peers; else - appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.target; + ctx->peers = ctx->target; appctx->st2 = STAT_ST_LIST; /* fall through */ case STAT_ST_LIST: - if (!appctx->ctx.cfgpeers.peers) { + if (!ctx->peers) { /* No more peers list. */ appctx->st2 = STAT_ST_END; } @@ -3958,17 +3967,17 @@ static int cli_io_handler_show_peers(struct appctx *appctx) chunk_appendf(&trash, "\n"); else 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; - appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peers->remote; - appctx->ctx.cfgpeers.peers = appctx->ctx.cfgpeers.peers->next; + ctx->peer = ctx->peers->remote; + ctx->peers = ctx->peers->next; appctx->st2 = STAT_ST_INFO; } break; case STAT_ST_INFO: - if (!appctx->ctx.cfgpeers.peer) { + if (!ctx->peer) { /* End of peer list */ if (show_all) appctx->st2 = STAT_ST_LIST; @@ -3976,10 +3985,10 @@ static int cli_io_handler_show_peers(struct appctx *appctx) appctx->st2 = STAT_ST_END; } 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; - appctx->ctx.cfgpeers.peer = appctx->ctx.cfgpeers.peer->next; + ctx->peer = ctx->peer->next; } break;