]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: cli: add private pointer and release function
authorThierry FOURNIER / OZON.IO <thierry.fournier@ozon.io>
Sat, 12 Nov 2016 09:51:33 +0000 (10:51 +0100)
committerWilly Tarreau <w@1wt.eu>
Fri, 18 Nov 2016 13:32:03 +0000 (14:32 +0100)
This pointer will be used for storing private context. With this,
the same executed function can handle more than one keyword. This
will be very useful for creation Lua cli bindings.

The release function is called when the command is terminated (give
back the hand to the prompt) or when the session is broken (timeout
or client closed).

include/proto/applet.h
include/proto/dumpstats.h
include/types/applet.h
src/dumpstats.c

index b256816b55119629e7b3575906282f12a8d83420..81c20e11aaec2353417efa0bafa9c77e11c1be2d 100644 (file)
@@ -40,6 +40,7 @@ void applet_run_active();
 static inline void appctx_init(struct appctx *appctx)
 {
        appctx->st0 = appctx->st1 = appctx->st2 = 0;
+       appctx->io_release = NULL;
 }
 
 /* Tries to allocate a new appctx and initialize its main fields. The appctx
index db60bab83bcdee91fba9363d679684177e258d58..12f972c6f8fbfcae81fdc6a90428d3794b431ed9 100644 (file)
@@ -413,8 +413,10 @@ struct cli_kw {
        const char *str_kw[5];   /* keywords ended by NULL, limited to 5
                                 separated keywords combination */
        const char *usage;   /* usage message */
-       int (*parse)(char **args, struct appctx *appctx);
+       int (*parse)(char **args, struct appctx *appctx, void *private);
        int (*io_handler)(struct appctx *appctx);
+       void (*io_release)(struct appctx *appctx);
+       void *private;
 };
 
 struct cli_kw_list {
index 1f094e6ba7d354dd18322eb1fceb5a0834f2194e..e60b08a3db8f90a89b462e4c69d760f764fdb547 100644 (file)
@@ -55,6 +55,8 @@ struct appctx {
        void *owner;               /* pointer to upper layer's entity (eg: stream interface) */
        struct act_rule *rule;     /* rule associated with the applet. */
        int (*io_handler)(struct appctx *appctx);  /* used within the cli_io_handler when st0 = STAT_CLI_O_CUSTOM */
+       void (*io_release)(struct appctx *appctx);  /* used within the cli_io_handler when st0 = STAT_CLI_O_CUSTOM,
+                                                      if the command is terminated or the session released */
        void *private;
 
        union {
index 491b0fe67f8ca86a5e2b4bb922ba249de07136de..5cc09aa22de93bdb50d648d10e0e7d22e97d716c 100644 (file)
@@ -1306,9 +1306,10 @@ static int stats_sock_parse_request(struct stream_interface *si, char *line)
        appctx->ctx.stats.flags = 0;
        if ((kw = cli_find_kw(args))) {
                if (kw->parse) {
-                       if (kw->parse(args, appctx) == 0 && kw->io_handler) {
+                       if (kw->parse(args, appctx, kw->private) == 0 && kw->io_handler) {
                                appctx->st0 = STAT_CLI_O_CUSTOM;
                                appctx->io_handler = kw->io_handler;
+                               appctx->io_release = kw->io_release;
                        }
                }
        } else if (strcmp(args[0], "show") == 0) {
@@ -2844,8 +2845,13 @@ static void cli_io_handler(struct appctx *appctx)
                                break;
                        case STAT_CLI_O_CUSTOM: /* use custom pointer */
                                if (appctx->io_handler)
-                                       if (appctx->io_handler(appctx))
+                                       if (appctx->io_handler(appctx)) {
                                                appctx->st0 = STAT_CLI_PROMPT;
+                                               if (appctx->io_release) {
+                                                       appctx->io_release(appctx);
+                                                       appctx->io_release = NULL;
+                                               }
+                                       }
                                break;
                        default: /* abnormal state */
                                si->flags |= SI_FL_ERR;
@@ -6752,6 +6758,10 @@ static int stats_dump_sess_to_buffer(struct stream_interface *si)
  */
 static void cli_release_handler(struct appctx *appctx)
 {
+       if (appctx->io_release) {
+               appctx->io_release(appctx);
+               appctx->io_release = NULL;
+       }
        if (appctx->st0 == STAT_CLI_O_SESS && appctx->st2 == STAT_ST_LIST) {
                if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
                        LIST_DEL(&appctx->ctx.sess.bref.users);