From 7cb0868ea8e741330cb1c37d7258e73ca47aa895 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 13 Jan 2023 19:37:12 +0100 Subject: Only use CLOCK_BOOTTIME if available and adjtimex(3) on Linux MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- timeprefix.1 | 9 +++++++++ timeprefix.c | 23 +++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/timeprefix.1 b/timeprefix.1 index 088b3ef..2e83572 100644 --- a/timeprefix.1 +++ b/timeprefix.1 @@ -18,3 +18,12 @@ can merged into one with .PP .B timeprefix also marks when the program started and terminated. +.SH BUGS +.B timeprefix +will use a clock that is supposed to be monotonic, +but is not necessarily system-wide, if +.I CLOCK_BOOTTIME +is not defined on the system. +.PP +.B timeprefix +only supports leap seconds on Linux. diff --git a/timeprefix.c b/timeprefix.c index f76e104..6be5fa6 100644 --- a/timeprefix.c +++ b/timeprefix.c @@ -1,19 +1,34 @@ /* See LICENSE file for copyright and license details. */ -#include +#ifdef __linux__ +# include +# define HAVE_ADJTIMEX +#endif #include #include #include +#ifndef CLOCK_MONOTONIC_RAW +# define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC +#endif +#ifndef CLOCK_BOOTTIME +# define CLOCK_BOOTTIME CLOCK_MONOTONIC_RAW +#endif + static void printline(const char *line) { +#ifdef HAVE_ADJTIMEX static struct timex timex; /* static to zero initialise it */ + int r; +#else + struct timespec walltime; +#endif struct timespec boottime; struct tm *utctime; - int r; clock_gettime(CLOCK_BOOTTIME, &boottime); +#ifdef HAVE_ADJTIMEX r = adjtimex(&timex); if (timex.time.tv_sec % (24 * 60 * 60) == 0) { if (r == TIME_INS) { @@ -32,6 +47,10 @@ printline(const char *line) } else { utctime = gmtime(&timex.time.tv_sec); } +#else + clock_gettime(CLOCK_REALTIME, &walltime); + utctime = gmtime(&walltime.tv_sec); +#endif printf("[%010lu.%04lu %i-%02i-%02i %02i:%02i:%02i UTC] %s", boottime.tv_sec, boottime.tv_nsec / 100000, -- cgit v1.2.3-70-g09d2