Skip to content

Commit

Permalink
imfile: improved rotation detection messages, slightly different mode
Browse files Browse the repository at this point in the history
Rotation detection seeks backwards, what caused issues as least in one isolated
case. We try to work around this by only doing positive seeks. We also have
added diagnostic information to the warning messages rsyslog emits on
rotation detection.

see also rsyslog/rsyslog#3249
  • Loading branch information
rgerhards committed Nov 8, 2018
1 parent 0452bfd commit ddf51d4
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions runtime/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -644,13 +644,13 @@ strmHandleEOF(strm_t *const pThis)

/* helper to checkTruncation */
static rsRetVal ATTR_NONNULL()
rereadTruncated(strm_t *const pThis, const char *const reason)
rereadTruncated(strm_t *const pThis, const char *const reason, const long long data)
{
DEFiRet;

LogMsg(errno, RS_RET_FILE_TRUNCATED, LOG_WARNING, "file '%s': truncation detected, "
"(%s) - re-start reading from beginning",
pThis->pszCurrFName, reason);
"(%s) - re-start reading from beginning (data %lld)",
pThis->pszCurrFName, reason, data);
DBGPRINTF("checkTruncation, file %s last buffer CHANGED\n", pThis->pszCurrFName);
CHKiRet(strmCloseFile(pThis));
CHKiRet(strmOpenFile(pThis));
Expand All @@ -675,34 +675,48 @@ checkTruncation(strm_t *const pThis)
{
DEFiRet;
int ret;
off64_t backseek;
off64_t newpos;
assert(pThis->bReopenOnTruncate);

DBGPRINTF("checkTruncation, file %s, iBufPtrMax %zd\n", pThis->pszCurrFName, pThis->iBufPtrMax);
if(pThis->iBufPtrMax == 0) {
FINALIZE;
}

int currpos = lseek64(pThis->fd, 0, SEEK_CUR);
backseek = -1 * (off64_t) pThis->iBufPtrMax;
dbgprintf("checkTruncation in actual processing, currpos %d, backseek is %d\n", (int)currpos, (int) backseek);
ret = lseek64(pThis->fd, backseek, SEEK_CUR);
const off64_t currpos = lseek64(pThis->fd, 0, SEEK_CUR);
if(currpos < 0) {
iRet = rereadTruncated(pThis, "cannot obtain current position", 0);
FINALIZE;
}

newpos = currpos - (off64_t) pThis->iBufPtrMax;
dbgprintf("checkTruncation in actual processing, currpos %lld, newpos is %lld\n",
(long long) currpos, (long long) newpos);
if(newpos < 0) {
LogError(0, RS_RET_INTERNAL_ERROR, "program bug: file '%s': newpos < 0 --> %lld, "
"currpos %lld, iBufPtrMax %lld",
pThis->pszCurrFName, (long long) newpos, (long long) currpos,
(long long) pThis->iBufPtrMax);
FINALIZE;
}

ret = lseek64(pThis->fd, newpos, SEEK_SET);
if(ret < 0) {
iRet = rereadTruncated(pThis, "cannot seek backward to begin of last block");
iRet = rereadTruncated(pThis, "cannot seek backward to begin of last block", newpos);
FINALIZE;
}

const ssize_t lenRead = read(pThis->fd, pThis->pIOBuf_truncation, pThis->iBufPtrMax);
dbgprintf("checkTruncation proof-read: %d bytes\n", (int) lenRead);
if(lenRead < 0) {
iRet = rereadTruncated(pThis, "last block could not be re-read");
iRet = rereadTruncated(pThis, "last block could not be re-read", pThis->iBufPtrMax);
FINALIZE;
}

if(!memcmp(pThis->pIOBuf_truncation, pThis->pIOBuf, pThis->iBufPtrMax)) {
DBGPRINTF("checkTruncation, file %s last buffer unchanged\n", pThis->pszCurrFName);
} else {
iRet = rereadTruncated(pThis, "last block data different");
iRet = rereadTruncated(pThis, "last block data different", 0);
}

finalize_it:
Expand Down

0 comments on commit ddf51d4

Please sign in to comment.