NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

bin/50322: tail -F <file> misbehaves with stdin closed



>Number:         50322
>Category:       bin
>Synopsis:       tail -F <file> misbehaves with stdin closed
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri Oct 09 17:25:00 +0000 2015
>Originator:     Timo Buhrmester
>Release:        7.0
>Organization:
>Environment:
NetBSD frozen.localdomain 7.0 NetBSD 7.0 (FRZKERN32) #0: Tue Sep 29 04:46:38 CEST 2015  build@frozen.localdomain:/usr/build/obj-frozen32-i386-i386/sys/arch/i386/compile/FRZKERN32 i386
>Description:
The point of tail -F /some/file is to follow the contents in /some/file, even if /some/file is occasionally replaced with a different file, e.g. due to newsyslog(8) running.  In this mode of operation, standard input is ignored, as it should be.

Now, if standard input happens to be closed, for example by running
tail -F /some/file <&-
or by it being used inside a script that has its standard input closed (possibly as a result of daemonizing the script), tail's attempt to fopen(3) /some/file will open the file at fd 0 - the typical fd for stdin.

However, later on, tail effectively uses fd == 0 to determine whether it is reading from stdin or not.  If /some/file was opened at fd 0, tail will hence consider itself to be reading from stdin and omit adding the vnode filters to kqueue/kevent.

The result is that it will not follow the file after newsyslog(8) rotated the file away; it will instead forever be stuck in kevent(2).

I've been hit by this for a long time, only yesterday managed to finally find this (Heisen)bug.

The root cause of the problem is that tail assumes fd 0 is standard input.  It does this in two places by comparing `fileno(fp)` to `STDIN_FILENO`.  (`fp` being a FILE * as returned by fopen(3) or freopen(3)).
It should instead compare `fp` to `stdin`.
>How-To-Repeat:
Terminal 1:
$ touch /tmp/foo
$ tail -F /tmp/foo <&-

Terminal 2:
$ echo foo >>/tmp/foo  # Outputs 'foo' on Terminal 1
$ rm /tmp/foo
$ echo bar >>/tmp/foo  # SHOULD output 'bar' on Terminal 1, but does not.
>Fix:
--- usr.bin/tail/forward.c.orig 2015-10-09 19:17:34.000000000 +0200
+++ usr.bin/tail/forward.c      2015-10-09 19:17:54.000000000 +0200
@@ -198,7 +198,7 @@
                        n = 0;
 
                        memset(ev, 0, sizeof(ev));
-                       if (fflag == 2 && fileno(fp) != STDIN_FILENO) {
+                       if (fflag == 2 && fp != stdin) {
                                EV_SET(&ev[n], fileno(fp), EVFILT_VNODE,
                                    EV_ADD | EV_ENABLE | EV_CLEAR,
                                    NOTE_DELETE | NOTE_RENAME, 0, 0);
@@ -240,7 +240,7 @@
                         */
                        (void) sleep(1);
 
-                       if (fflag == 2 && fileno(fp) != STDIN_FILENO &&
+                       if (fflag == 2 && fp != stdin &&
                            stat(fname, &statbuf) != -1) {
                                if (statbuf.st_ino != sbp->st_ino ||
                                    statbuf.st_dev != sbp->st_dev ||



Home | Main Index | Thread Index | Old Index