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