From: Jim Meyering Date: Mon, 7 Sep 2009 20:10:10 +0000 (+0200) Subject: tail: ignore -f for piped-stdin, as POSIX requires X-Git-Tag: v7.6~11 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=af6436559c2954d7f35b7fec29dfcbaa4ebdc03b;p=platform%2Fupstream%2Fcoreutils.git 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. --- diff --git a/NEWS b/NEWS index a5a6094..7d4f7e2 100644 --- a/NEWS +++ b/NEWS @@ -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] diff --git a/THANKS b/THANKS index 2410866..c6655eb 100644 --- a/THANKS +++ b/THANKS @@ -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 9288007..d75d9b3 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 6b3c2b1..42a12cf 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 0000000..b9f6ae3 --- /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 . + +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 0000000..406ebcc --- /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 . + +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