diff options
author | peter <peter@FreeBSD.org> | 1998-04-06 16:13:49 +0000 |
---|---|---|
committer | peter <peter@FreeBSD.org> | 1998-04-06 16:13:49 +0000 |
commit | 7c3222498acb0b317b49cca6311ffb697172d7ca (patch) | |
tree | ad45ab72c9092b4dc1f619c3792adae6338de17b | |
parent | 6e3ec235ff85715b4f3346989bae94f7e0aaacfe (diff) | |
download | FreeBSD-src-7c3222498acb0b317b49cca6311ffb697172d7ca.zip FreeBSD-src-7c3222498acb0b317b49cca6311ffb697172d7ca.tar.gz |
Add a new -F flag which is a superset of -f. It will cause tail to
stat() the file being followed and do a close/reopen if the file has been
renamed and/or rotated. This is damn useful for leaving running on files
in /var/log when newsyslog(8) rotates them.
-rw-r--r-- | usr.bin/tail/extern.h | 2 | ||||
-rw-r--r-- | usr.bin/tail/forward.c | 16 | ||||
-rw-r--r-- | usr.bin/tail/tail.1 | 21 | ||||
-rw-r--r-- | usr.bin/tail/tail.c | 7 |
4 files changed, 42 insertions, 4 deletions
diff --git a/usr.bin/tail/extern.h b/usr.bin/tail/extern.h index f9a8bdf..0ec272d 100644 --- a/usr.bin/tail/extern.h +++ b/usr.bin/tail/extern.h @@ -48,5 +48,5 @@ int lines __P((FILE *, off_t)); void ierr __P((void)); void oerr __P((void)); -extern int fflag, rflag, rval; +extern int Fflag, fflag, rflag, rval; extern char *fname; diff --git a/usr.bin/tail/forward.c b/usr.bin/tail/forward.c index 1c4020d..31f5ea6 100644 --- a/usr.bin/tail/forward.c +++ b/usr.bin/tail/forward.c @@ -86,6 +86,7 @@ forward(fp, style, off, sbp) { register int ch; struct timeval interval; + struct stat sb2; switch(style) { case FBYTES: @@ -179,6 +180,21 @@ forward(fp, style, off, sbp) (void) usleep(250000); clearerr(fp); + + if (Fflag && fileno(fp) != STDIN_FILENO && + stat(fname, &sb2) != -1) { + if (sb2.st_ino != sbp->st_ino || + sb2.st_dev != sbp->st_dev || + sb2.st_rdev != sbp->st_rdev || + sb2.st_nlink == 0) { + fp = freopen(fname, "r", fp); + if (fp == NULL) { + ierr(); + break; + } + *sbp = sb2; + } + } } } diff --git a/usr.bin/tail/tail.1 b/usr.bin/tail/tail.1 index 8c96737..be5a03f 100644 --- a/usr.bin/tail/tail.1 +++ b/usr.bin/tail/tail.1 @@ -42,7 +42,11 @@ .Nd display the last part of a file .Sh SYNOPSIS .Nm -.Op Fl f Li | Fl r +.Oo +.Fl F | +.Fl f | +.Fl r +.Oc .Oo .Fl b Ar number | .Fl c Ar number | @@ -91,6 +95,20 @@ data to be appended to the input. The .Fl f option is ignored if the standard input is a pipe, but not if it is a FIFO. +.It Fl F +The +.Fl F +option implies the +.Fl f +option, but +.Nm +will also check to see if the file being followed has been renamed or rotated. +The file is closed and reopened when +.Nm +detects that the filename being read from has a new inode number. +The +.Fl F +option is ignored if reading from standard input rather than a file. .It Fl n Ar number The location is .Ar number @@ -136,6 +154,7 @@ utility is expected to be a superset of the .St -p1003.2-92 specification. In particular, the +.Fl F , .Fl b and .Fl r diff --git a/usr.bin/tail/tail.c b/usr.bin/tail/tail.c index 4c7ea6f..454ef65 100644 --- a/usr.bin/tail/tail.c +++ b/usr.bin/tail/tail.c @@ -54,7 +54,7 @@ static char sccsid[] = "@(#)tail.c 8.1 (Berkeley) 6/6/93"; #include <err.h> #include "extern.h" -int fflag, rflag, rval; +int Fflag, fflag, rflag, rval; char *fname; static void obsolete __P((char **)); @@ -107,8 +107,11 @@ main(argc, argv) obsolete(argv); style = NOTSET; - while ((ch = getopt(argc, argv, "b:c:fn:r")) != -1) + while ((ch = getopt(argc, argv, "Fb:c:fn:r")) != -1) switch(ch) { + case 'F': /* -F is superset of (and implies) -f */ + Fflag = fflag = 1; + break; case 'b': ARG(512, FBYTES, RBYTES); break; |