Skip to content

Commit d0baac3

Browse files
ianlancetaylorgopherbot
authored andcommitted
syscall: always use prlimit for getrlimit/setrlimit on Linux
Linux added the prlimit system call in version 2.6.36. As our minimum Linux kernel version is now 3.2, simplify the various getrlimit/setlrimit implementations to just always use prlimit. For #67001 Change-Id: I2512c21c947d0bc83f8f9077c143163fd8d83be3 Reviewed-on: https://go-review.googlesource.com/c/go/+/609178 Reviewed-by: Cherry Mui <[email protected]> Reviewed-by: Damien Neil <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Tobias Klauser <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent c209892 commit d0baac3

24 files changed

+12
-609
lines changed

src/syscall/exec_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ func forkAndExecInChild1(argv0 *byte, argv, envv []*byte, chroot, dir *byte, att
632632

633633
// Restore original rlimit.
634634
if rlim != nil {
635-
rawSetrlimit(RLIMIT_NOFILE, rlim)
635+
RawSyscall6(SYS_PRLIMIT64, 0, RLIMIT_NOFILE, uintptr(unsafe.Pointer(rlim)), 0, 0, 0)
636636
}
637637

638638
// Enable tracing if requested.

src/syscall/syscall_linux.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,17 @@ func Munmap(b []byte) (err error) {
12881288
//sys Mlockall(flags int) (err error)
12891289
//sys Munlockall() (err error)
12901290

1291+
func Getrlimit(resource int, rlim *Rlimit) (err error) {
1292+
// prlimit1 is the same as prlimit when newlimit == nil
1293+
return prlimit1(0, resource, nil, rlim)
1294+
}
1295+
1296+
// setrlimit sets a resource limit.
1297+
// The Setrlimit function is in rlimit.go, and calls this one.
1298+
func setrlimit(resource int, rlim *Rlimit) (err error) {
1299+
return prlimit(0, resource, rlim, nil)
1300+
}
1301+
12911302
// prlimit changes a resource limit. We use a single definition so that
12921303
// we can tell StartProcess to not restore the original NOFILE limit.
12931304
//

src/syscall/syscall_linux_386.go

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -72,96 +72,6 @@ func mmap(addr uintptr, length uintptr, prot int, flags int, fd int, offset int6
7272
return mmap2(addr, length, prot, flags, fd, page)
7373
}
7474

75-
type rlimit32 struct {
76-
Cur uint32
77-
Max uint32
78-
}
79-
80-
//sysnb getrlimit(resource int, rlim *rlimit32) (err error) = SYS_GETRLIMIT
81-
82-
const rlimInf32 = ^uint32(0)
83-
const rlimInf64 = ^uint64(0)
84-
85-
func Getrlimit(resource int, rlim *Rlimit) (err error) {
86-
err = prlimit(0, resource, nil, rlim)
87-
if err != ENOSYS {
88-
return err
89-
}
90-
91-
rl := rlimit32{}
92-
err = getrlimit(resource, &rl)
93-
if err != nil {
94-
return
95-
}
96-
97-
if rl.Cur == rlimInf32 {
98-
rlim.Cur = rlimInf64
99-
} else {
100-
rlim.Cur = uint64(rl.Cur)
101-
}
102-
103-
if rl.Max == rlimInf32 {
104-
rlim.Max = rlimInf64
105-
} else {
106-
rlim.Max = uint64(rl.Max)
107-
}
108-
return
109-
}
110-
111-
//sysnb setrlimit1(resource int, rlim *rlimit32) (err error) = SYS_SETRLIMIT
112-
113-
func setrlimit(resource int, rlim *Rlimit) (err error) {
114-
err = prlimit(0, resource, rlim, nil)
115-
if err != ENOSYS {
116-
return err
117-
}
118-
119-
rl := rlimit32{}
120-
if rlim.Cur == rlimInf64 {
121-
rl.Cur = rlimInf32
122-
} else if rlim.Cur < uint64(rlimInf32) {
123-
rl.Cur = uint32(rlim.Cur)
124-
} else {
125-
return EINVAL
126-
}
127-
if rlim.Max == rlimInf64 {
128-
rl.Max = rlimInf32
129-
} else if rlim.Max < uint64(rlimInf32) {
130-
rl.Max = uint32(rlim.Max)
131-
} else {
132-
return EINVAL
133-
}
134-
135-
return setrlimit1(resource, &rl)
136-
}
137-
138-
//go:nosplit
139-
func rawSetrlimit(resource int, rlim *Rlimit) Errno {
140-
_, _, errno := RawSyscall6(SYS_PRLIMIT64, 0, uintptr(resource), uintptr(unsafe.Pointer(rlim)), 0, 0, 0)
141-
if errno != ENOSYS {
142-
return errno
143-
}
144-
145-
rl := rlimit32{}
146-
if rlim.Cur == rlimInf64 {
147-
rl.Cur = rlimInf32
148-
} else if rlim.Cur < uint64(rlimInf32) {
149-
rl.Cur = uint32(rlim.Cur)
150-
} else {
151-
return EINVAL
152-
}
153-
if rlim.Max == rlimInf64 {
154-
rl.Max = rlimInf32
155-
} else if rlim.Max < uint64(rlimInf32) {
156-
rl.Max = uint32(rlim.Max)
157-
} else {
158-
return EINVAL
159-
}
160-
161-
_, _, errno = RawSyscall(SYS_SETRLIMIT, uintptr(resource), uintptr(unsafe.Pointer(rlim)), 0)
162-
return errno
163-
}
164-
16575
// Underlying system call writes to newoffset via pointer.
16676
// Implemented in assembly to avoid allocation.
16777
func seek(fd int, offset int64, whence int) (newoffset int64, err Errno)

src/syscall/syscall_linux_amd64.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44

55
package syscall
66

7-
import (
8-
"unsafe"
9-
)
10-
117
const (
128
_SYS_setgroups = SYS_SETGROUPS
139
_SYS_clone3 = 435
@@ -23,7 +19,6 @@ const (
2319
//sysnb Getegid() (egid int)
2420
//sysnb Geteuid() (euid int)
2521
//sysnb Getgid() (gid int)
26-
//sysnb Getrlimit(resource int, rlim *Rlimit) (err error)
2722
//sysnb Getuid() (uid int)
2823
//sysnb InotifyInit() (fd int, err error)
2924
//sys Ioperm(from int, num int, on int) (err error)
@@ -38,7 +33,6 @@ const (
3833
//sys sendfile(outfd int, infd int, offset *int64, count int) (written int, err error)
3934
//sys Setfsgid(gid int) (err error)
4035
//sys Setfsuid(uid int) (err error)
41-
//sysnb setrlimit(resource int, rlim *Rlimit) (err error) = SYS_SETRLIMIT
4236
//sys Shutdown(fd int, how int) (err error)
4337
//sys Splice(rfd int, roff *int64, wfd int, woff *int64, len int, flags int) (n int64, err error)
4438
//sys Statfs(path string, buf *Statfs_t) (err error)
@@ -103,12 +97,6 @@ func Time(t *Time_t) (tt Time_t, err error) {
10397
//sys Utime(path string, buf *Utimbuf) (err error)
10498
//sys utimes(path string, times *[2]Timeval) (err error)
10599

106-
//go:nosplit
107-
func rawSetrlimit(resource int, rlim *Rlimit) Errno {
108-
_, _, errno := RawSyscall(SYS_SETRLIMIT, uintptr(resource), uintptr(unsafe.Pointer(rlim)), 0)
109-
return errno
110-
}
111-
112100
func setTimespec(sec, nsec int64) Timespec {
113101
return Timespec{Sec: sec, Nsec: nsec}
114102
}

src/syscall/syscall_linux_arm.go

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -124,96 +124,6 @@ func mmap(addr uintptr, length uintptr, prot int, flags int, fd int, offset int6
124124
return mmap2(addr, length, prot, flags, fd, page)
125125
}
126126

127-
type rlimit32 struct {
128-
Cur uint32
129-
Max uint32
130-
}
131-
132-
//sysnb getrlimit(resource int, rlim *rlimit32) (err error) = SYS_GETRLIMIT
133-
134-
const rlimInf32 = ^uint32(0)
135-
const rlimInf64 = ^uint64(0)
136-
137-
func Getrlimit(resource int, rlim *Rlimit) (err error) {
138-
err = prlimit(0, resource, nil, rlim)
139-
if err != ENOSYS {
140-
return err
141-
}
142-
143-
rl := rlimit32{}
144-
err = getrlimit(resource, &rl)
145-
if err != nil {
146-
return
147-
}
148-
149-
if rl.Cur == rlimInf32 {
150-
rlim.Cur = rlimInf64
151-
} else {
152-
rlim.Cur = uint64(rl.Cur)
153-
}
154-
155-
if rl.Max == rlimInf32 {
156-
rlim.Max = rlimInf64
157-
} else {
158-
rlim.Max = uint64(rl.Max)
159-
}
160-
return
161-
}
162-
163-
//sysnb setrlimit1(resource int, rlim *rlimit32) (err error) = SYS_SETRLIMIT
164-
165-
func setrlimit(resource int, rlim *Rlimit) (err error) {
166-
err = prlimit(0, resource, rlim, nil)
167-
if err != ENOSYS {
168-
return err
169-
}
170-
171-
rl := rlimit32{}
172-
if rlim.Cur == rlimInf64 {
173-
rl.Cur = rlimInf32
174-
} else if rlim.Cur < uint64(rlimInf32) {
175-
rl.Cur = uint32(rlim.Cur)
176-
} else {
177-
return EINVAL
178-
}
179-
if rlim.Max == rlimInf64 {
180-
rl.Max = rlimInf32
181-
} else if rlim.Max < uint64(rlimInf32) {
182-
rl.Max = uint32(rlim.Max)
183-
} else {
184-
return EINVAL
185-
}
186-
187-
return setrlimit1(resource, &rl)
188-
}
189-
190-
//go:nosplit
191-
func rawSetrlimit(resource int, rlim *Rlimit) Errno {
192-
_, _, errno := RawSyscall6(SYS_PRLIMIT64, 0, uintptr(resource), uintptr(unsafe.Pointer(rlim)), 0, 0, 0)
193-
if errno != ENOSYS {
194-
return errno
195-
}
196-
197-
rl := rlimit32{}
198-
if rlim.Cur == rlimInf64 {
199-
rl.Cur = rlimInf32
200-
} else if rlim.Cur < uint64(rlimInf32) {
201-
rl.Cur = uint32(rlim.Cur)
202-
} else {
203-
return EINVAL
204-
}
205-
if rlim.Max == rlimInf64 {
206-
rl.Max = rlimInf32
207-
} else if rlim.Max < uint64(rlimInf32) {
208-
rl.Max = uint32(rlim.Max)
209-
} else {
210-
return EINVAL
211-
}
212-
213-
_, _, errno = RawSyscall(SYS_SETRLIMIT, uintptr(resource), uintptr(unsafe.Pointer(rlim)), 0)
214-
return errno
215-
}
216-
217127
func (r *PtraceRegs) PC() uint64 { return uint64(r.Uregs[15]) }
218128

219129
func (r *PtraceRegs) SetPC(pc uint64) { r.Uregs[15] = uint32(pc) }

src/syscall/syscall_linux_arm64.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ func Fstatat(fd int, path string, stat *Stat_t, flags int) error {
2727
//sysnb Getegid() (egid int)
2828
//sysnb Geteuid() (euid int)
2929
//sysnb Getgid() (gid int)
30-
//sysnb getrlimit(resource int, rlim *Rlimit) (err error)
3130
//sysnb Getuid() (uid int)
3231
//sys Listen(s int, n int) (err error)
3332
//sys pread(fd int, p []byte, offset int64) (n int, err error) = SYS_PREAD64
@@ -37,7 +36,6 @@ func Fstatat(fd int, path string, stat *Stat_t, flags int) error {
3736
//sys sendfile(outfd int, infd int, offset *int64, count int) (written int, err error)
3837
//sys Setfsgid(gid int) (err error)
3938
//sys Setfsuid(uid int) (err error)
40-
//sysnb setrlimit1(resource int, rlim *Rlimit) (err error) = SYS_SETRLIMIT
4139
//sys Shutdown(fd int, how int) (err error)
4240
//sys Splice(rfd int, roff *int64, wfd int, woff *int64, len int, flags int) (n int64, err error)
4341

@@ -140,34 +138,6 @@ func utimes(path string, tv *[2]Timeval) (err error) {
140138
return utimensat(_AT_FDCWD, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), 0)
141139
}
142140

143-
// Getrlimit prefers the prlimit64 system call. See issue 38604.
144-
func Getrlimit(resource int, rlim *Rlimit) error {
145-
err := prlimit(0, resource, nil, rlim)
146-
if err != ENOSYS {
147-
return err
148-
}
149-
return getrlimit(resource, rlim)
150-
}
151-
152-
// setrlimit prefers the prlimit64 system call. See issue 38604.
153-
func setrlimit(resource int, rlim *Rlimit) error {
154-
err := prlimit(0, resource, rlim, nil)
155-
if err != ENOSYS {
156-
return err
157-
}
158-
return setrlimit1(resource, rlim)
159-
}
160-
161-
//go:nosplit
162-
func rawSetrlimit(resource int, rlim *Rlimit) Errno {
163-
_, _, errno := RawSyscall6(SYS_PRLIMIT64, 0, uintptr(resource), uintptr(unsafe.Pointer(rlim)), 0, 0, 0)
164-
if errno != ENOSYS {
165-
return errno
166-
}
167-
_, _, errno = RawSyscall(SYS_SETRLIMIT, uintptr(resource), uintptr(unsafe.Pointer(rlim)), 0)
168-
return errno
169-
}
170-
171141
func (r *PtraceRegs) PC() uint64 { return r.Pc }
172142

173143
func (r *PtraceRegs) SetPC(pc uint64) { r.Pc = pc }

src/syscall/syscall_linux_loong64.go

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -183,22 +183,6 @@ func utimes(path string, tv *[2]Timeval) (err error) {
183183
return utimensat(_AT_FDCWD, path, (*[2]Timespec)(unsafe.Pointer(&ts[0])), 0)
184184
}
185185

186-
// Getrlimit prefers the prlimit64 system call.
187-
func Getrlimit(resource int, rlim *Rlimit) error {
188-
return prlimit(0, resource, nil, rlim)
189-
}
190-
191-
// setrlimit prefers the prlimit64 system call.
192-
func setrlimit(resource int, rlim *Rlimit) error {
193-
return prlimit(0, resource, rlim, nil)
194-
}
195-
196-
//go:nosplit
197-
func rawSetrlimit(resource int, rlim *Rlimit) Errno {
198-
_, _, errno := RawSyscall6(SYS_PRLIMIT64, 0, uintptr(resource), uintptr(unsafe.Pointer(rlim)), 0, 0, 0)
199-
return errno
200-
}
201-
202186
func (r *PtraceRegs) GetEra() uint64 { return r.Era }
203187

204188
func (r *PtraceRegs) SetEra(era uint64) { r.Era = era }

src/syscall/syscall_linux_mips64x.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@
66

77
package syscall
88

9-
import (
10-
"unsafe"
11-
)
12-
139
const (
1410
_SYS_setgroups = SYS_SETGROUPS
1511
_SYS_clone3 = 5435
@@ -25,7 +21,6 @@ const (
2521
//sysnb Getegid() (egid int)
2622
//sysnb Geteuid() (euid int)
2723
//sysnb Getgid() (gid int)
28-
//sysnb Getrlimit(resource int, rlim *Rlimit) (err error)
2924
//sysnb Getuid() (uid int)
3025
//sysnb InotifyInit() (fd int, err error)
3126
//sys Lchown(path string, uid int, gid int) (err error)
@@ -38,7 +33,6 @@ const (
3833
//sys sendfile(outfd int, infd int, offset *int64, count int) (written int, err error)
3934
//sys Setfsgid(gid int) (err error)
4035
//sys Setfsuid(uid int) (err error)
41-
//sysnb setrlimit(resource int, rlim *Rlimit) (err error) = SYS_SETRLIMIT
4236
//sys Shutdown(fd int, how int) (err error)
4337
//sys Splice(rfd int, roff *int64, wfd int, woff *int64, len int, flags int) (n int64, err error)
4438
//sys Statfs(path string, buf *Statfs_t) (err error)
@@ -94,12 +88,6 @@ func Time(t *Time_t) (tt Time_t, err error) {
9488
//sys Utime(path string, buf *Utimbuf) (err error)
9589
//sys utimes(path string, times *[2]Timeval) (err error)
9690

97-
//go:nosplit
98-
func rawSetrlimit(resource int, rlim *Rlimit) Errno {
99-
_, _, errno := RawSyscall(SYS_SETRLIMIT, uintptr(resource), uintptr(unsafe.Pointer(rlim)), 0)
100-
return errno
101-
}
102-
10391
func setTimespec(sec, nsec int64) Timespec {
10492
return Timespec{Sec: sec, Nsec: nsec}
10593
}

0 commit comments

Comments
 (0)