kill.go 6.4 KB

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