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