From: Roy Marples Date: Tue, 12 May 2015 08:30:21 +0000 (+0000) Subject: Replace get_monotonic usage with the standard clock_gettime. X-Git-Tag: v6.9.0~39 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b3f1735bbff5416aa5c9c6f3a9258e4f7aeaeb80;p=thirdparty%2Fdhcpcd.git Replace get_monotonic usage with the standard clock_gettime. --- diff --git a/common.c b/common.c index f930ee00..71668cb9 100644 --- a/common.c +++ b/common.c @@ -25,11 +25,6 @@ * SUCH DAMAGE. */ -#ifdef __APPLE__ -# include -# include -#endif - #include #include @@ -81,64 +76,6 @@ get_hostname(char *buf, size_t buflen, int short_hostname) return buf; } -/* Handy function to get the time. - * We only care about time advancements, not the actual time itself - * Which is why we use CLOCK_MONOTONIC, but it is not available on all - * platforms. - */ -#define NO_MONOTONIC "host does not support a monotonic clock - timing can skew" -int -get_monotonic(struct timespec *ts) -{ - -#if defined(_POSIX_MONOTONIC_CLOCK) && defined(CLOCK_MONOTONIC) - return clock_gettime(CLOCK_MONOTONIC, ts); -#elif defined(__APPLE__) - /* We can use mach kernel functions here. - * This is crap though - why can't they implement clock_gettime?*/ - static struct mach_timebase_info info = { 0, 0 }; - static double factor = 0.0; - uint64_t nano; - long rem; - - if (!posix_clock_set) { - if (mach_timebase_info(&info) == KERN_SUCCESS) { - factor = (double)info.numer / (double)info.denom; - clock_monotonic = posix_clock_set = 1; - } - } - if (clock_monotonic) { - nano = mach_absolute_time(); - if ((info.denom != 1 || info.numer != 1) && factor != 0.0) - nano *= factor; - ts->tv_sec = nano / NSEC_PER_SEC; - ts->tv_nsec = nano % NSEC_PER_SEC; - if (ts->tv_nsec < 0) { - ts->tv_sec--; - ts->tv_nsec += NSEC_PER_SEC; - } - return 0; - } -#endif - -#if 0 - /* Something above failed, so fall back to gettimeofday */ - if (!posix_clock_set) { - logger(NULL, LOG_WARNING, NO_MONOTONIC); - posix_clock_set = 1; - } -#endif - { - struct timeval tv; - if (gettimeofday(&tv, NULL) == 0) { - TIMEVAL_TO_TIMESPEC(&tv, ts); - return 0; - } - } - - return -1; -} - #if USE_LOGFILE void logger_open(struct dhcpcd_ctx *ctx) @@ -301,7 +238,7 @@ uptime(void) { struct timespec tv; - if (get_monotonic(&tv) == -1) + if (clock_gettime(CLOCK_MONOTONIC, &tv) == -1) return -1; return tv.tv_sec; } diff --git a/dhcp6.c b/dhcp6.c index 9b00b402..e59b2c73 100644 --- a/dhcp6.c +++ b/dhcp6.c @@ -2106,7 +2106,7 @@ dhcp6_validatelease(struct interface *ifp, state->renew = state->rebind = state->expire = 0; state->lowpl = ND6_INFINITE_LIFETIME; if (!acquired) { - get_monotonic(&aq); + clock_gettime(CLOCK_MONOTONIC, &aq); acquired = &aq; } nia = dhcp6_findia(ifp, m, len, sfrom, acquired); @@ -2181,7 +2181,7 @@ dhcp6_readlease(struct interface *ifp, int validate) if ((now = time(NULL)) == -1) goto ex; - get_monotonic(&acquired); + clock_gettime(CLOCK_MONOTONIC, &acquired); acquired.tv_sec -= now - st.st_mtime; /* Check to see if the lease is still valid */ diff --git a/eloop.c b/eloop.c index cc8f62d9..001a92fe 100644 --- a/eloop.c +++ b/eloop.c @@ -268,7 +268,7 @@ eloop_q_timeout_add_tv(struct eloop_ctx *ctx, int queue, struct timespec now, w; struct eloop_timeout *t, *tt = NULL; - get_monotonic(&now); + clock_gettime(CLOCK_MONOTONIC, &now); timespecadd(&now, when, &w); /* Check for time_t overflow. */ if (timespeccmp(&w, &now, <)) { @@ -465,7 +465,7 @@ eloop_init(void *ectx, void (*signal_cb)(void *, int), const int *signals) struct timespec now; /* Check we have a working monotonic clock. */ - if (get_monotonic(&now) == -1) + if (clock_gettime(CLOCK_MONOTONIC, &now) == -1) return NULL; ctx = calloc(1, sizeof(*ctx)); diff --git a/if-bsd.c b/if-bsd.c index 14ea9130..b6df0b43 100644 --- a/if-bsd.c +++ b/if-bsd.c @@ -1125,7 +1125,7 @@ if_getlifetime6(struct ipv6_addr *ia) (uint32_t)(lifetime->ia6t_expire - MIN(t, lifetime->ia6t_expire)); /* Calculate the created time */ - get_monotonic(&ia->created); + clock_gettime(CLOCK_MONOTONIC, &ia->created); ia->created.tv_sec -= lifetime->ia6t_vltime - ia->prefix_vltime; } else diff --git a/ipv6.c b/ipv6.c index 759bc1e3..b3a3a5f2 100644 --- a/ipv6.c +++ b/ipv6.c @@ -685,7 +685,7 @@ ipv6_addaddr(struct ipv6_addr *ap, const struct timespec *now) struct timespec n; if (now == NULL) { - get_monotonic(&n); + clock_gettime(CLOCK_MONOTONIC, &n); now = &n; } timespecsub(now, &ap->acquired, &n); @@ -838,7 +838,7 @@ ipv6_addaddrs(struct ipv6_addrhead *addrs) if (ap->flags & IPV6_AF_NEW) i++; if (!timespecisset(&now)) - get_monotonic(&now); + clock_gettime(CLOCK_MONOTONIC, &now); ipv6_addaddr(ap, &now); } } @@ -884,7 +884,7 @@ ipv6_freedrop_addrs(struct ipv6_addrhead *addrs, int drop, DHCPCD_EXITING) && apf) { if (!timespecisset(&now)) - get_monotonic(&now); + clock_gettime(CLOCK_MONOTONIC, &now); ipv6_addaddr(apf, &now); } if (drop == 2) @@ -1475,7 +1475,7 @@ ipv6_tempdadcallback(void *arg) ia->iface->name); return; } - get_monotonic(&tv); + clock_gettime(CLOCK_MONOTONIC, &tv); if ((ia1 = ipv6_createtempaddr(ia, &tv)) == NULL) logger(ia->iface->ctx, LOG_ERR, "ipv6_createtempaddr: %m"); @@ -1701,7 +1701,7 @@ ipv6_regentempaddr(void *arg) logger(ia->iface->ctx, LOG_DEBUG, "%s: regen temp addr %s", ia->iface->name, ia->saddr); - get_monotonic(&tv); + clock_gettime(CLOCK_MONOTONIC, &tv); ia1 = ipv6_createtempaddr(ia, &tv); if (ia1) ipv6_addaddr(ia1, &tv); diff --git a/ipv6nd.c b/ipv6nd.c index 9c3ee10a..270e858f 100644 --- a/ipv6nd.c +++ b/ipv6nd.c @@ -325,7 +325,7 @@ ipv6nd_expire(struct interface *ifp, uint32_t seconds) if (ifp->ctx->ipv6 == NULL) return; - get_monotonic(&now); + clock_gettime(CLOCK_MONOTONIC, &now); TAILQ_FOREACH(rap, ifp->ctx->ipv6->ra_routers, next) { if (rap->iface == ifp) { @@ -846,7 +846,7 @@ ipv6nd_handlera(struct dhcpcd_ctx *dctx, struct interface *ifp, rap->data_len = len; } - get_monotonic(&rap->received); + clock_gettime(CLOCK_MONOTONIC, &rap->received); rap->flags = nd_ra->nd_ra_flags_reserved; if (new_rap == 0 && rap->lifetime == 0) logger(ifp->ctx, LOG_WARNING, "%s: %s router available", @@ -1429,7 +1429,7 @@ ipv6nd_expirera(void *arg) uint8_t expired, valid, validone; ifp = arg; - get_monotonic(&now); + clock_gettime(CLOCK_MONOTONIC, &now); expired = 0; timespecclear(&next);