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

« back to all changes in this revision

Viewing changes to outchannel.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
/* This is the output channel processing code of rsyslog.
 
2
 * Output channels - in the long term - will define how
 
3
 * messages will be sent to whatever file or other medium.
 
4
 * Currently, they mainly provide a way to store some file-related
 
5
 * information (most importantly the maximum file size allowed).
 
6
 * Please see syslogd.c for license information.
 
7
 * This code is placed under the GPL.
 
8
 * begun 2005-06-21 rgerhards
 
9
 */
 
10
#include "config.h"
 
11
 
 
12
#ifdef __FreeBSD__
 
13
#define BSD
 
14
#endif
 
15
 
 
16
#include "rsyslog.h"
 
17
#include <stdio.h>
 
18
#include <unistd.h>
 
19
#include <stdlib.h>
 
20
#include <string.h>
 
21
#include <ctype.h>
 
22
#include <assert.h>
 
23
#include "stringbuf.h"
 
24
#include "outchannel.h"
 
25
#include "syslogd.h"
 
26
 
 
27
static struct outchannel *ochRoot = NULL;       /* the root of the outchannel list */
 
28
static struct outchannel *ochLast = NULL;       /* points to the last element of the outchannel list */
 
29
 
 
30
/* Constructs a outchannel list object. Returns pointer to it
 
31
 * or NULL (if it fails).
 
32
 */
 
33
struct outchannel* ochConstruct(void)
 
34
{
 
35
        struct outchannel *pOch;
 
36
        if((pOch = calloc(1, sizeof(struct outchannel))) == NULL)
 
37
                return NULL;
 
38
        
 
39
        /* basic initialisaion is done via calloc() - need to
 
40
         * initialize only values != 0. */
 
41
 
 
42
        if(ochLast == NULL)
 
43
        { /* we are the first element! */
 
44
                ochRoot = ochLast = pOch;
 
45
        }
 
46
        else
 
47
        {
 
48
                ochLast->pNext = pOch;
 
49
                ochLast = pOch;
 
50
        }
 
51
 
 
52
        return(pOch);
 
53
}
 
54
 
 
55
 
 
56
/* skips the next comma and any whitespace
 
57
 * in front and after it.
 
58
 */
 
59
static void skip_Comma(char **pp)
 
60
{
 
61
        register char *p;
 
62
 
 
63
        assert(pp != NULL);
 
64
        assert(*pp != NULL);
 
65
 
 
66
        p = *pp;
 
67
        while(isspace((int)*p))
 
68
                ++p;
 
69
        if(*p == ',')
 
70
                ++p;
 
71
        while(isspace((int)*p))
 
72
                ++p;
 
73
        *pp = p;
 
74
}
 
75
 
 
76
/* helper to ochAddLine. Parses a comma-delimited field
 
77
 * The field is delimited by SP or comma. Leading whitespace
 
78
 * is "eaten" and does not become part of the field content.
 
79
 * returns: 0 - ok, 1 - failure
 
80
 */
 
81
static int get_Field(uchar **pp, uchar **pField)
 
82
{
 
83
        register uchar *p;
 
84
        rsCStrObj *pStrB;
 
85
 
 
86
        assert(pp != NULL);
 
87
        assert(*pp != NULL);
 
88
        assert(pField != NULL);
 
89
 
 
90
        skip_Comma((char**)pp);
 
91
        p = *pp;
 
92
 
 
93
        if((pStrB = rsCStrConstruct()) == NULL)
 
94
                 return 1;
 
95
        rsCStrSetAllocIncrement(pStrB, 32);
 
96
 
 
97
        /* copy the field */
 
98
        while(*p && *p != ' ' && *p != ',') {
 
99
                rsCStrAppendChar(pStrB, *p++);
 
100
        }
 
101
 
 
102
        *pp = p;
 
103
        rsCStrFinish(pStrB);
 
104
        if(rsCStrConvSzStrAndDestruct(pStrB, pField, 0) != RS_RET_OK)
 
105
                return 1;
 
106
 
 
107
        return 0;
 
108
}
 
109
 
 
110
 
 
111
/* helper to ochAddLine. Parses a off_t type from the
 
112
 * input line.
 
113
 * returns: 0 - ok, 1 - failure
 
114
 */
 
115
static int get_off_t(uchar **pp, off_t *pOff_t)
 
116
{
 
117
        register uchar *p;
 
118
        off_t val;
 
119
 
 
120
        assert(pp != NULL);
 
121
        assert(*pp != NULL);
 
122
        assert(pOff_t != NULL);
 
123
 
 
124
        skip_Comma((char**)pp);
 
125
        p = *pp;
 
126
 
 
127
        val = 0;
 
128
        while(*p && isdigit((int)*p)) {
 
129
                val = val * 10 + (*p - '0');
 
130
                ++p;
 
131
        }
 
132
 
 
133
        *pp = p;
 
134
        *pOff_t = val;
 
135
 
 
136
        return 0;
 
137
}
 
138
 
 
139
 
 
140
/* helper to ochAddLine. Parses everything from the
 
141
 * current position to the end of line and returns it
 
142
 * to the caller. Leading white space is removed, but
 
143
 * not trailing.
 
144
 * returns: 0 - ok, 1 - failure
 
145
 */
 
146
static inline int get_restOfLine(uchar **pp, uchar **pBuf)
 
147
{
 
148
        register uchar *p;
 
149
        rsCStrObj *pStrB;
 
150
 
 
151
        assert(pp != NULL);
 
152
        assert(*pp != NULL);
 
153
        assert(pBuf != NULL);
 
154
 
 
155
        skip_Comma((char**)pp);
 
156
        p = *pp;
 
157
 
 
158
        if((pStrB = rsCStrConstruct()) == NULL)
 
159
                 return 1;
 
160
        rsCStrSetAllocIncrement(pStrB, 32);
 
161
 
 
162
        /* copy the field */
 
163
        while(*p) {
 
164
                rsCStrAppendChar(pStrB, *p++);
 
165
        }
 
166
 
 
167
        *pp = p;
 
168
        rsCStrFinish(pStrB);
 
169
        if(rsCStrConvSzStrAndDestruct(pStrB, pBuf, 0) != RS_RET_OK)
 
170
                return 1;
 
171
 
 
172
        return 0;
 
173
}
 
174
 
 
175
 
 
176
/* Add a new outchannel line
 
177
 * returns pointer to new object if it succeeds, NULL otherwise.
 
178
 * An outchannel line is primarily a set of fields delemited by commas.
 
179
 * There might be some whitespace between the field (but not within)
 
180
 * and the commas. This can be removed.
 
181
 */
 
182
struct outchannel *ochAddLine(char* pName, uchar** ppRestOfConfLine)
 
183
{
 
184
        struct outchannel *pOch;
 
185
        uchar *p;
 
186
 
 
187
        assert(pName != NULL);
 
188
        assert(ppRestOfConfLine != NULL);
 
189
 
 
190
        if((pOch = ochConstruct()) == NULL)
 
191
                return NULL;
 
192
        
 
193
        pOch->iLenName = strlen(pName);
 
194
        pOch->pszName = (char*) malloc(sizeof(char) * (pOch->iLenName + 1));
 
195
        if(pOch->pszName == NULL) {
 
196
                dbgprintf("ochAddLine could not alloc memory for outchannel name!");
 
197
                pOch->iLenName = 0;
 
198
                return NULL;
 
199
                /* I know - we create a memory leak here - but I deem
 
200
                 * it acceptable as it is a) a very small leak b) very
 
201
                 * unlikely to happen. rgerhards 2004-11-17
 
202
                 */
 
203
        }
 
204
        memcpy(pOch->pszName, pName, pOch->iLenName + 1);
 
205
 
 
206
        /* now actually parse the line */
 
207
        p = *ppRestOfConfLine;
 
208
        assert(p != NULL);
 
209
 
 
210
        /* get params */
 
211
        get_Field(&p, &pOch->pszFileTemplate);
 
212
        if(*p) get_off_t(&p, &pOch->uSizeLimit);
 
213
        if(*p) get_restOfLine(&p, &pOch->cmdOnSizeLimit);
 
214
 
 
215
        *ppRestOfConfLine = p;
 
216
        return(pOch);
 
217
}
 
218
 
 
219
 
 
220
/* Find a outchannel object based on name. Search
 
221
 * currently is case-senstive (should we change?).
 
222
 * returns pointer to outchannel object if found and
 
223
 * NULL otherwise.
 
224
 * rgerhards 2004-11-17
 
225
 */
 
226
struct outchannel *ochFind(char *pName, int iLenName)
 
227
{
 
228
        struct outchannel *pOch;
 
229
 
 
230
        assert(pName != NULL);
 
231
 
 
232
        pOch = ochRoot;
 
233
        while(pOch != NULL &&
 
234
              !(pOch->iLenName == iLenName &&
 
235
                !strcmp(pOch->pszName, pName)
 
236
                ))
 
237
                {
 
238
                        pOch = pOch->pNext;
 
239
                }
 
240
        return(pOch);
 
241
}
 
242
 
 
243
/* Destroy the outchannel structure. This is for de-initialization
 
244
 * at program end. Everything is deleted.
 
245
 * rgerhards 2005-02-22
 
246
 */
 
247
void ochDeleteAll(void)
 
248
{
 
249
        struct outchannel *pOch, *pOchDel;
 
250
 
 
251
        pOch = ochRoot;
 
252
        while(pOch != NULL) {
 
253
                dbgprintf("Delete Outchannel: Name='%s'\n ", pOch->pszName == NULL? "NULL" : pOch->pszName);
 
254
                pOchDel = pOch;
 
255
                pOch = pOch->pNext;
 
256
                if(pOchDel->pszName != NULL)
 
257
                        free(pOchDel->pszName);
 
258
                free(pOchDel);
 
259
        }
 
260
}
 
261
 
 
262
 
 
263
/* Print the outchannel structure. This is more or less a 
 
264
 * debug or test aid, but anyhow I think it's worth it...
 
265
 */
 
266
void ochPrintList(void)
 
267
{
 
268
        struct outchannel *pOch;
 
269
 
 
270
        pOch = ochRoot;
 
271
        while(pOch != NULL) {
 
272
                dbgprintf("Outchannel: Name='%s'\n", pOch->pszName == NULL? "NULL" : pOch->pszName);
 
273
                dbgprintf("\tFile Template: '%s'\n", pOch->pszFileTemplate == NULL ? "NULL" : (char*) pOch->pszFileTemplate);
 
274
                dbgprintf("\tMax Size.....: %lu\n", pOch->uSizeLimit);
 
275
                dbgprintf("\tOnSizeLimtCmd: '%s'\n", pOch->cmdOnSizeLimit == NULL ? "NULL" : (char*) pOch->cmdOnSizeLimit);
 
276
                pOch = pOch->pNext; /* done, go next */
 
277
        }
 
278
}
 
279
/*
 
280
 * vi:set ai:
 
281
 */