Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ex_pty #1

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 43 additions & 57 deletions lib/cli_shell.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,68 +27,43 @@ defmodule NervesSSHShell.CLI do
defp maybe_set_term({term, _, _, _, _, _}) when is_list(term),
do: [{"TERM", List.to_string(term)}]

defp maybe_set_window_size(_os_pid, nil), do: :ok

defp maybe_set_window_size(os_pid, {_term, width, height, _, _, _}) do
# set initial window size in background, as is does not seem to work
# when doing it too early after creating the process
spawn(fn ->
Process.sleep(100)
:exec.winsz(os_pid, height, width)
end)
end

defp exec_command(cmd, %{pty_opts: pty_opts, env: env}) do
case pty_opts do
nil ->
{:ok, pid, os_pid} =
:exec.run(cmd, [
:stdin,
:stdout,
:stderr,
:monitor,
env: [:clear] ++ env ++ maybe_set_term(pty_opts)
])

if pty_opts, do: maybe_set_window_size(os_pid, pty_opts)
{:ok, pid, os_pid}

pty_opts ->
{:ok, pid, os_pid} =
:exec.run(cmd, [
:stdin,
:stdout,
{:stderr, :stdout},
:pty,
:pty_echo,
:monitor,
env: [:clear] ++ env ++ maybe_set_term(pty_opts)
])

maybe_set_window_size(os_pid, pty_opts)
{:ok, pid, os_pid}
defp maybe_add_env(term_env, env) do
if Enum.any?(env, fn env ->
match?("TERM=" <> _, env) or match?({"ENV", _}, env)
end) do
env
else
term_env ++ env
end
end

def init(_) do
{:ok, %{port_pid: nil, os_pid: nil, pty_opts: nil, cid: nil, cm: nil, env: []}}
{:ok, pty} = ExPTY.start_link()

{:ok,
%{
pty: pty,
pty_opts: nil,
cid: nil,
cm: nil,
env: []
}}
end

def handle_msg({:ssh_channel_up, channel_id, connection_manager}, state) do
{:ok, %{state | cid: channel_id, cm: connection_manager}}
end

# port closed
# pty closed
def handle_msg(
{:DOWN, os_pid, :process, port_id, _},
%{os_pid: os_pid, port_pid: port_id, cm: cm, cid: cid} = state
{:EXIT, pty, _reason},
%{pty: pty, cm: cm, cid: cid} = state
) do
:ssh_connection.send_eof(cm, cid)
{:stop, cid, state}
end

def handle_msg({what, os_pid, data} = _msg, %{cm: cm, cid: cid, os_pid: os_pid} = state)
when what in [:stdout, :stderr] do
def handle_msg({pty, {:data, data}} = _msg, %{cm: cm, cid: cid, pty: pty} = state) do
:ssh_connection.send(cm, cid, data)
{:ok, state}
end
Expand All @@ -98,7 +73,13 @@ defmodule NervesSSHShell.CLI do
{:ok, state}
end

def handle_ssh_msg({:ssh_cm, cm, {:pty, cid, want_reply, pty_opts} = _msg}, state = %{cm: cm}) do
def handle_ssh_msg(
{:ssh_cm, cm, {:pty, cid, want_reply, pty_opts} = _msg},
state = %{pty: pty, cm: cm}
) do
{_term, cols, rows, _, _, pty_settings} = pty_opts
ExPTY.set_pty_opts(pty, pty_settings)
ExPTY.winsz(pty, rows, cols)
:ssh_connection.reply_request(cm, want_reply, :success, cid)

{:ok, %{state | pty_opts: pty_opts}}
Expand All @@ -115,28 +96,33 @@ defmodule NervesSSHShell.CLI do

def handle_ssh_msg(
{:ssh_cm, cm, {:exec, cid, want_reply, command}},
state = %{cm: cm, cid: cid}
state = %{pty: pty, cm: cm, cid: cid, pty_opts: pty_opts, env: env}
)
when is_list(command) do
{:ok, pid, os_pid} = exec_command(List.to_string(command), state)
ExPTY.exec(
pty,
List.to_string(command) |> OptionParser.split(),
pty_opts |> maybe_set_term() |> maybe_add_env(env)
)

:ssh_connection.reply_request(cm, want_reply, :success, cid)
{:ok, %{state | os_pid: os_pid, port_pid: pid}}
{:ok, %{state | pty: pty}}
end

def handle_ssh_msg(
{:ssh_cm, cm, {:shell, cid, want_reply} = _msg},
state = %{cm: cm, cid: cid}
state = %{pty: pty, cm: cm, cid: cid, pty_opts: pty_opts, env: env}
) do
{:ok, pid, os_pid} = exec_command(get_shell_command(), state)
ExPTY.exec(pty, get_shell_command(), pty_opts |> maybe_set_term() |> maybe_add_env(env))
:ssh_connection.reply_request(cm, want_reply, :success, cid)
{:ok, %{state | os_pid: os_pid, port_pid: pid}}
{:ok, %{state | pty: pty}}
end

def handle_ssh_msg(
{:ssh_cm, _cm, {:data, channel_id, 0, data}},
state = %{os_pid: os_pid, cid: channel_id}
state = %{pty: pty, cid: channel_id}
) do
:exec.send(os_pid, data)
ExPTY.send_data(pty, data)

{:ok, state}
end
Expand Down Expand Up @@ -165,9 +151,9 @@ defmodule NervesSSHShell.CLI do

def handle_ssh_msg(
{:ssh_cm, cm, {:window_change, cid, width, height, _, _} = _msg},
state = %{os_pid: os_pid, cm: cm, cid: cid}
state = %{cm: cm, cid: cid, pty: pty}
) do
:exec.winsz(os_pid, height, width)
ExPTY.winsz(pty, height, width)

{:ok, state}
end
Expand Down
51 changes: 21 additions & 30 deletions lib/shell_subsystem.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,53 +16,44 @@ defmodule NervesSSHShell.ShellSubsystem do
end
end

defp maybe_set_term() do
if term = System.get_env("TERM") do
[{"TERM", term}]
else
[{"TERM", "xterm"}]
end
end
# defp maybe_set_term() do
# if term = System.get_env("TERM") do
# [{"TERM", term}]
# else
# [{"TERM", "xterm"}]
# end
# end

def init(_opts) do
{:ok, port_pid, os_pid} =
:exec.run(get_shell_command(), [
:stdin,
:stdout,
{:stderr, :stdout},
:pty,
:pty_echo,
:monitor,
env: maybe_set_term()
])

{:ok, %{os_pid: os_pid, port_pid: port_pid, cid: nil, cm: nil}}
{:ok, pty} = ExPTY.start_link()
ExPTY.exec(pty, get_shell_command())

{:ok, %{pty: pty, cid: nil, cm: nil}}
end

def handle_msg({:ssh_channel_up, channel_id, connection_manager}, state) do
{:ok, %{state | cid: channel_id, cm: connection_manager}}
end

# port closed
# pty closed
def handle_msg(
{:DOWN, os_pid, :process, port_pid, _},
%{os_pid: os_pid, port_pid: port_pid, cm: cm, cid: cid} = state
{:EXIT, pty, _reason},
%{pty: pty, cm: cm, cid: cid} = state
) do
:ssh_connection.send_eof(cm, cid)
{:stop, cid, state}
end

def handle_msg({what, os_pid, data}, %{os_pid: os_pid, cm: cm, cid: cid} = state)
when what in [:stdout, :stderr] do
def handle_msg({pty, {:data, data}} = _msg, %{cm: cm, cid: cid, pty: pty} = state) do
:ssh_connection.send(cm, cid, data)
{:ok, state}
end

def handle_ssh_msg(
{:ssh_cm, cm, {:data, cid, 0, data}},
state = %{os_pid: os_pid, cm: cm, cid: cid}
{:ssh_cm, _cm, {:data, channel_id, 0, data}},
state = %{pty: pty, cid: channel_id}
) do
:exec.send(os_pid, data)
ExPTY.send_data(pty, data)

{:ok, state}
end
Expand Down Expand Up @@ -90,10 +81,10 @@ defmodule NervesSSHShell.ShellSubsystem do
end

def handle_ssh_msg(
{:ssh_cm, cm, {:window_change, cid, width, height, _, _}},
state = %{os_pid: os_pid, cm: cm, cid: cid}
{:ssh_cm, cm, {:window_change, cid, width, height, _, _} = _msg},
state = %{cm: cm, cid: cid, pty: pty}
) do
:exec.winsz(os_pid, height, width)
ExPTY.winsz(pty, height, width)

{:ok, state}
end
Expand Down
3 changes: 1 addition & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ defmodule NervesSSHShell.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:nerves_runtime_shell, "~> 0.1.0"},
{:erlexec, github: "saleyn/erlexec", tag: "7f12101e5e7128d9a442974060cec81c90db71ef"}
{:ex_pty, github: "SteffenDE/ex_pty", branch: "main"}
]
end
end
3 changes: 2 additions & 1 deletion mix.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
%{
"erlexec": {:git, "https://github.com/saleyn/erlexec.git", "7f12101e5e7128d9a442974060cec81c90db71ef", [tag: "7f12101e5e7128d9a442974060cec81c90db71ef"]},
"elixir_make": {:hex, :elixir_make, "0.6.3", "bc07d53221216838d79e03a8019d0839786703129599e9619f4ab74c8c096eac", [:mix], [], "hexpm", "f5cbd651c5678bcaabdbb7857658ee106b12509cd976c2c2fca99688e1daf716"},
"ex_pty": {:git, "https://github.com/SteffenDE/ex_pty.git", "49a8300f3c97f969e7e9d753a0c134ea2c1adf71", [branch: "main"]},
}