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