capture.c 15.4 KB
Newer Older
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
/* 
 * File:	capture.c
 * Description: 
 * Author:	Leigh Stoller
 * 		Computer Science Dept.
 * 		University of Utah
 * Date:	27-Jul-92
 *
 * (c) Copyright 1992, 2000, University of Utah, all rights reserved.
 */

/*
 * Testbed note:  This code has developed over the last several
 * years in RCS.  This is an import of the current version of
 * capture from the /usr/src/utah RCS repository into the testbed,
 * with some new hacks to port it to Linux.
 *
 * - dga, 10/10/2000
 */

/*
 * A LITTLE hack to record output from a tty device to a file, and still
 * have it available to tip using a pty/tty pair.
 */
	
#include <sys/param.h>

#include <stdio.h>
#include <ctype.h>
#include <strings.h>
#include <syslog.h>
#ifdef HPBSD
#include <sgtty.h>
#else
#include <termios.h>
#endif
#include <errno.h>

#include <sys/param.h>
#include <sys/file.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/termios.h>

#define geterr(e)	strerror(e)

void quit(int);
void reinit(int);
52
53
void newrun(int);
void shutdown(int);
54
55
56
57
58
59
void cleanup(void);
void capture();
void usage();

#ifdef __linux__
#define _POSIX_VDISABLE '\0'
60
61
62
63
64
#define revoke(tty)	(0)
#endif

#ifdef HPBSD
#define TWOPROCESS	/* historic? */
65
66
67
68
69
70
71
72
73
74
75
76
77
#endif

/*
 *  Configurable things.
 */
#define DEVPATH		"/dev"
#ifdef HPBSD
#define LOGPATH		"/usr/adm/tiplogs"
#else
#define LOGPATH		"/var/log/tiplogs"
#endif
#define PIDNAME		"%s/%s.pid"
#define LOGNAME		"%s/%s.log"
78
#define RUNNAME		"%s/%s.run"
79
#define TTYNAME		"%s/tip/%s"
80
81
#define PTYNAME		"%s/tip/%s-pty"
#define DEVNAME		"%s/%s"
82
83
#define BUFSIZE		4096
#define DROP_THRESH	8192
84
85
86
87

char 	*Progname;
char 	*Pidname;
char	*Logname;
88
char	*Runname;
89
char	*Ttyname;
90
91
92
char	*Ptyname;
char	*Devname;
char	*Machine;
93
94
int	logfd, runfd, devfd, ptyfd;
int	hwflow = 0, speed = B9600, debug = 0, runfile = 0;
95
96
97
98
99
100
101
102
103
104
105
106
107

int
main(argc, argv)
	int argc;
	char **argv;
{
	char strbuf[MAXPATHLEN], *newstr();
	int flags, op, i;
	extern int optind;
	extern char *optarg;

	Progname = (Progname = rindex(argv[0], '/')) ? ++Progname : *argv;

108
	while ((op = getopt(argc, argv, "rds:H")) != EOF)
109
110
111
112
113
114
		switch (op) {

		case 'H':
			++hwflow;
			break;

115
116
117
118
119
120
121
122
		case 'd':
			debug++;
			break;

		case 'r':
			runfile++;
			break;

123
124
125
126
127
128
129
130
131
132
133
134
135
		case 's':
			if ((i = atoi(optarg)) == 0 ||
			    (speed = val2speed(i)) == 0)
				usage();
			break;
		}

	argc -= optind;
	argv += optind;

	if (argc != 2)
		usage();

136
137
138
139
140
#ifndef HPBSD
	if (!debug)
		(void)daemon(0, 0);
#endif

141
142
143
144
145
146
	Machine = argv[0];

	(void) sprintf(strbuf, PIDNAME, LOGPATH, argv[0]);
	Pidname = newstr(strbuf);
	(void) sprintf(strbuf, LOGNAME, LOGPATH, argv[0]);
	Logname = newstr(strbuf);
147
148
	(void) sprintf(strbuf, RUNNAME, LOGPATH, argv[0]);
	Runname = newstr(strbuf);
149
150
	(void) sprintf(strbuf, TTYNAME, DEVPATH, argv[0]);
	Ttyname = newstr(strbuf);
151
152
153
154
155
156
157
158
159
160
161
	(void) sprintf(strbuf, PTYNAME, DEVPATH, argv[0]);
	Ptyname = newstr(strbuf);
	(void) sprintf(strbuf, DEVNAME, DEVPATH, argv[1]);
	Devname = newstr(strbuf);

	openlog(Progname, LOG_PID, LOG_USER);
	dolog(LOG_NOTICE, "starting");

	signal(SIGINT, quit);
	signal(SIGTERM, quit);
	signal(SIGHUP, reinit);
162
163
164
	if (runfile)
		signal(SIGUSR1, newrun);
	signal(SIGUSR2, shutdown);
165
166

	/*
167
	 * Open up run/log file, console tty, and controlling pty.
168
169
170
	 */
	if ((logfd = open(Logname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
		die("open(%s) : %s", Logname, geterr(errno));
171
172
173
174
175
176
177
178
179
180
	if (chmod(Logname, 0640) < 0)
		die("chmod(%s) : %s", Logname, geterr(errno));

	if (runfile) {
		if ((runfd = open(Runname,O_WRONLY|O_CREAT|O_APPEND,0666)) < 0)
			die("open(%s) : %s", Runname, geterr(errno));
		if (chmod(Runname, 0640) < 0)
			die("chmod(%s) : %s", Runname, geterr(errno));
	}
	
181
182
183
184
185
186
	if ((ptyfd = open(Ptyname, O_RDWR, 0666)) < 0)
		die("open(%s) : %s", Ptyname, geterr(errno));
	if ((devfd = open(Devname, O_RDWR|O_NONBLOCK, 0666)) < 0)
		die("open(%s) : %s", Devname, geterr(errno));

	if (ioctl(devfd, TIOCEXCL, 0) < 0)
187
		warn("TIOCEXCL %s: %s", Devname, geterr(errno));
188
189
190
191
192
193
194
195
196
197

	writepid();
	rawmode(speed);

	capture();

	cleanup();
	exit(0);
}

198
199
200
#ifdef TWOPROCESS
int	pid;

201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
void
capture()
{
	flags = FNDELAY;
	(void) fcntl(ptyfd, F_SETFL, &flags);

	if (pid = fork())
		in();
	else
		out();
}

/*
 * Loop reading from the console device, writing data to log file and
 * to the pty for tip to pick up.
 */
in()
{
219
	char buf[BUFSIZE];
220
221
222
	int cc, omask;
	
	while (1) {
223
		if ((cc = read(devfd, buf, BUFSIZE)) < 0) {
224
225
226
227
228
			if ((errno == EWOULDBLOCK) || (errno == EINTR))
				continue;
			else
				die("read(%s) : %s", Devname, geterr(errno));
		}
229
230
		omask = sigblock(sigmask(SIGHUP)|sigmask(SIGTERM)|
				 sigmask(SIGUSR1)|sigmask(SIGUSR2));
231
232
233
234

		if (write(logfd, buf, cc) < 0)
			die("write(%s) : %s", Logname, geterr(errno));

235
236
237
238
239
		if (runfile) {
			if (write(runfd, buf, cc) < 0)
				die("write(%s) : %s", Runname, geterr(errno));
		}

240
241
242
243
244
245
246
247
248
249
250
251
252
		if (write(ptyfd, buf, cc) < 0) {
			if ((errno != EIO) && (errno != EWOULDBLOCK))
				die("write(%s) : %s", Ptyname, geterr(errno));
		}
		(void) sigsetmask(omask);
	}
}

/*
 * Loop reading input from pty (tip), and send off to the console device.
 */
out()
{
253
	char buf[BUFSIZE];
254
255
256
257
258
259
260
	int cc, omask;
	struct timeval timeout;

	timeout.tv_sec  = 0;
	timeout.tv_usec = 100000;
	
	while (1) {
261
		omask = sigblock(SIGUSR2);
262
		if ((cc = read(ptyfd, buf, BUFSIZE)) < 0) {
263
			(void) sigsetmask(omask);
264
265
266
267
268
269
270
271
			if ((errno == EIO) || (errno == EWOULDBLOCK) ||
			    (errno == EINTR)) {
				select(0, 0, 0, 0, &timeout);
				continue;
			}
			else
				die("read(%s) : %s", Ptyname, geterr(errno));
		}
272
273
274
275
		(void) sigsetmask(omask);

		omask = sigblock(sigmask(SIGHUP)|sigmask(SIGTERM)|
				 sigmask(SIGUSR1)|sigmask(SIGUSR2));
276
277
278
279
280
281
282
283
284
285
286
287

		if (write(devfd, buf, cc) < 0)
			die("write(%s) : %s", Devname, geterr(errno));
		
		(void) sigsetmask(omask);
	}
}
#else
void
capture()
{
	fd_set sfds, fds;
288
289
	int n, i, cc, lcc, omask;
	char buf[BUFSIZE];
290
	struct timeval timeout;
291
292
293
294
#ifdef LOG_DROPS
	int drop_topty_chars = 0;
	int drop_todev_chars = 0;
#endif
295
296

	timeout.tv_sec  = 0;
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
	timeout.tv_usec = 10000;	/* ~115 chars at 115.2 kbaud */

	/*
	 * XXX for now we make both directions non-blocking.  This is a
	 * quick hack to achieve the goal that capture never block
	 * uninterruptably for long periods of time (use threads).
	 * This has the unfortunate side-effect that we may drop chars
	 * from the perspective of the user (use threads).  A more exotic
	 * solution would be to poll the readiness of output (use threads)
	 * as well as input and not read from one end unless we can write
	 * the other (use threads).
	 *
	 * I keep thinking (use threads) that there is a better way to do
	 * this (use threads).  Hmm...
	 */
Mike Hibler's avatar
Mike Hibler committed
312
313
314
315
	if (fcntl(ptyfd, F_SETFL, O_NONBLOCK) < 0)
		die("fcntl(O_NONBLOCK): %s", Ptyname, geterr(errno));
	if (fcntl(devfd, F_SETFL, O_NONBLOCK) < 0)
		die("fcntl(O_NONBLOCK): %s", Devname, geterr(errno));
316
317
318
319
320
321
322
323
324

	n = devfd;
	if (devfd < ptyfd)
		n = ptyfd;
	++n;
	FD_ZERO(&sfds);
	FD_SET(devfd, &sfds);
	FD_SET(ptyfd, &sfds);
	for (;;) {
325
326
327
328
329
330
331
332
#ifdef LOG_DROPS
		if (drop_topty_chars >= DROP_THRESH) {
			warn("%d dev -> pty chars dropped", drop_topty_chars);
			drop_topty_chars = 0;
		}
		if (drop_todev_chars >= DROP_THRESH) {
			warn("%d pty -> dev chars dropped", drop_todev_chars);
			drop_todev_chars = 0;
333
		}
334
#endif
335
336
		fds = sfds;
		i = select(n, &fds, NULL, NULL, NULL);
337
338
339
340
341
342
343
		if (i < 0) {
			if (errno == EINTR) {
				warn("input select interrupted, continuing");
				continue;
			}
			die("select(%s): %s", Devname, geterr(errno));
		}
344
		if (i == 0) {
345
			warn("No fds ready!");
346
347
348
349
350
351
352
353
354
355
356
357
			sleep(1);
			continue;
		}
		if (FD_ISSET(devfd, &fds)) {
			errno = 0;
			cc = read(devfd, buf, sizeof(buf));
			if (cc < 0)
				die("read(%s) : %s", Devname, geterr(errno));
			if (cc == 0)
				die("read(%s) : EOF", Devname);
			errno = 0;

358
359
			omask = sigblock(sigmask(SIGHUP)|sigmask(SIGTERM)|
					 sigmask(SIGUSR1)|sigmask(SIGUSR2));
360
361
362
			for (lcc = 0; lcc < cc; lcc += i) {
				i = write(ptyfd, &buf[lcc], cc-lcc);
				if (i < 0) {
363
364
365
366
367
368
369
					/*
					 * Either tip is blocked (^S) or
					 * not running (the latter should
					 * return EIO but doesn't due to a
					 * pty bug).  Note that we have
					 * dropped some chars.
					 */
370
					if (errno == EIO || errno == EAGAIN) {
371
372
#ifdef LOG_DROPS
						drop_topty_chars += (cc-lcc);
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#endif
						goto dropped;
					}
					die("write(%s) : %s",
					    Ptyname, geterr(errno));
				}
				if (i == 0)
					die("write(%s) : zero-length", Ptyname);
			}
dropped:
			i = write(logfd, buf, cc);
			if (i < 0)
				die("write(%s) : %s", Logname, geterr(errno));
			if (i != cc)
				die("write(%s) : incomplete", Logname);
388
389
390
391
392
393
394
395
			if (runfile) {
				i = write(runfd, buf, cc);
				if (i < 0)
					die("write(%s) : %s",
					    Runname, geterr(errno));
				if (i != cc)
					die("write(%s) : incomplete", Runname);
			}
396
397
398
399
			(void) sigsetmask(omask);

		}
		if (FD_ISSET(ptyfd, &fds)) {
400
			omask = sigblock(sigmask(SIGUSR2));
401
402
			errno = 0;
			cc = read(ptyfd, buf, sizeof(buf), 0);
403
			(void) sigsetmask(omask);
404
405
406
407
408
409
410
411
412
413
414
415
			if (cc < 0) {
				/* XXX commonly observed */
				if (errno == EIO || errno == EAGAIN)
					continue;
				die("read(%s) : %s", Ptyname, geterr(errno));
			}
			if (cc == 0) {
				select(0, 0, 0, 0, &timeout);
				continue;
			}
			errno = 0;

416
417
			omask = sigblock(sigmask(SIGHUP)|sigmask(SIGTERM)|
					 sigmask(SIGUSR1));
418
419
420
			for (lcc = 0; lcc < cc; lcc += i) {
				i = write(devfd, &buf[lcc], cc-lcc);
				if (i < 0) {
421
422
423
424
					/*
					 * Device backed up (or FUBARed)
					 * Note that we dropped some chars.
					 */
425
					if (errno == EAGAIN) {
426
427
428
#ifdef LOG_DROPS
						drop_todev_chars += (cc-lcc);
#endif
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
						goto dropped2;
					}
					die("write(%s) : %s",
					    Devname, geterr(errno));
				}
				if (i == 0)
					die("write(%s) : zero-length", Devname);
			}
dropped2:
			(void) sigsetmask(omask);

		}
	}
}
#endif

/*
 * SIGHUP means we want to close the old log file (because it has probably
 * been moved) and start a new version of it.
 */
void
reinit(int sig)
{
	/*
	 * We know that the any pending write to the log file completed
	 * because we blocked SIGHUP during the write.
	 */
	close(logfd);
	
	if ((logfd = open(Logname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
		die("open(%s) : %s", Logname, geterr(errno));
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
	if (chmod(Logname, 0640) < 0)
		die("chmod(%s) : %s", Logname, geterr(errno));
	
	dolog(LOG_NOTICE, "new log started");

	if (runfile)
		newrun(sig);
}

/*
 * SIGUSR1 means we want to close the old run file (because it has probably
 * been moved) and start a new version of it.
 */
void
newrun(int sig)
{
	/*
	 * We know that the any pending write to the log file completed
	 * because we blocked SIGUSR1 during the write.
	 */
	close(runfd);
481

482
483
484
485
486
487
488
	if ((runfd = open(Runname, O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0)
		die("open(%s) : %s", Runname, geterr(errno));
	if (chmod(Runname, 0640) < 0)
		die("chmod(%s) : %s", Runname, geterr(errno));
	
	dolog(LOG_NOTICE, "new run started");
}
489

490
491
/*
 * SIGUSR2 means we want to revoke the other side of the pty to close the
492
493
494
 * tip down gracefully.  We flush all input/output pending on the pty,
 * do a revoke on the tty and then close and reopen the pty just to make
 * sure everyone is gone.
495
496
497
498
499
500
501
502
503
504
 */
void
shutdown(int sig)
{
	int ofd = ptyfd;
	
	/*
	 * We know that the any pending access to the pty completed
	 * because we blocked SIGUSR2 during the operation.
	 */
505
506
507
508
509
510
511
512
#ifdef HPBSD
	int zero = 0;
	ioctl(ptyfd, TIOCFLUSH, &zero);
#else
	tcflush(ptyfd, TCIOFLUSH);
#endif
	if (revoke(Ttyname) < 0)
		dolog(LOG_WARNING, "could not revoke access to tty");
513
514
515
516
	close(ptyfd);
	
	if ((ptyfd = open(Ptyname, O_RDWR, 0666)) < 0)
		die("open(%s) : %s", Ptyname, geterr(errno));
517
518
519
520
521
522
523
524
525
526

	/* XXX so we don't have to recompute the select mask */
	if (ptyfd != ofd) {
		dup2(ptyfd, ofd);
		close(ptyfd);
		ptyfd = ofd;
	}

	if (fcntl(ptyfd, F_SETFL, O_NONBLOCK) < 0)
		die("fcntl(O_NONBLOCK): %s", Ptyname, geterr(errno));
527
528
	
	dolog(LOG_NOTICE, "pty reset");
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
}

/*
 *  Display proper usage / error message and exit.
 */
void
usage()
{
	fprintf(stderr, "usage: %s machine tty\n", Progname);
	exit(1);
}

warn(format, arg0, arg1, arg2, arg3)
	char *format, *arg0, *arg1, *arg2, *arg3;
{
544
	char msgbuf[BUFSIZE];
545
546

	sprintf(msgbuf, format, arg0, arg1, arg2, arg3);
547
	dolog(LOG_WARNING, msgbuf);
548
549
550
551
552
}

die(format, arg0, arg1, arg2, arg3)
	char *format, *arg0, *arg1, *arg2, *arg3;
{
553
	char msgbuf[BUFSIZE];
554
555
556
557
558
559
560
561

	sprintf(msgbuf, format, arg0, arg1, arg2, arg3);
	dolog(LOG_ERR, msgbuf);
	quit(0);
}

dolog(level, msg)
{
562
	char msgbuf[BUFSIZE];
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577

	sprintf(msgbuf, "%s - %s", Machine, msg);
	syslog(level, msgbuf);
}

void
quit(int sig)
{
	cleanup();
	exit(1);
}

void
cleanup()
{
578
579
	dolog(LOG_NOTICE, "exiting");
#ifdef TWOPROCESS
580
581
	if (pid)
		(void) kill(pid, SIGTERM);
582
#endif
583
584
585
586
587
588
589
590
591
592
	(void) unlink(Pidname);
}

char *
newstr(str)
	char *str;
{
	char *malloc();
	register char *np;

593
594
	if ((np = malloc((unsigned) strlen(str) + 1)) == NULL)
		die("malloc: out of memory");
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625

	return(strcpy(np, str));
}

#if 0
char *
geterr(errindx)
int errindx;
{
	extern int sys_nerr;
	extern char *sys_errlist[];
	char syserr[25];

	if (errindx < sys_nerr)
		return(sys_errlist[errindx]);

	(void) sprintf(syserr, "unknown error (%d)", errindx);
	return(syserr);
}
#endif

/*
 * Open up PID file and write our pid into it.
 */
writepid()
{
	int fd;
	char buf[8];
	
	if ((fd = open(Pidname, O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0)
		die("open(%s) : %s", Pidname, geterr(errno));
626
627
628

	if (chmod(Pidname, 0644) < 0)
		die("chmod(%s) : %s", Pidname, geterr(errno));
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
	
	(void) sprintf(buf, "%d\n", getpid());
	
	if (write(fd, buf, strlen(buf)) < 0)
		die("write(%s) : %s", Pidname, geterr(errno));
	
	(void) close(fd);
}

/*
 * Put the console line into raw mode.
 */
rawmode(speed)
int speed;
{
#ifdef HPBSD
	struct sgttyb sgbuf;
	int bits;
	
	if (ioctl(devfd, TIOCGETP, (char *)&sgbuf) < 0)
		die("TIOCGETP(%s) : %s", Device, geterr(errno));
	sgbuf.sg_ispeed = sgbuf.sg_ospeed = speed;
	sgbuf.sg_flags = RAW|TANDEM;
	bits = LDECCTQ;
	if (ioctl(devfd, TIOCSETP, (char *)&sgbuf) < 0)
		die("TIOCSETP(%s) : %s", Device, geterr(errno));
	if (ioctl(devfd, TIOCLBIS, (char *)&bits) < 0)
		die("TIOCLBIS(%s) : %s", Device, geterr(errno));
#else
	struct termios t;

	if (tcgetattr(devfd, &t) < 0)
		die("tcgetattr(%s) : %s", Devname, geterr(errno));
	(void) cfsetispeed(&t, speed);
	(void) cfsetospeed(&t, speed);
	cfmakeraw(&t);
	t.c_cflag |= CLOCAL;
	if (hwflow)
#ifdef __linux__
	        t.c_cflag |= CRTSCTS;
#else
		t.c_cflag |= CCTS_OFLOW | CRTS_IFLOW;
#endif
	t.c_cc[VSTART] = t.c_cc[VSTOP] = _POSIX_VDISABLE;
	if (tcsetattr(devfd, TCSAFLUSH, &t) < 0)
		die("tcsetattr(%s) : %s", Devname, geterr(errno));
#endif
}

/*
 * From kgdbtunnel
 */
static struct speeds {
	int speed;		/* symbolic speed */
	int val;		/* numeric value */
} speeds[] = {
#ifdef B50
	{ B50,	50 },
#endif
#ifdef B75
	{ B75,	75 },
#endif
#ifdef B110
	{ B110,	110 },
#endif
#ifdef B134
	{ B134,	134 },
#endif
#ifdef B150
	{ B150,	150 },
#endif
#ifdef B200
	{ B200,	200 },
#endif
#ifdef B300
	{ B300,	300 },
#endif
#ifdef B600
	{ B600,	600 },
#endif
#ifdef B1200
	{ B1200, 1200 },
#endif
#ifdef B1800
	{ B1800, 1800 },
#endif
#ifdef B2400
	{ B2400, 2400 },
#endif
#ifdef B4800
	{ B4800, 4800 },
#endif
#ifdef B7200
	{ B7200, 7200 },
#endif
#ifdef B9600
	{ B9600, 9600 },
#endif
#ifdef B14400
	{ B14400, 14400 },
#endif
#ifdef B19200
	{ B19200, 19200 },
#endif
#ifdef B38400
	{ B38400, 38400 },
#endif
#ifdef B28800
	{ B28800, 28800 },
#endif
#ifdef B57600
	{ B57600, 57600 },
#endif
#ifdef B76800
	{ B76800, 76800 },
#endif
#ifdef B115200
	{ B115200, 115200 },
#endif
#ifdef B230400
	{ B230400, 230400 },
#endif
};
#define NSPEEDS (sizeof(speeds) / sizeof(speeds[0]))

int
val2speed(val)
	register int val;
{
	register int n;
	register struct speeds *sp;

	for (sp = speeds, n = NSPEEDS; n > 0; ++sp, --n)
		if (val == sp->val)
			return (sp->speed);

	return (0);
}