2017-02-01 00:45:59 +00:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
|
2017-02-06 20:16:36 +00:00
|
|
|
"github.com/urfave/cli"
|
2017-02-01 00:45:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var signalMap = map[string]syscall.Signal{
|
|
|
|
"ABRT": syscall.SIGABRT,
|
|
|
|
"ALRM": syscall.SIGALRM,
|
|
|
|
"BUS": syscall.SIGBUS,
|
|
|
|
"CHLD": syscall.SIGCHLD,
|
|
|
|
"CLD": syscall.SIGCLD,
|
|
|
|
"CONT": syscall.SIGCONT,
|
|
|
|
"FPE": syscall.SIGFPE,
|
|
|
|
"HUP": syscall.SIGHUP,
|
|
|
|
"ILL": syscall.SIGILL,
|
|
|
|
"INT": syscall.SIGINT,
|
|
|
|
"IO": syscall.SIGIO,
|
|
|
|
"IOT": syscall.SIGIOT,
|
|
|
|
"KILL": syscall.SIGKILL,
|
|
|
|
"PIPE": syscall.SIGPIPE,
|
|
|
|
"POLL": syscall.SIGPOLL,
|
|
|
|
"PROF": syscall.SIGPROF,
|
|
|
|
"PWR": syscall.SIGPWR,
|
|
|
|
"QUIT": syscall.SIGQUIT,
|
|
|
|
"SEGV": syscall.SIGSEGV,
|
|
|
|
"STKFLT": syscall.SIGSTKFLT,
|
|
|
|
"STOP": syscall.SIGSTOP,
|
|
|
|
"SYS": syscall.SIGSYS,
|
|
|
|
"TERM": syscall.SIGTERM,
|
|
|
|
"TRAP": syscall.SIGTRAP,
|
|
|
|
"TSTP": syscall.SIGTSTP,
|
|
|
|
"TTIN": syscall.SIGTTIN,
|
|
|
|
"TTOU": syscall.SIGTTOU,
|
|
|
|
"UNUSED": syscall.SIGUNUSED,
|
|
|
|
"URG": syscall.SIGURG,
|
|
|
|
"USR1": syscall.SIGUSR1,
|
|
|
|
"USR2": syscall.SIGUSR2,
|
|
|
|
"VTALRM": syscall.SIGVTALRM,
|
|
|
|
"WINCH": syscall.SIGWINCH,
|
|
|
|
"XCPU": syscall.SIGXCPU,
|
|
|
|
"XFSZ": syscall.SIGXFSZ,
|
|
|
|
}
|
|
|
|
|
|
|
|
var killCommand = cli.Command{
|
|
|
|
Name: "kill",
|
|
|
|
Usage: "kill sends the specified signal (default: SIGTERM) to the container's init process",
|
2017-02-06 20:16:36 +00:00
|
|
|
ArgsUsage: `<container-id> [signal]
|
2017-02-01 00:45:59 +00:00
|
|
|
|
|
|
|
Where "<container-id>" is the name for the instance of the container and
|
2017-02-06 20:16:36 +00:00
|
|
|
"[signal]" is the signal to be sent to the init process.
|
|
|
|
|
|
|
|
EXAMPLE:
|
2017-02-01 00:45:59 +00:00
|
|
|
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`,
|
2017-02-06 20:16:36 +00:00
|
|
|
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 {
|
|
|
|
if err := checkArgs(context, 1, minArgs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := checkArgs(context, 2, maxArgs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-02-01 00:45:59 +00:00
|
|
|
container, err := getContainer(context)
|
|
|
|
if err != nil {
|
2017-02-06 20:16:36 +00:00
|
|
|
return err
|
2017-02-01 00:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sigstr := context.Args().Get(1)
|
|
|
|
if sigstr == "" {
|
|
|
|
sigstr = "SIGTERM"
|
|
|
|
}
|
|
|
|
|
|
|
|
signal, err := parseSignal(sigstr)
|
|
|
|
if err != nil {
|
2017-02-06 20:16:36 +00:00
|
|
|
return err
|
2017-02-01 00:45:59 +00:00
|
|
|
}
|
2017-02-06 20:16:36 +00:00
|
|
|
if err := container.Signal(signal, context.Bool("all")); err != nil {
|
|
|
|
return err
|
2017-02-01 00:45:59 +00:00
|
|
|
}
|
2017-02-06 20:16:36 +00:00
|
|
|
return nil
|
2017-02-01 00:45:59 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseSignal(rawSignal string) (syscall.Signal, error) {
|
|
|
|
s, err := strconv.Atoi(rawSignal)
|
|
|
|
if err == nil {
|
2017-02-06 20:16:36 +00:00
|
|
|
sig := syscall.Signal(s)
|
|
|
|
for _, msig := range signalMap {
|
|
|
|
if sig == msig {
|
|
|
|
return sig, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1, fmt.Errorf("unknown signal %q", rawSignal)
|
2017-02-01 00:45:59 +00:00
|
|
|
}
|
|
|
|
signal, ok := signalMap[strings.TrimPrefix(strings.ToUpper(rawSignal), "SIG")]
|
|
|
|
if !ok {
|
|
|
|
return -1, fmt.Errorf("unknown signal %q", rawSignal)
|
|
|
|
}
|
|
|
|
return signal, nil
|
|
|
|
}
|