diff --git a/src/usr.sbin/ntpd/ntp.c b/src/usr.sbin/ntpd/ntp.c index 3a0f408d..dadc76c2 100644 --- a/src/usr.sbin/ntpd/ntp.c +++ b/src/usr.sbin/ntpd/ntp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntp.c,v 1.9 2004/06/22 07:59:48 alexander Exp $ */ +/* $OpenBSD: ntp.c,v 1.10 2004/07/05 07:46:16 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -38,6 +38,7 @@ struct l_fixedpt ref_ts; void ntp_sighdlr(int); int ntp_dispatch_imsg(void); int ntp_dispatch(int fd); +void ntp_adjtime(struct ntpd_conf *); void ntp_sighdlr(int sig) @@ -63,7 +64,7 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *conf) struct listen_addr *la; struct ntp_peer *p; struct ntp_peer **idx2peer = NULL; - time_t nextaction; + time_t nextaction, next_adjtime; void *newp; switch (pid = fork()) { @@ -114,6 +115,8 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *conf) TAILQ_FOREACH(p, &conf->ntp_peers, entry) peer_cnt++; + next_adjtime = time(NULL) + INTERVAL_ADJTIME; + while (ntp_quit == 0) { if (peer_cnt > idx2peer_elms || peer_cnt + IDX2PEER_RESERVE < idx2peer_elms) { @@ -146,11 +149,9 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *conf) bzero(pfd, sizeof(struct pollfd) * pfd_elms); bzero(idx2peer, sizeof(struct ntp_peer) * idx2peer_elms); - nextaction = time(NULL) + 240; + nextaction = next_adjtime; pfd[PFD_PIPE_MAIN].fd = ibuf_main.fd; pfd[PFD_PIPE_MAIN].events = POLLIN; - if (ibuf_main.w.queued > 0) - pfd[PFD_PIPE_MAIN].events |= POLLOUT; i = 1; TAILQ_FOREACH(la, &conf->listen_addrs, entry) { @@ -159,7 +160,6 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *conf) i++; } - idx_peers = i; TAILQ_FOREACH(p, &conf->ntp_peers, entry) { if (p->next > 0 && p->next < nextaction) @@ -176,6 +176,14 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *conf) } } + if (next_adjtime <= time(NULL)) { + next_adjtime = time(NULL) + INTERVAL_ADJTIME; + ntp_adjtime(conf); + } + + if (ibuf_main.w.queued > 0) + pfd[PFD_PIPE_MAIN].events |= POLLOUT; + timeout = nextaction - time(NULL); if (timeout < 0) timeout = 0; @@ -185,6 +193,7 @@ ntp_main(int pipe_prnt[2], struct ntpd_conf *conf) log_warn("poll error"); ntp_quit = 1; } + if (nfds > 0 && (pfd[PFD_PIPE_MAIN].revents & POLLOUT)) if (msgbuf_write(&ibuf_main.w) < 0) { log_warn("pipe write error (to parent)"); @@ -269,3 +278,28 @@ ntp_dispatch(int fd) return (0); } + +void +ntp_adjtime(struct ntpd_conf *conf) +{ + struct ntp_peer *p; + double offset_median = 0; + int offset_cnt = 0; + + TAILQ_FOREACH(p, &conf->ntp_peers, entry) { + if (p->state == STATE_NONE) + continue; + + offset_median += p->offset; + offset_cnt++; + } + + offset_median /= offset_cnt; + + if (offset_median >= 0.001 || offset_median <= 0.001) { + imsg_compose(&ibuf_main, IMSG_ADJTIME, 0, + &offset_median, sizeof(offset_median)); + TAILQ_FOREACH(p, &conf->ntp_peers, entry) + p->offset = 0; + } +} diff --git a/src/usr.sbin/ntpd/ntpd.c b/src/usr.sbin/ntpd/ntpd.c index 7d6eadac..336d5176 100644 --- a/src/usr.sbin/ntpd/ntpd.c +++ b/src/usr.sbin/ntpd/ntpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.c,v 1.5 2004/06/17 19:17:48 henning Exp $ */ +/* $OpenBSD: ntpd.c,v 1.6 2004/07/05 07:46:16 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -40,6 +40,7 @@ int main(int, char *[]); int check_child(pid_t, const char *); int reconfigure(char *); int dispatch_imsg(void); +void ntpd_adjtime(double); int rfd = -1; volatile sig_atomic_t quit = 0; @@ -227,6 +228,7 @@ dispatch_imsg(void) { struct imsg imsg; int n; + double d; if ((n = imsg_read(&ibuf)) == -1) return (-1); @@ -244,6 +246,11 @@ dispatch_imsg(void) break; switch (imsg.hdr.type) { + case IMSG_ADJTIME: + if (imsg.hdr.len != IMSG_HEADER_SIZE + sizeof(d)) + fatal("invalid IMSG_ADJTIME received"); + memcpy(&d, imsg.data, sizeof(d)); + ntpd_adjtime(d); default: break; } @@ -251,3 +258,14 @@ dispatch_imsg(void) } return (0); } + +void +ntpd_adjtime(double d) +{ + struct timeval tv; + + d_to_tv(d, &tv); + log_debug("calling adjtime, offset=%fs", d); + if (adjtime(&tv, NULL) == -1) + log_warn("adjtime failed"); +} diff --git a/src/usr.sbin/ntpd/ntpd.h b/src/usr.sbin/ntpd/ntpd.h index 77316d5a..a89e7c6b 100644 --- a/src/usr.sbin/ntpd/ntpd.h +++ b/src/usr.sbin/ntpd/ntpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ntpd.h,v 1.9 2004/07/04 22:24:20 henning Exp $ */ +/* $OpenBSD: ntpd.h,v 1.10 2004/07/05 07:46:16 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -36,6 +36,7 @@ #define NTPD_OPT_VERBOSE 0x0001 #define NTPD_OPT_VERBOSE2 0x0002 +#define INTERVAL_ADJTIME 120 /* call adjtime every n seconds */ #define INTERVAL_QUERY 60 /* sync with peers every n seconds */ #define QUERYTIME_MAX 30 /* single query might take n secs max */ @@ -101,7 +102,8 @@ struct imsgbuf { }; enum imsg_type { - IMSG_NONE + IMSG_NONE, + IMSG_ADJTIME }; struct imsg_hdr { @@ -176,5 +178,6 @@ int client_dispatch(struct ntp_peer *); /* util.c */ double gettime(void); +void d_to_tv(double, struct timeval *); double lfp_to_d(struct l_fixedpt); struct l_fixedpt d_to_lfp(double); diff --git a/src/usr.sbin/ntpd/util.c b/src/usr.sbin/ntpd/util.c index d16c0c76..3c618c4c 100644 --- a/src/usr.sbin/ntpd/util.c +++ b/src/usr.sbin/ntpd/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.2 2004/07/04 18:07:15 henning Exp $ */ +/* $OpenBSD: util.c,v 1.3 2004/07/05 07:46:16 henning Exp $ */ /* * Copyright (c) 2004 Alexander Guy @@ -31,6 +31,14 @@ gettime(void) return (tv.tv_sec + JAN_1970 + 1.0e-6 * tv.tv_usec); } + +void +d_to_tv(double d, struct timeval *tv) +{ + tv->tv_sec = (long)d; + tv->tv_usec = (d - tv->tv_sec) * 1000; +} + double lfp_to_d(struct l_fixedpt lfp) {