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