1*9f1b8a78Schristos# $NetBSD: t_wait.sh,v 1.8 2016/03/31 16:22:54 christos Exp $ 228604916Sjruoho# 328604916Sjruoho# Copyright (c) 2008, 2009, 2010 The NetBSD Foundation, Inc. 428604916Sjruoho# All rights reserved. 528604916Sjruoho# 628604916Sjruoho# Redistribution and use in source and binary forms, with or without 728604916Sjruoho# modification, are permitted provided that the following conditions 828604916Sjruoho# are met: 928604916Sjruoho# 1. Redistributions of source code must retain the above copyright 1028604916Sjruoho# notice, this list of conditions and the following disclaimer. 1128604916Sjruoho# 2. Redistributions in binary form must reproduce the above copyright 1228604916Sjruoho# notice, this list of conditions and the following disclaimer in the 1328604916Sjruoho# documentation and/or other materials provided with the distribution. 1428604916Sjruoho# 1528604916Sjruoho# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 1628604916Sjruoho# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 1728604916Sjruoho# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 1828604916Sjruoho# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 1928604916Sjruoho# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2028604916Sjruoho# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2128604916Sjruoho# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2228604916Sjruoho# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2328604916Sjruoho# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2428604916Sjruoho# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2528604916Sjruoho# POSSIBILITY OF SUCH DAMAGE. 2628604916Sjruoho# 27b189a005Schristos# the implementation of "sh" to test 28b189a005Schristos: ${TEST_SH:="/bin/sh"} 2928604916Sjruoho 30e0530cd4Schristosatf_test_case basic_wait 31e0530cd4Schristosbasic_wait_head() { 32e0530cd4Schristos atf_set "descr" "Tests simple uses of wait" 33e0530cd4Schristos} 34e0530cd4Schristosbasic_wait_body() { 35e0530cd4Schristos atf_require_prog sleep 36e0530cd4Schristos 37e0530cd4Schristos atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \ 38e0530cd4Schristos '(echo nothing >/dev/null) & wait' 39e0530cd4Schristos 40e0530cd4Schristos atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \ 41e0530cd4Schristos '(exit 3) & wait $!; S=$?; test $S -eq 3 || { 42e0530cd4Schristos echo "status: $S"; exit 1; }' 43e0530cd4Schristos 44e0530cd4Schristos atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \ 45e0530cd4Schristos 'sleep 3 & sleep 2 & sleep 1 & wait' 46e0530cd4Schristos 47e0530cd4Schristos atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \ 48e0530cd4Schristos 'sleep 3 & (exit 2) & sleep 1 & wait' 49e0530cd4Schristos} 50e0530cd4Schristos 5128604916Sjruohoatf_test_case individual 5228604916Sjruohoindividual_head() { 53e0530cd4Schristos atf_set "descr" "Tests that waiting for individual processes works" 5428604916Sjruoho} 5528604916Sjruohoindividual_body() { 56e0530cd4Schristos atf_require_prog sleep 57e0530cd4Schristos 58e0530cd4Schristos cat >individualhelper.sh <<\EOF 59e0530cd4Schristossleep 3 & P1=$! 60e0530cd4Schristossleep 1 & P2=$! 61e0530cd4Schristos 62e0530cd4Schristoswait ${P1} 63e0530cd4SchristosS=$? 64e0530cd4Schristosif [ $S -ne 0 ]; then 65e0530cd4Schristos echo "Waiting for first process failed: $S" 66e0530cd4Schristos exit 1 67e0530cd4Schristosfi 68e0530cd4Schristos 69e0530cd4Schristoswait ${P2} 70e0530cd4SchristosS=$? 71e0530cd4Schristosif [ $? -ne 0 ]; then 72e0530cd4Schristos echo "Waiting for second process failed" 73e0530cd4Schristos exit 1 74e0530cd4Schristosfi 75e0530cd4Schristos 76e0530cd4Schristosexit 0 77e0530cd4SchristosEOF 78e0530cd4Schristos output=$(${TEST_SH} individualhelper.sh 2>&1) 79e0530cd4Schristos [ $? -eq 0 ] || atf_fail "${output}" 80e0530cd4Schristos} 81e0530cd4Schristos 82e0530cd4Schristosatf_test_case jobs 83e0530cd4Schristosjobs_head() { 84e0530cd4Schristos atf_set "descr" "Tests that waiting for individual jobs works" 85e0530cd4Schristos} 86e0530cd4Schristosjobs_body() { 8728604916Sjruoho # atf-sh confuses wait for some reason; work it around by creating 8828604916Sjruoho # a helper script that executes /bin/sh directly. 89e0530cd4Schristos 90e0530cd4Schristos if ! ${TEST_SH} -c 'sleep 1 & wait %1' 2>/dev/null 91e0530cd4Schristos then 92e0530cd4Schristos atf_skip "No job control support in this shell" 93e0530cd4Schristos fi 94e0530cd4Schristos 957ec9eaaaSchristos cat >individualhelper.sh <<\EOF 9628604916Sjruohosleep 3 & 9728604916Sjruohosleep 1 & 9828604916Sjruoho 9928604916Sjruohowait %1 1007ec9eaaaSchristosif [ $? -ne 0 ]; then 101e0530cd4Schristos echo "Waiting for first job failed" 10228604916Sjruoho exit 1 10328604916Sjruohofi 10428604916Sjruoho 10528604916Sjruohowait %2 1067ec9eaaaSchristosif [ $? -ne 0 ]; then 107e0530cd4Schristos echo "Waiting for second job failed" 10828604916Sjruoho exit 1 10928604916Sjruohofi 11028604916Sjruoho 11128604916Sjruohoexit 0 11228604916SjruohoEOF 113e0530cd4Schristos output=$(${TEST_SH} individualhelper.sh 2>&1) 11428604916Sjruoho [ $? -eq 0 ] || atf_fail "${output}" 115e0530cd4Schristos 116e0530cd4Schristos cat >individualhelper.sh <<\EOF 117e0530cd4Schristos{ sleep 3; exit 3; } & 118e0530cd4Schristos{ sleep 1; exit 7; } & 119e0530cd4Schristos 120e0530cd4Schristoswait %1 121e0530cd4SchristosS=$? 122e0530cd4Schristosif [ $S -ne 3 ]; then 123e0530cd4Schristos echo "Waiting for first job failed - status: $S != 3 (expected)" 124e0530cd4Schristos exit 1 125e0530cd4Schristosfi 126e0530cd4Schristos 127e0530cd4Schristoswait %2 128e0530cd4SchristosS=$? 129e0530cd4Schristosif [ $S -ne 7 ]; then 130e0530cd4Schristos echo "Waiting for second job failed - status: $S != 7 (expected)" 131e0530cd4Schristos exit 1 132e0530cd4Schristosfi 133e0530cd4Schristos 134e0530cd4Schristosexit 0 135e0530cd4SchristosEOF 136e0530cd4Schristos 137e0530cd4Schristos output=$(${TEST_SH} individualhelper.sh 2>&1) 138e0530cd4Schristos [ $? -eq 0 ] || atf_fail "${output}" 1397ec9eaaaSchristos} 1407ec9eaaaSchristos 1417ec9eaaaSchristosatf_test_case kill 1427ec9eaaaSchristoskill_head() { 1437ec9eaaaSchristos atf_set "descr" "Tests that killing the shell while in wait calls trap" 1447ec9eaaaSchristos} 1457ec9eaaaSchristoskill_body() { 146e0530cd4Schristos atf_require_prog sleep 147e0530cd4Schristos atf_require_prog kill 1483f3bb7d2Sozaki-r 149e0530cd4Schristos s=killhelper.sh 150*9f1b8a78Schristos z=killhelper.$$ 151e0530cd4Schristos pid= 152e0530cd4Schristos 153e0530cd4Schristos # waiting for a specific process that is not a child 154e0530cd4Schristos # should return exit status of 127 according to the spec 155e0530cd4Schristos # This test is here before the next, to avoid that one 156e0530cd4Schristos # entering an infinite loop should the shell have a bug here. 157e0530cd4Schristos 158e0530cd4Schristos atf_check -s exit:127 -o empty -e ignore ${TEST_SH} -c 'wait 1' 159e0530cd4Schristos 160e0530cd4Schristos cat > "${s}" <<'EOF' 161e0530cd4Schristos 1627ec9eaaaSchristostrap "echo SIGHUP" 1 163b189a005Schristos(sleep 5; exit 3) & 1647ec9eaaaSchristossl=$! 1657ec9eaaaSchristoswait 166b189a005SchristosS=$? 167b189a005Schristosecho $S 168e0530cd4SchristosLS=9999 169e0530cd4Schristoswhile [ $S -ne 0 ] && [ $S != 127 ]; do 170e0530cd4Schristos wait $sl; S=$?; echo $S 171e0530cd4Schristos test $S = $LS && { echo "wait repeats..."; exit 2; } 172e0530cd4Schristos LS=$S 173e0530cd4Schristos done 1747ec9eaaaSchristosEOF 1753f3bb7d2Sozaki-r 176e0530cd4Schristos ${TEST_SH} $s > $z & 1773f3bb7d2Sozaki-r pid=$! 1783f3bb7d2Sozaki-r sleep 1 1793f3bb7d2Sozaki-r 180e0530cd4Schristos kill -HUP "${pid}" 181b189a005Schristos wait 1823f3bb7d2Sozaki-r 1837ec9eaaaSchristos output="$(cat $z | tr '\n' ' ')" 184e0530cd4Schristos 185b189a005Schristos if [ "$output" != "SIGHUP 129 3 127 " ]; then 186c74a9745Schristos atf_fail "${output} != 'SIGHUP 129 3 127 '" 1877ec9eaaaSchristos fi 18828604916Sjruoho} 18928604916Sjruoho 19028604916Sjruohoatf_init_test_cases() { 191e0530cd4Schristos atf_add_test_case basic_wait 19228604916Sjruoho atf_add_test_case individual 193e0530cd4Schristos atf_add_test_case jobs 1947ec9eaaaSchristos atf_add_test_case kill 19528604916Sjruoho} 196