Skip to content

Instantly share code, notes, and snippets.

@crosbymichael
Last active July 1, 2016 00:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crosbymichael/d8e9ae46f16200584e95 to your computer and use it in GitHub Desktop.
Save crosbymichael/d8e9ae46f16200584e95 to your computer and use it in GitHub Desktop.
package dontfearthereaper
import "syscall"
// PR_SET_CHILD_SUBREAPER
const prSetSubreaper = 0x36
func init() {
if _, _, err := syscall.RawSyscall(syscall.SYS_PRCTL, prSetSubreaper, 0, 0); err != 0 {
panic(err)
}
}
type Reaper interface {
Reap() ([]int, error)
}
func New() Reaper {
return &linuxReaper{}
}
type linuxReaper struct {
}
func (l *linuxReaper) Reap() ([]int, error) {
return l.reapPids()
}
func (l *linuxReaper) reapPids() ([]int, error) {
var (
ws syscall.WaitStatus
rus syscall.Rusage
pids []int
)
for {
pid, err := syscall.Wait4(-1, &ws, syscall.WNOHANG, &rus)
if err != nil {
if err == syscall.ECHILD {
return pids, nil
}
return nil, err
}
if pid <= 0 {
return pids, nil
}
pids = append(pids, pid)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment