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>
This commit is contained in:
Joachim Wiberg 2021-11-26 06:06:14 +01:00
parent 30a5c6628d
commit cea845aaf4

View File

@ -243,7 +243,12 @@ vsyslogp_r(int pri, struct syslog_data *data, const char *msgid,
} else } else
prlen = 0; prlen = 0;
if (gettimeofday(&tv, NULL) != -1) { if (gettimeofday(&tv, NULL) == -1) {
tv.tv_sec = time(NULL);
tv.tv_usec = 0;
}
{
/* strftime() implies tzset(), localtime_r() doesn't. */ /* strftime() implies tzset(), localtime_r() doesn't. */
tzset(); tzset();
now = (time_t) tv.tv_sec; now = (time_t) tv.tv_sec;