chiark / gitweb /
time-util: add clock_boottime_or_monotonic
authorTom Gundersen <teg@jklm.no>
Thu, 24 Jul 2014 16:36:37 +0000 (18:36 +0200)
committerTom Gundersen <teg@jklm.no>
Thu, 24 Jul 2014 17:02:58 +0000 (19:02 +0200)
CLOCK_BOOTTIME is not supported by timerfd on older kernels, so for the time beeing,
use this helper instead which will fallback to CLOCK_MONOTONIC if CLOCK_BOOTTIME is
not supported.

src/shared/time-util.c
src/shared/time-util.h

index fc79c569f4bceb52450a4a7be17ea09d6b8c817b..76ab143f34990af69a5aef064de90fcc027fcb20 100644 (file)
@@ -22,6 +22,7 @@
 #include <time.h>
 #include <string.h>
 #include <sys/timex.h>
+#include <sys/timerfd.h>
 
 #include "util.h"
 #include "time-util.h"
@@ -929,3 +930,21 @@ bool timezone_is_valid(const char *name) {
 
         return true;
 }
+
+clockid_t clock_boottime_or_monotonic(void) {
+        static clockid_t clock = -1;
+        int fd;
+
+        if (clock != -1)
+                return clock;
+
+        fd = timerfd_create(CLOCK_BOOTTIME, TFD_NONBLOCK|TFD_CLOEXEC);
+        if (fd < 0)
+                clock = CLOCK_MONOTONIC;
+        else {
+                safe_close(fd);
+                clock = CLOCK_BOOTTIME;
+        }
+
+        return clock;
+}
index 792cd2748936d1cb51ce0d5ef933959b7e56ea74..69a48c686bebd4a8509df511fac6bbb05c8b77ed 100644 (file)
@@ -98,3 +98,5 @@ bool ntp_synced(void);
 
 int get_timezones(char ***l);
 bool timezone_is_valid(const char *name);
+
+clockid_t clock_boottime_or_monotonic(void);