2016-01-21 05:57:02 +00:00
|
|
|
// +build linux
|
2015-12-07 18:36:48 +00:00
|
|
|
|
2016-06-03 22:00:49 +00:00
|
|
|
// Package osutils provide access to the Get Child and Set Child prctl
|
|
|
|
// flags.
|
|
|
|
// See http://man7.org/linux/man-pages/man2/prctl.2.html
|
2016-11-29 23:38:24 +00:00
|
|
|
package sys
|
2015-12-07 18:36:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2016-06-03 22:00:49 +00:00
|
|
|
// PR_SET_CHILD_SUBREAPER allows setting the child subreaper.
|
2015-12-07 18:36:48 +00:00
|
|
|
// If arg2 is nonzero, set the "child subreaper" attribute of the
|
|
|
|
// calling process; if arg2 is zero, unset the attribute. When a
|
|
|
|
// process is marked as a child subreaper, all of the children
|
|
|
|
// that it creates, and their descendants, will be marked as
|
|
|
|
// having a subreaper. In effect, a subreaper fulfills the role
|
|
|
|
// of init(1) for its descendant processes. Upon termination of
|
|
|
|
// a process that is orphaned (i.e., its immediate parent has
|
|
|
|
// already terminated) and marked as having a subreaper, the
|
|
|
|
// nearest still living ancestor subreaper will receive a SIGCHLD
|
|
|
|
// signal and be able to wait(2) on the process to discover its
|
|
|
|
// termination status.
|
2016-06-03 22:00:49 +00:00
|
|
|
const prSetChildSubreaper = 36
|
2015-12-07 18:36:48 +00:00
|
|
|
|
2016-06-03 22:00:49 +00:00
|
|
|
// PR_GET_CHILD_SUBREAPER allows retrieving the current child
|
|
|
|
// subreaper.
|
2017-02-09 09:30:21 +00:00
|
|
|
// Returns the "child subreaper" setting of the caller, in the
|
2015-12-07 18:36:48 +00:00
|
|
|
// location pointed to by (int *) arg2.
|
2016-06-03 22:00:49 +00:00
|
|
|
const prGetChildSubreaper = 37
|
2015-12-07 18:36:48 +00:00
|
|
|
|
|
|
|
// GetSubreaper returns the subreaper setting for the calling process
|
|
|
|
func GetSubreaper() (int, error) {
|
|
|
|
var i uintptr
|
2016-06-03 22:00:49 +00:00
|
|
|
if _, _, err := syscall.RawSyscall(syscall.SYS_PRCTL, prGetChildSubreaper, uintptr(unsafe.Pointer(&i)), 0); err != 0 {
|
2015-12-07 18:36:48 +00:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
return int(i), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSubreaper sets the value i as the subreaper setting for the calling process
|
|
|
|
func SetSubreaper(i int) error {
|
2016-06-03 22:00:49 +00:00
|
|
|
if _, _, err := syscall.RawSyscall(syscall.SYS_PRCTL, prSetChildSubreaper, uintptr(i), 0); err != 0 {
|
2015-12-07 18:36:48 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|