Skip to content

Commit

Permalink
Make time exit status behavior saner
Browse files Browse the repository at this point in the history
Match `timeout(1)` in error code where possible (the main benefit is
that unusual errors result in high error codes).

Signed-off-by: Tej Chajed <[email protected]>
  • Loading branch information
tchajed committed Aug 1, 2023
1 parent f62952d commit 1069910
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions benchmarking/src/time_bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ struct RawRunMeasurement {
self_usage: Usage,
timed_out: bool,
status: Option<i32>,
signal: Option<Signal>,
}

fn time_val_to_duration(time: TimeVal) -> Duration {
Expand Down Expand Up @@ -174,10 +173,6 @@ impl Time {
WaitStatus::Exited(_, status) => Some(status),
_ => None,
};
let signal = match wstatus {
WaitStatus::Signaled(_, signal, _) => Some(signal),
_ => None,
};
let real_time = start.elapsed();
let timed_out = *timeout.lock().unwrap();
let usage = getrusage(UsageWho::RUSAGE_CHILDREN)?;
Expand All @@ -188,7 +183,6 @@ impl Time {
self_usage,
timed_out,
status,
signal,
};
Ok(measurement)
}
Expand All @@ -214,9 +208,6 @@ impl Time {
// to match behavior of `timeout(1)`
return Ok(ExitCode::from(124u8));
}
if measurements.signal.is_some() {
return Ok(ExitCode::FAILURE);
}
if let Some(code) = measurements.status {
return Ok(ExitCode::from(code as u8));
}
Expand All @@ -230,11 +221,14 @@ impl Time {
dup2(null, io::stdout().as_raw_fd()).expect("could not replace stdout with null");
let err = exec::Command::new(&self.prog).args(&self.args).exec();
eprintln!("exec failed: {err}");
Ok(ExitCode::FAILURE)
// mimic `timeout(1)` (although it distinguishes the command
// being found vs not found)
Ok(ExitCode::from(126))
}
Err(err) => {
eprintln!("fork failed: {err}");
Ok(ExitCode::FAILURE)
// mimic `timeout(1)`
Ok(ExitCode::from(125))
}
}
}
Expand Down

0 comments on commit 1069910

Please sign in to comment.