19package org.sleuthkit.autopsy.coreutils;
21import java.io.BufferedReader;
22import java.io.IOException;
23import java.io.InputStream;
24import java.io.InputStreamReader;
28import java.util.concurrent.TimeUnit;
29import java.util.logging.Level;
30import org.apache.commons.lang3.SystemUtils;
31import org.sleuthkit.autopsy.core.UserPreferences;
73 return Thread.currentThread().isInterrupted();
94 this.startTimeInSeconds = (
new Date().getTime()) / 1000;
107 this.maxRunTimeInSeconds =
null;
109 this.startTimeInSeconds = (
new Date().getTime()) / 1000;
115 long currentTimeInSeconds = (
new Date().getTime()) / 1000;
116 return (currentTimeInSeconds - this.startTimeInSeconds) > this.maxRunTimeInSeconds;
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();
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() {
544 return this.exitValue;
553 private static class StreamToStringRedirect
extends Thread {
556 private final InputStream
is;
557 private final StringBuffer
output =
new StringBuffer();
558 private volatile boolean doRun =
false;
560 StreamToStringRedirect(
final InputStream anIs,
final String aType) {
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();
623 private static class StreamToWriterRedirect
extends Thread {
626 private final InputStream
is;
627 private volatile boolean doRun =
false;
630 StreamToWriterRedirect(
final InputStream anIs,
final Writer
writer) {
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);
static boolean getIsTimeOutEnabled()
static int getProcessTimeOutHrs()
boolean shouldTerminateProcess()
HybridTerminator(List< ProcessTerminator > terminators)
final List< ProcessTerminator > terminatorList
boolean shouldTerminateProcess()
final StringBuffer output
static final Logger logger
static final Logger logger
TimedProcessTerminator(long maxRunTimeInSeconds)
final long startTimeInSeconds
boolean shouldTerminateProcess()
final Long maxRunTimeInSeconds
static void killProcess(Process process)
static int execute(ProcessBuilder processBuilder, long terminationCheckInterval, TimeUnit units, ProcessTerminator terminator)
ExecUtil.StreamToStringRedirect errorStringRedirect
ExecUtil.StreamToWriterRedirect outputWriterRedirect
static int execute(ProcessBuilder processBuilder, ProcessTerminator terminator)
synchronized void execute(final Writer stdoutWriter, final String aCommand, final String... params)
static final TimeUnit DEFAULT_TERMINATION_CHECK_INTERVAL_UNITS
static int waitForProcess(String processName, Process process, long terminationCheckInterval, TimeUnit units, ProcessTerminator terminator)
static final TimeUnit MAX_WAIT_FOR_TERMINATION_UNITS
synchronized int getExitValue()
ExecUtil.StreamToStringRedirect outputStringRedirect
static int execute(ProcessBuilder processBuilder)
synchronized String execute(final String aCommand, final String... params)
static int waitForTermination(String processName, Process process, long terminationCheckInterval, TimeUnit units, ProcessTerminator terminator)
static final long MAX_WAIT_FOR_TERMINATION
static final Logger logger
static void terminateProcess(String processName, Process process)
static final long DEFAULT_TERMINATION_CHECK_INTERVAL
synchronized static Logger getLogger(String name)
List< Win32Process > getChildren()
boolean shouldTerminateProcess()