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));
59 void pll_trap P((int));
61 static struct sigaction newsigsys; /* new sigaction status */
62 static struct sigaction sigsys; /* current sigaction status */
63 static sigjmp_buf env; /* environment var. for pll_trap() */
66 static volatile int pll_control; /* (0) daemon, (1) kernel loop */
67 static volatile int status; /* most recent status bits */
68 static volatile int flash; /* most recent ntp_adjtime() bits */
70 static char optargs[] = "MNT:cde:f:hm:o:rs:t:";
78 extern int ntp_optind;
79 extern char *ntp_optarg;
83 struct ntptimeval ntv;
86 struct timex ntx, _ntx;
88 double ftemp, gtemp, htemp;
89 long time_frac; /* ntv.time.tv_frac_sec (us/ns) */
91 volatile unsigned ts_mask = TS_MASK; /* defaults to 20 bits (us) */
92 volatile unsigned ts_roundbit = TS_ROUNDBIT; /* defaults to 20 bits (us) */
93 volatile int fdigits = 6; /* fractional digits for us */
97 volatile int rawtime = 0;
99 memset((char *)&ntx, 0, sizeof(ntx));
101 while ((c = ntp_getopt(argc, argv, optargs)) != EOF) switch (c) {
104 ntx.modes |= MOD_MICRO;
109 ntx.modes |= MOD_NANO;
116 ntx.constant = atoi(ntp_optarg);
124 ntx.modes |= MOD_ESTERROR;
125 ntx.esterror = atoi(ntp_optarg);
128 ntx.modes |= MOD_FREQUENCY;
129 ntx.freq = (long)(atof(ntp_optarg) * SCALE_FREQ);
132 ntx.modes |= MOD_MAXERROR;
133 ntx.maxerror = atoi(ntp_optarg);
136 ntx.modes |= MOD_OFFSET;
137 ntx.offset = atoi(ntp_optarg);
143 ntx.modes |= MOD_STATUS;
144 ntx.status = atoi(ntp_optarg);
145 if (ntx.status < 0 || ntx.status >= 0x100) errflg++;
148 ntx.modes |= MOD_TIMECONST;
149 ntx.constant = atoi(ntp_optarg);
154 if (errflg || (ntp_optind != argc)) {
155 (void) fprintf(stderr,
156 "usage: %s [-%s]\n\n\
158 -c display the time taken to call ntp_gettime (us)\n\
159 -e esterror estimate of the error (us)\n\
160 -f frequency Frequency error (-500 .. 500) (ppm)\n\
161 -h display this help info\n\
162 -m maxerror max possible error (us)\n\
163 -o offset current offset (ms)\n\
164 -r print the unix and NTP time raw\n\
165 -s status Set the status bits\n\
166 -t timeconstant log2 of PLL time constant (0 .. %d)\n",
169 "-M switch to microsecond mode\n",
174 "-N switch to nanosecond mode\n",
180 "-T tai_offset set TAI offset\n",
193 * Test to make sure the sigaction() works in case of invalid
196 newsigsys.sa_handler = pll_trap;
197 newsigsys.sa_flags = 0;
198 if (sigaction(SIGSYS, &newsigsys, &sigsys)) {
199 perror("sigaction() fails to save SIGSYS trap");
206 * Make sure the trapcatcher works.
210 if (sigsetjmp(env, 1) == 0)
213 status = syscall(BADCALL, &ntv); /* dummy parameter */
214 if ((status < 0) && (errno == ENOSYS))
220 printf("sigaction() failed to catch an invalid syscall\n");
225 if (sigsetjmp(env, 1) == 0) {
227 for (c = 0; c < sizeof times / sizeof times[0]; c++) {
228 status = ntp_gettime(&ntv);
229 if ((status < 0) && (errno == ENOSYS))
233 times[c] = ntv.time.tv_frac_sec;
238 if (pll_control >= 0) {
239 printf("[ us %06d:", times[0]);
240 for (c = 1; c < sizeof times / sizeof times[0]; c++)
241 printf(" %d", times[c] - times[c - 1]);
246 if (sigsetjmp(env, 1) == 0) {
248 status = ntp_gettime(&ntv);
249 if ((status < 0) && (errno == ENOSYS))
254 _ntx.modes = 0; /* Ensure nothing is set */
256 if (sigsetjmp(env, 1) == 0) {
258 status = ntp_adjtime(&_ntx);
259 if ((status < 0) && (errno == ENOSYS))
265 if (pll_control < 0) {
266 printf("NTP user interface routines are not configured in this kernel.\n");
271 * Fetch timekeeping data and display.
273 status = ntp_gettime(&ntv);
275 perror("ntp_gettime() call fails");
277 printf("ntp_gettime() returns code %d (%s)\n",
278 status, timex_state(status));
279 time_frac = ntv.time.tv_frac_sec;
281 if (flash & STA_NANO) {
282 ntv.time.tv_frac_sec /= 1000;
283 ts_mask = 0xfffffffc; /* 1/2^30 */
284 ts_roundbit = 0x00000002;
288 tv.tv_sec = ntv.time.tv_sec;
289 tv.tv_usec = ntv.time.tv_frac_sec;
292 ts.l_uf += ts_roundbit;
294 printf(" time %s, (.%0*d),\n",
295 prettydate(&ts), fdigits, (int) time_frac);
296 printf(" maximum error %lu us, estimated error %lu us",
297 (u_long)ntv.maxerror, (u_long)ntv.esterror);
299 printf(" ntptime=%x.%x unixtime=%x.%0*d %s",
300 (unsigned int) ts.l_ui, (unsigned int) ts.l_uf,
301 (int) ntv.time.tv_sec, fdigits, (int) time_frac,
302 ctime((const time_t *) &ntv.time.tv_sec));
304 printf(", TAI offset %ld\n", (long)ntv.tai);
309 status = ntp_adjtime(&ntx);
311 perror((errno == EPERM) ?
312 "Must be root to set kernel values\nntp_adjtime() call fails" :
313 "ntp_adjtime() call fails");
316 printf("ntp_adjtime() returns code %d (%s)\n",
317 status, timex_state(status));
318 printf(" modes %s,\n", sprintb(ntx.modes, TIMEX_MOD_BITS));
319 ftemp = (double)ntx.offset;
321 if (flash & STA_NANO)
324 printf(" offset %.3f", ftemp);
325 ftemp = (double)ntx.freq / SCALE_FREQ;
326 printf(" us, frequency %.3f ppm, interval %d s,\n",
327 ftemp, 1 << ntx.shift);
328 printf(" maximum error %lu us, estimated error %lu us,\n",
329 (u_long)ntx.maxerror, (u_long)ntx.esterror);
330 printf(" status %s,\n", sprintb((u_int)ntx.status, TIMEX_STA_BITS));
331 ftemp = (double)ntx.tolerance / SCALE_FREQ;
332 gtemp = (double)ntx.precision;
334 if (flash & STA_NANO)
338 " time constant %lu, precision %.3f us, tolerance %.0f ppm,\n",
339 (u_long)ntx.constant, gtemp, ftemp);
342 ftemp = (double)ntx.ppsfreq / SCALE_FREQ;
343 gtemp = (double)ntx.stabil / SCALE_FREQ;
344 htemp = (double)ntx.jitter;
346 if (flash & STA_NANO)
350 " pps frequency %.3f ppm, stability %.3f ppm, jitter %.3f us,\n",
351 ftemp, gtemp, htemp);
352 printf(" intervals %lu, jitter exceeded %lu, stability exceeded %lu, errors %lu.\n",
353 (u_long)ntx.calcnt, (u_long)ntx.jitcnt,
354 (u_long)ntx.stbcnt, (u_long)ntx.errcnt);
359 * Put things back together the way we found them.
363 if (sigaction(SIGSYS, &sigsys, (struct sigaction *)NULL)) {
364 perror("sigaction() fails to restore SIGSYS trap");
373 * pll_trap - trap processor for undefined syscalls
386 * Print a value a la the %b format of the kernel's printf
391 register const char *bits
395 register int i, any = 0;
397 static char buf[132];
399 if (bits && *bits == 8)
400 (void)sprintf(buf, "0%o", v);
402 (void)sprintf(buf, "0x%x", v);
403 cp = buf + strlen(buf);
408 while ((i = *bits++) != 0) {
409 if (v & (1 << (i-1))) {
413 for (; (c = *bits) > 32; bits++)
416 for (; *bits > 32; bits++)
425 const char *timex_states[] = {
426 "OK", "INS", "DEL", "OOP", "WAIT", "ERROR"
436 if (s >= 0 && s < sizeof(timex_states) / sizeof(timex_states[0]))
437 return (timex_states[s]);
438 sprintf(buf, "TIME-#%d", s);