Fork/exec in Haskell

Here’s some simple code I put together. I’m mostly posting it so I won’t have any problems finding it in the future.

It’d be really nice to be able to, after the fork, close all open file descriptors in the child. But how can I find all the open file descriptors in a process? Ideally it should be fairly portable, though portability to major Unix/Linux systems is enough for me.

⟸ JSON in Haskell Making a choice from a list in Haskell, Vty (part 5, the last one) ⟹

bbb

The classic Unix way to do this is to find out the maximum valid file descriptor and then close all of them in a for loop (e.g., see bash). Looking over System.Posix.IO, it seems that it is lacking a function of the sort

unsafeMakeFd :: Int -> Fd

to replicate that strategy.

Twan van Laarhoven

Why are you using fork/exec as opposed to System.Process?

ephemient

  1. Just try to close all FDs up to getdtablesize() or sysconf(_SC_OPEN_MAX). Slow but mostly portable.

  2. Look at /proc/self/fd/. Faster but not as portable.

Magnus

@Twan, For this example there isn’t much reason, but in many cases it is nice to separate fork and exec.

@ephemient, Yes, it was getdtablesize() I was looking for, but couldn’t find.

@Blame, Huh?

ifp5

I’d better use /dev/fd/* instead of /proc/self/*, it’s much more portable.

Magnus

@bbb, It seems rather silly to iterate over all valid file descriptors, it’d be much nicer to only iterate over all open file descriptors. I’ll have to take a look at the bash code though, for educational purposes.

@ifp5, Yes, that sounds like a good way to go.

Leave a comment