chiark / gitweb /
Use CLOCK_MONOTONIC for all our timing needs, when possible
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 30 Mar 2020 17:12:00 +0000 (18:12 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 24 May 2020 20:43:30 +0000 (21:43 +0100)
Otherwise we can malfunction if the clock warps.

This depends on an unreleased adns feature, so the warning is
currently not actually printed.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
configure
configure.ac
resolver.c
secnet.c

index 7cf4421..d076bd3 100755 (executable)
--- a/configure
+++ b/configure
@@ -4250,9 +4250,7 @@ $as_echo "#define USE_MONOTONIC 1" >>confdefs.h
 
 else
 
-    { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: Using non-monotonic clock, due to lack of adns_if_monotonic" >&5
-$as_echo "$as_me: WARNING: Using non-monotonic clock, due to lack of adns_if_monotonic" >&2;}
-
+:
 fi
 
 
index 53810e4..aceed90 100644 (file)
@@ -140,7 +140,7 @@ fi
 AC_CHECK_DECL([adns_if_monotonic],[
     AC_DEFINE([USE_MONOTONIC],[1],[Use CLOCK_MONOTONIC and adns_if_monotonic])
 ],[
-    AC_MSG_WARN([Using non-monotonic clock, due to lack of adns_if_monotonic])
+: dnl AC_MSG_WARN([Using non-monotonic clock, due to lack of adns_if_monotonic])
 ],[AC_INCLUDES_DEFAULT
 #include <adns.h>
 ])
index 45738ea..3f9a6d5 100644 (file)
@@ -211,6 +211,9 @@ static list_t *adnsresolver_apply(closure_t *self, struct cloc loc,
     conf=dict_read_string(d,"config",False,"adns",loc);
 
     adns_initflags iflags = 0;
+#if USE_MONOTONIC
+    iflags |= adns_if_monotonic;
+#endif
 
     if (conf) {
        if (adns_init_strcfg(&st->ast, iflags, 0, conf)) {
index e15ee81..177c2ef 100644 (file)
--- a/secnet.c
+++ b/secnet.c
@@ -24,6 +24,7 @@
 #include <string.h>
 #include <getopt.h>
 #include <errno.h>
+#include <time.h>
 #include <unistd.h>
 #include <sys/socket.h>
 #include <arpa/inet.h>
@@ -356,9 +357,18 @@ static void run(void)
     int allocdfds=0, shortfall=0;
 
     do {
+#if USE_MONOTONIC
+       struct timespec ts;
+       if (clock_gettime(CLOCK_MONOTONIC, &ts)!=0) {
+           fatal_perror("main loop: clock_gettime(CLOCK_MONOTONIC,)");
+       }
+       tv_now_global.tv_sec =  ts.tv_sec;
+       tv_now_global.tv_usec = ts.tv_nsec / 1000;
+#else /* !USE_MONOTONIC */
        if (gettimeofday(&tv_now_global, NULL)!=0) {
            fatal_perror("main loop: gettimeofday");
        }
+#endif /* !USE_MONOTONIC */
        now_global=((uint64_t)tv_now_global.tv_sec*(uint64_t)1000)+
                   ((uint64_t)tv_now_global.tv_usec/(uint64_t)1000);
        idx=0;