4 * systime -- routines to fiddle a UNIX clock.
6 * ATTENTION: Get approval from Dave Mills on all changes to this file!
9 #include "ntp_machine.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>
22 #endif /* HAVE_UTMP_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
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) */
59 * get_systime - return system time in NTP timestamp format.
63 l_fp
*now
/* system time */
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
);
78 getclock(TIMEOFDAY
, &ts
);
80 now
->l_i
= (int32
)ts
.tv_sec
+ JAN_1970
;
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
;
90 } else if (dtemp
< 0) {
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
;
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
;
114 } else if (dtemp
< 0) {
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 */
130 double now
/* adjustment (s) */
133 struct timeval adjtv
; /* new adjustment */
134 struct timeval oadjtv
; /* residual adjustment */
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
;
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()
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");
179 * step_systime - step the system clock.
186 struct timeval timetv
, adjtv
, oldtimetv
;
189 #if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_GETCLOCK)
193 dtemp
= sys_residual
+ now
;
197 adjtv
.tv_sec
= (int32
)dtemp
;
198 adjtv
.tv_usec
= (u_int32
)((dtemp
-
199 (double)adjtv
.tv_sec
) * 1e6
+ .5);
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
);
209 (void) getclock(TIMEOFDAY
, &ts
);
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 */
221 printf("step_systime: step %.6f residual %.6f\n", now
, sys_residual
);
224 timetv
.tv_sec
-= adjtv
.tv_sec
;
225 timetv
.tv_usec
-= adjtv
.tv_usec
;
226 if (timetv
.tv_usec
< 0) {
228 timetv
.tv_usec
+= 1000000;
231 timetv
.tv_sec
+= adjtv
.tv_sec
;
232 timetv
.tv_usec
+= adjtv
.tv_usec
;
233 if (timetv
.tv_usec
>= 1000000) {
235 timetv
.tv_usec
-= 1000000;
238 if (ntp_set_tod(&timetv
, NULL
) != 0) {
239 msyslog(LOG_ERR
, "step-systime: %m");
244 #ifdef NEED_HPUX_ADJTIME
246 * CHECKME: is this correct when called by ntpdate?????
252 * FreeBSD, for example, has:
254 * char ut_line[UT_LINESIZE];
255 * char ut_name[UT_NAMESIZE];
256 * char ut_host[UT_HOSTSIZE];
259 * and appends line="|", name="date", host="", time for the OLD
260 * and appends line="{", name="date", host="", time for the NEW
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
)
282 memset((char *)&ut
, 0, sizeof(ut
));
285 memset((char *)&utx
, 0, sizeof(utx
));
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
;
297 ut
.ut_type
= NEW_TIME
;
298 (void)strcpy(ut
.ut_line
, NTIME_MSG
);
299 ut
.ut_time
= timetv
.tv_sec
;
302 # else /* not HAVE_PUTUTLINE */
303 # endif /* not HAVE_PUTUTLINE */
304 #endif /* UPDATE_UTMP */
309 # ifdef HAVE_PUTUTXLINE
310 utx
.ut_type
= OLD_TIME
;
311 (void)strcpy(utx
.ut_line
, OTIME_MSG
);
312 utx
.ut_tv
= oldtimetv
;
315 utx
.ut_type
= NEW_TIME
;
316 (void)strcpy(utx
.ut_line
, NTIME_MSG
);
320 # else /* not HAVE_PUTUTXLINE */
321 # endif /* not HAVE_PUTUTXLINE */
322 #endif /* UPDATE_UTMPX */
327 # ifdef HAVE_PUTUTLINE
329 ut
.ut_type
= OLD_TIME
;
330 (void)strcpy(ut
.ut_line
, OTIME_MSG
);
331 ut
.ut_time
= oldtimetv
.tv_sec
;
333 ut
.ut_type
= NEW_TIME
;
334 (void)strcpy(ut
.ut_line
, NTIME_MSG
);
335 ut
.ut_time
= timetv
.tv_sec
;
338 # else /* not HAVE_PUTUTLINE */
339 # endif /* not HAVE_PUTUTLINE */
340 #endif /* UPDATE_WTMP */
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
;
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 */
373 * Clock routines for the simulator - Harish Nair, with help
378 * The code that used to be here has been moved to ntpsim.c,
379 * where, IMHO, it rightfully belonged.