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];
78 int std_in = 0, std_out = 1;
83 char tbuf[1024], gbuf[1024];
90 tputs(gbuf, 1, putchar);
95 timestamp(const char *buf)
100 printf("\n---------------------------------------------\n");
102 strftime(btmp, 1024, "Time: %d %b %H:%M", localtime(&t));
103 printf("%s\n", btmp);
105 printf("---------------------------------------------\n");
114 tcgetattr (std_in, &otty);
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);
138 tcsetattr (std_in, TCSANOW, &otty);
143 fatal(int error, const char *buf)
147 errx(error, "fatal: %s", buf);
155 char snp[] = {_PATH_DEV "snpX"};
159 pos = strlen(snp) - 1;
165 if (opt_snpdev == NULL)
166 for (c = '0'; c <= '9'; c++) {
168 if ((f = open(snp, mode)) < 0) {
171 err(1, "open %s", snp);
176 if ((f = open(opt_snpdev, mode)) != -1)
178 fatal(EX_OSFILE, "cannot open snoop device");
184 cleanup(int signo __unused)
187 timestamp("Logging Exited.");
197 fprintf(stderr, "usage: watch [-ciotnW] [tty name]\n");
204 char *cbuf = gbuf, *term;
205 if (!opt_interactive)
207 if ((term = getenv("TERM")))
208 if (tgetent(tbuf, term) == 1)
209 if (tgetstr("cl", &cbuf))
221 ioctl(snp_io, SNPSTTY, &fd);
229 snp_tty = open(dev_name, O_RDONLY | O_NONBLOCK);
231 fatal(EX_DATAERR, "can't open device");
232 if (ioctl(snp_io, SNPSTTY, &snp_tty) != 0)
233 fatal(EX_UNAVAILABLE, "cannot attach to tty");
236 timestamp("Logging Started.");
241 set_dev(const char *name)
243 char buf[DEV_NAME_LEN];
246 if (strlen(name) > 5 && !strncmp(name, _PATH_DEV, sizeof _PATH_DEV - 1)) {
247 snprintf(buf, sizeof buf, "%s", name);
249 if (strlen(name) == 2)
250 sprintf(buf, "%s%s", _PATH_TTY, name);
252 sprintf(buf, "%s%s", _PATH_DEV, name);
255 if (*name == '\0' || stat(buf, &sb) < 0)
256 fatal(EX_DATAERR, "bad device name");
258 if ((sb.st_mode & S_IFMT) != S_IFCHR)
259 fatal(EX_DATAERR, "must be a character device");
261 strncpy(dev_name, buf, DEV_NAME_LEN);
267 ask_dev(char *dbuf, const char *msg)
269 char buf[DEV_NAME_LEN];
278 printf("Enter device name [%s]:", dbuf);
280 printf("Enter device name:");
282 if (fgets(buf, DEV_NAME_LEN - 1, stdin)) {
284 if (buf[len - 1] == '\n')
286 if (buf[0] != '\0' && buf[0] != ' ')
295 main(int ac, char *av[])
297 int ch, res, rv, nread;
298 size_t b_size = MIN_SIZE;
299 char *buf, chb[READB_LEN];
302 (void) setlocale(LC_TIME, "");
310 while ((ch = getopt(ac, av, "Wciotnf:")) != -1)
316 opt_reconn_close = 1;
322 opt_reconn_oflow = 1;
338 if (modfind("snp") == -1)
339 if (kldload("snp") == -1 || modfind("snp") == -1)
340 warn("snp module not available");
342 signal(SIGINT, cleanup);
347 if (*(av += optind) == NULL) {
348 if (opt_interactive && !opt_no_switch)
349 ask_dev(dev_name, MSG_INIT);
351 fatal(EX_DATAERR, "no device name given");
353 strncpy(dev_name, *av, DEV_NAME_LEN);
357 if (!(buf = (char *) malloc(b_size)))
358 fatal(EX_UNAVAILABLE, "malloc failed");
364 FD_SET(std_in, &fd_s);
365 FD_SET(snp_io, &fd_s);
366 res = select(snp_io + 1, &fd_s, NULL, NULL, NULL);
367 if (opt_interactive && FD_ISSET(std_in, &fd_s)) {
369 if ((res = ioctl(std_in, FIONREAD, &nread)) != 0)
370 fatal(EX_OSERR, "ioctl(FIONREAD)");
371 if (nread > READB_LEN)
373 rv = read(std_in, chb, nread);
374 if (rv == -1 || rv != nread)
375 fatal(EX_IOERR, "read (stdin) failed");
382 if (!opt_no_switch) {
384 ask_dev(dev_name, MSG_CHANGE);
390 rv = write(snp_io, chb, nread);
391 if (rv == -1 || rv != nread) {
396 ask_dev(dev_name, MSG_NOWRITE);
403 if (!FD_ISSET(snp_io, &fd_s))
406 if ((res = ioctl(snp_io, FIONREAD, &nread)) != 0)
407 fatal(EX_OSERR, "ioctl(FIONREAD)");
411 if (opt_reconn_oflow)
413 else if (opt_interactive && !opt_no_switch) {
414 ask_dev(dev_name, MSG_OFLOW);
421 if (opt_reconn_close)
423 else if (opt_interactive && !opt_no_switch) {
424 ask_dev(dev_name, MSG_CLOSED);
430 if (nread < (b_size / 2) && (b_size / 2) > MIN_SIZE) {
432 if (!(buf = (char *) malloc(b_size / 2)))
433 fatal(EX_UNAVAILABLE, "malloc failed");
436 if (nread > b_size) {
437 b_size = (nread % 2) ? (nread + 1) : (nread);
439 if (!(buf = (char *) malloc(b_size)))
440 fatal(EX_UNAVAILABLE, "malloc failed");
442 rv = read(snp_io, buf, nread);
443 if (rv == -1 || rv != nread)
444 fatal(EX_IOERR, "read failed");
445 rv = write(std_out, buf, nread);
446 if (rv == -1 || rv != nread)
447 fatal(EX_IOERR, "write failed");