On Mon, Nov 27, 2017 at 08:36:33PM -0600, Scott Cheloha wrote:
>
> > On Nov 27, 2017, at 9:54 AM, Jeremie Courreges-Anglas <[email protected]>
> > wrote:
> >
> > On Fri, Nov 24 2017, Scott Cheloha <[email protected]> wrote:
> >> Hi,
> >>
> >> [...]
> >>
> >> Thoughts and feedback?
> >
> > This seems to mix refactoring, eg changing the signature of
> > timer_add_event(), with semantic changes. Could you please
> > split your diff in seperate steps easier to review?
>
> Sure thing.
Here's part 1: use monotime for everything event-related.
--
Scott Cheloha
Index: sbin/isakmpd/connection.c
===================================================================
RCS file: /cvs/src/sbin/isakmpd/connection.c,v
retrieving revision 1.38
diff -u -p -r1.38 connection.c
--- sbin/isakmpd/connection.c 6 Aug 2017 13:54:04 -0000 1.38
+++ sbin/isakmpd/connection.c 29 Nov 2017 13:50:13 -0000
@@ -31,7 +31,6 @@
*/
#include <sys/queue.h>
-#include <sys/time.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>
@@ -71,7 +70,7 @@ struct connection_passive {
/* XXX Potential additions to 'connection_passive'. */
char *isakmp_peer;
struct sa *sa; /* XXX "Soft" ref to active sa? */
- struct timeval sa_expiration; /* XXX *sa may expire. */
+ struct timespec sa_expiration; /* XXX *sa may expire. */
#endif
};
@@ -144,11 +143,11 @@ connection_init(void)
static void
connection_checker(void *vconn)
{
- struct timeval now;
+ struct timespec now;
struct connection *conn = vconn;
char *name;
- gettimeofday(&now, 0);
+ clock_gettime(CLOCK_MONOTONIC, &now);
now.tv_sec += conf_get_num("General", "check-interval",
CHECK_INTERVAL);
conn->ev = timer_add_event("connection_checker",
@@ -272,7 +271,7 @@ int
connection_setup(char *name)
{
struct connection *conn = 0;
- struct timeval now;
+ struct timespec now;
/* Check for trials to add duplicate connections. */
if (connection_lookup(name)) {
@@ -291,7 +290,7 @@ connection_setup(char *name)
log_error("connection_setup: strdup (\"%s\") failed", name);
goto fail;
}
- gettimeofday(&now, 0);
+ clock_gettime(CLOCK_MONOTONIC, &now);
conn->ev = timer_add_event("connection_checker", connection_checker,
conn, &now);
if (!conn->ev) {
@@ -405,11 +404,11 @@ void
connection_report(void)
{
struct connection *conn;
- struct timeval now;
+ struct timespec now;
struct connection_passive *pconn;
struct doi *doi = doi_lookup(ISAKMP_DOI_ISAKMP);
- gettimeofday(&now, 0);
+ clock_gettime(CLOCK_MONOTONIC, &now);
for (conn = TAILQ_FIRST(&connections); conn;
conn = TAILQ_NEXT(conn, link))
LOG_DBG((LOG_REPORT, 0,
Index: sbin/isakmpd/dpd.c
===================================================================
RCS file: /cvs/src/sbin/isakmpd/dpd.c,v
retrieving revision 1.19
diff -u -p -r1.19 dpd.c
--- sbin/isakmpd/dpd.c 10 Dec 2015 17:27:00 -0000 1.19
+++ sbin/isakmpd/dpd.c 29 Nov 2017 13:50:13 -0000
@@ -216,23 +216,23 @@ dpd_timer_interval(u_int32_t offset)
static void
dpd_timer_reset(struct sa *sa, u_int32_t time_passed, enum dpd_tstate mode)
{
- struct timeval tv;
+ struct timespec ts;
if (sa->dpd_event)
timer_remove_event(sa->dpd_event);
- gettimeofday(&tv, 0);
+ clock_gettime(CLOCK_MONOTONIC, &ts);
switch (mode) {
case DPD_TIMER_NORMAL:
sa->dpd_failcount = 0;
- tv.tv_sec += dpd_timer_interval(time_passed);
+ ts.tv_sec += dpd_timer_interval(time_passed);
sa->dpd_event = timer_add_event("dpd_event", dpd_event, sa,
- &tv);
+ &ts);
break;
case DPD_TIMER_CHECK:
- tv.tv_sec += DPD_RETRANS_WAIT;
+ ts.tv_sec += DPD_RETRANS_WAIT;
sa->dpd_event = timer_add_event("dpd_check_event",
- dpd_check_event, sa, &tv);
+ dpd_check_event, sa, &ts);
break;
default:
break;
@@ -267,7 +267,7 @@ dpd_check_time(struct sa *sa, void *v_ar
struct sockaddr *dst;
struct proto *proto;
struct sa_kinfo *ksa;
- struct timeval tv;
+ struct timespec ts;
if (sa->phase == 1 || (args->isakmp_sa->flags & SA_FLAG_DPD) == 0 ||
dpd_find_sa(sa, args->isakmp_sa) == 0)
@@ -278,7 +278,7 @@ dpd_check_time(struct sa *sa, void *v_ar
return 0;
sa->transport->vtbl->get_src(sa->transport, &dst);
- gettimeofday(&tv, 0);
+ clock_gettime(CLOCK_MONOTONIC, &ts);
ksa = pf_key_v2_get_kernel_sa(proto->spi[1], proto->spi_sz[1],
proto->proto, dst);
@@ -287,10 +287,10 @@ dpd_check_time(struct sa *sa, void *v_ar
LOG_DBG((LOG_MESSAGE, 80, "dpd_check_time: "
"SA %p last use %u second(s) ago", sa,
- (u_int32_t)(tv.tv_sec - ksa->last_used)));
+ (u_int32_t)(ts.tv_sec - ksa->last_used)));
- if ((u_int32_t)(tv.tv_sec - ksa->last_used) < args->interval) {
- args->interval = (u_int32_t)(tv.tv_sec - ksa->last_used);
+ if ((u_int32_t)(ts.tv_sec - ksa->last_used) < args->interval) {
+ args->interval = (u_int32_t)(ts.tv_sec - ksa->last_used);
return 1;
}
return 0;
Index: sbin/isakmpd/exchange.c
===================================================================
RCS file: /cvs/src/sbin/isakmpd/exchange.c,v
retrieving revision 1.139
diff -u -p -r1.139 exchange.c
--- sbin/isakmpd/exchange.c 18 Sep 2017 07:42:52 -0000 1.139
+++ sbin/isakmpd/exchange.c 29 Nov 2017 13:50:14 -0000
@@ -587,7 +587,7 @@ static struct exchange *
exchange_create(int phase, int initiator, int doi, int type)
{
struct exchange *exchange;
- struct timeval expiration;
+ struct timespec expiration;
int delta;
/*
@@ -623,7 +623,7 @@ exchange_create(int phase, int initiator
return 0;
}
}
- gettimeofday(&expiration, 0);
+ clock_gettime(CLOCK_MONOTONIC, &expiration);
delta = conf_get_num("General", "Exchange-max-time",
EXCHANGE_MAX_TIME);
expiration.tv_sec += delta;
Index: sbin/isakmpd/isakmpd.c
===================================================================
RCS file: /cvs/src/sbin/isakmpd/isakmpd.c,v
retrieving revision 1.104
diff -u -p -r1.104 isakmpd.c
--- sbin/isakmpd/isakmpd.c 2 Apr 2016 14:37:42 -0000 1.104
+++ sbin/isakmpd/isakmpd.c 29 Nov 2017 13:50:14 -0000
@@ -391,7 +391,7 @@ main(int argc, char *argv[])
fd_set *rfds, *wfds;
int n, m;
size_t mask_size;
- struct timeval tv, *timeout;
+ struct timespec ts, *timeout;
closefrom(STDERR_FILENO + 1);
@@ -505,10 +505,10 @@ main(int argc, char *argv[])
n = m;
/* Find out when the next timed event is. */
- timeout = &tv;
+ timeout = &ts;
timer_next_event(&timeout);
- n = select(n, rfds, wfds, 0, timeout);
+ n = pselect(n, rfds, wfds, NULL, timeout, NULL);
if (n == -1) {
if (errno != EINTR) {
log_error("main: select");
Index: sbin/isakmpd/nat_traversal.c
===================================================================
RCS file: /cvs/src/sbin/isakmpd/nat_traversal.c,v
retrieving revision 1.24
diff -u -p -r1.24 nat_traversal.c
--- sbin/isakmpd/nat_traversal.c 20 Aug 2015 22:05:51 -0000 1.24
+++ sbin/isakmpd/nat_traversal.c 29 Nov 2017 13:50:14 -0000
@@ -387,7 +387,7 @@ nat_t_send_keepalive(void *v_arg)
{
struct sa *sa = (struct sa *)v_arg;
struct transport *t;
- struct timeval now;
+ struct timespec now;
int interval;
/* Send the keepalive message. */
@@ -398,7 +398,7 @@ nat_t_send_keepalive(void *v_arg)
interval = conf_get_num("General", "NAT-T-Keepalive", 0);
if (interval < 1)
interval = NAT_T_KEEPALIVE_INTERVAL;
- gettimeofday(&now, 0);
+ clock_gettime(CLOCK_MONOTONIC, &now);
now.tv_sec += interval;
sa->nat_t_keepalive = timer_add_event("nat_t_send_keepalive",
@@ -412,7 +412,7 @@ void
nat_t_setup_keepalive(struct sa *sa)
{
struct sockaddr *src;
- struct timeval now;
+ struct timespec now;
if (sa->initiator)
sa->transport->vtbl->get_src(sa->transport, &src);
@@ -422,7 +422,7 @@ nat_t_setup_keepalive(struct sa *sa)
if (!virtual_listen_lookup(src))
return;
- gettimeofday(&now, 0);
+ clock_gettime(CLOCK_MONOTONIC, &now);
now.tv_sec += NAT_T_KEEPALIVE_INTERVAL;
sa->nat_t_keepalive = timer_add_event("nat_t_send_keepalive",
Index: sbin/isakmpd/pf_key_v2.c
===================================================================
RCS file: /cvs/src/sbin/isakmpd/pf_key_v2.c,v
retrieving revision 1.199
diff -u -p -r1.199 pf_key_v2.c
--- sbin/isakmpd/pf_key_v2.c 6 Aug 2017 13:54:04 -0000 1.199
+++ sbin/isakmpd/pf_key_v2.c 29 Nov 2017 13:50:14 -0000
@@ -35,7 +35,6 @@
#include <sys/ioctl.h>
#include <sys/queue.h>
#include <sys/socket.h>
-#include <sys/time.h>
#include <sys/uio.h>
#include <net/pfkeyv2.h>
@@ -204,7 +203,7 @@ pf_key_v2_read(u_int32_t seq)
struct sadb_msg *msg;
struct sadb_msg hdr;
struct sadb_ext *ext;
- struct timeval tv;
+ struct timespec ts;
struct pollfd pfd[1];
pfd[0].fd = pf_key_v2_socket;
@@ -298,9 +297,9 @@ pf_key_v2_read(u_int32_t seq)
*/
if (seq && (msg->sadb_msg_pid != (u_int32_t) getpid() ||
msg->sadb_msg_seq != seq)) {
- gettimeofday(&tv, 0);
+ clock_gettime(CLOCK_MONOTONIC, &ts);
timer_add_event("pf_key_v2_notify",
- (void (*) (void *)) pf_key_v2_notify, ret, &tv);
+ (void (*) (void *)) pf_key_v2_notify, ret, &ts);
ret = 0;
continue;
}
Index: sbin/isakmpd/sa.c
===================================================================
RCS file: /cvs/src/sbin/isakmpd/sa.c,v
retrieving revision 1.123
diff -u -p -r1.123 sa.c
--- sbin/isakmpd/sa.c 9 Dec 2015 21:41:50 -0000 1.123
+++ sbin/isakmpd/sa.c 29 Nov 2017 13:50:14 -0000
@@ -1348,7 +1348,7 @@ sa_replace(struct sa *sa, struct sa *new
int
sa_setup_expirations(struct sa *sa)
{
- struct timeval expiration;
+ struct timespec expiration;
u_int64_t seconds = sa->seconds;
/*
@@ -1362,7 +1362,7 @@ sa_setup_expirations(struct sa *sa)
* XXX Better scheme to come?
*/
if (!sa->soft_death) {
- gettimeofday(&expiration, 0);
+ clock_gettime(CLOCK_MONOTONIC, &expiration);
/*
* XXX This should probably be configuration controlled
* somehow.
@@ -1382,7 +1382,7 @@ sa_setup_expirations(struct sa *sa)
sa_reference(sa);
}
if (!sa->death) {
- gettimeofday(&expiration, 0);
+ clock_gettime(CLOCK_MONOTONIC, &expiration);
LOG_DBG((LOG_TIMER, 95,
"sa_setup_expirations: SA %p hard timeout in %llu seconds",
sa, sa->seconds));
Index: sbin/isakmpd/timer.c
===================================================================
RCS file: /cvs/src/sbin/isakmpd/timer.c,v
retrieving revision 1.17
diff -u -p -r1.17 timer.c
--- sbin/isakmpd/timer.c 20 Aug 2015 22:02:21 -0000 1.17
+++ sbin/isakmpd/timer.c 29 Nov 2017 13:50:14 -0000
@@ -30,6 +30,8 @@
*/
#include <sys/queue.h>
+#include <sys/time.h>
+
#include <stdlib.h>
#include <string.h>
@@ -45,16 +47,16 @@ timer_init(void)
}
void
-timer_next_event(struct timeval **timeout)
+timer_next_event(struct timespec **timeout)
{
- struct timeval now;
+ struct timespec now;
if (TAILQ_FIRST(&events)) {
- gettimeofday(&now, 0);
- if (timercmp(&now, &TAILQ_FIRST(&events)->expiration, >=))
- timerclear(*timeout);
+ clock_gettime(CLOCK_MONOTONIC, &now);
+ if (timespeccmp(&now, &TAILQ_FIRST(&events)->expiration, >=))
+ timespecclear(*timeout);
else
- timersub(&TAILQ_FIRST(&events)->expiration, &now,
+ timespecsub(&TAILQ_FIRST(&events)->expiration, &now,
*timeout);
} else
*timeout = 0;
@@ -63,11 +65,12 @@ timer_next_event(struct timeval **timeou
void
timer_handle_expirations(void)
{
- struct timeval now;
+ struct timespec now;
struct event *n;
- gettimeofday(&now, 0);
- for (n = TAILQ_FIRST(&events); n && timercmp(&now, &n->expiration, >=);
+ clock_gettime(CLOCK_MONOTONIC, &now);
+ for (n = TAILQ_FIRST(&events);
+ n && timespeccmp(&now, &n->expiration, >=);
n = TAILQ_FIRST(&events)) {
LOG_DBG((LOG_TIMER, 10,
"timer_handle_expirations: event %s(%p)", n->name,
@@ -80,21 +83,21 @@ timer_handle_expirations(void)
struct event *
timer_add_event(char *name, void (*func)(void *), void *arg,
- struct timeval *expiration)
+ struct timespec *expiration)
{
struct event *ev = malloc(sizeof *ev);
struct event *n;
- struct timeval now;
+ struct timespec now;
if (!ev)
return 0;
ev->name = name;
ev->func = func;
ev->arg = arg;
- gettimeofday(&now, 0);
+ clock_gettime(CLOCK_MONOTONIC, &now);
memcpy(&ev->expiration, expiration, sizeof *expiration);
for (n = TAILQ_FIRST(&events);
- n && timercmp(expiration, &n->expiration, >=);
+ n && timespeccmp(expiration, &n->expiration, >=);
n = TAILQ_NEXT(n, link))
;
if (n) {
@@ -125,9 +128,9 @@ void
timer_report(void)
{
struct event *ev;
- struct timeval now;
+ struct timespec now;
- gettimeofday(&now, 0);
+ clock_gettime(CLOCK_MONOTONIC, &now);
for (ev = TAILQ_FIRST(&events); ev; ev = TAILQ_NEXT(ev, link))
LOG_DBG((LOG_REPORT, 0,
Index: sbin/isakmpd/timer.h
===================================================================
RCS file: /cvs/src/sbin/isakmpd/timer.h,v
retrieving revision 1.8
diff -u -p -r1.8 timer.h
--- sbin/isakmpd/timer.h 16 Jan 2015 06:39:59 -0000 1.8
+++ sbin/isakmpd/timer.h 29 Nov 2017 13:50:14 -0000
@@ -33,21 +33,22 @@
#define _TIMER_H_
#include <sys/queue.h>
-#include <sys/time.h>
+
+#include <time.h>
struct event {
TAILQ_ENTRY(event) link;
char *name;
void (*func) (void *);
void *arg;
- struct timeval expiration;
+ struct timespec expiration;
};
extern void timer_init(void);
-extern void timer_next_event(struct timeval **);
+extern void timer_next_event(struct timespec **);
extern void timer_handle_expirations(void);
extern struct event *timer_add_event(char *, void (*) (void *), void *,
- struct timeval *);
+ struct timespec *);
extern void timer_remove_event(struct event *);
extern void timer_report(void);
Index: sbin/isakmpd/transport.c
===================================================================
RCS file: /cvs/src/sbin/isakmpd/transport.c,v
retrieving revision 1.37
diff -u -p -r1.37 transport.c
--- sbin/isakmpd/transport.c 10 Mar 2016 07:32:16 -0000 1.37
+++ sbin/isakmpd/transport.c 29 Nov 2017 13:50:14 -0000
@@ -255,7 +255,7 @@ transport_send_messages(fd_set * fds)
struct message *msg;
struct exchange *exchange;
struct sockaddr *dst;
- struct timeval expiration;
+ struct timespec expiration;
int expiry, ok_to_drop_message;
char peer[NI_MAXHOST], peersv[NI_MAXSERV];
@@ -332,7 +332,8 @@ transport_send_messages(fd_set * fds)
exchange = 0;
#endif
} else {
- gettimeofday(&expiration, 0);
+ clock_gettime(CLOCK_MONOTONIC,
+ &expiration);
/*
* XXX Calculate from round trip
Index: sbin/isakmpd/ui.c
===================================================================
RCS file: /cvs/src/sbin/isakmpd/ui.c,v
retrieving revision 1.56
diff -u -p -r1.56 ui.c
--- sbin/isakmpd/ui.c 1 Dec 2014 23:05:18 -0000 1.56
+++ sbin/isakmpd/ui.c 29 Nov 2017 13:50:14 -0000
@@ -197,16 +197,16 @@ ui_conn_reinit_event(void *v)
static void
ui_conn_reinit(void)
{
- struct timeval tv;
+ struct timespec ts;
if (ui_cr_event)
timer_remove_event(ui_cr_event);
- gettimeofday(&tv, 0);
- tv.tv_sec += 5;
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ ts.tv_sec += 5;
ui_cr_event = timer_add_event("ui_conn_reinit", ui_conn_reinit_event,
- 0, &tv);
+ 0, &ts);
if (!ui_cr_event)
log_print("ui_conn_reinit: timer_add_event() failed. "
"Connections will not be updated.");
Index: sbin/isakmpd/util.c
===================================================================
RCS file: /cvs/src/sbin/isakmpd/util.c,v
retrieving revision 1.69
diff -u -p -r1.69 util.c
--- sbin/isakmpd/util.c 20 Aug 2015 22:02:21 -0000 1.69
+++ sbin/isakmpd/util.c 29 Nov 2017 13:50:14 -0000
@@ -554,15 +554,13 @@ check_file_secrecy_fd(int fd, char *name
/* Calculate timeout. Returns -1 on error. */
long
-get_timeout(struct timeval *timeout)
+get_timeout(struct timespec *timeout)
{
- struct timeval now, result;
+ struct timespec now, result;
- if (gettimeofday(&now, NULL) < 0)
+ if (clock_gettime(CLOCK_MONOTONIC, &now) == -1)
return -1;
-
- timersub(timeout, &now, &result);
-
+ timespecsub(timeout, &now, &result);
return result.tv_sec;
}
Index: sbin/isakmpd/util.h
===================================================================
RCS file: /cvs/src/sbin/isakmpd/util.h,v
retrieving revision 1.32
diff -u -p -r1.32 util.h
--- sbin/isakmpd/util.h 23 Jan 2014 01:04:28 -0000 1.32
+++ sbin/isakmpd/util.h 29 Nov 2017 13:50:14 -0000
@@ -60,7 +60,7 @@ extern int text2sockaddr(char *, ch
sa_family_t, int);
extern void util_ntoa(char **, int, u_int8_t *);
extern int zero_test(const u_int8_t *, size_t);
-extern long get_timeout(struct timeval *);
+extern long get_timeout(struct timespec *);
extern int expand_string(char *, size_t, const char *, const char *);
#endif /* _UTIL_H_ */