xref: /illumos-gate/usr/src/cmd/ast/libshell/common/tests/sun_solaris_cr_6722134_background_CHLD_trap.sh (revision 3aa6c13072f3d4792a18693e916aed260a496c1f)
1#
2# CDDL HEADER START
3#
4# The contents of this file are subject to the terms of the
5# Common Development and Distribution License (the "License").
6# You may not use this file except in compliance with the License.
7#
8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9# or http://www.opensolaris.org/os/licensing.
10# See the License for the specific language governing permissions
11# and limitations under the License.
12#
13# When distributing Covered Code, include this CDDL HEADER in each
14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15# If applicable, add the following below this CDDL HEADER, with the
16# fields enclosed by brackets "[]" replaced with your own identifying
17# information: Portions Copyright [yyyy] [name of copyright owner]
18#
19# CDDL HEADER END
20#
21
22#
23# Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24#
25
26#
27# This test checks whether ksh93 (like ksh88) generates calls a
28# CHLD/SIGCHLD trap for background jobs and _not_ for foreground jobs.
29#
30# This was reported as CR #6722134 ("*ksh93* (20080624_snapshot)
31# doesn't execute CHLD trap"):
32# -- snip --
33# With "set -o monitor" on and "set -o notify" off, ksh88 executes the CHLD
34# trap while waiting for interactive input when a background job completes.
35# ksh93 appears not to execute the CHLD trap when a background job terminates.
36# Probably related:  I noticed that with no CHLD trap set, but -o monitor and
37# -o notify set, there should be a similar asynchronous job completion notice.
38# It works in ksh88 but not in this ksh93 build.
39# -- snip --
40#
41
42# test setup
43function err_exit
44{
45	print -u2 -n "\t"
46	print -u2 -r ${Command}[$1]: "${@:2}"
47	(( Errors < 127 && Errors++ ))
48}
49alias err_exit='err_exit $LINENO'
50
51set -o nounset
52Command=${0##*/}
53integer Errors=0
54
55
56##
57## test one:
58##
59s="$($SHELL -c '
60set -o errexit
61integer i
62
63trap "print got_child" SIGCHLD
64
65sleep 5 &
66sleep 7 &
67for ((i=0 ; i < 15 ; i++)) ; do
68      print $i
69      sleep 1
70
71      # external, non-background command for which a SIGCHLD should
72      # _not_ be fired
73      /bin/true >/dev/null
74done
75print "loop finished"
76wait
77print "done"
78' 2>&1 )" || err_exit "test loop failed."
79
80[[ "$s" == ~(Er)$'14\nloop finished\ndone' ]] || err_exit "Expected '14\nloop finished\ndone' at the end of the output, got ${s}."
81[[ "$s" == ~(El)$'0\n1\n2' ]] || err_exit "Expected '0\n1\n2' as at the beginning of the output, got ${s}."
82
83integer count
84(( count=$(fgrep "got_child" <<< "$s" | wc -l) )) || err_exit "counting failed."
85(( count == 2 )) || err_exit "Expected count==2, got count==${count}."
86
87
88##
89## test two:
90## (same as test "one" except that this test has one more "sleep" child)
91##
92s="$($SHELL -c '
93set -o errexit
94integer i
95
96trap "print got_child" SIGCHLD
97
98sleep 5 &
99sleep 7 &
100sleep 9 &
101for ((i=0 ; i < 15 ; i++)) ; do
102      print $i
103      sleep 1
104
105      # external, non-background command for which a SIGCHLD should
106      # _not_ be fired
107      /bin/true >/dev/null
108done
109print "loop finished"
110wait
111print "done"
112' 2>&1 )" || err_exit "test loop failed."
113
114[[ "$s" == ~(Er)$'14\nloop finished\ndone' ]] || err_exit "Expected '14\nloop finished\ndone' at the end of the output, got ${s}."
115[[ "$s" == ~(El)$'0\n1\n2' ]] || err_exit "Expected '0\n1\n2' as at the beginning of the output, got ${s}."
116
117(( count=$(fgrep "got_child" <<< "$s" | wc -l) )) || err_exit "counting failed."
118(( count == 3 )) || err_exit "Expected count==3, got count==${count}."
119
120
121# tests done
122exit $((Errors))
123