watch.c revision 98140
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 98140 2002-06-12 04:28:15Z imp $";
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 <time.h>
40#include <unistd.h>
41
42#define MSG_INIT	"Snoop started."
43#define MSG_OFLOW	"Snoop stopped due to overflow. Reconnecting."
44#define MSG_CLOSED	"Snoop stopped due to tty close. Reconnecting."
45#define MSG_CHANGE	"Snoop device change by user request."
46#define MSG_NOWRITE	"Snoop device change due to write failure."
47
48
49#define DEV_NAME_LEN	1024	/* for /dev/ttyXX++ */
50#define MIN_SIZE	256
51
52#define CHR_SWITCH	24	/* Ctrl+X	 */
53#define CHR_CLEAR	23	/* Ctrl+V	 */
54
55static void	clear(void);
56static void	timestamp(const char *);
57static void	set_tty(void);
58static void	unset_tty(void);
59static void	fatal(int, const char *);
60static int	open_snp(void);
61static void	cleanup(int);
62static void	usage(void) __dead2;
63static void	setup_scr(void);
64static void	attach_snp(void);
65static void	detach_snp(void);
66static void	set_dev(const char *);
67static void	ask_dev(char *, const char *);
68
69int             opt_reconn_close = 0;
70int             opt_reconn_oflow = 0;
71int             opt_interactive = 1;
72int             opt_timestamp = 0;
73int		opt_write = 0;
74int		opt_no_switch = 0;
75const char	*opt_snpdev;
76
77char            dev_name[DEV_NAME_LEN];
78int             snp_io;
79dev_t		snp_tty;
80int             std_in = 0, std_out = 1;
81
82
83int             clear_ok = 0;
84struct termios  otty;
85char            tbuf[1024], gbuf[1024];
86
87
88static void
89clear(void)
90{
91	if (clear_ok)
92		tputs(gbuf, 1, putchar);
93	fflush(stdout);
94}
95
96static void
97timestamp(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(void)
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(void)
139{
140	tcsetattr (std_in, TCSANOW, &otty);
141}
142
143
144static void
145fatal(int error, const char *buf)
146{
147	unset_tty();
148	if (buf)
149		errx(error, "fatal: %s", buf);
150	else
151		exit(error);
152}
153
154static int
155open_snp(void)
156{
157	char            snp[] = {_PATH_DEV "snpX"};
158	char            c;
159	int             f, mode;
160
161	if (opt_write)
162		mode = O_RDWR;
163	else
164		mode = O_RDONLY;
165
166	if (opt_snpdev == NULL)
167		for (c = '0'; c <= '9'; c++) {
168			snp[8] = c;
169			if ((f = open(snp, mode)) < 0)
170				continue;
171			return f;
172		}
173	else
174		if ((f = open(opt_snpdev, mode)) != -1)
175			return (f);
176	fatal(EX_OSFILE, "cannot open snoop device");
177	return (0);
178}
179
180
181static void
182cleanup(int signo __unused)
183{
184	if (opt_timestamp)
185		timestamp("Logging Exited.");
186	close(snp_io);
187	unset_tty();
188	exit(EX_OK);
189}
190
191
192static void
193usage(void)
194{
195	fprintf(stderr, "usage: watch [-ciotnW] [tty name]\n");
196	exit(EX_USAGE);
197}
198
199static void
200setup_scr(void)
201{
202	char           *cbuf = gbuf, *term;
203	if (!opt_interactive)
204		return;
205	if ((term = getenv("TERM")))
206		if (tgetent(tbuf, term) == 1)
207			if (tgetstr("cl", &cbuf))
208				clear_ok = 1;
209	set_tty();
210	clear();
211}
212
213static void
214detach_snp(void)
215{
216	dev_t		dev;
217
218	dev = NODEV;
219	ioctl(snp_io, SNPSTTY, &dev);
220}
221
222static void
223attach_snp(void)
224{
225	if (ioctl(snp_io, SNPSTTY, &snp_tty) != 0)
226		fatal(EX_UNAVAILABLE, "cannot attach to tty");
227	if (opt_timestamp)
228		timestamp("Logging Started.");
229}
230
231
232static void
233set_dev(const char *name)
234{
235	char            buf[DEV_NAME_LEN];
236	struct stat	sb;
237
238	if (strlen(name) > 5 && !strncmp(name, _PATH_DEV, sizeof _PATH_DEV - 1)) {
239		snprintf(buf, sizeof buf, "%s", name);
240	} else {
241		if (strlen(name) == 2)
242			sprintf(buf, "%s%s", _PATH_TTY, name);
243		else
244			sprintf(buf, "%s%s", _PATH_DEV, name);
245	}
246
247	if (*name == '\0' || stat(buf, &sb) < 0)
248		fatal(EX_DATAERR, "bad device name");
249
250	if ((sb.st_mode & S_IFMT) != S_IFCHR)
251		fatal(EX_DATAERR, "must be a character device");
252
253	snp_tty = sb.st_rdev;
254	attach_snp();
255}
256
257void
258ask_dev(char *dbuf, const char *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 (dbuf)
269		printf("Enter device name [%s]:", dbuf);
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(dbuf, buf);
279	}
280	set_tty();
281}
282
283#define READB_LEN	5
284
285int
286main(int ac, char *av[])
287{
288	int             res, idata, rv;
289	size_t		nread, b_size = MIN_SIZE;
290	char            ch, *buf, chb[READB_LEN];
291	fd_set          fd_s;
292
293	(void) setlocale(LC_TIME, "");
294
295	if (isatty(std_out))
296		opt_interactive = 1;
297	else
298		opt_interactive = 0;
299
300
301	while ((ch = getopt(ac, av, "Wciotnf:")) != -1)
302		switch (ch) {
303		case 'W':
304			opt_write = 1;
305			break;
306		case 'c':
307			opt_reconn_close = 1;
308			break;
309		case 'i':
310			opt_interactive = 1;
311			break;
312		case 'o':
313			opt_reconn_oflow = 1;
314			break;
315		case 't':
316			opt_timestamp = 1;
317			break;
318		case 'n':
319			opt_no_switch = 1;
320			break;
321		case 'f':
322			opt_snpdev = optarg;
323			break;
324		case '?':
325		default:
326			usage();
327		}
328
329	if (modfind("snp") == -1)
330		if (kldload("snp") == -1 || modfind("snp") == -1)
331			warn("snp module not available");
332
333	signal(SIGINT, cleanup);
334
335	setup_scr();
336	snp_io = open_snp();
337
338	if (*(av += optind) == NULL) {
339		if (opt_interactive && !opt_no_switch)
340			ask_dev(dev_name, MSG_INIT);
341		else
342			fatal(EX_DATAERR, "no device name given");
343	} else
344		strncpy(dev_name, *av, DEV_NAME_LEN);
345
346	set_dev(dev_name);
347
348	if (!(buf = (char *) malloc(b_size)))
349		fatal(EX_UNAVAILABLE, "malloc failed");
350
351	FD_ZERO(&fd_s);
352
353	while (1) {
354		if (opt_interactive)
355			FD_SET(std_in, &fd_s);
356		FD_SET(snp_io, &fd_s);
357		res = select(snp_io + 1, &fd_s, NULL, NULL, NULL);
358		if (opt_interactive && FD_ISSET(std_in, &fd_s)) {
359
360			if ((res = ioctl(std_in, FIONREAD, &nread)) != 0)
361				fatal(EX_OSERR, "ioctl(FIONREAD)");
362			if (nread > READB_LEN)
363				nread = READB_LEN;
364			rv = read(std_in, chb, nread);
365			if (rv == -1 || (unsigned)rv != nread)
366				fatal(EX_IOERR, "read (stdin) failed");
367
368			switch (chb[0]) {
369			case CHR_CLEAR:
370				clear();
371				break;
372			case CHR_SWITCH:
373				if (!opt_no_switch) {
374					detach_snp();
375					ask_dev(dev_name, MSG_CHANGE);
376					set_dev(dev_name);
377					break;
378				}
379			default:
380				if (opt_write) {
381					rv = write(snp_io, chb, nread);
382					if (rv == -1 || (unsigned)rv != nread) {
383						detach_snp();
384						if (opt_no_switch)
385							fatal(EX_IOERR,
386							  "write failed");
387						ask_dev(dev_name, MSG_NOWRITE);
388						set_dev(dev_name);
389					}
390				}
391
392			}
393		}
394		if (!FD_ISSET(snp_io, &fd_s))
395			continue;
396
397		if ((res = ioctl(snp_io, FIONREAD, &idata)) != 0)
398			fatal(EX_OSERR, "ioctl(FIONREAD)");
399
400		switch (idata) {
401		case SNP_OFLOW:
402			if (opt_reconn_oflow)
403				attach_snp();
404			else if (opt_interactive && !opt_no_switch) {
405				ask_dev(dev_name, MSG_OFLOW);
406				set_dev(dev_name);
407			} else
408				cleanup(-1);
409		case SNP_DETACH:
410		case SNP_TTYCLOSE:
411			if (opt_reconn_close)
412				attach_snp();
413			else if (opt_interactive && !opt_no_switch) {
414				ask_dev(dev_name, MSG_CLOSED);
415				set_dev(dev_name);
416			} else
417				cleanup(-1);
418		default:
419			nread = (unsigned)idata;
420			if (nread < (b_size / 2) && (b_size / 2) > MIN_SIZE) {
421				free(buf);
422				if (!(buf = (char *) malloc(b_size / 2)))
423					fatal(EX_UNAVAILABLE, "malloc failed");
424				b_size = b_size / 2;
425			}
426			if (nread > b_size) {
427				b_size = (nread % 2) ? (nread + 1) : (nread);
428				free(buf);
429				if (!(buf = (char *) malloc(b_size)))
430					fatal(EX_UNAVAILABLE, "malloc failed");
431			}
432			rv = read(snp_io, buf, nread);
433			if (rv == -1 || (unsigned)rv != nread)
434				fatal(EX_IOERR, "read failed");
435			rv = write(std_out, buf, nread);
436			if (rv == -1 || (unsigned)rv != nread)
437				fatal(EX_IOERR, "write failed");
438		}
439	}			/* While */
440	return(0);
441}
442
443