github.com/kobeld/docker@v1.12.0-rc1/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 %d to %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  	container.ExitOnNext()
    72  
    73  	if !daemon.IsShuttingDown() {
    74  		container.HasBeenManuallyStopped = true
    75  	}
    76  
    77  	// if the container is currently restarting we do not need to send the signal
    78  	// to the process.  Telling the monitor that it should exit on it's next event
    79  	// loop is enough
    80  	if container.Restarting {
    81  		return nil
    82  	}
    83  
    84  	if err := daemon.kill(container, sig); err != nil {
    85  		err = fmt.Errorf("Cannot kill container %s: %s", container.ID, err)
    86  		// if container or process not exists, ignore the error
    87  		if strings.Contains(err.Error(), "container not found") ||
    88  			strings.Contains(err.Error(), "no such process") {
    89  			logrus.Warnf("container kill failed because of 'container not found' or 'no such process': %s", err.Error())
    90  		} else {
    91  			return err
    92  		}
    93  	}
    94  
    95  	attributes := map[string]string{
    96  		"signal": fmt.Sprintf("%d", sig),
    97  	}
    98  	daemon.LogContainerEventWithAttributes(container, "kill", attributes)
    99  	return nil
   100  }
   101  
   102  // Kill forcefully terminates a container.
   103  func (daemon *Daemon) Kill(container *container.Container) error {
   104  	if !container.IsRunning() {
   105  		return errNotRunning{container.ID}
   106  	}
   107  
   108  	// 1. Send SIGKILL
   109  	if err := daemon.killPossiblyDeadProcess(container, int(syscall.SIGKILL)); err != nil {
   110  		// While normally we might "return err" here we're not going to
   111  		// because if we can't stop the container by this point then
   112  		// its probably because its already stopped. Meaning, between
   113  		// the time of the IsRunning() call above and now it stopped.
   114  		// Also, since the err return will be environment specific we can't
   115  		// look for any particular (common) error that would indicate
   116  		// that the process is already dead vs something else going wrong.
   117  		// So, instead we'll give it up to 2 more seconds to complete and if
   118  		// by that time the container is still running, then the error
   119  		// we got is probably valid and so we return it to the caller.
   120  		if isErrNoSuchProcess(err) {
   121  			return nil
   122  		}
   123  
   124  		if container.IsRunning() {
   125  			container.WaitStop(2 * time.Second)
   126  			if container.IsRunning() {
   127  				return err
   128  			}
   129  		}
   130  	}
   131  
   132  	// 2. Wait for the process to die, in last resort, try to kill the process directly
   133  	if err := killProcessDirectly(container); err != nil {
   134  		if isErrNoSuchProcess(err) {
   135  			return nil
   136  		}
   137  		return err
   138  	}
   139  
   140  	container.WaitStop(-1 * time.Second)
   141  	return nil
   142  }
   143  
   144  // killPossibleDeadProcess is a wrapper around killSig() suppressing "no such process" error.
   145  func (daemon *Daemon) killPossiblyDeadProcess(container *container.Container, sig int) error {
   146  	err := daemon.killWithSignal(container, sig)
   147  	if err == syscall.ESRCH {
   148  		e := errNoSuchProcess{container.GetPID(), sig}
   149  		logrus.Debug(e)
   150  		return e
   151  	}
   152  	return err
   153  }
   154  
   155  func (daemon *Daemon) kill(c *container.Container, sig int) error {
   156  	return daemon.containerd.Signal(c.ID, sig)
   157  }