From b42bd7e40842a57ce1a4a631672a86e7d65c085f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B8ren=20Theilgaard?= Date: Tue, 8 Feb 2022 13:11:08 +0100 Subject: [PATCH] Trapping the script Using these signals: ``` SIGHUP SIGINT SIGQUIT SIGABRT SIGTERM SIGTSTP ``` Fixed #272 --- CHANGELOG.md | 1 + fragments/arguments.sh | 9 +++++++-- fragments/functions.sh | 13 ++++++++++++- fragments/main.sh | 1 + 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9fd54d4..1b731ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## v10 +- Now we trap the script, so if it is cancelled, we will run cleanupAndExit anyway, and have mounted diskimages unmounted and temporary files and folders removed. - ## v9 diff --git a/fragments/arguments.sh b/fragments/arguments.sh index 45a933c..9e43b25 100644 --- a/fragments/arguments.sh +++ b/fragments/arguments.sh @@ -1,4 +1,9 @@ -# MARK: check minimal macOS requirement +# MARK: 3 Arguments + +# Start trapping the script +trap_with_arg func_trap SIGHUP SIGINT SIGQUIT SIGABRT SIGTERM SIGTSTP + +# check minimal macOS requirement autoload is-at-least installedOSversion=$(sw_vers -productVersion) @@ -125,4 +130,4 @@ valuesfromarguments) fi ;; -# label descriptions start here +# Mark: label descriptions start here diff --git a/fragments/functions.sh b/fragments/functions.sh index a851077..0c603c8 100644 --- a/fragments/functions.sh +++ b/fragments/functions.sh @@ -1,4 +1,4 @@ -# MARK: Functions +# MARK: 2 Functions cleanupAndExit() { # $1 = exit code, $2 message, $3 level if [ -n "$dmgmount" ]; then @@ -29,6 +29,17 @@ cleanupAndExit() { # $1 = exit code, $2 message, $3 level exit "$1" } +# Two functions to trap the script +trap_with_arg() { + func="$1" ; shift + for sig ; do + trap "$func $sig" "$sig" + done +} +func_trap() { + cleanupAndExit 255 "Installomator has been terminated with: $1" ERROR +} + runAsUser() { if [[ $currentUser != "loginwindow" ]]; then uid=$(id -u "$currentUser") diff --git a/fragments/main.sh b/fragments/main.sh index 1de095f..02533fc 100644 --- a/fragments/main.sh +++ b/fragments/main.sh @@ -1,3 +1,4 @@ +# Mark: 4 Main *) # unknown label #printlog "unknown label $label"