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()) {
168 }
catch (InterruptedException exx) {
169 Logger.
getLogger(
ExecUtil.class.getName()).log(Level.INFO, String.format(
"Wait for process termination following killProcess was interrupted for command %s", processBuilder.command().get(0)));
172 }
while (process.isAlive());
173 }
catch (InterruptedException ex) {
174 if (process.isAlive()) {
179 }
catch (InterruptedException exx) {
180 Logger.
getLogger(
ExecUtil.class.getName()).log(Level.INFO, String.format(
"Wait for process termination following killProcess was interrupted for command %s", processBuilder.command().get(0)));
182 Logger.
getLogger(
ExecUtil.class.getName()).log(Level.INFO,
"Thread interrupted while running {0}", processBuilder.command().get(0));
183 Thread.currentThread().interrupt();
185 return process.exitValue();
194 if (process == null) {
201 List<Win32Process> children = parentProcess.
getChildren();
203 children.stream().forEach((child) -> {
208 process.destroyForcibly();
210 }
catch (Exception ex) {
211 logger.log(Level.WARNING,
"Error occurred when attempting to kill process: {0}", ex.getMessage());
236 public synchronized String
execute(
final String aCommand,
final String... params) throws IOException, InterruptedException {
238 String[] arrayCommand =
new String[params.length + 1];
239 arrayCommand[0] = aCommand;
241 StringBuilder arrayCommandToLog =
new StringBuilder();
242 arrayCommandToLog.append(aCommand).append(
" ");
244 for (
int i = 1; i < arrayCommand.length; i++) {
245 arrayCommand[i] = params[i - 1];
246 arrayCommandToLog.append(arrayCommand[i]).append(
" ");
249 final Runtime rt = Runtime.getRuntime();
250 logger.log(Level.INFO,
"Executing {0}", arrayCommandToLog.toString());
252 proc = rt.exec(arrayCommand);
263 this.exitValue = proc.waitFor();
283 public synchronized void execute(
final Writer stdoutWriter,
final String aCommand,
final String... params) throws IOException, InterruptedException {
286 String[] arrayCommand =
new String[params.length + 1];
287 arrayCommand[0] = aCommand;
289 StringBuilder arrayCommandToLog =
new StringBuilder();
290 arrayCommandToLog.append(aCommand).append(
" ");
292 for (
int i = 1; i < arrayCommand.length; i++) {
293 arrayCommand[i] = params[i - 1];
294 arrayCommandToLog.append(arrayCommand[i]).append(
" ");
297 final Runtime rt = Runtime.getRuntime();
298 logger.log(Level.INFO,
"Executing {0}", arrayCommandToLog.toString());
300 proc = rt.exec(arrayCommand);
311 this.exitValue = proc.waitFor();
312 logger.log(Level.INFO,
"{0} exit value: {1}",
new Object[]{aCommand, exitValue});
326 public synchronized void stop() {
369 private final InputStream
is;
370 private final StringBuffer
output =
new StringBuffer();
371 private volatile boolean doRun =
false;
386 final String SEP = System.getProperty(
"line.separator");
387 InputStreamReader isr;
388 BufferedReader br = null;
390 isr =
new InputStreamReader(this.is);
391 br =
new BufferedReader(isr);
393 while (doRun && (line = br.readLine()) != null) {
394 this.output.append(line).append(SEP);
396 }
catch (
final IOException ex) {
397 logger.log(Level.WARNING,
"Error redirecting stream to string buffer", ex);
402 }
catch (IOException ex) {
403 logger.log(Level.SEVERE,
"Error closing stream reader", ex);
424 return this.output.toString();
439 private final InputStream
is;
440 private volatile boolean doRun =
false;
457 final String SEP = System.getProperty(
"line.separator");
458 InputStreamReader isr;
459 BufferedReader br = null;
461 isr =
new InputStreamReader(this.is);
462 br =
new BufferedReader(isr);
464 while (doRun && (line = br.readLine()) != null) {
465 writer.append(line).append(SEP);
467 }
catch (
final IOException ex) {
468 logger.log(Level.SEVERE,
"Error reading output and writing to file writer", ex);
478 }
catch (IOException ex) {
479 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()