Skip to content

Commit

Permalink
add tests for prlimit
Browse files Browse the repository at this point in the history
Signed-off-by: lifubang <[email protected]>
  • Loading branch information
lifubang committed May 6, 2024
1 parent 4ce20f3 commit 4ec35da
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/syscall/syscall_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"syscall"
"testing"
"unsafe"
Expand Down Expand Up @@ -654,3 +655,56 @@ func TestAllThreadsSyscallBlockedSyscall(t *testing.T) {
wr.Close()
wg.Wait()
}

//go:linkname syscall_prlimit syscall.prlimit
func syscall_prlimit(pid, resource int, newlimit, old *syscall.Rlimit) error

//go:linkname syscall_origRlimitNofile syscall.origRlimitNofile
var syscall_origRlimitNofile atomic.Pointer[syscall.Rlimit]

func TestPrlimitSelf(t *testing.T) {
rlimOrig := syscall_origRlimitNofile.Load()

// Get the current nofile limit
var lim syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &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 := syscall_origRlimitNofile.Load()
if rlimOrig != nil && rlimLater == rlimOrig {
t.Fatal("The nofile limit cache should be cleared after setting our own nofile limit through prlimit!")
}
}

func TestPrlimitOtherProcess(t *testing.T) {
rlimOrig := syscall_origRlimitNofile.Load()

// Get the current nofile limit
var lim syscall.Rlimit
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim); err != nil {
t.Fatalf("Failed to get the current nofile limit: %v", err)
}

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

// 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)
}

cmd.Process.Kill()
cmd.Process.Wait()

rlimLater := syscall_origRlimitNofile.Load()
if rlimOrig != nil && rlimLater != rlimOrig {
t.Fatal("The nofile limit cache should not be cleared after setting other process's nofile limit through prlimit!")
}
}

0 comments on commit 4ec35da

Please sign in to comment.