]> git.ozlabs.org Git - ppp.git/blobdiff - pppd/sys-linux.c
pppd: linux: use monotonic time if possible
[ppp.git] / pppd / sys-linux.c
index 46e1ed419f4b494fb3d6f4781fdc0120ca808fab..b3959915938c19b758c38b0d267e0d2a95abce6a 100644 (file)
@@ -2167,7 +2167,6 @@ int ppp_available(void)
                }
            }
 
-           close (s);
            if (!ok) {
                slprintf(route_buffer, sizeof(route_buffer),
                         "Sorry - PPP driver version %d.%d.%d is out of date\n",
@@ -2177,6 +2176,7 @@ int ppp_available(void)
            }
        }
     }
+    close(s);
     return ok;
 }
 
@@ -2653,7 +2653,10 @@ get_pty(master_fdp, slave_fdp, slave_name, uid)
                warn("Couldn't unlock pty slave %s: %m", pty_name);
 #endif
            if ((sfd = open(pty_name, O_RDWR | O_NOCTTY)) < 0)
+           {
                warn("Couldn't open pty slave %s: %m", pty_name);
+               close(mfd);
+           }
        }
     }
 #endif /* TIOCGPTN */
@@ -2963,3 +2966,38 @@ ether_to_eui64(eui64_t *p_eui64)
     return 1;
 }
 #endif
+
+/********************************************************************
+ *
+ * get_time - Get current time, monotonic if possible.
+ */
+int
+get_time(struct timeval *tv)
+{
+/* Old glibc (< 2.3.4) does define CLOCK_MONOTONIC, but kernel may have it.
+ * Runtime checking makes it safe. */
+#ifndef CLOCK_MONOTONIC
+#define CLOCK_MONOTONIC 1
+#endif
+    static int monotonic = -1;
+    struct timespec ts;
+    int ret;
+
+    if (monotonic) {
+       ret = clock_gettime(CLOCK_MONOTONIC, &ts);
+       if (ret == 0) {
+           monotonic = 1;
+           if (tv) {
+               tv->tv_sec = ts.tv_sec;
+               tv->tv_usec = ts.tv_nsec / 1000;
+           }
+           return ret;
+       } else if (monotonic > 0)
+           return ret;
+
+       monotonic = 0;
+       warn("Couldn't use monotonic clock source: %m");
+    }
+
+    return gettimeofday(tv, NULL);
+}