0N/A/*
2362N/A * Copyright (c) 2002, 2007, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4680945 4873419
0N/A * @summary Check process exit code
0N/A * @author kladko, Martin Buchholz
0N/A */
0N/A
0N/Aimport java.io.File;
0N/A
0N/Apublic class ExitValue
0N/A{
0N/A
0N/A public static String join(String separator, String[] elts) {
0N/A String result = elts[0];
0N/A for (int i = 1; i < elts.length; ++i)
0N/A result = result + separator + elts[i];
0N/A return result;
0N/A }
0N/A
0N/A public static void checkExitValue(String[] commandArgs,
0N/A int expectedExitValue)
0N/A throws Exception
0N/A {
0N/A if (! (new File(commandArgs[0]).exists()))
0N/A return;
0N/A
0N/A System.out.println("Running command: " + join(" ", commandArgs));
0N/A Process proc = Runtime.getRuntime().exec(commandArgs);
0N/A int val;
0N/A byte[] buf = new byte[4096];
0N/A int n = proc.getErrorStream().read(buf);
0N/A if (n > 0)
0N/A throw new Exception
0N/A ("Unexpected stderr: "
0N/A + new String(buf, 0, n, "ASCII"));
0N/A if ((val = proc.waitFor()) != expectedExitValue)
0N/A throw new Exception
0N/A ("waitFor() returned unexpected value " + val);
0N/A if ((val = proc.exitValue()) != expectedExitValue)
0N/A throw new Exception
0N/A ("exitValue() returned unexpected value " + val);
0N/A }
0N/A
0N/A public static void checkPosixShellExitValue(String posixShellProgram,
0N/A int expectedExitValue)
0N/A throws Exception
0N/A {
0N/A checkExitValue(new String[] { "/bin/sh", "-c", posixShellProgram },
0N/A expectedExitValue);
0N/A }
0N/A
0N/A final static int EXIT_CODE = 5;
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A
0N/A String java = join(File.separator, new String []
0N/A { System.getProperty("java.home"), "bin", "java" });
0N/A
0N/A checkExitValue(new String[]
0N/A { java,
0N/A "-classpath", System.getProperty("test.classes", "."),
0N/A "ExitValue$Run", String.valueOf(EXIT_CODE)
0N/A }, EXIT_CODE);
0N/A
0N/A checkExitValue(new String[] { "/bin/true" }, 0);
0N/A
0N/A checkPosixShellExitValue("exit", 0);
0N/A
0N/A checkPosixShellExitValue("exit 7", 7);
0N/A
0N/A if (new File("/bin/kill").exists()) {
0N/A int sigoffset =
0N/A System.getProperty("os.name").equals("SunOS") ? 0 : 128;
0N/A checkPosixShellExitValue("/bin/kill -9 $$", sigoffset+9);
0N/A }
0N/A }
0N/A
0N/A public static class Run {
0N/A public static void main (String[] argv) {
0N/A System.exit(Integer.parseInt(argv[0]));
0N/A }
0N/A }
0N/A}