summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHenning Brauer <henning@cvs.openbsd.org>2004-07-05 07:46:17 +0000
committerHenning Brauer <henning@cvs.openbsd.org>2004-07-05 07:46:17 +0000
commit1e7f981d00467d42a37fabc57f408996a19611bb (patch)
tree78f2567686288d02c7345abbad652abec86ec6e5
parent12ced0bd9ce029f4677492b1801929b8233c7210 (diff)
calculate the median offset from all servers we sync to and call
adjtime() when necessary to keep the local clock in sync yes, that means ntpd syncs the local clock now.
-rw-r--r--usr.sbin/ntpd/ntp.c46
-rw-r--r--usr.sbin/ntpd/ntpd.c20
-rw-r--r--usr.sbin/ntpd/ntpd.h7
-rw-r--r--usr.sbin/ntpd/util.c10
4 files changed, 73 insertions, 10 deletions
diff --git a/usr.sbin/ntpd/ntp.c b/usr.sbin/ntpd/ntp.c
index 3a0f408d583..dadc76c2be9 100644
--- a/usr.sbin/ntpd/ntp.c
+++ b/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 <henning@openbsd.org>
@@ -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/usr.sbin/ntpd/ntpd.c b/usr.sbin/ntpd/ntpd.c
index 7d6eadac1d9..336d5176bf3 100644
--- a/usr.sbin/ntpd/ntpd.c
+++ b/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 <henning@openbsd.org>
@@ -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/usr.sbin/ntpd/ntpd.h b/usr.sbin/ntpd/ntpd.h
index 77316d5a6c4..a89e7c6b99f 100644
--- a/usr.sbin/ntpd/ntpd.h
+++ b/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 <henning@openbsd.org>
@@ -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/usr.sbin/ntpd/util.c b/usr.sbin/ntpd/util.c
index d16c0c7602d..3c618c4c40e 100644
--- a/usr.sbin/ntpd/util.c
+++ b/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 <alexander.guy@andern.org>
@@ -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)
{