240 Commits

Author SHA1 Message Date
Rainer Gerhards
38861e838a
Merge pull request #4228 from jaankit/rsyslog-stream
stream: Fix for segfault on imfile read
2020-04-28 10:48:42 +02:00
b84c9debea stream.c: Moved doSizeLimitProcessing check to strmWrite
The check was done in strmPhysWrite before which caused syslog
messages to split in the middle if the syslog message batch exceeded
the default IO Buffer size.

closes: https://github.com/rsyslog/rsyslog/issues/4233
2020-03-31 17:27:01 +02:00
Ankit Jain
ea96f4c99a stream: Fix for segfault on imfile read
- if cstrLen(pThis->prevMsgSegment) > maxMsgSize then len calculation
  become negative if cstrLen(thisLine) < cstrLen(pThis->prevMsgSegment)
  This causes illegal access to memory location and thus causing segfault.
- assigning len = 0 if cstrLen(pThis->prevMsgSegment) > maxMsgSize so that
  it access the correct memory location.

Signed-off-by: Ankit Jain <ankitja@vmware.com>
2020-03-19 12:01:53 +05:30
Rainer Gerhards
370b86133b
imfile: add new input parameter escapeLF.replacement
The new parameter permits to specify a replacement to be configured
when "escapeLF" is set to "on". Previously, a fixed replacement string
was used ("#012"/"\n") depending on circumstances. If the parameter is
set to an empty string, the LF is simply discarded.

closes https://github.com/rsyslog/rsyslog/issues/3889
2019-11-14 14:31:24 +01:00
Rainer Gerhards
9ca744c074
Merge pull request #3926 from rgerhards/i3833
enhancement: multithread disk queue
2019-10-30 13:22:59 +01:00
Rainer Gerhards
e88b956e9d
core/queue: provide ability to run diskqueue on multiple threads
see also https://github.com/rsyslog/rsyslog/issues/3543
closes https://github.com/rsyslog/rsyslog/issues/3833
2019-10-30 10:04:09 +01:00
Rainer Gerhards
19704e7095
omfile bugfix: file handle leak
The stream class does not close re-opened file descriptors.
This lead to leaking file handles and ultimately to the inability
to open any files/sockets/etc as rsyslog ran out of handles.

The bug was depending on timing. This involed different OS
thread scheduler timing as well as workload. The bug was more
common under the following conditions:

- async writing of files
- dynafiles
- not commiting file data at end of transaction

However it could be triggerred under other conditions as well.

The refactoring done in 8.1908 increased the likelyhood of
experienceing this bug. But it was not a real regression, the new
code was valid, but changed the timing so that the race was more
likely.

Thanks to Michael Biebl for reporting this bug and helping to
analyze it.

closes https://github.com/rsyslog/rsyslog/issues/3885
2019-10-24 09:23:27 +02:00
Rainer Gerhards
103a44263d
core queue bugfix: handle multi-queue-file delete correctly
Rsyslog may leave some dangling disk queue files under the following
conditions:

- batch sizes and/or messages are large
- queue files are comparatively small
- a batch spans more than two queue files (from n to n+m with m>1)

In this case, queue files n+1 to (n+m-1) are not deleted. This can
lead to problems when the queue is re-opened again. In extreme cases
this can also lead to stalled processing when the max disk space is
used up by such left-over queue files.

Using defaults this scenario is very unlikely, but it can happen,
especially when large messages are being processed.
2019-10-15 10:51:05 +02:00
Rainer Gerhards
7ad178cea3
omfile bugfix: race file when async writing is enabled
This seems to be a long-standing bug, introduced around 7 years ago.
It became more visible by properly closing files during HUP, which
was done in 8.1905.0 (and was another bugfix).

closes https://github.com/rsyslog/rsyslog/issues/3772
2019-07-31 11:36:27 +02:00
Rainer Gerhards
bb3febdf11
imfile: remove no longe needed debug info
This was originally added as aid to solve potential regressions.
But now it looks good for a while and we remove some of it as
it really is overdone.

Note: some other debug messages had already be removed, so this
closes https://github.com/rsyslog/rsyslog/issues/3046
2019-06-12 15:20:29 +02:00
Rainer Gerhards
62fbef7f72
omfile bugfix: FlushOnTXEnd does not work reliably with dynafiles
The flush was only done to the last dynafile in use at end of
transactions. Dynafiles that were also modified during the
transaction were not flushed.

Special thanks to Duy Nguyen for pointing us to the bug and
suggesting a solution.

This commit also contains a bit of cosmetic cleanup inside
the file stream class.

closes https://github.com/rsyslog/rsyslog/issues/2502
2019-04-29 15:56:41 +02:00
Rainer Gerhards
82cdd7b7de
usability: specify file name in error message (was only fd) 2019-03-20 11:32:13 +01:00
Rainer Gerhards
8d72b22981
omfile: work-around for "Bad file descriptor" errors
This works-around an issue we can reproduce e.g. via the
imtcp-tls-ossl-x509fingerprint.sh test. Here, omfile gets a write
error with reason EBADF. So far, I was not able to see an actual
coding error. However I traced this down to a multithreaded race
on open and close calls. I am very surprised to see this type
of issue, as I think the kernel guarantees that it does not happen.

Here is what I see in strace -f:
openssl accepts a socket:

[pid 66386] accept(4, {sa_family=AF_INET, sin_port=htons(59054), sin_addr=inet_addr("127.0.0.1")}, [128->16]) = 10

then, it works a bit with that socket, detects a failure and shuts it down. Sometimes, at the very same instant omfile on another thread tries to open on output file. Then the following happens:

[pid 66386] close(10)                   = 0
[pid 66389] openat(AT_FDCWD, "./rstb_356100_31fa9d20.out.log", O_WRONLY|O_CREAT|O_NOCTTY|O_APPEND|O_CLOEXEC, 0644 <unfinished ...>
[pid 66386] close(10 <unfinished ...>
[pid 66389] <... openat resumed> )      = 10
[pid 66386] <... close resumed> )       = 0
[pid 66386] poll([{fd=4, events=POLLIN}, {fd=5, events=POLLIN}], 2, -1 <unfinished ...>
[pid 66389] write(2, "file './rstb_356100_31fa9d20.out"..., 66file './rstb_356100_31fa9d20.out.log' opened as #10 with mode 420
) = 66
[pid 66389] ioctl(10, TCGETS, 0x7f59aeb89540) = -1 EBADF (Bad file descriptor)

This is **literally** from the log, without deleting or reordering
lines. I read it so that there is a race between `open` and `close`
where fd 10 is reused, but seemingly closed - resulting in the `EBADF`

While it smells like a kernel issue, it may be a well-hidden program
bug - if so, one I currently do not find. HOWEVER, this commit
works around the issue by reopening the file when we receive EBADF.
That's the best thing to do in that case, especially if it really is
a kernel bug. Data loss should not occur, as the previous writes
succeeded in that case.

The backdraw of this work-around is that it only "fixes" omfile. In
theory every part of rsyslog can be affected by this issues (queue
files, for example). So this is not to be considered a final solution
of the root issues (but a big step forward for known problem cases).

see also https://github.com/rsyslog/rsyslog/issues/3404
2019-01-09 16:24:30 +01:00
Rainer Gerhards
02e18b319d
core/stream: always invalidate fd on file close
while in theory, the fd should immediately be rewritten, in practice
we sometimes see some errors "bad file descriptor" that we cannot
explain. So we clean this up to remove a potential trouble cause.
2018-12-22 14:23:40 +01:00
Rainer Gerhards
f9101093cd imfile/stream: a bit of cleanup refactoring and a small bugfix
* truncation check did not necessarily detect if re-read of last
  block was too short (only hard errors were detected)
* consistently use correct lseek64() return type off64_t
* improve performance of rotation detection a bit
  after we fixed this code, we can go back to real backwards
  seeking what spares us one system call (which was in 8.39
  enabled for debugging purposes, so this is an overall win!).
2018-11-11 13:54:40 +01:00
Rainer Gerhards
9e520b07a5
bugfix imfile: rsyslog re-sends data for files larger 2GiB
This occurs always if and only if
- reopenOnTruncate="on" is set
- file grows over 2GiB in size

Then, the data is continously re-sent until the file becomes smaller
2GiB (due to truncation) or is deleted.

It is a regression introduced by 2d15cbc8221e385c5aa821e4a851d7498ed81850

closes https://github.com/rsyslog/rsyslog/issues/3249
2018-11-09 15:13:02 +01:00
Rainer Gerhards
ddf51d4167
imfile: improved rotation detection messages, slightly different mode
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 https://github.com/rsyslog/rsyslog/issues/3249
2018-11-08 13:13:50 +01:00
Rainer Gerhards
9bece39dc6
SQUASH
debug cleanup: remove some old, no longer used macros
2018-10-30 12:46:04 +01:00
Rainer Gerhards
7d2a3b9e5d
cleanup and small testbench plumbing improvement 2018-10-22 18:01:44 +02:00
Rainer Gerhards
f85ef7aabc
bugfix imfile: file change was not reliably detected
A change in the inode was not detected under all circumstances,
most importantly not in some logrotate cases.

Includes new tests made by Andre Lorbach. They now use the
logrotate tool natively to reproduce the issue.
2018-09-27 10:00:57 +02:00
Rainer Gerhards
8c7718d34f testbench: try to reproduce imfile "file no longer monitored" error
see also https://github.com/rsyslog/rsyslog/issues/1605
2018-09-22 15:52:57 +02:00
Rainer Gerhards
2d15cbc822 imfile: improve truncation detection
previously, truncation was only detected at end of file. Especially with
busy files that could cause loss of data and possibly also stall imfile
reading. The new code now also checks during each read. Obviously, there
is some additional overhead associated with that, but this is unavoidable.

It still is highly recommended NOT to turn on "reopenOnTruncate" in imfile.
Note that there are also inherant reliability issues. There is no way to
"fix" these, as they are caused by races between the process(es) who truncate
and rsyslog reading the file. But with the new code, the "problem window"
should be much smaller and, more importantly, imfile should not stall.

see also https://github.com/rsyslog/rsyslog/issues/2659
see also https://github.com/rsyslog/rsyslog/issues/1605
2018-09-22 15:52:38 +02:00
Rich Megginson
c902a0938f imfile: support for endmsg.regex
This adds support for endmsg.regex.  It is similar to
startmsg.regex except that it matches the line that denotes
the end of the message, rather than the start of the next message.
This is primarily for container log file use cases such as this:

    date stdout P start of message
    date stdout P  middle of message
    date stdout F  end of message

The `F` means this is the line which contains the final part of
the message.  The fully assembled message should be
`start of message middle of message end of message`.
`startmsg.regex="^[^ ]+ stdout F "` will match.
2018-09-11 20:01:47 -06:00
PascalWithopf
c83e3ed216 correct codestyle in rsyslog part 2 2018-08-01 16:11:33 +02:00
PascalWithopf
19133327cc correct codestyle in rsyslog 2018-07-31 09:44:27 +02:00
Radovan Sroka
7e88634f3d Fixed race in asyncWriter
strmFlushInternal needs to be called with locked mutex
2018-06-26 12:50:02 +02:00
Rainer Gerhards
d5cadd26d4 imfile: refactor state file format
We change it to json and also change the way it is stored and loaded.
This sets base to additional improvements in imfile.

closes https://github.com/rsyslog/rsyslog/issues/2529
2018-03-29 09:15:27 +02:00
Rainer Gerhards
193631eac7 stream/bugfix: memory leak on stream open if filename as already generated
this can happen if imfile reads a state file. On each open, memory for the
file name can be lost.

We detected this while working on imfile refactoring, there is no related
bug report. No specific test has been crafted, as the refactored imfile
tests catch it (as soon as they are merged).
2018-03-18 12:10:36 +01:00
Rainer Gerhards
c55f37ef71 bugfix/imfile: memory leak in readMode 0
Bug is actually in stream object, but currently exposed only via imfile.
Happens when in readMode 0 a partial line is read and no more data is
present in the file during that iteration. One partial message is lost
in this case.

closes https://github.com/rsyslog/rsyslog/issues/2421
2018-03-15 13:23:26 +01:00
Jan Gerhards
3db937f376 stream: use new errmsg interface
see also https://github.com/rsyslog/rsyslog/issues/1684
2018-02-05 16:24:46 +01:00
Rainer Gerhards
6a190552d2 core/cstr: stringbuf.c: cstrGetSzStrNoNULL shall not modify buffer
The currently done buffer modification (add of '\0') is bad, especially when
multiple threads access the same string. It is not really an issue that needs
to be urgently fixed, as always the same data is written. However, among others,
it will pollute the thread debugger and as such prevent more elaborate automatted
tests.

closes https://github.com/rsyslog/rsyslog/issues/1993
2017-12-31 18:30:58 +01:00
PascalWithopf
29c9fb1f9a codestyle: reduce line length to 120
lines are still checked for a length of 125 because
these are just some of the lines
2017-12-21 17:09:32 +01:00
PascalWithopf
79de51e4fc codestyle: reduce max line length to 125 2017-12-20 16:16:57 +01:00
Rainer Gerhards
a3f3d8fc6a core/file stream: do not silently ignore error condition
Detected by Coverity scan, CID 185349
2017-11-17 18:06:01 +01:00
PascalWithopf
e485c5c5bc codestyle: shorten lines to max 130 chars 2017-11-15 09:04:42 +01:00
Rainer Gerhards
dd1026c1dc file writer: improved error message
if a file cannot be opened but would need to be for the crypto provider
to work correctly, an error message is now emitted.

Root issue detected by Coverity scan, CID 185338
2017-11-07 17:35:18 +01:00
Rainer Gerhards
a71913029e file stream handler: ensure multiline read is kept valid
We ensure that the previous line segment is always valid... actually this
was already done with existing code, but Coverity scan did not detect this.
Maybe we now get a control flow issue because we do what already happened
in this case...

CID 185423
2017-11-03 19:01:13 +01:00
Rainer Gerhards
8597d15898
Merge pull request #1931 from rgerhards/coverity-8
another set of Coverity scan patches
2017-10-31 10:33:41 +01:00
Rainer Gerhards
a2101e9d52 core/file stream object bugfix: memory leak
If a multiline read errored out, a line buffer could be leaked.

Detected by Coverity scan, CID 185328
2017-10-30 18:53:21 +01:00
Rainer Gerhards
975ca5fedc core/file stream: fix potential infinite loop
Detected by Coverty scan (albeit as different issue), CID 185328
2017-10-29 19:13:04 +01:00
Rainer Gerhards
7933210f7c cosmetic: ensure strmOpenFile() has opened file when returning RS_RET_OK
this tries to fix a potential false positive in Coverity scan. If this does
not work out, we need to explicitely set it to false positive.

CID 185330
2017-10-28 12:58:04 +02:00
Rainer Gerhards
fffbcc2ddc Merge pull request #1758 from rgerhards/pr-1633
implement fileoffset metadata
2017-09-12 08:54:09 +02:00
Rainer Gerhards
0858209381 imfile: adjust offset metadata to give begin offset of file 2017-09-07 17:08:21 +02:00
Rainer Gerhards
e7ace190aa queue bugfix: file write error message was incorrect
when a queue was restarted from disk file, it almost always
emitted a message claiming
"file opened for non-append write, but already contains xxx bytes"
This message was wrong and did not indicate a real error condition.
The predicate check was incorrect.
2017-09-07 08:45:57 +02:00
PascalWithopf
2f40554acb codestyle: correct too long lines 2017-06-29 09:28:21 +02:00
Rainer Gerhards
82bfcf8453 imfile: multiline timeout does not work after rsyslog restart
The timeout feature for multiline reads does not correctly work
for files for which a state file existed. This is usually the
case for files that had been processed by a previous run and
that still exist on the new start. For all other files,
especially those monitored by a wildcard and newly created after
the rsyslog start, timeout worked as expected.

closes https://github.com/rsyslog/rsyslog/issues/1445
2017-06-01 10:12:33 +02:00
PascalWithopf
8ceaddde35 imfile: add posibility to truncate msg
closes https://github.com/rsyslog/rsyslog/issues/1552
2017-05-19 12:04:02 +02:00
Rupert
a6a24abf13 portability: fixes needed to build on osx
Thanks to github user hdatma
2017-05-15 09:05:05 +02:00
PascalWithopf
a0a6e5bbba imfile: fix problem that fileNotFoundError was reported repeatedly in polling mode
fix https://github.com/rsyslog/rsyslog/issues/1534
2017-05-03 10:00:28 +02:00
PascalWithopf
3f789a4458 imfile: add error message when file is not found and parameter to disable this message 2017-05-02 10:54:56 +02:00