github.com/hms58/moby@v1.13.1/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  		err = fmt.Errorf("Cannot kill container %s: %s", container.ID, err)
    96  		// if container or process not exists, ignore the error
    97  		if strings.Contains(err.Error(), "container not found") ||
    98  			strings.Contains(err.Error(), "no such process") {
    99  			logrus.Warnf("container kill failed because of 'container not found' or 'no such process': %s", err.Error())
   100  		} else {
   101  			return err
   102  		}
   103  	}
   104  
   105  	attributes := map[string]string{
   106  		"signal": fmt.Sprintf("%d", sig),
   107  	}
   108  	daemon.LogContainerEventWithAttributes(container, "kill", attributes)
   109  	return nil
   110  }
   111  
   112  // Kill forcefully terminates a container.
   113  func (daemon *Daemon) Kill(container *container.Container) error {
   114  	if !container.IsRunning() {
   115  		return errNotRunning{container.ID}
   116  	}
   117  
   118  	// 1. Send SIGKILL
   119  	if err := daemon.killPossiblyDeadProcess(container, int(syscall.SIGKILL)); err != nil {
   120  		// While normally we might "return err" here we're not going to
   121  		// because if we can't stop the container by this point then
   122  		// its probably because its already stopped. Meaning, between
   123  		// the time of the IsRunning() call above and now it stopped.
   124  		// Also, since the err return will be environment specific we can't
   125  		// look for any particular (common) error that would indicate
   126  		// that the process is already dead vs something else going wrong.
   127  		// So, instead we'll give it up to 2 more seconds to complete and if
   128  		// by that time the container is still running, then the error
   129  		// we got is probably valid and so we return it to the caller.
   130  		if isErrNoSuchProcess(err) {
   131  			return nil
   132  		}
   133  
   134  		if _, err2 := container.WaitStop(2 * time.Second); err2 != nil {
   135  			return err
   136  		}
   137  	}
   138  
   139  	// 2. Wait for the process to die, in last resort, try to kill the process directly
   140  	if err := killProcessDirectly(container); err != nil {
   141  		if isErrNoSuchProcess(err) {
   142  			return nil
   143  		}
   144  		return err
   145  	}
   146  
   147  	container.WaitStop(-1 * time.Second)
   148  	return nil
   149  }
   150  
   151  // killPossibleDeadProcess is a wrapper around killSig() suppressing "no such process" error.
   152  func (daemon *Daemon) killPossiblyDeadProcess(container *container.Container, sig int) error {
   153  	err := daemon.killWithSignal(container, sig)
   154  	if err == syscall.ESRCH {
   155  		e := errNoSuchProcess{container.GetPID(), sig}
   156  		logrus.Debug(e)
   157  		return e
   158  	}
   159  	return err
   160  }
   161  
   162  func (daemon *Daemon) kill(c *container.Container, sig int) error {
   163  	return daemon.containerd.Signal(c.ID, sig)
   164  }