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_US 0xfffff000 /* mask to usec, for time stamps */
57 #define TS_ROUNDBIT_US 0x00000800 /* round at this bit */
58 #define TS_DIGITS_US 6
60 #define TS_MASK_NS 0xfffffffc /* 1/2^30, for nsec */
61 #define TS_ROUNDBIT_NS 0x00000002
62 #define TS_DIGITS_NS 9
67 const char * sprintb (u_int, const char *);
68 const char * timex_state (int);
73 static struct sigaction newsigsys; /* new sigaction status */
74 static struct sigaction sigsys; /* current sigaction status */
75 static sigjmp_buf env; /* environment var. for pll_trap() */
78 static volatile int pll_control; /* (0) daemon, (1) kernel loop */
79 static volatile int status; /* most recent status bits */
80 static volatile int flash; /* most recent ntp_adjtime() bits */
81 char const * progname;
82 static char optargs[] = "MNT:cde:f:hm:o:rs:t:";
90 extern int ntp_optind;
91 extern char *ntp_optarg;
95 struct ntptimeval ntv;
98 struct timex ntx, _ntx;
99 int times[20] = { 0 };
100 double ftemp, gtemp, htemp;
101 double nscale = 1.0; /* assume usec scale for now */
102 long time_frac; /* ntv.time.tv_frac_sec (us/ns) */
104 volatile unsigned ts_mask = TS_MASK_US; /* defaults to 20 bits (us) */
105 volatile unsigned ts_roundbit = TS_ROUNDBIT_US; /* defaults to 20 bits (us) */
106 volatile int fdigits = TS_DIGITS_US; /* fractional digits for us */
111 volatile int rawtime = 0;
115 while ((ch = ntp_getopt(argc, argv, optargs)) != EOF) {
119 ntx.modes |= MOD_MICRO;
124 ntx.modes |= MOD_NANO;
127 #if defined(NTP_API) && NTP_API > 3
130 ntx.constant = atoi(ntp_optarg);
138 ntx.modes |= MOD_ESTERROR;
139 ntx.esterror = atoi(ntp_optarg);
143 ntx.modes |= MOD_FREQUENCY;
144 ntx.freq = (long)(atof(ntp_optarg) * SCALE_FREQ);
148 ntx.modes |= MOD_MAXERROR;
149 ntx.maxerror = atoi(ntp_optarg);
153 ntx.modes |= MOD_OFFSET;
154 ntx.offset = atoi(ntp_optarg);
162 ntx.modes |= MOD_STATUS;
163 ntx.status = atoi(ntp_optarg);
164 if (ntx.status < 0 || ntx.status >= 0x100)
169 ntx.modes |= MOD_TIMECONST;
170 ntx.constant = atoi(ntp_optarg);
177 if (errflg || (ntp_optind != argc)) {
179 "usage: %s [-%s]\n\n\
181 -c display the time taken to call ntp_gettime (us)\n\
182 -e esterror estimate of the error (us)\n\
183 -f frequency Frequency error (-500 .. 500) (ppm)\n\
184 -h display this help info\n\
185 -m maxerror max possible error (us)\n\
186 -o offset current offset (ms)\n\
187 -r print the unix and NTP time raw\n\
188 -s status Set the status bits\n\
189 -t timeconstant log2 of PLL time constant (0 .. %d)\n",
192 "-M switch to microsecond mode\n",
197 "-N switch to nanosecond mode\n",
203 "-T tai_offset set TAI offset\n",
216 * Test to make sure the sigaction() works in case of invalid
219 newsigsys.sa_handler = pll_trap;
220 newsigsys.sa_flags = 0;
221 if (sigaction(SIGSYS, &newsigsys, &sigsys)) {
222 perror("sigaction() fails to save SIGSYS trap");
229 * Make sure the trapcatcher works.
233 if (sigsetjmp(env, 1) == 0)
236 status = syscall(BADCALL, &ntv); /* dummy parameter */
237 if ((status < 0) && (errno == ENOSYS))
241 printf("sigaction() failed to catch an invalid syscall\n");
246 if (sigsetjmp(env, 1) == 0)
249 for (c = 0; c < COUNTOF(times); c++) {
250 status = ntp_gettime(&ntv);
251 if ((status < 0) && (errno == ENOSYS))
255 times[c] = ntv.time.tv_frac_sec;
258 if (pll_control >= 0) {
259 printf("[ us %06d:", times[0]);
260 for (c = 1; c < COUNTOF(times); c++)
261 printf(" %d", times[c] - times[c - 1]);
266 if (sigsetjmp(env, 1) == 0)
269 status = ntp_gettime(&ntv);
270 if ((status < 0) && (errno == ENOSYS))
273 _ntx.modes = 0; /* Ensure nothing is set */
275 if (sigsetjmp(env, 1) == 0)
278 status = ntp_adjtime(&_ntx);
279 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 = TS_MASK_NS;
302 ts_roundbit = TS_ROUNDBIT_NS;
303 fdigits = TS_DIGITS_NS;
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 %ld us, estimated error %ld us",
315 ntv.maxerror, 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));
322 #if defined(NTP_API) && NTP_API > 3
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));
339 if (flash & STA_NANO)
342 ftemp = (double)ntx.offset * nscale;
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 %ld us, estimated error %ld us,\n",
348 ntx.maxerror, 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 * nscale;
353 " time constant %lu, precision %.3f us, tolerance %.0f ppm,\n",
354 (u_long)ntx.constant, gtemp, ftemp);
357 ftemp = (double)ntx.ppsfreq / SCALE_FREQ;
358 gtemp = (double)ntx.stabil / SCALE_FREQ;
359 htemp = (double)ntx.jitter * nscale;
360 printf(" pps frequency %.3f ppm, stability %.3f ppm, jitter %.3f us,\n",
361 ftemp, gtemp, htemp);
362 printf(" intervals %lu, jitter exceeded %lu, stability exceeded %lu, errors %lu.\n",
363 (u_long)ntx.calcnt, (u_long)ntx.jitcnt,
364 (u_long)ntx.stbcnt, (u_long)ntx.errcnt);
369 * Put things back together the way we found them.
373 if (sigaction(SIGSYS, &sigsys, (struct sigaction *)NULL)) {
374 perror("sigaction() fails to restore SIGSYS trap");
383 * pll_trap - trap processor for undefined syscalls
396 * Print a value a la the %b format of the kernel's printf
409 static char buf[132];
411 if (bits != NULL && *bits == 8)
412 snprintf(buf, sizeof(buf), "0%o", v);
414 snprintf(buf, sizeof(buf), "0x%x", v);
415 cp = buf + strlen(buf);
416 cplim = buf + sizeof(buf);
422 while ((i = *bits++) != 0) {
423 if (v & (1 << (i - 1))) {
430 for (; (c = *bits) > 32; bits++) {
436 for (; *bits > 32; bits++)
448 return "sprintb buffer too small";
451 const char * const timex_states[] = {
452 "OK", "INS", "DEL", "OOP", "WAIT", "ERROR"
462 if ((size_t)s < COUNTOF(timex_states))
463 return timex_states[s];
464 snprintf(buf, sizeof(buf), "TIME-#%d", s);