timeout: add --foreground to support interactive commands
[platform/upstream/coreutils.git] / tests / misc / timeout-group
1 #!/bin/sh
2 # test program group handling
3
4 # Copyright (C) 2011 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 <http://www.gnu.org/licenses/>.
18
19 . "${srcdir=.}/init.sh"; path_prepend_ ../src
20 print_ver_ timeout
21
22 # construct a program group hierarchy as follows:
23 #  timeout-group - foreground group
24 #    group.sh - separate group
25 #      timeout.cmd - same group as group.sh
26 #
27 # We then send a SIGINT to the "separate group"
28 # to simulate what happens when a Ctrl-C
29 # is sent to the foreground group.
30
31 setsid true || skip_ "setsid required to control groups"
32
33 cat > timeout.cmd <<\EOF
34 #!/bin/sh
35 trap 'touch int.received; exit' INT
36 touch timeout.running
37 sleep 10
38 EOF
39 chmod a+x timeout.cmd
40
41 cat > group.sh <<\EOF
42 #!/bin/sh
43 timeout --foreground 5 ./timeout.cmd&
44 wait
45 EOF
46 chmod a+x group.sh
47
48 # Start above script in its own group.
49 # We could use timeout for this, but that assumes an implementation.
50 setsid ./group.sh &
51 until test -e timeout.running; do sleep .1; done
52 # Simulate a Ctrl-C to the group to test timely exit
53 # Note dash doesn't support signalling groups (a leading -)
54 env kill -INT -- -$!
55 wait
56 test -e int.received || fail=1
57
58 Exit $fail