[erlang-questions] open_port and raw mode (zlogin)
Per Hedeland
per@REDACTED
Sun Jan 27 14:07:00 CET 2013
Patrik Nyblom <pan@REDACTED> wrote:
>
>On 01/25/2013 07:13 PM, Heinz Nikolaus Gies wrote:
[snip]
>>
>> I've tracked down that a bit further into the zlogin code and it seems
>> to be related to a call to tcgetattr(fd, &term) in the code which
>> fails with the open_socket created connection.
>>
>> I'm a bit at a loss here what to do next sadly, any advice?
>>
>It seems zlogin expects a terminal as stdin, but if you open_port, stdin
>for the program will be a pipe, which you cannot do tcgetattr on. You
>will need to either find an option to zlogin that expects input from
>something not a terminal, or create a wrapper program that creates a pty
>, starts zlogin and forwards data from/to the pty and the pipe (which
>would be some hacking, but not impossible, that's basically what run_erl
>does, but to/from a named pipe - possibly there's already a program out
>there doing that, maybe someone on the list can enlighten us :))
Yes, there is actually such a wrapper program included with the Lux tool
that Håkan Mattson announced here a while back (in
http://erlang.org/pipermail/erlang-questions/2012-May/066891.html).
It's called 'runpty' and can be found at
https://github.com/hawk/lux/tree/master/c_src - a little demo:
29> P = open_port({spawn, "/bin/sh"},[]).
#Port<0.519>
30> P ! {self(), {command, "tty\n"}}.
{<0.57.0>,{command,"tty\n"}}
31> flush().
Shell got {#Port<0.519>,{data,"not a tty\n"}}
ok
32> erlang:port_close(P).
true
33> f(P).
ok
34> P = open_port({spawn, "runpty /bin/sh"},[]).
#Port<0.525>
35> P ! {self(), {command, "tty\n"}}.
{<0.57.0>,{command,"tty\n"}}
36> flush().
Shell got {#Port<0.525>,{data,"$ "}}
Shell got {#Port<0.525>,{data,"tty\r\n"}}
Shell got {#Port<0.525>,{data,"/dev/pts/12\r\n"}}
Shell got {#Port<0.525>,{data,"$ "}}
ok
37>
Note well that besides tty(1) working, the presence of a tty also (by
default) results in echo, pesky CRLF line termination, and an
interactive shell. It should work fine to just pass "runpty zlogin ..."
to open_port/2 though, but I haven't tried that.
--Per Hedeland
More information about the erlang-questions
mailing list