github.com/jwhonce/docker@v0.6.7-0.20190327063223-da823cf3a5a3/daemon/kill.go (about) 1 package daemon // import "github.com/docker/docker/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 libcontainerdtypes "github.com/docker/docker/libcontainerd/types" 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 container.CheckpointTo(daemon.containersReplica) 91 } 92 93 // if the container is currently restarting we do not need to send the signal 94 // to the process. Telling the monitor that it should exit on its next event 95 // loop is enough 96 if container.Restarting { 97 return nil 98 } 99 100 if err := daemon.kill(container, sig); err != nil { 101 if errdefs.IsNotFound(err) { 102 unpause = false 103 logrus.WithError(err).WithField("container", container.ID).WithField("action", "kill").Debug("container kill failed because of 'container not found' or 'no such process'") 104 } else { 105 return errors.Wrapf(err, "Cannot kill container %s", container.ID) 106 } 107 } 108 109 if unpause { 110 // above kill signal will be sent once resume is finished 111 if err := daemon.containerd.Resume(context.Background(), container.ID); err != nil { 112 logrus.Warnf("Cannot unpause container %s: %s", container.ID, err) 113 } 114 } 115 116 attributes := map[string]string{ 117 "signal": fmt.Sprintf("%d", sig), 118 } 119 daemon.LogContainerEventWithAttributes(container, "kill", attributes) 120 return nil 121 } 122 123 // Kill forcefully terminates a container. 124 func (daemon *Daemon) Kill(container *containerpkg.Container) error { 125 if !container.IsRunning() { 126 return errNotRunning(container.ID) 127 } 128 129 // 1. Send SIGKILL 130 if err := daemon.killPossiblyDeadProcess(container, int(syscall.SIGKILL)); err != nil { 131 // While normally we might "return err" here we're not going to 132 // because if we can't stop the container by this point then 133 // it's probably because it's already stopped. Meaning, between 134 // the time of the IsRunning() call above and now it stopped. 135 // Also, since the err return will be environment specific we can't 136 // look for any particular (common) error that would indicate 137 // that the process is already dead vs something else going wrong. 138 // So, instead we'll give it up to 2 more seconds to complete and if 139 // by that time the container is still running, then the error 140 // we got is probably valid and so we return it to the caller. 141 if isErrNoSuchProcess(err) { 142 return nil 143 } 144 145 ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) 146 defer cancel() 147 148 if status := <-container.Wait(ctx, containerpkg.WaitConditionNotRunning); status.Err() != nil { 149 return err 150 } 151 } 152 153 // 2. Wait for the process to die, in last resort, try to kill the process directly 154 if err := killProcessDirectly(container); err != nil { 155 if isErrNoSuchProcess(err) { 156 return nil 157 } 158 return err 159 } 160 161 // Wait for exit with no timeout. 162 // Ignore returned status. 163 <-container.Wait(context.Background(), containerpkg.WaitConditionNotRunning) 164 165 return nil 166 } 167 168 // killPossibleDeadProcess is a wrapper around killSig() suppressing "no such process" error. 169 func (daemon *Daemon) killPossiblyDeadProcess(container *containerpkg.Container, sig int) error { 170 err := daemon.killWithSignal(container, sig) 171 if errdefs.IsNotFound(err) { 172 e := errNoSuchProcess{container.GetPID(), sig} 173 logrus.Debug(e) 174 return e 175 } 176 return err 177 } 178 179 func (daemon *Daemon) kill(c *containerpkg.Container, sig int) error { 180 return daemon.containerd.SignalProcess(context.Background(), c.ID, libcontainerdtypes.InitProcessName, sig) 181 }