1 /*
  2  * Copyright (c) 1997, 2019, 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 4033560
 26    @summary 4033560 limited args of exec to 198 on Solaris. We check
 27             that we can actually exec more args than that.
 28    @author Anand Palaniswamy
 29    @run main/othervm ExecWithLotsOfArgs
 30 */
 31 
 32 import java.io.BufferedReader;
 33 import java.io.InputStreamReader;
 34 import java.io.File;
 35 import java.io.IOException;
 36 
 37 public class ExecWithLotsOfArgs {
 38 
 39     public static class EchoingHelper {
 40         public static void main(String[] args) {
 41             for (int i = 0; i < args.length; i++) {
 42                 System.out.println(args[i]);
 43             }
 44         }
 45     }
 46 
 47     public static void main(String[] args) throws Exception {
 48         String[] command = new String[300];
 49         int n = 0;
 50 
 51         /*
 52          * The Java program to exec. This is slightly fragile. Works
 53          * on Solaris and Win32.
 54          */
 55         command[n++] = System.getProperty("java.home") + File.separator +
 56             "bin" + File.separator + "java";
 57         if (System.getProperty("java.class.path") != null) {
 58             command[n++] = "-classpath";
 59             command[n++] = System.getProperty("java.class.path");
 60         }
 61 
 62         /*
 63          * The class with main() that the exec'd VM will run.
 64          */
 65         command[n++] = "ExecWithLotsOfArgs$EchoingHelper";
 66 
 67         /*
 68          * Make a long set of args n, n + 1, ... , 300.
 69          */
 70         for (int i = n; i < command.length; i++) {
 71             command[i] = new String(new Integer(i).toString());
 72         }
 73 
 74         /*
 75          * Do the exec.
 76          */
 77         Process p = null;
 78         p = Runtime.getRuntime().exec(command);
 79         BufferedReader in = new BufferedReader
 80             (new InputStreamReader(p.getInputStream()));
 81 
 82         /*
 83          * Read back all the strings and that the same were returned.
 84          */
 85         String s;
 86         int count = n;
 87         while ((s = in.readLine()) != null) {
 88             if (count >= command.length) {
 89                 failed("Was expecting " + (command.length - 2) +
 90                        " strings to be echo'ed back, but got " +
 91                        (count - 1) + " instead");
 92             }
 93             if (!s.equals(command[count])) {
 94                 failed("Exec'd process returned \"" +
 95                        s + "\", was expecting \""  +
 96                        command[count] + "\"");
 97             }
 98             count++;
 99         }
100 
101         /*
102          * Did we read anything at all?
103          */
104         if (count == n) {
105             /* Try reading the error stream to see if we got any diagnostics */
106             in = new BufferedReader(new InputStreamReader(p.getErrorStream()));
107             while ((s = in.readLine()) != null) {
108                 System.err.println("Error output: " + s);
109             }
110             failed("Exec'd process didn't writing anything to its stdout");
111         }
112     }
113 
114     private static void failed(String s) {
115         throw new RuntimeException("Failed: " + s);
116     }
117 }