]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Replace get_monotonic usage with the standard clock_gettime.
authorRoy Marples <roy@marples.name>
Tue, 12 May 2015 08:30:21 +0000 (08:30 +0000)
committerRoy Marples <roy@marples.name>
Tue, 12 May 2015 08:30:21 +0000 (08:30 +0000)
common.c
dhcp6.c
eloop.c
if-bsd.c
ipv6.c
ipv6nd.c

index f930ee00c517a535d25487c5b611a3fc892fe654..71668cb9a17705bf45f0d84799aa68ea3b39114a 100644 (file)
--- a/common.c
+++ b/common.c
  * SUCH DAMAGE.
  */
 
-#ifdef __APPLE__
-#  include <mach/mach_time.h>
-#  include <mach/kern_return.h>
-#endif
-
 #include <sys/param.h>
 #include <sys/time.h>
 
@@ -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 9b00b40285228736a09fb8bc22128ce8f84ab2f8..e59b2c731100303c839bd3efb117dd313bd5bc11 100644 (file)
--- 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 cc8f62d9eb8128c3c1f3c4609b69dc8741b39206..001a92fe4d25706e70d95e016b8435f2032953f6 100644 (file)
--- 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));
index 14ea91307a0cee8d877f2ca41a6960f2b5ae796d..b6df0b43640fbe75761922263c82e2a20af427a9 100644 (file)
--- 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 759bc1e30047f809cd6826249751063d28bb264d..b3a3a5f2b0bf37213bf8099ed9e41f4202643b81 100644 (file)
--- 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);
index 9c3ee10abcfb4f2cda47a7928fa0ff1625bf2e33..270e858fbf2e32fdbaaeef4b88ecf9b6fa6eb05c 100644 (file)
--- 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);