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