2018-05-03 22:55:49 +00:00
|
|
|
// +build !windows
|
|
|
|
|
2018-09-06 10:50:38 +00:00
|
|
|
package proxyprocess
|
2018-05-03 22:55:49 +00:00
|
|
|
|
|
|
|
import (
|
2018-05-04 01:25:11 +00:00
|
|
|
"fmt"
|
2018-05-03 22:55:49 +00:00
|
|
|
"os"
|
2018-06-07 17:27:28 +00:00
|
|
|
"os/exec"
|
2018-05-03 22:55:49 +00:00
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2018-05-04 01:25:11 +00:00
|
|
|
// findProcess for non-Windows. Note that this very likely doesn't
|
2018-05-03 22:55:49 +00:00
|
|
|
// work for all non-Windows platforms Go supports and we should expand
|
|
|
|
// support as we experience it.
|
2018-05-04 01:25:11 +00:00
|
|
|
func findProcess(pid int) (*os.Process, error) {
|
|
|
|
// FindProcess never fails on unix-like systems.
|
|
|
|
p, err := os.FindProcess(pid)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-05-03 22:55:49 +00:00
|
|
|
// On Unix-like systems, we can verify a process is alive by sending
|
|
|
|
// a 0 signal. This will do nothing to the process but will still
|
|
|
|
// return errors if the process is gone.
|
2018-05-04 01:25:11 +00:00
|
|
|
err = p.Signal(syscall.Signal(0))
|
2018-05-04 15:09:44 +00:00
|
|
|
if err == nil {
|
2018-05-04 01:25:11 +00:00
|
|
|
return p, nil
|
2018-05-03 22:55:49 +00:00
|
|
|
}
|
|
|
|
|
2018-05-04 15:09:44 +00:00
|
|
|
return nil, fmt.Errorf("process %d is dead or running as another user", pid)
|
2018-05-03 22:55:49 +00:00
|
|
|
}
|
2018-06-07 17:27:28 +00:00
|
|
|
|
|
|
|
// configureDaemon is called prior to Start to allow system-specific setup.
|
|
|
|
func configureDaemon(cmd *exec.Cmd) {
|
|
|
|
// Start it in a new sessions (and hence process group) so that killing agent
|
|
|
|
// (even with Ctrl-C) won't kill proxy.
|
|
|
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setsid: true}
|
|
|
|
}
|