2014-07-31 16:40:15 -04:00
|
|
|
package daemon
|
|
|
|
|
2015-09-16 14:56:26 -04:00
|
|
|
import (
|
2017-03-30 16:52:40 -04:00
|
|
|
"context"
|
2015-11-02 18:25:26 -05:00
|
|
|
"time"
|
|
|
|
|
2017-03-30 23:01:41 -04:00
|
|
|
containerpkg "github.com/docker/docker/container"
|
2017-07-19 10:20:13 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-09-16 14:56:26 -04:00
|
|
|
)
|
2015-03-25 03:44:12 -04:00
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
// ContainerStop looks for the given container and terminates it,
|
|
|
|
// waiting the given number of seconds before forcefully killing the
|
|
|
|
// container. If a negative number of seconds is given, ContainerStop
|
|
|
|
// will wait for a graceful termination. An error is returned if the
|
|
|
|
// container is not found, is already stopped, or if there is a
|
|
|
|
// problem stopping the container.
|
2016-06-06 23:29:05 -04:00
|
|
|
func (daemon *Daemon) ContainerStop(name string, seconds *int) 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-07-31 16:40:15 -04:00
|
|
|
}
|
2014-12-16 18:06:35 -05:00
|
|
|
if !container.IsRunning() {
|
2017-07-19 10:20:13 -04:00
|
|
|
return containerNotModifiedError{running: false}
|
2014-12-16 18:06:35 -05:00
|
|
|
}
|
2016-06-06 23:29:05 -04:00
|
|
|
if seconds == nil {
|
|
|
|
stopTimeout := container.StopTimeout()
|
|
|
|
seconds = &stopTimeout
|
|
|
|
}
|
|
|
|
if err := daemon.containerStop(container, *seconds); err != nil {
|
2017-07-19 10:20:13 -04:00
|
|
|
return errors.Wrapf(systemError{err}, "cannot stop container: %s", name)
|
2014-12-16 18:06:35 -05:00
|
|
|
}
|
2015-03-25 03:44:12 -04:00
|
|
|
return nil
|
2014-07-31 16:40:15 -04:00
|
|
|
}
|
2015-11-02 18:25:26 -05:00
|
|
|
|
|
|
|
// containerStop halts a container by sending a stop signal, waiting for the given
|
|
|
|
// duration in seconds, and then calling SIGKILL and waiting for the
|
|
|
|
// process to exit. If a negative duration is given, Stop will wait
|
|
|
|
// for the initial signal forever. If the container is not running Stop returns
|
|
|
|
// immediately.
|
2017-03-30 23:01:41 -04:00
|
|
|
func (daemon *Daemon) containerStop(container *containerpkg.Container, seconds int) error {
|
2015-11-02 18:25:26 -05:00
|
|
|
if !container.IsRunning() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-18 05:48:13 -04:00
|
|
|
daemon.stopHealthchecks(container)
|
|
|
|
|
2016-01-27 03:06:45 -05:00
|
|
|
stopSignal := container.StopSignal()
|
|
|
|
// 1. Send a stop signal
|
|
|
|
if err := daemon.killPossiblyDeadProcess(container, stopSignal); err != nil {
|
2016-08-15 19:51:45 -04:00
|
|
|
// While normally we might "return err" here we're not going to
|
|
|
|
// because if we can't stop the container by this point then
|
2016-12-23 07:48:25 -05:00
|
|
|
// it's probably because it's already stopped. Meaning, between
|
2016-08-15 19:51:45 -04:00
|
|
|
// the time of the IsRunning() call above and now it stopped.
|
|
|
|
// Also, since the err return will be environment 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 force kill it.
|
2017-03-30 16:52:40 -04:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2017-03-30 23:01:41 -04:00
|
|
|
if status := <-container.Wait(ctx, containerpkg.WaitConditionNotRunning); status.Err() != nil {
|
2016-08-15 19:51:45 -04:00
|
|
|
logrus.Infof("Container failed to stop after sending signal %d to the process, force killing", stopSignal)
|
|
|
|
if err := daemon.killPossiblyDeadProcess(container, 9); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-02 18:25:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Wait for the process to exit on its own
|
2017-03-30 16:52:40 -04:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(seconds)*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2017-03-30 23:01:41 -04:00
|
|
|
if status := <-container.Wait(ctx, containerpkg.WaitConditionNotRunning); status.Err() != nil {
|
2016-01-27 03:06:45 -05:00
|
|
|
logrus.Infof("Container %v failed to exit within %d seconds of signal %d - using the force", container.ID, seconds, stopSignal)
|
2015-11-02 18:25:26 -05:00
|
|
|
// 3. If it doesn't, then send SIGKILL
|
|
|
|
if err := daemon.Kill(container); err != nil {
|
2017-03-30 16:52:40 -04:00
|
|
|
// Wait without a timeout, ignore result.
|
2017-09-06 04:54:24 -04:00
|
|
|
<-container.Wait(context.Background(), containerpkg.WaitConditionNotRunning)
|
2015-11-02 18:25:26 -05:00
|
|
|
logrus.Warn(err) // Don't return error because we only care that container is stopped, not what function stopped it
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-03 12:33:13 -05:00
|
|
|
daemon.LogContainerEvent(container, "stop")
|
2015-11-02 18:25:26 -05:00
|
|
|
return nil
|
|
|
|
}
|