@@ -654,3 +654,66 @@ func TestAllThreadsSyscallBlockedSyscall(t *testing.T) {
654
654
wr .Close ()
655
655
wg .Wait ()
656
656
}
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