bug-coreutils
[Top][All Lists]
Advanced

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

Re: tail -f problem


From: Jim Meyering
Subject: Re: tail -f problem
Date: Mon, 07 Sep 2009 22:48:32 +0200

Ulrich Drepper wrote:
> Jim Meyering wrote:
>> Thanks for the report!

Ulrich and Ren reported that the command,

  echo foobar | tail -c3 -f

would block indefinitely, while POSIX says that it must not.

Here's how I expect to fix this.
Maybe I'm splitting hairs, but rather than
call this a bug in NEWS, (it's more like GIGO) I've put
it in a new "POSIX conformance" category.

This seems small and safe enough that
I'm leaning toward including it in coreutils-7.6,
but I'll wait for a second opinion and/or review.

>From d4d7daeb391faafda795dff87212bbfe9611d26c Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Mon, 7 Sep 2009 22:10:10 +0200
Subject: [PATCH 1/2] 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.
---
 NEWS                 |    6 ++++++
 THANKS               |    1 +
 src/tail.c           |   16 +++++++++++++++-
 tests/Makefile.am    |    2 ++
 tests/tail-2/pipe-f  |   32 ++++++++++++++++++++++++++++++++
 tests/tail-2/pipe-f2 |   37 +++++++++++++++++++++++++++++++++++++
 6 files changed, 93 insertions(+), 1 deletions(-)
 create mode 100755 tests/tail-2/pipe-f
 create mode 100755 tests/tail-2/pipe-f2

diff --git a/NEWS b/NEWS
index a5a6094..9efe5fc 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'd 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                        address@hidden
 Raul Miller                         address@hidden
 Raúl Núñez de Arenas Coronado       address@hidden
 Reuben Thomas                       address@hidden
+Ren Yang                            address@hidden
 Richard A Downing                   address@hidden
 Richard Braakman                    address@hidden
 Richard Dawe                        address@hidden
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 <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 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 <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
--
1.6.4.2.419.gab238


>From aed1bee7a862c613e243050a0be14493ea6fedc4 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Mon, 7 Sep 2009 22:36:05 +0200
Subject: [PATCH 2/2] tail: syntax-only: use "false", rather than equivalent, 0

* src/tail.c (record_open_fd): Initialize "->ignore" to false, not 0.
---
 src/tail.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/src/tail.c b/src/tail.c
index d75d9b3..63874bb 100644
--- a/src/tail.c
+++ b/src/tail.c
@@ -358,7 +358,7 @@ record_open_fd (struct File_spec *f, int fd,
   f->mode = st->st_mode;
   f->blocking = blocking;
   f->n_unchanged_stats = 0;
-  f->ignore = 0;
+  f->ignore = false;
 }

 /* Close the file with descriptor FD and name FILENAME.  */
--
1.6.4.2.419.gab238




reply via email to

[Prev in Thread] Current Thread [Next in Thread]