diff --git a/crates/shim/Cargo.toml b/crates/shim/Cargo.toml index ae369f7..1a37b61 100644 --- a/crates/shim/Cargo.toml +++ b/crates/shim/Cargo.toml @@ -24,6 +24,7 @@ serde = "1.0.136" uuid = { version = "0.8.2", features = ["v4"] } signal-hook = "0.3.13" oci-spec = "0.5.4" +prctl = "1.0.0" containerd-shim-protos = { path = "../shim-protos", version = "0.1.2" } diff --git a/crates/shim/src/lib.rs b/crates/shim/src/lib.rs index 80589c7..85853a5 100644 --- a/crates/shim/src/lib.rs +++ b/crates/shim/src/lib.rs @@ -207,7 +207,7 @@ where let signals = setup_signals(&config); if !config.no_sub_reaper { - reap::set_subreaper().map_err(io_error!(e, "set subreaper"))?; + reap::set_subreaper()?; } let mut shim = T::new( diff --git a/crates/shim/src/reap.rs b/crates/shim/src/reap.rs index bf563f0..db75373 100644 --- a/crates/shim/src/reap.rs +++ b/crates/shim/src/reap.rs @@ -14,7 +14,7 @@ limitations under the License. */ -use std::io::Result; +use crate::error::Result; #[cfg(target_os = "linux")] /// Set current process as subreaper for child processes. @@ -26,21 +26,23 @@ use std::io::Result; /// it is the subreaper process that will receive a SIGCHLD signal and will be able to `wait()` /// on the process to discover its termination status. pub fn set_subreaper() -> Result<()> { - use libc::PR_SET_CHILD_SUBREAPER; - use std::io::Error; - - // Set current process as `subreaper` for child processes if the second parameter is non-zero, - // otherwise unset the attribute. - // Safe because we trust the kernel and have checked the result. - let code = unsafe { libc::prctl(PR_SET_CHILD_SUBREAPER, 1, 0, 0) }; - if code != 0 { - Err(Error::from_raw_os_error(code)) - } else { - Ok(()) - } + use crate::error::Error; + prctl::set_child_subreaper(true).map_err(other_error!(code, "linux prctl returned")) } #[cfg(not(target_os = "linux"))] pub fn set_subreaper() -> Result<()> { Ok(()) } + +#[cfg(test)] +#[cfg(target_os = "linux")] +mod tests { + use crate::reap::set_subreaper; + + #[test] + fn test_set_subreaper() { + set_subreaper().unwrap(); + assert!(prctl::get_child_subreaper().unwrap()); + } +}