tests: avoid false failure in tail inotify test
[coreutils.git] / tests / misc / timeout-group.sh
blobb79316f95d55567725428efb00df12ea15252b34
1 #!/bin/sh
2 # test program group handling
4 # Copyright (C) 2011-2016 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 <http://www.gnu.org/licenses/>.
19 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
20 print_ver_ timeout
21 require_trap_signame_
22 require_kill_group_
24 # construct a program group hierarchy as follows:
25 # timeout-group - foreground group
26 # group.sh - separate group
27 # timeout.cmd - same group as group.sh
29 # We then send a SIGINT to the "separate group"
30 # to simulate what happens when a Ctrl-C
31 # is sent to the foreground group.
33 setsid true || skip_ "setsid required to control groups"
35 printf '%s\n' '#!'"$SHELL" > timeout.cmd || framework_failure_
36 cat >> timeout.cmd <<\EOF
37 trap 'touch int.received; exit' INT
38 touch timeout.running
39 count=$1
40 until test -e int.received || test $count = 0; do
41 sleep 1
42 count=$(expr $count - 1)
43 done
44 EOF
45 chmod a+x timeout.cmd
47 cat > group.sh <<EOF
48 #!$SHELL
49 trap '' INT
50 timeout --foreground 25 ./timeout.cmd 20&
51 wait
52 EOF
53 chmod a+x group.sh
55 check_timeout_cmd_running()
57 local delay="$1"
58 test -e timeout.running ||
59 { sleep $delay; return 1; }
62 # Terminate any background processes
63 cleanup_() { kill $pid 2>/dev/null && wait $pid; }
65 # Start above script in its own group.
66 # We could use timeout for this, but that assumes an implementation.
67 setsid ./group.sh & pid=$!
68 # Wait 6.3s for timeout.cmd to start
69 retry_delay_ check_timeout_cmd_running .1 6 || fail=1
70 # Simulate a Ctrl-C to the group to test timely exit
71 kill -INT -- -$pid
72 wait
73 test -e int.received || fail=1
75 rm -f int.received timeout.running
78 # Ensure cascaded timeouts work
79 # or more generally, ensure we timeout
80 # commands that create their own group
81 # This didn't work before 8.13.
83 start=$(date +%s)
85 # Note the first timeout must send a signal that
86 # the second is handling for it to be propagated to the command.
87 # SIGINT, SIGTERM, SIGALRM etc. are implicit.
88 timeout -sALRM 30 timeout -sINT 25 ./timeout.cmd 20 & pid=$!
89 # Wait 6.3s for timeout.cmd to start
90 retry_delay_ check_timeout_cmd_running .1 6 || fail=1
91 kill -ALRM $pid # trigger the alarm of the first timeout command
92 wait $pid
93 ret=$?
94 test $ret -eq 124 ||
95 skip_ "timeout returned $ret. SIGALRM not handled?"
96 test -e int.received || fail=1
98 end=$(date +%s)
100 test $(expr $end - $start) -lt 20 ||
101 skip_ "timeout.cmd didn't receive a signal until after sleep?"
103 Exit $fail