prctl.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // +build linux
  2. // http://man7.org/linux/man-pages/man2/prctl.2.html
  3. package osutils
  4. import (
  5. "syscall"
  6. "unsafe"
  7. )
  8. // If arg2 is nonzero, set the "child subreaper" attribute of the
  9. // calling process; if arg2 is zero, unset the attribute. When a
  10. // process is marked as a child subreaper, all of the children
  11. // that it creates, and their descendants, will be marked as
  12. // having a subreaper. In effect, a subreaper fulfills the role
  13. // of init(1) for its descendant processes. Upon termination of
  14. // a process that is orphaned (i.e., its immediate parent has
  15. // already terminated) and marked as having a subreaper, the
  16. // nearest still living ancestor subreaper will receive a SIGCHLD
  17. // signal and be able to wait(2) on the process to discover its
  18. // termination status.
  19. const PR_SET_CHILD_SUBREAPER = 36
  20. // Return the "child subreaper" setting of the caller, in the
  21. // location pointed to by (int *) arg2.
  22. const PR_GET_CHILD_SUBREAPER = 37
  23. // GetSubreaper returns the subreaper setting for the calling process
  24. func GetSubreaper() (int, error) {
  25. var i uintptr
  26. if _, _, err := syscall.RawSyscall(syscall.SYS_PRCTL, PR_GET_CHILD_SUBREAPER, uintptr(unsafe.Pointer(&i)), 0); err != 0 {
  27. return -1, err
  28. }
  29. return int(i), nil
  30. }
  31. // SetSubreaper sets the value i as the subreaper setting for the calling process
  32. func SetSubreaper(i int) error {
  33. if _, _, err := syscall.RawSyscall(syscall.SYS_PRCTL, PR_SET_CHILD_SUBREAPER, uintptr(i), 0); err != 0 {
  34. return err
  35. }
  36. return nil
  37. }