19 package org.sleuthkit.autopsy.coreutils;
 
   21 import com.sun.javafx.PlatformUtil;
 
   22 import java.io.BufferedReader;
 
   23 import java.io.IOException;
 
   24 import java.io.InputStream;
 
   25 import java.io.InputStreamReader;
 
   26 import java.io.Writer;
 
   27 import java.util.Date;
 
   28 import java.util.List;
 
   29 import java.util.concurrent.TimeUnit;
 
   30 import java.util.logging.Level;
 
   74             this.startTimeInSeconds = (
new Date().getTime()) / 1000;
 
   82             long currentTimeInSeconds = (
new Date().getTime()) / 1000;
 
   97     public static int execute(ProcessBuilder processBuilder) 
throws SecurityException, IOException {
 
  103             public boolean shouldTerminateProcess() {
 
  137     public static int execute(ProcessBuilder processBuilder, 
long timeOut, TimeUnit units, 
ProcessTerminator terminator) 
throws SecurityException, IOException {
 
  138         Process process = processBuilder.start();
 
  141                 process.waitFor(timeOut, units);
 
  142                 if (process.isAlive() && terminator.shouldTerminateProcess()) {
 
  145             } 
while (process.isAlive());
 
  146         } 
catch (InterruptedException ex) {
 
  147             if (process.isAlive()) {
 
  150             Logger.
getLogger(
ExecUtil.class.getName()).log(Level.INFO, 
"Thread interrupted while running {0}", processBuilder.command().get(0)); 
 
  151             Thread.currentThread().interrupt();
 
  153         return process.exitValue();
 
  162         if (process == null) {
 
  169                 List<Win32Process> children = parentProcess.
getChildren();
 
  171                 children.stream().forEach((child) -> {
 
  176                 process.destroyForcibly();
 
  178         } 
catch (Exception ex) {
 
  179             logger.log(Level.WARNING, 
"Error occurred when attempting to kill process: {0}", ex.getMessage()); 
 
  203     public synchronized String 
execute(
final String aCommand, 
final String... params) throws IOException, InterruptedException {
 
  205         String[] arrayCommand = 
new String[params.length + 1];
 
  206         arrayCommand[0] = aCommand;
 
  208         StringBuilder arrayCommandToLog = 
new StringBuilder();
 
  209         arrayCommandToLog.append(aCommand).append(
" ");
 
  211         for (
int i = 1; i < arrayCommand.length; i++) {
 
  212             arrayCommand[i] = params[i - 1];
 
  213             arrayCommandToLog.append(arrayCommand[i]).append(
" ");
 
  216         final Runtime rt = Runtime.getRuntime();
 
  217         logger.log(Level.INFO, 
"Executing {0}", arrayCommandToLog.toString()); 
 
  219         proc = rt.exec(arrayCommand);
 
  230         this.exitValue = proc.waitFor();
 
  249     public synchronized void execute(
final Writer stdoutWriter, 
final String aCommand, 
final String... params) throws IOException, InterruptedException {
 
  252         String[] arrayCommand = 
new String[params.length + 1];
 
  253         arrayCommand[0] = aCommand;
 
  255         StringBuilder arrayCommandToLog = 
new StringBuilder();
 
  256         arrayCommandToLog.append(aCommand).append(
" ");
 
  258         for (
int i = 1; i < arrayCommand.length; i++) {
 
  259             arrayCommand[i] = params[i - 1];
 
  260             arrayCommandToLog.append(arrayCommand[i]).append(
" ");
 
  263         final Runtime rt = Runtime.getRuntime();
 
  264         logger.log(Level.INFO, 
"Executing {0}", arrayCommandToLog.toString()); 
 
  266         proc = rt.exec(arrayCommand);
 
  277         this.exitValue = proc.waitFor();
 
  278         logger.log(Level.INFO, 
"{0} exit value: {1}", 
new Object[]{aCommand, exitValue}); 
 
  292     public synchronized void stop() {
 
  335         private final InputStream 
is;
 
  336         private final StringBuffer 
output = 
new StringBuffer();
 
  337         private volatile boolean doRun = 
false;
 
  352             final String SEP = System.getProperty(
"line.separator");
 
  353             InputStreamReader isr;
 
  354             BufferedReader br = null;
 
  356                 isr = 
new InputStreamReader(this.is);
 
  357                 br = 
new BufferedReader(isr);
 
  359                 while (doRun && (line = br.readLine()) != null) {
 
  360                     this.output.append(line).append(SEP);
 
  362             } 
catch (
final IOException ex) {
 
  363                 logger.log(Level.WARNING, 
"Error redirecting stream to string buffer", ex); 
 
  368                     } 
catch (IOException ex) {
 
  369                         logger.log(Level.SEVERE, 
"Error closing stream reader", ex); 
 
  390             return this.output.toString();
 
  405         private final InputStream 
is;
 
  406         private volatile boolean doRun = 
false;
 
  423             final String SEP = System.getProperty(
"line.separator");
 
  424             InputStreamReader isr;
 
  425             BufferedReader br = null;
 
  427                 isr = 
new InputStreamReader(this.is);
 
  428                 br = 
new BufferedReader(isr);
 
  430                 while (doRun && (line = br.readLine()) != null) {
 
  431                     writer.append(line).append(SEP);
 
  433             } 
catch (
final IOException ex) {
 
  434                 logger.log(Level.SEVERE, 
"Error reading output and writing to file writer", ex); 
 
  444                 } 
catch (IOException ex) {
 
  445                     logger.log(Level.SEVERE, 
"Error flushing file writer", ex); 
 
ExecUtil.StreamToStringRedirect errorStringRedirect
final long startTimeInSeconds
static int execute(ProcessBuilder processBuilder, ProcessTerminator terminator)
final long maxRunTimeInSeconds
ExecUtil.StreamToWriterRedirect outputWriterRedirect
static final long DEFAULT_TIMEOUT
static int execute(ProcessBuilder processBuilder)
static final TimeUnit DEFAULT_TIMEOUT_UNITS
synchronized void execute(final Writer stdoutWriter, final String aCommand, final String...params)
TimedProcessTerminator(long maxRunTimeInSeconds)
ExecUtil.StreamToStringRedirect outputStringRedirect
static void killProcess(Process process)
List< Win32Process > getChildren()
synchronized int getExitValue()
static int execute(ProcessBuilder processBuilder, long timeOut, TimeUnit units, ProcessTerminator terminator)
static final Logger logger
final StringBuffer output
synchronized String execute(final String aCommand, final String...params)
boolean shouldTerminateProcess()
static Logger getLogger(String name)
boolean shouldTerminateProcess()