-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproc_posix.go
More file actions
52 lines (45 loc) · 1.21 KB
/
proc_posix.go
File metadata and controls
52 lines (45 loc) · 1.21 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
//go:build !windows
package main
import (
"os"
"os/exec"
"syscall"
"time"
)
// setCmdSysProcAttr places the child into its own process group on POSIX systems
func setCmdSysProcAttr(cmd *exec.Cmd) {
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
}
// forwardSignal forwards signals to the child's process group
func forwardSignal(cmd *exec.Cmd, sig os.Signal) {
if cmd == nil || cmd.Process == nil {
return
}
_ = syscall.Kill(-cmd.Process.Pid, signalToSys(sig))
}
// escalateTerminate tries SIGTERM then SIGKILL after a short delay
func escalateTerminate(cmd *exec.Cmd) {
if cmd == nil || cmd.Process == nil {
return
}
pid := cmd.Process.Pid
_ = syscall.Kill(-pid, syscall.SIGTERM)
t2 := time.NewTimer(5 * time.Second)
<-t2.C
_ = syscall.Kill(-pid, syscall.SIGKILL)
}
// forceKill immediately terminates the process group
func forceKill(cmd *exec.Cmd) {
if cmd == nil || cmd.Process == nil {
return
}
_ = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
}
func signalToSys(sig os.Signal) syscall.Signal {
switch s := sig.(type) {
case syscall.Signal:
return s
default:
return syscall.SIGINT
}
}