// For use by the daemon: signal an outcome back to Run in the invoking tool,
// causing it to return. Do nothing if the process wasn't invoked with Run.
556823
func SignalOutcome(outcome error) (err error) {
	// Is there anything to do?
	if gGobEncoder == nil {
		return
	}

	// Write out the outcome.
	msg := &outcomeMsg{
		Successful: outcome == nil,
	}

	if !msg.Successful {
		msg.ErrorMsg = outcome.Error()
	}

	err = sendMsg(msg)

	return
}
1163601
func (v *Future) GetWaitTimeout(t time.Duration) (bool, interface{}) {
	select {
	case <-v.signal:
		return true, v.result
	case <-time.After(t):
		return false, nil
	}
}
1038476
func (d *Daemon) Process() (*os.Process, error) {
	p, err := d.Pid()
	if err != nil {
		return nil, err
	}
	return os.FindProcess(p)
}
1112278
func (mr *MockClockMockRecorder) After(arg0 interface{}) *gomock.Call {
	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "After", reflect.TypeOf((*MockClock)(nil).After), arg0)
}
947367
func (remote *RemoteDebugger) CallbackEvent(method string, cb EventCallback) {
	remote.Lock()
	remote.callbacks[method] = cb
	remote.Unlock()
}
685692
func (c *client) SignalProcess(_ context.Context, containerID, processID string, signal int) error {
	ctr, p, err := c.getProcess(containerID, processID)
	if err != nil {
		return err
	}

	logger := c.logger.WithFields(logrus.Fields{
		"container": containerID,
		"process":   processID,
		"pid":       p.pid,
		"signal":    signal,
	})
	logger.Debug("Signal()")

	if processID == libcontainerdtypes.InitProcessName {
		if syscall.Signal(signal) == syscall.SIGKILL {
			// Terminate the compute system
			ctr.Lock()
			ctr.terminateInvoked = true
			if err := ctr.hcsContainer.Terminate(); err != nil {
				if !hcsshim.IsPending(err) {
					logger.WithError(err).Error("failed to terminate hccshim container")
				}
			}
			ctr.Unlock()
		} else {
			// Shut down the container
			if err := ctr.hcsContainer.Shutdown(); err != nil {
				if !hcsshim.IsPending(err) && !hcsshim.IsAlreadyStopped(err) {
					// ignore errors
					logger.WithError(err).Error("failed to shutdown hccshim container")
				}
			}
		}
	} else {
		return p.hcsProcess.Kill()
	}

	return nil
}
722921
func (process *Process) Kill() (_ bool, err error) {
	process.handleLock.RLock()
	defer process.handleLock.RUnlock()

	operation := "hcsshim::Process::Kill"
	process.logOperationBegin(operation)
	defer func() { process.logOperationEnd(operation, err) }()

	if process.handle == 0 {
		return false, makeProcessError(process, operation, ErrAlreadyClosed, nil)
	}

	var resultp *uint16
	syscallWatcher(process.logctx, func() {
		err = hcsTerminateProcess(process.handle, &resultp)
	})
	events := processHcsResult(resultp)
	delivered, err := process.processSignalResult(err)
	if err != nil {
		err = makeProcessError(process, operation, err, events)
	}
	return delivered, err
}
682223
func (process *process) Kill() error {
	found, err := process.p.Kill()
	if err != nil {
		return convertProcessError(err, process)
	}
	if !found {
		return &ProcessError{Process: process, Err: ErrElementNotFound, Operation: "hcsshim::Process::Kill"}
	}
	return nil
}
36202
func init() {
	daemon.AppName = "server"
	daemon.PidFile = "server.pid"

	printStatusErr := func(e error) {
		fmt.Printf("Checking status of %s FAILED\n", daemon.AppName)
		fmt.Printf("Details: %s\n", e.Error())
	}
	printFailed := func(e error) {
		fmt.Println("FAILED")
		fmt.Printf("Details: %s\n", e.Error())
	}

	// Helper function for custom daemon starting.
	start := func() {
		fmt.Printf("Starting %s...", daemon.AppName)
		sig := make(chan os.Signal)
		signal.Notify(sig, syscall.SIGUSR1)
		cmd, err := daemon.StartCommand()
		if err != nil {
			printFailed(err)
		}
		if err = cmd.Start(); err != nil {
			printFailed(err)
			return
		}
		select {
		case <-sig: // received "OK" signal from child process
			fmt.Println("OK")
		case <-time.After(10 * time.Second): // timeout for waiting signal
			fmt.Println("TIMEOUTED")
			fmt.Println("Error: signal from child process not received")
			fmt.Println("Check logs/application.log for details")
		case err := <-func() chan error {
			ch := make(chan error)
			go func() {
				err := cmd.Wait()
				if err == nil {
					err = fmt.Errorf("child process unexpectedly stopped")
				}
				ch <- err
			}()
			return ch
		}(): // child process unexpectedly stopped without sending signal
			printFailed(err)
		}
	}

	// Implements scenario of starting media-api.
	daemon.SetAction("start", func() {
		switch isRunning, _, err := daemon.Status(); {
		case err != nil:
			printStatusErr(err)
		case isRunning:
			fmt.Printf(
				"%s is already started and running now\n", daemon.AppName,
			)
		default:
			start()
		}
	})

	daemon.SetAction("restart", func() {
		isRunning, process, err := daemon.Status()
		if err != nil {
			printStatusErr(err)
			return
		}
		if isRunning {
			fmt.Printf("Stopping %s...", daemon.AppName)
			if err := daemon.Stop(process); err != nil {
				printFailed(err)
				return
			} else {
				fmt.Println("OK")
			}
		}
		start()
	})
}
294676
func (f *future) result() (loader.PluginInstance, error) {
	return f.instance, f.err
}
1059765
func (m *ResultWriter) Run() error {
	defer m.Ctx.Recover()
	defer func() {
		close(m.msgOutCh) // closing output channels is the signal to stop
	}()
	select {
	case err := <-m.errCh:
		u.Errorf("got error:  %v", err)
		return err
	case <-m.sigCh:
		// u.Debugf("%p got resultwriter.Run() sigquit?", m)
		return nil
	}
}
553621
func (e *UniversalExecutor) Signal(s os.Signal) error {
	if e.childCmd.Process == nil {
		return fmt.Errorf("Task not yet run")
	}

	e.logger.Debug("sending signal to PID", "signal", s, "pid", e.childCmd.Process.Pid)
	err := e.childCmd.Process.Signal(s)
	if err != nil {
		e.logger.Error("sending signal failed", "signal", s, "error", err)
		return err
	}

	return nil
}
994664
func (h *signalHandler) forward(process *libcontainer.Process, tty *tty, detach bool) (int, error) {
	// make sure we know the pid of our main process so that we can return
	// after it dies.
	if detach && h.notifySocket == nil {
		return 0, nil
	}

	pid1, err := process.Pid()
	if err != nil {
		return -1, err
	}

	if h.notifySocket != nil {
		if detach {
			h.notifySocket.run(pid1)
			return 0, nil
		}
		go h.notifySocket.run(0)
	}

	// Perform the initial tty resize. Always ignore errors resizing because
	// stdout might have disappeared (due to races with when SIGHUP is sent).
	_ = tty.resize()
	// Handle and forward signals.
	for s := range h.signals {
		switch s {
		case unix.SIGWINCH:
			// Ignore errors resizing, as above.
			_ = tty.resize()
		case unix.SIGCHLD:
			exits, err := h.reap()
			if err != nil {
				logrus.Error(err)
			}
			for _, e := range exits {
				logrus.WithFields(logrus.Fields{
					"pid":    e.pid,
					"status": e.status,
				}).Debug("process exited")
				if e.pid == pid1 {
					// call Wait() on the process even though we already have the exit
					// status because we must ensure that any of the go specific process
					// fun such as flushing pipes are complete before we return.
					process.Wait()
					if h.notifySocket != nil {
						h.notifySocket.Close()
					}
					return e.status, nil
				}
			}
		default:
			logrus.Debugf("sending signal to process %s", s)
			if err := unix.Kill(pid1, s.(syscall.Signal)); err != nil {
				logrus.Error(err)
			}
		}
	}
	return -1, nil
}
1157956
func (s *SubscribeToken) Result() map[string]byte {
	s.m.RLock()
	defer s.m.RUnlock()
	return s.subResult
}
480910
func (dbp *Process) kill() (err error) {
	if dbp.exited {
		return nil
	}
	if !dbp.threads[dbp.pid].Stopped() {
		return errors.New("process must be stopped in order to kill it")
	}
	if err = sys.Kill(-dbp.pid, sys.SIGKILL); err != nil {
		return errors.New("could not deliver signal " + err.Error())
	}
	if _, _, err = dbp.wait(dbp.pid, 0); err != nil {
		return
	}
	dbp.postExit()
	return
}
714807
func (rp *Reprovider) Run(proc goprocess.Process) {
	ctx := goprocessctx.WithProcessClosing(rp.ctx, proc)
	defer close(rp.closing)

	// dont reprovide immediately.
	// may have just started the daemon and shutting it down immediately.
	// probability( up another minute | uptime ) increases with uptime.
	after := time.After(time.Minute)
	var done doneFunc
	for {
		if rp.tick == 0 {
			after = make(chan time.Time)
		}

		select {
		case <-ctx.Done():
			return
		case done = <-rp.trigger:
		case <-after:
		}

		// 'mute' the trigger channel so when `ipfs bitswap reprovide` is called
		// a 'reprovider is already running' error is returned
		unmute := rp.muteTrigger()

		err := rp.reprovide(ctx)
		if err != nil {
			log.Debug(err)
		}

		if done != nil {
			done(err)
		}

		unmute()

		after = time.After(rp.tick)
	}
}
986183
func WaitForInspectResult(t testingT, name, expr, expected string, timeout time.Duration, cmdOperators ...CmdOperator) {
	after := time.After(timeout)

	args := []string{"inspect", "-f", expr, name}
	for {
		result := Docker(Args(args...), cmdOperators...)
		if result.Error != nil {
			if !strings.Contains(strings.ToLower(result.Stderr()), "no such") {
				t.Fatalf("error executing docker inspect: %v\n%s",
					result.Stderr(), result.Stdout())
			}
			select {
			case <-after:
				t.Fatal(result.Error)
			default:
				time.Sleep(10 * time.Millisecond)
				continue
			}
		}

		out := strings.TrimSpace(result.Stdout())
		if out == expected {
			break
		}

		select {
		case <-after:
			t.Fatalf("condition \"%q == %q\" not true in time (%v)", out, expected, timeout)
		default:
		}

		time.Sleep(100 * time.Millisecond)
	}
}