From d18469d608ef58f785a0c6a16c488ef5951d3791 Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Tue, 9 Jun 2020 20:11:22 +0200 Subject: [PATCH] bin/package: exit with nonzero status on build failure Build scripts and the like ought to be able to check the results of the build using the exit status of the build command. bin/package, src/cmd/INIT/package.sh: - Add global error_status variable to keep track of the highest exit status of all commands using capture(). Exit with that. - capture(): * When capturing build output using 'tee', change the regular pipe construct (which doesn't preserve the exit status of pipe elements except the last one) to a couple of background jobs connected with a FIFO (named pipe). This allows getting the build's exit status using the "wait" command. * Update error_status on every invocation of capture(). (cherry picked from commit 22c3a6e1b32f3ab7eeca45adb76863982dc3e334) --- bin/package | 15 ++++++++++++++- src/cmd/INIT/package.sh | 15 ++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/bin/package b/bin/package index 4b1272594..9904d4109 100755 --- a/bin/package +++ b/bin/package @@ -3966,13 +3966,18 @@ capture() # file command ... 0) if executable ! $TEE then TEE=tee fi + rm -f $o.fifo + mkfifo -m 600 $o.fifo || exit + $TEE -a $o < $o.fifo & { case $s in ?*) echo "$s" ;; esac showenv $action "$@" - } < /dev/null 2>&1 | $TEE -a $o + } > $o.fifo 2>&1 & + rm $o.fifo # unlink early + wait "$!" # get build's exit status ;; *) { case $s in @@ -3987,6 +3992,10 @@ capture() # file command ... *) $make "$@" ;; esac + exit_status=$? + if test "$exit_status" -gt "$error_status" + then error_status=$exit_status + fi } package_install() # dest sum @@ -4358,6 +4367,8 @@ isascii() return $__isascii__ } +error_status=0 + case $action in admin) while test ! -f $admin_db @@ -7358,3 +7369,5 @@ TEST) set '' $target $package ;; esac + +exit "$error_status" diff --git a/src/cmd/INIT/package.sh b/src/cmd/INIT/package.sh index 99f56e322..59c9cdd5b 100644 --- a/src/cmd/INIT/package.sh +++ b/src/cmd/INIT/package.sh @@ -3965,13 +3965,18 @@ capture() # file command ... 0) if executable ! $TEE then TEE=tee fi + rm -f $o.fifo + mkfifo -m 600 $o.fifo || exit + $TEE -a $o < $o.fifo & { case $s in ?*) echo "$s" ;; esac showenv $action "$@" - } < /dev/null 2>&1 | $TEE -a $o + } > $o.fifo 2>&1 & + rm $o.fifo # unlink early + wait "$!" # get build's exit status ;; *) { case $s in @@ -3986,6 +3991,10 @@ capture() # file command ... *) $make "$@" ;; esac + exit_status=$? + if test "$exit_status" -gt "$error_status" + then error_status=$exit_status + fi } package_install() # dest sum @@ -4357,6 +4366,8 @@ isascii() return $__isascii__ } +error_status=0 + case $action in admin) while test ! -f $admin_db @@ -7357,3 +7368,5 @@ TEST) set '' $target $package ;; esac + +exit "$error_status"