systime.c revision 293896
1/* 2 * systime -- routines to fiddle a UNIX clock. 3 * 4 * ATTENTION: Get approval from Dave Mills on all changes to this file! 5 * 6 */ 7#include <config.h> 8 9#include "ntp.h" 10#include "ntp_syslog.h" 11#include "ntp_stdlib.h" 12#include "ntp_random.h" 13#include "iosignal.h" 14#include "timevalops.h" 15#include "timespecops.h" 16#include "ntp_calendar.h" 17 18#ifdef HAVE_SYS_PARAM_H 19# include <sys/param.h> 20#endif 21#ifdef HAVE_UTMP_H 22# include <utmp.h> 23#endif /* HAVE_UTMP_H */ 24#ifdef HAVE_UTMPX_H 25# include <utmpx.h> 26#endif /* HAVE_UTMPX_H */ 27 28int allow_panic = FALSE; /* allow panic correction (-g) */ 29int enable_panic_check = TRUE; /* Can we check allow_panic's state? */ 30 31#ifndef USE_COMPILETIME_PIVOT 32# define USE_COMPILETIME_PIVOT 1 33#endif 34 35/* 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 41 * residues. 42 * 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. 47 * 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 53 * the system clock. 54 * 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. 62 * 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() 70 * readings. 71 */ 72double sys_tick = 0; /* tick size or time to read (s) */ 73double sys_fuzz = 0; /* min. time to read the clock (s) */ 74long sys_fuzz_nsec = 0; /* min. time to read the clock (ns) */ 75double measured_tick; /* non-overridable sys_tick (s) */ 76double sys_residual = 0; /* adjustment residue (s) */ 77int trunc_os_clock; /* sys_tick > measured_tick */ 78time_stepped_callback step_callback; 79 80#ifndef SIM 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. 85 */ 86static int lamport_violated; /* clock was stepped back */ 87#endif /* !SIM */ 88 89#ifdef DEBUG 90static int systime_init_done; 91# define DONE_SYSTIME_INIT() systime_init_done = TRUE 92#else 93# define DONE_SYSTIME_INIT() do {} while (FALSE) 94#endif 95 96#ifdef HAVE_SIGNALED_IO 97int using_sigio; 98#endif 99 100#ifdef SYS_WINNT 101CRITICAL_SECTION get_systime_cs; 102#endif 103 104 105void 106set_sys_fuzz( 107 double fuzz_val 108 ) 109{ 110 sys_fuzz = fuzz_val; 111 INSIST(sys_fuzz >= 0); 112 INSIST(sys_fuzz <= 1.0); 113 sys_fuzz_nsec = (long)(sys_fuzz * 1e9 + 0.5); 114} 115 116 117void 118init_systime(void) 119{ 120 INIT_GET_SYSTIME_CRITSEC(); 121 INIT_WIN_PRECISE_TIME(); 122 DONE_SYSTIME_INIT(); 123} 124 125 126#ifndef SIM /* ntpsim.c has get_systime() and friends for sim */ 127 128static inline void 129get_ostime( 130 struct timespec * tsp 131 ) 132{ 133 int rc; 134 long ticks; 135 136#if defined(HAVE_CLOCK_GETTIME) 137 rc = clock_gettime(CLOCK_REALTIME, tsp); 138#elif defined(HAVE_GETCLOCK) 139 rc = getclock(TIMEOFDAY, tsp); 140#else 141 struct timeval tv; 142 143 rc = GETTIMEOFDAY(&tv, NULL); 144 tsp->tv_sec = tv.tv_sec; 145 tsp->tv_nsec = tv.tv_usec * 1000; 146#endif 147 if (rc < 0) { 148 msyslog(LOG_ERR, "read system clock failed: %m (%d)", 149 errno); 150 exit(1); 151 } 152 153 if (trunc_os_clock) { 154 ticks = (long)((tsp->tv_nsec * 1e-9) / sys_tick); 155 tsp->tv_nsec = (long)(ticks * 1e9 * sys_tick); 156 } 157} 158 159 160/* 161 * get_systime - return system time in NTP timestamp format. 162 */ 163void 164get_systime( 165 l_fp *now /* system time */ 166 ) 167{ 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 */ 176 double dfuzz; 177 double ddelta; 178 l_fp result; 179 l_fp lfpfuzz; 180 l_fp lfpdelta; 181 182 get_ostime(&ts); 183 DEBUG_REQUIRE(systime_init_done); 184 ENTER_GET_SYSTIME_CRITSEC(); 185 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.) 193 */ 194 if (cmp_tspec(add_tspec_ns(ts, 50000000), ts_last) < 0) 195 lamport_violated = 1; 196 ts_last = ts; 197 198 /* 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. 204 */ 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) { 210 msyslog(LOG_ERR, 211 "get_systime Lamport advance exceeds one second (%.9f)", 212 ts_lam.tv_sec + 213 1e-9 * ts_lam.tv_nsec); 214 exit(1); 215 } 216 if (!lamport_violated) 217 ts = ts_min; 218 } 219 ts_prev_log = ts_prev; 220 ts_prev = ts; 221 } else { 222 /* 223 * Quiet "ts_prev_log.tv_sec may be used uninitialized" 224 * warning from x86 gcc 4.5.2. 225 */ 226 ZERO(ts_prev_log); 227 } 228 229 /* convert from timespec to l_fp fixed-point */ 230 result = tspec_stamp_to_lfp(ts); 231 232 /* 233 * Add in the fuzz. 234 */ 235 dfuzz = ntp_random() * 2. / FRAC * sys_fuzz; 236 DTOLFP(dfuzz, &lfpfuzz); 237 L_ADD(&result, &lfpfuzz); 238 239 /* 240 * Ensure result is strictly greater than prior result (ignoring 241 * sys_residual's effect for now) once sys_fuzz has been 242 * determined. 243 */ 244 if (!USING_SIGIO()) { 245 if (!L_ISZERO(&lfp_prev) && !lamport_violated) { 246 if (!L_ISGTU(&result, &lfp_prev) && 247 sys_fuzz > 0.) { 248 msyslog(LOG_ERR, "ts_prev %s ts_min %s", 249 tspectoa(ts_prev_log), 250 tspectoa(ts_min)); 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", 255 dfuzz); 256 lfpdelta = lfp_prev; 257 L_SUB(&lfpdelta, &result); 258 LFPTOD(&lfpdelta, ddelta); 259 msyslog(LOG_ERR, 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); 263 } 264 } 265 lfp_prev = result; 266 dfuzz_prev = dfuzz; 267 if (lamport_violated) 268 lamport_violated = FALSE; 269 } 270 LEAVE_GET_SYSTIME_CRITSEC(); 271 *now = result; 272} 273 274 275/* 276 * adj_systime - adjust system time by the argument. 277 */ 278#if !defined SYS_WINNT 279int /* 0 okay, 1 error */ 280adj_systime( 281 double now /* adjustment (s) */ 282 ) 283{ 284 struct timeval adjtv; /* new adjustment */ 285 struct timeval oadjtv; /* residual adjustment */ 286 double quant; /* quantize to multiples of */ 287 double dtemp; 288 long ticks; 289 int isneg = 0; 290 291 /* 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. 299 */ 300 if (0. == now) { 301 if (enable_panic_check && allow_panic) { 302 msyslog(LOG_ERR, "adj_systime: allow_panic is TRUE!"); 303 INSIST(!allow_panic); 304 } 305 return TRUE; 306 } 307 308 /* 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. 313 */ 314 dtemp = now + sys_residual; 315 if (dtemp < 0) { 316 isneg = 1; 317 dtemp = -dtemp; 318 } 319 adjtv.tv_sec = (long)dtemp; 320 dtemp -= adjtv.tv_sec; 321 if (sys_tick > sys_fuzz) 322 quant = sys_tick; 323 else 324 quant = 1e-6; 325 ticks = (long)(dtemp / quant + .5); 326 adjtv.tv_usec = (long)(ticks * quant * 1e6); 327 dtemp -= adjtv.tv_usec / 1e6; 328 sys_residual = dtemp; 329 330 /* 331 * Convert to signed seconds and microseconds for the Unix 332 * adjtime() system call. Note we purposely lose the adjtime() 333 * leftover. 334 */ 335 if (isneg) { 336 adjtv.tv_sec = -adjtv.tv_sec; 337 adjtv.tv_usec = -adjtv.tv_usec; 338 sys_residual = -sys_residual; 339 } 340 if (adjtv.tv_sec != 0 || adjtv.tv_usec != 0) { 341 if (adjtime(&adjtv, &oadjtv) < 0) { 342 msyslog(LOG_ERR, "adj_systime: %m"); 343 if (enable_panic_check && allow_panic) { 344 msyslog(LOG_ERR, "adj_systime: allow_panic is TRUE!"); 345 } 346 return FALSE; 347 } 348 } 349 if (enable_panic_check && allow_panic) { 350 msyslog(LOG_ERR, "adj_systime: allow_panic is TRUE!"); 351 } 352 return TRUE; 353} 354#endif 355 356 357/* 358 * step_systime - step the system clock. 359 */ 360 361int 362step_systime( 363 double step 364 ) 365{ 366 time_t pivot; /* for ntp era unfolding */ 367 struct timeval timetv, tvlast, tvdiff; 368 struct timespec timets; 369 struct calendar jd; 370 l_fp fp_ofs, fp_sys; /* offset and target system time in FP */ 371 372 /* 373 * Get pivot time for NTP era unfolding. Since we don't step 374 * very often, we can afford to do the whole calculation from 375 * scratch. And we're not in the time-critical path yet. 376 */ 377#if SIZEOF_TIME_T > 4 378 /* 379 * This code makes sure the resulting time stamp for the new 380 * system time is in the 2^32 seconds starting at 1970-01-01, 381 * 00:00:00 UTC. 382 */ 383 pivot = 0x80000000; 384#if USE_COMPILETIME_PIVOT 385 /* 386 * Add the compile time minus 10 years to get a possible target 387 * area of (compile time - 10 years) to (compile time + 126 388 * years). This should be sufficient for a given binary of 389 * NTPD. 390 */ 391 if (ntpcal_get_build_date(&jd)) { 392 jd.year -= 10; 393 pivot += ntpcal_date_to_time(&jd); 394 } else { 395 msyslog(LOG_ERR, 396 "step-systime: assume 1970-01-01 as build date"); 397 } 398#else 399 UNUSED_LOCAL(jd); 400#endif /* USE_COMPILETIME_PIVOT */ 401#else 402 UNUSED_LOCAL(jd); 403 /* This makes sure the resulting time stamp is on or after 404 * 1969-12-31/23:59:59 UTC and gives us additional two years, 405 * from the change of NTP era in 2036 to the UNIX rollover in 406 * 2038. (Minus one second, but that won't hurt.) We *really* 407 * need a longer 'time_t' after that! Or a different baseline, 408 * but that would cause other serious trouble, too. 409 */ 410 pivot = 0x7FFFFFFF; 411#endif 412 413 /* get the complete jump distance as l_fp */ 414 DTOLFP(sys_residual, &fp_sys); 415 DTOLFP(step, &fp_ofs); 416 L_ADD(&fp_ofs, &fp_sys); 417 418 /* ---> time-critical path starts ---> */ 419 420 /* get the current time as l_fp (without fuzz) and as struct timeval */ 421 get_ostime(&timets); 422 fp_sys = tspec_stamp_to_lfp(timets); 423 tvlast.tv_sec = timets.tv_sec; 424 tvlast.tv_usec = (timets.tv_nsec + 500) / 1000; 425 426 /* get the target time as l_fp */ 427 L_ADD(&fp_sys, &fp_ofs); 428 429 /* unfold the new system time */ 430 timetv = lfp_stamp_to_tval(fp_sys, &pivot); 431 432 /* now set new system time */ 433 if (ntp_set_tod(&timetv, NULL) != 0) { 434 msyslog(LOG_ERR, "step-systime: %m"); 435 if (enable_panic_check && allow_panic) { 436 msyslog(LOG_ERR, "step_systime: allow_panic is TRUE!"); 437 } 438 return FALSE; 439 } 440 441 /* <--- time-critical path ended with 'ntp_set_tod()' <--- */ 442 443 sys_residual = 0; 444 lamport_violated = (step < 0); 445 if (step_callback) 446 (*step_callback)(); 447 448#ifdef NEED_HPUX_ADJTIME 449 /* 450 * CHECKME: is this correct when called by ntpdate????? 451 */ 452 _clear_adjtime(); 453#endif 454 455 /* 456 * FreeBSD, for example, has: 457 * struct utmp { 458 * char ut_line[UT_LINESIZE]; 459 * char ut_name[UT_NAMESIZE]; 460 * char ut_host[UT_HOSTSIZE]; 461 * long ut_time; 462 * }; 463 * and appends line="|", name="date", host="", time for the OLD 464 * and appends line="{", name="date", host="", time for the NEW // } 465 * to _PATH_WTMP . 466 * 467 * Some OSes have utmp, some have utmpx. 468 */ 469 470 /* 471 * Write old and new time entries in utmp and wtmp if step 472 * adjustment is greater than one second. 473 * 474 * This might become even Uglier... 475 */ 476 tvdiff = abs_tval(sub_tval(timetv, tvlast)); 477 if (tvdiff.tv_sec > 0) { 478#ifdef HAVE_UTMP_H 479 struct utmp ut; 480#endif 481#ifdef HAVE_UTMPX_H 482 struct utmpx utx; 483#endif 484 485#ifdef HAVE_UTMP_H 486 ZERO(ut); 487#endif 488#ifdef HAVE_UTMPX_H 489 ZERO(utx); 490#endif 491 492 /* UTMP */ 493 494#ifdef UPDATE_UTMP 495# ifdef HAVE_PUTUTLINE 496# ifndef _PATH_UTMP 497# define _PATH_UTMP UTMP_FILE 498# endif 499 utmpname(_PATH_UTMP); 500 ut.ut_type = OLD_TIME; 501 strlcpy(ut.ut_line, OTIME_MSG, sizeof(ut.ut_line)); 502 ut.ut_time = tvlast.tv_sec; 503 setutent(); 504 pututline(&ut); 505 ut.ut_type = NEW_TIME; 506 strlcpy(ut.ut_line, NTIME_MSG, sizeof(ut.ut_line)); 507 ut.ut_time = timetv.tv_sec; 508 setutent(); 509 pututline(&ut); 510 endutent(); 511# else /* not HAVE_PUTUTLINE */ 512# endif /* not HAVE_PUTUTLINE */ 513#endif /* UPDATE_UTMP */ 514 515 /* UTMPX */ 516 517#ifdef UPDATE_UTMPX 518# ifdef HAVE_PUTUTXLINE 519 utx.ut_type = OLD_TIME; 520 strlcpy(utx.ut_line, OTIME_MSG, sizeof(utx.ut_line)); 521 utx.ut_tv = tvlast; 522 setutxent(); 523 pututxline(&utx); 524 utx.ut_type = NEW_TIME; 525 strlcpy(utx.ut_line, NTIME_MSG, sizeof(utx.ut_line)); 526 utx.ut_tv = timetv; 527 setutxent(); 528 pututxline(&utx); 529 endutxent(); 530# else /* not HAVE_PUTUTXLINE */ 531# endif /* not HAVE_PUTUTXLINE */ 532#endif /* UPDATE_UTMPX */ 533 534 /* WTMP */ 535 536#ifdef UPDATE_WTMP 537# ifdef HAVE_PUTUTLINE 538# ifndef _PATH_WTMP 539# define _PATH_WTMP WTMP_FILE 540# endif 541 utmpname(_PATH_WTMP); 542 ut.ut_type = OLD_TIME; 543 strlcpy(ut.ut_line, OTIME_MSG, sizeof(ut.ut_line)); 544 ut.ut_time = tvlast.tv_sec; 545 setutent(); 546 pututline(&ut); 547 ut.ut_type = NEW_TIME; 548 strlcpy(ut.ut_line, NTIME_MSG, sizeof(ut.ut_line)); 549 ut.ut_time = timetv.tv_sec; 550 setutent(); 551 pututline(&ut); 552 endutent(); 553# else /* not HAVE_PUTUTLINE */ 554# endif /* not HAVE_PUTUTLINE */ 555#endif /* UPDATE_WTMP */ 556 557 /* WTMPX */ 558 559#ifdef UPDATE_WTMPX 560# ifdef HAVE_PUTUTXLINE 561 utx.ut_type = OLD_TIME; 562 utx.ut_tv = tvlast; 563 strlcpy(utx.ut_line, OTIME_MSG, sizeof(utx.ut_line)); 564# ifdef HAVE_UPDWTMPX 565 updwtmpx(WTMPX_FILE, &utx); 566# else /* not HAVE_UPDWTMPX */ 567# endif /* not HAVE_UPDWTMPX */ 568# else /* not HAVE_PUTUTXLINE */ 569# endif /* not HAVE_PUTUTXLINE */ 570# ifdef HAVE_PUTUTXLINE 571 utx.ut_type = NEW_TIME; 572 utx.ut_tv = timetv; 573 strlcpy(utx.ut_line, NTIME_MSG, sizeof(utx.ut_line)); 574# ifdef HAVE_UPDWTMPX 575 updwtmpx(WTMPX_FILE, &utx); 576# else /* not HAVE_UPDWTMPX */ 577# endif /* not HAVE_UPDWTMPX */ 578# else /* not HAVE_PUTUTXLINE */ 579# endif /* not HAVE_PUTUTXLINE */ 580#endif /* UPDATE_WTMPX */ 581 582 } 583 if (enable_panic_check && allow_panic) { 584 msyslog(LOG_ERR, "step_systime: allow_panic is TRUE!"); 585 INSIST(!allow_panic); 586 } 587 return TRUE; 588} 589 590#endif /* !SIM */ 591