1 /*
   2  * Copyright (c) 2012, 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 import java.io.*;
  25 import java.util.concurrent.TimeUnit;
  26 
  27 public class ProcessKillTest implements Runnable {
  28 
  29     private Process p;
  30 
  31     public ProcessKillTest() throws Exception {
  32         ProcessBuilder bldr;
  33         if (isOS("win"))
  34             bldr = new ProcessBuilder("ping", "-t", "127.0.0.1");
  35         else
  36             bldr = new ProcessBuilder("./ProcessTrap.sh");
  37         bldr.redirectErrorStream(true);
  38         bldr.directory(new File("."));
  39         p = bldr.start();
  40     }
  41 
  42     public Process getProc() {
  43         return p;
  44     }
  45 
  46     public void killProc(boolean force) throws Exception {
  47         if(force) {
  48             p.destroyForcibly().waitFor();
  49             // System.out.println(p.exitValue());
  50             if ((isOS("sol") && p.exitValue() != 9) ||
  51                 ((isOS("lin") || isOS("mac")) && p.exitValue() != 137))
  52                 throw new RuntimeException("Test failed: wrong exit value");
  53         } else {
  54             p.destroy();
  55         }
  56     }
  57 
  58     public boolean isAlive() {
  59         return p.isAlive();
  60     }
  61 
  62     public void run() {
  63         try {
  64             String line;
  65             BufferedReader is =
  66                 new BufferedReader(new InputStreamReader(p.getInputStream()));
  67             while ((line = is.readLine()) != null)
  68                 System.err.println("ProcessTrap: " + line);
  69         } catch(IOException e) {
  70             if (!e.getMessage().equals("Stream closed")) {
  71                 throw new RuntimeException(e);
  72             }
  73         }
  74     }
  75 
  76     public static boolean isOS(String os) {
  77         return System.getProperty("os.name").toLowerCase().indexOf(os) == 0;
  78     }
  79 
  80     public static void runTest(ProcessKillTest test) throws Exception {
  81         TimeUnit nanos = TimeUnit.NANOSECONDS;
  82     
  83         if(isOS("sol") || isOS("lin"))
  84             test.killProc(false);   // make sure the SIGTERM is trapped
  85         
  86         long start = System.nanoTime();
  87         if (!test.isAlive() ||
  88             test.getProc().waitFor(0, TimeUnit.MILLISECONDS)) {
  89             throw new RuntimeException(
  90                 "Test failed: Process exited prematurely");
  91         }
  92         long end = System.nanoTime();
  93         if(nanos.toMillis(end - start) > 10)
  94             throw new RuntimeException(
  95                 "Test failed: waitFor took too long");
  96 
  97         start = System.nanoTime();
  98         test.getProc().waitFor(3000, TimeUnit.MILLISECONDS);
  99         end = System.nanoTime();
 100         if(nanos.toMillis(end - start) < 3000)
 101             throw new RuntimeException(
 102                 "Test failed: waitFor didn't take long enough");
 103         
 104         if(isOS("win"))
 105             test.killProc(false); // make sure killProc(false) works on win
 106         else
 107             test.killProc(true);
 108         test.getProc().waitFor();
 109         
 110         start = System.nanoTime();
 111         if (test.isAlive() || 
 112             !test.getProc().waitFor(0, TimeUnit.MILLISECONDS)) {
 113             throw new RuntimeException(
 114                 "Test failed: Process hasn't been killed, please terminate " +
 115                 test.getProc().toString() + " manually");
 116         }
 117         end = System.nanoTime();
 118         if(nanos.toMillis(end - start) > 10)
 119             throw new RuntimeException(
 120                 "Test failed: waitFor took too long with a timeout of 0");
 121         
 122         
 123         start = System.nanoTime();
 124         test.getProc().waitFor(100, TimeUnit.MILLISECONDS);
 125         end = System.nanoTime();
 126         if(nanos.toMillis(end - start) > 10)
 127             throw new RuntimeException(
 128                 "Test failed: waitFor took too long on dead process");
 129         
 130         System.out.println("Test passed");
 131     }
 132 
 133     public static void main(String args[]) throws Exception {
 134         ProcessKillTest test = new ProcessKillTest();
 135         new Thread(test).start();
 136         Thread.sleep(1000);
 137         runTest(test);
 138     }
 139 }