2019-01-12 04:58:27 +00:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/urfave/cli"
|
2020-08-10 17:43:49 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2019-01-12 04:58:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var killCommand = cli.Command{
|
|
|
|
Name: "kill",
|
|
|
|
Usage: "kill sends the specified signal (default: SIGTERM) to the container's init process",
|
|
|
|
ArgsUsage: `<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.
|
|
|
|
|
|
|
|
EXAMPLE:
|
|
|
|
For example, if the container id is "ubuntu01" the following will send a "KILL"
|
|
|
|
signal to the init process of the "ubuntu01" container:
|
2020-08-10 17:43:49 +00:00
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
# 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 {
|
|
|
|
if err := checkArgs(context, 1, minArgs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := checkArgs(context, 2, maxArgs); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
container, err := getContainer(context)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sigstr := context.Args().Get(1)
|
|
|
|
if sigstr == "" {
|
|
|
|
sigstr = "SIGTERM"
|
|
|
|
}
|
|
|
|
|
|
|
|
signal, err := parseSignal(sigstr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return container.Signal(signal, context.Bool("all"))
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
func parseSignal(rawSignal string) (unix.Signal, error) {
|
2019-01-12 04:58:27 +00:00
|
|
|
s, err := strconv.Atoi(rawSignal)
|
|
|
|
if err == nil {
|
2020-08-10 17:43:49 +00:00
|
|
|
return unix.Signal(s), nil
|
|
|
|
}
|
|
|
|
sig := strings.ToUpper(rawSignal)
|
|
|
|
if !strings.HasPrefix(sig, "SIG") {
|
|
|
|
sig = "SIG" + sig
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
signal := unix.SignalNum(sig)
|
|
|
|
if signal == 0 {
|
2019-01-12 04:58:27 +00:00
|
|
|
return -1, fmt.Errorf("unknown signal %q", rawSignal)
|
|
|
|
}
|
|
|
|
return signal, nil
|
|
|
|
}
|