]> Cypherpunks repositories - gostls13.git/commitdiff
syscall: make origRlimitNofile atomic.Pointer[Rlimit]
authorJes Cok <xigua67damn@gmail.com>
Fri, 29 Sep 2023 06:06:40 +0000 (06:06 +0000)
committerGopher Robot <gobot@golang.org>
Fri, 29 Sep 2023 18:51:35 +0000 (18:51 +0000)
Currently we are bootstrapping with Go 1.20, origRlimitNofile can
be changed to atomic.Pointer[Rlimit].

Change-Id: I00ce9d1a9030bd5dbd34e3dc6c4e38683a87be86
GitHub-Last-Rev: f2ccdb38412019d10661ed6be42086b445e411bf
GitHub-Pull-Request: golang/go#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>
src/syscall/exec_bsd.go
src/syscall/exec_freebsd.go
src/syscall/exec_libc.go
src/syscall/exec_libc2.go
src/syscall/exec_linux.go
src/syscall/exec_unix.go
src/syscall/export_rlimit_test.go
src/syscall/rlimit.go
src/syscall/syscall_linux.go

index 0b0cd24e690c2e469163f99c1cabd894b622534a..149cc2f11c128c5c08a19af107a3428c43b4ceb2 100644 (file)
@@ -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
@@ -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.
index bd198d09b426c4a13b177eba568caa580fa96892..3226cb88cd999aabfd9efa8149309dff06b62ac6 100644 (file)
@@ -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)
@@ -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.
index 44557867eb7b6c83028247d5763c991b8e75f6e3..768e8c131c13231311629cb3c85e73ec9c1ddfe9 100644 (file)
@@ -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
@@ -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.
index 4fca701d6b3d8f9645a30f3643d2498ea2b9705e..7a6750084486cf7566120a2b08edd78a15112cc7 100644 (file)
@@ -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
@@ -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.
index ac06fbf8248ce26b6c49ca5c4205310afed1c6cf..e1c71b5a347f8f15e9ef41a00791110f9af80a0d 100644 (file)
@@ -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")
@@ -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.
index c6a6caaa70894606144bfecbe87293236ead1d29..469b6601982d739d076cb2136000c4286b065d23 100644 (file)
@@ -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
index 320e331c5882eaa15f61b9da33e6661dcdb8d745..25f0ef82c34a45ecba987a7fe0b5adce6071785c 100644 (file)
@@ -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}
 }
index cc7935d37be12a23429846ed5e3f5991092c9f95..fdc0d1bf1f17393d4703c6bc09e68f7d5939e20b 100644 (file)
@@ -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
@@ -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)
@@ -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
 }
index aa0cf111fefabbb4d9f5c5b0d267f5d890602218..ad72a1d018563c6d9e5a076b95858176fb78821c 100644 (file)
@@ -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
 }