From: Ondřej Surý Date: Mon, 16 Mar 2020 08:58:30 +0000 (+0100) Subject: Use clock_gettime() instead of gettimeofday() for isc_stdtime function X-Git-Tag: v9.17.1~34^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=e691b89a9a1b3c238c725af07a560137c428d875;p=thirdparty%2Fbind9.git Use clock_gettime() instead of gettimeofday() for isc_stdtime function This also removes Solaris 2.8 broken gettimeofday() workaround --- diff --git a/lib/isc/unix/stdtime.c b/lib/isc/unix/stdtime.c index 33c86714af2..a68ad31b175 100644 --- a/lib/isc/unix/stdtime.c +++ b/lib/isc/unix/stdtime.c @@ -11,68 +11,41 @@ /*! \file */ +#include #include #include /* NULL */ #include /* NULL */ -#include #include +#include #include +#include #include -#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; } diff --git a/lib/isc/unix/time.c b/lib/isc/unix/time.c index 8fc1ec8c4ce..5a773d7ecdf 100644 --- a/lib/isc/unix/time.c +++ b/lib/isc/unix/time.c @@ -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