Allow admin to (un)set logging
authorJosé Bollo <jose.bollo@iot.bzh>
Thu, 13 Jun 2019 19:17:46 +0000 (21:17 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Thu, 13 Jun 2019 19:17:46 +0000 (21:17 +0200)
Change-Id: I5479c9e35512ab77a005692903733f7d1af627a3
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
src/main-cynadm.c
src/rcyn-client.c
src/rcyn-client.h
src/rcyn-protocol.c
src/rcyn-protocol.h
src/rcyn-protocol.txt
src/rcyn-server.c

index 8a5c492..59253f1 100644 (file)
@@ -29,6 +29,7 @@
 #include "rcyn-protocol.h"
 #include "expire.h"
 
+
 #define _HELP_        'h'
 #define _SOCKET_      's'
 #define _VERSION_     'v'
@@ -74,7 +75,7 @@ static
 const char
 help__text[] =
        "\n"
-       "Commands are: list, set, drop, check, test, cache, clear, quit, help\n"
+       "Commands are: list, set, drop, check, test, cache, clear, quit, log, help\n"
        "Type 'help command' to get help on the command\n"
        "Type 'help expiration' to get help on expirations\n"
        "\n"
@@ -179,6 +180,21 @@ help_test_text[] =
        "\n"
 ;
 
+static
+const char
+help_log_text[] =
+       "\n"
+       "Command: log [on|off]\n"
+       "\n"
+       "With the 'on' or 'off' arguments, set the logging state to what required.\n"
+       "In all cases, prints the logging state.\n"
+       "\n"
+       "Examples:\n"
+       "\n"
+       "  log on                  activates the logging\n"
+       "\n"
+;
+
 static
 const char
 help_cache_text[] =
@@ -483,6 +499,28 @@ int do_check(int ac, char **av, int (*f)(rcyn_t*,const rcyn_key_t*))
        return uc;
 }
 
+int do_log(int ac, char **av)
+{
+       int uc, rc;
+       int on = 0, off = 0;
+       int n = plink(ac, av, &uc, 2);
+
+       if (n > 1) {
+               on = !strcmp(av[1], "on");
+               off = !strcmp(av[1], "off");
+               if (!on && !off) {
+                       fprintf(stderr, "bad argument '%s'\n", av[1]);
+                       return uc;
+               }
+       }
+       rc = rcyn_log(rcyn, on, off);
+       if (rc < 0)
+               fprintf(stderr, "error %s\n", strerror(-rc));
+       else
+               fprintf(stdout, "logging %s\n", rc ? "on" : "off");
+       return uc;
+}
+
 int do_help(int ac, char **av)
 {
        if (ac > 1 && !strcmp(av[1], "list"))
@@ -499,6 +537,8 @@ int do_help(int ac, char **av)
                fprintf(stdout, "%s", help_cache_text);
        else if (ac > 1 && !strcmp(av[1], "clear"))
                fprintf(stdout, "%s", help_clear_text);
+       else if (ac > 1 && !strcmp(av[1], "log"))
+               fprintf(stdout, "%s", help_log_text);
        else if (ac > 1 && !strcmp(av[1], "quit"))
                fprintf(stdout, "%s", help_quit_text);
        else if (ac > 1 && !strcmp(av[1], "help"))
@@ -535,6 +575,9 @@ int do_any(int ac, char **av)
        if (!strcmp(av[0], "cache"))
                return do_check(ac, av, rcyn_cache_check);
 
+       if (!strcmp(av[0], "log"))
+               return do_log(ac, av);
+
        if (!strcmp(av[0], "clear")) {
                rcyn_cache_clear(rcyn);
                return 1;
index 575d72c..769c6c0 100644 (file)
@@ -195,48 +195,6 @@ putxkv(
        }
 }
 
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
 static
 int
 wait_input(
@@ -678,6 +636,31 @@ rcyn_get(
        return rc;
 }
 
+int
+rcyn_log(
+       rcyn_t *rcyn,
+       int on,
+       int off
+) {
+       int rc;
+
+       if (rcyn->type != rcyn_Admin)
+               return -EPERM;
+       if (rcyn->async.requests != NULL)
+               return -EINPROGRESS;
+
+       rc = ensure_opened(rcyn);
+       if (rc < 0)
+               return rc;
+
+       rc = putxkv(rcyn, _log_, off ? _off_ : on ? _on_ : 0, 0, 0);
+       if (rc >= 0)
+               rc = wait_done(rcyn);
+
+       return rc < 0 ? rc : rcyn->reply.count < 2 ? 0 : !strcmp(rcyn->reply.fields[1], _on_);
+}
+
+
 int
 rcyn_drop(
        rcyn_t *rcyn,
index ad93689..f0fffe9 100644 (file)
@@ -110,6 +110,14 @@ rcyn_get(
        void *closure
 );
 
+extern
+int
+rcyn_log(
+       rcyn_t *rcyn,
+       int on,
+       int off
+);
+
 extern
 int
 rcyn_drop(
index e3071e9..d570b04 100644 (file)
@@ -26,6 +26,7 @@ const char
        _enter_[] = "enter",
        _get_[] = "get",
        _leave_[] = "leave",
+       _log_[] = "log",
        _rcyn_[] = "rcyn",
        _set_[] = "set",
        _test_[] = "test";
@@ -40,6 +41,8 @@ const char
        _error_[] = "error",
        _item_[] = "item",
        _no_[] = "no",
+       _on_[] = "on",
+       _off_[] = "off",
        _yes_[] = "yes";
 
 #if !defined(RCYN_DEFAULT_SOCKET_SCHEME)
index c408dac..8640186 100644 (file)
@@ -24,6 +24,7 @@ extern const char
        _enter_[],
        _get_[],
        _leave_[],
+       _log_[],
        _rcyn_[],
        _set_[],
        _test_[];
@@ -38,6 +39,8 @@ extern const char
        _error_[],
        _item_[],
        _no_[],
+       _off_[],
+       _on_[],
        _yes_[];
 
 extern const char
index 72789a2..582a912 100644 (file)
@@ -47,6 +47,11 @@ leave critical (admin)
   c->s leave [commit|rollback]
   s->c done|error ...
 
+set/get logging status (admin)
+
+  c->s log [on|off]
+  s->c done (on|off)
+
 register agent (agent):
 
   c->s agent NAME [ARGS...]
index 6974265..b062c89 100644 (file)
@@ -110,11 +110,11 @@ dolog(
        const char *fields[]
 ) {
        static const char types[3][6] = { "check", "agent", "admin" };
-       static const char dir[2] = { '<', '>' };
+       static const char dir[2] = { '>', '<' };
 
        int i;
 
-       fprintf(stderr, "%p.%s %c%c", cli, types[cli->type], dir[!c2s], dir[!c2s]);
+       fprintf(stderr, "%p%c%c%s", cli, dir[!c2s], dir[!c2s], types[cli->type]);
        for (i = 0 ; i < count ; i++)
                fprintf(stderr, " %s", fields[i]);
        fprintf(stderr, "\n");
@@ -317,6 +317,7 @@ onrequest(
        int count,
        const char *args[]
 ) {
+       bool nextlog;
        int rc;
        data_key_t key;
        data_value_t value;
@@ -412,6 +413,20 @@ onrequest(
                        send_done_or_error(cli, rc);
                        return;
                }
+               if (ckarg(args[0], _log_, 1) && count <= 2) {
+                       nextlog = rcyn_server_log;
+                       if (cli->type != rcyn_Admin)
+                               break;
+                       if (count == 2) {
+                               if (!ckarg(args[1], _on_, 0) && !ckarg(args[1], _off_, 0))
+                                       break;
+                               nextlog = ckarg(args[1], _on_, 0);
+                       }
+                       putx(cli, _done_, nextlog ? _on_ : _off_, NULL);
+                       flushw(cli);
+                       rcyn_server_log = nextlog;
+                       return;
+               }
                break;
        case 's': /* set */
                if (ckarg(args[0], _set_, 1) && (count == 6 || count == 7)) {