From fbb8d3e8feb5a66fba1624219d6ae831457dbacc Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Tue, 4 Aug 2015 10:45:31 -0700 Subject: [PATCH] Add parse signal function for runc kill Signed-off-by: Michael Crosby --- README.md | 7 ---- kill.go | 89 +++++++++++++++++++++++++++++---------------- main_unsupported.go | 1 + signal_linux.go | 43 ---------------------- 4 files changed, 59 insertions(+), 81 deletions(-) delete mode 100644 signal_linux.go diff --git a/README.md b/README.md index 89f26dbdabe..4e83fe9d161 100644 --- a/README.md +++ b/README.md @@ -237,10 +237,3 @@ WorkingDirectory=/containers/minecraftbuild [Install] WantedBy=multi-user.target ``` -##Usage: -runc --id=runc kill -####Arguments can be signal number or signal name in string - -##Example -runc --id=runc kill TERM -runc --id=runc kill 15 diff --git a/kill.go b/kill.go index 1141682d808..6fbbba89475 100644 --- a/kill.go +++ b/kill.go @@ -4,50 +4,77 @@ package main import ( "fmt" - "github.com/codegangsta/cli" "strconv" "strings" "syscall" + + "github.com/codegangsta/cli" ) +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 a container", + Usage: "kill sends the specified signal to the container's init process", Action: func(context *cli.Context) { container, err := getContainer(context) if err != nil { - fatal(fmt.Errorf("%s", err)) + fatal(err) } - sigStr := context.Args().First() - state, err := container.Status() + signal, err := parseSignal(context.Args().First()) if err != nil { - fatal(fmt.Errorf("Container not running %d", state)) - // return here + fatal(err) } - var sig uint64 - sigN, err := strconv.ParseUint(sigStr, 10, 5) - if err != nil { - //The signal is not a number, treat it as a string (either like - //KILL" or like "SIGKILL") - syscallSig, ok := SignalMap[strings.TrimPrefix(sigStr, "SIG")] - if !ok { - fatal(fmt.Errorf("Invalid Signal: %s", sigStr)) - } - sig = uint64(syscallSig) - errVal := container.Signal(syscall.Signal(sig)) - if errVal != nil { - fatal(fmt.Errorf("%s", errVal)) - } - } else { - sig = sigN - errVar := container.Signal(syscall.Signal(sig)) - if errVar != nil { - fatal(fmt.Errorf("%s", errVar)) - } - - } - if sig == 0 { - fatal(fmt.Errorf("Invalid signal: %s", sigStr)) + if err := container.Signal(signal); err != nil { + fatal(err) } }, } + +func parseSignal(rawSignal string) (syscall.Signal, error) { + s, err := strconv.Atoi(rawSignal) + if err == nil { + return syscall.Signal(s), nil + } + signal, ok := signalMap[strings.TrimPrefix(strings.ToUpper(rawSignal), "SIG")] + if !ok { + return -1, fmt.Errorf("unknown signal %q", rawSignal) + } + return signal, nil +} diff --git a/main_unsupported.go b/main_unsupported.go index 837324c6b8c..00031e473cf 100644 --- a/main_unsupported.go +++ b/main_unsupported.go @@ -16,6 +16,7 @@ var ( eventsCommand cli.Command restoreCommand cli.Command specCommand cli.Command + killCommand cli.Command ) func runAction(*cli.Context) { diff --git a/signal_linux.go b/signal_linux.go deleted file mode 100644 index 81adb59ec35..00000000000 --- a/signal_linux.go +++ /dev/null @@ -1,43 +0,0 @@ -package main - -import ( - "syscall" -) - -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, -}