From 767692160d646f66a184bbfa934c471369c10635 Mon Sep 17 00:00:00 2001 From: j Date: Fri, 2 Jan 2026 22:02:46 +1300 Subject: [PATCH] fix cd bug in ds_run, and fix ds ssh so it doesnt hang process --- source/agent-remote/ds_run.sh | 3 +-- source/src/commands/ssh.cpp | 3 ++- source/src/utils/execute.cpp | 29 +++++++++++++++++++++++++++++ source/src/utils/execute.hpp | 3 ++- 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/source/agent-remote/ds_run.sh b/source/agent-remote/ds_run.sh index 367c986..4c33178 100755 --- a/source/agent-remote/ds_run.sh +++ b/source/agent-remote/ds_run.sh @@ -86,5 +86,4 @@ fi [[ -f "${COMMAND_TO_RUN}" ]] || _die "Command not found: ${DSCOMMAND} (looked for ${TEMPLATE_PATH}/${DSCOMMAND}[.sh])" # -- Execute the command with any remaining arguments -- -cd "${TEMPLATE_PATH}" -exec bash cd "${COMMAND_PATH}" && "${COMMAND_TO_RUN}" "$@" +cd "${COMMAND_PATH}" && exec "${COMMAND_TO_RUN}" "$@" diff --git a/source/src/commands/ssh.cpp b/source/src/commands/ssh.cpp index 3f27645..c4df717 100644 --- a/source/src/commands/ssh.cpp +++ b/source/src/commands/ssh.cpp @@ -54,7 +54,8 @@ namespace dropshell info << "SSHing into " << server << ":" << remote_path << " as user " << user << std::endl; - execute_ssh_command(server_env.get_SSH_INFO(user), sCommand(remote_path, bash_cmd, env_vars), cMode::Interactive); + // Use ReplaceProcess so dropshell exits and ssh takes over completely + execute_ssh_command(server_env.get_SSH_INFO(user), sCommand(remote_path, bash_cmd, env_vars), cMode::Interactive | cMode::ReplaceProcess); return true; } diff --git a/source/src/utils/execute.cpp b/source/src/utils/execute.cpp index 49a55a4..fc4ca01 100644 --- a/source/src/utils/execute.cpp +++ b/source/src/utils/execute.cpp @@ -56,6 +56,23 @@ namespace dropshell } } + // ---------------------------------------------------------------------------------------------------------- + // execute_local_command_replace - replaces current process with the command (never returns on success) + // ---------------------------------------------------------------------------------------------------------- + bool execute_local_command_replace(const std::string &full_command) + { + if (full_command.empty()) + return false; + + // Use exec to replace the current process with a shell running the command + // This means dropshell exits immediately and ssh (or whatever) takes over + execlp("/bin/bash", "bash", "-c", full_command.c_str(), nullptr); + + // If we get here, exec failed + perror("exec failed"); + return false; + } + // ---------------------------------------------------------------------------------------------------------- // execute_local_command // ---------------------------------------------------------------------------------------------------------- @@ -117,6 +134,18 @@ namespace dropshell { sCommand command(directory_to_run_in, command_to_run, env_vars); + if (hasFlag(mode, cMode::ReplaceProcess)) + { + ASSERT(output == nullptr, "ReplaceProcess mode and an output string cannot be used together"); + // For ReplaceProcess, run the command directly without bb64 wrapping + // (the caller is responsible for constructing the full command) + std::string full_cmd = command_to_run; + if (!directory_to_run_in.empty()) + full_cmd = "cd " + quote(directory_to_run_in) + " && " + full_cmd; + return execute_local_command_replace(full_cmd); + // Note: execute_local_command_replace never returns on success + } + if (hasFlag(mode, cMode::Interactive)) { ASSERT(output == nullptr, "Interactive mode and an output string cannot be used together"); diff --git a/source/src/utils/execute.hpp b/source/src/utils/execute.hpp index 18297ff..a4c6302 100644 --- a/source/src/utils/execute.hpp +++ b/source/src/utils/execute.hpp @@ -14,7 +14,8 @@ enum class cMode { Defaults = 0, Interactive = 1, Silent = 2, - NoBB64 = 4 + NoBB64 = 4, + ReplaceProcess = 8 // Use exec to replace current process (dropshell exits immediately) }; inline cMode operator&(cMode lhs, cMode rhs) {return static_cast(static_cast(lhs) & static_cast(rhs));}