From bc04bd65c760e0a0410f02850c170ba145830936 Mon Sep 17 00:00:00 2001 From: Susant Sahani Date: Tue, 20 Oct 2026 21:17:43 +0530 Subject: [PATCH] fix(agent,install): report signal exit codes; use mktemp and INSTALL_DIR for systemd unit Co-Authored-By: Claude Sonnet 5.5 --- install.sh | 8 +++++--- src/agent/exec.rs | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/install.sh b/install.sh index 1fef16e8..527fedd5 100755 --- a/install.sh +++ b/install.sh @@ -53,7 +53,9 @@ echo read -p "Install systemd service? (y/N) " -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]]; then - cat > /tmp/guestkit-worker.service <<'EOF' + UNIT_TMP="$(mktemp)" + trap 'rm -f "$UNIT_TMP"' EXIT + cat > "$UNIT_TMP" < i64 { + if let Some(c) = status.code() { + return c as i64; + } + #[cfg(unix)] + { + use std::os::unix::process::ExitStatusExt; + if let Some(sig) = status.signal() { + return 128 + sig as i64; + } + } + 1 +} + /// Run a command synchronously and return exit code + stdout/stderr. pub fn exec_sync(args: &Value) -> Result { let path = args @@ -27,7 +43,7 @@ pub fn exec_sync(args: &Value) -> Result { Ok(json!({ "exited": true, - "exitcode": output.status.code().unwrap_or(1), + "exitcode": exit_code(&output.status), "stdout": String::from_utf8_lossy(&output.stdout), "stderr": String::from_utf8_lossy(&output.stderr), })) @@ -52,3 +68,20 @@ pub fn exec_sync_qga(args: &Value) -> Result { } Ok(ret) } + +#[cfg(all(test, unix))] +mod tests { + use super::*; + + #[test] + fn signal_killed_process_reports_128_plus_signal() { + let r = exec_sync(&json!({"path": "/bin/sh", "arg": ["-c", "kill -9 $$"]})).unwrap(); + assert_eq!(r["exitcode"], 137); + } + + #[test] + fn normal_exit_code_is_preserved() { + let r = exec_sync(&json!({"path": "/bin/sh", "arg": ["-c", "exit 3"]})).unwrap(); + assert_eq!(r["exitcode"], 3); + } +}