-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdio_proctree_unix.go
More file actions
87 lines (81 loc) · 2.19 KB
/
Copy pathstdio_proctree_unix.go
File metadata and controls
87 lines (81 loc) · 2.19 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//go:build !windows
package acpruntime
import (
"os"
"os/exec"
"strconv"
"strings"
"syscall"
)
// configureProcessGroup puts the agent process in its own process group so we
// can later signal the whole tree (npm -> node -> children) on teardown. This
// is Unix-only; the Windows variant lives in stdio_proctree_windows.go.
func configureProcessGroup(cmd *exec.Cmd) {
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
}
// processGroupIDAfterStart returns the process group id of the started command,
// or 0 if it cannot be determined. Used to target the whole tree on teardown.
func processGroupIDAfterStart(cmd *exec.Cmd) int {
if cmd.Process == nil {
return 0
}
pgid, err := syscall.Getpgid(cmd.Process.Pid)
if err != nil {
return 0
}
return pgid
}
// signalProcessTree delivers signal to the process tree rooted at pgid/process.
// It first targets direct descendants discovered via ps, then the whole group
// via the negative pgid, then falls back to signaling the leader itself.
func signalProcessTree(pgid int, process *os.Process, signal syscall.Signal) error {
if process != nil {
for _, pid := range descendantPIDs(process.Pid) {
_ = syscall.Kill(pid, signal)
}
}
if pgid > 0 {
if err := syscall.Kill(-pgid, signal); err == nil {
return nil
}
}
if process == nil {
return nil
}
return process.Signal(signal)
}
// descendantPIDs enumerates the descendant PIDs of rootPID using ps.
func descendantPIDs(rootPID int) []int {
if rootPID <= 0 {
return nil
}
output, err := exec.Command("ps", "-axo", "pid=,ppid=").Output()
if err != nil {
return nil
}
children := make(map[int][]int)
for _, line := range strings.Split(string(output), "\n") {
fields := strings.Fields(line)
if len(fields) != 2 {
continue
}
pid, err := strconv.Atoi(fields[0])
if err != nil {
continue
}
ppid, err := strconv.Atoi(fields[1])
if err != nil {
continue
}
children[ppid] = append(children[ppid], pid)
}
var descendants []int
stack := append([]int(nil), children[rootPID]...)
for len(stack) > 0 {
pid := stack[len(stack)-1]
stack = stack[:len(stack)-1]
descendants = append(descendants, pid)
stack = append(stack, children[pid]...)
}
return descendants
}