Debian Codegraph @

runtime/modules.c

raw
1 /* modules.c
2 * This is the implementation of syslogd modules object.
3 * This object handles plug-ins and build-in modules of all kind.
4 *
5 * Modules are reference-counted. Anyone who access a module must call
6 * Use() before any function is accessed and Release() when he is done.
7 * When the reference count reaches 0, rsyslog unloads the module (that
8 * may be changed in the future to cache modules). Rsyslog does NOT
9 * unload modules with a reference count > 0, even if the unload
10 * method is called!
11 *
12 * File begun on 2007-07-22 by RGerhards
13 *
14 * Copyright 2007-2026 Rainer Gerhards and Adiscon GmbH.
15 *
16 * This file is part of the rsyslog runtime library.
17 *
18 * The rsyslog runtime library is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU Lesser General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
22 *
23 * The rsyslog runtime library is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU Lesser General Public License for more details.
27 *
28 * You should have received a copy of the GNU Lesser General Public License
29 * along with the rsyslog runtime library. If not, see <http://www.gnu.org/licenses/>.
30 *
31 * A copy of the GPL can be found in the file "COPYING" in this distribution.
32 * A copy of the LGPL can be found in the file "COPYING.LESSER" in this distribution.
33 */
34 #include "config.h"
35 #include <stdio.h>
36 #include <stdarg.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <time.h>
40 #include <assert.h>
41 #include <errno.h>
42 #include <pthread.h>
43 #ifdef OS_BSD
44 #include "libgen.h"
45 #endif
46
47 #include <dlfcn.h> /* TODO: replace this with the libtools equivalent! */
48
49 #include <unistd.h>
50 #include <sys/file.h>
51
52 #include "rsyslog.h"
53 #include "rainerscript.h"
54 #include "cfsysline.h"
55 #include "rsconf.h"
56 #include "modules.h"
57 #include "errmsg.h"
58 #include "parser.h"
59 #include "strgen.h"
60
61 /* static data */
62 DEFobjStaticHelpers;
63 DEFobjCurrIf(strgen)
64
65 static modInfo_t *pLoadedModules = NULL; /* list of currently-loaded modules */
66 static modInfo_t *pLoadedModulesLast = NULL; /* tail-pointer */
67
68 /* already dlopen()-ed libs */
69 static struct dlhandle_s *pHandles = NULL;
70
71 static uchar *pModDir; /* directory where loadable modules are found */
72
73 /* tables for interfacing with the v6 config system */
74 /* action (instance) parameters */
75 static struct cnfparamdescr actpdescr[] = {{"load", eCmdHdlrGetWord, 1}};
76 static struct cnfparamblk pblk = {CNFPARAMBLK_VERSION, sizeof(actpdescr) / sizeof(struct cnfparamdescr), actpdescr};
77
78
79 typedef rsRetVal (*pModInit_t)(int, int *, rsRetVal (**)(), rsRetVal (*)(), modInfo_t *);
80
81 /* we provide a set of dummy functions for modules that do not support the
82 * some interfaces.
83 * On the commit feature: As the modules do not support it, they commit each message they
84 * receive, and as such the dummies can always return RS_RET_OK without causing
85 * harm. This simplifies things as in action processing we do not need to check
86 * if the transactional entry points exist.
87 */
88 static rsRetVal dummyBeginTransaction(__attribute__((unused)) void *dummy) {
89 return RS_RET_OK;
90 }
91 static rsRetVal dummyEndTransaction(__attribute__((unused)) void *dummy) {
92 return RS_RET_OK;
93 }
94 static rsRetVal dummyIsCompatibleWithFeature(__attribute__((unused)) syslogFeature eFeat) {
95 return RS_RET_INCOMPATIBLE;
96 }
97 static rsRetVal dummynewActInst(uchar *modName,
98 struct nvlst __attribute__((unused)) * dummy1,
99 void __attribute__((unused)) * *dummy2,
100 omodStringRequest_t __attribute__((unused)) * *dummy3) {
101 LogError(0, RS_RET_CONFOBJ_UNSUPPORTED,
102 "config objects are not "
103 "supported by module '%s' -- legacy config options "
104 "MUST be used instead",
105 modName);
106 return RS_RET_CONFOBJ_UNSUPPORTED;
107 }
108
109 #ifdef DEBUG
110 /* we add some home-grown support to track our users (and detect who does not free us). In
111 * the long term, this should probably be migrated into debug.c (TODO). -- rgerhards, 2008-03-11
112 */
113
114 /* add a user to the current list of users (always at the root) */
115 static void modUsrAdd(modInfo_t *pThis, const char *pszUsr) {
116 modUsr_t *pUsr;
117
118 if ((pUsr = calloc(1, sizeof(modUsr_t))) == NULL) goto finalize_it;
119
120 if ((pUsr->pszFile = strdup(pszUsr)) == NULL) {
121 free(pUsr);
122 goto finalize_it;
123 }
124
125 if (pThis->pModUsrRoot != NULL) {
126 pUsr->pNext = pThis->pModUsrRoot;
127 }
128 pThis->pModUsrRoot = pUsr;
129
130 finalize_it:
131 return;
132 }
133
134
135 /* remove a user from the current user list
136 * rgerhards, 2008-03-11
137 */
138 static void modUsrDel(modInfo_t *pThis, const char *pszUsr) {
139 modUsr_t *pUsr;
140 modUsr_t *pPrev = NULL;
141
142 for (pUsr = pThis->pModUsrRoot; pUsr != NULL; pUsr = pUsr->pNext) {
143 if (!strcmp(pUsr->pszFile, pszUsr))
144 break;
145 else
146 pPrev = pUsr;
147 }
148
149 if (pUsr == NULL) {
150 dbgprintf("oops - tried to delete user %s from module %s and it wasn't registered as one...\n", pszUsr,
151 pThis->pszName);
152 } else {
153 if (pPrev == NULL) {
154 /* This was at the root! */
155 pThis->pModUsrRoot = pUsr->pNext;
156 } else {
157 pPrev->pNext = pUsr->pNext;
158 }
159 /* free resources */
160 free(pUsr->pszFile);
161 free(pUsr);
162 pUsr = NULL; /* just to make sure... */
163 }
164 }
165
166
167 /* print a short list all all source files using the module in question
168 * rgerhards, 2008-03-11
169 */
170 static void modUsrPrint(modInfo_t *pThis) {
171 modUsr_t *pUsr;
172
173 for (pUsr = pThis->pModUsrRoot; pUsr != NULL; pUsr = pUsr->pNext) {
174 dbgprintf("\tmodule %s is currently in use by file %s\n", pThis->pszName, pUsr->pszFile);
175 }
176 }
177
178
179 /* print all loaded modules and who is accessing them. This is primarily intended
180 * to be called at end of run to detect "module leaks" and who is causing them.
181 * rgerhards, 2008-03-11
182 */
183 static void modUsrPrintAll(void) {
184 modInfo_t *pMod;
185
186 for (pMod = pLoadedModules; pMod != NULL; pMod = pMod->pNext) {
187 dbgprintf("printing users of loadable module %s, refcount %u, ptr %p, type %d\n", pMod->pszName, pMod->uRefCnt,
188 pMod, pMod->eType);
189 modUsrPrint(pMod);
190 }
191 }
192
193 #endif /* #ifdef DEBUG */
194
195
196 /* Construct a new module object
197 */
198 static rsRetVal moduleConstruct(modInfo_t **pThis) {
199 modInfo_t *pNew;
200
201 if ((pNew = calloc(1, sizeof(modInfo_t))) == NULL) return RS_RET_OUT_OF_MEMORY;
202
203 /* OK, we got the element, now initialize members that should
204 * not be zero-filled.
205 */
206
207 *pThis = pNew;
208 return RS_RET_OK;
209 }
210
211
212 /* Destructs a module object. The object must not be linked to the
213 * linked list of modules. Please note that all other dependencies on this
214 * modules must have been removed before (e.g. CfSysLineHandlers!)
215 */
216 static void moduleDestruct(modInfo_t *pThis) {
217 assert(pThis != NULL);
218 free(pThis->pszName);
219 free(pThis->cnfName);
220 if (pThis->pModHdlr != NULL) {
221 #ifdef VALGRIND
222 DBGPRINTF(
223 "moduleDestruct: compiled with valgrind, do "
224 "not unload module\n");
225 #else
226 if (glblUnloadModules) {
227 if (pThis->eKeepType == eMOD_NOKEEP) {
228 dlclose(pThis->pModHdlr);
229 }
230 } else {
231 DBGPRINTF(
232 "moduleDestruct: not unloading module "
233 "due to user configuration\n");
234 }
235 #endif
236 }
237
238 free(pThis);
239 }
240
241
242 /* This enables a module to query the core for specific features.
243 * rgerhards, 2009-04-22
244 */
245 static rsRetVal queryCoreFeatureSupport(int *pBool, unsigned uFeat) {
246 DEFiRet;
247
248 if (pBool == NULL) ABORT_FINALIZE(RS_RET_PARAM_ERROR);
249
250 *pBool = (uFeat & CORE_FEATURE_BATCHING) ? 1 : 0;
251
252 finalize_it:
253 RETiRet;
254 }
255
256
257 /* The following function is the queryEntryPoint for host-based entry points.
258 * Modules may call it to get access to core interface functions. Please note
259 * that utility functions can be accessed via shared libraries - at least this
260 * is my current shool of thinking.
261 * Please note that the implementation as a query interface allows one to take
262 * care of plug-in interface version differences. -- rgerhards, 2007-07-31
263 * ... but often it better not to use a new interface. So we now add core
264 * functions here that a plugin may request. -- rgerhards, 2009-04-22
265 */
266 static rsRetVal queryHostEtryPt(uchar *name, rsRetVal (**pEtryPoint)()) {
267 DEFiRet;
268
269 if ((name == NULL) || (pEtryPoint == NULL)) ABORT_FINALIZE(RS_RET_PARAM_ERROR);
270
271 if (!strcmp((char *)name, "regCfSysLineHdlr")) {
272 *pEtryPoint = regCfSysLineHdlr;
273 } else if (!strcmp((char *)name, "objGetObjInterface")) {
274 *pEtryPoint = objGetObjInterface;
275 } else if (!strcmp((char *)name, "OMSRgetSupportedTplOpts")) {
276 *pEtryPoint = OMSRgetSupportedTplOpts;
277 } else if (!strcmp((char *)name, "queryCoreFeatureSupport")) {
278 *pEtryPoint = queryCoreFeatureSupport;
279 } else {
280 *pEtryPoint = NULL; /* to be on the safe side */
281 ABORT_FINALIZE(RS_RET_ENTRY_POINT_NOT_FOUND);
282 }
283
284 finalize_it:
285 RETiRet;
286 }
287
288
289 /* get the name of a module
290 */
291 uchar *modGetName(modInfo_t *pThis) {
292 return ((pThis->pszName == NULL) ? (uchar *)"" : pThis->pszName);
293 }
294
295
296 /* get the state-name of a module. The state name is its name
297 * together with a short description of the module state (which
298 * is pulled from the module itself.
299 * rgerhards, 2007-07-24
300 * TODO: the actual state name is not yet pulled
301 */
302 static uchar *modGetStateName(modInfo_t *pThis) {
303 return (modGetName(pThis));
304 }
305
306
307 /* Add a module to the loaded module linked list
308 */
309 static void ATTR_NONNULL() addModToGlblList(modInfo_t *const pThis) {
310 assert(pThis != NULL);
311
312 if (pLoadedModules == NULL) {
313 pLoadedModules = pLoadedModulesLast = pThis;
314 } else {
315 /* there already exist entries */
316 pThis->pPrev = pLoadedModulesLast;
317 pLoadedModulesLast->pNext = pThis;
318 pLoadedModulesLast = pThis;
319 }
320 }
321
322
323 /* ready module for config processing. this includes checking if the module
324 * is already in the config, so this function may return errors. Returns a
325 * pointer to the last module inthe current config. That pointer needs to
326 * be passed to addModToCnfLst() when it is called later in the process.
327 */
328 rsRetVal readyModForCnf(modInfo_t *pThis, cfgmodules_etry_t **ppNew, cfgmodules_etry_t **ppLast) {
329 cfgmodules_etry_t *pNew = NULL;
330 cfgmodules_etry_t *pLast;
331 DEFiRet;
332 assert(pThis != NULL);
333
334 if (loadConf == NULL) {
335 FINALIZE; /* we are in an early init state */
336 }
337
338 /* check for duplicates and, as a side-activity, identify last node */
339 pLast = loadConf->modules.root;
340 if (pLast != NULL) {
341 while (1) { /* loop broken inside */
342 if (pLast->pMod == pThis) {
343 DBGPRINTF("module '%s' already in this config\n", modGetName(pThis));
344 if (strncmp((char *)modGetName(pThis), "builtin:", sizeof("builtin:") - 1)) {
345 LogError(0, RS_RET_MODULE_ALREADY_IN_CONF, "module '%s' already in this config, cannot be added\n",
346 modGetName(pThis));
347 ABORT_FINALIZE(RS_RET_MODULE_ALREADY_IN_CONF);
348 }
349 FINALIZE;
350 }
351 if (pLast->next == NULL) break;
352 pLast = pLast->next;
353 }
354 }
355
356 /* if we reach this point, pLast is the tail pointer and this module is new
357 * inside the currently loaded config. So, iff it is an input module, let's
358 * pass it a pointer which it can populate with a pointer to its module conf.
359 */
360
361 CHKmalloc(pNew = malloc(sizeof(cfgmodules_etry_t)));
362 pNew->canActivate = 1;
363 pNew->next = NULL;
364 pNew->pMod = pThis;
365
366 if (pThis->beginCnfLoad != NULL) {
367 CHKiRet(pThis->beginCnfLoad(&pNew->modCnf, loadConf));
368 }
369
370 *ppLast = pLast;
371 *ppNew = pNew;
372 finalize_it:
373 if (iRet != RS_RET_OK) {
374 if (pNew != NULL) free(pNew);
375 }
376 RETiRet;
377 }
378
379
380 /* abort the creation of a module entry without adding it to the
381 * module list. Needed to prevent mem leaks.
382 */
383 static inline void abortCnfUse(cfgmodules_etry_t **pNew) {
384 if (pNew != NULL) {
385 free(*pNew);
386 *pNew = NULL;
387 }
388 }
389
390
391 /* Add a module to the config module list for current loadConf.
392 * Requires last pointer obtained by readyModForCnf().
393 * The module pointer is handed over to this function. It is no
394 * longer available to caller one we are called.
395 */
396 rsRetVal ATTR_NONNULL(1) addModToCnfList(cfgmodules_etry_t **const pNew, cfgmodules_etry_t *const pLast) {
397 DEFiRet;
398 assert(*pNew != NULL);
399
400 if (loadConf == NULL) {
401 abortCnfUse(pNew);
402 FINALIZE; /* we are in an early init state */
403 }
404
405 if (pLast == NULL) {
406 loadConf->modules.root = *pNew;
407 } else {
408 /* there already exist entries */
409 pLast->next = *pNew;
410 }
411
412 finalize_it:
413 *pNew = NULL;
414 RETiRet;
415 }
416
417
418 /* Get the next module pointer - this is used to traverse the list.
419 * The function returns the next pointer or NULL, if there is no next one.
420 * The last object must be provided to the function. If NULL is provided,
421 * it starts at the root of the list. Even in this case, NULL may be
422 * returned - then, the list is empty.
423 * rgerhards, 2007-07-23
424 */
425 static modInfo_t *GetNxt(modInfo_t *pThis) {
426 modInfo_t *pNew;
427
428 if (pThis == NULL)
429 pNew = pLoadedModules;
430 else
431 pNew = pThis->pNext;
432
433 return (pNew);
434 }
435
436
437 /* this function is like GetNxt(), but it returns pointers to
438 * the configmodules entry, which than can be used to obtain the
439 * actual module pointer. Note that it returns those for
440 * modules of specific type only. Only modules from the provided
441 * config are returned. Note that processing speed could be improved,
442 * but this is really not relevant, as config file loading is not really
443 * something we are concerned about in regard to runtime.
444 */
445 static cfgmodules_etry_t *GetNxtCnfType(rsconf_t *cnf, cfgmodules_etry_t *node, eModType_t rqtdType) {
446 if (node == NULL) { /* start at beginning of module list */
447 node = cnf->modules.root;
448 } else {
449 node = node->next;
450 }
451
452 if (rqtdType != eMOD_ANY) { /* if any, we already have the right one! */
453 while (node != NULL && node->pMod->eType != rqtdType) {
454 node = node->next;
455 }
456 }
457
458 return node;
459 }
460
461
462 /* Find a module with the given conf name and type. Returns NULL if none
463 * can be found, otherwise module found.
464 */
465 static modInfo_t *FindWithCnfName(rsconf_t *cnf, uchar *name, eModType_t rqtdType) {
466 cfgmodules_etry_t *node;
467
468 ;
469 for (node = cnf->modules.root; node != NULL; node = node->next) {
470 if (node->pMod->eType != rqtdType || node->pMod->cnfName == NULL) continue;
471 if (!strcasecmp((char *)node->pMod->cnfName, (char *)name)) break;
472 }
473
474 return node == NULL ? NULL : node->pMod;
475 }
476
477
478 /* Prepare a module for unloading.
479 * This is currently a dummy, to be filled when we have a plug-in
480 * interface - rgerhards, 2007-08-09
481 * rgerhards, 2007-11-21:
482 * When this function is called, all instance-data must already have
483 * been destroyed. In the case of output modules, this happens when the
484 * rule set is being destroyed. When we implement other module types, we
485 * need to think how we handle it there (and if we have any instance data).
486 * rgerhards, 2008-03-10: reject unload request if the module has a reference
487 * count > 0.
488 */
489 static rsRetVal modPrepareUnload(modInfo_t *pThis) {
490 DEFiRet;
491 void *pModCookie;
492
493 assert(pThis != NULL);
494
495 if (pThis->uRefCnt > 0) {
496 dbgprintf("rejecting unload of module '%s' because it has a refcount of %d\n", pThis->pszName, pThis->uRefCnt);
497 ABORT_FINALIZE(RS_RET_MODULE_STILL_REFERENCED);
498 }
499
500 CHKiRet(pThis->modGetID(&pModCookie));
501 pThis->modExit(); /* tell the module to get ready for unload */
502 CHKiRet(unregCfSysLineHdlrs4Owner(pModCookie));
503
504 finalize_it:
505 RETiRet;
506 }
507
508
509 /* Add an already-loaded module to the module linked list. This function does
510 * everything needed to fully initialize the module.
511 */
512 static rsRetVal doModInit(pModInit_t modInit, uchar *name, void *pModHdlr, modInfo_t **pNewModule) {
513 rsRetVal localRet;
514 modInfo_t *pNew = NULL;
515 uchar *pName;
516 strgen_t *pStrgen; /* used for strgen modules */
517 rsRetVal (*GetName)(uchar **);
518 rsRetVal (*modGetType)(eModType_t *pType);
519 rsRetVal (*modGetKeepType)(eModKeepType_t *pKeepType);
520 struct dlhandle_s *pHandle = NULL;
521 rsRetVal (*getModCnfName)(uchar **cnfName);
522 uchar *cnfName;
523 DEFiRet;
524
525 assert(modInit != NULL);
526
527 if ((iRet = moduleConstruct(&pNew)) != RS_RET_OK) {
528 pNew = NULL;
529 FINALIZE;
530 }
531
532 CHKiRet((*modInit)(CURR_MOD_IF_VERSION, &pNew->iIFVers, &pNew->modQueryEtryPt, queryHostEtryPt, pNew));
533
534 if (pNew->iIFVers != CURR_MOD_IF_VERSION) {
535 ABORT_FINALIZE(RS_RET_MISSING_INTERFACE);
536 }
537
538 /* We now poll the module to see what type it is. We do this only once as this
539 * can never change in the lifetime of an module. -- rgerhards, 2007-12-14
540 */
541 CHKiRet((*pNew->modQueryEtryPt)((uchar *)"getType", &modGetType));
542 CHKiRet((*modGetType)(&pNew->eType));
543 CHKiRet((*pNew->modQueryEtryPt)((uchar *)"getKeepType", &modGetKeepType));
544 CHKiRet((*modGetKeepType)(&pNew->eKeepType));
545 dbgprintf("module %s of type %d being loaded (keepType=%d).\n", name, pNew->eType, pNew->eKeepType);
546
547 /* OK, we know we can successfully work with the module. So we now fill the
548 * rest of the data elements. First we load the interfaces common to all
549 * module types.
550 */
551 CHKiRet((*pNew->modQueryEtryPt)((uchar *)"modGetID", &pNew->modGetID));
552 CHKiRet((*pNew->modQueryEtryPt)((uchar *)"modExit", &pNew->modExit));
553 localRet = (*pNew->modQueryEtryPt)((uchar *)"isCompatibleWithFeature", &pNew->isCompatibleWithFeature);
554 if (localRet == RS_RET_MODULE_ENTRY_POINT_NOT_FOUND)
555 pNew->isCompatibleWithFeature = dummyIsCompatibleWithFeature;
556 else if (localRet != RS_RET_OK)
557 ABORT_FINALIZE(localRet);
558 localRet = (*pNew->modQueryEtryPt)((uchar *)"setModCnf", &pNew->setModCnf);
559 if (localRet == RS_RET_MODULE_ENTRY_POINT_NOT_FOUND)
560 pNew->setModCnf = NULL;
561 else if (localRet != RS_RET_OK)
562 ABORT_FINALIZE(localRet);
563
564 /* optional calls for new config system */
565 localRet = (*pNew->modQueryEtryPt)((uchar *)"getModCnfName", &getModCnfName);
566 if (localRet == RS_RET_OK) {
567 if (getModCnfName(&cnfName) == RS_RET_OK) pNew->cnfName = (uchar *)strdup((char *)cnfName);
568 /**< we do not care if strdup() fails, we can accept that */
569 else
570 pNew->cnfName = NULL;
571 dbgprintf("module config name is '%s'\n", cnfName);
572 }
573 localRet = (*pNew->modQueryEtryPt)((uchar *)"beginCnfLoad", &pNew->beginCnfLoad);
574 if (localRet == RS_RET_OK) {
575 dbgprintf("module %s supports rsyslog v6 config interface\n", name);
576 CHKiRet((*pNew->modQueryEtryPt)((uchar *)"endCnfLoad", &pNew->endCnfLoad));
577 CHKiRet((*pNew->modQueryEtryPt)((uchar *)"freeCnf", &pNew->freeCnf));
578 CHKiRet((*pNew->modQueryEtryPt)((uchar *)"checkCnf", &pNew->checkCnf));
579 CHKiRet((*pNew->modQueryEtryPt)((uchar *)"activateCnf", &pNew->activateCnf));
580 localRet = (*pNew->modQueryEtryPt)((uchar *)"activateCnfPrePrivDrop", &pNew->activateCnfPrePrivDrop);
581 if (localRet == RS_RET_MODULE_ENTRY_POINT_NOT_FOUND) {
582 pNew->activateCnfPrePrivDrop = NULL;
583 } else {
584 CHKiRet(localRet);
585 }
586 } else if (localRet == RS_RET_MODULE_ENTRY_POINT_NOT_FOUND) {
587 pNew->beginCnfLoad = NULL; /* flag as non-present */
588 } else {
589 ABORT_FINALIZE(localRet);
590 }
591 /* ... and now the module-specific interfaces */
592 switch (pNew->eType) {
593 case eMOD_IN:
594 CHKiRet((*pNew->modQueryEtryPt)((uchar *)"runInput", &pNew->mod.im.runInput));
595 CHKiRet((*pNew->modQueryEtryPt)((uchar *)"willRun", &pNew->mod.im.willRun));
596 CHKiRet((*pNew->modQueryEtryPt)((uchar *)"afterRun", &pNew->mod.im.afterRun));
597 pNew->mod.im.bCanRun = 0;
598 localRet = (*pNew->modQueryEtryPt)((uchar *)"newInpInst", &pNew->mod.im.newInpInst);
599 if (localRet == RS_RET_MODULE_ENTRY_POINT_NOT_FOUND) {
600 pNew->mod.im.newInpInst = NULL;
601 } else if (localRet != RS_RET_OK) {
602 ABORT_FINALIZE(localRet);
603 }
604 localRet = (*pNew->modQueryEtryPt)((uchar *)"doHUP", &pNew->doHUP);
605 if (localRet != RS_RET_OK && localRet != RS_RET_MODULE_ENTRY_POINT_NOT_FOUND) ABORT_FINALIZE(localRet);
606
607 break;
608 case eMOD_OUT:
609 CHKiRet((*pNew->modQueryEtryPt)((uchar *)"freeInstance", &pNew->freeInstance));
610 CHKiRet((*pNew->modQueryEtryPt)((uchar *)"dbgPrintInstInfo", &pNew->dbgPrintInstInfo));
611 CHKiRet((*pNew->modQueryEtryPt)((uchar *)"parseSelectorAct", &pNew->mod.om.parseSelectorAct));
612 CHKiRet((*pNew->modQueryEtryPt)((uchar *)"tryResume", &pNew->tryResume));
613 CHKiRet((*pNew->modQueryEtryPt)((uchar *)"createWrkrInstance", &pNew->mod.om.createWrkrInstance));
614 CHKiRet((*pNew->modQueryEtryPt)((uchar *)"freeWrkrInstance", &pNew->mod.om.freeWrkrInstance));
615
616 /* try load optional interfaces */
617 localRet = (*pNew->modQueryEtryPt)((uchar *)"doHUP", &pNew->doHUP);
618 if (localRet != RS_RET_OK && localRet != RS_RET_MODULE_ENTRY_POINT_NOT_FOUND) ABORT_FINALIZE(localRet);
619
620 localRet = (*pNew->modQueryEtryPt)((uchar *)"doHUPWrkr", &pNew->doHUPWrkr);
621 if (localRet != RS_RET_OK && localRet != RS_RET_MODULE_ENTRY_POINT_NOT_FOUND) ABORT_FINALIZE(localRet);
622
623 localRet = (*pNew->modQueryEtryPt)((uchar *)"SetShutdownImmdtPtr", &pNew->mod.om.SetShutdownImmdtPtr);
624 if (localRet != RS_RET_OK && localRet != RS_RET_MODULE_ENTRY_POINT_NOT_FOUND) ABORT_FINALIZE(localRet);
625
626 localRet = (*pNew->modQueryEtryPt)((uchar *)"setActionInfo", &pNew->mod.om.setActionInfo);
627 if (localRet == RS_RET_MODULE_ENTRY_POINT_NOT_FOUND) {
628 pNew->mod.om.setActionInfo = NULL;
629 } else if (localRet != RS_RET_OK) {
630 ABORT_FINALIZE(localRet);
631 }
632
633 pNew->mod.om.supportsTX = 1;
634 localRet = (*pNew->modQueryEtryPt)((uchar *)"beginTransaction", &pNew->mod.om.beginTransaction);
635 if (localRet == RS_RET_MODULE_ENTRY_POINT_NOT_FOUND) {
636 pNew->mod.om.beginTransaction = dummyBeginTransaction;
637 pNew->mod.om.supportsTX = 0;
638 } else if (localRet != RS_RET_OK) {
639 ABORT_FINALIZE(localRet);
640 }
641
642 localRet = (*pNew->modQueryEtryPt)((uchar *)"doAction", &pNew->mod.om.doAction);
643 if (localRet == RS_RET_MODULE_ENTRY_POINT_NOT_FOUND) {
644 pNew->mod.om.doAction = NULL;
645 } else if (localRet != RS_RET_OK) {
646 ABORT_FINALIZE(localRet);
647 }
648
649 localRet = (*pNew->modQueryEtryPt)((uchar *)"commitTransaction", &pNew->mod.om.commitTransaction);
650 if (localRet == RS_RET_MODULE_ENTRY_POINT_NOT_FOUND) {
651 pNew->mod.om.commitTransaction = NULL;
652 } else if (localRet != RS_RET_OK) {
653 ABORT_FINALIZE(localRet);
654 }
655
656 if (pNew->mod.om.doAction == NULL && pNew->mod.om.commitTransaction == NULL) {
657 LogError(0, RS_RET_INVLD_OMOD,
658 "module %s does neither provide doAction() "
659 "nor commitTransaction() interface - cannot "
660 "load",
661 name);
662 ABORT_FINALIZE(RS_RET_INVLD_OMOD);
663 }
664
665 if (pNew->mod.om.commitTransaction != NULL) {
666 if (pNew->mod.om.doAction != NULL) {
667 LogError(0, RS_RET_INVLD_OMOD,
668 "module %s provides both doAction() "
669 "and commitTransaction() interface, using "
670 "commitTransaction()",
671 name);
672 pNew->mod.om.doAction = NULL;
673 }
674 if (pNew->mod.om.beginTransaction == NULL) {
675 LogError(0, RS_RET_INVLD_OMOD,
676 "module %s provides both commitTransaction() "
677 "but does not provide beginTransaction() - "
678 "cannot load",
679 name);
680 ABORT_FINALIZE(RS_RET_INVLD_OMOD);
681 }
682 }
683
684
685 localRet = (*pNew->modQueryEtryPt)((uchar *)"endTransaction", &pNew->mod.om.endTransaction);
686 if (localRet == RS_RET_MODULE_ENTRY_POINT_NOT_FOUND) {
687 pNew->mod.om.endTransaction = dummyEndTransaction;
688 } else if (localRet != RS_RET_OK) {
689 ABORT_FINALIZE(localRet);
690 }
691
692 localRet = (*pNew->modQueryEtryPt)((uchar *)"newActInst", &pNew->mod.om.newActInst);
693 if (localRet == RS_RET_MODULE_ENTRY_POINT_NOT_FOUND) {
694 pNew->mod.om.newActInst = dummynewActInst;
695 } else if (localRet != RS_RET_OK) {
696 ABORT_FINALIZE(localRet);
697 }
698 break;
699 case eMOD_LIB:
700 break;
701 case eMOD_PARSER:
702 localRet = (*pNew->modQueryEtryPt)((uchar *)"parse2", &pNew->mod.pm.parse2);
703 if (localRet == RS_RET_OK) {
704 pNew->mod.pm.parse = NULL;
705 CHKiRet((*pNew->modQueryEtryPt)((uchar *)"newParserInst", &pNew->mod.pm.newParserInst));
706 localRet = (*pNew->modQueryEtryPt)((uchar *)"checkParserInst", &pNew->mod.pm.checkParserInst);
707 if (localRet == RS_RET_MODULE_ENTRY_POINT_NOT_FOUND) {
708 pNew->mod.pm.checkParserInst = NULL;
709 } else {
710 CHKiRet(localRet);
711 }
712 CHKiRet((*pNew->modQueryEtryPt)((uchar *)"freeParserInst", &pNew->mod.pm.freeParserInst));
713 } else if (localRet == RS_RET_MODULE_ENTRY_POINT_NOT_FOUND) {
714 pNew->mod.pm.parse2 = NULL;
715 pNew->mod.pm.newParserInst = NULL;
716 pNew->mod.pm.checkParserInst = NULL;
717 pNew->mod.pm.freeParserInst = NULL;
718 CHKiRet((*pNew->modQueryEtryPt)((uchar *)"parse", &pNew->mod.pm.parse));
719 } else {
720 ABORT_FINALIZE(localRet);
721 }
722 CHKiRet((*pNew->modQueryEtryPt)((uchar *)"GetParserName", &GetName));
723 CHKiRet(GetName(&pName));
724 CHKiRet(parserConstructViaModAndName(pNew, pName, NULL));
725 break;
726 case eMOD_STRGEN:
727 /* first, we need to obtain the strgen object. We could not do that during
728 * init as that would have caused class bootstrap issues which are not
729 * absolutely necessary. Note that we can call objUse() multiple times, it
730 * handles that.
731 */
732 CHKiRet(objUse(strgen, CORE_COMPONENT));
733 CHKiRet((*pNew->modQueryEtryPt)((uchar *)"strgen", &pNew->mod.sm.strgen));
734 CHKiRet((*pNew->modQueryEtryPt)((uchar *)"GetName", &GetName));
735 CHKiRet(GetName(&pName));
736 CHKiRet(strgen.Construct(&pStrgen));
737 CHKiRet(strgen.SetName(pStrgen, pName));
738 CHKiRet(strgen.SetModPtr(pStrgen, pNew));
739 CHKiRet(strgen.ConstructFinalize(pStrgen));
740 break;
741 case eMOD_FUNCTION:
742 CHKiRet((*pNew->modQueryEtryPt)((uchar *)"getFunctArray", &pNew->mod.fm.getFunctArray));
743 int version;
744 struct scriptFunct *functArray;
745 pNew->mod.fm.getFunctArray(&version, &functArray);
746 dbgprintf("LLL: %s\n", functArray[0].fname);
747 addMod2List(version, functArray);
748 break;
749 case eMOD_ANY: /* this is mostly to keep the compiler happy! */
750 DBGPRINTF("PROGRAM ERROR: eMOD_ANY set as module type\n");
751 assert(0);
752 break;
753 default:
754 // No action needed for other cases
755 break;
756 }
757
758 pNew->pszName = (uchar *)strdup((char *)name); /* we do not care if strdup() fails, we can accept that */
759 pNew->pModHdlr = pModHdlr;
760 if (pModHdlr == NULL) {
761 pNew->eLinkType = eMOD_LINK_STATIC;
762 } else {
763 pNew->eLinkType = eMOD_LINK_DYNAMIC_LOADED;
764
765 /* if we need to keep the linked module, save it */
766 if (pNew->eKeepType == eMOD_KEEP) {
767 /* see if we have this one already */
768 for (pHandle = pHandles; pHandle; pHandle = pHandle->next) {
769 if (!strcmp((char *)name, (char *)pHandle->pszName)) break;
770 }
771
772 /* not found, create it */
773 if (!pHandle) {
774 if ((pHandle = malloc(sizeof(*pHandle))) == NULL) {
775 ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
776 }
777 if ((pHandle->pszName = (uchar *)strdup((char *)name)) == NULL) {
778 free(pHandle);
779 ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
780 }
781 pHandle->pModHdlr = pModHdlr;
782 pHandle->next = pHandles;
783
784 pHandles = pHandle;
785 }
786 }
787 }
788
789 /* we initialized the structure, now let's add it to the linked list of modules */
790 addModToGlblList(pNew);
791 *pNewModule = pNew;
792
793 finalize_it:
794 if (iRet != RS_RET_OK) {
795 if (pNew != NULL) moduleDestruct(pNew);
796 *pNewModule = NULL;
797 }
798
799 RETiRet;
800 }
801
802 /* Print loaded modules. This is more or less a
803 * debug or test aid, but anyhow I think it's worth it...
804 * This only works if the dbgprintf() subsystem is initialized.
805 * TODO: update for new input modules!
806 */
807 static void modPrintList(void) {
808 modInfo_t *pMod;
809
810 pMod = GetNxt(NULL);
811 while (pMod != NULL) {
812 dbgprintf("Loaded Module: Name='%s', IFVersion=%d, ", (char *)modGetName(pMod), pMod->iIFVers);
813 dbgprintf("type=");
814 switch (pMod->eType) {
815 case eMOD_OUT:
816 dbgprintf("output");
817 break;
818 case eMOD_IN:
819 dbgprintf("input");
820 break;
821 case eMOD_LIB:
822 dbgprintf("library");
823 break;
824 case eMOD_PARSER:
825 dbgprintf("parser");
826 break;
827 case eMOD_STRGEN:
828 dbgprintf("strgen");
829 break;
830 case eMOD_FUNCTION:
831 dbgprintf("function");
832 break;
833 case eMOD_ANY: /* this is mostly to keep the compiler happy! */
834 DBGPRINTF("PROGRAM ERROR: eMOD_ANY set as module type\n");
835 assert(0);
836 break;
837 default:
838 // No action needed for other cases
839 break;
840 }
841 dbgprintf(" module.\n");
842 dbgprintf("Entry points:\n");
843 dbgprintf("\tqueryEtryPt: 0x%lx\n", (unsigned long)pMod->modQueryEtryPt);
844 dbgprintf("\tdbgPrintInstInfo: 0x%lx\n", (unsigned long)pMod->dbgPrintInstInfo);
845 dbgprintf("\tfreeInstance: 0x%lx\n", (unsigned long)pMod->freeInstance);
846 dbgprintf("\tbeginCnfLoad: 0x%lx\n", (unsigned long)pMod->beginCnfLoad);
847 dbgprintf("\tSetModCnf: 0x%lx\n", (unsigned long)pMod->setModCnf);
848 dbgprintf("\tcheckCnf: 0x%lx\n", (unsigned long)pMod->checkCnf);
849 dbgprintf("\tactivateCnfPrePrivDrop: 0x%lx\n", (unsigned long)pMod->activateCnfPrePrivDrop);
850 dbgprintf("\tactivateCnf: 0x%lx\n", (unsigned long)pMod->activateCnf);
851 dbgprintf("\tfreeCnf: 0x%lx\n", (unsigned long)pMod->freeCnf);
852 switch (pMod->eType) {
853 case eMOD_OUT:
854 dbgprintf("Output Module Entry Points:\n");
855 dbgprintf("\tdoAction: %p\n", pMod->mod.om.doAction);
856 dbgprintf("\tparseSelectorAct: %p\n", pMod->mod.om.parseSelectorAct);
857 dbgprintf("\tnewActInst: %p\n",
858 (pMod->mod.om.newActInst == dummynewActInst) ? NULL : pMod->mod.om.newActInst);
859 dbgprintf("\ttryResume: %p\n", pMod->tryResume);
860 dbgprintf("\tdoHUP: %p\n", pMod->doHUP);
861 dbgprintf(
862 "\tBeginTransaction: %p\n",
863 ((pMod->mod.om.beginTransaction == dummyBeginTransaction) ? NULL : pMod->mod.om.beginTransaction));
864 dbgprintf("\tEndTransaction: %p\n",
865 ((pMod->mod.om.endTransaction == dummyEndTransaction) ? NULL : pMod->mod.om.endTransaction));
866 break;
867 case eMOD_IN:
868 dbgprintf("Input Module Entry Points\n");
869 dbgprintf("\trunInput: 0x%lx\n", (unsigned long)pMod->mod.im.runInput);
870 dbgprintf("\twillRun: 0x%lx\n", (unsigned long)pMod->mod.im.willRun);
871 dbgprintf("\tafterRun: 0x%lx\n", (unsigned long)pMod->mod.im.afterRun);
872 break;
873 case eMOD_LIB:
874 break;
875 case eMOD_PARSER:
876 dbgprintf("Parser Module Entry Points\n");
877 dbgprintf("\tparse: 0x%lx\n", (unsigned long)pMod->mod.pm.parse);
878 dbgprintf("\tparse2: %p\n", pMod->mod.pm.parse2);
879 dbgprintf("\tnewParserInst: %p\n", pMod->mod.pm.newParserInst);
880 dbgprintf("\tcheckParserInst: %p\n", pMod->mod.pm.checkParserInst);
881 dbgprintf("\tfreeParserInst: %p\n", pMod->mod.pm.freeParserInst);
882 break;
883 case eMOD_STRGEN:
884 dbgprintf("Strgen Module Entry Points\n");
885 dbgprintf("\tstrgen: 0x%lx\n", (unsigned long)pMod->mod.sm.strgen);
886 break;
887 case eMOD_FUNCTION:
888 dbgprintf("Function Module Entry Points\n");
889 dbgprintf("\tgetFunctArray: 0x%lx\n", (unsigned long)pMod->mod.fm.getFunctArray);
890 break;
891 case eMOD_ANY: /* this is mostly to keep the compiler happy! */
892 break;
893 default:
894 // No action needed for other cases
895 break;
896 }
897 dbgprintf("\n");
898 pMod = GetNxt(pMod); /* done, go next */
899 }
900 }
901
902
903 /* HUP all modules that support it - except for actions, which
904 * need (and have) specialised HUP handling.
905 */
906 void modDoHUP(void) {
907 modInfo_t *pMod;
908
909 pthread_mutex_lock(&mutObjGlobalOp);
910 pMod = GetNxt(NULL);
911 while (pMod != NULL) {
912 if (pMod->eType != eMOD_OUT && pMod->doHUP != NULL) {
913 DBGPRINTF("HUPing module %s\n", (char *)modGetName(pMod));
914 pMod->doHUP(NULL);
915 }
916 pMod = GetNxt(pMod); /* done, go next */
917 }
918 pthread_mutex_unlock(&mutObjGlobalOp);
919 }
920
921
922 /* unlink and destroy a module. The caller must provide a pointer to the module
923 * itself as well as one to its immediate predecessor.
924 * rgerhards, 2008-02-26
925 */
926 static rsRetVal modUnlinkAndDestroy(modInfo_t **ppThis) {
927 DEFiRet;
928 modInfo_t *pThis;
929
930 assert(ppThis != NULL);
931 pThis = *ppThis;
932 assert(pThis != NULL);
933
934 pthread_mutex_lock(&mutObjGlobalOp);
935
936 /* first check if we are permitted to unload */
937 if (pThis->eType == eMOD_LIB) {
938 if (pThis->uRefCnt > 0) {
939 dbgprintf("module %s NOT unloaded because it still has a refcount of %u\n", pThis->pszName, pThis->uRefCnt);
940 ABORT_FINALIZE(RS_RET_MODULE_STILL_REFERENCED);
941 }
942 }
943
944 /* we need to unlink the module before we can destruct it -- rgerhards, 2008-02-26 */
945 if (pThis->pPrev == NULL) {
946 /* module is root, so we need to set a new root */
947 pLoadedModules = pThis->pNext;
948 } else {
949 pThis->pPrev->pNext = pThis->pNext;
950 }
951
952 if (pThis->pNext == NULL) {
953 pLoadedModulesLast = pThis->pPrev;
954 } else {
955 pThis->pNext->pPrev = pThis->pPrev;
956 }
957
958 /* finally, we are ready for the module to go away... */
959 dbgprintf("Unloading module %s\n", modGetName(pThis));
960 CHKiRet(modPrepareUnload(pThis));
961 *ppThis = pThis->pNext;
962
963 moduleDestruct(pThis);
964
965 finalize_it:
966 pthread_mutex_unlock(&mutObjGlobalOp);
967 RETiRet;
968 }
969
970
971 /* unload all loaded modules of a specific type (use eMOD_ALL if you want to
972 * unload all module types). The unload happens only if the module is no longer
973 * referenced. So some modules may survive this call.
974 * rgerhards, 2008-03-11
975 */
976 static rsRetVal modUnloadAndDestructAll(eModLinkType_t modLinkTypesToUnload) {
977 DEFiRet;
978 modInfo_t *pModCurr; /* module currently being processed */
979
980 pModCurr = GetNxt(NULL);
981 while (pModCurr != NULL) {
982 if (modLinkTypesToUnload == eMOD_LINK_ALL || pModCurr->eLinkType == modLinkTypesToUnload) {
983 if (modUnlinkAndDestroy(&pModCurr) == RS_RET_MODULE_STILL_REFERENCED) {
984 pModCurr = GetNxt(pModCurr);
985 } else {
986 /* Note: if the module was successfully unloaded, it has updated the
987 * pModCurr pointer to the next module. However, the unload process may
988 * still have indirectly referenced the pointer list in a way that the
989 * unloaded module is not aware of. So we restart the unload process
990 * to make sure we do not fall into a trap (what we did ;)). The
991 * performance toll is minimal. -- rgerhards, 2008-04-28
992 */
993 pModCurr = GetNxt(NULL);
994 }
995 } else {
996 pModCurr = GetNxt(pModCurr);
997 }
998 }
999
1000 RETiRet;
1001 }
1002
1003 /* find module with given name in global list */
1004 static rsRetVal findModule(uchar *pModName, int iModNameLen, modInfo_t **pMod) {
1005 modInfo_t *pModInfo;
1006 uchar *pModNameCmp;
1007 DEFiRet;
1008
1009 pModInfo = GetNxt(NULL);
1010 while (pModInfo != NULL) {
1011 if (!strncmp((char *)pModName, (char *)(pModNameCmp = modGetName(pModInfo)), iModNameLen) &&
1012 (!*(pModNameCmp + iModNameLen) || !strcmp((char *)pModNameCmp + iModNameLen, ".so"))) {
1013 dbgprintf("Module '%s' found\n", pModName);
1014 break;
1015 }
1016 pModInfo = GetNxt(pModInfo);
1017 }
1018 *pMod = pModInfo;
1019 RETiRet;
1020 }
1021
1022
1023 /* load a module and initialize it, based on doModLoad() from conf.c
1024 * rgerhards, 2008-03-05
1025 * varmojfekoj added support for dynamically loadable modules on 2007-08-13
1026 * rgerhards, 2007-09-25: please note that the non-threadsafe function dlerror() is
1027 * called below. This is ok because modules are currently only loaded during
1028 * configuration file processing, which is executed on a single thread. Should we
1029 * change that design at any stage (what is unlikely), we need to find a
1030 * replacement.
1031 * rgerhards, 2011-04-27:
1032 * Parameter "bConfLoad" tells us if the load was triggered by a config handler, in
1033 * which case we need to tie the loaded module to the current config. If bConfLoad == 0,
1034 * the system loads a module for internal reasons, this is not directly tied to a
1035 * configuration. We could also think if it would be useful to add only certain types
1036 * of modules, but the current implementation at least looks simpler.
1037 * Note: pvals = NULL means legacy config system
1038 */
1039 static rsRetVal ATTR_NONNULL(1) Load(uchar *const pModName, const sbool bConfLoad, struct nvlst *const lst) {
1040 size_t iPathLen, iModNameLen;
1041 int bHasExtension;
1042 void *pModHdlr;
1043 pModInit_t pModInit;
1044 modInfo_t *pModInfo;
1045 cfgmodules_etry_t *pNew = NULL;
1046 cfgmodules_etry_t *pLast = NULL;
1047 uchar *pModDirCurr, *pModDirNext;
1048 struct dlhandle_s *pHandle = NULL;
1049 uchar pathBuf[PATH_MAX + 1];
1050 uchar *pPathBuf = pathBuf;
1051 size_t lenPathBuf = sizeof(pathBuf);
1052 rsRetVal localRet;
1053 cstr_t *load_err_msg = NULL;
1054 DEFiRet;
1055
1056 assert(pModName != NULL);
1057 DBGPRINTF("Requested to load module '%s'\n", pModName);
1058
1059 iModNameLen = strlen((char *)pModName);
1060 /* overhead for a full path is potentially 1 byte for a slash,
1061 * three bytes for ".so" and one byte for '\0'.
1062 */
1063 #define PATHBUF_OVERHEAD 1 + iModNameLen + 3 + 1
1064
1065 pthread_mutex_lock(&mutObjGlobalOp);
1066
1067 if (iModNameLen > 3 && !strcmp((char *)pModName + iModNameLen - 3, ".so")) {
1068 iModNameLen -= 3;
1069 bHasExtension = RSTRUE;
1070 } else
1071 bHasExtension = RSFALSE;
1072
1073 CHKiRet(findModule(pModName, iModNameLen, &pModInfo));
1074 if (pModInfo != NULL) {
1075 DBGPRINTF("Module '%s' already loaded\n", pModName);
1076 if (bConfLoad) {
1077 localRet = readyModForCnf(pModInfo, &pNew, &pLast);
1078 if (pModInfo->setModCnf != NULL && localRet == RS_RET_OK) {
1079 if (!strncmp((char *)pModName, "builtin:", sizeof("builtin:") - 1)) {
1080 if (pModInfo->bSetModCnfCalled) {
1081 LogError(0, RS_RET_DUP_PARAM, "parameters for built-in module %s already set - ignored\n",
1082 pModName);
1083 ABORT_FINALIZE(RS_RET_DUP_PARAM);
1084 } else {
1085 /* for built-in moules, we need to call setModConf,
1086 * because there is no way to set parameters at load
1087 * time for obvious reasons...
1088 */
1089 if (lst != NULL) pModInfo->setModCnf(lst);
1090 pModInfo->bSetModCnfCalled = 1;
1091 }
1092 } else {
1093 /* regular modules need to be added to conf list (for
1094 * builtins, this happend during initial load).
1095 */
1096 addModToCnfList(&pNew, pLast);
1097 }
1098 }
1099 }
1100 FINALIZE;
1101 }
1102
1103 pModDirCurr = (uchar *)((pModDir == NULL) ? _PATH_MODDIR : (char *)pModDir);
1104 pModDirNext = NULL;
1105 pModHdlr = NULL;
1106 do { /* now build our load module name */
1107 if (*pModName == '/' || *pModName == '.') {
1108 if (lenPathBuf < PATHBUF_OVERHEAD) {
1109 if (pPathBuf != pathBuf) /* already malloc()ed memory? */
1110 free(pPathBuf);
1111 /* we always alloc enough memory for everything we potentiall need to add */
1112 lenPathBuf = PATHBUF_OVERHEAD;
1113 CHKmalloc(pPathBuf = malloc(lenPathBuf));
1114 }
1115 *pPathBuf = '\0'; /* we do not need to append the path - its already in the module name */
1116 iPathLen = 0;
1117 } else {
1118 *pPathBuf = '\0';
1119
1120 iPathLen = strlen((char *)pModDirCurr);
1121 pModDirNext = (uchar *)strchr((char *)pModDirCurr, ':');
1122 if (pModDirNext) iPathLen = (size_t)(pModDirNext - pModDirCurr);
1123
1124 if (iPathLen == 0) {
1125 if (pModDirNext) {
1126 pModDirCurr = pModDirNext + 1;
1127 continue;
1128 }
1129 break;
1130 } else if (iPathLen > lenPathBuf - PATHBUF_OVERHEAD) {
1131 if (pPathBuf != pathBuf) /* already malloc()ed memory? */
1132 free(pPathBuf);
1133 /* we always alloc enough memory for everything we potentiall need to add */
1134 lenPathBuf = iPathLen + PATHBUF_OVERHEAD;
1135 CHKmalloc(pPathBuf = malloc(lenPathBuf));
1136 }
1137
1138 memcpy((char *)pPathBuf, (char *)pModDirCurr, iPathLen);
1139 if ((pPathBuf[iPathLen - 1] != '/')) {
1140 /* we have space, made sure in previous check */
1141 pPathBuf[iPathLen++] = '/';
1142 }
1143 pPathBuf[iPathLen] = '\0';
1144
1145 if (pModDirNext) pModDirCurr = pModDirNext + 1;
1146 }
1147
1148 /* ... add actual name ... */
1149 strncat((char *)pPathBuf, (char *)pModName, lenPathBuf - iPathLen - 1);
1150
1151 /* now see if we have an extension and, if not, append ".so" */
1152 if (!bHasExtension) {
1153 /* we do not have an extension and so need to add ".so"
1154 * TODO: I guess this is highly importable, so we should change the
1155 * algo over time... -- rgerhards, 2008-03-05
1156 */
1157 strncat((char *)pPathBuf, ".so", lenPathBuf - strlen((char *)pPathBuf) - 1);
1158 }
1159
1160 /* complete load path constructed, so ... GO! */
1161 dbgprintf("loading module '%s'\n", pPathBuf);
1162
1163 /* see if we have this one already */
1164 for (pHandle = pHandles; pHandle; pHandle = pHandle->next) {
1165 if (!strcmp((char *)pModName, (char *)pHandle->pszName)) {
1166 pModHdlr = pHandle->pModHdlr;
1167 break;
1168 }
1169 }
1170
1171 /* not found, try to dynamically link it */
1172 if (!pModHdlr) {
1173 pModHdlr = dlopen((char *)pPathBuf, RTLD_NOW);
1174 }
1175
1176 if (pModHdlr == NULL) {
1177 char errmsg[4096];
1178 snprintf(errmsg, sizeof(errmsg), "%strying to load module %s: %s",
1179 (load_err_msg == NULL) ? "" : " //////// ", pPathBuf, dlerror());
1180 if (load_err_msg == NULL) {
1181 rsCStrConstructFromszStr(&load_err_msg, (uchar *)errmsg);
1182 } else {
1183 rsCStrAppendStr(load_err_msg, (uchar *)errmsg);
1184 }
1185 }
1186
1187 } while (pModHdlr == NULL && *pModName != '/' && pModDirNext);
1188
1189 if (load_err_msg != NULL) {
1190 cstrFinalize(load_err_msg);
1191 }
1192
1193 if (!pModHdlr) {
1194 LogError(0, RS_RET_MODULE_LOAD_ERR_DLOPEN, "could not load module '%s', errors: %s", pModName,
1195 (load_err_msg == NULL) ? "NONE SEEN???" : (const char *)cstrGetSzStrNoNULL(load_err_msg));
1196 ABORT_FINALIZE(RS_RET_MODULE_LOAD_ERR_DLOPEN);
1197 }
1198 if (!(pModInit = (pModInit_t)dlsym(pModHdlr, "modInit"))) {
1199 LogError(0, RS_RET_MODULE_LOAD_ERR_NO_INIT, "could not load module '%s', dlsym: %s\n", pPathBuf, dlerror());
1200 dlclose(pModHdlr);
1201 ABORT_FINALIZE(RS_RET_MODULE_LOAD_ERR_NO_INIT);
1202 }
1203 if ((iRet = doModInit(pModInit, (uchar *)pModName, pModHdlr, &pModInfo)) != RS_RET_OK) {
1204 LogError(0, RS_RET_MODULE_LOAD_ERR_INIT_FAILED, "could not load module '%s', rsyslog error %d\n", pPathBuf,
1205 iRet);
1206 dlclose(pModHdlr);
1207 ABORT_FINALIZE(RS_RET_MODULE_LOAD_ERR_INIT_FAILED);
1208 }
1209
1210 if (bConfLoad) {
1211 readyModForCnf(pModInfo, &pNew, &pLast);
1212 if (pModInfo->setModCnf != NULL) {
1213 if (lst != NULL) {
1214 localRet = pModInfo->setModCnf(lst);
1215 if (localRet != RS_RET_OK) {
1216 LogError(0, localRet, "module '%s', failed processing config parameters", pPathBuf);
1217 ABORT_FINALIZE(localRet);
1218 }
1219 }
1220 pModInfo->bSetModCnfCalled = 1;
1221 }
1222 addModToCnfList(&pNew, pLast);
1223 }
1224
1225 finalize_it:
1226 if (load_err_msg != NULL) {
1227 cstrDestruct(&load_err_msg);
1228 }
1229 if (pPathBuf != pathBuf) /* used malloc()ed memory? */
1230 free(pPathBuf);
1231 if (iRet != RS_RET_OK) abortCnfUse(&pNew);
1232 free(pNew); /* is NULL again if properly consumed, else clean up */
1233 pthread_mutex_unlock(&mutObjGlobalOp);
1234 RETiRet;
1235 }
1236
1237
1238 /* the v6+ way of loading modules: process a "module(...)" directive.
1239 * rgerhards, 2012-06-20
1240 */
1241 rsRetVal modulesProcessCnf(struct cnfobj *o) {
1242 struct cnfparamvals *pvals;
1243 uchar *cnfModName = NULL;
1244 int typeIdx;
1245 DEFiRet;
1246
1247 pvals = nvlstGetParams(o->nvlst, &pblk, NULL);
1248 if (pvals == NULL) {
1249 ABORT_FINALIZE(RS_RET_ERR);
1250 }
1251 DBGPRINTF("modulesProcessCnf params:\n");
1252 cnfparamsPrint(&pblk, pvals);
1253 typeIdx = cnfparamGetIdx(&pblk, "load");
1254 if (pvals[typeIdx].bUsed == 0) {
1255 LogError(0, RS_RET_CONF_RQRD_PARAM_MISSING, "module type missing");
1256 ABORT_FINALIZE(RS_RET_CONF_RQRD_PARAM_MISSING);
1257 }
1258
1259 cnfModName = (uchar *)es_str2cstr(pvals[typeIdx].val.d.estr, NULL);
1260 iRet = Load(cnfModName, 1, o->nvlst);
1261
1262 finalize_it:
1263 free(cnfModName);
1264 cnfparamvalsDestruct(pvals, &pblk);
1265 RETiRet;
1266 }
1267
1268
1269 /* set the default module load directory. A NULL value may be provided, in
1270 * which case any previous value is deleted but no new one set. The caller-provided
1271 * string is duplicated. If it needs to be freed, that's the caller's duty.
1272 * rgerhards, 2008-03-07
1273 */
1274 static rsRetVal SetModDir(uchar *pszModDir) {
1275 DEFiRet;
1276
1277 dbgprintf("setting default module load directory '%s'\n", pszModDir);
1278 if (pModDir != NULL) {
1279 free(pModDir);
1280 }
1281
1282 pModDir = (uchar *)strdup((char *)pszModDir);
1283
1284 RETiRet;
1285 }
1286
1287
1288 /* Reference-Counting object access: add 1 to the current reference count. Must be
1289 * called by anyone interested in using a module. -- rgerhards, 20080-03-10
1290 */
1291 static rsRetVal Use(const char *srcFile, modInfo_t *pThis) {
1292 DEFiRet;
1293
1294 assert(pThis != NULL);
1295 pThis->uRefCnt++;
1296 dbgprintf("source file %s requested reference for module '%s', reference count now %u\n", srcFile, pThis->pszName,
1297 pThis->uRefCnt);
1298
1299 #ifdef DEBUG
1300 modUsrAdd(pThis, srcFile);
1301 #endif
1302
1303 RETiRet;
1304 }
1305
1306
1307 /* Reference-Counting object access: subract one from the current refcount. Must
1308 * by called by anyone who no longer needs a module. If count reaches 0, the
1309 * module is unloaded. -- rgerhards, 20080-03-10
1310 */
1311 static rsRetVal Release(const char *srcFile, modInfo_t **ppThis) {
1312 DEFiRet;
1313 modInfo_t *pThis;
1314
1315 assert(ppThis != NULL);
1316 pThis = *ppThis;
1317 assert(pThis != NULL);
1318 if (pThis->uRefCnt == 0) {
1319 /* oops, we are already at 0? */
1320 dbgprintf("internal error: module '%s' already has a refcount of 0 (released by %s)!\n", pThis->pszName,
1321 srcFile);
1322 } else {
1323 --pThis->uRefCnt;
1324 dbgprintf("file %s released module '%s', reference count now %u\n", srcFile, pThis->pszName, pThis->uRefCnt);
1325 #ifdef DEBUG
1326 modUsrDel(pThis, srcFile);
1327 modUsrPrint(pThis);
1328 #endif
1329 }
1330
1331 if (pThis->uRefCnt == 0) {
1332 /* we have a zero refcount, so we must unload the module */
1333 dbgprintf("module '%s' has zero reference count, unloading...\n", pThis->pszName);
1334 modUnlinkAndDestroy(&pThis);
1335 /* we must NOT do a *ppThis = NULL, because ppThis now points into freed memory!
1336 * If in doubt, see obj.c::ReleaseObj() for how we are called.
1337 */
1338 }
1339
1340 RETiRet;
1341 }
1342
1343
1344 /* exit our class
1345 * rgerhards, 2008-03-11
1346 */
1347 BEGINObjClassExit(module, OBJ_IS_LOADABLE_MODULE) /* CHANGE class also in END MACRO! */
1348 CODESTARTObjClassExit(module);
1349 /* release objects we no longer need */
1350 free(pModDir);
1351 #ifdef DEBUG
1352 modUsrPrintAll(); /* debug aid - TODO: integrate with debug.c, at least the settings! */
1353 #endif
1354 ENDObjClassExit(module)
1355
1356
1357 /* queryInterface function
1358 * rgerhards, 2008-03-05
1359 */
1360 BEGINobjQueryInterface(module)
1361 CODESTARTobjQueryInterface(module);
1362 if (pIf->ifVersion != moduleCURR_IF_VERSION) { /* check for current version, increment on each change */
1363 ABORT_FINALIZE(RS_RET_INTERFACE_NOT_SUPPORTED);
1364 }
1365
1366 /* ok, we have the right interface, so let's fill it
1367 * Please note that we may also do some backwards-compatibility
1368 * work here (if we can support an older interface version - that,
1369 * of course, also affects the "if" above).
1370 */
1371 pIf->GetNxt = GetNxt;
1372 pIf->GetNxtCnfType = GetNxtCnfType;
1373 pIf->GetName = modGetName;
1374 pIf->GetStateName = modGetStateName;
1375 pIf->PrintList = modPrintList;
1376 pIf->FindWithCnfName = FindWithCnfName;
1377 pIf->UnloadAndDestructAll = modUnloadAndDestructAll;
1378 pIf->doModInit = doModInit;
1379 pIf->SetModDir = SetModDir;
1380 pIf->Load = Load;
1381 pIf->Use = Use;
1382 pIf->Release = Release;
1383 finalize_it:
1384 ENDobjQueryInterface(module)
1385
1386
1387 /* Initialize our class. Must be called as the very first method
1388 * before anything else is called inside this class.
1389 * rgerhards, 2008-03-05
1390 */
1391 BEGINAbstractObjClassInit(module, 1, OBJ_IS_CORE_MODULE) /* class, version - CHANGE class also in END MACRO! */
1392 uchar *pModPath;
1393
1394 /* use any module load path specified in the environment */
1395 if ((pModPath = (uchar *)getenv("RSYSLOG_MODDIR")) != NULL) {
1396 SetModDir(pModPath);
1397 }
1398
1399 /* now check if another module path was set via the command line (-M)
1400 * if so, that overrides the environment. Please note that we must use
1401 * a global setting here because the command line parser can NOT call
1402 * into the module object, because it is not initialized at that point. So
1403 * instead a global setting is changed and we pick it up as soon as we
1404 * initialize -- rgerhards, 2008-04-04
1405 */
1406 if (glblModPath != NULL) {
1407 SetModDir(glblModPath);
1408 }
1409
1410 /* request objects we use */
1411 ENDObjClassInit(module)
1412
1413 /* vi:set ai:
1414 */