[Haskell-cafe] error handling (esp. IO/exceptions)

Donn Cave donn at avvanta.com
Tue Apr 14 18:19:50 EDT 2009


Quoth brian at lorf.org,
> On Tuesday, 14.04.09 at 22:13, Lennart Augustsson wrote:
>> So the right way to do this (like opening a file), is to try executing
>> it and let the OS tell you if it failed.
>
> I know, but the various functions that create processes don't help me
> know whether the program actually ran or not. For example,
>
>> createProcess (proc "nosuch" []) >>= \(_,_,_,ph) -> waitForProcess ph
>
> returns ExitCode 127. If I use the same code to run 'test', a C program
> that returns 127, I get the same thing.

Right, awkward problem.  Don't know if it can be solved in a portable
way, but you (as author of a createProcess-like function) can use a pipe
from the forked process.  The code demo I append works, given FFI support
for the POSIX functions it uses, but may not be appropriate for use with
GHC (I'm using nhc98.)

	Donn
------------

spawn file cmd env = do
	(e0, e1) <- pipe
	-- F_CLOEXEC: close fd on (sucessful) exec.
	fcntlSetFlag e1 F_CLOEXEC
	t <- fork (fex e0 e1)
	close e1
	rx <- readFd e0 256
	if null rx
		then return t
		else ioError (userError rx)
	where
		fex e0 e1 = do
			close e0
			catch	(execve file cmd env)
				(\ e -> writeFd e1 (ioeGetErrorString e))



More information about the Haskell-Cafe mailing list