2014-07-31 16:24:54 -04:00
|
|
|
package daemon
|
|
|
|
|
2015-10-12 19:34:03 -04:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
|
|
|
"syscall"
|
2015-11-02 18:25:26 -05:00
|
|
|
"time"
|
2015-10-12 19:34:03 -04:00
|
|
|
|
2015-11-02 18:25:26 -05:00
|
|
|
"github.com/Sirupsen/logrus"
|
2015-11-12 14:55:17 -05:00
|
|
|
"github.com/docker/docker/container"
|
2015-11-02 18:25:26 -05:00
|
|
|
derr "github.com/docker/docker/errors"
|
2015-10-12 19:34:03 -04:00
|
|
|
"github.com/docker/docker/pkg/signal"
|
|
|
|
)
|
2014-07-31 16:24:54 -04:00
|
|
|
|
|
|
|
// ContainerKill send signal to the container
|
|
|
|
// If no signal is given (sig 0), then Kill with SIGKILL and wait
|
|
|
|
// for the container to exit.
|
|
|
|
// If a signal is given, then just send it to the container and return.
|
2015-09-29 13:51:40 -04:00
|
|
|
func (daemon *Daemon) ContainerKill(name string, sig uint64) error {
|
2015-12-11 12:39:28 -05:00
|
|
|
container, err := daemon.GetContainer(name)
|
2014-12-16 18:06:35 -05:00
|
|
|
if err != nil {
|
2015-03-25 03:44:12 -04:00
|
|
|
return err
|
2014-12-16 18:06:35 -05:00
|
|
|
}
|
|
|
|
|
2015-10-12 19:34:03 -04:00
|
|
|
if sig != 0 && !signal.ValidSignalForPlatform(syscall.Signal(sig)) {
|
|
|
|
return fmt.Errorf("The %s daemon does not support signal %d", runtime.GOOS, sig)
|
|
|
|
}
|
|
|
|
|
2014-12-16 18:06:35 -05:00
|
|
|
// If no signal is passed, or SIGKILL, perform regular Kill (SIGKILL + wait())
|
|
|
|
if sig == 0 || syscall.Signal(sig) == syscall.SIGKILL {
|
2015-11-11 20:19:39 -05:00
|
|
|
return daemon.Kill(container)
|
2014-07-31 16:24:54 -04:00
|
|
|
}
|
2015-11-11 20:19:39 -05:00
|
|
|
return daemon.killWithSignal(container, int(sig))
|
2014-07-31 16:24:54 -04:00
|
|
|
}
|
2015-11-02 18:25:26 -05:00
|
|
|
|
|
|
|
// killWithSignal sends the container the given signal. This wrapper for the
|
|
|
|
// host specific kill command prepares the container before attempting
|
|
|
|
// to send the signal. An error is returned if the container is paused
|
|
|
|
// or not running, or if there is a problem returned from the
|
|
|
|
// underlying kill command.
|
2015-11-12 14:55:17 -05:00
|
|
|
func (daemon *Daemon) killWithSignal(container *container.Container, sig int) error {
|
2015-11-02 18:25:26 -05:00
|
|
|
logrus.Debugf("Sending %d to %s", sig, container.ID)
|
|
|
|
container.Lock()
|
|
|
|
defer container.Unlock()
|
|
|
|
|
|
|
|
// We could unpause the container for them rather than returning this error
|
|
|
|
if container.Paused {
|
|
|
|
return derr.ErrorCodeUnpauseContainer.WithArgs(container.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !container.Running {
|
|
|
|
return derr.ErrorCodeNotRunning.WithArgs(container.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
container.ExitOnNext()
|
|
|
|
|
|
|
|
// if the container is currently restarting we do not need to send the signal
|
|
|
|
// to the process. Telling the monitor that it should exit on it's next event
|
|
|
|
// loop is enough
|
|
|
|
if container.Restarting {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := daemon.kill(container, sig); err != nil {
|
Remove redundant error message
Currently some commands including `kill`, `pause`, `restart`, `rm`,
`rmi`, `stop`, `unpause`, `udpate`, `wait` will print a lot of error
message on client side, with a lot of redundant messages, this commit is
trying to remove the unuseful and redundant information for user.
Signed-off-by: Zhang Wei <zhangwei555@huawei.com>
2016-02-03 01:29:15 -05:00
|
|
|
return derr.ErrorCodeCantKill.WithArgs(container.ID, err)
|
2015-11-02 18:25:26 -05:00
|
|
|
}
|
|
|
|
|
2016-01-07 17:14:05 -05:00
|
|
|
attributes := map[string]string{
|
|
|
|
"signal": fmt.Sprintf("%d", sig),
|
|
|
|
}
|
|
|
|
daemon.LogContainerEventWithAttributes(container, "kill", attributes)
|
2015-11-02 18:25:26 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Kill forcefully terminates a container.
|
2015-11-12 14:55:17 -05:00
|
|
|
func (daemon *Daemon) Kill(container *container.Container) error {
|
2015-11-02 18:25:26 -05:00
|
|
|
if !container.IsRunning() {
|
|
|
|
return derr.ErrorCodeNotRunning.WithArgs(container.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1. Send SIGKILL
|
|
|
|
if err := daemon.killPossiblyDeadProcess(container, int(syscall.SIGKILL)); err != nil {
|
|
|
|
// While normally we might "return err" here we're not going to
|
|
|
|
// because if we can't stop the container by this point then
|
|
|
|
// its probably because its already stopped. Meaning, between
|
|
|
|
// the time of the IsRunning() call above and now it stopped.
|
|
|
|
// Also, since the err return will be exec driver specific we can't
|
|
|
|
// look for any particular (common) error that would indicate
|
|
|
|
// that the process is already dead vs something else going wrong.
|
|
|
|
// So, instead we'll give it up to 2 more seconds to complete and if
|
|
|
|
// by that time the container is still running, then the error
|
|
|
|
// we got is probably valid and so we return it to the caller.
|
|
|
|
|
|
|
|
if container.IsRunning() {
|
|
|
|
container.WaitStop(2 * time.Second)
|
|
|
|
if container.IsRunning() {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Wait for the process to die, in last resort, try to kill the process directly
|
|
|
|
if err := killProcessDirectly(container); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
container.WaitStop(-1 * time.Second)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-13 11:00:39 -05:00
|
|
|
// killPossibleDeadProcess is a wrapper around killSig() suppressing "no such process" error.
|
2015-11-12 14:55:17 -05:00
|
|
|
func (daemon *Daemon) killPossiblyDeadProcess(container *container.Container, sig int) error {
|
2015-11-02 18:25:26 -05:00
|
|
|
err := daemon.killWithSignal(container, sig)
|
|
|
|
if err == syscall.ESRCH {
|
|
|
|
logrus.Debugf("Cannot kill process (pid=%d) with signal %d: no such process.", container.GetPID(), sig)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|