Skip to content

Commit 5eabc76

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

File tree

3 files changed

+73
-0
lines changed

3 files changed

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

0 commit comments

Comments
 (0)