|
1 /* |
|
2 * Copyright (c) 2007, 2016, 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 /* @test |
|
25 @bug 6598160 |
|
26 @summary Windows IPv6 Socket implementation doesn't set the handle to not inherit |
|
27 @author Chris Hegarty |
|
28 */ |
|
29 |
|
30 import java.net.BindException; |
|
31 import java.net.ServerSocket; |
|
32 import java.io.File; |
|
33 import java.io.IOException; |
|
34 |
|
35 /** |
|
36 * This test is only really applicable to Windows machines that are running IPv6, but |
|
37 * it should still pass on other platforms so we can just run it. |
|
38 */ |
|
39 |
|
40 public class InheritHandle |
|
41 { |
|
42 static String java = System.getProperty("java.home") + File.separator + |
|
43 "bin" + File.separator + "java"; |
|
44 |
|
45 public static void main(String[] args) { |
|
46 if (args.length == 1) { |
|
47 doWait(); |
|
48 } else { |
|
49 startTest(); |
|
50 } |
|
51 |
|
52 } |
|
53 |
|
54 static void startTest() { |
|
55 ServerSocket ss; |
|
56 int port; |
|
57 Process process; |
|
58 |
|
59 // create a ServerSocket listening on any port |
|
60 try { |
|
61 ss = new ServerSocket(0); |
|
62 port = ss.getLocalPort(); |
|
63 System.out.println("First ServerSocket listening on port " + port); |
|
64 } catch (IOException e) { |
|
65 System.out.println("Cannot create ServerSocket"); |
|
66 e.printStackTrace(); |
|
67 return; |
|
68 } |
|
69 |
|
70 // create another java process that simply waits. If the bug is present this |
|
71 // process will inherit the native IPv6 handle for ss and cause the second |
|
72 // ServerSocket constructor to throw a BindException |
|
73 try { |
|
74 process = Runtime.getRuntime().exec(java + " InheritHandle -doWait"); |
|
75 } catch (IOException ioe) { |
|
76 System.out.println("Cannot create process"); |
|
77 ioe.printStackTrace(); |
|
78 try { |
|
79 ss.close(); |
|
80 } catch (IOException e) { |
|
81 e.printStackTrace(); |
|
82 } |
|
83 return; |
|
84 } |
|
85 |
|
86 // Allow some time for the process to get started |
|
87 try { |
|
88 System.out.println("waiting..."); |
|
89 Thread.sleep(2 * 1000); |
|
90 |
|
91 System.out.println("Now close the socket and try to create another" + |
|
92 " one listening on the same port"); |
|
93 ss.close(); |
|
94 int retries = 0; |
|
95 while (retries < 5) { |
|
96 try (ServerSocket s = new ServerSocket(port);) { |
|
97 System.out.println("Second ServerSocket created successfully"); |
|
98 break; |
|
99 } catch (BindException e) { |
|
100 System.out.println("BindException \"" + e.getMessage() + "\", retrying..."); |
|
101 Thread.sleep(100L); |
|
102 retries ++; |
|
103 continue; |
|
104 } |
|
105 } |
|
106 |
|
107 } catch (InterruptedException ie) { |
|
108 } catch (IOException ioe) { |
|
109 throw new RuntimeException("Failed: " + ioe); |
|
110 } finally { |
|
111 process.destroy(); |
|
112 } |
|
113 |
|
114 System.out.println("OK"); |
|
115 } |
|
116 |
|
117 static void doWait() { |
|
118 try { |
|
119 Thread.sleep(200 * 1000); |
|
120 } catch (InterruptedException ie) { |
|
121 } |
|
122 } |
|
123 } |