1#!/bin/sh 2# Ensure tail tracks symlinks properly. 3 4# Copyright (C) 2013-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# Function to count number of lines from tail 23# while ignoring transient errors due to resource limits 24countlines_ () 25{ 26 grep -Ev 'inotify (resources exhausted|cannot be used)' out | wc -l 27} 28 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. 32wait4lines_ () 33{ 34 local delay=$1 35 local elc=$2 # Expected line count. 36 [ "$(countlines_)" -ge "$elc" ] || { sleep $delay; return 1; } 37} 38 39# Terminate any background tail process 40cleanup_() { kill $pid 2>/dev/null && wait $pid; } 41 42# speedup non inotify case 43fastpoll='-s.1 --max-unchanged-stats=1' 44 45# Ensure changing targets of cli specified symlinks are handled. 46# Prior to v8.22, inotify would fail to recognize changes in the targets. 47# Clear 'out' so that we can check its contents without races. 48>out || framework_failure_ 49ln -nsf target symlink || framework_failure_ 50timeout 10 tail $fastpoll -F symlink >out 2>&1 & pid=$! 51# Wait for "cannot open..." 52retry_delay_ wait4lines_ .1 6 1 || { cat out; fail=1; } 53echo "X" > target || framework_failure_ 54# Wait for the expected output. 55retry_delay_ wait4lines_ .1 6 3 || { cat out; fail=1; } 56cleanup_ 57# Expect 3 lines in the output file. 58[ "$(countlines_)" = 3 ] || { fail=1; cat out; } 59grep -F 'cannot open' out || { fail=1; cat out; } 60grep -F 'has appeared' out || { fail=1; cat out; } 61grep '^X$' out || { fail=1; cat out; } 62rm -f target out || framework_failure_ 63 64# Ensure we correctly handle the source symlink itself changing. 65# I.e., that we don't operate solely on the targets. 66# Clear 'out' so that we can check its contents without races. 67>out || framework_failure_ 68echo "X1" > target1 || framework_failure_ 69ln -nsf target1 symlink || framework_failure_ 70timeout 10 tail $fastpoll -F symlink >out 2>&1 & pid=$! 71# Wait for the expected output. 72retry_delay_ wait4lines_ .1 6 1 || { cat out; fail=1; } 73ln -nsf target2 symlink || framework_failure_ 74# Wait for "become inaccess..." 75retry_delay_ wait4lines_ .1 6 2 || { cat out; fail=1; } 76echo "X2" > target2 || framework_failure_ 77# Wait for the expected output. 78retry_delay_ wait4lines_ .1 6 4 || { cat out; fail=1; } 79cleanup_ 80# Expect 4 lines in the output file. 81[ "$(countlines_)" = 4 ] || { fail=1; cat out; } 82grep -F 'become inacce' out || { fail=1; cat out; } 83grep -F 'has appeared' out || { fail=1; cat out; } 84grep '^X1$' out || { fail=1; cat out; } 85grep '^X2$' out || { fail=1; cat out; } 86rm -f target1 target2 out || framework_failure_ 87 88# Note other symlink edge cases are currently just diagnosed 89# rather than being handled. I.e., if you specify a missing item, 90# or existing file that later change to a symlink, if inotify 91# is in use, you'll get a diagnostic saying that link will 92# no longer be tailed. 93 94Exit $fail 95