From 80fde82fa9aaa353e8c0425cfdce8627b1271cbe Mon Sep 17 00:00:00 2001
From: boli <boli@localhost>
Date: Thu, 06 Mar 2008 22:39:36 +0000
Subject: [PATCH] Fix for potential deadlock in StaticUtils.exec method. This first showed up with issue 2116 but apparently the fix was not adequate to fully address the problem. This problem surfaced now because executing chmod apparently produced output on stderr and/or stdout which was not read by StaticUtils.exec when no output was expected.
---
opends/src/server/org/opends/server/util/StaticUtils.java | 113 ++++++++++++++++++--------------------------------------
1 files changed, 37 insertions(+), 76 deletions(-)
diff --git a/opends/src/server/org/opends/server/util/StaticUtils.java b/opends/src/server/org/opends/server/util/StaticUtils.java
index 3be3a1c..4ff81da 100644
--- a/opends/src/server/org/opends/server/util/StaticUtils.java
+++ b/opends/src/server/org/opends/server/util/StaticUtils.java
@@ -2348,10 +2348,15 @@
*
* @throws SecurityException If the security policy will not allow the
* command to be executed.
+ *
+ * @throws InterruptedException If the current thread is interrupted by
+ * another thread while it is waiting, then
+ * the wait is ended and an InterruptedException
+ * is thrown.
*/
public static int exec(String command, String[] args, File workingDirectory,
Map<String,String> environment, List<String> output)
- throws IOException, SecurityException
+ throws IOException, SecurityException, InterruptedException
{
// See whether we'll allow the use of exec on this system. If not, then
// throw an exception.
@@ -2387,101 +2392,57 @@
Process process = processBuilder.start();
- if (output == null)
+ // We must exhaust stdout and stderr before calling waitfor. Since we
+ // redirected the error stream, we just have to read from stdout.
+ InputStream processStream = process.getInputStream();
+ BufferedReader reader =
+ new BufferedReader(new InputStreamReader(processStream));
+ String line = null;
+
+ try
{
+ while((line = reader.readLine()) != null)
+ {
+ if(output != null)
+ {
+ output.add(line);
+ }
+ }
+ }
+ catch(IOException ioe)
+ {
+ // If this happens, then we have no choice but to forcefully terminate
+ // the process.
try
{
- return process.waitFor();
+ process.destroy();
}
- catch (InterruptedException ie)
+ catch (Exception e)
{
if (debugEnabled())
{
- TRACER.debugCaught(DebugLogLevel.ERROR, ie);
+ TRACER.debugCaught(DebugLogLevel.ERROR, e);
}
-
- // If this happens, then we have no choice but to forcefully terminate
- // the process.
- try
- {
- process.destroy();
- }
- catch (Exception e)
- {
- if (debugEnabled())
- {
- TRACER.debugCaught(DebugLogLevel.ERROR, e);
- }
- }
-
- return process.exitValue();
}
+
+ throw ioe;
}
- else
+ finally
{
- InputStream processStream = process.getInputStream();
- BufferedReader reader =
- new BufferedReader(new InputStreamReader(
- process.getInputStream()));
-
try
{
- while (processStream.available() > 0)
- {
- String line = reader.readLine();
- if (line == null)
- {
- break;
- }
- else
- {
- output.add(line);
- }
- }
+ reader.close();
}
- finally
- {
- try
- {
- reader.close();
- }
- catch (Exception e)
- {
- if (debugEnabled())
- {
- TRACER.debugCaught(DebugLogLevel.ERROR, e);
- }
- }
- }
-
- try
- {
- return process.waitFor();
- }
- catch (InterruptedException ie)
+ catch(IOException e)
{
if (debugEnabled())
{
- TRACER.debugCaught(DebugLogLevel.ERROR, ie);
+ TRACER.debugCaught(DebugLogLevel.ERROR, e);
}
-
- // If this happens, then we have no choice but to forcefully terminate
- // the process.
- try
- {
- process.destroy();
- }
- catch (Exception e)
- {
- if (debugEnabled())
- {
- TRACER.debugCaught(DebugLogLevel.ERROR, e);
- }
- }
-
- return process.exitValue();
}
}
+
+ return process.waitFor();
}
--
Gitblit v1.10.0