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 "ntp_unixtime.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 */
55 char *sprintb P((u_int, const char *));
56 const char *timex_state P((int));
57 volatile int debug = 0;
60 void pll_trap P((int));
62 static struct sigaction newsigsys; /* new sigaction status */
63 static struct sigaction sigsys; /* current sigaction status */
64 static sigjmp_buf env; /* environment var. for pll_trap() */
67 static volatile int pll_control; /* (0) daemon, (1) kernel loop */
68 static volatile int status; /* most recent status bits */
69 static volatile int flash; /* most recent ntp_adjtime() bits */
71 static char optargs[] = "MNT:cde:f:hm:o:rs:t:";
79 extern int ntp_optind;
80 extern char *ntp_optarg;
84 struct ntptimeval ntv;
87 struct timex ntx, _ntx;
89 double ftemp, gtemp, htemp;
90 long time_frac; /* ntv.time.tv_frac_sec (us/ns) */
92 unsigned ts_mask = TS_MASK; /* defaults to 20 bits (us) */
93 unsigned ts_roundbit = TS_ROUNDBIT; /* defaults to 20 bits (us) */
94 int fdigits = 6; /* fractional digits for us */
100 memset((char *)&ntx, 0, sizeof(ntx));
102 while ((c = ntp_getopt(argc, argv, optargs)) != EOF) switch (c) {
105 ntx.modes |= MOD_MICRO;
110 ntx.modes |= MOD_NANO;
117 ntx.constant = atoi(ntp_optarg);
128 ntx.modes |= MOD_ESTERROR;
129 ntx.esterror = atoi(ntp_optarg);
132 ntx.modes |= MOD_FREQUENCY;
133 ntx.freq = (long)(atof(ntp_optarg) * SCALE_FREQ);
136 ntx.modes |= MOD_MAXERROR;
137 ntx.maxerror = atoi(ntp_optarg);
140 ntx.modes |= MOD_OFFSET;
141 ntx.offset = atoi(ntp_optarg);
147 ntx.modes |= MOD_STATUS;
148 ntx.status = atoi(ntp_optarg);
149 if (ntx.status < 0 || ntx.status >= 0x100) errflg++;
152 ntx.modes |= MOD_TIMECONST;
153 ntx.constant = atoi(ntp_optarg);
158 if (errflg || (ntp_optind != argc)) {
159 (void) fprintf(stderr,
160 "usage: %s [-%s]\n\n\
162 -c display the time taken to call ntp_gettime (us)\n\
163 -e esterror estimate of the error (us)\n\
164 -f frequency Frequency error (-500 .. 500) (ppm)\n\
165 -h display this help info\n\
166 -m maxerror max possible error (us)\n\
167 -o offset current offset (ms)\n\
168 -r print the unix and NTP time raw\n\
169 -l leap Set the leap bits\n\
170 -t timeconstant log2 of PLL time constant (0 .. %d)\n",
173 "-M switch to microsecond mode\n",
178 "-N switch to nanosecond mode\n",
184 "-T tai_offset set TAI offset\n",
197 * Test to make sure the sigaction() works in case of invalid
200 newsigsys.sa_handler = pll_trap;
201 newsigsys.sa_flags = 0;
202 if (sigaction(SIGSYS, &newsigsys, &sigsys)) {
203 perror("sigaction() fails to save SIGSYS trap");
210 * Make sure the trapcatcher works.
214 if (sigsetjmp(env, 1) == 0)
217 status = syscall(BADCALL, &ntv); /* dummy parameter */
218 if ((status < 0) && (errno == ENOSYS))
224 printf("sigaction() failed to catch an invalid syscall\n");
229 if (sigsetjmp(env, 1) == 0) {
231 for (c = 0; c < sizeof times / sizeof times[0]; c++) {
232 status = ntp_gettime(&ntv);
233 if ((status < 0) && (errno == ENOSYS))
237 times[c] = ntv.time.tv_frac_sec;
242 if (pll_control >= 0) {
243 printf("[ us %06d:", times[0]);
244 for (c = 1; c < sizeof times / sizeof times[0]; c++)
245 printf(" %d", times[c] - times[c - 1]);
250 if (sigsetjmp(env, 1) == 0) {
252 status = ntp_gettime(&ntv);
253 if ((status < 0) && (errno == ENOSYS))
258 _ntx.modes = 0; /* Ensure nothing is set */
260 if (sigsetjmp(env, 1) == 0) {
262 status = ntp_adjtime(&_ntx);
263 if ((status < 0) && (errno == ENOSYS))
269 if (pll_control < 0) {
270 printf("NTP user interface routines are not configured in this kernel.\n");
275 * Fetch timekeeping data and display.
277 status = ntp_gettime(&ntv);
279 perror("ntp_gettime() call fails");
281 printf("ntp_gettime() returns code %d (%s)\n",
282 status, timex_state(status));
283 time_frac = ntv.time.tv_frac_sec;
285 if (flash & STA_NANO) {
286 ntv.time.tv_frac_sec /= 1000;
287 ts_mask = 0xfffffffc; /* 1/2^30 */
288 ts_roundbit = 0x00000002;
292 tv.tv_sec = ntv.time.tv_sec;
293 tv.tv_usec = ntv.time.tv_frac_sec;
296 ts.l_uf += ts_roundbit;
298 printf(" time %s, (.%0*d),\n",
299 prettydate(&ts), fdigits, (int) time_frac);
300 printf(" maximum error %lu us, estimated error %lu us",
301 (u_long)ntv.maxerror, (u_long)ntv.esterror);
303 printf(" ntptime=%x.%x unixtime=%x.%0*d %s",
304 (unsigned int) ts.l_ui, (unsigned int) ts.l_uf,
305 (int) ntv.time.tv_sec, fdigits, (int) time_frac,
306 ctime((const time_t *) &ntv.time.tv_sec));
308 printf(", TAI offset %d\n", ntv.tai);
313 status = ntp_adjtime(&ntx);
315 perror((errno == EPERM) ?
316 "Must be root to set kernel values\nntp_adjtime() call fails" :
317 "ntp_adjtime() call fails");
320 printf("ntp_adjtime() returns code %d (%s)\n",
321 status, timex_state(status));
322 printf(" modes %s,\n", sprintb(ntx.modes, TIMEX_MOD_BITS));
323 ftemp = (double)ntx.offset;
325 if (flash & STA_NANO)
328 printf(" offset %.3f", ftemp);
329 ftemp = (double)ntx.freq / SCALE_FREQ;
330 printf(" us, frequency %.3f ppm, interval %d s,\n",
331 ftemp, 1 << ntx.shift);
332 printf(" maximum error %lu us, estimated error %lu us,\n",
333 (u_long)ntx.maxerror, (u_long)ntx.esterror);
334 printf(" status %s,\n", sprintb((u_int)ntx.status, TIMEX_STA_BITS));
335 ftemp = (double)ntx.tolerance / SCALE_FREQ;
336 gtemp = (double)ntx.precision;
338 if (flash & STA_NANO)
342 " time constant %lu, precision %.3f us, tolerance %.0f ppm,\n",
343 (u_long)ntx.constant, gtemp, ftemp);
346 ftemp = (double)ntx.ppsfreq / SCALE_FREQ;
347 gtemp = (double)ntx.stabil / SCALE_FREQ;
348 htemp = (double)ntx.jitter;
350 if (flash & STA_NANO)
354 " pps frequency %.3f ppm, stability %.3f ppm, jitter %.3f us,\n",
355 ftemp, gtemp, htemp);
356 printf(" intervals %lu, jitter exceeded %lu, stability exceeded %lu, errors %lu.\n",
357 (u_long)ntx.calcnt, (u_long)ntx.jitcnt,
358 (u_long)ntx.stbcnt, (u_long)ntx.errcnt);
363 * Put things back together the way we found them.
367 if (sigaction(SIGSYS, &sigsys, (struct sigaction *)NULL)) {
368 perror("sigaction() fails to restore SIGSYS trap");
377 * pll_trap - trap processor for undefined syscalls
390 * Print a value a la the %b format of the kernel's printf
395 register const char *bits
399 register int i, any = 0;
401 static char buf[132];
403 if (bits && *bits == 8)
404 (void)sprintf(buf, "0%o", v);
406 (void)sprintf(buf, "0x%x", v);
407 cp = buf + strlen(buf);
412 while ((i = *bits++) != 0) {
413 if (v & (1 << (i-1))) {
417 for (; (c = *bits) > 32; bits++)
420 for (; *bits > 32; bits++)
429 const char *timex_states[] = {
430 "OK", "INS", "DEL", "OOP", "WAIT", "ERROR"
440 if (s >= 0 && s <= sizeof(timex_states) / sizeof(timex_states[0]))
441 return (timex_states[s]);
442 sprintf(buf, "TIME-#%d", s);