utils_unix.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. //go:build !windows
  2. // +build !windows
  3. package utils
  4. import (
  5. "fmt"
  6. "os"
  7. "strconv"
  8. "golang.org/x/sys/unix"
  9. )
  10. // EnsureProcHandle returns whether or not the given file handle is on procfs.
  11. func EnsureProcHandle(fh *os.File) error {
  12. var buf unix.Statfs_t
  13. if err := unix.Fstatfs(int(fh.Fd()), &buf); err != nil {
  14. return fmt.Errorf("ensure %s is on procfs: %w", fh.Name(), err)
  15. }
  16. if buf.Type != unix.PROC_SUPER_MAGIC {
  17. return fmt.Errorf("%s is not on procfs", fh.Name())
  18. }
  19. return nil
  20. }
  21. // CloseExecFrom applies O_CLOEXEC to all file descriptors currently open for
  22. // the process (except for those below the given fd value).
  23. func CloseExecFrom(minFd int) error {
  24. fdDir, err := os.Open("/proc/self/fd")
  25. if err != nil {
  26. return err
  27. }
  28. defer fdDir.Close()
  29. if err := EnsureProcHandle(fdDir); err != nil {
  30. return err
  31. }
  32. fdList, err := fdDir.Readdirnames(-1)
  33. if err != nil {
  34. return err
  35. }
  36. for _, fdStr := range fdList {
  37. fd, err := strconv.Atoi(fdStr)
  38. // Ignore non-numeric file names.
  39. if err != nil {
  40. continue
  41. }
  42. // Ignore descriptors lower than our specified minimum.
  43. if fd < minFd {
  44. continue
  45. }
  46. // Intentionally ignore errors from unix.CloseOnExec -- the cases where
  47. // this might fail are basically file descriptors that have already
  48. // been closed (including and especially the one that was created when
  49. // os.ReadDir did the "opendir" syscall).
  50. unix.CloseOnExec(fd)
  51. }
  52. return nil
  53. }
  54. // NewSockPair returns a new unix socket pair
  55. func NewSockPair(name string) (parent *os.File, child *os.File, err error) {
  56. fds, err := unix.Socketpair(unix.AF_LOCAL, unix.SOCK_STREAM|unix.SOCK_CLOEXEC, 0)
  57. if err != nil {
  58. return nil, nil, err
  59. }
  60. return os.NewFile(uintptr(fds[1]), name+"-p"), os.NewFile(uintptr(fds[0]), name+"-c"), nil
  61. }