Skip to content

Commit fa4f951

Browse files
callthingsoffgopherbot
authored andcommitted
syscall: make origRlimitNofile atomic.Pointer[Rlimit]
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>
1 parent 9bfaaa1 commit fa4f951

9 files changed

+26
-28
lines changed

src/syscall/exec_bsd.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
6464
ngroups, groups uintptr
6565
)
6666

67-
rlim, rlimOK := origRlimitNofile.Load().(Rlimit)
67+
rlim := origRlimitNofile.Load()
6868

6969
// guard against side effects of shuffling fds below.
7070
// 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
276276
}
277277

278278
// Restore original rlimit.
279-
if rlimOK && rlim.Cur != 0 {
280-
RawSyscall(SYS_SETRLIMIT, uintptr(RLIMIT_NOFILE), uintptr(unsafe.Pointer(&rlim)), 0)
279+
if rlim != nil {
280+
RawSyscall(SYS_SETRLIMIT, uintptr(RLIMIT_NOFILE), uintptr(unsafe.Pointer(rlim)), 0)
281281
}
282282

283283
// Time to exec.

src/syscall/exec_freebsd.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
7171
upid uintptr
7272
)
7373

74-
rlim, rlimOK := origRlimitNofile.Load().(Rlimit)
74+
rlim := origRlimitNofile.Load()
7575

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

302302
// Restore original rlimit.
303-
if rlimOK && rlim.Cur != 0 {
304-
RawSyscall(SYS_SETRLIMIT, uintptr(RLIMIT_NOFILE), uintptr(unsafe.Pointer(&rlim)), 0)
303+
if rlim != nil {
304+
RawSyscall(SYS_SETRLIMIT, uintptr(RLIMIT_NOFILE), uintptr(unsafe.Pointer(rlim)), 0)
305305
}
306306

307307
// Time to exec.

src/syscall/exec_libc.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
9191
ngroups, groups uintptr
9292
)
9393

94-
rlim, rlimOK := origRlimitNofile.Load().(Rlimit)
94+
rlim := origRlimitNofile.Load()
9595

9696
// guard against side effects of shuffling fds below.
9797
// 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
296296
}
297297

298298
// Restore original rlimit.
299-
if rlimOK && rlim.Cur != 0 {
300-
setrlimit1(RLIMIT_NOFILE, unsafe.Pointer(&rlim))
299+
if rlim != nil {
300+
setrlimit1(RLIMIT_NOFILE, unsafe.Pointer(rlim))
301301
}
302302

303303
// Time to exec.

src/syscall/exec_libc2.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr
6565
ngroups, groups uintptr
6666
)
6767

68-
rlim, rlimOK := origRlimitNofile.Load().(Rlimit)
68+
rlim := origRlimitNofile.Load()
6969

7070
// guard against side effects of shuffling fds below.
7171
// 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
272272
}
273273

274274
// Restore original rlimit.
275-
if rlimOK && rlim.Cur != 0 {
276-
rawSyscall(abi.FuncPCABI0(libc_setrlimit_trampoline), uintptr(RLIMIT_NOFILE), uintptr(unsafe.Pointer(&rlim)), 0)
275+
if rlim != nil {
276+
rawSyscall(abi.FuncPCABI0(libc_setrlimit_trampoline), uintptr(RLIMIT_NOFILE), uintptr(unsafe.Pointer(rlim)), 0)
277277
}
278278

279279
// Time to exec.

src/syscall/exec_linux.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func forkAndExecInChild1(argv0 *byte, argv, envv []*byte, chroot, dir *byte, att
248248
c uintptr
249249
)
250250

251-
rlim, rlimOK := origRlimitNofile.Load().(Rlimit)
251+
rlim := origRlimitNofile.Load()
252252

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

630630
// Restore original rlimit.
631-
if rlimOK && rlim.Cur != 0 {
632-
rawSetrlimit(RLIMIT_NOFILE, &rlim)
631+
if rlim != nil {
632+
rawSetrlimit(RLIMIT_NOFILE, rlim)
633633
}
634634

635635
// Enable tracing if requested.

src/syscall/exec_unix.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,9 @@ func Exec(argv0 string, argv []string, envv []string) (err error) {
281281
}
282282
runtime_BeforeExec()
283283

284-
rlim, rlimOK := origRlimitNofile.Load().(Rlimit)
285-
if rlimOK && rlim.Cur != 0 {
286-
Setrlimit(RLIMIT_NOFILE, &rlim)
284+
rlim := origRlimitNofile.Load()
285+
if rlim != nil {
286+
Setrlimit(RLIMIT_NOFILE, rlim)
287287
}
288288

289289
var err1 error

src/syscall/export_rlimit_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
package syscall
88

99
func OrigRlimitNofile() Rlimit {
10-
if rlim, ok := origRlimitNofile.Load().(Rlimit); ok {
11-
return rlim
10+
if rlim := origRlimitNofile.Load(); rlim != nil {
11+
return *rlim
1212
}
1313
return Rlimit{0, 0}
1414
}

src/syscall/rlimit.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ import (
1010
"sync/atomic"
1111
)
1212

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

1816
// Some systems set an artificially low soft limit on open file count, for compatibility
1917
// with code that uses select and its hard-coded maximum file descriptor
@@ -32,7 +30,7 @@ var origRlimitNofile atomic.Value // of Rlimit
3230
func init() {
3331
var lim Rlimit
3432
if err := Getrlimit(RLIMIT_NOFILE, &lim); err == nil && lim.Cur != lim.Max {
35-
origRlimitNofile.Store(lim)
33+
origRlimitNofile.Store(&lim)
3634
lim.Cur = lim.Max
3735
adjustFileLimit(&lim)
3836
setrlimit(RLIMIT_NOFILE, &lim)
@@ -42,9 +40,9 @@ func init() {
4240
func Setrlimit(resource int, rlim *Rlimit) error {
4341
err := setrlimit(resource, rlim)
4442
if err == nil && resource == RLIMIT_NOFILE {
45-
// Store zeroes in origRlimitNofile to tell StartProcess
43+
// Store nil in origRlimitNofile to tell StartProcess
4644
// to not adjust the rlimit in the child process.
47-
origRlimitNofile.Store(Rlimit{0, 0})
45+
origRlimitNofile.Store(nil)
4846
}
4947
return err
5048
}

src/syscall/syscall_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,7 @@ func Munmap(b []byte) (err error) {
12771277
func prlimit(pid int, resource int, newlimit *Rlimit, old *Rlimit) (err error) {
12781278
err = prlimit1(pid, resource, newlimit, old)
12791279
if err == nil && newlimit != nil && resource == RLIMIT_NOFILE {
1280-
origRlimitNofile.Store(Rlimit{0, 0})
1280+
origRlimitNofile.Store(nil)
12811281
}
12821282
return err
12831283
}

0 commit comments

Comments
 (0)