#include <config.h>
#include "ntp.h"
#include "ntp_syslog.h"
#include "ntp_stdlib.h"
#include "ntp_random.h"
#include "iosignal.h"
#include "timevalops.h"
#include "timespecops.h"
#include "ntp_calendar.h"
#include <os/trace.h>
#ifdef HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
#ifdef HAVE_UTMP_H
# include <utmp.h>
#endif
#ifdef HAVE_UTMPX_H
# include <utmpx.h>
#endif
int allow_panic = FALSE;
int enable_panic_check = TRUE;
#ifndef USE_COMPILETIME_PIVOT
# define USE_COMPILETIME_PIVOT 1
#endif
double sys_tick = 0;
double sys_fuzz = 0;
long sys_fuzz_nsec = 0;
double measured_tick;
double sys_residual = 0;
int trunc_os_clock;
time_stepped_callback step_callback;
#ifndef SIM
static int lamport_violated;
#endif
#ifdef DEBUG
static int systime_init_done;
# define DONE_SYSTIME_INIT() systime_init_done = TRUE
#else
# define DONE_SYSTIME_INIT() do {} while (FALSE)
#endif
#ifdef HAVE_SIGNALED_IO
int using_sigio;
#endif
#ifdef SYS_WINNT
CRITICAL_SECTION get_systime_cs;
#endif
void
set_sys_fuzz(
double fuzz_val
)
{
sys_fuzz = fuzz_val;
INSIST(sys_fuzz >= 0);
INSIST(sys_fuzz <= 1.0);
sys_fuzz_nsec = (long)(sys_fuzz * 1e9 + 0.5);
}
void
init_systime(void)
{
INIT_GET_SYSTIME_CRITSEC();
INIT_WIN_PRECISE_TIME();
DONE_SYSTIME_INIT();
}
#ifndef SIM
static inline void
get_ostime(
struct timespec * tsp
)
{
int rc;
long ticks;
#if defined(HAVE_CLOCK_GETTIME)
rc = clock_gettime(CLOCK_REALTIME, tsp);
#elif defined(HAVE_GETCLOCK)
rc = getclock(TIMEOFDAY, tsp);
#else
struct timeval tv;
rc = GETTIMEOFDAY(&tv, NULL);
tsp->tv_sec = tv.tv_sec;
tsp->tv_nsec = tv.tv_usec * 1000;
#endif
if (rc < 0) {
msyslog(LOG_ERR, "read system clock failed: %m (%d)",
errno);
exit(1);
}
if (trunc_os_clock) {
ticks = (long)((tsp->tv_nsec * 1e-9) / sys_tick);
tsp->tv_nsec = (long)(ticks * 1e9 * sys_tick);
}
}
void
get_systime(
l_fp *now
)
{
static struct timespec ts_last;
static struct timespec ts_prev;
static l_fp lfp_prev;
static double dfuzz_prev;
struct timespec ts;
struct timespec ts_min = {0};
struct timespec ts_lam;
struct timespec ts_prev_log;
double dfuzz;
double ddelta;
l_fp result;
l_fp lfpfuzz;
l_fp lfpdelta;
get_ostime(&ts);
DEBUG_REQUIRE(systime_init_done);
ENTER_GET_SYSTIME_CRITSEC();
if (cmp_tspec(add_tspec_ns(ts, 50000000), ts_last) < 0)
lamport_violated = 1;
ts_last = ts;
if (!USING_SIGIO()) {
ts_min = add_tspec_ns(ts_prev, sys_fuzz_nsec);
if (cmp_tspec(ts, ts_min) < 0) {
ts_lam = sub_tspec(ts_min, ts);
if (ts_lam.tv_sec > 0 && !lamport_violated) {
msyslog(LOG_ERR,
"get_systime Lamport advance exceeds one second (%.9f)",
ts_lam.tv_sec +
1e-9 * ts_lam.tv_nsec);
exit(1);
}
if (!lamport_violated)
ts = ts_min;
}
ts_prev_log = ts_prev;
ts_prev = ts;
} else {
ZERO(ts_prev_log);
}
result = tspec_stamp_to_lfp(ts);
dfuzz = ntp_random() * 2. / FRAC * sys_fuzz;
DTOLFP(dfuzz, &lfpfuzz);
L_ADD(&result, &lfpfuzz);
if (!USING_SIGIO()) {
if (!L_ISZERO(&lfp_prev) && !lamport_violated) {
if (!L_ISGTU(&result, &lfp_prev) &&
sys_fuzz > 0.) {
msyslog(LOG_ERR, "ts_prev %s ts_min %s",
tspectoa(ts_prev_log),
tspectoa(ts_min));
msyslog(LOG_ERR, "ts %s", tspectoa(ts));
msyslog(LOG_ERR, "sys_fuzz %ld nsec, prior fuzz %.9f",
sys_fuzz_nsec, dfuzz_prev);
msyslog(LOG_ERR, "this fuzz %.9f",
dfuzz);
lfpdelta = lfp_prev;
L_SUB(&lfpdelta, &result);
LFPTOD(&lfpdelta, ddelta);
msyslog(LOG_ERR,
"prev get_systime 0x%x.%08x is %.9f later than 0x%x.%08x",
lfp_prev.l_ui, lfp_prev.l_uf,
ddelta, result.l_ui, result.l_uf);
}
}
lfp_prev = result;
dfuzz_prev = dfuzz;
if (lamport_violated)
lamport_violated = FALSE;
}
LEAVE_GET_SYSTIME_CRITSEC();
*now = result;
}
#if !defined SYS_WINNT
int
adj_systime(
double now
)
{
struct timeval adjtv;
struct timeval oadjtv;
double quant;
double dtemp;
long ticks;
int isneg = 0;
static double last_now;
if (0. == now) {
if (enable_panic_check && allow_panic) {
msyslog(LOG_ERR, "adj_systime: allow_panic is TRUE!");
}
return TRUE;
}
os_trace_debug("step: %f", now);
dtemp = now + sys_residual;
if (dtemp < 0) {
isneg = 1;
dtemp = -dtemp;
}
adjtv.tv_sec = (long)dtemp;
dtemp -= adjtv.tv_sec;
if (sys_tick > sys_fuzz)
quant = sys_tick;
else
quant = 1e-6;
ticks = (long)(dtemp / quant + .5);
adjtv.tv_usec = (long)(ticks * quant * 1.e6 + .5);
if (adjtv.tv_usec >= 1000000) {
adjtv.tv_sec += 1;
adjtv.tv_usec -= 1000000;
dtemp -= 1.;
}
sys_residual = dtemp - adjtv.tv_usec * 1.e-6;
if (isneg) {
adjtv.tv_sec = -adjtv.tv_sec;
adjtv.tv_usec = -adjtv.tv_usec;
sys_residual = -sys_residual;
}
if (adjtv.tv_sec != 0 || adjtv.tv_usec != 0) {
if (adjtime(&adjtv, &oadjtv) < 0) {
msyslog(LOG_ERR, "adj_systime: %m");
if (enable_panic_check && allow_panic) {
msyslog(LOG_ERR, "adj_systime: allow_panic is TRUE!");
}
return FALSE;
}
if (now != last_now) {
os_trace_debug("adjust: %f", now);
}
last_now = now;
}
if (enable_panic_check && allow_panic) {
msyslog(LOG_ERR, "adj_systime: allow_panic is TRUE!");
}
return TRUE;
}
#endif
int
step_systime(
double step
)
{
time_t pivot;
struct timeval timetv, tvlast, tvdiff;
struct timespec timets;
struct calendar jd;
l_fp fp_ofs, fp_sys;
#if SIZEOF_TIME_T > 4
pivot = 0x80000000;
#if USE_COMPILETIME_PIVOT
if (ntpcal_get_build_date(&jd)) {
jd.year -= 10;
pivot += ntpcal_date_to_time(&jd);
} else {
msyslog(LOG_ERR,
"step-systime: assume 1970-01-01 as build date");
}
#else
UNUSED_LOCAL(jd);
#endif
#else
UNUSED_LOCAL(jd);
pivot = 0x7FFFFFFF;
#endif
DTOLFP(sys_residual, &fp_sys);
DTOLFP(step, &fp_ofs);
L_ADD(&fp_ofs, &fp_sys);
get_ostime(&timets);
fp_sys = tspec_stamp_to_lfp(timets);
tvlast.tv_sec = timets.tv_sec;
tvlast.tv_usec = (timets.tv_nsec + 500) / 1000;
L_ADD(&fp_sys, &fp_ofs);
timetv = lfp_stamp_to_tval(fp_sys, &pivot);
if (ntp_set_tod(&timetv, NULL) != 0) {
msyslog(LOG_ERR, "step-systime: %m");
if (enable_panic_check && allow_panic) {
msyslog(LOG_ERR, "step_systime: allow_panic is TRUE!");
}
return FALSE;
}
sys_residual = 0;
lamport_violated = (step < 0);
if (step_callback)
(*step_callback)();
#ifdef NEED_HPUX_ADJTIME
_clear_adjtime();
#endif
tvdiff = abs_tval(sub_tval(timetv, tvlast));
if (tvdiff.tv_sec > 0) {
#ifdef HAVE_UTMP_H
struct utmp ut;
#endif
#ifdef HAVE_UTMPX_H
struct utmpx utx;
#endif
#ifdef HAVE_UTMP_H
ZERO(ut);
#endif
#ifdef HAVE_UTMPX_H
ZERO(utx);
#endif
#ifdef UPDATE_UTMP
# ifdef HAVE_PUTUTLINE
# ifndef _PATH_UTMP
# define _PATH_UTMP UTMP_FILE
# endif
utmpname(_PATH_UTMP);
ut.ut_type = OLD_TIME;
strlcpy(ut.ut_line, OTIME_MSG, sizeof(ut.ut_line));
ut.ut_time = tvlast.tv_sec;
setutent();
pututline(&ut);
ut.ut_type = NEW_TIME;
strlcpy(ut.ut_line, NTIME_MSG, sizeof(ut.ut_line));
ut.ut_time = timetv.tv_sec;
setutent();
pututline(&ut);
endutent();
# else
# endif
#endif
#ifdef UPDATE_UTMPX
# ifdef HAVE_PUTUTXLINE
utx.ut_type = OLD_TIME;
strlcpy(utx.ut_line, OTIME_MSG, sizeof(utx.ut_line));
utx.ut_tv = tvlast;
setutxent();
pututxline(&utx);
utx.ut_type = NEW_TIME;
strlcpy(utx.ut_line, NTIME_MSG, sizeof(utx.ut_line));
utx.ut_tv = timetv;
setutxent();
pututxline(&utx);
endutxent();
# else
# endif
#endif
#ifdef UPDATE_WTMP
# ifdef HAVE_PUTUTLINE
# ifndef _PATH_WTMP
# define _PATH_WTMP WTMP_FILE
# endif
utmpname(_PATH_WTMP);
ut.ut_type = OLD_TIME;
strlcpy(ut.ut_line, OTIME_MSG, sizeof(ut.ut_line));
ut.ut_time = tvlast.tv_sec;
setutent();
pututline(&ut);
ut.ut_type = NEW_TIME;
strlcpy(ut.ut_line, NTIME_MSG, sizeof(ut.ut_line));
ut.ut_time = timetv.tv_sec;
setutent();
pututline(&ut);
endutent();
# else
# endif
#endif
#ifdef UPDATE_WTMPX
# ifdef HAVE_PUTUTXLINE
utx.ut_type = OLD_TIME;
utx.ut_tv = tvlast;
strlcpy(utx.ut_line, OTIME_MSG, sizeof(utx.ut_line));
# ifdef HAVE_UPDWTMPX
updwtmpx(WTMPX_FILE, &utx);
# else
# endif
# else
# endif
# ifdef HAVE_PUTUTXLINE
utx.ut_type = NEW_TIME;
utx.ut_tv = timetv;
strlcpy(utx.ut_line, NTIME_MSG, sizeof(utx.ut_line));
# ifdef HAVE_UPDWTMPX
updwtmpx(WTMPX_FILE, &utx);
# else
# endif
# else
# endif
#endif
}
if (enable_panic_check && allow_panic) {
msyslog(LOG_ERR, "step_systime: allow_panic is TRUE!");
}
return TRUE;
}
#endif