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.
This commit is contained in:
Pádraig Brady
2024-10-18 13:40:13 +01:00
parent 10406103b8
commit 177fcec66d
4 changed files with 50 additions and 5 deletions

4
NEWS
View File

@@ -47,6 +47,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".]

View File

@@ -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))
{

View File

@@ -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 \

36
tests/tail/pid-pipe.sh Executable file
View File

@@ -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 $fastpoll --pid=$! fifo && fail=1
cleanup_
Exit $fail