19 package org.sleuthkit.autopsy.coreutils;
21 import java.io.BufferedReader;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.InputStreamReader;
25 import java.io.Writer;
26 import java.util.Date;
27 import java.util.List;
28 import java.util.concurrent.TimeUnit;
29 import java.util.logging.Level;
30 import org.apache.commons.lang3.SystemUtils;
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;
136 this.terminatorList = terminators;
142 if(terminator.shouldTerminateProcess()) {
172 public static int execute(ProcessBuilder processBuilder)
throws SecurityException, IOException {
175 public boolean shouldTerminateProcess() {
234 public static int execute(ProcessBuilder processBuilder,
long terminationCheckInterval, TimeUnit units,
ProcessTerminator terminator)
throws SecurityException, IOException {
235 return waitForTermination(processBuilder.command().get(0), processBuilder.start(), terminationCheckInterval, units, terminator);
266 return waitForProcess(processName, process, terminationCheckInterval, units, terminator);
267 }
catch (InterruptedException ex) {
272 Thread.currentThread().interrupt();
273 throw new IOException(String.format(
"Interrupted executing %s", processName), ex);
298 private static int waitForProcess(String processName, Process process,
long terminationCheckInterval, TimeUnit units,
ProcessTerminator terminator)
throws IOException, InterruptedException {
301 process.waitFor(terminationCheckInterval, units);
302 }
catch (InterruptedException ex) {
303 logger.log(Level.WARNING, String.format(
"Interrupted executing %s", processName), ex);
304 Thread.currentThread().interrupt();
316 if (process.isAlive() && terminator.shouldTerminateProcess()) {
319 }
while (process.isAlive());
328 return process.exitValue();
345 String processName = process.toString();
348 }
catch (IOException ex) {
349 logger.log(Level.WARNING, String.format(
"Error occured executing %s", processName), ex);
350 }
catch (InterruptedException ex) {
351 logger.log(Level.WARNING, String.format(
"Interrupted executing %s", processName), ex);
352 Thread.currentThread().interrupt();
369 private static void terminateProcess(String processName, Process process)
throws IOException, InterruptedException {
370 if (process == null || !process.isAlive()) {
374 if (SystemUtils.IS_OS_WINDOWS) {
377 List<Win32Process> children = parentProcess.
getChildren();
378 children.stream().forEach((child) -> {
382 }
catch (Exception ex) {
387 throw new IOException(String.format(
"Error occured terminating %s", processName), ex);
390 process.destroyForcibly();
393 if (!process.waitFor(MAX_WAIT_FOR_TERMINATION, MAX_WAIT_FOR_TERMINATION_UNITS)) {
394 throw new IOException(String.format(
"Failed to terminate %s after %d %s", processName, MAX_WAIT_FOR_TERMINATION, MAX_WAIT_FOR_TERMINATION_UNITS));
422 public synchronized String
execute(
final String aCommand,
final String... params) throws IOException, InterruptedException {
424 String[] arrayCommand =
new String[params.length + 1];
425 arrayCommand[0] = aCommand;
427 StringBuilder arrayCommandToLog =
new StringBuilder();
428 arrayCommandToLog.append(aCommand).append(
" ");
430 for (
int i = 1; i < arrayCommand.length; i++) {
431 arrayCommand[i] = params[i - 1];
432 arrayCommandToLog.append(arrayCommand[i]).append(
" ");
435 final Runtime rt = Runtime.getRuntime();
436 logger.log(Level.INFO,
"Executing {0}", arrayCommandToLog.toString());
438 proc = rt.exec(arrayCommand);
449 this.exitValue = proc.waitFor();
470 public synchronized void execute(
final Writer stdoutWriter,
final String aCommand,
final String... params) throws IOException, InterruptedException {
473 String[] arrayCommand =
new String[params.length + 1];
474 arrayCommand[0] = aCommand;
476 StringBuilder arrayCommandToLog =
new StringBuilder();
477 arrayCommandToLog.append(aCommand).append(
" ");
479 for (
int i = 1; i < arrayCommand.length; i++) {
480 arrayCommand[i] = params[i - 1];
481 arrayCommandToLog.append(arrayCommand[i]).append(
" ");
484 final Runtime rt = Runtime.getRuntime();
485 logger.log(Level.INFO,
"Executing {0}", arrayCommandToLog.toString());
487 proc = rt.exec(arrayCommand);
498 this.exitValue = proc.waitFor();
499 logger.log(Level.INFO,
"{0} exit value: {1}",
new Object[]{aCommand, exitValue});
513 public synchronized void stop() {
556 private final InputStream
is;
557 private final StringBuffer
output =
new StringBuffer();
558 private volatile boolean doRun =
false;
573 final String SEP = System.getProperty(
"line.separator");
574 InputStreamReader isr;
575 BufferedReader br = null;
577 isr =
new InputStreamReader(this.is);
578 br =
new BufferedReader(isr);
580 while (doRun && (line = br.readLine()) != null) {
581 this.output.append(line).append(SEP);
583 }
catch (
final IOException ex) {
584 logger.log(Level.WARNING,
"Error redirecting stream to string buffer", ex);
589 }
catch (IOException ex) {
590 logger.log(Level.SEVERE,
"Error closing stream reader", ex);
611 return this.output.toString();
626 private final InputStream
is;
627 private volatile boolean doRun =
false;
644 final String SEP = System.getProperty(
"line.separator");
645 InputStreamReader isr;
646 BufferedReader br = null;
648 isr =
new InputStreamReader(this.is);
649 br =
new BufferedReader(isr);
651 while (doRun && (line = br.readLine()) != null) {
652 writer.append(line).append(SEP);
654 }
catch (
final IOException ex) {
655 logger.log(Level.SEVERE,
"Error reading output and writing to file writer", ex);
665 }
catch (IOException ex) {
666 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()
final List< ProcessTerminator > terminatorList
boolean shouldTerminateProcess()
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()
HybridTerminator(List< ProcessTerminator > terminators)
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()