So that events fire punctually even if the system clock is changed.
ok?
--
Scott Cheloha
Index: usr.sbin/sasyncd/sasyncd.c
===================================================================
RCS file: /cvs/src/usr.sbin/sasyncd/sasyncd.c,v
retrieving revision 1.27
diff -u -p -r1.27 sasyncd.c
--- usr.sbin/sasyncd/sasyncd.c 10 Apr 2017 09:27:08 -0000 1.27
+++ usr.sbin/sasyncd/sasyncd.c 12 Feb 2018 21:01:10 -0000
@@ -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()");
Index: usr.sbin/sasyncd/sasyncd.h
===================================================================
RCS file: /cvs/src/usr.sbin/sasyncd/sasyncd.h,v
retrieving revision 1.18
diff -u -p -r1.18 sasyncd.h
--- usr.sbin/sasyncd/sasyncd.h 18 Jul 2016 21:22:09 -0000 1.18
+++ usr.sbin/sasyncd/sasyncd.h 12 Feb 2018 21:01:10 -0000
@@ -37,7 +37,7 @@ enum RUNSTATE { INIT = 0, SLAVE, MASTER
#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 *);
Index: usr.sbin/sasyncd/timer.c
===================================================================
RCS file: /cvs/src/usr.sbin/sasyncd/timer.c,v
retrieving revision 1.6
diff -u -p -r1.6 timer.c
--- usr.sbin/sasyncd/timer.c 27 Aug 2016 01:30:39 -0000 1.6
+++ usr.sbin/sasyncd/timer.c 12 Feb 2018 21:01:10 -0000
@@ -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, vo
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;