-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
proc_posix.go
53 lines (42 loc) · 996 Bytes
/
proc_posix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//go:build !windows
// +build !windows
package main
import (
"os"
"os/signal"
"golang.org/x/sys/unix"
)
const sigint = unix.SIGINT
const sigterm = unix.SIGTERM
const sighup = unix.SIGHUP
var cmdStart = []string{"/bin/sh", "-c"}
var procAttrs = &unix.SysProcAttr{Setpgid: true}
func terminateProc(proc *procInfo, signal os.Signal) error {
p := proc.cmd.Process
if p == nil {
return nil
}
pgid, err := unix.Getpgid(p.Pid)
if err != nil {
return err
}
// use pgid, ref: http://unix.stackexchange.com/questions/14815/process-descendants
pid := p.Pid
if pgid == p.Pid {
pid = -1 * pid
}
target, err := os.FindProcess(pid)
if err != nil {
return err
}
return target.Signal(signal)
}
// killProc kills the proc with pid pid, as well as its children.
func killProc(process *os.Process) error {
return unix.Kill(-1*process.Pid, unix.SIGKILL)
}
func notifyCh() <-chan os.Signal {
sc := make(chan os.Signal, 10)
signal.Notify(sc, sigterm, sigint, sighup)
return sc
}