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