mirror of
https://gitlab.isc.org/isc-projects/bind9
synced 2025-09-03 16:15:27 +00:00
use clock_gettime() instead of gettimeofday() for isc_itme functions
This commit is contained in:
committed by
Evan Hunt
parent
a85a65f96e
commit
33bf90331b
@@ -36,15 +36,10 @@
|
|||||||
#define NS_PER_MS 1000000 /*%< Nanoseconds per millisecond. */
|
#define NS_PER_MS 1000000 /*%< Nanoseconds per millisecond. */
|
||||||
#define US_PER_S 1000000 /*%< Microseconds per second. */
|
#define US_PER_S 1000000 /*%< Microseconds per second. */
|
||||||
|
|
||||||
/*
|
#ifdef CLOCK_REALTIME_COARSE
|
||||||
* All of the INSIST()s checks of nanoseconds < NS_PER_S are for
|
#define CLOCKSOURCE CLOCK_REALTIME_COARSE
|
||||||
* consistency checking of the type. In lieu of magic numbers, it
|
#else
|
||||||
* is the best we've got. The check is only performed on functions which
|
#define CLOCKSOURCE CLOCK_REALTIME
|
||||||
* need an initialized type.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef ISC_FIX_TV_USEC
|
|
||||||
#define ISC_FIX_TV_USEC 1
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*%
|
/*%
|
||||||
@@ -54,32 +49,6 @@
|
|||||||
static const isc_interval_t zero_interval = { 0, 0 };
|
static const isc_interval_t zero_interval = { 0, 0 };
|
||||||
const isc_interval_t * const isc_interval_zero = &zero_interval;
|
const isc_interval_t * const isc_interval_zero = &zero_interval;
|
||||||
|
|
||||||
#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 was are called from the logging functions.
|
|
||||||
*/
|
|
||||||
if (fixed)
|
|
||||||
(void)syslog(LOG_ERR, "gettimeofday returned bad tv_usec: corrected");
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void
|
void
|
||||||
isc_interval_set(isc_interval_t *i,
|
isc_interval_set(isc_interval_t *i,
|
||||||
unsigned int seconds, unsigned int nanoseconds)
|
unsigned int seconds, unsigned int nanoseconds)
|
||||||
@@ -141,76 +110,52 @@ isc_time_isepoch(const isc_time_t *t) {
|
|||||||
|
|
||||||
isc_result_t
|
isc_result_t
|
||||||
isc_time_now(isc_time_t *t) {
|
isc_time_now(isc_time_t *t) {
|
||||||
struct timeval tv;
|
struct timespec ts;
|
||||||
char strbuf[ISC_STRERRORSIZE];
|
char strbuf[ISC_STRERRORSIZE];
|
||||||
|
|
||||||
REQUIRE(t != NULL);
|
REQUIRE(t != NULL);
|
||||||
|
|
||||||
if (gettimeofday(&tv, NULL) == -1) {
|
if (clock_gettime(CLOCKSOURCE, &ts) == -1) {
|
||||||
strerror_r(errno, strbuf, sizeof(strbuf));
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
||||||
UNEXPECTED_ERROR(__FILE__, __LINE__, "%s", strbuf);
|
UNEXPECTED_ERROR(__FILE__, __LINE__, "%s", strbuf);
|
||||||
return (ISC_R_UNEXPECTED);
|
return (ISC_R_UNEXPECTED);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
if (ts.tv_sec < 0 || ts.tv_nsec < 0 || ts.tv_nsec >= NS_PER_S) {
|
||||||
* Does POSIX guarantee the signedness of tv_sec and tv_usec? If not,
|
|
||||||
* then this test will generate warnings for platforms on which it is
|
|
||||||
* unsigned. In any event, the chances of any of these problems
|
|
||||||
* happening are pretty much zero, but since the libisc library ensures
|
|
||||||
* certain things to be true ...
|
|
||||||
*/
|
|
||||||
#if ISC_FIX_TV_USEC
|
|
||||||
fix_tv_usec(&tv);
|
|
||||||
if (tv.tv_sec < 0)
|
|
||||||
return (ISC_R_UNEXPECTED);
|
return (ISC_R_UNEXPECTED);
|
||||||
#else
|
}
|
||||||
if (tv.tv_sec < 0 || tv.tv_usec < 0 || tv.tv_usec >= US_PER_S)
|
|
||||||
return (ISC_R_UNEXPECTED);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Ensure the tv_sec value fits in t->seconds.
|
* Ensure the tv_sec value fits in t->seconds.
|
||||||
*/
|
*/
|
||||||
if (sizeof(tv.tv_sec) > sizeof(t->seconds) &&
|
if (sizeof(ts.tv_sec) > sizeof(t->seconds) &&
|
||||||
((tv.tv_sec | (unsigned int)-1) ^ (unsigned int)-1) != 0U)
|
((ts.tv_sec | (unsigned int)-1) ^ (unsigned int)-1) != 0U)
|
||||||
return (ISC_R_RANGE);
|
return (ISC_R_RANGE);
|
||||||
|
|
||||||
t->seconds = tv.tv_sec;
|
t->seconds = ts.tv_sec;
|
||||||
t->nanoseconds = tv.tv_usec * NS_PER_US;
|
t->nanoseconds = ts.tv_nsec;
|
||||||
|
|
||||||
return (ISC_R_SUCCESS);
|
return (ISC_R_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
isc_result_t
|
isc_result_t
|
||||||
isc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i) {
|
isc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i) {
|
||||||
struct timeval tv;
|
struct timespec ts;
|
||||||
char strbuf[ISC_STRERRORSIZE];
|
char strbuf[ISC_STRERRORSIZE];
|
||||||
|
|
||||||
REQUIRE(t != NULL);
|
REQUIRE(t != NULL);
|
||||||
REQUIRE(i != NULL);
|
REQUIRE(i != NULL);
|
||||||
INSIST(i->nanoseconds < NS_PER_S);
|
INSIST(i->nanoseconds < NS_PER_S);
|
||||||
|
|
||||||
if (gettimeofday(&tv, NULL) == -1) {
|
if (clock_gettime(CLOCKSOURCE, &ts) == -1) {
|
||||||
strerror_r(errno, strbuf, sizeof(strbuf));
|
strerror_r(errno, strbuf, sizeof(strbuf));
|
||||||
UNEXPECTED_ERROR(__FILE__, __LINE__, "%s", strbuf);
|
UNEXPECTED_ERROR(__FILE__, __LINE__, "%s", strbuf);
|
||||||
return (ISC_R_UNEXPECTED);
|
return (ISC_R_UNEXPECTED);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
if (ts.tv_sec < 0 || ts.tv_nsec < 0 || ts.tv_nsec >= NS_PER_S) {
|
||||||
* Does POSIX guarantee the signedness of tv_sec and tv_usec? If not,
|
|
||||||
* then this test will generate warnings for platforms on which it is
|
|
||||||
* unsigned. In any event, the chances of any of these problems
|
|
||||||
* happening are pretty much zero, but since the libisc library ensures
|
|
||||||
* certain things to be true ...
|
|
||||||
*/
|
|
||||||
#if ISC_FIX_TV_USEC
|
|
||||||
fix_tv_usec(&tv);
|
|
||||||
if (tv.tv_sec < 0)
|
|
||||||
return (ISC_R_UNEXPECTED);
|
return (ISC_R_UNEXPECTED);
|
||||||
#else
|
}
|
||||||
if (tv.tv_sec < 0 || tv.tv_usec < 0 || tv.tv_usec >= US_PER_S)
|
|
||||||
return (ISC_R_UNEXPECTED);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Ensure the resulting seconds value fits in the size of an
|
* Ensure the resulting seconds value fits in the size of an
|
||||||
@@ -218,12 +163,12 @@ isc_time_nowplusinterval(isc_time_t *t, const isc_interval_t *i) {
|
|||||||
* note that even if both values == INT_MAX, then when added
|
* note that even if both values == INT_MAX, then when added
|
||||||
* and getting another 1 added below the result is UINT_MAX.)
|
* and getting another 1 added below the result is UINT_MAX.)
|
||||||
*/
|
*/
|
||||||
if ((tv.tv_sec > INT_MAX || i->seconds > INT_MAX) &&
|
if ((ts.tv_sec > INT_MAX || i->seconds > INT_MAX) &&
|
||||||
((long long)tv.tv_sec + i->seconds > UINT_MAX))
|
((long long)ts.tv_sec + i->seconds > UINT_MAX))
|
||||||
return (ISC_R_RANGE);
|
return (ISC_R_RANGE);
|
||||||
|
|
||||||
t->seconds = tv.tv_sec + i->seconds;
|
t->seconds = ts.tv_sec + i->seconds;
|
||||||
t->nanoseconds = tv.tv_usec * NS_PER_US + i->nanoseconds;
|
t->nanoseconds = ts.tv_nsec + i->nanoseconds;
|
||||||
if (t->nanoseconds >= NS_PER_S) {
|
if (t->nanoseconds >= NS_PER_S) {
|
||||||
t->seconds++;
|
t->seconds++;
|
||||||
t->nanoseconds -= NS_PER_S;
|
t->nanoseconds -= NS_PER_S;
|
||||||
|
Reference in New Issue
Block a user