mirror of https://github.com/xSmurf/oz.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
781 B
46 lines
781 B
10 years ago
|
package oz
|
||
|
|
||
|
import (
|
||
|
"syscall"
|
||
|
"github.com/op/go-logging"
|
||
|
"os"
|
||
|
"os/signal"
|
||
|
)
|
||
|
|
||
|
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
|
||
|
}
|
||
|
}
|
||
|
}
|