4841e123fa17d5224b6c312b813caaa091c1b8eb
[openwrt/svn-archive/archive.git] / net / openntpd / patches / 01-adjtimex_linux.patch
1 diff -urN openntpd-3.7p1.orig/client.c openntpd-3.7p1/client.c
2 --- openntpd-3.7p1.orig/client.c 2005-03-13 13:36:38.000000000 +0100
3 +++ openntpd-3.7p1/client.c 2006-02-23 16:27:53.686827824 +0100
4 @@ -306,7 +306,7 @@
5 priv_adjtime();
6
7 for (i = 0; i < OFFSET_ARRAY_SIZE; i++)
8 - if (p->reply[i].rcvd <= p->reply[best].rcvd)
9 + /* if (p->reply[i].rcvd <= p->reply[best].rcvd) */
10 p->reply[i].good = 0;
11
12 return (0);
13 diff -urN openntpd-3.7p1.orig/configure.ac openntpd-3.7p1/configure.ac
14 --- openntpd-3.7p1.orig/configure.ac 2005-05-23 13:11:08.000000000 +0200
15 +++ openntpd-3.7p1/configure.ac 2006-02-23 16:27:53.688827520 +0100
16 @@ -466,6 +466,11 @@
17 [ builtin_arc4random=$withval ]
18 )
19
20 +AC_ARG_WITH(adjtimex,
21 + [ --with-adjtimex Use adjtimex to adjust kernel skew],
22 + [ AC_DEFINE(USE_ADJTIMEX, [], [Use adjust skew with adjtimex (experimental)]) ]
23 +)
24 +
25 # Search for OpenSSL if required.
26 if test "$ac_cv_func_arc4random" != "yes" && test "x$builtin_arc4random" != "xyes"; then
27 saved_CPPFLAGS="$CPPFLAGS"
28 diff -urN openntpd-3.7p1.orig/defines.h openntpd-3.7p1/defines.h
29 --- openntpd-3.7p1.orig/defines.h 2005-05-23 02:16:33.000000000 +0200
30 +++ openntpd-3.7p1/defines.h 2006-02-23 16:27:53.688827520 +0100
31 @@ -20,6 +20,10 @@
32 # define setproctitle(x)
33 #endif
34
35 +#ifdef USE_ADJTIMEX
36 +# define adjtime(a,b) (_compat_adjtime((a),(b)))
37 +#endif
38 +
39 #if !defined(SA_LEN)
40 # if defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
41 # define SA_LEN(x) ((x)->sa_len)
42 diff -urN openntpd-3.7p1.orig/openbsd-compat/Makefile.in openntpd-3.7p1/openbsd-compat/Makefile.in
43 --- openntpd-3.7p1.orig/openbsd-compat/Makefile.in 2004-12-20 00:41:36.000000000 +0100
44 +++ openntpd-3.7p1/openbsd-compat/Makefile.in 2006-02-23 16:27:53.901795144 +0100
45 @@ -9,7 +9,7 @@
46 OPENBSD= asprintf.o daemon.o inet_pton.o strlcpy.o
47 COMPAT= atomicio.o bsd-arc4random.o bsd-misc.o bsd-poll.o \
48 bsd-snprintf.o fake-rfc2553.o uidswap.o
49 -PORT= port-qnx.o
50 +PORT= port-linux.o port-qnx.o
51
52 VPATH=@srcdir@
53 CC=@CC@
54 diff -urN openntpd-3.7p1.orig/openbsd-compat/openbsd-compat.h openntpd-3.7p1/openbsd-compat/openbsd-compat.h
55 --- openntpd-3.7p1.orig/openbsd-compat/openbsd-compat.h 2004-12-19 04:04:22.000000000 +0100
56 +++ openntpd-3.7p1/openbsd-compat/openbsd-compat.h 2006-02-23 16:27:53.948788000 +0100
57 @@ -44,6 +44,11 @@
58 __attribute__((__format__ (printf, 2, 3)));
59 #endif
60
61 +#ifdef USE_ADJTIMEX
62 +# include <sys/time.h>
63 +int _compat_adjtime(const struct timeval *, struct timeval *);
64 +#endif
65 +
66 #ifndef HAVE_INET_PTON
67 int inet_pton(int, const char *, void *);
68 #endif
69 diff -urN openntpd-3.7p1.orig/openbsd-compat/port-linux.c openntpd-3.7p1/openbsd-compat/port-linux.c
70 --- openntpd-3.7p1.orig/openbsd-compat/port-linux.c 1970-01-01 01:00:00.000000000 +0100
71 +++ openntpd-3.7p1/openbsd-compat/port-linux.c 2006-02-23 16:27:53.996780704 +0100
72 @@ -0,0 +1,105 @@
73 +/* $Id$ */
74 +
75 +/*
76 + * Copyright (c) 2004 Darren Tucker <dtucker at zip com au>
77 + *
78 + * Permission to use, copy, modify, and distribute this software for any
79 + * purpose with or without fee is hereby granted, provided that the above
80 + * copyright notice and this permission notice appear in all copies.
81 + *
82 + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
83 + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
84 + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
85 + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
86 + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
87 + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
88 + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
89 + */
90 +
91 +#include "includes.h"
92 +
93 +#ifdef USE_ADJTIMEX
94 +#include <sys/timex.h>
95 +#include <errno.h>
96 +#ifdef adjtime
97 +# undef adjtime
98 +#endif
99 +
100 +#include "ntpd.h"
101 +
102 +/* scale factor used by adjtimex freq param. 1 ppm = 65536 */
103 +#define ADJTIMEX_FREQ_SCALE 65536
104 +
105 +/* maximum change to skew per adjustment, in PPM */
106 +#define MAX_SKEW_DELTA 5.0
107 +
108 +int
109 +_compat_adjtime(const struct timeval *delta, struct timeval *olddelta)
110 +{
111 + static struct timeval tlast = {0,0};
112 + static double tskew = 0;
113 + static int synced = -1;
114 + struct timeval tnow, tdelta;
115 + double skew = 0, newskew, deltaskew, adjust, interval = 0;
116 + struct timex tmx;
117 + int result, saved_errno;
118 +
119 + gettimeofday(&tnow, NULL);
120 + adjust = (double)delta->tv_sec;
121 + adjust += (double)delta->tv_usec / 1000000;
122 +
123 + /* Even if the caller doesn't care about the olddelta, we do */
124 + if (olddelta == NULL)
125 + olddelta = &tdelta;
126 +
127 + result = adjtime(delta, olddelta);
128 + saved_errno = errno;
129 +
130 + if (olddelta->tv_sec == 0 && olddelta->tv_usec == 0 &&
131 + synced != INT_MAX)
132 + synced++;
133 + else
134 + synced = 0;
135 +
136 + /*
137 + * do skew calculations if we have synced
138 + */
139 + if (synced == 0 ) {
140 + tmx.modes = 0;
141 + if (adjtimex(&tmx) == -1)
142 + log_warn("adjtimex get failed");
143 + else
144 + tskew = (double)tmx.freq / ADJTIMEX_FREQ_SCALE;
145 + } else if (synced >= 1) {
146 + interval = (double)(tnow.tv_sec - tlast.tv_sec);
147 + interval += (double)(tnow.tv_usec - tlast.tv_usec) / 1000000;
148 +
149 + skew = (adjust * 1000000) / interval;
150 + newskew = ((tskew * synced) + skew) / synced;
151 + deltaskew = newskew - tskew;
152 +
153 + if (deltaskew > MAX_SKEW_DELTA) {
154 + log_info("skew change %0.3lf exceeds limit", deltaskew);
155 + tskew += MAX_SKEW_DELTA;
156 + } else if (deltaskew < -MAX_SKEW_DELTA) {
157 + log_info("skew change %0.3lf exceeds limit", deltaskew);
158 + tskew -= MAX_SKEW_DELTA;
159 + } else {
160 + tskew = newskew;
161 + }
162 +
163 + /* Adjust the kernel skew. */
164 + tmx.freq = (long)(tskew * ADJTIMEX_FREQ_SCALE);
165 + tmx.modes = ADJ_FREQUENCY;
166 + if (adjtimex(&tmx) == -1)
167 + log_warn("adjtimex set freq failed");
168 + }
169 +
170 + log_debug("interval %0.3lf skew %0.3lf total skew %0.3lf", interval,
171 + skew, tskew);
172 +
173 + tlast = tnow;
174 + errno = saved_errno;
175 + return result;
176 +}
177 +#endif