From 64893302017a8dacd25e4946a6ef5648fe59235b Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Mon, 5 Jun 2023 16:19:22 +0200 Subject: [run-virt] Make sure cleanexit is not run from subshell --- .../run-virt-includes/vmchooser_runvirt_functions.inc | 12 +++++++++++- .../run-virt/data/opt/openslx/vmchooser/vmchooser-run_virt | 2 ++ 2 files changed, 13 insertions(+), 1 deletion(-) (limited to 'core/modules/run-virt/data') diff --git a/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/vmchooser_runvirt_functions.inc b/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/vmchooser_runvirt_functions.inc index fe624ab5..3bfa8bb5 100644 --- a/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/vmchooser_runvirt_functions.inc +++ b/core/modules/run-virt/data/opt/openslx/vmchooser/run-virt-includes/vmchooser_runvirt_functions.inc @@ -90,6 +90,11 @@ $BODY" declare -ag CLEANUP_TASKS=() add_cleanup() { [ $# -lt 1 ] && writelog "'${FUNCNAME[0]}' needs at least one argument! $# given." && return + if [ "$BASHPID" != "$MAINPID" ]; then + writelog "WARNING: '${BASH_SOURCE[1]}:${FUNCNAME[1]}' tried to add cleanup hook '$1' in a subshell, this does not work... (main: $MAINPID, current: $BASHPID)" + else + writelog "Normal add_cleanup from '${BASH_SOURCE[1]}:${FUNCNAME[1]}' for '$1'" + fi # check if the given function name is already used while [ $# -ne 0 ]; do if array_contains CLEANUP_TASKS "$1"; then @@ -121,6 +126,11 @@ add_cleanup() { cleanexit() { trap "" SIGHUP SIGINT SIGTERM EXIT writelog "Cleanexit '$1' triggered by '${BASH_SOURCE[1]}:${FUNCNAME[1]}'" + if [ "$BASHPID" != "$MAINPID" ]; then + writelog "Running in a subshell, killing parent... (main: $MAINPID, current: $BASHPID)" + kill "$MAINPID" + exit 0 + fi usleep 250000 local TASK if [ "${#CLEANUP_TASKS[@]}" -gt 0 ]; then @@ -151,7 +161,7 @@ cleanexit() { # Now see if we need to do the catch all error stuff # if 0 given, exit 0 - [ "x$1" = "x0" ] && exit 0 + [ "$1" = "0" ] && exit 0 # if no code was given, exit 129 [ $# -eq 0 ] && writelog "Cleanexit called without arguments! Dev error?" diff --git a/core/modules/run-virt/data/opt/openslx/vmchooser/vmchooser-run_virt b/core/modules/run-virt/data/opt/openslx/vmchooser/vmchooser-run_virt index f5b79012..241e0f9e 100755 --- a/core/modules/run-virt/data/opt/openslx/vmchooser/vmchooser-run_virt +++ b/core/modules/run-virt/data/opt/openslx/vmchooser/vmchooser-run_virt @@ -85,6 +85,8 @@ fi readonly LOGFILE main() { + declare -rg MAINPID="$BASHPID" + writelog "MAINPID: $MAINPID" # Functions needed by vmchooser-run_virt (writelog(), cleanexit(), safesource()) declare -rg RUN_VIRT_INCLUDE_DIR="${VMCHOOSER_DIR}/run-virt-includes" if ! source "${RUN_VIRT_INCLUDE_DIR}/vmchooser_runvirt_functions.inc"; then -- cgit v1.2.3-55-g7522