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