How to read / write from erlang to named pipe?

I need an erlang application to read and write on a named pipe.

Opening a named pipe as a file will result in an error with eisdir .

I wrote the following module, but it is fragile and feels wrong in many ways. Also after a while he does not read. Is there any way to make it more ... elegant?

 -module(port_forwarder). -export([start/2, forwarder/2]). -include("logger.hrl"). start(From, To)-> spawn(fun() -> forwarder(From, To) end). forwarder(FromFile, ToFile) -> To = open_port({spawn,"/bin/cat > " ++ ToFifo}, [binary, out, eof,{packet, 4}]), From = open_port({spawn,"/bin/cat " ++ FromFifo}, [binary, in, eof, {packet, 4}]), forwarder(From, To, nil). forwarder(From, To, Pid) -> receive {Manager, {command, Bin}} -> ?ERROR("Sending : ~p", [Bin]), To ! {self(), {command, Bin}}, forwarder(From, To, Manager); {From ,{data,Data}} -> Pid ! {self(), {data, Data}}, forwarder(From, To, Pid); E -> ?ERROR("Quitting, first message not understood : ~p", [E]) end. 

As you may have noticed, it mimics the port format in what it accepts or returns. I want it to replace the C code, which will read the other ends of the pipes and run from the debugger.

+4
source share
2 answers

I think the eisdir failure comes from this code if you are running on Unix.

https://github.com/erlang/otp/blob/master/erts/emulator/drivers/unix/unix_efile.c

See efile_openfile and efile_may_openfile . They both perform checks assuming that the file !IS_REG(f) is a directory. This seems like a mistake, but there may be good reasons not to open irregular files. It is interesting to note kludge for /dev/null .

I was also struck by this problem. Maybe it's time to scratch the itch.

+2
source

I just ran into this problem. If other users find this topic in the future, the reason Erlang does not support opening named pipes is the same as why device files cannot be opened. This link summarizes the rationale:

http://www.erlang.org/faq/problems.html#id56464

+2
source

Source: https://habr.com/ru/post/1306926/


All Articles