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