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;
 
   78             this.startTimeInSeconds = (
new Date().getTime()) / 1000;
 
   94                 this.maxRunTimeInSeconds = Long.MAX_VALUE;
 
   96             this.startTimeInSeconds = (
new Date().getTime()) / 1000;
 
  101             long currentTimeInSeconds = (
new Date().getTime()) / 1000;
 
  118     public static int execute(ProcessBuilder processBuilder) 
throws SecurityException, IOException {
 
  121             public boolean shouldTerminateProcess() {
 
  159     public static int execute(ProcessBuilder processBuilder, 
long timeOut, TimeUnit units, 
ProcessTerminator terminator) 
throws SecurityException, IOException {
 
  160         Process process = processBuilder.start();
 
  163                 process.waitFor(timeOut, units);
 
  164                 if (process.isAlive() && terminator.shouldTerminateProcess()) {
 
  167             } 
while (process.isAlive());
 
  168         } 
catch (InterruptedException ex) {
 
  169             if (process.isAlive()) {
 
  172             Logger.
getLogger(
ExecUtil.class.getName()).log(Level.INFO, 
"Thread interrupted while running {0}", processBuilder.command().get(0)); 
 
  173             Thread.currentThread().interrupt();
 
  175         return process.exitValue();
 
  184         if (process == null) {
 
  191                 List<Win32Process> children = parentProcess.
getChildren();
 
  193                 children.stream().forEach((child) -> {
 
  198                 process.destroyForcibly();
 
  200         } 
catch (Exception ex) {
 
  201             logger.log(Level.WARNING, 
"Error occurred when attempting to kill process: {0}", ex.getMessage()); 
 
  226     public synchronized String 
execute(
final String aCommand, 
final String... params) throws IOException, InterruptedException {
 
  228         String[] arrayCommand = 
new String[params.length + 1];
 
  229         arrayCommand[0] = aCommand;
 
  231         StringBuilder arrayCommandToLog = 
new StringBuilder();
 
  232         arrayCommandToLog.append(aCommand).append(
" ");
 
  234         for (
int i = 1; i < arrayCommand.length; i++) {
 
  235             arrayCommand[i] = params[i - 1];
 
  236             arrayCommandToLog.append(arrayCommand[i]).append(
" ");
 
  239         final Runtime rt = Runtime.getRuntime();
 
  240         logger.log(Level.INFO, 
"Executing {0}", arrayCommandToLog.toString()); 
 
  242         proc = rt.exec(arrayCommand);
 
  253         this.exitValue = proc.waitFor();
 
  273     public synchronized void execute(
final Writer stdoutWriter, 
final String aCommand, 
final String... params) throws IOException, InterruptedException {
 
  276         String[] arrayCommand = 
new String[params.length + 1];
 
  277         arrayCommand[0] = aCommand;
 
  279         StringBuilder arrayCommandToLog = 
new StringBuilder();
 
  280         arrayCommandToLog.append(aCommand).append(
" ");
 
  282         for (
int i = 1; i < arrayCommand.length; i++) {
 
  283             arrayCommand[i] = params[i - 1];
 
  284             arrayCommandToLog.append(arrayCommand[i]).append(
" ");
 
  287         final Runtime rt = Runtime.getRuntime();
 
  288         logger.log(Level.INFO, 
"Executing {0}", arrayCommandToLog.toString()); 
 
  290         proc = rt.exec(arrayCommand);
 
  301         this.exitValue = proc.waitFor();
 
  302         logger.log(Level.INFO, 
"{0} exit value: {1}", 
new Object[]{aCommand, exitValue}); 
 
  316     public synchronized void stop() {
 
  359         private final InputStream 
is;
 
  360         private final StringBuffer 
output = 
new StringBuffer();
 
  361         private volatile boolean doRun = 
false;
 
  376             final String SEP = System.getProperty(
"line.separator");
 
  377             InputStreamReader isr;
 
  378             BufferedReader br = null;
 
  380                 isr = 
new InputStreamReader(this.is);
 
  381                 br = 
new BufferedReader(isr);
 
  383                 while (doRun && (line = br.readLine()) != null) {
 
  384                     this.output.append(line).append(SEP);
 
  386             } 
catch (
final IOException ex) {
 
  387                 logger.log(Level.WARNING, 
"Error redirecting stream to string buffer", ex); 
 
  392                     } 
catch (IOException ex) {
 
  393                         logger.log(Level.SEVERE, 
"Error closing stream reader", ex); 
 
  414             return this.output.toString();
 
  429         private final InputStream 
is;
 
  430         private volatile boolean doRun = 
false;
 
  447             final String SEP = System.getProperty(
"line.separator");
 
  448             InputStreamReader isr;
 
  449             BufferedReader br = null;
 
  451                 isr = 
new InputStreamReader(this.is);
 
  452                 br = 
new BufferedReader(isr);
 
  454                 while (doRun && (line = br.readLine()) != null) {
 
  455                     writer.append(line).append(SEP);
 
  457             } 
catch (
final IOException ex) {
 
  458                 logger.log(Level.SEVERE, 
"Error reading output and writing to file writer", ex); 
 
  468                 } 
catch (IOException ex) {
 
  469                     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 int getProcessTimeOutHrs()
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()
static boolean getIsTimeOutEnabled()
synchronized int getExitValue()
synchronized static Logger getLogger(String name)
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()
boolean shouldTerminateProcess()