diff options
author | Matthew Heon <matthew.heon@gmail.com> | 2017-11-01 14:38:21 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-01 14:38:21 -0400 |
commit | f5019df3f5da9030ce21e5c8ad3d3921a6585e7f (patch) | |
tree | 05412dcc190ca026dbe51a4ef72bb91ff646e7c6 /libkpod/kill.go | |
parent | 2b74391cd5281f6fdf391ff8ad50fd1490f6bf89 (diff) | |
parent | eab0737f1189a7b88f0a37a6b894ca4345b6853f (diff) | |
download | podman-f5019df3f5da9030ce21e5c8ad3d3921a6585e7f.tar.gz podman-f5019df3f5da9030ce21e5c8ad3d3921a6585e7f.tar.bz2 podman-f5019df3f5da9030ce21e5c8ad3d3921a6585e7f.zip |
Merge pull request #1 from mheon/master
Initial checkin
Diffstat (limited to 'libkpod/kill.go')
-rw-r--r-- | libkpod/kill.go | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/libkpod/kill.go b/libkpod/kill.go new file mode 100644 index 000000000..fe7fbc2ba --- /dev/null +++ b/libkpod/kill.go @@ -0,0 +1,45 @@ +package libkpod + +import ( + "github.com/docker/docker/pkg/signal" + "github.com/projectatomic/libpod/oci" + "github.com/projectatomic/libpod/utils" + "github.com/pkg/errors" + "os" + "syscall" +) + +// Reverse lookup signal string from its map +func findStringInSignalMap(killSignal syscall.Signal) (string, error) { + for k, v := range signal.SignalMap { + if v == killSignal { + return k, nil + } + } + return "", errors.Errorf("unable to convert signal to string") + +} + +// ContainerKill sends the user provided signal to the containers primary process. +func (c *ContainerServer) ContainerKill(container string, killSignal syscall.Signal) (string, error) { // nolint + ctr, err := c.LookupContainer(container) + if err != nil { + return "", errors.Wrapf(err, "failed to find container %s", container) + } + c.runtime.UpdateStatus(ctr) + cStatus := c.runtime.ContainerStatus(ctr) + + // If the container is not running, error and move on. + if cStatus.Status != oci.ContainerStateRunning { + return "", errors.Errorf("cannot kill container %s: it is not running", container) + } + signalString, err := findStringInSignalMap(killSignal) + if err != nil { + return "", err + } + if err := utils.ExecCmdWithStdStreams(os.Stdin, os.Stdout, os.Stderr, c.runtime.Path(ctr), "kill", ctr.ID(), signalString); err != nil { + return "", err + } + c.ContainerStateToDisk(ctr) + return ctr.ID(), nil +} |