kill.go 4.8 KB

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