1 /*
  2  * Copyright (c) 2006, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * author Edward Wang
 26  *
 27  * @test
 28  * @bug 6368984
 29  * @key intermittent
 30  * @summary Configuring unconnected Socket before passing to implAccept
 31  *          can cause fd leak.
 32  *          This test may fail intermittently if foreign processes will
 33  *          try to establish connection to the test server socket.
 34  * @requires os.family != "windows"
 35  * @requires vm.flagless
 36  * @library /test/lib
 37  * @build jdk.test.lib.Utils
 38  *        jdk.test.lib.Asserts
 39  *        jdk.test.lib.JDKToolFinder
 40  *        jdk.test.lib.JDKToolLauncher
 41  *        jdk.test.lib.Platform
 42  *        jdk.test.lib.process.*
 43  *        AcceptCauseFileDescriptorLeak
 44  * @run main/othervm AcceptCauseFileDescriptorLeak root
 45  */
 46 
 47 import java.io.IOException;
 48 import java.net.InetAddress;
 49 import java.net.InetSocketAddress;
 50 import java.net.ServerSocket;
 51 import java.net.Socket;
 52 import java.util.List;
 53 
 54 import jdk.test.lib.JDKToolFinder;
 55 import jdk.test.lib.process.OutputAnalyzer;
 56 
 57 public class AcceptCauseFileDescriptorLeak {
 58     private static final int REPS = 2048;
 59     private static final int THRESHOLD = 1024;
 60 
 61     public static void main(String[] args) throws Exception {
 62         if (args.length != 0) {
 63             OutputAnalyzer analyzer = execCmd("ulimit -n -H");
 64             String output = analyzer.getOutput();
 65             if (output == null || output.length() == 0) {
 66                 throw new RuntimeException("\"ulimit -n -H\" output nothing"
 67                         + " and its exit code is " + analyzer.getExitValue());
 68             } else {
 69                 output = output.trim();
 70                 // Set max open file descriptors to 1024
 71                 // if it is unlimited or greater than 1024,
 72                 // otherwise just do test directly
 73                 if ("unlimited".equals(output)
 74                         || Integer.valueOf(output) > THRESHOLD) {
 75                     analyzer = execCmd("ulimit -n " + THRESHOLD + "; "
 76                             + composeJavaTestStr());
 77                     System.out.println("Output: ["
 78                             + analyzer.getOutput() + "]");
 79                     int rc = analyzer.getExitValue();
 80                     if (rc != 0) {
 81                         throw new RuntimeException(
 82                                 "Unexpected exit code: " + rc);
 83                     }
 84                     return;
 85                 }
 86             }
 87         }
 88 
 89         final ServerSocket ss = new ServerSocket() {
 90             public Socket accept() throws IOException {
 91                 Socket s = new Socket() {
 92                 };
 93                 s.setSoTimeout(10000);
 94                 implAccept(s);
 95                 return s;
 96             }
 97         };
 98         ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
 99         Thread t = new Thread(new Runnable() {
100             public void run() {
101                 int repsCompleted = 0;
102                 try {
103                     for (; repsCompleted < REPS; repsCompleted++) {
104                         (new Socket(InetAddress.getLoopbackAddress(), ss.getLocalPort())).close();
105                     }
106                 } catch (IOException e) {
107                     e.printStackTrace();
108                 } finally {
109                     System.out.println("Client iterations completed:" + repsCompleted);
110                 }
111             }
112         });
113         t.start();
114         int repsCompleted = 0;
115         try {
116             for (; repsCompleted < REPS; repsCompleted++) {
117                 ss.accept().close();
118             }
119         } finally {
120             System.out.println("Server iterations completed:" + repsCompleted);
121             ss.close();
122         }
123         t.join();
124     }
125 
126     /**
127      * Execute command with shell
128      *
129      * @param  command
130      * @return OutputAnalyzer
131      * @throws IOException
132      */
133     static OutputAnalyzer execCmd(String command) throws IOException {
134         List<String> cmd = List.of("sh", "-c", command);
135         System.out.println("Executing: " + cmd);
136         ProcessBuilder pb = new ProcessBuilder(cmd);
137         return new OutputAnalyzer(pb.start());
138     }
139 
140     static String composeJavaTestStr() {
141         return JDKToolFinder.getTestJDKTool("java") + " "
142                 + AcceptCauseFileDescriptorLeak.class.getName();
143     }
144 }
145