1#!/bin/sh
2# Ensure that tail does not ignore a tailed-forever file that has been
3# replaced between tail's initial read-to-EOF, and when the inotify watches
4# are established in tail_forever_inotify.  That new file would be ignored
5# indefinitely.
6
7# Copyright (C) 2015-2023 Free Software Foundation, Inc.
8
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18
19# You should have received a copy of the GNU General Public License
20# along with this program.  If not, see <https://www.gnu.org/licenses/>.
21
22. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
23print_ver_ tail sleep
24
25grep '^#define HAVE_INOTIFY 1' "$CONFIG_HEADER" >/dev/null && is_local_dir_ . \
26  || skip_ 'inotify is not supported'
27
28# Terminate any background gdb/tail process
29cleanup_() {
30  kill $pid 2>/dev/null && wait $pid
31  kill $sleep 2>/dev/null && wait $sleep
32}
33
34touch file || framework_failure_
35touch tail.out || framework_failure_
36
37( timeout 10s gdb --version ) > gdb.out 2>&1
38case $(cat gdb.out) in
39    *'GNU gdb'*) ;;
40    *) skip_ "can't run gdb";;
41esac
42
43# Break on a line rather than a symbol, to cater for inline functions
44break_src="$abs_top_srcdir/src/tail.c"
45break_line=$(grep -n ^tail_forever_inotify "$break_src") || framework_failure_
46break_line=$(echo "$break_line" | cut -d: -f1) || framework_failure_
47
48
49# Note we get tail to monitor a background sleep process
50# rather than using timeout(1), as timeout sends SIGCONT
51# signals to its monitored process, and gdb (7.9 at least)
52# has _intermittent_ issues with this.
53# Sending SIGCONT resulted in either delayed child termination,
54# or no child termination resulting in a hung test.
55# See https://sourceware.org/bugzilla/show_bug.cgi?id=18364
56
57env sleep 10 & sleep=$!
58
59# See if gdb works and
60# tail_forever_inotify is compiled and run
61gdb -nx --batch-silent \
62    --eval-command="break $break_line"             \
63    --eval-command="run --pid=$sleep -f file"      \
64    --eval-command='quit'                          \
65    tail < /dev/null > gdb.out 2>&1
66
67kill $sleep || skip_ 'breakpoint not hit'
68wait $sleep
69
70# FIXME: The above is seen to _intermittently_ fail with:
71# warning: .dynamic section for "/lib/libc.so.6" is not at the expected address
72# warning: difference appears to be caused by prelink, adjusting expectations
73compare /dev/null gdb.out || skip_ "can't set breakpoints in tail"
74
75env sleep 10 & sleep=$!
76
77echo never-seen-with-tail-8.23 > file.new || framework_failure_
78
79# Run "tail -F file", stopping to replace with a new file before
80# inotify initialization, and then continue.  Before the fix,
81# changes to the new file would effectively be ignored.
82gdb -nx --batch-silent \
83    --eval-command="break $break_line"             \
84    --eval-command="run --pid=$sleep -F file 2>tail.err >>tail.out"       \
85    --eval-command='shell mv file.new file'        \
86    --eval-command='continue'                      \
87    --eval-command='quit'                          \
88    tail < /dev/null > /dev/null 2>&1 & pid=$!
89
90# Note even updating the watched 'file' wouldn't have output
91# anything between coreutils 7.5 and 8.23 inclusive as
92# The old file descriptor (still held open by tail) was being fstat().
93
94tail --pid=$pid -f tail.out | (read REPLY; kill $pid)
95
96# gdb has a bug in Debian's gdb-6.8-3 at least that causes it to not
97# cleanup and exit correctly when it receives a SIGTERM, but
98# killing sleep, should cause the tail process and thus gdb to exit.
99kill $sleep
100wait $sleep
101
102wait $pid
103
104compare /dev/null tail.out && { cat tail.err; fail=1; }
105
106Exit $fail
107