"internal/syscall/unix"
"internal/testenv"
"os"
+ "os/exec"
"syscall"
"testing"
)
t.Errorf("SendSignal: got %v, want %v", err, syscall.ESRCH)
}
}
+
+// Issue #69284
+func TestPidfdLeak(t *testing.T) {
+ testenv.MustHaveExec(t)
+ exe, err := os.Executable()
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ // Find the next 10 descriptors.
+ // We need to get more than one descriptor in practice;
+ // the pidfd winds up not being the next descriptor.
+ const count = 10
+ want := make([]int, count)
+ for i := range count {
+ var err error
+ want[i], err = syscall.Open(exe, syscall.O_RDONLY, 0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ }
+
+ // Close the descriptors.
+ for _, d := range want {
+ syscall.Close(d)
+ }
+
+ // Start a process 10 times.
+ for range 10 {
+ // For testing purposes this has to be an absolute path.
+ // Otherwise we will fail finding the executable
+ // and won't start a process at all.
+ cmd := exec.Command("/noSuchExecutable")
+ cmd.Run()
+ }
+
+ // Open the next 10 descriptors again.
+ got := make([]int, count)
+ for i := range count {
+ var err error
+ got[i], err = syscall.Open(exe, syscall.O_RDONLY, 0)
+ if err != nil {
+ t.Fatal(err)
+ }
+ }
+
+ // Close the descriptors
+ for _, d := range got {
+ syscall.Close(d)
+ }
+
+ t.Logf("got %v", got)
+ t.Logf("want %v", want)
+
+ // Allow some slack for runtime epoll descriptors and the like.
+ if got[count-1] > want[count-1]+5 {
+ t.Errorf("got descriptor %d, want %d", got[count-1], want[count-1])
+ }
+}
}
}
+// forkAndExecFailureCleanup cleans up after an exec failure.
+func forkAndExecFailureCleanup(attr *ProcAttr, sys *SysProcAttr) {
+ // Nothing to do.
+}
+
func ioctlPtr(fd, req uintptr, arg unsafe.Pointer) (err Errno) {
return ioctl(fd, req, uintptr(arg))
}