kill.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package daemon
  2. import (
  3. "context"
  4. "fmt"
  5. "runtime"
  6. "strings"
  7. "syscall"
  8. "time"
  9. containerpkg "github.com/docker/docker/container"
  10. "github.com/docker/docker/pkg/signal"
  11. "github.com/pkg/errors"
  12. "github.com/sirupsen/logrus"
  13. )
  14. type errNoSuchProcess struct {
  15. pid int
  16. signal int
  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. // isErrNoSuchProcess returns true if the error
  23. // is an instance of errNoSuchProcess.
  24. func isErrNoSuchProcess(err error) bool {
  25. _, ok := err.(errNoSuchProcess)
  26. return ok
  27. }
  28. // ContainerKill sends signal to the container
  29. // If no signal is given (sig 0), then Kill with SIGKILL and wait
  30. // for the container to exit.
  31. // If a signal is given, then just send it to the container and return.
  32. func (daemon *Daemon) ContainerKill(name string, sig uint64) error {
  33. container, err := daemon.GetContainer(name)
  34. if err != nil {
  35. return err
  36. }
  37. if sig != 0 && !signal.ValidSignalForPlatform(syscall.Signal(sig)) {
  38. return fmt.Errorf("The %s daemon does not support signal %d", runtime.GOOS, sig)
  39. }
  40. // If no signal is passed, or SIGKILL, perform regular Kill (SIGKILL + wait())
  41. if sig == 0 || syscall.Signal(sig) == syscall.SIGKILL {
  42. return daemon.Kill(container)
  43. }
  44. return daemon.killWithSignal(container, int(sig))
  45. }
  46. // killWithSignal sends the container the given signal. This wrapper for the
  47. // host specific kill command prepares the container before attempting
  48. // to send the signal. An error is returned if the container is paused
  49. // or not running, or if there is a problem returned from the
  50. // underlying kill command.
  51. func (daemon *Daemon) killWithSignal(container *containerpkg.Container, sig int) error {
  52. logrus.Debugf("Sending kill signal %d to container %s", sig, container.ID)
  53. container.Lock()
  54. defer container.Unlock()
  55. if !container.Running {
  56. return errNotRunning(container.ID)
  57. }
  58. var unpause bool
  59. if container.Config.StopSignal != "" && syscall.Signal(sig) != syscall.SIGKILL {
  60. containerStopSignal, err := signal.ParseSignal(container.Config.StopSignal)
  61. if err != nil {
  62. return err
  63. }
  64. if containerStopSignal == syscall.Signal(sig) {
  65. container.ExitOnNext()
  66. unpause = container.Paused
  67. }
  68. } else {
  69. container.ExitOnNext()
  70. unpause = container.Paused
  71. }
  72. if !daemon.IsShuttingDown() {
  73. container.HasBeenManuallyStopped = true
  74. }
  75. // if the container is currently restarting we do not need to send the signal
  76. // to the process. Telling the monitor that it should exit on its next event
  77. // loop is enough
  78. if container.Restarting {
  79. return nil
  80. }
  81. if err := daemon.kill(container, sig); err != nil {
  82. err = errors.Wrapf(err, "Cannot kill container %s", container.ID)
  83. // if container or process not exists, ignore the error
  84. // TODO: we shouldn't have to parse error strings from containerd
  85. if strings.Contains(err.Error(), "container not found") ||
  86. strings.Contains(err.Error(), "no such process") {
  87. logrus.Warnf("container kill failed because of 'container not found' or 'no such process': %s", err.Error())
  88. unpause = false
  89. } else {
  90. return err
  91. }
  92. }
  93. if unpause {
  94. // above kill signal will be sent once resume is finished
  95. if err := daemon.containerd.Resume(container.ID); err != nil {
  96. logrus.Warn("Cannot unpause container %s: %s", container.ID, err)
  97. }
  98. }
  99. attributes := map[string]string{
  100. "signal": fmt.Sprintf("%d", sig),
  101. }
  102. daemon.LogContainerEventWithAttributes(container, "kill", attributes)
  103. return nil
  104. }
  105. // Kill forcefully terminates a container.
  106. func (daemon *Daemon) Kill(container *containerpkg.Container) error {
  107. if !container.IsRunning() {
  108. return errNotRunning(container.ID)
  109. }
  110. // 1. Send SIGKILL
  111. if err := daemon.killPossiblyDeadProcess(container, int(syscall.SIGKILL)); err != nil {
  112. // While normally we might "return err" here we're not going to
  113. // because if we can't stop the container by this point then
  114. // it's probably because it's already stopped. Meaning, between
  115. // the time of the IsRunning() call above and now it stopped.
  116. // Also, since the err return will be environment specific we can't
  117. // look for any particular (common) error that would indicate
  118. // that the process is already dead vs something else going wrong.
  119. // So, instead we'll give it up to 2 more seconds to complete and if
  120. // by that time the container is still running, then the error
  121. // we got is probably valid and so we return it to the caller.
  122. if isErrNoSuchProcess(err) {
  123. return nil
  124. }
  125. ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
  126. defer cancel()
  127. if status := <-container.Wait(ctx, containerpkg.WaitConditionNotRunning); status.Err() != nil {
  128. return err
  129. }
  130. }
  131. // 2. Wait for the process to die, in last resort, try to kill the process directly
  132. if err := killProcessDirectly(container); err != nil {
  133. if isErrNoSuchProcess(err) {
  134. return nil
  135. }
  136. return err
  137. }
  138. // Wait for exit with no timeout.
  139. // Ignore returned status.
  140. <-container.Wait(context.Background(), containerpkg.WaitConditionNotRunning)
  141. return nil
  142. }
  143. // killPossibleDeadProcess is a wrapper around killSig() suppressing "no such process" error.
  144. func (daemon *Daemon) killPossiblyDeadProcess(container *containerpkg.Container, sig int) error {
  145. err := daemon.killWithSignal(container, sig)
  146. if err == syscall.ESRCH {
  147. e := errNoSuchProcess{container.GetPID(), sig}
  148. logrus.Debug(e)
  149. return e
  150. }
  151. return err
  152. }
  153. func (daemon *Daemon) kill(c *containerpkg.Container, sig int) error {
  154. return daemon.containerd.Signal(c.ID, sig)
  155. }