From: Zbigniew Jędrzejewski-Szmek Date: Mon, 21 May 2018 18:39:09 +0000 (+0200) Subject: Always allow timestamps to be printed X-Git-Tag: v239~172^2~7 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d3d280242cc9cdfaf0184860029ddd738b6fa952;p=thirdparty%2Fsystemd.git Always allow timestamps to be printed If the timestamp is above 9999-12-30, (or 2038-something-something on 32 bit), use XXXX-XX-XX XX:XX:XX as the replacement. The problem with refusing to print timestamps is that our code accepts such timestamps, so we can't really just refuse to process them afterwards. Also, it makes journal files non-portable, because suddently we might completely refuse to print entries which are totally OK on a different machine. --- diff --git a/src/basic/time-util.c b/src/basic/time-util.c index 5d278d42cb8..0601d4fa92d 100644 --- a/src/basic/time-util.c +++ b/src/basic/time-util.c @@ -282,8 +282,11 @@ static char *format_timestamp_internal( return NULL; /* Timestamp is unset */ /* Let's not format times with years > 9999 */ - if (t > USEC_TIMESTAMP_FORMATTABLE_MAX) - return NULL; + if (t > USEC_TIMESTAMP_FORMATTABLE_MAX) { + assert(l >= strlen("--- XXXX-XX-XX XX:XX:XX") + 1); + strcpy(buf, "--- XXXX-XX-XX XX:XX:XX"); + return buf; + } sec = (time_t) (t / USEC_PER_SEC); /* Round down */ diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c index 365c9130097..401f3630527 100644 --- a/src/shared/logs-show.c +++ b/src/shared/logs-show.c @@ -301,11 +301,6 @@ static int output_timestamp_realtime(FILE *f, sd_journal *j, OutputMode mode, Ou if (r < 0) return log_error_errno(r, "Failed to get realtime timestamp: %m"); - if (x > USEC_TIMESTAMP_FORMATTABLE_MAX) { - log_error("Timestamp cannot be printed"); - return -EINVAL; - } - if (IN_SET(mode, OUTPUT_SHORT_FULL, OUTPUT_WITH_UNIT)) { const char *k; @@ -314,7 +309,7 @@ static int output_timestamp_realtime(FILE *f, sd_journal *j, OutputMode mode, Ou else k = format_timestamp(buf, sizeof(buf), x); if (!k) { - log_error("Failed to format timestamp."); + log_error("Failed to format timestamp: %"PRIu64, x); return -EINVAL; } diff --git a/src/test/test-time-util.c b/src/test/test-time-util.c index 5b8e7c7b35d..ab4b7ce282f 100644 --- a/src/test/test-time-util.c +++ b/src/test/test-time-util.c @@ -287,11 +287,12 @@ static void test_format_timestamp_utc(void) { #if SIZEOF_TIME_T == 8 test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX, "Thu 9999-12-30 23:59:59 UTC"); + test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, "--- XXXX-XX-XX XX:XX:XX"); #elif SIZEOF_TIME_T == 4 test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX, "Tue 2038-01-19 03:14:07 UTC"); + test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX + 1, "--- XXXX-XX-XX XX:XX:XX"); #endif - test_format_timestamp_utc_one(USEC_TIMESTAMP_FORMATTABLE_MAX+1, NULL); test_format_timestamp_utc_one(USEC_INFINITY, NULL); }