linux.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // +build linux
  2. package system
  3. import (
  4. "os"
  5. "os/exec"
  6. "syscall" // only for exec
  7. "unsafe"
  8. "github.com/opencontainers/runc/libcontainer/user"
  9. "golang.org/x/sys/unix"
  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 unix.Rlimit) error {
  48. _, _, err := unix.RawSyscall6(unix.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 := unix.Prctl(unix.PR_SET_PDEATHSIG, sig, 0, 0, 0); err != nil {
  56. return err
  57. }
  58. return nil
  59. }
  60. func GetParentDeathSignal() (ParentDeathSignal, error) {
  61. var sig int
  62. if err := unix.Prctl(unix.PR_GET_PDEATHSIG, uintptr(unsafe.Pointer(&sig)), 0, 0, 0); err != nil {
  63. return -1, err
  64. }
  65. return ParentDeathSignal(sig), nil
  66. }
  67. func SetKeepCaps() error {
  68. if err := unix.Prctl(unix.PR_SET_KEEPCAPS, 1, 0, 0, 0); err != nil {
  69. return err
  70. }
  71. return nil
  72. }
  73. func ClearKeepCaps() error {
  74. if err := unix.Prctl(unix.PR_SET_KEEPCAPS, 0, 0, 0, 0); err != nil {
  75. return err
  76. }
  77. return nil
  78. }
  79. func Setctty() error {
  80. if err := unix.IoctlSetInt(0, unix.TIOCSCTTY, 0); err != nil {
  81. return err
  82. }
  83. return nil
  84. }
  85. // RunningInUserNS detects whether we are currently running in a user namespace.
  86. // Originally copied from github.com/lxc/lxd/shared/util.go
  87. func RunningInUserNS() bool {
  88. uidmap, err := user.CurrentProcessUIDMap()
  89. if err != nil {
  90. // This kernel-provided file only exists if user namespaces are supported
  91. return false
  92. }
  93. return UIDMapInUserNS(uidmap)
  94. }
  95. func UIDMapInUserNS(uidmap []user.IDMap) bool {
  96. /*
  97. * We assume we are in the initial user namespace if we have a full
  98. * range - 4294967295 uids starting at uid 0.
  99. */
  100. if len(uidmap) == 1 && uidmap[0].ID == 0 && uidmap[0].ParentID == 0 && uidmap[0].Count == 4294967295 {
  101. return false
  102. }
  103. return true
  104. }
  105. // GetParentNSeuid returns the euid within the parent user namespace
  106. func GetParentNSeuid() int64 {
  107. euid := int64(os.Geteuid())
  108. uidmap, err := user.CurrentProcessUIDMap()
  109. if err != nil {
  110. // This kernel-provided file only exists if user namespaces are supported
  111. return euid
  112. }
  113. for _, um := range uidmap {
  114. if um.ID <= euid && euid <= um.ID+um.Count-1 {
  115. return um.ParentID + euid - um.ID
  116. }
  117. }
  118. return euid
  119. }
  120. // SetSubreaper sets the value i as the subreaper setting for the calling process
  121. func SetSubreaper(i int) error {
  122. return unix.Prctl(PR_SET_CHILD_SUBREAPER, uintptr(i), 0, 0, 0)
  123. }
  124. // GetSubreaper returns the subreaper setting for the calling process
  125. func GetSubreaper() (int, error) {
  126. var i uintptr
  127. if err := unix.Prctl(unix.PR_GET_CHILD_SUBREAPER, uintptr(unsafe.Pointer(&i)), 0, 0, 0); err != nil {
  128. return -1, err
  129. }
  130. return int(i), nil
  131. }