2 * SPDX-License-Identifier: MIT
4 * Copyright (c) 1995 Ugen J.S.Antsilevich
6 * Redistribution and use in source forms, with and without modification,
7 * are permitted provided that this entire comment appears intact.
9 * Redistribution in binary form may occur without any restrictions.
10 * Obviously, it would be nice if you gave credit where credit is due
11 * but requiring it would be too onerous.
13 * This software is provided ``AS IS'' without any warranties of any kind.
18 #include <sys/cdefs.h>
19 __FBSDID("$FreeBSD$");
21 #include <sys/param.h>
22 #include <sys/fcntl.h>
23 #include <sys/filio.h>
24 #include <sys/snoop.h>
26 #include <sys/linker.h>
27 #include <sys/module.h>
43 #define MSG_INIT "Snoop started."
44 #define MSG_OFLOW "Snoop stopped due to overflow. Reconnecting."
45 #define MSG_CLOSED "Snoop stopped due to tty close. Reconnecting."
46 #define MSG_CHANGE "Snoop device change by user request."
47 #define MSG_NOWRITE "Snoop device change due to write failure."
49 #define DEV_NAME_LEN 1024 /* for /dev/ttyXX++ */
52 #define CHR_SWITCH 24 /* Ctrl+X */
53 #define CHR_CLEAR 23 /* Ctrl+V */
55 static void clear(void);
56 static void timestamp(const char *);
57 static void set_tty(void);
58 static void unset_tty(void);
59 static void fatal(int, const char *);
60 static int open_snp(void);
61 static void cleanup(int);
62 static void usage(void) __dead2;
63 static void setup_scr(void);
64 static void attach_snp(void);
65 static void detach_snp(void);
66 static void set_dev(const char *);
67 static void ask_dev(char *, const char *);
69 int opt_reconn_close = 0;
70 int opt_reconn_oflow = 0;
71 int opt_interactive = 1;
72 int opt_timestamp = 0;
74 int opt_no_switch = 0;
75 const char *opt_snpdev;
77 char dev_name[DEV_NAME_LEN];
79 int std_in = 0, std_out = 1;
83 char tbuf[1024], gbuf[1024];
90 tputs(gbuf, 1, putchar);
95 timestamp(const char *buf)
101 printf("\n---------------------------------------------\n");
103 strftime(btmp, 1024, "Time: %d %b %H:%M", localtime(&t));
104 printf("%s\n", btmp);
106 printf("---------------------------------------------\n");
116 ntty.c_lflag &= ~ICANON; /* disable canonical operation */
117 ntty.c_lflag &= ~ECHO;
119 ntty.c_lflag &= ~FLUSHO;
122 ntty.c_lflag &= ~PENDIN;
125 ntty.c_lflag &= ~IEXTEN;
127 ntty.c_cc[VMIN] = 1; /* minimum of one character */
128 ntty.c_cc[VTIME] = 0; /* timeout value */
130 ntty.c_cc[VINTR] = 07; /* ^G */
131 ntty.c_cc[VQUIT] = 07; /* ^G */
132 tcsetattr(std_in, TCSANOW, &ntty);
139 tcsetattr(std_in, TCSANOW, &otty);
143 fatal(int error, const char *buf)
148 errx(error, "fatal: %s", buf);
163 if (opt_snpdev == NULL)
164 f = open(_PATH_DEV "snp", mode);
166 f = open(opt_snpdev, mode);
168 fatal(EX_OSFILE, "cannot open snoop device");
174 cleanup(int signo __unused)
178 timestamp("Logging Exited.");
188 fprintf(stderr, "usage: watch [-ciotnW] [tty name]\n");
195 char *cbuf = gbuf, *term;
197 if (!opt_interactive)
199 if ((term = getenv("TERM")))
200 if (tgetent(tbuf, term) == 1)
201 if (tgetstr("cl", &cbuf))
213 ioctl(snp_io, SNPSTTY, &fd);
221 snp_tty = open(dev_name, O_RDONLY | O_NONBLOCK);
223 fatal(EX_DATAERR, "can't open device");
224 if (ioctl(snp_io, SNPSTTY, &snp_tty) != 0)
225 fatal(EX_UNAVAILABLE, "cannot attach to tty");
228 timestamp("Logging Started.");
232 set_dev(const char *name)
234 char buf[DEV_NAME_LEN];
237 if (strlen(name) > 5 && !strncmp(name, _PATH_DEV, sizeof _PATH_DEV - 1)) {
238 snprintf(buf, sizeof buf, "%s", name);
240 if (strlen(name) == 2)
241 sprintf(buf, "%s%s", _PATH_TTY, name);
243 sprintf(buf, "%s%s", _PATH_DEV, name);
246 if (*name == '\0' || stat(buf, &sb) < 0)
247 fatal(EX_DATAERR, "bad device name");
249 if ((sb.st_mode & S_IFMT) != S_IFCHR)
250 fatal(EX_DATAERR, "must be a character device");
252 strlcpy(dev_name, buf, sizeof(dev_name));
258 ask_dev(char *dbuf, const char *msg)
260 char buf[DEV_NAME_LEN];
269 printf("Enter device name [%s]:", dbuf);
271 printf("Enter device name:");
273 if (fgets(buf, DEV_NAME_LEN - 1, stdin)) {
275 if (buf[len - 1] == '\n')
277 if (buf[0] != '\0' && buf[0] != ' ')
286 main(int ac, char *av[])
288 int ch, res, rv, nread;
289 size_t b_size = MIN_SIZE;
290 char *buf, chb[READB_LEN];
293 (void) setlocale(LC_TIME, "");
300 while ((ch = getopt(ac, av, "Wciotnf:")) != -1)
306 opt_reconn_close = 1;
312 opt_reconn_oflow = 1;
328 tcgetattr(std_in, &otty);
330 if (modfind("snp") == -1)
331 if (kldload("snp") == -1 || modfind("snp") == -1)
332 warn("snp module not available");
334 signal(SIGINT, cleanup);
339 if (*(av += optind) == NULL) {
340 if (opt_interactive && !opt_no_switch)
341 ask_dev(dev_name, MSG_INIT);
343 fatal(EX_DATAERR, "no device name given");
345 strlcpy(dev_name, *av, sizeof(dev_name));
349 if (!(buf = (char *) malloc(b_size)))
350 fatal(EX_UNAVAILABLE, "malloc failed");
356 FD_SET(std_in, &fd_s);
357 FD_SET(snp_io, &fd_s);
358 res = select(snp_io + 1, &fd_s, NULL, NULL, NULL);
359 if (opt_interactive && FD_ISSET(std_in, &fd_s)) {
361 if ((res = ioctl(std_in, FIONREAD, &nread)) != 0)
362 fatal(EX_OSERR, "ioctl(FIONREAD)");
363 if (nread > READB_LEN)
365 rv = read(std_in, chb, nread);
366 if (rv == -1 || rv != nread)
367 fatal(EX_IOERR, "read (stdin) failed");
374 if (!opt_no_switch) {
376 ask_dev(dev_name, MSG_CHANGE);
382 rv = write(snp_io, chb, nread);
383 if (rv == -1 || rv != nread) {
388 ask_dev(dev_name, MSG_NOWRITE);
395 if (!FD_ISSET(snp_io, &fd_s))
398 if ((res = ioctl(snp_io, FIONREAD, &nread)) != 0)
399 fatal(EX_OSERR, "ioctl(FIONREAD)");
403 if (opt_reconn_oflow)
405 else if (opt_interactive && !opt_no_switch) {
406 ask_dev(dev_name, MSG_OFLOW);
413 if (opt_reconn_close)
415 else if (opt_interactive && !opt_no_switch) {
416 ask_dev(dev_name, MSG_CLOSED);
422 if (nread < (b_size / 2) && (b_size / 2) > MIN_SIZE) {
424 if (!(buf = (char *) malloc(b_size / 2)))
425 fatal(EX_UNAVAILABLE, "malloc failed");
428 if (nread > b_size) {
429 b_size = (nread % 2) ? (nread + 1) : (nread);
431 if (!(buf = (char *) malloc(b_size)))
432 fatal(EX_UNAVAILABLE, "malloc failed");
434 rv = read(snp_io, buf, nread);
435 if (rv == -1 || rv != nread)
436 fatal(EX_IOERR, "read failed");
437 rv = write(std_out, buf, nread);
438 if (rv == -1 || rv != nread)
439 fatal(EX_IOERR, "write failed");