Skip to content

Commit

Permalink
Add parse signal function for runc kill
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
  • Loading branch information
crosbymichael committed Aug 4, 2015
1 parent 5a4e4da commit fbb8d3e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 81 deletions.
7 changes: 0 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,3 @@ WorkingDirectory=/containers/minecraftbuild
[Install]
WantedBy=multi-user.target
```
##Usage:
runc --id=runc kill <arguments>
####Arguments can be signal number or signal name in string

##Example
runc --id=runc kill TERM
runc --id=runc kill 15
89 changes: 58 additions & 31 deletions kill.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 1 addition & 0 deletions main_unsupported.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var (
eventsCommand cli.Command
restoreCommand cli.Command
specCommand cli.Command
killCommand cli.Command
)

func runAction(*cli.Context) {
Expand Down
43 changes: 0 additions & 43 deletions signal_linux.go

This file was deleted.

0 comments on commit fbb8d3e

Please sign in to comment.