2018-02-05 16:05:59 -05:00
|
|
|
package daemon // import "github.com/docker/docker/daemon"
|
2014-07-31 16:40:15 -04:00
|
|
|
|
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"
|
|
|
|
|
2021-08-20 18:23:26 -04:00
|
|
|
containertypes "github.com/docker/docker/api/types/container"
|
2021-08-21 18:46:07 -04:00
|
|
|
"github.com/docker/docker/container"
|
2018-01-11 14:53:06 -05:00
|
|
|
"github.com/docker/docker/errdefs"
|
2021-08-22 06:46:43 -04:00
|
|
|
"github.com/moby/sys/signal"
|
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
|
|
|
|
2018-04-16 19:58:42 -04:00
|
|
|
// ContainerStop looks for the given container and stops it.
|
|
|
|
// In case the container fails to stop gracefully within a time duration
|
|
|
|
// specified by the timeout argument, in seconds, it is forcefully
|
|
|
|
// terminated (killed).
|
|
|
|
//
|
|
|
|
// If the timeout is nil, the container's StopTimeout value is used, if set,
|
|
|
|
// otherwise the engine default. A negative timeout value can be specified,
|
|
|
|
// meaning no timeout, i.e. no forceful termination is performed.
|
2021-08-20 18:23:26 -04:00
|
|
|
func (daemon *Daemon) ContainerStop(ctx context.Context, name string, options containertypes.StopOptions) error {
|
2021-08-21 18:46:07 -04:00
|
|
|
ctr, 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
|
|
|
}
|
2021-08-21 18:46:07 -04:00
|
|
|
if !ctr.IsRunning() {
|
2021-08-20 16:43:13 -04:00
|
|
|
return containerNotModifiedError{}
|
2014-12-16 18:06:35 -05:00
|
|
|
}
|
2021-08-20 18:23:26 -04:00
|
|
|
err = daemon.containerStop(ctx, ctr, options)
|
2021-08-20 16:43:13 -04:00
|
|
|
if err != nil {
|
2017-11-28 23:09:37 -05:00
|
|
|
return errdefs.System(errors.Wrapf(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
|
|
|
|
2018-04-16 19:58:42 -04:00
|
|
|
// containerStop sends a stop signal, waits, sends a kill signal.
|
2021-08-20 18:23:26 -04:00
|
|
|
func (daemon *Daemon) containerStop(ctx context.Context, ctr *container.Container, options containertypes.StopOptions) (retErr error) {
|
2021-08-21 18:46:07 -04:00
|
|
|
if !ctr.IsRunning() {
|
2015-11-02 18:25:26 -05:00
|
|
|
return nil
|
|
|
|
}
|
2021-08-20 16:43:13 -04:00
|
|
|
|
|
|
|
var (
|
2022-05-01 19:16:56 -04:00
|
|
|
stopSignal = ctr.StopSignal()
|
2021-08-20 16:43:13 -04:00
|
|
|
stopTimeout = ctr.StopTimeout()
|
|
|
|
)
|
2021-08-22 06:46:43 -04:00
|
|
|
if options.Signal != "" {
|
|
|
|
sig, err := signal.ParseSignal(options.Signal)
|
|
|
|
if err != nil {
|
|
|
|
return errdefs.InvalidParameter(err)
|
|
|
|
}
|
2022-05-01 18:28:17 -04:00
|
|
|
stopSignal = sig
|
2021-08-22 06:46:43 -04:00
|
|
|
}
|
2021-08-20 18:23:26 -04:00
|
|
|
if options.Timeout != nil {
|
|
|
|
stopTimeout = *options.Timeout
|
2021-08-20 16:43:13 -04:00
|
|
|
}
|
|
|
|
|
2020-10-24 16:15:58 -04:00
|
|
|
var wait time.Duration
|
2021-08-20 16:43:13 -04:00
|
|
|
if stopTimeout >= 0 {
|
|
|
|
wait = time.Duration(stopTimeout) * time.Second
|
2020-10-24 16:15:58 -04:00
|
|
|
}
|
2021-08-20 16:57:09 -04:00
|
|
|
defer func() {
|
|
|
|
if retErr == nil {
|
|
|
|
daemon.LogContainerEvent(ctr, "stop")
|
|
|
|
}
|
|
|
|
}()
|
2017-03-30 16:52:40 -04:00
|
|
|
|
2020-10-24 16:15:58 -04:00
|
|
|
// 1. Send a stop signal
|
2021-08-21 18:46:07 -04:00
|
|
|
err := daemon.killPossiblyDeadProcess(ctr, stopSignal)
|
2020-10-24 16:15:58 -04:00
|
|
|
if err != nil {
|
|
|
|
wait = 2 * time.Second
|
2015-11-02 18:25:26 -05:00
|
|
|
}
|
|
|
|
|
2020-10-24 16:15:58 -04:00
|
|
|
var subCtx context.Context
|
|
|
|
var cancel context.CancelFunc
|
2021-08-20 16:43:13 -04:00
|
|
|
if stopTimeout >= 0 {
|
2020-10-24 16:15:58 -04:00
|
|
|
subCtx, cancel = context.WithTimeout(ctx, wait)
|
|
|
|
} else {
|
|
|
|
subCtx, cancel = context.WithCancel(ctx)
|
2018-04-16 19:58:42 -04:00
|
|
|
}
|
2020-10-24 16:15:58 -04:00
|
|
|
defer cancel()
|
2017-03-30 16:52:40 -04:00
|
|
|
|
2021-08-21 18:46:07 -04:00
|
|
|
if status := <-ctr.Wait(subCtx, container.WaitConditionNotRunning); status.Err() == nil {
|
2020-10-24 16:15:58 -04:00
|
|
|
// container did exit, so ignore any previous errors and return
|
2021-08-20 16:57:09 -04:00
|
|
|
return nil
|
2020-10-24 16:15:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// the container has still not exited, and the kill function errored, so log the error here:
|
2021-08-21 18:46:07 -04:00
|
|
|
logrus.WithError(err).WithField("container", ctr.ID).Errorf("Error sending stop (signal %d) to container", stopSignal)
|
2020-10-24 16:15:58 -04:00
|
|
|
}
|
2021-08-20 16:43:13 -04:00
|
|
|
if stopTimeout < 0 {
|
2020-10-24 16:15:58 -04:00
|
|
|
// if the client requested that we never kill / wait forever, but container.Wait was still
|
|
|
|
// interrupted (parent context cancelled, for example), we should propagate the signal failure
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-21 18:46:07 -04:00
|
|
|
logrus.WithField("container", ctr.ID).Infof("Container failed to exit within %s of signal %d - using the force", wait, stopSignal)
|
2021-08-20 16:57:09 -04:00
|
|
|
|
|
|
|
// Stop either failed or container didn't exit, so fallback to kill.
|
2021-08-21 18:46:07 -04:00
|
|
|
if err := daemon.Kill(ctr); err != nil {
|
2020-10-24 16:15:58 -04:00
|
|
|
// got a kill error, but give container 2 more seconds to exit just in case
|
|
|
|
subCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
|
|
|
defer cancel()
|
2021-08-20 16:57:09 -04:00
|
|
|
status := <-ctr.Wait(subCtx, container.WaitConditionNotRunning)
|
|
|
|
if status.Err() != nil {
|
|
|
|
logrus.WithError(err).WithField("container", ctr.ID).Errorf("error killing container: %v", status.Err())
|
|
|
|
return err
|
2015-11-02 18:25:26 -05:00
|
|
|
}
|
2021-08-20 16:57:09 -04:00
|
|
|
// container did exit, so ignore previous errors and continue
|
2015-11-02 18:25:26 -05:00
|
|
|
}
|
|
|
|
|
2021-08-20 16:57:09 -04:00
|
|
|
return nil
|
2015-11-02 18:25:26 -05:00
|
|
|
}
|