diff --git a/src/benchmark/mod.rs b/src/benchmark/mod.rs index e2b6f6f..3d4bd14 100644 --- a/src/benchmark/mod.rs +++ b/src/benchmark/mod.rs @@ -18,6 +18,7 @@ use crate::output::warnings::Warnings; use crate::shell::execute_and_time; use crate::timer::wallclocktimer::WallClockTimer; use crate::timer::{TimerStart, TimerStop}; +use crate::util::exit_code::extract_exit_code; use crate::util::min_max::{max, min}; use crate::util::units::Second; @@ -223,27 +224,6 @@ fn run_cleanup_command( run_intermediate_command(shell, command, output_policy, error_output) } -#[cfg(unix)] -fn extract_exit_code(status: ExitStatus) -> Option { - use std::os::unix::process::ExitStatusExt; - - /* From the ExitStatus::code documentation: - "On Unix, this will return None if the process was terminated by a signal." - In that case, ExitStatusExt::signal should never return None. - */ - status.code().or_else(|| - /* To differentiate between "normal" exit codes and signals, we are using - something similar to bash exit codes (https://tldp.org/LDP/abs/html/exitcodes.html) - by adding 128 to a signal integer value. - */ - status.signal().map(|s| 128 + s)) -} - -#[cfg(not(unix))] -fn extract_exit_code(status: ExitStatus) -> Option { - status.code() -} - /// Run the benchmark for a single shell command pub fn run_benchmark( num: usize, diff --git a/src/util/exit_code.rs b/src/util/exit_code.rs new file mode 100644 index 0000000..9ad30b5 --- /dev/null +++ b/src/util/exit_code.rs @@ -0,0 +1,22 @@ +use std::process::ExitStatus; + +#[cfg(unix)] +pub fn extract_exit_code(status: ExitStatus) -> Option { + use std::os::unix::process::ExitStatusExt; + + // From the ExitStatus::code documentation: + // + // "On Unix, this will return None if the process was terminated by a signal." + // + // In that case, ExitStatusExt::signal should never return None. + // + // To differentiate between "normal" exit codes and signals, we are using a technique + // similar to bash (https://tldp.org/LDP/abs/html/exitcodes.html) and add 128 to the + // signal value. + status.code().or_else(|| status.signal().map(|s| s + 128)) +} + +#[cfg(not(unix))] +pub fn extract_exit_code(status: ExitStatus) -> Option { + status.code() +} diff --git a/src/util/mod.rs b/src/util/mod.rs index bdf1dea..8f5e063 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,3 +1,4 @@ +pub mod exit_code; pub mod min_max; pub mod number; pub mod units;