summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorotto <otto@openbsd.org>2019-05-30 13:42:19 +0000
committerotto <otto@openbsd.org>2019-05-30 13:42:19 +0000
commite6588cf8db92395a561796abd17d124f10d55f9e (patch)
tree5babd4e9f4cb27565745f7fd3973ccfc5ed29288
parentFix the initialization of bp before calling vfs_getcwd_common (diff)
downloadwireguard-openbsd-e6588cf8db92395a561796abd17d124f10d55f9e.tar.xz
wireguard-openbsd-e6588cf8db92395a561796abd17d124f10d55f9e.zip
Use proper algorithm for median computation; use fabs() for computing
an absolute value and fix poll loop to first generate messages and then compute poll flags the write cases. This makes the timeout workaround for constraints unneeded. ok reyk@ tb@
-rw-r--r--usr.sbin/ntpd/constraint.c28
-rw-r--r--usr.sbin/ntpd/ntp.c15
2 files changed, 21 insertions, 22 deletions
diff --git a/usr.sbin/ntpd/constraint.c b/usr.sbin/ntpd/constraint.c
index 166e037f03f..13c05f92bd7 100644
--- a/usr.sbin/ntpd/constraint.c
+++ b/usr.sbin/ntpd/constraint.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: constraint.c,v 1.43 2019/05/28 06:49:46 otto Exp $ */
+/* $OpenBSD: constraint.c,v 1.44 2019/05/30 13:42:19 otto Exp $ */
/*
* Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org>
@@ -41,6 +41,7 @@
#include <ctype.h>
#include <tls.h>
#include <pwd.h>
+#include <math.h>
#include "ntpd.h"
@@ -773,7 +774,7 @@ constraint_update(void)
{
struct constraint *cstr;
int cnt, i;
- time_t *sum;
+ time_t *values;
time_t now;
now = getmonotime();
@@ -784,29 +785,31 @@ constraint_update(void)
continue;
cnt++;
}
+ if (cnt == 0)
+ return;
- if ((sum = calloc(cnt, sizeof(time_t))) == NULL)
+ if ((values = calloc(cnt, sizeof(time_t))) == NULL)
fatal("calloc");
i = 0;
TAILQ_FOREACH(cstr, &conf->constraints, entry) {
if (cstr->state != STATE_REPLY_RECEIVED)
continue;
- sum[i++] = cstr->constraint + (now - cstr->last);
+ values[i++] = cstr->constraint + (now - cstr->last);
}
- qsort(sum, cnt, sizeof(time_t), constraint_cmp);
+ qsort(values, cnt, sizeof(time_t), constraint_cmp);
/* calculate median */
i = cnt / 2;
if (cnt % 2 == 0)
- if (sum[i - 1] < sum[i])
- i -= 1;
+ conf->constraint_median = (values[i - 1] + values[i]) / 2;
+ else
+ conf->constraint_median = values[i];
conf->constraint_last = now;
- conf->constraint_median = sum[i];
- free(sum);
+ free(values);
}
void
@@ -826,7 +829,7 @@ int
constraint_check(double val)
{
struct timeval tv;
- double constraint;
+ double diff;
time_t now;
if (conf->constraint_median == 0)
@@ -836,10 +839,9 @@ constraint_check(double val)
now = getmonotime();
tv.tv_sec = conf->constraint_median + (now - conf->constraint_last);
tv.tv_usec = 0;
- constraint = gettime_from_timeval(&tv);
+ diff = fabs(val - gettime_from_timeval(&tv));
- if (((val - constraint) > CONSTRAINT_MARGIN) ||
- ((constraint - val) > CONSTRAINT_MARGIN)) {
+ if (diff > CONSTRAINT_MARGIN) {
/* XXX get new constraint if too many errors happened */
if (conf->constraint_errors++ >
(CONSTRAINT_ERROR_MARGIN * peer_cnt)) {
diff --git a/usr.sbin/ntpd/ntp.c b/usr.sbin/ntpd/ntp.c
index 7f7a42b79ae..25670f84731 100644
--- a/usr.sbin/ntpd/ntp.c
+++ b/usr.sbin/ntpd/ntp.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: ntp.c,v 1.151 2019/05/29 18:48:33 otto Exp $ */
+/* $OpenBSD: ntp.c,v 1.152 2019/05/30 13:42:19 otto Exp $ */
/*
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -316,6 +316,11 @@ ntp_main(struct ntpd_conf *nconf, struct passwd *pw, int argc, char **argv)
(peer_cnt == 0 && sensors_cnt == 0)))
priv_settime(0); /* no good peers, don't wait */
+ TAILQ_FOREACH(cstr, &conf->constraints, entry) {
+ if (constraint_query(cstr) == -1)
+ continue;
+ }
+
if (ibuf_main->w.queued > 0)
pfd[PFD_PIPE_MAIN].events |= POLLOUT;
if (ibuf_dns->w.queued > 0)
@@ -330,15 +335,7 @@ ntp_main(struct ntpd_conf *nconf, struct passwd *pw, int argc, char **argv)
}
ctls = i;
- TAILQ_FOREACH(cstr, &conf->constraints, entry) {
- if (constraint_query(cstr) == -1)
- continue;
- }
-
now = getmonotime();
- if (constraint_cnt)
- nextaction = now + 1;
-
timeout = nextaction - now;
if (timeout < 0)
timeout = 0;