install trap to call cleanup_and_exit always on EXIT
authorLudwig Nussel <ludwig.nussel@suse.de>
Thu, 26 Jun 2008 13:59:47 +0000 (13:59 +0000)
committerLudwig Nussel <ludwig.nussel@suse.de>
Thu, 26 Jun 2008 13:59:47 +0000 (13:59 +0000)
without the trap it could happen that an image stays mounted when the build script exits

build
init_buildsystem

diff --git a/build b/build
index 9712642..d29d905 100755 (executable)
--- a/build
+++ b/build
@@ -160,6 +160,7 @@ usage () {
 #  cleanup_and_exit
 #
 cleanup_and_exit () {
+    trap EXIT
     test -z "$1" && set 0
     if test -n "$RUNNING_IN_XEN" ; then
        cd /
@@ -181,6 +182,11 @@ cleanup_and_exit () {
     exit $1
 }
 
+fail_exit()
+{
+  cleanup_and_exit 1
+}
+
 shellquote()
 {
     for arg; do
@@ -460,6 +466,10 @@ mkdir_build_root()
     fi
 }
 
+#### main ####
+
+trap fail_exit EXIT
+
 shopt -s nullglob
 
 if detect_xen_2nd_stage ; then
@@ -653,7 +663,7 @@ if test -n "$KILL" ; then
     if test -z "$VM_IMAGE" ; then
        if ! $BUILD_DIR/killchroot -s 9 $BUILD_ROOT ; then
            echo "could not kill build in $BUILD_ROOT"
-           exit 1
+           cleanup_and_exit 1
        fi
     else
        XENID="${VM_IMAGE%/root}"
@@ -661,11 +671,11 @@ if test -n "$KILL" ; then
        if xm list "build:$XENID" >/dev/null 2>&1 ; then
            if ! xm destroy "build:$XENID" ; then
                echo "could not kill xen build $XENID"
-               exit 1
+               cleanup_and_exit 1
            fi
        fi
     fi
-    exit 0
+    cleanup_and_exit 0
 fi
 
 if [ -z "$RPMLIST" ]; then
@@ -686,7 +696,7 @@ find_spec_files
 
 if test -n "$LIST_STATE" ; then
     BUILD_ROOT=`mktemp -d /var/tmp/build-list-state-XXXXXX`
-    test -d "$BUILD_ROOT" || exit 1
+    test -d "$BUILD_ROOT" || cleanup_and_exit 1
     SPECFILE=$SPECFILES # only one specified anyways
     if test "$SPECFILE" != "${SPECFILE%.src.rpm}" ; then
        rm -rf $BUILD_ROOT/usr/src/packages
@@ -932,12 +942,12 @@ for SPECFILE in "${SPECFILES[@]}" ; do
            BUILDSTATUS=`dd if="$XENSWAP" bs=12 count=1 2>/dev/null`
            case $BUILDSTATUS in
              BUILDSTATUS[0-9])
-               exit ${BUILDSTATUS#BUILDSTATUS}
+               cleanup_and_exit ${BUILDSTATUS#BUILDSTATUS}
                ;;
            esac
-           exit 1
+           cleanup_and_exit 1
        fi
-       exit 0
+       cleanup_and_exit 0
     fi
 
     if test "$DO_INIT" = true ; then
index 9c36137..1280bc3 100755 (executable)
@@ -81,6 +81,7 @@ PKGS=("$@")
 #
 
 function cleanup_and_exit {
+    trap EXIT
     [ "$BUILD_ROOT" != / ] || chown $browner $BUILD_ROOT
     # umount so init_buildsystem can be used standalone
 # XXX: use stat -f /dev/pts/ -c %T  to check whether it's mounted and not suppress errors then?
@@ -258,6 +259,13 @@ function validate_cache_file {
     fi
 }
 
+fail_exit()
+{
+  cleanup_and_exit 1
+}
+
+trap fail_exit EXIT
+
 if [ "$BUILD_ROOT" = / ]; then
     read dummy dummy browner dummy < <(ls -ld /)
 fi
@@ -342,7 +350,7 @@ else
           echo "${SRC##*/}"
        done < $BUILD_ROOT/.init_b_cache/rpmlist
        rm -rf $BUILD_ROOT/.init_b_cache
-       exit 0
+       cleanup_and_exit 0
     fi
 
     PACKAGES_TO_INSTALL=
@@ -459,7 +467,7 @@ if test -n "$PREPARE_XEN" ; then
     echo "PACKAGES_TO_RUNSCRIPTS='${PACKAGES_TO_RUNSCRIPTS//"'"/$Q}'" >> $BUILD_ROOT/.build/init_buildsystem.data
     echo "PSUF='$PSUF'" >> $BUILD_ROOT/.build/init_buildsystem.data
     rm -f $BUILD_IS_RUNNING
-    exit 0
+    cleanup_and_exit 0
 fi
 
 mkdir -p $BUILD_ROOT/proc