1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
|
/*
* 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 */
#include "ntp_fp.h"
#include "timevalops.h"
#include "ntp_syscall.h"
#include "ntp_stdlib.h"
#include <stdio.h>
#include <ctype.h>
#include <signal.h>
#include <setjmp.h>
#ifdef NTP_SYSCALLS_STD
# ifndef SYS_DECOSF1
# define BADCALL -1 /* this is supposed to be a bad syscall */
# endif /* SYS_DECOSF1 */
#endif
#ifdef HAVE_STRUCT_NTPTIMEVAL_TIME_TV_NSEC
#define tv_frac_sec tv_nsec
#else
#define tv_frac_sec tv_usec
#endif
#define TIMEX_MOD_BITS \
"\20\1OFFSET\2FREQUENCY\3MAXERROR\4ESTERROR\5STATUS\6TIMECONST\
\13PLL\14FLL\15MICRO\16NANO\17CLKB\20CLKA"
#define TIMEX_STA_BITS \
"\20\1PLL\2PPSFREQ\3PPSTIME\4FLL\5INS\6DEL\7UNSYNC\10FREQHOLD\
\11PPSSIGNAL\12PPSJITTER\13PPSWANDER\14PPSERROR\15CLOCKERR\
\16NANO\17MODE\20CLK"
#define SCALE_FREQ 65536 /* frequency scale */
/*
* 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
#define TS_MASK_NS 0xfffffffc /* 1/2^30, for nsec */
#define TS_ROUNDBIT_NS 0x00000002
#define TS_DIGITS_NS 9
/*
* Function prototypes
*/
const char * sprintb (u_int, const char *);
const char * timex_state (int);
#ifdef SIGSYS
void pll_trap (int);
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;
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;
ZERO(ntx);
progname = argv[0];
while ((ch = ntp_getopt(argc, argv, optargs)) != EOF) {
switch (ch) {
#ifdef MOD_MICRO
case 'M':
ntx.modes |= MOD_MICRO;
break;
#endif
#ifdef MOD_NANO
case 'N':
ntx.modes |= MOD_NANO;
break;
#endif
#if defined(NTP_API) && NTP_API > 3
case 'T':
ntx.modes = MOD_TAI;
ntx.constant = atoi(ntp_optarg);
break;
#endif
case 'c':
cost++;
break;
case 'e':
ntx.modes |= MOD_ESTERROR;
ntx.esterror = atoi(ntp_optarg);
break;
case 'f':
ntx.modes |= MOD_FREQUENCY;
ntx.freq = (long)(atof(ntp_optarg) * SCALE_FREQ);
break;
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;
}
/*
* Fetch timekeeping data and display.
*/
status = ntp_gettime(&ntv);
if (status < 0) {
perror("ntp_gettime() call fails");
} else {
printf("ntp_gettime() returns code %d (%s)\n",
status, timex_state(status));
time_frac = ntv.time.tv_frac_sec;
#ifdef STA_NANO
if (flash & STA_NANO) {
ntv.time.tv_frac_sec /= 1000;
ts_mask = TS_MASK_NS;
ts_roundbit = TS_ROUNDBIT_NS;
fdigits = TS_DIGITS_NS;
}
#endif
tv.tv_sec = ntv.time.tv_sec;
tv.tv_usec = ntv.time.tv_frac_sec;
TVTOTS(&tv, &ts);
ts.l_ui += JAN_1970;
ts.l_uf += ts_roundbit;
ts.l_uf &= ts_mask;
printf(" time %s, (.%0*d),\n",
prettydate(&ts), fdigits, (int)time_frac);
printf(" maximum error %ld us, estimated error %ld us",
ntv.maxerror, ntv.esterror);
if (rawtime)
printf(" ntptime=%x.%x unixtime=%x.%0*d %s",
(u_int)ts.l_ui, (u_int)ts.l_uf,
(int)ntv.time.tv_sec, fdigits,
(int)time_frac,
ctime((time_t *)&ntv.time.tv_sec));
#if defined(NTP_API) && NTP_API > 3
printf(", TAI offset %ld\n", (long)ntv.tai);
#else
printf("\n");
#endif /* NTP_API */
}
status = ntp_adjtime(&ntx);
if (status < 0) {
perror((errno == EPERM) ?
"Must be root to set kernel values\nntp_adjtime() call fails" :
"ntp_adjtime() call fails");
} else {
flash = ntx.status;
printf("ntp_adjtime() returns code %d (%s)\n",
status, timex_state(status));
printf(" modes %s,\n", sprintb(ntx.modes, TIMEX_MOD_BITS));
#ifdef STA_NANO
if (flash & STA_NANO)
nscale = 1e-3;
#endif
ftemp = (double)ntx.offset * nscale;
printf(" offset %.3f", ftemp);
ftemp = (double)ntx.freq / SCALE_FREQ;
printf(" us, frequency %.3f ppm, interval %d s,\n",
ftemp, 1 << ntx.shift);
printf(" maximum error %ld us, estimated error %ld us,\n",
ntx.maxerror, ntx.esterror);
printf(" status %s,\n", sprintb((u_int)ntx.status, TIMEX_STA_BITS));
ftemp = (double)ntx.tolerance / SCALE_FREQ;
gtemp = (double)ntx.precision * nscale;
printf(
" time constant %lu, precision %.3f us, tolerance %.0f ppm,\n",
(u_long)ntx.constant, gtemp, ftemp);
if (ntx.shift == 0)
exit(0);
ftemp = (double)ntx.ppsfreq / SCALE_FREQ;
gtemp = (double)ntx.stabil / SCALE_FREQ;
htemp = (double)ntx.jitter * nscale;
printf(" pps frequency %.3f ppm, stability %.3f ppm, jitter %.3f us,\n",
ftemp, gtemp, htemp);
printf(" intervals %lu, jitter exceeded %lu, stability exceeded %lu, errors %lu.\n",
(u_long)ntx.calcnt, (u_long)ntx.jitcnt,
(u_long)ntx.stbcnt, (u_long)ntx.errcnt);
return 0;
}
/*
* 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);
}
#ifdef SIGSYS
/*
* pll_trap - trap processor for undefined syscalls
*/
void
pll_trap(
int arg
)
{
pll_control--;
siglongjmp(env, 1);
}
#endif
/*
* 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;
overrun:
return "sprintb buffer too small";
}
const char * const timex_states[] = {
"OK", "INS", "DEL", "OOP", "WAIT", "ERROR"
};
const char *
timex_state(
int s
)
{
static char buf[32];
if ((size_t)s < COUNTOF(timex_states))
return timex_states[s];
snprintf(buf, sizeof(buf), "TIME-#%d", s);
return buf;
}
|