From: jca Date: Tue, 5 Dec 2017 20:31:45 +0000 (+0000) Subject: Use clock_gettime(CLOCK_MONOTONIC) to schedule timers X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=6ee513e5aba73254713aefffb1d985e0a57a9b85;p=openbsd Use clock_gettime(CLOCK_MONOTONIC) to schedule timers From Scott Cheloha, ok tb@ --- diff --git a/sbin/isakmpd/connection.c b/sbin/isakmpd/connection.c index a2a7e7489da..9185cc248f5 100644 --- a/sbin/isakmpd/connection.c +++ b/sbin/isakmpd/connection.c @@ -1,4 +1,4 @@ -/* $OpenBSD: connection.c,v 1.38 2017/08/06 13:54:04 mpi Exp $ */ +/* $OpenBSD: connection.c,v 1.39 2017/12/05 20:31:45 jca Exp $ */ /* $EOM: connection.c,v 1.28 2000/11/23 12:21:18 niklas Exp $ */ /* @@ -31,7 +31,6 @@ */ #include -#include #include #include #include @@ -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, diff --git a/sbin/isakmpd/dpd.c b/sbin/isakmpd/dpd.c index 24c4098229c..7d14e445d52 100644 --- a/sbin/isakmpd/dpd.c +++ b/sbin/isakmpd/dpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dpd.c,v 1.19 2015/12/10 17:27:00 mmcc Exp $ */ +/* $OpenBSD: dpd.c,v 1.20 2017/12/05 20:31:45 jca Exp $ */ /* * Copyright (c) 2004 Håkan Olsson. All rights reserved. @@ -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_arg) 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_arg) 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_arg) 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; diff --git a/sbin/isakmpd/exchange.c b/sbin/isakmpd/exchange.c index 7a6575e1637..019d03ada5a 100644 --- a/sbin/isakmpd/exchange.c +++ b/sbin/isakmpd/exchange.c @@ -1,4 +1,4 @@ -/* $OpenBSD: exchange.c,v 1.139 2017/09/18 07:42:52 mpi Exp $ */ +/* $OpenBSD: exchange.c,v 1.140 2017/12/05 20:31:45 jca Exp $ */ /* $EOM: exchange.c,v 1.143 2000/12/04 00:02:25 angelos Exp $ */ /* @@ -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, int doi, int type) 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; diff --git a/sbin/isakmpd/isakmpd.c b/sbin/isakmpd/isakmpd.c index 9142d863040..b957e7dc1cd 100644 --- a/sbin/isakmpd/isakmpd.c +++ b/sbin/isakmpd/isakmpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: isakmpd.c,v 1.104 2016/04/02 14:37:42 krw Exp $ */ +/* $OpenBSD: isakmpd.c,v 1.105 2017/12/05 20:31:45 jca Exp $ */ /* $EOM: isakmpd.c,v 1.54 2000/10/05 09:28:22 niklas Exp $ */ /* @@ -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"); diff --git a/sbin/isakmpd/nat_traversal.c b/sbin/isakmpd/nat_traversal.c index 991cbda3519..c74c5ead7d1 100644 --- a/sbin/isakmpd/nat_traversal.c +++ b/sbin/isakmpd/nat_traversal.c @@ -1,4 +1,4 @@ -/* $OpenBSD: nat_traversal.c,v 1.24 2015/08/20 22:05:51 deraadt Exp $ */ +/* $OpenBSD: nat_traversal.c,v 1.25 2017/12/05 20:31:45 jca Exp $ */ /* * Copyright (c) 2004 Håkan Olsson. All rights reserved. @@ -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", diff --git a/sbin/isakmpd/pf_key_v2.c b/sbin/isakmpd/pf_key_v2.c index 606ff3bb648..9194cd7fb0e 100644 --- a/sbin/isakmpd/pf_key_v2.c +++ b/sbin/isakmpd/pf_key_v2.c @@ -1,4 +1,4 @@ -/* $OpenBSD: pf_key_v2.c,v 1.199 2017/08/06 13:54:04 mpi Exp $ */ +/* $OpenBSD: pf_key_v2.c,v 1.200 2017/12/05 20:31:45 jca Exp $ */ /* $EOM: pf_key_v2.c,v 1.79 2000/12/12 00:33:19 niklas Exp $ */ /* @@ -35,7 +35,6 @@ #include #include #include -#include #include #include @@ -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; } diff --git a/sbin/isakmpd/sa.c b/sbin/isakmpd/sa.c index 49d8ab71628..26c63a9d007 100644 --- a/sbin/isakmpd/sa.c +++ b/sbin/isakmpd/sa.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sa.c,v 1.123 2015/12/09 21:41:50 naddy Exp $ */ +/* $OpenBSD: sa.c,v 1.124 2017/12/05 20:31:45 jca Exp $ */ /* $EOM: sa.c,v 1.112 2000/12/12 00:22:52 niklas Exp $ */ /* @@ -1348,7 +1348,7 @@ sa_replace(struct sa *sa, struct sa *new_sa) 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)); diff --git a/sbin/isakmpd/timer.c b/sbin/isakmpd/timer.c index 614ac6fa44b..ea7e5041fe3 100644 --- a/sbin/isakmpd/timer.c +++ b/sbin/isakmpd/timer.c @@ -1,4 +1,4 @@ -/* $OpenBSD: timer.c,v 1.17 2015/08/20 22:02:21 deraadt Exp $ */ +/* $OpenBSD: timer.c,v 1.18 2017/12/05 20:31:45 jca Exp $ */ /* $EOM: timer.c,v 1.13 2000/02/20 19:58:42 niklas Exp $ */ /* @@ -30,6 +30,8 @@ */ #include +#include + #include #include @@ -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 **timeout) 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, diff --git a/sbin/isakmpd/timer.h b/sbin/isakmpd/timer.h index 6ddbcea6bb3..dcb1c218c9f 100644 --- a/sbin/isakmpd/timer.h +++ b/sbin/isakmpd/timer.h @@ -1,4 +1,4 @@ -/* $OpenBSD: timer.h,v 1.8 2015/01/16 06:39:59 deraadt Exp $ */ +/* $OpenBSD: timer.h,v 1.9 2017/12/05 20:31:45 jca Exp $ */ /* $EOM: timer.h,v 1.6 1999/04/11 22:35:55 ho Exp $ */ /* @@ -33,21 +33,22 @@ #define _TIMER_H_ #include -#include + +#include 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); diff --git a/sbin/isakmpd/transport.c b/sbin/isakmpd/transport.c index 65fb31d3e97..07b2f3ece8f 100644 --- a/sbin/isakmpd/transport.c +++ b/sbin/isakmpd/transport.c @@ -1,4 +1,4 @@ -/* $OpenBSD: transport.c,v 1.37 2016/03/10 07:32:16 yasuoka Exp $ */ +/* $OpenBSD: transport.c,v 1.38 2017/12/05 20:31:45 jca Exp $ */ /* $EOM: transport.c,v 1.43 2000/10/10 12:36:39 provos Exp $ */ /* @@ -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 diff --git a/sbin/isakmpd/ui.c b/sbin/isakmpd/ui.c index bb5f8c0a3ee..f22fe0170eb 100644 --- a/sbin/isakmpd/ui.c +++ b/sbin/isakmpd/ui.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ui.c,v 1.56 2014/12/01 23:05:18 tedu Exp $ */ +/* $OpenBSD: ui.c,v 1.57 2017/12/05 20:31:45 jca Exp $ */ /* $EOM: ui.c,v 1.43 2000/10/05 09:25:12 niklas Exp $ */ /* @@ -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."); diff --git a/sbin/isakmpd/util.c b/sbin/isakmpd/util.c index 036a2e5586e..e535f922aa6 100644 --- a/sbin/isakmpd/util.c +++ b/sbin/isakmpd/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.69 2015/08/20 22:02:21 deraadt Exp $ */ +/* $OpenBSD: util.c,v 1.70 2017/12/05 20:31:45 jca Exp $ */ /* $EOM: util.c,v 1.23 2000/11/23 12:22:08 niklas Exp $ */ /* @@ -554,15 +554,13 @@ check_file_secrecy_fd(int fd, char *name, size_t *file_size) /* 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; } diff --git a/sbin/isakmpd/util.h b/sbin/isakmpd/util.h index ffb49351138..2ba66c9f4c0 100644 --- a/sbin/isakmpd/util.h +++ b/sbin/isakmpd/util.h @@ -1,4 +1,4 @@ -/* $OpenBSD: util.h,v 1.32 2014/01/23 01:04:28 deraadt Exp $ */ +/* $OpenBSD: util.h,v 1.33 2017/12/05 20:31:45 jca Exp $ */ /* $EOM: util.h,v 1.10 2000/10/24 13:33:39 niklas Exp $ */ /* @@ -60,7 +60,7 @@ extern int text2sockaddr(char *, char *, struct sockaddr **, 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_ */