]> git.ozlabs.org Git - ppp.git/commitdiff
Merge branch 'monotonic-time' of https://github.com/themiron/ppp
authorPaul Mackerras <paulus@ozlabs.org>
Mon, 25 May 2020 03:26:55 +0000 (13:26 +1000)
committerPaul Mackerras <paulus@ozlabs.org>
Mon, 25 May 2020 03:26:55 +0000 (13:26 +1000)
Signed-off-by: Paul Mackerras <paulus@ozlabs.org>
pppd/Makefile.linux
pppd/main.c
pppd/plugins/radius/buildreq.c
pppd/plugins/rp-pppoe/discovery.c
pppd/pppd.h
pppd/sys-linux.c
pppd/sys-solaris.c
pppd/tty.c

index 853b15c14be42703aa5ff203da287ded9209794a..a22dcfab9d74f9658a1522bada021a41249aa90d 100644 (file)
@@ -33,7 +33,7 @@ endif
 # CC = gcc
 #
 COPTS = -O2 -pipe -Wall -g
-LIBS =
+LIBS = -lrt
 
 # Uncomment the next line to include support for Microsoft's
 # MS-CHAP authentication protocol.  Also, edit plugins/radius/Makefile.linux.
index 652240cc6868bf13b4404be886beff2231263cd0..c18ea510582adc7f9365ad2497a7d9a39c4a2072 100644 (file)
@@ -520,7 +520,7 @@ main(argc, argv)
            info("Starting link");
        }
 
-       gettimeofday(&start_time, NULL);
+       get_time(&start_time);
        script_unsetenv("CONNECT_TIME");
        script_unsetenv("BYTES_SENT");
        script_unsetenv("BYTES_RCVD");
@@ -1228,7 +1228,7 @@ reset_link_stats(u)
 {
     if (!get_ppp_stats(u, &old_link_stats))
        return;
-    gettimeofday(&start_time, NULL);
+    get_time(&start_time);
 }
 
 /*
@@ -1242,7 +1242,7 @@ update_link_stats(u)
     char numbuf[32];
 
     if (!get_ppp_stats(u, &link_stats)
-       || gettimeofday(&now, NULL) < 0)
+       || get_time(&now) < 0)
        return;
     link_connect_time = now.tv_sec - start_time.tv_sec;
     link_stats_valid = 1;
@@ -1289,7 +1289,7 @@ timeout(func, arg, secs, usecs)
        fatal("Out of memory in timeout()!");
     newp->c_arg = arg;
     newp->c_func = func;
-    gettimeofday(&timenow, NULL);
+    get_time(&timenow);
     newp->c_time.tv_sec = timenow.tv_sec + secs;
     newp->c_time.tv_usec = timenow.tv_usec + usecs;
     if (newp->c_time.tv_usec >= 1000000) {
@@ -1343,7 +1343,7 @@ calltimeout()
     while (callout != NULL) {
        p = callout;
 
-       if (gettimeofday(&timenow, NULL) < 0)
+       if (get_time(&timenow) < 0)
            fatal("Failed to get time of day: %m");
        if (!(p->c_time.tv_sec < timenow.tv_sec
              || (p->c_time.tv_sec == timenow.tv_sec
@@ -1368,7 +1368,7 @@ timeleft(tvp)
     if (callout == NULL)
        return NULL;
 
-    gettimeofday(&timenow, NULL);
+    get_time(&timenow);
     tvp->tv_sec = callout->c_time.tv_sec - timenow.tv_sec;
     tvp->tv_usec = callout->c_time.tv_usec - timenow.tv_usec;
     if (tvp->tv_usec < 0) {
index 955b05234b8e408513a3d04eb4d04a28da900475..3edd5ea92e1c0e0a864975670f5fa57b8aa87aa0 100644 (file)
@@ -293,7 +293,7 @@ int rc_acct_using_server(SERVER *acctserver,
        SEND_DATA       data;
        VALUE_PAIR      *adt_vp;
        int             result;
-       time_t          start_time, dtime;
+       struct timeval  start_time, dtime;
        char            msg[4096];
        int             i;
        int             timeout = rc_conf_int("radius_timeout");
@@ -320,11 +320,11 @@ int rc_acct_using_server(SERVER *acctserver,
         * Fill in Acct-Delay-Time
         */
 
-       dtime = 0;
-       if ((adt_vp = rc_avpair_add(&(data.send_pairs), PW_ACCT_DELAY_TIME, &dtime, 0, VENDOR_NONE)) == NULL)
+       dtime.tv_sec = 0;
+       if ((adt_vp = rc_avpair_add(&(data.send_pairs), PW_ACCT_DELAY_TIME, &dtime.tv_sec, 0, VENDOR_NONE)) == NULL)
                return (ERROR_RC);
 
-       start_time = time(NULL);
+       get_time(&start_time);
        result = ERROR_RC;
        for(i=0; (i<acctserver->max) && (result != OK_RC) && (result != BADRESP_RC)
                ; i++)
@@ -336,8 +336,9 @@ int rc_acct_using_server(SERVER *acctserver,
                rc_buildreq(&data, PW_ACCOUNTING_REQUEST, acctserver->name[i],
                            acctserver->port[i], timeout, retries);
 
-               dtime = time(NULL) - start_time;
-               rc_avpair_assign(adt_vp, &dtime, 0);
+               get_time(&dtime);
+               dtime.tv_sec -= start_time.tv_sec;
+               rc_avpair_assign(adt_vp, &dtime.tv_sec, 0);
 
                result = rc_send_server (&data, msg, NULL);
        }
index 2aebcd1c13ac1205511e32bade4d9d22442668b7..a4e4cec93ce1c21ebd8841813b6ee33cdc67d13f 100644 (file)
@@ -45,8 +45,8 @@ static int time_left(struct timeval *diff, struct timeval *exp)
 {
     struct timeval now;
 
-    if (gettimeofday(&now, NULL) < 0) {
-       error("gettimeofday: %m");
+    if (get_time(&now) < 0) {
+       error("get_time: %m");
        return 0;
     }
 
@@ -353,8 +353,8 @@ waitForPADO(PPPoEConnection *conn, int timeout)
     conn->seenMaxPayload = 0;
     conn->error = 0;
 
-    if (gettimeofday(&expire_at, NULL) < 0) {
-       error("gettimeofday (waitForPADO): %m");
+    if (get_time(&expire_at) < 0) {
+       error("get_time (waitForPADO): %m");
        return;
     }
     expire_at.tv_sec += timeout;
@@ -533,8 +533,8 @@ waitForPADS(PPPoEConnection *conn, int timeout)
     PPPoEPacket packet;
     int len;
 
-    if (gettimeofday(&expire_at, NULL) < 0) {
-       error("gettimeofday (waitForPADS): %m");
+    if (get_time(&expire_at) < 0) {
+       error("get_time (waitForPADS): %m");
        return;
     }
     expire_at.tv_sec += timeout;
index b31b78da7e727a005edf3544487721a70d465620..4ab2ac1309902b05b21fe71a5113f3d16a066800 100644 (file)
@@ -713,6 +713,8 @@ int  cipxfaddr __P((int));
 #endif
 int  get_if_hwaddr __P((u_char *addr, char *name));
 char *get_first_ethernet __P((void));
+int get_time __P((struct timeval *));
+                               /* Get current time, monotonic if possible. */
 
 /* Procedures exported from options.c */
 int setipaddr __P((char *, char **, int)); /* Set local/remote ip addresses */
index a0531e9d351d094e138600e3b24945c9b27d1f60..fc309ad267d02b6ac4e3f66b56ecdf1733bd610e 100644 (file)
@@ -3165,3 +3165,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);
+}
index 0b993a50139ed0136d8511f7ee5838c64ea6e0df..65b173a79695b7e80b8b421072532073be513ca0 100644 (file)
 #include <sys/dlpi.h>
 #include <sys/stat.h>
 #include <sys/mkdev.h>
+#include <sys/time.h>
 #include <net/if.h>
 #include <net/if_arp.h>
 #include <net/route.h>
@@ -2863,3 +2864,13 @@ get_pty(master_fdp, slave_fdp, slave_name, uid)
 
     return 1;
 }
+
+/********************************************************************
+ *
+ * get_time - Get current time, monotonic if possible.
+ */
+int
+get_time(struct timeval *tv)
+{
+    return gettimeofday(tv, NULL);
+}
index 7ece6753a0f3ac19733d556f8b4bbe5b5cc3a7c2..4d5b2dbb534a330d32ee0eb316515049b7ea9d05 100644 (file)
@@ -1071,7 +1071,7 @@ charshunt(ifd, ofd, record_file)
     pty_readable = stdin_readable = 1;
 
     ilevel = olevel = 0;
-    gettimeofday(&levelt, NULL);
+    get_time(&levelt);
     if (max_data_rate) {
        max_level = max_data_rate / 10;
        if (max_level < 100)
@@ -1120,7 +1120,7 @@ charshunt(ifd, ofd, record_file)
            int nbt;
            struct timeval now;
 
-           gettimeofday(&now, NULL);
+           get_time(&now);
            dt = (now.tv_sec - levelt.tv_sec
                  + (now.tv_usec - levelt.tv_usec) / 1e6);
            nbt = (int)(dt * max_data_rate);