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.
15 * $FreeBSD: src/usr.sbin/watch/watch.c,v 1.18.2.3 2002/08/17 00:59:03 mikeh Exp $
16 * $DragonFly: src/usr.sbin/watch/watch.c,v 1.3 2003/11/03 19:31:44 eirikn Exp $
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>
40 #define MSG_INIT "Snoop started."
41 #define MSG_OFLOW "Snoop stopped due to overflow. Reconnecting."
42 #define MSG_CLOSED "Snoop stopped due to tty close. Reconnecting."
43 #define MSG_CHANGE "Snoop device change by user request."
44 #define MSG_NOWRITE "Snoop device change due to write failure."
47 #define DEV_NAME_LEN 1024 /* for /dev/ttyXX++ */
50 #define CHR_SWITCH 24 /* Ctrl+X */
51 #define CHR_CLEAR 23 /* Ctrl+V */
53 static void clear(void);
54 static void timestamp (const char *);
55 static void set_tty(void);
56 static void unset_tty(void);
57 static void fatal(int, const char *);
58 static int open_snp(void);
59 static void cleanup(int);
60 static void usage(void);
61 static void setup_scr(void);
62 static void attach_snp(void);
63 static void detach_snp(void);
64 static void set_dev(const char *);
65 static void ask_dev(char *, const char *);
67 int opt_reconn_close = 0;
68 int opt_reconn_oflow = 0;
69 int opt_interactive = 1;
70 int opt_timestamp = 0;
72 int opt_no_switch = 0;
73 const char *opt_snpdev;
75 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);
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);
150 errx(error, "fatal: %s", buf);
158 char snp[] = {_PATH_DEV "snpX"};
167 if (opt_snpdev == NULL)
168 for (c = '0'; c <= '9'; c++) {
170 if ((f = open(snp, mode)) < 0)
175 if ((f = open(opt_snpdev, mode)) != -1)
177 fatal(EX_OSFILE, "cannot open snoop device");
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, &dev);
227 if (ioctl(snp_io, SNPSTTY, &snp_tty) != 0)
228 fatal(EX_UNAVAILABLE, "cannot attach to tty");
230 timestamp("Logging Started.");
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);
245 if (strlen(name) == 2)
246 sprintf(buf, "%s%s", _PATH_TTY, name);
248 sprintf(buf, "%s%s", _PATH_DEV, name);
251 if (*name == '\0' || stat(buf, &sb) < 0)
252 fatal(EX_DATAERR, "bad device name");
254 if ((sb.st_mode & S_IFMT) != S_IFCHR)
255 fatal(EX_DATAERR, "must be a character device");
257 snp_tty = sb.st_rdev;
266 char buf[DEV_NAME_LEN];
275 printf("Enter device name [%s]:", dbuf);
277 printf("Enter device name:");
279 if (fgets(buf, DEV_NAME_LEN - 1, stdin)) {
281 if (buf[len - 1] == '\n')
283 if (buf[0] != '\0' && buf[0] != ' ')
297 size_t nread, b_size = MIN_SIZE;
298 char ch, *buf, chb[READB_LEN];
301 (void) setlocale(LC_TIME, "");
309 while ((ch = getopt(ac, av, "Wciotnf:")) != -1)
315 opt_reconn_close = 1;
321 opt_reconn_oflow = 1;
337 if (modfind("snp") == -1)
338 if (kldload("snp") == -1 || modfind("snp") == -1)
339 warn("snp module not available");
341 signal(SIGINT, cleanup);
346 if (*(av += optind) == NULL) {
347 if (opt_interactive && !opt_no_switch)
348 ask_dev(dev_name, MSG_INIT);
350 fatal(EX_DATAERR, "no device name given");
352 strncpy(dev_name, *av, DEV_NAME_LEN);
356 if (!(buf = (char *) malloc(b_size)))
357 fatal(EX_UNAVAILABLE, "malloc failed");
363 FD_SET(std_in, &fd_s);
364 FD_SET(snp_io, &fd_s);
365 res = select(snp_io + 1, &fd_s, NULL, NULL, NULL);
366 if (opt_interactive && FD_ISSET(std_in, &fd_s)) {
368 if ((res = ioctl(std_in, FIONREAD, &nread)) != 0)
369 fatal(EX_OSERR, "ioctl(FIONREAD)");
370 if (nread > READB_LEN)
372 rv = read(std_in, chb, nread);
373 if (rv == -1 || (unsigned)rv != nread)
374 fatal(EX_IOERR, "read (stdin) failed");
381 if (!opt_no_switch) {
383 ask_dev(dev_name, MSG_CHANGE);
389 rv = write(snp_io, chb, nread);
390 if (rv == -1 || (unsigned)rv != nread) {
393 fatal(EX_IOERR, "write failed");
394 ask_dev(dev_name, MSG_NOWRITE);
401 if (!FD_ISSET(snp_io, &fd_s))
404 if ((res = ioctl(snp_io, FIONREAD, &idata)) != 0)
405 fatal(EX_OSERR, "ioctl(FIONREAD)");
409 if (opt_reconn_oflow)
411 else if (opt_interactive && !opt_no_switch) {
412 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);
428 nread = (unsigned)idata;
429 if (nread < (b_size / 2) && (b_size / 2) > MIN_SIZE) {
431 if (!(buf = (char *) malloc(b_size / 2)))
432 fatal(EX_UNAVAILABLE, "malloc failed");
435 if (nread > b_size) {
436 b_size = (nread % 2) ? (nread + 1) : (nread);
438 if (!(buf = (char *) malloc(b_size)))
439 fatal(EX_UNAVAILABLE, "malloc failed");
441 rv = read(snp_io, buf, nread);
442 if (rv == -1 || (unsigned)rv != nread)
443 fatal(EX_IOERR, "read failed");
444 rv = write(std_out, buf, nread);
445 if (rv == -1 || (unsigned)rv != nread)
446 fatal(EX_IOERR, "write failed");