From 1aa6fd14680772e6a140d3c236a549b5e4014348 Mon Sep 17 00:00:00 2001 From: Jeffrey Finkelstein Date: Fri, 11 Mar 2022 22:40:47 -0500 Subject: [PATCH] timeout: fix bug in --preserve-status mode Fix a bug where `timeout --preserve-status` was not correctly preserving the status code of the child process if it timed out. When that happens, the status code of the child process is considered to be the signal number (in this case, `SIGTERM`). The exit status of `timeout` is then 128 plus the numeric code associated with `SIGTERM`. --- src/uu/timeout/src/timeout.rs | 8 +++++++- tests/by-util/test_timeout.rs | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/uu/timeout/src/timeout.rs b/src/uu/timeout/src/timeout.rs index f02f0a0a9..3da0bcd2a 100644 --- a/src/uu/timeout/src/timeout.rs +++ b/src/uu/timeout/src/timeout.rs @@ -282,7 +282,13 @@ fn timeout( report_if_verbose(signal, &cmd[0], verbose); process.send_signal(signal)?; match kill_after { - None => Err(ExitStatus::CommandTimedOut.into()), + None => { + if preserve_status { + Err(ExitStatus::SignalSent(signal).into()) + } else { + Err(ExitStatus::CommandTimedOut.into()) + } + } Some(kill_after) => { match wait_or_kill_process( process, diff --git a/tests/by-util/test_timeout.rs b/tests/by-util/test_timeout.rs index 5db91a44d..b9c8a59ed 100644 --- a/tests/by-util/test_timeout.rs +++ b/tests/by-util/test_timeout.rs @@ -53,3 +53,14 @@ fn test_command_empty_args() { .fails() .stderr_contains("timeout: empty string"); } + +#[test] +fn test_preserve_status() { + new_ucmd!() + .args(&["--preserve-status", ".1", "sleep", "10"]) + .fails() + // 128 + SIGTERM = 128 + 15 + .code_is(128 + 15) + .no_stderr() + .no_stdout(); +}