-
Notifications
You must be signed in to change notification settings - Fork 1
/
reaper.go
164 lines (133 loc) · 2.92 KB
/
reaper.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"bufio"
"io"
"os"
"os/signal"
"path/filepath"
"strconv"
"strings"
"time"
"golang.org/x/sys/unix"
)
var (
childCatcher = make(chan os.Signal, 1)
procDir = "/proc"
waiterFunc waiter = unix.Wait4
)
type waiter func(int, *unix.WaitStatus, int, *unix.Rusage) (int, error)
func init() {
signal.Notify(childCatcher, unix.SIGCHLD)
}
func reap() {
var err error
for range childCatcher {
err = reapLoop()
if err != nil {
sugar.Errorw("reading proc failed",
"error", err.Error(),
)
}
// Bit of breathing room, maybe?
time.Sleep(time.Millisecond * 100)
}
}
func reapLoop() (err error) {
pids, err := getZombiePids()
if err != nil {
return
}
if len(pids) == 0 {
return
}
var (
status = new(unix.WaitStatus)
rusage = new(unix.Rusage)
)
sugar.Infow("reaper is reaping",
"count", len(pids),
"pids", pids,
)
for _, pid := range pids {
_, err = waiterFunc(pid, status, unix.WNOHANG, rusage)
if err != nil {
break
}
}
return
}
// getZombiePids runs through proc, looking for processes which are
// in a zombie state.
//
// It does this by finding any directory in `/proc/` whch starts with
// a number, and then passing that field to a function who can inspect
// that process (should it be a process) for zombie status, returning a
// slice containing any process in a zombie state.
func getZombiePids() (pids []int, err error) {
f, err := os.Open(procDir)
if err != nil {
return
}
defer f.Close() // #nosec: G307
pids = make([]int, 0)
var (
names []string
isZ bool
)
for {
names, err = f.Readdirnames(20)
if err != nil {
if err == io.EOF {
err = nil
}
break
}
for _, name := range names {
if name[0] < '0' || name[0] > '9' {
continue
}
isZ = checkZombie(name)
if !isZ {
continue
}
pid, err := strconv.ParseInt(name, 10, 0)
if err != nil {
continue
}
pids = append(pids, int(pid))
}
}
return
}
// checkZombie reads the file /proc/${pid}/status, looking for whether
// or not the field 'State' signifies the process as a Zombie
//
// pid is a string here purely because at the time we check the pid, it
// makes sense to leave it as a string and not to strconv it until we need
// to.
func checkZombie(pid string) bool {
f, err := os.Open(filepath.Join(procDir, pid, "status")) // #nosec: G304
if err != nil {
// swallow errors; maybe the pid has ended, or maybe there's another
// issue that would stop us waiting on the process anyway /shrug
return false
}
defer f.Close() // #nosec: G307
var fields []string
scanner := bufio.NewScanner(f)
for scanner.Scan() {
fields = strings.Fields(scanner.Text())
if fields[0] != "State:" {
continue
}
if len(fields) > 1 && fields[1] == "Z" {
return true
}
// At this point we've read the State line, and it wasn't
// a zombie (or something weird happened).
//
// In anycase, sack it off
break
}
return false
}