kill.go 5.3 KB

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