On Sat, Jan 06 2018, Scott Cheloha <scottchel...@gmail.com> wrote:
> 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.

Yes, but I don't think you need to change the format/cast here, the
difference should fit in an int and this idiom is used elsewhere.

> Thoughts?  ok?

ok jca@ for the use of CLOCK_MONOTONIC

> --
> 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:
>

-- 
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE

Reply via email to