aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJosé Bollo <jose.bollo@iot.bzh>2019-06-13 13:32:26 +0200
committerJosé Bollo <jose.bollo@iot.bzh>2019-06-13 13:32:26 +0200
commitffd3be0e695228fc75ab582d78959f052787d44a (patch)
treefee43a05d9310864f4d03898b6f36caddeecc446
parent2f9e8eae28057d8c17373227f4bc375847f0cbd1 (diff)
Add log option for tracking protocol
Change-Id: Ifb780ac4133732a4218fa3018b9ecec9f3b67718 Signed-off-by: José Bollo <jose.bollo@iot.bzh>
-rw-r--r--src/main-cynarad.c15
-rw-r--r--src/rcyn-server.c38
-rw-r--r--src/rcyn-server.h4
3 files changed, 52 insertions, 5 deletions
diff --git a/src/main-cynarad.c b/src/main-cynarad.c
index 19f0f4d..772fed6 100644
--- a/src/main-cynarad.c
+++ b/src/main-cynarad.c
@@ -61,6 +61,7 @@
#define _GROUP_ 'g'
#define _HELP_ 'h'
#define _INIT_ 'i'
+#define _LOG_ 'l'
#define _MAKEDBDIR_ 'm'
#define _MAKESOCKDIR_ 'M'
#define _OWNSOCKDIR_ 'O'
@@ -72,7 +73,7 @@
static
const char
-shortopts[] = "d:g:hi:mMOoS:u:v"
+shortopts[] = "d:g:hi:lmMOoS:u:v"
#if defined(WITH_SYSTEMD_ACTIVATION)
"s"
#endif
@@ -85,6 +86,7 @@ longopts[] = {
{ "group", 1, NULL, _GROUP_ },
{ "help", 0, NULL, _HELP_ },
{ "init", 1, NULL, _INIT_ },
+ { "log", 0, NULL, _LOG_ },
{ "make-db-dir", 0, NULL, _MAKEDBDIR_ },
{ "make-socket-dir", 0, NULL, _MAKESOCKDIR_ },
{ "own-db-dir", 0, NULL, _OWNDBDIR_ },
@@ -112,6 +114,7 @@ helptxt[] =
" -g, --group xxx set the group\n"
" -i, --init xxx initialize if needed the database with file xxx\n"
" (default: "DEFAULT_INIT_FILE"\n"
+ " -l, --log activate log of transactions\n"
" -d, --dbdir xxx set the directory of database\n"
" (default: "DEFAULT_DB_DIR")\n"
" -m, --make-db-dir make the database directory\n"
@@ -144,6 +147,7 @@ int main(int ac, char **av)
int makedbdir = 0;
int owndbdir = 0;
int ownsockdir = 0;
+ int flog = 0;
int help = 0;
int version = 0;
int error = 0;
@@ -180,6 +184,9 @@ int main(int ac, char **av)
case _INIT_:
init = optarg;
break;
+ case _LOG_:
+ flog = 1;
+ break;
case _MAKEDBDIR_:
makedbdir = 1;
break;
@@ -315,7 +322,7 @@ int main(int ac, char **av)
if (db_is_empty()) {
rc = dbinit_add_file(init);
if (rc < 0) {
- fprintf(stderr, "can't initialise database: %m\n");
+ fprintf(stderr, "can't initialize database: %m\n");
return 1;
}
}
@@ -324,10 +331,12 @@ int main(int ac, char **av)
cyn_changeid_reset();
/* initialize server */
+ setvbuf(stderr, NULL, _IOLBF, 1000);
+ rcyn_server_log = flog;
signal(SIGPIPE, SIG_IGN); /* avoid SIGPIPE! */
rc = rcyn_server_create(&server, spec_socket_admin, spec_socket_check, spec_socket_agent);
if (rc < 0) {
- fprintf(stderr, "can't initialise server: %m\n");
+ fprintf(stderr, "can't initialize server: %m\n");
return 1;
}
diff --git a/src/rcyn-server.c b/src/rcyn-server.c
index 8f14fdb..d23c4fb 100644
--- a/src/rcyn-server.c
+++ b/src/rcyn-server.c
@@ -39,13 +39,17 @@
#include "socket.h"
#include "pollitem.h"
+/** should log? */
+int
+rcyn_server_log = 0;
+
+/** local enumeration of socket/client kind */
typedef enum rcyn_type {
rcyn_Check,
rcyn_Agent,
rcyn_Admin
} rcyn_type_t;
-
/** structure that represents a rcyn client */
struct client
{
@@ -53,7 +57,7 @@ struct client
prot_t *prot;
/** type of client */
- rcyn_type_t type;
+ unsigned type: 2;
/** the version of the protocol used */
unsigned version: 4;
@@ -97,6 +101,25 @@ struct rcyn_server
pollitem_t check;
};
+static
+void
+dolog(
+ client_t *cli,
+ int c2s,
+ int count,
+ const char *fields[]
+) {
+ static const char types[3][6] = { "check", "agent", "admin" };
+ static const char dir[2] = { '<', '>' };
+
+ int i;
+
+ fprintf(stderr, "%p.%s %c%c", cli, types[cli->type], dir[!c2s], dir[!c2s]);
+ for (i = 0 ; i < count ; i++)
+ fprintf(stderr, " %s", fields[i]);
+ fprintf(stderr, "\n");
+}
+
/**
* Check 'arg' against 'value' beginning at offset accepting it if 'arg' prefixes 'value'
* Returns 1 if matching or 0 if not.
@@ -159,6 +182,7 @@ putx(
va_list l;
int rc;
+ /* store temporary in fields */
n = 0;
va_start(l, cli);
p = va_arg(l, const char *);
@@ -169,6 +193,12 @@ putx(
p = va_arg(l, const char *);
}
va_end(l);
+
+ /* emit the log */
+ if (rcyn_server_log)
+ dolog(cli, 0, n, fields);
+
+ /* send now */
rc = prot_put(cli->prot, n, fields);
if (rc == -ECANCELED) {
rc = flushw(cli);
@@ -295,6 +325,10 @@ onrequest(
if (count == 0)
return;
+ /* emit the log */
+ if (rcyn_server_log)
+ dolog(cli, 1, count, args);
+
/* version hand-shake */
if (!cli->version) {
if (!ckarg(args[0], _rcyn_, 0) || count != 2 || !ckarg(args[1], "1", 0))
diff --git a/src/rcyn-server.h b/src/rcyn-server.h
index 5758070..b702b73 100644
--- a/src/rcyn-server.h
+++ b/src/rcyn-server.h
@@ -22,6 +22,10 @@ struct rcyn_server;
typedef struct rcyn_server rcyn_server_t;
extern
+int
+rcyn_server_log;
+
+extern
void
rcyn_server_destroy(
rcyn_server_t *server