Skip to content

Commit

Permalink
Merge pull request #1180 from crosbymichael/kill-all
Browse files Browse the repository at this point in the history
Add --all flag to kill
  • Loading branch information
Mrunal Patel authored Nov 9, 2016
2 parents 8779fa5 + e58671e commit 5137186
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 11 deletions.
2 changes: 2 additions & 0 deletions contrib/completions/bash/runc
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ _runc_kill() {
local boolean_options="
--help
-h
--all
-a
"

case "$prev" in
Expand Down
4 changes: 2 additions & 2 deletions delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
)

func killContainer(container libcontainer.Container) error {
container.Signal(syscall.SIGKILL)
container.Signal(syscall.SIGKILL, false)
for i := 0; i < 100; i++ {
time.Sleep(100 * time.Millisecond)
if err := container.Signal(syscall.Signal(0)); err != nil {
if err := container.Signal(syscall.Signal(0), false); err != nil {
destroy(container)
return nil
}
Expand Down
8 changes: 7 additions & 1 deletion kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ For example, if the container id is "ubuntu01" the following will send a "KILL"
signal to the init process of the "ubuntu01" container:
# runc kill ubuntu01 KILL`,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "all, a",
Usage: "send the specified signal to all processes inside the container",
},
},
Action: func(context *cli.Context) error {
container, err := getContainer(context)
if err != nil {
Expand All @@ -77,7 +83,7 @@ signal to the init process of the "ubuntu01" container:
if err != nil {
return err
}
if err := container.Signal(signal); err != nil {
if err := container.Signal(signal, context.Bool("all")); err != nil {
return err
}
return nil
Expand Down
5 changes: 4 additions & 1 deletion libcontainer/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,12 @@ type BaseContainer interface {

// Signal sends the provided signal code to the container's initial process.
//
// If all is specified the signal is sent to all processes in the container
// including the initial process.
//
// errors:
// SystemError - System error.
Signal(s os.Signal) error
Signal(s os.Signal, all bool) error

// Exec signals the container to exec the users process at the end of the init.
//
Expand Down
5 changes: 4 additions & 1 deletion libcontainer/container_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,10 @@ func (c *linuxContainer) start(process *Process, isInit bool) error {
return nil
}

func (c *linuxContainer) Signal(s os.Signal) error {
func (c *linuxContainer) Signal(s os.Signal, all bool) error {
if all {
return signalAllProcesses(c.cgroupManager, s)
}
if err := c.initProcess.signal(s); err != nil {
return newSystemErrorWithCause(err, "signaling init process")
}
Expand Down
6 changes: 3 additions & 3 deletions libcontainer/init_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,10 +334,10 @@ func setOomScoreAdj(oomScoreAdj int, pid int) error {
return ioutil.WriteFile(path, []byte(strconv.Itoa(oomScoreAdj)), 0600)
}

// killCgroupProcesses freezes then iterates over all the processes inside the
// signalAllProcesses freezes then iterates over all the processes inside the
// manager's cgroups sending a SIGKILL to each process then waiting for them to
// exit.
func killCgroupProcesses(m cgroups.Manager) error {
func signalAllProcesses(m cgroups.Manager, s os.Signal) error {
var procs []*os.Process
if err := m.Freeze(configs.Frozen); err != nil {
logrus.Warn(err)
Expand All @@ -354,7 +354,7 @@ func killCgroupProcesses(m cgroups.Manager) error {
continue
}
procs = append(procs, p)
if err := p.Kill(); err != nil {
if err := p.Signal(s); err != nil {
logrus.Warn(err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion libcontainer/process_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ func (p *initProcess) wait() (*os.ProcessState, error) {
}
// we should kill all processes in cgroup when init is died if we use host PID namespace
if p.sharePidns {
killCgroupProcesses(p.manager)
signalAllProcesses(p.manager, syscall.SIGKILL)
}
return p.cmd.ProcessState, nil
}
Expand Down
2 changes: 1 addition & 1 deletion libcontainer/state_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type containerState interface {

func destroy(c *linuxContainer) error {
if !c.config.Namespaces.Contains(configs.NEWPID) {
if err := killCgroupProcesses(c.cgroupManager); err != nil {
if err := signalAllProcesses(c.cgroupManager, syscall.SIGKILL); err != nil {
logrus.Warn(err)
}
}
Expand Down
5 changes: 4 additions & 1 deletion man/runc-kill.8.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
runc kill - kill sends the specified signal (default: SIGTERM) to the container's init process

# SYNOPSIS
runc kill <container-id> <signal>
runc kill [command options] <container-id> <signal>

Where "<container-id>" is the name for the instance of the container and
"<signal>" is the signal to be sent to the init process.

# OPTIONS
--all, -a send the specified signal to all processes inside the container

# EXAMPLE

For example, if the container id is "ubuntu01" the following will send a "KILL"
Expand Down

0 comments on commit 5137186

Please sign in to comment.