1 /*
   2  * Copyright (c) 2012, 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 /*
  25  * @test
  26  * @bug 4244896
  27  * @summary Test for the various platform specific implementations of
  28  *          destroyForcibly.
  29  */
  30 
  31 import java.io.BufferedReader;
  32 import java.io.BufferedWriter;
  33 import java.io.File;
  34 import java.io.FileWriter;
  35 import java.io.IOException;
  36 import java.io.InputStreamReader;
  37 
  38 abstract class ProcessTest implements Runnable {
  39     ProcessBuilder bldr;
  40     Process p;
  41 
  42     public void run() {
  43         try {
  44             String line;
  45             BufferedReader is =
  46                 new BufferedReader(new InputStreamReader(p.getInputStream()));
  47             while ((line = is.readLine()) != null)
  48                 System.err.println("ProcessTrap: " + line);
  49         } catch(IOException e) {
  50             if (!e.getMessage().matches("[Ss]tream [Cc]losed")) {
  51                 throw new RuntimeException(e);
  52             }
  53         }
  54     }
  55 
  56     public void runTest() throws Exception {
  57         // The destroy() method is not tested because
  58         // the process streams are closed by the destroy() call.
  59         // After a destroy() call, the process terminates with a
  60         // SIGPIPE even if it was trapping SIGTERM.
  61         // So skip the trap test and go straight to destroyForcibly().
  62         p.destroyForcibly();
  63         p.waitFor();
  64         if (p.isAlive())
  65             throw new RuntimeException("Problem terminating the process.");
  66     }
  67 }
  68 
  69 class UnixTest extends ProcessTest {
  70     public UnixTest(File script) throws IOException {
  71         script.deleteOnExit();
  72         createScript(script);
  73         bldr = new ProcessBuilder(script.getCanonicalPath());
  74         bldr.redirectErrorStream(true);
  75         bldr.directory(new File("."));
  76         p = bldr.start();
  77     }
  78 
  79     void createScript(File processTrapScript) throws IOException {
  80         processTrapScript.deleteOnExit();
  81         try (FileWriter fstream = new FileWriter(processTrapScript);
  82              BufferedWriter out = new BufferedWriter(fstream)) {
  83             out.write("#!/bin/bash\n" +
  84                 "echo \\\"ProcessTrap.sh started\\\"\n" +
  85                 "while :\n" +
  86                 "do\n" +
  87                 "    sleep 1;\n" +
  88                 "done\n");
  89         }
  90         processTrapScript.setExecutable(true, true);
  91     }
  92 
  93 }
  94 
  95 class WindowsTest extends ProcessTest {
  96     public WindowsTest() throws IOException {
  97         bldr = new ProcessBuilder("ftp");
  98         bldr.redirectErrorStream(true);
  99         bldr.directory(new File("."));
 100         p = bldr.start();
 101     }
 102 
 103 }
 104 
 105 public class DestroyTest {
 106 
 107     public static ProcessTest getTest() throws Exception {
 108         String osName = System.getProperty("os.name");
 109         if (osName.startsWith("Windows")) {
 110             return new WindowsTest();
 111         } else {
 112             File userDir = new File(System.getProperty("user.dir", "."));
 113             File tempFile = File.createTempFile("ProcessTrap-", ".sh", userDir);
 114             if (osName.startsWith("Linux")
 115                     || osName.startsWith("Mac OS")
 116                     || osName.equals("SunOS")
 117                     || osName.equals("AIX")) {
 118                 return new UnixTest(tempFile);
 119             }
 120         }
 121         return null;
 122     }
 123 
 124     public static void main(String args[]) throws Exception {
 125         ProcessTest test = getTest();
 126         if (test == null) {
 127             throw new RuntimeException("Unrecognised OS");
 128         } else {
 129             new Thread(test).start();
 130             Thread.sleep(1000);
 131             test.runTest();
 132         }
 133     }
 134 }
 135