Skip to content

Commit 565a905

Browse files
committed
add tests for prlimit
Signed-off-by: lifubang <[email protected]>
1 parent 4ce20f3 commit 565a905

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

src/syscall/export_linux_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
var (
1212
RawSyscallNoError = rawSyscallNoError
1313
ForceClone3 = &forceClone3
14+
Prlimit = prlimit
1415
)
1516

1617
const (

src/syscall/export_rlimit_test.go

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

77
package syscall
88

9+
import "sync/atomic"
10+
911
func OrigRlimitNofile() *Rlimit {
1012
return origRlimitNofile.Load()
1113
}
14+
15+
func GetInternalOrigRlimitNofile() *atomic.Pointer[Rlimit] {
16+
return &origRlimitNofile
17+
}

src/syscall/syscall_linux_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,3 +654,66 @@ func TestAllThreadsSyscallBlockedSyscall(t *testing.T) {
654654
wr.Close()
655655
wg.Wait()
656656
}
657+
658+
func TestPrlimitSelf(t *testing.T) {
659+
origLimit := syscall.OrigRlimitNofile()
660+
origRlimitNofile := syscall.GetInternalOrigRlimitNofile()
661+
defer origRlimitNofile.Store(origLimit)
662+
663+
origRlimitNofile.Store(&syscall.Rlimit{
664+
Cur: 1024,
665+
Max: 65536,
666+
})
667+
668+
// Get the current nofile limit
669+
var lim syscall.Rlimit
670+
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim); err != nil {
671+
t.Fatalf("Failed to get the current nofile limit: %v", err)
672+
}
673+
// Set current process's nofile limit through prlimit
674+
if err := syscall.Prlimit(0, syscall.RLIMIT_NOFILE, &lim, nil); err != nil {
675+
t.Fatalf("Prlimit self failed: %v", err)
676+
}
677+
678+
rlimLater := origRlimitNofile.Load()
679+
if rlimLater != nil {
680+
t.Fatalf("origRlimitNofile expected to be nil, but got %v", rlimLater)
681+
}
682+
}
683+
684+
func TestPrlimitOtherProcess(t *testing.T) {
685+
origLimit := syscall.OrigRlimitNofile()
686+
origRlimitNofile := syscall.GetInternalOrigRlimitNofile()
687+
defer origRlimitNofile.Store(origLimit)
688+
689+
origRlimitNofile.Store(&syscall.Rlimit{
690+
Cur: 1024,
691+
Max: 65536,
692+
})
693+
rlimOrig := origRlimitNofile.Load()
694+
695+
// Get the current nofile limit
696+
var lim syscall.Rlimit
697+
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim); err != nil {
698+
t.Fatalf("Failed to get the current nofile limit: %v", err)
699+
}
700+
701+
// Start a child process firstly,
702+
// so we can use Prlimit to set it's nofile limit.
703+
cmd := exec.Command("sleep", "infinity")
704+
cmd.Start()
705+
defer func() {
706+
cmd.Process.Kill()
707+
cmd.Process.Wait()
708+
}()
709+
710+
// Set child process's nofile rlimit through prlimit
711+
if err := syscall.Prlimit(cmd.Process.Pid, syscall.RLIMIT_NOFILE, &lim, nil); err != nil {
712+
t.Fatalf("Prlimit(%d) failed: %v", cmd.Process.Pid, err)
713+
}
714+
715+
rlimLater := origRlimitNofile.Load()
716+
if rlimLater != rlimOrig {
717+
t.Fatalf("origRlimitNofile expected to be %v, but got %v", rlimOrig, rlimLater)
718+
}
719+
}

0 commit comments

Comments
 (0)