]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
CLEANUP: ring: pass the ring watch flags to ring_attach_cli(), not in ctx.cli
authorWilly Tarreau <w@1wt.eu>
Thu, 5 May 2022 13:18:57 +0000 (15:18 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 6 May 2022 16:13:36 +0000 (18:13 +0200)
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).

include/haproxy/ring-t.h
include/haproxy/ring.h
src/errors.c
src/ring.c
src/sink.c

index a379265cff9832d280a084705f26a44dc7f8f163..7e4d5188aa1dbc2ebec26a0a770969faa30ba611 100644 (file)
  *                 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
index c400f915d7106b5062f8b64e809f8d63d640c120..a3a55d8fb18fc7a736155c984acd664a3586c1e8 100644 (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);
 
index 109ec0aaaee829333f7d73f6225902e4dd3045cc..bd3c271f42c33d159612f490871383b7fd6bc1a1 100644 (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 */
index bfbeed998fff72ca7689bfe683906d15546064a9..0314233044720a86e3b46fcdb83244e9ad235b8d 100644 (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)
                 */
index 65a218d635808a801caf619a58efa806f59507bd..9f0bfb8bf8d1ff3dd1eee03d2b0d8d394a9eb697 100644 (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 */