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;
70 return Thread.currentThread().isInterrupted();
91 this.startTimeInSeconds = (
new Date().getTime()) / 1000;
107 this.maxRunTimeInSeconds = Long.MAX_VALUE;
109 this.startTimeInSeconds = (
new Date().getTime()) / 1000;
114 long currentTimeInSeconds = (
new Date().getTime()) / 1000;
132 public static int execute(ProcessBuilder processBuilder)
throws SecurityException, IOException {
135 public boolean shouldTerminateProcess() {
178 public static int execute(ProcessBuilder processBuilder,
long terminationCheckInterval, TimeUnit units,
ProcessTerminator terminator)
throws SecurityException, IOException {
179 return waitForTermination(processBuilder.command().get(0), processBuilder.start(), terminationCheckInterval, units, terminator);
202 public static int waitForTermination(String processName, Process process,
long terminationCheckInterval, TimeUnit units,
ProcessTerminator terminator)
throws SecurityException, IOException {
205 process.waitFor(terminationCheckInterval, units);
206 if (process.isAlive() && terminator.shouldTerminateProcess()) {
210 }
catch (InterruptedException ex) {
211 logger.log(Level.WARNING, String.format(
"Thread running %s was interrupted before the process completed", processName), ex);
214 }
while (process.isAlive());
215 }
catch (InterruptedException ex) {
216 if (process.isAlive()) {
221 }
catch (InterruptedException exx) {
222 logger.log(Level.WARNING, String.format(
"Thread running %s was interrupted before the process completed", processName), exx);
224 logger.log(Level.WARNING, String.format(
"Thread running %s was interrupted before the process completed", processName), ex);
225 Thread.currentThread().interrupt();
227 return process.exitValue();
236 if (process == null) {
243 List<Win32Process> children = parentProcess.
getChildren();
245 children.stream().forEach((child) -> {
250 process.destroyForcibly();
252 }
catch (Exception ex) {
253 logger.log(Level.WARNING,
"Error occurred when attempting to kill process: {0}", ex.getMessage());
280 public synchronized String
execute(
final String aCommand,
final String... params) throws IOException, InterruptedException {
282 String[] arrayCommand =
new String[params.length + 1];
283 arrayCommand[0] = aCommand;
285 StringBuilder arrayCommandToLog =
new StringBuilder();
286 arrayCommandToLog.append(aCommand).append(
" ");
288 for (
int i = 1; i < arrayCommand.length; i++) {
289 arrayCommand[i] = params[i - 1];
290 arrayCommandToLog.append(arrayCommand[i]).append(
" ");
293 final Runtime rt = Runtime.getRuntime();
294 logger.log(Level.INFO,
"Executing {0}", arrayCommandToLog.toString());
296 proc = rt.exec(arrayCommand);
307 this.exitValue = proc.waitFor();
328 public synchronized void execute(
final Writer stdoutWriter,
final String aCommand,
final String... params) throws IOException, InterruptedException {
331 String[] arrayCommand =
new String[params.length + 1];
332 arrayCommand[0] = aCommand;
334 StringBuilder arrayCommandToLog =
new StringBuilder();
335 arrayCommandToLog.append(aCommand).append(
" ");
337 for (
int i = 1; i < arrayCommand.length; i++) {
338 arrayCommand[i] = params[i - 1];
339 arrayCommandToLog.append(arrayCommand[i]).append(
" ");
342 final Runtime rt = Runtime.getRuntime();
343 logger.log(Level.INFO,
"Executing {0}", arrayCommandToLog.toString());
345 proc = rt.exec(arrayCommand);
356 this.exitValue = proc.waitFor();
357 logger.log(Level.INFO,
"{0} exit value: {1}",
new Object[]{aCommand, exitValue});
371 public synchronized void stop() {
414 private final InputStream
is;
415 private final StringBuffer
output =
new StringBuffer();
416 private volatile boolean doRun =
false;
431 final String SEP = System.getProperty(
"line.separator");
432 InputStreamReader isr;
433 BufferedReader br = null;
435 isr =
new InputStreamReader(this.is);
436 br =
new BufferedReader(isr);
438 while (doRun && (line = br.readLine()) != null) {
439 this.output.append(line).append(SEP);
441 }
catch (
final IOException ex) {
442 logger.log(Level.WARNING,
"Error redirecting stream to string buffer", ex);
447 }
catch (IOException ex) {
448 logger.log(Level.SEVERE,
"Error closing stream reader", ex);
469 return this.output.toString();
484 private final InputStream
is;
485 private volatile boolean doRun =
false;
502 final String SEP = System.getProperty(
"line.separator");
503 InputStreamReader isr;
504 BufferedReader br = null;
506 isr =
new InputStreamReader(this.is);
507 br =
new BufferedReader(isr);
509 while (doRun && (line = br.readLine()) != null) {
510 writer.append(line).append(SEP);
512 }
catch (
final IOException ex) {
513 logger.log(Level.SEVERE,
"Error reading output and writing to file writer", ex);
523 }
catch (IOException ex) {
524 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)
final long maxRunTimeInSeconds
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)
TimedProcessTerminator(long maxRunTimeInSeconds)
static final long DEFAULT_CHECK_INTERVAL
ExecUtil.StreamToStringRedirect outputStringRedirect
static void killProcess(Process process)
List< Win32Process > getChildren()
static final TimeUnit DEFAULT_CHECK_INTERVAL_UNITS
static boolean getIsTimeOutEnabled()
synchronized int getExitValue()
synchronized static Logger getLogger(String name)
boolean shouldTerminateProcess()
static final Logger logger
final StringBuffer output
synchronized String execute(final String aCommand, final String...params)
boolean shouldTerminateProcess()
boolean shouldTerminateProcess()