forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reaper.go
39 lines (34 loc) · 783 Bytes
/
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
// +build linux
package proc
import (
"os"
"os/signal"
"syscall"
"github.com/golang/glog"
)
// StartReaper starts a goroutine to reap processes if called from a process
// that has pid 1.
func StartReaper() {
if os.Getpid() == 1 {
glog.V(4).Infof("Launching reaper")
go func() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGCHLD)
for {
// Wait for a child to terminate
sig := <-sigs
glog.V(4).Infof("Signal received: %v", sig)
for {
// Reap processes
glog.V(4).Infof("Waiting to reap")
cpid, _ := syscall.Wait4(-1, nil, syscall.WNOHANG, nil)
if cpid < 1 {
glog.V(4).Infof("No more processes to reap.")
break
}
glog.V(4).Infof("Reaped process with pid %d", cpid)
}
}
}()
}
}