1c0b746e5SOllivier Robert /*
2c0b746e5SOllivier Robert * NTP test program
3c0b746e5SOllivier Robert *
4c0b746e5SOllivier Robert * This program tests to see if the NTP user interface routines
5c0b746e5SOllivier Robert * ntp_gettime() and ntp_adjtime() have been implemented in the kernel.
6c0b746e5SOllivier Robert * If so, each of these routines is called to display current timekeeping
7c0b746e5SOllivier Robert * data.
8c0b746e5SOllivier Robert *
9c0b746e5SOllivier Robert * For more information, see the README.kern file in the doc directory
10c0b746e5SOllivier Robert * of the xntp3 distribution.
11c0b746e5SOllivier Robert */
12224ba2bdSOllivier Robert
13c0b746e5SOllivier Robert #ifdef HAVE_CONFIG_H
14c0b746e5SOllivier Robert # include <config.h>
15c0b746e5SOllivier Robert #endif /* HAVE_CONFIG_H */
16c0b746e5SOllivier Robert
17c0b746e5SOllivier Robert #include "ntp_fp.h"
182b15cb3dSCy Schubert #include "timevalops.h"
19c0b746e5SOllivier Robert #include "ntp_syscall.h"
20c0b746e5SOllivier Robert #include "ntp_stdlib.h"
21c0b746e5SOllivier Robert
22224ba2bdSOllivier Robert #include <stdio.h>
23224ba2bdSOllivier Robert #include <ctype.h>
24224ba2bdSOllivier Robert #include <signal.h>
25224ba2bdSOllivier Robert #include <setjmp.h>
26224ba2bdSOllivier Robert
27c0b746e5SOllivier Robert #ifdef NTP_SYSCALLS_STD
28c0b746e5SOllivier Robert # ifndef SYS_DECOSF1
29c0b746e5SOllivier Robert # define BADCALL -1 /* this is supposed to be a bad syscall */
30c0b746e5SOllivier Robert # endif /* SYS_DECOSF1 */
31c0b746e5SOllivier Robert #endif
32c0b746e5SOllivier Robert
33224ba2bdSOllivier Robert #ifdef HAVE_STRUCT_NTPTIMEVAL_TIME_TV_NSEC
34c0b746e5SOllivier Robert #define tv_frac_sec tv_nsec
35c0b746e5SOllivier Robert #else
36c0b746e5SOllivier Robert #define tv_frac_sec tv_usec
37c0b746e5SOllivier Robert #endif
38c0b746e5SOllivier Robert
39c0b746e5SOllivier Robert
40c0b746e5SOllivier Robert #define TIMEX_MOD_BITS \
41c0b746e5SOllivier Robert "\20\1OFFSET\2FREQUENCY\3MAXERROR\4ESTERROR\5STATUS\6TIMECONST\
42c0b746e5SOllivier Robert \13PLL\14FLL\15MICRO\16NANO\17CLKB\20CLKA"
43c0b746e5SOllivier Robert
44c0b746e5SOllivier Robert #define TIMEX_STA_BITS \
45c0b746e5SOllivier Robert "\20\1PLL\2PPSFREQ\3PPSTIME\4FLL\5INS\6DEL\7UNSYNC\10FREQHOLD\
46c0b746e5SOllivier Robert \11PPSSIGNAL\12PPSJITTER\13PPSWANDER\14PPSERROR\15CLOCKERR\
47c0b746e5SOllivier Robert \16NANO\17MODE\20CLK"
48c0b746e5SOllivier Robert
49c0b746e5SOllivier Robert #define SCALE_FREQ 65536 /* frequency scale */
50c0b746e5SOllivier Robert
512b15cb3dSCy Schubert /*
522b15cb3dSCy Schubert * These constants are used to round the time stamps computed from
532b15cb3dSCy Schubert * a struct timeval to the microsecond (more or less). This keeps
542b15cb3dSCy Schubert * things neat.
552b15cb3dSCy Schubert */
56*2d4e511cSCy Schubert #define TS_MASK_US 0xfffff000 /* mask to usec, for time stamps */
57*2d4e511cSCy Schubert #define TS_ROUNDBIT_US 0x00000800 /* round at this bit */
58*2d4e511cSCy Schubert #define TS_DIGITS_US 6
59*2d4e511cSCy Schubert
60*2d4e511cSCy Schubert #define TS_MASK_NS 0xfffffffc /* 1/2^30, for nsec */
61*2d4e511cSCy Schubert #define TS_ROUNDBIT_NS 0x00000002
62*2d4e511cSCy Schubert #define TS_DIGITS_NS 9
63c0b746e5SOllivier Robert
64c0b746e5SOllivier Robert /*
65c0b746e5SOllivier Robert * Function prototypes
66c0b746e5SOllivier Robert */
672b15cb3dSCy Schubert const char * sprintb (u_int, const char *);
682b15cb3dSCy Schubert const char * timex_state (int);
69c0b746e5SOllivier Robert
70c0b746e5SOllivier Robert #ifdef SIGSYS
712b15cb3dSCy Schubert void pll_trap (int);
72c0b746e5SOllivier Robert
73c0b746e5SOllivier Robert static struct sigaction newsigsys; /* new sigaction status */
74c0b746e5SOllivier Robert static struct sigaction sigsys; /* current sigaction status */
75c0b746e5SOllivier Robert static sigjmp_buf env; /* environment var. for pll_trap() */
76c0b746e5SOllivier Robert #endif
77c0b746e5SOllivier Robert
78c0b746e5SOllivier Robert static volatile int pll_control; /* (0) daemon, (1) kernel loop */
79c0b746e5SOllivier Robert static volatile int status; /* most recent status bits */
80c0b746e5SOllivier Robert static volatile int flash; /* most recent ntp_adjtime() bits */
819034852cSGleb Smirnoff char const * progname;
82224ba2bdSOllivier Robert static char optargs[] = "MNT:cde:f:hm:o:rs:t:";
83c0b746e5SOllivier Robert
84c0b746e5SOllivier Robert int
main(int argc,char * argv[])85c0b746e5SOllivier Robert main(
86c0b746e5SOllivier Robert int argc,
87c0b746e5SOllivier Robert char *argv[]
88c0b746e5SOllivier Robert )
89c0b746e5SOllivier Robert {
90c0b746e5SOllivier Robert extern int ntp_optind;
91c0b746e5SOllivier Robert extern char *ntp_optarg;
92c0b746e5SOllivier Robert #ifdef SUBST_ADJTIMEX
93c0b746e5SOllivier Robert struct timex ntv;
94c0b746e5SOllivier Robert #else
95c0b746e5SOllivier Robert struct ntptimeval ntv;
96c0b746e5SOllivier Robert #endif
97c0b746e5SOllivier Robert struct timeval tv;
98c0b746e5SOllivier Robert struct timex ntx, _ntx;
999034852cSGleb Smirnoff int times[20] = { 0 };
100c0b746e5SOllivier Robert double ftemp, gtemp, htemp;
101*2d4e511cSCy Schubert double nscale = 1.0; /* assume usec scale for now */
102c0b746e5SOllivier Robert long time_frac; /* ntv.time.tv_frac_sec (us/ns) */
103c0b746e5SOllivier Robert l_fp ts;
104*2d4e511cSCy Schubert volatile unsigned ts_mask = TS_MASK_US; /* defaults to 20 bits (us) */
105*2d4e511cSCy Schubert volatile unsigned ts_roundbit = TS_ROUNDBIT_US; /* defaults to 20 bits (us) */
106*2d4e511cSCy Schubert volatile int fdigits = TS_DIGITS_US; /* fractional digits for us */
1072b15cb3dSCy Schubert size_t c;
1082b15cb3dSCy Schubert int ch;
109c0b746e5SOllivier Robert int errflg = 0;
110c0b746e5SOllivier Robert int cost = 0;
1119c2daa00SOllivier Robert volatile int rawtime = 0;
112c0b746e5SOllivier Robert
1132b15cb3dSCy Schubert ZERO(ntx);
114c0b746e5SOllivier Robert progname = argv[0];
1152b15cb3dSCy Schubert while ((ch = ntp_getopt(argc, argv, optargs)) != EOF) {
1162b15cb3dSCy Schubert switch (ch) {
117224ba2bdSOllivier Robert #ifdef MOD_MICRO
118224ba2bdSOllivier Robert case 'M':
119224ba2bdSOllivier Robert ntx.modes |= MOD_MICRO;
120224ba2bdSOllivier Robert break;
121224ba2bdSOllivier Robert #endif
122224ba2bdSOllivier Robert #ifdef MOD_NANO
123224ba2bdSOllivier Robert case 'N':
124224ba2bdSOllivier Robert ntx.modes |= MOD_NANO;
125224ba2bdSOllivier Robert break;
126224ba2bdSOllivier Robert #endif
127*2d4e511cSCy Schubert #if defined(NTP_API) && NTP_API > 3
128224ba2bdSOllivier Robert case 'T':
129224ba2bdSOllivier Robert ntx.modes = MOD_TAI;
130224ba2bdSOllivier Robert ntx.constant = atoi(ntp_optarg);
131224ba2bdSOllivier Robert break;
132224ba2bdSOllivier Robert #endif
133c0b746e5SOllivier Robert case 'c':
134c0b746e5SOllivier Robert cost++;
135c0b746e5SOllivier Robert break;
1362b15cb3dSCy Schubert
137c0b746e5SOllivier Robert case 'e':
138c0b746e5SOllivier Robert ntx.modes |= MOD_ESTERROR;
139c0b746e5SOllivier Robert ntx.esterror = atoi(ntp_optarg);
140c0b746e5SOllivier Robert break;
1412b15cb3dSCy Schubert
142c0b746e5SOllivier Robert case 'f':
143c0b746e5SOllivier Robert ntx.modes |= MOD_FREQUENCY;
144c0b746e5SOllivier Robert ntx.freq = (long)(atof(ntp_optarg) * SCALE_FREQ);
145c0b746e5SOllivier Robert break;
1462b15cb3dSCy Schubert
147c0b746e5SOllivier Robert case 'm':
148c0b746e5SOllivier Robert ntx.modes |= MOD_MAXERROR;
149c0b746e5SOllivier Robert ntx.maxerror = atoi(ntp_optarg);
150c0b746e5SOllivier Robert break;
1512b15cb3dSCy Schubert
152c0b746e5SOllivier Robert case 'o':
153c0b746e5SOllivier Robert ntx.modes |= MOD_OFFSET;
154c0b746e5SOllivier Robert ntx.offset = atoi(ntp_optarg);
155c0b746e5SOllivier Robert break;
1562b15cb3dSCy Schubert
157c0b746e5SOllivier Robert case 'r':
158c0b746e5SOllivier Robert rawtime++;
159c0b746e5SOllivier Robert break;
1602b15cb3dSCy Schubert
161c0b746e5SOllivier Robert case 's':
162c0b746e5SOllivier Robert ntx.modes |= MOD_STATUS;
163c0b746e5SOllivier Robert ntx.status = atoi(ntp_optarg);
1642b15cb3dSCy Schubert if (ntx.status < 0 || ntx.status >= 0x100)
1652b15cb3dSCy Schubert errflg++;
166c0b746e5SOllivier Robert break;
1672b15cb3dSCy Schubert
168c0b746e5SOllivier Robert case 't':
169c0b746e5SOllivier Robert ntx.modes |= MOD_TIMECONST;
170c0b746e5SOllivier Robert ntx.constant = atoi(ntp_optarg);
171c0b746e5SOllivier Robert break;
1722b15cb3dSCy Schubert
173c0b746e5SOllivier Robert default:
174c0b746e5SOllivier Robert errflg++;
175c0b746e5SOllivier Robert }
1762b15cb3dSCy Schubert }
177c0b746e5SOllivier Robert if (errflg || (ntp_optind != argc)) {
1782b15cb3dSCy Schubert fprintf(stderr,
179c0b746e5SOllivier Robert "usage: %s [-%s]\n\n\
180224ba2bdSOllivier Robert %s%s%s\
181c0b746e5SOllivier Robert -c display the time taken to call ntp_gettime (us)\n\
182c0b746e5SOllivier Robert -e esterror estimate of the error (us)\n\
183c0b746e5SOllivier Robert -f frequency Frequency error (-500 .. 500) (ppm)\n\
184c0b746e5SOllivier Robert -h display this help info\n\
185c0b746e5SOllivier Robert -m maxerror max possible error (us)\n\
186c0b746e5SOllivier Robert -o offset current offset (ms)\n\
187c0b746e5SOllivier Robert -r print the unix and NTP time raw\n\
1889c2daa00SOllivier Robert -s status Set the status bits\n\
189c0b746e5SOllivier Robert -t timeconstant log2 of PLL time constant (0 .. %d)\n",
190224ba2bdSOllivier Robert progname, optargs,
191224ba2bdSOllivier Robert #ifdef MOD_MICRO
192224ba2bdSOllivier Robert "-M switch to microsecond mode\n",
193224ba2bdSOllivier Robert #else
194224ba2bdSOllivier Robert "",
195224ba2bdSOllivier Robert #endif
196224ba2bdSOllivier Robert #ifdef MOD_NANO
197224ba2bdSOllivier Robert "-N switch to nanosecond mode\n",
198224ba2bdSOllivier Robert #else
199224ba2bdSOllivier Robert "",
200224ba2bdSOllivier Robert #endif
201224ba2bdSOllivier Robert #ifdef NTP_API
202224ba2bdSOllivier Robert # if NTP_API > 3
203224ba2bdSOllivier Robert "-T tai_offset set TAI offset\n",
204224ba2bdSOllivier Robert # else
205224ba2bdSOllivier Robert "",
206224ba2bdSOllivier Robert # endif
207224ba2bdSOllivier Robert #else
208224ba2bdSOllivier Robert "",
209224ba2bdSOllivier Robert #endif
210224ba2bdSOllivier Robert MAXTC);
211c0b746e5SOllivier Robert exit(2);
212c0b746e5SOllivier Robert }
213c0b746e5SOllivier Robert
214c0b746e5SOllivier Robert #ifdef SIGSYS
215c0b746e5SOllivier Robert /*
216c0b746e5SOllivier Robert * Test to make sure the sigaction() works in case of invalid
217c0b746e5SOllivier Robert * syscall codes.
218c0b746e5SOllivier Robert */
219c0b746e5SOllivier Robert newsigsys.sa_handler = pll_trap;
220c0b746e5SOllivier Robert newsigsys.sa_flags = 0;
221c0b746e5SOllivier Robert if (sigaction(SIGSYS, &newsigsys, &sigsys)) {
222c0b746e5SOllivier Robert perror("sigaction() fails to save SIGSYS trap");
223c0b746e5SOllivier Robert exit(1);
224c0b746e5SOllivier Robert }
225c0b746e5SOllivier Robert #endif /* SIGSYS */
226c0b746e5SOllivier Robert
227c0b746e5SOllivier Robert #ifdef BADCALL
228c0b746e5SOllivier Robert /*
229c0b746e5SOllivier Robert * Make sure the trapcatcher works.
230c0b746e5SOllivier Robert */
231c0b746e5SOllivier Robert pll_control = 1;
232c0b746e5SOllivier Robert #ifdef SIGSYS
233*2d4e511cSCy Schubert if (sigsetjmp(env, 1) == 0)
234c0b746e5SOllivier Robert #endif
235*2d4e511cSCy Schubert {
236c0b746e5SOllivier Robert status = syscall(BADCALL, &ntv); /* dummy parameter */
237c0b746e5SOllivier Robert if ((status < 0) && (errno == ENOSYS))
238c0b746e5SOllivier Robert --pll_control;
239c0b746e5SOllivier Robert }
240c0b746e5SOllivier Robert if (pll_control)
241c0b746e5SOllivier Robert printf("sigaction() failed to catch an invalid syscall\n");
242c0b746e5SOllivier Robert #endif /* BADCALL */
243c0b746e5SOllivier Robert
244c0b746e5SOllivier Robert if (cost) {
245c0b746e5SOllivier Robert #ifdef SIGSYS
246*2d4e511cSCy Schubert if (sigsetjmp(env, 1) == 0)
247c0b746e5SOllivier Robert #endif
248*2d4e511cSCy Schubert {
2492b15cb3dSCy Schubert for (c = 0; c < COUNTOF(times); c++) {
250c0b746e5SOllivier Robert status = ntp_gettime(&ntv);
251c0b746e5SOllivier Robert if ((status < 0) && (errno == ENOSYS))
252c0b746e5SOllivier Robert --pll_control;
253c0b746e5SOllivier Robert if (pll_control < 0)
254c0b746e5SOllivier Robert break;
255c0b746e5SOllivier Robert times[c] = ntv.time.tv_frac_sec;
256c0b746e5SOllivier Robert }
257c0b746e5SOllivier Robert }
258c0b746e5SOllivier Robert if (pll_control >= 0) {
259c0b746e5SOllivier Robert printf("[ us %06d:", times[0]);
2602b15cb3dSCy Schubert for (c = 1; c < COUNTOF(times); c++)
261c0b746e5SOllivier Robert printf(" %d", times[c] - times[c - 1]);
262c0b746e5SOllivier Robert printf(" ]\n");
263c0b746e5SOllivier Robert }
264c0b746e5SOllivier Robert }
265c0b746e5SOllivier Robert #ifdef SIGSYS
266*2d4e511cSCy Schubert if (sigsetjmp(env, 1) == 0)
267c0b746e5SOllivier Robert #endif
268*2d4e511cSCy Schubert {
269c0b746e5SOllivier Robert status = ntp_gettime(&ntv);
270c0b746e5SOllivier Robert if ((status < 0) && (errno == ENOSYS))
271c0b746e5SOllivier Robert --pll_control;
272c0b746e5SOllivier Robert }
273c0b746e5SOllivier Robert _ntx.modes = 0; /* Ensure nothing is set */
274c0b746e5SOllivier Robert #ifdef SIGSYS
275*2d4e511cSCy Schubert if (sigsetjmp(env, 1) == 0)
276c0b746e5SOllivier Robert #endif
277*2d4e511cSCy Schubert {
278c0b746e5SOllivier Robert status = ntp_adjtime(&_ntx);
279c0b746e5SOllivier Robert if ((status < 0) && (errno == ENOSYS))
280c0b746e5SOllivier Robert --pll_control;
281c0b746e5SOllivier Robert flash = _ntx.status;
282c0b746e5SOllivier Robert }
283c0b746e5SOllivier Robert if (pll_control < 0) {
284c0b746e5SOllivier Robert printf("NTP user interface routines are not configured in this kernel.\n");
285c0b746e5SOllivier Robert goto lexit;
286c0b746e5SOllivier Robert }
287c0b746e5SOllivier Robert
288c0b746e5SOllivier Robert /*
289c0b746e5SOllivier Robert * Fetch timekeeping data and display.
290c0b746e5SOllivier Robert */
291c0b746e5SOllivier Robert status = ntp_gettime(&ntv);
2922b15cb3dSCy Schubert if (status < 0) {
293c0b746e5SOllivier Robert perror("ntp_gettime() call fails");
2942b15cb3dSCy Schubert } else {
295c0b746e5SOllivier Robert printf("ntp_gettime() returns code %d (%s)\n",
296c0b746e5SOllivier Robert status, timex_state(status));
297c0b746e5SOllivier Robert time_frac = ntv.time.tv_frac_sec;
298c0b746e5SOllivier Robert #ifdef STA_NANO
299c0b746e5SOllivier Robert if (flash & STA_NANO) {
300c0b746e5SOllivier Robert ntv.time.tv_frac_sec /= 1000;
301*2d4e511cSCy Schubert ts_mask = TS_MASK_NS;
302*2d4e511cSCy Schubert ts_roundbit = TS_ROUNDBIT_NS;
303*2d4e511cSCy Schubert fdigits = TS_DIGITS_NS;
304c0b746e5SOllivier Robert }
305c0b746e5SOllivier Robert #endif
306c0b746e5SOllivier Robert tv.tv_sec = ntv.time.tv_sec;
307c0b746e5SOllivier Robert tv.tv_usec = ntv.time.tv_frac_sec;
308c0b746e5SOllivier Robert TVTOTS(&tv, &ts);
309c0b746e5SOllivier Robert ts.l_ui += JAN_1970;
310c0b746e5SOllivier Robert ts.l_uf += ts_roundbit;
311c0b746e5SOllivier Robert ts.l_uf &= ts_mask;
312c0b746e5SOllivier Robert printf(" time %s, (.%0*d),\n",
313c0b746e5SOllivier Robert prettydate(&ts), fdigits, (int)time_frac);
314*2d4e511cSCy Schubert printf(" maximum error %ld us, estimated error %ld us",
315*2d4e511cSCy Schubert ntv.maxerror, ntv.esterror);
316224ba2bdSOllivier Robert if (rawtime)
317224ba2bdSOllivier Robert printf(" ntptime=%x.%x unixtime=%x.%0*d %s",
3182b15cb3dSCy Schubert (u_int)ts.l_ui, (u_int)ts.l_uf,
3192b15cb3dSCy Schubert (int)ntv.time.tv_sec, fdigits,
3202b15cb3dSCy Schubert (int)time_frac,
3212b15cb3dSCy Schubert ctime((time_t *)&ntv.time.tv_sec));
322*2d4e511cSCy Schubert #if defined(NTP_API) && NTP_API > 3
3239c2daa00SOllivier Robert printf(", TAI offset %ld\n", (long)ntv.tai);
324224ba2bdSOllivier Robert #else
325224ba2bdSOllivier Robert printf("\n");
326224ba2bdSOllivier Robert #endif /* NTP_API */
327c0b746e5SOllivier Robert }
328c0b746e5SOllivier Robert status = ntp_adjtime(&ntx);
3292b15cb3dSCy Schubert if (status < 0) {
330c0b746e5SOllivier Robert perror((errno == EPERM) ?
331c0b746e5SOllivier Robert "Must be root to set kernel values\nntp_adjtime() call fails" :
332c0b746e5SOllivier Robert "ntp_adjtime() call fails");
3332b15cb3dSCy Schubert } else {
334c0b746e5SOllivier Robert flash = ntx.status;
335c0b746e5SOllivier Robert printf("ntp_adjtime() returns code %d (%s)\n",
336c0b746e5SOllivier Robert status, timex_state(status));
337c0b746e5SOllivier Robert printf(" modes %s,\n", sprintb(ntx.modes, TIMEX_MOD_BITS));
338c0b746e5SOllivier Robert #ifdef STA_NANO
339c0b746e5SOllivier Robert if (flash & STA_NANO)
340*2d4e511cSCy Schubert nscale = 1e-3;
341c0b746e5SOllivier Robert #endif
342*2d4e511cSCy Schubert ftemp = (double)ntx.offset * nscale;
343c0b746e5SOllivier Robert printf(" offset %.3f", ftemp);
344c0b746e5SOllivier Robert ftemp = (double)ntx.freq / SCALE_FREQ;
345c0b746e5SOllivier Robert printf(" us, frequency %.3f ppm, interval %d s,\n",
346c0b746e5SOllivier Robert ftemp, 1 << ntx.shift);
347*2d4e511cSCy Schubert printf(" maximum error %ld us, estimated error %ld us,\n",
348*2d4e511cSCy Schubert ntx.maxerror, ntx.esterror);
349c0b746e5SOllivier Robert printf(" status %s,\n", sprintb((u_int)ntx.status, TIMEX_STA_BITS));
350c0b746e5SOllivier Robert ftemp = (double)ntx.tolerance / SCALE_FREQ;
351*2d4e511cSCy Schubert gtemp = (double)ntx.precision * nscale;
352c0b746e5SOllivier Robert printf(
353c0b746e5SOllivier Robert " time constant %lu, precision %.3f us, tolerance %.0f ppm,\n",
354c0b746e5SOllivier Robert (u_long)ntx.constant, gtemp, ftemp);
355c0b746e5SOllivier Robert if (ntx.shift == 0)
356c0b746e5SOllivier Robert exit(0);
357c0b746e5SOllivier Robert ftemp = (double)ntx.ppsfreq / SCALE_FREQ;
358c0b746e5SOllivier Robert gtemp = (double)ntx.stabil / SCALE_FREQ;
359*2d4e511cSCy Schubert htemp = (double)ntx.jitter * nscale;
360*2d4e511cSCy Schubert printf(" pps frequency %.3f ppm, stability %.3f ppm, jitter %.3f us,\n",
361c0b746e5SOllivier Robert ftemp, gtemp, htemp);
362c0b746e5SOllivier Robert printf(" intervals %lu, jitter exceeded %lu, stability exceeded %lu, errors %lu.\n",
363c0b746e5SOllivier Robert (u_long)ntx.calcnt, (u_long)ntx.jitcnt,
364c0b746e5SOllivier Robert (u_long)ntx.stbcnt, (u_long)ntx.errcnt);
3652b15cb3dSCy Schubert return 0;
366c0b746e5SOllivier Robert }
367c0b746e5SOllivier Robert
368c0b746e5SOllivier Robert /*
369c0b746e5SOllivier Robert * Put things back together the way we found them.
370c0b746e5SOllivier Robert */
371c0b746e5SOllivier Robert lexit:
372c0b746e5SOllivier Robert #ifdef SIGSYS
373c0b746e5SOllivier Robert if (sigaction(SIGSYS, &sigsys, (struct sigaction *)NULL)) {
374c0b746e5SOllivier Robert perror("sigaction() fails to restore SIGSYS trap");
375c0b746e5SOllivier Robert exit(1);
376c0b746e5SOllivier Robert }
377c0b746e5SOllivier Robert #endif
378c0b746e5SOllivier Robert exit(0);
379c0b746e5SOllivier Robert }
380c0b746e5SOllivier Robert
381c0b746e5SOllivier Robert #ifdef SIGSYS
382c0b746e5SOllivier Robert /*
383c0b746e5SOllivier Robert * pll_trap - trap processor for undefined syscalls
384c0b746e5SOllivier Robert */
385c0b746e5SOllivier Robert void
pll_trap(int arg)386c0b746e5SOllivier Robert pll_trap(
387c0b746e5SOllivier Robert int arg
388c0b746e5SOllivier Robert )
389c0b746e5SOllivier Robert {
390c0b746e5SOllivier Robert pll_control--;
391c0b746e5SOllivier Robert siglongjmp(env, 1);
392c0b746e5SOllivier Robert }
393c0b746e5SOllivier Robert #endif
394c0b746e5SOllivier Robert
395c0b746e5SOllivier Robert /*
396c0b746e5SOllivier Robert * Print a value a la the %b format of the kernel's printf
397c0b746e5SOllivier Robert */
3982b15cb3dSCy Schubert const char *
sprintb(u_int v,const char * bits)399c0b746e5SOllivier Robert sprintb(
4002b15cb3dSCy Schubert u_int v,
4012b15cb3dSCy Schubert const char * bits
402c0b746e5SOllivier Robert )
403c0b746e5SOllivier Robert {
4042b15cb3dSCy Schubert char *cp;
4052b15cb3dSCy Schubert char *cplim;
4062b15cb3dSCy Schubert int i;
4072b15cb3dSCy Schubert int any;
4082b15cb3dSCy Schubert char c;
409c0b746e5SOllivier Robert static char buf[132];
410c0b746e5SOllivier Robert
4112b15cb3dSCy Schubert if (bits != NULL && *bits == 8)
4122b15cb3dSCy Schubert snprintf(buf, sizeof(buf), "0%o", v);
413c0b746e5SOllivier Robert else
4142b15cb3dSCy Schubert snprintf(buf, sizeof(buf), "0x%x", v);
415c0b746e5SOllivier Robert cp = buf + strlen(buf);
4162b15cb3dSCy Schubert cplim = buf + sizeof(buf);
4172b15cb3dSCy Schubert if (bits != NULL) {
418ea906c41SOllivier Robert bits++;
419c0b746e5SOllivier Robert *cp++ = ' ';
420c0b746e5SOllivier Robert *cp++ = '(';
4212b15cb3dSCy Schubert any = FALSE;
422c0b746e5SOllivier Robert while ((i = *bits++) != 0) {
423c0b746e5SOllivier Robert if (v & (1 << (i - 1))) {
4242b15cb3dSCy Schubert if (any) {
425c0b746e5SOllivier Robert *cp++ = ',';
4262b15cb3dSCy Schubert if (cp >= cplim)
4272b15cb3dSCy Schubert goto overrun;
4282b15cb3dSCy Schubert }
4292b15cb3dSCy Schubert any = TRUE;
4302b15cb3dSCy Schubert for (; (c = *bits) > 32; bits++) {
431c0b746e5SOllivier Robert *cp++ = c;
4322b15cb3dSCy Schubert if (cp >= cplim)
4332b15cb3dSCy Schubert goto overrun;
4342b15cb3dSCy Schubert }
4352b15cb3dSCy Schubert } else {
436c0b746e5SOllivier Robert for (; *bits > 32; bits++)
437c0b746e5SOllivier Robert continue;
438c0b746e5SOllivier Robert }
4392b15cb3dSCy Schubert }
440c0b746e5SOllivier Robert *cp++ = ')';
4412b15cb3dSCy Schubert if (cp >= cplim)
4422b15cb3dSCy Schubert goto overrun;
443c0b746e5SOllivier Robert }
444c0b746e5SOllivier Robert *cp = '\0';
4452b15cb3dSCy Schubert return buf;
4462b15cb3dSCy Schubert
4472b15cb3dSCy Schubert overrun:
4482b15cb3dSCy Schubert return "sprintb buffer too small";
449c0b746e5SOllivier Robert }
450c0b746e5SOllivier Robert
4512b15cb3dSCy Schubert const char * const timex_states[] = {
452c0b746e5SOllivier Robert "OK", "INS", "DEL", "OOP", "WAIT", "ERROR"
453c0b746e5SOllivier Robert };
454c0b746e5SOllivier Robert
455c0b746e5SOllivier Robert const char *
timex_state(int s)456c0b746e5SOllivier Robert timex_state(
4572b15cb3dSCy Schubert int s
458c0b746e5SOllivier Robert )
459c0b746e5SOllivier Robert {
460c0b746e5SOllivier Robert static char buf[32];
461c0b746e5SOllivier Robert
4622b15cb3dSCy Schubert if ((size_t)s < COUNTOF(timex_states))
4632b15cb3dSCy Schubert return timex_states[s];
4642b15cb3dSCy Schubert snprintf(buf, sizeof(buf), "TIME-#%d", s);
4652b15cb3dSCy Schubert return buf;
466c0b746e5SOllivier Robert }
467