Skip to content

Commit

Permalink
syscall: make origRlimitNofile atomic.Pointer[Rlimit]
Browse files Browse the repository at this point in the history
Currently we are bootstrapping with Go 1.20, origRlimitNofile can
be changed to atomic.Pointer[Rlimit].

Change-Id: I00ce9d1a9030bd5dbd34e3dc6c4e38683a87be86
GitHub-Last-Rev: f2ccdb3
GitHub-Pull-Request: #63274
Reviewed-on: https://go-review.googlesource.com/c/go/+/531516
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: qiulaidongfeng <2645477756@qq.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
  • Loading branch information
callthingsoff authored and gopherbot committed Sep 29, 2023
1 parent 9bfaaa1 commit fa4f951
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 28 deletions.
6 changes: 3 additions & 3 deletions src/syscall/exec_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
ngroups, groups uintptr
)

rlim, rlimOK := origRlimitNofile.Load().(Rlimit)
rlim := origRlimitNofile.Load()

// guard against side effects of shuffling fds below.
// Make sure that nextfd is beyond any currently open files so
Expand Down Expand Up @@ -276,8 +276,8 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
}

// Restore original rlimit.
if rlimOK && rlim.Cur != 0 {
RawSyscall(SYS_SETRLIMIT, uintptr(RLIMIT_NOFILE), uintptr(unsafe.Pointer(&rlim)), 0)
if rlim != nil {
RawSyscall(SYS_SETRLIMIT, uintptr(RLIMIT_NOFILE), uintptr(unsafe.Pointer(rlim)), 0)
}

// Time to exec.
Expand Down
6 changes: 3 additions & 3 deletions src/syscall/exec_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
upid uintptr
)

rlim, rlimOK := origRlimitNofile.Load().(Rlimit)
rlim := origRlimitNofile.Load()

// Record parent PID so child can test if it has died.
ppid, _, _ := RawSyscall(SYS_GETPID, 0, 0, 0)
Expand Down Expand Up @@ -300,8 +300,8 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
}

// Restore original rlimit.
if rlimOK && rlim.Cur != 0 {
RawSyscall(SYS_SETRLIMIT, uintptr(RLIMIT_NOFILE), uintptr(unsafe.Pointer(&rlim)), 0)
if rlim != nil {
RawSyscall(SYS_SETRLIMIT, uintptr(RLIMIT_NOFILE), uintptr(unsafe.Pointer(rlim)), 0)
}

// Time to exec.
Expand Down
6 changes: 3 additions & 3 deletions src/syscall/exec_libc.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
ngroups, groups uintptr
)

rlim, rlimOK := origRlimitNofile.Load().(Rlimit)
rlim := origRlimitNofile.Load()

// guard against side effects of shuffling fds below.
// Make sure that nextfd is beyond any currently open files so
Expand Down Expand Up @@ -296,8 +296,8 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
}

// Restore original rlimit.
if rlimOK && rlim.Cur != 0 {
setrlimit1(RLIMIT_NOFILE, unsafe.Pointer(&rlim))
if rlim != nil {
setrlimit1(RLIMIT_NOFILE, unsafe.Pointer(rlim))
}

// Time to exec.
Expand Down
6 changes: 3 additions & 3 deletions src/syscall/exec_libc2.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
ngroups, groups uintptr
)

rlim, rlimOK := origRlimitNofile.Load().(Rlimit)
rlim := origRlimitNofile.Load()

// guard against side effects of shuffling fds below.
// Make sure that nextfd is beyond any currently open files so
Expand Down Expand Up @@ -272,8 +272,8 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
}

// Restore original rlimit.
if rlimOK && rlim.Cur != 0 {
rawSyscall(abi.FuncPCABI0(libc_setrlimit_trampoline), uintptr(RLIMIT_NOFILE), uintptr(unsafe.Pointer(&rlim)), 0)
if rlim != nil {
rawSyscall(abi.FuncPCABI0(libc_setrlimit_trampoline), uintptr(RLIMIT_NOFILE), uintptr(unsafe.Pointer(rlim)), 0)
}

// Time to exec.
Expand Down
6 changes: 3 additions & 3 deletions src/syscall/exec_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ func forkAndExecInChild1(argv0 *byte, argv, envv []*byte, chroot, dir *byte, att
c uintptr
)

rlim, rlimOK := origRlimitNofile.Load().(Rlimit)
rlim := origRlimitNofile.Load()

if sys.UidMappings != nil {
puid = []byte("/proc/self/uid_map\000")
Expand Down Expand Up @@ -628,8 +628,8 @@ func forkAndExecInChild1(argv0 *byte, argv, envv []*byte, chroot, dir *byte, att
}

// Restore original rlimit.
if rlimOK && rlim.Cur != 0 {
rawSetrlimit(RLIMIT_NOFILE, &rlim)
if rlim != nil {
rawSetrlimit(RLIMIT_NOFILE, rlim)
}

// Enable tracing if requested.
Expand Down
6 changes: 3 additions & 3 deletions src/syscall/exec_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,9 @@ func Exec(argv0 string, argv []string, envv []string) (err error) {
}
runtime_BeforeExec()

rlim, rlimOK := origRlimitNofile.Load().(Rlimit)
if rlimOK && rlim.Cur != 0 {
Setrlimit(RLIMIT_NOFILE, &rlim)
rlim := origRlimitNofile.Load()
if rlim != nil {
Setrlimit(RLIMIT_NOFILE, rlim)
}

var err1 error
Expand Down
4 changes: 2 additions & 2 deletions src/syscall/export_rlimit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
package syscall

func OrigRlimitNofile() Rlimit {
if rlim, ok := origRlimitNofile.Load().(Rlimit); ok {
return rlim
if rlim := origRlimitNofile.Load(); rlim != nil {
return *rlim
}
return Rlimit{0, 0}
}
12 changes: 5 additions & 7 deletions src/syscall/rlimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ import (
"sync/atomic"
)

// origRlimitNofile, if not {0, 0}, is the original soft RLIMIT_NOFILE.
// When we can assume that we are bootstrapping with Go 1.19,
// this can be atomic.Pointer[Rlimit].
var origRlimitNofile atomic.Value // of Rlimit
// origRlimitNofile, if non-nil, is the original soft RLIMIT_NOFILE.
var origRlimitNofile atomic.Pointer[Rlimit]

// 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
Expand All @@ -32,7 +30,7 @@ var origRlimitNofile atomic.Value // of Rlimit
func init() {
var lim Rlimit
if err := Getrlimit(RLIMIT_NOFILE, &lim); err == nil && lim.Cur != lim.Max {
origRlimitNofile.Store(lim)
origRlimitNofile.Store(&lim)
lim.Cur = lim.Max
adjustFileLimit(&lim)
setrlimit(RLIMIT_NOFILE, &lim)
Expand All @@ -42,9 +40,9 @@ func init() {
func Setrlimit(resource int, rlim *Rlimit) error {
err := setrlimit(resource, rlim)
if err == nil && resource == RLIMIT_NOFILE {
// Store zeroes in origRlimitNofile to tell StartProcess
// Store nil in origRlimitNofile to tell StartProcess
// to not adjust the rlimit in the child process.
origRlimitNofile.Store(Rlimit{0, 0})
origRlimitNofile.Store(nil)
}
return err
}
2 changes: 1 addition & 1 deletion src/syscall/syscall_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,7 @@ func Munmap(b []byte) (err error) {
func prlimit(pid int, resource int, newlimit *Rlimit, old *Rlimit) (err error) {
err = prlimit1(pid, resource, newlimit, old)
if err == nil && newlimit != nil && resource == RLIMIT_NOFILE {
origRlimitNofile.Store(Rlimit{0, 0})
origRlimitNofile.Store(nil)
}
return err
}

0 comments on commit fa4f951

Please sign in to comment.