shim: refine set subreaper

Signed-off-by: Zhang Tianyang <burning9699@gmail.com>
This commit is contained in:
Zhang Tianyang 2022-03-03 17:24:24 +08:00
parent 375bc65791
commit fda23cd59f
3 changed files with 17 additions and 14 deletions

View File

@ -24,6 +24,7 @@ serde = "1.0.136"
uuid = { version = "0.8.2", features = ["v4"] } uuid = { version = "0.8.2", features = ["v4"] }
signal-hook = "0.3.13" signal-hook = "0.3.13"
oci-spec = "0.5.4" oci-spec = "0.5.4"
prctl = "1.0.0"
containerd-shim-protos = { path = "../shim-protos", version = "0.1.2" } containerd-shim-protos = { path = "../shim-protos", version = "0.1.2" }

View File

@ -207,7 +207,7 @@ where
let signals = setup_signals(&config); let signals = setup_signals(&config);
if !config.no_sub_reaper { if !config.no_sub_reaper {
reap::set_subreaper().map_err(io_error!(e, "set subreaper"))?; reap::set_subreaper()?;
} }
let mut shim = T::new( let mut shim = T::new(

View File

@ -14,7 +14,7 @@
limitations under the License. limitations under the License.
*/ */
use std::io::Result; use crate::error::Result;
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
/// Set current process as subreaper for child processes. /// 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()` /// 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. /// on the process to discover its termination status.
pub fn set_subreaper() -> Result<()> { pub fn set_subreaper() -> Result<()> {
use libc::PR_SET_CHILD_SUBREAPER; use crate::error::Error;
use std::io::Error; prctl::set_child_subreaper(true).map_err(other_error!(code, "linux prctl returned"))
// 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(())
}
} }
#[cfg(not(target_os = "linux"))] #[cfg(not(target_os = "linux"))]
pub fn set_subreaper() -> Result<()> { pub fn set_subreaper() -> Result<()> {
Ok(()) 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());
}
}