@@ -654,3 +654,69 @@ 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
+
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