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 7cf44213db46f41742d4d17078188055feefa231..d076bd327dd7291e3903b5f4590887ea83a5cb30 100755 (executable)
--- a/configure
+++ b/configure
@@ -4250,9 +4250,7 @@ $as_echo "#define USE_MONOTONIC 1" >>confdefs.h
 
 else
 
 
 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
 
 
 fi
 
 
index 53810e4a53eb1cbfae521db0edf8de898974b31c..aceed9014346659261b9f92ef6e36edf583d369a 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_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>
 ])
 ],[AC_INCLUDES_DEFAULT
 #include <adns.h>
 ])
index 45738eab5aa74523c2a8e66794bd87eefc52b873..3f9a6d5fb8bdc2fc9c431d61c3b7d7a513a0a124 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;
     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)) {
 
     if (conf) {
        if (adns_init_strcfg(&st->ast, iflags, 0, conf)) {
index e15ee8156f7f78e4b9ac25f63c36d7610bf55457..177c2ef893bb459c67a387d3410c9a736c82f9d4 100644 (file)
--- a/secnet.c
+++ b/secnet.c
@@ -24,6 +24,7 @@
 #include <string.h>
 #include <getopt.h>
 #include <errno.h>
 #include <string.h>
 #include <getopt.h>
 #include <errno.h>
+#include <time.h>
 #include <unistd.h>
 #include <sys/socket.h>
 #include <arpa/inet.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 {
     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");
        }
        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;
        now_global=((uint64_t)tv_now_global.tv_sec*(uint64_t)1000)+
                   ((uint64_t)tv_now_global.tv_usec/(uint64_t)1000);
        idx=0;