kill.go 5.8 KB

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