Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/ProcessBuilder/DestroyTest.java
47209 views
/*1* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 424489626* @summary Test for the various platform specific implementations of27* destroyForcibly.28*/2930import java.io.*;31import java.util.ArrayList;32import java.util.concurrent.TimeUnit;3334abstract class ProcessTest implements Runnable {35ProcessBuilder bldr;36Process p;3738public Process killProc(boolean force) throws Exception {39if (force) {40p.destroyForcibly();41} else {42p.destroy();43}44return p;45}4647public boolean isAlive() {48return p.isAlive();49}5051public void run() {52try {53String line;54BufferedReader is =55new BufferedReader(new InputStreamReader(p.getInputStream()));56while ((line = is.readLine()) != null)57System.err.println("ProcessTrap: " + line);58} catch(IOException e) {59if (!e.getMessage().matches("[Ss]tream [Cc]losed")) {60throw new RuntimeException(e);61}62}63}6465public abstract void runTest() throws Exception;66}6768class UnixTest extends ProcessTest {69public UnixTest(File script) throws IOException {70script.deleteOnExit();71createScript(script);72bldr = new ProcessBuilder(script.getCanonicalPath());73bldr.redirectErrorStream(true);74bldr.directory(new File("."));75p = bldr.start();76}7778void createScript(File processTrapScript) throws IOException {79processTrapScript.deleteOnExit();80FileWriter fstream = new FileWriter(processTrapScript);81try (BufferedWriter out = new BufferedWriter(fstream)) {82out.write("#!/bin/bash\n" +83"echo \\\"ProcessTrap.sh started: trapping SIGTERM/SIGINT\\\"\n" +84"trap bashtrap SIGTERM SIGINT\n" +85"bashtrap()\n" +86"{\n" +87" echo \\\"SIGTERM/SIGINT detected!\\\"\n" +88"}\n" +89"\n" +90"while :\n" +91"do\n" +92" sleep 1;\n" +93"done\n");94}95processTrapScript.setExecutable(true, true);96}9798@Override99public void runTest() throws Exception {100killProc(false);101Thread.sleep(1000);102if (!p.isAlive())103throw new RuntimeException("Process terminated prematurely.");104killProc(true).waitFor();105if (p.isAlive())106throw new RuntimeException("Problem terminating the process.");107}108}109110class MacTest extends UnixTest {111public MacTest(File script) throws IOException {112super(script);113}114115@Override116public void runTest() throws Exception {117// On Mac, it appears that when we close the processes streams118// after a destroy() call, the process terminates with a119// SIGPIPE even if it was trapping the SIGTERM, so as with120// windows, we skip the trap test and go straight to destroyForcibly().121killProc(true).waitFor();122if (p.isAlive())123throw new RuntimeException("Problem terminating the process.");124}125}126127class WindowsTest extends ProcessTest {128public WindowsTest() throws IOException {129bldr = new ProcessBuilder("ftp");130bldr.redirectErrorStream(true);131bldr.directory(new File("."));132p = bldr.start();133}134135@Override136public void runTest() throws Exception {137killProc(true).waitFor();138}139}140141public class DestroyTest {142143public static ProcessTest getTest() throws Exception {144String osName = System.getProperty("os.name");145if (osName.startsWith("Windows")) {146return new WindowsTest();147} else if (osName.startsWith("Linux") == true) {148return new UnixTest(149File.createTempFile("ProcessTrap-", ".sh",null));150} else if (osName.startsWith("Mac OS")) {151return new MacTest(152File.createTempFile("ProcessTrap-", ".sh",null));153} else if (osName.equals("SunOS")) {154return new UnixTest(155File.createTempFile("ProcessTrap-", ".sh",null));156} else if (osName.equals("AIX")) {157return new UnixTest(158File.createTempFile("ProcessTrap-", ".sh",null));159}160return null;161}162163public static void main(String args[]) throws Exception {164ProcessTest test = getTest();165if (test == null) {166throw new RuntimeException("Unrecognised OS");167} else {168new Thread(test).start();169Thread.sleep(1000);170test.runTest();171}172}173}174175176177