mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

boli
06.39.2008 80fde82fa9aaa353e8c0425cfdce8627b1271cbe
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.

Fix for issue 3009
1 files modified
83 ■■■■ changed files
opends/src/server/org/opends/server/util/StaticUtils.java 83 ●●●● patch | view | raw | blame | history
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,19 +2392,25 @@
    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
      {
        return process.waitFor();
      }
      catch (InterruptedException ie)
      while((line = reader.readLine()) != null)
      {
        if (debugEnabled())
        if(output != null)
        {
          TRACER.debugCaught(DebugLogLevel.ERROR, ie);
          output.add(line);
        }
      }
    }
    catch(IOException ioe)
    {
        // If this happens, then we have no choice but to forcefully terminate
        // the process.
        try
@@ -2414,30 +2425,7 @@
          }
        }
        return process.exitValue();
      }
    }
    else
    {
      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);
          }
        }
      throw ioe;
      }
      finally
      {
@@ -2445,7 +2433,7 @@
        {
          reader.close();
        }
        catch (Exception e)
      catch(IOException e)
        {
          if (debugEnabled())
          {
@@ -2454,35 +2442,8 @@
        }
      }
      try
      {
        return process.waitFor();
      }
      catch (InterruptedException ie)
      {
        if (debugEnabled())
        {
          TRACER.debugCaught(DebugLogLevel.ERROR, ie);
        }
        // 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();
      }
    }
  }