/*
* NTP test program
*
* This program tests to see if the NTP user interface routines
* ntp_gettime() and ntp_adjtime() have been implemented in the kernel.
* If so, each of these routines is called to display current timekeeping
* data.
*
* For more information, see the README.kern file in the doc directory
* of the xntp3 distribution.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */
/*
* These constants are used to round the time stamps computed from
* a struct timeval to the microsecond (more or less). This keeps
* things neat.
*/
#define TS_MASK_US 0xfffff000 /* mask to usec, for time stamps */
#define TS_ROUNDBIT_US 0x00000800 /* round at this bit */
#define TS_DIGITS_US 6
static struct sigaction newsigsys; /* new sigaction status */
static struct sigaction sigsys; /* current sigaction status */
static sigjmp_buf env; /* environment var. for pll_trap() */
#endif
static volatile int pll_control; /* (0) daemon, (1) kernel loop */
static volatile int status; /* most recent status bits */
static volatile int flash; /* most recent ntp_adjtime() bits */
char const * progname;
static char optargs[] = "MNT:cde:f:hm:o:rs:t:";
int
main(
int argc,
char *argv[]
)
{
extern int ntp_optind;
extern char *ntp_optarg;
#ifdef SUBST_ADJTIMEX
struct timex ntv;
#else
struct ntptimeval ntv;
#endif
struct timeval tv;
struct timex ntx, _ntx;
int times[20] = { 0 };
double ftemp, gtemp, htemp;
volatile double nscale = 1.0; /* assume usec scale for now */
long time_frac; /* ntv.time.tv_frac_sec (us/ns) */
l_fp ts;
volatile unsigned ts_mask = TS_MASK_US; /* defaults to 20 bits (us) */
volatile unsigned ts_roundbit = TS_ROUNDBIT_US; /* defaults to 20 bits (us) */
volatile int fdigits = TS_DIGITS_US; /* fractional digits for us */
size_t c;
int ch;
int errflg = 0;
int cost = 0;
volatile int rawtime = 0;
case 'm':
ntx.modes |= MOD_MAXERROR;
ntx.maxerror = atoi(ntp_optarg);
break;
case 'o':
ntx.modes |= MOD_OFFSET;
ntx.offset = atoi(ntp_optarg);
break;
case 'r':
rawtime++;
break;
case 's':
ntx.modes |= MOD_STATUS;
ntx.status = atoi(ntp_optarg);
if (ntx.status < 0 || ntx.status >= 0x100)
errflg++;
break;
case 't':
ntx.modes |= MOD_TIMECONST;
ntx.constant = atoi(ntp_optarg);
break;
default:
errflg++;
}
}
if (errflg || (ntp_optind != argc)) {
fprintf(stderr,
"usage: %s [-%s]\n\n\
%s%s%s\
-c display the time taken to call ntp_gettime (us)\n\
-e esterror estimate of the error (us)\n\
-f frequency Frequency error (-500 .. 500) (ppm)\n\
-h display this help info\n\
-m maxerror max possible error (us)\n\
-o offset current offset (ms)\n\
-r print the unix and NTP time raw\n\
-s status Set the status bits\n\
-t timeconstant log2 of PLL time constant (0 .. %d)\n",
progname, optargs,
#ifdef MOD_MICRO
"-M switch to microsecond mode\n",
#else
"",
#endif
#ifdef MOD_NANO
"-N switch to nanosecond mode\n",
#else
"",
#endif
#ifdef NTP_API
# if NTP_API > 3
"-T tai_offset set TAI offset\n",
# else
"",
# endif
#else
"",
#endif
MAXTC);
exit(2);
}
#ifdef SIGSYS
/*
* Test to make sure the sigaction() works in case of invalid
* syscall codes.
*/
newsigsys.sa_handler = pll_trap;
newsigsys.sa_flags = 0;
if (sigaction(SIGSYS, &newsigsys, &sigsys)) {
perror("sigaction() fails to save SIGSYS trap");
exit(1);
}
#endif /* SIGSYS */
#ifdef BADCALL
/*
* Make sure the trapcatcher works.
*/
pll_control = 1;
#ifdef SIGSYS
if (sigsetjmp(env, 1) == 0)
#endif
{
status = syscall(BADCALL, &ntv); /* dummy parameter */
if ((status < 0) && (errno == ENOSYS))
--pll_control;
}
if (pll_control)
printf("sigaction() failed to catch an invalid syscall\n");
#endif /* BADCALL */
if (cost) {
#ifdef SIGSYS
if (sigsetjmp(env, 1) == 0)
#endif
{
for (c = 0; c < COUNTOF(times); c++) {
status = ntp_gettime(&ntv);
if ((status < 0) && (errno == ENOSYS))
--pll_control;
if (pll_control < 0)
break;
times[c] = ntv.time.tv_frac_sec;
}
}
if (pll_control >= 0) {
printf("[ us %06d:", times[0]);
for (c = 1; c < COUNTOF(times); c++)
printf(" %d", times[c] - times[c - 1]);
printf(" ]\n");
}
}
#ifdef SIGSYS
if (sigsetjmp(env, 1) == 0)
#endif
{
status = ntp_gettime(&ntv);
if ((status < 0) && (errno == ENOSYS))
--pll_control;
}
_ntx.modes = 0; /* Ensure nothing is set */
#ifdef SIGSYS
if (sigsetjmp(env, 1) == 0)
#endif
{
status = ntp_adjtime(&_ntx);
if ((status < 0) && (errno == ENOSYS))
--pll_control;
flash = _ntx.status;
}
if (pll_control < 0) {
printf("NTP user interface routines are not configured in this kernel.\n");
goto lexit;
}
/*
* Put things back together the way we found them.
*/
lexit:
#ifdef SIGSYS
if (sigaction(SIGSYS, &sigsys, (struct sigaction *)NULL)) {
perror("sigaction() fails to restore SIGSYS trap");
exit(1);
}
#endif
exit(0);
}
/*
* Print a value a la the %b format of the kernel's printf
*/
const char *
sprintb(
u_int v,
const char * bits
)
{
char *cp;
char *cplim;
int i;
int any;
char c;
static char buf[132];
if (bits != NULL && *bits == 8)
snprintf(buf, sizeof(buf), "0%o", v);
else
snprintf(buf, sizeof(buf), "0x%x", v);
cp = buf + strlen(buf);
cplim = buf + sizeof(buf);
if (bits != NULL) {
bits++;
*cp++ = ' ';
*cp++ = '(';
any = FALSE;
while ((i = *bits++) != 0) {
if (v & (1 << (i - 1))) {
if (any) {
*cp++ = ',';
if (cp >= cplim)
goto overrun;
}
any = TRUE;
for (; (c = *bits) > 32; bits++) {
*cp++ = c;
if (cp >= cplim)
goto overrun;
}
} else {
for (; *bits > 32; bits++)
continue;
}
}
*cp++ = ')';
if (cp >= cplim)
goto overrun;
}
*cp = '\0';
return buf;