CLEANUP: ring: pass the ring watch flags to ring_attach_cli(), not in ctx.cli

The ring watch flags (wait, seek end) were dangerously passed via ctx.cli.i0
from "show buf" in sink.c:cli_parse_show_events(), or implicitly reset in
"show errors". That's very unconvenient, difficult to follow, and prone to
short-term breakage.

Let's pass an extra argument to ring_attach_cli() to take these flags, now
defined in ring-t.h as RING_WF_*, and let the function set them itself
where appropriate (still ctx.cli.i0 for now).
This commit is contained in:
Willy Tarreau 2022-05-05 15:18:57 +02:00
parent 40e952f1a6
commit cba8838e59
5 changed files with 17 additions and 10 deletions

View File

@ -92,6 +92,10 @@
* removed
*/
/* ring watch flags to be used when watching the ring */
#define RING_WF_WAIT_MODE 0x00000001 /* wait for new contents */
#define RING_WF_SEEK_NEW 0x00000002 /* seek to new contents */
struct ring {
struct buffer buf; // storage area
size_t ofs; // absolute offset in history of the buffer's head

View File

@ -33,7 +33,7 @@ void ring_free(struct ring *ring);
ssize_t ring_write(struct ring *ring, size_t maxlen, const struct ist pfx[], size_t npfx, const struct ist msg[], size_t nmsg);
int ring_attach(struct ring *ring);
void ring_detach_appctx(struct ring *ring, struct appctx *appctx, size_t ofs);
int ring_attach_cli(struct ring *ring, struct appctx *appctx);
int ring_attach_cli(struct ring *ring, struct appctx *appctx, uint flags);
int cli_io_handler_show_ring(struct appctx *appctx);
void cli_io_release_show_ring(struct appctx *appctx);

View File

@ -356,7 +356,7 @@ static int cli_parse_show_startup_logs(char **args, char *payload, struct appctx
if (!startup_logs)
return cli_msg(appctx, LOG_INFO, "\n"); // nothing to print
return ring_attach_cli(startup_logs, appctx);
return ring_attach_cli(startup_logs, appctx, 0);
}
/* register cli keywords */

View File

@ -248,9 +248,9 @@ void ring_detach_appctx(struct ring *ring, struct appctx *appctx, size_t ofs)
* meant to be used when registering a CLI function to dump a buffer, so it
* returns zero on success, or non-zero on failure with a message in the appctx
* CLI context. It automatically sets the io_handler and io_release callbacks if
* they were not set.
* they were not set. The <flags> take a combination of RING_WF_*.
*/
int ring_attach_cli(struct ring *ring, struct appctx *appctx)
int ring_attach_cli(struct ring *ring, struct appctx *appctx, uint flags)
{
if (!ring_attach(ring))
return cli_err(appctx,
@ -263,6 +263,7 @@ int ring_attach_cli(struct ring *ring, struct appctx *appctx)
appctx->io_release = cli_io_release_show_ring;
appctx->ctx.cli.p0 = ring;
appctx->ctx.cli.o0 = ~0; // start from the oldest event
appctx->ctx.cli.i0 = flags;
return 0;
}
@ -306,7 +307,7 @@ int cli_io_handler_show_ring(struct appctx *appctx)
ofs = 0;
/* going to the end means looking at tail-1 */
if (appctx->ctx.cli.i0 & 2)
if (appctx->ctx.cli.i0 & RING_WF_SEEK_NEW)
ofs += b_data(buf) - 1;
HA_ATOMIC_INC(b_peek(buf, ofs));
@ -357,7 +358,7 @@ int cli_io_handler_show_ring(struct appctx *appctx)
appctx->ctx.cli.o0 = ofs;
HA_RWLOCK_RDUNLOCK(LOGSRV_LOCK, &ring->lock);
if (ret && (appctx->ctx.cli.i0 & 1)) {
if (ret && (appctx->ctx.cli.i0 & RING_WF_WAIT_MODE)) {
/* we've drained everything and are configured to wait for more
* data or an event (keypress, close)
*/

View File

@ -234,6 +234,7 @@ int sink_announce_dropped(struct sink *sink, int facility)
static int cli_parse_show_events(char **args, char *payload, struct appctx *appctx, void *private)
{
struct sink *sink;
uint ring_flags;
int arg;
args++; // make args[1] the 1st arg
@ -264,17 +265,18 @@ static int cli_parse_show_events(char **args, char *payload, struct appctx *appc
if (sink->type != SINK_TYPE_BUFFER)
return cli_msg(appctx, LOG_NOTICE, "Nothing to report for this sink");
ring_flags = 0;
for (arg = 2; *args[arg]; arg++) {
if (strcmp(args[arg], "-w") == 0)
appctx->ctx.cli.i0 |= 1; // wait mode
ring_flags |= RING_WF_WAIT_MODE;
else if (strcmp(args[arg], "-n") == 0)
appctx->ctx.cli.i0 |= 2; // seek to new
ring_flags |= RING_WF_SEEK_NEW;
else if (strcmp(args[arg], "-nw") == 0 || strcmp(args[arg], "-wn") == 0)
appctx->ctx.cli.i0 |= 3; // seek to new + wait
ring_flags |= RING_WF_WAIT_MODE | RING_WF_SEEK_NEW;
else
return cli_err(appctx, "unknown option");
}
return ring_attach_cli(sink->ctx.ring, appctx);
return ring_attach_cli(sink->ctx.ring, appctx, ring_flags);
}
/* Pre-configures a ring proxy to emit connections */