#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#ifdef HAVE_SYS_IOCTL_H
# include <sys/ioctl.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <sys/stat.h>
#ifdef HAVE_IEEEFP_H
# include <ieeefp.h>
#endif
#ifdef HAVE_MATH_H
# include <math.h>
#endif
#if defined(VMS)
# include <descrip.h>
#endif /* VMS */
/*
* Defines used by the leapseconds stuff
*/
#define MAX_TAI 100 /* max TAI offset (s) */
#define L_DAY 86400UL /* seconds per day */
#define L_YEAR (L_DAY * 365) /* days per year */
#define L_LYEAR (L_YEAR + L_DAY) /* days per leap year */
#define L_4YEAR (L_LYEAR + 3 * L_YEAR) /* days per leap cycle */
#define L_CENT (L_4YEAR * 25) /* days per century */
/*
* This contains odds and ends, including the hourly stats, various
* configuration items, leapseconds stuff, etc.
*/
/*
* File names
*/
static char *key_file_name; /* keys file name */
static char *leapfile_name; /* leapseconds file name */
static struct stat leapfile_stat; /* leapseconds file stat() buffer */
static int /*BOOL*/have_leapfile = FALSE;
static int /*BOOL*/chck_leaphash = TRUE;
char *stats_drift_file; /* frequency file name */
static char *stats_temp_file; /* temp frequency file name */
static double wander_resid; /* last frequency update */
double wander_threshold = 1e-7; /* initial frequency threshold */
/*
* This controls whether stats are written to the fileset. Provided
* so that ntpdc can turn off stats when the file system fills up.
*/
int stats_control;
/*
* Last frequency written to file.
*/
static double prev_drift_comp; /* last frequency update */
/*
* Sometimes having a Sun can be a drag.
*
* The kernel variable dosynctodr controls whether the system's
* soft clock is kept in sync with the battery clock. If it
* is zero, then the soft clock is not synced, and the battery
* clock is simply left to rot. That means that when the system
* reboots, the battery clock (which has probably gone wacky)
* sets the soft clock. That means ntpd starts off with a very
* confused idea of what time it is. It then takes a large
* amount of time to figure out just how wacky the battery clock
* has made things drift, etc, etc. The solution is to make the
* battery clock sync up to system time. The way to do THAT is
* to simply set the time of day to the current time of day, but
* as quickly as possible. This may, or may not be a sensible
* thing to do.
*
* CAVEAT: settimeofday() steps the sun clock by about 800 us,
* so setting DOSYNCTODR seems a bad idea in the
* case of us resolution
*/
#if !defined(VMS)
/*
* (prr) getpriority returns -1 on error, but -1 is also a valid
* return value (!), so instead we have to zero errno before the
* call and check it for non-zero afterwards.
*/
errno = 0;
prio_set = 0;
o_prio = getpriority(PRIO_PROCESS,0); /* Save setting */
/*
* (prr) if getpriority succeeded, call setpriority to raise
* scheduling priority as high as possible. If that succeeds
* as well, set the prio_set flag so we remember to reset
* priority to its previous value below. Note that on Solaris
* 2.6 (and beyond?), both getpriority and setpriority will fail
* with ESRCH, because sched_setscheduler (called from main) put
* us in the real-time scheduling class which setpriority
* doesn't know about. Being in the real-time class is better
* than anything setpriority can do, anyhow, so this error is
* silently ignored.
*/
if ((errno == 0) && (setpriority(PRIO_PROCESS,0,-20) == 0))
prio_set = 1; /* overdrive */
#endif /* VMS */
#ifdef HAVE_GETCLOCK
(void) getclock(TIMEOFDAY, &ts);
tv.tv_sec = ts.tv_sec;
tv.tv_usec = ts.tv_nsec / 1000;
#else /* not HAVE_GETCLOCK */
GETTIMEOFDAY(&tv,(struct timezone *)NULL);
#endif /* not HAVE_GETCLOCK */
if (ntp_set_tod(&tv,(struct timezone *)NULL) != 0)
msyslog(LOG_ERR, "can't sync battery time: %m");
#if !defined(VMS)
if (prio_set)
setpriority(PRIO_PROCESS, 0, o_prio); /* downshift */
#endif /* VMS */
#endif /* DOSYNCTODR */
record_sys_stats();
if (stats_drift_file != 0) {
/*
* When the frequency file is written, initialize the
* prev_drift_comp and wander_resid. Thereafter,
* reduce the wander_resid by half each hour. When
* the difference between the prev_drift_comp and
* drift_comp is less than the wander_resid, update
* the frequncy file. This minimizes the file writes to
* nonvolaile storage.
*/
DPRINTF(1, ("write_stats: frequency %.6f thresh %.6f, freq %.6f\n",
(prev_drift_comp - drift_comp) * 1e6, wander_resid *
1e6, drift_comp * 1e6));
#ifndef NO_RENAME
if (rename(stats_temp_file, stats_drift_file))
msyslog(LOG_WARNING,
"Unable to rename temp drift file %s to %s, %m",
stats_temp_file, stats_drift_file);
#else
/* we have no rename NFS of ftp in use */
if ((fp = fopen(stats_drift_file, "w")) ==
NULL) {
msyslog(LOG_ERR,
"frequency file %s: %m",
stats_drift_file);
return;
}
#endif
/*
* If an option was given on the command line make sure it takes
* precedence over the configuration file, as command-line options
* are processed first. Similarly, if an option is given in the
* configuration file, do not allow it to be overridden with runtime
* configuration. Done by simply remembering an option was already
* seen.
*/
static int
allow_config(
u_int option,
int/*BOOL*/ cmdopt
)
{
static u_int seen = 0; /* stat options previously set */
u_int mask;
int retv;
/*
* stats_config - configure the stats operation
*/
void
stats_config(
int item,
const char *invalue, /* only one type so far */
int optflag
)
{
FILE *fp = NULL;
const char *value;
size_t len;
double old_drift;
l_fp now;
time_t ttnow;
char dirsep_or_nul;
#ifndef VMS
static const char temp_ext[] = ".TEMP";
#else
static const char temp_ext[] = "-TEMP";
#endif
/*
* Expand environment strings under Windows NT, since the
* command interpreter doesn't do this, the program must.
*/
#ifdef SYS_WINNT
char newvalue[MAX_PATH], parameter[MAX_PATH];
if (!ExpandEnvironmentStrings(invalue, newvalue, MAX_PATH)) {
switch (item) {
case STATS_FREQ_FILE:
strlcpy(parameter, "STATS_FREQ_FILE",
sizeof(parameter));
break;
case STATS_LEAP_FILE:
strlcpy(parameter, "STATS_LEAP_FILE",
sizeof(parameter));
break;
case STATS_STATSDIR:
strlcpy(parameter, "STATS_STATSDIR",
sizeof(parameter));
break;
case STATS_PID_FILE:
strlcpy(parameter, "STATS_PID_FILE",
sizeof(parameter));
break;
default:
strlcpy(parameter, "UNKNOWN",
sizeof(parameter));
break;
}
value = invalue;
msyslog(LOG_ERR,
"ExpandEnvironmentStrings(%s) failed: %m\n",
parameter);
} else {
value = newvalue;
}
#else
value = invalue;
#endif /* SYS_WINNT */
switch (item) {
/*
* Open and read frequency file.
*/
case STATS_FREQ_FILE:
if (!allow_config(STATS_FREQ_FILE, optflag)) {
break;
}
if (!value || 0 == (len = strlen(value))) {
free(stats_drift_file);
free(stats_temp_file);
stats_drift_file = stats_temp_file = NULL;
} else {
stats_drift_file = erealloc(stats_drift_file,
1 + len);
stats_temp_file = erealloc(stats_temp_file,
len + sizeof(temp_ext));
memcpy(stats_drift_file, value, 1 + len);
memcpy(stats_temp_file, value, len);
memcpy(stats_temp_file + len, temp_ext,
sizeof(temp_ext));
}
/*
* Open drift file and read frequency. If the file is
* missing or contains errors, tell the loop to reset.
*/
if (NULL == stats_drift_file) {
goto nofreq;
} else if ((fp = fopen(stats_drift_file, "r")) == NULL) {
if (errno != ENOENT) {
msyslog(LOG_WARNING,
"cannot read frequency file %s: %m",
stats_drift_file);
}
goto nofreq;
} else if (fscanf(fp, "%lf", &old_drift) != 1) {
msyslog(LOG_ERR,
"format error frequency file %s",
stats_drift_file);
nofreq:
prev_drift_comp = 0.0;
loop_config(LOOP_NOFREQ, prev_drift_comp);
} else {
loop_config(LOOP_FREQ, old_drift);
prev_drift_comp = drift_comp;
msyslog(LOG_INFO,
"initial drift restored to %.6f",
old_drift);
}
if (NULL != fp) {
fclose(fp);
}
break;
/*
* Specify statistics directory.
*/
case STATS_STATSDIR:
if (!allow_config(STATS_STATSDIR, optflag)) {
break;
}
/* - 2 since value may be missing the DIR_SEP. */
len = strlen(value);
if (len > sizeof(statsdir) - 2) {
msyslog(LOG_ERR,
"statsdir %s too long (>%u)", value,
(u_int)sizeof(statsdir) - 2);
break;
}
/* Add a DIR_SEP unless we already have one. */
if (0 == len || DIR_SEP == value[len - 1]) {
dirsep_or_nul = '\0';
} else {
dirsep_or_nul = DIR_SEP;
}
snprintf(statsdir, sizeof(statsdir), "%s%c",
value, dirsep_or_nul);
filegen_statsdir();
break;
/*
* Read leapseconds file.
*
* By default a leap file without SHA1 signature is accepted,
* but if there is a signature line, the signature must be
* valid or the leapfile line in ntp.conf must have ignorehash.
*/
case STATS_LEAP_FILE:
if (NULL == value || 0 == (len = strlen(value))) {
break;
}
leapfile_name = erealloc(leapfile_name, len + 1);
memcpy(leapfile_name, value, len + 1);
chck_leaphash = optflag;
if (leapsec_load_file(
leapfile_name, &leapfile_stat,
TRUE, TRUE, chck_leaphash)) {
leap_signature_t lsig;
/*
* record_raw_stats - write raw timestamps to file
*
* file format
* day (MJD)
* time (s past midnight)
* peer ip address
* IP address
* t1 t2 t3 t4 timestamps
* leap, version, mode, stratum, ppoll, precision, root delay, root dispersion, REFID
* length and hex dump of any EFs and any legacy MAC.
*/
void
record_raw_stats(
sockaddr_u *srcadr,
sockaddr_u *dstadr,
l_fp *t1, /* originate timestamp */
l_fp *t2, /* receive timestamp */
l_fp *t3, /* transmit timestamp */
l_fp *t4, /* destination timestamp */
int leap,
int version,
int mode,
int stratum,
int ppoll,
int precision,
double root_delay, /* seconds */
double root_dispersion,/* seconds */
u_int32 refid,
int len,
u_char *extra
)
{
l_fp now;
u_long day;
if (!stats_control)
return;
/*
* Mode 6 and mode 7 packets do not have the format of normal
* NTP packets and will log garbage. So don't. [Bug 3774]
*/
if (MODE_CONTROL == mode || MODE_PRIVATE == mode)
return;
fprintf(rawstats.fp, " %d: ", len);
for (i = 0; i < len; ++i) {
fprintf(rawstats.fp, "%02x", extra[i]);
}
}
fprintf(rawstats.fp, "\n");
fflush(rawstats.fp);
}
}
/*
* record_sys_stats - write system statistics to file
*
* file format
* day (MJD)
* time (s past midnight)
* time since reset
* packets recieved
* packets for this host
* current version
* old version
* access denied
* bad length or format
* bad authentication
* declined
* rate exceeded
* KoD sent
*/
void
record_sys_stats(void)
{
l_fp now;
u_long day;
/*
* record_proto_stats - write system statistics to file
*
* file format
* day (MJD)
* time (s past midnight)
* text message
*/
void
record_proto_stats(
char *str /* text string */
)
{
l_fp now;
u_long day;
/*
* check_leap_file - See if the leapseconds file has been updated.
*
* Returns: n/a
*
* Note: This loads a new leapfile on the fly. Currently a leap file
* without SHA1 signature is accepted, but if there is a signature line,
* the signature must be valid unless the ntp.conf leapfile line specified
* ignorehash.
*/
void
check_leap_file(
int is_daily_check,
uint32_t ntptime,
const time_t * systime
)
{
/* just do nothing if there is no leap file */
if ( ! (leapfile_name && *leapfile_name))
return;
/* try to load leapfile, force it if no leapfile loaded yet */
if (leapsec_load_file(
leapfile_name, &leapfile_stat,
!have_leapfile, is_daily_check, chck_leaphash))
have_leapfile = TRUE;
else if (!have_leapfile)
return;
/*
* check expiration of a loaded leap table
*/
static void
check_leap_expiration(
int is_daily_check,
uint32_t ntptime ,
const time_t *systime
)
{
static const char * const logPrefix = "leapsecond file";
int rc;
/* test the expiration of the leap data and log with proper
* level and frequency (once/hour or once/day, depending on the
* state.
*/
rc = leapsec_daystolive(ntptime, systime);
if (rc == 0) {
msyslog(LOG_WARNING,
"%s ('%s'): will expire in less than one day",
logPrefix, leapfile_name);
} else if (is_daily_check && rc < 28) {
if (rc < 0)
msyslog(LOG_ERR,
"%s ('%s'): expired %d day%s ago",
logPrefix, leapfile_name, -rc, (rc == -1 ? "" : "s"));
else
msyslog(LOG_WARNING,
"%s ('%s'): will expire in less than %d days",
logPrefix, leapfile_name, 1+rc);
}
}
/*
* getauthkeys - read the authentication keys from the specified file
*/
void
getauthkeys(
const char *keyfile
)
{
size_t len;
/*
* ntpd_time_stepped is called back by step_systime(), allowing ntpd
* to do any one-time processing necessitated by the step.
*/
void
ntpd_time_stepped(void)
{
u_int saved_mon_enabled;
/*
* flush the monitor MRU list which contains l_fp timestamps
* which should not be compared across the step.
*/
if (MON_OFF != mon_enabled) {
saved_mon_enabled = mon_enabled;
mon_stop(MON_OFF);
mon_start(saved_mon_enabled);
}
/* inform interpolating Windows code to allow time to go back */
#ifdef SYS_WINNT
win_time_stepped();
#endif
}
#ifdef DEBUG
void
append_flagstr(
char * flagstr,
size_t sz,
const char * text
)
{
if ('\0' != flagstr[0]) {
strlcat(flagstr, ",", sz);
}
/* bail if we ran out of room */
INSIST(strlcat(flagstr, text, sz) < sz);
}
#endif /* DEBUG */