2 * Copyright (c) 1995 Ugen J.S.Antsilevich
4 * Redistribution and use in source forms, with and without modification,
5 * are permitted provided that this entire comment appears intact.
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.
11 * This software is provided ``AS IS'' without any warranties of any kind.
16 #include <sys/cdefs.h>
17 __FBSDID("$FreeBSD$");
19 #include <sys/param.h>
20 #include <sys/fcntl.h>
21 #include <sys/filio.h>
22 #include <sys/snoop.h>
24 #include <sys/linker.h>
25 #include <sys/module.h>
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."
48 #define DEV_NAME_LEN 1024 /* for /dev/ttyXX++ */
51 #define CHR_SWITCH 24 /* Ctrl+X */
52 #define CHR_CLEAR 23 /* Ctrl+V */
54 static void clear(void);
55 static void timestamp(const char *);
56 static void set_tty(void);
57 static void unset_tty(void);
58 static void fatal(int, const char *);
59 static int open_snp(void);
60 static void cleanup(int);
61 static void usage(void) __dead2;
62 static void setup_scr(void);
63 static void attach_snp(void);
64 static void detach_snp(void);
65 static void set_dev(const char *);
66 static void ask_dev(char *, const char *);
68 int opt_reconn_close = 0;
69 int opt_reconn_oflow = 0;
70 int opt_interactive = 1;
71 int opt_timestamp = 0;
73 int opt_no_switch = 0;
74 const char *opt_snpdev;
76 char dev_name[DEV_NAME_LEN];
79 int std_in = 0, std_out = 1;
84 char tbuf[1024], gbuf[1024];
91 tputs(gbuf, 1, putchar);
96 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");
115 tcgetattr (std_in, &otty);
117 ntty.c_lflag &= ~ICANON; /* disable canonical operation */
118 ntty.c_lflag &= ~ECHO;
120 ntty.c_lflag &= ~FLUSHO;
123 ntty.c_lflag &= ~PENDIN;
126 ntty.c_lflag &= ~IEXTEN;
128 ntty.c_cc[VMIN] = 1; /* minimum of one character */
129 ntty.c_cc[VTIME] = 0; /* timeout value */
131 ntty.c_cc[VINTR] = 07; /* ^G */
132 ntty.c_cc[VQUIT] = 07; /* ^G */
133 tcsetattr (std_in, TCSANOW, &ntty);
139 tcsetattr (std_in, TCSANOW, &otty);
144 fatal(int error, const char *buf)
148 errx(error, "fatal: %s", buf);
156 char snp[] = {_PATH_DEV "snpX"};
160 pos = strlen(snp) - 1;
166 if (opt_snpdev == NULL)
167 for (c = '0'; c <= '9'; c++) {
169 if ((f = open(snp, mode)) < 0) {
172 err(1, "open %s", snp);
177 if ((f = open(opt_snpdev, mode)) != -1)
179 fatal(EX_OSFILE, "cannot open snoop device");
185 cleanup(int signo __unused)
188 timestamp("Logging Exited.");
198 fprintf(stderr, "usage: watch [-ciotnW] [tty name]\n");
205 char *cbuf = gbuf, *term;
206 if (!opt_interactive)
208 if ((term = getenv("TERM")))
209 if (tgetent(tbuf, term) == 1)
210 if (tgetstr("cl", &cbuf))
222 ioctl(snp_io, SNPSTTY, &dev);
228 if (ioctl(snp_io, SNPSTTY, &snp_tty) != 0)
229 fatal(EX_UNAVAILABLE, "cannot attach to tty");
231 timestamp("Logging Started.");
236 set_dev(const char *name)
238 char buf[DEV_NAME_LEN];
241 if (strlen(name) > 5 && !strncmp(name, _PATH_DEV, sizeof _PATH_DEV - 1)) {
242 snprintf(buf, sizeof buf, "%s", name);
244 if (strlen(name) == 2)
245 sprintf(buf, "%s%s", _PATH_TTY, name);
247 sprintf(buf, "%s%s", _PATH_DEV, name);
250 if (*name == '\0' || stat(buf, &sb) < 0)
251 fatal(EX_DATAERR, "bad device name");
253 if ((sb.st_mode & S_IFMT) != S_IFCHR)
254 fatal(EX_DATAERR, "must be a character device");
256 snp_tty = sb.st_rdev;
261 ask_dev(char *dbuf, const char *msg)
263 char buf[DEV_NAME_LEN];
272 printf("Enter device name [%s]:", dbuf);
274 printf("Enter device name:");
276 if (fgets(buf, DEV_NAME_LEN - 1, stdin)) {
278 if (buf[len - 1] == '\n')
280 if (buf[0] != '\0' && buf[0] != ' ')
289 main(int ac, char *av[])
291 int ch, res, rv, nread;
292 size_t b_size = MIN_SIZE;
293 char *buf, chb[READB_LEN];
296 (void) setlocale(LC_TIME, "");
304 while ((ch = getopt(ac, av, "Wciotnf:")) != -1)
310 opt_reconn_close = 1;
316 opt_reconn_oflow = 1;
332 if (modfind("snp") == -1)
333 if (kldload("snp") == -1 || modfind("snp") == -1)
334 warn("snp module not available");
336 signal(SIGINT, cleanup);
341 if (*(av += optind) == NULL) {
342 if (opt_interactive && !opt_no_switch)
343 ask_dev(dev_name, MSG_INIT);
345 fatal(EX_DATAERR, "no device name given");
347 strncpy(dev_name, *av, DEV_NAME_LEN);
351 if (!(buf = (char *) malloc(b_size)))
352 fatal(EX_UNAVAILABLE, "malloc failed");
358 FD_SET(std_in, &fd_s);
359 FD_SET(snp_io, &fd_s);
360 res = select(snp_io + 1, &fd_s, NULL, NULL, NULL);
361 if (opt_interactive && FD_ISSET(std_in, &fd_s)) {
363 if ((res = ioctl(std_in, FIONREAD, &nread)) != 0)
364 fatal(EX_OSERR, "ioctl(FIONREAD)");
365 if (nread > READB_LEN)
367 rv = read(std_in, chb, nread);
368 if (rv == -1 || rv != nread)
369 fatal(EX_IOERR, "read (stdin) failed");
376 if (!opt_no_switch) {
378 ask_dev(dev_name, MSG_CHANGE);
384 rv = write(snp_io, chb, nread);
385 if (rv == -1 || rv != nread) {
390 ask_dev(dev_name, MSG_NOWRITE);
397 if (!FD_ISSET(snp_io, &fd_s))
400 if ((res = ioctl(snp_io, FIONREAD, &nread)) != 0)
401 fatal(EX_OSERR, "ioctl(FIONREAD)");
405 if (opt_reconn_oflow)
407 else if (opt_interactive && !opt_no_switch) {
408 ask_dev(dev_name, MSG_OFLOW);
415 if (opt_reconn_close)
417 else if (opt_interactive && !opt_no_switch) {
418 ask_dev(dev_name, MSG_CLOSED);
424 if (nread < (b_size / 2) && (b_size / 2) > MIN_SIZE) {
426 if (!(buf = (char *) malloc(b_size / 2)))
427 fatal(EX_UNAVAILABLE, "malloc failed");
430 if (nread > b_size) {
431 b_size = (nread % 2) ? (nread + 1) : (nread);
433 if (!(buf = (char *) malloc(b_size)))
434 fatal(EX_UNAVAILABLE, "malloc failed");
436 rv = read(snp_io, buf, nread);
437 if (rv == -1 || rv != nread)
438 fatal(EX_IOERR, "read failed");
439 rv = write(std_out, buf, nread);
440 if (rv == -1 || rv != nread)
441 fatal(EX_IOERR, "write failed");