On 19/10/2024 08:48, Bernhard Voelker wrote:
Hi Padraig,
On 10/18/24 14:50, Pádraig Brady wrote:
In my testing the initial open() of the fifo blocks,
before the -f loops are processed.
If the monitored file was written by another process,
this_does_ unblock things for me, and --pid is processed.
The attached patch avoids the blocking behavior with fifos,
and thus honors the --pid checking in parallel (and passes all tests).
Thanks, this solves the issue for my case:
$ rm f; mkfifo f ; sleep 5 & timeout 10 ~/coreutils/src/tail -f --pid=$! f;
echo $?
[3] 1174778
[3]+ Done sleep 5
0
However I've only thought about this for a couple of minutes,
so need to consider all the implications.
I didn't have the time to check further either.
I'll push the attached later,
which is a little more conservative
in that it only opens with O_NONBLOCK if possibly needed.
cheers,
Pádraog
From 2e405c46dcf82f49a596026bcf0ac7d75926da82 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <p...@draigbrady.com>
Date: Fri, 18 Oct 2024 13:40:13 +0100
Subject: [PATCH] tail: honor --pid with fifos
* src/tail.c (tail_file): Open files with O_NONBLOCK
if we might need async processing.
(pipe_bytes): Ignore EAGAIN read() errors.
(pipe_lines): Likewise.
* tests/tail/pid-pipe.sh: Add a new test.
* tests/local.mk: Reference the new test.
* NEWS: Mention the bug fix.
Reported by Berhard Voelker.
---
NEWS | 4 ++++
src/tail.c | 14 +++++++++-----
tests/local.mk | 1 +
tests/tail/pid-pipe.sh | 36 ++++++++++++++++++++++++++++++++++++
4 files changed, 50 insertions(+), 5 deletions(-)
create mode 100755 tests/tail/pid-pipe.sh
diff --git a/NEWS b/NEWS
index 331a06358..dd7f365f8 100644
--- a/NEWS
+++ b/NEWS
@@ -44,6 +44,10 @@ GNU coreutils NEWS -*- outline -*-
where inotify is not used, when all followed files become inaccessible.
[This bug was present in "the beginning".]
+ `tail --follow --pid=PID` will now exit when the PID dies,
+ even in the presence of blocking inputs like unopened fifos.
+ [This bug was present in "the beginning".]
+
'tail -c 4096 /dev/zero' no longer loops forever.
[This bug was present in "the beginning".]
diff --git a/src/tail.c b/src/tail.c
index aa53d2519..30207e288 100644
--- a/src/tail.c
+++ b/src/tail.c
@@ -698,7 +698,7 @@ pipe_lines (char const *pretty_filename, int fd, uintmax_t n_lines,
free (tmp);
- if (n_read < 0)
+ if (n_read < 0 && errno != EAGAIN)
{
error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
ok = false;
@@ -826,7 +826,7 @@ pipe_bytes (char const *pretty_filename, int fd, uintmax_t n_bytes,
free (tmp);
- if (n_read < 0)
+ if (n_read < 0 && errno != EAGAIN)
{
error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
ok = false;
@@ -2003,11 +2003,14 @@ tail (char const *filename, int fd, uintmax_t n_units,
Return true if successful. */
static bool
-tail_file (struct File_spec *f, uintmax_t n_units)
+tail_file (struct File_spec *f, uintmax_t n_files, uintmax_t n_units)
{
int fd;
bool ok;
+ /* Avoid blocking if we may need to process asynchronously. */
+ bool nonblocking = forever && (nbpids || n_files > 1);
+
bool is_stdin = (STREQ (f->name, "-"));
if (is_stdin)
@@ -2017,7 +2020,8 @@ tail_file (struct File_spec *f, uintmax_t n_units)
xset_binary_mode (STDIN_FILENO, O_BINARY);
}
else
- fd = open (f->name, O_RDONLY | O_BINARY);
+ fd = open (f->name, O_RDONLY | O_BINARY
+ | nonblocking ? O_NONBLOCK : 0);
f->tailable = !(reopen_inaccessible_files && fd == -1);
@@ -2456,7 +2460,7 @@ main (int argc, char **argv)
xset_binary_mode (STDOUT_FILENO, O_BINARY);
for (i = 0; i < n_files; i++)
- ok &= tail_file (&F[i], n_units);
+ ok &= tail_file (&F[i], n_files, n_units);
if (forever && ignore_fifo_and_pipe (F, n_files))
{
diff --git a/tests/local.mk b/tests/local.mk
index bd8f2c876..12e30b449 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -268,6 +268,7 @@ all_tests = \
tests/misc/xstrtol.pl \
tests/tail/overlay-headers.sh \
tests/tail/pid.sh \
+ tests/tail/pid-pipe.sh \
tests/od/od.pl \
tests/od/od-endian.sh \
tests/od/od-float.sh \
diff --git a/tests/tail/pid-pipe.sh b/tests/tail/pid-pipe.sh
new file mode 100755
index 000000000..da0e0574c
--- /dev/null
+++ b/tests/tail/pid-pipe.sh
@@ -0,0 +1,36 @@
+#!/bin/sh
+# Ensure that "tail --pid=PID fifo" exits responsively
+
+# Copyright (C) 2025 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 <https://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+print_ver_ tail
+
+mkfifo_or_skip_ fifo
+
+# Terminate any background process
+cleanup_() { kill $pid 2>/dev/null && wait $pid; }
+
+# Speedup the non inotify case
+fastpoll='-s.1 --max-unchanged-stats=1'
+
+sleep 1 & pid=$!
+
+returns_ 124 timeout 10 tail -f --pid=$! fifo && fail=1
+
+cleanup_
+
+Exit $fail
--
2.47.1