| 1 |
/* strgen.c |
| 2 |
* Module to handle string generators. These are C modules that receive |
| 3 |
* the message object and return a custom-built string. The primary purpose |
| 4 |
* for their existence is performance -- they do the same as template strings, but |
| 5 |
* potentially faster (if well implmented). |
| 6 |
* |
| 7 |
* Module begun 2010-06-01 by Rainer Gerhards |
| 8 |
* |
| 9 |
* Copyright 2010 Rainer Gerhards and Adiscon GmbH. |
| 10 |
* |
| 11 |
* This file is part of the rsyslog runtime library. |
| 12 |
* |
| 13 |
* The rsyslog runtime library is free software: you can redistribute it and/or modify |
| 14 |
* it under the terms of the GNU Lesser General Public License as published by |
| 15 |
* the Free Software Foundation, either version 3 of the License, or |
| 16 |
* (at your option) any later version. |
| 17 |
* |
| 18 |
* The rsyslog runtime library is distributed in the hope that it will be useful, |
| 19 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 |
* GNU Lesser General Public License for more details. |
| 22 |
* |
| 23 |
* You should have received a copy of the GNU Lesser General Public License |
| 24 |
* along with the rsyslog runtime library. If not, see <http://www.gnu.org/licenses/>. |
| 25 |
* |
| 26 |
* A copy of the GPL can be found in the file "COPYING" in this distribution. |
| 27 |
* A copy of the LGPL can be found in the file "COPYING.LESSER" in this distribution. |
| 28 |
*/ |
| 29 |
#include "config.h" |
| 30 |
#include <stdlib.h> |
| 31 |
#include <string.h> |
| 32 |
#include <assert.h> |
| 33 |
|
| 34 |
#include "rsyslog.h" |
| 35 |
#include "msg.h" |
| 36 |
#include "obj.h" |
| 37 |
#include "errmsg.h" |
| 38 |
#include "strgen.h" |
| 39 |
#include "ruleset.h" |
| 40 |
#include "unicode-helper.h" |
| 41 |
#include "cfsysline.h" |
| 42 |
|
| 43 |
/* definitions for objects we access */ |
| 44 |
DEFobjStaticHelpers; |
| 45 |
DEFobjCurrIf(glbl) DEFobjCurrIf(ruleset) |
| 46 |
|
| 47 |
/* static data */ |
| 48 |
|
| 49 |
/* config data */ |
| 50 |
|
| 51 |
/* This is the list of all strgens known to us. |
| 52 |
* This is also used to unload all modules on shutdown. |
| 53 |
*/ |
| 54 |
strgenList_t *pStrgenLstRoot = NULL; |
| 55 |
|
| 56 |
|
| 57 |
/* initialize (but NOT allocate) a strgen list. Primarily meant as a hook |
| 58 |
* which can be used to extend the list in the future. So far, just sets |
| 59 |
* it to NULL. |
| 60 |
*/ |
| 61 |
static rsRetVal InitStrgenList(strgenList_t **pListRoot) { |
| 62 |
*pListRoot = NULL; |
| 63 |
return RS_RET_OK; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
/* destruct a strgen list. The list elements are destroyed, but the strgen objects |
| 68 |
* themselves are not modified. (That is done at a late stage during rsyslogd |
| 69 |
* shutdown and need not be considered here.) |
| 70 |
*/ |
| 71 |
static rsRetVal DestructStrgenList(strgenList_t **ppListRoot) { |
| 72 |
strgenList_t *pStrgenLst; |
| 73 |
strgenList_t *pStrgenLstDel; |
| 74 |
|
| 75 |
pStrgenLst = *ppListRoot; |
| 76 |
while (pStrgenLst != NULL) { |
| 77 |
pStrgenLstDel = pStrgenLst; |
| 78 |
pStrgenLst = pStrgenLst->pNext; |
| 79 |
free(pStrgenLstDel); |
| 80 |
} |
| 81 |
*ppListRoot = NULL; |
| 82 |
return RS_RET_OK; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
/* Add a strgen to the list. We use a VERY simple and ineffcient algorithm, |
| 87 |
* but it is employed only for a few milliseconds during config processing. So |
| 88 |
* I prefer to keep it very simple and with simple data structures. Unfortunately, |
| 89 |
* we need to preserve the order, but I don't like to add a tail pointer as that |
| 90 |
* would require a container object. So I do the extra work to skip to the tail |
| 91 |
* when adding elements... |
| 92 |
*/ |
| 93 |
static rsRetVal AddStrgenToList(strgenList_t **ppListRoot, strgen_t *pStrgen) { |
| 94 |
strgenList_t *pThis; |
| 95 |
strgenList_t *pTail; |
| 96 |
DEFiRet; |
| 97 |
|
| 98 |
CHKmalloc(pThis = malloc(sizeof(strgenList_t))); |
| 99 |
pThis->pStrgen = pStrgen; |
| 100 |
pThis->pNext = NULL; |
| 101 |
|
| 102 |
if (*ppListRoot == NULL) { |
| 103 |
pThis->pNext = *ppListRoot; |
| 104 |
*ppListRoot = pThis; |
| 105 |
} else { |
| 106 |
/* find tail first */ |
| 107 |
for (pTail = *ppListRoot; pTail->pNext != NULL; pTail = pTail->pNext) /* just search, do nothing else */ |
| 108 |
; |
| 109 |
/* add at tail */ |
| 110 |
pTail->pNext = pThis; |
| 111 |
} |
| 112 |
|
| 113 |
finalize_it: |
| 114 |
RETiRet; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
/* find a strgen based on the provided name */ |
| 119 |
static rsRetVal FindStrgen(strgen_t **ppStrgen, uchar *pName) { |
| 120 |
strgenList_t *pThis; |
| 121 |
DEFiRet; |
| 122 |
|
| 123 |
for (pThis = pStrgenLstRoot; pThis != NULL; pThis = pThis->pNext) { |
| 124 |
if (ustrcmp(pThis->pStrgen->pName, pName) == 0) { |
| 125 |
*ppStrgen = pThis->pStrgen; |
| 126 |
FINALIZE; /* found it, iRet still eq. OK! */ |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
iRet = RS_RET_PARSER_NOT_FOUND; |
| 131 |
|
| 132 |
finalize_it: |
| 133 |
RETiRet; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
/* --- END helper functions for strgen list handling --- */ |
| 138 |
|
| 139 |
|
| 140 |
BEGINobjConstruct(strgen) /* be sure to specify the object type also in END macro! */ |
| 141 |
ENDobjConstruct(strgen) |
| 142 |
|
| 143 |
/* ConstructionFinalizer. The most important chore is to add the strgen object |
| 144 |
* to our global list of available strgens. |
| 145 |
* rgerhards, 2009-11-03 |
| 146 |
*/ |
| 147 |
static rsRetVal strgenConstructFinalize(strgen_t *pThis) { |
| 148 |
DEFiRet; |
| 149 |
|
| 150 |
ISOBJ_TYPE_assert(pThis, strgen); |
| 151 |
CHKiRet(AddStrgenToList(&pStrgenLstRoot, pThis)); |
| 152 |
DBGPRINTF("Strgen '%s' added to list of available strgens.\n", pThis->pName); |
| 153 |
|
| 154 |
finalize_it: |
| 155 |
RETiRet; |
| 156 |
} |
| 157 |
|
| 158 |
BEGINobjDestruct(strgen) /* be sure to specify the object type also in END and CODESTART macros! */ |
| 159 |
CODESTARTobjDestruct(strgen); |
| 160 |
dbgprintf("destructing strgen '%s'\n", pThis->pName); |
| 161 |
free(pThis->pName); |
| 162 |
ENDobjDestruct(strgen) |
| 163 |
|
| 164 |
/* set the strgen name - string is copied over, call can continue to use it, |
| 165 |
* but must free it if desired. |
| 166 |
*/ |
| 167 |
static rsRetVal SetName(strgen_t *pThis, uchar *name) { |
| 168 |
DEFiRet; |
| 169 |
|
| 170 |
ISOBJ_TYPE_assert(pThis, strgen); |
| 171 |
assert(name != NULL); |
| 172 |
|
| 173 |
if (pThis->pName != NULL) { |
| 174 |
free(pThis->pName); |
| 175 |
pThis->pName = NULL; |
| 176 |
} |
| 177 |
|
| 178 |
CHKmalloc(pThis->pName = ustrdup(name)); |
| 179 |
|
| 180 |
finalize_it: |
| 181 |
RETiRet; |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
/* set a pointer to "our" module. Note that no module |
| 186 |
* pointer must already be set. |
| 187 |
*/ |
| 188 |
static rsRetVal SetModPtr(strgen_t *pThis, modInfo_t *pMod) { |
| 189 |
ISOBJ_TYPE_assert(pThis, strgen); |
| 190 |
assert(pMod != NULL); |
| 191 |
assert(pThis->pModule == NULL); |
| 192 |
pThis->pModule = pMod; |
| 193 |
return RS_RET_OK; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
/* queryInterface function-- rgerhards, 2009-11-03 |
| 198 |
*/ |
| 199 |
BEGINobjQueryInterface(strgen) |
| 200 |
CODESTARTobjQueryInterface(strgen); |
| 201 |
if (pIf->ifVersion != strgenCURR_IF_VERSION) { /* check for current version, increment on each change */ |
| 202 |
ABORT_FINALIZE(RS_RET_INTERFACE_NOT_SUPPORTED); |
| 203 |
} |
| 204 |
|
| 205 |
/* ok, we have the right interface, so let's fill it |
| 206 |
* Please note that we may also do some backwards-compatibility |
| 207 |
* work here (if we can support an older interface version - that, |
| 208 |
* of course, also affects the "if" above). |
| 209 |
*/ |
| 210 |
pIf->Construct = strgenConstruct; |
| 211 |
pIf->ConstructFinalize = strgenConstructFinalize; |
| 212 |
pIf->Destruct = strgenDestruct; |
| 213 |
pIf->SetName = SetName; |
| 214 |
pIf->SetModPtr = SetModPtr; |
| 215 |
pIf->InitStrgenList = InitStrgenList; |
| 216 |
pIf->DestructStrgenList = DestructStrgenList; |
| 217 |
pIf->AddStrgenToList = AddStrgenToList; |
| 218 |
pIf->FindStrgen = FindStrgen; |
| 219 |
finalize_it: |
| 220 |
ENDobjQueryInterface(strgen) |
| 221 |
|
| 222 |
|
| 223 |
/* This destroys the master strgenlist and all of its strgen entries. MUST only be |
| 224 |
* done when the module is shut down. Strgen modules are NOT unloaded, rsyslog |
| 225 |
* does that at a later stage for all dynamically loaded modules. |
| 226 |
*/ |
| 227 |
static void destroyMasterStrgenList(void) { |
| 228 |
strgenList_t *pStrgenLst; |
| 229 |
strgenList_t *pStrgenLstDel; |
| 230 |
|
| 231 |
pStrgenLst = pStrgenLstRoot; |
| 232 |
while (pStrgenLst != NULL) { |
| 233 |
strgenDestruct(&pStrgenLst->pStrgen); |
| 234 |
pStrgenLstDel = pStrgenLst; |
| 235 |
pStrgenLst = pStrgenLst->pNext; |
| 236 |
free(pStrgenLstDel); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
/* Exit our class. |
| 241 |
* rgerhards, 2009-11-04 |
| 242 |
*/ |
| 243 |
BEGINObjClassExit(strgen, OBJ_IS_CORE_MODULE) /* class, version */ |
| 244 |
destroyMasterStrgenList(); |
| 245 |
objRelease(glbl, CORE_COMPONENT); |
| 246 |
objRelease(ruleset, CORE_COMPONENT); |
| 247 |
ENDObjClassExit(strgen) |
| 248 |
|
| 249 |
|
| 250 |
/* Initialize the strgen class. Must be called as the very first method |
| 251 |
* before anything else is called inside this class. |
| 252 |
* rgerhards, 2009-11-02 |
| 253 |
*/ |
| 254 |
BEGINObjClassInit(strgen, 1, OBJ_IS_CORE_MODULE) /* class, version */ |
| 255 |
/* request objects we use */ |
| 256 |
CHKiRet(objUse(glbl, CORE_COMPONENT)); |
| 257 |
CHKiRet(objUse(ruleset, CORE_COMPONENT)); |
| 258 |
InitStrgenList(&pStrgenLstRoot); |
| 259 |
ENDObjClassInit(strgen) |