From 112dbfd4c38dbb85a0164b73e83bd076a727e8f0 Mon Sep 17 00:00:00 2001 From: agnostic-apollo Date: Mon, 11 Nov 2024 19:10:06 +0500 Subject: [PATCH] fix(termux-bootstrap-second-stage.sh): do not exit with the wrong "second stage has already been run before" error if `ln` process used to create lock file got killed instead of failing to create lock file if it already existed Related https://github.com/termux/termux-app/issues/4219 --- .../termux-bootstrap-second-stage.sh | 48 +++++++++++++++++-- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/scripts/bootstrap/termux-bootstrap-second-stage.sh b/scripts/bootstrap/termux-bootstrap-second-stage.sh index 00d0a8eb65097f..6de13a96a658d7 100755 --- a/scripts/bootstrap/termux-bootstrap-second-stage.sh +++ b/scripts/bootstrap/termux-bootstrap-second-stage.sh @@ -92,16 +92,29 @@ run_bootstrap_second_stage() { local return_value - if ! ln -s "termux-bootstrap-second-stage.sh" \ - "@TERMUX_BOOTSTRAPS__BOOTSTRAP_CONFIG_DIR@/termux-bootstrap-second-stage.sh.lock" 2>/dev/null; then - log "The termux bootstrap second stage has already been run before and cannot be run again." - log "If you still want to force run it again (not recommended), \ + local output + + output="$(ln -s "termux-bootstrap-second-stage.sh" \ + "@TERMUX_BOOTSTRAPS__BOOTSTRAP_CONFIG_DIR@/termux-bootstrap-second-stage.sh.lock" 2>&1)" + return_value=$? + if [ $return_value -ne 0 ]; then + if [ $return_value -eq 1 ] && [[ "$output" == *"File exists"* ]]; then + log "The termux bootstrap second stage has already been run before and cannot be run again." + log "If you still want to force run it again (not recommended), \ like in case of previous failure and it must be re-run again for testing, \ then delete the '@TERMUX_BOOTSTRAPS__BOOTSTRAP_CONFIG_DIR@/termux-bootstrap-second-stage.sh.lock' \ file manually and run 'termux-bootstrap-second-stage.sh' again." - return 0 + return 0 + else + log_error "$output" + log_error "Failed to create lock file for termux bootstrap second stage at \ +'@TERMUX_BOOTSTRAPS__BOOTSTRAP_CONFIG_DIR@/termux-bootstrap-second-stage.sh.lock'" + warn_if_process_killed "$return_value" "ln" + return $return_value + fi fi + log "Running termux bootstrap second stage" run_bootstrap_second_stage_inner return_value=$? @@ -112,6 +125,7 @@ file manually and run 'termux-bootstrap-second-stage.sh' again." log "The termux bootstrap second stage completed successfully" + return 0 } @@ -360,6 +374,30 @@ run_package_postinst_maintainer_scripts() { } + + + + +warn_if_process_killed() { + + local return_value="${1:-}" + local command="${2:-}" + + if [[ "$return_value" == "137" ]]; then + log_error "The '$command' command was apparently killed with SIGKILL (signal 9). \ +This may have been due to the security policies of the Android OS installed on your device. +Check https://github.com/termux/termux-app/issues/4219 for more info." + return 0 + fi + + return 1 + +} + + + + + # If running in bash, run script logic, otherwise exit with usage error if [ -n "${BASH_VERSION:-}" ]; then # If script is sourced, return with error, otherwise call main function