2 # Exercise tail's behavior regarding missing files with/without --retry.
4 # Copyright (C) 2013-2023 Free Software Foundation, Inc.
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.
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.
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/>.
19 .
"${srcdir=.}/tests/init.sh"; path_prepend_ .
/src
22 # Function to count number of lines from tail
23 # while ignoring transient errors due to resource limits
26 grep -Ev 'inotify (resources exhausted|cannot be used)' out |
wc -l
29 # Function to check the expected line count in 'out'.
30 # Called via retry_delay_(). Sleep some time - see retry_delay_() - if the
31 # line count is still smaller than expected.
35 local elc
=$2 # Expected line count.
36 [ "$(countlines_)" -ge "$elc" ] ||
{ sleep $delay; return 1; }
39 # Terminate any background tail process
40 cleanup_
() { kill $pid 2>/dev
/null
&& wait $pid; }
42 # Speedup the non inotify case
43 fastpoll
='-s.1 --max-unchanged-stats=1'
46 # Retry without --follow results in a warning.
48 tail --retry file > out
2>&1 || fail
=1
49 [ "$(countlines_)" = 1 ] ||
{ cat out
; fail
=1; }
50 grep -F 'tail: warning: --retry ignored' out ||
{ cat out
; fail
=1; }
53 # The same with a missing file: expect error message and exit 1.
54 returns_
1 tail --retry missing
> out
2>&1 || fail
=1
55 [ "$(countlines_)" = 2 ] ||
{ cat out
; fail
=1; }
56 grep -F 'tail: warning: --retry ignored' out ||
{ cat out
; fail
=1; }
58 for mode
in '' '---disable-inotify'; do
61 # Ensure that "tail --retry --follow=name" waits for the file to appear.
62 # Clear 'out' so that we can check its contents without races
63 >out || framework_failure_
65 tail $mode $fastpoll --follow=name
--retry missing
>out
2>&1 & pid
=$
!
66 # Wait for "cannot open" error.
67 retry_delay_ wait4lines_
.1 6 1 ||
{ cat out
; fail
=1; }
68 echo "X" > missing || framework_failure_
69 # Wait for the expected output.
70 retry_delay_ wait4lines_
.1 6 3 ||
{ cat out
; fail
=1; }
72 # Expect 3 lines in the output file.
73 [ "$(countlines_)" = 3 ] ||
{ fail
=1; cat out
; }
74 grep -F 'cannot open' out ||
{ fail
=1; cat out
; }
75 grep -F 'has appeared' out ||
{ fail
=1; cat out
; }
76 grep '^X$' out ||
{ fail
=1; cat out
; }
77 rm -f missing out || framework_failure_
80 # Ensure that "tail --retry --follow=descriptor" waits for the file to appear.
81 # tail-8.21 failed at this (since the implementation of the inotify support).
83 tail $mode $fastpoll --follow=descriptor
--retry missing
>out
2>&1 & pid
=$
!
84 # Wait for "cannot open" error.
85 retry_delay_ wait4lines_
.1 6 2 ||
{ cat out
; fail
=1; }
86 echo "X1" > missing || framework_failure_
87 # Wait for the expected output.
88 retry_delay_ wait4lines_
.1 6 4 ||
{ cat out
; fail
=1; }
89 # Ensure truncation is detected
90 # tail-8.25 failed at this (as assumed non file and went into blocking mode)
91 echo "X" > missing || framework_failure_
92 retry_delay_ wait4lines_
.1 6 6 ||
{ cat out
; fail
=1; }
94 [ "$(countlines_)" = 6 ] ||
{ fail
=1; cat out
; }
95 grep -F 'retry only effective for the initial open' out \
96 ||
{ fail
=1; cat out
; }
97 grep -F 'cannot open' out ||
{ fail
=1; cat out
; }
98 grep -F 'has appeared' out ||
{ fail
=1; cat out
; }
99 grep '^X1$' out ||
{ fail
=1; cat out
; }
100 grep -F 'file truncated' out ||
{ fail
=1; cat out
; }
101 grep '^X$' out ||
{ fail
=1; cat out
; }
102 rm -f missing out || framework_failure_
105 # Ensure that tail --follow=descriptor --retry exits when the file appears
106 # untailable. Expect exit status 1.
108 tail $mode $fastpoll --follow=descriptor
--retry missing
>out
2>&1 & pid
=$
!
109 # Wait for "cannot open" error.
110 retry_delay_ wait4lines_
.1 6 2 ||
{ cat out
; fail
=1; }
111 mkdir missing || framework_failure_
# Create untailable
112 # Wait for the expected output.
113 retry_delay_ wait4lines_
.1 6 4 ||
{ cat out
; fail
=1; }
116 [ "$(countlines_)" = 4 ] ||
{ fail
=1; cat out
; }
117 grep -F 'retry only effective for the initial open' out \
118 ||
{ fail
=1; cat out
; }
119 grep -F 'cannot open' out ||
{ fail
=1; cat out
; }
120 grep -F 'replaced with an untailable file' out ||
{ fail
=1; cat out
; }
121 grep -F 'no files remaining' out ||
{ fail
=1; cat out
; }
122 [ $rc = 1 ] ||
{ fail
=1; cat out
; }
123 rm -fd missing out || framework_failure_
126 # Ensure that --follow=descriptor (without --retry) does *not* try
127 # to open a file after an initial fail, even when there are other
128 # tailable files. This was an issue in <= 8.25.
129 touch existing || framework_failure_
130 tail $mode $fastpoll --follow=descriptor missing existing
>out
2>&1 & pid
=$
!
131 retry_delay_ wait4lines_
.1 6 2 ||
{ cat out
; fail
=1; }
132 [ "$(countlines_)" = 2 ] ||
{ fail
=1; cat out
; }
133 grep -F 'cannot open' out ||
{ fail
=1; cat out
; }
134 echo "Y" > missing || framework_failure_
135 echo "X" > existing || framework_failure_
136 retry_delay_ wait4lines_
.1 6 3 ||
{ cat out
; fail
=1; }
137 [ "$(countlines_)" = 3 ] ||
{ fail
=1; cat out
; }
138 grep '^X$' out ||
{ fail
=1; cat out
; }
139 grep '^Y$' out
&& { fail
=1; cat out
; }
141 rm -f missing out existing || framework_failure_
144 # Ensure that --follow=descriptor (without --retry) does *not wait* for the
145 # file to appear. Expect 2 lines in the output file ("cannot open" +
146 # "no files remaining") and exit status 1.
147 returns_
1 tail $mode --follow=descriptor missing
>out
2>&1 || fail
=1
148 [ "$(countlines_)" = 2 ] ||
{ fail
=1; cat out
; }
149 grep -F 'cannot open' out ||
{ fail
=1; cat out
; }
150 grep -F 'no files remaining' out ||
{ fail
=1; cat out
; }
151 rm -f out || framework_failure_
154 # Likewise for --follow=name (without --retry).
155 returns_
1 tail $mode --follow=name missing
>out
2>&1 || fail
=1
156 [ "$(countlines_)" = 2 ] ||
{ fail
=1; cat out
; }
157 grep -F 'cannot open' out ||
{ fail
=1; cat out
; }
158 grep -F 'no files remaining' out ||
{ fail
=1; cat out
; }
159 rm -f out || framework_failure_
162 # Ensure that tail -F retries when the file is initially untailable.
163 if ! cat .
>/dev
/null
; then
164 mkdir untailable || framework_failure_
166 tail $mode $fastpoll -F untailable
>out
2>&1 & pid
=$
!
167 # Wait for "cannot follow" error.
168 retry_delay_ wait4lines_
.1 6 2 ||
{ cat out
; fail
=1; }
169 { rmdir untailable
; echo foo
> untailable
; } || framework_failure_
170 # Wait for the expected output.
171 retry_delay_ wait4lines_
.1 6 4 ||
{ cat out
; fail
=1; }
173 [ "$(countlines_)" = 4 ] ||
{ fail
=1; cat out
; }
174 grep -F 'cannot follow' out ||
{ fail
=1; cat out
; }
175 # The first is the common case, "has appeared" arises with slow rmdir.
176 grep -E 'become accessible|has appeared' out ||
{ fail
=1; cat out
; }
177 grep -F 'giving up' out
&& { fail
=1; cat out
; }
178 grep -F 'foo' out ||
{ fail
=1; cat out
; }
179 rm -fd untailable out || framework_failure_