1 /*
   2  * @test
   3  * @bug 5049299
   4  * @summary (process) Use posix_spawn, not fork, on S10 to avoid swap exhaustion 
   5  * @compile BasicLauncher.java Basic.java
   6  * @run main BasicLauncher
   7  */
   8 
   9 import java.io.*;
  10 import java.nio.file.*;
  11 
  12 public class BasicLauncher {
  13 
  14     private static boolean passed = false;
  15 
  16     public static void main(String args[]) throws Exception {
  17         String osName = System.getProperty("os.name");
  18         if (osName.startsWith("SunOS")) {
  19             BasicLauncher l = new BasicLauncher();
  20             l.start();
  21         }
  22     }
  23 
  24     private void start() throws Exception {
  25         String separator = System.getProperty("file.separator");
  26         String jdkpath = System.getProperty("test.jdk") + separator + "bin" + separator;
  27         String srcpath = System.getProperty("test.src", ".") + separator;
  28         String testClasses = System.getProperty("test.classes", ".");
  29 
  30         ProcessBuilder builder = new ProcessBuilder(
  31             jdkpath + "java",
  32             "-cp",
  33             testClasses,
  34             "-Djdk.lang.Process.launchMechanism=posix_spawn",
  35             "Basic");
  36         builder.redirectErrorStream(true);
  37         Process testProc = builder.start();
  38         printProcessThread ppt =
  39                 new printProcessThread(testProc, "testproc");
  40         ppt.start();
  41         testProc.waitFor();
  42         System.out.println("testproc done");
  43 
  44         if (!passed)
  45             throw new RuntimeException("Test Failed: ");
  46     }
  47 
  48 
  49     class printProcessThread extends Thread {
  50         Process p;
  51         String pName;
  52 
  53         public printProcessThread(Process p, String pName) {
  54             this.p = p;
  55             this.pName = pName;
  56         }
  57 
  58         @Override
  59         public void run() {
  60             try (BufferedReader reader =
  61                 new BufferedReader(new InputStreamReader(p.getInputStream()))) {
  62                 String line;
  63                 while ((line = reader.readLine()) != null) {
  64                     System.out.println("[Output: " + pName + "]" + line);
  65                     if (line.contains("failed = 0")) {
  66                         passed = true;
  67                     }
  68                 }
  69 
  70             } catch (Exception e) {
  71                 System.out.println("Exception encountered in " + pName
  72                         + " thread\n" + e);
  73             }
  74         }
  75     }
  76 }