linux.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //go:build linux
  2. // +build linux
  3. package system
  4. import (
  5. "os"
  6. "os/exec"
  7. "unsafe"
  8. "golang.org/x/sys/unix"
  9. )
  10. type ParentDeathSignal int
  11. func (p ParentDeathSignal) Restore() error {
  12. if p == 0 {
  13. return nil
  14. }
  15. current, err := GetParentDeathSignal()
  16. if err != nil {
  17. return err
  18. }
  19. if p == current {
  20. return nil
  21. }
  22. return p.Set()
  23. }
  24. func (p ParentDeathSignal) Set() error {
  25. return SetParentDeathSignal(uintptr(p))
  26. }
  27. // Eaccess is similar to unix.Access except for setuid/setgid binaries
  28. // it checks against the effective (rather than real) uid and gid.
  29. func Eaccess(path string) error {
  30. err := unix.Faccessat2(unix.AT_FDCWD, path, unix.X_OK, unix.AT_EACCESS)
  31. if err != unix.ENOSYS && err != unix.EPERM { //nolint:errorlint // unix errors are bare
  32. return err
  33. }
  34. // Faccessat2() not available; check if we are a set[ug]id binary.
  35. if os.Getuid() == os.Geteuid() && os.Getgid() == os.Getegid() {
  36. // For a non-set[ug]id binary, use access(2).
  37. return unix.Access(path, unix.X_OK)
  38. }
  39. // For a setuid/setgid binary, there is no fallback way
  40. // so assume we can execute the binary.
  41. return nil
  42. }
  43. func Execv(cmd string, args []string, env []string) error {
  44. name, err := exec.LookPath(cmd)
  45. if err != nil {
  46. return err
  47. }
  48. return Exec(name, args, env)
  49. }
  50. func Exec(cmd string, args []string, env []string) error {
  51. for {
  52. err := unix.Exec(cmd, args, env)
  53. if err != unix.EINTR { //nolint:errorlint // unix errors are bare
  54. return &os.PathError{Op: "exec", Path: cmd, Err: err}
  55. }
  56. }
  57. }
  58. func SetParentDeathSignal(sig uintptr) error {
  59. if err := unix.Prctl(unix.PR_SET_PDEATHSIG, sig, 0, 0, 0); err != nil {
  60. return err
  61. }
  62. return nil
  63. }
  64. func GetParentDeathSignal() (ParentDeathSignal, error) {
  65. var sig int
  66. if err := unix.Prctl(unix.PR_GET_PDEATHSIG, uintptr(unsafe.Pointer(&sig)), 0, 0, 0); err != nil {
  67. return -1, err
  68. }
  69. return ParentDeathSignal(sig), nil
  70. }
  71. func SetKeepCaps() error {
  72. if err := unix.Prctl(unix.PR_SET_KEEPCAPS, 1, 0, 0, 0); err != nil {
  73. return err
  74. }
  75. return nil
  76. }
  77. func ClearKeepCaps() error {
  78. if err := unix.Prctl(unix.PR_SET_KEEPCAPS, 0, 0, 0, 0); err != nil {
  79. return err
  80. }
  81. return nil
  82. }
  83. func Setctty() error {
  84. if err := unix.IoctlSetInt(0, unix.TIOCSCTTY, 0); err != nil {
  85. return err
  86. }
  87. return nil
  88. }
  89. // SetSubreaper sets the value i as the subreaper setting for the calling process
  90. func SetSubreaper(i int) error {
  91. return unix.Prctl(unix.PR_SET_CHILD_SUBREAPER, uintptr(i), 0, 0, 0)
  92. }
  93. // GetSubreaper returns the subreaper setting for the calling process
  94. func GetSubreaper() (int, error) {
  95. var i uintptr
  96. if err := unix.Prctl(unix.PR_GET_CHILD_SUBREAPER, uintptr(unsafe.Pointer(&i)), 0, 0, 0); err != nil {
  97. return -1, err
  98. }
  99. return int(i), nil
  100. }