summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcheloha <cheloha@cvs.openbsd.org>2018-04-10 15:58:22 +0000
committercheloha <cheloha@cvs.openbsd.org>2018-04-10 15:58:22 +0000
commitd5031e8381d1d54e45858aea86db74ed6a13adc7 (patch)
tree417dbdb1adfa9c9d94169476285168d9a22b0699
parentfd2f6a0c9e39aa13a271b7715bd663828c307d1b (diff)
Schedule events against the monotonic clock.
So that events fire punctually, even if the system clock is changed. ok jca@
-rw-r--r--usr.sbin/sasyncd/sasyncd.c13
-rw-r--r--usr.sbin/sasyncd/sasyncd.h6
-rw-r--r--usr.sbin/sasyncd/timer.c37
3 files changed, 29 insertions, 27 deletions
diff --git a/usr.sbin/sasyncd/sasyncd.c b/usr.sbin/sasyncd/sasyncd.c
index 8214457e77c..96b19c75865 100644
--- a/usr.sbin/sasyncd/sasyncd.c
+++ b/usr.sbin/sasyncd/sasyncd.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: sasyncd.c,v 1.27 2017/04/10 09:27:08 reyk Exp $ */
+/* $OpenBSD: sasyncd.c,v 1.28 2018/04/10 15:58:21 cheloha Exp $ */
/*
* Copyright (c) 2005 Håkan Olsson. All rights reserved.
@@ -31,7 +31,7 @@
#include <sys/types.h>
-#include <sys/time.h>
+
#include <errno.h>
#include <fcntl.h>
#include <pwd.h>
@@ -39,6 +39,7 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
+#include <time.h>
#include <unistd.h>
#include "sasyncd.h"
@@ -55,7 +56,7 @@ sasyncd_stop(int s)
static int
sasyncd_run(pid_t ppid)
{
- struct timeval *timeout, tv;
+ struct timespec *timeout, ts;
fd_set *rfds, *wfds;
size_t fdsetsize;
int maxfd, n;
@@ -101,10 +102,10 @@ sasyncd_run(pid_t ppid)
if (cfgstate.route_socket + 1 > maxfd)
maxfd = cfgstate.route_socket + 1;
- timeout = &tv;
- timer_next_event(&tv);
+ timeout = &ts;
+ timer_next_event(&ts);
- n = select(maxfd, rfds, wfds, 0, timeout);
+ n = pselect(maxfd, rfds, wfds, NULL, timeout, NULL);
if (n == -1) {
if (errno != EINTR) {
log_err("select()");
diff --git a/usr.sbin/sasyncd/sasyncd.h b/usr.sbin/sasyncd/sasyncd.h
index 4f2847ca3dd..e6b3b3a74e5 100644
--- a/usr.sbin/sasyncd/sasyncd.h
+++ b/usr.sbin/sasyncd/sasyncd.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: sasyncd.h,v 1.18 2016/07/18 21:22:09 benno Exp $ */
+/* $OpenBSD: sasyncd.h,v 1.19 2018/04/10 15:58:21 cheloha Exp $ */
/*
* Copyright (c) 2005 Håkan Olsson. All rights reserved.
@@ -37,7 +37,7 @@ enum RUNSTATE { INIT = 0, SLAVE, MASTER, FAIL };
#define CARPSTATES { "INIT", "SLAVE", "MASTER", "FAIL" }
struct syncpeer;
-struct timeval;
+struct timespec;
struct cfgstate {
enum RUNSTATE runstate;
@@ -171,6 +171,6 @@ void pfkey_snapshot(void *);
/* timer.c */
void timer_init(void);
-void timer_next_event(struct timeval *);
+void timer_next_event(struct timespec *);
void timer_run(void);
int timer_add(char *, u_int32_t, void (*)(void *), void *);
diff --git a/usr.sbin/sasyncd/timer.c b/usr.sbin/sasyncd/timer.c
index 798718dc5d8..144950efebd 100644
--- a/usr.sbin/sasyncd/timer.c
+++ b/usr.sbin/sasyncd/timer.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: timer.c,v 1.6 2016/08/27 01:30:39 guenther Exp $ */
+/* $OpenBSD: timer.c,v 1.7 2018/04/10 15:58:21 cheloha Exp $ */
/*
* Copyright (c) 2005 Håkan Olsson. All rights reserved.
@@ -36,6 +36,7 @@
#include <stdlib.h>
#include <string.h>
+#include <time.h>
#include "sasyncd.h"
@@ -46,7 +47,7 @@
*/
struct event {
TAILQ_ENTRY (event) next;
- struct timeval expire;
+ struct timespec expire;
char *name;
void (*fun) (void *);
void *arg;
@@ -66,20 +67,20 @@ timer_init(void)
* the select() call in the main loop.
*/
void
-timer_next_event(struct timeval *tv)
+timer_next_event(struct timespec *ts)
{
- struct timeval now;
+ struct timespec now;
struct event *e = TAILQ_FIRST(&events);
if (e) {
- gettimeofday(&now, 0);
- if (timercmp(&now, &e->expire, >=))
- timerclear(tv);
+ clock_gettime(CLOCK_MONOTONIC, &now);
+ if (timespeccmp(&now, &e->expire, >=))
+ timespecclear(ts);
else
- timersub(&e->expire, &now, tv);
+ timespecsub(&e->expire, &now, ts);
} else {
- tv->tv_sec = 60; /* "Best guess". */
- tv->tv_usec = 0;
+ ts->tv_sec = 60; /* "Best guess". */
+ ts->tv_nsec = 0;
}
}
@@ -90,11 +91,11 @@ timer_next_event(struct timeval *tv)
void
timer_run(void)
{
- struct timeval now;
+ struct timespec now;
struct event *e;
- gettimeofday(&now, 0);
- for (e = TAILQ_FIRST(&events); e && timercmp(&now, &e->expire, >=);
+ clock_gettime(CLOCK_MONOTONIC, &now);
+ for (e = TAILQ_FIRST(&events); e && timespeccmp(&now, &e->expire, >=);
e = TAILQ_FIRST(&events)) {
TAILQ_REMOVE(&events, e, next);
log_msg(2, "timer_run: event \"%s\"",
@@ -110,7 +111,7 @@ timer_run(void)
int
timer_add(char *name, u_int32_t when, void (*function)(void *), void *arg)
{
- struct timeval now, tmp;
+ struct timespec now, tmp;
struct event *e, *new;
new = calloc(1, sizeof *new);
@@ -123,17 +124,17 @@ timer_add(char *name, u_int32_t when, void (*function)(void *), void *arg)
new->fun = function;
new->arg = arg;
- memset(&tmp, 0, sizeof tmp);
tmp.tv_sec = when;
- gettimeofday(&now, 0);
- timeradd(&now, &tmp, &new->expire);
+ tmp.tv_nsec = 0;
+ clock_gettime(CLOCK_MONOTONIC, &now);
+ timespecadd(&now, &tmp, &new->expire);
log_msg(2, "timer_add: new event \"%s\" (expiring in %us)",
name ? name : "<unknown>", when);
/* Insert the new event in the queue so it's always sorted. */
for (e = TAILQ_FIRST(&events); e; e = TAILQ_NEXT(e, next)) {
- if (timercmp(&new->expire, &e->expire, >=))
+ if (timespeccmp(&new->expire, &e->expire, >=))
continue;
TAILQ_INSERT_BEFORE(e, new, next);
return 0;