xref: /titanic_44/usr/src/lib/libshell/common/tests/sigchld.sh (revision 95efa359b507db290a2484b8f615822b1e06096f)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 1982-2008 AT&T Intellectual Property          #
5#                      and is licensed under the                       #
6#                  Common Public License, Version 1.0                  #
7#                    by AT&T Intellectual Property                     #
8#                                                                      #
9#                A copy of the License is available at                 #
10#            http://www.opensource.org/licenses/cpl1.0.txt             #
11#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
12#                                                                      #
13#              Information and Software Systems Research               #
14#                            AT&T Research                             #
15#                           Florham Park NJ                            #
16#                                                                      #
17#                  David Korn <dgk@research.att.com>                   #
18#                                                                      #
19########################################################################
20function err_exit
21{
22	print -u2 -n "\t"
23	print -u2 -r ${Command}[$1]: "${@:2}"
24	(( Errors+=1 ))
25}
26
27alias err_exit='err_exit $LINENO'
28
29float DELAY=${1:-0.5}
30integer FOREGROUND=10 BACKGROUND=2 Errors=0
31
32s=$($SHELL -c '
33integer i foreground=0 background=0
34float delay='$DELAY' d=0 s=0
35
36set --errexit
37
38trap "(( background++ ))" CHLD
39
40(( d = delay ))
41for ((i = 0; i < '$BACKGROUND'; i++))
42do	sleep $d &
43	(( d *= 4 ))
44	(( s += d ))
45done
46for ((i = 0; i < '$FOREGROUND'; i++))
47do	(( foreground++ ))
48	sleep $delay
49	(( s -= delay ))
50	$SHELL -c : > /dev/null # foreground does not generate SIGCHLD
51done
52if	(( (s += delay) < 1 ))
53then	(( s = 1 ))
54fi
55sleep $s
56wait
57print foreground=$foreground background=$background
58') || err_exit "test loop failed"
59
60eval $s
61
62(( foreground == FOREGROUND )) || err_exit "expected $FOREGROUND foreground -- got $foreground (DELAY=$DELAY)"
63(( background == BACKGROUND )) || err_exit "expected $BACKGROUND background -- got $background (DELAY=$DELAY)"
64
65exit $((Errors))
66