]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Use clock_gettime() instead of gettimeofday() for isc_stdtime function
authorOndřej Surý <ondrej@isc.org>
Mon, 16 Mar 2020 08:58:30 +0000 (09:58 +0100)
committerOndřej Surý <ondrej@isc.org>
Wed, 18 Mar 2020 15:02:24 +0000 (16:02 +0100)
This also removes Solaris 2.8 broken gettimeofday() workaround

lib/isc/unix/stdtime.c
lib/isc/unix/time.c

index 33c86714af2a47b0e43ede1c75217c2684a52df3..a68ad31b1750ea3405688caee5f1412b143fa209 100644 (file)
 
 /*! \file */
 
+#include <errno.h>
 #include <stdbool.h>
 #include <stddef.h> /* NULL */
 #include <stdlib.h> /* NULL */
-#include <sys/time.h>
 #include <syslog.h>
+#include <time.h>
 
 #include <isc/stdtime.h>
+#include <isc/strerr.h>
 #include <isc/util.h>
 
-#ifndef ISC_FIX_TV_USEC
-#define ISC_FIX_TV_USEC 1
-#endif /* ifndef ISC_FIX_TV_USEC */
+#define NS_PER_S 1000000000 /*%< Nanoseconds per second. */
 
-#define US_PER_S 1000000
-
-#if ISC_FIX_TV_USEC
-static inline void
-fix_tv_usec(struct timeval *tv) {
-       bool fixed = false;
-
-       if (tv->tv_usec < 0) {
-               fixed = true;
-               do {
-                       tv->tv_sec -= 1;
-                       tv->tv_usec += US_PER_S;
-               } while (tv->tv_usec < 0);
-       } else if (tv->tv_usec >= US_PER_S) {
-               fixed = true;
-               do {
-                       tv->tv_sec += 1;
-                       tv->tv_usec -= US_PER_S;
-               } while (tv->tv_usec >= US_PER_S);
-       }
-       /*
-        * Call syslog directly as we are called from the logging functions.
-        */
-       if (fixed) {
-               (void)syslog(LOG_ERR, "gettimeofday returned bad tv_usec: "
-                                     "corrected");
-       }
-}
-#endif /* if ISC_FIX_TV_USEC */
+#if defined(CLOCK_REALTIME_COARSE)
+#define CLOCKSOURCE CLOCK_REALTIME_COARSE
+#elif defined(CLOCK_REALTIME_FAST)
+#define CLOCKSOURCE CLOCK_REALTIME_FAST
+#else /* if defined(CLOCK_REALTIME_COARSE) */
+#define CLOCKSOURCE CLOCK_REALTIME
+#endif /* if defined(CLOCK_REALTIME_COARSE) */
 
 void
 isc_stdtime_get(isc_stdtime_t *t) {
-       struct timeval tv;
-
-       /*
-        * Set 't' to the number of seconds since 00:00:00 UTC, January 1,
-        * 1970.
-        */
-
        REQUIRE(t != NULL);
 
-       RUNTIME_CHECK(gettimeofday(&tv, NULL) != -1);
+       struct timespec ts;
+
+       if (clock_gettime(CLOCKSOURCE, &ts) == -1) {
+               char strbuf[ISC_STRERRORSIZE];
+               strerror_r(errno, strbuf, sizeof(strbuf));
+               isc_error_fatal(__FILE__, __LINE__, "clock_gettime failed: %s",
+                               strbuf);
+       }
 
-#if ISC_FIX_TV_USEC
-       fix_tv_usec(&tv);
-       INSIST(tv.tv_usec >= 0);
-#else  /* if ISC_FIX_TV_USEC */
-       INSIST(tv.tv_usec >= 0 && tv.tv_usec < US_PER_S);
-#endif /* if ISC_FIX_TV_USEC */
+       REQUIRE(ts.tv_sec > 0 && ts.tv_nsec > 0 && ts.tv_nsec < NS_PER_S);
 
-       *t = (unsigned int)tv.tv_sec;
+       *t = (unsigned int)ts.tv_sec;
 }
index 8fc1ec8c4cea3cbd3c626681604e61e481d8ece1..5a773d7ecdf5fb2e76eaf2ac0ad93b0b6664a2c5 100644 (file)
@@ -32,7 +32,6 @@
 #define NS_PER_S  1000000000 /*%< Nanoseconds per second. */
 #define NS_PER_US 1000      /*%< Nanoseconds per microsecond. */
 #define NS_PER_MS 1000000    /*%< Nanoseconds per millisecond. */
-#define US_PER_S  1000000    /*%< Microseconds per second. */
 
 #if defined(CLOCK_REALTIME_COARSE)
 #define CLOCKSOURCE CLOCK_REALTIME_COARSE