Last active
July 1, 2016 00:15
-
-
Save crosbymichael/d8e9ae46f16200584e95 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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