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;
73 return Thread.currentThread().isInterrupted();
94 this.startTimeInSeconds = (
new Date().getTime()) / 1000;
107 this.maxRunTimeInSeconds = null;
109 this.startTimeInSeconds = (
new Date().getTime()) / 1000;
114 if (maxRunTimeInSeconds != null) {
115 long currentTimeInSeconds = (
new Date().getTime()) / 1000;
145 public static int execute(ProcessBuilder processBuilder)
throws SecurityException, IOException {
148 public boolean shouldTerminateProcess() {
207 public static int execute(ProcessBuilder processBuilder,
long terminationCheckInterval, TimeUnit units,
ProcessTerminator terminator)
throws SecurityException, IOException {
208 return waitForTermination(processBuilder.command().get(0), processBuilder.start(), terminationCheckInterval, units, terminator);
239 return waitForProcess(processName, process, terminationCheckInterval, units, terminator);
240 }
catch (InterruptedException ex) {
245 Thread.currentThread().interrupt();
246 throw new IOException(String.format(
"Interrupted executing %s", processName), ex);
271 private static int waitForProcess(String processName, Process process,
long terminationCheckInterval, TimeUnit units,
ProcessTerminator terminator)
throws IOException, InterruptedException {
274 process.waitFor(terminationCheckInterval, units);
275 }
catch (InterruptedException ex) {
276 logger.log(Level.WARNING, String.format(
"Interrupted executing %s", processName), ex);
277 Thread.currentThread().interrupt();
289 if (process.isAlive() && terminator.shouldTerminateProcess()) {
292 }
while (process.isAlive());
301 return process.exitValue();
318 String processName = process.toString();
321 }
catch (IOException ex) {
322 logger.log(Level.WARNING, String.format(
"Error occured executing %s", processName), ex);
323 }
catch (InterruptedException ex) {
324 logger.log(Level.WARNING, String.format(
"Interrupted executing %s", processName), ex);
325 Thread.currentThread().interrupt();
342 private static void terminateProcess(String processName, Process process)
throws IOException, InterruptedException {
343 if (process == null || !process.isAlive()) {
350 List<Win32Process> children = parentProcess.
getChildren();
351 children.stream().forEach((child) -> {
355 }
catch (Exception ex) {
360 throw new IOException(String.format(
"Error occured terminating %s", processName), ex);
363 process.destroyForcibly();
366 if (!process.waitFor(MAX_WAIT_FOR_TERMINATION, MAX_WAIT_FOR_TERMINATION_UNITS)) {
367 throw new IOException(String.format(
"Failed to terminate %s after %d %s", processName, MAX_WAIT_FOR_TERMINATION, MAX_WAIT_FOR_TERMINATION_UNITS));
395 public synchronized String
execute(
final String aCommand,
final String... params) throws IOException, InterruptedException {
397 String[] arrayCommand =
new String[params.length + 1];
398 arrayCommand[0] = aCommand;
400 StringBuilder arrayCommandToLog =
new StringBuilder();
401 arrayCommandToLog.append(aCommand).append(
" ");
403 for (
int i = 1; i < arrayCommand.length; i++) {
404 arrayCommand[i] = params[i - 1];
405 arrayCommandToLog.append(arrayCommand[i]).append(
" ");
408 final Runtime rt = Runtime.getRuntime();
409 logger.log(Level.INFO,
"Executing {0}", arrayCommandToLog.toString());
411 proc = rt.exec(arrayCommand);
422 this.exitValue = proc.waitFor();
443 public synchronized void execute(
final Writer stdoutWriter,
final String aCommand,
final String... params) throws IOException, InterruptedException {
446 String[] arrayCommand =
new String[params.length + 1];
447 arrayCommand[0] = aCommand;
449 StringBuilder arrayCommandToLog =
new StringBuilder();
450 arrayCommandToLog.append(aCommand).append(
" ");
452 for (
int i = 1; i < arrayCommand.length; i++) {
453 arrayCommand[i] = params[i - 1];
454 arrayCommandToLog.append(arrayCommand[i]).append(
" ");
457 final Runtime rt = Runtime.getRuntime();
458 logger.log(Level.INFO,
"Executing {0}", arrayCommandToLog.toString());
460 proc = rt.exec(arrayCommand);
471 this.exitValue = proc.waitFor();
472 logger.log(Level.INFO,
"{0} exit value: {1}",
new Object[]{aCommand, exitValue});
486 public synchronized void stop() {
529 private final InputStream
is;
530 private final StringBuffer
output =
new StringBuffer();
531 private volatile boolean doRun =
false;
546 final String SEP = System.getProperty(
"line.separator");
547 InputStreamReader isr;
548 BufferedReader br = null;
550 isr =
new InputStreamReader(this.is);
551 br =
new BufferedReader(isr);
553 while (doRun && (line = br.readLine()) != null) {
554 this.output.append(line).append(SEP);
556 }
catch (
final IOException ex) {
557 logger.log(Level.WARNING,
"Error redirecting stream to string buffer", ex);
562 }
catch (IOException ex) {
563 logger.log(Level.SEVERE,
"Error closing stream reader", ex);
584 return this.output.toString();
599 private final InputStream
is;
600 private volatile boolean doRun =
false;
617 final String SEP = System.getProperty(
"line.separator");
618 InputStreamReader isr;
619 BufferedReader br = null;
621 isr =
new InputStreamReader(this.is);
622 br =
new BufferedReader(isr);
624 while (doRun && (line = br.readLine()) != null) {
625 writer.append(line).append(SEP);
627 }
catch (
final IOException ex) {
628 logger.log(Level.SEVERE,
"Error reading output and writing to file writer", ex);
638 }
catch (IOException ex) {
639 logger.log(Level.SEVERE,
"Error flushing file writer", ex);
ExecUtil.StreamToStringRedirect errorStringRedirect
static int execute(ProcessBuilder processBuilder, long terminationCheckInterval, TimeUnit units, ProcessTerminator terminator)
final long startTimeInSeconds
static int execute(ProcessBuilder processBuilder, ProcessTerminator terminator)
ExecUtil.StreamToWriterRedirect outputWriterRedirect
static int execute(ProcessBuilder processBuilder)
static int getProcessTimeOutHrs()
synchronized void execute(final Writer stdoutWriter, final String aCommand, final String...params)
static int waitForTermination(String processName, Process process, long terminationCheckInterval, TimeUnit units, ProcessTerminator terminator)
static void terminateProcess(String processName, Process process)
TimedProcessTerminator(long maxRunTimeInSeconds)
ExecUtil.StreamToStringRedirect outputStringRedirect
static void killProcess(Process process)
List< Win32Process > getChildren()
static boolean getIsTimeOutEnabled()
static final TimeUnit MAX_WAIT_FOR_TERMINATION_UNITS
static int waitForProcess(String processName, Process process, long terminationCheckInterval, TimeUnit units, ProcessTerminator terminator)
synchronized int getExitValue()
final Long maxRunTimeInSeconds
static final long MAX_WAIT_FOR_TERMINATION
static final long DEFAULT_TERMINATION_CHECK_INTERVAL
synchronized static Logger getLogger(String name)
static final TimeUnit DEFAULT_TERMINATION_CHECK_INTERVAL_UNITS
boolean shouldTerminateProcess()
static final Logger logger
final StringBuffer output
synchronized String execute(final String aCommand, final String...params)
boolean shouldTerminateProcess()
boolean shouldTerminateProcess()