kill.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package 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/docker/docker/libcontainerd"
  11. "github.com/docker/docker/pkg/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. daemon.stopHealthchecks(container)
  57. if !container.Running {
  58. return errNotRunning(container.ID)
  59. }
  60. var unpause bool
  61. if container.Config.StopSignal != "" && syscall.Signal(sig) != syscall.SIGKILL {
  62. containerStopSignal, err := signal.ParseSignal(container.Config.StopSignal)
  63. if err != nil {
  64. return err
  65. }
  66. if containerStopSignal == syscall.Signal(sig) {
  67. container.ExitOnNext()
  68. unpause = container.Paused
  69. }
  70. } else {
  71. container.ExitOnNext()
  72. unpause = container.Paused
  73. }
  74. if !daemon.IsShuttingDown() {
  75. container.HasBeenManuallyStopped = true
  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. } else {
  88. return errors.Wrapf(err, "Cannot kill container %s", container.ID)
  89. }
  90. }
  91. if unpause {
  92. // above kill signal will be sent once resume is finished
  93. if err := daemon.containerd.Resume(context.Background(), container.ID); err != nil {
  94. logrus.Warn("Cannot unpause container %s: %s", container.ID, err)
  95. }
  96. }
  97. attributes := map[string]string{
  98. "signal": fmt.Sprintf("%d", sig),
  99. }
  100. daemon.LogContainerEventWithAttributes(container, "kill", attributes)
  101. return nil
  102. }
  103. // Kill forcefully terminates a container.
  104. func (daemon *Daemon) Kill(container *containerpkg.Container) error {
  105. if !container.IsRunning() {
  106. return errNotRunning(container.ID)
  107. }
  108. // 1. Send SIGKILL
  109. if err := daemon.killPossiblyDeadProcess(container, int(syscall.SIGKILL)); err != nil {
  110. // While normally we might "return err" here we're not going to
  111. // because if we can't stop the container by this point then
  112. // it's probably because it's already stopped. Meaning, between
  113. // the time of the IsRunning() call above and now it stopped.
  114. // Also, since the err return will be environment specific we can't
  115. // look for any particular (common) error that would indicate
  116. // that the process is already dead vs something else going wrong.
  117. // So, instead we'll give it up to 2 more seconds to complete and if
  118. // by that time the container is still running, then the error
  119. // we got is probably valid and so we return it to the caller.
  120. if isErrNoSuchProcess(err) {
  121. return nil
  122. }
  123. ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
  124. defer cancel()
  125. if status := <-container.Wait(ctx, containerpkg.WaitConditionNotRunning); status.Err() != nil {
  126. return err
  127. }
  128. }
  129. // 2. Wait for the process to die, in last resort, try to kill the process directly
  130. if err := killProcessDirectly(container); err != nil {
  131. if isErrNoSuchProcess(err) {
  132. return nil
  133. }
  134. return err
  135. }
  136. // Wait for exit with no timeout.
  137. // Ignore returned status.
  138. <-container.Wait(context.Background(), containerpkg.WaitConditionNotRunning)
  139. return nil
  140. }
  141. // killPossibleDeadProcess is a wrapper around killSig() suppressing "no such process" error.
  142. func (daemon *Daemon) killPossiblyDeadProcess(container *containerpkg.Container, sig int) error {
  143. err := daemon.killWithSignal(container, sig)
  144. if errdefs.IsNotFound(err) {
  145. e := errNoSuchProcess{container.GetPID(), sig}
  146. logrus.Debug(e)
  147. return e
  148. }
  149. return err
  150. }
  151. func (daemon *Daemon) kill(c *containerpkg.Container, sig int) error {
  152. return daemon.containerd.SignalProcess(context.Background(), c.ID, libcontainerd.InitProcessName, sig)
  153. }