// killPossibleDeadProcess is a wrapper around killSig() suppressing "no such process" error.
427583
func (daemon *Daemon) killPossiblyDeadProcess(container *containerpkg.Container, sig int) error {
	err := daemon.killWithSignal(container, sig)
	if errdefs.IsNotFound(err) {
		e := errNoSuchProcess{container.GetPID(), sig}
		logrus.Debug(e)
		return e
	}
	return err
}
439123
func (c *ContainerServer) ContainerKill(container string, killSignal syscall.Signal) (string, error) {
	ctr, err := c.LookupContainer(container)
	if err != nil {
		return "", errors.Wrapf(err, "failed to find container %s", container)
	}
	c.runtime.UpdateContainerStatus(ctr)
	cStatus := ctr.State()

	// If the container is not running, error and move on.
	if cStatus.Status != oci.ContainerStateRunning {
		return "", errors.Errorf("cannot kill container %s: it is not running", container)
	}

	if err = c.runtime.SignalContainer(ctr, killSignal); err != nil {
		return "", err
	}

	c.ContainerStateToDisk(ctr)
	return ctr.ID(), nil
}
478135
func (l *HostStateListener) PostProcess(p map[string]struct{}) {
	// We are running all of the containers we are supposed to, now
	// shut down any containers we are not supposed to be running
	l.mu.Lock()
	defer l.mu.Unlock()
	stateIDs := l.getExistingThreadStateIDs()
	var orphanedStates []string
	for _, s := range stateIDs {
		if _, ok := p[s]; !ok {
			orphanedStates = append(orphanedStates, s)
			plog.WithField("stateid", s).Info("Detected orphaned container")
		}
	}

	if len(orphanedStates) > 0 {
		l.cleanUpContainers(orphanedStates, false)
		plog.WithField("count", len(orphanedStates)).Info("Cleaned up all orphaned containers")
	}
}
637426
func (a *ArchiveOptions) PostProcess(cwd string) {
	// Set default blacklist only if none is set.
	if len(a.Blacklist) == 0 {
		// This cannot be generalized as ".*" as there is known use that require
		// a ".pki" directory to be mapped.
		a.Blacklist = stringlistflag.Flag{
			// Temporary python files.
			"*.pyc",
			// Temporary vim files.
			"*.swp",
			".git",
			".hg",
			".svn",
		}
	}
	if !filepath.IsAbs(a.Isolate) {
		a.Isolate = filepath.Join(cwd, a.Isolate)
	}
	a.Isolate = filepath.Clean(a.Isolate)

	if !filepath.IsAbs(a.Isolated) {
		a.Isolated = filepath.Join(cwd, a.Isolated)
	}
	a.Isolated = filepath.Clean(a.Isolated)

	for k, v := range a.PathVariables {
		// This is due to a Windows + GYP specific issue, where double-quoted paths
		// would get mangled in a way that cannot be resolved unless a space is
		// injected.
		a.PathVariables[k] = strings.TrimSpace(v)
	}
}
23802
func (m *ProcessManager) Kill(pid int64) bool {
	m.mu.Lock()
	entry, ok := m.entries[pid]
	m.mu.Unlock()

	if ok {
		entry.Kill()
		return true
	}

	return false
}
763126
func (s *ServerRPC) ExecSignal(ctx context.Context, req *types.ExecSignalRequest) (*types.ExecSignalResponse, error) {
	err := s.daemon.KillExec(req.ContainerID, req.ExecID, req.Signal)
	if err != nil {
		return nil, err
	}

	return &types.ExecSignalResponse{}, nil
}
1171144
func (p *setnsProcess) terminate() error {
	if p.cmd.Process == nil {
		return nil
	}
	err := p.cmd.Process.Kill()
	if _, werr := p.wait(); err == nil {
		err = werr
	}
	return err
}
994729
func KillGracefully(c KubernetesClientInterface, containerID string) error {
	log.Infof("SIGTERM containerID %q: %s", containerID, syscall.SIGTERM.String())
	err := TerminatePodWithContainerID(c, containerID, syscall.SIGTERM)
	if err != nil {
		return err
	}
	err = WaitForTermination(c, containerID, time.Second*KillGracePeriod)
	if err == nil {
		log.Infof("ContainerID %q successfully killed", containerID)
		return nil
	}
	log.Infof("SIGKILL containerID %q: %s", containerID, syscall.SIGKILL.String())
	err = TerminatePodWithContainerID(c, containerID, syscall.SIGKILL)
	if err != nil {
		return err
	}
	err = WaitForTermination(c, containerID, time.Second*KillGracePeriod)
	if err != nil {
		return err
	}
	log.Infof("ContainerID %q successfully killed", containerID)
	return nil
}
1315216
func (p *Process) ClearBreakpoint(addr uint64) (*proc.Breakpoint, error) {
	if p.exited {
		return nil, &proc.ErrProcessExited{Pid: p.conn.pid}
	}
	return p.breakpoints.Clear(addr, func(bp *proc.Breakpoint) error {
		return p.conn.clearBreakpoint(bp.Addr)
	})
}
556309
func (p *RemoteMonitor) KillRemoteEnforcer(contextID string, force bool) error {

	p.Lock()
	s, err := p.activeProcesses.Get(contextID)
	if err != nil {
		p.Unlock()
		return fmt.Errorf("unable to find process for context: %s", contextID)
	}

	p.activeProcesses.Remove(contextID) // nolint errcheck
	p.Unlock()

	procInfo, ok := s.(*processInfo)
	if !ok {
		return fmt.Errorf("internal error - invalid type for process")
	}

	procInfo.Lock()
	defer procInfo.Unlock()

	if procInfo.process == nil {
		return fmt.Errorf("cannot find process for context: %s", contextID)
	}

	req := &rpcwrapper.Request{
		Payload: procInfo.process.Pid,
	}
	resp := &rpcwrapper.Response{}

	c := make(chan error, 1)
	go func() {
		c <- p.rpc.RemoteCall(contextID, remoteenforcer.EnforcerExit, req, resp)
	}()

	select {
	case err := <-c:
		if err != nil && force {
			zap.L().Error("Failed to stop gracefully - forcing kill",
				zap.Error(err))
			procInfo.process.Kill() // nolint
		}
	case <-time.After(5 * time.Second):
		if force {
			zap.L().Error("Time out on terminating remote enforcer - forcing kill")
			procInfo.process.Kill() // nolint
		}
	}

	p.rpc.DestroyRPCClient(contextID)

	return nil
}
145265
func (daemon *Daemon) ContainerKill(name string, sig uint64) error {
	container, err := daemon.GetContainer(name)
	if err != nil {
		return err
	}

	if sig != 0 && !signal.ValidSignalForPlatform(syscall.Signal(sig)) {
		return fmt.Errorf("The %s daemon does not support signal %d", runtime.GOOS, sig)
	}

	// If no signal is passed, or SIGKILL, perform regular Kill (SIGKILL + wait())
	if sig == 0 || syscall.Signal(sig) == syscall.SIGKILL {
		return daemon.Kill(container)
	}
	return daemon.killWithSignal(container, int(sig))
}
175594
func IsProcessAlive(pid int) bool {
	err := unix.Kill(pid, syscall.Signal(0))
	if err == nil || err == unix.EPERM {
		return true
	}

	return false
}
697288
func (r *Reader) ReadStatusFromPID(pid int) (*File, error) {
	r.Lock()
	defer r.Unlock()

	file, err := os.Open("/proc/" + strconv.Itoa(pid) + "/status")
	if err != nil {
		return &File{}, errors.Errorf("failed to read process %d status file: %v", pid, err)
	}
	defer func() {
		if err := file.Close(); err != nil {
			r.Log.Errorf("failed to close status file for pid %d: %v", pid, err)
		}
	}()

	return r.parse(file), nil
}
845544
func processIOReader(fd io.Reader, contextID string) {
	reader := bufio.NewReader(fd)
	for {
		str, err := reader.ReadString('\n')
		if err != nil {
			return
		}
		fmt.Print("[" + contextID + "]:" + str)
	}
}
1100058
func (g *Generator) SetLinuxResourcesMemoryDisableOOMKiller(disable bool) {
	g.initConfigLinuxResourcesMemory()
	g.Config.Linux.Resources.Memory.DisableOOMKiller = &disable
}
1328657
func (p *Parser) parseKillQueryStatement() (*KillQueryStatement, error) {
	qid, err := p.ParseUInt64()
	if err != nil {
		return nil, err
	}

	var host string
	if tok, _, _ := p.ScanIgnoreWhitespace(); tok == ON {
		host, err = p.ParseIdent()
		if err != nil {
			return nil, err
		}
	} else {
		p.Unscan()
	}
	return &KillQueryStatement{QueryID: qid, Host: host}, nil
}
691777
func (cmd *Cmd) Stop() error {
	cmd.stoppingValue.Store(true)
	if err := cmd.Process.Signal(syscall.SIGKILL); err != nil {
		return err
	}
	return nil
}