1
0
mirror of https://github.com/golang/go synced 2024-11-17 13:04:54 -07:00

os, syscall: move rlimit code to syscall

In CL 393354 the os package was changed to raise the open file rlimit
at program start. That code is not inherently tied to the os package.
This CL moves it into the syscall package.

This is in preparation for future changes to restore the original
soft rlimit when exec'ing a new program.

For #46279

Change-Id: I981401b0345d017fd39fdd3dfbb58069be36c272
Reviewed-on: https://go-review.googlesource.com/c/go/+/476096
Reviewed-by: Cherry Mui <cherryyz@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Auto-Submit: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
Ian Lance Taylor 2023-03-13 15:20:11 -07:00 committed by Gopher Robot
parent 1d06667bc4
commit 491153a71a
4 changed files with 13 additions and 19 deletions

View File

@ -4,9 +4,7 @@
//go:build unix
package os
import "syscall"
package syscall
// Some systems set an artificially low soft limit on open file count, for compatibility
// with code that uses select and its hard-coded maximum file descriptor
@ -23,10 +21,10 @@ import "syscall"
// Code that really wants Go to leave the limit alone can set the hard limit,
// which Go of course has no choice but to respect.
func init() {
var lim syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim); err == nil && lim.Cur != lim.Max {
var lim Rlimit
if err := Getrlimit(RLIMIT_NOFILE, &lim); err == nil && lim.Cur != lim.Max {
lim.Cur = lim.Max
adjustFileLimit(&lim)
syscall.Setrlimit(syscall.RLIMIT_NOFILE, &lim)
Setrlimit(RLIMIT_NOFILE, &lim)
}
}

View File

@ -4,15 +4,13 @@
//go:build darwin
package os
import "syscall"
package syscall
// adjustFileLimit adds per-OS limitations on the Rlimit used for RLIMIT_NOFILE. See rlimit.go.
func adjustFileLimit(lim *syscall.Rlimit) {
func adjustFileLimit(lim *Rlimit) {
// On older macOS, setrlimit(RLIMIT_NOFILE, lim) with lim.Cur = infinity fails.
// Set to the value of kern.maxfilesperproc instead.
n, err := syscall.SysctlUint32("kern.maxfilesperproc")
n, err := SysctlUint32("kern.maxfilesperproc")
if err != nil {
return
}

View File

@ -4,9 +4,7 @@
//go:build aix || dragonfly || freebsd || linux || netbsd || openbsd || solaris
package os
import "syscall"
package syscall
// adjustFileLimit adds per-OS limitations on the Rlimit used for RLIMIT_NOFILE. See rlimit.go.
func adjustFileLimit(lim *syscall.Rlimit) {}
func adjustFileLimit(lim *Rlimit) {}

View File

@ -2,10 +2,10 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package os_test
package syscall_test
import (
. "os"
"os"
"runtime"
"testing"
)
@ -24,9 +24,9 @@ func TestOpenFileLimit(t *testing.T) {
fileCount = 768
}
var files []*File
var files []*os.File
for i := 0; i < fileCount; i++ {
f, err := Open("rlimit.go")
f, err := os.Open("rlimit.go")
if err != nil {
t.Error(err)
break