xref: /minix3/tests/bin/sh/t_wait.sh (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc# $NetBSD: t_wait.sh,v 1.3 2015/09/30 06:08:36 ozaki-r Exp $
211be35a1SLionel Sambuc#
311be35a1SLionel Sambuc# Copyright (c) 2008, 2009, 2010 The NetBSD Foundation, Inc.
411be35a1SLionel Sambuc# All rights reserved.
511be35a1SLionel Sambuc#
611be35a1SLionel Sambuc# Redistribution and use in source and binary forms, with or without
711be35a1SLionel Sambuc# modification, are permitted provided that the following conditions
811be35a1SLionel Sambuc# are met:
911be35a1SLionel Sambuc# 1. Redistributions of source code must retain the above copyright
1011be35a1SLionel Sambuc#    notice, this list of conditions and the following disclaimer.
1111be35a1SLionel Sambuc# 2. Redistributions in binary form must reproduce the above copyright
1211be35a1SLionel Sambuc#    notice, this list of conditions and the following disclaimer in the
1311be35a1SLionel Sambuc#    documentation and/or other materials provided with the distribution.
1411be35a1SLionel Sambuc#
1511be35a1SLionel Sambuc# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1611be35a1SLionel Sambuc# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
1711be35a1SLionel Sambuc# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1811be35a1SLionel Sambuc# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
1911be35a1SLionel Sambuc# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2011be35a1SLionel Sambuc# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2111be35a1SLionel Sambuc# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2211be35a1SLionel Sambuc# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2311be35a1SLionel Sambuc# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2411be35a1SLionel Sambuc# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2511be35a1SLionel Sambuc# POSSIBILITY OF SUCH DAMAGE.
2611be35a1SLionel Sambuc#
2711be35a1SLionel Sambuc
2811be35a1SLionel Sambucatf_test_case individual
2911be35a1SLionel Sambucindividual_head() {
3011be35a1SLionel Sambuc	atf_set "descr" "Tests that waiting for individual jobs works"
3111be35a1SLionel Sambuc}
3211be35a1SLionel Sambucindividual_body() {
3311be35a1SLionel Sambuc	# atf-sh confuses wait for some reason; work it around by creating
3411be35a1SLionel Sambuc	# a helper script that executes /bin/sh directly.
35*0a6a1f1dSLionel Sambuc	cat >individualhelper.sh <<\EOF
3611be35a1SLionel Sambucsleep 3 &
3711be35a1SLionel Sambucsleep 1 &
3811be35a1SLionel Sambuc
3911be35a1SLionel Sambucwait %1
40*0a6a1f1dSLionel Sambucif [ $? -ne 0 ]; then
4111be35a1SLionel Sambuc    echo "Waiting of first job failed"
4211be35a1SLionel Sambuc    exit 1
4311be35a1SLionel Sambucfi
4411be35a1SLionel Sambuc
4511be35a1SLionel Sambucwait %2
46*0a6a1f1dSLionel Sambucif [ $? -ne 0 ]; then
4711be35a1SLionel Sambuc    echo "Waiting of second job failed"
4811be35a1SLionel Sambuc    exit 1
4911be35a1SLionel Sambucfi
5011be35a1SLionel Sambuc
5111be35a1SLionel Sambucexit 0
5211be35a1SLionel SambucEOF
53*0a6a1f1dSLionel Sambuc	output=$(/bin/sh individualhelper.sh)
5411be35a1SLionel Sambuc	[ $? -eq 0 ] || atf_fail "${output}"
55*0a6a1f1dSLionel Sambuc	rm -f individualhelper.sh
56*0a6a1f1dSLionel Sambuc}
57*0a6a1f1dSLionel Sambuc
58*0a6a1f1dSLionel Sambucatf_test_case kill
59*0a6a1f1dSLionel Sambuckill_head() {
60*0a6a1f1dSLionel Sambuc	atf_set "descr" "Tests that killing the shell while in wait calls trap"
61*0a6a1f1dSLionel Sambuc}
62*0a6a1f1dSLionel Sambuckill_body() {
63*0a6a1f1dSLionel Sambuc	# atf-sh confuses wait for some reason; work it around by creating
64*0a6a1f1dSLionel Sambuc	# a helper script that executes /bin/sh directly.
65*0a6a1f1dSLionel Sambuc	local s=$PWD/killhelper.sh
66*0a6a1f1dSLionel Sambuc	local z=/tmp/killhelper.$$
67*0a6a1f1dSLionel Sambuc	local pid=
68*0a6a1f1dSLionel Sambuc
69*0a6a1f1dSLionel Sambuc	cat >$s <<\EOF
70*0a6a1f1dSLionel Sambuc#!/bin/sh
71*0a6a1f1dSLionel Sambuctrap "echo SIGHUP" 1
72*0a6a1f1dSLionel Sambucsleep 10 &
73*0a6a1f1dSLionel Sambucsl=$!
74*0a6a1f1dSLionel Sambucwait
75*0a6a1f1dSLionel Sambucecho $?
76*0a6a1f1dSLionel SambucEOF
77*0a6a1f1dSLionel Sambuc	chmod +x $s
78*0a6a1f1dSLionel Sambuc
79*0a6a1f1dSLionel Sambuc	$s > $z &
80*0a6a1f1dSLionel Sambuc	pid=$!
81*0a6a1f1dSLionel Sambuc	sleep 1
82*0a6a1f1dSLionel Sambuc
83*0a6a1f1dSLionel Sambuc	# XXX: built-in kill does not work?
84*0a6a1f1dSLionel Sambuc	/bin/kill -HUP $pid
85*0a6a1f1dSLionel Sambuc	sleep 1
86*0a6a1f1dSLionel Sambuc
87*0a6a1f1dSLionel Sambuc	output="$(cat $z | tr '\n' ' ')"
88*0a6a1f1dSLionel Sambuc	rm -f $s $z
89*0a6a1f1dSLionel Sambuc	if [ "$output" != "SIGHUP 129 " ]; then
90*0a6a1f1dSLionel Sambuc		atf_fail "${output} != 'SIGHUP 129 '"
91*0a6a1f1dSLionel Sambuc	fi
9211be35a1SLionel Sambuc}
9311be35a1SLionel Sambuc
9411be35a1SLionel Sambucatf_init_test_cases() {
9511be35a1SLionel Sambuc	atf_add_test_case individual
96*0a6a1f1dSLionel Sambuc	atf_add_test_case kill
9711be35a1SLionel Sambuc}
98