| 1 |
/* The serial stream class. |
| 2 |
* |
| 3 |
* A serial stream provides serial data access. In theory, serial streams |
| 4 |
* can be implemented via a number of methods (e.g. files or in-memory |
| 5 |
* streams). In practice, there currently only exist the file type (aka |
| 6 |
* "driver"). |
| 7 |
* |
| 8 |
* File begun on 2008-01-09 by RGerhards |
| 9 |
* Large modifications in 2009-06 to support using it with omfile, including zip writer. |
| 10 |
* Note that this file obtains the zlib wrapper object is needed, but it never frees it |
| 11 |
* again. While this sounds like a leak (and one may argue it actually is), there is no |
| 12 |
* harm associated with that. The reason is that strm is a core object, so it is terminated |
| 13 |
* only when rsyslogd exists. As we could only release on termination (or else bear more |
| 14 |
* overhead for keeping track of how many users we have), not releasing zlibw is OK, because |
| 15 |
* it will be released when rsyslogd terminates. We may want to revisit this decision if |
| 16 |
* it turns out to be problematic. Then, we need to quasi-refcount the number of accesses |
| 17 |
* to the object. |
| 18 |
* |
| 19 |
* Copyright 2008-2022 Rainer Gerhards and Adiscon GmbH. |
| 20 |
* |
| 21 |
* This file is part of the rsyslog runtime library. |
| 22 |
* |
| 23 |
* The rsyslog runtime library is free software: you can redistribute it and/or modify |
| 24 |
* it under the terms of the GNU Lesser General Public License as published by |
| 25 |
* the Free Software Foundation, either version 3 of the License, or |
| 26 |
* (at your option) any later version. |
| 27 |
* |
| 28 |
* The rsyslog runtime library is distributed in the hope that it will be useful, |
| 29 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 30 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 31 |
* GNU Lesser General Public License for more details. |
| 32 |
* |
| 33 |
* You should have received a copy of the GNU Lesser General Public License |
| 34 |
* along with the rsyslog runtime library. If not, see <http://www.gnu.org/licenses/>. |
| 35 |
* |
| 36 |
* A copy of the GPL can be found in the file "COPYING" in this distribution. |
| 37 |
* A copy of the LGPL can be found in the file "COPYING.LESSER" in this distribution. |
| 38 |
*/ |
| 39 |
#include "config.h" |
| 40 |
|
| 41 |
#include <stdlib.h> |
| 42 |
#include <string.h> |
| 43 |
#include <assert.h> |
| 44 |
#include <signal.h> |
| 45 |
#include <pthread.h> |
| 46 |
#include <fcntl.h> |
| 47 |
#include <unistd.h> |
| 48 |
#include <sys/types.h> |
| 49 |
#include <sys/stat.h> /* required for HP UX */ |
| 50 |
#include <errno.h> |
| 51 |
#include <pthread.h> |
| 52 |
#include <poll.h> |
| 53 |
#ifdef HAVE_SYS_PRCTL_H |
| 54 |
#include <sys/prctl.h> |
| 55 |
#endif |
| 56 |
|
| 57 |
#include "rsyslog.h" |
| 58 |
#include "stringbuf.h" |
| 59 |
#include "srUtils.h" |
| 60 |
#include "obj.h" |
| 61 |
#include "stream.h" |
| 62 |
#include "unicode-helper.h" |
| 63 |
#include "module-template.h" |
| 64 |
#include "errmsg.h" |
| 65 |
#include "zstdw.h" |
| 66 |
#include "cryprov.h" |
| 67 |
#include "datetime.h" |
| 68 |
#include "rsconf.h" |
| 69 |
|
| 70 |
/* some platforms do not have large file support :( */ |
| 71 |
#ifndef O_LARGEFILE |
| 72 |
#define O_LARGEFILE 0 |
| 73 |
#endif |
| 74 |
#ifndef HAVE_LSEEK64 |
| 75 |
#define lseek64(fd, offset, whence) lseek(fd, offset, whence) |
| 76 |
#endif |
| 77 |
|
| 78 |
/* static data */ |
| 79 |
DEFobjStaticHelpers; |
| 80 |
DEFobjCurrIf(zlibw) DEFobjCurrIf(zstdw) |
| 81 |
|
| 82 |
/* forward definitions */ |
| 83 |
static rsRetVal strmFlushInternal(strm_t *pThis, int bFlushZip); |
| 84 |
static rsRetVal strmWrite(strm_t *__restrict__ const pThis, const uchar *__restrict__ const pBuf, const size_t lenBuf); |
| 85 |
static rsRetVal strmOpenFile(strm_t *pThis); |
| 86 |
static rsRetVal strmCloseFile(strm_t *pThis); |
| 87 |
static void *asyncWriterThread(void *pPtr); |
| 88 |
static rsRetVal doZipWrite(strm_t *pThis, uchar *pBuf, size_t lenBuf, int bFlush); |
| 89 |
static rsRetVal doZipFinish(strm_t *pThis); |
| 90 |
static rsRetVal strmPhysWrite(strm_t *pThis, uchar *pBuf, size_t lenBuf); |
| 91 |
static rsRetVal strmSeekCurrOffs(strm_t *pThis); |
| 92 |
|
| 93 |
|
| 94 |
/* methods */ |
| 95 |
|
| 96 |
|
| 97 |
/* note: this may return NULL if not line segment is currently set */ |
| 98 |
// TODO: due to the cstrFinalize() this is not totally clean, albeit for our |
| 99 |
// current use case it does not hurt -- refactor! rgerhards, 2018-03-27 |
| 100 |
const uchar *ATTR_NONNULL() strmGetPrevLineSegment(strm_t *const pThis) { |
| 101 |
const uchar *ret = NULL; |
| 102 |
if (pThis->prevLineSegment != NULL) { |
| 103 |
cstrFinalize(pThis->prevLineSegment); |
| 104 |
ret = rsCStrGetSzStrNoNULL(pThis->prevLineSegment); |
| 105 |
} |
| 106 |
return ret; |
| 107 |
} |
| 108 |
/* note: this may return NULL if not line segment is currently set */ |
| 109 |
// TODO: due to the cstrFinalize() this is not totally clean, albeit for our |
| 110 |
// current use case it does not hurt -- refactor! rgerhards, 2018-03-27 |
| 111 |
const uchar *ATTR_NONNULL() strmGetPrevMsgSegment(strm_t *const pThis) { |
| 112 |
const uchar *ret = NULL; |
| 113 |
if (pThis->prevMsgSegment != NULL) { |
| 114 |
cstrFinalize(pThis->prevMsgSegment); |
| 115 |
ret = rsCStrGetSzStrNoNULL(pThis->prevMsgSegment); |
| 116 |
} |
| 117 |
return ret; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
int ATTR_NONNULL() strmGetPrevWasNL(const strm_t *const pThis) { |
| 122 |
return pThis->bPrevWasNL; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
/* output (current) file name for debug log purposes. Falls back to various |
| 127 |
* levels of impreciseness if more precise name is not known. |
| 128 |
*/ |
| 129 |
static const char *getFileDebugName(const strm_t *const pThis) { |
| 130 |
return (pThis->pszCurrFName == NULL) ? ((pThis->pszFName == NULL) ? "N/A" : (char *)pThis->pszFName) |
| 131 |
: (const char *)pThis->pszCurrFName; |
| 132 |
} |
| 133 |
|
| 134 |
/* Try to resolve a size limit situation. This is used to support custom-file size handlers |
| 135 |
* for omfile. It first runs the command, and then checks if we are still above the size |
| 136 |
* treshold. Note that this works only with single file names, NOT with circular names. |
| 137 |
* Note that pszCurrFName can NOT be taken from pThis, because the stream is closed when |
| 138 |
* we are called (and that destroys pszCurrFName, as there is NO CURRENT file name!). So |
| 139 |
* we need to receive the name as a parameter. |
| 140 |
* initially wirtten 2005-06-21, moved to this class & updates 2009-06-01, both rgerhards |
| 141 |
*/ |
| 142 |
static rsRetVal resolveFileSizeLimit(strm_t *pThis, uchar *pszCurrFName) { |
| 143 |
uchar *pParams; |
| 144 |
uchar *pCmd; |
| 145 |
uchar *p; |
| 146 |
off_t actualFileSize; |
| 147 |
rsRetVal localRet; |
| 148 |
DEFiRet; |
| 149 |
ISOBJ_TYPE_assert(pThis, strm); |
| 150 |
assert(pszCurrFName != NULL); |
| 151 |
|
| 152 |
if (pThis->pszSizeLimitCmd == NULL) { |
| 153 |
ABORT_FINALIZE(RS_RET_NON_SIZELIMITCMD); /* nothing we can do in this case... */ |
| 154 |
} |
| 155 |
|
| 156 |
/* we first check if we have command line parameters. We assume this, |
| 157 |
* when we have a space in the program name. If we find it, everything after |
| 158 |
* the space is treated as a single argument. |
| 159 |
*/ |
| 160 |
CHKmalloc(pCmd = ustrdup(pThis->pszSizeLimitCmd)); |
| 161 |
|
| 162 |
for (p = pCmd; *p && *p != ' '; ++p) { |
| 163 |
/* JUST SKIP */ |
| 164 |
} |
| 165 |
|
| 166 |
if (*p == ' ') { |
| 167 |
*p = '\0'; /* pretend string-end */ |
| 168 |
pParams = p + 1; |
| 169 |
} else |
| 170 |
pParams = NULL; |
| 171 |
|
| 172 |
/* the execProg() below is probably not great, but at least is is |
| 173 |
* fairly secure now. Once we change the way file size limits are |
| 174 |
* handled, we should also revisit how this command is run (and |
| 175 |
* with which parameters). rgerhards, 2007-07-20 |
| 176 |
*/ |
| 177 |
execProg(pCmd, 1, pParams, pThis->bSizeLimitCmdPassFileName ? pszCurrFName : NULL); |
| 178 |
|
| 179 |
free(pCmd); |
| 180 |
|
| 181 |
localRet = getFileSize(pszCurrFName, &actualFileSize); |
| 182 |
|
| 183 |
if (localRet == RS_RET_OK && actualFileSize >= pThis->iSizeLimit) { |
| 184 |
ABORT_FINALIZE(RS_RET_SIZELIMITCMD_DIDNT_RESOLVE); /* OK, it didn't work out... */ |
| 185 |
} else if (localRet != RS_RET_FILE_NOT_FOUND) { |
| 186 |
/* file not found is OK, the command may have moved away the file */ |
| 187 |
ABORT_FINALIZE(localRet); |
| 188 |
} |
| 189 |
|
| 190 |
finalize_it: |
| 191 |
if (iRet != RS_RET_OK) { |
| 192 |
if (iRet == RS_RET_SIZELIMITCMD_DIDNT_RESOLVE) { |
| 193 |
LogError(0, RS_RET_ERR, |
| 194 |
"file size limit cmd for file '%s' " |
| 195 |
"did no resolve situation\n", |
| 196 |
pszCurrFName); |
| 197 |
} else { |
| 198 |
LogError(0, RS_RET_ERR, |
| 199 |
"file size limit cmd for file '%s' " |
| 200 |
"failed with code %d.\n", |
| 201 |
pszCurrFName, iRet); |
| 202 |
} |
| 203 |
pThis->bDisabled = 1; |
| 204 |
} |
| 205 |
|
| 206 |
RETiRet; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
/* Check if the file has grown beyond the configured omfile iSizeLimit |
| 211 |
* and, if so, initiate processing. |
| 212 |
*/ |
| 213 |
static rsRetVal doSizeLimitProcessing(strm_t *pThis) { |
| 214 |
uchar *pszCurrFName = NULL; |
| 215 |
DEFiRet; |
| 216 |
|
| 217 |
ISOBJ_TYPE_assert(pThis, strm); |
| 218 |
assert(pThis->iSizeLimit != 0); |
| 219 |
assert(pThis->fd != -1); |
| 220 |
|
| 221 |
if (pThis->iCurrOffs >= pThis->iSizeLimit) { |
| 222 |
/* strmCloseFile() destroys the current file name, so we |
| 223 |
* need to preserve it. |
| 224 |
*/ |
| 225 |
CHKmalloc(pszCurrFName = ustrdup(pThis->pszCurrFName)); |
| 226 |
CHKiRet(strmCloseFile(pThis)); |
| 227 |
CHKiRet(resolveFileSizeLimit(pThis, pszCurrFName)); |
| 228 |
} |
| 229 |
|
| 230 |
finalize_it: |
| 231 |
free(pszCurrFName); |
| 232 |
RETiRet; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
/* now, we define type-specific handlers. The provide a generic functionality, |
| 237 |
* but for this specific type of strm. The mapping to these handlers happens during |
| 238 |
* strm construction. Later on, handlers are called by pointers present in the |
| 239 |
* strm instance object. |
| 240 |
*/ |
| 241 |
|
| 242 |
/** |
| 243 |
* @brief Open the underlying file descriptor and initialize stream state. |
| 244 |
* |
| 245 |
* This performs the actual open(2), detects TTYs, and initializes the crypto |
| 246 |
* provider if configured. In async mode, a mutex protects the open/init sequence |
| 247 |
* from concurrent close/reopen operations. |
| 248 |
*/ |
| 249 |
static rsRetVal doPhysOpen(strm_t *pThis) { |
| 250 |
int iFlags = 0; |
| 251 |
struct stat statOpen; |
| 252 |
const sbool bDoLock = (pThis->bAsyncWrite != 0); |
| 253 |
sbool bNeedOpenErrLog = 0; |
| 254 |
int openErrno = 0; |
| 255 |
rsRetVal openErrcode = RS_RET_OK; |
| 256 |
DEFiRet; |
| 257 |
ISOBJ_TYPE_assert(pThis, strm); |
| 258 |
|
| 259 |
/* compute which flags we need to provide to open */ |
| 260 |
switch (pThis->tOperationsMode) { |
| 261 |
case STREAMMODE_READ: |
| 262 |
iFlags = O_CLOEXEC | O_NOCTTY | O_RDONLY; |
| 263 |
break; |
| 264 |
case STREAMMODE_WRITE: /* legacy mode used inside queue engine */ |
| 265 |
iFlags = O_CLOEXEC | O_NOCTTY | O_WRONLY | O_CREAT; |
| 266 |
break; |
| 267 |
case STREAMMODE_WRITE_TRUNC: |
| 268 |
iFlags = O_CLOEXEC | O_NOCTTY | O_WRONLY | O_CREAT | O_TRUNC; |
| 269 |
break; |
| 270 |
case STREAMMODE_WRITE_APPEND: |
| 271 |
iFlags = O_CLOEXEC | O_NOCTTY | O_WRONLY | O_CREAT | O_APPEND; |
| 272 |
break; |
| 273 |
case STREAMMMODE_INVALID: |
| 274 |
default: |
| 275 |
assert(0); |
| 276 |
break; |
| 277 |
} |
| 278 |
if (pThis->sType == STREAMTYPE_NAMED_PIPE) { |
| 279 |
DBGPRINTF("Note: stream '%s' is a named pipe, open with O_NONBLOCK\n", pThis->pszCurrFName); |
| 280 |
iFlags |= O_NONBLOCK; |
| 281 |
} |
| 282 |
|
| 283 |
/* Keep lock held through open + init to avoid async close/open races. */ |
| 284 |
if (bDoLock) d_pthread_mutex_lock(&pThis->mut); |
| 285 |
pThis->fd = open((char *)pThis->pszCurrFName, iFlags | O_LARGEFILE, pThis->tOpenMode); |
| 286 |
|
| 287 |
const int errno_save = errno; /* dbgprintf can mangle it! */ |
| 288 |
DBGPRINTF("file '%s' opened as #%d with mode %d\n", pThis->pszCurrFName, pThis->fd, (int)pThis->tOpenMode); |
| 289 |
if (pThis->fd == -1) { |
| 290 |
const rsRetVal errcode = (errno_save == ENOENT) ? RS_RET_FILE_NOT_FOUND : RS_RET_FILE_OPEN_ERROR; |
| 291 |
if (pThis->fileNotFoundError) { |
| 292 |
if (pThis->noRepeatedErrorOutput == 0) { |
| 293 |
/* Defer LogError until after mutex unlock to avoid lock-order inversion |
| 294 |
* with internal message logging paths. |
| 295 |
*/ |
| 296 |
bNeedOpenErrLog = 1; |
| 297 |
openErrno = errno_save; |
| 298 |
openErrcode = errcode; |
| 299 |
pThis->noRepeatedErrorOutput = 1; |
| 300 |
} |
| 301 |
} else { |
| 302 |
DBGPRINTF("file '%s': open error", pThis->pszCurrFName); |
| 303 |
} |
| 304 |
ABORT_FINALIZE(errcode); |
| 305 |
} else { |
| 306 |
pThis->noRepeatedErrorOutput = 0; |
| 307 |
} |
| 308 |
|
| 309 |
if (pThis->tOperationsMode == STREAMMODE_READ) { |
| 310 |
if (fstat(pThis->fd, &statOpen) == -1) { |
| 311 |
DBGPRINTF("Error: cannot obtain inode# for file %s\n", pThis->pszCurrFName); |
| 312 |
ABORT_FINALIZE(RS_RET_IO_ERROR); |
| 313 |
} |
| 314 |
pThis->inode = statOpen.st_ino; |
| 315 |
} |
| 316 |
|
| 317 |
if (!ustrcmp(pThis->pszCurrFName, UCHAR_CONSTANT(_PATH_CONSOLE)) || isatty(pThis->fd)) { |
| 318 |
DBGPRINTF("file %d is a tty-type file\n", pThis->fd); |
| 319 |
pThis->bIsTTY = 1; |
| 320 |
} else { |
| 321 |
pThis->bIsTTY = 0; |
| 322 |
} |
| 323 |
|
| 324 |
if (pThis->cryprov != NULL) { |
| 325 |
CHKiRet(pThis->cryprov->OnFileOpen(pThis->cryprovData, pThis->pszCurrFName, &pThis->cryprovFileData, |
| 326 |
(pThis->tOperationsMode == STREAMMODE_READ) ? 'r' : 'w')); |
| 327 |
pThis->cryprov->SetDeleteOnClose(pThis->cryprovFileData, pThis->bDeleteOnClose); |
| 328 |
} |
| 329 |
|
| 330 |
finalize_it: |
| 331 |
if (bDoLock) d_pthread_mutex_unlock(&pThis->mut); |
| 332 |
if (bNeedOpenErrLog) { |
| 333 |
LogError(openErrno, openErrcode, "file '%s': open error", pThis->pszCurrFName); |
| 334 |
} |
| 335 |
RETiRet; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
static rsRetVal strmSetCurrFName(strm_t *pThis) { |
| 340 |
DEFiRet; |
| 341 |
|
| 342 |
if (pThis->sType == STREAMTYPE_FILE_CIRCULAR) { |
| 343 |
CHKiRet(genFileName(&pThis->pszCurrFName, pThis->pszDir, pThis->lenDir, pThis->pszFName, pThis->lenFName, |
| 344 |
pThis->iCurrFNum, pThis->iFileNumDigits)); |
| 345 |
} else { |
| 346 |
if (pThis->pszDir == NULL) { |
| 347 |
if ((pThis->pszCurrFName = ustrdup(pThis->pszFName)) == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 348 |
} else { |
| 349 |
CHKiRet(genFileName(&pThis->pszCurrFName, pThis->pszDir, pThis->lenDir, pThis->pszFName, pThis->lenFName, |
| 350 |
-1, 0)); |
| 351 |
} |
| 352 |
} |
| 353 |
finalize_it: |
| 354 |
RETiRet; |
| 355 |
} |
| 356 |
|
| 357 |
/* This function checks if the actual file has changed and, if so, resets the |
| 358 |
* offset. This is support for monitoring files. It should be called after |
| 359 |
* deserializing the strm object and before doing any other operation on it |
| 360 |
* (most importantly not an open or seek!). |
| 361 |
*/ |
| 362 |
static rsRetVal CheckFileChange(strm_t *pThis) { |
| 363 |
struct stat statName; |
| 364 |
DEFiRet; |
| 365 |
|
| 366 |
CHKiRet(strmSetCurrFName(pThis)); |
| 367 |
if (stat((char *)pThis->pszCurrFName, &statName) == -1) ABORT_FINALIZE(RS_RET_IO_ERROR); |
| 368 |
DBGPRINTF( |
| 369 |
"CheckFileChange: stream/after deserialize checking for file change " |
| 370 |
"on '%s', inode %u/%u, size/currOffs %llu/%llu\n", |
| 371 |
pThis->pszCurrFName, (unsigned)pThis->inode, (unsigned)statName.st_ino, (long long unsigned)statName.st_size, |
| 372 |
(long long unsigned)pThis->iCurrOffs); |
| 373 |
if (pThis->inode != statName.st_ino || statName.st_size < pThis->iCurrOffs) { |
| 374 |
DBGPRINTF("stream: file %s has changed\n", pThis->pszCurrFName); |
| 375 |
pThis->iCurrOffs = 0; |
| 376 |
} |
| 377 |
finalize_it: |
| 378 |
RETiRet; |
| 379 |
} |
| 380 |
|
| 381 |
|
| 382 |
/* open a strm file |
| 383 |
* It is OK to call this function when the stream is already open. In that |
| 384 |
* case, it returns immediately with RS_RET_OK |
| 385 |
*/ |
| 386 |
static rsRetVal strmOpenFile(strm_t *pThis) { |
| 387 |
DEFiRet; |
| 388 |
off_t offset; |
| 389 |
|
| 390 |
assert(pThis != NULL); |
| 391 |
|
| 392 |
if (pThis->fd != -1) ABORT_FINALIZE(RS_RET_OK); |
| 393 |
|
| 394 |
free(pThis->pszCurrFName); |
| 395 |
pThis->pszCurrFName = NULL; /* used to prevent mem leak in case of error */ |
| 396 |
|
| 397 |
if (pThis->pszFName == NULL) ABORT_FINALIZE(RS_RET_FILE_PREFIX_MISSING); |
| 398 |
|
| 399 |
CHKiRet(strmSetCurrFName(pThis)); |
| 400 |
|
| 401 |
CHKiRet(doPhysOpen(pThis)); |
| 402 |
|
| 403 |
pThis->iCurrOffs = 0; |
| 404 |
pThis->iBufPtrMax = 0; |
| 405 |
CHKiRet(getFileSize(pThis->pszCurrFName, &offset)); |
| 406 |
if (pThis->tOperationsMode == STREAMMODE_WRITE_APPEND) { |
| 407 |
pThis->iCurrOffs = offset; |
| 408 |
} else if (pThis->tOperationsMode == STREAMMODE_WRITE_TRUNC) { |
| 409 |
if (offset != 0) { |
| 410 |
LogError(0, 0, |
| 411 |
"file '%s' opened for truncate write, but " |
| 412 |
"already contains %zd bytes\n", |
| 413 |
pThis->pszCurrFName, (ssize_t)offset); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
DBGOPRINT((obj_t *)pThis, "opened file '%s' for %s as %d\n", pThis->pszCurrFName, |
| 418 |
(pThis->tOperationsMode == STREAMMODE_READ) ? "READ" : "WRITE", pThis->fd); |
| 419 |
|
| 420 |
finalize_it: |
| 421 |
if (iRet == RS_RET_OK) { |
| 422 |
assert(pThis->fd != -1); |
| 423 |
} else { |
| 424 |
if (pThis->pszCurrFName != NULL) { |
| 425 |
free(pThis->pszCurrFName); |
| 426 |
pThis->pszCurrFName = NULL; /* just to prevent mis-adressing down the road... */ |
| 427 |
} |
| 428 |
if (pThis->fd != -1) { |
| 429 |
close(pThis->fd); |
| 430 |
pThis->fd = -1; |
| 431 |
} |
| 432 |
} |
| 433 |
RETiRet; |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
/* wait for the output writer thread to be done. This must be called before actions |
| 438 |
* that require data to be persisted. May be called in non-async mode and is a null |
| 439 |
* operation than. Must be called with the mutex locked. |
| 440 |
*/ |
| 441 |
static void strmWaitAsyncWriterDone(strm_t *pThis) { |
| 442 |
if (pThis->bAsyncWrite) { |
| 443 |
/* awake writer thread and make it write out everything */ |
| 444 |
while (pThis->iCnt > 0) { |
| 445 |
pthread_cond_signal(&pThis->notEmpty); |
| 446 |
d_pthread_cond_wait(&pThis->isEmpty, &pThis->mut); |
| 447 |
} |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
/* stop the writer thread (we MUST be runnnig asynchronously when this method |
| 452 |
* is called!). Note that the mutex must be locked! -- rgerhards, 2009-07-06 |
| 453 |
*/ |
| 454 |
static void stopWriter(strm_t *const pThis) { |
| 455 |
pThis->bStopWriter = 1; |
| 456 |
pthread_cond_signal(&pThis->notEmpty); |
| 457 |
d_pthread_mutex_unlock(&pThis->mut); |
| 458 |
pthread_join(pThis->writerThreadID, NULL); |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
/* close a strm file |
| 463 |
* Note that the bDeleteOnClose flag is honored. If it is set, the file will be |
| 464 |
* deleted after close. This is in support for the qRead thread. |
| 465 |
* Note: it is valid to call this function when the physical file is closed. If so, |
| 466 |
* strmCloseFile() will still check if there is any unwritten data inside buffers |
| 467 |
* (this may be the case) and, if so, will open the file, write the data, and then |
| 468 |
* close it again (this is done via strmFlushInternal and friends). |
| 469 |
*/ |
| 470 |
static rsRetVal strmCloseFile(strm_t *pThis) { |
| 471 |
off64_t currOffs; |
| 472 |
DEFiRet; |
| 473 |
|
| 474 |
assert(pThis != NULL); |
| 475 |
DBGOPRINT((obj_t *)pThis, "file %d(%s) closing, bDeleteOnClose %d\n", pThis->fd, getFileDebugName(pThis), |
| 476 |
pThis->bDeleteOnClose); |
| 477 |
|
| 478 |
if (pThis->tOperationsMode != STREAMMODE_READ) { |
| 479 |
if (pThis->bAsyncWrite) { |
| 480 |
strmWaitAsyncWriterDone(pThis); |
| 481 |
} |
| 482 |
strmFlushInternal(pThis, 0); |
| 483 |
if (pThis->iZipLevel) { |
| 484 |
doZipFinish(pThis); |
| 485 |
} |
| 486 |
if (pThis->bAsyncWrite) { |
| 487 |
stopWriter(pThis); |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
/* if we have a signature provider, we must make sure that the crypto |
| 492 |
* state files are opened and proper close processing happens. */ |
| 493 |
if (pThis->cryprov != NULL && pThis->fd == -1) { |
| 494 |
const rsRetVal localRet = strmOpenFile(pThis); |
| 495 |
if (localRet != RS_RET_OK) { |
| 496 |
LogError(0, localRet, |
| 497 |
"could not open file %s, this " |
| 498 |
"may result in problems with encryption - " |
| 499 |
"unfortunately, we cannot do anything against " |
| 500 |
"this.", |
| 501 |
pThis->pszCurrFName); |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
/* the file may already be closed (or never have opened), so guard |
| 506 |
* against this. -- rgerhards, 2010-03-19 |
| 507 |
*/ |
| 508 |
if (pThis->fd != -1) { |
| 509 |
DBGOPRINT((obj_t *)pThis, "file %d(%s) closing\n", pThis->fd, getFileDebugName(pThis)); |
| 510 |
currOffs = lseek64(pThis->fd, 0, SEEK_CUR); |
| 511 |
close(pThis->fd); |
| 512 |
pThis->fd = -1; |
| 513 |
pThis->inode = 0; |
| 514 |
if (pThis->cryprov != NULL) { |
| 515 |
pThis->cryprov->OnFileClose(pThis->cryprovFileData, currOffs); |
| 516 |
pThis->cryprovFileData = NULL; |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
if (pThis->fdDir != -1) { |
| 521 |
/* close associated directory handle, if it is open */ |
| 522 |
close(pThis->fdDir); |
| 523 |
pThis->fdDir = -1; |
| 524 |
} |
| 525 |
|
| 526 |
if (pThis->bDeleteOnClose) { |
| 527 |
if (pThis->pszCurrFName == NULL) { |
| 528 |
CHKiRet(genFileName(&pThis->pszCurrFName, pThis->pszDir, pThis->lenDir, pThis->pszFName, pThis->lenFName, |
| 529 |
pThis->iCurrFNum, pThis->iFileNumDigits)); |
| 530 |
} |
| 531 |
DBGPRINTF("strmCloseFile: deleting '%s'\n", pThis->pszCurrFName); |
| 532 |
if (unlink((char *)pThis->pszCurrFName) == -1) { |
| 533 |
char errStr[1024]; |
| 534 |
int err = errno; |
| 535 |
rs_strerror_r(err, errStr, sizeof(errStr)); |
| 536 |
DBGPRINTF("error %d unlinking '%s' - ignored: %s\n", errno, pThis->pszCurrFName, errStr); |
| 537 |
} |
| 538 |
} |
| 539 |
|
| 540 |
pThis->iCurrOffs = 0; /* we are back at begin of file */ |
| 541 |
|
| 542 |
finalize_it: |
| 543 |
free(pThis->pszCurrFName); |
| 544 |
pThis->pszCurrFName = NULL; |
| 545 |
RETiRet; |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
/* switch to next strm file |
| 550 |
* This method must only be called if we are in a multi-file mode! |
| 551 |
*/ |
| 552 |
static rsRetVal strmNextFile(strm_t *pThis) { |
| 553 |
DEFiRet; |
| 554 |
|
| 555 |
assert(pThis != NULL); |
| 556 |
assert(pThis->sType == STREAMTYPE_FILE_CIRCULAR); |
| 557 |
assert(pThis->fd != -1); |
| 558 |
|
| 559 |
if (pThis->iMaxFiles <= 0) { |
| 560 |
LogError(0, RS_RET_CONF_PARAM_INVLD, "stream: circular file mode requires iMaxFiles > 0, but is %d", |
| 561 |
pThis->iMaxFiles); |
| 562 |
ABORT_FINALIZE(RS_RET_CONF_PARAM_INVLD); |
| 563 |
} |
| 564 |
|
| 565 |
CHKiRet(strmCloseFile(pThis)); |
| 566 |
|
| 567 |
/* we do modulo operation to ensure we obey the iMaxFile property. This will always |
| 568 |
* result in a file number lower than iMaxFile, so it if wraps, the name is back to |
| 569 |
* 0, which results in the first file being overwritten. Not desired for queues, so |
| 570 |
* make sure their iMaxFiles is large enough. But it is well-desired for other |
| 571 |
* use cases, e.g. a circular output log file. -- rgerhards, 2008-01-10 |
| 572 |
*/ |
| 573 |
pThis->iCurrFNum = (pThis->iCurrFNum + 1) % pThis->iMaxFiles; |
| 574 |
|
| 575 |
finalize_it: |
| 576 |
RETiRet; |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
/* handle the EOF case of a stream |
| 581 |
* The EOF case is somewhat complicated, as the proper action depends on the |
| 582 |
* mode the stream is in. If there are multiple files (circular logs, most |
| 583 |
* important use case is queue files!), we need to close the current file and |
| 584 |
* try to open the next one. |
| 585 |
* rgerhards, 2008-02-13 |
| 586 |
*/ |
| 587 |
static rsRetVal ATTR_NONNULL() strmHandleEOF(strm_t *const pThis) { |
| 588 |
DEFiRet; |
| 589 |
|
| 590 |
ISOBJ_TYPE_assert(pThis, strm); |
| 591 |
switch (pThis->sType) { |
| 592 |
case STREAMTYPE_FILE_SINGLE: |
| 593 |
case STREAMTYPE_NAMED_PIPE: |
| 594 |
ABORT_FINALIZE(RS_RET_EOF); |
| 595 |
break; |
| 596 |
case STREAMTYPE_FILE_CIRCULAR: |
| 597 |
/* we have multiple files and need to switch to the next one */ |
| 598 |
/* TODO: think about emulating EOF in this case (not yet needed) */ |
| 599 |
DBGOPRINT((obj_t *)pThis, "file %d EOF\n", pThis->fd); |
| 600 |
CHKiRet(strmNextFile(pThis)); |
| 601 |
break; |
| 602 |
case STREAMTYPE_FILE_MONITOR: |
| 603 |
DBGOPRINT((obj_t *)pThis, "file '%s' (%d) EOF, rotationCheck %d\n", pThis->pszCurrFName, pThis->fd, |
| 604 |
pThis->rotationCheck); |
| 605 |
DBGPRINTF("RGER: EOF!\n"); |
| 606 |
ABORT_FINALIZE(RS_RET_EOF); |
| 607 |
break; |
| 608 |
default: |
| 609 |
// No action needed for other cases |
| 610 |
break; |
| 611 |
} |
| 612 |
|
| 613 |
finalize_it: |
| 614 |
RETiRet; |
| 615 |
} |
| 616 |
|
| 617 |
|
| 618 |
/* helper to checkTruncation */ |
| 619 |
static rsRetVal ATTR_NONNULL() |
| 620 |
rereadTruncated(strm_t *const pThis, const int err_no, const char *const reason, const long long data) { |
| 621 |
DEFiRet; |
| 622 |
|
| 623 |
LogMsg(err_no, RS_RET_FILE_TRUNCATED, LOG_WARNING, |
| 624 |
"file '%s': truncation detected, " |
| 625 |
"(%s) - re-start reading from beginning (data %lld)", |
| 626 |
pThis->pszCurrFName, reason, data); |
| 627 |
DBGPRINTF("checkTruncation, file %s last buffer CHANGED\n", pThis->pszCurrFName); |
| 628 |
CHKiRet(strmCloseFile(pThis)); |
| 629 |
CHKiRet(strmOpenFile(pThis)); |
| 630 |
iRet = RS_RET_FILE_TRUNCATED; |
| 631 |
|
| 632 |
finalize_it: |
| 633 |
RETiRet; |
| 634 |
} |
| 635 |
/* helper to read: |
| 636 |
* Check if file has been truncated since last read and, if so, re-set reading |
| 637 |
* to begin of file. To detect truncation, we try to re-read the last block. |
| 638 |
* If that does not succeed or different data than from the original read is |
| 639 |
* returned, truncation is assumed. |
| 640 |
* NOTE: this function must be called only if truncation is enabled AND |
| 641 |
* when the previous read buffer still is valid (aka "before the next read"). |
| 642 |
* It is ok to call with a 0-size buffer, which we than assume as begin of |
| 643 |
* reading. In that case, no truncation will be detected. |
| 644 |
* rgerhards, 2018-09-20 |
| 645 |
*/ |
| 646 |
static rsRetVal ATTR_NONNULL() checkTruncation(strm_t *const pThis) { |
| 647 |
DEFiRet; |
| 648 |
off64_t ret; |
| 649 |
assert(pThis->bReopenOnTruncate); |
| 650 |
assert(pThis->fd != -1); |
| 651 |
|
| 652 |
DBGPRINTF("checkTruncation, file %s, iBufPtrMax %zd\n", pThis->pszCurrFName, pThis->iBufPtrMax); |
| 653 |
if (pThis->iBufPtrMax == 0) { |
| 654 |
FINALIZE; |
| 655 |
} |
| 656 |
|
| 657 |
const off64_t backseek = -1 * (off64_t)pThis->iBufPtrMax; |
| 658 |
ret = lseek64(pThis->fd, backseek, SEEK_CUR); |
| 659 |
if (ret < 0) { |
| 660 |
iRet = rereadTruncated(pThis, errno, "cannot seek backward to begin of last block", backseek); |
| 661 |
FINALIZE; |
| 662 |
} |
| 663 |
|
| 664 |
const ssize_t lenRead = read(pThis->fd, pThis->pIOBuf_truncation, pThis->iBufPtrMax); |
| 665 |
if (lenRead != (ssize_t)pThis->iBufPtrMax) { |
| 666 |
iRet = rereadTruncated(pThis, errno, "last block could not be re-read", lenRead); |
| 667 |
FINALIZE; |
| 668 |
} |
| 669 |
|
| 670 |
if (!memcmp(pThis->pIOBuf_truncation, pThis->pIOBuf, pThis->iBufPtrMax)) { |
| 671 |
DBGPRINTF("checkTruncation, file %s last buffer unchanged\n", pThis->pszCurrFName); |
| 672 |
} else { |
| 673 |
iRet = rereadTruncated(pThis, errno, "last block data different", 0); |
| 674 |
} |
| 675 |
|
| 676 |
finalize_it: |
| 677 |
RETiRet; |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
/* read the next buffer from disk |
| 682 |
* rgerhards, 2008-02-13 |
| 683 |
*/ |
| 684 |
static rsRetVal strmReadBuf(strm_t *pThis, int *padBytes) { |
| 685 |
DEFiRet; |
| 686 |
int bRun; |
| 687 |
long iLenRead; |
| 688 |
size_t actualDataLen; |
| 689 |
size_t toRead; |
| 690 |
ssize_t bytesLeft; |
| 691 |
|
| 692 |
ISOBJ_TYPE_assert(pThis, strm); |
| 693 |
/* We need to try read at least twice because we may run into EOF and need to switch files. */ |
| 694 |
bRun = 1; |
| 695 |
while (bRun) { |
| 696 |
/* first check if we need to (re)open the file. We may have switched to a new one in |
| 697 |
* circular mode or it may have been rewritten (rotated) if we monitor a file |
| 698 |
* rgerhards, 2008-02-13 |
| 699 |
*/ |
| 700 |
CHKiRet(strmOpenFile(pThis)); |
| 701 |
if (pThis->cryprov == NULL) { |
| 702 |
toRead = pThis->sIOBufSize; |
| 703 |
} else { |
| 704 |
CHKiRet(pThis->cryprov->GetBytesLeftInBlock(pThis->cryprovFileData, &bytesLeft)); |
| 705 |
if (bytesLeft == -1 || bytesLeft > (ssize_t)pThis->sIOBufSize) { |
| 706 |
toRead = pThis->sIOBufSize; |
| 707 |
} else { |
| 708 |
toRead = (size_t)bytesLeft; |
| 709 |
} |
| 710 |
} |
| 711 |
if (pThis->bReopenOnTruncate) { |
| 712 |
rsRetVal localRet = checkTruncation(pThis); |
| 713 |
if (localRet == RS_RET_FILE_TRUNCATED) { |
| 714 |
continue; |
| 715 |
} |
| 716 |
CHKiRet(localRet); |
| 717 |
} |
| 718 |
iLenRead = read(pThis->fd, pThis->pIOBuf, toRead); |
| 719 |
DBGOPRINT((obj_t *)pThis, "file %d read %ld bytes\n", pThis->fd, iLenRead); |
| 720 |
DBGOPRINT((obj_t *)pThis, "file %d read %*s\n", pThis->fd, (unsigned)iLenRead, (char *)pThis->pIOBuf); |
| 721 |
/* end crypto */ |
| 722 |
if (iLenRead == 0) { |
| 723 |
CHKiRet(strmHandleEOF(pThis)); |
| 724 |
} else if (iLenRead < 0) |
| 725 |
ABORT_FINALIZE(RS_RET_IO_ERROR); |
| 726 |
else { /* good read */ |
| 727 |
/* here we place our crypto interface */ |
| 728 |
if (pThis->cryprov != NULL) { |
| 729 |
actualDataLen = iLenRead; |
| 730 |
pThis->cryprov->Decrypt(pThis->cryprovFileData, pThis->pIOBuf, &actualDataLen); |
| 731 |
*padBytes = iLenRead - actualDataLen; |
| 732 |
iLenRead = actualDataLen; |
| 733 |
DBGOPRINT((obj_t *)pThis, |
| 734 |
"encrypted file %d pad bytes %d, actual " |
| 735 |
"data %ld\n", |
| 736 |
pThis->fd, *padBytes, iLenRead); |
| 737 |
} else { |
| 738 |
*padBytes = 0; |
| 739 |
} |
| 740 |
pThis->iBufPtrMax = iLenRead; |
| 741 |
bRun = 0; /* exit loop */ |
| 742 |
} |
| 743 |
} |
| 744 |
/* if we reach this point, we had a good read */ |
| 745 |
pThis->iBufPtr = 0; |
| 746 |
|
| 747 |
finalize_it: |
| 748 |
RETiRet; |
| 749 |
} |
| 750 |
|
| 751 |
|
| 752 |
/* debug output of current buffer */ |
| 753 |
void strmDebugOutBuf(const strm_t *const pThis) { |
| 754 |
int strtIdx = pThis->iBufPtr - 50; |
| 755 |
if (strtIdx < 0) strtIdx = 0; |
| 756 |
DBGOPRINT((obj_t *)pThis, "strmRead ungetc %d, index %zd, max %zd, buf '%.*s', CURR: '%.*s'\n", pThis->iUngetC, |
| 757 |
pThis->iBufPtr, pThis->iBufPtrMax, (int)pThis->iBufPtrMax - strtIdx, pThis->pIOBuf + strtIdx, |
| 758 |
(int)(pThis->iBufPtrMax - pThis->iBufPtr), pThis->pIOBuf + pThis->iBufPtr); |
| 759 |
} |
| 760 |
|
| 761 |
/* logically "read" a character from a file. What actually happens is that |
| 762 |
* data is taken from the buffer. Only if the buffer is full, data is read |
| 763 |
* directly from file. In that case, a read is performed blockwise. |
| 764 |
* rgerhards, 2008-01-07 |
| 765 |
* NOTE: needs to be enhanced to support sticking with a strm entry (if not |
| 766 |
* deleted). |
| 767 |
*/ |
| 768 |
static rsRetVal strmReadChar(strm_t *pThis, uchar *pC) { |
| 769 |
int padBytes = 0; /* in crypto mode, we may have some padding (non-data) bytes */ |
| 770 |
DEFiRet; |
| 771 |
|
| 772 |
assert(pThis != NULL); |
| 773 |
assert(pC != NULL); |
| 774 |
|
| 775 |
/* DEV debug only: DBGOPRINT((obj_t*) pThis, "strmRead index %zd, max %zd\n", pThis->iBufPtr, |
| 776 |
pThis->iBufPtrMax); */ |
| 777 |
if (pThis->iUngetC != -1) { /* do we have an "unread" char that we need to provide? */ |
| 778 |
*pC = pThis->iUngetC; |
| 779 |
++pThis->iCurrOffs; /* one more octet read */ |
| 780 |
pThis->iUngetC = -1; |
| 781 |
ABORT_FINALIZE(RS_RET_OK); |
| 782 |
} |
| 783 |
|
| 784 |
/* do we need to obtain a new buffer? */ |
| 785 |
if (pThis->iBufPtr >= pThis->iBufPtrMax) { |
| 786 |
CHKiRet(strmReadBuf(pThis, &padBytes)); |
| 787 |
} |
| 788 |
pThis->iCurrOffs += padBytes; |
| 789 |
|
| 790 |
/* if we reach this point, we have data available in the buffer */ |
| 791 |
|
| 792 |
*pC = pThis->pIOBuf[pThis->iBufPtr++]; |
| 793 |
++pThis->iCurrOffs; /* one more octet read */ |
| 794 |
|
| 795 |
finalize_it: |
| 796 |
RETiRet; |
| 797 |
} |
| 798 |
|
| 799 |
|
| 800 |
/* unget a single character just like ungetc(). As with that call, there is only a single |
| 801 |
* character buffering capability. |
| 802 |
* rgerhards, 2008-01-07 |
| 803 |
*/ |
| 804 |
static rsRetVal strmUnreadChar(strm_t *pThis, uchar c) { |
| 805 |
assert(pThis != NULL); |
| 806 |
assert(pThis->iUngetC == -1); |
| 807 |
pThis->iUngetC = c; |
| 808 |
--pThis->iCurrOffs; /* one less octet read - NOTE: this can cause problems if we got a file change |
| 809 |
and immediately do an unread and the file is on a buffer boundary and the stream is then persisted. |
| 810 |
With the queue, this can not happen as an Unread is only done on record begin, which is never split |
| 811 |
accross files. For other cases we accept the very remote risk. -- rgerhards, 2008-01-12 */ |
| 812 |
|
| 813 |
return RS_RET_OK; |
| 814 |
} |
| 815 |
|
| 816 |
/* read a 'paragraph' from a strm file. |
| 817 |
* A paragraph may be terminated by a LF, by a LFLF, or by LF<not whitespace> depending on the option set. |
| 818 |
* The termination LF characters are read, but are |
| 819 |
* not returned in the buffer (it is discared). The caller is responsible for |
| 820 |
* destruction of the returned CStr object! -- dlang 2010-12-13 |
| 821 |
* |
| 822 |
* Parameter mode controls legacy multi-line processing: |
| 823 |
* mode = 0 single line mode (equivalent to ReadLine) |
| 824 |
* mode = 1 LFLF mode (paragraph, blank line between entries) |
| 825 |
* mode = 2 LF <not whitespace> mode, a log line starts at the beginning of |
| 826 |
* a line, but following lines that are indented are part of the same log entry |
| 827 |
*/ |
| 828 |
static rsRetVal ATTR_NONNULL(1, 2) strmReadLine(strm_t *const pThis, |
| 829 |
cstr_t **ppCStr, |
| 830 |
uint8_t mode, |
| 831 |
sbool bEscapeLF, |
| 832 |
const uchar *const escapeLFString, |
| 833 |
uint32_t trimLineOverBytes, |
| 834 |
int64 *const strtOffs) { |
| 835 |
uchar c; |
| 836 |
uchar finished; |
| 837 |
const int escapeLFString_len = (escapeLFString == NULL) ? 4 : strlen((char *)escapeLFString); |
| 838 |
DEFiRet; |
| 839 |
|
| 840 |
assert(pThis != NULL); |
| 841 |
assert(ppCStr != NULL); |
| 842 |
|
| 843 |
CHKiRet(cstrConstruct(ppCStr)); |
| 844 |
CHKiRet(strmReadChar(pThis, &c)); |
| 845 |
|
| 846 |
/* append previous message to current message if necessary */ |
| 847 |
if (pThis->prevLineSegment != NULL) { |
| 848 |
cstrFinalize(pThis->prevLineSegment); |
| 849 |
dbgprintf("readLine: have previous line segment: '%s'\n", rsCStrGetSzStrNoNULL(pThis->prevLineSegment)); |
| 850 |
CHKiRet(cstrAppendCStr(*ppCStr, pThis->prevLineSegment)); |
| 851 |
cstrDestruct(&pThis->prevLineSegment); |
| 852 |
} |
| 853 |
if (mode == 0) { |
| 854 |
while (c != '\n') { |
| 855 |
CHKiRet(cstrAppendChar(*ppCStr, c)); |
| 856 |
CHKiRet(strmReadChar(pThis, &c)); |
| 857 |
} |
| 858 |
if (trimLineOverBytes > 0 && (uint32_t)cstrLen(*ppCStr) > trimLineOverBytes) { |
| 859 |
/* Truncate long line at trimLineOverBytes position */ |
| 860 |
dbgprintf("Truncate long line at %u, mode %d\n", trimLineOverBytes, mode); |
| 861 |
rsCStrTruncate(*ppCStr, cstrLen(*ppCStr) - trimLineOverBytes); |
| 862 |
cstrAppendChar(*ppCStr, '\n'); |
| 863 |
} |
| 864 |
cstrFinalize(*ppCStr); |
| 865 |
} else if (mode == 1) { |
| 866 |
finished = 0; |
| 867 |
while (finished == 0) { |
| 868 |
if (c != '\n') { |
| 869 |
CHKiRet(cstrAppendChar(*ppCStr, c)); |
| 870 |
CHKiRet(strmReadChar(pThis, &c)); |
| 871 |
pThis->bPrevWasNL = 0; |
| 872 |
} else { |
| 873 |
if ((((*ppCStr)->iStrLen) > 0)) { |
| 874 |
if (pThis->bPrevWasNL && escapeLFString_len > 0) { |
| 875 |
rsCStrTruncate(*ppCStr, (bEscapeLF) ? escapeLFString_len : 1); |
| 876 |
/* remove the prior newline */ |
| 877 |
finished = 1; |
| 878 |
} else { |
| 879 |
if (bEscapeLF) { |
| 880 |
if (escapeLFString == NULL) { |
| 881 |
CHKiRet(rsCStrAppendStrWithLen(*ppCStr, (uchar *)"#012", sizeof("#012") - 1)); |
| 882 |
} else { |
| 883 |
CHKiRet(rsCStrAppendStrWithLen(*ppCStr, escapeLFString, escapeLFString_len)); |
| 884 |
} |
| 885 |
} else { |
| 886 |
CHKiRet(cstrAppendChar(*ppCStr, c)); |
| 887 |
} |
| 888 |
CHKiRet(strmReadChar(pThis, &c)); |
| 889 |
pThis->bPrevWasNL = 1; |
| 890 |
} |
| 891 |
} else { |
| 892 |
finished = 1; /* this is a blank line, a \n with nothing since |
| 893 |
the last complete record */ |
| 894 |
} |
| 895 |
} |
| 896 |
} |
| 897 |
cstrFinalize(*ppCStr); |
| 898 |
pThis->bPrevWasNL = 0; |
| 899 |
} else if (mode == 2) { |
| 900 |
/* indented follow-up lines */ |
| 901 |
finished = 0; |
| 902 |
while (finished == 0) { |
| 903 |
if ((*ppCStr)->iStrLen == 0) { |
| 904 |
if (c != '\n') { |
| 905 |
/* nothing in the buffer, and it's not a newline, add it to the buffer */ |
| 906 |
CHKiRet(cstrAppendChar(*ppCStr, c)); |
| 907 |
CHKiRet(strmReadChar(pThis, &c)); |
| 908 |
} else { |
| 909 |
finished = 1; /* this is a blank line, a \n with nothing since the |
| 910 |
last complete record */ |
| 911 |
} |
| 912 |
} else { |
| 913 |
if (pThis->bPrevWasNL) { |
| 914 |
if ((c == ' ') || (c == '\t')) { |
| 915 |
CHKiRet(cstrAppendChar(*ppCStr, c)); |
| 916 |
CHKiRet(strmReadChar(pThis, &c)); |
| 917 |
pThis->bPrevWasNL = 0; |
| 918 |
} else { |
| 919 |
/* clean things up by putting the character we just read back into |
| 920 |
* the input buffer and removing the LF character that is |
| 921 |
* currently at the |
| 922 |
* end of the output string */ |
| 923 |
CHKiRet(strmUnreadChar(pThis, c)); |
| 924 |
if (bEscapeLF && escapeLFString_len > 0) { |
| 925 |
rsCStrTruncate(*ppCStr, (bEscapeLF) ? escapeLFString_len : 1); |
| 926 |
} |
| 927 |
finished = 1; |
| 928 |
} |
| 929 |
} else { /* not the first character after a newline, add it to the buffer */ |
| 930 |
if (c == '\n') { |
| 931 |
pThis->bPrevWasNL = 1; |
| 932 |
if (bEscapeLF && escapeLFString_len > 0) { |
| 933 |
if (escapeLFString == NULL) { |
| 934 |
CHKiRet(rsCStrAppendStrWithLen(*ppCStr, (uchar *)"#012", sizeof("#012") - 1)); |
| 935 |
} else { |
| 936 |
CHKiRet(rsCStrAppendStrWithLen(*ppCStr, escapeLFString, escapeLFString_len)); |
| 937 |
} |
| 938 |
} else { |
| 939 |
CHKiRet(cstrAppendChar(*ppCStr, c)); |
| 940 |
} |
| 941 |
} else { |
| 942 |
CHKiRet(cstrAppendChar(*ppCStr, c)); |
| 943 |
} |
| 944 |
CHKiRet(strmReadChar(pThis, &c)); |
| 945 |
} |
| 946 |
} |
| 947 |
} |
| 948 |
if (trimLineOverBytes > 0 && (uint32_t)cstrLen(*ppCStr) > trimLineOverBytes) { |
| 949 |
/* Truncate long line at trimLineOverBytes position */ |
| 950 |
dbgprintf("Truncate long line at %u, mode %d\n", trimLineOverBytes, mode); |
| 951 |
rsCStrTruncate(*ppCStr, cstrLen(*ppCStr) - trimLineOverBytes); |
| 952 |
cstrAppendChar(*ppCStr, '\n'); |
| 953 |
} |
| 954 |
cstrFinalize(*ppCStr); |
| 955 |
pThis->bPrevWasNL = 0; |
| 956 |
} |
| 957 |
|
| 958 |
finalize_it: |
| 959 |
if (iRet == RS_RET_OK) { |
| 960 |
if (strtOffs != NULL) { |
| 961 |
*strtOffs = pThis->strtOffs; |
| 962 |
} |
| 963 |
pThis->strtOffs = pThis->iCurrOffs; /* we are at begin of next line */ |
| 964 |
} else { |
| 965 |
DBGPRINTF("RGER: strmReadLine iRet %d\n", iRet); |
| 966 |
if (*ppCStr != NULL) { |
| 967 |
if (cstrLen(*ppCStr) > 0) { |
| 968 |
/* we may have an empty string in an unsuccesfull poll or after restart! */ |
| 969 |
if (rsCStrConstructFromCStr(&pThis->prevLineSegment, *ppCStr) != RS_RET_OK) { |
| 970 |
/* we cannot do anything against this, but we can at least |
| 971 |
* ensure we do not have any follow-on errors. |
| 972 |
*/ |
| 973 |
pThis->prevLineSegment = NULL; |
| 974 |
} |
| 975 |
} |
| 976 |
cstrDestruct(ppCStr); |
| 977 |
} |
| 978 |
} |
| 979 |
|
| 980 |
RETiRet; |
| 981 |
} |
| 982 |
|
| 983 |
/* check if the current multi line read is timed out |
| 984 |
* @return 0 - no timeout, something else - timeout |
| 985 |
*/ |
| 986 |
int strmReadMultiLine_isTimedOut(const strm_t *const __restrict__ pThis) { |
| 987 |
/* note: order of evaluation is choosen so that the most inexpensive |
| 988 |
* processing flow is used. |
| 989 |
*/ |
| 990 |
DBGPRINTF( |
| 991 |
"strmReadMultiline_isTimedOut: prevMsgSeg %p, readTimeout %d, " |
| 992 |
"lastRead %lld\n", |
| 993 |
pThis->prevMsgSegment, pThis->readTimeout, (long long)pThis->lastRead); |
| 994 |
return ((pThis->readTimeout) && (pThis->prevMsgSegment != NULL) && |
| 995 |
(getTime(NULL) > pThis->lastRead + pThis->readTimeout)); |
| 996 |
} |
| 997 |
|
| 998 |
/* read a multi-line message from a strm file. |
| 999 |
* The multi-line message is terminated based on the user-provided |
| 1000 |
* startRegex or endRegex (Posix ERE). For performance reasons, the regex |
| 1001 |
* must already have been compiled by the user. |
| 1002 |
* added 2015-05-12 rgerhards |
| 1003 |
*/ |
| 1004 |
rsRetVal ATTR_NONNULL(1, 2) strmReadMultiLine(strm_t *pThis, |
| 1005 |
cstr_t **ppCStr, |
| 1006 |
regex_t *start_preg, |
| 1007 |
regex_t *end_preg, |
| 1008 |
const sbool bEscapeLF, |
| 1009 |
const uchar *const escapeLFString, |
| 1010 |
const sbool discardTruncatedMsg, |
| 1011 |
const sbool msgDiscardingError, |
| 1012 |
int64 *const strtOffs) { |
| 1013 |
uchar c; |
| 1014 |
uchar finished = 0; |
| 1015 |
cstr_t *thisLine = NULL; |
| 1016 |
rsRetVal readCharRet; |
| 1017 |
const time_t tCurr = pThis->readTimeout ? getTime(NULL) : 0; |
| 1018 |
size_t maxMsgSize = glblGetMaxLine(runConf); |
| 1019 |
DEFiRet; |
| 1020 |
|
| 1021 |
do { |
| 1022 |
CHKiRet(strmReadChar(pThis, &c)); /* immediately exit on EOF */ |
| 1023 |
pThis->lastRead = tCurr; |
| 1024 |
CHKiRet(cstrConstruct(&thisLine)); |
| 1025 |
/* append previous message to current message if necessary */ |
| 1026 |
if (pThis->prevLineSegment != NULL) { |
| 1027 |
CHKiRet(cstrAppendCStr(thisLine, pThis->prevLineSegment)); |
| 1028 |
cstrDestruct(&pThis->prevLineSegment); |
| 1029 |
} |
| 1030 |
|
| 1031 |
while (c != '\n') { |
| 1032 |
CHKiRet(cstrAppendChar(thisLine, c)); |
| 1033 |
readCharRet = strmReadChar(pThis, &c); |
| 1034 |
if (readCharRet == RS_RET_EOF) { /* end of file reached without \n? */ |
| 1035 |
CHKiRet(rsCStrConstructFromCStr(&pThis->prevLineSegment, thisLine)); |
| 1036 |
} |
| 1037 |
CHKiRet(readCharRet); |
| 1038 |
} |
| 1039 |
cstrFinalize(thisLine); |
| 1040 |
|
| 1041 |
/* we have a line, now let's assemble the message */ |
| 1042 |
const int isStartMatch = |
| 1043 |
start_preg ? !regexec(start_preg, (char *)rsCStrGetSzStrNoNULL(thisLine), 0, NULL, 0) : 0; |
| 1044 |
const int isEndMatch = end_preg ? !regexec(end_preg, (char *)rsCStrGetSzStrNoNULL(thisLine), 0, NULL, 0) : 0; |
| 1045 |
|
| 1046 |
if (isStartMatch) { |
| 1047 |
/* in this case, the *previous* message is complete and we are |
| 1048 |
* at the start of a new one. |
| 1049 |
*/ |
| 1050 |
if (pThis->ignoringMsg == 0) { |
| 1051 |
if (pThis->prevMsgSegment != NULL) { |
| 1052 |
/* may be NULL in initial poll! */ |
| 1053 |
finished = 1; |
| 1054 |
*ppCStr = pThis->prevMsgSegment; |
| 1055 |
} |
| 1056 |
} |
| 1057 |
CHKiRet(rsCStrConstructFromCStr(&pThis->prevMsgSegment, thisLine)); |
| 1058 |
pThis->ignoringMsg = 0; |
| 1059 |
} else { |
| 1060 |
if (pThis->ignoringMsg == 0) { |
| 1061 |
if (pThis->prevMsgSegment == NULL) { |
| 1062 |
/* may be NULL in initial poll or after timeout! */ |
| 1063 |
CHKiRet(rsCStrConstructFromCStr(&pThis->prevMsgSegment, thisLine)); |
| 1064 |
} else { |
| 1065 |
if (bEscapeLF) { |
| 1066 |
if (escapeLFString == NULL) { |
| 1067 |
rsCStrAppendStrWithLen(pThis->prevMsgSegment, (uchar *)"\\n", 2); |
| 1068 |
} else { |
| 1069 |
rsCStrAppendStr(pThis->prevMsgSegment, escapeLFString); |
| 1070 |
} |
| 1071 |
} else { |
| 1072 |
cstrAppendChar(pThis->prevMsgSegment, '\n'); |
| 1073 |
} |
| 1074 |
|
| 1075 |
|
| 1076 |
size_t currLineLen = cstrLen(thisLine); |
| 1077 |
if (currLineLen > 0) { |
| 1078 |
size_t len; |
| 1079 |
if ((len = cstrLen(pThis->prevMsgSegment) + currLineLen) < maxMsgSize) { |
| 1080 |
CHKiRet(cstrAppendCStr(pThis->prevMsgSegment, thisLine)); |
| 1081 |
/* we could do this faster, but for now keep it simple */ |
| 1082 |
} else { |
| 1083 |
if (cstrLen(pThis->prevMsgSegment) > maxMsgSize) { |
| 1084 |
len = 0; |
| 1085 |
} else { |
| 1086 |
len = currLineLen - (len - maxMsgSize); |
| 1087 |
for (size_t z = 0; z < len; z++) { |
| 1088 |
cstrAppendChar(pThis->prevMsgSegment, thisLine->pBuf[z]); |
| 1089 |
} |
| 1090 |
} |
| 1091 |
finished = 1; |
| 1092 |
*ppCStr = pThis->prevMsgSegment; |
| 1093 |
CHKiRet(rsCStrConstructFromszStr(&pThis->prevMsgSegment, thisLine->pBuf + len)); |
| 1094 |
if (discardTruncatedMsg == 1) { |
| 1095 |
pThis->ignoringMsg = 1; |
| 1096 |
} |
| 1097 |
if (msgDiscardingError == 1) { |
| 1098 |
if (discardTruncatedMsg == 1) { |
| 1099 |
LogError(0, RS_RET_ERR, |
| 1100 |
"imfile error: message received is " |
| 1101 |
"larger than max msg size; " |
| 1102 |
"rest of message will not be " |
| 1103 |
"processed"); |
| 1104 |
} else { |
| 1105 |
LogError(0, RS_RET_ERR, |
| 1106 |
"imfile error: message received is " |
| 1107 |
"larger than max msg size; message " |
| 1108 |
"will be split and processed as " |
| 1109 |
"another message"); |
| 1110 |
} |
| 1111 |
} |
| 1112 |
} |
| 1113 |
} |
| 1114 |
} |
| 1115 |
} |
| 1116 |
} |
| 1117 |
if (isEndMatch) { |
| 1118 |
/* in this case, the *current* message is complete and we are |
| 1119 |
* at the end of it. |
| 1120 |
*/ |
| 1121 |
if (pThis->ignoringMsg == 0) { |
| 1122 |
if (pThis->prevMsgSegment != NULL) { |
| 1123 |
finished = 1; |
| 1124 |
*ppCStr = pThis->prevMsgSegment; |
| 1125 |
pThis->prevMsgSegment = NULL; |
| 1126 |
} |
| 1127 |
} |
| 1128 |
pThis->ignoringMsg = 0; |
| 1129 |
} |
| 1130 |
cstrDestruct(&thisLine); |
| 1131 |
} while (finished == 0); |
| 1132 |
|
| 1133 |
finalize_it: |
| 1134 |
*strtOffs = pThis->strtOffs; |
| 1135 |
if (thisLine != NULL) { |
| 1136 |
cstrDestruct(&thisLine); |
| 1137 |
} |
| 1138 |
if (iRet == RS_RET_OK) { |
| 1139 |
pThis->strtOffs = pThis->iCurrOffs; /* we are at begin of next line */ |
| 1140 |
cstrFinalize(*ppCStr); |
| 1141 |
} else { |
| 1142 |
if (pThis->readTimeout && (pThis->prevMsgSegment != NULL) && (tCurr > pThis->lastRead + pThis->readTimeout)) { |
| 1143 |
if (rsCStrConstructFromCStr(ppCStr, pThis->prevMsgSegment) == RS_RET_OK) { |
| 1144 |
cstrFinalize(*ppCStr); |
| 1145 |
cstrDestruct(&pThis->prevMsgSegment); |
| 1146 |
pThis->lastRead = tCurr; |
| 1147 |
pThis->strtOffs = pThis->iCurrOffs; /* we are at begin of next line */ |
| 1148 |
dbgprintf("stream: generated msg based on timeout: %s\n", cstrGetSzStrNoNULL(*ppCStr)); |
| 1149 |
iRet = RS_RET_OK; |
| 1150 |
} |
| 1151 |
} |
| 1152 |
} |
| 1153 |
RETiRet; |
| 1154 |
} |
| 1155 |
|
| 1156 |
/* Standard-Constructor for the strm object |
| 1157 |
*/ |
| 1158 |
BEGINobjConstruct(strm) /* be sure to specify the object type also in END macro! */ |
| 1159 |
pThis->iCurrFNum = 1; |
| 1160 |
pThis->fd = -1; |
| 1161 |
pThis->fdDir = -1; |
| 1162 |
pThis->iUngetC = -1; |
| 1163 |
pThis->bVeryReliableZip = 0; |
| 1164 |
pThis->sType = STREAMTYPE_FILE_SINGLE; |
| 1165 |
pThis->sIOBufSize = glblGetIOBufSize(); |
| 1166 |
pThis->tOpenMode = 0600; |
| 1167 |
pThis->compressionDriver = STRM_COMPRESS_ZIP; |
| 1168 |
pThis->pszSizeLimitCmd = NULL; |
| 1169 |
pThis->bSizeLimitCmdPassFileName = 1; |
| 1170 |
pThis->prevLineSegment = NULL; |
| 1171 |
pThis->prevMsgSegment = NULL; |
| 1172 |
pThis->strtOffs = 0; |
| 1173 |
pThis->ignoringMsg = 0; |
| 1174 |
pThis->bPrevWasNL = 0; |
| 1175 |
pThis->fileNotFoundError = 1; |
| 1176 |
pThis->noRepeatedErrorOutput = 0; |
| 1177 |
pThis->lastRead = getTime(NULL); |
| 1178 |
ENDobjConstruct(strm) |
| 1179 |
|
| 1180 |
|
| 1181 |
/* ConstructionFinalizer |
| 1182 |
* rgerhards, 2008-01-09 |
| 1183 |
*/ |
| 1184 |
static rsRetVal strmConstructFinalize(strm_t *pThis) { |
| 1185 |
pthread_mutexattr_t mutAttr; |
| 1186 |
rsRetVal localRet; |
| 1187 |
int i; |
| 1188 |
DEFiRet; |
| 1189 |
|
| 1190 |
assert(pThis != NULL); |
| 1191 |
|
| 1192 |
pThis->iBufPtrMax = 0; /* results in immediate read request */ |
| 1193 |
if (pThis->iZipLevel) { /* do we need a zip buf? */ |
| 1194 |
if (pThis->compressionDriver == STRM_COMPRESS_ZSTD) { |
| 1195 |
localRet = objUse(zstdw, LM_ZSTDW_FILENAME); |
| 1196 |
if (localRet != RS_RET_OK) { |
| 1197 |
pThis->iZipLevel = 0; |
| 1198 |
LogError(0, localRet, |
| 1199 |
"stream was requested with zstd compression mode, " |
| 1200 |
"but zstdw module unavailable - using without compression\n"); |
| 1201 |
} |
| 1202 |
} else { |
| 1203 |
assert(pThis->compressionDriver == STRM_COMPRESS_ZIP); |
| 1204 |
localRet = objUse(zlibw, LM_ZLIBW_FILENAME); |
| 1205 |
if (localRet != RS_RET_OK) { |
| 1206 |
pThis->iZipLevel = 0; |
| 1207 |
LogError(0, localRet, |
| 1208 |
"stream was requested with zip mode, but zlibw " |
| 1209 |
"module unavailable - using without zip\n"); |
| 1210 |
} |
| 1211 |
} |
| 1212 |
/* we use the same size as the original buf, as we would like |
| 1213 |
* to make sure we can write out everything with a SINGLE api call! |
| 1214 |
* We add another 128 bytes to take care of the gzip header and "all eventualities". |
| 1215 |
*/ |
| 1216 |
CHKmalloc(pThis->pZipBuf = (Bytef *)malloc(pThis->sIOBufSize + 128)); |
| 1217 |
} |
| 1218 |
|
| 1219 |
/* if we are set to sync, we must obtain a file handle to the directory for fsync() purposes */ |
| 1220 |
if (pThis->bSync && !pThis->bIsTTY && pThis->pszDir != NULL) { |
| 1221 |
pThis->fdDir = open((char *)pThis->pszDir, O_RDONLY | O_CLOEXEC | O_NOCTTY); |
| 1222 |
if (pThis->fdDir == -1) { |
| 1223 |
char errStr[1024]; |
| 1224 |
int err = errno; |
| 1225 |
rs_strerror_r(err, errStr, sizeof(errStr)); |
| 1226 |
DBGPRINTF( |
| 1227 |
"error %d opening directory file for fsync() use - fsync for directory " |
| 1228 |
"disabled: %s\n", |
| 1229 |
errno, errStr); |
| 1230 |
} |
| 1231 |
} |
| 1232 |
|
| 1233 |
/* if we have a flush interval, we need to do async writes in any case */ |
| 1234 |
if (pThis->iFlushInterval != 0) { |
| 1235 |
pThis->bAsyncWrite = 1; |
| 1236 |
} |
| 1237 |
|
| 1238 |
DBGPRINTF("file stream %s params: flush interval %d, async write %d\n", getFileDebugName(pThis), |
| 1239 |
pThis->iFlushInterval, pThis->bAsyncWrite); |
| 1240 |
|
| 1241 |
/* if we work asynchronously, we need a couple of synchronization objects */ |
| 1242 |
if (pThis->bAsyncWrite) { |
| 1243 |
/* the mutex must be recursive, because objects may call into other |
| 1244 |
* object identifiers recursively. |
| 1245 |
*/ |
| 1246 |
pthread_mutexattr_init(&mutAttr); |
| 1247 |
pthread_mutexattr_settype(&mutAttr, PTHREAD_MUTEX_RECURSIVE); |
| 1248 |
pthread_mutex_init(&pThis->mut, &mutAttr); |
| 1249 |
pthread_cond_init(&pThis->notFull, 0); |
| 1250 |
pthread_cond_init(&pThis->notEmpty, 0); |
| 1251 |
pthread_cond_init(&pThis->isEmpty, 0); |
| 1252 |
pThis->iCnt = pThis->iEnq = pThis->iDeq = 0; |
| 1253 |
for (i = 0; i < STREAM_ASYNC_NUMBUFS; ++i) { |
| 1254 |
CHKmalloc(pThis->asyncBuf[i].pBuf = (uchar *)malloc(pThis->sIOBufSize)); |
| 1255 |
} |
| 1256 |
pThis->pIOBuf = pThis->asyncBuf[0].pBuf; |
| 1257 |
pThis->bStopWriter = 0; |
| 1258 |
if (pthread_create(&pThis->writerThreadID, &default_thread_attr, asyncWriterThread, pThis) != 0) |
| 1259 |
DBGPRINTF("ERROR: stream %p cold not create writer thread\n", pThis); |
| 1260 |
} else { |
| 1261 |
/* we work synchronously, so we need to alloc a fixed pIOBuf */ |
| 1262 |
CHKmalloc(pThis->pIOBuf = (uchar *)malloc(pThis->sIOBufSize)); |
| 1263 |
CHKmalloc(pThis->pIOBuf_truncation = (char *)malloc(pThis->sIOBufSize)); |
| 1264 |
} |
| 1265 |
|
| 1266 |
finalize_it: |
| 1267 |
RETiRet; |
| 1268 |
} |
| 1269 |
|
| 1270 |
|
| 1271 |
/* destructor for the strm object */ |
| 1272 |
BEGINobjDestruct(strm) /* be sure to specify the object type also in END and CODESTART macros! */ |
| 1273 |
int i; |
| 1274 |
CODESTARTobjDestruct(strm); |
| 1275 |
/* we need to stop the ZIP writer */ |
| 1276 |
if (pThis->bAsyncWrite) /* Note: mutex will be unlocked in strmCloseFile/stopWriter! */ |
| 1277 |
d_pthread_mutex_lock(&pThis->mut); |
| 1278 |
|
| 1279 |
/* strmClose() will handle read-only files as well as need to open |
| 1280 |
* files that have unwritten buffers. -- rgerhards, 2010-03-09 |
| 1281 |
*/ |
| 1282 |
strmCloseFile(pThis); |
| 1283 |
|
| 1284 |
if (pThis->bAsyncWrite) { |
| 1285 |
pthread_mutex_destroy(&pThis->mut); |
| 1286 |
pthread_cond_destroy(&pThis->notFull); |
| 1287 |
pthread_cond_destroy(&pThis->notEmpty); |
| 1288 |
pthread_cond_destroy(&pThis->isEmpty); |
| 1289 |
for (i = 0; i < STREAM_ASYNC_NUMBUFS; ++i) { |
| 1290 |
free(pThis->asyncBuf[i].pBuf); |
| 1291 |
} |
| 1292 |
} else { |
| 1293 |
free(pThis->pIOBuf); |
| 1294 |
free(pThis->pIOBuf_truncation); |
| 1295 |
} |
| 1296 |
|
| 1297 |
/* Finally, we can free the resources. |
| 1298 |
* IMPORTANT: we MUST free this only AFTER the ansyncWriter has been stopped, else |
| 1299 |
* we get random errors... |
| 1300 |
*/ |
| 1301 |
if (pThis->compressionDriver == STRM_COMPRESS_ZSTD) { |
| 1302 |
zstdw.Destruct(pThis); |
| 1303 |
} |
| 1304 |
if (pThis->prevLineSegment) cstrDestruct(&pThis->prevLineSegment); |
| 1305 |
if (pThis->prevMsgSegment) cstrDestruct(&pThis->prevMsgSegment); |
| 1306 |
free(pThis->pszDir); |
| 1307 |
free(pThis->pZipBuf); |
| 1308 |
free(pThis->pszCurrFName); |
| 1309 |
free(pThis->pszFName); |
| 1310 |
free(pThis->pszSizeLimitCmd); |
| 1311 |
pThis->bStopWriter = 2; /* RG: use as flag for destruction */ |
| 1312 |
ENDobjDestruct(strm) |
| 1313 |
|
| 1314 |
|
| 1315 |
/* check if we need to open a new file (in output mode only). |
| 1316 |
* The decision is based on file size AND record delimition state. |
| 1317 |
* This method may also be called on a closed file, in which case |
| 1318 |
* it immediately returns. |
| 1319 |
*/ |
| 1320 |
static rsRetVal strmCheckNextOutputFile(strm_t *pThis) { |
| 1321 |
DEFiRet; |
| 1322 |
|
| 1323 |
if (pThis->fd == -1 || pThis->sType != STREAMTYPE_FILE_CIRCULAR) FINALIZE; |
| 1324 |
|
| 1325 |
/* wait for output to be empty, so that our counts are correct */ |
| 1326 |
strmWaitAsyncWriterDone(pThis); |
| 1327 |
|
| 1328 |
if (pThis->iCurrOffs >= pThis->iMaxFileSize) { |
| 1329 |
DBGOPRINT((obj_t *)pThis, "max file size %ld reached for %d, now %ld - starting new file\n", |
| 1330 |
(long)pThis->iMaxFileSize, pThis->fd, (long)pThis->iCurrOffs); |
| 1331 |
CHKiRet(strmNextFile(pThis)); |
| 1332 |
} |
| 1333 |
|
| 1334 |
finalize_it: |
| 1335 |
RETiRet; |
| 1336 |
} |
| 1337 |
|
| 1338 |
|
| 1339 |
/* try to recover a tty after a write error. This may have happend |
| 1340 |
* due to vhangup(), and, if so, we can simply re-open it. |
| 1341 |
*/ |
| 1342 |
#ifdef linux |
| 1343 |
#define ERR_TTYHUP EIO |
| 1344 |
#else |
| 1345 |
#define ERR_TTYHUP EBADF |
| 1346 |
#endif |
| 1347 |
static rsRetVal tryTTYRecover(strm_t *pThis, int err) { |
| 1348 |
DEFiRet; |
| 1349 |
ISOBJ_TYPE_assert(pThis, strm); |
| 1350 |
#ifndef __FreeBSD__ |
| 1351 |
if (err == ERR_TTYHUP) { |
| 1352 |
#else |
| 1353 |
/* Try to reopen our file descriptor even on errno 6, FreeBSD bug 200429 |
| 1354 |
* Also try on errno 5, FreeBSD bug 211033 |
| 1355 |
*/ |
| 1356 |
if (err == ERR_TTYHUP || err == ENXIO || err == EIO) { |
| 1357 |
#endif /* __FreeBSD__ */ |
| 1358 |
close(pThis->fd); |
| 1359 |
pThis->fd = -1; |
| 1360 |
CHKiRet(doPhysOpen(pThis)); |
| 1361 |
} |
| 1362 |
|
| 1363 |
finalize_it: |
| 1364 |
RETiRet; |
| 1365 |
} |
| 1366 |
#undef ER_TTYHUP |
| 1367 |
|
| 1368 |
|
| 1369 |
/* issue write() api calls until either the buffer is completely |
| 1370 |
* written or an error occurred (it may happen that multiple writes |
| 1371 |
* are required, what is perfectly legal. On exit, *pLenBuf contains |
| 1372 |
* the number of bytes actually written. |
| 1373 |
* rgerhards, 2009-06-08 |
| 1374 |
*/ |
| 1375 |
static rsRetVal ATTR_NONNULL(1, 2, 3) doWriteCall(strm_t *pThis, uchar *pBuf, size_t *pLenBuf) { |
| 1376 |
ssize_t lenBuf; |
| 1377 |
ssize_t iTotalWritten; |
| 1378 |
ssize_t iWritten; |
| 1379 |
char *pWriteBuf; |
| 1380 |
DEFiRet; |
| 1381 |
ISOBJ_TYPE_assert(pThis, strm); |
| 1382 |
#ifdef __FreeBSD__ |
| 1383 |
sbool crnlNow = 0; |
| 1384 |
#endif /* __FreeBSD__ */ |
| 1385 |
|
| 1386 |
lenBuf = *pLenBuf; |
| 1387 |
pWriteBuf = (char *)pBuf; |
| 1388 |
iTotalWritten = 0; |
| 1389 |
do { |
| 1390 |
#ifdef __FreeBSD__ |
| 1391 |
if (pThis->bIsTTY && !pThis->iZipLevel && !pThis->cryprov) { |
| 1392 |
char *pNl = NULL; |
| 1393 |
if (crnlNow == 0) |
| 1394 |
pNl = strchr(pWriteBuf, '\n'); |
| 1395 |
else |
| 1396 |
crnlNow = 0; |
| 1397 |
if (pNl == pWriteBuf) { |
| 1398 |
iWritten = write(pThis->fd, "\r", 1); |
| 1399 |
if (iWritten > 0) { |
| 1400 |
crnlNow = 1; |
| 1401 |
iWritten = 0; |
| 1402 |
} |
| 1403 |
} else |
| 1404 |
iWritten = write(pThis->fd, pWriteBuf, pNl ? pNl - pWriteBuf : lenBuf); |
| 1405 |
} else |
| 1406 |
#endif /* __FreeBSD__ */ |
| 1407 |
iWritten = write(pThis->fd, pWriteBuf, lenBuf); |
| 1408 |
if (iWritten < 0) { |
| 1409 |
const int err = errno; |
| 1410 |
iWritten = 0; /* we have written NO bytes! */ |
| 1411 |
if (err == EBADF) { |
| 1412 |
DBGPRINTF( |
| 1413 |
"file %s: errno %d, fd %d no longer valid, recovery by " |
| 1414 |
"reopen; if you see this, consider reporting at " |
| 1415 |
"https://github.com/rsyslog/rsyslog/issues/3404 " |
| 1416 |
"so that we know when it happens. Include output of uname -a. " |
| 1417 |
"OS error reason", |
| 1418 |
pThis->pszCurrFName, err, pThis->fd); |
| 1419 |
pThis->fd = -1; |
| 1420 |
CHKiRet(doPhysOpen(pThis)); |
| 1421 |
} else { |
| 1422 |
if (err != EINTR) { |
| 1423 |
LogError(err, RS_RET_IO_ERROR, |
| 1424 |
"file '%s'[%d] write error - see " |
| 1425 |
"https://www.rsyslog.com/solving-rsyslog-write-errors/ for help " |
| 1426 |
"OS error", |
| 1427 |
pThis->pszCurrFName, pThis->fd); |
| 1428 |
} |
| 1429 |
if (err == EINTR) { |
| 1430 |
/*NO ERROR, just continue */; |
| 1431 |
} else if (!pThis->bIsTTY && (err == ENOTCONN || err == EIO)) { |
| 1432 |
/* Failure for network file system, thus file needs to be closed |
| 1433 |
* and reopened. */ |
| 1434 |
close(pThis->fd); |
| 1435 |
pThis->fd = -1; |
| 1436 |
CHKiRet(doPhysOpen(pThis)); |
| 1437 |
} else { |
| 1438 |
if (pThis->bIsTTY) { |
| 1439 |
CHKiRet(tryTTYRecover(pThis, err)); |
| 1440 |
} else { |
| 1441 |
ABORT_FINALIZE(RS_RET_IO_ERROR); |
| 1442 |
/* Would it make sense to cover more error cases? So far, I |
| 1443 |
* do not see good reason to do so. |
| 1444 |
*/ |
| 1445 |
} |
| 1446 |
} |
| 1447 |
} |
| 1448 |
} |
| 1449 |
/* advance buffer to next write position */ |
| 1450 |
iTotalWritten += iWritten; |
| 1451 |
lenBuf -= iWritten; |
| 1452 |
pWriteBuf += iWritten; |
| 1453 |
} while (lenBuf > 0); /* Warning: do..while()! */ |
| 1454 |
|
| 1455 |
DBGOPRINT((obj_t *)pThis, "file %d write wrote %d bytes\n", pThis->fd, (int)iWritten); |
| 1456 |
|
| 1457 |
finalize_it: |
| 1458 |
*pLenBuf = iTotalWritten; |
| 1459 |
RETiRet; |
| 1460 |
} |
| 1461 |
|
| 1462 |
|
| 1463 |
/* write memory buffer to a stream object. |
| 1464 |
*/ |
| 1465 |
static rsRetVal doWriteInternal(strm_t *pThis, uchar *pBuf, const size_t lenBuf, const int bFlush) { |
| 1466 |
DEFiRet; |
| 1467 |
|
| 1468 |
DBGOPRINT((obj_t *)pThis, "file %d(%s) doWriteInternal: bFlush %d\n", pThis->fd, getFileDebugName(pThis), bFlush); |
| 1469 |
|
| 1470 |
if (pThis->iZipLevel) { |
| 1471 |
CHKiRet(doZipWrite(pThis, pBuf, lenBuf, bFlush)); |
| 1472 |
} else { |
| 1473 |
/* write without zipping */ |
| 1474 |
CHKiRet(strmPhysWrite(pThis, pBuf, lenBuf)); |
| 1475 |
} |
| 1476 |
|
| 1477 |
finalize_it: |
| 1478 |
RETiRet; |
| 1479 |
} |
| 1480 |
|
| 1481 |
|
| 1482 |
/* This function is called to "do" an async write call, what primarily means that |
| 1483 |
* the data is handed over to the writer thread (which will then do the actual write |
| 1484 |
* in parallel). Note that the stream mutex has already been locked by the |
| 1485 |
* strmWrite...() calls. Also note that we always have only a single producer, |
| 1486 |
* so we can simply serially assign the next free buffer to it and be sure that |
| 1487 |
* the very some producer comes back in sequence to submit the then-filled buffers. |
| 1488 |
* This also enables us to timout on partially written buffers. -- rgerhards, 2009-07-06 |
| 1489 |
*/ |
| 1490 |
static rsRetVal doAsyncWriteInternal(strm_t *pThis, size_t lenBuf, const int bFlushZip) { |
| 1491 |
DEFiRet; |
| 1492 |
ISOBJ_TYPE_assert(pThis, strm); |
| 1493 |
|
| 1494 |
DBGOPRINT((obj_t *)pThis, |
| 1495 |
"file %d(%s) doAsyncWriteInternal at begin: " |
| 1496 |
"iCnt %d, iEnq %d, bFlushZip %d\n", |
| 1497 |
pThis->fd, getFileDebugName(pThis), pThis->iCnt, pThis->iEnq, bFlushZip); |
| 1498 |
/* the -1 below is important, because we need one buffer for the main thread! */ |
| 1499 |
while (pThis->iCnt >= STREAM_ASYNC_NUMBUFS - 1) d_pthread_cond_wait(&pThis->notFull, &pThis->mut); |
| 1500 |
|
| 1501 |
pThis->asyncBuf[pThis->iEnq % STREAM_ASYNC_NUMBUFS].lenBuf = lenBuf; |
| 1502 |
pThis->pIOBuf = pThis->asyncBuf[++pThis->iEnq % STREAM_ASYNC_NUMBUFS].pBuf; |
| 1503 |
if (!pThis->bFlushNow) /* if we already need to flush, do not overwrite */ |
| 1504 |
pThis->bFlushNow = bFlushZip; |
| 1505 |
|
| 1506 |
pThis->bDoTimedWait = 0; /* everything written, no need to timeout partial buffer writes */ |
| 1507 |
if (++pThis->iCnt == 1) { |
| 1508 |
pthread_cond_signal(&pThis->notEmpty); |
| 1509 |
DBGOPRINT((obj_t *)pThis, "doAsyncWriteInternal signaled notEmpty\n"); |
| 1510 |
} |
| 1511 |
DBGOPRINT((obj_t *)pThis, |
| 1512 |
"file %d(%s) doAsyncWriteInternal at exit: " |
| 1513 |
"iCnt %d, iEnq %d, bFlushZip %d\n", |
| 1514 |
pThis->fd, getFileDebugName(pThis), pThis->iCnt, pThis->iEnq, bFlushZip); |
| 1515 |
|
| 1516 |
RETiRet; |
| 1517 |
} |
| 1518 |
|
| 1519 |
|
| 1520 |
/* schedule writing to the stream. Depending on our concurrency settings, |
| 1521 |
* this either directly writes to the stream or schedules writing via |
| 1522 |
* the background thread. -- rgerhards, 2009-07-07 |
| 1523 |
*/ |
| 1524 |
static rsRetVal strmSchedWrite(strm_t *pThis, uchar *pBuf, size_t lenBuf, const int bFlushZip) { |
| 1525 |
DEFiRet; |
| 1526 |
|
| 1527 |
assert(pThis != NULL); |
| 1528 |
|
| 1529 |
/* we need to reset the buffer pointer BEFORE calling the actual write |
| 1530 |
* function. Otherwise, in circular mode, the write function will |
| 1531 |
* potentially close the file, then close will flush and as the |
| 1532 |
* buffer pointer is nonzero, will re-call into this code here. In |
| 1533 |
* the end result, we than have a problem (and things are screwed |
| 1534 |
* up). So we reset the buffer pointer first, and all this can |
| 1535 |
* not happen. It is safe to do so, because that pointer is NOT |
| 1536 |
* used inside the write functions. -- rgerhads, 2010-03-10 |
| 1537 |
*/ |
| 1538 |
pThis->iBufPtr = 0; /* we are at the begin of a new buffer */ |
| 1539 |
if (pThis->bAsyncWrite) { |
| 1540 |
CHKiRet(doAsyncWriteInternal(pThis, lenBuf, bFlushZip)); |
| 1541 |
} else { |
| 1542 |
CHKiRet(doWriteInternal(pThis, pBuf, lenBuf, bFlushZip)); |
| 1543 |
} |
| 1544 |
|
| 1545 |
|
| 1546 |
finalize_it: |
| 1547 |
RETiRet; |
| 1548 |
} |
| 1549 |
|
| 1550 |
|
| 1551 |
/* This is the writer thread for asynchronous mode. |
| 1552 |
* -- rgerhards, 2009-07-06 |
| 1553 |
*/ |
| 1554 |
static void *asyncWriterThread(void *pPtr) { |
| 1555 |
int iDeq; |
| 1556 |
struct timespec t; |
| 1557 |
sbool bTimedOut = 0; |
| 1558 |
strm_t *pThis = (strm_t *)pPtr; |
| 1559 |
int err; |
| 1560 |
uchar thrdName[256] = "rs:"; |
| 1561 |
ISOBJ_TYPE_assert(pThis, strm); |
| 1562 |
|
| 1563 |
ustrncpy(thrdName + 3, pThis->pszFName, sizeof(thrdName) - 4); |
| 1564 |
dbgOutputTID((char *)thrdName); |
| 1565 |
#if defined(HAVE_PRCTL) && defined(PR_SET_NAME) |
| 1566 |
if (prctl(PR_SET_NAME, (char *)thrdName, 0, 0, 0) != 0) { |
| 1567 |
DBGPRINTF("prctl failed, not setting thread name for '%s'\n", "stream writer"); |
| 1568 |
} |
| 1569 |
#endif |
| 1570 |
|
| 1571 |
d_pthread_mutex_lock(&pThis->mut); |
| 1572 |
while (1) { /* loop broken inside */ |
| 1573 |
while (pThis->iCnt == 0) { |
| 1574 |
DBGOPRINT((obj_t *)pThis, |
| 1575 |
"file %d(%s) asyncWriterThread new iteration, " |
| 1576 |
"iCnt %d, bTimedOut %d, iFlushInterval %d\n", |
| 1577 |
pThis->fd, getFileDebugName(pThis), pThis->iCnt, bTimedOut, pThis->iFlushInterval); |
| 1578 |
if (pThis->bStopWriter) { |
| 1579 |
pthread_cond_broadcast(&pThis->isEmpty); |
| 1580 |
d_pthread_mutex_unlock(&pThis->mut); |
| 1581 |
goto finalize_it; /* break main loop */ |
| 1582 |
} |
| 1583 |
if (bTimedOut && pThis->iBufPtr > 0) { |
| 1584 |
/* if we timed out, we need to flush pending data */ |
| 1585 |
strmFlushInternal(pThis, 1); |
| 1586 |
bTimedOut = 0; |
| 1587 |
continue; |
| 1588 |
} |
| 1589 |
bTimedOut = 0; |
| 1590 |
if (pThis->bDoTimedWait) { |
| 1591 |
timeoutComp(&t, pThis->iFlushInterval * 1000); /* 1000 *millisconds* */ |
| 1592 |
if ((err = pthread_cond_timedwait(&pThis->notEmpty, &pThis->mut, &t)) != 0) { |
| 1593 |
DBGOPRINT((obj_t *)pThis, "file %d(%s) asyncWriterThread timed out\n", pThis->fd, |
| 1594 |
getFileDebugName(pThis)); |
| 1595 |
bTimedOut = 1; /* simulate in any case */ |
| 1596 |
if (err != ETIMEDOUT) { |
| 1597 |
char errStr[1024]; |
| 1598 |
rs_strerror_r(err, errStr, sizeof(errStr)); |
| 1599 |
DBGPRINTF( |
| 1600 |
"stream async writer timeout with error (%d): %s - " |
| 1601 |
"ignoring\n", |
| 1602 |
err, errStr); |
| 1603 |
} |
| 1604 |
} |
| 1605 |
} else { |
| 1606 |
d_pthread_cond_wait(&pThis->notEmpty, &pThis->mut); |
| 1607 |
} |
| 1608 |
} |
| 1609 |
|
| 1610 |
DBGOPRINT((obj_t *)pThis, |
| 1611 |
"file %d(%s) asyncWriterThread awoken, " |
| 1612 |
"iCnt %d, bTimedOut %d\n", |
| 1613 |
pThis->fd, getFileDebugName(pThis), pThis->iCnt, bTimedOut); |
| 1614 |
bTimedOut = 0; /* we may have timed out, but there *is* work to do... */ |
| 1615 |
|
| 1616 |
iDeq = pThis->iDeq++ % STREAM_ASYNC_NUMBUFS; |
| 1617 |
const int bFlush = (pThis->bFlushNow || bTimedOut) ? 1 : 0; |
| 1618 |
pThis->bFlushNow = 0; |
| 1619 |
|
| 1620 |
/* now we can do the actual write in parallel */ |
| 1621 |
d_pthread_mutex_unlock(&pThis->mut); |
| 1622 |
doWriteInternal(pThis, pThis->asyncBuf[iDeq].pBuf, pThis->asyncBuf[iDeq].lenBuf, bFlush); |
| 1623 |
// TODO: error check????? 2009-07-06 |
| 1624 |
d_pthread_mutex_lock(&pThis->mut); |
| 1625 |
|
| 1626 |
--pThis->iCnt; |
| 1627 |
if (pThis->iCnt < STREAM_ASYNC_NUMBUFS) { |
| 1628 |
pthread_cond_signal(&pThis->notFull); |
| 1629 |
if (pThis->iCnt == 0) pthread_cond_broadcast(&pThis->isEmpty); |
| 1630 |
} |
| 1631 |
} |
| 1632 |
/* Not reached */ |
| 1633 |
|
| 1634 |
finalize_it: |
| 1635 |
DBGOPRINT((obj_t *)pThis, "file %d(%s) asyncWriterThread terminated\n", pThis->fd, getFileDebugName(pThis)); |
| 1636 |
return NULL; /* to keep pthreads happy */ |
| 1637 |
} |
| 1638 |
|
| 1639 |
|
| 1640 |
/* sync the file to disk, so that any unwritten data is persisted. This |
| 1641 |
* also syncs the directory and thus makes sure that the file survives |
| 1642 |
* fatal failure. Note that we do NOT return an error status if the |
| 1643 |
* sync fails. Doing so would probably cause more trouble than it |
| 1644 |
* is worth (read: data loss may occur where we otherwise might not |
| 1645 |
* have it). -- rgerhards, 2009-06-08 |
| 1646 |
*/ |
| 1647 |
#undef SYNCCALL |
| 1648 |
#if defined(HAVE_FDATASYNC) && !defined(__APPLE__) |
| 1649 |
#define SYNCCALL(x) fdatasync(x) |
| 1650 |
#else |
| 1651 |
#define SYNCCALL(x) fsync(x) |
| 1652 |
#endif |
| 1653 |
static rsRetVal syncFile(strm_t *pThis) { |
| 1654 |
int ret; |
| 1655 |
DEFiRet; |
| 1656 |
|
| 1657 |
if (pThis->bIsTTY) FINALIZE; /* TTYs can not be synced */ |
| 1658 |
|
| 1659 |
DBGPRINTF("syncing file %d\n", pThis->fd); |
| 1660 |
ret = SYNCCALL(pThis->fd); |
| 1661 |
if (ret != 0) { |
| 1662 |
char errStr[1024]; |
| 1663 |
int err = errno; |
| 1664 |
rs_strerror_r(err, errStr, sizeof(errStr)); |
| 1665 |
DBGPRINTF("sync failed for file %d with error (%d): %s - ignoring\n", pThis->fd, err, errStr); |
| 1666 |
} |
| 1667 |
|
| 1668 |
if (pThis->fdDir != -1) { |
| 1669 |
if (fsync(pThis->fdDir) != 0) DBGPRINTF("stream/syncFile: fsync returned error, ignoring\n"); |
| 1670 |
} |
| 1671 |
|
| 1672 |
finalize_it: |
| 1673 |
RETiRet; |
| 1674 |
} |
| 1675 |
#undef SYNCCALL |
| 1676 |
|
| 1677 |
/* physically write to the output file. the provided data is ready for |
| 1678 |
* writing (e.g. zipped if we are requested to do that). |
| 1679 |
* Note that if the write() API fails, we do not reset any pointers, but return |
| 1680 |
* an error code. That means we may redo work in the next iteration. |
| 1681 |
* rgerhards, 2009-06-04 |
| 1682 |
*/ |
| 1683 |
static rsRetVal strmPhysWrite(strm_t *pThis, uchar *pBuf, size_t lenBuf) { |
| 1684 |
size_t iWritten; |
| 1685 |
DEFiRet; |
| 1686 |
ISOBJ_TYPE_assert(pThis, strm); |
| 1687 |
|
| 1688 |
DBGPRINTF("strmPhysWrite, stream %p, len %u\n", pThis, (unsigned)lenBuf); |
| 1689 |
if (pThis->fd == -1) CHKiRet(strmOpenFile(pThis)); |
| 1690 |
|
| 1691 |
/* here we place our crypto interface */ |
| 1692 |
if (pThis->cryprov != NULL) { |
| 1693 |
pThis->cryprov->Encrypt(pThis->cryprovFileData, pBuf, &lenBuf); |
| 1694 |
} |
| 1695 |
/* end crypto */ |
| 1696 |
|
| 1697 |
iWritten = lenBuf; |
| 1698 |
CHKiRet(doWriteCall(pThis, pBuf, &iWritten)); |
| 1699 |
|
| 1700 |
pThis->iCurrOffs += iWritten; |
| 1701 |
/* update user counter, if provided */ |
| 1702 |
if (pThis->pUsrWCntr != NULL) *pThis->pUsrWCntr += iWritten; |
| 1703 |
|
| 1704 |
if (pThis->bSync) { |
| 1705 |
CHKiRet(syncFile(pThis)); |
| 1706 |
} |
| 1707 |
|
| 1708 |
if (pThis->sType == STREAMTYPE_FILE_CIRCULAR) { |
| 1709 |
CHKiRet(strmCheckNextOutputFile(pThis)); |
| 1710 |
} |
| 1711 |
|
| 1712 |
finalize_it: |
| 1713 |
RETiRet; |
| 1714 |
} |
| 1715 |
|
| 1716 |
|
| 1717 |
/* write the output buffer in zip mode |
| 1718 |
* This means we compress it first and then do a physical write. |
| 1719 |
* Note that we always do a full deflateInit ... deflate ... deflateEnd |
| 1720 |
* sequence. While this is not optimal, we need to do it because we need |
| 1721 |
* to ensure that the file is readable even when we are aborted. Doing the |
| 1722 |
* full sequence brings us as far towards this goal as possible (and not |
| 1723 |
* doing it would be a total failure). It may be worth considering to |
| 1724 |
* add a config switch so that the user can decide the risk he is ready |
| 1725 |
* to take, but so far this is not yet implemented (not even requested ;)). |
| 1726 |
* rgerhards, 2009-06-04 |
| 1727 |
*/ |
| 1728 |
static rsRetVal doZipWrite(strm_t *pThis, uchar *pBuf, size_t lenBuf, const int bFlush) { |
| 1729 |
if (pThis->compressionDriver == STRM_COMPRESS_ZSTD) { |
| 1730 |
return zstdw.doStrmWrite(pThis, pBuf, lenBuf, bFlush, strmPhysWrite); |
| 1731 |
} else { |
| 1732 |
return zlibw.doStrmWrite(pThis, pBuf, lenBuf, bFlush, strmPhysWrite); |
| 1733 |
} |
| 1734 |
} |
| 1735 |
|
| 1736 |
|
| 1737 |
/* finish zlib buffer, to be called before closing the ZIP file (if |
| 1738 |
* running in stream mode). |
| 1739 |
*/ |
| 1740 |
static rsRetVal doZipFinish(strm_t *pThis) { |
| 1741 |
if (pThis->compressionDriver == STRM_COMPRESS_ZSTD) { |
| 1742 |
return zstdw.doCompressFinish(pThis, strmPhysWrite); |
| 1743 |
} else { |
| 1744 |
return zlibw.doCompressFinish(pThis, strmPhysWrite); |
| 1745 |
} |
| 1746 |
} |
| 1747 |
|
| 1748 |
/* flush stream output buffer to persistent storage. This can be called at any time |
| 1749 |
* and is automatically called when the output buffer is full. |
| 1750 |
* rgerhards, 2008-01-10 |
| 1751 |
*/ |
| 1752 |
static rsRetVal strmFlushInternal(strm_t *pThis, int bFlushZip) { |
| 1753 |
DEFiRet; |
| 1754 |
|
| 1755 |
assert(pThis != NULL); |
| 1756 |
DBGOPRINT((obj_t *)pThis, "strmFlushinternal: file %d(%s) flush, buflen %ld%s\n", pThis->fd, |
| 1757 |
getFileDebugName(pThis), (long)pThis->iBufPtr, (pThis->iBufPtr == 0) ? " (no need to flush)" : ""); |
| 1758 |
|
| 1759 |
if (pThis->tOperationsMode != STREAMMODE_READ && pThis->iBufPtr > 0) { |
| 1760 |
iRet = strmSchedWrite(pThis, pThis->pIOBuf, pThis->iBufPtr, bFlushZip); |
| 1761 |
} |
| 1762 |
|
| 1763 |
RETiRet; |
| 1764 |
} |
| 1765 |
|
| 1766 |
|
| 1767 |
/* flush stream output buffer to persistent storage. This can be called at any time |
| 1768 |
* and is automatically called when the output buffer is full. This function is for |
| 1769 |
* use by EXTERNAL callers. Do NOT use it internally. It locks the async writer |
| 1770 |
* mutex if ther is need to do so. |
| 1771 |
* rgerhards, 2010-03-18 |
| 1772 |
*/ |
| 1773 |
static rsRetVal strmFlush(strm_t *pThis) { |
| 1774 |
DEFiRet; |
| 1775 |
|
| 1776 |
assert(pThis != NULL); |
| 1777 |
|
| 1778 |
DBGOPRINT((obj_t *)pThis, "file %d strmFlush\n", pThis->fd); |
| 1779 |
|
| 1780 |
if (pThis->bAsyncWrite) d_pthread_mutex_lock(&pThis->mut); |
| 1781 |
CHKiRet(strmFlushInternal(pThis, 1)); |
| 1782 |
|
| 1783 |
finalize_it: |
| 1784 |
if (pThis->bAsyncWrite) d_pthread_mutex_unlock(&pThis->mut); |
| 1785 |
|
| 1786 |
RETiRet; |
| 1787 |
} |
| 1788 |
|
| 1789 |
|
| 1790 |
/* seek a stream to a specific location. Pending writes are flushed, read data |
| 1791 |
* is invalidated. |
| 1792 |
* rgerhards, 2008-01-12 |
| 1793 |
*/ |
| 1794 |
static rsRetVal ATTR_NONNULL() strmSeek(strm_t *pThis, const off64_t offs) { |
| 1795 |
DEFiRet; |
| 1796 |
|
| 1797 |
ISOBJ_TYPE_assert(pThis, strm); |
| 1798 |
|
| 1799 |
if (pThis->fd == -1) { |
| 1800 |
CHKiRet(strmOpenFile(pThis)); |
| 1801 |
} else { |
| 1802 |
CHKiRet(strmFlushInternal(pThis, 0)); |
| 1803 |
} |
| 1804 |
DBGOPRINT((obj_t *)pThis, "file %d seek, pos %llu\n", pThis->fd, (long long unsigned)offs); |
| 1805 |
const off64_t i = lseek64(pThis->fd, offs, SEEK_SET); |
| 1806 |
if (i != offs) { |
| 1807 |
LogError(errno, RS_RET_IO_ERROR, |
| 1808 |
"file %s: unexpected error seeking to " |
| 1809 |
"offset %lld (ret %lld) - further malfunctions may happen", |
| 1810 |
pThis->pszCurrFName, (long long)i, (long long)offs); |
| 1811 |
ABORT_FINALIZE(RS_RET_IO_ERROR); |
| 1812 |
} |
| 1813 |
pThis->strtOffs = pThis->iCurrOffs = offs; /* we are now at *this* offset */ |
| 1814 |
pThis->iBufPtr = 0; /* buffer invalidated */ |
| 1815 |
|
| 1816 |
finalize_it: |
| 1817 |
RETiRet; |
| 1818 |
} |
| 1819 |
|
| 1820 |
/* multi-file seek, seeks to file number & offset within file. This |
| 1821 |
* is a support function for the queue, in circular mode. DO NOT USE |
| 1822 |
* IT FOR OTHER NEEDS - it may not work as expected. It will |
| 1823 |
* seek to the new position and delete interim files, as it skips them. |
| 1824 |
* Note: this code can be removed when the queue gets a new disk store |
| 1825 |
* handler (if and when it does ;)). |
| 1826 |
* The output parameter bytesDel receives the number of bytes that have |
| 1827 |
* been deleted (if a file is deleted) or 0 if nothing was deleted. |
| 1828 |
* rgerhards, 2012-11-07 |
| 1829 |
*/ |
| 1830 |
rsRetVal strmMultiFileSeek(strm_t *pThis, unsigned int FNum, off64_t offs, off64_t *bytesDel) { |
| 1831 |
struct stat statBuf; |
| 1832 |
int skipped_files; |
| 1833 |
off64_t deletedBytes; |
| 1834 |
DEFiRet; |
| 1835 |
ISOBJ_TYPE_assert(pThis, strm); |
| 1836 |
|
| 1837 |
if (FNum == 0 && offs == 0) { /* happens during queue init */ |
| 1838 |
*bytesDel = 0; |
| 1839 |
FINALIZE; |
| 1840 |
} |
| 1841 |
|
| 1842 |
skipped_files = FNum - pThis->iCurrFNum; |
| 1843 |
*bytesDel = 0; |
| 1844 |
|
| 1845 |
while (skipped_files > 0) { |
| 1846 |
CHKiRet(genFileName(&pThis->pszCurrFName, pThis->pszDir, pThis->lenDir, pThis->pszFName, pThis->lenFName, |
| 1847 |
pThis->iCurrFNum, pThis->iFileNumDigits)); |
| 1848 |
dbgprintf("rger: processing file %s\n", pThis->pszCurrFName); |
| 1849 |
memset(&statBuf, 0, sizeof(statBuf)); |
| 1850 |
deletedBytes = 0; |
| 1851 |
if (stat((char *)pThis->pszCurrFName, &statBuf) != 0) { |
| 1852 |
LogError(errno, RS_RET_IO_ERROR, |
| 1853 |
"unexpected error doing a stat() " |
| 1854 |
"on file %s - further malfunctions may happen", |
| 1855 |
pThis->pszCurrFName); |
| 1856 |
/* we do NOT error-terminate here as this could worsen the |
| 1857 |
* situation. As such, we just keep running and try to delete |
| 1858 |
* as many files as possible. |
| 1859 |
*/ |
| 1860 |
} else { |
| 1861 |
deletedBytes = statBuf.st_size; |
| 1862 |
*bytesDel += deletedBytes; |
| 1863 |
} |
| 1864 |
DBGPRINTF( |
| 1865 |
"strmMultiFileSeek: detected new filenum, was %u, new %u, " |
| 1866 |
"deleting '%s' (%lld bytes)\n", |
| 1867 |
pThis->iCurrFNum, FNum, pThis->pszCurrFName, (long long)deletedBytes); |
| 1868 |
unlink((char *)pThis->pszCurrFName); |
| 1869 |
if (pThis->cryprov != NULL) pThis->cryprov->DeleteStateFiles(pThis->pszCurrFName); |
| 1870 |
free(pThis->pszCurrFName); |
| 1871 |
pThis->pszCurrFName = NULL; |
| 1872 |
pThis->iCurrFNum++; |
| 1873 |
--skipped_files; |
| 1874 |
} |
| 1875 |
DBGOPRINT((obj_t *)pThis, "strmMultiFileSeek: deleted %lld bytes in this run\n", (long long)*bytesDel); |
| 1876 |
pThis->strtOffs = pThis->iCurrOffs = offs; |
| 1877 |
|
| 1878 |
finalize_it: |
| 1879 |
RETiRet; |
| 1880 |
} |
| 1881 |
|
| 1882 |
|
| 1883 |
/* seek to current offset. This is primarily a helper to readjust the OS file |
| 1884 |
* pointer after a strm object has been deserialized. |
| 1885 |
*/ |
| 1886 |
static rsRetVal strmSeekCurrOffs(strm_t *pThis) { |
| 1887 |
off64_t targetOffs; |
| 1888 |
uchar c; |
| 1889 |
DEFiRet; |
| 1890 |
|
| 1891 |
ISOBJ_TYPE_assert(pThis, strm); |
| 1892 |
|
| 1893 |
if (pThis->cryprov == NULL || pThis->tOperationsMode != STREAMMODE_READ) { |
| 1894 |
iRet = strmSeek(pThis, pThis->iCurrOffs); |
| 1895 |
FINALIZE; |
| 1896 |
} |
| 1897 |
|
| 1898 |
/* As the cryprov may use CBC or similiar things, we need to read skip data */ |
| 1899 |
targetOffs = pThis->iCurrOffs; |
| 1900 |
pThis->strtOffs = pThis->iCurrOffs = 0; |
| 1901 |
DBGOPRINT((obj_t *)pThis, "encrypted, doing skip read of %lld bytes\n", (long long)targetOffs); |
| 1902 |
while (targetOffs != pThis->iCurrOffs) { |
| 1903 |
CHKiRet(strmReadChar(pThis, &c)); |
| 1904 |
} |
| 1905 |
finalize_it: |
| 1906 |
RETiRet; |
| 1907 |
} |
| 1908 |
|
| 1909 |
|
| 1910 |
/* write a *single* character to a stream object -- rgerhards, 2008-01-10 |
| 1911 |
*/ |
| 1912 |
static rsRetVal strmWriteChar(strm_t *__restrict__ const pThis, const uchar c) { |
| 1913 |
DEFiRet; |
| 1914 |
|
| 1915 |
assert(pThis != NULL); |
| 1916 |
|
| 1917 |
if (pThis->bAsyncWrite) d_pthread_mutex_lock(&pThis->mut); |
| 1918 |
|
| 1919 |
if (pThis->bDisabled) ABORT_FINALIZE(RS_RET_STREAM_DISABLED); |
| 1920 |
|
| 1921 |
/* if the buffer is full, we need to flush before we can write */ |
| 1922 |
if (pThis->iBufPtr == pThis->sIOBufSize) { |
| 1923 |
CHKiRet(strmFlushInternal(pThis, 0)); |
| 1924 |
} |
| 1925 |
|
| 1926 |
/* we now always have space for one character, so we simply copy it */ |
| 1927 |
*(pThis->pIOBuf + pThis->iBufPtr) = c; |
| 1928 |
pThis->iBufPtr++; |
| 1929 |
|
| 1930 |
finalize_it: |
| 1931 |
if (pThis->bAsyncWrite) d_pthread_mutex_unlock(&pThis->mut); |
| 1932 |
|
| 1933 |
RETiRet; |
| 1934 |
} |
| 1935 |
|
| 1936 |
|
| 1937 |
/* write an integer value (actually a long) to a stream object |
| 1938 |
* Note that we do not need to lock the mutex here, because we call |
| 1939 |
* strmWrite(), which does the lock (aka: we must not lock it, else we |
| 1940 |
* would run into a recursive lock, resulting in a deadlock!) |
| 1941 |
*/ |
| 1942 |
static rsRetVal strmWriteLong(strm_t *__restrict__ const pThis, const long i) { |
| 1943 |
DEFiRet; |
| 1944 |
uchar szBuf[32]; |
| 1945 |
|
| 1946 |
assert(pThis != NULL); |
| 1947 |
|
| 1948 |
CHKiRet(srUtilItoA((char *)szBuf, sizeof(szBuf), (number_t)i)); |
| 1949 |
CHKiRet(strmWrite(pThis, szBuf, strlen((char *)szBuf))); |
| 1950 |
|
| 1951 |
finalize_it: |
| 1952 |
RETiRet; |
| 1953 |
} |
| 1954 |
|
| 1955 |
|
| 1956 |
/* write memory buffer to a stream object. |
| 1957 |
* process the data in chunks and copy it over to our buffer. The caller-provided data |
| 1958 |
* may theoritically be larger than our buffer. In that case, we do multiple copies. One |
| 1959 |
* may argue if it were more efficient to write out the caller-provided buffer in that case |
| 1960 |
* and earlier versions of rsyslog did this. However, this introduces a lot of complexity |
| 1961 |
* inside the buffered writer and potential performance bottlenecks when trying to solve |
| 1962 |
* it. Now keep in mind that we actually do (almost?) never have a case where the |
| 1963 |
* caller-provided buffer is larger than our one. So instead of optimizing a case |
| 1964 |
* which normally does not exist, we expect some degradation in its case but make us |
| 1965 |
* perform better in the regular cases. -- rgerhards, 2009-07-07 |
| 1966 |
* Note: the pThis->iBufPtr == pThis->sIOBufSize logic below looks a bit like an |
| 1967 |
* on-off error. In fact, it is not, because iBufPtr always points to the next |
| 1968 |
* *free* byte in the buffer. So if it is sIOBufSize - 1, there actually is one |
| 1969 |
* free byte left. This came up during a code walkthrough and was considered |
| 1970 |
* worth nothing. -- rgerhards, 2010-03-10 |
| 1971 |
*/ |
| 1972 |
static rsRetVal ATTR_NONNULL(1, 2) |
| 1973 |
strmWrite(strm_t *__restrict__ const pThis, const uchar *__restrict__ const pBuf, size_t lenBuf) { |
| 1974 |
DEFiRet; |
| 1975 |
size_t iWrite; |
| 1976 |
size_t iOffset; |
| 1977 |
|
| 1978 |
assert(pThis != NULL); |
| 1979 |
assert(pBuf != NULL); |
| 1980 |
|
| 1981 |
if (pThis->bDisabled) ABORT_FINALIZE(RS_RET_STREAM_DISABLED); |
| 1982 |
|
| 1983 |
if (pThis->bAsyncWrite) d_pthread_mutex_lock(&pThis->mut); |
| 1984 |
|
| 1985 |
iOffset = 0; |
| 1986 |
do { |
| 1987 |
if (pThis->iBufPtr == pThis->sIOBufSize) { |
| 1988 |
CHKiRet(strmFlushInternal(pThis, 0)); /* get a new buffer for rest of data */ |
| 1989 |
} |
| 1990 |
iWrite = pThis->sIOBufSize - pThis->iBufPtr; /* this fits in current buf */ |
| 1991 |
if (iWrite > lenBuf) iWrite = lenBuf; |
| 1992 |
memcpy(pThis->pIOBuf + pThis->iBufPtr, pBuf + iOffset, iWrite); |
| 1993 |
pThis->iBufPtr += iWrite; |
| 1994 |
iOffset += iWrite; |
| 1995 |
lenBuf -= iWrite; |
| 1996 |
} while (lenBuf > 0); |
| 1997 |
|
| 1998 |
/* now check if the buffer right at the end of the write is full and, if so, |
| 1999 |
* write it. This seems more natural than waiting (hours?) for the next message... |
| 2000 |
*/ |
| 2001 |
if (pThis->iBufPtr == pThis->sIOBufSize) { |
| 2002 |
CHKiRet(strmFlushInternal(pThis, 0)); /* get a new buffer for rest of data */ |
| 2003 |
} |
| 2004 |
if (pThis->fd != -1 && pThis->iSizeLimit != 0) { /* Only check if fd already set */ |
| 2005 |
CHKiRet(doSizeLimitProcessing(pThis)); |
| 2006 |
} |
| 2007 |
|
| 2008 |
|
| 2009 |
finalize_it: |
| 2010 |
if (pThis->bAsyncWrite) { |
| 2011 |
if (pThis->bDoTimedWait == 0) { |
| 2012 |
/* we potentially have a partial buffer, so re-activate the |
| 2013 |
* writer thread that it can set and pick up timeouts. |
| 2014 |
*/ |
| 2015 |
pThis->bDoTimedWait = 1; |
| 2016 |
pthread_cond_signal(&pThis->notEmpty); |
| 2017 |
} |
| 2018 |
d_pthread_mutex_unlock(&pThis->mut); |
| 2019 |
} |
| 2020 |
|
| 2021 |
RETiRet; |
| 2022 |
} |
| 2023 |
|
| 2024 |
|
| 2025 |
/* property set methods */ |
| 2026 |
/* simple ones first */ |
| 2027 |
DEFpropSetMeth(strm, iMaxFileSize, int64) DEFpropSetMeth(strm, iFileNumDigits, int) |
| 2028 |
DEFpropSetMeth(strm, tOperationsMode, int) DEFpropSetMeth(strm, tOpenMode, mode_t) |
| 2029 |
DEFpropSetMeth(strm, compressionDriver, strm_compressionDriver_t) DEFpropSetMeth(strm, sType, strmType_t) |
| 2030 |
DEFpropSetMeth(strm, iZipLevel, int) DEFpropSetMeth(strm, bVeryReliableZip, int) |
| 2031 |
DEFpropSetMeth(strm, bSync, int) DEFpropSetMeth(strm, bReopenOnTruncate, int) |
| 2032 |
DEFpropSetMeth(strm, sIOBufSize, size_t) DEFpropSetMeth(strm, iSizeLimit, off_t) |
| 2033 |
DEFpropSetMeth(strm, iFlushInterval, int) DEFpropSetMeth(strm, pszSizeLimitCmd, uchar *) |
| 2034 |
DEFpropSetMeth(strm, bSizeLimitCmdPassFileName, int) |
| 2035 |
DEFpropSetMeth(strm, cryprov, cryprov_if_t *) DEFpropSetMeth(strm, cryprovData, void *) |
| 2036 |
|
| 2037 |
/* sets timeout in seconds */ |
| 2038 |
void ATTR_NONNULL() strmSetReadTimeout(strm_t *const __restrict__ pThis, const int val) { |
| 2039 |
ISOBJ_TYPE_assert(pThis, strm); |
| 2040 |
pThis->readTimeout = val; |
| 2041 |
} |
| 2042 |
|
| 2043 |
static rsRetVal ATTR_NONNULL() strmSetbDeleteOnClose(strm_t *const pThis, const int val) { |
| 2044 |
ISOBJ_TYPE_assert(pThis, strm); |
| 2045 |
pThis->bDeleteOnClose = val; |
| 2046 |
if (pThis->cryprov != NULL) { |
| 2047 |
pThis->cryprov->SetDeleteOnClose(pThis->cryprovFileData, pThis->bDeleteOnClose); |
| 2048 |
} |
| 2049 |
return RS_RET_OK; |
| 2050 |
} |
| 2051 |
|
| 2052 |
static rsRetVal ATTR_NONNULL() strmSetiMaxFiles(strm_t *const pThis, const int iNewVal) { |
| 2053 |
ISOBJ_TYPE_assert(pThis, strm); |
| 2054 |
pThis->iMaxFiles = iNewVal; |
| 2055 |
pThis->iFileNumDigits = getNumberDigits(iNewVal); |
| 2056 |
return RS_RET_OK; |
| 2057 |
} |
| 2058 |
|
| 2059 |
static rsRetVal ATTR_NONNULL() strmSetFileNotFoundError(strm_t *const pThis, const int pFileNotFoundError) { |
| 2060 |
ISOBJ_TYPE_assert(pThis, strm); |
| 2061 |
pThis->fileNotFoundError = pFileNotFoundError; |
| 2062 |
return RS_RET_OK; |
| 2063 |
} |
| 2064 |
|
| 2065 |
|
| 2066 |
/* set the stream's file prefix |
| 2067 |
* The passed-in string is duplicated. So if the caller does not need |
| 2068 |
* it any longer, it must free it. |
| 2069 |
* rgerhards, 2008-01-09 |
| 2070 |
*/ |
| 2071 |
static rsRetVal strmSetFName(strm_t *pThis, uchar *pszName, size_t iLenName) { |
| 2072 |
DEFiRet; |
| 2073 |
|
| 2074 |
assert(pThis != NULL); |
| 2075 |
assert(pszName != NULL); |
| 2076 |
|
| 2077 |
if (iLenName < 1) ABORT_FINALIZE(RS_RET_FILE_PREFIX_MISSING); |
| 2078 |
|
| 2079 |
if (pThis->pszFName != NULL) free(pThis->pszFName); |
| 2080 |
|
| 2081 |
if ((pThis->pszFName = malloc(iLenName + 1)) == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 2082 |
|
| 2083 |
memcpy(pThis->pszFName, pszName, iLenName + 1); /* always think about the \0! */ |
| 2084 |
pThis->lenFName = iLenName; |
| 2085 |
|
| 2086 |
finalize_it: |
| 2087 |
RETiRet; |
| 2088 |
} |
| 2089 |
|
| 2090 |
|
| 2091 |
/* set the stream's directory |
| 2092 |
* The passed-in string is duplicated. So if the caller does not need |
| 2093 |
* it any longer, it must free it. |
| 2094 |
* rgerhards, 2008-01-09 |
| 2095 |
*/ |
| 2096 |
static rsRetVal strmSetDir(strm_t *pThis, uchar *pszDir, size_t iLenDir) { |
| 2097 |
DEFiRet; |
| 2098 |
|
| 2099 |
assert(pThis != NULL); |
| 2100 |
assert(pszDir != NULL); |
| 2101 |
|
| 2102 |
if (iLenDir < 1) ABORT_FINALIZE(RS_RET_FILE_PREFIX_MISSING); |
| 2103 |
|
| 2104 |
CHKmalloc(pThis->pszDir = malloc(iLenDir + 1)); |
| 2105 |
|
| 2106 |
memcpy(pThis->pszDir, pszDir, iLenDir + 1); /* always think about the \0! */ |
| 2107 |
pThis->lenDir = iLenDir; |
| 2108 |
|
| 2109 |
finalize_it: |
| 2110 |
RETiRet; |
| 2111 |
} |
| 2112 |
|
| 2113 |
|
| 2114 |
/* support for data records |
| 2115 |
* The stream class is able to write to multiple files. However, there are |
| 2116 |
* situation (actually quite common), where a single data record should not |
| 2117 |
* be split across files. This may be problematic if multiple stream write |
| 2118 |
* calls are used to create the record. To support that, we provide the |
| 2119 |
* bInRecord status variable. If it is set, no file spliting occurs. Once |
| 2120 |
* it is set to 0, a check is done if a split is necessary and it then |
| 2121 |
* happens. For a record-oriented caller, the proper sequence is: |
| 2122 |
* |
| 2123 |
* strmRecordBegin() |
| 2124 |
* strmWrite...() |
| 2125 |
* strmRecordEnd() |
| 2126 |
* |
| 2127 |
* Please note that records do not affect the writing of output buffers. They |
| 2128 |
* are always written when full. The only thing affected is circular files |
| 2129 |
* creation. So it is safe to write large records. |
| 2130 |
* |
| 2131 |
* IMPORTANT: RecordBegin() can not be nested! It is a programming error |
| 2132 |
* if RecordBegin() is called while already in a record! |
| 2133 |
* |
| 2134 |
* rgerhards, 2008-01-10 |
| 2135 |
*/ |
| 2136 |
static rsRetVal strmRecordBegin(strm_t *pThis) { |
| 2137 |
assert(pThis != NULL); |
| 2138 |
assert(pThis->bInRecord == 0); |
| 2139 |
pThis->bInRecord = 1; |
| 2140 |
return RS_RET_OK; |
| 2141 |
} |
| 2142 |
|
| 2143 |
static rsRetVal strmRecordEnd(strm_t *pThis) { |
| 2144 |
DEFiRet; |
| 2145 |
assert(pThis != NULL); |
| 2146 |
assert(pThis->bInRecord == 1); |
| 2147 |
|
| 2148 |
pThis->bInRecord = 0; |
| 2149 |
iRet = strmCheckNextOutputFile(pThis); /* check if we need to switch files */ |
| 2150 |
|
| 2151 |
RETiRet; |
| 2152 |
} |
| 2153 |
/* end stream record support functions */ |
| 2154 |
|
| 2155 |
|
| 2156 |
/* This method serializes a stream object. That means the whole |
| 2157 |
* object is modified into text form. That text form is suitable for |
| 2158 |
* later reconstruction of the object. |
| 2159 |
* The most common use case for this method is the creation of an |
| 2160 |
* on-disk representation of the message object. |
| 2161 |
* We do not serialize the dynamic properties. |
| 2162 |
* rgerhards, 2008-01-10 |
| 2163 |
*/ |
| 2164 |
static rsRetVal strmSerialize(strm_t *pThis, strm_t *pStrm) { |
| 2165 |
DEFiRet; |
| 2166 |
int i; |
| 2167 |
int64 l; |
| 2168 |
|
| 2169 |
ISOBJ_TYPE_assert(pThis, strm); |
| 2170 |
ISOBJ_TYPE_assert(pStrm, strm); |
| 2171 |
|
| 2172 |
strmFlushInternal(pThis, 0); |
| 2173 |
CHKiRet(obj.BeginSerialize(pStrm, (obj_t *)pThis)); |
| 2174 |
|
| 2175 |
objSerializeSCALAR(pStrm, iCurrFNum, INT); /* implicit cast is OK for persistance */ |
| 2176 |
objSerializePTR(pStrm, pszFName, PSZ); |
| 2177 |
objSerializeSCALAR(pStrm, iMaxFiles, INT); |
| 2178 |
objSerializeSCALAR(pStrm, bDeleteOnClose, INT); |
| 2179 |
|
| 2180 |
i = pThis->sType; |
| 2181 |
objSerializeSCALAR_VAR(pStrm, sType, INT, i); |
| 2182 |
|
| 2183 |
i = pThis->tOperationsMode; |
| 2184 |
objSerializeSCALAR_VAR(pStrm, tOperationsMode, INT, i); |
| 2185 |
|
| 2186 |
i = pThis->tOpenMode; |
| 2187 |
objSerializeSCALAR_VAR(pStrm, tOpenMode, INT, i); |
| 2188 |
|
| 2189 |
l = pThis->iCurrOffs; |
| 2190 |
objSerializeSCALAR_VAR(pStrm, iCurrOffs, INT64, l); |
| 2191 |
|
| 2192 |
l = pThis->inode; |
| 2193 |
objSerializeSCALAR_VAR(pStrm, inode, INT64, l); |
| 2194 |
|
| 2195 |
l = pThis->strtOffs; |
| 2196 |
objSerializeSCALAR_VAR(pStrm, strtOffs, INT64, l); |
| 2197 |
|
| 2198 |
dbgprintf("strmSerialize: pThis->prevLineSegment %p\n", pThis->prevLineSegment); |
| 2199 |
if (pThis->prevLineSegment != NULL) { |
| 2200 |
cstrFinalize(pThis->prevLineSegment); |
| 2201 |
objSerializePTR(pStrm, prevLineSegment, CSTR); |
| 2202 |
} |
| 2203 |
|
| 2204 |
if (pThis->prevMsgSegment != NULL) { |
| 2205 |
cstrFinalize(pThis->prevMsgSegment); |
| 2206 |
objSerializePTR(pStrm, prevMsgSegment, CSTR); |
| 2207 |
} |
| 2208 |
|
| 2209 |
i = pThis->bPrevWasNL; |
| 2210 |
objSerializeSCALAR_VAR(pStrm, bPrevWasNL, INT, i); |
| 2211 |
|
| 2212 |
CHKiRet(obj.EndSerialize(pStrm)); |
| 2213 |
|
| 2214 |
finalize_it: |
| 2215 |
RETiRet; |
| 2216 |
} |
| 2217 |
|
| 2218 |
|
| 2219 |
/* duplicate a stream object excluding dynamic properties. This function is |
| 2220 |
* primarily meant to provide a duplicate that later on can be used to access |
| 2221 |
* the data. This is needed, for example, for a restart of the disk queue. |
| 2222 |
* Note that ConstructFinalize() is NOT called. So our caller may change some |
| 2223 |
* properties before finalizing things. |
| 2224 |
* rgerhards, 2009-05-26 |
| 2225 |
*/ |
| 2226 |
static rsRetVal strmDup(strm_t *const pThis, strm_t **ppNew) { |
| 2227 |
strm_t *pNew = NULL; |
| 2228 |
DEFiRet; |
| 2229 |
|
| 2230 |
ISOBJ_TYPE_assert(pThis, strm); |
| 2231 |
assert(ppNew != NULL); |
| 2232 |
|
| 2233 |
CHKiRet(strmConstruct(&pNew)); |
| 2234 |
pNew->sType = pThis->sType; |
| 2235 |
pNew->iCurrFNum = pThis->iCurrFNum; |
| 2236 |
CHKmalloc(pNew->pszFName = ustrdup(pThis->pszFName)); |
| 2237 |
pNew->lenFName = pThis->lenFName; |
| 2238 |
CHKmalloc(pNew->pszDir = ustrdup(pThis->pszDir)); |
| 2239 |
pNew->lenDir = pThis->lenDir; |
| 2240 |
pNew->tOperationsMode = pThis->tOperationsMode; |
| 2241 |
pNew->tOpenMode = pThis->tOpenMode; |
| 2242 |
pNew->compressionDriver = pThis->compressionDriver; |
| 2243 |
pNew->iMaxFileSize = pThis->iMaxFileSize; |
| 2244 |
pNew->iMaxFiles = pThis->iMaxFiles; |
| 2245 |
pNew->iFileNumDigits = pThis->iFileNumDigits; |
| 2246 |
pNew->bDeleteOnClose = pThis->bDeleteOnClose; |
| 2247 |
pNew->iCurrOffs = pThis->iCurrOffs; |
| 2248 |
|
| 2249 |
*ppNew = pNew; |
| 2250 |
pNew = NULL; |
| 2251 |
|
| 2252 |
finalize_it: |
| 2253 |
if (pNew != NULL) strmDestruct(&pNew); |
| 2254 |
|
| 2255 |
RETiRet; |
| 2256 |
} |
| 2257 |
|
| 2258 |
|
| 2259 |
static rsRetVal SetCompressionWorkers(strm_t *const pThis, int num_wrkrs) { |
| 2260 |
ISOBJ_TYPE_assert(pThis, strm); |
| 2261 |
if (num_wrkrs < 0) num_wrkrs = 1; |
| 2262 |
pThis->zstd.num_wrkrs = num_wrkrs; |
| 2263 |
return RS_RET_OK; |
| 2264 |
} |
| 2265 |
|
| 2266 |
|
| 2267 |
/* set a user write-counter. This counter is initialized to zero and |
| 2268 |
* receives the number of bytes written. It is accurate only after a |
| 2269 |
* flush(). This hook is provided as a means to control disk size usage. |
| 2270 |
* The pointer must be valid at all times (so if it is on the stack, be sure |
| 2271 |
* to remove it when you exit the function). Pointers are removed by |
| 2272 |
* calling strmSetWCntr() with a NULL param. Only one pointer is settable, |
| 2273 |
* any new set overwrites the previous one. |
| 2274 |
* rgerhards, 2008-02-27 |
| 2275 |
*/ |
| 2276 |
static rsRetVal strmSetWCntr(strm_t *pThis, number_t *pWCnt) { |
| 2277 |
DEFiRet; |
| 2278 |
|
| 2279 |
ISOBJ_TYPE_assert(pThis, strm); |
| 2280 |
|
| 2281 |
if (pWCnt != NULL) *pWCnt = 0; |
| 2282 |
pThis->pUsrWCntr = pWCnt; |
| 2283 |
|
| 2284 |
RETiRet; |
| 2285 |
} |
| 2286 |
|
| 2287 |
|
| 2288 |
#include "stringbuf.h" |
| 2289 |
|
| 2290 |
/* This function can be used as a generic way to set properties. |
| 2291 |
* rgerhards, 2008-01-11 |
| 2292 |
*/ |
| 2293 |
#define isProp(name) !rsCStrSzStrCmp(pProp->pcsName, UCHAR_CONSTANT(name), sizeof(name) - 1) |
| 2294 |
static rsRetVal strmSetProperty(strm_t *pThis, var_t *pProp) { |
| 2295 |
DEFiRet; |
| 2296 |
|
| 2297 |
ISOBJ_TYPE_assert(pThis, strm); |
| 2298 |
assert(pProp != NULL); |
| 2299 |
|
| 2300 |
if (isProp("sType")) { |
| 2301 |
CHKiRet(strmSetsType(pThis, (strmType_t)pProp->val.num)); |
| 2302 |
} else if (isProp("iCurrFNum")) { |
| 2303 |
pThis->iCurrFNum = (unsigned)pProp->val.num; |
| 2304 |
} else if (isProp("pszFName")) { |
| 2305 |
CHKiRet(strmSetFName(pThis, rsCStrGetSzStrNoNULL(pProp->val.pStr), rsCStrLen(pProp->val.pStr))); |
| 2306 |
} else if (isProp("tOperationsMode")) { |
| 2307 |
CHKiRet(strmSettOperationsMode(pThis, pProp->val.num)); |
| 2308 |
} else if (isProp("tOpenMode")) { |
| 2309 |
CHKiRet(strmSettOpenMode(pThis, pProp->val.num)); |
| 2310 |
} else if (isProp("iCurrOffs")) { |
| 2311 |
pThis->iCurrOffs = pProp->val.num; |
| 2312 |
} else if (isProp("inode")) { |
| 2313 |
pThis->inode = (ino_t)pProp->val.num; |
| 2314 |
} else if (isProp("strtOffs")) { |
| 2315 |
pThis->strtOffs = pProp->val.num; |
| 2316 |
} else if (isProp("iMaxFileSize")) { |
| 2317 |
CHKiRet(strmSetiMaxFileSize(pThis, pProp->val.num)); |
| 2318 |
} else if (isProp("fileNotFoundError")) { |
| 2319 |
CHKiRet(strmSetFileNotFoundError(pThis, pProp->val.num)); |
| 2320 |
} else if (isProp("iMaxFiles")) { |
| 2321 |
CHKiRet(strmSetiMaxFiles(pThis, pProp->val.num)); |
| 2322 |
} else if (isProp("iFileNumDigits")) { |
| 2323 |
CHKiRet(strmSetiFileNumDigits(pThis, pProp->val.num)); |
| 2324 |
} else if (isProp("bDeleteOnClose")) { |
| 2325 |
CHKiRet(strmSetbDeleteOnClose(pThis, pProp->val.num)); |
| 2326 |
} else if (isProp("prevLineSegment")) { |
| 2327 |
CHKiRet(rsCStrConstructFromCStr(&pThis->prevLineSegment, pProp->val.pStr)); |
| 2328 |
} else if (isProp("prevMsgSegment")) { |
| 2329 |
CHKiRet(rsCStrConstructFromCStr(&pThis->prevMsgSegment, pProp->val.pStr)); |
| 2330 |
} else if (isProp("bPrevWasNL")) { |
| 2331 |
pThis->bPrevWasNL = (sbool)pProp->val.num; |
| 2332 |
} |
| 2333 |
|
| 2334 |
finalize_it: |
| 2335 |
RETiRet; |
| 2336 |
} |
| 2337 |
#undef isProp |
| 2338 |
|
| 2339 |
|
| 2340 |
/* return the current offset inside the stream. Note that on two consequtive calls, the offset |
| 2341 |
* reported on the second call may actually be lower than on the first call. This is due to |
| 2342 |
* file circulation. A caller must deal with that. -- rgerhards, 2008-01-30 |
| 2343 |
*/ |
| 2344 |
static rsRetVal strmGetCurrOffset(strm_t *pThis, int64 *pOffs) { |
| 2345 |
DEFiRet; |
| 2346 |
|
| 2347 |
ISOBJ_TYPE_assert(pThis, strm); |
| 2348 |
assert(pOffs != NULL); |
| 2349 |
|
| 2350 |
*pOffs = pThis->iCurrOffs; |
| 2351 |
|
| 2352 |
RETiRet; |
| 2353 |
} |
| 2354 |
|
| 2355 |
static rsRetVal strmSync(strm_t *pStrmDest, const strm_t *pStrmSrc) { |
| 2356 |
DEFiRet; |
| 2357 |
|
| 2358 |
ISOBJ_TYPE_assert(pStrmDest, strm); |
| 2359 |
ISOBJ_TYPE_assert(pStrmSrc, strm); |
| 2360 |
|
| 2361 |
pStrmDest->iCurrFNum = pStrmSrc->iCurrFNum; |
| 2362 |
pStrmDest->iCurrOffs = pStrmSrc->iCurrOffs; |
| 2363 |
pStrmDest->strtOffs = pStrmSrc->strtOffs; |
| 2364 |
|
| 2365 |
RETiRet; |
| 2366 |
} |
| 2367 |
|
| 2368 |
|
| 2369 |
/* queryInterface function |
| 2370 |
* rgerhards, 2008-02-29 |
| 2371 |
*/ |
| 2372 |
BEGINobjQueryInterface(strm) |
| 2373 |
CODESTARTobjQueryInterface(strm); |
| 2374 |
if (pIf->ifVersion != strmCURR_IF_VERSION) { /* check for current version, increment on each change */ |
| 2375 |
ABORT_FINALIZE(RS_RET_INTERFACE_NOT_SUPPORTED); |
| 2376 |
} |
| 2377 |
|
| 2378 |
/* ok, we have the right interface, so let's fill it |
| 2379 |
* Please note that we may also do some backwards-compatibility |
| 2380 |
* work here (if we can support an older interface version - that, |
| 2381 |
* of course, also affects the "if" above). |
| 2382 |
*/ |
| 2383 |
pIf->Construct = strmConstruct; |
| 2384 |
pIf->ConstructFinalize = strmConstructFinalize; |
| 2385 |
pIf->Destruct = strmDestruct; |
| 2386 |
pIf->ReadChar = strmReadChar; |
| 2387 |
pIf->UnreadChar = strmUnreadChar; |
| 2388 |
pIf->ReadLine = strmReadLine; |
| 2389 |
pIf->SeekCurrOffs = strmSeekCurrOffs; |
| 2390 |
pIf->Write = strmWrite; |
| 2391 |
pIf->WriteChar = strmWriteChar; |
| 2392 |
pIf->WriteLong = strmWriteLong; |
| 2393 |
pIf->SetFName = strmSetFName; |
| 2394 |
pIf->SetFileNotFoundError = strmSetFileNotFoundError; |
| 2395 |
pIf->SetDir = strmSetDir; |
| 2396 |
pIf->Flush = strmFlush; |
| 2397 |
pIf->RecordBegin = strmRecordBegin; |
| 2398 |
pIf->RecordEnd = strmRecordEnd; |
| 2399 |
pIf->Serialize = strmSerialize; |
| 2400 |
pIf->GetCurrOffset = strmGetCurrOffset; |
| 2401 |
pIf->Dup = strmDup; |
| 2402 |
pIf->Sync = strmSync; |
| 2403 |
pIf->SetCompressionWorkers = SetCompressionWorkers; |
| 2404 |
pIf->SetWCntr = strmSetWCntr; |
| 2405 |
pIf->CheckFileChange = CheckFileChange; |
| 2406 |
/* set methods */ |
| 2407 |
pIf->SetbDeleteOnClose = strmSetbDeleteOnClose; |
| 2408 |
pIf->SetiMaxFileSize = strmSetiMaxFileSize; |
| 2409 |
pIf->SetiMaxFiles = strmSetiMaxFiles; |
| 2410 |
pIf->SetiFileNumDigits = strmSetiFileNumDigits; |
| 2411 |
pIf->SettOperationsMode = strmSettOperationsMode; |
| 2412 |
pIf->SettOpenMode = strmSettOpenMode; |
| 2413 |
pIf->SetcompressionDriver = strmSetcompressionDriver; |
| 2414 |
pIf->SetsType = strmSetsType; |
| 2415 |
pIf->SetiZipLevel = strmSetiZipLevel; |
| 2416 |
pIf->SetbVeryReliableZip = strmSetbVeryReliableZip; |
| 2417 |
pIf->SetbSync = strmSetbSync; |
| 2418 |
pIf->SetbReopenOnTruncate = strmSetbReopenOnTruncate; |
| 2419 |
pIf->SetsIOBufSize = strmSetsIOBufSize; |
| 2420 |
pIf->SetiSizeLimit = strmSetiSizeLimit; |
| 2421 |
pIf->SetiFlushInterval = strmSetiFlushInterval; |
| 2422 |
pIf->SetpszSizeLimitCmd = strmSetpszSizeLimitCmd; |
| 2423 |
pIf->SetbSizeLimitCmdPassFileName = strmSetbSizeLimitCmdPassFileName; |
| 2424 |
pIf->Setcryprov = strmSetcryprov; |
| 2425 |
pIf->SetcryprovData = strmSetcryprovData; |
| 2426 |
finalize_it: |
| 2427 |
ENDobjQueryInterface(strm) |
| 2428 |
|
| 2429 |
|
| 2430 |
/* Initialize the stream class. Must be called as the very first method |
| 2431 |
* before anything else is called inside this class. |
| 2432 |
* rgerhards, 2008-01-09 |
| 2433 |
*/ |
| 2434 |
BEGINObjClassInit(strm, 1, OBJ_IS_CORE_MODULE) |
| 2435 |
/* request objects we use */ |
| 2436 |
|
| 2437 |
OBJSetMethodHandler(objMethod_SERIALIZE, strmSerialize); |
| 2438 |
OBJSetMethodHandler(objMethod_SETPROPERTY, strmSetProperty); |
| 2439 |
OBJSetMethodHandler(objMethod_CONSTRUCTION_FINALIZER, strmConstructFinalize); |
| 2440 |
ENDObjClassInit(strm) |