From 8a9f105e75d4c080ccaf4202c99d9cb6ea55812e Mon Sep 17 00:00:00 2001 From: Perdixky <3293789706@qq.com> Date: Wed, 3 Dec 2025 10:17:42 +0800 Subject: [PATCH 1/3] Eliminate repeated sudo password prompts during installation --- sdata/lib/functions.sh | 50 ++++++++++++++++++++++++++++++++++++++++++ setup | 10 +++++++++ 2 files changed, 60 insertions(+) diff --git a/sdata/lib/functions.sh b/sdata/lib/functions.sh index b6dcae99d..ecaf1fcc1 100644 --- a/sdata/lib/functions.sh +++ b/sdata/lib/functions.sh @@ -80,6 +80,56 @@ function prevent_sudo_or_root(){ root) echo -e "${STY_RED}[$0]: This script is NOT to be executed with sudo or as root. Aborting...${STY_RST}";exit 1;; esac } + +# Initialize sudo session and keep it alive in background +# Store PID in a global variable that can be accessed by trap +declare -g SUDO_KEEPALIVE_PID="" + +function sudo_init_keepalive(){ + # Check if sudo is available + if ! command -v sudo >/dev/null 2>&1; then + return 0 + fi + + # Only initialize for install-related commands that need sudo + case "${SUBCMD_NAME}" in + install|install-deps|install-setups) ;; + *) return 0 ;; + esac + + # Skip if already initialized + if [[ -n "$SUDO_KEEPALIVE_PID" ]] && kill -0 "$SUDO_KEEPALIVE_PID" 2>/dev/null; then + return 0 + fi + + # Prompt for sudo password once at the beginning + echo -e "${STY_CYAN}[$0]: Requesting sudo privileges for installation...${STY_RST}" + if ! sudo -v; then + echo -e "${STY_RED}[$0]: Failed to obtain sudo privileges. Aborting...${STY_RST}" + exit 1 + fi + + # Start background process to keep sudo session alive + # This updates the sudo timestamp every 60 seconds + ( + while true; do + sleep 60 + sudo -v 2>/dev/null || exit 0 + done + ) & + SUDO_KEEPALIVE_PID=$! + + echo -e "${STY_GREEN}[$0]: Sudo session initialized and will be kept alive (PID: $SUDO_KEEPALIVE_PID)${STY_RST}" +} + +# Stop the sudo keepalive background process +function sudo_stop_keepalive(){ + if [[ -n "$SUDO_KEEPALIVE_PID" ]] && kill -0 "$SUDO_KEEPALIVE_PID" 2>/dev/null; then + kill "$SUDO_KEEPALIVE_PID" 2>/dev/null + wait "$SUDO_KEEPALIVE_PID" 2>/dev/null + SUDO_KEEPALIVE_PID="" + fi +} function git_auto_unshallow(){ # We need this function for latest_commit_hash to work properly if [[ -f "$(git rev-parse --git-dir)/shallow" ]]; then diff --git a/setup b/setup index 8a6e2d82f..226a1ba85 100755 --- a/setup +++ b/setup @@ -8,6 +8,10 @@ source ./sdata/lib/package-installers.sh source ./sdata/lib/dist-determine.sh prevent_sudo_or_root + +# Trap to ensure sudo keepalive is stopped on exit +trap sudo_stop_keepalive EXIT INT TERM + set -e ##################################################################################### @@ -71,6 +75,8 @@ case ${SUBCMD_NAME} in $function done pause + # Initialize sudo keepalive for the entire install process + sudo_init_keepalive if [[ "${SKIP_ALLGREETING}" != true ]]; then source ${SUBCMD_DIR}/0.greeting.sh fi @@ -89,6 +95,8 @@ case ${SUBCMD_NAME} in $function done pause + # Initialize sudo keepalive for dependency installation + sudo_init_keepalive if [[ "${SKIP_ALLDEPS}" != true ]]; then source ${SUBCMD_DIR}/1.deps-router.sh fi @@ -98,6 +106,8 @@ case ${SUBCMD_NAME} in $function done pause + # Initialize sudo keepalive for setup steps + sudo_init_keepalive if [[ "${SKIP_ALLSETUPS}" != true ]]; then source ${SUBCMD_DIR}/2.setups.sh fi From d3c1ae14b859d345d0446a7a240b72075dbca518 Mon Sep 17 00:00:00 2001 From: Perdixky <3293789706@qq.com> Date: Wed, 3 Dec 2025 20:58:45 +0800 Subject: [PATCH 2/3] Move trap to each needed subcommand --- setup | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/setup b/setup index 226a1ba85..9174eeb93 100755 --- a/setup +++ b/setup @@ -8,10 +8,6 @@ source ./sdata/lib/package-installers.sh source ./sdata/lib/dist-determine.sh prevent_sudo_or_root - -# Trap to ensure sudo keepalive is stopped on exit -trap sudo_stop_keepalive EXIT INT TERM - set -e ##################################################################################### @@ -77,6 +73,8 @@ case ${SUBCMD_NAME} in pause # Initialize sudo keepalive for the entire install process sudo_init_keepalive + # Set trap to cleanup when this subcommand exits + trap sudo_stop_keepalive EXIT INT TERM if [[ "${SKIP_ALLGREETING}" != true ]]; then source ${SUBCMD_DIR}/0.greeting.sh fi @@ -97,6 +95,8 @@ case ${SUBCMD_NAME} in pause # Initialize sudo keepalive for dependency installation sudo_init_keepalive + # Set trap to cleanup when this subcommand exits + trap sudo_stop_keepalive EXIT INT TERM if [[ "${SKIP_ALLDEPS}" != true ]]; then source ${SUBCMD_DIR}/1.deps-router.sh fi @@ -108,6 +108,8 @@ case ${SUBCMD_NAME} in pause # Initialize sudo keepalive for setup steps sudo_init_keepalive + # Set trap to cleanup when this subcommand exits + trap sudo_stop_keepalive EXIT INT TERM if [[ "${SKIP_ALLSETUPS}" != true ]]; then source ${SUBCMD_DIR}/2.setups.sh fi From ed9e510c32fd7f0f656878350ffed7fc0b41d349 Mon Sep 17 00:00:00 2001 From: "Celestial.y" Date: Wed, 3 Dec 2025 21:35:36 +0800 Subject: [PATCH 3/3] Remove determine subcommand install* --- sdata/lib/functions.sh | 6 ------ 1 file changed, 6 deletions(-) diff --git a/sdata/lib/functions.sh b/sdata/lib/functions.sh index ecaf1fcc1..0fe24f0ed 100644 --- a/sdata/lib/functions.sh +++ b/sdata/lib/functions.sh @@ -91,12 +91,6 @@ function sudo_init_keepalive(){ return 0 fi - # Only initialize for install-related commands that need sudo - case "${SUBCMD_NAME}" in - install|install-deps|install-setups) ;; - *) return 0 ;; - esac - # Skip if already initialized if [[ -n "$SUDO_KEEPALIVE_PID" ]] && kill -0 "$SUDO_KEEPALIVE_PID" 2>/dev/null; then return 0