1#!/bin/sh
2# ensure that tail -f doesn't hang in various cases
3
4# Copyright (C) 2009-2023 Free Software Foundation, Inc.
5
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
19. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20print_ver_ tail test head
21trap_sigpipe_or_skip_
22
23# Speedup the non inotify case
24fastpoll='-s.1 --max-unchanged-stats=1'
25
26for mode in '' '---disable-inotify'; do
27# Ensure :|tail -f doesn't hang, per POSIX
28echo oo > exp || framework_failure_
29echo foo | timeout 10 tail -f $mode $fastpoll -c3 > out || fail=1
30compare exp out || fail=1
31cat <<\EOF > exp || framework_failure_
32==> standard input <==
33ar
34EOF
35echo bar | returns_ 1 \
36 timeout 10 tail -f $mode $fastpoll -c3 - missing > out || fail=1
37compare exp out || fail=1
38
39# This would wait indefinitely before v8.28 due to no EPIPE being
40# generated due to no data written after the first small amount.
41# Also check tail exits if SIGPIPE is being ignored.
42# Note 'trap - SIGPIPE' is ineffective if the initiating shell
43# has ignored SIGPIPE, but that's not the normal case.
44case $host_triplet in
45  *aix*) echo  'avoiding due to no way to detect closed outputs on AIX' ;;
46  *)
47for disposition in '' '-'; do
48  (trap "$disposition" PIPE;
49   returns_ 124 timeout 10 \
50    tail -n2 -f $mode $fastpoll out && touch timed_out) |
51  head -n2 > out2
52  test -e timed_out && fail=1
53  compare exp out2 || fail=1
54  rm -f timed_out
55done ;;
56esac
57
58# This would wait indefinitely before v8.28 (until first write)
59# test -w /dev/stdout is used to check that >&- is effective
60# which was seen not to be the case on NetBSD 7.1 / x86_64:
61if env test -w /dev/stdout >/dev/null &&
62   env test ! -w /dev/stdout >&-; then
63  (returns_ 1 timeout 10 tail -f $mode $fastpoll /dev/null >&-) || fail=1
64fi
65done
66
67Exit $fail
68