1999-12-09 13:01:21 +00:00
|
|
|
/*
|
|
|
|
* ntp_util.c - stuff I didn't have any other place for
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
2001-08-29 14:35:15 +00:00
|
|
|
# include <config.h>
|
1999-12-09 13:01:21 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "ntpd.h"
|
|
|
|
#include "ntp_unixtime.h"
|
|
|
|
#include "ntp_filegen.h"
|
|
|
|
#include "ntp_if.h"
|
|
|
|
#include "ntp_stdlib.h"
|
2013-12-04 21:33:17 +00:00
|
|
|
#include "ntp_assert.h"
|
2014-12-20 22:52:39 +00:00
|
|
|
#include "ntp_calendar.h"
|
|
|
|
#include "ntp_leapsec.h"
|
|
|
|
#include "lib_strbuf.h"
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2001-08-29 14:35:15 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
|
|
|
# include <sys/ioctl.h>
|
|
|
|
#endif
|
2014-12-20 22:52:39 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
#include <sys/stat.h>
|
2001-08-29 14:35:15 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_IEEEFP_H
|
|
|
|
# include <ieeefp.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_MATH_H
|
|
|
|
# include <math.h>
|
|
|
|
#endif
|
|
|
|
|
1999-12-09 13:01:21 +00:00
|
|
|
#if defined(VMS)
|
2008-08-18 14:26:05 +00:00
|
|
|
# include <descrip.h>
|
1999-12-09 13:01:21 +00:00
|
|
|
#endif /* VMS */
|
|
|
|
|
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* Defines used by the leapseconds stuff
|
1999-12-09 13:01:21 +00:00
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
#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 */
|
|
|
|
|
1999-12-09 13:01:21 +00:00
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* This contains odds and ends, including the hourly stats, various
|
|
|
|
* configuration items, leapseconds stuff, etc.
|
1999-12-09 13:01:21 +00:00
|
|
|
*/
|
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* File names
|
1999-12-09 13:01:21 +00:00
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
static char *key_file_name; /* keys file name */
|
2014-12-20 22:52:39 +00:00
|
|
|
static char *leapfile_name; /* leapseconds file name */
|
|
|
|
static struct stat leapfile_stat; /* leapseconds file stat() buffer */
|
|
|
|
static int /*BOOL*/have_leapfile = FALSE;
|
2013-12-04 21:33:17 +00:00
|
|
|
char *stats_drift_file; /* frequency file name */
|
|
|
|
static char *stats_temp_file; /* temp frequency file name */
|
2014-12-20 22:52:39 +00:00
|
|
|
static double wander_resid; /* last frequency update */
|
|
|
|
double wander_threshold = 1e-7; /* initial frequency threshold */
|
1999-12-09 13:01:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Statistics file stuff
|
|
|
|
*/
|
|
|
|
#ifndef NTP_VAR
|
2008-08-18 14:26:05 +00:00
|
|
|
# ifndef SYS_WINNT
|
|
|
|
# define NTP_VAR "/var/NTP/" /* NOTE the trailing '/' */
|
|
|
|
# else
|
2013-12-04 21:33:17 +00:00
|
|
|
# define NTP_VAR "c:\\var\\ntp\\" /* NOTE the trailing '\\' */
|
2008-08-18 14:26:05 +00:00
|
|
|
# endif /* SYS_WINNT */
|
1999-12-09 13:01:21 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
char statsdir[MAXFILENAME] = NTP_VAR;
|
2013-12-04 21:33:17 +00:00
|
|
|
static FILEGEN peerstats;
|
|
|
|
static FILEGEN loopstats;
|
|
|
|
static FILEGEN clockstats;
|
|
|
|
static FILEGEN rawstats;
|
|
|
|
static FILEGEN sysstats;
|
|
|
|
static FILEGEN protostats;
|
2014-12-20 22:52:39 +00:00
|
|
|
static FILEGEN cryptostats;
|
|
|
|
static FILEGEN timingstats;
|
2013-12-04 21:33:17 +00:00
|
|
|
|
1999-12-09 13:01:21 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
/*
|
2014-12-20 22:52:39 +00:00
|
|
|
* Last frequency written to file.
|
2008-08-18 14:26:05 +00:00
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
static double prev_drift_comp; /* last frequency update */
|
2008-08-18 14:26:05 +00:00
|
|
|
|
1999-12-09 13:01:21 +00:00
|
|
|
/*
|
2014-12-20 22:52:39 +00:00
|
|
|
* Function prototypes
|
1999-12-09 13:01:21 +00:00
|
|
|
*/
|
2014-12-20 22:52:39 +00:00
|
|
|
static void record_sys_stats(void);
|
|
|
|
void ntpd_time_stepped(void);
|
|
|
|
static void check_leap_expiration(int, uint32_t, const time_t*);
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
|
|
|
* Prototypes
|
|
|
|
*/
|
|
|
|
#ifdef DEBUG
|
|
|
|
void uninit_util(void);
|
|
|
|
#endif
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
|
|
|
* uninit_util - free memory allocated by init_util
|
|
|
|
*/
|
|
|
|
#ifdef DEBUG
|
|
|
|
void
|
|
|
|
uninit_util(void)
|
|
|
|
{
|
|
|
|
#if defined(_MSC_VER) && defined (_DEBUG)
|
|
|
|
_CrtCheckMemory();
|
|
|
|
#endif
|
|
|
|
if (stats_drift_file) {
|
|
|
|
free(stats_drift_file);
|
|
|
|
free(stats_temp_file);
|
|
|
|
stats_drift_file = NULL;
|
|
|
|
stats_temp_file = NULL;
|
|
|
|
}
|
|
|
|
if (key_file_name) {
|
|
|
|
free(key_file_name);
|
|
|
|
key_file_name = NULL;
|
|
|
|
}
|
|
|
|
filegen_unregister("peerstats");
|
|
|
|
filegen_unregister("loopstats");
|
|
|
|
filegen_unregister("clockstats");
|
|
|
|
filegen_unregister("rawstats");
|
|
|
|
filegen_unregister("sysstats");
|
|
|
|
filegen_unregister("protostats");
|
2014-12-20 22:52:39 +00:00
|
|
|
#ifdef AUTOKEY
|
2013-12-04 21:33:17 +00:00
|
|
|
filegen_unregister("cryptostats");
|
2014-12-20 22:52:39 +00:00
|
|
|
#endif /* AUTOKEY */
|
2013-12-04 21:33:17 +00:00
|
|
|
#ifdef DEBUG_TIMING
|
|
|
|
filegen_unregister("timingstats");
|
2014-12-20 22:52:39 +00:00
|
|
|
#endif /* DEBUG_TIMING */
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
#if defined(_MSC_VER) && defined (_DEBUG)
|
|
|
|
_CrtCheckMemory();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif /* DEBUG */
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
2014-12-20 22:52:39 +00:00
|
|
|
* init_util - initialize the util module of ntpd
|
2013-12-04 21:33:17 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
init_util(void)
|
|
|
|
{
|
2014-12-20 22:52:39 +00:00
|
|
|
filegen_register(statsdir, "peerstats", &peerstats);
|
|
|
|
filegen_register(statsdir, "loopstats", &loopstats);
|
2013-12-04 21:33:17 +00:00
|
|
|
filegen_register(statsdir, "clockstats", &clockstats);
|
2014-12-20 22:52:39 +00:00
|
|
|
filegen_register(statsdir, "rawstats", &rawstats);
|
|
|
|
filegen_register(statsdir, "sysstats", &sysstats);
|
2013-12-04 21:33:17 +00:00
|
|
|
filegen_register(statsdir, "protostats", &protostats);
|
|
|
|
filegen_register(statsdir, "cryptostats", &cryptostats);
|
|
|
|
filegen_register(statsdir, "timingstats", &timingstats);
|
2014-12-20 22:52:39 +00:00
|
|
|
/*
|
|
|
|
* register with libntp ntp_set_tod() to call us back
|
|
|
|
* when time is stepped.
|
|
|
|
*/
|
|
|
|
step_callback = &ntpd_time_stepped;
|
2013-12-04 21:33:17 +00:00
|
|
|
#ifdef DEBUG
|
2014-12-20 22:52:39 +00:00
|
|
|
atexit(&uninit_util);
|
2013-12-04 21:33:17 +00:00
|
|
|
#endif /* DEBUG */
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* hourly_stats - print some interesting stats
|
|
|
|
*/
|
|
|
|
void
|
2008-08-18 14:26:05 +00:00
|
|
|
write_stats(void)
|
1999-12-09 13:01:21 +00:00
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
FILE *fp;
|
1999-12-09 13:01:21 +00:00
|
|
|
#ifdef DOSYNCTODR
|
|
|
|
struct timeval tv;
|
|
|
|
#if !defined(VMS)
|
2013-12-04 21:33:17 +00:00
|
|
|
int prio_set;
|
1999-12-09 13:01:21 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_GETCLOCK
|
2014-12-20 22:52:39 +00:00
|
|
|
struct timespec ts;
|
1999-12-09 13:01:21 +00:00
|
|
|
#endif
|
2013-12-04 21:33:17 +00:00
|
|
|
int o_prio;
|
1999-12-09 13:01:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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,
|
2014-12-20 22:52:39 +00:00
|
|
|
* so setting DOSYNCTODR seems a bad idea in the
|
|
|
|
* case of us resolution
|
1999-12-09 13:01:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#if !defined(VMS)
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
|
|
|
* (prr) getpriority returns -1 on error, but -1 is also a valid
|
2004-07-20 15:01:56 +00:00
|
|
|
* return value (!), so instead we have to zero errno before the
|
|
|
|
* call and check it for non-zero afterwards.
|
1999-12-09 13:01:21 +00:00
|
|
|
*/
|
|
|
|
errno = 0;
|
|
|
|
prio_set = 0;
|
|
|
|
o_prio = getpriority(PRIO_PROCESS,0); /* Save setting */
|
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
/*
|
|
|
|
* (prr) if getpriority succeeded, call setpriority to raise
|
1999-12-09 13:01:21 +00:00
|
|
|
* scheduling priority as high as possible. If that succeeds
|
|
|
|
* as well, set the prio_set flag so we remember to reset
|
2004-07-20 15:01:56 +00:00
|
|
|
* 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.
|
1999-12-09 13:01:21 +00:00
|
|
|
*/
|
|
|
|
if ((errno == 0) && (setpriority(PRIO_PROCESS,0,-20) == 0))
|
2004-07-20 15:01:56 +00:00
|
|
|
prio_set = 1; /* overdrive */
|
1999-12-09 13:01:21 +00:00
|
|
|
#endif /* VMS */
|
|
|
|
#ifdef HAVE_GETCLOCK
|
2014-12-20 22:52:39 +00:00
|
|
|
(void) getclock(TIMEOFDAY, &ts);
|
|
|
|
tv.tv_sec = ts.tv_sec;
|
|
|
|
tv.tv_usec = ts.tv_nsec / 1000;
|
1999-12-09 13:01:21 +00:00
|
|
|
#else /* not HAVE_GETCLOCK */
|
|
|
|
GETTIMEOFDAY(&tv,(struct timezone *)NULL);
|
|
|
|
#endif /* not HAVE_GETCLOCK */
|
2013-12-04 21:33:17 +00:00
|
|
|
if (ntp_set_tod(&tv,(struct timezone *)NULL) != 0)
|
1999-12-09 13:01:21 +00:00
|
|
|
msyslog(LOG_ERR, "can't sync battery time: %m");
|
|
|
|
#if !defined(VMS)
|
|
|
|
if (prio_set)
|
2004-07-20 15:01:56 +00:00
|
|
|
setpriority(PRIO_PROCESS, 0, o_prio); /* downshift */
|
1999-12-09 13:01:21 +00:00
|
|
|
#endif /* VMS */
|
|
|
|
#endif /* DOSYNCTODR */
|
2004-07-20 15:01:56 +00:00
|
|
|
record_sys_stats();
|
2014-12-20 22:52:39 +00:00
|
|
|
if (stats_drift_file != 0) {
|
2013-12-04 21:33:17 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* When the frequency file is written, initialize the
|
2014-12-20 22:52:39 +00:00
|
|
|
* 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.
|
2013-12-04 21:33:17 +00:00
|
|
|
*/
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (debug)
|
2014-12-20 22:52:39 +00:00
|
|
|
printf("write_stats: frequency %.6lf thresh %.6lf, freq %.6lf\n",
|
|
|
|
(prev_drift_comp - drift_comp) * 1e6, wander_resid *
|
|
|
|
1e6, drift_comp * 1e6);
|
2013-12-04 21:33:17 +00:00
|
|
|
#endif
|
2014-12-20 22:52:39 +00:00
|
|
|
if (fabs(prev_drift_comp - drift_comp) < wander_resid) {
|
|
|
|
wander_resid *= 0.5;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
prev_drift_comp = drift_comp;
|
|
|
|
wander_resid = wander_threshold;
|
|
|
|
if ((fp = fopen(stats_temp_file, "w")) == NULL) {
|
|
|
|
msyslog(LOG_ERR, "frequency file %s: %m",
|
|
|
|
stats_temp_file);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
fprintf(fp, "%.3f\n", drift_comp * 1e6);
|
|
|
|
(void)fclose(fp);
|
|
|
|
/* atomic */
|
1999-12-09 13:01:21 +00:00
|
|
|
#ifdef SYS_WINNT
|
2014-12-20 22:52:39 +00:00
|
|
|
if (_unlink(stats_drift_file)) /* rename semantics differ under NT */
|
|
|
|
msyslog(LOG_WARNING,
|
|
|
|
"Unable to remove prior drift file %s, %m",
|
|
|
|
stats_drift_file);
|
1999-12-09 13:01:21 +00:00
|
|
|
#endif /* SYS_WINNT */
|
|
|
|
|
|
|
|
#ifndef NO_RENAME
|
2014-12-20 22:52:39 +00:00
|
|
|
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);
|
1999-12-09 13:01:21 +00:00
|
|
|
#else
|
2014-12-20 22:52:39 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
1999-12-09 13:01:21 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(VMS)
|
2014-12-20 22:52:39 +00:00
|
|
|
/* PURGE */
|
|
|
|
{
|
|
|
|
$DESCRIPTOR(oldvers,";-1");
|
|
|
|
struct dsc$descriptor driftdsc = {
|
|
|
|
strlen(stats_drift_file), 0, 0,
|
|
|
|
stats_drift_file };
|
|
|
|
while(lib$delete_file(&oldvers,
|
|
|
|
&driftdsc) & 1);
|
2013-12-04 21:33:17 +00:00
|
|
|
}
|
2014-12-20 22:52:39 +00:00
|
|
|
#endif
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* stats_config - configure the stats operation
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
stats_config(
|
|
|
|
int item,
|
2008-08-18 14:26:05 +00:00
|
|
|
const char *invalue /* only one type so far */
|
1999-12-09 13:01:21 +00:00
|
|
|
)
|
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
FILE *fp;
|
2008-08-18 14:26:05 +00:00
|
|
|
const char *value;
|
2016-01-08 08:06:14 +00:00
|
|
|
size_t len;
|
2014-12-20 22:52:39 +00:00
|
|
|
double old_drift;
|
|
|
|
l_fp now;
|
|
|
|
time_t ttnow;
|
2013-12-04 21:33:17 +00:00
|
|
|
#ifndef VMS
|
|
|
|
const char temp_ext[] = ".TEMP";
|
|
|
|
#else
|
|
|
|
const char temp_ext[] = "-TEMP";
|
|
|
|
#endif
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
/*
|
|
|
|
* Expand environment strings under Windows NT, since the
|
|
|
|
* command interpreter doesn't do this, the program must.
|
1999-12-09 13:01:21 +00:00
|
|
|
*/
|
|
|
|
#ifdef SYS_WINNT
|
|
|
|
char newvalue[MAX_PATH], parameter[MAX_PATH];
|
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
if (!ExpandEnvironmentStrings(invalue, newvalue, MAX_PATH)) {
|
2014-12-20 22:52:39 +00:00
|
|
|
switch (item) {
|
|
|
|
case STATS_FREQ_FILE:
|
|
|
|
strlcpy(parameter, "STATS_FREQ_FILE",
|
|
|
|
sizeof(parameter));
|
1999-12-09 13:01:21 +00:00
|
|
|
break;
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
case STATS_LEAP_FILE:
|
|
|
|
strlcpy(parameter, "STATS_LEAP_FILE",
|
|
|
|
sizeof(parameter));
|
2013-12-04 21:33:17 +00:00
|
|
|
break;
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
case STATS_STATSDIR:
|
|
|
|
strlcpy(parameter, "STATS_STATSDIR",
|
|
|
|
sizeof(parameter));
|
1999-12-09 13:01:21 +00:00
|
|
|
break;
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
case STATS_PID_FILE:
|
|
|
|
strlcpy(parameter, "STATS_PID_FILE",
|
|
|
|
sizeof(parameter));
|
1999-12-09 13:01:21 +00:00
|
|
|
break;
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
default:
|
|
|
|
strlcpy(parameter, "UNKNOWN",
|
|
|
|
sizeof(parameter));
|
1999-12-09 13:01:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
value = invalue;
|
|
|
|
msyslog(LOG_ERR,
|
2014-12-20 22:52:39 +00:00
|
|
|
"ExpandEnvironmentStrings(%s) failed: %m\n",
|
|
|
|
parameter);
|
2004-07-20 15:01:56 +00:00
|
|
|
} else {
|
1999-12-09 13:01:21 +00:00
|
|
|
value = newvalue;
|
2004-07-20 15:01:56 +00:00
|
|
|
}
|
2014-12-20 22:52:39 +00:00
|
|
|
#else
|
1999-12-09 13:01:21 +00:00
|
|
|
value = invalue;
|
|
|
|
#endif /* SYS_WINNT */
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
switch (item) {
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
|
|
|
* Open and read frequency file.
|
|
|
|
*/
|
|
|
|
case STATS_FREQ_FILE:
|
|
|
|
if (!value || (len = strlen(value)) == 0)
|
|
|
|
break;
|
1999-12-09 13:01:21 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
stats_drift_file = erealloc(stats_drift_file, len + 1);
|
|
|
|
stats_temp_file = erealloc(stats_temp_file,
|
2014-12-20 22:52:39 +00:00
|
|
|
len + sizeof(".TEMP"));
|
|
|
|
memcpy(stats_drift_file, value, (size_t)(len+1));
|
|
|
|
memcpy(stats_temp_file, value, (size_t)len);
|
|
|
|
memcpy(stats_temp_file + len, temp_ext, sizeof(temp_ext));
|
1999-12-09 13:01:21 +00:00
|
|
|
|
|
|
|
/*
|
2004-07-20 15:01:56 +00:00
|
|
|
* Open drift file and read frequency. If the file is
|
|
|
|
* missing or contains errors, tell the loop to reset.
|
1999-12-09 13:01:21 +00:00
|
|
|
*/
|
2013-12-04 21:33:17 +00:00
|
|
|
if ((fp = fopen(stats_drift_file, "r")) == NULL)
|
1999-12-09 13:01:21 +00:00
|
|
|
break;
|
2013-12-04 21:33:17 +00:00
|
|
|
|
1999-12-09 13:01:21 +00:00
|
|
|
if (fscanf(fp, "%lf", &old_drift) != 1) {
|
2013-12-04 21:33:17 +00:00
|
|
|
msyslog(LOG_ERR,
|
|
|
|
"format error frequency file %s",
|
|
|
|
stats_drift_file);
|
2004-07-20 15:01:56 +00:00
|
|
|
fclose(fp);
|
1999-12-09 13:01:21 +00:00
|
|
|
break;
|
2013-12-04 21:33:17 +00:00
|
|
|
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
2004-07-20 15:01:56 +00:00
|
|
|
fclose(fp);
|
2014-12-20 22:52:39 +00:00
|
|
|
loop_config(LOOP_FREQ, old_drift);
|
|
|
|
prev_drift_comp = drift_comp;
|
1999-12-09 13:01:21 +00:00
|
|
|
break;
|
2013-12-04 21:33:17 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Specify statistics directory.
|
|
|
|
*/
|
|
|
|
case STATS_STATSDIR:
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
/* - 1 since value may be missing the DIR_SEP. */
|
|
|
|
if (strlen(value) >= sizeof(statsdir) - 1) {
|
1999-12-09 13:01:21 +00:00
|
|
|
msyslog(LOG_ERR,
|
2013-12-04 21:33:17 +00:00
|
|
|
"statsdir too long (>%d, sigh)",
|
2014-12-20 22:52:39 +00:00
|
|
|
(int)sizeof(statsdir) - 2);
|
1999-12-09 13:01:21 +00:00
|
|
|
} else {
|
2013-12-04 21:33:17 +00:00
|
|
|
int add_dir_sep;
|
2016-01-08 08:06:14 +00:00
|
|
|
size_t value_l;
|
2013-12-04 21:33:17 +00:00
|
|
|
|
|
|
|
/* Add a DIR_SEP unless we already have one. */
|
2014-12-20 22:52:39 +00:00
|
|
|
value_l = strlen(value);
|
|
|
|
if (0 == value_l)
|
|
|
|
add_dir_sep = FALSE;
|
2013-12-04 21:33:17 +00:00
|
|
|
else
|
|
|
|
add_dir_sep = (DIR_SEP !=
|
|
|
|
value[value_l - 1]);
|
|
|
|
|
|
|
|
if (add_dir_sep)
|
2014-12-20 22:52:39 +00:00
|
|
|
snprintf(statsdir, sizeof(statsdir),
|
|
|
|
"%s%c", value, DIR_SEP);
|
2013-12-04 21:33:17 +00:00
|
|
|
else
|
2014-12-20 22:52:39 +00:00
|
|
|
snprintf(statsdir, sizeof(statsdir),
|
|
|
|
"%s", value);
|
|
|
|
filegen_statsdir();
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
|
|
|
* Open pid file.
|
|
|
|
*/
|
|
|
|
case STATS_PID_FILE:
|
1999-12-09 13:01:21 +00:00
|
|
|
if ((fp = fopen(value, "w")) == NULL) {
|
2013-12-04 21:33:17 +00:00
|
|
|
msyslog(LOG_ERR, "pid file %s: %m",
|
|
|
|
value);
|
1999-12-09 13:01:21 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-12-04 21:33:17 +00:00
|
|
|
fprintf(fp, "%d", (int)getpid());
|
2014-12-20 22:52:39 +00:00
|
|
|
fclose(fp);
|
1999-12-09 13:01:21 +00:00
|
|
|
break;
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
|
|
|
* Read leapseconds file.
|
2014-12-20 22:52:39 +00:00
|
|
|
*
|
|
|
|
* Note: Currently a leap file without SHA1 signature is
|
|
|
|
* accepted, but if there is a signature line, the signature
|
|
|
|
* must be valid or the file is rejected.
|
2013-12-04 21:33:17 +00:00
|
|
|
*/
|
|
|
|
case STATS_LEAP_FILE:
|
2014-12-20 22:52:39 +00:00
|
|
|
if (!value || (len = strlen(value)) == 0)
|
2013-12-04 21:33:17 +00:00
|
|
|
break;
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
leapfile_name = erealloc(leapfile_name, len + 1);
|
|
|
|
memcpy(leapfile_name, value, len + 1);
|
|
|
|
|
|
|
|
if (leapsec_load_file(
|
|
|
|
leapfile_name, &leapfile_stat, TRUE, TRUE))
|
|
|
|
{
|
|
|
|
leap_signature_t lsig;
|
|
|
|
|
|
|
|
get_systime(&now);
|
|
|
|
time(&ttnow);
|
|
|
|
leapsec_getsig(&lsig);
|
|
|
|
mprintf_event(EVNT_TAI, NULL,
|
|
|
|
"%d leap %s %s %s",
|
|
|
|
lsig.taiof,
|
|
|
|
fstostr(lsig.ttime),
|
|
|
|
leapsec_expired(now.l_ui, NULL)
|
|
|
|
? "expired"
|
|
|
|
: "expires",
|
|
|
|
fstostr(lsig.etime));
|
|
|
|
|
|
|
|
have_leapfile = TRUE;
|
|
|
|
|
|
|
|
/* force an immediate daily expiration check of
|
|
|
|
* the leap seconds table
|
|
|
|
*/
|
|
|
|
check_leap_expiration(TRUE, now.l_ui, &ttnow);
|
2013-12-04 21:33:17 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1999-12-09 13:01:21 +00:00
|
|
|
/* oh well */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
1999-12-09 13:01:21 +00:00
|
|
|
/*
|
|
|
|
* record_peer_stats - write peer statistics to file
|
|
|
|
*
|
|
|
|
* file format:
|
2013-12-04 21:33:17 +00:00
|
|
|
* day (MJD)
|
1999-12-09 13:01:21 +00:00
|
|
|
* time (s past UTC midnight)
|
2013-12-04 21:33:17 +00:00
|
|
|
* IP address
|
|
|
|
* status word (hex)
|
|
|
|
* offset
|
|
|
|
* delay
|
|
|
|
* dispersion
|
|
|
|
* jitter
|
1999-12-09 13:01:21 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
record_peer_stats(
|
2013-12-04 21:33:17 +00:00
|
|
|
sockaddr_u *addr,
|
2004-07-20 15:01:56 +00:00
|
|
|
int status,
|
2013-12-04 21:33:17 +00:00
|
|
|
double offset, /* offset */
|
|
|
|
double delay, /* delay */
|
|
|
|
double dispersion, /* dispersion */
|
|
|
|
double jitter /* jitter */
|
1999-12-09 13:01:21 +00:00
|
|
|
)
|
|
|
|
{
|
2004-07-20 15:01:56 +00:00
|
|
|
l_fp now;
|
|
|
|
u_long day;
|
1999-12-09 13:01:21 +00:00
|
|
|
|
|
|
|
if (!stats_control)
|
|
|
|
return;
|
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
get_systime(&now);
|
|
|
|
filegen_setup(&peerstats, now.l_ui);
|
|
|
|
day = now.l_ui / 86400 + MJD_1900;
|
|
|
|
now.l_ui %= 86400;
|
1999-12-09 13:01:21 +00:00
|
|
|
if (peerstats.fp != NULL) {
|
|
|
|
fprintf(peerstats.fp,
|
2013-12-04 21:33:17 +00:00
|
|
|
"%lu %s %s %x %.9f %.9f %.9f %.9f\n", day,
|
|
|
|
ulfptoa(&now, 3), stoa(addr), status, offset,
|
|
|
|
delay, dispersion, jitter);
|
1999-12-09 13:01:21 +00:00
|
|
|
fflush(peerstats.fp);
|
|
|
|
}
|
|
|
|
}
|
2008-08-18 14:26:05 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
1999-12-09 13:01:21 +00:00
|
|
|
/*
|
|
|
|
* record_loop_stats - write loop filter statistics to file
|
|
|
|
*
|
|
|
|
* file format:
|
2013-12-04 21:33:17 +00:00
|
|
|
* day (MJD)
|
1999-12-09 13:01:21 +00:00
|
|
|
* time (s past midnight)
|
2013-12-04 21:33:17 +00:00
|
|
|
* offset
|
|
|
|
* frequency (PPM)
|
|
|
|
* jitter
|
|
|
|
* wnder (PPM)
|
|
|
|
* time constant (log2)
|
1999-12-09 13:01:21 +00:00
|
|
|
*/
|
|
|
|
void
|
2001-08-29 14:35:15 +00:00
|
|
|
record_loop_stats(
|
2013-12-04 21:33:17 +00:00
|
|
|
double offset, /* offset */
|
|
|
|
double freq, /* frequency (PPM) */
|
|
|
|
double jitter, /* jitter */
|
|
|
|
double wander, /* wander (PPM) */
|
2004-07-20 15:01:56 +00:00
|
|
|
int spoll
|
2001-08-29 14:35:15 +00:00
|
|
|
)
|
1999-12-09 13:01:21 +00:00
|
|
|
{
|
2004-07-20 15:01:56 +00:00
|
|
|
l_fp now;
|
|
|
|
u_long day;
|
1999-12-09 13:01:21 +00:00
|
|
|
|
|
|
|
if (!stats_control)
|
|
|
|
return;
|
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
get_systime(&now);
|
|
|
|
filegen_setup(&loopstats, now.l_ui);
|
|
|
|
day = now.l_ui / 86400 + MJD_1900;
|
|
|
|
now.l_ui %= 86400;
|
1999-12-09 13:01:21 +00:00
|
|
|
if (loopstats.fp != NULL) {
|
2008-08-18 14:26:05 +00:00
|
|
|
fprintf(loopstats.fp, "%lu %s %.9f %.3f %.9f %.6f %d\n",
|
2004-07-20 15:01:56 +00:00
|
|
|
day, ulfptoa(&now, 3), offset, freq * 1e6, jitter,
|
2013-12-04 21:33:17 +00:00
|
|
|
wander * 1e6, spoll);
|
1999-12-09 13:01:21 +00:00
|
|
|
fflush(loopstats.fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
1999-12-09 13:01:21 +00:00
|
|
|
/*
|
|
|
|
* record_clock_stats - write clock statistics to file
|
|
|
|
*
|
|
|
|
* file format:
|
2013-12-04 21:33:17 +00:00
|
|
|
* day (MJD)
|
1999-12-09 13:01:21 +00:00
|
|
|
* time (s past midnight)
|
2013-12-04 21:33:17 +00:00
|
|
|
* IP address
|
1999-12-09 13:01:21 +00:00
|
|
|
* text message
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
record_clock_stats(
|
2013-12-04 21:33:17 +00:00
|
|
|
sockaddr_u *addr,
|
|
|
|
const char *text /* timecode string */
|
1999-12-09 13:01:21 +00:00
|
|
|
)
|
|
|
|
{
|
2004-07-20 15:01:56 +00:00
|
|
|
l_fp now;
|
|
|
|
u_long day;
|
1999-12-09 13:01:21 +00:00
|
|
|
|
|
|
|
if (!stats_control)
|
|
|
|
return;
|
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
get_systime(&now);
|
|
|
|
filegen_setup(&clockstats, now.l_ui);
|
|
|
|
day = now.l_ui / 86400 + MJD_1900;
|
|
|
|
now.l_ui %= 86400;
|
1999-12-09 13:01:21 +00:00
|
|
|
if (clockstats.fp != NULL) {
|
2013-12-04 21:33:17 +00:00
|
|
|
fprintf(clockstats.fp, "%lu %s %s %s\n", day,
|
|
|
|
ulfptoa(&now, 3), stoa(addr), text);
|
1999-12-09 13:01:21 +00:00
|
|
|
fflush(clockstats.fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
/*
|
|
|
|
* mprintf_clock_stats - write clock statistics to file with
|
|
|
|
* msnprintf-style formatting.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
mprintf_clock_stats(
|
|
|
|
sockaddr_u *addr,
|
|
|
|
const char *fmt,
|
|
|
|
...
|
|
|
|
)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
int rc;
|
|
|
|
char msg[512];
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
rc = mvsnprintf(msg, sizeof(msg), fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
if (stats_control)
|
|
|
|
record_clock_stats(addr, msg);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
1999-12-09 13:01:21 +00:00
|
|
|
/*
|
|
|
|
* record_raw_stats - write raw timestamps to file
|
|
|
|
*
|
|
|
|
* file format
|
2013-12-04 21:33:17 +00:00
|
|
|
* day (MJD)
|
1999-12-09 13:01:21 +00:00
|
|
|
* time (s past midnight)
|
|
|
|
* peer ip address
|
2013-12-04 21:33:17 +00:00
|
|
|
* IP address
|
1999-12-09 13:01:21 +00:00
|
|
|
* t1 t2 t3 t4 timestamps
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
record_raw_stats(
|
2013-12-04 21:33:17 +00:00
|
|
|
sockaddr_u *srcadr,
|
|
|
|
sockaddr_u *dstadr,
|
|
|
|
l_fp *t1, /* originate timestamp */
|
|
|
|
l_fp *t2, /* receive timestamp */
|
|
|
|
l_fp *t3, /* transmit timestamp */
|
2014-12-20 22:52:39 +00:00
|
|
|
l_fp *t4, /* destination timestamp */
|
|
|
|
int leap,
|
|
|
|
int version,
|
|
|
|
int mode,
|
|
|
|
int stratum,
|
2015-02-05 20:53:33 +00:00
|
|
|
int ppoll,
|
2014-12-20 22:52:39 +00:00
|
|
|
int precision,
|
|
|
|
double root_delay, /* seconds */
|
|
|
|
double root_dispersion,/* seconds */
|
|
|
|
u_int32 refid
|
1999-12-09 13:01:21 +00:00
|
|
|
)
|
|
|
|
{
|
2004-07-20 15:01:56 +00:00
|
|
|
l_fp now;
|
|
|
|
u_long day;
|
1999-12-09 13:01:21 +00:00
|
|
|
|
|
|
|
if (!stats_control)
|
|
|
|
return;
|
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
get_systime(&now);
|
|
|
|
filegen_setup(&rawstats, now.l_ui);
|
|
|
|
day = now.l_ui / 86400 + MJD_1900;
|
|
|
|
now.l_ui %= 86400;
|
1999-12-09 13:01:21 +00:00
|
|
|
if (rawstats.fp != NULL) {
|
2014-12-20 22:52:39 +00:00
|
|
|
fprintf(rawstats.fp, "%lu %s %s %s %s %s %s %s %d %d %d %d %d %d %.6f %.6f %s\n",
|
|
|
|
day, ulfptoa(&now, 3),
|
|
|
|
stoa(srcadr), dstadr ? stoa(dstadr) : "-",
|
|
|
|
ulfptoa(t1, 9), ulfptoa(t2, 9),
|
|
|
|
ulfptoa(t3, 9), ulfptoa(t4, 9),
|
2015-02-05 20:53:33 +00:00
|
|
|
leap, version, mode, stratum, ppoll, precision,
|
2014-12-20 22:52:39 +00:00
|
|
|
root_delay, root_dispersion, refid_str(refid, stratum));
|
1999-12-09 13:01:21 +00:00
|
|
|
fflush(rawstats.fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-20 15:01:56 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* record_sys_stats - write system statistics to file
|
|
|
|
*
|
|
|
|
* file format
|
2013-12-04 21:33:17 +00:00
|
|
|
* day (MJD)
|
2004-07-20 15:01:56 +00:00
|
|
|
* time (s past midnight)
|
2013-12-04 21:33:17 +00:00
|
|
|
* time since reset
|
2004-07-20 15:01:56 +00:00
|
|
|
* packets recieved
|
2013-12-04 21:33:17 +00:00
|
|
|
* packets for this host
|
2004-07-20 15:01:56 +00:00
|
|
|
* current version
|
2013-12-04 21:33:17 +00:00
|
|
|
* old version
|
2004-07-20 15:01:56 +00:00
|
|
|
* access denied
|
|
|
|
* bad length or format
|
|
|
|
* bad authentication
|
2013-12-04 21:33:17 +00:00
|
|
|
* declined
|
2004-07-20 15:01:56 +00:00
|
|
|
* rate exceeded
|
2013-12-04 21:33:17 +00:00
|
|
|
* KoD sent
|
2004-07-20 15:01:56 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
record_sys_stats(void)
|
|
|
|
{
|
|
|
|
l_fp now;
|
|
|
|
u_long day;
|
|
|
|
|
|
|
|
if (!stats_control)
|
|
|
|
return;
|
|
|
|
|
|
|
|
get_systime(&now);
|
|
|
|
filegen_setup(&sysstats, now.l_ui);
|
|
|
|
day = now.l_ui / 86400 + MJD_1900;
|
|
|
|
now.l_ui %= 86400;
|
|
|
|
if (sysstats.fp != NULL) {
|
2013-12-04 21:33:17 +00:00
|
|
|
fprintf(sysstats.fp,
|
|
|
|
"%lu %s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n",
|
|
|
|
day, ulfptoa(&now, 3), current_time - sys_stattime,
|
|
|
|
sys_received, sys_processed, sys_newversion,
|
|
|
|
sys_oldversion, sys_restricted, sys_badlength,
|
|
|
|
sys_badauth, sys_declined, sys_limitrejected,
|
|
|
|
sys_kodsent);
|
2004-07-20 15:01:56 +00:00
|
|
|
fflush(sysstats.fp);
|
|
|
|
proto_clr_stats();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
if (!stats_control)
|
|
|
|
return;
|
|
|
|
|
|
|
|
get_systime(&now);
|
|
|
|
filegen_setup(&protostats, now.l_ui);
|
|
|
|
day = now.l_ui / 86400 + MJD_1900;
|
|
|
|
now.l_ui %= 86400;
|
|
|
|
if (protostats.fp != NULL) {
|
|
|
|
fprintf(protostats.fp, "%lu %s %s\n", day,
|
|
|
|
ulfptoa(&now, 3), str);
|
|
|
|
fflush(protostats.fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
#ifdef AUTOKEY
|
2004-07-20 15:01:56 +00:00
|
|
|
/*
|
|
|
|
* record_crypto_stats - write crypto statistics to file
|
|
|
|
*
|
|
|
|
* file format:
|
|
|
|
* day (mjd)
|
|
|
|
* time (s past midnight)
|
2013-12-04 21:33:17 +00:00
|
|
|
* peer ip address
|
2004-07-20 15:01:56 +00:00
|
|
|
* text message
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
record_crypto_stats(
|
2013-12-04 21:33:17 +00:00
|
|
|
sockaddr_u *addr,
|
|
|
|
const char *text /* text message */
|
2004-07-20 15:01:56 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
l_fp now;
|
|
|
|
u_long day;
|
|
|
|
|
|
|
|
if (!stats_control)
|
|
|
|
return;
|
|
|
|
|
|
|
|
get_systime(&now);
|
|
|
|
filegen_setup(&cryptostats, now.l_ui);
|
|
|
|
day = now.l_ui / 86400 + MJD_1900;
|
|
|
|
now.l_ui %= 86400;
|
|
|
|
if (cryptostats.fp != NULL) {
|
|
|
|
if (addr == NULL)
|
2013-12-04 21:33:17 +00:00
|
|
|
fprintf(cryptostats.fp, "%lu %s 0.0.0.0 %s\n",
|
2004-07-20 15:01:56 +00:00
|
|
|
day, ulfptoa(&now, 3), text);
|
|
|
|
else
|
|
|
|
fprintf(cryptostats.fp, "%lu %s %s %s\n",
|
|
|
|
day, ulfptoa(&now, 3), stoa(addr), text);
|
|
|
|
fflush(cryptostats.fp);
|
|
|
|
}
|
|
|
|
}
|
2014-12-20 22:52:39 +00:00
|
|
|
#endif /* AUTOKEY */
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
#ifdef DEBUG_TIMING
|
|
|
|
/*
|
2013-12-04 21:33:17 +00:00
|
|
|
* record_timing_stats - write timing statistics to file
|
2008-08-18 14:26:05 +00:00
|
|
|
*
|
|
|
|
* file format:
|
|
|
|
* day (mjd)
|
|
|
|
* time (s past midnight)
|
|
|
|
* text message
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
record_timing_stats(
|
2013-12-04 21:33:17 +00:00
|
|
|
const char *text /* text message */
|
2008-08-18 14:26:05 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
static unsigned int flshcnt;
|
|
|
|
l_fp now;
|
|
|
|
u_long day;
|
|
|
|
|
|
|
|
if (!stats_control)
|
|
|
|
return;
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
get_systime(&now);
|
|
|
|
filegen_setup(&timingstats, now.l_ui);
|
|
|
|
day = now.l_ui / 86400 + MJD_1900;
|
|
|
|
now.l_ui %= 86400;
|
|
|
|
if (timingstats.fp != NULL) {
|
2013-12-04 21:33:17 +00:00
|
|
|
fprintf(timingstats.fp, "%lu %s %s\n", day, lfptoa(&now,
|
|
|
|
3), text);
|
2008-08-18 14:26:05 +00:00
|
|
|
if (++flshcnt % 100 == 0)
|
|
|
|
fflush(timingstats.fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2013-12-04 21:33:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2014-12-20 22:52:39 +00:00
|
|
|
* check_leap_file - See if the leapseconds file has been updated.
|
2013-12-04 21:33:17 +00:00
|
|
|
*
|
2014-12-20 22:52:39 +00:00
|
|
|
* 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 or the file is rejected.
|
2013-12-04 21:33:17 +00:00
|
|
|
*/
|
2014-12-20 22:52:39 +00:00
|
|
|
void
|
|
|
|
check_leap_file(
|
|
|
|
int is_daily_check,
|
|
|
|
uint32_t ntptime ,
|
|
|
|
const time_t *systime
|
2013-12-04 21:33:17 +00:00
|
|
|
)
|
|
|
|
{
|
2014-12-20 22:52:39 +00:00
|
|
|
/* 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))
|
|
|
|
have_leapfile = TRUE;
|
|
|
|
else if (!have_leapfile)
|
|
|
|
return;
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
check_leap_expiration(is_daily_check, ntptime, systime);
|
2013-12-04 21:33:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-12-20 22:52:39 +00:00
|
|
|
* check expiration of a loaded leap table
|
2013-12-04 21:33:17 +00:00
|
|
|
*/
|
2014-12-20 22:52:39 +00:00
|
|
|
static void
|
|
|
|
check_leap_expiration(
|
|
|
|
int is_daily_check,
|
|
|
|
uint32_t ntptime ,
|
|
|
|
const time_t *systime
|
2013-12-04 21:33:17 +00:00
|
|
|
)
|
|
|
|
{
|
2014-12-20 22:52:39 +00:00
|
|
|
static const char * const logPrefix = "leapsecond file";
|
|
|
|
int rc;
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
/* test the expiration of the leap data and log with proper
|
|
|
|
* level and frequency (once/hour or once/day, depending on the
|
|
|
|
* state.
|
2013-12-04 21:33:17 +00:00
|
|
|
*/
|
2014-12-20 22:52:39 +00:00
|
|
|
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 less than %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);
|
2013-12-04 21:33:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-09 13:01:21 +00:00
|
|
|
/*
|
|
|
|
* getauthkeys - read the authentication keys from the specified file
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
getauthkeys(
|
2008-08-18 14:26:05 +00:00
|
|
|
const char *keyfile
|
1999-12-09 13:01:21 +00:00
|
|
|
)
|
|
|
|
{
|
2016-01-08 08:06:14 +00:00
|
|
|
size_t len;
|
1999-12-09 13:01:21 +00:00
|
|
|
|
|
|
|
len = strlen(keyfile);
|
2013-12-04 21:33:17 +00:00
|
|
|
if (!len)
|
1999-12-09 13:01:21 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
#ifndef SYS_WINNT
|
2013-12-04 21:33:17 +00:00
|
|
|
key_file_name = erealloc(key_file_name, len + 1);
|
2014-12-20 22:52:39 +00:00
|
|
|
memcpy(key_file_name, keyfile, len + 1);
|
1999-12-09 13:01:21 +00:00
|
|
|
#else
|
2013-12-04 21:33:17 +00:00
|
|
|
key_file_name = erealloc(key_file_name, _MAX_PATH);
|
|
|
|
if (len + 1 > _MAX_PATH)
|
|
|
|
return;
|
|
|
|
if (!ExpandEnvironmentStrings(keyfile, key_file_name,
|
|
|
|
_MAX_PATH)) {
|
1999-12-09 13:01:21 +00:00
|
|
|
msyslog(LOG_ERR,
|
2013-12-04 21:33:17 +00:00
|
|
|
"ExpandEnvironmentStrings(KEY_FILE) failed: %m");
|
2014-12-20 22:52:39 +00:00
|
|
|
strlcpy(key_file_name, keyfile, _MAX_PATH);
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
2014-12-20 22:52:39 +00:00
|
|
|
key_file_name = erealloc(key_file_name,
|
|
|
|
1 + strlen(key_file_name));
|
1999-12-09 13:01:21 +00:00
|
|
|
#endif /* SYS_WINNT */
|
|
|
|
|
|
|
|
authreadkeys(key_file_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* rereadkeys - read the authentication key file over again.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
rereadkeys(void)
|
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
if (NULL != key_file_name)
|
|
|
|
authreadkeys(key_file_name);
|
1999-12-09 13:01:21 +00:00
|
|
|
}
|
2004-07-20 15:01:56 +00:00
|
|
|
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2008-08-18 14:26:05 +00:00
|
|
|
#if notyet
|
|
|
|
/*
|
|
|
|
* ntp_exit - document explicitly that ntpd has exited
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ntp_exit(int retval)
|
|
|
|
{
|
2013-12-04 21:33:17 +00:00
|
|
|
msyslog(LOG_ERR, "EXITING with return code %d", retval);
|
|
|
|
exit(retval);
|
2008-08-18 14:26:05 +00:00
|
|
|
}
|
|
|
|
#endif
|
2013-12-04 21:33:17 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* fstostr - prettyprint NTP seconds
|
|
|
|
*/
|
|
|
|
char * fstostr(
|
|
|
|
time_t ntp_stamp
|
|
|
|
)
|
|
|
|
{
|
2014-12-20 22:52:39 +00:00
|
|
|
char * buf;
|
|
|
|
struct calendar tm;
|
|
|
|
|
|
|
|
LIB_GETBUF(buf);
|
|
|
|
if (ntpcal_ntp_to_date(&tm, (u_int32)ntp_stamp, NULL) < 0)
|
|
|
|
snprintf(buf, LIB_BUFLENGTH, "ntpcal_ntp_to_date: %ld: range error",
|
|
|
|
(long)ntp_stamp);
|
2013-12-04 21:33:17 +00:00
|
|
|
else
|
2014-12-20 22:52:39 +00:00
|
|
|
snprintf(buf, LIB_BUFLENGTH, "%04d%02d%02d%02d%02d",
|
|
|
|
tm.year, tm.month, tm.monthday,
|
|
|
|
tm.hour, tm.minute);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
2013-12-04 21:33:17 +00:00
|
|
|
|
2014-12-20 22:52:39 +00:00
|
|
|
/*
|
|
|
|
* 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
|
2013-12-04 21:33:17 +00:00
|
|
|
}
|