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 a background process called in a subshell can
28# cause it to wait for the child process instead of exiting.
29#
30# This was reported as CR #6881017 ("Subshell doesn't exit, holds pipe
31# open preventing callers from exiting"):
32# ------------ snip ------------
33# The following scenario hangs with snv_122, 100% reproducible:
34#
35# Create a script hangit:
36# -----
37# #!/bin/ksh
38# ( sleep 100000 </dev/null >/dev/null 2>&1 & )
39# exit 0
40# -----
41#
42# Run the following command:
43# hangit | tee -a /tmp/log
44#
45# The hang can be eliminated either by removing the "exit 0" line (?!?), or by
46# redirecting the subshell output to /dev/null.
47#
48# This is pretty nasty. I've whittled it down to this simple case but am seeing
49# it in a much more subtle and complex environment where there are several
50# intermediate calling scripts which have exited and eventually the parent pipes
51# the output and hangs on the open pipe. It was hard to track down.
52# ------------ snip ------------
53#
54
55# test setup
56function err_exit
57{
58	print -u2 -n "\t"
59	print -u2 -r ${Command}[$1]: "${@:2}"
60	(( Errors < 127 && Errors++ ))
61}
62alias err_exit='err_exit $LINENO'
63
64set -o nounset
65Command=${0##*/}
66integer Errors=0
67
68float tstart tstop tdiff
69
70# run test with 10 second timeout
71(( tstart=SECONDS ))
72$SHELL -c '( sleep 10 </dev/null >/dev/null 2>&1 & ) ; exit 0' | cat >/dev/null
73(( tstop=SECONDS ))
74
75# we remove two seconds below to make sure we don't run into issues
76# with smaller xntpd adjustments
77(( tdiff=tstop-tstart ))
78(( tdiff < (10.-2.) )) || err_exit "test run needed ${tdiff} seconds to complete (instead of < 8.)"
79
80# tests done
81exit $((Errors))
82