Hey,

Use monotime here so the correct session duration is logged even
in the event of a clock jump.

While here, I think it's a good idea to cast time_t to the widest
practical integer type when printing it to minimize the likelihood
of truncation on platforms where int is smaller than time_t, or if
time_t is a floating-point type.

At least, that's the impression I got when I read that POSIX allows
time_t to be basically anything.  AFAICT we typically cast it to
long long.

Thoughts?  ok?

--
Scott Cheloha

Index: usr.sbin/authpf/authpf.c
===================================================================
RCS file: /cvs/src/usr.sbin/authpf/authpf.c,v
retrieving revision 1.125
diff -u -p -r1.125 authpf.c
--- usr.sbin/authpf/authpf.c    29 Mar 2016 14:53:27 -0000      1.125
+++ usr.sbin/authpf/authpf.c    6 Jan 2018 15:47:09 -0000
@@ -21,7 +21,6 @@
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
-#include <sys/time.h>
 #include <sys/wait.h>
 
 #include <netinet/in.h>
@@ -39,6 +38,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <syslog.h>
+#include <time.h>
 #include <unistd.h>
 
 #include "pathnames.h"
@@ -65,7 +65,7 @@ char   luser[LOGIN_NAME_MAX]; /* username
 char    ipsrc[256];            /* ip as a string */
 char    pidfile[PATH_MAX];     /* we save pid in this file. */
 
-struct timeval Tstart, Tend;   /* start and end times of session */
+struct timespec        Tstart, Tend;   /* start and end times of session */
 
 volatile sig_atomic_t  want_death;
 static void            need_death(int signo);
@@ -819,14 +819,14 @@ change_filter(int add, const char *luser
                        goto error;
                }
 
-               gettimeofday(&Tstart, NULL);
+               clock_gettime(CLOCK_MONOTONIC, &Tstart);
                syslog(LOG_INFO, "allowing %s, user %s", ipsrc, luser);
        } else {
                remove_stale_rulesets();
 
-               gettimeofday(&Tend, NULL);
-               syslog(LOG_INFO, "removed %s, user %s - duration %d seconds",
-                   ipsrc, luser, (int)(Tend.tv_sec - Tstart.tv_sec));
+               clock_gettime(CLOCK_MONOTONIC, &Tend);
+               syslog(LOG_INFO, "removed %s, user %s - duration %lld seconds",
+                   ipsrc, luser, (long long)(Tend.tv_sec - Tstart.tv_sec));
        }
        return (0);
 no_mem:

Reply via email to