| 1 |
/* imtuxedoulog.c |
| 2 |
* |
| 3 |
* This is the input module for reading Tuxedo ULOG files. The particularity of this file |
| 4 |
* is that the timestamp is split between the filename (date) and the log line (time). |
| 5 |
* So this module switches on the date base betwwen files to open only the current file. |
| 6 |
* The log line is parsed according to the Tuxedo format. The ECID is extracted as a |
| 7 |
* structured data attribute. |
| 8 |
* |
| 9 |
* Work originally begun on 2019-01-11 by Philippe Duveau |
| 10 |
* |
| 11 |
* This file is contribution of rsyslog. |
| 12 |
* |
| 13 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 14 |
* you may not use this file except in compliance with the License. |
| 15 |
* You may obtain a copy of the License at |
| 16 |
* |
| 17 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 18 |
* -or- |
| 19 |
* see COPYING.ASL20 in the source distribution |
| 20 |
* |
| 21 |
* Unless required by applicable law or agreed to in writing, software |
| 22 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 23 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 24 |
* See the License for the specific language governing permissions and |
| 25 |
* limitations under the License. |
| 26 |
*/ |
| 27 |
#include "config.h" |
| 28 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <assert.h> |
| 31 |
#include <string.h> |
| 32 |
#include <errno.h> |
| 33 |
#include <fcntl.h> |
| 34 |
#include <pthread.h> /* do NOT remove: will soon be done by the module generation macros */ |
| 35 |
#include <sys/types.h> |
| 36 |
#include <unistd.h> |
| 37 |
#include <glob.h> |
| 38 |
#include <poll.h> |
| 39 |
#include <fnmatch.h> |
| 40 |
#ifdef HAVE_SYS_TIME_H |
| 41 |
#include <sys/time.h> |
| 42 |
#endif |
| 43 |
#ifdef HAVE_SYS_STAT_H |
| 44 |
#include <sys/stat.h> |
| 45 |
#endif |
| 46 |
#ifdef _AIX |
| 47 |
#include <alloca.h> |
| 48 |
#endif |
| 49 |
#include "rsyslog.h" /* error codes etc... */ |
| 50 |
#include "dirty.h" |
| 51 |
#include "cfsysline.h" /* access to config file objects */ |
| 52 |
#include "module-template.h" /* generic module interface code - very important, read it! */ |
| 53 |
#include "srUtils.h" /* some utility functions */ |
| 54 |
#include "msg.h" |
| 55 |
#include "stream.h" |
| 56 |
#include "errmsg.h" |
| 57 |
#include "glbl.h" |
| 58 |
#include "unicode-helper.h" |
| 59 |
#include "prop.h" |
| 60 |
#include "stringbuf.h" |
| 61 |
#include "ruleset.h" |
| 62 |
#include "ratelimit.h" |
| 63 |
|
| 64 |
struct instanceConf_s { |
| 65 |
uchar *pszUlogBaseName; |
| 66 |
uchar *pszCurrFName; |
| 67 |
struct tm currTm; |
| 68 |
uchar *pszTag; |
| 69 |
size_t lenTag; |
| 70 |
uchar *pszStateFile; |
| 71 |
uchar *pszBindRuleset; |
| 72 |
int nMultiSub; |
| 73 |
int iPersistStateInterval; |
| 74 |
int iFacility; |
| 75 |
int iSeverity; |
| 76 |
strm_t *pStrm; /* its stream (NULL if not assigned) */ |
| 77 |
int maxLinesAtOnce; |
| 78 |
ruleset_t *pBindRuleset; /* ruleset to bind listener to (use system default if unspecified) */ |
| 79 |
ratelimit_t *ratelimiter; |
| 80 |
multi_submit_t multiSub; |
| 81 |
int nRecords; |
| 82 |
struct instanceConf_s *next; |
| 83 |
struct instanceConf_s *prev; |
| 84 |
}; |
| 85 |
|
| 86 |
/* config variables */ |
| 87 |
struct modConfData_s { |
| 88 |
rsconf_t *pConf; /* our overall config object */ |
| 89 |
}; |
| 90 |
|
| 91 |
static instanceConf_t *confRoot = NULL; |
| 92 |
static modConfData_t *loadModConf = NULL; /* modConf ptr to use for the current load process */ |
| 93 |
static modConfData_t *runModConf = NULL; /* modConf ptr to use for run process */ |
| 94 |
|
| 95 |
|
| 96 |
MODULE_TYPE_INPUT /* must be present for input modules, do not remove */ |
| 97 |
MODULE_TYPE_NOKEEP; |
| 98 |
MODULE_CNFNAME("imtuxedoulog") |
| 99 |
|
| 100 |
/* defines */ |
| 101 |
|
| 102 |
/* Module static data */ |
| 103 |
DEF_IMOD_STATIC_DATA /* must be present, starts static data */ |
| 104 |
DEFobjCurrIf(glbl) DEFobjCurrIf(strm) DEFobjCurrIf(prop) DEFobjCurrIf(ruleset) |
| 105 |
|
| 106 |
#define NUM_MULTISUB 1024 /* default max number of submits */ |
| 107 |
#define DFLT_PollInterval 10 |
| 108 |
|
| 109 |
int iPollInterval = DFLT_PollInterval; |
| 110 |
int iPersistStateInterval = 0; /* how often if state file to be persisted? (default 0->never) */ |
| 111 |
|
| 112 |
struct syslogTime syslogTz; |
| 113 |
|
| 114 |
static prop_t *pInputName = NULL; |
| 115 |
/* there is only one global inputName for all messages generated by this input */ |
| 116 |
|
| 117 |
/* module-global parameters */ |
| 118 |
/* input instance parameters */ |
| 119 |
static struct cnfparamdescr inppdescr[] = {{"ulogbase", eCmdHdlrString, CNFPARAM_REQUIRED}, |
| 120 |
{"tag", eCmdHdlrString, CNFPARAM_REQUIRED}, |
| 121 |
{"severity", eCmdHdlrSeverity, 0}, |
| 122 |
{"facility", eCmdHdlrFacility, 0}, |
| 123 |
{"ruleset", eCmdHdlrString, 0}, |
| 124 |
{"maxlinesatonce", eCmdHdlrInt, 0}, |
| 125 |
{"persiststateinterval", eCmdHdlrInt, 0}, |
| 126 |
{"maxsubmitatonce", eCmdHdlrInt, 0}}; |
| 127 |
static struct cnfparamblk inppblk = {CNFPARAMBLK_VERSION, sizeof(inppdescr) / sizeof(struct cnfparamdescr), inppdescr}; |
| 128 |
|
| 129 |
#include "im-helper.h" /* must be included AFTER the type definitions! */ |
| 130 |
|
| 131 |
static uchar *mkFileNameWithTime(instanceConf_t *in) { |
| 132 |
uchar out[MAXFNAME]; |
| 133 |
struct timeval tp; |
| 134 |
#if defined(__hpux) |
| 135 |
struct timezone tz; |
| 136 |
gettimeofday(&tp, &tz); |
| 137 |
#else |
| 138 |
gettimeofday(&tp, NULL); |
| 139 |
#endif |
| 140 |
localtime_r(&tp.tv_sec, &(in->currTm)); |
| 141 |
snprintf((char *)out, MAXFNAME, "%s.%02d%02d%02d", (char *)in->pszUlogBaseName, in->currTm.tm_mon + 1, |
| 142 |
in->currTm.tm_mday, in->currTm.tm_year % 100); |
| 143 |
return ustrdup(out); |
| 144 |
} |
| 145 |
|
| 146 |
/* |
| 147 |
* Helper function to combine statefile and workdir |
| 148 |
*/ |
| 149 |
static int getFullStateFileName(uchar *pszstatefile, uchar *pszout, int ilenout) { |
| 150 |
int lenout; |
| 151 |
const uchar *pszworkdir; |
| 152 |
|
| 153 |
/* Get Raw Workdir, if it is NULL we need to propper handle it */ |
| 154 |
pszworkdir = glblGetWorkDirRaw(runModConf->pConf); |
| 155 |
|
| 156 |
/* Construct file name */ |
| 157 |
lenout = snprintf((char *)pszout, ilenout, "%s/%s", (char *)(pszworkdir == NULL ? "." : (char *)pszworkdir), |
| 158 |
(char *)pszstatefile); |
| 159 |
|
| 160 |
/* return out length */ |
| 161 |
return lenout; |
| 162 |
} |
| 163 |
|
| 164 |
/* this generates a state file name suitable for the current file. To avoid |
| 165 |
* malloc calls, it must be passed a buffer which should be MAXFNAME large. |
| 166 |
* Note: the buffer is not necessarily populated ... always ONLY use the |
| 167 |
* RETURN VALUE! |
| 168 |
*/ |
| 169 |
static uchar *ATTR_NONNULL(2) getStateFileName(instanceConf_t *const __restrict__ pInst, |
| 170 |
uchar *const __restrict__ buf, |
| 171 |
size_t lenbuf, |
| 172 |
const uchar *pszFileName) { |
| 173 |
uchar *ret; |
| 174 |
|
| 175 |
/* Use pszFileName parameter if set */ |
| 176 |
pszFileName = pszFileName == NULL ? pInst->pszUlogBaseName : pszFileName; |
| 177 |
|
| 178 |
DBGPRINTF("getStateFileName for '%s'\n", pszFileName); |
| 179 |
if (pInst == NULL || pInst->pszStateFile == NULL) { |
| 180 |
lenbuf = snprintf((char *)buf, lenbuf - 1, "imtuxedoulog-state:%s", pszFileName); |
| 181 |
buf[lenbuf] = '\0'; /* be on the safe side... */ |
| 182 |
uchar *p = buf; |
| 183 |
for (; *p; ++p) { |
| 184 |
if (*p == '/') *p = '-'; |
| 185 |
} |
| 186 |
ret = buf; |
| 187 |
} else { |
| 188 |
ret = pInst->pszStateFile; |
| 189 |
} |
| 190 |
return ret; |
| 191 |
} |
| 192 |
|
| 193 |
/* this func parses the line according to samples described in README.md |
| 194 |
*/ |
| 195 |
static rsRetVal parseMsg(smsg_t *pMsg, char *rawMsg, size_t msgLen, instanceConf_t *const __restrict__ pInst) { |
| 196 |
char *prog, *host, *text = NULL, *strtData = NULL, *tmp; |
| 197 |
int hour, min, sec; |
| 198 |
rsRetVal ret; |
| 199 |
|
| 200 |
hour = (rawMsg[0] ^ 0x30) * 10 + (rawMsg[1] ^ 0x30); |
| 201 |
min = (rawMsg[2] ^ 0x30) * 10 + (rawMsg[3] ^ 0x30); |
| 202 |
sec = (rawMsg[4] ^ 0x30) * 10 + (rawMsg[5] ^ 0x30); |
| 203 |
|
| 204 |
if (hour < 0 || hour > 23 || min < 0 || min > 59 || sec < 0 || sec > 59) return RS_RET_COULD_NOT_PARSE; |
| 205 |
|
| 206 |
host = rawMsg + ((rawMsg[10] == '.') ? 11 : 10); |
| 207 |
|
| 208 |
prog = memchr(host, '!', msgLen - (host - rawMsg)); |
| 209 |
|
| 210 |
if (prog == NULL) return RS_RET_COULD_NOT_PARSE; |
| 211 |
|
| 212 |
prog++; |
| 213 |
|
| 214 |
strtData = memchr(prog, ':', msgLen - (prog - rawMsg)); |
| 215 |
|
| 216 |
if (strtData == NULL) return RS_RET_COULD_NOT_PARSE; |
| 217 |
|
| 218 |
pMsg->tTIMESTAMP.year = pInst->currTm.tm_year + 1900; |
| 219 |
pMsg->tTIMESTAMP.month = pInst->currTm.tm_mon + 1; |
| 220 |
pMsg->tTIMESTAMP.day = pInst->currTm.tm_mday; |
| 221 |
pMsg->tTIMESTAMP.hour = hour; |
| 222 |
pMsg->tTIMESTAMP.minute = min; |
| 223 |
pMsg->tTIMESTAMP.second = sec; |
| 224 |
pMsg->tTIMESTAMP.OffsetMode = syslogTz.OffsetMode; |
| 225 |
pMsg->tTIMESTAMP.OffsetHour = syslogTz.OffsetHour; |
| 226 |
pMsg->tTIMESTAMP.OffsetMinute = syslogTz.OffsetMinute; |
| 227 |
|
| 228 |
pMsg->tTIMESTAMP.secfrac = atoi(rawMsg + 7); |
| 229 |
/* secfracprecision depends on the char on position 9 (case 1 or case 2) */ |
| 230 |
pMsg->tTIMESTAMP.secfracPrecision = (rawMsg[9] == '.') ? 2 : 3; |
| 231 |
|
| 232 |
for (tmp = strtData; prog < tmp && *tmp != '.'; tmp--); |
| 233 |
if (tmp > prog) |
| 234 |
*tmp = '\0'; |
| 235 |
else |
| 236 |
*strtData = '\0'; |
| 237 |
|
| 238 |
strtData = strtData + 2; |
| 239 |
|
| 240 |
/* Case 4 */ |
| 241 |
if (memcmp(strtData, "gtrid", 5) == 0) { |
| 242 |
strtData = memchr(strtData, ':', msgLen - (strtData - rawMsg)); |
| 243 |
if (strtData != NULL) strtData += 2; |
| 244 |
} |
| 245 |
|
| 246 |
text = strtData; |
| 247 |
|
| 248 |
/* ecid point to message text or the word ECID */ |
| 249 |
if (strtData != NULL && memcmp(strtData, "ECID", 4) == 0) { |
| 250 |
text = memchr(strtData + 6, '>', msgLen - (strtData - rawMsg)); |
| 251 |
/* case 3 : we have the word ECID */ |
| 252 |
if (text != NULL) { |
| 253 |
*(--strtData) = '['; |
| 254 |
strtData[5] = '='; |
| 255 |
strtData[6] = '\"'; |
| 256 |
*text++ = '\"'; |
| 257 |
*text++ = ']'; |
| 258 |
text++; |
| 259 |
ret = MsgAddToStructuredData(pMsg, (uchar *)strtData, text - strtData); |
| 260 |
if (ret != RS_RET_OK) LogMsg(0, ret, LOG_WARNING, "Add StructuredData to message failed."); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
/* now compute the new length */ |
| 265 |
msgLen -= text - rawMsg; |
| 266 |
|
| 267 |
if (text != NULL) MsgSetRawMsg(pMsg, text, msgLen); |
| 268 |
|
| 269 |
MsgSetMSGoffs(pMsg, 0); |
| 270 |
|
| 271 |
/* set hostname */ |
| 272 |
MsgSetHOSTNAME(pMsg, (const uchar *)host, prog - host - 1); |
| 273 |
|
| 274 |
if (*prog == '\0') return 0; |
| 275 |
|
| 276 |
/* set procid */ |
| 277 |
ret = MsgSetPROCID(pMsg, prog); |
| 278 |
if (ret != RS_RET_OK) LogMsg(0, ret, LOG_WARNING, "Set PROCID to message failed."); |
| 279 |
|
| 280 |
return RS_RET_OK; |
| 281 |
} |
| 282 |
|
| 283 |
/* enqueue the read file line as a message. The provided string is |
| 284 |
* not freed - this must be done by the caller. |
| 285 |
*/ |
| 286 |
#define MAX_OFFSET_REPRESENTATION_NUM_BYTES 20 |
| 287 |
static rsRetVal enqLine(instanceConf_t *const __restrict__ pInst, cstr_t *const __restrict__ cstrLine) { |
| 288 |
DEFiRet; |
| 289 |
smsg_t *pMsg; |
| 290 |
const size_t msgLen = cstrLen(cstrLine); |
| 291 |
rsRetVal ret; |
| 292 |
|
| 293 |
if (msgLen == 0) { |
| 294 |
/* we do not process empty lines */ |
| 295 |
FINALIZE; |
| 296 |
} |
| 297 |
|
| 298 |
CHKiRet(msgConstruct(&pMsg)); |
| 299 |
|
| 300 |
if (parseMsg(pMsg, (char *)rsCStrGetSzStrNoNULL(cstrLine), msgLen, pInst) != RS_RET_OK) { |
| 301 |
if ((ret = msgDestruct(&pMsg)) != RS_RET_OK) LogMsg(0, ret, LOG_ERR, "msgDestruct failed."); |
| 302 |
FINALIZE; |
| 303 |
} |
| 304 |
|
| 305 |
MsgSetInputName(pMsg, pInputName); |
| 306 |
MsgSetTAG(pMsg, pInst->pszTag, pInst->lenTag); |
| 307 |
msgSetPRI(pMsg, pInst->iFacility | pInst->iSeverity); |
| 308 |
MsgSetRuleset(pMsg, pInst->pBindRuleset); |
| 309 |
if ((ret = MsgSetFlowControlType(pMsg, eFLOWCTL_FULL_DELAY)) != RS_RET_OK) |
| 310 |
LogMsg(0, ret, LOG_WARNING, "Set Flow Control to message failed."); |
| 311 |
if ((ret = MsgSetAPPNAME(pMsg, (const char *)pInst->pszTag)) != RS_RET_OK) |
| 312 |
LogMsg(0, ret, LOG_WARNING, "Set APPNAME to message failed."); |
| 313 |
|
| 314 |
if ((iRet = ratelimitAddMsg(pInst->ratelimiter, &pInst->multiSub, pMsg)) != RS_RET_OK) { |
| 315 |
if ((ret = msgDestruct(&pMsg)) != RS_RET_OK) LogMsg(0, ret, LOG_ERR, "msgDestruct failed."); |
| 316 |
} |
| 317 |
finalize_it: |
| 318 |
RETiRet; |
| 319 |
} |
| 320 |
|
| 321 |
/* try to open a file which has a state file. If the state file does not |
| 322 |
* exist or cannot be read, an error is returned. |
| 323 |
*/ |
| 324 |
static rsRetVal ATTR_NONNULL(1) openFileWithStateFile(instanceConf_t *const __restrict__ pInst) { |
| 325 |
DEFiRet; |
| 326 |
strm_t *psSF = NULL; |
| 327 |
uchar pszSFNam[MAXFNAME]; |
| 328 |
size_t lenSFNam; |
| 329 |
struct stat stat_buf; |
| 330 |
uchar statefile[MAXFNAME]; |
| 331 |
|
| 332 |
uchar *const statefn = getStateFileName(pInst, statefile, sizeof(statefile), NULL); |
| 333 |
DBGPRINTF("trying to open state for '%s', state file '%s'\n", pInst->pszUlogBaseName, statefn); |
| 334 |
|
| 335 |
/* Get full path and file name */ |
| 336 |
lenSFNam = getFullStateFileName(statefn, pszSFNam, sizeof(pszSFNam)); |
| 337 |
|
| 338 |
/* check if the file exists */ |
| 339 |
if (stat((char *)pszSFNam, &stat_buf) == -1) { |
| 340 |
if (errno == ENOENT) { |
| 341 |
DBGPRINTF("NO state file (%s) exists for '%s'\n", pszSFNam, pInst->pszUlogBaseName); |
| 342 |
ABORT_FINALIZE(RS_RET_FILE_NOT_FOUND); |
| 343 |
} else { |
| 344 |
char errStr[1024]; |
| 345 |
rs_strerror_r(errno, errStr, sizeof(errStr)); |
| 346 |
DBGPRINTF("error trying to access state file for '%s':%s\n", pInst->pszUlogBaseName, errStr); |
| 347 |
ABORT_FINALIZE(RS_RET_IO_ERROR); |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
/* If we reach this point, we have a state file */ |
| 352 |
|
| 353 |
CHKiRet(strm.Construct(&psSF)); |
| 354 |
CHKiRet(strm.SettOperationsMode(psSF, STREAMMODE_READ)); |
| 355 |
CHKiRet(strm.SetsType(psSF, STREAMTYPE_FILE_SINGLE)); |
| 356 |
CHKiRet(strm.SetFName(psSF, pszSFNam, lenSFNam)); |
| 357 |
CHKiRet(strm.SetFileNotFoundError(psSF, 1)); |
| 358 |
CHKiRet(strm.ConstructFinalize(psSF)); |
| 359 |
|
| 360 |
/* read back in the object */ |
| 361 |
CHKiRet(obj.Deserialize(&pInst->pStrm, (uchar *)"strm", psSF, NULL, pInst)); |
| 362 |
DBGPRINTF( |
| 363 |
"deserialized state file, state file base name '%s', " |
| 364 |
"configured base name '%s'\n", |
| 365 |
pInst->pStrm->pszFName, pInst->pszUlogBaseName); |
| 366 |
if (ustrcmp(pInst->pStrm->pszFName, pInst->pszCurrFName)) { |
| 367 |
LogError(0, RS_RET_STATEFILE_WRONG_FNAME, |
| 368 |
"imtuxedoulog: state file '%s' " |
| 369 |
"contains file name '%s', but is used for file '%s'. State " |
| 370 |
"file deleted, starting from begin of file.", |
| 371 |
pszSFNam, pInst->pStrm->pszFName, pInst->pszCurrFName); |
| 372 |
|
| 373 |
unlink((char *)pszSFNam); |
| 374 |
ABORT_FINALIZE(RS_RET_STATEFILE_WRONG_FNAME); |
| 375 |
} |
| 376 |
|
| 377 |
strm.CheckFileChange(pInst->pStrm); |
| 378 |
CHKiRet(strm.SeekCurrOffs(pInst->pStrm)); |
| 379 |
|
| 380 |
/* note: we do not delete the state file, so that the last position remains |
| 381 |
* known even in the case that rsyslogd aborts for some reason (like powerfail) |
| 382 |
*/ |
| 383 |
|
| 384 |
finalize_it: |
| 385 |
if (psSF != NULL) strm.Destruct(&psSF); |
| 386 |
|
| 387 |
RETiRet; |
| 388 |
} |
| 389 |
|
| 390 |
/* try to open a file for which no state file exists. This function does NOT |
| 391 |
* check if a state file actually exists or not -- this must have been |
| 392 |
* checked before calling it. |
| 393 |
*/ |
| 394 |
static rsRetVal openFileWithoutStateFile(instanceConf_t *const __restrict__ pInst) { |
| 395 |
DEFiRet; |
| 396 |
|
| 397 |
DBGPRINTF("clean startup withOUT state file for '%s'\n", pInst->pszUlogBaseName); |
| 398 |
if (pInst->pStrm != NULL) strm.Destruct(&pInst->pStrm); |
| 399 |
CHKiRet(strm.Construct(&pInst->pStrm)); |
| 400 |
CHKiRet(strm.SettOperationsMode(pInst->pStrm, STREAMMODE_READ)); |
| 401 |
CHKiRet(strm.SetsType(pInst->pStrm, STREAMTYPE_FILE_MONITOR)); |
| 402 |
CHKiRet(strm.SetFName(pInst->pStrm, pInst->pszCurrFName, strlen((char *)pInst->pszCurrFName))); |
| 403 |
CHKiRet(strm.SetFileNotFoundError(pInst->pStrm, 1)); |
| 404 |
CHKiRet(strm.ConstructFinalize(pInst->pStrm)); |
| 405 |
|
| 406 |
finalize_it: |
| 407 |
RETiRet; |
| 408 |
} |
| 409 |
|
| 410 |
/* try to open a file. This involves checking if there is a status file and, |
| 411 |
* if so, reading it in. Processing continues from the last known location. |
| 412 |
*/ |
| 413 |
static rsRetVal openFile(instanceConf_t *const __restrict__ pInst) { |
| 414 |
DEFiRet; |
| 415 |
|
| 416 |
CHKiRet_Hdlr(openFileWithStateFile(pInst)) { |
| 417 |
CHKiRet(openFileWithoutStateFile(pInst)); |
| 418 |
} |
| 419 |
|
| 420 |
CHKiRet(strm.SetbReopenOnTruncate(pInst->pStrm, 1)); |
| 421 |
|
| 422 |
finalize_it: |
| 423 |
RETiRet; |
| 424 |
} |
| 425 |
|
| 426 |
/* This function persists information for a specific file being monitored. |
| 427 |
* To do so, it simply persists the stream object. We do NOT abort on error |
| 428 |
* iRet as that makes matters worse (at least we can try persisting the others...). |
| 429 |
* rgerhards, 2008-02-13 |
| 430 |
*/ |
| 431 |
static void persistStrmState(instanceConf_t *pInst) { |
| 432 |
DEFiRet; |
| 433 |
strm_t *psSF = NULL; /* state file (stream) */ |
| 434 |
size_t lenDir; |
| 435 |
uchar statefile[MAXFNAME]; |
| 436 |
|
| 437 |
uchar *const statefn = getStateFileName(pInst, statefile, sizeof(statefile), NULL); |
| 438 |
DBGPRINTF("persisting state for '%s' to file '%s'\n", pInst->pszUlogBaseName, statefn); |
| 439 |
CHKiRet(strm.Construct(&psSF)); |
| 440 |
lenDir = ustrlen(glbl.GetWorkDir(runModConf->pConf)); |
| 441 |
if (lenDir > 0) CHKiRet(strm.SetDir(psSF, glbl.GetWorkDir(runModConf->pConf), lenDir)); |
| 442 |
CHKiRet(strm.SettOperationsMode(psSF, STREAMMODE_WRITE_TRUNC)); |
| 443 |
CHKiRet(strm.SetsType(psSF, STREAMTYPE_FILE_SINGLE)); |
| 444 |
CHKiRet(strm.SetFName(psSF, statefn, strlen((char *)statefn))); |
| 445 |
CHKiRet(strm.SetFileNotFoundError(psSF, 1)); |
| 446 |
CHKiRet(strm.ConstructFinalize(psSF)); |
| 447 |
|
| 448 |
CHKiRet(strm.Serialize(pInst->pStrm, psSF)); |
| 449 |
CHKiRet(strm.Flush(psSF)); |
| 450 |
|
| 451 |
CHKiRet(strm.Destruct(&psSF)); |
| 452 |
|
| 453 |
finalize_it: |
| 454 |
if (psSF != NULL) strm.Destruct(&psSF); |
| 455 |
|
| 456 |
if (iRet != RS_RET_OK) { |
| 457 |
LogError(0, iRet, |
| 458 |
"imtuxedoulog: could not persist state " |
| 459 |
"file %s - data may be repeated on next " |
| 460 |
"startup. Is WorkDirectory set?", |
| 461 |
statefn); |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
/* The following is a cancel cleanup handler for strmReadLine(). It is necessary in case |
| 466 |
* strmReadLine() is cancelled while processing the stream. -- rgerhards, 2008-03-27 |
| 467 |
*/ |
| 468 |
static void pollFileCancelCleanup(void *pArg) { |
| 469 |
cstr_t **ppCStr = (cstr_t **)pArg; |
| 470 |
if (*ppCStr != NULL) { |
| 471 |
rsCStrDestruct(ppCStr); |
| 472 |
} |
| 473 |
} |
| 474 |
|
| 475 |
static void pollFileReal(instanceConf_t *pInst, int *pbHadFileData, cstr_t **ppCStr) { |
| 476 |
DEFiRet; |
| 477 |
|
| 478 |
int nProcessed = 0; |
| 479 |
if (pInst->pStrm == NULL) { |
| 480 |
CHKiRet(openFile(pInst)); /* open file */ |
| 481 |
} |
| 482 |
|
| 483 |
/* loop below will be exited when strmReadLine() returns EOF */ |
| 484 |
while (glbl.GetGlobalInputTermState() == 0) { |
| 485 |
if (pInst->maxLinesAtOnce != 0 && nProcessed >= pInst->maxLinesAtOnce) break; |
| 486 |
CHKiRet(strm.ReadLine(pInst->pStrm, ppCStr, 0, 0, NULL, -1, NULL)); |
| 487 |
++nProcessed; |
| 488 |
if (pbHadFileData != NULL) *pbHadFileData = 1; /* this is just a flag, so set it and forget it */ |
| 489 |
CHKiRet(enqLine(pInst, *ppCStr)); /* process line */ |
| 490 |
rsCStrDestruct(ppCStr); /* discard string (must be done by us!) */ |
| 491 |
|
| 492 |
if (pInst->iPersistStateInterval > 0 && ++pInst->nRecords >= pInst->iPersistStateInterval) { |
| 493 |
persistStrmState(pInst); |
| 494 |
pInst->nRecords = 0; |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
finalize_it: |
| 499 |
multiSubmitFlush(&pInst->multiSub); |
| 500 |
|
| 501 |
if (*ppCStr != NULL) { |
| 502 |
rsCStrDestruct(ppCStr); |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
/* poll a file, need to check file rollover etc. open file if not open */ |
| 507 |
static void pollFile(instanceConf_t *pInst, int *pbHadFileData) { |
| 508 |
cstr_t *pCStr = NULL; |
| 509 |
/* Note: we must do pthread_cleanup_push() immediately, because the POSIX macros |
| 510 |
* otherwise do not work if I include the _cleanup_pop() inside an if... -- rgerhards, 2008-08-14 |
| 511 |
*/ |
| 512 |
pthread_cleanup_push(pollFileCancelCleanup, &pCStr); |
| 513 |
pollFileReal(pInst, pbHadFileData, &pCStr); |
| 514 |
pthread_cleanup_pop(0); |
| 515 |
} |
| 516 |
|
| 517 |
|
| 518 |
/* create input instance, set default parameters, and |
| 519 |
* add it to the list of instances. |
| 520 |
*/ |
| 521 |
static rsRetVal ATTR_NONNULL(1) createInstance(instanceConf_t **const pinst) { |
| 522 |
instanceConf_t *inst; |
| 523 |
DEFiRet; |
| 524 |
CHKmalloc(inst = malloc(sizeof(instanceConf_t))); |
| 525 |
inst->next = NULL; |
| 526 |
inst->pBindRuleset = NULL; |
| 527 |
inst->ratelimiter = NULL; |
| 528 |
inst->pStrm = NULL; |
| 529 |
inst->multiSub.ppMsgs = NULL; |
| 530 |
|
| 531 |
inst->pszBindRuleset = NULL; |
| 532 |
inst->pszUlogBaseName = NULL; |
| 533 |
inst->pszCurrFName = NULL; |
| 534 |
inst->pszTag = NULL; |
| 535 |
inst->pszStateFile = NULL; |
| 536 |
inst->nMultiSub = NUM_MULTISUB; |
| 537 |
inst->iSeverity = 5; |
| 538 |
inst->iFacility = 128; |
| 539 |
inst->maxLinesAtOnce = 0; |
| 540 |
inst->iPersistStateInterval = 0; |
| 541 |
inst->nRecords = 0; |
| 542 |
|
| 543 |
*pinst = inst; |
| 544 |
finalize_it: |
| 545 |
RETiRet; |
| 546 |
} |
| 547 |
|
| 548 |
/* This adds a new listener object to the bottom of the list, but |
| 549 |
* it does NOT initialize any data members except for the list |
| 550 |
* pointers themselves. |
| 551 |
*/ |
| 552 |
static rsRetVal ATTR_NONNULL() lstnAdd(instanceConf_t *pInst) { |
| 553 |
DEFiRet; |
| 554 |
CHKiRet(ratelimitNew(&pInst->ratelimiter, "imtuxedoulog", (char *)pInst->pszUlogBaseName)); |
| 555 |
CHKmalloc(pInst->multiSub.ppMsgs = malloc(pInst->nMultiSub * sizeof(smsg_t *))); |
| 556 |
pInst->multiSub.maxElem = pInst->nMultiSub; |
| 557 |
pInst->multiSub.nElem = 0; |
| 558 |
|
| 559 |
/* insert it at the begin of the list */ |
| 560 |
pInst->prev = NULL; |
| 561 |
pInst->next = confRoot; |
| 562 |
|
| 563 |
if (confRoot != NULL) confRoot->prev = pInst; |
| 564 |
|
| 565 |
confRoot = pInst; |
| 566 |
|
| 567 |
finalize_it: |
| 568 |
RETiRet; |
| 569 |
} |
| 570 |
|
| 571 |
/* delete a listener object */ |
| 572 |
static void ATTR_NONNULL(1) lstnDel(instanceConf_t *pInst) { |
| 573 |
DBGPRINTF("lstnDel called for %s\n", pInst->pszUlogBaseName); |
| 574 |
if (pInst->pStrm != NULL) { /* stream open? */ |
| 575 |
persistStrmState(pInst); |
| 576 |
strm.Destruct(&(pInst->pStrm)); |
| 577 |
} |
| 578 |
if (pInst->ratelimiter != NULL) ratelimitDestruct(pInst->ratelimiter); |
| 579 |
if (pInst->multiSub.ppMsgs != NULL) free(pInst->multiSub.ppMsgs); |
| 580 |
|
| 581 |
free(pInst->pszUlogBaseName); |
| 582 |
if (pInst->pszCurrFName != NULL) free(pInst->pszCurrFName); |
| 583 |
if (pInst->pszTag) free(pInst->pszTag); |
| 584 |
if (pInst->pszStateFile) free(pInst->pszStateFile); |
| 585 |
if (pInst->pszBindRuleset != NULL) free(pInst->pszBindRuleset); |
| 586 |
free(pInst); |
| 587 |
} |
| 588 |
|
| 589 |
/* Monitor files in traditional polling mode. |
| 590 |
*/ |
| 591 |
static void do_polling(void) { |
| 592 |
int bHadFileData; /* were there at least one file with data during this run? */ |
| 593 |
struct stat sb; |
| 594 |
while (glbl.GetGlobalInputTermState() == 0) { |
| 595 |
do { |
| 596 |
instanceConf_t *pInst; |
| 597 |
bHadFileData = 0; |
| 598 |
for (pInst = confRoot; pInst != NULL; pInst = pInst->next) { |
| 599 |
uchar *temp = mkFileNameWithTime(pInst); |
| 600 |
|
| 601 |
DBGPRINTF("imtuxedoulog: do_polling start '%s' / '%s'\n", pInst->pszUlogBaseName, temp); |
| 602 |
/* |
| 603 |
* Is the file name is different : a rotation time is reached |
| 604 |
* If so, then it the new file exists ? and is a file ? |
| 605 |
*/ |
| 606 |
if (temp && stat((const char *)temp, &sb) == 0 && S_ISREG(sb.st_mode) && |
| 607 |
(pInst->pszCurrFName == NULL || strcmp((char *)temp, (char *)pInst->pszCurrFName) != 0)) { |
| 608 |
DBGPRINTF( |
| 609 |
"imtuxedoulog: timed file : rotation reach " |
| 610 |
"switching form '%s' to '%s' !", |
| 611 |
(char *)pInst->pszUlogBaseName, temp); |
| 612 |
|
| 613 |
/* first of all change the listener datas */ |
| 614 |
if (pInst->pszCurrFName != NULL) { |
| 615 |
free(pInst->pszCurrFName); |
| 616 |
strm.Destruct(&pInst->pStrm); |
| 617 |
} |
| 618 |
pInst->pszCurrFName = temp; |
| 619 |
temp = NULL; |
| 620 |
|
| 621 |
/* And finish by destroy the stream object, so the next polling will recreate |
| 622 |
* it based on new data. |
| 623 |
*/ |
| 624 |
if (glbl.GetGlobalInputTermState() == 1) break; /* terminate input! */ |
| 625 |
} |
| 626 |
if (temp) free(temp); |
| 627 |
|
| 628 |
/* let's poll the file */ |
| 629 |
if (pInst->pszCurrFName != NULL) pollFile(pInst, &bHadFileData); |
| 630 |
|
| 631 |
DBGPRINTF("imtuxedoulog: do_polling end for '%s'\n", pInst->pszUlogBaseName); |
| 632 |
if (pInst->iPersistStateInterval == -1) persistStrmState(pInst); |
| 633 |
} |
| 634 |
} while (bHadFileData == 1 && glbl.GetGlobalInputTermState() == 0); |
| 635 |
|
| 636 |
/* Note: the additional 10ns wait is vitally important. It guards rsyslog |
| 637 |
* against totally hogging the CPU if the users selects a polling interval |
| 638 |
* of 0 seconds. It doesn't hurt any other valid scenario. So do not remove. |
| 639 |
* rgerhards, 2008-02-14 |
| 640 |
*/ |
| 641 |
if (glbl.GetGlobalInputTermState() == 0) srSleep(iPollInterval, 10); |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
BEGINnewInpInst |
| 646 |
struct cnfparamvals *pvals; |
| 647 |
instanceConf_t *inst; |
| 648 |
int i; |
| 649 |
CODESTARTnewInpInst; |
| 650 |
DBGPRINTF("newInpInst (imtuxedoulog)\n"); |
| 651 |
|
| 652 |
pvals = nvlstGetParams(lst, &inppblk, NULL); |
| 653 |
if (pvals == NULL) { |
| 654 |
ABORT_FINALIZE(RS_RET_MISSING_CNFPARAMS); |
| 655 |
} |
| 656 |
|
| 657 |
if (Debug) { |
| 658 |
DBGPRINTF("input param blk in imtuxedoulog:\n"); |
| 659 |
cnfparamsPrint(&inppblk, pvals); |
| 660 |
} |
| 661 |
|
| 662 |
CHKiRet(createInstance(&inst)); |
| 663 |
|
| 664 |
for (i = 0; i < inppblk.nParams; ++i) { |
| 665 |
if (!pvals[i].bUsed) continue; |
| 666 |
if (!strcmp(inppblk.descr[i].name, "ulogbase")) { |
| 667 |
inst->pszUlogBaseName = (uchar *)es_str2cstr(pvals[i].val.d.estr, NULL); |
| 668 |
} else if (!strcmp(inppblk.descr[i].name, "tag")) { |
| 669 |
inst->pszTag = (uchar *)es_str2cstr(pvals[i].val.d.estr, NULL); |
| 670 |
inst->lenTag = es_strlen(pvals[i].val.d.estr); |
| 671 |
} else if (!strcmp(inppblk.descr[i].name, "ruleset")) { |
| 672 |
inst->pszBindRuleset = (uchar *)es_str2cstr(pvals[i].val.d.estr, NULL); |
| 673 |
} else if (!strcmp(inppblk.descr[i].name, "severity")) { |
| 674 |
inst->iSeverity = pvals[i].val.d.n; |
| 675 |
} else if (!strcmp(inppblk.descr[i].name, "facility")) { |
| 676 |
inst->iFacility = pvals[i].val.d.n; |
| 677 |
} else if (!strcmp(inppblk.descr[i].name, "maxlinesatonce")) { |
| 678 |
inst->maxLinesAtOnce = pvals[i].val.d.n; |
| 679 |
} else if (!strcmp(inppblk.descr[i].name, "persiststateinterval")) { |
| 680 |
inst->iPersistStateInterval = pvals[i].val.d.n; |
| 681 |
} else if (!strcmp(inppblk.descr[i].name, "maxsubmitatonce")) { |
| 682 |
inst->nMultiSub = pvals[i].val.d.n; |
| 683 |
} else { |
| 684 |
DBGPRINTF( |
| 685 |
"program error, non-handled " |
| 686 |
"param '%s'\n", |
| 687 |
inppblk.descr[i].name); |
| 688 |
} |
| 689 |
} |
| 690 |
if (inst->pszUlogBaseName == NULL) { |
| 691 |
lstnDel(inst); |
| 692 |
LogError(0, RS_RET_FILE_NOT_SPECIFIED, "ulogbase is not configured - no input will be gathered"); |
| 693 |
ABORT_FINALIZE(RS_RET_FILE_NOT_SPECIFIED); |
| 694 |
} |
| 695 |
|
| 696 |
if ((iRet = lstnAdd(inst)) != RS_RET_OK) { |
| 697 |
LogError(0, iRet, "add input %s to list failed", inst->pszUlogBaseName); |
| 698 |
lstnDel(inst); |
| 699 |
ABORT_FINALIZE(iRet); |
| 700 |
} |
| 701 |
|
| 702 |
finalize_it: |
| 703 |
CODE_STD_FINALIZERnewInpInst cnfparamvalsDestruct(pvals, &inppblk); |
| 704 |
ENDnewInpInst |
| 705 |
|
| 706 |
BEGINbeginCnfLoad |
| 707 |
CODESTARTbeginCnfLoad; |
| 708 |
loadModConf = pModConf; |
| 709 |
pModConf->pConf = pConf; |
| 710 |
ENDbeginCnfLoad |
| 711 |
|
| 712 |
BEGINendCnfLoad |
| 713 |
CODESTARTendCnfLoad; |
| 714 |
ENDendCnfLoad |
| 715 |
|
| 716 |
BEGINcheckCnf |
| 717 |
instanceConf_t *inst; |
| 718 |
CODESTARTcheckCnf; |
| 719 |
for (inst = confRoot; inst != NULL; inst = inst->next) { |
| 720 |
std_checkRuleset(pModConf, inst); |
| 721 |
} |
| 722 |
ENDcheckCnf |
| 723 |
|
| 724 |
BEGINactivateCnf |
| 725 |
CODESTARTactivateCnf; |
| 726 |
runModConf = pModConf; |
| 727 |
ENDactivateCnf |
| 728 |
|
| 729 |
BEGINfreeCnf |
| 730 |
CODESTARTfreeCnf; |
| 731 |
ENDfreeCnf |
| 732 |
|
| 733 |
BEGINrunInput |
| 734 |
CODESTARTrunInput; |
| 735 |
do_polling(); |
| 736 |
DBGPRINTF("terminating upon request of rsyslog core\n"); |
| 737 |
ENDrunInput |
| 738 |
|
| 739 |
BEGINwillRun |
| 740 |
CODESTARTwillRun; |
| 741 |
/* we need to create the inputName property (only once during our lifetime) */ |
| 742 |
CHKiRet(prop.Construct(&pInputName)); |
| 743 |
CHKiRet(prop.SetString(pInputName, UCHAR_CONSTANT("imtuxedoulog"), sizeof("imtuxedoulog") - 1)); |
| 744 |
CHKiRet(prop.ConstructFinalize(pInputName)); |
| 745 |
finalize_it: |
| 746 |
ENDwillRun |
| 747 |
|
| 748 |
BEGINafterRun |
| 749 |
CODESTARTafterRun; |
| 750 |
while (confRoot != NULL) { |
| 751 |
instanceConf_t *inst = confRoot; |
| 752 |
confRoot = confRoot->next; |
| 753 |
lstnDel(inst); |
| 754 |
} |
| 755 |
|
| 756 |
if (pInputName != NULL) prop.Destruct(&pInputName); |
| 757 |
ENDafterRun |
| 758 |
|
| 759 |
BEGINisCompatibleWithFeature |
| 760 |
CODESTARTisCompatibleWithFeature; |
| 761 |
if (eFeat == sFEATURENonCancelInputTermination) iRet = RS_RET_OK; |
| 762 |
ENDisCompatibleWithFeature |
| 763 |
|
| 764 |
BEGINmodExit |
| 765 |
CODESTARTmodExit; |
| 766 |
/* release objects we used */ |
| 767 |
objRelease(strm, CORE_COMPONENT); |
| 768 |
objRelease(glbl, CORE_COMPONENT); |
| 769 |
objRelease(prop, CORE_COMPONENT); |
| 770 |
objRelease(ruleset, CORE_COMPONENT); |
| 771 |
ENDmodExit |
| 772 |
|
| 773 |
BEGINqueryEtryPt |
| 774 |
CODESTARTqueryEtryPt; |
| 775 |
CODEqueryEtryPt_STD_IMOD_QUERIES; |
| 776 |
CODEqueryEtryPt_STD_CONF2_QUERIES; |
| 777 |
CODEqueryEtryPt_STD_CONF2_IMOD_QUERIES; |
| 778 |
CODEqueryEtryPt_IsCompatibleWithFeature_IF_OMOD_QUERIES; |
| 779 |
ENDqueryEtryPt |
| 780 |
|
| 781 |
static inline void std_checkRuleset_genErrMsg(__attribute__((unused)) modConfData_t *modConf, instanceConf_t *inst) { |
| 782 |
LogError(0, NO_ERRCODE, |
| 783 |
"imtuxedoulog: ruleset '%s' for ULOG base %s not found - " |
| 784 |
"using default ruleset instead", |
| 785 |
inst->pszBindRuleset, inst->pszUlogBaseName); |
| 786 |
} |
| 787 |
|
| 788 |
BEGINmodInit() |
| 789 |
CODESTARTmodInit; |
| 790 |
*ipIFVersProvided = CURR_MOD_IF_VERSION; /* we only support the current interface specification */ |
| 791 |
CODEmodInit_QueryRegCFSLineHdlr CHKiRet(objUse(glbl, CORE_COMPONENT)); |
| 792 |
CHKiRet(objUse(strm, CORE_COMPONENT)); |
| 793 |
CHKiRet(objUse(ruleset, CORE_COMPONENT)); |
| 794 |
CHKiRet(objUse(prop, CORE_COMPONENT)); |
| 795 |
ENDmodInit |