1
0
mirror of https://github.com/containers/youki synced 2024-11-23 01:11:58 +01:00

support the all option in the kill command.

Signed-off-by: utam0k <k0ma@utam0k.jp>
This commit is contained in:
utam0k 2022-05-23 21:19:37 +09:00
parent 607d28b99d
commit 0f3f43b66c
No known key found for this signature in database
GPG Key ID: 14ACDDE088DF059E
3 changed files with 25 additions and 5 deletions

@ -1,6 +1,7 @@
use super::{Container, ContainerStatus};
use crate::signal::Signal;
use anyhow::{bail, Context, Result};
use libcgroups::common::create_cgroup_manager;
use nix::sys::signal::{self};
impl Container {
@ -18,17 +19,34 @@ impl Container {
/// .as_init("/var/run/docker/bundle")
/// .build()?;
///
/// container.kill(Signal::SIGKILL)?;
/// container.kill(Signal::SIGKILL, false)?;
/// # Ok(())
/// # }
/// ```
pub fn kill<S: Into<Signal>>(&mut self, signal: S) -> Result<()> {
pub fn kill<S: Into<Signal>>(&mut self, signal: S, all: bool) -> Result<()> {
let signal = signal.into().into_raw();
let pids = if all {
let cgroups_path = self.spec()?.cgroup_path;
let use_systemd = self
.systemd()
.context("container state does not contain cgroup manager")?;
let cmanger = create_cgroup_manager(&cgroups_path, use_systemd, self.id())?;
cmanger.get_all_pids()?
} else {
vec![self
.pid()
.context("failed to get the pid of the container")?]
};
self.refresh_status()
.context("failed to refresh container status")?;
if self.can_kill() {
log::debug!("kill signal {} to {}", signal, self.pid().unwrap());
signal::kill(self.pid().unwrap(), signal)?;
pids.into_iter().try_for_each(|pid| {
log::debug!("kill signal {} to {}", signal, pid);
signal::kill(pid, signal)
})?;
self.set_status(ContainerStatus::Stopped).save()?;
std::process::exit(0)
} else {

@ -6,4 +6,6 @@ pub struct Kill {
#[clap(forbid_empty_values = true, required = true)]
pub container_id: String,
pub signal: String,
#[clap(short, long)]
pub all: bool,
}

@ -10,5 +10,5 @@ use liboci_cli::Kill;
pub fn kill(args: Kill, root_path: PathBuf) -> Result<()> {
let mut container = load_container(root_path, &args.container_id)?;
let signal: Signal = args.signal.as_str().try_into()?;
container.kill(signal)
container.kill(signal, args.all)
}