]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ntp/util/ntptime.c
Virgin import of ntpd 4.0.98f
[FreeBSD/FreeBSD.git] / contrib / ntp / util / ntptime.c
1 /*
2  * NTP test program
3  *
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
7  * data.
8  *
9  * For more information, see the README.kern file in the doc directory
10  * of the xntp3 distribution.
11  */
12 #ifdef HAVE_CONFIG_H
13 # include <config.h>
14 #endif /* HAVE_CONFIG_H */
15
16 #include <stdio.h>
17 #include <ctype.h>
18 #include <sys/time.h>
19 #include <signal.h>
20 #include <setjmp.h>
21
22 #include "ntp_fp.h"
23 #include "ntp_unixtime.h"
24 #include "ntp_syscall.h"
25 #include "ntp_stdlib.h"
26
27 #ifdef NTP_SYSCALLS_STD
28 # ifndef SYS_DECOSF1
29 #  define BADCALL -1            /* this is supposed to be a bad syscall */
30 # endif /* SYS_DECOSF1 */
31 #endif
32
33 #ifdef HAVE_TV_NSEC_IN_NTPTIMEVAL
34 #define tv_frac_sec tv_nsec
35 #else
36 #define tv_frac_sec tv_usec
37 #endif
38
39
40 #define TIMEX_MOD_BITS \
41 "\20\1OFFSET\2FREQUENCY\3MAXERROR\4ESTERROR\5STATUS\6TIMECONST\
42 \13PLL\14FLL\15MICRO\16NANO\17CLKB\20CLKA"
43  
44 #define TIMEX_STA_BITS \
45 "\20\1PLL\2PPSFREQ\3PPSTIME\4FLL\5INS\6DEL\7UNSYNC\10FREQHOLD\
46 \11PPSSIGNAL\12PPSJITTER\13PPSWANDER\14PPSERROR\15CLOCKERR\
47 \16NANO\17MODE\20CLK"
48
49 #define SCALE_FREQ 65536                /* frequency scale */
50
51
52 /*
53  * Function prototypes
54  */
55 char *sprintb           P((u_int, const char *));
56 const char *timex_state P((int));
57 volatile int debug = 0;
58
59 #ifdef SIGSYS
60 void pll_trap           P((int));
61
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() */
65 #endif
66
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 */
70 char* progname;
71 static char optargs[] = "cde:f:hm:o:rs:t:";
72
73 int
74 main(
75         int argc,
76         char *argv[]
77         )
78 {
79         extern int ntp_optind;
80         extern char *ntp_optarg;
81 #ifdef SUBST_ADJTIMEX
82       struct timex ntv;
83 #else
84         struct ntptimeval ntv;
85 #endif
86         struct timeval tv;
87         struct timex ntx, _ntx;
88         int     times[20];
89         double ftemp, gtemp, htemp;
90         long time_frac;                         /* ntv.time.tv_frac_sec (us/ns) */
91         l_fp ts;
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 */
95         int c;
96         int errflg      = 0;
97         int cost        = 0;
98         int rawtime     = 0;
99
100         memset((char *)&ntx, 0, sizeof(ntx));
101         progname = argv[0];
102         while ((c = ntp_getopt(argc, argv, optargs)) != EOF) switch (c) {
103             case 'c':
104                 cost++;
105                 break;
106             case 'd':
107                 debug++;
108                 break;
109             case 'e':
110                 ntx.modes |= MOD_ESTERROR;
111                 ntx.esterror = atoi(ntp_optarg);
112                 break;
113             case 'f':
114                 ntx.modes |= MOD_FREQUENCY;
115                 ntx.freq = (long)(atof(ntp_optarg) * SCALE_FREQ);
116                 break;
117             case 'm':
118                 ntx.modes |= MOD_MAXERROR;
119                 ntx.maxerror = atoi(ntp_optarg);
120                 break;
121             case 'o':
122                 ntx.modes |= MOD_OFFSET;
123                 ntx.offset = atoi(ntp_optarg);
124                 break;
125             case 'r':
126                 rawtime++;
127                 break;
128             case 's':
129                 ntx.modes |= MOD_STATUS;
130                 ntx.status = atoi(ntp_optarg);
131                 if (ntx.status < 0 || ntx.status > 4) errflg++;
132                 break;
133             case 't':
134                 ntx.modes |= MOD_TIMECONST;
135                 ntx.constant = atoi(ntp_optarg);
136                 break;
137             default:
138                 errflg++;
139         }
140         if (errflg || (ntp_optind != argc)) {
141                 (void) fprintf(stderr,
142                                "usage: %s [-%s]\n\n\
143 -c              display the time taken to call ntp_gettime (us)\n\
144 -e esterror     estimate of the error (us)\n\
145 -f frequency    Frequency error (-500 .. 500) (ppm)\n\
146 -h              display this help info\n\
147 -m maxerror     max possible error (us)\n\
148 -o offset       current offset (ms)\n\
149 -r              print the unix and NTP time raw\n\
150 -l leap         Set the leap bits\n\
151 -t timeconstant log2 of PLL time constant (0 .. %d)\n",
152                                progname, optargs, MAXTC);
153                 exit(2);
154         }
155
156 #ifdef SIGSYS
157         /*
158          * Test to make sure the sigaction() works in case of invalid
159          * syscall codes.
160          */
161         newsigsys.sa_handler = pll_trap;
162         newsigsys.sa_flags = 0;
163         if (sigaction(SIGSYS, &newsigsys, &sigsys)) {
164                 perror("sigaction() fails to save SIGSYS trap");
165                 exit(1);
166         }
167 #endif /* SIGSYS */
168
169 #ifdef  BADCALL
170         /*
171          * Make sure the trapcatcher works.
172          */
173         pll_control = 1;
174 #ifdef SIGSYS
175         if (sigsetjmp(env, 1) == 0)
176         {
177 #endif
178                 status = syscall(BADCALL, &ntv); /* dummy parameter */
179                 if ((status < 0) && (errno == ENOSYS))
180                         --pll_control;
181 #ifdef SIGSYS
182         }
183 #endif
184         if (pll_control)
185             printf("sigaction() failed to catch an invalid syscall\n");
186 #endif /* BADCALL */
187
188         if (cost) {
189 #ifdef SIGSYS
190                 if (sigsetjmp(env, 1) == 0) {
191 #endif
192                         for (c = 0; c < sizeof times / sizeof times[0]; c++) {
193                                 status = ntp_gettime(&ntv);
194                                 if ((status < 0) && (errno == ENOSYS))
195                                         --pll_control;
196                                 if (pll_control < 0)
197                                         break;
198                                 times[c] = ntv.time.tv_frac_sec;
199                         }
200 #ifdef SIGSYS
201                 }
202 #endif
203                 if (pll_control >= 0) {
204                         printf("[ us %06d:", times[0]);
205                         for (c = 1; c < sizeof times / sizeof times[0]; c++)
206                             printf(" %d", times[c] - times[c - 1]);
207                         printf(" ]\n");
208                 }
209         }
210 #ifdef SIGSYS
211         if (sigsetjmp(env, 1) == 0) {
212 #endif
213                 status = ntp_gettime(&ntv);
214                 if ((status < 0) && (errno == ENOSYS))
215                         --pll_control;
216 #ifdef SIGSYS
217         }
218 #endif
219         _ntx.modes = 0;                         /* Ensure nothing is set */
220 #ifdef SIGSYS
221         if (sigsetjmp(env, 1) == 0) {
222 #endif
223                 status = ntp_adjtime(&_ntx);
224                 if ((status < 0) && (errno == ENOSYS))
225                         --pll_control;
226                 flash = _ntx.status;
227 #ifdef SIGSYS
228         }
229 #endif
230         if (pll_control < 0) {
231                 printf("NTP user interface routines are not configured in this kernel.\n");
232                 goto lexit;
233         }
234
235         /*
236          * Fetch timekeeping data and display.
237          */
238         status = ntp_gettime(&ntv);
239         if (status < 0)
240                 perror("ntp_gettime() call fails");
241         else {
242                 printf("ntp_gettime() returns code %d (%s)\n",
243                     status, timex_state(status));
244                 time_frac = ntv.time.tv_frac_sec;
245 #ifdef STA_NANO
246                 if (flash & STA_NANO) {
247                         ntv.time.tv_frac_sec /= 1000;
248                         ts_mask = 0xfffffffc;   /* 1/2^30 */
249                         ts_roundbit = 0x00000002;
250                         fdigits = 9;
251                 }
252 #endif
253                 tv.tv_sec = ntv.time.tv_sec;
254                 tv.tv_usec = ntv.time.tv_frac_sec;
255                 TVTOTS(&tv, &ts);
256                 ts.l_ui += JAN_1970;
257                 ts.l_uf += ts_roundbit;
258                 ts.l_uf &= ts_mask;
259                 printf("  time %s, (.%0*d),\n",
260                        prettydate(&ts), fdigits, (int) time_frac);
261                 printf("  maximum error %lu us, estimated error %lu us.\n",
262                        (u_long)ntv.maxerror, (u_long)ntv.esterror);
263                 if (rawtime) printf("  ntptime=%x.%x unixtime=%x.%0*d %s",
264                     (unsigned int) ts.l_ui, (unsigned int) ts.l_uf,
265                     (int) ntv.time.tv_sec, fdigits, (int) time_frac,
266                     ctime((const time_t *) &ntv.time.tv_sec));
267         }
268         status = ntp_adjtime(&ntx);
269         if (status < 0)
270                 perror((errno == EPERM) ? 
271                    "Must be root to set kernel values\nntp_adjtime() call fails" :
272                    "ntp_adjtime() call fails");
273         else {
274                 flash = ntx.status;
275                 printf("ntp_adjtime() returns code %d (%s)\n",
276                      status, timex_state(status));
277                 printf("  modes %s,\n", sprintb(ntx.modes, TIMEX_MOD_BITS));
278                 ftemp = (double)ntx.offset;
279 #ifdef STA_NANO
280                 if (flash & STA_NANO)
281                         ftemp /= 1000.0;
282 #endif
283                 printf("  offset %.3f", ftemp);
284                 ftemp = (double)ntx.freq / SCALE_FREQ;
285                 printf(" us, frequency %.3f ppm, interval %d s,\n",
286                      ftemp, 1 << ntx.shift);
287                 printf("  maximum error %lu us, estimated error %lu us,\n",
288                      (u_long)ntx.maxerror, (u_long)ntx.esterror);
289                 printf("  status %s,\n", sprintb((u_int)ntx.status, TIMEX_STA_BITS));
290                 ftemp = (double)ntx.tolerance / SCALE_FREQ;
291                 gtemp = (double)ntx.precision;
292 #ifdef STA_NANO
293                 if (flash & STA_NANO)
294                         gtemp /= 1000.0;
295 #endif
296                 printf(
297                     "  time constant %lu, precision %.3f us, tolerance %.0f ppm,\n",
298                     (u_long)ntx.constant, gtemp, ftemp);
299                 if (ntx.shift == 0)
300                         exit (0);
301                 ftemp = (double)ntx.ppsfreq / SCALE_FREQ;
302                 gtemp = (double)ntx.stabil / SCALE_FREQ;
303                 htemp = (double)ntx.jitter;
304 #ifdef STA_NANO
305                 if (flash & STA_NANO)
306                         htemp /= 1000.0;
307 #endif
308                 printf(
309                     "  pps frequency %.3f ppm, stability %.3f ppm, jitter %.3f us,\n",
310                     ftemp, gtemp, htemp);
311                 printf("  intervals %lu, jitter exceeded %lu, stability exceeded %lu, errors %lu.\n",
312                     (u_long)ntx.calcnt, (u_long)ntx.jitcnt,
313                     (u_long)ntx.stbcnt, (u_long)ntx.errcnt);
314                 return (0);
315         }
316
317         /*
318          * Put things back together the way we found them.
319          */
320     lexit:
321 #ifdef SIGSYS
322         if (sigaction(SIGSYS, &sigsys, (struct sigaction *)NULL)) {
323                 perror("sigaction() fails to restore SIGSYS trap");
324                 exit(1);
325         }
326 #endif
327         exit(0);
328 }
329
330 #ifdef SIGSYS
331 /*
332  * pll_trap - trap processor for undefined syscalls
333  */
334 void
335 pll_trap(
336         int arg
337         )
338 {
339         pll_control--;
340         siglongjmp(env, 1);
341 }
342 #endif
343
344 /*
345  * Print a value a la the %b format of the kernel's printf
346  */
347 char *
348 sprintb(
349         register u_int v,
350         register const char *bits
351         )
352 {
353         register char *cp;
354         register int i, any = 0;
355         register char c;
356         static char buf[132];
357
358         if (bits && *bits == 8)
359             (void)sprintf(buf, "0%o", v);
360         else
361             (void)sprintf(buf, "0x%x", v);
362         cp = buf + strlen(buf);
363         bits++;
364         if (bits) {
365                 *cp++ = ' ';
366                 *cp++ = '(';
367                 while ((i = *bits++) != 0) {
368                         if (v & (1 << (i-1))) {
369                                 if (any)
370                                     *cp++ = ',';
371                                 any = 1;
372                                 for (; (c = *bits) > 32; bits++)
373                                     *cp++ = c;
374                         } else
375                             for (; *bits > 32; bits++)
376                                 continue;
377                 }
378                 *cp++ = ')';
379         }
380         *cp = '\0';
381         return (buf);
382 }
383
384 const char *timex_states[] = {
385         "OK", "INS", "DEL", "OOP", "WAIT", "ERROR"
386 };
387
388 const char *
389 timex_state(
390         register int s
391         )
392 {
393         static char buf[32];
394
395         if (s >= 0 && s <= sizeof(timex_states) / sizeof(timex_states[0]))
396             return (timex_states[s]);
397         sprintf(buf, "TIME-#%d", s);
398         return (buf);
399 }