[erlang-questions] file descriptors and ioctls

Hugo Mills hugo@REDACTED
Mon Sep 16 15:16:34 CEST 2013

   Is there a way of either turning a POSIX file descriptor into an
erlang (raw) file descriptor or vice-versa? Alternatively, is there an
ioctl interface for erlang that will allow me to call ioctls on an
open file?

   I'm trying to talk to some hardware (it's a DVB receiver) -- most
of the time, I can just read from an open file (e.g. device node) to
get data from it, but occasionally I need to be able to call an ioctl
to change its configuration. The most attractive approach I can see
here is to write a NIF which will perform the ioctl(s) on an
already-opened file.

   Clearly, if I use file:open(..., [raw]), it's dealing with
something close to a traditional POSIX filehandle -- but the returned
file:fd() is opaque in the latest documentation, so it's not obvious
how, if at all, I can go from that to a POSIX fd reliably and with a
reasonable degree of future-proofing.

   Going the other way (opening the file in the NIF and passing back a
POSIX fd, to convert to a raw file:fd()) is also not clear to me
whether it can be done.

   I note that open_port({fd, In, Out}) exists, but it looks like it
probably shouldn't be used, from the slightly off-putting description
in the manual.

   The final option, which will definitely work, is to write the whole
thing in C -- open the file, handle the ioctls and read the data --
and run it as a port, but that puts most of that part of the
application in C instead of erlang, and I'd quite like to avoid that,
given that my main goal here is to learn how to write erlang programs. :)

   Can anyone offer me any advice or comment on the above options and


=== Hugo Mills: hugo@REDACTED carfax.org.uk | darksatanic.net | lug.org.uk ===
  PGP key: 65E74AC0 from wwwkeys.eu.pgp.net or http://www.carfax.org.uk
  --- Attempted murder, now honestly, what is that?  Do they give a ---  
                  Nobel Prize for attempted chemistry?                   
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 828 bytes
Desc: Digital signature
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130916/4abb4084/attachment.bin>

More information about the erlang-questions mailing list