1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-31 04:57:45 +00:00

timeout: don't kill the process if -k is not set

`timeout` used to set the timeout to 0 when -k was not set. This
collided with the behavior of 0 timeouts, which disable the timeout.
When -k is not set the process should not be killed.
This commit is contained in:
Michael Debertol 2021-06-11 20:44:25 +02:00
parent b4efd5a749
commit f909751155

View file

@ -42,7 +42,7 @@ pub mod options {
struct Config { struct Config {
foreground: bool, foreground: bool,
kill_after: Duration, kill_after: Option<Duration>,
signal: usize, signal: usize,
duration: Duration, duration: Duration,
preserve_status: bool, preserve_status: bool,
@ -66,10 +66,9 @@ impl Config {
_ => uucore::signals::signal_by_name_or_value("TERM").unwrap(), _ => uucore::signals::signal_by_name_or_value("TERM").unwrap(),
}; };
let kill_after: Duration = match options.value_of(options::KILL_AFTER) { let kill_after = options
Some(time) => uucore::parse_time::from_str(time).unwrap(), .value_of(options::KILL_AFTER)
None => Duration::new(0, 0), .map(|time| uucore::parse_time::from_str(time).unwrap());
};
let duration: Duration = let duration: Duration =
uucore::parse_time::from_str(options.value_of(options::DURATION).unwrap()).unwrap(); uucore::parse_time::from_str(options.value_of(options::DURATION).unwrap()).unwrap();
@ -178,7 +177,7 @@ fn timeout(
cmd: &[String], cmd: &[String],
duration: Duration, duration: Duration,
signal: usize, signal: usize,
kill_after: Duration, kill_after: Option<Duration>,
foreground: bool, foreground: bool,
preserve_status: bool, preserve_status: bool,
verbose: bool, verbose: bool,
@ -217,27 +216,32 @@ fn timeout(
); );
} }
return_if_err!(ERR_EXIT_STATUS, process.send_signal(signal)); return_if_err!(ERR_EXIT_STATUS, process.send_signal(signal));
match process.wait_or_timeout(kill_after) { if let Some(kill_after) = kill_after {
Ok(Some(status)) => { match process.wait_or_timeout(kill_after) {
if preserve_status { Ok(Some(status)) => {
status.code().unwrap_or_else(|| status.signal().unwrap()) if preserve_status {
} else { status.code().unwrap_or_else(|| status.signal().unwrap())
124 } else {
124
}
} }
} Ok(None) => {
Ok(None) => { if verbose {
if verbose { show_error!("sending signal KILL to command '{}'", cmd[0]);
show_error!("sending signal KILL to command '{}'", cmd[0]); }
return_if_err!(
ERR_EXIT_STATUS,
process.send_signal(
uucore::signals::signal_by_name_or_value("KILL").unwrap()
)
);
return_if_err!(ERR_EXIT_STATUS, process.wait());
137
} }
return_if_err!( Err(_) => 124,
ERR_EXIT_STATUS,
process
.send_signal(uucore::signals::signal_by_name_or_value("KILL").unwrap())
);
return_if_err!(ERR_EXIT_STATUS, process.wait());
137
} }
Err(_) => 124, } else {
124
} }
} }
Err(_) => { Err(_) => {