forked from evergreen-ci/evergreen
/
subtree_linux.go
63 lines (58 loc) · 1.71 KB
/
subtree_linux.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
54
55
56
57
58
59
60
61
62
63
package shell
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"github.com/evergreen-ci/evergreen/plugin"
"github.com/mongodb/grip/slogger"
)
func trackProcess(key string, pid int, log plugin.Logger) {
// trackProcess is a noop on linux, because we detect all the processes to be killed in
// cleanup() and we don't need to do any special bookkeeping up-front.
}
// getEnv returns a slice of environment variables for the given pid, in the form
// []string{"VAR1=FOO", "VAR2=BAR", ...}
// This function works by reading from /proc/$PID/environ, so the values returned only reflect
// the values of the environment variables at the time that the process was started.
func getEnv(pid int) ([]string, error) {
env, err := ioutil.ReadFile(fmt.Sprintf("/proc/%d/environ", pid))
if err != nil {
// This is probably either "permission denied" because we do not own the process,
// or the process simply doesn't exist anymore.
return nil, err
}
parts := bytes.Split(env, []byte{0})
results := make([]string, 0, len(parts))
for _, part := range parts {
if len(part) == 0 {
continue
}
results = append(results, string(part))
}
return results, nil
}
func cleanup(key string, log plugin.Logger) error {
pids, err := listProc()
if err != nil {
return err
}
pidMarker := fmt.Sprintf("EVR_AGENT_PID=%v", os.Getpid())
taskMarker := fmt.Sprintf("EVR_TASK_ID=%v", key)
for _, pid := range pids {
env, err := getEnv(pid)
if err != nil {
continue
}
if envHasMarkers(env, pidMarker, taskMarker) {
p := os.Process{}
p.Pid = pid
if err := p.Kill(); err != nil {
log.LogTask(slogger.INFO, "Killing %v failed: %v", pid, err)
} else {
log.LogTask(slogger.INFO, "Killed process %v", pid)
}
}
}
return nil
}