~ubuntu-branches/ubuntu/lucid/rsyslog/lucid

« back to all changes in this revision

Viewing changes to cfsysline.c

  • Committer: Bazaar Package Importer
  • Author(s): Michael Biebl
  • Date: 2007-10-19 17:21:49 UTC
  • Revision ID: james.westby@ubuntu.com-20071019172149-ie6ej2xve33mxiu7
Tags: upstream-1.19.10
ImportĀ upstreamĀ versionĀ 1.19.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* cfsysline.c
 
2
 * Implementation of the configuration system line object.
 
3
 *
 
4
 * File begun on 2007-07-30 by RGerhards
 
5
 *
 
6
 * Copyright 2007 Rainer Gerhards and Adiscon GmbH.
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or
 
9
 * modify it under the terms of the GNU General Public License
 
10
 * as published by the Free Software Foundation; either version 2
 
11
 * of the License, or (at your option) any later version.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 * GNU General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with this program; if not, write to the Free Software
 
20
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
21
 *
 
22
 * A copy of the GPL can be found in the file "COPYING" in this distribution.
 
23
 */
 
24
#include "config.h"
 
25
 
 
26
#include "rsyslog.h"
 
27
#include <stdio.h>
 
28
#include <stdlib.h>
 
29
#include <assert.h>
 
30
#include <string.h>
 
31
#include <errno.h>
 
32
#include <ctype.h>
 
33
#include <pwd.h>
 
34
#include <grp.h>
 
35
 
 
36
#include "syslogd.h" /* TODO: when the module interface & library design is done, this should be able to go away */
 
37
#include "cfsysline.h"
 
38
#include "srUtils.h"
 
39
 
 
40
 
 
41
/* static data */
 
42
linkedList_t llCmdList; /* this is NOT a pointer - no typo here ;) */
 
43
 
 
44
/* --------------- START functions for handling canned syntaxes --------------- */
 
45
 
 
46
 
 
47
/* parse a character from the config line
 
48
 * added 2007-07-17 by rgerhards
 
49
 * TODO: enhance this function to handle different classes of characters
 
50
 * HINT: check if char is ' and, if so, use 'c' where c may also be things
 
51
 * like \t etc.
 
52
 */
 
53
static rsRetVal doGetChar(uchar **pp, rsRetVal (*pSetHdlr)(void*, uid_t), void *pVal)
 
54
{
 
55
        DEFiRet;
 
56
 
 
57
        assert(pp != NULL);
 
58
        assert(*pp != NULL);
 
59
 
 
60
        skipWhiteSpace(pp); /* skip over any whitespace */
 
61
 
 
62
        /* if we are not at a '\0', we have our new char - no validity checks here... */
 
63
        if(**pp == '\0') {
 
64
                logerror("No character available");
 
65
                iRet = RS_RET_NOT_FOUND;
 
66
        } else {
 
67
                if(pSetHdlr == NULL) {
 
68
                        /* we should set value directly to var */
 
69
                        *((uchar*)pVal) = **pp;
 
70
                } else {
 
71
                        /* we set value via a set function */
 
72
                        CHKiRet(pSetHdlr(pVal, **pp));
 
73
                }
 
74
                ++(*pp); /* eat processed char */
 
75
        }
 
76
 
 
77
finalize_it:
 
78
        return iRet;
 
79
}
 
80
 
 
81
 
 
82
/* Parse a number from the configuration line. This is more or less
 
83
 * a shell to call the custom handler.
 
84
 * rgerhards, 2007-07-31
 
85
 */
 
86
static rsRetVal doCustomHdlr(uchar **pp, rsRetVal (*pSetHdlr)(uchar**, void*), void *pVal)
 
87
{
 
88
        DEFiRet;
 
89
 
 
90
        assert(pp != NULL);
 
91
        assert(*pp != NULL);
 
92
 
 
93
        CHKiRet(pSetHdlr(pp, pVal));
 
94
 
 
95
finalize_it:
 
96
        return iRet;
 
97
}
 
98
 
 
99
 
 
100
/* Parse a number from the configuration line.
 
101
 * rgerhards, 2007-07-31
 
102
 */
 
103
static rsRetVal doGetInt(uchar **pp, rsRetVal (*pSetHdlr)(void*, uid_t), void *pVal)
 
104
{
 
105
        uchar *p;
 
106
        DEFiRet;
 
107
        int i;  
 
108
 
 
109
        assert(pp != NULL);
 
110
        assert(*pp != NULL);
 
111
        
 
112
        skipWhiteSpace(pp); /* skip over any whitespace */
 
113
        p = *pp;
 
114
 
 
115
        if(!isdigit((int) *p)) {
 
116
                errno = 0;
 
117
                logerror("invalid number");
 
118
                ABORT_FINALIZE(RS_RET_INVALID_INT);
 
119
        }
 
120
 
 
121
        /* pull value */
 
122
        for(i = 0 ; *p && isdigit((int) *p) ; ++p)
 
123
                i = i * 10 + *p - '0';
 
124
 
 
125
        if(pSetHdlr == NULL) {
 
126
                /* we should set value directly to var */
 
127
                *((int*)pVal) = i;
 
128
        } else {
 
129
                /* we set value via a set function */
 
130
                CHKiRet(pSetHdlr(pVal, i));
 
131
        }
 
132
 
 
133
        *pp = p;
 
134
 
 
135
finalize_it:
 
136
        return iRet;
 
137
}
 
138
 
 
139
 
 
140
/* Parse and interpet a $FileCreateMode and $umask line. This function
 
141
 * pulls the creation mode and, if successful, stores it
 
142
 * into the global variable so that the rest of rsyslogd
 
143
 * opens files with that mode. Any previous value will be
 
144
 * overwritten.
 
145
 * HINT: if we store the creation mode in selector_t, we
 
146
 * can even specify multiple modes simply be virtue of
 
147
 * being placed in the right section of rsyslog.conf
 
148
 * rgerhards, 2007-07-4 (happy independence day to my US friends!)
 
149
 * Parameter **pp has a pointer to the current config line.
 
150
 * On exit, it will be updated to the processed position.
 
151
 */
 
152
static rsRetVal doFileCreateMode(uchar **pp, rsRetVal (*pSetHdlr)(void*, uid_t), void *pVal)
 
153
{
 
154
        uchar *p;
 
155
        DEFiRet;
 
156
        uchar errMsg[128];      /* for dynamic error messages */
 
157
        int iVal;       
 
158
 
 
159
        assert(pp != NULL);
 
160
        assert(*pp != NULL);
 
161
        
 
162
        skipWhiteSpace(pp); /* skip over any whitespace */
 
163
        p = *pp;
 
164
 
 
165
        /* for now, we parse and accept only octal numbers
 
166
         * Sequence of tests is important, we are using boolean shortcuts
 
167
         * to avoid addressing invalid memory!
 
168
         */
 
169
        if(!(   (*p == '0')
 
170
             && (*(p+1) && *(p+1) >= '0' && *(p+1) <= '7')
 
171
             && (*(p+2) && *(p+2) >= '0' && *(p+2) <= '7')
 
172
             && (*(p+3) && *(p+3) >= '0' && *(p+3) <= '7')  )  ) {
 
173
                snprintf((char*) errMsg, sizeof(errMsg)/sizeof(uchar),
 
174
                         "value must be octal (e.g 0644).");
 
175
                errno = 0;
 
176
                logerror((char*) errMsg);
 
177
                ABORT_FINALIZE(RS_RET_INVALID_VALUE);
 
178
        }
 
179
 
 
180
        /*  we reach this code only if the octal number is ok - so we can now
 
181
         *  compute the value.
 
182
         */
 
183
        iVal  = (*(p+1)-'0') * 64 + (*(p+2)-'0') * 8 + (*(p+3)-'0');
 
184
 
 
185
        if(pSetHdlr == NULL) {
 
186
                /* we should set value directly to var */
 
187
                *((int*)pVal) = iVal;
 
188
        } else {
 
189
                /* we set value via a set function */
 
190
                CHKiRet(pSetHdlr(pVal, iVal));
 
191
        }
 
192
 
 
193
        p += 4; /* eat the octal number */
 
194
        *pp = p;
 
195
 
 
196
finalize_it:
 
197
        return iRet;
 
198
}
 
199
 
 
200
 
 
201
/* Parse and interpret an on/off inside a config file line. This is most
 
202
 * often used for boolean options, but of course it may also be used
 
203
 * for other things. The passed-in pointer is updated to point to
 
204
 * the first unparsed character on exit. Function emits error messages
 
205
 * if the value is neither on or off. It returns 0 if the option is off,
 
206
 * 1 if it is on and another value if there was an error.
 
207
 * rgerhards, 2007-07-15
 
208
 */
 
209
static int doParseOnOffOption(uchar **pp)
 
210
{
 
211
        uchar *pOptStart;
 
212
        uchar szOpt[32];
 
213
 
 
214
        assert(pp != NULL);
 
215
        assert(*pp != NULL);
 
216
 
 
217
        pOptStart = *pp;
 
218
        skipWhiteSpace(pp); /* skip over any whitespace */
 
219
 
 
220
        if(getSubString(pp, (char*) szOpt, sizeof(szOpt) / sizeof(uchar), ' ')  != 0) {
 
221
                logerror("Invalid $-configline - could not extract on/off option");
 
222
                return -1;
 
223
        }
 
224
        
 
225
        if(!strcmp((char*)szOpt, "on")) {
 
226
                return 1;
 
227
        } else if(!strcmp((char*)szOpt, "off")) {
 
228
                return 0;
 
229
        } else {
 
230
                logerrorSz("Option value must be on or off, but is '%s'", (char*)pOptStart);
 
231
                return -1;
 
232
        }
 
233
}
 
234
 
 
235
 
 
236
/* extract a groupname and return its gid.
 
237
 * rgerhards, 2007-07-17
 
238
 */
 
239
static rsRetVal doGetGID(uchar **pp, rsRetVal (*pSetHdlr)(void*, uid_t), void *pVal)
 
240
{
 
241
        struct group *pgBuf;
 
242
        struct group gBuf;
 
243
        DEFiRet;
 
244
        uchar szName[256];
 
245
        char stringBuf[2048];   /* I hope this is large enough... */
 
246
 
 
247
        assert(pp != NULL);
 
248
        assert(*pp != NULL);
 
249
 
 
250
        if(getSubString(pp, (char*) szName, sizeof(szName) / sizeof(uchar), ' ')  != 0) {
 
251
                logerror("could not extract group name");
 
252
                ABORT_FINALIZE(RS_RET_NOT_FOUND);
 
253
        }
 
254
 
 
255
        getgrnam_r((char*)szName, &gBuf, stringBuf, sizeof(stringBuf), &pgBuf);
 
256
 
 
257
        if(pgBuf == NULL) {
 
258
                logerrorSz("ID for group '%s' could not be found or error", (char*)szName);
 
259
                iRet = RS_RET_NOT_FOUND;
 
260
        } else {
 
261
                if(pSetHdlr == NULL) {
 
262
                        /* we should set value directly to var */
 
263
                        *((gid_t*)pVal) = pgBuf->gr_gid;
 
264
                } else {
 
265
                        /* we set value via a set function */
 
266
                        CHKiRet(pSetHdlr(pVal, pgBuf->gr_gid));
 
267
                }
 
268
                dbgprintf("gid %d obtained for group '%s'\n", pgBuf->gr_gid, szName);
 
269
        }
 
270
 
 
271
        skipWhiteSpace(pp); /* skip over any whitespace */
 
272
 
 
273
finalize_it:
 
274
        return iRet;
 
275
}
 
276
 
 
277
 
 
278
/* extract a username and return its uid.
 
279
 * rgerhards, 2007-07-17
 
280
 */
 
281
static rsRetVal doGetUID(uchar **pp, rsRetVal (*pSetHdlr)(void*, uid_t), void *pVal)
 
282
{
 
283
        struct passwd *ppwBuf;
 
284
        struct passwd pwBuf;
 
285
        DEFiRet;
 
286
        uchar szName[256];
 
287
        char stringBuf[2048];   /* I hope this is large enough... */
 
288
 
 
289
        assert(pp != NULL);
 
290
        assert(*pp != NULL);
 
291
 
 
292
        if(getSubString(pp, (char*) szName, sizeof(szName) / sizeof(uchar), ' ')  != 0) {
 
293
                logerror("could not extract user name");
 
294
                ABORT_FINALIZE(RS_RET_NOT_FOUND);
 
295
        }
 
296
 
 
297
        getpwnam_r((char*)szName, &pwBuf, stringBuf, sizeof(stringBuf), &ppwBuf);
 
298
 
 
299
        if(ppwBuf == NULL) {
 
300
                logerrorSz("ID for user '%s' could not be found or error", (char*)szName);
 
301
                iRet = RS_RET_NOT_FOUND;
 
302
        } else {
 
303
                if(pSetHdlr == NULL) {
 
304
                        /* we should set value directly to var */
 
305
                        *((uid_t*)pVal) = ppwBuf->pw_uid;
 
306
                } else {
 
307
                        /* we set value via a set function */
 
308
                        CHKiRet(pSetHdlr(pVal, ppwBuf->pw_uid));
 
309
                }
 
310
                dbgprintf("uid %d obtained for user '%s'\n", ppwBuf->pw_uid, szName);
 
311
        }
 
312
 
 
313
        skipWhiteSpace(pp); /* skip over any whitespace */
 
314
 
 
315
finalize_it:
 
316
        return iRet;
 
317
}
 
318
 
 
319
 
 
320
/* Parse and process an binary cofig option. pVal must be
 
321
 * a pointer to an integer which is to receive the option
 
322
 * value.
 
323
 * rgerhards, 2007-07-15
 
324
 */
 
325
static rsRetVal doBinaryOptionLine(uchar **pp, rsRetVal (*pSetHdlr)(void*, int), void *pVal)
 
326
{
 
327
        int iOption;
 
328
        DEFiRet;
 
329
 
 
330
        assert(pp != NULL);
 
331
        assert(*pp != NULL);
 
332
 
 
333
        if((iOption = doParseOnOffOption(pp)) == -1)
 
334
                return RS_RET_ERR;      /* nothing left to do */
 
335
        
 
336
        if(pSetHdlr == NULL) {
 
337
                /* we should set value directly to var */
 
338
                *((int*)pVal) = iOption;
 
339
        } else {
 
340
                /* we set value via a set function */
 
341
                CHKiRet(pSetHdlr(pVal, iOption));
 
342
        }
 
343
 
 
344
        skipWhiteSpace(pp); /* skip over any whitespace */
 
345
 
 
346
finalize_it:
 
347
        return iRet;
 
348
}
 
349
 
 
350
 
 
351
/* Parse and a word config line option. A word is a consequitive
 
352
 * sequence of non-whitespace characters. pVal must be
 
353
 * a pointer to a string which is to receive the option
 
354
 * value. The returned string must be freed by the caller.
 
355
 * rgerhards, 2007-09-07
 
356
 */
 
357
static rsRetVal doGetWord(uchar **pp, rsRetVal (*pSetHdlr)(void*, uchar*), void *pVal)
 
358
{
 
359
        DEFiRet;
 
360
        rsCStrObj *pStrB;
 
361
        uchar *p;
 
362
        uchar *pNewVal;
 
363
 
 
364
        assert(pp != NULL);
 
365
        assert(*pp != NULL);
 
366
 
 
367
        if((pStrB = rsCStrConstruct()) == NULL) 
 
368
                return RS_RET_OUT_OF_MEMORY;
 
369
 
 
370
        /* parse out the word */
 
371
        p = *pp;
 
372
 
 
373
        while(*p && !isspace((int) *p)) {
 
374
                CHKiRet(rsCStrAppendChar(pStrB, *p++));
 
375
        }
 
376
        CHKiRet(rsCStrFinish(pStrB));
 
377
 
 
378
        CHKiRet(rsCStrConvSzStrAndDestruct(pStrB, &pNewVal, 0));
 
379
 
 
380
        /* we got the word, now set it */
 
381
        if(pSetHdlr == NULL) {
 
382
                /* we should set value directly to var */
 
383
                *((uchar**)pVal) = pNewVal;
 
384
        } else {
 
385
                /* we set value via a set function */
 
386
                CHKiRet(pSetHdlr(pVal, pNewVal));
 
387
        }
 
388
 
 
389
        *pp = p;
 
390
        skipWhiteSpace(pp); /* skip over any whitespace */
 
391
 
 
392
finalize_it:
 
393
        if(iRet != RS_RET_OK) {
 
394
                if(pStrB != NULL)
 
395
                        rsCStrDestruct(pStrB);
 
396
        }
 
397
 
 
398
        return iRet;
 
399
}
 
400
 
 
401
 
 
402
/* --------------- END functions for handling canned syntaxes --------------- */
 
403
 
 
404
/* destructor for cslCmdHdlr
 
405
 * pThis is actually a cslCmdHdlr_t, but we do not cast it as all we currently
 
406
 * need to do is free it.
 
407
 */
 
408
static rsRetVal cslchDestruct(void *pThis)
 
409
{
 
410
        assert(pThis != NULL);
 
411
        free(pThis);
 
412
        
 
413
        return RS_RET_OK;
 
414
}
 
415
 
 
416
 
 
417
/* constructor for cslCmdHdlr
 
418
 */
 
419
static rsRetVal cslchConstruct(cslCmdHdlr_t **ppThis)
 
420
{
 
421
        cslCmdHdlr_t *pThis;
 
422
        DEFiRet;
 
423
 
 
424
        assert(ppThis != NULL);
 
425
        if((pThis = calloc(1, sizeof(cslCmdHdlr_t))) == NULL) {
 
426
                ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
 
427
        }
 
428
 
 
429
finalize_it:
 
430
        *ppThis = pThis;
 
431
        return iRet;
 
432
}
 
433
 
 
434
/* set data members for this object
 
435
 */
 
436
rsRetVal cslchSetEntry(cslCmdHdlr_t *pThis, ecslCmdHdrlType eType, rsRetVal (*pHdlr)(), void *pData)
 
437
{
 
438
        assert(pThis != NULL);
 
439
        assert(eType != eCmdHdlrInvalid);
 
440
 
 
441
        pThis->eType = eType;
 
442
        pThis->cslCmdHdlr = pHdlr;
 
443
        pThis->pData = pData;
 
444
 
 
445
        return RS_RET_OK;
 
446
}
 
447
 
 
448
 
 
449
/* call the specified handler
 
450
 */
 
451
static rsRetVal cslchCallHdlr(cslCmdHdlr_t *pThis, uchar **ppConfLine)
 
452
{
 
453
        DEFiRet;
 
454
        rsRetVal (*pHdlr)() = NULL;
 
455
        assert(pThis != NULL);
 
456
        assert(ppConfLine != NULL);
 
457
 
 
458
        switch(pThis->eType) {
 
459
        case eCmdHdlrCustomHandler:
 
460
                pHdlr = doCustomHdlr;
 
461
                break;
 
462
        case eCmdHdlrUID:
 
463
                pHdlr = doGetUID;
 
464
                break;
 
465
        case eCmdHdlrGID:
 
466
                pHdlr = doGetGID;
 
467
                break;
 
468
        case eCmdHdlrBinary:
 
469
                pHdlr = doBinaryOptionLine;
 
470
                break;
 
471
        case eCmdHdlrFileCreateMode:
 
472
                pHdlr = doFileCreateMode;
 
473
                break;
 
474
        case eCmdHdlrInt:
 
475
                pHdlr = doGetInt;
 
476
                break;
 
477
        case eCmdHdlrGetChar:
 
478
                pHdlr = doGetChar;
 
479
                break;
 
480
        case eCmdHdlrGetWord:
 
481
                pHdlr = doGetWord;
 
482
                break;
 
483
        default:
 
484
                iRet = RS_RET_NOT_IMPLEMENTED;
 
485
                goto finalize_it;
 
486
        }
 
487
 
 
488
        /* we got a pointer to the handler, so let's call it */
 
489
        assert(pHdlr != NULL);
 
490
        CHKiRet(pHdlr(ppConfLine, pThis->cslCmdHdlr, pThis->pData));
 
491
 
 
492
finalize_it:
 
493
        return iRet;
 
494
}
 
495
 
 
496
 
 
497
/* ---------------------------------------------------------------------- *
 
498
 * now come the handlers for cslCmd_t
 
499
 * ---------------------------------------------------------------------- */
 
500
 
 
501
/* destructor for a cslCmd list key (a string as of now)
 
502
 */
 
503
static rsRetVal cslcKeyDestruct(void *pData)
 
504
{
 
505
        free(pData); /* we do not need to cast as all we do is free it anyway... */
 
506
        return RS_RET_OK;
 
507
}
 
508
 
 
509
/* destructor for cslCmd
 
510
 */
 
511
static rsRetVal cslcDestruct(void *pData)
 
512
{
 
513
        cslCmd_t *pThis = (cslCmd_t*) pData;
 
514
 
 
515
        assert(pThis != NULL);
 
516
 
 
517
        llDestroy(&pThis->llCmdHdlrs);
 
518
        free(pThis);
 
519
        
 
520
        return RS_RET_OK;
 
521
}
 
522
 
 
523
 
 
524
/* constructor for cslCmd
 
525
 */
 
526
static rsRetVal cslcConstruct(cslCmd_t **ppThis, int bChainingPermitted)
 
527
{
 
528
        cslCmd_t *pThis;
 
529
        DEFiRet;
 
530
 
 
531
        assert(ppThis != NULL);
 
532
        if((pThis = calloc(1, sizeof(cslCmd_t))) == NULL) {
 
533
                ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
 
534
        }
 
535
 
 
536
        pThis->bChainingPermitted = bChainingPermitted;
 
537
 
 
538
        CHKiRet(llInit(&pThis->llCmdHdlrs, cslchDestruct, NULL, NULL));
 
539
 
 
540
finalize_it:
 
541
        *ppThis = pThis;
 
542
        return iRet;
 
543
}
 
544
 
 
545
 
 
546
/* add a handler entry to a known command
 
547
 */
 
548
static rsRetVal cslcAddHdlr(cslCmd_t *pThis, ecslCmdHdrlType eType, rsRetVal (*pHdlr)(), void *pData)
 
549
{
 
550
        DEFiRet;
 
551
        cslCmdHdlr_t *pCmdHdlr = NULL;
 
552
 
 
553
        assert(pThis != NULL);
 
554
 
 
555
        CHKiRet(cslchConstruct(&pCmdHdlr));
 
556
        CHKiRet(cslchSetEntry(pCmdHdlr, eType, pHdlr, pData));
 
557
        CHKiRet(llAppend(&pThis->llCmdHdlrs, NULL, pCmdHdlr));
 
558
 
 
559
finalize_it:
 
560
        if(iRet != RS_RET_OK) {
 
561
                if(pHdlr != NULL)
 
562
                        cslchDestruct(pCmdHdlr);
 
563
        }
 
564
 
 
565
        return iRet;
 
566
}
 
567
 
 
568
 
 
569
/* function that initializes this module here. This is primarily a hook
 
570
 * for syslogd.
 
571
 */
 
572
rsRetVal cfsyslineInit(void)
 
573
{
 
574
        DEFiRet;
 
575
 
 
576
        CHKiRet(llInit(&llCmdList, cslcDestruct, cslcKeyDestruct, strcasecmp));
 
577
 
 
578
finalize_it:
 
579
        return iRet;
 
580
}
 
581
 
 
582
 
 
583
/* function that registers cfsysline handlers.
 
584
 * The supplied pCmdName is copied and a new buffer is allocated. This
 
585
 * buffer is automatically destroyed when the element is freed, the
 
586
 * caller does not need to take care of that. The caller must, however,
 
587
 * free pCmdName if he allocated it dynamically! -- rgerhards, 2007-08-09
 
588
 */
 
589
rsRetVal regCfSysLineHdlr(uchar *pCmdName, int bChainingPermitted, ecslCmdHdrlType eType, rsRetVal (*pHdlr)(), void *pData)
 
590
{
 
591
        cslCmd_t *pThis;
 
592
        uchar *pMyCmdName;
 
593
        DEFiRet;
 
594
 
 
595
        iRet = llFind(&llCmdList, (void *) pCmdName, (void*) &pThis);
 
596
        if(iRet == RS_RET_NOT_FOUND) {
 
597
                /* new command */
 
598
                CHKiRet(cslcConstruct(&pThis, bChainingPermitted));
 
599
                CHKiRet_Hdlr(cslcAddHdlr(pThis, eType, pHdlr, pData)) {
 
600
                        cslcDestruct(pThis);
 
601
                        goto finalize_it;
 
602
                }
 
603
                /* important: add to list, AFTER everything else is OK. Else
 
604
                 * we mess up things in the error case.
 
605
                 */
 
606
                if((pMyCmdName = (uchar*) strdup((char*)pCmdName)) == NULL) {
 
607
                        cslcDestruct(pThis);
 
608
                        ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY);
 
609
                }
 
610
                CHKiRet_Hdlr(llAppend(&llCmdList, pMyCmdName, (void*) pThis)) {
 
611
                        cslcDestruct(pThis);
 
612
                        goto finalize_it;
 
613
                }
 
614
        } else {
 
615
                /* command already exists, are we allowed to chain? */
 
616
                if(pThis->bChainingPermitted == 0 || bChainingPermitted == 0) {
 
617
                        ABORT_FINALIZE(RS_RET_CHAIN_NOT_PERMITTED);
 
618
                }
 
619
                CHKiRet_Hdlr(cslcAddHdlr(pThis, eType, pHdlr, pData)) {
 
620
                        cslcDestruct(pThis);
 
621
                        goto finalize_it;
 
622
                }
 
623
        }
 
624
 
 
625
finalize_it:
 
626
        return iRet;
 
627
}
 
628
 
 
629
 
 
630
rsRetVal unregCfSysLineHdlrs(void)
 
631
{
 
632
        return llDestroy(&llCmdList);
 
633
}
 
634
 
 
635
 
 
636
/* process a cfsysline command (based on handler structure)
 
637
 * param "p" is a pointer to the command line after the command. Should be
 
638
 * updated.
 
639
 */
 
640
rsRetVal processCfSysLineCommand(uchar *pCmdName, uchar **p)
 
641
{
 
642
        DEFiRet;
 
643
        rsRetVal iRetLL; /* for linked list handling */
 
644
        cslCmd_t *pCmd;
 
645
        cslCmdHdlr_t *pCmdHdlr;
 
646
        linkedListCookie_t llCookieCmdHdlr;
 
647
        uchar *pHdlrP; /* the handler's private p (else we could only call one handler) */
 
648
        int bWasOnceOK; /* was the result of an handler at least once RS_RET_OK? */
 
649
        uchar *pOKp = NULL; /* returned conf line pointer when it was OK */
 
650
 
 
651
        iRet = llFind(&llCmdList, (void *) pCmdName, (void*) &pCmd);
 
652
 
 
653
        if(iRet == RS_RET_NOT_FOUND) {
 
654
                logerror("invalid or yet-unknown config file command - have you forgotten to load a module?");
 
655
        }
 
656
 
 
657
        if(iRet != RS_RET_OK)
 
658
                goto finalize_it;
 
659
 
 
660
        llCookieCmdHdlr = NULL;
 
661
        bWasOnceOK = 0;
 
662
        while((iRetLL = llGetNextElt(&pCmd->llCmdHdlrs, &llCookieCmdHdlr, (void*)&pCmdHdlr)) == RS_RET_OK) {
 
663
                /* for the time being, we ignore errors during handlers. The
 
664
                 * reason is that handlers are independent. An error in one
 
665
                 * handler does not necessarily mean that another one will
 
666
                 * fail, too. Later, we might add a config variable to control
 
667
                 * this behaviour (but I am not sure if that is rally
 
668
                 * necessary). -- rgerhards, 2007-07-31
 
669
                 */
 
670
                pHdlrP = *p;
 
671
                if((iRet = cslchCallHdlr(pCmdHdlr, &pHdlrP)) == RS_RET_OK) {
 
672
                        bWasOnceOK = 1;
 
673
                        pOKp = pHdlrP;
 
674
                }
 
675
        }
 
676
 
 
677
        if(bWasOnceOK == 1) {
 
678
                *p = pOKp;
 
679
                iRet = RS_RET_OK;
 
680
        }
 
681
 
 
682
        if(iRetLL != RS_RET_END_OF_LINKEDLIST)
 
683
                iRet = iRetLL;
 
684
 
 
685
finalize_it:
 
686
        return iRet;
 
687
}
 
688
 
 
689
 
 
690
/* debug print the command handler structure
 
691
 */
 
692
void dbgPrintCfSysLineHandlers(void)
 
693
{
 
694
        DEFiRet;
 
695
 
 
696
        cslCmd_t *pCmd;
 
697
        cslCmdHdlr_t *pCmdHdlr;
 
698
        linkedListCookie_t llCookieCmd;
 
699
        linkedListCookie_t llCookieCmdHdlr;
 
700
        uchar *pKey;
 
701
 
 
702
        printf("\nSytem Line Configuration Commands:\n");
 
703
        llCookieCmd = NULL;
 
704
        while((iRet = llGetNextElt(&llCmdList, &llCookieCmd, (void*)&pCmd)) == RS_RET_OK) {
 
705
                llGetKey(llCookieCmd, (void*) &pKey); /* TODO: using the cookie is NOT clean! */
 
706
                printf("\tCommand '%s':\n", pKey);
 
707
                llCookieCmdHdlr = NULL;
 
708
                while((iRet = llGetNextElt(&pCmd->llCmdHdlrs, &llCookieCmdHdlr, (void*)&pCmdHdlr)) == RS_RET_OK) {
 
709
                        printf("\t\ttype : %d\n", pCmdHdlr->eType);
 
710
                        printf("\t\tpData: 0x%x\n", (unsigned) pCmdHdlr->pData);
 
711
                        printf("\t\tHdlr : 0x%x\n", (unsigned) pCmdHdlr->cslCmdHdlr);
 
712
                        printf("\n");
 
713
                }
 
714
        }
 
715
        printf("\n");
 
716
 
 
717
}
 
718
 
 
719
/*
 
720
 * vi:set ai:
 
721
 */