github.imxd.top/hashicorp/consul@v1.4.5/agent/proxyprocess/process_unix.go (about)

     1  // +build !windows
     2  
     3  package proxyprocess
     4  
     5  import (
     6  	"fmt"
     7  	"os"
     8  	"os/exec"
     9  	"syscall"
    10  )
    11  
    12  // findProcess for non-Windows. Note that this very likely doesn't
    13  // work for all non-Windows platforms Go supports and we should expand
    14  // support as we experience it.
    15  func findProcess(pid int) (*os.Process, error) {
    16  	// FindProcess never fails on unix-like systems.
    17  	p, err := os.FindProcess(pid)
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  
    22  	// On Unix-like systems, we can verify a process is alive by sending
    23  	// a 0 signal. This will do nothing to the process but will still
    24  	// return errors if the process is gone.
    25  	err = p.Signal(syscall.Signal(0))
    26  	if err == nil {
    27  		return p, nil
    28  	}
    29  
    30  	return nil, fmt.Errorf("process %d is dead or running as another user", pid)
    31  }
    32  
    33  // configureDaemon is called prior to Start to allow system-specific setup.
    34  func configureDaemon(cmd *exec.Cmd) {
    35  	// Start it in a new sessions (and hence process group) so that killing agent
    36  	// (even with Ctrl-C) won't kill proxy.
    37  	cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
    38  }