[Haskell-cafe] ptys and hGetContent problem

Nick Bowler nbowler at elliptictech.com
Mon Mar 8 17:11:46 EST 2010

On 20:38 Mon 08 Mar     , Mathijs Kwik wrote:
> Hi all,
> I found this blogpost from Bryan O'Sullivan
> http://www.serpentine.com/blog/2008/09/30/unix-hacking-in-haskell-better-pseudoterminal-support/
> and I wanted to try it out.
> Before moving to an interactive command (which needs pty), I just did
> a small test for "ls -l /" to see if it worked.
> I got it to compile, but when running, it throws an exception when
> reaching the end of the output (in this case because I evaluate the
> length to force reading all).
> Main: /dev/ptmx: hGetContents: hardware fault (Input/output error)

You have just stumbled into the wonderful world of pseudo-terminals,
where their behaviour is subtly different on every bloody platform.  It
appears that on your platform, after the last user closes the slave port
(i.e. after your child process terminates), subsequent reads from the
master port return EIO.

One would normally detect this condition with the poll system call, by
looking for POLLHUP on the master port.

On some platforms (but evidently not yours), the last close of the slave
port causes the behaviour you seem to have expected, where a subsequent
read returns 0.

> What's wrong? :)

Presumably the problem is that handle-based I/O is not suitable for
pseudo-terminal masters.  Definitely not lazy I/O.

> And further...
> If I do want to use an interactive program which needs input, how do I
> send ctrl-d or ctrl-c?
> tail -f needs ctrl-c (or I need to kill the process)

These so-called "control characters" are normally configured by termios.
If suitably configured, the appropriate action will be performed when
the control characters are written to the master port.

Nick Bowler, Elliptic Technologies (http://www.elliptictech.com/)

More information about the Haskell-Cafe mailing list