summaryrefslogtreecommitdiffstats
path: root/runtime
diff options
context:
space:
mode:
Diffstat (limited to 'runtime')
-rw-r--r--runtime/msg.c57
-rw-r--r--runtime/rsyslog.h1
-rw-r--r--runtime/ruleset.c22
3 files changed, 67 insertions, 13 deletions
diff --git a/runtime/msg.c b/runtime/msg.c
index f1f7997c..15eb7abb 100644
--- a/runtime/msg.c
+++ b/runtime/msg.c
@@ -2420,7 +2420,6 @@ getCEEPropVal(msg_t *pM, es_str_t *propName, uchar **pRes, int *buflen, unsigned
if(*pbMustBeFreed)
free(*pRes);
*pRes = NULL;
-dbgprintf("AAAA: enter getCEEPropVal\n");
// TODO: mutex?
if(pM->json == NULL) goto finalize_it;
@@ -2428,9 +2427,7 @@ dbgprintf("AAAA: enter getCEEPropVal\n");
field = pM->json;
} else {
name = (uchar*)es_str2cstr(propName, NULL);
-dbgprintf("AAAA: name to search '%s'\n", name);
leaf = jsonPathGetLeaf(name, ustrlen(name));
-dbgprintf("AAAA: leaf '%s'\n", leaf);
CHKiRet(jsonPathFindParent(pM, name, leaf, &parent, 1));
field = json_object_object_get(parent, (char*)leaf);
}
@@ -2439,7 +2436,6 @@ dbgprintf("AAAA: leaf '%s'\n", leaf);
*pbMustBeFreed = 0;
} else {
*pRes = (uchar*) strdup(json_object_get_string(field));
-dbgprintf("AAAA: json_object_get_string() returns '%s'\n", *pRes);
*buflen = (int) ustrlen(*pRes);
*pbMustBeFreed = 1;
}
@@ -2465,7 +2461,6 @@ msgGetCEEPropJSON(msg_t *pM, es_str_t *propName, struct json_object **pjson)
struct json_object *parent;
DEFiRet;
-dbgprintf("AAAA: enter getCEEPropJSON\n");
// TODO: mutex?
if(pM->json == NULL) {
ABORT_FINALIZE(RS_RET_NOT_FOUND);
@@ -2476,9 +2471,7 @@ dbgprintf("AAAA: enter getCEEPropJSON\n");
FINALIZE;
}
name = (uchar*)es_str2cstr(propName, NULL);
-dbgprintf("AAAA: name to search '%s'\n", name);
leaf = jsonPathGetLeaf(name, ustrlen(name));
-dbgprintf("AAAA: leaf '%s'\n", leaf);
CHKiRet(jsonPathFindParent(pM, name, leaf, &parent, 1));
*pjson = json_object_object_get(parent, (char*)leaf);
if(*pjson == NULL) {
@@ -3685,9 +3678,9 @@ jsonPathFindNext(struct json_object *root, uchar **name, uchar *leaf,
++p;
for(i = 0 ; *p && *p != '!' && p != leaf && i < sizeof(namebuf)-1 ; ++i, ++p)
namebuf[i] = *p;
- if(i == 0) {
+ if(i > 0) {
namebuf[i] = '\0';
- dbgprintf("AAAA: next JSONP elt: '%s'\n", namebuf);
+ dbgprintf("AAAA: next JSONPath elt: '%s'\n", namebuf);
json = json_object_object_get(root, (char*)namebuf);
} else
json = root;
@@ -3713,7 +3706,6 @@ jsonPathFindParent(msg_t *pM, uchar *name, uchar *leaf, struct json_object **par
*parent = pM->json;
while(name < leaf-1) {
jsonPathFindNext(*parent, &name, leaf, parent, bCreate);
-dbgprintf("AAAA: name %p, leaf %p\n", name, leaf);
}
RETiRet;
}
@@ -3726,7 +3718,7 @@ jsonMerge(struct json_object *existing, struct json_object *json)
struct json_object_iter it;
json_object_object_foreachC(json, it) {
-dbgprintf("AAAA jsonMerge adds '%s'\n", it.key);
+DBGPRINTF("AAAA jsonMerge adds '%s'\n", it.key);
json_object_object_add(existing, it.key,
json_object_get(it.val));
}
@@ -3762,8 +3754,23 @@ msgAddJSON(msg_t *pM, uchar *name, struct json_object *json)
leafnode = json_object_object_get(parent, (char*)leaf);
if(leafnode == NULL)
json_object_object_add(parent, (char*)leaf, json);
- else
- CHKiRet(jsonMerge(pM->json, json));
+ else {
+ if(json_object_get_type(json) == json_type_object) {
+ CHKiRet(jsonMerge(pM->json, json));
+ } else {
+//dbgprintf("AAAA: leafnode already exists, type is %d, update with %d\n", (int)json_object_get_type(leafnode), (int)json_object_get_type(json));
+ /* TODO: improve the code below, however, the current
+ * state is not really bad */
+ if(json_object_get_type(leafnode) == json_type_object) {
+ DBGPRINTF("msgAddJSON: trying to update a container "
+ "node with a leaf, name is '%s' - "
+ "forbidden\n", name);
+ ABORT_FINALIZE(RS_RET_INVLD_SETOP);
+ }
+ json_object_object_del(parent, (char*)leaf);
+ json_object_object_add(parent, (char*)leaf, json);
+ }
+ }
}
finalize_it:
@@ -3771,6 +3778,30 @@ finalize_it:
RETiRet;
}
+rsRetVal
+msgSetJSONFromVar(msg_t *pMsg, uchar *varname, struct var *var)
+{
+ struct json_object *json = NULL;
+ char *cstr;
+ DEFiRet;
+ switch(var->datatype) {
+ case 'S':/* string */
+ cstr = es_str2cstr(var->d.estr, NULL);
+ json = json_object_new_string(cstr);
+ free(cstr);
+ break;
+ case 'N':/* number (integer) */
+ json = json_object_new_int((int) var->d.n);
+ break;
+ default:DBGPRINTF("msgSetJSONFromVar: unsupported datatype %c\n",
+ var->datatype);
+ ABORT_FINALIZE(RS_RET_ERR);
+ }
+ msgAddJSON(pMsg, varname+1, json);
+finalize_it:
+ RETiRet;
+}
+
/* dummy */
rsRetVal msgQueryInterface(void) { return RS_RET_NOT_IMPLEMENTED; }
diff --git a/runtime/rsyslog.h b/runtime/rsyslog.h
index fe9bb4cc..2977f6f3 100644
--- a/runtime/rsyslog.h
+++ b/runtime/rsyslog.h
@@ -387,6 +387,7 @@ enum rsRetVal_ /** return value. All methods return this if not specified oth
RS_RET_JNAME_INVALID = -2302, /**< JSON path is invalid */
RS_RET_JSON_PARSE_ERR = -2303, /**< we had a problem parsing JSON (or extra data) */
RS_RET_BSD_BLOCKS_UNSUPPORTED = -2304, /**< BSD-style config blocks are no longer supported */
+ RS_RET_INVLD_SETOP = -2305, /**< invalid variable set operation, incompatible type */
/* RainerScript error messages (range 1000.. 1999) */
RS_RET_SYSVAR_NOT_FOUND = 1001, /**< system variable could not be found (maybe misspelled) */
diff --git a/runtime/ruleset.c b/runtime/ruleset.c
index faec122c..392473e9 100644
--- a/runtime/ruleset.c
+++ b/runtime/ruleset.c
@@ -206,6 +206,7 @@ static inline sbool *newActive(batch_t *pBatch)
}
static inline void freeActive(sbool *active) { free(active); }
+
/* for details, see scriptExec() header comment! */
/* call action for all messages with filter on */
static rsRetVal
@@ -219,6 +220,24 @@ dbgprintf("RRRR: execAct: batch of %d elements, active %p\n", batchNumMsgs(pBatc
}
/* for details, see scriptExec() header comment! */
+static rsRetVal
+execSet(struct cnfstmt *stmt, batch_t *pBatch, sbool *active)
+{
+ int i;
+ struct var result;
+ DEFiRet;
+ for(i = 0 ; i < batchNumMsgs(pBatch) && !*(pBatch->pbShutdownImmediate) ; ++i) {
+ if( pBatch->pElem[i].state != BATCH_STATE_DISC
+ && (active == NULL || active[i])) {
+ cnfexprEval(stmt->d.s_set.expr, &result, pBatch->pElem[i].pUsrp);
+ msgSetJSONFromVar(pBatch->pElem[i].pUsrp, stmt->d.s_set.varname,
+ &result);
+ }
+ }
+ RETiRet;
+}
+
+/* for details, see scriptExec() header comment! */
/* "stop" simply discards the filtered items - it's just a (hopefully more intuitive
* shortcut for users.
*/
@@ -452,6 +471,9 @@ dbgprintf("RRRR: scriptExec: batch of %d elements, active %p, stmt %p, nodetype
case S_IF:
execIf(stmt, pBatch, active);
break;
+ case S_SET:
+ execSet(stmt, pBatch, active);
+ break;
case S_PRIFILT:
execPRIFILT(stmt, pBatch, active);
break;