[Haskell-cafe] Discussion: The CLOEXEC problem

Niklas Hambüchen mail at nh2.me
Sun Aug 30 13:42:07 UTC 2015

On 30/08/15 15:19, Alexander Kjeldaas wrote:
> The directory is irrelevant.  fork() + exec() is not an atomic operation:
> * Thread 1 writes its file completely (opens and closes an Fd 1, using
> * Thread 2 starts writing its file (Fd 2 open for writing, using O_CLOEXEC)
> * Thread 1 starts executing "myBinary" by calling *fork()*. Fd 2 is
> inherited by the child process
> * Thread 2 finishes writing (closes its Fd 2)
> * Thread 2 executes "myBinary", which fails with `Text file busy`
> because an Fd is still open to it in the child of Process 1
> * Thread 1 executes "myBinary" (calling exec()). Fd 2 is automatically
> closed during exec(), but it's too late.
> You need the file descriptor to not be inherited by a child process,
> which is != from O_CLOEXEC.

You are right. This makes solving my original problem impossible, and

* writing the file
* then renaming it
* then executing it

seems to be the only way to do it safely.

Let us then move the discussion back to whether CLOEXEC by default or not.

More information about the Haskell-Cafe mailing list