]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: cli: define usermsgs print context
authorAmaury Denoyelle <adenoyelle@haproxy.com>
Thu, 10 Nov 2022 13:24:51 +0000 (14:24 +0100)
committerAmaury Denoyelle <adenoyelle@haproxy.com>
Thu, 10 Nov 2022 15:42:47 +0000 (16:42 +0100)
CLI 'add server' handler relies on usermsgs_ctx to display errors in
internal function on CLI output. This may be also extended to other
handlers.

However, to not clutter stderr from another contextes, usermsgs_ctx must
be resetted when it is not needed anymore. This operation cannot be
conducted in the CLI parse handler as display is conducted after it.

To achieve this, define new CLI states CLI_ST_PRINT_UMSG /
CLI_ST_PRINT_UMSGERR. Their principles is nearly identical to states for
dynamic messages printing.

include/haproxy/cli-t.h
include/haproxy/cli.h
src/cli.c

index 7570954b29fd8e3b06323adabb339498a5625115..5d2c079778fb5f32a35942f64f7ca625bb94a13c 100644 (file)
@@ -58,6 +58,8 @@ enum {
        CLI_ST_PRINT_ERR,  /* display const error in cli->msg */
        CLI_ST_PRINT_DYN,  /* display dynamic message in cli->err. After the display, free the pointer */
        CLI_ST_PRINT_DYNERR, /* display dynamic error in cli->err. After the display, free the pointer */
+       CLI_ST_PRINT_UMSG, /* display usermsgs_ctx buffer. After the display, usermsgs_ctx is resetted. */
+       CLI_ST_PRINT_UMSGERR, /* display usermsgs_ctx buffer as error. After the display, usermsgs_ctx is resetted. */
        CLI_ST_CALLBACK,   /* custom callback pointer */
 };
 
index bb5c23c9874b1dbe4f5d70fc94d67bf49587b9a8..6b049b855a62c3bb3bf00d2482ff65c5b6da3ad7 100644 (file)
@@ -108,5 +108,31 @@ static inline int cli_dynerr(struct appctx *appctx, char *err)
        return 1;
 }
 
+/* updates the CLI's context to log messages stored in thread-local
+ * usermsgs_ctx at <severity> level. usermsgs_ctx will be resetted when done.
+ * This is for use in CLI parsers to deal with quick response messages.
+ *
+ * Always returns 1.
+ */
+static inline int cli_umsg(struct appctx *appctx, int severity)
+{
+       struct cli_print_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
+
+       ctx->severity = severity;
+       appctx->st0 = CLI_ST_PRINT_UMSG;
+       return 1;
+}
+
+/* updates the CLI's context to log messages stored in thread-local
+ * usermsgs_ctx using error level. usermsgs_ctx will be resetted when done.
+ * This is for use in CLI parsers to deal with quick response messages.
+ *
+ * Always returns 1.
+ */
+static inline int cli_umsgerr(struct appctx *appctx)
+{
+       appctx->st0 = CLI_ST_PRINT_UMSGERR;
+       return 1;
+}
 
 #endif /* _HAPROXY_CLI_H */
index 406b850ce2d498efe091b65e705f14404fccab01..4e7ae14963d71d1564f6eeb8fe770dc04d4a5cd7 100644 (file)
--- a/src/cli.c
+++ b/src/cli.c
@@ -1042,6 +1042,8 @@ static void cli_io_handler(struct appctx *appctx)
                        case CLI_ST_PRINT_ERR:   /* print const error in msg */
                        case CLI_ST_PRINT_DYN:   /* print dyn message in msg, free */
                        case CLI_ST_PRINT_DYNERR: /* print dyn error in err, free */
+                       case CLI_ST_PRINT_UMSG:  /* print usermsgs_ctx and reset it */
+                       case CLI_ST_PRINT_UMSGERR: /* print usermsgs_ctx as error and reset it */
                                /* the message is in the svcctx */
                                ctx = applet_reserve_svcctx(appctx, sizeof(*ctx));
                                if (appctx->st0 == CLI_ST_PRINT || appctx->st0 == CLI_ST_PRINT_ERR) {
@@ -1058,6 +1060,12 @@ static void cli_io_handler(struct appctx *appctx)
                                                msg = "Out of memory.\n";
                                        }
                                }
+                               else if (appctx->st0 == CLI_ST_PRINT_UMSG ||
+                                        appctx->st0 == CLI_ST_PRINT_UMSGERR) {
+                                       sev = appctx->st0 == CLI_ST_PRINT_UMSGERR ?
+                                               LOG_ERR : ctx->severity;
+                                       msg = usermsgs_str();
+                               }
                                else {
                                        sev = LOG_ERR;
                                        msg = "Internal error.\n";
@@ -1068,6 +1076,10 @@ static void cli_io_handler(struct appctx *appctx)
                                            appctx->st0 == CLI_ST_PRINT_DYNERR) {
                                                ha_free(&ctx->err);
                                        }
+                                       else if (appctx->st0 == CLI_ST_PRINT_UMSG ||
+                                                appctx->st0 == CLI_ST_PRINT_UMSGERR) {
+                                               usermsgs_clr(NULL);
+                                       }
                                        appctx->st0 = CLI_ST_PROMPT;
                                }
                                else
@@ -1198,6 +1210,9 @@ static void cli_release_handler(struct appctx *appctx)
 
                ha_free(&ctx->err);
        }
+       else if (appctx->st0 == CLI_ST_PRINT_UMSG || appctx->st0 == CLI_ST_PRINT_UMSGERR) {
+               usermsgs_clr(NULL);
+       }
 }
 
 /* This function dumps all environmnent variables to the buffer. It returns 0