diff --git a/src/tools/runk/libcontainer/src/container.rs b/src/tools/runk/libcontainer/src/container.rs index fce2d9ecd4..260ea9d367 100644 --- a/src/tools/runk/libcontainer/src/container.rs +++ b/src/tools/runk/libcontainer/src/container.rs @@ -83,14 +83,6 @@ impl Container { } pub fn kill(&self, signal: Signal, all: bool) -> Result<()> { - if self.state == ContainerState::Stopped { - return Err(anyhow!( - "container {} can't be killed because it is {:?}", - self.status.id, - self.state - )); - } - if all { let pids = self.processes()?; for pid in pids { @@ -100,6 +92,16 @@ impl Container { kill(pid, signal)?; } } else { + // If --all option is not specified and the container is stopped, + // kill operation generates an error in accordance with the OCI runtime spec. + if self.state == ContainerState::Stopped { + return Err(anyhow!( + "container {} can't be killed because it is {:?}", + self.status.id, + self.state + )); + } + let pid = Pid::from_raw(self.status.pid); if status::is_process_running(pid)? { kill(pid, signal)?; diff --git a/src/tools/runk/libcontainer/src/status.rs b/src/tools/runk/libcontainer/src/status.rs index 6a3480c591..a5187e1f4f 100644 --- a/src/tools/runk/libcontainer/src/status.rs +++ b/src/tools/runk/libcontainer/src/status.rs @@ -141,7 +141,7 @@ pub fn is_process_running(pid: Pid) -> Result { match kill(pid, None) { Err(errno) => { if errno != Errno::ESRCH { - return Err(anyhow!("no such process")); + return Err(anyhow!("failed to kill process {}: {:?}", pid, errno)); } Ok(false) }