1#!/bin/sh
2# Make sure that 'tail -f' returns immediately if a file doesn't exist
3# while 'tail -F' waits for it to appear.
4
5# Copyright (C) 2003-2020 Free Software Foundation, Inc.
6
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
20. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
21print_ver_ tail
22
23grep '^#define HAVE_INOTIFY 1' "$CONFIG_HEADER" >/dev/null \
24  && HAVE_INOTIFY=1
25
26inotify_failed_re='inotify (resources exhausted|cannot be used)'
27
28touch here || framework_failure_
29{ touch unreadable && chmod a-r unreadable; } || framework_failure_
30
31# Terminate any background tail process
32cleanup_() { kill $pid 2>/dev/null && wait $pid; }
33
34# speedup non inotify case
35fastpoll='-s.1 --max-unchanged-stats=1'
36
37for mode in '' '---disable-inotify'; do
38  returns_ 124 timeout 10 tail $fastpoll -f $mode not_here && fail=1
39
40  if test ! -r unreadable; then # can't test this when root
41    returns_ 124 timeout 10 tail $fastpoll -f $mode unreadable && fail=1
42  fi
43
44  returns_ 124 timeout .1 tail $fastpoll -f $mode here 2>tail.err || fail=1
45
46  # 'tail -F' must wait in any case.
47
48  returns_ 124 timeout .1 tail $fastpoll -F $mode here 2>>tail.err || fail=1
49
50  if test ! -r unreadable; then # can't test this when root
51    returns_ 124 timeout .1 tail $fastpoll -F $mode unreadable || fail=1
52  fi
53
54  returns_ 124 timeout .1 tail $fastpoll -F $mode not_here || fail=1
55
56  grep -Ev "$inotify_failed_re" tail.err > x
57  mv x tail.err
58  compare /dev/null tail.err || fail=1
59  >tail.err
60done
61
62if test "$HAVE_INOTIFY" && test -z "$mode" && is_local_dir_ .; then
63  # Ensure -F never follows a descriptor after rename
64  # either with tiny or significant delays between operations
65  tail_F()
66  {
67    local delay="$1"
68
69    > k && > tail.out && > tail.err || framework_failure_
70    tail $fastpoll -F $mode k >tail.out 2>tail.err & pid=$!
71    sleep $delay
72    mv k l
73    sleep $delay
74    touch k
75    mv k l
76    sleep $delay
77    echo NO >> l
78    sleep $delay
79    cleanup_
80    rm -f k l
81
82    test -s tail.out \
83      && ! grep -E "$inotify_failed_re" tail.err >/dev/null
84  }
85
86  retry_delay_ tail_F 0 1 && { cat tail.out; fail=1; }
87  retry_delay_ tail_F .2 1 && { cat tail.out; fail=1; }
88fi
89
90Exit $fail
91