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 * @test 26 * @bug 7147084 27 * @run main/othervm InheritIOEHandle 28 * @summary inherit IOE handles and MS CreateProcess limitations (kb315939) 29 */ 30 31 import java.io.BufferedReader; 32 import java.io.File; 33 import java.io.IOException; 34 import java.io.InputStreamReader; 35 36 public class InheritIOEHandle { 37 private static enum APP { 38 B, C; 39 } 40 private static File stopC = new File(".\\StopC.txt"); 41 private static String SIGNAL = "After call child process"; 42 private static String JAVA_EXE = System.getProperty("java.home") 43 + File.separator + "bin" 44 + File.separator + "java"; 45 46 private static String[] getCommandArray(String processName) { 47 String[] cmdArray = { 48 JAVA_EXE, 49 "-cp", 50 System.getProperty("java.class.path"), 51 InheritIOEHandle.class.getName(), 52 processName 53 }; 54 return cmdArray; 55 } 56 57 public static void main(String[] args) throws Exception { 58 if (!System.getProperty("os.name").startsWith("Windows")) { 59 return; 60 } 61 62 if (args.length > 0) { 63 APP app = APP.valueOf(args[0]); 64 switch (app) { 65 case B: 66 performB(); 67 break; 68 case C: 69 performC(); 70 break; 71 } 72 return; 73 } 74 performA(); 75 } 76 77 private static void performA() { 78 try { 79 stopC.delete(); 80 81 ProcessBuilder builder = new ProcessBuilder( 82 getCommandArray(APP.B.name())); 83 builder.redirectErrorStream(true); 84 85 Process process = builder.start(); 86 87 process.getOutputStream().close(); 88 process.getErrorStream().close(); 89 90 try (BufferedReader in = new BufferedReader( new InputStreamReader( 91 process.getInputStream(), "utf-8"))) 92 { 93 String result; 94 while ((result = in.readLine()) != null) { 95 if (!SIGNAL.equals(result)) { 96 throw new Error("Catastrophe in process B! Bad output."); 97 } 98 } 99 } 100 101 // If JDK-7147084 is not fixed that point is unreachable. 102 103 // write signal file 104 stopC.createNewFile(); 105 106 System.err.println("Read stream finished."); 107 } catch (IOException ex) { 108 throw new Error("Catastrophe in process A!", ex); 109 } 110 } 111 112 private static void performB() { 113 try { 114 ProcessBuilder builder = new ProcessBuilder( 115 getCommandArray(APP.C.name())); 116 117 Process process = builder.start(); 118 119 process.getInputStream().close(); 120 process.getOutputStream().close(); 121 process.getErrorStream().close(); 122 123 System.out.println(SIGNAL); 124 125 // JDK-7147084 subject: 126 // Process C inherits the [System.out] handle and 127 // handle close in B does not finalize the streaming for A. 128 // (handle reference count > 1). 129 } catch (IOException ex) { 130 throw new Error("Catastrophe in process B!", ex); 131 } 132 } 133 134 private static void performC() { 135 // If JDK-7147084 is not fixed the loop is 5min long. 136 for (int i = 0; i < 5*60; ++i) { 137 try { 138 Thread.sleep(1000); 139 // check for sucess 140 if (stopC.exists()) 141 break; 142 } catch (InterruptedException ex) { 143 // that is ok. Longer sleep - better effect. 144 } 145 } 146 } 147 } | 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 * @test 26 * @bug 7147084 27 * @run main/othervm InheritIOEHandle 28 * @summary inherit IOE handles and MS CreateProcess limitations (kb315939) 29 */ 30 31 import java.io.BufferedReader; 32 import java.io.File; 33 import java.io.IOException; 34 import java.io.InputStreamReader; 35 36 public class InheritIOEHandle { 37 private static enum APP { 38 A, B, C; 39 } 40 41 private static File stopC = new File("StopC.txt"); 42 private static String SIGNAL = "After call child process"; 43 private static String JAVA_EXE = System.getProperty("java.home") 44 + File.separator + "bin" 45 + File.separator + "java"; 46 47 private static String[] getCommandArray(String processName) { 48 String[] cmdArray = { 49 JAVA_EXE, 50 "-cp", 51 System.getProperty("java.class.path"), 52 InheritIOEHandle.class.getName(), 53 processName 54 }; 55 return cmdArray; 56 } 57 58 public static void main(String[] args) throws Exception { 59 if (!System.getProperty("os.name").startsWith("Windows")) { 60 return; 61 } 62 63 APP app = (args.length > 0) ? APP.valueOf(args[0]) : APP.A; 64 switch (app) { 65 case A: 66 performA(); 67 break; 68 case B: 69 performB(); 70 break; 71 case C: 72 performC(); 73 break; 74 } 75 } 76 77 private static void performA() { 78 try { 79 stopC.delete(); 80 81 ProcessBuilder builder = new ProcessBuilder( 82 getCommandArray(APP.B.name())); 83 builder.redirectErrorStream(true); 84 85 Process process = builder.start(); 86 87 process.getOutputStream().close(); 88 process.getErrorStream().close(); 89 90 boolean isSignalReceived = false; 91 try (BufferedReader in = new BufferedReader(new InputStreamReader( 92 process.getInputStream(), "utf-8"))) 93 { 94 String result; 95 while ((result = in.readLine()) != null) { 96 if (SIGNAL.equals(result)) { 97 isSignalReceived = true; 98 break; 99 } else { 100 throw new RuntimeException("Catastrophe in process B! Bad output."); 101 } 102 } 103 104 } 105 if (!isSignalReceived) { 106 throw new RuntimeException("Signal from B was not received"); 107 } 108 109 // If JDK-7147084 is not fixed that point is unreachable. 110 System.out.println("Received signal from B, creating file StopC"); 111 // write signal file 112 boolean isFileStopC = stopC.createNewFile(); 113 if (!isFileStopC) { 114 throw new RuntimeException("Signal file StopC.txt was not created. TEST or INFRA bug"); 115 } 116 117 process.waitFor(); 118 119 System.err.println("Read stream finished."); 120 } catch (IOException ex) { 121 throw new RuntimeException("Catastrophe in process A!", ex); 122 } catch (InterruptedException ex) { 123 throw new RuntimeException("A was interrupted while waiting for B", ex); 124 } 125 } 126 127 private static void performB() { 128 try { 129 ProcessBuilder builder = new ProcessBuilder( 130 getCommandArray(APP.C.name())); 131 132 Process process = builder.start(); 133 134 process.getInputStream().close(); 135 process.getOutputStream().close(); 136 process.getErrorStream().close(); 137 138 System.out.println(SIGNAL); 139 process.waitFor(); 140 141 // JDK-7147084 subject: 142 // Process C inherits the [System.out] handle and 143 // handle close in B does not finalize the streaming for A. 144 // (handle reference count > 1). 145 } catch (IOException ex) { 146 throw new RuntimeException("Catastrophe in process B!", ex); 147 } catch (InterruptedException ex) { 148 throw new RuntimeException("B was interrupted while waiting for C", ex); 149 } 150 } 151 152 private static void performC() { 153 // If JDK-7147084 is not fixed the loop is 5min long. 154 for (int i = 0; i < 5 * 60; ++i) { 155 try { 156 Thread.sleep(1000); 157 } catch (InterruptedException ex) { 158 // that is ok. Longer sleep - better effect. 159 } 160 // check for success 161 if (stopC.exists()) { 162 break; 163 } 164 } 165 } 166 } |