Fix problems found in QA pass of build 1792795001
This commit is contained in:
@@ -3,6 +3,8 @@ package procgroup
|
||||
import (
|
||||
"context"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
@@ -25,6 +27,41 @@ func group(cmd *exec.Cmd) *Cmd {
|
||||
return &Cmd{Cmd: cmd}
|
||||
}
|
||||
|
||||
// Kill also kills descendants that left the group: agents run their commands in sessions of their own.
|
||||
func (cmd *Cmd) Kill() error {
|
||||
return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
||||
pids := descendants(cmd.Process.Pid)
|
||||
err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
|
||||
for _, pid := range pids {
|
||||
syscall.Kill(pid, syscall.SIGKILL)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func descendants(root int) []int {
|
||||
out, err := exec.Command("ps", "-A", "-o", "pid=,ppid=").Output()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
children := map[int][]int{}
|
||||
for _, line := range strings.Split(string(out), "\n") {
|
||||
f := strings.Fields(line)
|
||||
if len(f) != 2 {
|
||||
continue
|
||||
}
|
||||
pid, err1 := strconv.Atoi(f[0])
|
||||
ppid, err2 := strconv.Atoi(f[1])
|
||||
if err1 != nil || err2 != nil {
|
||||
continue
|
||||
}
|
||||
children[ppid] = append(children[ppid], pid)
|
||||
}
|
||||
pids := []int{}
|
||||
queue := children[root]
|
||||
for len(queue) > 0 {
|
||||
pid := queue[0]
|
||||
queue = queue[1:]
|
||||
pids = append(pids, pid)
|
||||
queue = append(queue, children[pid]...)
|
||||
}
|
||||
return pids
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user