From f4f2a493b05b5d3247994769325598655afe7fe0 Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Mon, 21 Oct 2013 17:12:07 +0200 Subject: work a bit toward string handling unification --- template.c | 80 ++++++++++++++++++++++++++------------------------------------ 1 file changed, 34 insertions(+), 46 deletions(-) (limited to 'template.c') diff --git a/template.c b/template.c index 65de7547..92728607 100644 --- a/template.c +++ b/template.c @@ -169,7 +169,7 @@ tplToString(struct template *pTpl, msg_t *pMsg, uchar **ppBuf, size_t *pLenBuf, * not worth the effort, as this passing mode is not expected * in subtree mode and so most probably only used for debug & test. */ - getCEEPropVal(pMsg, pTpl->subtree, &pVal, &iLenVal, &bMustBeFreed); + getCEEPropVal(pMsg, pTpl->subtree, pTpl->subtreeLen, &pVal, &iLenVal, &bMustBeFreed); if(iLenVal >= (rs_size_t)*pLenBuf) /* we reserve one char for the final \0! */ CHKiRet(ExtendBuf(ppBuf, pLenBuf, iLenVal + 1)); memcpy(*ppBuf, pVal, iLenVal+1); @@ -194,8 +194,8 @@ tplToString(struct template *pTpl, msg_t *pMsg, uchar **ppBuf, size_t *pLenBuf, bMustBeFreed = 0; } else if(pTpe->eEntryType == FIELD) { pVal = (uchar*) MsgGetProp(pMsg, pTpe, pTpe->data.field.propid, - pTpe->data.field.propName, &iLenVal, - &bMustBeFreed, ttNow); + pTpe->data.field.propName, pTpe->data.field.propNameLen, + &iLenVal, &bMustBeFreed, ttNow); /* we now need to check if we should use SQL option. In this case, * we must go over the generated string and escape '\'' characters. * rgerhards, 2005-09-22: the option values below look somewhat misplaced, @@ -269,7 +269,7 @@ tplToArray(struct template *pTpl, msg_t *pMsg, uchar*** ppArr, struct syslogTime * using array passing, so I simply could not test it. */ CHKmalloc(pArr = calloc(2, sizeof(uchar*))); - getCEEPropVal(pMsg, pTpl->subtree, &pVal, &propLen, &bMustBeFreed); + getCEEPropVal(pMsg, pTpl->subtree, pTpl->subtreeLen, &pVal, &propLen, &bMustBeFreed); if(bMustBeFreed) { /* if it must be freed, it is our own private copy... */ pArr[0] = pVal; /* ... so we can use it! */ } else { @@ -291,8 +291,8 @@ tplToArray(struct template *pTpl, msg_t *pMsg, uchar*** ppArr, struct syslogTime CHKmalloc(pArr[iArr] = (uchar*)strdup((char*) pTpe->data.constant.pConstant)); } else if(pTpe->eEntryType == FIELD) { pVal = (uchar*) MsgGetProp(pMsg, pTpe, pTpe->data.field.propid, - pTpe->data.field.propName, &propLen, - &bMustBeFreed, ttNow); + pTpe->data.field.propName, pTpe->data.field.propNameLen, + &propLen, &bMustBeFreed, ttNow); if(bMustBeFreed) { /* if it must be freed, it is our own private copy... */ pArr[iArr] = pVal; /* ... so we can use it! */ } else { @@ -327,7 +327,7 @@ tplToJSON(struct template *pTpl, msg_t *pMsg, struct json_object **pjson, struct DEFiRet; if(pTpl->subtree != NULL){ - localRet = jsonFind(pMsg->json, pTpl->subtree, pjson); + localRet = jsonFind(pMsg->json, pTpl->subtree, pTpl->subtreeLen, pjson); if(*pjson == NULL) { /* we need to have a root object! */ *pjson = json_object_new_object(); @@ -346,7 +346,7 @@ tplToJSON(struct template *pTpl, msg_t *pMsg, struct json_object **pjson, struct json_object_object_add(json, (char*)pTpe->fieldName, jsonf); } else if(pTpe->eEntryType == FIELD) { if(pTpe->data.field.propid == PROP_CEE) { - localRet = msgGetCEEPropJSON(pMsg, pTpe->data.field.propName, &jsonf); + localRet = msgGetCEEPropJSON(pMsg, pTpe->data.field.propName, pTpe->data.field.propNameLen, &jsonf); if(localRet == RS_RET_OK) { json_object_object_add(json, (char*)pTpe->fieldName, json_object_get(jsonf)); } else { @@ -357,7 +357,7 @@ tplToJSON(struct template *pTpl, msg_t *pMsg, struct json_object **pjson, struct } } } else if(pTpe->data.field.propid == PROP_LOCAL_VAR) { - localRet = msgGetLocalVarJSON(pMsg, pTpe->data.field.propName, &jsonf); + localRet = msgGetLocalVarJSON(pMsg, pTpe->data.field.propName, pTpe->data.field.propNameLen, &jsonf); if(localRet == RS_RET_OK) { json_object_object_add(json, (char*)pTpe->fieldName, json_object_get(jsonf)); } else { @@ -368,7 +368,7 @@ tplToJSON(struct template *pTpl, msg_t *pMsg, struct json_object **pjson, struct } } } else if(pTpe->data.field.propid == PROP_GLOBAL_VAR) { - localRet = msgGetGlobalVarJSON(pTpe->data.field.propName, &jsonf); + localRet = msgGetGlobalVarJSON(pTpe->data.field.propName, pTpe->data.field.propNameLen, &jsonf); if(localRet == RS_RET_OK) { json_object_object_add(json, (char*)pTpe->fieldName, json_object_get(jsonf)); } else { @@ -380,7 +380,8 @@ tplToJSON(struct template *pTpl, msg_t *pMsg, struct json_object **pjson, struct } } else { pVal = (uchar*) MsgGetProp(pMsg, pTpe, pTpe->data.field.propid, - pTpe->data.field.propName, &propLen, + pTpe->data.field.propName, + pTpe->data.field.propNameLen, &propLen, &bMustBeFreed, ttNow); if(pTpe->data.field.options.bMandatory || propLen > 0) { jsonf = json_object_new_string_len((char*)pVal, propLen); @@ -557,6 +558,7 @@ tplConstruct(rsconf_t *conf) struct template *pTpl; if((pTpl = calloc(1, sizeof(struct template))) == NULL) return NULL; +pTpl->subtree = NULL; /* basic initialisation is done via calloc() - need to * initialize only values != 0. */ @@ -815,17 +817,14 @@ do_Parameter(uchar **pp, struct template *pTpl) } if(pTpe->data.field.propid == PROP_CEE) { /* in CEE case, we need to preserve the actual property name */ - if((pTpe->data.field.propName = es_newStrFromCStr((char*)cstrGetSzStrNoNULL(pStrProp)+1, cstrLen(pStrProp)-1)) == NULL) { - cstrDestruct(&pStrProp); - ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); - } + pTpe->data.field.propName = ustrdup(cstrGetSzStrNoNULL(pStrProp)+1); + pTpe->data.field.propNameLen = cstrLen(pStrProp)-1; } else if(pTpe->data.field.propid == PROP_LOCAL_VAR || pTpe->data.field.propid == PROP_GLOBAL_VAR) { /* in these cases, we need to preserve the actual property name, but correct the root ID (bang vs. dot) */ - if((pTpe->data.field.propName = es_newStrFromCStr((char*)cstrGetSzStrNoNULL(pStrProp)+1, cstrLen(pStrProp)-1)) == NULL) { - cstrDestruct(&pStrProp); - ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); - } - es_getBufAddr(pTpe->data.field.propName)[0] = '!'; /* patch root name */ + pTpe->data.field.propName = ustrdup(cstrGetSzStrNoNULL(pStrProp)+1); + pTpe->data.field.propNameLen = cstrLen(pStrProp)-1; + pTpe->data.field.propName[0] = '!'; /* patch root name */ +#warning do we really need to patch root name? } /* Check frompos, if it has an R, then topos should be a regex */ @@ -1608,12 +1607,13 @@ createPropertyTpe(struct template *pTpl, struct cnfobj *o) CHKiRet(propNameToID(name, &pTpe->data.field.propid)); if(pTpe->data.field.propid == PROP_CEE) { /* in CEE case, we need to preserve the actual property name */ - pTpe->data.field.propName = es_newStrFromCStr((char*)cstrGetSzStrNoNULL(name)+1, - cstrLen(name)-1); + pTpe->data.field.propName = ustrdup(cstrGetSzStrNoNULL(name)+1); + pTpe->data.field.propNameLen = cstrLen(name)-1; } else if(pTpe->data.field.propid == PROP_LOCAL_VAR || pTpe->data.field.propid == PROP_GLOBAL_VAR) { /* in these case, we need to preserve the actual property name, but correct the root ID (bang vs. dot) */ - pTpe->data.field.propName = es_newStrFromCStr((char*)cstrGetSzStrNoNULL(name)+1, cstrLen(name)-1); - es_getBufAddr(pTpe->data.field.propName)[0] = '!'; /* patch root name */ + pTpe->data.field.propName = ustrdup(cstrGetSzStrNoNULL(name)+1); + pTpe->data.field.propNameLen = cstrLen(name)-1; + pTpe->data.field.propName[0] = '!'; /* patch root name */ } pTpe->data.field.options.bDropLastLF = droplastlf; pTpe->data.field.options.bSPIffNo1stSP = spifno1stsp; @@ -1747,7 +1747,7 @@ tplProcessCnf(struct cnfobj *o) char *name = NULL; uchar *tplStr = NULL; uchar *plugin = NULL; - es_str_t *subtree = NULL; + uchar *subtree = NULL; uchar *p; enum { T_STRING, T_PLUGIN, T_LIST, T_SUBTREE } tplType = T_STRING; /* init just to keep compiler happy: mandatory parameter */ @@ -1795,10 +1795,7 @@ tplProcessCnf(struct cnfobj *o) free(name); /* overall assigned */ ABORT_FINALIZE(RS_RET_ERR); } else { - /* TODO: unify strings! */ - char *cstr = es_str2cstr(pvals[i].val.d.estr, NULL); - subtree = es_newStrFromBuf(cstr+1, es_strlen(pvals[i].val.d.estr)-1); - free(cstr); + subtree = (uchar*)es_str2cstr(pvals[i].val.d.estr, NULL); } } else if(!strcmp(pblk.descr[i].name, "plugin")) { plugin = (uchar*) es_str2cstr(pvals[i].val.d.estr, NULL); @@ -1912,6 +1909,7 @@ tplProcessCnf(struct cnfobj *o) case T_LIST: createListTpl(pTpl, o); break; case T_SUBTREE: pTpl->subtree = subtree; + pTpl->subtreeLen = ustrlen(subtree); break; } @@ -2003,8 +2001,7 @@ void tplDeleteAll(rsconf_t *conf) regexp.regfree(&(pTpeDel->data.field.re)); } } - if(pTpeDel->data.field.propName != NULL) - es_deleteStr(pTpeDel->data.field.propName); + free(pTpeDel->data.field.propName); #endif break; } @@ -2015,8 +2012,7 @@ void tplDeleteAll(rsconf_t *conf) pTplDel = pTpl; pTpl = pTpl->pNext; free(pTplDel->pszName); - if(pTplDel->subtree != NULL) - es_deleteStr(pTplDel->subtree); + free(pTplDel->subtree); free(pTplDel); } ENDfunc @@ -2063,8 +2059,7 @@ void tplDeleteNew(rsconf_t *conf) regexp.regfree(&(pTpeDel->data.field.re)); } } - if(pTpeDel->data.field.propName != NULL) - es_deleteStr(pTpeDel->data.field.propName); + free(pTpeDel->data.field.propName); #endif break; } @@ -2074,8 +2069,7 @@ void tplDeleteNew(rsconf_t *conf) pTplDel = pTpl; pTpl = pTpl->pNext; free(pTplDel->pszName); - if(pTplDel->subtree != NULL) - es_deleteStr(pTplDel->subtree); + free(pTplDel->subtree); free(pTplDel); } ENDfunc @@ -2119,17 +2113,11 @@ void tplPrintList(rsconf_t *conf) case FIELD: dbgprintf("(FIELD), value: '%d' ", pTpe->data.field.propid); if(pTpe->data.field.propid == PROP_CEE) { - char *cstr = es_str2cstr(pTpe->data.field.propName, NULL); - dbgprintf("[EE-Property: '%s'] ", cstr); - free(cstr); + dbgprintf("[EE-Property: '%s'] ", pTpe->data.field.propName); } else if(pTpe->data.field.propid == PROP_LOCAL_VAR) { - char *cstr = es_str2cstr(pTpe->data.field.propName, NULL); - dbgprintf("[Local Var: '%s'] ", cstr); - free(cstr); + dbgprintf("[Local Var: '%s'] ", pTpe->data.field.propName); } else if(pTpe->data.field.propid == PROP_GLOBAL_VAR) { - char *cstr = es_str2cstr(pTpe->data.field.propName, NULL); - dbgprintf("[Global Var: '%s'] ", cstr); - free(cstr); + dbgprintf("[Global Var: '%s'] ", pTpe->data.field.propName); } switch(pTpe->data.field.eDateFormat) { case tplFmtDefault: -- cgit v1.2.3 From 7a15d40a42d6f2b30b70cfee6b5435133ada9ac7 Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Mon, 21 Oct 2013 17:22:32 +0200 Subject: refactor: somewhat simplify property-name-to-id mapping --- template.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'template.c') diff --git a/template.c b/template.c index 92728607..b09a3515 100644 --- a/template.c +++ b/template.c @@ -809,7 +809,7 @@ do_Parameter(uchar **pp, struct template *pTpl) /* got the name */ cstrFinalize(pStrProp); - if(propNameToID(pStrProp, &pTpe->data.field.propid) != RS_RET_OK) { + if(propNameToID(cstrGetSzStrNoNULL(pStrProp), &pTpe->data.field.propid) != RS_RET_OK) { errmsg.LogError(0, RS_RET_TPL_INVLD_PROP, "template '%s': invalid parameter '%s'", pTpl->pszName, cstrGetSzStrNoNULL(pStrProp)); cstrDestruct(&pStrProp); @@ -1604,7 +1604,7 @@ createPropertyTpe(struct template *pTpl, struct cnfobj *o) /* apply */ CHKmalloc(pTpe = tpeConstruct(pTpl)); pTpe->eEntryType = FIELD; - CHKiRet(propNameToID(name, &pTpe->data.field.propid)); + CHKiRet(propNameToID(cstrGetSzStrNoNULL(name), &pTpe->data.field.propid)); if(pTpe->data.field.propid == PROP_CEE) { /* in CEE case, we need to preserve the actual property name */ pTpe->data.field.propName = ustrdup(cstrGetSzStrNoNULL(name)+1); -- cgit v1.2.3 From e4e19176298d6ac76c463565fa192c2349731156 Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Mon, 21 Oct 2013 17:33:46 +0200 Subject: refactor: more simplification in property handling --- template.c | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) (limited to 'template.c') diff --git a/template.c b/template.c index b09a3515..68aaf86e 100644 --- a/template.c +++ b/template.c @@ -815,12 +815,10 @@ do_Parameter(uchar **pp, struct template *pTpl) cstrDestruct(&pStrProp); ABORT_FINALIZE(RS_RET_TPL_INVLD_PROP); } - if(pTpe->data.field.propid == PROP_CEE) { - /* in CEE case, we need to preserve the actual property name */ - pTpe->data.field.propName = ustrdup(cstrGetSzStrNoNULL(pStrProp)+1); - pTpe->data.field.propNameLen = cstrLen(pStrProp)-1; - } else if(pTpe->data.field.propid == PROP_LOCAL_VAR || pTpe->data.field.propid == PROP_GLOBAL_VAR) { - /* in these cases, we need to preserve the actual property name, but correct the root ID (bang vs. dot) */ + if(pTpe->data.field.propid == PROP_CEE || + pTpe->data.field.propid == PROP_LOCAL_VAR || + pTpe->data.field.propid == PROP_GLOBAL_VAR) { + /* in these cases, we need the field name for later processing */ pTpe->data.field.propName = ustrdup(cstrGetSzStrNoNULL(pStrProp)+1); pTpe->data.field.propNameLen = cstrLen(pStrProp)-1; pTpe->data.field.propName[0] = '!'; /* patch root name */ @@ -1605,12 +1603,10 @@ createPropertyTpe(struct template *pTpl, struct cnfobj *o) CHKmalloc(pTpe = tpeConstruct(pTpl)); pTpe->eEntryType = FIELD; CHKiRet(propNameToID(cstrGetSzStrNoNULL(name), &pTpe->data.field.propid)); - if(pTpe->data.field.propid == PROP_CEE) { - /* in CEE case, we need to preserve the actual property name */ - pTpe->data.field.propName = ustrdup(cstrGetSzStrNoNULL(name)+1); - pTpe->data.field.propNameLen = cstrLen(name)-1; - } else if(pTpe->data.field.propid == PROP_LOCAL_VAR || pTpe->data.field.propid == PROP_GLOBAL_VAR) { - /* in these case, we need to preserve the actual property name, but correct the root ID (bang vs. dot) */ + if(pTpe->data.field.propid == PROP_CEE || + pTpe->data.field.propid == PROP_LOCAL_VAR || + pTpe->data.field.propid == PROP_GLOBAL_VAR) { + /* in these cases, we need the fieldname for later processing */ pTpe->data.field.propName = ustrdup(cstrGetSzStrNoNULL(name)+1); pTpe->data.field.propNameLen = cstrLen(name)-1; pTpe->data.field.propName[0] = '!'; /* patch root name */ -- cgit v1.2.3 From 7d39740b3d88dbd0432806e5f8da32c49cdb69f1 Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Tue, 22 Oct 2013 17:55:35 +0200 Subject: refactor: use common code for message property description processing in all cases except script var access -- this comes next... --- template.c | 77 ++++++++++++++++++++++---------------------------------------- 1 file changed, 27 insertions(+), 50 deletions(-) (limited to 'template.c') diff --git a/template.c b/template.c index 68aaf86e..8afb01f0 100644 --- a/template.c +++ b/template.c @@ -1,7 +1,7 @@ /* This is the template processing code of rsyslog. * begun 2004-11-17 rgerhards * - * Copyright 2004-2012 Rainer Gerhards and Adiscon + * Copyright 2004-2013 Rainer Gerhards and Adiscon * * This file is part of rsyslog. * @@ -193,8 +193,7 @@ tplToString(struct template *pTpl, msg_t *pMsg, uchar **ppBuf, size_t *pLenBuf, iLenVal = pTpe->data.constant.iLenConstant; bMustBeFreed = 0; } else if(pTpe->eEntryType == FIELD) { - pVal = (uchar*) MsgGetProp(pMsg, pTpe, pTpe->data.field.propid, - pTpe->data.field.propName, pTpe->data.field.propNameLen, + pVal = (uchar*) MsgGetProp(pMsg, pTpe, &pTpe->data.field.msgProp, &iLenVal, &bMustBeFreed, ttNow); /* we now need to check if we should use SQL option. In this case, * we must go over the generated string and escape '\'' characters. @@ -290,8 +289,7 @@ tplToArray(struct template *pTpl, msg_t *pMsg, uchar*** ppArr, struct syslogTime if(pTpe->eEntryType == CONSTANT) { CHKmalloc(pArr[iArr] = (uchar*)strdup((char*) pTpe->data.constant.pConstant)); } else if(pTpe->eEntryType == FIELD) { - pVal = (uchar*) MsgGetProp(pMsg, pTpe, pTpe->data.field.propid, - pTpe->data.field.propName, pTpe->data.field.propNameLen, + pVal = (uchar*) MsgGetProp(pMsg, pTpe, &pTpe->data.field.msgProp, &propLen, &bMustBeFreed, ttNow); if(bMustBeFreed) { /* if it must be freed, it is our own private copy... */ pArr[iArr] = pVal; /* ... so we can use it! */ @@ -345,8 +343,8 @@ tplToJSON(struct template *pTpl, msg_t *pMsg, struct json_object **pjson, struct jsonf = json_object_new_string((char*) pTpe->data.constant.pConstant); json_object_object_add(json, (char*)pTpe->fieldName, jsonf); } else if(pTpe->eEntryType == FIELD) { - if(pTpe->data.field.propid == PROP_CEE) { - localRet = msgGetCEEPropJSON(pMsg, pTpe->data.field.propName, pTpe->data.field.propNameLen, &jsonf); + if(pTpe->data.field.msgProp.id == PROP_CEE) { + localRet = msgGetCEEPropJSON(pMsg, pTpe->data.field.msgProp.name, pTpe->data.field.msgProp.nameLen, &jsonf); if(localRet == RS_RET_OK) { json_object_object_add(json, (char*)pTpe->fieldName, json_object_get(jsonf)); } else { @@ -356,8 +354,8 @@ tplToJSON(struct template *pTpl, msg_t *pMsg, struct json_object **pjson, struct json_object_object_add(json, (char*)pTpe->fieldName, NULL); } } - } else if(pTpe->data.field.propid == PROP_LOCAL_VAR) { - localRet = msgGetLocalVarJSON(pMsg, pTpe->data.field.propName, pTpe->data.field.propNameLen, &jsonf); + } else if(pTpe->data.field.msgProp.id == PROP_LOCAL_VAR) { + localRet = msgGetLocalVarJSON(pMsg, pTpe->data.field.msgProp.name, pTpe->data.field.msgProp.nameLen, &jsonf); if(localRet == RS_RET_OK) { json_object_object_add(json, (char*)pTpe->fieldName, json_object_get(jsonf)); } else { @@ -367,8 +365,9 @@ tplToJSON(struct template *pTpl, msg_t *pMsg, struct json_object **pjson, struct json_object_object_add(json, (char*)pTpe->fieldName, NULL); } } - } else if(pTpe->data.field.propid == PROP_GLOBAL_VAR) { - localRet = msgGetGlobalVarJSON(pTpe->data.field.propName, pTpe->data.field.propNameLen, &jsonf); +#if 0 /* do not remove this code *for the moment* -- rgerhards, 2013-10-22 */ + } else if(pTpe->data.field.msgProp.id == PROP_GLOBAL_VAR) { + localRet = msgGetGlobalVarJSON(pTpe->data.field.msgProp.name, pTpe->data.field.msgProp.nameLen, &jsonf); if(localRet == RS_RET_OK) { json_object_object_add(json, (char*)pTpe->fieldName, json_object_get(jsonf)); } else { @@ -378,11 +377,10 @@ tplToJSON(struct template *pTpl, msg_t *pMsg, struct json_object **pjson, struct json_object_object_add(json, (char*)pTpe->fieldName, NULL); } } +#endif } else { - pVal = (uchar*) MsgGetProp(pMsg, pTpe, pTpe->data.field.propid, - pTpe->data.field.propName, - pTpe->data.field.propNameLen, &propLen, - &bMustBeFreed, ttNow); + pVal = (uchar*) MsgGetProp(pMsg, pTpe, &pTpe->data.field.msgProp, + &propLen, &bMustBeFreed, ttNow); if(pTpe->data.field.options.bMandatory || propLen > 0) { jsonf = json_object_new_string_len((char*)pVal, propLen); json_object_object_add(json, (char*)pTpe->fieldName, jsonf); @@ -809,21 +807,8 @@ do_Parameter(uchar **pp, struct template *pTpl) /* got the name */ cstrFinalize(pStrProp); - if(propNameToID(cstrGetSzStrNoNULL(pStrProp), &pTpe->data.field.propid) != RS_RET_OK) { - errmsg.LogError(0, RS_RET_TPL_INVLD_PROP, "template '%s': invalid parameter '%s'", - pTpl->pszName, cstrGetSzStrNoNULL(pStrProp)); - cstrDestruct(&pStrProp); - ABORT_FINALIZE(RS_RET_TPL_INVLD_PROP); - } - if(pTpe->data.field.propid == PROP_CEE || - pTpe->data.field.propid == PROP_LOCAL_VAR || - pTpe->data.field.propid == PROP_GLOBAL_VAR) { - /* in these cases, we need the field name for later processing */ - pTpe->data.field.propName = ustrdup(cstrGetSzStrNoNULL(pStrProp)+1); - pTpe->data.field.propNameLen = cstrLen(pStrProp)-1; - pTpe->data.field.propName[0] = '!'; /* patch root name */ -#warning do we really need to patch root name? - } + CHKiRet(msgPropDescrFill(&pTpe->data.field.msgProp, cstrGetSzStrNoNULL(pStrProp), + cstrLen(pStrProp))); /* Check frompos, if it has an R, then topos should be a regex */ if(*p == ':') { @@ -1120,8 +1105,7 @@ do_Parameter(uchar **pp, struct template *pTpl) /* save field name - if none was given, use the property name instead */ if(pStrField == NULL) { - if(pTpe->data.field.propid == PROP_CEE || pTpe->data.field.propid == PROP_LOCAL_VAR || - pTpe->data.field.propid == PROP_GLOBAL_VAR) { + if(pTpe->data.field.msgProp.id == PROP_CEE || pTpe->data.field.msgProp.id == PROP_LOCAL_VAR) { /* in CEE case, we remove "$!"/"$." from the fieldname - it's just our indicator */ pTpe->fieldName = ustrdup(cstrGetSzStrNoNULL(pStrProp)+2); pTpe->lenFieldName = cstrLen(pStrProp)-2; @@ -1602,15 +1586,8 @@ createPropertyTpe(struct template *pTpl, struct cnfobj *o) /* apply */ CHKmalloc(pTpe = tpeConstruct(pTpl)); pTpe->eEntryType = FIELD; - CHKiRet(propNameToID(cstrGetSzStrNoNULL(name), &pTpe->data.field.propid)); - if(pTpe->data.field.propid == PROP_CEE || - pTpe->data.field.propid == PROP_LOCAL_VAR || - pTpe->data.field.propid == PROP_GLOBAL_VAR) { - /* in these cases, we need the fieldname for later processing */ - pTpe->data.field.propName = ustrdup(cstrGetSzStrNoNULL(name)+1); - pTpe->data.field.propNameLen = cstrLen(name)-1; - pTpe->data.field.propName[0] = '!'; /* patch root name */ - } + CHKiRet(msgPropDescrFill(&pTpe->data.field.msgProp, cstrGetSzStrNoNULL(name), + cstrLen(name))); pTpe->data.field.options.bDropLastLF = droplastlf; pTpe->data.field.options.bSPIffNo1stSP = spifno1stsp; pTpe->data.field.options.bMandatory = mandatory; @@ -1997,8 +1974,8 @@ void tplDeleteAll(rsconf_t *conf) regexp.regfree(&(pTpeDel->data.field.re)); } } - free(pTpeDel->data.field.propName); #endif + msgPropDescrDestruct(&pTpeDel->data.field.msgProp); break; } free(pTpeDel->fieldName); @@ -2055,8 +2032,8 @@ void tplDeleteNew(rsconf_t *conf) regexp.regfree(&(pTpeDel->data.field.re)); } } - free(pTpeDel->data.field.propName); #endif + msgPropDescrDestruct(&pTpeDel->data.field.msgProp); break; } /*dbgprintf("\n");*/ @@ -2107,13 +2084,13 @@ void tplPrintList(rsconf_t *conf) pTpe->data.constant.pConstant); break; case FIELD: - dbgprintf("(FIELD), value: '%d' ", pTpe->data.field.propid); - if(pTpe->data.field.propid == PROP_CEE) { - dbgprintf("[EE-Property: '%s'] ", pTpe->data.field.propName); - } else if(pTpe->data.field.propid == PROP_LOCAL_VAR) { - dbgprintf("[Local Var: '%s'] ", pTpe->data.field.propName); - } else if(pTpe->data.field.propid == PROP_GLOBAL_VAR) { - dbgprintf("[Global Var: '%s'] ", pTpe->data.field.propName); + dbgprintf("(FIELD), value: '%d' ", pTpe->data.field.msgProp.id); + if(pTpe->data.field.msgProp.id == PROP_CEE) { + dbgprintf("[EE-Property: '%s'] ", pTpe->data.field.msgProp.name); + } else if(pTpe->data.field.msgProp.id == PROP_LOCAL_VAR) { + dbgprintf("[Local Var: '%s'] ", pTpe->data.field.msgProp.name); + //} else if(pTpe->data.field.propid == PROP_GLOBAL_VAR) { + // dbgprintf("[Global Var: '%s'] ", pTpe->data.field.propName); } switch(pTpe->data.field.eDateFormat) { case tplFmtDefault: -- cgit v1.2.3 From eb9adf9baad5dca9dc2f30f45dc6eaf7607ac8f8 Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Wed, 23 Oct 2013 13:08:56 +0200 Subject: refactor: simplify JSON variable access --- template.c | 30 ++++-------------------------- 1 file changed, 4 insertions(+), 26 deletions(-) (limited to 'template.c') diff --git a/template.c b/template.c index 8afb01f0..e97428db 100644 --- a/template.c +++ b/template.c @@ -343,8 +343,10 @@ tplToJSON(struct template *pTpl, msg_t *pMsg, struct json_object **pjson, struct jsonf = json_object_new_string((char*) pTpe->data.constant.pConstant); json_object_object_add(json, (char*)pTpe->fieldName, jsonf); } else if(pTpe->eEntryType == FIELD) { - if(pTpe->data.field.msgProp.id == PROP_CEE) { - localRet = msgGetCEEPropJSON(pMsg, pTpe->data.field.msgProp.name, pTpe->data.field.msgProp.nameLen, &jsonf); + if(pTpe->data.field.msgProp.id == PROP_CEE || + pTpe->data.field.msgProp.id == PROP_LOCAL_VAR || + pTpe->data.field.msgProp.id == PROP_GLOBAL_VAR ) { + localRet = msgGetJSONPropJSON(pMsg, &pTpe->data.field.msgProp, &jsonf); if(localRet == RS_RET_OK) { json_object_object_add(json, (char*)pTpe->fieldName, json_object_get(jsonf)); } else { @@ -354,30 +356,6 @@ tplToJSON(struct template *pTpl, msg_t *pMsg, struct json_object **pjson, struct json_object_object_add(json, (char*)pTpe->fieldName, NULL); } } - } else if(pTpe->data.field.msgProp.id == PROP_LOCAL_VAR) { - localRet = msgGetLocalVarJSON(pMsg, pTpe->data.field.msgProp.name, pTpe->data.field.msgProp.nameLen, &jsonf); - if(localRet == RS_RET_OK) { - json_object_object_add(json, (char*)pTpe->fieldName, json_object_get(jsonf)); - } else { - DBGPRINTF("tplToJSON: error %d looking up local variable %s\n", - localRet, pTpe->fieldName); - if(pTpe->data.field.options.bMandatory) { - json_object_object_add(json, (char*)pTpe->fieldName, NULL); - } - } -#if 0 /* do not remove this code *for the moment* -- rgerhards, 2013-10-22 */ - } else if(pTpe->data.field.msgProp.id == PROP_GLOBAL_VAR) { - localRet = msgGetGlobalVarJSON(pTpe->data.field.msgProp.name, pTpe->data.field.msgProp.nameLen, &jsonf); - if(localRet == RS_RET_OK) { - json_object_object_add(json, (char*)pTpe->fieldName, json_object_get(jsonf)); - } else { - DBGPRINTF("tplToJSON: error %d looking up local variable %s\n", - localRet, pTpe->fieldName); - if(pTpe->data.field.options.bMandatory) { - json_object_object_add(json, (char*)pTpe->fieldName, NULL); - } - } -#endif } else { pVal = (uchar*) MsgGetProp(pMsg, pTpe, &pTpe->data.field.msgProp, &propLen, &bMustBeFreed, ttNow); -- cgit v1.2.3 From c0f44743898954aee7f846ce630280a80297b80d Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Wed, 23 Oct 2013 15:12:30 +0200 Subject: refactor get.*PropVal() series of functions --- template.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) (limited to 'template.c') diff --git a/template.c b/template.c index e97428db..807acd75 100644 --- a/template.c +++ b/template.c @@ -163,13 +163,13 @@ tplToString(struct template *pTpl, msg_t *pMsg, uchar **ppBuf, size_t *pLenBuf, FINALIZE; } - if(pTpl->subtree != NULL) { + if(pTpl->bHaveSubtree) { /* only a single CEE subtree must be provided */ /* note: we could optimize the code below, however, this is * not worth the effort, as this passing mode is not expected * in subtree mode and so most probably only used for debug & test. */ - getCEEPropVal(pMsg, pTpl->subtree, pTpl->subtreeLen, &pVal, &iLenVal, &bMustBeFreed); + getJSONPropVal(pMsg, &pTpl->subtree, &pVal, &iLenVal, &bMustBeFreed); if(iLenVal >= (rs_size_t)*pLenBuf) /* we reserve one char for the final \0! */ CHKiRet(ExtendBuf(ppBuf, pLenBuf, iLenVal + 1)); memcpy(*ppBuf, pVal, iLenVal+1); @@ -263,12 +263,12 @@ tplToArray(struct template *pTpl, msg_t *pMsg, uchar*** ppArr, struct syslogTime assert(pMsg != NULL); assert(ppArr != NULL); - if(pTpl->subtree) { + if(pTpl->bHaveSubtree) { /* Note: this mode is untested, as there is no official plugin * using array passing, so I simply could not test it. */ CHKmalloc(pArr = calloc(2, sizeof(uchar*))); - getCEEPropVal(pMsg, pTpl->subtree, pTpl->subtreeLen, &pVal, &propLen, &bMustBeFreed); + getJSONPropVal(pMsg, &pTpl->subtree, &pVal, &propLen, &bMustBeFreed); if(bMustBeFreed) { /* if it must be freed, it is our own private copy... */ pArr[0] = pVal; /* ... so we can use it! */ } else { @@ -324,8 +324,8 @@ tplToJSON(struct template *pTpl, msg_t *pMsg, struct json_object **pjson, struct rsRetVal localRet; DEFiRet; - if(pTpl->subtree != NULL){ - localRet = jsonFind(pMsg->json, pTpl->subtree, pTpl->subtreeLen, pjson); + if(pTpl->bHaveSubtree){ + localRet = jsonFind(pMsg->json, pTpl->subtree.name, pTpl->subtree.nameLen, pjson); if(*pjson == NULL) { /* we need to have a root object! */ *pjson = json_object_new_object(); @@ -534,7 +534,6 @@ tplConstruct(rsconf_t *conf) struct template *pTpl; if((pTpl = calloc(1, sizeof(struct template))) == NULL) return NULL; -pTpl->subtree = NULL; /* basic initialisation is done via calloc() - need to * initialize only values != 0. */ @@ -1698,8 +1697,9 @@ tplProcessCnf(struct cnfobj *o) char *name = NULL; uchar *tplStr = NULL; uchar *plugin = NULL; - uchar *subtree = NULL; uchar *p; + msgPropDescr_t subtree; + sbool bHaveSubtree = 0; enum { T_STRING, T_PLUGIN, T_LIST, T_SUBTREE } tplType = T_STRING; /* init just to keep compiler happy: mandatory parameter */ int i; @@ -1746,7 +1746,11 @@ tplProcessCnf(struct cnfobj *o) free(name); /* overall assigned */ ABORT_FINALIZE(RS_RET_ERR); } else { - subtree = (uchar*)es_str2cstr(pvals[i].val.d.estr, NULL); + uchar *cstr; + cstr = (uchar*)es_str2cstr(pvals[i].val.d.estr, NULL); + CHKiRet(msgPropDescrFill(&subtree, cstr, ustrlen(cstr))); + free(cstr); + bHaveSubtree = 1; } } else if(!strcmp(pblk.descr[i].name, "plugin")) { plugin = (uchar*) es_str2cstr(pvals[i].val.d.estr, NULL); @@ -1789,7 +1793,7 @@ tplProcessCnf(struct cnfobj *o) } } - if(subtree == NULL) { + if(bHaveSubtree) { if(tplType == T_SUBTREE) { errmsg.LogError(0, RS_RET_ERR, "template '%s' of type subtree needs " "subtree parameter", name); @@ -1859,8 +1863,8 @@ tplProcessCnf(struct cnfobj *o) break; case T_LIST: createListTpl(pTpl, o); break; - case T_SUBTREE: pTpl->subtree = subtree; - pTpl->subtreeLen = ustrlen(subtree); + case T_SUBTREE: memcpy(&pTpl->subtree, &subtree, sizeof(msgPropDescr_t)); + pTpl->bHaveSubtree = 1; break; } @@ -1963,7 +1967,8 @@ void tplDeleteAll(rsconf_t *conf) pTplDel = pTpl; pTpl = pTpl->pNext; free(pTplDel->pszName); - free(pTplDel->subtree); + if(pTplDel->bHaveSubtree) + msgPropDescrDestruct(&pTplDel->subtree); free(pTplDel); } ENDfunc @@ -2020,7 +2025,8 @@ void tplDeleteNew(rsconf_t *conf) pTplDel = pTpl; pTpl = pTpl->pNext; free(pTplDel->pszName); - free(pTplDel->subtree); + if(pTplDel->bHaveSubtree) + msgPropDescrDestruct(&pTplDel->subtree); free(pTplDel); } ENDfunc -- cgit v1.2.3 From 059a3168246f803936de63ced1d356d649d4ad16 Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Wed, 23 Oct 2013 15:59:23 +0200 Subject: refactor: align jsonFind() calling interface with recent changes --- template.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'template.c') diff --git a/template.c b/template.c index 807acd75..b4f28443 100644 --- a/template.c +++ b/template.c @@ -325,7 +325,7 @@ tplToJSON(struct template *pTpl, msg_t *pMsg, struct json_object **pjson, struct DEFiRet; if(pTpl->bHaveSubtree){ - localRet = jsonFind(pMsg->json, pTpl->subtree.name, pTpl->subtree.nameLen, pjson); + localRet = jsonFind(pMsg->json, &pTpl->subtree, pjson); if(*pjson == NULL) { /* we need to have a root object! */ *pjson = json_object_new_object(); -- cgit v1.2.3 From cdc306767df06937c2d14b459a6963154fb6d400 Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Wed, 23 Oct 2013 16:10:55 +0200 Subject: fix small memleak introduced during refactoring --- template.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'template.c') diff --git a/template.c b/template.c index b4f28443..ebb3f422 100644 --- a/template.c +++ b/template.c @@ -754,7 +754,7 @@ static rsRetVal do_Parameter(uchar **pp, struct template *pTpl) { uchar *p; - cstr_t *pStrProp; + cstr_t *pStrProp = NULL; cstr_t *pStrField = NULL; struct templateEntry *pTpe; int iNum; /* to compute numbers */ @@ -1099,10 +1099,11 @@ do_Parameter(uchar **pp, struct template *pTpl) DBGPRINTF("template/do_Parameter: fieldName is NULL!\n"); ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); } - cstrDestruct(&pStrProp); if(*p) ++p; /* eat '%' */ *pp = p; finalize_it: + if(pStrProp != NULL) + cstrDestruct(&pStrProp); RETiRet; } -- cgit v1.2.3