1#!/bin/sh
2# Test tail -f -
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
21
22#################
23# tail -f - would fail with the initial inotify implementation
24
25check_tail_output()
26{
27  local delay="$1"
28  grep "$tail_re" out ||
29    { sleep $delay; return 1; }
30}
31
32# Terminate any background tail process
33cleanup_() { kill $pid 2>/dev/null && wait $pid; }
34
35# Speedup the non inotify case
36fastpoll='-s.1 --max-unchanged-stats=1'
37
38echo line > in || framework_failure_
39echo line > exp || framework_failure_
40
41for mode in '' '---disable-inotify'; do
42  > out || framework_failure_
43
44  tail $mode -f $fastpoll < in > out 2> err & pid=$!
45
46  # Wait up to 12.7s for output to appear:
47  tail_re='line' retry_delay_ check_tail_output .1 7 ||
48    { echo "$0: a: unexpected delay?"; cat out; fail=1; }
49
50  # Ensure there was no error output.
51  compare /dev/null err || fail=1
52
53  cleanup_
54done
55
56
57#################
58# Before coreutils-8.26 this would induce an UMR under UBSAN
59returns_ 1 timeout 10 tail -f - <&- 2>errt || fail=1
60cat <<\EOF >exp || framework_failure_
61tail: cannot fstat 'standard input'
62tail: error reading 'standard input'
63tail: no files remaining
64tail: -
65EOF
66sed 's/\(tail:.*\):.*/\1/' errt > err || framework_failure_
67compare exp err || fail=1
68
69
70#################
71# Before coreutils-8.28 this would erroneously issue a warning
72if tty -s </dev/tty && test -t 0; then
73  for input in '' '-' '/dev/tty'; do
74    returns_ 124 timeout 0.1 tail -f $input 2>err || fail=1
75    compare /dev/null err || fail=1
76  done
77
78  tail -f - /dev/null </dev/tty 2> out & pid=$!
79  # Wait up to 12.7s for output to appear:
80  tail_re='ineffective' retry_delay_ check_tail_output .1 7 ||
81    { cat out; fail=1; }
82  cleanup_
83fi
84
85Exit $fail
86