This patch adds support for the Linux adjtimex call to OpenNTPd, to adjust
the kernel to adjust for systematic clock skew/drift.

Apply with:
cd openntpd-20040824p && patch -p0 </tmp/openntpd-linux-adjtimex.patch

then either rebuild configure with "autoconf" or define USE_ADJTIMEX with:
CFLAGS=-DUSE_ADJTIMEX ./configure && make

Index: client.c
===================================================================
RCS file: /usr/local/cvs/openntpd-portable/client.c,v
retrieving revision 1.14
diff -u -p -r1.14 client.c
--- client.c	20 Aug 2004 11:44:24 -0000	1.14
+++ client.c	1 Sep 2004 04:03:23 -0000
@@ -270,7 +270,8 @@ client_update(struct ntp_peer *p)
 	 * clock filter
 	 * find the offset which arrived with the lowest delay
 	 * use that as the peer update
-	 * invalidate it and all older ones
+	 * Afterwards, invalidate all offsets: since we're going to adjust,
+	 * the old ones will not be valid any more anyway.
 	 */
 
 	for (i = 0; good == 0 && i < OFFSET_ARRAY_SIZE; i++)
@@ -293,8 +294,7 @@ client_update(struct ntp_peer *p)
 	ntp_adjtime();
 
 	for (i = 0; i < OFFSET_ARRAY_SIZE; i++)
-		if (p->reply[i].rcvd <= p->reply[best].rcvd)
-			p->reply[i].good = 0;
+		p->reply[i].good = 0;
 
 	return (0);
 }
Index: configure.ac
===================================================================
RCS file: /usr/local/cvs/openntpd-portable/configure.ac,v
retrieving revision 1.27
diff -u -p -r1.27 configure.ac
--- configure.ac	1 Sep 2004 09:40:20 -0000	1.27
+++ configure.ac	1 Sep 2004 09:40:29 -0000
@@ -44,6 +44,7 @@ AC_CANONICAL_HOST
 case "$host" in
 *-*-linux*)
 	AC_DEFINE(DONT_IGNORE_SIGCHLD,[], [Don't set SIGCHLD to SIG_IGN])
+	AC_DEFINE(USE_ADJTIMEX, [], [Use adjtimex instead of adjtime])
 	;;
 *-*-darwin*)
 	AC_DEFINE(SETEUID_BREAKS_SETUID,[],[setuid after seteuid doesn't work])
Index: defines.h
===================================================================
RCS file: /usr/local/cvs/openntpd-portable/defines.h,v
retrieving revision 1.12
diff -u -p -r1.12 defines.h
--- defines.h	20 Aug 2004 11:28:05 -0000	1.12
+++ defines.h	1 Sep 2004 03:57:26 -0000
@@ -20,6 +20,10 @@
 # define setproctitle(x)
 #endif
 
+#ifdef USE_ADJTIMEX
+# define adjtime(a,b)	(_compat_adjtime((a),(b)))
+#endif
+
 #if !defined(SA_LEN)
 # if defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
 #  define SA_LEN(x)	((x)->sa_len)
Index: openbsd-compat/Makefile.in
===================================================================
RCS file: /usr/local/cvs/openntpd-portable/openbsd-compat/Makefile.in,v
retrieving revision 1.4
diff -u -p -r1.4 Makefile.in
--- openbsd-compat/Makefile.in	16 Jul 2004 11:49:51 -0000	1.4
+++ openbsd-compat/Makefile.in	1 Sep 2004 10:34:50 -0000
@@ -8,6 +8,7 @@ all:	libopenbsd-compat.a
 
 OPENBSD=	asprintf.o daemon.o strlcpy.o
 COMPAT=		bsd-arc4random.o bsd-misc.o uidswap.o
+PORT=		port-linux.o
 
 VPATH=@srcdir@
 CC=@CC@
@@ -22,14 +23,14 @@ INSTALL=@INSTALL@
 
 $(COMPAT): ../config.h
 $(OPENBSD): ../config.h
-
+$(PORT): ../config.h
 
 .c.o:
 	$(CC) $(CFLAGS) $(CPPFLAGS) -c $<
 
-libopenbsd-compat.a:	$(COMPAT) $(OPENBSD)
-	$(AR) rv $@ $(COMPAT) $(OPENBSD)
+libopenbsd-compat.a:	$(COMPAT) $(OPENBSD) $(PORT)
+	$(AR) rv $@ $(COMPAT) $(OPENBSD) $(PORT)
 	$(RANLIB) $@
 
 clean:
-	rm -f $(COMPAT) $(OPENBSD) libopenbsd-compat.a
+	rm -f $(COMPAT) $(OPENBSD) $(PORT) libopenbsd-compat.a
Index: openbsd-compat/openbsd-compat.h
===================================================================
RCS file: /usr/local/cvs/openntpd-portable/openbsd-compat/openbsd-compat.h,v
retrieving revision 1.4
diff -u -p -r1.4 openbsd-compat.h
--- openbsd-compat/openbsd-compat.h	16 Jul 2004 12:50:24 -0000	1.4
+++ openbsd-compat/openbsd-compat.h	1 Sep 2004 04:08:07 -0000
@@ -37,4 +37,9 @@ int      asprintf(char **, const char *,
                 __attribute__((__format__ (printf, 2, 3)));
 #endif
 
+#ifdef USE_ADJTIMEX
+# include <sys/time.h>
+int _compat_adjtime(const struct timeval *, struct timeval *);
+#endif
+
 int permanently_set_uid(struct passwd *);
Index: openbsd-compat/port-linux.c
===================================================================
RCS file: openbsd-compat/port-linux.c
diff -N openbsd-compat/port-linux.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ openbsd-compat/port-linux.c	1 Sep 2004 10:36:34 -0000
@@ -0,0 +1,107 @@
+/* $Id$ */
+
+/*
+ * Copyright (c) 2004 Darren Tucker <dtucker at zip com au>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include "includes.h"
+
+#ifdef USE_ADJTIMEX
+#include <sys/timex.h>
+#include <errno.h>
+#ifdef adjtime
+# undef adjtime
+#endif
+
+/* scale factor used by adjtimex freq param.  1 ppm = 65536 */
+#define ADJTIMEX_FREQ_SCALE 65536
+
+/*
+ * Prevent warnings.  We can't just include ntpd.h because of conflicting
+ * definitions of ntp_adjtime()
+ */
+void log_warn(const char *, ...);
+void log_info(const char *, ...);
+
+int
+_compat_adjtime(const struct timeval *delta, struct timeval *olddelta)
+{
+	static struct timeval tlast = {0,0};
+	static double lastadj = 0;
+	static int synced = -1;
+	struct timeval tnow, tdelta;
+	double adjust, interval, skew;
+	struct timex tmx;
+	int result, saved_errno;
+
+	if (olddelta == NULL)
+		olddelta = &tdelta;
+	adjust = (double)delta->tv_sec;
+	adjust += (double)delta->tv_usec / 1000000;
+
+	result = adjtime(delta, olddelta);
+	saved_errno = errno;
+
+	if (olddelta->tv_sec == 0 && olddelta->tv_usec == 0 &&
+	    synced != INT_MAX)
+		synced++;
+	else
+		synced = 0;
+
+	/* get current kernel params */
+	memset(&tmx, 0, sizeof(tmx));
+	if (adjtimex(&tmx) == -1) {
+		log_warn("adjtimex get failed");
+		return result;
+	}
+	log_info("synced %d kernel skew %0.3f ppm", synced,
+	    (double)tmx.freq / ADJTIMEX_FREQ_SCALE);
+
+	/*
+	 * do skew calculations if we have been synced for 2 or more
+	 * consecutive adjusts
+	 */
+	gettimeofday(&tnow, NULL);
+	if (synced >= 2) {
+		/*
+		 * calculate elapsed time between adjusts, accounting
+		 * for the previous adjust
+		 */
+		interval = (double)(tnow.tv_sec - tlast.tv_sec);
+		interval += (double)(tnow.tv_usec - tlast.tv_usec) / 1000000;
+		interval += lastadj;
+
+		/* calculate skew in parts per million for measured interval */
+		skew = (adjust * 1000000) / interval;
+
+		/*
+		 * Adjust the kernel skew.  We add half the measured skew for
+		 * last interval to the kernel skew, which should
+		 * asymptotically approach the true systematic clock skew.
+		 */
+		tmx.freq += (long)(skew * ADJTIMEX_FREQ_SCALE / 2);
+		tmx.modes = ADJ_FREQUENCY;
+		if (adjtimex(&tmx) == -1)
+			log_warn("adjtimex set freq failed");
+		log_info("interval %0.3fs skew %0.3f kernel skew %0.3f",
+		    interval, skew, (double)tmx.freq / ADJTIMEX_FREQ_SCALE);
+	}
+	lastadj = adjust;
+	tlast = tnow;
+
+	errno = saved_errno;
+	return result;
+}
+#endif
