unix: cap RLIMIT_NOFILE soft limit in TestRlimit on darwin

On some machines, kern.maxfilesperproc is 4096. If Rlimit.Cur is larger
than that, Setrlimit will get an errEINVAL.

Same as CL 246658 did in package syscall.

Updates golang/go#40564

Change-Id: I8c45a23352fa2039772e04205680640e8a0e1840
Reviewed-on: https://go-review.googlesource.com/c/sys/+/250000
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matt Layher <mdlayher@gmail.com>
This commit is contained in:
Tobias Klauser
2020-08-24 13:15:25 +00:00
committed by Tobias Klauser
parent 2bddbd2f0c
commit c12d262b63
+8 -5
View File
@@ -377,11 +377,11 @@ func TestRlimit(t *testing.T) {
} }
set := rlimit set := rlimit
set.Cur = set.Max - 1 set.Cur = set.Max - 1
if runtime.GOOS == "darwin" && set.Cur > 10240 { if runtime.GOOS == "darwin" && set.Cur > 4096 {
// The max file limit is 10240, even though // rlim_min for RLIMIT_NOFILE should be equal to
// the max returned by Getrlimit is 1<<63-1. // or lower than kern.maxfilesperproc, which on
// This is OPEN_MAX in sys/syslimits.h. // some machines are 4096. See #40564.
set.Cur = 10240 set.Cur = 4096
} }
err = unix.Setrlimit(unix.RLIMIT_NOFILE, &set) err = unix.Setrlimit(unix.RLIMIT_NOFILE, &set)
if err != nil { if err != nil {
@@ -394,6 +394,9 @@ func TestRlimit(t *testing.T) {
} }
set = rlimit set = rlimit
set.Cur = set.Max - 1 set.Cur = set.Max - 1
if runtime.GOOS == "darwin" && set.Cur > 4096 {
set.Cur = 4096
}
if set != get { if set != get {
// Seems like Darwin requires some privilege to // Seems like Darwin requires some privilege to
// increase the soft limit of rlimit sandbox, though // increase the soft limit of rlimit sandbox, though