aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2023-01-13 19:37:12 +0100
committerMattias Andrée <maandree@kth.se>2023-01-13 19:37:12 +0100
commit7cb0868ea8e741330cb1c37d7258e73ca47aa895 (patch)
tree56d012c4807c9384e36c4097ad77d5180d9e8cdf
parentImprove makefile (diff)
downloadtimeprefix-7cb0868ea8e741330cb1c37d7258e73ca47aa895.tar.gz
timeprefix-7cb0868ea8e741330cb1c37d7258e73ca47aa895.tar.bz2
timeprefix-7cb0868ea8e741330cb1c37d7258e73ca47aa895.tar.xz
Only use CLOCK_BOOTTIME if available and adjtimex(3) on Linux
Signed-off-by: Mattias Andrée <maandree@kth.se>
-rw-r--r--timeprefix.19
-rw-r--r--timeprefix.c23
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 <sys/timex.h>
+#ifdef __linux__
+# include <sys/timex.h>
+# define HAVE_ADJTIMEX
+#endif
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
+#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,