1 /* $NetBSD: systime.c,v 1.1.1.3 2013/12/27 23:30:48 christos Exp $ */ 2 3 /* 4 * systime -- routines to fiddle a UNIX clock. 5 * 6 * ATTENTION: Get approval from Dave Mills on all changes to this file! 7 * 8 */ 9 #include <config.h> 10 11 #include "ntp.h" 12 #include "ntp_syslog.h" 13 #include "ntp_stdlib.h" 14 #include "ntp_random.h" 15 #include "iosignal.h" 16 #include "timevalops.h" 17 #include "timespecops.h" 18 #include "ntp_calendar.h" 19 20 #ifdef HAVE_SYS_PARAM_H 21 # include <sys/param.h> 22 #endif 23 #ifdef HAVE_UTMP_H 24 # include <utmp.h> 25 #endif /* HAVE_UTMP_H */ 26 #ifdef HAVE_UTMPX_H 27 # include <utmpx.h> 28 #endif /* HAVE_UTMPX_H */ 29 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 */ 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; 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 */ 86 static int lamport_violated; /* clock was stepped back */ 87 #endif /* !SIM */ 88 89 #ifdef DEBUG 90 static 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 97 int using_sigio; 98 #endif 99 100 #ifdef SYS_WINNT 101 CRITICAL_SECTION get_systime_cs; 102 #endif 103 104 105 void 106 set_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 117 void 118 init_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 128 static inline void 129 get_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 */ 163 void 164 get_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 279 int /* 0 okay, 1 error */ 280 adj_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 return TRUE; 302 303 /* 304 * Most Unix adjtime() implementations adjust the system clock 305 * in microsecond quanta, but some adjust in 10-ms quanta. We 306 * carefully round the adjustment to the nearest quantum, then 307 * adjust in quanta and keep the residue for later. 308 */ 309 dtemp = now + sys_residual; 310 if (dtemp < 0) { 311 isneg = 1; 312 dtemp = -dtemp; 313 } 314 adjtv.tv_sec = (long)dtemp; 315 dtemp -= adjtv.tv_sec; 316 if (sys_tick > sys_fuzz) 317 quant = sys_tick; 318 else 319 quant = 1e-6; 320 ticks = (long)(dtemp / quant + .5); 321 adjtv.tv_usec = (long)(ticks * quant * 1e6); 322 dtemp -= adjtv.tv_usec / 1e6; 323 sys_residual = dtemp; 324 325 /* 326 * Convert to signed seconds and microseconds for the Unix 327 * adjtime() system call. Note we purposely lose the adjtime() 328 * leftover. 329 */ 330 if (isneg) { 331 adjtv.tv_sec = -adjtv.tv_sec; 332 adjtv.tv_usec = -adjtv.tv_usec; 333 sys_residual = -sys_residual; 334 } 335 if (adjtv.tv_sec != 0 || adjtv.tv_usec != 0) { 336 if (adjtime(&adjtv, &oadjtv) < 0) { 337 msyslog(LOG_ERR, "adj_systime: %m"); 338 return FALSE; 339 } 340 } 341 return TRUE; 342 } 343 #endif 344 345 346 /* 347 * step_systime - step the system clock. 348 */ 349 350 int 351 step_systime( 352 double step 353 ) 354 { 355 time_t pivot; /* for ntp era unfolding */ 356 struct timeval timetv, tvlast, tvdiff; 357 struct timespec timets; 358 struct calendar jd; 359 l_fp fp_ofs, fp_sys; /* offset and target system time in FP */ 360 361 /* 362 * Get pivot time for NTP era unfolding. Since we don't step 363 * very often, we can afford to do the whole calculation from 364 * scratch. And we're not in the time-critical path yet. 365 */ 366 #if SIZEOF_TIME_T > 4 367 /* 368 * This code makes sure the resulting time stamp for the new 369 * system time is in the 2^32 seconds starting at 1970-01-01, 370 * 00:00:00 UTC. 371 */ 372 pivot = 0x80000000; 373 #if USE_COMPILETIME_PIVOT 374 /* 375 * Add the compile time minus 10 years to get a possible target 376 * area of (compile time - 10 years) to (compile time + 126 377 * years). This should be sufficient for a given binary of 378 * NTPD. 379 */ 380 if (ntpcal_get_build_date(&jd)) { 381 jd.year -= 10; 382 pivot += ntpcal_date_to_time(&jd); 383 } else { 384 msyslog(LOG_ERR, 385 "step-systime: assume 1970-01-01 as build date"); 386 } 387 #else 388 UNUSED_LOCAL(jd); 389 #endif /* USE_COMPILETIME_PIVOT */ 390 #else 391 UNUSED_LOCAL(jd); 392 /* This makes sure the resulting time stamp is on or after 393 * 1969-12-31/23:59:59 UTC and gives us additional two years, 394 * from the change of NTP era in 2036 to the UNIX rollover in 395 * 2038. (Minus one second, but that won't hurt.) We *really* 396 * need a longer 'time_t' after that! Or a different baseline, 397 * but that would cause other serious trouble, too. 398 */ 399 pivot = 0x7FFFFFFF; 400 #endif 401 402 /* get the complete jump distance as l_fp */ 403 DTOLFP(sys_residual, &fp_sys); 404 DTOLFP(step, &fp_ofs); 405 L_ADD(&fp_ofs, &fp_sys); 406 407 /* ---> time-critical path starts ---> */ 408 409 /* get the current time as l_fp (without fuzz) and as struct timeval */ 410 get_ostime(&timets); 411 fp_sys = tspec_stamp_to_lfp(timets); 412 tvlast.tv_sec = timets.tv_sec; 413 tvlast.tv_usec = (timets.tv_nsec + 500) / 1000; 414 415 /* get the target time as l_fp */ 416 L_ADD(&fp_sys, &fp_ofs); 417 418 /* unfold the new system time */ 419 timetv = lfp_stamp_to_tval(fp_sys, &pivot); 420 421 /* now set new system time */ 422 if (ntp_set_tod(&timetv, NULL) != 0) { 423 msyslog(LOG_ERR, "step-systime: %m"); 424 return FALSE; 425 } 426 427 /* <--- time-critical path ended with 'ntp_set_tod()' <--- */ 428 429 sys_residual = 0; 430 lamport_violated = (step < 0); 431 if (step_callback) 432 (*step_callback)(); 433 434 #ifdef NEED_HPUX_ADJTIME 435 /* 436 * CHECKME: is this correct when called by ntpdate????? 437 */ 438 _clear_adjtime(); 439 #endif 440 441 /* 442 * FreeBSD, for example, has: 443 * struct utmp { 444 * char ut_line[UT_LINESIZE]; 445 * char ut_name[UT_NAMESIZE]; 446 * char ut_host[UT_HOSTSIZE]; 447 * long ut_time; 448 * }; 449 * and appends line="|", name="date", host="", time for the OLD 450 * and appends line="{", name="date", host="", time for the NEW 451 * to _PATH_WTMP . 452 * 453 * Some OSes have utmp, some have utmpx. 454 */ 455 456 /* 457 * Write old and new time entries in utmp and wtmp if step 458 * adjustment is greater than one second. 459 * 460 * This might become even Uglier... 461 */ 462 tvdiff = abs_tval(sub_tval(timetv, tvlast)); 463 if (tvdiff.tv_sec > 0) { 464 #ifdef HAVE_UTMP_H 465 struct utmp ut; 466 #endif 467 #ifdef HAVE_UTMPX_H 468 struct utmpx utx; 469 #endif 470 471 #ifdef HAVE_UTMP_H 472 ZERO(ut); 473 #endif 474 #ifdef HAVE_UTMPX_H 475 ZERO(utx); 476 #endif 477 478 /* UTMP */ 479 480 #ifdef UPDATE_UTMP 481 # ifdef HAVE_PUTUTLINE 482 # ifndef _PATH_UTMP 483 # define _PATH_UTMP UTMP_FILE 484 # endif 485 utmpname(_PATH_UTMP); 486 ut.ut_type = OLD_TIME; 487 strlcpy(ut.ut_line, OTIME_MSG, sizeof(ut.ut_line)); 488 ut.ut_time = tvlast.tv_sec; 489 setutent(); 490 pututline(&ut); 491 ut.ut_type = NEW_TIME; 492 strlcpy(ut.ut_line, NTIME_MSG, sizeof(ut.ut_line)); 493 ut.ut_time = timetv.tv_sec; 494 setutent(); 495 pututline(&ut); 496 endutent(); 497 # else /* not HAVE_PUTUTLINE */ 498 # endif /* not HAVE_PUTUTLINE */ 499 #endif /* UPDATE_UTMP */ 500 501 /* UTMPX */ 502 503 #ifdef UPDATE_UTMPX 504 # ifdef HAVE_PUTUTXLINE 505 utx.ut_type = OLD_TIME; 506 strlcpy(utx.ut_line, OTIME_MSG, sizeof(utx.ut_line)); 507 utx.ut_tv = tvlast; 508 setutxent(); 509 pututxline(&utx); 510 utx.ut_type = NEW_TIME; 511 strlcpy(utx.ut_line, NTIME_MSG, sizeof(utx.ut_line)); 512 utx.ut_tv = timetv; 513 setutxent(); 514 pututxline(&utx); 515 endutxent(); 516 # else /* not HAVE_PUTUTXLINE */ 517 # endif /* not HAVE_PUTUTXLINE */ 518 #endif /* UPDATE_UTMPX */ 519 520 /* WTMP */ 521 522 #ifdef UPDATE_WTMP 523 # ifdef HAVE_PUTUTLINE 524 # ifndef _PATH_WTMP 525 # define _PATH_WTMP WTMP_FILE 526 # endif 527 utmpname(_PATH_WTMP); 528 ut.ut_type = OLD_TIME; 529 strlcpy(ut.ut_line, OTIME_MSG, sizeof(ut.ut_line)); 530 ut.ut_time = tvlast.tv_sec; 531 setutent(); 532 pututline(&ut); 533 ut.ut_type = NEW_TIME; 534 strlcpy(ut.ut_line, NTIME_MSG, sizeof(ut.ut_line)); 535 ut.ut_time = timetv.tv_sec; 536 setutent(); 537 pututline(&ut); 538 endutent(); 539 # else /* not HAVE_PUTUTLINE */ 540 # endif /* not HAVE_PUTUTLINE */ 541 #endif /* UPDATE_WTMP */ 542 543 /* WTMPX */ 544 545 #ifdef UPDATE_WTMPX 546 # ifdef HAVE_PUTUTXLINE 547 utx.ut_type = OLD_TIME; 548 utx.ut_tv = tvlast; 549 strlcpy(utx.ut_line, OTIME_MSG, sizeof(utx.ut_line)); 550 # ifdef HAVE_UPDWTMPX 551 updwtmpx(WTMPX_FILE, &utx); 552 # else /* not HAVE_UPDWTMPX */ 553 # endif /* not HAVE_UPDWTMPX */ 554 # else /* not HAVE_PUTUTXLINE */ 555 # endif /* not HAVE_PUTUTXLINE */ 556 # ifdef HAVE_PUTUTXLINE 557 utx.ut_type = NEW_TIME; 558 utx.ut_tv = timetv; 559 strlcpy(utx.ut_line, NTIME_MSG, sizeof(utx.ut_line)); 560 # ifdef HAVE_UPDWTMPX 561 updwtmpx(WTMPX_FILE, &utx); 562 # else /* not HAVE_UPDWTMPX */ 563 # endif /* not HAVE_UPDWTMPX */ 564 # else /* not HAVE_PUTUTXLINE */ 565 # endif /* not HAVE_PUTUTXLINE */ 566 #endif /* UPDATE_WTMPX */ 567 568 } 569 return TRUE; 570 } 571 572 #endif /* !SIM */ 573