runtime-rs: Ignore SIGTERM signal in shim

When enabling systemd cgroup driver and sandbox cgroup only, the shim is
under a systemd unit. When the unit is stopping, systemd sends SIGTERM to
the shim. The shim can't exit immediately, as there are some cleanups to
do. Therefore, ignoring SIGTERM is required here. The shim should complete
the work within a period (Kata sets it to 300s by default). Once a timeout
occurs, systemd will send SIGKILL.

Signed-off-by: Xuewei Niu <niuxuewei.nxw@antgroup.com>
This commit is contained in:
Xuewei Niu
2025-07-16 17:02:28 +08:00
parent 79f29bc523
commit 635272f3e8

View File

@@ -13,6 +13,7 @@ use anyhow::{anyhow, Context, Result};
use nix::{
mount::{mount, MsFlags},
sched::{self, CloneFlags},
sys::signal::{signal, SigHandler, Signal},
};
use shim::{config, Args, Error, ShimExecutor};
@@ -159,6 +160,17 @@ fn real_main() -> Result<()> {
Ok(())
}
fn main() {
// When enabling systemd cgroup driver and sandbox cgroup only, the
// shim is under a systemd unit. When the unit is stopping, systemd
// sends SIGTERM to the shim. The shim can't exit immediately, as there
// are some cleanups to do. Therefore, ignoring SIGTERM is required
// here. The shim should complete the work within a period (Kata sets
// it to 300s by default). Once a timeout occurs, systemd will send
// SIGKILL.
unsafe {
signal(Signal::SIGTERM, SigHandler::SigIgn).unwrap();
}
if let Err(err) = real_main() {
show_version(Some(err));
}