From 11942d45dcf0f4def7de6faa141ae1175c6a3a22 Mon Sep 17 00:00:00 2001 From: otto <> Date: Thu, 30 May 2019 13:42:19 +0000 Subject: [PATCH] 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@ --- src/usr.sbin/ntpd/constraint.c | 28 +++++++++++++++------------- src/usr.sbin/ntpd/ntp.c | 15 ++++++--------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/usr.sbin/ntpd/constraint.c b/src/usr.sbin/ntpd/constraint.c index 166e037f..13c05f92 100644 --- a/src/usr.sbin/ntpd/constraint.c +++ b/src/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 @@ -41,6 +41,7 @@ #include #include #include +#include #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/src/usr.sbin/ntpd/ntp.c b/src/usr.sbin/ntpd/ntp.c index 7f7a42b7..25670f84 100644 --- a/src/usr.sbin/ntpd/ntp.c +++ b/src/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 @@ -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;