2
* This is the implementation of syslogd modules object.
3
* This object handles plug-ins and build-in modules of all kind.
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
12
* File begun on 2007-07-22 by RGerhards
14
* Copyright 2007 Rainer Gerhards and Adiscon GmbH.
16
* This file is part of the rsyslog runtime library.
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.
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.
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/>.
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.
47
#include <dlfcn.h> /* TODO: replace this with the libtools equivalent! */
52
#include "cfsysline.h"
60
static modInfo_t *pLoadedModules = NULL; /* list of currently-loaded modules */
61
static modInfo_t *pLoadedModulesLast = NULL; /* tail-pointer */
64
uchar *pModDir = NULL; /* read-only after startup */
68
/* we add some home-grown support to track our users (and detect who does not free us). In
69
* the long term, this should probably be migrated into debug.c (TODO). -- rgerhards, 2008-03-11
72
/* add a user to the current list of users (always at the root) */
74
modUsrAdd(modInfo_t *pThis, char *pszUsr)
79
if((pUsr = calloc(1, sizeof(modUsr_t))) == NULL)
82
if((pUsr->pszFile = strdup(pszUsr)) == NULL) {
87
if(pThis->pModUsrRoot != NULL) {
88
pUsr->pNext = pThis->pModUsrRoot;
90
pThis->pModUsrRoot = pUsr;
97
/* remove a user from the current user list
98
* rgerhards, 2008-03-11
101
modUsrDel(modInfo_t *pThis, char *pszUsr)
104
modUsr_t *pPrev = NULL;
106
for(pUsr = pThis->pModUsrRoot ; pUsr != NULL ; pUsr = pUsr->pNext) {
107
if(!strcmp(pUsr->pszFile, pszUsr))
114
dbgprintf("oops - tried to delete user %s from module %s and it wasn't registered as one...\n",
115
pszUsr, pThis->pszName);
118
/* This was at the root! */
119
pThis->pModUsrRoot = pUsr->pNext;
121
pPrev->pNext = pUsr->pNext;
123
/* free ressources */
126
pUsr = NULL; /* just to make sure... */
131
/* print a short list all all source files using the module in question
132
* rgerhards, 2008-03-11
135
modUsrPrint(modInfo_t *pThis)
139
for(pUsr = pThis->pModUsrRoot ; pUsr != NULL ; pUsr = pUsr->pNext) {
140
dbgprintf("\tmodule %s is currently in use by file %s\n",
141
pThis->pszName, pUsr->pszFile);
146
/* print all loaded modules and who is accessing them. This is primarily intended
147
* to be called at end of run to detect "module leaks" and who is causing them.
148
* rgerhards, 2008-03-11
157
for(pMod = pLoadedModules ; pMod != NULL ; pMod = pMod->pNext) {
158
dbgprintf("printing users of loadable module %s, refcount %u, ptr %p, type %d\n", pMod->pszName, pMod->uRefCnt, pMod, pMod->eType);
164
#endif /* #ifdef DEBUG */
167
/* Construct a new module object
169
static rsRetVal moduleConstruct(modInfo_t **pThis)
173
if((pNew = calloc(1, sizeof(modInfo_t))) == NULL)
174
return RS_RET_OUT_OF_MEMORY;
176
/* OK, we got the element, now initialize members that should
177
* not be zero-filled.
185
/* Destructs a module object. The object must not be linked to the
186
* linked list of modules. Please note that all other dependencies on this
187
* modules must have been removed before (e.g. CfSysLineHandlers!)
189
static void moduleDestruct(modInfo_t *pThis)
191
assert(pThis != NULL);
192
if(pThis->pszName != NULL)
193
free(pThis->pszName);
194
if(pThis->pModHdlr != NULL) {
196
# warning "dlclose disabled for valgrind"
198
dlclose(pThis->pModHdlr);
206
/* The following function is the queryEntryPoint for host-based entry points.
207
* Modules may call it to get access to core interface functions. Please note
208
* that utility functions can be accessed via shared libraries - at least this
209
* is my current shool of thinking.
210
* Please note that the implementation as a query interface allows to take
211
* care of plug-in interface version differences. -- rgerhards, 2007-07-31
213
static rsRetVal queryHostEtryPt(uchar *name, rsRetVal (**pEtryPoint)())
217
if((name == NULL) || (pEtryPoint == NULL))
218
return RS_RET_PARAM_ERROR;
220
if(!strcmp((char*) name, "regCfSysLineHdlr")) {
221
*pEtryPoint = regCfSysLineHdlr;
222
} else if(!strcmp((char*) name, "objGetObjInterface")) {
223
*pEtryPoint = objGetObjInterface;
225
*pEtryPoint = NULL; /* to be on the safe side */
226
ABORT_FINALIZE(RS_RET_ENTRY_POINT_NOT_FOUND);
234
/* get the name of a module
236
static uchar *modGetName(modInfo_t *pThis)
238
return((pThis->pszName == NULL) ? (uchar*) "" : pThis->pszName);
242
/* get the state-name of a module. The state name is its name
243
* together with a short description of the module state (which
244
* is pulled from the module itself.
245
* rgerhards, 2007-07-24
246
* TODO: the actual state name is not yet pulled
248
static uchar *modGetStateName(modInfo_t *pThis)
250
return(modGetName(pThis));
254
/* Add a module to the loaded module linked list
257
addModToList(modInfo_t *pThis)
259
assert(pThis != NULL);
261
if(pLoadedModules == NULL) {
262
pLoadedModules = pLoadedModulesLast = pThis;
264
/* there already exist entries */
265
pThis->pPrev = pLoadedModulesLast;
266
pLoadedModulesLast->pNext = pThis;
267
pLoadedModulesLast = pThis;
272
/* Get the next module pointer - this is used to traverse the list.
273
* The function returns the next pointer or NULL, if there is no next one.
274
* The last object must be provided to the function. If NULL is provided,
275
* it starts at the root of the list. Even in this case, NULL may be
276
* returned - then, the list is empty.
277
* rgerhards, 2007-07-23
279
static modInfo_t *GetNxt(modInfo_t *pThis)
284
pNew = pLoadedModules;
292
/* this function is like GetNxt(), but it returns pointers to
293
* modules of specific type only. As we currently deal just with output modules,
294
* it is a dummy, to be filled with real code later.
295
* rgerhards, 2007-07-24
297
static modInfo_t *GetNxtType(modInfo_t *pThis, eModType_t rqtdType)
299
modInfo_t *pMod = pThis;
303
} while(!(pMod == NULL || pMod->eType == rqtdType)); /* warning: do ... while() */
309
/* Prepare a module for unloading.
310
* This is currently a dummy, to be filled when we have a plug-in
311
* interface - rgerhards, 2007-08-09
312
* rgerhards, 2007-11-21:
313
* When this function is called, all instance-data must already have
314
* been destroyed. In the case of output modules, this happens when the
315
* rule set is being destroyed. When we implement other module types, we
316
* need to think how we handle it there (and if we have any instance data).
317
* rgerhards, 2008-03-10: reject unload request if the module has a reference
321
modPrepareUnload(modInfo_t *pThis)
326
assert(pThis != NULL);
328
if(pThis->uRefCnt > 0) {
329
dbgprintf("rejecting unload of module '%s' because it has a refcount of %d\n",
330
pThis->pszName, pThis->uRefCnt);
331
ABORT_FINALIZE(RS_RET_MODULE_STILL_REFERENCED);
334
CHKiRet(pThis->modGetID(&pModCookie));
335
pThis->modExit(); /* tell the module to get ready for unload */
336
CHKiRet(unregCfSysLineHdlrs4Owner(pModCookie));
343
/* Add an already-loaded module to the module linked list. This function does
344
* everything needed to fully initialize the module.
347
doModInit(rsRetVal (*modInit)(int, int*, rsRetVal(**)(), rsRetVal(*)(), modInfo_t*), uchar *name, void *pModHdlr)
350
modInfo_t *pNew = NULL;
351
rsRetVal (*modGetType)(eModType_t *pType);
353
assert(modInit != NULL);
355
if((iRet = moduleConstruct(&pNew)) != RS_RET_OK) {
357
ABORT_FINALIZE(iRet);
360
CHKiRet((*modInit)(CURR_MOD_IF_VERSION, &pNew->iIFVers, &pNew->modQueryEtryPt, queryHostEtryPt, pNew));
362
if(pNew->iIFVers != CURR_MOD_IF_VERSION) {
363
ABORT_FINALIZE(RS_RET_MISSING_INTERFACE);
366
/* We now poll the module to see what type it is. We do this only once as this
367
* can never change in the lifetime of an module. -- rgerhards, 2007-12-14
369
CHKiRet((*pNew->modQueryEtryPt)((uchar*)"getType", &modGetType));
370
CHKiRet((iRet = (*modGetType)(&pNew->eType)) != RS_RET_OK);
371
dbgprintf("module of type %d being loaded.\n", pNew->eType);
373
/* OK, we know we can successfully work with the module. So we now fill the
374
* rest of the data elements. First we load the interfaces common to all
377
CHKiRet((*pNew->modQueryEtryPt)((uchar*)"modGetID", &pNew->modGetID));
378
CHKiRet((*pNew->modQueryEtryPt)((uchar*)"modExit", &pNew->modExit));
380
/* ... and now the module-specific interfaces */
381
switch(pNew->eType) {
383
CHKiRet((*pNew->modQueryEtryPt)((uchar*)"runInput", &pNew->mod.im.runInput));
384
CHKiRet((*pNew->modQueryEtryPt)((uchar*)"willRun", &pNew->mod.im.willRun));
385
CHKiRet((*pNew->modQueryEtryPt)((uchar*)"afterRun", &pNew->mod.im.afterRun));
388
CHKiRet((*pNew->modQueryEtryPt)((uchar*)"freeInstance", &pNew->freeInstance));
389
CHKiRet((*pNew->modQueryEtryPt)((uchar*)"dbgPrintInstInfo", &pNew->dbgPrintInstInfo));
390
CHKiRet((*pNew->modQueryEtryPt)((uchar*)"doAction", &pNew->mod.om.doAction));
391
CHKiRet((*pNew->modQueryEtryPt)((uchar*)"parseSelectorAct", &pNew->mod.om.parseSelectorAct));
392
CHKiRet((*pNew->modQueryEtryPt)((uchar*)"isCompatibleWithFeature", &pNew->isCompatibleWithFeature));
393
CHKiRet((*pNew->modQueryEtryPt)((uchar*)"tryResume", &pNew->tryResume));
399
pNew->pszName = (uchar*) strdup((char*)name); /* we do not care if strdup() fails, we can accept that */
400
pNew->pModHdlr = pModHdlr;
401
/* TODO: take this from module */
403
pNew->eLinkType = eMOD_LINK_STATIC;
405
pNew->eLinkType = eMOD_LINK_DYNAMIC_LOADED;
407
/* we initialized the structure, now let's add it to the linked list of modules */
411
if(iRet != RS_RET_OK) {
413
moduleDestruct(pNew);
419
/* Print loaded modules. This is more or less a
420
* debug or test aid, but anyhow I think it's worth it...
421
* This only works if the dbgprintf() subsystem is initialized.
422
* TODO: update for new input modules!
424
static void modPrintList(void)
429
while(pMod != NULL) {
430
dbgprintf("Loaded Module: Name='%s', IFVersion=%d, ",
431
(char*) modGetName(pMod), pMod->iIFVers);
433
switch(pMod->eType) {
441
dbgprintf("library");
444
dbgprintf(" module.\n");
445
dbgprintf("Entry points:\n");
446
dbgprintf("\tqueryEtryPt: 0x%lx\n", (unsigned long) pMod->modQueryEtryPt);
447
dbgprintf("\tdoAction: 0x%lx\n", (unsigned long) pMod->mod.om.doAction);
448
dbgprintf("\tparseSelectorAct: 0x%lx\n", (unsigned long) pMod->mod.om.parseSelectorAct);
449
dbgprintf("\tdbgPrintInstInfo: 0x%lx\n", (unsigned long) pMod->dbgPrintInstInfo);
450
dbgprintf("\tfreeInstance: 0x%lx\n", (unsigned long) pMod->freeInstance);
452
pMod = GetNxt(pMod); /* done, go next */
457
/* unlink and destroy a module. The caller must provide a pointer to the module
458
* itself as well as one to its immediate predecessor.
459
* rgerhards, 2008-02-26
462
modUnlinkAndDestroy(modInfo_t **ppThis)
467
assert(ppThis != NULL);
469
assert(pThis != NULL);
471
/* first check if we are permitted to unload */
472
if(pThis->eType == eMOD_LIB) {
473
if(pThis->uRefCnt > 0) {
474
dbgprintf("module %s NOT unloaded because it still has a refcount of %u\n",
475
pThis->pszName, pThis->uRefCnt);
479
ABORT_FINALIZE(RS_RET_MODULE_STILL_REFERENCED);
483
/* we need to unlink the module before we can destruct it -- rgerhards, 2008-02-26 */
484
if(pThis->pPrev == NULL) {
485
/* module is root, so we need to set a new root */
486
pLoadedModules = pThis->pNext;
488
pThis->pPrev->pNext = pThis->pNext;
491
if(pThis->pNext == NULL) {
492
pLoadedModulesLast = pThis->pPrev;
494
pThis->pNext->pPrev = pThis->pPrev;
497
/* finally, we are ready for the module to go away... */
498
dbgprintf("Unloading module %s\n", modGetName(pThis));
499
CHKiRet(modPrepareUnload(pThis));
500
*ppThis = pThis->pNext;
502
moduleDestruct(pThis);
509
/* unload all loaded modules of a specific type (use eMOD_ALL if you want to
510
* unload all module types). The unload happens only if the module is no longer
511
* referenced. So some modules may survive this call.
512
* rgerhards, 2008-03-11
515
modUnloadAndDestructAll(eModLinkType_t modLinkTypesToUnload)
518
modInfo_t *pModCurr; /* module currently being processed */
520
pModCurr = GetNxt(NULL);
521
while(pModCurr != NULL) {
522
if(modLinkTypesToUnload == eMOD_LINK_ALL || pModCurr->eLinkType == modLinkTypesToUnload) {
523
if(modUnlinkAndDestroy(&pModCurr) == RS_RET_MODULE_STILL_REFERENCED) {
524
pModCurr = GetNxt(pModCurr);
526
/* Note: if the module was successfully unloaded, it has updated the
527
* pModCurr pointer to the next module. However, the unload process may
528
* still have indirectly referenced the pointer list in a way that the
529
* unloaded module is not aware of. So we restart the unload process
530
* to make sure we do not fall into a trap (what we did ;)). The
531
* performance toll is minimal. -- rgerhards, 2008-04-28
533
pModCurr = GetNxt(NULL);
536
pModCurr = GetNxt(pModCurr);
542
if(pLoadedModules != NULL) {
543
dbgprintf("modules still loaded after module.UnloadAndDestructAll:\n");
553
/* load a module and initialize it, based on doModLoad() from conf.c
554
* rgerhards, 2008-03-05
555
* varmojfekoj added support for dynamically loadable modules on 2007-08-13
556
* rgerhards, 2007-09-25: please note that the non-threadsafe function dlerror() is
557
* called below. This is ok because modules are currently only loaded during
558
* configuration file processing, which is executed on a single thread. Should we
559
* change that design at any stage (what is unlikely), we need to find a
563
Load(uchar *pModName)
567
size_t iPathLen, iModNameLen;
568
uchar szPath[PATH_MAX];
571
void *pModHdlr, *pModInit;
574
assert(pModName != NULL);
575
dbgprintf("Requested to load module '%s'\n", pModName);
577
iModNameLen = strlen((char *) pModName);
578
if(iModNameLen > 3 && !strcmp((char *) pModName + iModNameLen - 3, ".so")) {
580
bHasExtension = TRUE;
582
bHasExtension = FALSE;
584
pModInfo = GetNxt(NULL);
585
while(pModInfo != NULL) {
586
if(!strncmp((char *) pModName, (char *) (pModNameCmp = modGetName(pModInfo)), iModNameLen) &&
587
(!*(pModNameCmp + iModNameLen) || !strcmp((char *) pModNameCmp + iModNameLen, ".so"))) {
588
dbgprintf("Module '%s' already loaded\n", pModName);
589
ABORT_FINALIZE(RS_RET_OK);
591
pModInfo = GetNxt(pModInfo);
594
/* now build our load module name */
595
if(*pModName == '/') {
596
*szPath = '\0'; /* we do not need to append the path - its already in the module name */
600
strncat((char *) szPath, (pModDir == NULL) ? _PATH_MODDIR : (char*) pModDir, sizeof(szPath) - 1);
601
iPathLen = strlen((char*) szPath);
602
if((szPath[iPathLen - 1] != '/')) {
603
if((iPathLen <= sizeof(szPath) - 2)) {
604
szPath[iPathLen++] = '/';
605
szPath[iPathLen] = '\0';
607
errmsg.LogError(0, RS_RET_MODULE_LOAD_ERR_PATHLEN, "could not load module '%s', path too long\n", pModName);
608
ABORT_FINALIZE(RS_RET_MODULE_LOAD_ERR_PATHLEN);
613
/* ... add actual name ... */
614
strncat((char *) szPath, (char *) pModName, sizeof(szPath) - iPathLen - 1);
616
/* now see if we have an extension and, if not, append ".so" */
618
/* we do not have an extension and so need to add ".so"
619
* TODO: I guess this is highly importable, so we should change the
620
* algo over time... -- rgerhards, 2008-03-05
622
/* ... so now add the extension */
623
strncat((char *) szPath, ".so", sizeof(szPath) - strlen((char*) szPath) - 1);
627
if(iPathLen + strlen((char*) pModName) >= sizeof(szPath)) {
628
errmsg.LogError(0, RS_RET_MODULE_LOAD_ERR_PATHLEN, "could not load module '%s', path too long\n", pModName);
629
ABORT_FINALIZE(RS_RET_MODULE_LOAD_ERR_PATHLEN);
632
/* complete load path constructed, so ... GO! */
633
dbgprintf("loading module '%s'\n", szPath);
634
if(!(pModHdlr = dlopen((char *) szPath, RTLD_NOW))) {
635
errmsg.LogError(0, RS_RET_MODULE_LOAD_ERR_DLOPEN, "could not load module '%s', dlopen: %s\n", szPath, dlerror());
636
ABORT_FINALIZE(RS_RET_MODULE_LOAD_ERR_DLOPEN);
638
if(!(pModInit = dlsym(pModHdlr, "modInit"))) {
639
errmsg.LogError(0, RS_RET_MODULE_LOAD_ERR_NO_INIT, "could not load module '%s', dlsym: %s\n", szPath, dlerror());
641
ABORT_FINALIZE(RS_RET_MODULE_LOAD_ERR_NO_INIT);
643
if((iRet = doModInit(pModInit, (uchar*) pModName, pModHdlr)) != RS_RET_OK) {
644
errmsg.LogError(0, RS_RET_MODULE_LOAD_ERR_INIT_FAILED, "could not load module '%s', rsyslog error %d\n", szPath, iRet);
646
ABORT_FINALIZE(RS_RET_MODULE_LOAD_ERR_INIT_FAILED);
654
/* set the default module load directory. A NULL value may be provided, in
655
* which case any previous value is deleted but no new one set. The caller-provided
656
* string is duplicated. If it needs to be freed, that's the caller's duty.
657
* rgerhards, 2008-03-07
660
SetModDir(uchar *pszModDir)
664
dbgprintf("setting default module load directory '%s'\n", pszModDir);
665
if(pModDir != NULL) {
669
pModDir = (uchar*) strdup((char*)pszModDir);
675
/* Reference-Counting object access: add 1 to the current reference count. Must be
676
* called by anyone interested in using a module. -- rgerhards, 20080-03-10
679
Use(char *srcFile, modInfo_t *pThis)
683
assert(pThis != NULL);
685
dbgprintf("source file %s requested reference for module '%s', reference count now %u\n",
686
srcFile, pThis->pszName, pThis->uRefCnt);
689
modUsrAdd(pThis, srcFile);
697
/* Reference-Counting object access: subract one from the current refcount. Must
698
* by called by anyone who no longer needs a module. If count reaches 0, the
699
* module is unloaded. -- rgerhards, 20080-03-10
702
Release(char *srcFile, modInfo_t **ppThis)
707
assert(ppThis != NULL);
709
assert(pThis != NULL);
710
if(pThis->uRefCnt == 0) {
711
/* oops, we are already at 0? */
712
dbgprintf("internal error: module '%s' already has a refcount of 0 (released by %s)!\n",
713
pThis->pszName, srcFile);
716
dbgprintf("file %s released module '%s', reference count now %u\n",
717
srcFile, pThis->pszName, pThis->uRefCnt);
719
modUsrDel(pThis, srcFile);
724
if(pThis->uRefCnt == 0) {
725
/* we have a zero refcount, so we must unload the module */
726
dbgprintf("module '%s' has zero reference count, unloading...\n", pThis->pszName);
727
modUnlinkAndDestroy(&pThis);
728
/* we must NOT do a *ppThis = NULL, because ppThis now points into freed memory!
729
* If in doubt, see obj.c::ReleaseObj() for how we are called.
739
* rgerhards, 2008-03-11
741
BEGINObjClassExit(module, OBJ_IS_LOADABLE_MODULE) /* CHANGE class also in END MACRO! */
742
CODESTARTObjClassExit(module)
743
/* release objects we no longer need */
744
objRelease(errmsg, CORE_COMPONENT);
747
modUsrPrintAll(); /* debug aid - TODO: integrate with debug.c, at least the settings! */
749
ENDObjClassExit(module)
752
/* queryInterface function
753
* rgerhards, 2008-03-05
755
BEGINobjQueryInterface(module)
756
CODESTARTobjQueryInterface(module)
757
if(pIf->ifVersion != moduleCURR_IF_VERSION) { /* check for current version, increment on each change */
758
ABORT_FINALIZE(RS_RET_INTERFACE_NOT_SUPPORTED);
761
/* ok, we have the right interface, so let's fill it
762
* Please note that we may also do some backwards-compatibility
763
* work here (if we can support an older interface version - that,
764
* of course, also affects the "if" above).
766
pIf->GetNxt = GetNxt;
767
pIf->GetNxtType = GetNxtType;
768
pIf->GetName = modGetName;
769
pIf->GetStateName = modGetStateName;
770
pIf->PrintList = modPrintList;
771
pIf->UnloadAndDestructAll = modUnloadAndDestructAll;
772
pIf->doModInit = doModInit;
773
pIf->SetModDir = SetModDir;
776
pIf->Release = Release;
778
ENDobjQueryInterface(module)
781
/* Initialize our class. Must be called as the very first method
782
* before anything else is called inside this class.
783
* rgerhards, 2008-03-05
785
BEGINAbstractObjClassInit(module, 1, OBJ_IS_CORE_MODULE) /* class, version - CHANGE class also in END MACRO! */
788
/* use any module load path specified in the environment */
789
if((pModPath = (uchar*) getenv("RSYSLOG_MODDIR")) != NULL) {
793
/* now check if another module path was set via the command line (-M)
794
* if so, that overrides the environment. Please note that we must use
795
* a global setting here because the command line parser can NOT call
796
* into the module object, because it is not initialized at that point. So
797
* instead a global setting is changed and we pick it up as soon as we
798
* initialize -- rgerhards, 2008-04-04
800
if(glblModPath != NULL) {
801
SetModDir(glblModPath);
804
/* request objects we use */
805
CHKiRet(objUse(errmsg, CORE_COMPONENT));
806
ENDObjClassInit(module)