Getting the file descriptor from a handle, without closing it

Volker Wysk pf3 at
Sat Oct 1 08:30:40 CEST 2011


I need to get the file descriptors of some handles, but the handles should not 
be modified. They should not be closed by the operation.

I guess, that the handle gets closed for a reason. But I'm using the revealed 
file descriptors in a way which should pose no problems for the integrity of 
the GHC library.

In order to make a non-closing handleToFd function, I'm studying GHC's library 
source code. But there's a lot which I can't make much sense, because it uses 
non-standard language features (GHC extensions), which I'm not familiar with.

I don't mean you to explain these GHC extensions to me. I just want to give an 
impression of the problems I encounter:


data FD = FD {
  fdFD :: {-# UNPACK #-} !CInt,
  fdIsNonBlocking :: {-# UNPACK #-} !Int

What is that exclamation mark? And that "{-# UNPACK #-}"?


handleToFd' :: Handle -> Handle__ -> IO (Handle__, Fd)
handleToFd' h h_ at Handle__{haType=_,..} = do
  case cast haDevice of
       -- ...

haDevice should be a function. How could you cast it?


data Handle__
  = forall dev enc_state dec_state . 
      (IODevice dev, BufferedIO dev, Typeable dev) =>
    Handle__ {
      -- ...
    deriving Typeable

What's that "forall" thing?


handleToFd' h h_ at Handle__{haType=_,..} = do
  case cast haDevice of
    Nothing -> -- ...
    Just fd -> do
      -- ...
      return (Handle__{haType=ClosedHandle,..}, 
                                 Fd (fromIntegral (FD.fdFD fd)))

What's this ".." inside "Handle__{haType=ClosedHandle,..}"?

If anyone can point out to me, how this non-blocking handleToFd function 
should be made, I would be grateful.

Volker Wysk

More information about the Glasgow-haskell-users mailing list