kill.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package daemon // import "github.com/docker/docker/daemon"
  2. import (
  3. "context"
  4. "fmt"
  5. "runtime"
  6. "syscall"
  7. "time"
  8. containerpkg "github.com/docker/docker/container"
  9. "github.com/docker/docker/errdefs"
  10. libcontainerdtypes "github.com/docker/docker/libcontainerd/types"
  11. "github.com/moby/sys/signal"
  12. "github.com/pkg/errors"
  13. "github.com/sirupsen/logrus"
  14. )
  15. type errNoSuchProcess struct {
  16. pid int
  17. signal int
  18. }
  19. func (e errNoSuchProcess) Error() string {
  20. return fmt.Sprintf("Cannot kill process (pid=%d) with signal %d: no such process.", e.pid, e.signal)
  21. }
  22. func (errNoSuchProcess) NotFound() {}
  23. // isErrNoSuchProcess returns true if the error
  24. // is an instance of errNoSuchProcess.
  25. func isErrNoSuchProcess(err error) bool {
  26. _, ok := err.(errNoSuchProcess)
  27. return ok
  28. }
  29. // ContainerKill sends signal to the container
  30. // If no signal is given (sig 0), then Kill with SIGKILL and wait
  31. // for the container to exit.
  32. // If a signal is given, then just send it to the container and return.
  33. func (daemon *Daemon) ContainerKill(name string, sig uint64) error {
  34. container, err := daemon.GetContainer(name)
  35. if err != nil {
  36. return err
  37. }
  38. if sig != 0 && !signal.ValidSignalForPlatform(syscall.Signal(sig)) {
  39. return fmt.Errorf("The %s daemon does not support signal %d", runtime.GOOS, sig)
  40. }
  41. // If no signal is passed, or SIGKILL, perform regular Kill (SIGKILL + wait())
  42. if sig == 0 || syscall.Signal(sig) == syscall.SIGKILL {
  43. return daemon.Kill(container)
  44. }
  45. return daemon.killWithSignal(container, int(sig))
  46. }
  47. // killWithSignal sends the container the given signal. This wrapper for the
  48. // host specific kill command prepares the container before attempting
  49. // to send the signal. An error is returned if the container is paused
  50. // or not running, or if there is a problem returned from the
  51. // underlying kill command.
  52. func (daemon *Daemon) killWithSignal(container *containerpkg.Container, sig int) error {
  53. logrus.Debugf("Sending kill signal %d to container %s", sig, container.ID)
  54. container.Lock()
  55. defer container.Unlock()
  56. if !container.Running {
  57. return errNotRunning(container.ID)
  58. }
  59. var unpause bool
  60. if container.Config.StopSignal != "" && syscall.Signal(sig) != syscall.SIGKILL {
  61. containerStopSignal, err := signal.ParseSignal(container.Config.StopSignal)
  62. if err != nil {
  63. return err
  64. }
  65. if containerStopSignal == syscall.Signal(sig) {
  66. container.ExitOnNext()
  67. unpause = container.Paused
  68. }
  69. } else {
  70. container.ExitOnNext()
  71. unpause = container.Paused
  72. }
  73. if !daemon.IsShuttingDown() {
  74. container.HasBeenManuallyStopped = true
  75. container.CheckpointTo(daemon.containersReplica)
  76. }
  77. // if the container is currently restarting we do not need to send the signal
  78. // to the process. Telling the monitor that it should exit on its next event
  79. // loop is enough
  80. if container.Restarting {
  81. return nil
  82. }
  83. if err := daemon.kill(container, sig); err != nil {
  84. if errdefs.IsNotFound(err) {
  85. unpause = false
  86. logrus.WithError(err).WithField("container", container.ID).WithField("action", "kill").Debug("container kill failed because of 'container not found' or 'no such process'")
  87. go func() {
  88. // We need to clean up this container but it is possible there is a case where we hit here before the exit event is processed
  89. // but after it was fired off.
  90. // So let's wait the container's stop timeout amount of time to see if the event is eventually processed.
  91. // Doing this has the side effect that if no event was ever going to come we are waiting a a longer period of time uneccessarily.
  92. // But this prevents race conditions in processing the container.
  93. ctx, cancel := context.WithTimeout(context.TODO(), time.Duration(container.StopTimeout())*time.Second)
  94. defer cancel()
  95. s := <-container.Wait(ctx, containerpkg.WaitConditionNotRunning)
  96. if s.Err() != nil {
  97. daemon.handleContainerExit(container, nil)
  98. }
  99. }()
  100. } else {
  101. return errors.Wrapf(err, "Cannot kill container %s", container.ID)
  102. }
  103. }
  104. if unpause {
  105. // above kill signal will be sent once resume is finished
  106. if err := daemon.containerd.Resume(context.Background(), container.ID); err != nil {
  107. logrus.Warnf("Cannot unpause container %s: %s", container.ID, err)
  108. }
  109. }
  110. attributes := map[string]string{
  111. "signal": fmt.Sprintf("%d", sig),
  112. }
  113. daemon.LogContainerEventWithAttributes(container, "kill", attributes)
  114. return nil
  115. }
  116. // Kill forcefully terminates a container.
  117. func (daemon *Daemon) Kill(container *containerpkg.Container) error {
  118. if !container.IsRunning() {
  119. return errNotRunning(container.ID)
  120. }
  121. // 1. Send SIGKILL
  122. if err := daemon.killPossiblyDeadProcess(container, int(syscall.SIGKILL)); err != nil {
  123. // kill failed, check if process is no longer running.
  124. if isErrNoSuchProcess(err) {
  125. return nil
  126. }
  127. }
  128. ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
  129. defer cancel()
  130. status := <-container.Wait(ctx, containerpkg.WaitConditionNotRunning)
  131. if status.Err() == nil {
  132. return nil
  133. }
  134. logrus.WithError(status.Err()).WithField("container", container.ID).Error("Container failed to exit within 10 seconds of kill - trying direct SIGKILL")
  135. if err := killProcessDirectly(container); err != nil {
  136. if isErrNoSuchProcess(err) {
  137. return nil
  138. }
  139. return err
  140. }
  141. // wait for container to exit one last time, if it doesn't then kill didnt work, so return error
  142. ctx2, cancel2 := context.WithTimeout(context.Background(), 2*time.Second)
  143. defer cancel2()
  144. if status := <-container.Wait(ctx2, containerpkg.WaitConditionNotRunning); status.Err() != nil {
  145. return errors.New("tried to kill container, but did not receive an exit event")
  146. }
  147. return nil
  148. }
  149. // killPossibleDeadProcess is a wrapper around killSig() suppressing "no such process" error.
  150. func (daemon *Daemon) killPossiblyDeadProcess(container *containerpkg.Container, sig int) error {
  151. err := daemon.killWithSignal(container, sig)
  152. if errdefs.IsNotFound(err) {
  153. e := errNoSuchProcess{container.GetPID(), sig}
  154. logrus.Debug(e)
  155. return e
  156. }
  157. return err
  158. }
  159. func (daemon *Daemon) kill(c *containerpkg.Container, sig int) error {
  160. return daemon.containerd.SignalProcess(context.Background(), c.ID, libcontainerdtypes.InitProcessName, sig)
  161. }