~ubuntu-branches/ubuntu/karmic/rsyslog/karmic

« back to all changes in this revision

Viewing changes to runtime/modules.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Biebl
  • Date: 2009-04-08 00:59:14 UTC
  • mfrom: (1.1.9 upstream) (3.2.6 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090408005914-gfyay7o45szt05zl
Tags: 3.20.5-1
* New upstream release.
* debian/rsyslog.logcheck.ignore.server
  - Install a logcheck ignore file for rsyslog (using dh_installlogcheck).
    Thanks to Kim Holviala for the patch. Closes: #522164

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 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 "rsyslog.h"
 
36
#include <stdio.h>
 
37
#include <stdarg.h>
 
38
#include <stdlib.h>
 
39
#include <string.h>
 
40
#include <time.h>
 
41
#include <assert.h>
 
42
#include <errno.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 "cfsysline.h"
 
53
#include "modules.h"
 
54
#include "errmsg.h"
 
55
 
 
56
/* static data */
 
57
DEFobjStaticHelpers
 
58
DEFobjCurrIf(errmsg)
 
59
 
 
60
static modInfo_t *pLoadedModules = NULL;        /* list of currently-loaded modules */
 
61
static modInfo_t *pLoadedModulesLast = NULL;    /* tail-pointer */
 
62
 
 
63
/* config settings */
 
64
uchar   *pModDir = NULL; /* read-only after startup */
 
65
 
 
66
 
 
67
#ifdef DEBUG
 
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
 
70
 */
 
71
 
 
72
/* add a user to the current list of users (always at the root) */
 
73
static void
 
74
modUsrAdd(modInfo_t *pThis, char *pszUsr)
 
75
{
 
76
        modUsr_t *pUsr;
 
77
 
 
78
        BEGINfunc
 
79
        if((pUsr = calloc(1, sizeof(modUsr_t))) == NULL)
 
80
                goto finalize_it;
 
81
 
 
82
        if((pUsr->pszFile = strdup(pszUsr)) == NULL) {
 
83
                free(pUsr);
 
84
                goto finalize_it;
 
85
        }
 
86
 
 
87
        if(pThis->pModUsrRoot != NULL) {
 
88
                pUsr->pNext = pThis->pModUsrRoot;
 
89
        }
 
90
        pThis->pModUsrRoot = pUsr;
 
91
 
 
92
finalize_it:
 
93
        ENDfunc;
 
94
}
 
95
 
 
96
 
 
97
/* remove a user from the current user list
 
98
 * rgerhards, 2008-03-11
 
99
 */
 
100
static void
 
101
modUsrDel(modInfo_t *pThis, char *pszUsr)
 
102
{
 
103
        modUsr_t *pUsr;
 
104
        modUsr_t *pPrev = NULL;
 
105
 
 
106
        for(pUsr = pThis->pModUsrRoot ; pUsr != NULL ; pUsr = pUsr->pNext) {
 
107
                if(!strcmp(pUsr->pszFile, pszUsr))
 
108
                        break;
 
109
                else
 
110
                        pPrev = pUsr;
 
111
        }
 
112
 
 
113
        if(pUsr == NULL) {
 
114
                dbgprintf("oops - tried to delete user %s from module %s and it wasn't registered as one...\n",
 
115
                          pszUsr, pThis->pszName);
 
116
        } else {
 
117
                if(pPrev == NULL) {
 
118
                        /* This was at the root! */
 
119
                        pThis->pModUsrRoot = pUsr->pNext;
 
120
                } else {
 
121
                        pPrev->pNext = pUsr->pNext;
 
122
                }
 
123
                /* free ressources */
 
124
                free(pUsr->pszFile);
 
125
                free(pUsr);
 
126
                pUsr = NULL; /* just to make sure... */
 
127
        }
 
128
}
 
129
 
 
130
 
 
131
/* print a short list all all source files using the module in question
 
132
 * rgerhards, 2008-03-11
 
133
 */
 
134
static void
 
135
modUsrPrint(modInfo_t *pThis)
 
136
{
 
137
        modUsr_t *pUsr;
 
138
 
 
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);
 
142
        }
 
143
}
 
144
 
 
145
 
 
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
 
149
 */
 
150
//static void
 
151
void
 
152
modUsrPrintAll(void)
 
153
{
 
154
        modInfo_t *pMod;
 
155
 
 
156
        BEGINfunc
 
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);
 
159
                modUsrPrint(pMod);
 
160
        }
 
161
        ENDfunc
 
162
}
 
163
 
 
164
#endif /* #ifdef DEBUG */
 
165
 
 
166
 
 
167
/* Construct a new module object
 
168
 */
 
169
static rsRetVal moduleConstruct(modInfo_t **pThis)
 
170
{
 
171
        modInfo_t *pNew;
 
172
 
 
173
        if((pNew = calloc(1, sizeof(modInfo_t))) == NULL)
 
174
                return RS_RET_OUT_OF_MEMORY;
 
175
 
 
176
        /* OK, we got the element, now initialize members that should
 
177
         * not be zero-filled.
 
178
         */
 
179
 
 
180
        *pThis = pNew;
 
181
        return RS_RET_OK;
 
182
}
 
183
 
 
184
 
 
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!)
 
188
 */
 
189
static void moduleDestruct(modInfo_t *pThis)
 
190
{
 
191
        assert(pThis != NULL);
 
192
        if(pThis->pszName != NULL)
 
193
                free(pThis->pszName);
 
194
        if(pThis->pModHdlr != NULL) {
 
195
#       ifdef   VALGRIND
 
196
#               warning "dlclose disabled for valgrind"
 
197
#       else
 
198
                dlclose(pThis->pModHdlr);
 
199
#       endif
 
200
        }
 
201
 
 
202
        free(pThis);
 
203
}
 
204
 
 
205
 
 
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
 
212
 */
 
213
static rsRetVal queryHostEtryPt(uchar *name, rsRetVal (**pEtryPoint)())
 
214
{
 
215
        DEFiRet;
 
216
 
 
217
        if((name == NULL) || (pEtryPoint == NULL))
 
218
                return RS_RET_PARAM_ERROR;
 
219
 
 
220
        if(!strcmp((char*) name, "regCfSysLineHdlr")) {
 
221
                *pEtryPoint = regCfSysLineHdlr;
 
222
        } else if(!strcmp((char*) name, "objGetObjInterface")) {
 
223
                *pEtryPoint = objGetObjInterface;
 
224
        } else {
 
225
                *pEtryPoint = NULL; /* to  be on the safe side */
 
226
                ABORT_FINALIZE(RS_RET_ENTRY_POINT_NOT_FOUND);
 
227
        }
 
228
 
 
229
finalize_it:
 
230
        RETiRet;
 
231
}
 
232
 
 
233
 
 
234
/* get the name of a module
 
235
 */
 
236
static uchar *modGetName(modInfo_t *pThis)
 
237
{
 
238
        return((pThis->pszName == NULL) ? (uchar*) "" : pThis->pszName);
 
239
}
 
240
 
 
241
 
 
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
 
247
 */
 
248
static uchar *modGetStateName(modInfo_t *pThis)
 
249
{
 
250
        return(modGetName(pThis));
 
251
}
 
252
 
 
253
 
 
254
/* Add a module to the loaded module linked list
 
255
 */
 
256
static inline void
 
257
addModToList(modInfo_t *pThis)
 
258
{
 
259
        assert(pThis != NULL);
 
260
 
 
261
        if(pLoadedModules == NULL) {
 
262
                pLoadedModules = pLoadedModulesLast = pThis;
 
263
        } else {
 
264
                /* there already exist entries */
 
265
                pThis->pPrev = pLoadedModulesLast;
 
266
                pLoadedModulesLast->pNext = pThis;
 
267
                pLoadedModulesLast = pThis;
 
268
        }
 
269
}
 
270
 
 
271
 
 
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
 
278
 */
 
279
static modInfo_t *GetNxt(modInfo_t *pThis)
 
280
{
 
281
        modInfo_t *pNew;
 
282
 
 
283
        if(pThis == NULL)
 
284
                pNew = pLoadedModules;
 
285
        else
 
286
                pNew = pThis->pNext;
 
287
 
 
288
        return(pNew);
 
289
}
 
290
 
 
291
 
 
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
 
296
 */
 
297
static modInfo_t *GetNxtType(modInfo_t *pThis, eModType_t rqtdType)
 
298
{
 
299
        modInfo_t *pMod = pThis;
 
300
 
 
301
        do {
 
302
                pMod = GetNxt(pMod);
 
303
        } while(!(pMod == NULL || pMod->eType == rqtdType)); /* warning: do ... while() */
 
304
 
 
305
        return pMod;
 
306
}
 
307
 
 
308
 
 
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
 
318
 * count > 0.
 
319
 */
 
320
static rsRetVal
 
321
modPrepareUnload(modInfo_t *pThis)
 
322
{
 
323
        DEFiRet;
 
324
        void *pModCookie;
 
325
 
 
326
        assert(pThis != NULL);
 
327
 
 
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);
 
332
        }
 
333
 
 
334
        CHKiRet(pThis->modGetID(&pModCookie));
 
335
        pThis->modExit(); /* tell the module to get ready for unload */
 
336
        CHKiRet(unregCfSysLineHdlrs4Owner(pModCookie));
 
337
 
 
338
finalize_it:
 
339
        RETiRet;
 
340
}
 
341
 
 
342
 
 
343
/* Add an already-loaded module to the module linked list. This function does
 
344
 * everything needed to fully initialize the module.
 
345
 */
 
346
static rsRetVal
 
347
doModInit(rsRetVal (*modInit)(int, int*, rsRetVal(**)(), rsRetVal(*)(), modInfo_t*), uchar *name, void *pModHdlr)
 
348
{
 
349
        DEFiRet;
 
350
        modInfo_t *pNew = NULL;
 
351
        rsRetVal (*modGetType)(eModType_t *pType);
 
352
 
 
353
        assert(modInit != NULL);
 
354
 
 
355
        if((iRet = moduleConstruct(&pNew)) != RS_RET_OK) {
 
356
                pNew = NULL;
 
357
                ABORT_FINALIZE(iRet);
 
358
        }
 
359
 
 
360
        CHKiRet((*modInit)(CURR_MOD_IF_VERSION, &pNew->iIFVers, &pNew->modQueryEtryPt, queryHostEtryPt, pNew));
 
361
 
 
362
        if(pNew->iIFVers != CURR_MOD_IF_VERSION) {
 
363
                ABORT_FINALIZE(RS_RET_MISSING_INTERFACE);
 
364
        }
 
365
 
 
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
 
368
         */
 
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);
 
372
        
 
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
 
375
         * module types.
 
376
         */
 
377
        CHKiRet((*pNew->modQueryEtryPt)((uchar*)"modGetID", &pNew->modGetID));
 
378
        CHKiRet((*pNew->modQueryEtryPt)((uchar*)"modExit", &pNew->modExit));
 
379
 
 
380
        /* ... and now the module-specific interfaces */
 
381
        switch(pNew->eType) {
 
382
                case eMOD_IN:
 
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));
 
386
                        break;
 
387
                case eMOD_OUT:
 
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));
 
394
                        break;
 
395
                case eMOD_LIB:
 
396
                        break;
 
397
        }
 
398
 
 
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 */
 
402
        if(pModHdlr == NULL)
 
403
                pNew->eLinkType = eMOD_LINK_STATIC;
 
404
        else
 
405
                pNew->eLinkType = eMOD_LINK_DYNAMIC_LOADED;
 
406
 
 
407
        /* we initialized the structure, now let's add it to the linked list of modules */
 
408
        addModToList(pNew);
 
409
 
 
410
finalize_it:
 
411
        if(iRet != RS_RET_OK) {
 
412
                if(pNew != NULL)
 
413
                        moduleDestruct(pNew);
 
414
        }
 
415
 
 
416
        RETiRet;
 
417
}
 
418
 
 
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!
 
423
 */
 
424
static void modPrintList(void)
 
425
{
 
426
        modInfo_t *pMod;
 
427
 
 
428
        pMod = GetNxt(NULL);
 
429
        while(pMod != NULL) {
 
430
                dbgprintf("Loaded Module: Name='%s', IFVersion=%d, ",
 
431
                        (char*) modGetName(pMod), pMod->iIFVers);
 
432
                dbgprintf("type=");
 
433
                switch(pMod->eType) {
 
434
                case eMOD_OUT:
 
435
                        dbgprintf("output");
 
436
                        break;
 
437
                case eMOD_IN:
 
438
                        dbgprintf("input");
 
439
                        break;
 
440
                case eMOD_LIB:
 
441
                        dbgprintf("library");
 
442
                        break;
 
443
                }
 
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);
 
451
                dbgprintf("\n");
 
452
                pMod = GetNxt(pMod); /* done, go next */
 
453
        }
 
454
}
 
455
 
 
456
 
 
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
 
460
 */
 
461
static rsRetVal
 
462
modUnlinkAndDestroy(modInfo_t **ppThis)
 
463
{
 
464
        DEFiRet;
 
465
        modInfo_t *pThis;
 
466
 
 
467
        assert(ppThis != NULL);
 
468
        pThis = *ppThis;
 
469
        assert(pThis != NULL);
 
470
 
 
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);
 
476
#                       ifdef DEBUG
 
477
                        //modUsrPrintAll();
 
478
#                       endif
 
479
                        ABORT_FINALIZE(RS_RET_MODULE_STILL_REFERENCED);
 
480
                }
 
481
        }
 
482
 
 
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;
 
487
        } else {
 
488
                pThis->pPrev->pNext = pThis->pNext;
 
489
        }
 
490
 
 
491
        if(pThis->pNext == NULL) {
 
492
                pLoadedModulesLast = pThis->pPrev;
 
493
        } else {
 
494
                pThis->pNext->pPrev = pThis->pPrev;
 
495
        }
 
496
 
 
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;
 
501
 
 
502
        moduleDestruct(pThis);
 
503
 
 
504
finalize_it:
 
505
        RETiRet;
 
506
}
 
507
 
 
508
 
 
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
 
513
 */
 
514
static rsRetVal
 
515
modUnloadAndDestructAll(eModLinkType_t modLinkTypesToUnload)
 
516
{
 
517
        DEFiRet;
 
518
        modInfo_t *pModCurr; /* module currently being processed */
 
519
 
 
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);
 
525
                        } else {
 
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
 
532
                                 */
 
533
                                pModCurr = GetNxt(NULL);
 
534
                        }
 
535
                } else {
 
536
                        pModCurr = GetNxt(pModCurr);
 
537
                }
 
538
        }
 
539
 
 
540
#       ifdef DEBUG
 
541
        /* DEV DEBUG only!
 
542
                if(pLoadedModules != NULL) {
 
543
                        dbgprintf("modules still loaded after module.UnloadAndDestructAll:\n");
 
544
                        modUsrPrintAll();
 
545
                }
 
546
        */
 
547
#       endif
 
548
 
 
549
        RETiRet;
 
550
}
 
551
 
 
552
 
 
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
 
560
 * replacement.
 
561
 */
 
562
static rsRetVal
 
563
Load(uchar *pModName)
 
564
{
 
565
        DEFiRet;
 
566
        
 
567
        size_t iPathLen, iModNameLen;
 
568
        uchar szPath[PATH_MAX];
 
569
        uchar *pModNameCmp;
 
570
        int bHasExtension;
 
571
        void *pModHdlr, *pModInit;
 
572
        modInfo_t *pModInfo;
 
573
 
 
574
        assert(pModName != NULL);
 
575
        dbgprintf("Requested to load module '%s'\n", pModName);
 
576
 
 
577
        iModNameLen = strlen((char *) pModName);
 
578
        if(iModNameLen > 3 && !strcmp((char *) pModName + iModNameLen - 3, ".so")) {
 
579
                iModNameLen -= 3;
 
580
                bHasExtension = TRUE;
 
581
        } else
 
582
                bHasExtension = FALSE;
 
583
 
 
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);
 
590
                }
 
591
                pModInfo = GetNxt(pModInfo);
 
592
        }
 
593
 
 
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 */
 
597
                iPathLen = 0;
 
598
        } else {
 
599
                *szPath = '\0';
 
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';
 
606
                        } else {
 
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);
 
609
                        }
 
610
                }
 
611
        }
 
612
 
 
613
        /* ... add actual name ... */
 
614
        strncat((char *) szPath, (char *) pModName, sizeof(szPath) - iPathLen - 1);
 
615
 
 
616
        /* now see if we have an extension and, if not, append ".so" */
 
617
        if(!bHasExtension) {
 
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
 
621
                 */
 
622
                /* ... so now add the extension */
 
623
                strncat((char *) szPath, ".so", sizeof(szPath) - strlen((char*) szPath) - 1);
 
624
                iPathLen += 3;
 
625
        }
 
626
 
 
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);
 
630
        }
 
631
 
 
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);
 
637
        }
 
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());
 
640
                dlclose(pModHdlr);
 
641
                ABORT_FINALIZE(RS_RET_MODULE_LOAD_ERR_NO_INIT);
 
642
        }
 
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);
 
645
                dlclose(pModHdlr);
 
646
                ABORT_FINALIZE(RS_RET_MODULE_LOAD_ERR_INIT_FAILED);
 
647
        }
 
648
 
 
649
finalize_it:
 
650
        RETiRet;
 
651
}
 
652
 
 
653
 
 
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
 
658
 */
 
659
static rsRetVal
 
660
SetModDir(uchar *pszModDir)
 
661
{
 
662
        DEFiRet;
 
663
 
 
664
        dbgprintf("setting default module load directory '%s'\n", pszModDir);
 
665
        if(pModDir != NULL) {
 
666
                free(pModDir);
 
667
        }
 
668
 
 
669
        pModDir = (uchar*) strdup((char*)pszModDir);
 
670
 
 
671
        RETiRet;
 
672
}
 
673
 
 
674
 
 
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
 
677
 */
 
678
static rsRetVal
 
679
Use(char *srcFile, modInfo_t *pThis)
 
680
{
 
681
        DEFiRet;
 
682
 
 
683
        assert(pThis != NULL);
 
684
        pThis->uRefCnt++;
 
685
        dbgprintf("source file %s requested reference for module '%s', reference count now %u\n",
 
686
                  srcFile, pThis->pszName, pThis->uRefCnt);
 
687
 
 
688
#       ifdef DEBUG
 
689
        modUsrAdd(pThis, srcFile);
 
690
#       endif
 
691
 
 
692
        RETiRet;
 
693
 
 
694
}
 
695
 
 
696
 
 
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
 
700
 */
 
701
static rsRetVal
 
702
Release(char *srcFile, modInfo_t **ppThis)
 
703
{
 
704
        DEFiRet;
 
705
        modInfo_t *pThis;
 
706
 
 
707
        assert(ppThis != NULL);
 
708
        pThis = *ppThis;
 
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);
 
714
        } else {
 
715
                --pThis->uRefCnt;
 
716
                dbgprintf("file %s released module '%s', reference count now %u\n",
 
717
                          srcFile, pThis->pszName, pThis->uRefCnt);
 
718
#               ifdef DEBUG
 
719
                modUsrDel(pThis, srcFile);
 
720
                modUsrPrint(pThis);
 
721
#               endif
 
722
        }
 
723
 
 
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.
 
730
                 */
 
731
        }
 
732
 
 
733
        RETiRet;
 
734
 
 
735
}
 
736
 
 
737
 
 
738
/* exit our class
 
739
 * rgerhards, 2008-03-11
 
740
 */
 
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);
 
745
 
 
746
#       ifdef DEBUG
 
747
        modUsrPrintAll(); /* debug aid - TODO: integrate with debug.c, at least the settings! */
 
748
#       endif
 
749
ENDObjClassExit(module)
 
750
 
 
751
 
 
752
/* queryInterface function
 
753
 * rgerhards, 2008-03-05
 
754
 */
 
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);
 
759
        }
 
760
 
 
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).
 
765
         */
 
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;
 
774
        pIf->Load = Load;
 
775
        pIf->Use = Use;
 
776
        pIf->Release = Release;
 
777
finalize_it:
 
778
ENDobjQueryInterface(module)
 
779
 
 
780
 
 
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
 
784
 */
 
785
BEGINAbstractObjClassInit(module, 1, OBJ_IS_CORE_MODULE) /* class, version - CHANGE class also in END MACRO! */
 
786
        uchar *pModPath;
 
787
 
 
788
        /* use any module load path specified in the environment */
 
789
        if((pModPath = (uchar*) getenv("RSYSLOG_MODDIR")) != NULL) {
 
790
                SetModDir(pModPath);
 
791
        }
 
792
 
 
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
 
799
         */
 
800
        if(glblModPath != NULL) {
 
801
                SetModDir(glblModPath);
 
802
        }
 
803
 
 
804
        /* request objects we use */
 
805
        CHKiRet(objUse(errmsg, CORE_COMPONENT));
 
806
ENDObjClassInit(module)
 
807
 
 
808
/* vi:set ai:
 
809
 */