Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/ksh93/tests/sigchld.sh
1810 views
1
########################################################################
2
# #
3
# This software is part of the ast package #
4
# Copyright (c) 1982-2012 AT&T Intellectual Property #
5
# and is licensed under the #
6
# Eclipse Public License, Version 1.0 #
7
# by AT&T Intellectual Property #
8
# #
9
# A copy of the License is available at #
10
# http://www.eclipse.org/org/documents/epl-v10.html #
11
# (with md5 checksum b35adb5213ca9657e911e9befb180842) #
12
# #
13
# Information and Software Systems Research #
14
# AT&T Research #
15
# Florham Park NJ #
16
# #
17
# David Korn <[email protected]> #
18
# #
19
########################################################################
20
function err_exit
21
{
22
print -u2 -n "\t"
23
print -u2 -r ${Command}[$1]: "${@:2}"
24
(( Errors+=1 ))
25
}
26
27
alias err_exit='err_exit $LINENO'
28
29
float DELAY=${1:-0.2}
30
integer FOREGROUND=10 BACKGROUND=2 Errors=0
31
32
s=$($SHELL -c '
33
integer i foreground=0 background=0
34
float delay='$DELAY' d=0 s=0
35
36
set --errexit
37
38
trap "(( background++ ))" CHLD
39
40
(( d = delay ))
41
for ((i = 0; i < '$BACKGROUND'; i++))
42
do sleep $d &
43
(( d *= 4 ))
44
(( s += d ))
45
done
46
for ((i = 0; i < '$FOREGROUND'; i++))
47
do (( foreground++ ))
48
sleep $delay
49
(( s -= delay ))
50
$SHELL -c : > /dev/null # foreground does not generate SIGCHLD
51
done
52
if (( (s += delay) < 1 ))
53
then (( s = 1 ))
54
fi
55
sleep $s
56
wait
57
print foreground=$foreground background=$background
58
') || err_exit "test loop failed"
59
60
eval $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
65
set --noerrexit
66
67
if [[ ${.sh.version} == Version?*([[:upper:]])J* ]]
68
then
69
70
jobmax=4
71
got=$($SHELL -c '
72
JOBMAX='$jobmax' JOBCOUNT=$(('$jobmax'*2))
73
integer running=0 maxrunning=0
74
trap "((running--))" CHLD
75
for ((i=0; i<JOBCOUNT; i++))
76
do sleep 1 &
77
if ((++running > maxrunning))
78
then ((maxrunning=running))
79
fi
80
done
81
wait
82
print running=$running maxrunning=$maxrunning
83
')
84
exp='running=0 maxrunning='$jobmax
85
[[ $got == $exp ]] || err_exit "SIGCHLD trap queueing failed -- expected '$exp', got '$got'"
86
87
got=$($SHELL -c '
88
typeset -A proc
89
90
trap "
91
print \${proc[\$!].name} \${proc[\$!].status} \$?
92
unset proc[\$!]
93
" CHLD
94
95
{ sleep 3; print a; exit 1; } &
96
proc[$!]=( name=a status=1 )
97
98
{ sleep 2; print b; exit 2; } &
99
proc[$!]=( name=b status=2 )
100
101
{ sleep 1; print c; exit 3; } &
102
proc[$!]=( name=c status=3 )
103
104
while (( ${#proc[@]} ))
105
do sleep -s
106
done
107
')
108
exp='c\nc 3 3\nb\nb 2 2\na\na 1 1'
109
[[ $got == $exp ]] || err_exit "SIGCHLD trap queueing failed -- expected $(printf %q "$exp"), got $(printf %q "$got")"
110
111
fi
112
113
{
114
got=$( ( sleep 1;print $'\n') | $SHELL -c 'function handler { : ;}
115
trap handler CHLD; sleep .3 & IFS= read; print good')
116
} 2> /dev/null
117
[[ $got == good ]] || err_exit 'SIGCLD handler effects read behavior'
118
119
set -- $(
120
(
121
$SHELL -xc $'
122
trap \'wait $!; print $! $?\' CHLD
123
{ sleep 0.1; exit 9; } &
124
print $!
125
sleep 0.5
126
'
127
) 2>/dev/null; print $?
128
)
129
if (( $# != 4 ))
130
then err_exit "CHLD trap failed -- expected 4 args, got $#"
131
elif (( $4 != 0 ))
132
then err_exit "CHLD trap failed -- exit code $4"
133
elif (( $1 != $2 ))
134
then err_exit "child pid mismatch -- got '$1' != '$2'"
135
elif (( $3 != 9 ))
136
then err_exit "child status mismatch -- expected '9', got '$3'"
137
fi
138
139
trap '' CHLD
140
integer d
141
for ((d=0; d < 2000; d++))
142
do if print foo | grep bar
143
then break
144
fi
145
done
146
(( d==2000 )) || err_exit "trap '' CHLD causes side effects d=$d"
147
trap - CHLD
148
149
tmp=$(mktemp -dt)
150
trap 'rm -rf $tmp' EXIT
151
x=$($SHELL 2> /dev/null -ic '/bin/notfound; sleep .5 & sleep 1;jobs')
152
[[ $x == *Done* ]] || err_exit 'SIGCHLD blocked after notfound'
153
x=$($SHELL 2> /dev/null -ic 'kill -0 12345678901234567876; sleep .5 & sleep 1;jobs')
154
[[ $x == *Done* ]] || err_exit 'SIGCHLD blocked after error message'
155
print 'set -o monitor;sleep .5 & sleep 1;jobs' > $tmp/foobar
156
chmod +x $tmp/foobar
157
x=$($SHELL -c "echo | $tmp/foobar")
158
[[ $x == *Done* ]] || err_exit 'SIGCHLD blocked for script at end of pipeline'
159
160
exit $((Errors<125?Errors:125))
161
162