Move exit code function to separate file

This commit is contained in:
David Peter 2022-02-20 13:36:30 +01:00 committed by David Peter
parent 811e6b5fcd
commit b658e20110
3 changed files with 24 additions and 21 deletions

View File

@ -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<i32> {
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<i32> {
status.code()
}
/// Run the benchmark for a single shell command
pub fn run_benchmark(
num: usize,

22
src/util/exit_code.rs Normal file
View File

@ -0,0 +1,22 @@
use std::process::ExitStatus;
#[cfg(unix)]
pub fn extract_exit_code(status: ExitStatus) -> Option<i32> {
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<i32> {
status.code()
}

View File

@ -1,3 +1,4 @@
pub mod exit_code;
pub mod min_max;
pub mod number;
pub mod units;