4 * This program tests to see if the NTP user interface routines
5 * ntp_gettime() and ntp_adjtime() have been implemented in the kernel.
6 * If so, each of these routines is called to display current timekeeping
9 * For more information, see the README.kern file in the doc directory
10 * of the xntp3 distribution.
15 #endif /* HAVE_CONFIG_H */
18 #include "timevalops.h"
19 #include "ntp_syscall.h"
20 #include "ntp_stdlib.h"
27 #ifdef NTP_SYSCALLS_STD
29 # define BADCALL -1 /* this is supposed to be a bad syscall */
30 # endif /* SYS_DECOSF1 */
33 #ifdef HAVE_STRUCT_NTPTIMEVAL_TIME_TV_NSEC
34 #define tv_frac_sec tv_nsec
36 #define tv_frac_sec tv_usec
40 #define TIMEX_MOD_BITS \
41 "\20\1OFFSET\2FREQUENCY\3MAXERROR\4ESTERROR\5STATUS\6TIMECONST\
42 \13PLL\14FLL\15MICRO\16NANO\17CLKB\20CLKA"
44 #define TIMEX_STA_BITS \
45 "\20\1PLL\2PPSFREQ\3PPSTIME\4FLL\5INS\6DEL\7UNSYNC\10FREQHOLD\
46 \11PPSSIGNAL\12PPSJITTER\13PPSWANDER\14PPSERROR\15CLOCKERR\
49 #define SCALE_FREQ 65536 /* frequency scale */
52 * These constants are used to round the time stamps computed from
53 * a struct timeval to the microsecond (more or less). This keeps
56 #define TS_MASK 0xfffff000 /* mask to usec, for time stamps */
57 #define TS_ROUNDBIT 0x00000800 /* round at this bit */
62 const char * sprintb (u_int, const char *);
63 const char * timex_state (int);
68 static struct sigaction newsigsys; /* new sigaction status */
69 static struct sigaction sigsys; /* current sigaction status */
70 static sigjmp_buf env; /* environment var. for pll_trap() */
73 static volatile int pll_control; /* (0) daemon, (1) kernel loop */
74 static volatile int status; /* most recent status bits */
75 static volatile int flash; /* most recent ntp_adjtime() bits */
76 char const * progname;
77 static char optargs[] = "MNT:cde:f:hm:o:rs:t:";
85 extern int ntp_optind;
86 extern char *ntp_optarg;
90 struct ntptimeval ntv;
93 struct timex ntx, _ntx;
94 int times[20] = { 0 };
95 double ftemp, gtemp, htemp;
96 long time_frac; /* ntv.time.tv_frac_sec (us/ns) */
98 volatile unsigned ts_mask = TS_MASK; /* defaults to 20 bits (us) */
99 volatile unsigned ts_roundbit = TS_ROUNDBIT; /* defaults to 20 bits (us) */
100 volatile int fdigits = 6; /* fractional digits for us */
105 volatile int rawtime = 0;
109 while ((ch = ntp_getopt(argc, argv, optargs)) != EOF) {
113 ntx.modes |= MOD_MICRO;
118 ntx.modes |= MOD_NANO;
125 ntx.constant = atoi(ntp_optarg);
134 ntx.modes |= MOD_ESTERROR;
135 ntx.esterror = atoi(ntp_optarg);
139 ntx.modes |= MOD_FREQUENCY;
140 ntx.freq = (long)(atof(ntp_optarg) * SCALE_FREQ);
144 ntx.modes |= MOD_MAXERROR;
145 ntx.maxerror = atoi(ntp_optarg);
149 ntx.modes |= MOD_OFFSET;
150 ntx.offset = atoi(ntp_optarg);
158 ntx.modes |= MOD_STATUS;
159 ntx.status = atoi(ntp_optarg);
160 if (ntx.status < 0 || ntx.status >= 0x100)
165 ntx.modes |= MOD_TIMECONST;
166 ntx.constant = atoi(ntp_optarg);
173 if (errflg || (ntp_optind != argc)) {
175 "usage: %s [-%s]\n\n\
177 -c display the time taken to call ntp_gettime (us)\n\
178 -e esterror estimate of the error (us)\n\
179 -f frequency Frequency error (-500 .. 500) (ppm)\n\
180 -h display this help info\n\
181 -m maxerror max possible error (us)\n\
182 -o offset current offset (ms)\n\
183 -r print the unix and NTP time raw\n\
184 -s status Set the status bits\n\
185 -t timeconstant log2 of PLL time constant (0 .. %d)\n",
188 "-M switch to microsecond mode\n",
193 "-N switch to nanosecond mode\n",
199 "-T tai_offset set TAI offset\n",
212 * Test to make sure the sigaction() works in case of invalid
215 newsigsys.sa_handler = pll_trap;
216 newsigsys.sa_flags = 0;
217 if (sigaction(SIGSYS, &newsigsys, &sigsys)) {
218 perror("sigaction() fails to save SIGSYS trap");
225 * Make sure the trapcatcher works.
229 if (sigsetjmp(env, 1) == 0) {
231 status = syscall(BADCALL, &ntv); /* dummy parameter */
232 if ((status < 0) && (errno == ENOSYS))
238 printf("sigaction() failed to catch an invalid syscall\n");
243 if (sigsetjmp(env, 1) == 0) {
245 for (c = 0; c < COUNTOF(times); c++) {
246 status = ntp_gettime(&ntv);
247 if ((status < 0) && (errno == ENOSYS))
251 times[c] = ntv.time.tv_frac_sec;
256 if (pll_control >= 0) {
257 printf("[ us %06d:", times[0]);
258 for (c = 1; c < COUNTOF(times); c++)
259 printf(" %d", times[c] - times[c - 1]);
264 if (sigsetjmp(env, 1) == 0) {
266 status = ntp_gettime(&ntv);
267 if ((status < 0) && (errno == ENOSYS))
272 _ntx.modes = 0; /* Ensure nothing is set */
274 if (sigsetjmp(env, 1) == 0) {
276 status = ntp_adjtime(&_ntx);
277 if ((status < 0) && (errno == ENOSYS))
283 if (pll_control < 0) {
284 printf("NTP user interface routines are not configured in this kernel.\n");
289 * Fetch timekeeping data and display.
291 status = ntp_gettime(&ntv);
293 perror("ntp_gettime() call fails");
295 printf("ntp_gettime() returns code %d (%s)\n",
296 status, timex_state(status));
297 time_frac = ntv.time.tv_frac_sec;
299 if (flash & STA_NANO) {
300 ntv.time.tv_frac_sec /= 1000;
301 ts_mask = 0xfffffffc; /* 1/2^30 */
302 ts_roundbit = 0x00000002;
306 tv.tv_sec = ntv.time.tv_sec;
307 tv.tv_usec = ntv.time.tv_frac_sec;
310 ts.l_uf += ts_roundbit;
312 printf(" time %s, (.%0*d),\n",
313 prettydate(&ts), fdigits, (int)time_frac);
314 printf(" maximum error %lu us, estimated error %lu us",
315 (u_long)ntv.maxerror, (u_long)ntv.esterror);
317 printf(" ntptime=%x.%x unixtime=%x.%0*d %s",
318 (u_int)ts.l_ui, (u_int)ts.l_uf,
319 (int)ntv.time.tv_sec, fdigits,
321 ctime((time_t *)&ntv.time.tv_sec));
323 printf(", TAI offset %ld\n", (long)ntv.tai);
328 status = ntp_adjtime(&ntx);
330 perror((errno == EPERM) ?
331 "Must be root to set kernel values\nntp_adjtime() call fails" :
332 "ntp_adjtime() call fails");
335 printf("ntp_adjtime() returns code %d (%s)\n",
336 status, timex_state(status));
337 printf(" modes %s,\n", sprintb(ntx.modes, TIMEX_MOD_BITS));
338 ftemp = (double)ntx.offset;
340 if (flash & STA_NANO)
343 printf(" offset %.3f", ftemp);
344 ftemp = (double)ntx.freq / SCALE_FREQ;
345 printf(" us, frequency %.3f ppm, interval %d s,\n",
346 ftemp, 1 << ntx.shift);
347 printf(" maximum error %lu us, estimated error %lu us,\n",
348 (u_long)ntx.maxerror, (u_long)ntx.esterror);
349 printf(" status %s,\n", sprintb((u_int)ntx.status, TIMEX_STA_BITS));
350 ftemp = (double)ntx.tolerance / SCALE_FREQ;
351 gtemp = (double)ntx.precision;
353 if (flash & STA_NANO)
357 " time constant %lu, precision %.3f us, tolerance %.0f ppm,\n",
358 (u_long)ntx.constant, gtemp, ftemp);
361 ftemp = (double)ntx.ppsfreq / SCALE_FREQ;
362 gtemp = (double)ntx.stabil / SCALE_FREQ;
363 htemp = (double)ntx.jitter;
365 if (flash & STA_NANO)
369 " pps frequency %.3f ppm, stability %.3f ppm, jitter %.3f us,\n",
370 ftemp, gtemp, htemp);
371 printf(" intervals %lu, jitter exceeded %lu, stability exceeded %lu, errors %lu.\n",
372 (u_long)ntx.calcnt, (u_long)ntx.jitcnt,
373 (u_long)ntx.stbcnt, (u_long)ntx.errcnt);
378 * Put things back together the way we found them.
382 if (sigaction(SIGSYS, &sigsys, (struct sigaction *)NULL)) {
383 perror("sigaction() fails to restore SIGSYS trap");
392 * pll_trap - trap processor for undefined syscalls
405 * Print a value a la the %b format of the kernel's printf
418 static char buf[132];
420 if (bits != NULL && *bits == 8)
421 snprintf(buf, sizeof(buf), "0%o", v);
423 snprintf(buf, sizeof(buf), "0x%x", v);
424 cp = buf + strlen(buf);
425 cplim = buf + sizeof(buf);
431 while ((i = *bits++) != 0) {
432 if (v & (1 << (i - 1))) {
439 for (; (c = *bits) > 32; bits++) {
445 for (; *bits > 32; bits++)
457 return "sprintb buffer too small";
460 const char * const timex_states[] = {
461 "OK", "INS", "DEL", "OOP", "WAIT", "ERROR"
471 if ((size_t)s < COUNTOF(timex_states))
472 return timex_states[s];
473 snprintf(buf, sizeof(buf), "TIME-#%d", s);