diff options
| author | Ian Lance Taylor <iant@golang.org> | 2023-03-13 20:59:14 -0700 |
|---|---|---|
| committer | Ian Lance Taylor <iant@google.com> | 2023-03-15 17:21:30 +0000 |
| commit | f5eef58e4381259cbd84b3f2074c79607fb5c821 (patch) | |
| tree | 1405152029a813b5e8efa81c1cd10212a6eb12a0 /src/syscall/exec_unix_test.go | |
| parent | 491153a71ab2bae3fe9a586489320573448511ab (diff) | |
| download | go-f5eef58e4381259cbd84b3f2074c79607fb5c821.tar.xz | |
syscall: restore original NOFILE rlimit in child process
If we increased the NOFILE rlimit when starting the program,
restore the original rlimit when forking a child process.
For #46279
Change-Id: Ia5d2af9ef435e5932965c15eec2e428d2130d230
Reviewed-on: https://go-review.googlesource.com/c/go/+/476097
Reviewed-by: Bryan Mills <bcmills@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
TryBot-Bypass: Ian Lance Taylor <iant@google.com>
Diffstat (limited to 'src/syscall/exec_unix_test.go')
| -rw-r--r-- | src/syscall/exec_unix_test.go | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/syscall/exec_unix_test.go b/src/syscall/exec_unix_test.go index 942a254cb9..2e5e3df374 100644 --- a/src/syscall/exec_unix_test.go +++ b/src/syscall/exec_unix_test.go @@ -7,12 +7,15 @@ package syscall_test import ( + "bytes" + "fmt" "internal/testenv" "io" "math/rand" "os" "os/exec" "os/signal" + "strconv" "syscall" "testing" "time" @@ -345,3 +348,42 @@ func TestExecHelper(t *testing.T) { t.Error("syscall.Exec returned") } + +// Test that rlimit values are restored by exec. +func TestRlimitRestored(t *testing.T) { + if os.Getenv("GO_WANT_HELPER_PROCESS") != "" { + fmt.Println(syscall.OrigRlimitNofile().Cur) + os.Exit(0) + } + + orig := syscall.OrigRlimitNofile() + if orig.Cur == 0 { + t.Skip("skipping test because rlimit not adjusted at startup") + } + + executable, err := os.Executable() + if err != nil { + executable = os.Args[0] + } + + cmd := testenv.Command(t, executable, "-test.run=TestRlimitRestored") + cmd = testenv.CleanCmdEnv(cmd) + cmd.Env = append(cmd.Env, "GO_WANT_HELPER_PROCESS=1") + + out, err := cmd.CombinedOutput() + if len(out) > 0 { + t.Logf("%s", out) + } + if err != nil { + t.Fatalf("subprocess failed: %v", err) + } + s := string(bytes.TrimSpace(out)) + v, err := strconv.ParseUint(s, 10, 64) + if err != nil { + t.Fatalf("could not parse %q as number: %v", s, v) + } + + if v != uint64(orig.Cur) { + t.Errorf("exec rlimit = %d, want %d", v, orig) + } +} |
