timeout: disable pre-existing SIGCHLD handlers

Needed to make a GNU test pass
This commit is contained in:
Michael Debertol 2021-06-10 20:03:33 +02:00
parent 0f9bc8e974
commit b4efd5a749
3 changed files with 17 additions and 1 deletions

3
Cargo.lock generated
View file

@ -1,5 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "Inflector"
version = "0.11.4"
@ -2606,6 +2608,7 @@ version = "0.0.6"
dependencies = [
"clap",
"libc",
"nix 0.20.0",
"uucore",
"uucore_procs",
]

View file

@ -17,6 +17,7 @@ path = "src/timeout.rs"
[dependencies]
clap = "2.33"
libc = "0.2.42"
nix = "0.20.0"
uucore = { version=">=0.0.8", package="uucore", path="../../uucore", features=["parse_time", "process", "signals"] }
uucore_procs = { version=">=0.0.5", package="uucore_procs", path="../../uucore_procs" }

View file

@ -5,7 +5,7 @@
// * For the full copyright and license information, please view the LICENSE
// * file that was distributed with this source code.
// spell-checker:ignore (ToDO) tstr sigstr cmdname setpgid
// spell-checker:ignore (ToDO) tstr sigstr cmdname setpgid sigchld
#[macro_use]
extern crate uucore;
@ -161,6 +161,17 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
)
}
/// Remove pre-existing SIGCHLD handlers that would make waiting for the child's exit code fail.
fn unblock_sigchld() {
unsafe {
nix::sys::signal::signal(
nix::sys::signal::Signal::SIGCHLD,
nix::sys::signal::SigHandler::SigDfl,
)
.unwrap();
}
}
/// TODO: Improve exit codes, and make them consistent with the GNU Coreutils exit codes.
fn timeout(
@ -194,6 +205,7 @@ fn timeout(
}
}
};
unblock_sigchld();
match process.wait_or_timeout(duration) {
Ok(Some(status)) => status.code().unwrap_or_else(|| status.signal().unwrap()),
Ok(None) => {