Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / watch / watch.c
1 /*
2  * Copyright (c) 1995 Ugen J.S.Antsilevich
3  *
4  * Redistribution and use in source forms, with and without modification,
5  * are permitted provided that this entire comment appears intact.
6  *
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.
10  *
11  * This software is provided ``AS IS'' without any warranties of any kind.
12  *
13  * Snoop stuff.
14  */
15
16 #ifndef lint
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 $";
19 #endif /* not lint */
20
21 #include <sys/param.h>
22 #include <sys/fcntl.h>
23 #include <sys/filio.h>
24 #include <sys/snoop.h>
25 #include <sys/stat.h>
26 #include <sys/linker.h>
27 #include <sys/module.h>
28
29 #include <err.h>
30 #include <locale.h>
31 #include <paths.h>
32 #include <signal.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <sysexits.h>
37 #include <termcap.h>
38 #include <termios.h>
39 #include <time.h>
40 #include <unistd.h>
41
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."
47
48
49 #define DEV_NAME_LEN    1024    /* for /dev/ttyXX++ */
50 #define MIN_SIZE        256
51
52 #define CHR_SWITCH      24      /* Ctrl+X        */
53 #define CHR_CLEAR       23      /* Ctrl+V        */
54
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 *));
68
69 int             opt_reconn_close = 0;
70 int             opt_reconn_oflow = 0;
71 int             opt_interactive = 1;
72 int             opt_timestamp = 0;
73 int             opt_write = 0;
74 int             opt_no_switch = 0;
75 const char      *opt_snpdev;
76
77 char            dev_name[DEV_NAME_LEN];
78 int             snp_io;
79 dev_t           snp_tty;
80 int             std_in = 0, std_out = 1;
81
82
83 int             clear_ok = 0;
84 struct termios  otty;
85 char            tbuf[1024], gbuf[1024];
86
87
88 static void
89 clear()
90 {
91         if (clear_ok)
92                 tputs(gbuf, 1, putchar);
93         fflush(stdout);
94 }
95
96 static void
97 timestamp(buf)
98         const char     *buf;
99 {
100         time_t          t;
101         char            btmp[1024];
102         clear();
103         printf("\n---------------------------------------------\n");
104         t = time(NULL);
105         strftime(btmp, 1024, "Time: %d %b %H:%M", localtime(&t));
106         printf("%s\n", btmp);
107         printf("%s\n", buf);
108         printf("---------------------------------------------\n");
109         fflush(stdout);
110 }
111
112 static void
113 set_tty()
114 {
115         struct termios  ntty;
116
117         tcgetattr (std_in, &otty);
118         ntty = otty;
119         ntty.c_lflag &= ~ICANON;    /* disable canonical operation  */
120         ntty.c_lflag &= ~ECHO;
121 #ifdef FLUSHO
122         ntty.c_lflag &= ~FLUSHO;
123 #endif
124 #ifdef PENDIN
125         ntty.c_lflag &= ~PENDIN;
126 #endif
127 #ifdef IEXTEN
128         ntty.c_lflag &= ~IEXTEN;
129 #endif
130         ntty.c_cc[VMIN] = 1;        /* minimum of one character */
131         ntty.c_cc[VTIME] = 0;       /* timeout value        */
132
133         ntty.c_cc[VINTR] = 07;   /* ^G */
134         ntty.c_cc[VQUIT] = 07;   /* ^G */
135         tcsetattr (std_in, TCSANOW, &ntty);
136 }
137
138 static void
139 unset_tty()
140 {
141         tcsetattr (std_in, TCSANOW, &otty);
142 }
143
144
145 static void
146 fatal(error, buf)
147         int                   error;
148         const char           *buf;
149 {
150         unset_tty();
151         if (buf)
152                 errx(error, "fatal: %s", buf);
153         else
154                 exit(error);
155 }
156
157 static int
158 open_snp()
159 {
160         char            snp[] = {_PATH_DEV "snpX"};
161         char            c;
162         int             f, mode;
163
164         if (opt_write)
165                 mode = O_RDWR;
166         else
167                 mode = O_RDONLY;
168
169         if (opt_snpdev == NULL)
170                 for (c = '0'; c <= '9'; c++) {
171                         snp[8] = c;
172                         if ((f = open(snp, mode)) < 0)
173                                 continue;
174                         return f;
175                 }
176         else
177                 if ((f = open(opt_snpdev, mode)) != -1)
178                         return (f);
179         fatal(EX_OSFILE, "cannot open snoop device");
180         return (0);
181 }
182
183
184 static void
185 cleanup(signo)
186         int             signo __unused;
187 {
188         if (opt_timestamp)
189                 timestamp("Logging Exited.");
190         close(snp_io);
191         unset_tty();
192         exit(EX_OK);
193 }
194
195
196 static void
197 usage()
198 {
199         fprintf(stderr, "usage: watch [-ciotnW] [tty name]\n");
200         exit(EX_USAGE);
201 }
202
203 static void
204 setup_scr()
205 {
206         char           *cbuf = gbuf, *term;
207         if (!opt_interactive)
208                 return;
209         if ((term = getenv("TERM")))
210                 if (tgetent(tbuf, term) == 1)
211                         if (tgetstr("cl", &cbuf))
212                                 clear_ok = 1;
213         set_tty();
214         clear();
215 }
216
217 static void
218 detach_snp()
219 {
220         dev_t           dev;
221
222         dev = NODEV;
223         ioctl(snp_io, SNPSTTY, &dev);
224 }
225
226 static void
227 attach_snp()
228 {
229         if (ioctl(snp_io, SNPSTTY, &snp_tty) != 0)
230                 fatal(EX_UNAVAILABLE, "cannot attach to tty");
231         if (opt_timestamp)
232                 timestamp("Logging Started.");
233 }
234
235
236 static void
237 set_dev(name)
238         const char     *name;
239 {
240         char            buf[DEV_NAME_LEN];
241         struct stat     sb;
242
243         if (strlen(name) > 5 && !strncmp(name, _PATH_DEV, sizeof _PATH_DEV - 1)) {
244                 snprintf(buf, sizeof buf, "%s", name);
245         }
246         else {
247                 if (strlen(name) == 2)
248                         sprintf(buf, "%s%s", _PATH_TTY, name);
249                 else
250                         sprintf(buf, "%s%s", _PATH_DEV, name);
251         }
252
253         if (*name == '\0' || stat(buf, &sb) < 0)
254                 fatal(EX_DATAERR, "bad device name");
255
256         if ((sb.st_mode & S_IFMT) != S_IFCHR)
257                 fatal(EX_DATAERR, "must be a character device");
258
259         snp_tty = sb.st_rdev;
260         attach_snp();
261 }
262
263 void
264 ask_dev(dbuf, msg)
265         char           *dbuf;
266         const char     *msg;
267 {
268         char            buf[DEV_NAME_LEN];
269         int             len;
270
271         clear();
272         unset_tty();
273
274         if (msg)
275                 printf("%s\n", msg);
276         if (dbuf)
277                 printf("Enter device name [%s]:", dbuf);
278         else
279                 printf("Enter device name:");
280
281         if (fgets(buf, DEV_NAME_LEN - 1, stdin)) {
282                 len = strlen(buf);
283                 if (buf[len - 1] == '\n')
284                         buf[len - 1] = '\0';
285                 if (buf[0] != '\0' && buf[0] != ' ')
286                         strcpy(dbuf, buf);
287         }
288         set_tty();
289 }
290
291 #define READB_LEN       5
292
293 int
294 main(ac, av)
295         int             ac;
296         char          **av;
297 {
298         int             res, idata, rv;
299         size_t          nread, b_size = MIN_SIZE;
300         char            ch, *buf, chb[READB_LEN];
301         fd_set          fd_s;
302
303         (void) setlocale(LC_TIME, "");
304
305         if (isatty(std_out))
306                 opt_interactive = 1;
307         else
308                 opt_interactive = 0;
309
310
311         while ((ch = getopt(ac, av, "Wciotnf:")) != -1)
312                 switch (ch) {
313                 case 'W':
314                         opt_write = 1;
315                         break;
316                 case 'c':
317                         opt_reconn_close = 1;
318                         break;
319                 case 'i':
320                         opt_interactive = 1;
321                         break;
322                 case 'o':
323                         opt_reconn_oflow = 1;
324                         break;
325                 case 't':
326                         opt_timestamp = 1;
327                         break;
328                 case 'n':
329                         opt_no_switch = 1;
330                         break;
331                 case 'f':
332                         opt_snpdev = optarg;
333                         break;
334                 case '?':
335                 default:
336                         usage();
337                 }
338
339         if (modfind("snp") == -1)
340                 if (kldload("snp") == -1 || modfind("snp") == -1)
341                         warn("snp module not available");
342
343         signal(SIGINT, cleanup);
344
345         setup_scr();
346         snp_io = open_snp();
347
348         if (*(av += optind) == NULL) {
349                 if (opt_interactive && !opt_no_switch)
350                         ask_dev(dev_name, MSG_INIT);
351                 else
352                         fatal(EX_DATAERR, "no device name given");
353         } else
354                 strncpy(dev_name, *av, DEV_NAME_LEN);
355
356         set_dev(dev_name);
357
358         if (!(buf = (char *) malloc(b_size)))
359                 fatal(EX_UNAVAILABLE, "malloc failed");
360
361         FD_ZERO(&fd_s);
362
363         while (1) {
364                 if (opt_interactive)
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)) {
369
370                         if ((res = ioctl(std_in, FIONREAD, &nread)) != 0)
371                                 fatal(EX_OSERR, "ioctl(FIONREAD)");
372                         if (nread > READB_LEN)
373                                 nread = READB_LEN;
374                         rv = read(std_in, chb, nread);
375                         if (rv == -1 || (unsigned)rv != nread)
376                                 fatal(EX_IOERR, "read (stdin) failed");
377
378                         switch (chb[0]) {
379                         case CHR_CLEAR:
380                                 clear();
381                                 break;
382                         case CHR_SWITCH:
383                                 if (!opt_no_switch) {
384                                         detach_snp();
385                                         ask_dev(dev_name, MSG_CHANGE);
386                                         set_dev(dev_name);
387                                         break;
388                                 }
389                         default:
390                                 if (opt_write) {
391                                         rv = write(snp_io, chb, nread);
392                                         if (rv == -1 || (unsigned)rv != nread) {
393                                                 detach_snp();
394                                                 if (opt_no_switch)
395                                                         fatal(EX_IOERR, "write failed");
396                                                 ask_dev(dev_name, MSG_NOWRITE);
397                                                 set_dev(dev_name);
398                                         }
399                                 }
400
401                         }
402                 }
403                 if (!FD_ISSET(snp_io, &fd_s))
404                         continue;
405
406                 if ((res = ioctl(snp_io, FIONREAD, &idata)) != 0)
407                         fatal(EX_OSERR, "ioctl(FIONREAD)");
408
409                 switch (idata) {
410                 case SNP_OFLOW:
411                         if (opt_reconn_oflow)
412                                 attach_snp();
413                         else if (opt_interactive && !opt_no_switch) {
414                                 ask_dev(dev_name, MSG_OFLOW);
415                                 set_dev(dev_name);
416                         } else
417                                 cleanup(-1);
418                         break;
419                 case SNP_DETACH:
420                 case SNP_TTYCLOSE:
421                         if (opt_reconn_close)
422                                 attach_snp();
423                         else if (opt_interactive && !opt_no_switch) {
424                                 ask_dev(dev_name, MSG_CLOSED);
425                                 set_dev(dev_name);
426                         } else
427                                 cleanup(-1);
428                         break;
429                 default:
430                         nread = (unsigned)idata;
431                         if (nread < (b_size / 2) && (b_size / 2) > MIN_SIZE) {
432                                 free(buf);
433                                 if (!(buf = (char *) malloc(b_size / 2)))
434                                         fatal(EX_UNAVAILABLE, "malloc failed");
435                                 b_size = b_size / 2;
436                         }
437                         if (nread > b_size) {
438                                 b_size = (nread % 2) ? (nread + 1) : (nread);
439                                 free(buf);
440                                 if (!(buf = (char *) malloc(b_size)))
441                                         fatal(EX_UNAVAILABLE, "malloc failed");
442                         }
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");
449                 }
450         }                       /* While */
451         return(0);
452 }
453