1 1.8 christos # $NetBSD: t_wait.sh,v 1.8 2016/03/31 16:22:54 christos Exp $ 2 1.1 jruoho # 3 1.1 jruoho # Copyright (c) 2008, 2009, 2010 The NetBSD Foundation, Inc. 4 1.1 jruoho # All rights reserved. 5 1.1 jruoho # 6 1.1 jruoho # Redistribution and use in source and binary forms, with or without 7 1.1 jruoho # modification, are permitted provided that the following conditions 8 1.1 jruoho # are met: 9 1.1 jruoho # 1. Redistributions of source code must retain the above copyright 10 1.1 jruoho # notice, this list of conditions and the following disclaimer. 11 1.1 jruoho # 2. Redistributions in binary form must reproduce the above copyright 12 1.1 jruoho # notice, this list of conditions and the following disclaimer in the 13 1.1 jruoho # documentation and/or other materials provided with the distribution. 14 1.1 jruoho # 15 1.1 jruoho # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 16 1.1 jruoho # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17 1.1 jruoho # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 1.1 jruoho # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19 1.1 jruoho # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 1.1 jruoho # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 1.1 jruoho # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 1.1 jruoho # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 1.1 jruoho # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 1.1 jruoho # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 1.1 jruoho # POSSIBILITY OF SUCH DAMAGE. 26 1.1 jruoho # 27 1.4 christos # the implementation of "sh" to test 28 1.4 christos : ${TEST_SH:="/bin/sh"} 29 1.1 jruoho 30 1.7 christos atf_test_case basic_wait 31 1.7 christos basic_wait_head() { 32 1.7 christos atf_set "descr" "Tests simple uses of wait" 33 1.7 christos } 34 1.7 christos basic_wait_body() { 35 1.7 christos atf_require_prog sleep 36 1.7 christos 37 1.7 christos atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \ 38 1.7 christos '(echo nothing >/dev/null) & wait' 39 1.7 christos 40 1.7 christos atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \ 41 1.7 christos '(exit 3) & wait $!; S=$?; test $S -eq 3 || { 42 1.7 christos echo "status: $S"; exit 1; }' 43 1.7 christos 44 1.7 christos atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \ 45 1.7 christos 'sleep 3 & sleep 2 & sleep 1 & wait' 46 1.7 christos 47 1.7 christos atf_check -s exit:0 -o empty -e empty ${TEST_SH} -c \ 48 1.7 christos 'sleep 3 & (exit 2) & sleep 1 & wait' 49 1.7 christos } 50 1.7 christos 51 1.1 jruoho atf_test_case individual 52 1.1 jruoho individual_head() { 53 1.7 christos atf_set "descr" "Tests that waiting for individual processes works" 54 1.7 christos } 55 1.7 christos individual_body() { 56 1.7 christos atf_require_prog sleep 57 1.7 christos 58 1.7 christos cat >individualhelper.sh <<\EOF 59 1.7 christos sleep 3 & P1=$! 60 1.7 christos sleep 1 & P2=$! 61 1.7 christos 62 1.7 christos wait ${P1} 63 1.7 christos S=$? 64 1.7 christos if [ $S -ne 0 ]; then 65 1.7 christos echo "Waiting for first process failed: $S" 66 1.7 christos exit 1 67 1.7 christos fi 68 1.7 christos 69 1.7 christos wait ${P2} 70 1.7 christos S=$? 71 1.7 christos if [ $? -ne 0 ]; then 72 1.7 christos echo "Waiting for second process failed" 73 1.7 christos exit 1 74 1.7 christos fi 75 1.7 christos 76 1.7 christos exit 0 77 1.7 christos EOF 78 1.7 christos output=$(${TEST_SH} individualhelper.sh 2>&1) 79 1.7 christos [ $? -eq 0 ] || atf_fail "${output}" 80 1.7 christos } 81 1.7 christos 82 1.7 christos atf_test_case jobs 83 1.7 christos jobs_head() { 84 1.1 jruoho atf_set "descr" "Tests that waiting for individual jobs works" 85 1.1 jruoho } 86 1.7 christos jobs_body() { 87 1.1 jruoho # atf-sh confuses wait for some reason; work it around by creating 88 1.1 jruoho # a helper script that executes /bin/sh directly. 89 1.7 christos 90 1.7 christos if ! ${TEST_SH} -c 'sleep 1 & wait %1' 2>/dev/null 91 1.7 christos then 92 1.7 christos atf_skip "No job control support in this shell" 93 1.7 christos fi 94 1.7 christos 95 1.2 christos cat >individualhelper.sh <<\EOF 96 1.1 jruoho sleep 3 & 97 1.1 jruoho sleep 1 & 98 1.1 jruoho 99 1.1 jruoho wait %1 100 1.2 christos if [ $? -ne 0 ]; then 101 1.7 christos echo "Waiting for first job failed" 102 1.1 jruoho exit 1 103 1.1 jruoho fi 104 1.1 jruoho 105 1.1 jruoho wait %2 106 1.2 christos if [ $? -ne 0 ]; then 107 1.7 christos echo "Waiting for second job failed" 108 1.1 jruoho exit 1 109 1.1 jruoho fi 110 1.1 jruoho 111 1.1 jruoho exit 0 112 1.1 jruoho EOF 113 1.7 christos output=$(${TEST_SH} individualhelper.sh 2>&1) 114 1.7 christos [ $? -eq 0 ] || atf_fail "${output}" 115 1.7 christos 116 1.7 christos cat >individualhelper.sh <<\EOF 117 1.7 christos { sleep 3; exit 3; } & 118 1.7 christos { sleep 1; exit 7; } & 119 1.7 christos 120 1.7 christos wait %1 121 1.7 christos S=$? 122 1.7 christos if [ $S -ne 3 ]; then 123 1.7 christos echo "Waiting for first job failed - status: $S != 3 (expected)" 124 1.7 christos exit 1 125 1.7 christos fi 126 1.7 christos 127 1.7 christos wait %2 128 1.7 christos S=$? 129 1.7 christos if [ $S -ne 7 ]; then 130 1.7 christos echo "Waiting for second job failed - status: $S != 7 (expected)" 131 1.7 christos exit 1 132 1.7 christos fi 133 1.7 christos 134 1.7 christos exit 0 135 1.7 christos EOF 136 1.7 christos 137 1.7 christos output=$(${TEST_SH} individualhelper.sh 2>&1) 138 1.1 jruoho [ $? -eq 0 ] || atf_fail "${output}" 139 1.2 christos } 140 1.2 christos 141 1.2 christos atf_test_case kill 142 1.2 christos kill_head() { 143 1.2 christos atf_set "descr" "Tests that killing the shell while in wait calls trap" 144 1.2 christos } 145 1.2 christos kill_body() { 146 1.7 christos atf_require_prog sleep 147 1.7 christos atf_require_prog kill 148 1.7 christos 149 1.7 christos s=killhelper.sh 150 1.8 christos z=killhelper.$$ 151 1.7 christos pid= 152 1.7 christos 153 1.7 christos # waiting for a specific process that is not a child 154 1.7 christos # should return exit status of 127 according to the spec 155 1.7 christos # This test is here before the next, to avoid that one 156 1.7 christos # entering an infinite loop should the shell have a bug here. 157 1.7 christos 158 1.7 christos atf_check -s exit:127 -o empty -e ignore ${TEST_SH} -c 'wait 1' 159 1.7 christos 160 1.7 christos cat > "${s}" <<'EOF' 161 1.3 ozaki 162 1.2 christos trap "echo SIGHUP" 1 163 1.4 christos (sleep 5; exit 3) & 164 1.2 christos sl=$! 165 1.2 christos wait 166 1.4 christos S=$? 167 1.4 christos echo $S 168 1.7 christos LS=9999 169 1.7 christos while [ $S -ne 0 ] && [ $S != 127 ]; do 170 1.7 christos wait $sl; S=$?; echo $S 171 1.7 christos test $S = $LS && { echo "wait repeats..."; exit 2; } 172 1.7 christos LS=$S 173 1.7 christos done 174 1.2 christos EOF 175 1.3 ozaki 176 1.7 christos ${TEST_SH} $s > $z & 177 1.3 ozaki pid=$! 178 1.3 ozaki sleep 1 179 1.3 ozaki 180 1.7 christos kill -HUP "${pid}" 181 1.4 christos wait 182 1.3 ozaki 183 1.2 christos output="$(cat $z | tr '\n' ' ')" 184 1.7 christos 185 1.4 christos if [ "$output" != "SIGHUP 129 3 127 " ]; then 186 1.6 christos atf_fail "${output} != 'SIGHUP 129 3 127 '" 187 1.2 christos fi 188 1.1 jruoho } 189 1.1 jruoho 190 1.1 jruoho atf_init_test_cases() { 191 1.7 christos atf_add_test_case basic_wait 192 1.1 jruoho atf_add_test_case individual 193 1.7 christos atf_add_test_case jobs 194 1.2 christos atf_add_test_case kill 195 1.1 jruoho } 196