Use err(3).

This commit is contained in:
Philippe Charnier 1997-10-27 12:21:10 +00:00
parent 6fd3c7fdea
commit dc763e50ee
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=30773
2 changed files with 38 additions and 30 deletions

View File

@ -19,26 +19,26 @@ allows the superuser to examine all data coming through a specified tty.
writes to standard output. writes to standard output.
.Pp .Pp
The options are as follows: The options are as follows:
.Bl -tag -width "-l nul " .Bl -tag -width indent
.It Fl c .It Fl c
Reconnect on close. If the tty observed by Reconnect on close. If the tty observed by
.Nm watch .Nm
is closed, automatically reattach to the same tty. is closed, automatically reattach to the same tty.
If this option is not specified, If this option is not specified,
.Nm watch .Nm
will request a new tty if running in interactive mode or exit if running will request a new tty if running in interactive mode or exit if running
without a controlling tty. without a controlling tty.
.It Fl i .It Fl i
Force interactive mode. Force interactive mode.
Interactive mode is a default if Interactive mode is a default if
.Nm watch .Nm
is started from a tty. is started from a tty.
If output is redirected to a file, interactive mode can still be requested If output is redirected to a file, interactive mode can still be requested
by specifying this option. by specifying this option.
.It Fl o .It Fl o
Reconnect on overflow. Reconnect on overflow.
The behavior of The behavior of
.Nm watch .Nm
if the observed tty overflows is similar to the behavior if the observed tty if the observed tty overflows is similar to the behavior if the observed tty
is closed. is closed.
For more info see For more info see
@ -63,14 +63,14 @@ While running in interactive mode, all user input is discarded except for:
.Bl -tag -width "XXXX" -compact .Bl -tag -width "XXXX" -compact
.It Sy "<control-G>" .It Sy "<control-G>"
Exit Exit
.Nm watch . .Nm Ns .
.It Sy "<control-W>" .It Sy "<control-W>"
Clear screen. Clear screen.
.It Sy "<control-X>" .It Sy "<control-X>"
Change attached tty. Change attached tty.
.Sh RESTRICTIONS .Sh RESTRICTIONS
Only the superuser can run Only the superuser can run
.Nm watch . .Nm Ns .
.Sh SEE ALSO .Sh SEE ALSO
.Xr pty 4 , .Xr pty 4 ,
.Xr sio 4 , .Xr sio 4 ,
@ -79,7 +79,7 @@ Only the superuser can run
No terminal emulation is performed. No terminal emulation is performed.
All user output is reproduced as-is. All user output is reproduced as-is.
.Sh AUTHOR .Sh AUTHOR
Ugen J.S. Antsilevich <ugen@NetVision.net.il> .An Ugen J.S. Antsilevich Aq ugen@NetVision.net.il
.Sh HISTORY .Sh HISTORY
.Nm Watch .Nm Watch
first appeared in first appeared in

View File

@ -13,12 +13,18 @@
* Snoop stuff. * Snoop stuff.
*/ */
#ifndef lint
static const char rcsid[] =
"$Id$";
#endif /* not lint */
#include <sys/types.h> #include <sys/types.h>
#include <sys/fcntl.h> #include <sys/fcntl.h>
#include <sys/filio.h> #include <sys/filio.h>
#include <sys/snoop.h> #include <sys/snoop.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <err.h>
#include <locale.h> #include <locale.h>
#include <signal.h> #include <signal.h>
#include <stdio.h> #include <stdio.h>
@ -125,8 +131,9 @@ fatal(err, buf)
{ {
unset_tty(); unset_tty();
if (buf) if (buf)
fprintf(stderr, "Fatal: %s\n", buf); errx(err, "fatal: %s", buf);
exit(err); else
exit(err);
} }
int int
@ -147,7 +154,8 @@ open_snp()
continue; continue;
return f; return f;
} }
fatal(EX_OSFILE, "Cannot open snoop device."); fatal(EX_OSFILE, "cannot open snoop device");
return (0);
} }
@ -162,10 +170,10 @@ cleanup()
} }
void static void
show_usage() usage()
{ {
printf("watch -[ciotnW] [tty name]\n"); fprintf(stderr, "usage: watch [-ciotnW] [tty name]\n");
exit(EX_USAGE); exit(EX_USAGE);
} }
@ -194,7 +202,8 @@ ctoh(c)
if (c >= 'a' && c <= 'f') if (c >= 'a' && c <= 'f')
return (int) (c - 'a' + 10); return (int) (c - 'a' + 10);
fatal(EX_DATAERR, "Bad tty number."); fatal(EX_DATAERR, "bad tty number");
return (0);
} }
@ -211,7 +220,7 @@ void
attach_snp() attach_snp()
{ {
if (ioctl(snp_io, SNPSTTY, &snp_tty) != 0) if (ioctl(snp_io, SNPSTTY, &snp_tty) != 0)
fatal(EX_UNAVAILABLE, "Cannot attach to tty."); fatal(EX_UNAVAILABLE, "cannot attach to tty");
if (opt_timestamp) if (opt_timestamp)
timestamp("Logging Started."); timestamp("Logging Started.");
} }
@ -235,10 +244,10 @@ set_dev(name)
} }
if (*name == '\0' || stat(buf, &sb) < 0) if (*name == '\0' || stat(buf, &sb) < 0)
fatal(EX_DATAERR, "Bad device name."); fatal(EX_DATAERR, "bad device name");
if ((sb.st_mode & S_IFMT) != S_IFCHR) if ((sb.st_mode & S_IFMT) != S_IFCHR)
fatal(EX_DATAERR, "Must be a character device."); fatal(EX_DATAERR, "must be a character device");
snp_tty = sb.st_rdev; snp_tty = sb.st_rdev;
attach_snp(); attach_snp();
@ -313,8 +322,7 @@ main(ac, av)
break; break;
case '?': case '?':
default: default:
show_usage(); usage();
exit(1);
} }
signal(SIGINT, cleanup); signal(SIGINT, cleanup);
@ -326,14 +334,14 @@ main(ac, av)
if (opt_interactive && !opt_no_switch) if (opt_interactive && !opt_no_switch)
ask_dev(dev_name, MSG_INIT); ask_dev(dev_name, MSG_INIT);
else else
fatal(EX_DATAERR, "No device name given."); fatal(EX_DATAERR, "no device name given");
} else } else
strncpy(dev_name, *av, DEV_NAME_LEN); strncpy(dev_name, *av, DEV_NAME_LEN);
set_dev(dev_name); set_dev(dev_name);
if (!(buf = (char *) malloc(b_size))) if (!(buf = (char *) malloc(b_size)))
fatal(EX_UNAVAILABLE, "Cannot malloc()."); fatal(EX_UNAVAILABLE, "malloc failed");
FD_ZERO(&fd_s); FD_ZERO(&fd_s);
@ -345,11 +353,11 @@ main(ac, av)
if (opt_interactive && FD_ISSET(std_in, &fd_s)) { if (opt_interactive && FD_ISSET(std_in, &fd_s)) {
if ((res = ioctl(std_in, FIONREAD, &nread)) != 0) if ((res = ioctl(std_in, FIONREAD, &nread)) != 0)
fatal(EX_OSERR, "ioctl() failed."); fatal(EX_OSERR, "ioctl(FIONREAD)");
if (nread > READB_LEN) if (nread > READB_LEN)
nread = READB_LEN; nread = READB_LEN;
if (read(std_in,chb,nread)!=nread) if (read(std_in,chb,nread)!=nread)
fatal(EX_IOERR, "read (stdin) failed."); fatal(EX_IOERR, "read (stdin) failed");
switch (chb[0]) { switch (chb[0]) {
case CHR_CLEAR: case CHR_CLEAR:
@ -367,7 +375,7 @@ main(ac, av)
if (write(snp_io,chb,nread) != nread) { if (write(snp_io,chb,nread) != nread) {
detach_snp(); detach_snp();
if (opt_no_switch) if (opt_no_switch)
fatal(EX_IOERR, "Write failed."); fatal(EX_IOERR, "write failed");
ask_dev(dev_name, MSG_NOWRITE); ask_dev(dev_name, MSG_NOWRITE);
set_dev(dev_name); set_dev(dev_name);
} }
@ -379,7 +387,7 @@ main(ac, av)
continue; continue;
if ((res = ioctl(snp_io, FIONREAD, &nread)) != 0) if ((res = ioctl(snp_io, FIONREAD, &nread)) != 0)
fatal(EX_OSERR, "ioctl() failed."); fatal(EX_OSERR, "ioctl(FIONREAD)");
switch (nread) { switch (nread) {
case SNP_OFLOW: case SNP_OFLOW:
@ -403,19 +411,19 @@ main(ac, av)
if (nread < (b_size / 2) && (b_size / 2) > MIN_SIZE) { if (nread < (b_size / 2) && (b_size / 2) > MIN_SIZE) {
free(buf); free(buf);
if (!(buf = (char *) malloc(b_size / 2))) if (!(buf = (char *) malloc(b_size / 2)))
fatal(EX_UNAVAILABLE, "Cannot malloc()"); fatal(EX_UNAVAILABLE, "malloc failed");
b_size = b_size / 2; b_size = b_size / 2;
} }
if (nread > b_size) { if (nread > b_size) {
b_size = (nread % 2) ? (nread + 1) : (nread); b_size = (nread % 2) ? (nread + 1) : (nread);
free(buf); free(buf);
if (!(buf = (char *) malloc(b_size))) if (!(buf = (char *) malloc(b_size)))
fatal(EX_UNAVAILABLE, "Cannot malloc()"); fatal(EX_UNAVAILABLE, "malloc failed");
} }
if (read(snp_io, buf, nread) < nread) if (read(snp_io, buf, nread) < nread)
fatal(EX_IOERR, "read failed."); fatal(EX_IOERR, "read failed");
if (write(std_out, buf, nread) < nread) if (write(std_out, buf, nread) < nread)
fatal(EX_IOERR, "write failed."); fatal(EX_IOERR, "write failed");
} }
} /* While */ } /* While */
} }