summaryrefslogtreecommitdiff
path: root/libkpod/kill.go
blob: 74c525818ef1a5a0c0afd4d001ba8ae095c2a7d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package libkpod

import (
	"github.com/docker/docker/pkg/signal"
	"github.com/pkg/errors"
	"github.com/projectatomic/libpod/oci"
	"github.com/projectatomic/libpod/utils"
	"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
}