kill.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. if container.Config.StopSignal != "" {
  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. }
  67. } else {
  68. container.ExitOnNext()
  69. }
  70. if !daemon.IsShuttingDown() {
  71. container.HasBeenManuallyStopped = true
  72. }
  73. // if the container is currently restarting we do not need to send the signal
  74. // to the process. Telling the monitor that it should exit on its next event
  75. // loop is enough
  76. if container.Restarting {
  77. return nil
  78. }
  79. if err := daemon.kill(container, sig); err != nil {
  80. err = fmt.Errorf("Cannot kill container %s: %s", container.ID, err)
  81. // if container or process not exists, ignore the error
  82. if strings.Contains(err.Error(), "container not found") ||
  83. strings.Contains(err.Error(), "no such process") {
  84. logrus.Warnf("container kill failed because of 'container not found' or 'no such process': %s", err.Error())
  85. } else {
  86. return err
  87. }
  88. }
  89. attributes := map[string]string{
  90. "signal": fmt.Sprintf("%d", sig),
  91. }
  92. daemon.LogContainerEventWithAttributes(container, "kill", attributes)
  93. return nil
  94. }
  95. // Kill forcefully terminates a container.
  96. func (daemon *Daemon) Kill(container *container.Container) error {
  97. if !container.IsRunning() {
  98. return errNotRunning{container.ID}
  99. }
  100. // 1. Send SIGKILL
  101. if err := daemon.killPossiblyDeadProcess(container, int(syscall.SIGKILL)); err != nil {
  102. // While normally we might "return err" here we're not going to
  103. // because if we can't stop the container by this point then
  104. // its probably because its already stopped. Meaning, between
  105. // the time of the IsRunning() call above and now it stopped.
  106. // Also, since the err return will be environment specific we can't
  107. // look for any particular (common) error that would indicate
  108. // that the process is already dead vs something else going wrong.
  109. // So, instead we'll give it up to 2 more seconds to complete and if
  110. // by that time the container is still running, then the error
  111. // we got is probably valid and so we return it to the caller.
  112. if isErrNoSuchProcess(err) {
  113. return nil
  114. }
  115. if _, err2 := container.WaitStop(2 * time.Second); err2 != nil {
  116. return err
  117. }
  118. }
  119. // 2. Wait for the process to die, in last resort, try to kill the process directly
  120. if err := killProcessDirectly(container); err != nil {
  121. if isErrNoSuchProcess(err) {
  122. return nil
  123. }
  124. return err
  125. }
  126. container.WaitStop(-1 * time.Second)
  127. return nil
  128. }
  129. // killPossibleDeadProcess is a wrapper around killSig() suppressing "no such process" error.
  130. func (daemon *Daemon) killPossiblyDeadProcess(container *container.Container, sig int) error {
  131. err := daemon.killWithSignal(container, sig)
  132. if err == syscall.ESRCH {
  133. e := errNoSuchProcess{container.GetPID(), sig}
  134. logrus.Debug(e)
  135. return e
  136. }
  137. return err
  138. }
  139. func (daemon *Daemon) kill(c *container.Container, sig int) error {
  140. return daemon.containerd.Signal(c.ID, sig)
  141. }