linux.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // +build linux
  2. package system
  3. import (
  4. "bufio"
  5. "fmt"
  6. "os"
  7. "os/exec"
  8. "syscall"
  9. "unsafe"
  10. )
  11. // If arg2 is nonzero, set the "child subreaper" attribute of the
  12. // calling process; if arg2 is zero, unset the attribute. When a
  13. // process is marked as a child subreaper, all of the children
  14. // that it creates, and their descendants, will be marked as
  15. // having a subreaper. In effect, a subreaper fulfills the role
  16. // of init(1) for its descendant processes. Upon termination of
  17. // a process that is orphaned (i.e., its immediate parent has
  18. // already terminated) and marked as having a subreaper, the
  19. // nearest still living ancestor subreaper will receive a SIGCHLD
  20. // signal and be able to wait(2) on the process to discover its
  21. // termination status.
  22. const PR_SET_CHILD_SUBREAPER = 36
  23. type ParentDeathSignal int
  24. func (p ParentDeathSignal) Restore() error {
  25. if p == 0 {
  26. return nil
  27. }
  28. current, err := GetParentDeathSignal()
  29. if err != nil {
  30. return err
  31. }
  32. if p == current {
  33. return nil
  34. }
  35. return p.Set()
  36. }
  37. func (p ParentDeathSignal) Set() error {
  38. return SetParentDeathSignal(uintptr(p))
  39. }
  40. func Execv(cmd string, args []string, env []string) error {
  41. name, err := exec.LookPath(cmd)
  42. if err != nil {
  43. return err
  44. }
  45. return syscall.Exec(name, args, env)
  46. }
  47. func Prlimit(pid, resource int, limit syscall.Rlimit) error {
  48. _, _, err := syscall.RawSyscall6(syscall.SYS_PRLIMIT64, uintptr(pid), uintptr(resource), uintptr(unsafe.Pointer(&limit)), uintptr(unsafe.Pointer(&limit)), 0, 0)
  49. if err != 0 {
  50. return err
  51. }
  52. return nil
  53. }
  54. func SetParentDeathSignal(sig uintptr) error {
  55. if _, _, err := syscall.RawSyscall(syscall.SYS_PRCTL, syscall.PR_SET_PDEATHSIG, sig, 0); err != 0 {
  56. return err
  57. }
  58. return nil
  59. }
  60. func GetParentDeathSignal() (ParentDeathSignal, error) {
  61. var sig int
  62. _, _, err := syscall.RawSyscall(syscall.SYS_PRCTL, syscall.PR_GET_PDEATHSIG, uintptr(unsafe.Pointer(&sig)), 0)
  63. if err != 0 {
  64. return -1, err
  65. }
  66. return ParentDeathSignal(sig), nil
  67. }
  68. func SetKeepCaps() error {
  69. if _, _, err := syscall.RawSyscall(syscall.SYS_PRCTL, syscall.PR_SET_KEEPCAPS, 1, 0); err != 0 {
  70. return err
  71. }
  72. return nil
  73. }
  74. func ClearKeepCaps() error {
  75. if _, _, err := syscall.RawSyscall(syscall.SYS_PRCTL, syscall.PR_SET_KEEPCAPS, 0, 0); err != 0 {
  76. return err
  77. }
  78. return nil
  79. }
  80. func Setctty() error {
  81. if _, _, err := syscall.RawSyscall(syscall.SYS_IOCTL, 0, uintptr(syscall.TIOCSCTTY), 0); err != 0 {
  82. return err
  83. }
  84. return nil
  85. }
  86. /*
  87. * Detect whether we are currently running in a user namespace.
  88. * Copied from github.com/lxc/lxd/shared/util.go
  89. */
  90. func RunningInUserNS() bool {
  91. file, err := os.Open("/proc/self/uid_map")
  92. if err != nil {
  93. /*
  94. * This kernel-provided file only exists if user namespaces are
  95. * supported
  96. */
  97. return false
  98. }
  99. defer file.Close()
  100. buf := bufio.NewReader(file)
  101. l, _, err := buf.ReadLine()
  102. if err != nil {
  103. return false
  104. }
  105. line := string(l)
  106. var a, b, c int64
  107. fmt.Sscanf(line, "%d %d %d", &a, &b, &c)
  108. /*
  109. * We assume we are in the initial user namespace if we have a full
  110. * range - 4294967295 uids starting at uid 0.
  111. */
  112. if a == 0 && b == 0 && c == 4294967295 {
  113. return false
  114. }
  115. return true
  116. }
  117. // SetSubreaper sets the value i as the subreaper setting for the calling process
  118. func SetSubreaper(i int) error {
  119. return Prctl(PR_SET_CHILD_SUBREAPER, uintptr(i), 0, 0, 0)
  120. }
  121. func Prctl(option int, arg2, arg3, arg4, arg5 uintptr) (err error) {
  122. _, _, e1 := syscall.Syscall6(syscall.SYS_PRCTL, uintptr(option), arg2, arg3, arg4, arg5, 0)
  123. if e1 != 0 {
  124. err = e1
  125. }
  126. return
  127. }