diff options
author | Jim Meyering <meyering@redhat.com> | 2009-09-07 22:10:10 +0200 |
---|---|---|
committer | Jim Meyering <meyering@redhat.com> | 2009-09-07 23:19:56 +0200 |
commit | af6436559c2954d7f35b7fec29dfcbaa4ebdc03b (patch) | |
tree | 4c5d53442d729d663c15809899bd5c03bdf13b6d | |
parent | fd9750b0ffb691d2ad97551eada5c2802df3c805 (diff) | |
download | coreutils-af6436559c2954d7f35b7fec29dfcbaa4ebdc03b.tar.xz |
tail: ignore -f for piped-stdin, as POSIX requires
* src/tail.c (main): Tailing a pipe "forever" is not useful,
and POSIX specifies that tail ignore the -f when there is no
file argument and stdin is a FIFO or pipe. So we do that.
In addition, GNU tail excludes "-" arguments from the list of files
to tail forever, when the associated file descriptor is connected
to a FIFO or pipe. Before this change, ":|tail -f" would hang.
Reported by Ren Yang and Ulrich Drepper.
* tests/tail-2/pipe-f: Test for this.
* tests/tail-2/pipe-f2: Ensure tail doesn't exit early for a fifo.
* tests/Makefile.am (TESTS): Add these tests.
* NEWS (POSIX conformance): Mention it.
-rw-r--r-- | NEWS | 6 | ||||
-rw-r--r-- | THANKS | 1 | ||||
-rw-r--r-- | src/tail.c | 16 | ||||
-rw-r--r-- | tests/Makefile.am | 2 | ||||
-rwxr-xr-x | tests/tail-2/pipe-f | 32 | ||||
-rwxr-xr-x | tests/tail-2/pipe-f2 | 37 |
6 files changed, 93 insertions, 1 deletions
@@ -46,6 +46,12 @@ GNU coreutils NEWS -*- outline -*- cp --reflink accepts a new "auto" parameter which falls back to a standard copy if creating a copy-on-write clone is not possible. +** POSIX conformance + + tail -f now ignores "-" when stdin is a pipe or FIFO, per POSIX. + Now, :|tail -f terminates immediately. Before, it would block indefinitely. + [the old behavior dates back to the original implementation] + * Noteworthy changes in release 7.5 (2009-08-20) [stable] @@ -490,6 +490,7 @@ Ralph Loader loader@maths.ox.ac.uk Raul Miller moth@magenta.com Raúl Núñez de Arenas Coronado raul@pleyades.net Reuben Thomas rrt@sc3d.org +Ren Yang ryang@redhat.com Richard A Downing richard.downing@bcs.org.uk Richard Braakman dark@xs4all.nl Richard Dawe rich@phekda.freeserve.co.uk diff --git a/src/tail.c b/src/tail.c index 9288007ca..d75d9b3e4 100644 --- a/src/tail.c +++ b/src/tail.c @@ -1979,7 +1979,21 @@ main (int argc, char **argv) for (i = 0; i < n_files; i++) ok &= tail_file (&F[i], n_units); - if (forever) + /* When there is no FILE operand and stdin is a pipe or FIFO + POSIX requires that tail ignore the -f option. + Since we allow multiple FILE operands, we extend that to say: + ignore any "-" operand that corresponds to a pipe or FIFO. */ + size_t n_viable = 0; + for (i = 0; i < n_files; i++) + { + if (STREQ (F[i].name, "-") && !F[i].ignore + && 0 <= F[i].fd && S_ISFIFO (F[i].mode)) + F[i].ignore = true; + else + ++n_viable; + } + + if (forever && n_viable) { #if HAVE_INOTIFY /* If the user specifies stdin via a command line argument of "-", diff --git a/tests/Makefile.am b/tests/Makefile.am index 6b3c2b149..42a12cfac 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -429,6 +429,8 @@ TESTS = \ tail-2/big-4gb \ tail-2/flush-initial \ tail-2/follow-stdin \ + tail-2/pipe-f \ + tail-2/pipe-f2 \ tail-2/proc-ksyms \ tail-2/start-middle \ touch/dangling-symlink \ diff --git a/tests/tail-2/pipe-f b/tests/tail-2/pipe-f new file mode 100755 index 000000000..b9f6ae389 --- /dev/null +++ b/tests/tail-2/pipe-f @@ -0,0 +1,32 @@ +#!/bin/sh +# ensure that :|tail -f doesn't hang, per POSIX + +# Copyright (C) 2009 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +if test "$VERBOSE" = yes; then + set -x + tail --version +fi + +. $srcdir/test-lib.sh + +fail=0 +echo foo | timeout 2 tail -f -c3 > out || fail=1 +echo oo > exp || fail=1 + +compare out exp || fail=1 + +Exit $fail diff --git a/tests/tail-2/pipe-f2 b/tests/tail-2/pipe-f2 new file mode 100755 index 000000000..406ebcc91 --- /dev/null +++ b/tests/tail-2/pipe-f2 @@ -0,0 +1,37 @@ +#!/bin/sh +# Ensure that "tail -f fifo" tails indefinitely. + +# Copyright (C) 2009 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +if test "$VERBOSE" = yes; then + set -x + tail --version +fi + +. $srcdir/test-lib.sh + +mkfifo_or_skip_ fifo + +echo 1 > fifo & +echo 1 > exp || framework_failure + +fail=0 +timeout 1 tail -f fifo > out +test $? = 124 || fail=1 + +compare out exp || fail=1 + +Exit $fail |