<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - ExecuteAndWait fails when called second time on OSX"
   href="http://llvm.org/bugs/show_bug.cgi?id=18869">18869</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>ExecuteAndWait fails when called second time on OSX
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Macintosh
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>MacOS X
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>Support Libraries
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>elviss@elviss.lv
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The second call to ExecuteAndWait returns -1, because Wait sets ChildPid to -1,
and catches EINTR the first time it is called. Subsequent Wait calls return pid
of previous processes and fail.
The following code produces the error on OSX, no problems for other platforms.

int main()
{
    const char* args0[] = { "/bin/ls", NULL};
    std::cout << llvm::sys::ExecuteAndWait(args0[0], args0) << "\n";

    //wait until the first one finishes
    sleep(1);

    const char* args1[] = { "/bin/pwd", NULL};
    std::cout << llvm::sys::ExecuteAndWait(args1[0], args1) << "\n";

    return 0;
}

Commenting line 335 in /lib/Support/Unix/Program.inc (ChildPid = -1; // mimic a
wait() using waitpid()) fixes the problem and I think it is OK to wait only for
the launched process not for any process to finish. LLVM 3.3 was looping until
waitpid returned the correct pid, that way Wait also works correctly.</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>