Skip to content

Commit

Permalink
feed containerd-shim monitor during tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Prendes <[email protected]>
  • Loading branch information
jprendes committed Jan 23, 2025
1 parent 4a4f9b0 commit f29e21c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions crates/containerd-shim-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ containerd-shim = { workspace = true }
containerd-shim-wasm-test-modules = { workspace = true, optional = true }
oci-tar-builder = { workspace = true, optional = true }
env_logger = { workspace = true, optional = true }
signal-hook = { version = "0.3", optional = true }
git-version = { version = "0.3.9" }
libc = { workspace = true }
log = { workspace = true }
Expand Down Expand Up @@ -94,12 +95,14 @@ tempfile = { workspace = true }
oci-tar-builder = { workspace = true }
rand = "0.8"
temp-env = "0.3"
signal-hook = "0.3"

[features]
testing = [
"dep:containerd-shim-wasm-test-modules",
"dep:env_logger",
"dep:tempfile",
"dep:signal-hook",
"dep:oci-tar-builder",
]
opentelemetry = [
Expand Down
54 changes: 53 additions & 1 deletion crates/containerd-shim-wasm/src/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@ use std::os::unix::fs::symlink;
#[cfg(windows)]
use std::os::windows::fs::symlink_file as symlink;
use std::path::Path;
use std::sync::Once;
use std::time::Duration;

use anyhow::{bail, Result};
use containerd_shim::monitor::monitor_notify_by_pid;
pub use containerd_shim_wasm_test_modules as modules;
use libc::{SIGINT, SIGTERM};
use libc::{SIGCHLD, SIGINT, SIGTERM};
use nix::errno::Errno;
use nix::sys::wait::{waitpid, WaitPidFlag, WaitStatus};
use nix::unistd::Pid;
use oci_spec::runtime::{
get_default_namespaces, LinuxBuilder, LinuxNamespace, LinuxNamespaceType, ProcessBuilder,
RootBuilder, SpecBuilder,
};
use signal_hook::iterator::Signals;

use crate::sandbox::{Instance, InstanceConfig};

Expand Down Expand Up @@ -50,6 +56,9 @@ where
pub fn new() -> Result<Self> {
zygote::Zygote::init();

// since we don't bootstrap containerd-shim, we need to feed it's monitor manually
setup_reaper();

// start logging
// to enable logging run `export RUST_LOG=trace` and append cargo command with
// --show-output before running test
Expand Down Expand Up @@ -310,6 +319,49 @@ where
}
}

fn setup_reaper() {
static SIGNALS: Once = Once::new();
SIGNALS.call_once(|| {
// this is taken from containerd-shim's signal handler implementation
let mut signals = Signals::new([SIGCHLD]).expect("new signal failed");
std::thread::spawn(move || {
loop {
for sig in signals.wait() {
match sig {
SIGCHLD => loop {
log::info!("SHIM HANDLING SIGCHLD");
// Note that this thread sticks to child even it is suspended.
match waitpid(Some(Pid::from_raw(-1)), Some(WaitPidFlag::WNOHANG)) {
Ok(WaitStatus::Exited(pid, status)) => {
log::debug!("child {} terminated(status = {})", pid, status);
if let Err(e) = monitor_notify_by_pid(pid.as_raw(), status) {
log::error!("failed to send exit event {e}");
}
}
Ok(WaitStatus::Signaled(pid, sig, _)) => {
log::debug!("child {} terminated(signal = {})", pid, sig);
if let Err(e) =
monitor_notify_by_pid(pid.as_raw(), 128 + sig as i32)
{
log::error!("failed to send signal event {}", e);
}
}
Ok(WaitStatus::StillAlive) => break,
Err(Errno::ECHILD) => break,
// stick until all children will be successfully waited, even some unexpected error occurs.
Err(e) => log::warn!("error occurred in signal handler: {}", e),
// stick until exit
_ => {}
}
},
_ => { /* no-op */ }
}
}
}
});
});
}

pub mod oci_helpers {
use std::fs::{write, File};
use std::process::{Command, Stdio};
Expand Down

0 comments on commit f29e21c

Please sign in to comment.