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[] =
18 "$FreeBSD: src/usr.sbin/watch/watch.c,v 1.18.2.3 2002/08/17 00:59:03 mikeh Exp $";
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>
42 #define MSG_INIT "Snoop started."
43 #define MSG_OFLOW "Snoop stopped due to overflow. Reconnecting."
44 #define MSG_CLOSED "Snoop stopped due to tty close. Reconnecting."
45 #define MSG_CHANGE "Snoop device change by user request."
46 #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 __P((void));
56 static void timestamp __P((const char *));
57 static void set_tty __P((void));
58 static void unset_tty __P((void));
59 static void fatal __P((int, const char *));
60 static int open_snp __P((void));
61 static void cleanup __P((int));
62 static void usage __P((void));
63 static void setup_scr __P((void));
64 static void attach_snp __P((void));
65 static void detach_snp __P((void));
66 static void set_dev __P((const char *));
67 static void ask_dev __P((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];
80 int std_in = 0, std_out = 1;
85 char tbuf[1024], gbuf[1024];
92 tputs(gbuf, 1, putchar);
103 printf("\n---------------------------------------------\n");
105 strftime(btmp, 1024, "Time: %d %b %H:%M", localtime(&t));
106 printf("%s\n", btmp);
108 printf("---------------------------------------------\n");
117 tcgetattr (std_in, &otty);
119 ntty.c_lflag &= ~ICANON; /* disable canonical operation */
120 ntty.c_lflag &= ~ECHO;
122 ntty.c_lflag &= ~FLUSHO;
125 ntty.c_lflag &= ~PENDIN;
128 ntty.c_lflag &= ~IEXTEN;
130 ntty.c_cc[VMIN] = 1; /* minimum of one character */
131 ntty.c_cc[VTIME] = 0; /* timeout value */
133 ntty.c_cc[VINTR] = 07; /* ^G */
134 ntty.c_cc[VQUIT] = 07; /* ^G */
135 tcsetattr (std_in, TCSANOW, &ntty);
141 tcsetattr (std_in, TCSANOW, &otty);
152 errx(error, "fatal: %s", buf);
160 char snp[] = {_PATH_DEV "snpX"};
169 if (opt_snpdev == NULL)
170 for (c = '0'; c <= '9'; c++) {
172 if ((f = open(snp, mode)) < 0)
177 if ((f = open(opt_snpdev, mode)) != -1)
179 fatal(EX_OSFILE, "cannot open snoop device");
189 timestamp("Logging Exited.");
199 fprintf(stderr, "usage: watch [-ciotnW] [tty name]\n");
206 char *cbuf = gbuf, *term;
207 if (!opt_interactive)
209 if ((term = getenv("TERM")))
210 if (tgetent(tbuf, term) == 1)
211 if (tgetstr("cl", &cbuf))
223 ioctl(snp_io, SNPSTTY, &dev);
229 if (ioctl(snp_io, SNPSTTY, &snp_tty) != 0)
230 fatal(EX_UNAVAILABLE, "cannot attach to tty");
232 timestamp("Logging Started.");
240 char buf[DEV_NAME_LEN];
243 if (strlen(name) > 5 && !strncmp(name, _PATH_DEV, sizeof _PATH_DEV - 1)) {
244 snprintf(buf, sizeof buf, "%s", name);
247 if (strlen(name) == 2)
248 sprintf(buf, "%s%s", _PATH_TTY, name);
250 sprintf(buf, "%s%s", _PATH_DEV, name);
253 if (*name == '\0' || stat(buf, &sb) < 0)
254 fatal(EX_DATAERR, "bad device name");
256 if ((sb.st_mode & S_IFMT) != S_IFCHR)
257 fatal(EX_DATAERR, "must be a character device");
259 snp_tty = sb.st_rdev;
268 char buf[DEV_NAME_LEN];
277 printf("Enter device name [%s]:", dbuf);
279 printf("Enter device name:");
281 if (fgets(buf, DEV_NAME_LEN - 1, stdin)) {
283 if (buf[len - 1] == '\n')
285 if (buf[0] != '\0' && buf[0] != ' ')
299 size_t nread, b_size = MIN_SIZE;
300 char ch, *buf, chb[READB_LEN];
303 (void) setlocale(LC_TIME, "");
311 while ((ch = getopt(ac, av, "Wciotnf:")) != -1)
317 opt_reconn_close = 1;
323 opt_reconn_oflow = 1;
339 if (modfind("snp") == -1)
340 if (kldload("snp") == -1 || modfind("snp") == -1)
341 warn("snp module not available");
343 signal(SIGINT, cleanup);
348 if (*(av += optind) == NULL) {
349 if (opt_interactive && !opt_no_switch)
350 ask_dev(dev_name, MSG_INIT);
352 fatal(EX_DATAERR, "no device name given");
354 strncpy(dev_name, *av, DEV_NAME_LEN);
358 if (!(buf = (char *) malloc(b_size)))
359 fatal(EX_UNAVAILABLE, "malloc failed");
365 FD_SET(std_in, &fd_s);
366 FD_SET(snp_io, &fd_s);
367 res = select(snp_io + 1, &fd_s, NULL, NULL, NULL);
368 if (opt_interactive && FD_ISSET(std_in, &fd_s)) {
370 if ((res = ioctl(std_in, FIONREAD, &nread)) != 0)
371 fatal(EX_OSERR, "ioctl(FIONREAD)");
372 if (nread > READB_LEN)
374 rv = read(std_in, chb, nread);
375 if (rv == -1 || (unsigned)rv != nread)
376 fatal(EX_IOERR, "read (stdin) failed");
383 if (!opt_no_switch) {
385 ask_dev(dev_name, MSG_CHANGE);
391 rv = write(snp_io, chb, nread);
392 if (rv == -1 || (unsigned)rv != nread) {
395 fatal(EX_IOERR, "write failed");
396 ask_dev(dev_name, MSG_NOWRITE);
403 if (!FD_ISSET(snp_io, &fd_s))
406 if ((res = ioctl(snp_io, FIONREAD, &idata)) != 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 nread = (unsigned)idata;
431 if (nread < (b_size / 2) && (b_size / 2) > MIN_SIZE) {
433 if (!(buf = (char *) malloc(b_size / 2)))
434 fatal(EX_UNAVAILABLE, "malloc failed");
437 if (nread > b_size) {
438 b_size = (nread % 2) ? (nread + 1) : (nread);
440 if (!(buf = (char *) malloc(b_size)))
441 fatal(EX_UNAVAILABLE, "malloc failed");
443 rv = read(snp_io, buf, nread);
444 if (rv == -1 || (unsigned)rv != nread)
445 fatal(EX_IOERR, "read failed");
446 rv = write(std_out, buf, nread);
447 if (rv == -1 || (unsigned)rv != nread)
448 fatal(EX_IOERR, "write failed");