2015-08-03 18:42:20 +08:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
2015-08-05 01:45:31 +08:00
|
|
|
|
2017-05-11 23:06:37 +08:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
|
2016-06-07 02:45:46 +08:00
|
|
|
"github.com/urfave/cli"
|
2015-08-03 18:42:20 +08:00
|
|
|
)
|
|
|
|
|
2015-08-05 01:45:31 +08:00
|
|
|
var signalMap = map[string]syscall.Signal{
|
2017-05-11 23:06:37 +08:00
|
|
|
"ABRT": unix.SIGABRT,
|
|
|
|
"ALRM": unix.SIGALRM,
|
|
|
|
"BUS": unix.SIGBUS,
|
|
|
|
"CHLD": unix.SIGCHLD,
|
|
|
|
"CLD": unix.SIGCLD,
|
|
|
|
"CONT": unix.SIGCONT,
|
|
|
|
"FPE": unix.SIGFPE,
|
|
|
|
"HUP": unix.SIGHUP,
|
|
|
|
"ILL": unix.SIGILL,
|
|
|
|
"INT": unix.SIGINT,
|
|
|
|
"IO": unix.SIGIO,
|
|
|
|
"IOT": unix.SIGIOT,
|
|
|
|
"KILL": unix.SIGKILL,
|
|
|
|
"PIPE": unix.SIGPIPE,
|
|
|
|
"POLL": unix.SIGPOLL,
|
|
|
|
"PROF": unix.SIGPROF,
|
|
|
|
"PWR": unix.SIGPWR,
|
|
|
|
"QUIT": unix.SIGQUIT,
|
|
|
|
"SEGV": unix.SIGSEGV,
|
|
|
|
"STKFLT": unix.SIGSTKFLT,
|
|
|
|
"STOP": unix.SIGSTOP,
|
|
|
|
"SYS": unix.SIGSYS,
|
|
|
|
"TERM": unix.SIGTERM,
|
|
|
|
"TRAP": unix.SIGTRAP,
|
|
|
|
"TSTP": unix.SIGTSTP,
|
|
|
|
"TTIN": unix.SIGTTIN,
|
|
|
|
"TTOU": unix.SIGTTOU,
|
|
|
|
"UNUSED": unix.SIGUNUSED,
|
|
|
|
"URG": unix.SIGURG,
|
|
|
|
"USR1": unix.SIGUSR1,
|
|
|
|
"USR2": unix.SIGUSR2,
|
|
|
|
"VTALRM": unix.SIGVTALRM,
|
|
|
|
"WINCH": unix.SIGWINCH,
|
|
|
|
"XCPU": unix.SIGXCPU,
|
|
|
|
"XFSZ": unix.SIGXFSZ,
|
2015-08-05 01:45:31 +08:00
|
|
|
}
|
|
|
|
|
2015-08-03 18:42:20 +08:00
|
|
|
var killCommand = cli.Command{
|
|
|
|
Name: "kill",
|
2015-08-13 23:42:54 +08:00
|
|
|
Usage: "kill sends the specified signal (default: SIGTERM) to the container's init process",
|
2017-01-19 05:49:44 +08:00
|
|
|
ArgsUsage: `<container-id> [signal]
|
2016-02-11 01:30:06 +08:00
|
|
|
|
|
|
|
Where "<container-id>" is the name for the instance of the container and
|
2017-01-19 05:49:44 +08:00
|
|
|
"[signal]" is the signal to be sent to the init process.
|
2016-05-24 16:59:24 +08:00
|
|
|
|
|
|
|
EXAMPLE:
|
2016-02-11 01:30:06 +08: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`,
|
2016-11-08 07:22:27 +08:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.BoolFlag{
|
|
|
|
Name: "all, a",
|
|
|
|
Usage: "send the specified signal to all processes inside the container",
|
|
|
|
},
|
|
|
|
},
|
2016-05-10 13:58:09 +08:00
|
|
|
Action: func(context *cli.Context) error {
|
2017-01-19 05:49:44 +08:00
|
|
|
if err := checkArgs(context, 1, minArgs); err != nil {
|
2016-10-28 23:43:10 +08:00
|
|
|
return err
|
|
|
|
}
|
2017-02-03 00:32:52 +08:00
|
|
|
if err := checkArgs(context, 2, maxArgs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-03 18:42:20 +08:00
|
|
|
container, err := getContainer(context)
|
|
|
|
if err != nil {
|
2016-05-10 13:58:09 +08:00
|
|
|
return err
|
2015-08-03 18:42:20 +08:00
|
|
|
}
|
2015-08-13 23:42:54 +08:00
|
|
|
|
2016-02-09 06:25:03 +08:00
|
|
|
sigstr := context.Args().Get(1)
|
2015-08-13 23:42:54 +08:00
|
|
|
if sigstr == "" {
|
|
|
|
sigstr = "SIGTERM"
|
|
|
|
}
|
|
|
|
|
|
|
|
signal, err := parseSignal(sigstr)
|
2015-08-03 18:42:20 +08:00
|
|
|
if err != nil {
|
2016-05-10 13:58:09 +08:00
|
|
|
return err
|
2015-08-03 18:42:20 +08:00
|
|
|
}
|
2016-11-08 07:22:27 +08:00
|
|
|
if err := container.Signal(signal, context.Bool("all")); err != nil {
|
2016-05-10 13:58:09 +08:00
|
|
|
return err
|
2015-08-03 18:42:20 +08:00
|
|
|
}
|
2016-05-10 13:58:09 +08:00
|
|
|
return nil
|
2015-08-03 18:42:20 +08:00
|
|
|
},
|
|
|
|
}
|
2015-08-05 01:45:31 +08:00
|
|
|
|
|
|
|
func parseSignal(rawSignal string) (syscall.Signal, error) {
|
|
|
|
s, err := strconv.Atoi(rawSignal)
|
|
|
|
if err == nil {
|
2016-10-11 14:15:04 +08:00
|
|
|
sig := syscall.Signal(s)
|
|
|
|
for _, msig := range signalMap {
|
|
|
|
if sig == msig {
|
|
|
|
return sig, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1, fmt.Errorf("unknown signal %q", rawSignal)
|
2015-08-05 01:45:31 +08:00
|
|
|
}
|
|
|
|
signal, ok := signalMap[strings.TrimPrefix(strings.ToUpper(rawSignal), "SIG")]
|
|
|
|
if !ok {
|
|
|
|
return -1, fmt.Errorf("unknown signal %q", rawSignal)
|
|
|
|
}
|
|
|
|
return signal, nil
|
|
|
|
}
|