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.
17 static const char rcsid[] =
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>
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 __P((void));
55 static void timestamp __P((const char *));
56 static void set_tty __P((void));
57 static void unset_tty __P((void));
58 static void fatal __P((int, const char *));
59 static int open_snp __P((void));
60 static void cleanup __P((int));
61 static void usage __P((void));
62 static void setup_scr __P((void));
63 static void attach_snp __P((void));
64 static void detach_snp __P((void));
65 static void set_dev __P((const char *));
66 static void ask_dev __P((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);
102 printf("\n---------------------------------------------\n");
104 strftime(btmp, 1024, "Time: %d %b %H:%M", localtime(&t));
105 printf("%s\n", btmp);
107 printf("---------------------------------------------\n");
116 tcgetattr (std_in, &otty);
118 ntty.c_lflag &= ~ICANON; /* disable canonical operation */
119 ntty.c_lflag &= ~ECHO;
121 ntty.c_lflag &= ~FLUSHO;
124 ntty.c_lflag &= ~PENDIN;
127 ntty.c_lflag &= ~IEXTEN;
129 ntty.c_cc[VMIN] = 1; /* minimum of one character */
130 ntty.c_cc[VTIME] = 0; /* timeout value */
132 ntty.c_cc[VINTR] = 07; /* ^G */
133 ntty.c_cc[VQUIT] = 07; /* ^G */
134 tcsetattr (std_in, TCSANOW, &ntty);
140 tcsetattr (std_in, TCSANOW, &otty);
151 errx(error, "fatal: %s", buf);
159 char snp[] = {_PATH_DEV "snpX"};
168 if (opt_snpdev == NULL)
169 for (c = '0'; c <= '9'; c++) {
171 if ((f = open(snp, mode)) < 0)
176 if ((f = open(opt_snpdev, mode)) != -1)
178 fatal(EX_OSFILE, "cannot open snoop device");
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.");
239 char buf[DEV_NAME_LEN];
242 if (strlen(name) > 5 && !strncmp(name, _PATH_DEV, sizeof _PATH_DEV - 1)) {
243 snprintf(buf, sizeof buf, "%s", name);
246 if (strlen(name) == 2)
247 sprintf(buf, "%s%s", _PATH_TTY, name);
249 sprintf(buf, "%s%s", _PATH_DEV, name);
252 if (*name == '\0' || stat(buf, &sb) < 0)
253 fatal(EX_DATAERR, "bad device name");
255 if ((sb.st_mode & S_IFMT) != S_IFCHR)
256 fatal(EX_DATAERR, "must be a character device");
258 snp_tty = sb.st_rdev;
267 char buf[DEV_NAME_LEN];
276 printf("Enter device name [%s]:", dbuf);
278 printf("Enter device name:");
280 if (fgets(buf, DEV_NAME_LEN - 1, stdin)) {
282 if (buf[len - 1] == '\n')
284 if (buf[0] != '\0' && buf[0] != ' ')
298 size_t nread, b_size = MIN_SIZE;
299 char ch, *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 || (unsigned)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 || (unsigned)rv != nread) {
394 fatal(EX_IOERR, "write failed");
395 ask_dev(dev_name, MSG_NOWRITE);
402 if (!FD_ISSET(snp_io, &fd_s))
405 if ((res = ioctl(snp_io, FIONREAD, &idata)) != 0)
406 fatal(EX_OSERR, "ioctl(FIONREAD)");
410 if (opt_reconn_oflow)
412 else if (opt_interactive && !opt_no_switch) {
413 ask_dev(dev_name, MSG_OFLOW);
419 if (opt_reconn_close)
421 else if (opt_interactive && !opt_no_switch) {
422 ask_dev(dev_name, MSG_CLOSED);
427 nread = (unsigned)idata;
428 if (nread < (b_size / 2) && (b_size / 2) > MIN_SIZE) {
430 if (!(buf = (char *) malloc(b_size / 2)))
431 fatal(EX_UNAVAILABLE, "malloc failed");
434 if (nread > b_size) {
435 b_size = (nread % 2) ? (nread + 1) : (nread);
437 if (!(buf = (char *) malloc(b_size)))
438 fatal(EX_UNAVAILABLE, "malloc failed");
440 rv = read(snp_io, buf, nread);
441 if (rv == -1 || (unsigned)rv != nread)
442 fatal(EX_IOERR, "read failed");
443 rv = write(std_out, buf, nread);
444 if (rv == -1 || (unsigned)rv != nread)
445 fatal(EX_IOERR, "write failed");