forked from subgraph/oz
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchild.go
More file actions
45 lines (39 loc) · 781 Bytes
/
child.go
File metadata and controls
45 lines (39 loc) · 781 Bytes
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
package oz
import (
"github.com/op/go-logging"
"os"
"os/signal"
"syscall"
)
func ReapChildProcs(log *logging.Logger, callback func(int, syscall.WaitStatus)) chan os.Signal {
sigs := make(chan os.Signal, 3)
signal.Notify(sigs, syscall.SIGCHLD)
go func() {
for {
<-sigs
handleSIGCHLD(log, callback)
}
}()
return sigs
}
func handleSIGCHLD(log *logging.Logger, callback func(int, syscall.WaitStatus)) {
var wstatus syscall.WaitStatus
for {
pid, err := syscall.Wait4(-1, &wstatus, syscall.WNOHANG, nil)
switch err {
case syscall.ECHILD:
return
case syscall.EINTR:
case nil:
if pid == 0 {
return
}
callback(pid, wstatus)
default:
if log != nil {
log.Warning("syscall.Wait4() returned error: %v", err)
}
return
}
}
}