summaryrefslogtreecommitdiffstats
path: root/contrib/ntp/clockstuff/clktest.c
blob: 04df4b9bb331f2ba9ecf6b5ab5c63ad658031931 (plain)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* clktest.c,v 3.1 1993/07/06 01:05:23 jbj Exp
 * clktest - test the clock line discipline
 *
 * usage: clktest -b bps -f -t timeo -s cmd -c char1 -a char2 /dev/whatever
 */

#include "clktest-opts.h"

#define	STREQ(a, b)	(*(a) == *(b) && strcmp((a), (b)) == 0)

#if defined(ULT_2_0_SUCKS)
#ifndef sigmask
#define	sigmask(m)	(1<<(m))
#endif
#endif

#ifndef STREAM
# ifndef CLKLDISC
    CLOCK_LINE_DISCIPLINE_NEEDED_BY_THIS_PROGRAM;
# endif
#else
# ifdef CLKLDISC
    ONLY_ONE_CLOCK_LINE_DISCIPLINE_FOR_THIS_PROGRAM;
# endif
#endif

/*
 * Mask for blocking SIGIO and SIGALRM
 */
#define	BLOCKSIGMASK	(sigmask(SIGIO)|sigmask(SIGALRM))

#define progname clktestOptions.pzProgName

struct timeval timeout = { 0 };
char *cmd = NULL;
int cmdlen;

#ifdef CLKLDISC
u_long magic1 = DEFMAGIC;
u_long magic2 = DEFMAGIC;
#endif

int speed = B9600;
int ttflags = RAW|EVENP|ODDP;

volatile int wasalarmed;
volatile int iosig;

struct timeval lasttv;

extern u_long ustotslo[];
extern u_long ustotsmid[];
extern u_long ustotshi[];

int alarming();
int ioready();

/*
 * main - parse arguments and handle options
 */
int
main(
	int argc,
	char *argv[]
	)
{
	int fd;
	struct sgttyb ttyb;
	struct itimerval itimer;

#ifdef STREAM
	magic[0] = 0;
#endif

	{
	    int ct = optionProcess( &clktestOptions, argc, argv );
	    if (HAVE_OPT(COMMAND) && (strlen(OPT_ARG(COMMAND)) == 0)) {
		fputs( "The command option string must not be empty\n", stderr );
		USAGE( EXIT_FAILURE );
	    }

	    if ((argc -= ct) != 1) {
		fputs( "Missing tty device name\n", stderr );
		USAGE( EXIT_FAILURE );
	    }
	    argv += ct;
	}
#ifdef STREAM
	if (!strlen(magic))
	    strcpy(magic,DEFMAGIC);
#endif

	fd = open(*argv, HAVE_OPT(TIMEOUT) ? O_RDWR : O_RDONLY, 0777);
	if (fd == -1) {
		fprintf(stderr, "%s: open(%s): ", progname, *argv);
		perror("");
		exit(1);
	}

	if (ioctl(fd, TIOCEXCL, (char *)0) < 0) {
		(void) fprintf(stderr, "%s: ioctl(TIOCEXCL): ", progname);
		perror("");
		exit(1);
	}

	/*
	 * If we have the clock discipline, set the port to raw.  Otherwise
	 * we run cooked.
	 */
	ttyb.sg_ispeed = ttyb.sg_ospeed = speed;
#ifdef CLKLDISC
	ttyb.sg_erase = (char)magic1;
	ttyb.sg_kill = (char)magic2;
#endif
	ttyb.sg_flags = (short)ttflags;
	if (ioctl(fd, TIOCSETP, (char *)&ttyb) < 0) {
		(void) fprintf(stderr, "%s: ioctl(TIOCSETP): ", progname);
		perror("");
		exit(1);
	}

	if (fcntl(fd, F_SETOWN, getpid()) == -1) {
		(void) fprintf(stderr, "%s: fcntl(F_SETOWN): ", progname);
		perror("");
		exit(1);
	}

#ifdef CLKLDISC
	{
		int ldisc;
		ldisc = CLKLDISC;
		if (ioctl(fd, TIOCSETD, (char *)&ldisc) < 0) {
			(void) fprintf(stderr, "%s: ioctl(TIOCSETD): ", progname);
			perror("");
			exit(1);
		}
	}
#endif
#ifdef STREAM
	if (ioctl(fd, I_POP, 0) >=0 ) ;
	if (ioctl(fd, I_PUSH, "clk") < 0) {
		(void) fprintf(stderr, "%s: ioctl(I_PUSH): ", progname);
		perror("");
		exit(1);
	}
	if (ioctl(fd, CLK_SETSTR, magic) < 0) {
		(void) fprintf(stderr, "%s: ioctl(CLK_SETSTR): ", progname);
		perror("");
		exit(1);
	}
#endif


	(void) gettimeofday(&lasttv, (struct timezone *)0);
	if (HAVE_OPT(TIMEOUT)) {
		/*
		 * set non-blocking, async I/O on the descriptor
		 */
		iosig = 0;
		(void) signal(SIGIO, ioready);
		if (fcntl(fd, F_SETFL, FNDELAY|FASYNC) < 0) {
			(void) fprintf(stderr, "%s: fcntl(F_SETFL): ",
				       progname);
			perror("");
			exit(1);
		}

		/*
		 * Set up the alarm interrupt.
		 */
		wasalarmed = 0;
		(void) signal(SIGALRM, alarming);
		timeout.tv_sec = OPT_VALUE_TIMEOUT;
		itimer.it_interval = itimer.it_value = timeout;
		setitimer(ITIMER_REAL, &itimer, (struct itimerval *)0);
		doboth(fd);
	}
	doioonly(fd);
}


/*
 * doboth - handle both I/O and alarms via SIGIO
 */
int
doboth(
	int fd
	)
{
	int n;
	int sawalarm;
	int sawiosig;
	int omask;
	fd_set fds;
	struct timeval tvzero;

	sawalarm = 0;
	sawiosig = 0;
	FD_ZERO(&fds);
	for (;;) {
		omask = sigblock(BLOCKSIGMASK);
		if (wasalarmed) {		/* alarmed? */
			sawalarm = 1;
			wasalarmed = 0;
		}
		if (iosig) {
			sawiosig = 1;
			iosig = 0;
		}

		if (!sawalarm && !sawiosig) {
			/*
			 * Nothing to do.  Wait for something.
			 */
			sigpause(omask);
			if (wasalarmed) {		/* alarmed? */
				sawalarm = 1;
				wasalarmed = 0;
			}
			if (iosig) {
				sawiosig = 1;
				iosig = 0;
			}
		}
		(void)sigsetmask(omask);

		if (sawiosig) {

			do {
				tvzero.tv_sec = tvzero.tv_usec = 0;
				FD_SET(fd, &fds);
				n = select(fd+1, &fds, (fd_set *)0,
					   (fd_set *)0, &tvzero);
				if (n > 0)
				    doio(fd);
			} while (n > 0);

			if (n == -1) {
				(void) fprintf(stderr, "%s: select: ",
					       progname);
				perror("");
				exit(1);
			}
			sawiosig = 0;
		}
		if (sawalarm) {
			doalarm(fd);
			sawalarm = 0;
		}
	}
}


/*
 * doioonly - do I/O.  This avoids the use of signals
 */
int
doioonly(
	int fd
	)
{
	int n;
	fd_set fds;

	FD_ZERO(&fds);
	for (;;) {
		FD_SET(fd, &fds);
		n = select(fd+1, &fds, (fd_set *)0, (fd_set *)0,
			   (struct timeval *)0);
		if (n > 0)
		    doio(fd);
	}
}


/*
 * doio - read a buffer full of stuff and print it out
 */
int
doio(
	int fd
	)
{
	register char *rp, *rpend;
	register char *cp;
	register int i;
	char raw[512];
	struct timeval tv, tvd;
	int rlen;
	int ind;
	char cooked[2049];
	static char *digits = "0123456789abcdef";

	rlen = read(fd, raw, sizeof(raw));
	if (rlen < 0) {
		(void) fprintf(stderr, "%s: read(): ", progname);
		perror("");
		return;
	}
	if (rlen == 0) {
		(void) printf("Zero length read\n");
		return;
	}

	cp = cooked;
	rp = raw;
	rpend = &raw[rlen];
	ind = 0;

	while (rp < rpend) {
		ind = 1;
		if (isprint(*rp))
		    *cp++ = *rp;
		else {
			*cp++ = '<';
			*cp++ = digits[((*rp)>>4) & 0xf];
			*cp++ = digits[*rp & 0xf];
			*cp++ = '>';
		}
		if (
#ifdef CLKLDISC
			(*rp == (char)magic1 || *rp == (char)magic2)
#else
			( strchr( magic, *rp) != NULL )
#endif
			) {
			rp++;
			ind = 0;
			*cp = '\0';
			if ((rpend - rp) < sizeof(struct timeval)) {
				(void)printf(
					"Too little data (%d): %s\n",
					rpend-rp, cooked);
				return;
			}

			tv.tv_sec = 0;
			for (i = 0; i < 4; i++) {
				tv.tv_sec <<= 8;
				tv.tv_sec |= ((long)*rp++) & 0xff;
			}
			tv.tv_usec = 0;
			for (i = 0; i < 4; i++) {
				tv.tv_usec <<= 8;
				tv.tv_usec |= ((long)*rp++) & 0xff;
			}

			tvd.tv_sec = tv.tv_sec - lasttv.tv_sec;
			tvd.tv_usec = tv.tv_usec - lasttv.tv_usec;
			if (tvd.tv_usec < 0) {
				tvd.tv_usec += 1000000;
				tvd.tv_sec--;
			}

			(void)printf("%lu.%06lu %lu.%06lu %s\n",
				     tv.tv_sec, tv.tv_usec, tvd.tv_sec, tvd.tv_usec,
				     cooked);
			lasttv = tv;
		} else {
			rp++;
		}
	}

	if (ind) {
		*cp = '\0';
		(void)printf("Incomplete data: %s\n", cooked);
	}
}


/*
 * doalarm - send a string out the port, if we have one.
 */
int
doalarm(
	int fd
	)
{
	int n;

	if (! HAVE_OPT(COMMAND))
	    return;

	n = write(fd, cmd, cmdlen);

	if (n < 0) {
		(void) fprintf(stderr, "%s: write(): ", progname);
		perror("");
	} else if (n < cmdlen) {
		(void) printf("Short write (%d bytes, should be %d)\n",
			      n, cmdlen);
	}
}


/*
 * alarming - receive alarm interupt
 */
void
alarming(void)
{
	wasalarmed = 1;
}

/*
 * ioready - handle SIGIO interrupt
 */
void
ioready(void)
{
	iosig = 1;
}
OpenPOWER on IntegriCloud