Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / ntp / dist / libntp / systime.c
blobbe5f1f1cfcabe549a32c6f6307398304c6ceaf6c
1 /* $NetBSD$ */
3 /*
4 * systime -- routines to fiddle a UNIX clock.
6 * ATTENTION: Get approval from Dave Mills on all changes to this file!
8 */
9 #include "ntp_machine.h"
10 #include "ntp_fp.h"
11 #include "ntp_syslog.h"
12 #include "ntp_unixtime.h"
13 #include "ntp_stdlib.h"
14 #include "ntp_random.h"
15 #include "ntpd.h" /* for sys_precision */
17 #ifdef HAVE_SYS_PARAM_H
18 # include <sys/param.h>
19 #endif
20 #ifdef HAVE_UTMP_H
21 # include <utmp.h>
22 #endif /* HAVE_UTMP_H */
23 #ifdef HAVE_UTMPX_H
24 # include <utmpx.h>
25 #endif /* HAVE_UTMPX_H */
28 #define FUZZ 500e-6 /* fuzz pivot */
31 * These routines (get_systime, step_systime, adj_systime) implement an
32 * interface between the system independent NTP clock and the Unix
33 * system clock in various architectures and operating systems. Time is
34 * a precious quantity in these routines and every effort is made to
35 * minimize errors by unbiased rounding and amortizing adjustment
36 * residues.
38 * In order to improve the apparent resolution, provide unbiased
39 * rounding and insure that the readings cannot be predicted, the low-
40 * order unused portion of the time below the resolution limit is filled
41 * with an unbiased random fuzz.
43 * The sys_tick variable secifies the system clock tick interval in
44 * seconds. For systems that can interpolate between timer interrupts,
45 * the resolution is presumed much less than the time to read the system
46 * clock, which is the value of sys_tick after the precision has been
47 * determined. For those systems that cannot interpolate between timer
48 * interrupts, sys_tick will be much larger in the order of 10 ms, so the
49 * fuzz should be that value. For Sunses the tick is not interpolated, but
50 * the system clock is derived from a 2-MHz oscillator, so the resolution
51 * is 500 ns and sys_tick is 500 ns.
53 double sys_tick = 0; /* precision (time to read the clock) */
54 double sys_residual = 0; /* adjustment residue (s) */
56 #ifndef SIM
59 * get_systime - return system time in NTP timestamp format.
61 void
62 get_systime(
63 l_fp *now /* system time */
66 double dtemp;
68 #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_GETCLOCK)
69 struct timespec ts; /* seconds and nanoseconds */
72 * Convert Unix timespec from seconds and nanoseconds to NTP
73 * seconds and fraction.
75 # ifdef HAVE_CLOCK_GETTIME
76 clock_gettime(CLOCK_REALTIME, &ts);
77 # else
78 getclock(TIMEOFDAY, &ts);
79 # endif
80 now->l_i = (int32)ts.tv_sec + JAN_1970;
81 dtemp = 0;
82 if (sys_tick > FUZZ)
83 dtemp = ntp_random() * 2. / FRAC * sys_tick * 1e9;
84 else if (sys_tick > 0)
85 dtemp = ntp_random() * 2. / FRAC;
86 dtemp = (ts.tv_nsec + dtemp) * 1e-9 + sys_residual;
87 if (dtemp >= 1.) {
88 dtemp -= 1.;
89 now->l_i++;
90 } else if (dtemp < 0) {
91 dtemp += 1.;
92 now->l_i--;
94 now->l_uf = (u_int32)(dtemp * FRAC);
96 #else /* HAVE_CLOCK_GETTIME || HAVE_GETCLOCK */
97 struct timeval tv; /* seconds and microseconds */
100 * Convert Unix timeval from seconds and microseconds to NTP
101 * seconds and fraction.
103 GETTIMEOFDAY(&tv, NULL);
104 now->l_i = tv.tv_sec + JAN_1970;
105 dtemp = 0;
106 if (sys_tick > FUZZ)
107 dtemp = ntp_random() * 2. / FRAC * sys_tick * 1e6;
108 else if (sys_tick > 0)
109 dtemp = ntp_random() * 2. / FRAC;
110 dtemp = (tv.tv_usec + dtemp) * 1e-6 + sys_residual;
111 if (dtemp >= 1.) {
112 dtemp -= 1.;
113 now->l_i++;
114 } else if (dtemp < 0) {
115 dtemp += 1.;
116 now->l_i--;
118 now->l_uf = (u_int32)(dtemp * FRAC);
120 #endif /* HAVE_CLOCK_GETTIME || HAVE_GETCLOCK */
125 * adj_systime - adjust system time by the argument.
127 #if !defined SYS_WINNT
128 int /* 0 okay, 1 error */
129 adj_systime(
130 double now /* adjustment (s) */
133 struct timeval adjtv; /* new adjustment */
134 struct timeval oadjtv; /* residual adjustment */
135 double dtemp;
136 long ticks;
137 int isneg = 0;
140 * Most Unix adjtime() implementations adjust the system clock
141 * in microsecond quanta, but some adjust in 10-ms quanta. We
142 * carefully round the adjustment to the nearest quantum, then
143 * adjust in quanta and keep the residue for later.
145 dtemp = now + sys_residual;
146 if (dtemp < 0) {
147 isneg = 1;
148 dtemp = -dtemp;
150 adjtv.tv_sec = (long)dtemp;
151 dtemp -= adjtv.tv_sec;
152 ticks = (long)(dtemp / sys_tick + .5);
153 adjtv.tv_usec = (long)(ticks * sys_tick * 1e6);
154 dtemp -= adjtv.tv_usec / 1e6;
155 sys_residual = dtemp;
158 * Convert to signed seconds and microseconds for the Unix
159 * adjtime() system call. Note we purposely lose the adjtime()
160 * leftover.
162 if (isneg) {
163 adjtv.tv_sec = -adjtv.tv_sec;
164 adjtv.tv_usec = -adjtv.tv_usec;
165 sys_residual = -sys_residual;
167 if (adjtv.tv_sec != 0 || adjtv.tv_usec != 0) {
168 if (adjtime(&adjtv, &oadjtv) < 0) {
169 msyslog(LOG_ERR, "adj_systime: %m");
170 return (0);
173 return (1);
175 #endif
179 * step_systime - step the system clock.
182 step_systime(
183 double now
186 struct timeval timetv, adjtv, oldtimetv;
187 int isneg = 0;
188 double dtemp;
189 #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_GETCLOCK)
190 struct timespec ts;
191 #endif
193 dtemp = sys_residual + now;
194 if (dtemp < 0) {
195 isneg = 1;
196 dtemp = - dtemp;
197 adjtv.tv_sec = (int32)dtemp;
198 adjtv.tv_usec = (u_int32)((dtemp -
199 (double)adjtv.tv_sec) * 1e6 + .5);
200 } else {
201 adjtv.tv_sec = (int32)dtemp;
202 adjtv.tv_usec = (u_int32)((dtemp -
203 (double)adjtv.tv_sec) * 1e6 + .5);
205 #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_GETCLOCK)
206 # ifdef HAVE_CLOCK_GETTIME
207 (void) clock_gettime(CLOCK_REALTIME, &ts);
208 # else
209 (void) getclock(TIMEOFDAY, &ts);
210 # endif
211 timetv.tv_sec = ts.tv_sec;
212 timetv.tv_usec = ts.tv_nsec / 1000;
213 #else /* not HAVE_GETCLOCK */
214 (void) GETTIMEOFDAY(&timetv, (struct timezone *)0);
215 #endif /* not HAVE_GETCLOCK */
217 oldtimetv = timetv;
219 #ifdef DEBUG
220 if (debug)
221 printf("step_systime: step %.6f residual %.6f\n", now, sys_residual);
222 #endif
223 if (isneg) {
224 timetv.tv_sec -= adjtv.tv_sec;
225 timetv.tv_usec -= adjtv.tv_usec;
226 if (timetv.tv_usec < 0) {
227 timetv.tv_sec--;
228 timetv.tv_usec += 1000000;
230 } else {
231 timetv.tv_sec += adjtv.tv_sec;
232 timetv.tv_usec += adjtv.tv_usec;
233 if (timetv.tv_usec >= 1000000) {
234 timetv.tv_sec++;
235 timetv.tv_usec -= 1000000;
238 if (ntp_set_tod(&timetv, NULL) != 0) {
239 msyslog(LOG_ERR, "step-systime: %m");
240 return (0);
242 sys_residual = 0;
244 #ifdef NEED_HPUX_ADJTIME
246 * CHECKME: is this correct when called by ntpdate?????
248 _clear_adjtime();
249 #endif
252 * FreeBSD, for example, has:
253 * struct utmp {
254 * char ut_line[UT_LINESIZE];
255 * char ut_name[UT_NAMESIZE];
256 * char ut_host[UT_HOSTSIZE];
257 * long ut_time;
258 * };
259 * and appends line="|", name="date", host="", time for the OLD
260 * and appends line="{", name="date", host="", time for the NEW
261 * to _PATH_WTMP .
263 * Some OSes have utmp, some have utmpx.
267 * Write old and new time entries in utmp and wtmp if step
268 * adjustment is greater than one second.
270 * This might become even Uglier...
272 if (oldtimetv.tv_sec != timetv.tv_sec)
274 #ifdef HAVE_UTMP_H
275 struct utmp ut;
276 #endif
277 #ifdef HAVE_UTMPX_H
278 struct utmpx utx;
279 #endif
281 #ifdef HAVE_UTMP_H
282 memset((char *)&ut, 0, sizeof(ut));
283 #endif
284 #ifdef HAVE_UTMPX_H
285 memset((char *)&utx, 0, sizeof(utx));
286 #endif
288 /* UTMP */
290 #ifdef UPDATE_UTMP
291 # ifdef HAVE_PUTUTLINE
292 ut.ut_type = OLD_TIME;
293 (void)strcpy(ut.ut_line, OTIME_MSG);
294 ut.ut_time = oldtimetv.tv_sec;
295 pututline(&ut);
296 setutent();
297 ut.ut_type = NEW_TIME;
298 (void)strcpy(ut.ut_line, NTIME_MSG);
299 ut.ut_time = timetv.tv_sec;
300 pututline(&ut);
301 endutent();
302 # else /* not HAVE_PUTUTLINE */
303 # endif /* not HAVE_PUTUTLINE */
304 #endif /* UPDATE_UTMP */
306 /* UTMPX */
308 #ifdef UPDATE_UTMPX
309 # ifdef HAVE_PUTUTXLINE
310 utx.ut_type = OLD_TIME;
311 (void)strcpy(utx.ut_line, OTIME_MSG);
312 utx.ut_tv = oldtimetv;
313 pututxline(&utx);
314 setutxent();
315 utx.ut_type = NEW_TIME;
316 (void)strcpy(utx.ut_line, NTIME_MSG);
317 utx.ut_tv = timetv;
318 pututxline(&utx);
319 endutxent();
320 # else /* not HAVE_PUTUTXLINE */
321 # endif /* not HAVE_PUTUTXLINE */
322 #endif /* UPDATE_UTMPX */
324 /* WTMP */
326 #ifdef UPDATE_WTMP
327 # ifdef HAVE_PUTUTLINE
328 utmpname(WTMP_FILE);
329 ut.ut_type = OLD_TIME;
330 (void)strcpy(ut.ut_line, OTIME_MSG);
331 ut.ut_time = oldtimetv.tv_sec;
332 pututline(&ut);
333 ut.ut_type = NEW_TIME;
334 (void)strcpy(ut.ut_line, NTIME_MSG);
335 ut.ut_time = timetv.tv_sec;
336 pututline(&ut);
337 endutent();
338 # else /* not HAVE_PUTUTLINE */
339 # endif /* not HAVE_PUTUTLINE */
340 #endif /* UPDATE_WTMP */
342 /* WTMPX */
344 #ifdef UPDATE_WTMPX
345 # ifdef HAVE_PUTUTXLINE
346 utx.ut_type = OLD_TIME;
347 utx.ut_tv = oldtimetv;
348 (void)strcpy(utx.ut_line, OTIME_MSG);
349 # ifdef HAVE_UPDWTMPX
350 updwtmpx(WTMPX_FILE, &utx);
351 # else /* not HAVE_UPDWTMPX */
352 # endif /* not HAVE_UPDWTMPX */
353 # else /* not HAVE_PUTUTXLINE */
354 # endif /* not HAVE_PUTUTXLINE */
355 # ifdef HAVE_PUTUTXLINE
356 utx.ut_type = NEW_TIME;
357 utx.ut_tv = timetv;
358 (void)strcpy(utx.ut_line, NTIME_MSG);
359 # ifdef HAVE_UPDWTMPX
360 updwtmpx(WTMPX_FILE, &utx);
361 # else /* not HAVE_UPDWTMPX */
362 # endif /* not HAVE_UPDWTMPX */
363 # else /* not HAVE_PUTUTXLINE */
364 # endif /* not HAVE_PUTUTXLINE */
365 #endif /* UPDATE_WTMPX */
368 return (1);
371 #else /* SIM */
373 * Clock routines for the simulator - Harish Nair, with help
377 /* SK:
378 * The code that used to be here has been moved to ntpsim.c,
379 * where, IMHO, it rightfully belonged.
382 #endif