Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syscall: don't change local limit if prlimit used for another process #67185

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/syscall/export_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
var (
RawSyscallNoError = rawSyscallNoError
ForceClone3 = &forceClone3
Prlimit = prlimit
)

const (
Expand Down
6 changes: 6 additions & 0 deletions src/syscall/export_rlimit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

package syscall

import "sync/atomic"

func OrigRlimitNofile() *Rlimit {
return origRlimitNofile.Load()
}

func GetInternalOrigRlimitNofile() *atomic.Pointer[Rlimit] {
return &origRlimitNofile
}
2 changes: 1 addition & 1 deletion src/syscall/syscall_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,7 @@ func Munmap(b []byte) (err error) {
// This is unexported but can be called from x/sys/unix.
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 {
if err == nil && newlimit != nil && resource == RLIMIT_NOFILE && (pid == 0 || pid == Getpid()) {
origRlimitNofile.Store(nil)
}
return err
Expand Down
66 changes: 66 additions & 0 deletions src/syscall/syscall_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,3 +654,69 @@ func TestAllThreadsSyscallBlockedSyscall(t *testing.T) {
wr.Close()
wg.Wait()
}

func TestPrlimitSelf(t *testing.T) {
origLimit := syscall.OrigRlimitNofile()
origRlimitNofile := syscall.GetInternalOrigRlimitNofile()

if origLimit == nil {
defer origRlimitNofile.Store(origLimit)
origRlimitNofile.Store(&syscall.Rlimit{
Cur: 1024,
Max: 65536,
})
}

// Get current process's nofile limit
var lim syscall.Rlimit
if err := syscall.Prlimit(0, syscall.RLIMIT_NOFILE, nil, &lim); err != nil {
t.Fatalf("Failed to get the current nofile limit: %v", err)
}
// Set current process's nofile limit through prlimit
if err := syscall.Prlimit(0, syscall.RLIMIT_NOFILE, &lim, nil); err != nil {
t.Fatalf("Prlimit self failed: %v", err)
}

rlimLater := origRlimitNofile.Load()
if rlimLater != nil {
t.Fatalf("origRlimitNofile got=%v, want=nil", rlimLater)
}
}

func TestPrlimitOtherProcess(t *testing.T) {
origLimit := syscall.OrigRlimitNofile()
origRlimitNofile := syscall.GetInternalOrigRlimitNofile()

if origLimit == nil {
defer origRlimitNofile.Store(origLimit)
origRlimitNofile.Store(&syscall.Rlimit{
Cur: 1024,
Max: 65536,
})
}
rlimOrig := origRlimitNofile.Load()

// Start a child process firstly,
// so we can use Prlimit to set it's nofile limit.
cmd := exec.Command("sleep", "infinity")
cmd.Start()
defer func() {
cmd.Process.Kill()
cmd.Process.Wait()
}()

// Get child process's current nofile limit
var lim syscall.Rlimit
if err := syscall.Prlimit(cmd.Process.Pid, syscall.RLIMIT_NOFILE, nil, &lim); err != nil {
t.Fatalf("Failed to get the current nofile limit: %v", err)
}
// Set child process's nofile rlimit through prlimit
if err := syscall.Prlimit(cmd.Process.Pid, syscall.RLIMIT_NOFILE, &lim, nil); err != nil {
t.Fatalf("Prlimit(%d) failed: %v", cmd.Process.Pid, err)
}

rlimLater := origRlimitNofile.Load()
if rlimLater != rlimOrig {
t.Fatalf("origRlimitNofile got=%v, want=%v", rlimLater, rlimOrig)
}
}