294 Commits

Author SHA1 Message Date
Steffen Nurpmeso
949e80f150 Implement forced log file rotation upon SIGUSR2 2022-03-14 05:52:46 +01:00
Steffen Nurpmeso
7038e51a0f "notify": address Joachim Wiberg's comments (pull/45) 2022-03-12 17:22:18 +01:00
Steffen Nurpmeso
72f2faef6e Add "notify" keyword 2022-03-12 15:28:02 +01:00
Joachim Wiberg
6022d3c7d0 Fix #49: add support for -8 command line option to allow 8-bit data
This patch allows the user to disable the 8-bit data check in the log
message validator.  If you have experienced problems with logging any
unicode (utf-8) messages after v1.6, this option is for you.

The correct way to handle this is to add proper parser support for the
Unicode BOM, defined in RFC5424[1], as NetBSD syslogd does[2], search
for IS_BOM().

[1]: https://datatracker.ietf.org/doc/html/rfc5424#appendix-A.8
[2]: http://cvsweb.netbsd.org/bsdweb.cgi/src/usr.sbin/syslogd/syslogd.c?rev=1.138

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-03-07 20:57:00 +01:00
Joachim Wiberg
aceb4cddcf Add missing -H option to usage text, issue #41
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-03-05 18:37:49 +01:00
Joachim Wiberg
e4330515e8 Disable KernLog in container
No need to save seqno when we've detected being in container and have
disabled kernel logging.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-03-05 15:31:46 +01:00
Joachim Wiberg
40622ef6c7 Fix build error introduced when fixing issue #48
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-03-05 15:31:46 +01:00
Joachim Wiberg
c534556790 Auto-detect if we're running in a container and disable klogd
This patch adds a very rudimentary container check.  When one, of a
select few containers, are detected, sysklogd disables the kernel
logging -- since there's no point in logging kernel messages other
than from the host system.

Issue #48

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-02-14 00:28:28 +01:00
Joachim Wiberg
29e932008d Fix #48: add option ('-K') to disable kernel logging
This patch adds support for disabling kernel logging, opensys().  This
is in addition to the character device validation check, and primarily
for use in container use-cases -- where logging kernel is not needed.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-02-13 23:40:06 +01:00
Joachim Wiberg
49b99584a4 Verify the kernel log fifo is a proper character device
Issue #48 describes a problem with 100% CPU load in a container
use-case.  Turns out one of the issues was that /dev/kmsg was
not a proper character device.  This patch adds a very basic
check to ensure /dev/kmsg is usable.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-02-13 23:03:30 +01:00
Joachim Wiberg
e61e5abb88 Follow-up to 9856e07, rename '-K' option to '-t'
We need the '-K' option to disable kernel logging, so this option needs
to be renamed, unfortunately.  Fortunately it's not been released yet.

Issue #42

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2022-02-13 22:53:02 +01:00
Joachim Wiberg
f4f2ad365e Redo DNS lookup on failure to send to remote server
When entering the forwarding suspend timer, free any previous address
info and do a new DNS lookup when the timer elapses.  The failure to
send may be because we're using a stale IP address.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-11-27 20:21:45 +01:00
Joachim Wiberg
f516ff6023 Fix #36: retry DNS lookup of remote syslog servers with res_init()
This patch replaces the INET_SUSPEND_TIME for DNS lookup with a 5 sec
back-off to prevent DNS lookup on each message.

Also, reorder WARN() and NOTE() so they are called *after* setting the
f_type, otherwise we unleash endless recursive loops.

To avoid filling up the log with "Failed resolving ..." messages every
time we retry, we set a flag to remember we've already logged warning.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-11-27 20:18:21 +01:00
Joachim Wiberg
0a0380cbdd Minor, spellcheck comments
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-11-27 19:16:41 +01:00
Joachim Wiberg
9856e07e40 Fix #42: add option to always trust kernel timestamp
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-11-26 06:50:31 +01:00
Joachim Wiberg
e69b0fe812 Fix #43: avoid assert() on and around 19 January 2038 03:14:07 UTC
When time_t wraps around on 32-bit UNIX systems we shouldn't assert (and
cause syslogd to be continously restarted) but instead try to handle the
wraparound more gracefully.

This change, initially proposed by Raul Porancea, checks for wraparound
and allows syslogd to continue on error.  Logging with invalid date is
better than no logs at all.  Thanks Raul for tracking this one down!

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-11-26 06:21:00 +01:00
Joachim Wiberg
cea845aaf4 libsyslog: handle EOVERFLOW from gettimeofday()
Turns out that gettimeofday() can return EOVERFLOW on systems with
32-bit time_t.  This occurs when the UNIX Epoch wraps around, the
exact time is 03:14:07 UTC on 19 January 2038.

EOVERFLOW is not documented in gettimeofday(2), but instead of messing
up the entire syslog message -- causing syslogd to drop it -- we can
handle the overflow by falling back to time(NULL) (returning seconds
since start of Epoch) and rely on syslogd to, in turn, handle the
wraparound gracefully.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-11-26 06:17:59 +01:00
Joachim Wiberg
30a5c6628d Avoid NULL pointers to internal logit() function
The logit() function winds up calling vfprintf(), GLIBC is friendly
enough to check for NULL and replace segfault with "(null)", but other
C-libs may not handle it as gracefully.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-11-26 06:02:49 +01:00
Joachim Wiberg
ac9749a240 Minor, slight improvement in debug output
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-11-24 09:03:07 +01:00
Joachim Wiberg
9f6fbb3301 After initial read of /dev/kmsg, ignore kernel timestamp
The spec[1] says the /dev/kmsg timestamp is a monotonic clock and in
microseconds.  After a while you realize it's also relative to the boot
of the system, that fact was probably too obvious to be put in the spec.
However, what's *not* in the spec, and what takes a while to realize, is
that this monotonic time is *not* adjusted for suspend/resume cycles ...

On a frequently used laptop this can manifest itself as follows.  The
kernel is stuck on Nov 15, and for the life of me I cannot find any to
adjust for this offset:

    $ dmesg -T |tail -1; date
    [Mon Nov 15 01:42:08 2021] wlan0: Limiting TX power to 23 (23 - 0) dBm as advertised by 18:e8:29:55:b0:62
    Tue 23 Nov 2021 05:20:53 PM CET

Hence this patch.  After initial "emptying" of /dev/kmsg when syslogd
starts up, we raise a flag (denoting done with backlog), and after this
point we ignore the kernel's idea of time and replace it with the actual
time we have now, the same that userspace messages are logged with.

Sure, there will be occasions where there's a LOT of kernel messages to
read and we won't be able to keep track.  Yet, this patch is better than
the current state (where we log Nov 15).

[1]: https://www.kernel.org/doc/Documentation/ABI/testing/dev-kmsg

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-11-23 17:16:17 +01:00
Joachim Wiberg
c7e30c6bd0 Follow-up to eb454d7: use time(NULL) instead of weird calculus
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-11-23 17:14:31 +01:00
Joachim Wiberg
1018d4a7f4 Avoid NULL pointer to vsnprintf()
GLIBC is friendly enough to check for NULL and replace segfault with
"(null)", but other C-libs may not handle it as gracefully.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-11-22 04:35:01 +01:00
Joachim Wiberg
b0d4e4cc3f Fix #40: update docs and online help text wrt. caching of kmsg seqno
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-07-26 14:04:45 +02:00
Joachim Wiberg
e381bc3620 Fix #38: add option -C file for alt. kernel seqno cache file
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-06-30 22:39:09 +02:00
Joachim Wiberg
39ffa1b4cb Fix #34: regression in internal logging, loss of restart msg
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-06-30 22:27:58 +02:00
Joachim Wiberg
75524d1878 syslogd: minor, improve flog() internal logging a bit
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-06-30 22:27:58 +02:00
Joachim Wiberg
eed3335caa logger: fix \m -> \n, found on Slackware current
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-06-30 22:27:58 +02:00
Joachim Wiberg
fc5096f1c2 syslogd: support for extracting user level messages from /dev/kmsg
It is well established practise on Linux to use /dev/kmsg (old or
new API) before syslogd is up (and /dev/log exists).  This patch
enables support for extracting non-kernel log messages and logging
them with their proper facility and priority.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-06-21 23:15:10 +02:00
Joachim Wiberg
7ce44f5b9e syslogd: ignore EINVAL from kernel, silently restart polling
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-06-21 23:14:57 +02:00
Joachim Wiberg
36778bee4a logger: support for -k, early log to /dev/kmsg
This patch adds support for logging to /dev/kmsg, which can be highly
useful for early scripts that run long before syslogd has started and
/dev/log is available.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-06-21 23:11:54 +02:00
Joachim Wiberg
10ccdc3da1 logger: only attempt logrotate if target is file
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-06-20 09:10:26 +02:00
Joachim Wiberg
e3d6ff8737 Check boundary in every step of parse_rfc5424()
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-05-06 11:59:33 +02:00
Joachim Wiberg
d1f1702277 Check boundary first in parse_rfc5424()
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-05-06 10:09:34 +02:00
Joachim Wiberg
ebced817a7 Ensure argument to printsys() is always nul terminated
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-05-06 10:03:01 +02:00
Joachim Wiberg
52fc3f7176 Ensure received data is nul terminated, found by Coverity Scan
Coverity found two possible untrusted loop bounds, in unix_cb() and
inet_cb(), that were indeed possibly unterminated strings.  These
were classified as medium.  A third finding, marked high, was found
in kernel_cb(), which upon further investigation seems bogus.

This patch terminates the buffers received in unix_cb() and inet_cb()
but only changes to 0 from \0 termination in kernel_cb().

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-05-06 09:42:45 +02:00
Joachim Wiberg
65ceec1171 Fix GCC warning: integer constant is so large that it is unsigned
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-05-06 08:55:26 +02:00
Joachim Wiberg
e5ee2446a0 Follow-up to 92a4fb3: allow kernel log dupes around edge of seqno
Although hihgly unlikely, if the kernel log sequence number (seqno)
reaches the end of its MAX value (18446744073709551615) we allow for
dupes to handle the wrap-around back to zero (0) in the counter.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-05-06 08:47:30 +02:00
Joachim Wiberg
92a4fb3318 Fix #29: prevent repeating kernel messages when syslogd is restarted
This patch fixes the problem with kernel messages being repeated when
syslogd is restarted at runtime.  This is achieved by caching the last
seqno read from /dev/kmsg to /run/syslogd.cache.  The latter is usually
a ram disk these days so it should be a fairly quick op.

Excessive updates are prevented by only caching after handling all
callbacks in the socket_poll() loop, and only updating the cache
if there has been any new kernel messages since last update.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-05-05 05:17:15 +02:00
Joachim Wiberg
1de66a04c0 logger: minor, staticify
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-05-05 04:52:48 +02:00
Joachim Wiberg
eb454d7d37 Fix #28: log messages stuck in 1969
The timer_now() API, introduced in 2019, returns time relative to boot.
Useful for relative time comparisons, but when used for absolute time,
e.g. for log messages, it must be offset with boot_time.

This patch fixes issue #28, but also wall messages, which exhibits the
same problem.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-05-05 03:25:22 +02:00
Joachim Wiberg
4ab8bc7b40 Fix #31: invalid time for kernel log messages on 32-bit machines
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-05-05 03:04:52 +02:00
Joachim Wiberg
d722584191 Fix #32: fix logging of remote kernel messages being mapped to uucp
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-05-04 10:45:13 +02:00
Joachim Wiberg
2731591709 syslogd: Fix spelling error found by lintian
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-02-21 15:50:13 +01:00
Joachim Wiberg
d00c9dac74 Minor, refactor in preparation for checking kmesg seqno
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-02-21 13:05:26 +01:00
Joachim Wiberg
0901310226 Load or reload timezone data on init/SIGHUP
This should fix any lingering issues with logging with the wrong
timezone at boot.  As long as syslogd gets HUP'ed after setting
the new timezone.

Improvements to this welcome, of course.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-02-21 11:47:42 +01:00
Joachim Wiberg
f54c1d1f30 Fix issue with parsing /dev/kmsg time, off by one error
Problem and proposed fix reported by opty on #troglobit at freenode.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-02-21 11:46:09 +01:00
Joachim Wiberg
d2444c720f Fix #27: bug in /dev/kmsg priority parser, intrdocued in v2.2.0
This patch fixes a bug in the kernel log priority parser introduced in
v2.2.0 with the new support for /dev/kmsg, replacing /proc/kmsg which
has another format for the log priority.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-01-26 22:04:49 +01:00
Joachim Wiberg
406e4299a1 logger: No need for appending error message, err() already does this
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-01-25 00:13:06 +01:00
Joachim Wiberg
ea2565ce4e Drop %m gnuism from internal log macro
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-01-25 00:13:06 +01:00
Joachim Wiberg
3e4000b25a Fix #26: handle Linux EPIPE on /dev/kmsg
When Linux CONFIG_LOG_BUF_SHIFT is set too low, or too many messages are
generated by the kernel, /dev/kmsg will overflow.  This is signaled with
EPIPE to userspace.  We can use the seqnos to figure out how many we've
lost, but seqnos are currently ignored.

> In case records get overwritten while /dev/kmsg is held open, or
> records get faster overwritten than they are read, the next read()
> will return -EPIPE and the current reading position gets updated to
> the next available record. The passed sequence numbers allow the log
> consumer to calculate the amount of lost messages.

-- https://lwn.net/Articles/490690/

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
2021-01-25 00:12:29 +01:00