fd.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package sys
  2. import (
  3. "fmt"
  4. "math"
  5. "os"
  6. "runtime"
  7. "strconv"
  8. "github.com/cilium/ebpf/internal/unix"
  9. )
  10. var ErrClosedFd = unix.EBADF
  11. type FD struct {
  12. raw int
  13. }
  14. func newFD(value int) *FD {
  15. if onLeakFD != nil {
  16. // Attempt to store the caller's stack for the given fd value.
  17. // Panic if fds contains an existing stack for the fd.
  18. old, exist := fds.LoadOrStore(value, callersFrames())
  19. if exist {
  20. f := old.(*runtime.Frames)
  21. panic(fmt.Sprintf("found existing stack for fd %d:\n%s", value, FormatFrames(f)))
  22. }
  23. }
  24. fd := &FD{value}
  25. runtime.SetFinalizer(fd, (*FD).finalize)
  26. return fd
  27. }
  28. // finalize is set as the FD's runtime finalizer and
  29. // sends a leak trace before calling FD.Close().
  30. func (fd *FD) finalize() {
  31. if fd.raw < 0 {
  32. return
  33. }
  34. // Invoke the fd leak callback. Calls LoadAndDelete to guarantee the callback
  35. // is invoked at most once for one sys.FD allocation, runtime.Frames can only
  36. // be unwound once.
  37. f, ok := fds.LoadAndDelete(fd.Int())
  38. if ok && onLeakFD != nil {
  39. onLeakFD(f.(*runtime.Frames))
  40. }
  41. _ = fd.Close()
  42. }
  43. // NewFD wraps a raw fd with a finalizer.
  44. //
  45. // You must not use the raw fd after calling this function, since the underlying
  46. // file descriptor number may change. This is because the BPF UAPI assumes that
  47. // zero is not a valid fd value.
  48. func NewFD(value int) (*FD, error) {
  49. if value < 0 {
  50. return nil, fmt.Errorf("invalid fd %d", value)
  51. }
  52. fd := newFD(value)
  53. if value != 0 {
  54. return fd, nil
  55. }
  56. dup, err := fd.Dup()
  57. _ = fd.Close()
  58. return dup, err
  59. }
  60. func (fd *FD) String() string {
  61. return strconv.FormatInt(int64(fd.raw), 10)
  62. }
  63. func (fd *FD) Int() int {
  64. return fd.raw
  65. }
  66. func (fd *FD) Uint() uint32 {
  67. if fd.raw < 0 || int64(fd.raw) > math.MaxUint32 {
  68. // Best effort: this is the number most likely to be an invalid file
  69. // descriptor. It is equal to -1 (on two's complement arches).
  70. return math.MaxUint32
  71. }
  72. return uint32(fd.raw)
  73. }
  74. func (fd *FD) Close() error {
  75. if fd.raw < 0 {
  76. return nil
  77. }
  78. return unix.Close(fd.disown())
  79. }
  80. func (fd *FD) disown() int {
  81. value := int(fd.raw)
  82. fds.Delete(int(value))
  83. fd.raw = -1
  84. runtime.SetFinalizer(fd, nil)
  85. return value
  86. }
  87. func (fd *FD) Dup() (*FD, error) {
  88. if fd.raw < 0 {
  89. return nil, ErrClosedFd
  90. }
  91. // Always require the fd to be larger than zero: the BPF API treats the value
  92. // as "no argument provided".
  93. dup, err := unix.FcntlInt(uintptr(fd.raw), unix.F_DUPFD_CLOEXEC, 1)
  94. if err != nil {
  95. return nil, fmt.Errorf("can't dup fd: %v", err)
  96. }
  97. return newFD(dup), nil
  98. }
  99. // File takes ownership of FD and turns it into an [*os.File].
  100. //
  101. // You must not use the FD after the call returns.
  102. //
  103. // Returns nil if the FD is not valid.
  104. func (fd *FD) File(name string) *os.File {
  105. if fd.raw < 0 {
  106. return nil
  107. }
  108. return os.NewFile(uintptr(fd.disown()), name)
  109. }