2 * systime -- routines to fiddle a UNIX clock.
4 * ATTENTION: Get approval from Dave Mills on all changes to this file!
10 #include "ntp_syslog.h"
11 #include "ntp_stdlib.h"
12 #include "ntp_random.h"
14 #include "timevalops.h"
15 #include "timespecops.h"
16 #include "ntp_calendar.h"
18 #ifdef HAVE_SYS_PARAM_H
19 # include <sys/param.h>
23 #endif /* HAVE_UTMP_H */
26 #endif /* HAVE_UTMPX_H */
28 int allow_panic = FALSE; /* allow panic correction (-g) */
29 int enable_panic_check = TRUE; /* Can we check allow_panic's state? */
31 #ifndef USE_COMPILETIME_PIVOT
32 # define USE_COMPILETIME_PIVOT 1
36 * These routines (get_systime, step_systime, adj_systime) implement an
37 * interface between the system independent NTP clock and the Unix
38 * system clock in various architectures and operating systems. Time is
39 * a precious quantity in these routines and every effort is made to
40 * minimize errors by unbiased rounding and amortizing adjustment
43 * In order to improve the apparent resolution, provide unbiased
44 * rounding and most importantly ensure that the readings cannot be
45 * predicted, the low-order unused portion of the time below the minimum
46 * time to read the clock is filled with an unbiased random fuzz.
48 * The sys_tick variable specifies the system clock tick interval in
49 * seconds, for stepping clocks, defined as those which return times
50 * less than MINSTEP greater than the previous reading. For systems that
51 * use a high-resolution counter such that each clock reading is always
52 * at least MINSTEP greater than the prior, sys_tick is the time to read
55 * The sys_fuzz variable measures the minimum time to read the system
56 * clock, regardless of its precision. When reading the system clock
57 * using get_systime() after sys_tick and sys_fuzz have been determined,
58 * ntpd ensures each unprocessed clock reading is no less than sys_fuzz
59 * later than the prior unprocessed reading, and then fuzzes the bits
60 * below sys_fuzz in the timestamp returned, ensuring each of its
61 * resulting readings is strictly later than the previous.
63 * When slewing the system clock using adj_systime() (with the kernel
64 * loop discipline unavailable or disabled), adjtime() offsets are
65 * quantized to sys_tick, if sys_tick is greater than sys_fuzz, which
66 * is to say if the OS presents a stepping clock. Otherwise, offsets
67 * are quantized to the microsecond resolution of adjtime()'s timeval
68 * input. The remaining correction sys_residual is carried into the
69 * next adjtime() and meanwhile is also factored into get_systime()
72 double sys_tick = 0; /* tick size or time to read (s) */
73 double sys_fuzz = 0; /* min. time to read the clock (s) */
74 long sys_fuzz_nsec = 0; /* min. time to read the clock (ns) */
75 double measured_tick; /* non-overridable sys_tick (s) */
76 double sys_residual = 0; /* adjustment residue (s) */
77 int trunc_os_clock; /* sys_tick > measured_tick */
78 time_stepped_callback step_callback;
81 /* perlinger@ntp.org: As 'get_sysime()' does it's own check for clock
82 * backstepping, this could probably become a local variable in
83 * 'get_systime()' and the cruft associated with communicating via a
84 * static value could be removed after the v4.2.8 release.
86 static int lamport_violated; /* clock was stepped back */
90 static int systime_init_done;
91 # define DONE_SYSTIME_INIT() systime_init_done = TRUE
93 # define DONE_SYSTIME_INIT() do {} while (FALSE)
96 #ifdef HAVE_SIGNALED_IO
101 CRITICAL_SECTION get_systime_cs;
111 INSIST(sys_fuzz >= 0);
112 INSIST(sys_fuzz <= 1.0);
113 sys_fuzz_nsec = (long)(sys_fuzz * 1e9 + 0.5);
120 INIT_GET_SYSTIME_CRITSEC();
121 INIT_WIN_PRECISE_TIME();
126 #ifndef SIM /* ntpsim.c has get_systime() and friends for sim */
130 struct timespec * tsp
136 #if defined(HAVE_CLOCK_GETTIME)
137 rc = clock_gettime(CLOCK_REALTIME, tsp);
138 #elif defined(HAVE_GETCLOCK)
139 rc = getclock(TIMEOFDAY, tsp);
143 rc = GETTIMEOFDAY(&tv, NULL);
144 tsp->tv_sec = tv.tv_sec;
145 tsp->tv_nsec = tv.tv_usec * 1000;
148 msyslog(LOG_ERR, "read system clock failed: %m (%d)",
153 if (trunc_os_clock) {
154 ticks = (long)((tsp->tv_nsec * 1e-9) / sys_tick);
155 tsp->tv_nsec = (long)(ticks * 1e9 * sys_tick);
161 * get_systime - return system time in NTP timestamp format.
165 l_fp *now /* system time */
168 static struct timespec ts_last; /* last sampled os time */
169 static struct timespec ts_prev; /* prior os time */
170 static l_fp lfp_prev; /* prior result */
171 static double dfuzz_prev; /* prior fuzz */
172 struct timespec ts; /* seconds and nanoseconds */
173 struct timespec ts_min; /* earliest permissible */
174 struct timespec ts_lam; /* lamport fictional increment */
175 struct timespec ts_prev_log; /* for msyslog only */
183 DEBUG_REQUIRE(systime_init_done);
184 ENTER_GET_SYSTIME_CRITSEC();
186 /* First check if here was a Lamport violation, that is, two
187 * successive calls to 'get_ostime()' resulted in negative
188 * time difference. Use a few milliseconds of permissible
189 * tolerance -- being too sharp can hurt here. (This is intented
190 * for the Win32 target, where the HPC interpolation might
191 * introduce small steps backward. It should not be an issue on
192 * systems where get_ostime() results in a true syscall.)
194 if (cmp_tspec(add_tspec_ns(ts, 50000000), ts_last) < 0)
195 lamport_violated = 1;
199 * After default_get_precision() has set a nonzero sys_fuzz,
200 * ensure every reading of the OS clock advances by at least
201 * sys_fuzz over the prior reading, thereby assuring each
202 * fuzzed result is strictly later than the prior. Limit the
203 * necessary fiction to 1 second.
205 if (!USING_SIGIO()) {
206 ts_min = add_tspec_ns(ts_prev, sys_fuzz_nsec);
207 if (cmp_tspec(ts, ts_min) < 0) {
208 ts_lam = sub_tspec(ts_min, ts);
209 if (ts_lam.tv_sec > 0 && !lamport_violated) {
211 "get_systime Lamport advance exceeds one second (%.9f)",
213 1e-9 * ts_lam.tv_nsec);
216 if (!lamport_violated)
219 ts_prev_log = ts_prev;
223 * Quiet "ts_prev_log.tv_sec may be used uninitialized"
224 * warning from x86 gcc 4.5.2.
229 /* convert from timespec to l_fp fixed-point */
230 result = tspec_stamp_to_lfp(ts);
235 dfuzz = ntp_random() * 2. / FRAC * sys_fuzz;
236 DTOLFP(dfuzz, &lfpfuzz);
237 L_ADD(&result, &lfpfuzz);
240 * Ensure result is strictly greater than prior result (ignoring
241 * sys_residual's effect for now) once sys_fuzz has been
244 if (!USING_SIGIO()) {
245 if (!L_ISZERO(&lfp_prev) && !lamport_violated) {
246 if (!L_ISGTU(&result, &lfp_prev) &&
248 msyslog(LOG_ERR, "ts_prev %s ts_min %s",
249 tspectoa(ts_prev_log),
251 msyslog(LOG_ERR, "ts %s", tspectoa(ts));
252 msyslog(LOG_ERR, "sys_fuzz %ld nsec, prior fuzz %.9f",
253 sys_fuzz_nsec, dfuzz_prev);
254 msyslog(LOG_ERR, "this fuzz %.9f",
257 L_SUB(&lfpdelta, &result);
258 LFPTOD(&lfpdelta, ddelta);
260 "prev get_systime 0x%x.%08x is %.9f later than 0x%x.%08x",
261 lfp_prev.l_ui, lfp_prev.l_uf,
262 ddelta, result.l_ui, result.l_uf);
267 if (lamport_violated)
268 lamport_violated = FALSE;
270 LEAVE_GET_SYSTIME_CRITSEC();
276 * adj_systime - adjust system time by the argument.
278 #if !defined SYS_WINNT
279 int /* 0 okay, 1 error */
281 double now /* adjustment (s) */
284 struct timeval adjtv; /* new adjustment */
285 struct timeval oadjtv; /* residual adjustment */
286 double quant; /* quantize to multiples of */
292 * The Windows port adj_systime() depends on being called each
293 * second even when there's no additional correction, to allow
294 * emulation of adjtime() behavior on top of an API that simply
295 * sets the current rate. This POSIX implementation needs to
296 * ignore invocations with zero correction, otherwise ongoing
297 * EVNT_NSET adjtime() can be aborted by a tiny adjtime()
298 * triggered by sys_residual.
301 if (enable_panic_check && allow_panic) {
302 msyslog(LOG_ERR, "adj_systime: allow_panic is TRUE!");
303 INSIST(!allow_panic);
309 * Most Unix adjtime() implementations adjust the system clock
310 * in microsecond quanta, but some adjust in 10-ms quanta. We
311 * carefully round the adjustment to the nearest quantum, then
312 * adjust in quanta and keep the residue for later.
314 dtemp = now + sys_residual;
319 adjtv.tv_sec = (long)dtemp;
320 dtemp -= adjtv.tv_sec;
321 if (sys_tick > sys_fuzz)
325 ticks = (long)(dtemp / quant + .5);
326 adjtv.tv_usec = (long)(ticks * quant * 1.e6 + .5);
327 /* The rounding in the conversions could us push over the
328 * limits: make sure the result is properly normalised!
329 * note: sign comes later, all numbers non-negative here.
331 if (adjtv.tv_usec >= 1000000) {
333 adjtv.tv_usec -= 1000000;
336 /* set the new residual with leftover from correction */
337 sys_residual = dtemp - adjtv.tv_usec * 1.e-6;
340 * Convert to signed seconds and microseconds for the Unix
341 * adjtime() system call. Note we purposely lose the adjtime()
345 adjtv.tv_sec = -adjtv.tv_sec;
346 adjtv.tv_usec = -adjtv.tv_usec;
347 sys_residual = -sys_residual;
349 if (adjtv.tv_sec != 0 || adjtv.tv_usec != 0) {
350 if (adjtime(&adjtv, &oadjtv) < 0) {
351 msyslog(LOG_ERR, "adj_systime: %m");
352 if (enable_panic_check && allow_panic) {
353 msyslog(LOG_ERR, "adj_systime: allow_panic is TRUE!");
358 if (enable_panic_check && allow_panic) {
359 msyslog(LOG_ERR, "adj_systime: allow_panic is TRUE!");
367 * step_systime - step the system clock.
375 time_t pivot; /* for ntp era unfolding */
376 struct timeval timetv, tvlast, tvdiff;
377 struct timespec timets;
379 l_fp fp_ofs, fp_sys; /* offset and target system time in FP */
382 * Get pivot time for NTP era unfolding. Since we don't step
383 * very often, we can afford to do the whole calculation from
384 * scratch. And we're not in the time-critical path yet.
386 #if SIZEOF_TIME_T > 4
388 * This code makes sure the resulting time stamp for the new
389 * system time is in the 2^32 seconds starting at 1970-01-01,
393 #if USE_COMPILETIME_PIVOT
395 * Add the compile time minus 10 years to get a possible target
396 * area of (compile time - 10 years) to (compile time + 126
397 * years). This should be sufficient for a given binary of
400 if (ntpcal_get_build_date(&jd)) {
402 pivot += ntpcal_date_to_time(&jd);
405 "step-systime: assume 1970-01-01 as build date");
409 #endif /* USE_COMPILETIME_PIVOT */
412 /* This makes sure the resulting time stamp is on or after
413 * 1969-12-31/23:59:59 UTC and gives us additional two years,
414 * from the change of NTP era in 2036 to the UNIX rollover in
415 * 2038. (Minus one second, but that won't hurt.) We *really*
416 * need a longer 'time_t' after that! Or a different baseline,
417 * but that would cause other serious trouble, too.
422 /* get the complete jump distance as l_fp */
423 DTOLFP(sys_residual, &fp_sys);
424 DTOLFP(step, &fp_ofs);
425 L_ADD(&fp_ofs, &fp_sys);
427 /* ---> time-critical path starts ---> */
429 /* get the current time as l_fp (without fuzz) and as struct timeval */
431 fp_sys = tspec_stamp_to_lfp(timets);
432 tvlast.tv_sec = timets.tv_sec;
433 tvlast.tv_usec = (timets.tv_nsec + 500) / 1000;
435 /* get the target time as l_fp */
436 L_ADD(&fp_sys, &fp_ofs);
438 /* unfold the new system time */
439 timetv = lfp_stamp_to_tval(fp_sys, &pivot);
441 /* now set new system time */
442 if (ntp_set_tod(&timetv, NULL) != 0) {
443 msyslog(LOG_ERR, "step-systime: %m");
444 if (enable_panic_check && allow_panic) {
445 msyslog(LOG_ERR, "step_systime: allow_panic is TRUE!");
450 /* <--- time-critical path ended with 'ntp_set_tod()' <--- */
453 lamport_violated = (step < 0);
457 #ifdef NEED_HPUX_ADJTIME
459 * CHECKME: is this correct when called by ntpdate?????
465 * FreeBSD, for example, has:
467 * char ut_line[UT_LINESIZE];
468 * char ut_name[UT_NAMESIZE];
469 * char ut_host[UT_HOSTSIZE];
472 * and appends line="|", name="date", host="", time for the OLD
473 * and appends line="{", name="date", host="", time for the NEW // }
476 * Some OSes have utmp, some have utmpx.
480 * Write old and new time entries in utmp and wtmp if step
481 * adjustment is greater than one second.
483 * This might become even Uglier...
485 tvdiff = abs_tval(sub_tval(timetv, tvlast));
486 if (tvdiff.tv_sec > 0) {
504 # ifdef HAVE_PUTUTLINE
506 # define _PATH_UTMP UTMP_FILE
508 utmpname(_PATH_UTMP);
509 ut.ut_type = OLD_TIME;
510 strlcpy(ut.ut_line, OTIME_MSG, sizeof(ut.ut_line));
511 ut.ut_time = tvlast.tv_sec;
514 ut.ut_type = NEW_TIME;
515 strlcpy(ut.ut_line, NTIME_MSG, sizeof(ut.ut_line));
516 ut.ut_time = timetv.tv_sec;
520 # else /* not HAVE_PUTUTLINE */
521 # endif /* not HAVE_PUTUTLINE */
522 #endif /* UPDATE_UTMP */
527 # ifdef HAVE_PUTUTXLINE
528 utx.ut_type = OLD_TIME;
529 strlcpy(utx.ut_line, OTIME_MSG, sizeof(utx.ut_line));
533 utx.ut_type = NEW_TIME;
534 strlcpy(utx.ut_line, NTIME_MSG, sizeof(utx.ut_line));
539 # else /* not HAVE_PUTUTXLINE */
540 # endif /* not HAVE_PUTUTXLINE */
541 #endif /* UPDATE_UTMPX */
546 # ifdef HAVE_PUTUTLINE
548 # define _PATH_WTMP WTMP_FILE
550 utmpname(_PATH_WTMP);
551 ut.ut_type = OLD_TIME;
552 strlcpy(ut.ut_line, OTIME_MSG, sizeof(ut.ut_line));
553 ut.ut_time = tvlast.tv_sec;
556 ut.ut_type = NEW_TIME;
557 strlcpy(ut.ut_line, NTIME_MSG, sizeof(ut.ut_line));
558 ut.ut_time = timetv.tv_sec;
562 # else /* not HAVE_PUTUTLINE */
563 # endif /* not HAVE_PUTUTLINE */
564 #endif /* UPDATE_WTMP */
569 # ifdef HAVE_PUTUTXLINE
570 utx.ut_type = OLD_TIME;
572 strlcpy(utx.ut_line, OTIME_MSG, sizeof(utx.ut_line));
573 # ifdef HAVE_UPDWTMPX
574 updwtmpx(WTMPX_FILE, &utx);
575 # else /* not HAVE_UPDWTMPX */
576 # endif /* not HAVE_UPDWTMPX */
577 # else /* not HAVE_PUTUTXLINE */
578 # endif /* not HAVE_PUTUTXLINE */
579 # ifdef HAVE_PUTUTXLINE
580 utx.ut_type = NEW_TIME;
582 strlcpy(utx.ut_line, NTIME_MSG, sizeof(utx.ut_line));
583 # ifdef HAVE_UPDWTMPX
584 updwtmpx(WTMPX_FILE, &utx);
585 # else /* not HAVE_UPDWTMPX */
586 # endif /* not HAVE_UPDWTMPX */
587 # else /* not HAVE_PUTUTXLINE */
588 # endif /* not HAVE_PUTUTXLINE */
589 #endif /* UPDATE_WTMPX */
592 if (enable_panic_check && allow_panic) {
593 msyslog(LOG_ERR, "step_systime: allow_panic is TRUE!");
594 INSIST(!allow_panic);