aboutsummaryrefslogtreecommitdiff
path: root/usr.sbin/xntpd/util/ntptime.c
blob: dc60d6fa10229ff1ef5566d57ad982d0572fc497 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * NTP test program
 *
 * This program tests to see if the NTP user interface routines
 * ntp_gettime() and ntp_adjtime() have been implemented in the kernel.
 * If so, each of these routines is called to display current timekeeping
 * data.
 *
 * For more information, see the README.kern file in the doc directory
 * of the xntp3 distribution.
 */
#include <stdio.h>
#include <ctype.h>
#include <sys/time.h>
#include <signal.h>
#include <errno.h>

#include "ntp_fp.h"
#include "ntp_unixtime.h"
#include "ntp_stdlib.h"

#ifndef	SYS_DECOSF1
#define BADCALL -1		/* this is supposed to be a bad syscall */
#endif /* SYS_DECOSF1 */

#ifdef KERNEL_PLL
#include <sys/timex.h>
#define ntp_gettime(t)  syscall(SYS_ntp_gettime, (t))
#define ntp_adjtime(t)  syscall(SYS_ntp_adjtime, (t))
#else /* KERNEL_PLL */
#include "ntp_timex.h"
#define	SYS_ntp_adjtime NTP_SYSCALL_ADJ
#define	SYS_ntp_gettime NTP_SYSCALL_GET
#endif /* KERNEL_PLL */

/*
 * Function prototypes
 */
extern int sigvec	P((int, struct sigvec *, struct sigvec *));
extern int syscall	P((int, void *, ...));
void pll_trap		P((void));

static struct sigvec newsigsys;	/* new sigvec status */
static struct sigvec sigsys;	/* current sigvec status */
static int pll_control;		/* (0) daemon, (1) kernel loop */

static char* progname;
static char optargs[] = "ce:f:hm:o:rs:t:";

void
main(argc, argv)
	int argc;
	char *argv[];
{
	extern int ntp_optind;
	extern char *ntp_optarg;
	int status;
	struct ntptimeval ntv;
	struct timex ntx, _ntx;
	int	times[20];
	double ftemp, gtemp;
	l_fp ts;
	int c;
	int errflg	= 0;
	int cost	= 0;
	int rawtime	= 0;

	memset((char *)&ntx, 0, sizeof(ntx));
        progname = argv[0];
	while ((c = ntp_getopt(argc, argv, optargs)) != EOF) switch (c) {
		case 'c':
			cost++;
			break;
		case 'e':
			ntx.mode |= ADJ_ESTERROR;
			ntx.esterror = atoi(ntp_optarg);
			break;
		case 'f':
			ntx.mode |= ADJ_FREQUENCY;
			ntx.frequency = (int) (atof(ntp_optarg)
					       * (1 << SHIFT_USEC));
			if (ntx.frequency < (-100 << SHIFT_USEC)
			||  ntx.frequency > ( 100 << SHIFT_USEC)) errflg++;
			break;
		case 'm':
			ntx.mode |= ADJ_MAXERROR;
			ntx.maxerror = atoi(ntp_optarg);
			break;
		case 'o':
			ntx.mode |= ADJ_OFFSET;
			ntx.offset = atoi(ntp_optarg);
			break;
		case 'r':
			rawtime++;
			break;
		case 's':
			ntx.mode |= ADJ_STATUS;
			ntx.status = atoi(ntp_optarg);
			if (ntx.status < 0 || ntx.status > 4) errflg++;
			break;
		case 't':
			ntx.mode |= ADJ_TIMECONST;
			ntx.time_constant = atoi(ntp_optarg);
			if (ntx.time_constant < 0 || ntx.time_constant > MAXTC)
				errflg++;
			break;
		default:
			errflg++;
	}
	if (errflg || (ntp_optind != argc)) {
		(void) fprintf(stderr,
			"usage: %s [-%s]\n\n\
	-c		display the time taken to call ntp_gettime (us)\n\
	-e esterror	estimate of the error (us)\n\
	-f frequency	Frequency error (-100 .. 100) (ppm)\n\
	-h		display this help info\n\
	-m maxerror	max possible error (us)\n\
	-o offset	current offset (ms)\n\
	-r		print the unix and NTP time raw\n\
	-s status	Set the status (0 .. 4)\n\
	-t timeconstant	log2 of PLL time constant (0 .. %d)\n",
		progname, optargs, MAXTC);
		exit(2);
	}


	/*
	 * Test to make sure the sigvec() works in case of invalid
	 * syscall codes.
	 */
	newsigsys.sv_handler = pll_trap;
	newsigsys.sv_mask = 0;
	newsigsys.sv_flags = 0;
	if (sigvec(SIGSYS, &newsigsys, &sigsys)) {
		perror("sigvec() fails to save SIGSYS trap");
		exit(1);
	}

#ifdef	BADCALL
	/*
	 * Make sure the trapcatcher works.
	 */
	pll_control = 1;
	(void)syscall(BADCALL, &ntv); /* dummy parameter f. ANSI compilers */
	if (pll_control)
		printf("sigvec() failed to catch an invalid syscall\n");
#endif

	if (cost) {
		for (c=0; c< sizeof times / sizeof times[0]; c++) {
			(void)ntp_gettime(&ntv);
			if (pll_control < 0) break;
			times[c] = ntv.time.tv_usec;
		}
		if (pll_control >= 0) {
			printf("[ usec %06d:", times[0]);
			for (c=1; c< sizeof times / sizeof times[0]; c++) printf(" %d", times[c] - times[c-1]);
			printf(" ]\n");
		}
	}
	(void)ntp_gettime(&ntv);
	_ntx.mode = 0;				/* Ensure nothing is set */
	(void)ntp_adjtime(&_ntx);
	if (pll_control < 0) {
		printf("NTP user interface routines are not configured in this kernel.\n");
		goto lexit;
	}

	/*
	 * Fetch timekeeping data and display.
	 */
	if ((status = ntp_gettime(&ntv)) < 0)
		perror("ntp_gettime() call fails");
	else {
		printf("ntp_gettime() returns code %d\n", status);
		TVTOTS(&ntv.time, &ts);
		ts.l_uf += TS_ROUNDBIT;		/* guaranteed not to overflow */
		ts.l_ui += JAN_1970;
		ts.l_uf &= TS_MASK;
		printf("  time: %s, (.%06d)\n",
		    prettydate(&ts), ntv.time.tv_usec);
		printf("  confidence interval: %ld usec, estimated error: %ld usec\n",
		    ntv.maxerror, ntv.esterror);
		if (rawtime) printf("  ntptime=%x.%x unixtime=%x.%06d %s",
			ts.l_ui, ts.l_uf,
			ntv.time.tv_sec, ntv.time.tv_usec,
			ctime(&ntv.time.tv_sec));
	}
	if ((status = ntp_adjtime(&ntx)) < 0) perror((errno == EPERM) ? 
		">> Must be root to set kernel values\n>> ntp_adjtime() call fails" :
		">> ntp_adjtime() call fails");
	else {
		printf("ntp_adjtime() returns code %d\n", status);
		ftemp = ntx.frequency;
		ftemp /= (1 << SHIFT_USEC);
		printf("  mode: %02x, offset: %ld usec, frequency:%8.3f ppm,\n",
		    ntx.mode, ntx.offset, ftemp);
		printf("  confidence interval: %ld usec, estimated error: %ld usec,\n",
		    ntx.maxerror, ntx.esterror);
		ftemp = ntx.tolerance;
		ftemp /= (1 << SHIFT_USEC);
		printf("  status: %d, time constant: %ld, precision: %ld usec, tolerance:%4.0f ppm\n",
		    ntx.status, ntx.time_constant, ntx.precision, ftemp);
		if (ntx.shift == 0)
			return;
		ftemp = ntx.ybar;
		ftemp /= (1 << SHIFT_USEC);
		gtemp = ntx.disp;
		gtemp /= (1 << SHIFT_USEC);
		printf("  pps frequency%8.3f ppm, pps dispersion:%8.3f ppm, interval:%4d sec,\n  intervals:%5ld, jitter exceeded:%4ld, dispersion exceeded:%4ld\n",
		    ftemp, gtemp, 1 << ntx.shift, ntx.calcnt, ntx.jitcnt,
		    ntx.discnt);
	}

	/*
	 * Put things back together the way we found them.
	 */
lexit:	if (sigvec(SIGSYS, &sigsys, (struct sigvec *)NULL)) {
		perror("sigvec() fails to restore SIGSYS trap");
		exit(1);
	}
	exit(0);
}

/*
 * pll1_trap - trap processor for undefined syscalls
 */
void
pll_trap()
{
	pll_control--;
}