watch.c revision 30773
1/*
2 * Copyright (c) 1995 Ugen J.S.Antsilevich
3 *
4 * Redistribution and use in source forms, with and without modification,
5 * are permitted provided that this entire comment appears intact.
6 *
7 * Redistribution in binary form may occur without any restrictions.
8 * Obviously, it would be nice if you gave credit where credit is due
9 * but requiring it would be too onerous.
10 *
11 * This software is provided ``AS IS'' without any warranties of any kind.
12 *
13 * Snoop stuff.
14 */
15
16#ifndef lint
17static const char rcsid[] =
18	"$Id$";
19#endif /* not lint */
20
21#include <sys/types.h>
22#include <sys/fcntl.h>
23#include <sys/filio.h>
24#include <sys/snoop.h>
25#include <sys/stat.h>
26
27#include <err.h>
28#include <locale.h>
29#include <signal.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <sysexits.h>
34#include <termcap.h>
35#include <termios.h>
36#include <unistd.h>
37
38#define MSG_INIT	"Snoop started."
39#define MSG_OFLOW	"Snoop stopped due to overflow. Reconnecting."
40#define MSG_CLOSED	"Snoop stopped due to tty close. Reconnecting."
41#define MSG_CHANGE	"Snoop device change by user request."
42#define MSG_NOWRITE	"Snoop device change due to write failure."
43
44
45#define DEV_NAME_LEN	1024	/* for /dev/ttyXX++ */
46#define MIN_SIZE	256
47
48#define CHR_SWITCH	24	/* Ctrl+X	 */
49#define CHR_CLEAR	23	/* Ctrl+V	 */
50
51
52int             opt_reconn_close = 0;
53int             opt_reconn_oflow = 0;
54int             opt_interactive = 1;
55int             opt_timestamp = 0;
56int		opt_write = 0;
57int		opt_no_switch = 0;
58
59char            dev_name[DEV_NAME_LEN];
60int             snp_io;
61dev_t		snp_tty;
62int             std_in = 0, std_out = 1;
63
64
65int             clear_ok = 0;
66struct termios  otty;
67char            tbuf[1024], buf[1024];
68
69
70void
71clear()
72{
73	if (clear_ok)
74		tputs(buf, 1, putchar);
75	fflush(stdout);
76}
77
78void
79timestamp(buf)
80	char           *buf;
81{
82	time_t          t;
83	char            btmp[1024];
84	clear();
85	printf("\n---------------------------------------------\n");
86	t = time(NULL);
87	strftime(btmp, 1024, "Time: %d %b %H:%M", localtime(&t));
88	printf("%s\n", btmp);
89	printf("%s\n", buf);
90	printf("---------------------------------------------\n");
91	fflush(stdout);
92}
93
94void
95set_tty()
96{
97	struct termios  ntty;
98
99	tcgetattr (std_in, &otty);
100	ntty = otty;
101	ntty.c_lflag &= ~ICANON;    /* disable canonical operation  */
102	ntty.c_lflag &= ~ECHO;
103#ifdef FLUSHO
104	ntty.c_lflag &= ~FLUSHO;
105#endif
106#ifdef PENDIN
107	ntty.c_lflag &= ~PENDIN;
108#endif
109#ifdef IEXTEN
110	ntty.c_lflag &= ~IEXTEN;
111#endif
112	ntty.c_cc[VMIN] = 1;        /* minimum of one character */
113	ntty.c_cc[VTIME] = 0;       /* timeout value        */
114
115	ntty.c_cc[VINTR] = 07;   /* ^G */
116	ntty.c_cc[VQUIT] = 07;   /* ^G */
117	tcsetattr (std_in, TCSANOW, &ntty);
118}
119
120void
121unset_tty()
122{
123	tcsetattr (std_in, TCSANOW, &otty);
124}
125
126
127void
128fatal(err, buf)
129	unsigned int   err;
130	char           *buf;
131{
132	unset_tty();
133	if (buf)
134		errx(err, "fatal: %s", buf);
135	else
136		exit(err);
137}
138
139int
140open_snp()
141{
142	char            snp[] = {"/dev/snpX"};
143	char            c;
144	int             f, mode;
145
146	if (opt_write)
147		mode = O_RDWR;
148	else
149		mode = O_RDONLY;
150
151	for (c = '0'; c <= '9'; c++) {
152		snp[8] = c;
153		if ((f = open(snp, mode)) < 0)
154			continue;
155		return f;
156	}
157	fatal(EX_OSFILE, "cannot open snoop device");
158	return (0);
159}
160
161
162void
163cleanup()
164{
165	if (opt_timestamp)
166		timestamp("Logging Exited.");
167	close(snp_io);
168	unset_tty();
169	exit(EX_OK);
170}
171
172
173static void
174usage()
175{
176	fprintf(stderr, "usage: watch [-ciotnW] [tty name]\n");
177	exit(EX_USAGE);
178}
179
180void
181setup_scr()
182{
183	char           *cbuf = buf, *term;
184	if (!opt_interactive)
185		return;
186	if ((term = getenv("TERM")))
187		if (tgetent(tbuf, term) == 1)
188			if (tgetstr("cl", &cbuf))
189				clear_ok = 1;
190	set_tty();
191	clear();
192}
193
194
195int
196ctoh(c)
197	char            c;
198{
199	if (c >= '0' && c <= '9')
200		return (int) (c - '0');
201
202	if (c >= 'a' && c <= 'f')
203		return (int) (c - 'a' + 10);
204
205	fatal(EX_DATAERR, "bad tty number");
206	return (0);
207}
208
209
210void
211detach_snp()
212{
213	dev_t		dev;
214
215	dev = -1;
216	ioctl(snp_io, SNPSTTY, &dev);
217}
218
219void
220attach_snp()
221{
222	if (ioctl(snp_io, SNPSTTY, &snp_tty) != 0)
223		fatal(EX_UNAVAILABLE, "cannot attach to tty");
224	if (opt_timestamp)
225		timestamp("Logging Started.");
226}
227
228
229void
230set_dev(name)
231	char           *name;
232{
233	char            buf[DEV_NAME_LEN];
234	struct stat	sb;
235
236	if (strlen(name) > 5 && !strncmp(name, "/dev/", 5)) {
237		snprintf(buf, sizeof buf, "%s", name);
238	}
239	else {
240		if (strlen(name) == 2)
241			sprintf(buf, "/dev/tty%s", name);
242		else
243			sprintf(buf, "/dev/%s", name);
244	}
245
246	if (*name == '\0' || stat(buf, &sb) < 0)
247		fatal(EX_DATAERR, "bad device name");
248
249	if ((sb.st_mode & S_IFMT) != S_IFCHR)
250		fatal(EX_DATAERR, "must be a character device");
251
252	snp_tty = sb.st_rdev;
253	attach_snp();
254}
255
256void
257ask_dev(dev_name, msg)
258	char           *dev_name, *msg;
259{
260	char            buf[DEV_NAME_LEN];
261	int             len;
262
263	clear();
264	unset_tty();
265
266	if (msg)
267		printf("%s\n", msg);
268	if (dev_name)
269		printf("Enter device name [%s]:", dev_name);
270	else
271		printf("Enter device name:");
272
273	if (fgets(buf, DEV_NAME_LEN - 1, stdin)) {
274		len = strlen(buf);
275		if (buf[len - 1] == '\n')
276			buf[len - 1] = '\0';
277		if (buf[0] != '\0' && buf[0] != ' ')
278			strcpy(dev_name, buf);
279	}
280	set_tty();
281}
282
283#define READB_LEN	5
284
285void
286main(ac, av)
287	int             ac;
288	char          **av;
289{
290	int             res, nread, b_size = MIN_SIZE;
291	extern int      optind;
292	char            ch, *buf, chb[READB_LEN];
293	fd_set          fd_s;
294
295	(void) setlocale(LC_TIME, "");
296
297	if (isatty(std_out))
298		opt_interactive = 1;
299	else
300		opt_interactive = 0;
301
302
303	while ((ch = getopt(ac, av, "Wciotn")) != -1)
304		switch (ch) {
305		case 'W':
306			opt_write = 1;
307			break;
308		case 'c':
309			opt_reconn_close = 1;
310			break;
311		case 'i':
312			opt_interactive = 1;
313			break;
314		case 'o':
315			opt_reconn_oflow = 1;
316			break;
317		case 't':
318			opt_timestamp = 1;
319			break;
320		case 'n':
321			opt_no_switch = 1;
322			break;
323		case '?':
324		default:
325			usage();
326		}
327
328	signal(SIGINT, cleanup);
329
330	setup_scr();
331	snp_io = open_snp();
332
333	if (*(av += optind) == NULL) {
334		if (opt_interactive && !opt_no_switch)
335			ask_dev(dev_name, MSG_INIT);
336		else
337			fatal(EX_DATAERR, "no device name given");
338	} else
339		strncpy(dev_name, *av, DEV_NAME_LEN);
340
341	set_dev(dev_name);
342
343	if (!(buf = (char *) malloc(b_size)))
344		fatal(EX_UNAVAILABLE, "malloc failed");
345
346	FD_ZERO(&fd_s);
347
348	while (1) {
349		if (opt_interactive)
350			FD_SET(std_in, &fd_s);
351		FD_SET(snp_io, &fd_s);
352		res = select(snp_io + 1, &fd_s, NULL, NULL, NULL);
353		if (opt_interactive && FD_ISSET(std_in, &fd_s)) {
354
355			if ((res = ioctl(std_in, FIONREAD, &nread)) != 0)
356				fatal(EX_OSERR, "ioctl(FIONREAD)");
357			if (nread > READB_LEN)
358				nread = READB_LEN;
359			if (read(std_in,chb,nread)!=nread)
360				fatal(EX_IOERR, "read (stdin) failed");
361
362			switch (chb[0]) {
363			case CHR_CLEAR:
364				clear();
365				break;
366			case CHR_SWITCH:
367				if (opt_no_switch)
368					break;
369				detach_snp();
370				ask_dev(dev_name, MSG_CHANGE);
371				set_dev(dev_name);
372				break;
373			default:
374				if (opt_write) {
375					if (write(snp_io,chb,nread) != nread) {
376						detach_snp();
377						if (opt_no_switch)
378							fatal(EX_IOERR, "write failed");
379						ask_dev(dev_name, MSG_NOWRITE);
380						set_dev(dev_name);
381					}
382				}
383
384			}
385		}
386		if (!FD_ISSET(snp_io, &fd_s))
387			continue;
388
389		if ((res = ioctl(snp_io, FIONREAD, &nread)) != 0)
390			fatal(EX_OSERR, "ioctl(FIONREAD)");
391
392		switch (nread) {
393		case SNP_OFLOW:
394			if (opt_reconn_oflow)
395				attach_snp();
396			else if (opt_interactive && !opt_no_switch) {
397				ask_dev(dev_name, MSG_OFLOW);
398				set_dev(dev_name);
399			} else
400				cleanup();
401		case SNP_DETACH:
402		case SNP_TTYCLOSE:
403			if (opt_reconn_close)
404				attach_snp();
405			else if (opt_interactive && !opt_no_switch) {
406				ask_dev(dev_name, MSG_CLOSED);
407				set_dev(dev_name);
408			} else
409				cleanup();
410		default:
411			if (nread < (b_size / 2) && (b_size / 2) > MIN_SIZE) {
412				free(buf);
413				if (!(buf = (char *) malloc(b_size / 2)))
414					fatal(EX_UNAVAILABLE, "malloc failed");
415				b_size = b_size / 2;
416			}
417			if (nread > b_size) {
418				b_size = (nread % 2) ? (nread + 1) : (nread);
419				free(buf);
420				if (!(buf = (char *) malloc(b_size)))
421					fatal(EX_UNAVAILABLE, "malloc failed");
422			}
423			if (read(snp_io, buf, nread) < nread)
424				fatal(EX_IOERR, "read failed");
425			if (write(std_out, buf, nread) < nread)
426				fatal(EX_IOERR, "write failed");
427		}
428	}			/* While */
429}
430
431