[erlang-questions] file descriptors and ioctls

Geoff Cant nem@REDACTED
Mon Sep 16 20:02:12 CEST 2013


You can open existing file descriptors as an erlang port by either doing erlang:open_port({fd, InFD, OutFD}, [...]), or you could use something like msantos' procket library ( https://github.com/msantos/procket ).

Procket is probably more appropriate as it has ioctl support.

Cheers,
--
Geoff

On 2013-09-16, at 06:16 , Hugo Mills <hugo@REDACTED> wrote:

>   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
> observations?
> 
>   Thanks,
>   Hugo.








More information about the erlang-questions mailing list