[Haskell-cafe] createProcess running non-existent programs

Donn Cave donn at avvanta.com
Wed Aug 15 07:25:41 CEST 2012

Quoth Alexander Kjeldaas <alexander.kjeldaas at gmail.com>,

> See access(2)

... a classic "code smell" in UNIX programming, for the same reasons.

We can solve this problem in an efficient way that works well, and equally
well, on any POSIX platform that supports F_CLOEXEC on pipes, and I can't
think of anything that doesn't.  The appended code is the basic gist of it.
This was not invented by the Python world, but as suggested it's one of
the things that we'd get from a review of their subprocess module.


spawn file cmd env = do
  (e0, e1) <- pipe
  fcntlSetFlag e1 F_CLOEXEC
  t <- fork (fex e0 e1)
  close e1
  rx <- readFd e0 256
  if null rx
     then return t
     else ioerr (chrToErrType rx) file
     fex e0 e1 = do
       close e0
       catch  (execve file cmd env)
              (\ e -> writeFd e1 (errToChr e : ioeGetErrorString e))
     ioerr (e, s) file = ioError (mkIOError e s Nothing (Just file))

More information about the Haskell-Cafe mailing list