Commit ef8a9fa3 authored by Juliusz Chroboczek's avatar Juliusz Chroboczek

More time paranoia.

parent 8abdd950
...@@ -60,17 +60,19 @@ kernel_ll_addresses(char *ifname, int ifindex, ...@@ -60,17 +60,19 @@ kernel_ll_addresses(char *ifname, int ifindex,
return j; return j;
} }
/* Like gettimeofday, but should return monotonic time. If POSIX clocks /* Like gettimeofday, but returns monotonic time. If POSIX clocks are not
are not available, falls back to gettimeofday. */ available, falls back to gettimeofday but enforces monotonicity. */
int int
gettime(struct timeval *tv) gettime(struct timeval *tv)
{ {
int rc;
static time_t offset = 0, previous = 0;
#if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 && defined(CLOCK_MONOTONIC) #if defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 && defined(CLOCK_MONOTONIC)
static int have_posix_clocks = -1; static int have_posix_clocks = -1;
if(UNLIKELY(have_posix_clocks < 0)) { if(UNLIKELY(have_posix_clocks < 0)) {
struct timespec ts; struct timespec ts;
int rc;
rc = clock_gettime(CLOCK_MONOTONIC, &ts); rc = clock_gettime(CLOCK_MONOTONIC, &ts);
if(rc < 0) { if(rc < 0) {
have_posix_clocks = 0; have_posix_clocks = 0;
...@@ -91,5 +93,14 @@ gettime(struct timeval *tv) ...@@ -91,5 +93,14 @@ gettime(struct timeval *tv)
} }
#endif #endif
return gettimeofday(tv, NULL); rc = gettimeofday(tv, NULL);
if(rc < 0)
return rc;
tv->tv_sec += offset;
if(UNLIKELY(previous > tv->tv_sec)) {
offset += previous - tv->tv_sec;
tv->tv_sec = previous;
}
previous = tv->tv_sec;
return rc;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment