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".]
free (tmp);
- if (n_read < 0)
+ if (n_read < 0 && errno != EAGAIN)
{
error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
ok = false;
free (tmp);
- if (n_read < 0)
+ if (n_read < 0 && errno != EAGAIN)
{
error (0, errno, _("error reading %s"), quoteaf (pretty_filename));
ok = false;
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)
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);
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))
{
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 \
--- /dev/null
+#!/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