|
5 | 5 | package syscall_test
|
6 | 6 |
|
7 | 7 | import (
|
| 8 | + "context" |
8 | 9 | "fmt"
|
9 | 10 | "internal/testenv"
|
10 | 11 | "io"
|
@@ -720,3 +721,197 @@ func TestPrlimitOtherProcess(t *testing.T) {
|
720 | 721 | t.Fatalf("origRlimitNofile got=%v, want=%v", rlimLater, rlimOrig)
|
721 | 722 | }
|
722 | 723 | }
|
| 724 | + |
| 725 | +const magicRlimitValue = 42 |
| 726 | + |
| 727 | +// TestPrlimitFileLimit tests that we can start a Go program, use |
| 728 | +// prlimit to change its NOFILE limit, and have that updated limit be |
| 729 | +// seen by children. See issue #66797. |
| 730 | +func TestPrlimitFileLimit(t *testing.T) { |
| 731 | + switch os.Getenv("GO_WANT_HELPER_PROCESS") { |
| 732 | + case "prlimit1": |
| 733 | + testPrlimitFileLimitHelper1(t) |
| 734 | + return |
| 735 | + case "prlimit2": |
| 736 | + testPrlimitFileLimitHelper2(t) |
| 737 | + return |
| 738 | + } |
| 739 | + |
| 740 | + origRlimitNofile := syscall.GetInternalOrigRlimitNofile() |
| 741 | + defer origRlimitNofile.Store(origRlimitNofile.Load()) |
| 742 | + |
| 743 | + // Set our rlimit to magic+1/max. |
| 744 | + // That will also become the rlimit of the child. |
| 745 | + |
| 746 | + var lim syscall.Rlimit |
| 747 | + if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim); err != nil { |
| 748 | + t.Fatal(err) |
| 749 | + } |
| 750 | + max := lim.Max |
| 751 | + |
| 752 | + lim = syscall.Rlimit{ |
| 753 | + Cur: magicRlimitValue + 1, |
| 754 | + Max: max, |
| 755 | + } |
| 756 | + if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &lim); err != nil { |
| 757 | + t.Fatal(err) |
| 758 | + } |
| 759 | + |
| 760 | + ctx, cancel := context.WithCancel(context.Background()) |
| 761 | + defer cancel() |
| 762 | + |
| 763 | + exe, err := os.Executable() |
| 764 | + if err != nil { |
| 765 | + t.Fatal(err) |
| 766 | + } |
| 767 | + |
| 768 | + r1, w1, err := os.Pipe() |
| 769 | + if err != nil { |
| 770 | + t.Fatal(err) |
| 771 | + } |
| 772 | + defer r1.Close() |
| 773 | + defer w1.Close() |
| 774 | + |
| 775 | + r2, w2, err := os.Pipe() |
| 776 | + if err != nil { |
| 777 | + t.Fatal(err) |
| 778 | + } |
| 779 | + defer r2.Close() |
| 780 | + defer w2.Close() |
| 781 | + |
| 782 | + var output strings.Builder |
| 783 | + |
| 784 | + const arg = "-test.run=^TestPrlimitFileLimit$" |
| 785 | + cmd := testenv.CommandContext(t, ctx, exe, arg, "-test.v") |
| 786 | + cmd = testenv.CleanCmdEnv(cmd) |
| 787 | + cmd.Env = append(cmd.Env, "GO_WANT_HELPER_PROCESS=prlimit1") |
| 788 | + cmd.ExtraFiles = []*os.File{r1, w2} |
| 789 | + cmd.Stdout = &output |
| 790 | + cmd.Stderr = &output |
| 791 | + |
| 792 | + t.Logf("running %s %s", exe, arg) |
| 793 | + |
| 794 | + if err := cmd.Start(); err != nil { |
| 795 | + t.Fatal(err) |
| 796 | + } |
| 797 | + |
| 798 | + // Wait for the child to start. |
| 799 | + b := make([]byte, 1) |
| 800 | + if n, err := r2.Read(b); err != nil { |
| 801 | + t.Fatal(err) |
| 802 | + } else if n != 1 { |
| 803 | + t.Fatalf("read %d bytes, want 1", n) |
| 804 | + } |
| 805 | + |
| 806 | + // Set the child's prlimit. |
| 807 | + lim = syscall.Rlimit{ |
| 808 | + Cur: magicRlimitValue, |
| 809 | + Max: max, |
| 810 | + } |
| 811 | + if err := syscall.Prlimit(cmd.Process.Pid, syscall.RLIMIT_NOFILE, &lim, nil); err != nil { |
| 812 | + t.Fatalf("Prlimit failed: %v", err) |
| 813 | + } |
| 814 | + |
| 815 | + // Tell the child to continue. |
| 816 | + if n, err := w1.Write(b); err != nil { |
| 817 | + t.Fatal(err) |
| 818 | + } else if n != 1 { |
| 819 | + t.Fatalf("wrote %d bytes, want 1", n) |
| 820 | + } |
| 821 | + |
| 822 | + err = cmd.Wait() |
| 823 | + if output.Len() > 0 { |
| 824 | + t.Logf("%s", output.String()) |
| 825 | + } |
| 826 | + |
| 827 | + if err != nil { |
| 828 | + t.Errorf("child failed: %v", err) |
| 829 | + } |
| 830 | +} |
| 831 | + |
| 832 | +// testPrlimitFileLimitHelper1 is run by TestPrlimitFileLimit. |
| 833 | +func testPrlimitFileLimitHelper1(t *testing.T) { |
| 834 | + var lim syscall.Rlimit |
| 835 | + if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim); err != nil { |
| 836 | + t.Fatal(err) |
| 837 | + } |
| 838 | + t.Logf("helper1 rlimit is %v", lim) |
| 839 | + t.Logf("helper1 cached rlimit is %v", syscall.OrigRlimitNofile()) |
| 840 | + |
| 841 | + // Tell the parent that we are ready. |
| 842 | + b := []byte{0} |
| 843 | + if n, err := syscall.Write(4, b); err != nil { |
| 844 | + t.Fatal(err) |
| 845 | + } else if n != 1 { |
| 846 | + t.Fatalf("wrote %d bytes, want 1", n) |
| 847 | + } |
| 848 | + |
| 849 | + // Wait for the parent to tell us that prlimit was used. |
| 850 | + if n, err := syscall.Read(3, b); err != nil { |
| 851 | + t.Fatal(err) |
| 852 | + } else if n != 1 { |
| 853 | + t.Fatalf("read %d bytes, want 1", n) |
| 854 | + } |
| 855 | + |
| 856 | + if err := syscall.Close(3); err != nil { |
| 857 | + t.Errorf("Close(3): %v", err) |
| 858 | + } |
| 859 | + if err := syscall.Close(4); err != nil { |
| 860 | + t.Errorf("Close(4): %v", err) |
| 861 | + } |
| 862 | + |
| 863 | + if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim); err != nil { |
| 864 | + t.Fatal(err) |
| 865 | + } |
| 866 | + t.Logf("after prlimit helper1 rlimit is %v", lim) |
| 867 | + t.Logf("after prlimit helper1 cached rlimit is %v", syscall.OrigRlimitNofile()) |
| 868 | + |
| 869 | + // Start the grandchild, which should see the rlimit |
| 870 | + // set by the prlimit called by the parent. |
| 871 | + |
| 872 | + ctx, cancel := context.WithCancel(context.Background()) |
| 873 | + defer cancel() |
| 874 | + |
| 875 | + exe, err := os.Executable() |
| 876 | + if err != nil { |
| 877 | + t.Fatal(err) |
| 878 | + } |
| 879 | + |
| 880 | + const arg = "-test.run=^TestPrlimitFileLimit$" |
| 881 | + cmd := testenv.CommandContext(t, ctx, exe, arg, "-test.v") |
| 882 | + cmd = testenv.CleanCmdEnv(cmd) |
| 883 | + cmd.Env = append(cmd.Env, "GO_WANT_HELPER_PROCESS=prlimit2") |
| 884 | + t.Logf("running %s %s", exe, arg) |
| 885 | + out, err := cmd.CombinedOutput() |
| 886 | + if len(out) > 0 { |
| 887 | + t.Logf("%s", out) |
| 888 | + } |
| 889 | + if err != nil { |
| 890 | + t.Errorf("grandchild failed: %v", err) |
| 891 | + } else { |
| 892 | + fmt.Println("OK") |
| 893 | + } |
| 894 | +} |
| 895 | + |
| 896 | +// testPrlimitFileLimitHelper2 is run by testPrlimitFileLimit1. |
| 897 | +func testPrlimitFileLimitHelper2(t *testing.T) { |
| 898 | + var lim syscall.Rlimit |
| 899 | + if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &lim); err != nil { |
| 900 | + t.Fatal(err) |
| 901 | + } |
| 902 | + |
| 903 | + t.Logf("helper2 rlimit is %v", lim) |
| 904 | + cached := syscall.OrigRlimitNofile() |
| 905 | + t.Logf("helper2 cached rlimit is %v", cached) |
| 906 | + |
| 907 | + // The value return by Getrlimit will have been adjusted. |
| 908 | + // We should have cached the value set by prlimit called by the parent. |
| 909 | + |
| 910 | + if cached == nil { |
| 911 | + t.Fatal("no cached rlimit") |
| 912 | + } else if cached.Cur != magicRlimitValue { |
| 913 | + t.Fatalf("cached rlimit is %d, want %d", cached.Cur, magicRlimitValue) |
| 914 | + } |
| 915 | + |
| 916 | + fmt.Println("OK") |
| 917 | +} |
0 commit comments