1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-01 05:27: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,6 +216,7 @@ fn timeout(
); );
} }
return_if_err!(ERR_EXIT_STATUS, process.send_signal(signal)); return_if_err!(ERR_EXIT_STATUS, process.send_signal(signal));
if let Some(kill_after) = kill_after {
match process.wait_or_timeout(kill_after) { match process.wait_or_timeout(kill_after) {
Ok(Some(status)) => { Ok(Some(status)) => {
if preserve_status { if preserve_status {
@ -231,14 +231,18 @@ fn timeout(
} }
return_if_err!( return_if_err!(
ERR_EXIT_STATUS, ERR_EXIT_STATUS,
process process.send_signal(
.send_signal(uucore::signals::signal_by_name_or_value("KILL").unwrap()) uucore::signals::signal_by_name_or_value("KILL").unwrap()
)
); );
return_if_err!(ERR_EXIT_STATUS, process.wait()); return_if_err!(ERR_EXIT_STATUS, process.wait());
137 137
} }
Err(_) => 124, Err(_) => 124,
} }
} else {
124
}
} }
Err(_) => { Err(_) => {
return_if_err!(ERR_EXIT_STATUS, process.send_signal(signal)); return_if_err!(ERR_EXIT_STATUS, process.send_signal(signal));