| 1 |
// Copyright 2023 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package os_test |
| 6 |
|
| 7 |
import ( |
| 8 |
"errors" |
| 9 |
"internal/syscall/unix" |
| 10 |
"internal/testenv" |
| 11 |
"os" |
| 12 |
"os/exec" |
| 13 |
"syscall" |
| 14 |
"testing" |
| 15 |
) |
| 16 |
|
| 17 |
func TestFindProcessViaPidfd(t *testing.T) { |
| 18 |
testenv.MustHaveGoBuild(t) |
| 19 |
t.Parallel() |
| 20 |
|
| 21 |
if err := os.CheckPidfdOnce(); err != nil { |
| 22 |
// Non-pidfd code paths tested in exec_unix_test.go. |
| 23 |
t.Skipf("skipping: pidfd not available: %v", err) |
| 24 |
} |
| 25 |
|
| 26 |
p, err := os.StartProcess(testenv.GoToolPath(t), []string{"go"}, &os.ProcAttr{}) |
| 27 |
if err != nil { |
| 28 |
t.Fatalf("starting test process: %v", err) |
| 29 |
} |
| 30 |
p.Wait() |
| 31 |
|
| 32 |
// Use pid of a non-existing process. |
| 33 |
proc, err := os.FindProcess(p.Pid) |
| 34 |
// FindProcess should never return errors on Unix. |
| 35 |
if err != nil { |
| 36 |
t.Fatalf("FindProcess: got error %v, want <nil>", err) |
| 37 |
} |
| 38 |
// FindProcess should never return nil Process. |
| 39 |
if proc == nil { |
| 40 |
t.Fatal("FindProcess: got nil, want non-nil") |
| 41 |
} |
| 42 |
if proc.Status() != os.StatusDone { |
| 43 |
t.Fatalf("got process status: %v, want %d", proc.Status(), os.StatusDone) |
| 44 |
} |
| 45 |
|
| 46 |
// Check that all Process' public methods work as expected with |
| 47 |
// "done" Process. |
| 48 |
if err := proc.Kill(); err != os.ErrProcessDone { |
| 49 |
t.Errorf("Kill: got %v, want %v", err, os.ErrProcessDone) |
| 50 |
} |
| 51 |
if err := proc.Signal(os.Kill); err != os.ErrProcessDone { |
| 52 |
t.Errorf("Signal: got %v, want %v", err, os.ErrProcessDone) |
| 53 |
} |
| 54 |
if _, err := proc.Wait(); !errors.Is(err, syscall.ECHILD) { |
| 55 |
t.Errorf("Wait: got %v, want %v", err, os.ErrProcessDone) |
| 56 |
} |
| 57 |
// Release never returns errors on Unix. |
| 58 |
if err := proc.Release(); err != nil { |
| 59 |
t.Fatalf("Release: got %v, want <nil>", err) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
func TestStartProcessWithPidfd(t *testing.T) { |
| 64 |
testenv.MustHaveGoBuild(t) |
| 65 |
t.Parallel() |
| 66 |
|
| 67 |
if err := os.CheckPidfdOnce(); err != nil { |
| 68 |
// Non-pidfd code paths tested in exec_unix_test.go. |
| 69 |
t.Skipf("skipping: pidfd not available: %v", err) |
| 70 |
} |
| 71 |
|
| 72 |
var pidfd int |
| 73 |
p, err := os.StartProcess(testenv.GoToolPath(t), []string{"go"}, &os.ProcAttr{ |
| 74 |
Sys: &syscall.SysProcAttr{ |
| 75 |
PidFD: &pidfd, |
| 76 |
}, |
| 77 |
}) |
| 78 |
if err != nil { |
| 79 |
t.Fatalf("starting test process: %v", err) |
| 80 |
} |
| 81 |
defer syscall.Close(pidfd) |
| 82 |
|
| 83 |
if _, err := p.Wait(); err != nil { |
| 84 |
t.Fatalf("Wait: got %v, want <nil>", err) |
| 85 |
} |
| 86 |
|
| 87 |
// Check the pidfd is still valid |
| 88 |
err = unix.PidFDSendSignal(uintptr(pidfd), syscall.Signal(0)) |
| 89 |
if !errors.Is(err, syscall.ESRCH) { |
| 90 |
t.Errorf("SendSignal: got %v, want %v", err, syscall.ESRCH) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
// Issue #69284 |
| 95 |
func TestPidfdLeak(t *testing.T) { |
| 96 |
exe := testenv.Executable(t) |
| 97 |
|
| 98 |
// Find the next 10 descriptors. |
| 99 |
// We need to get more than one descriptor in practice; |
| 100 |
// the pidfd winds up not being the next descriptor. |
| 101 |
const count = 10 |
| 102 |
want := make([]int, count) |
| 103 |
for i := range count { |
| 104 |
var err error |
| 105 |
want[i], err = syscall.Open(exe, syscall.O_RDONLY, 0) |
| 106 |
if err != nil { |
| 107 |
t.Fatal(err) |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
// Close the descriptors. |
| 112 |
for _, d := range want { |
| 113 |
syscall.Close(d) |
| 114 |
} |
| 115 |
|
| 116 |
// Start a process 10 times. |
| 117 |
for range 10 { |
| 118 |
// For testing purposes this has to be an absolute path. |
| 119 |
// Otherwise we will fail finding the executable |
| 120 |
// and won't start a process at all. |
| 121 |
cmd := exec.Command("/noSuchExecutable") |
| 122 |
cmd.Run() |
| 123 |
} |
| 124 |
|
| 125 |
// Open the next 10 descriptors again. |
| 126 |
got := make([]int, count) |
| 127 |
for i := range count { |
| 128 |
var err error |
| 129 |
got[i], err = syscall.Open(exe, syscall.O_RDONLY, 0) |
| 130 |
if err != nil { |
| 131 |
t.Fatal(err) |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
// Close the descriptors |
| 136 |
for _, d := range got { |
| 137 |
syscall.Close(d) |
| 138 |
} |
| 139 |
|
| 140 |
t.Logf("got %v", got) |
| 141 |
t.Logf("want %v", want) |
| 142 |
|
| 143 |
// Allow some slack for runtime epoll descriptors and the like. |
| 144 |
if got[count-1] > want[count-1]+5 { |
| 145 |
t.Errorf("got descriptor %d, want %d", got[count-1], want[count-1]) |
| 146 |
} |
| 147 |
} |