Skip to content

Commit

Permalink
ssh: rename ssh_subsystem_sup to ssh_connection_sup
Browse files Browse the repository at this point in the history
- rename in order to improve supervision readability
  • Loading branch information
u3s committed Sep 4, 2024
1 parent 4727d51 commit 532436e
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 106 deletions.
2 changes: 1 addition & 1 deletion lib/compiler/test/compile_SUITE_data/ssh_connect.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -269,5 +269,5 @@
suggest_window_size,
suggest_packet_size,
exec,
sub_system_supervisor
connection_supervisor
}).
2 changes: 1 addition & 1 deletion lib/ssh/src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ MODULES= \
ssh_sftpd \
ssh_sftpd_file\
ssh_shell \
ssh_subsystem_sup \
ssh_connection_sup \
ssh_system_sup \
ssh_tcpip_forward_srv \
ssh_tcpip_forward_client \
Expand Down
4 changes: 2 additions & 2 deletions lib/ssh/src/ssh.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
ssh_sftpd,
ssh_sftpd_file,
ssh_sftpd_file_api,
ssh_subsystem_sup,
ssh_connection_sup,
ssh_tcpip_forward_client,
ssh_tcpip_forward_srv,
ssh_tcpip_forward_acceptor_sup,
Expand All @@ -51,7 +51,7 @@
ssh_acceptor,
ssh_channel_sup,
ssh_connection_handler,
ssh_subsystem_sup,
ssh_connection_sup,
ssh_system_sup
]},
{default_filter, rm} %% rm | filter
Expand Down
4 changes: 2 additions & 2 deletions lib/ssh/src/ssh.erl
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ continue_connect(Socket, Options0, NegTimeout) ->
port = SockPort,
profile = ?GET_OPT(profile,Options)
},
ssh_system_sup:start_subsystem(client, Address, Socket, Options).
ssh_system_sup:start_connection(client, Address, Socket, Options).

%%--------------------------------------------------------------------
-doc "Closes an SSH connection.".
Expand Down Expand Up @@ -532,7 +532,7 @@ daemon(Socket, UserOptions) ->
profile = ?GET_OPT(profile,Options0)
},
Options = ?PUT_INTERNAL_OPT({connected_socket, Socket}, Options0),
case ssh_system_sup:start_subsystem(server, Address, Socket, Options) of
case ssh_system_sup:start_connection(server, Address, Socket, Options) of
{ok,Pid} ->
{ok,Pid};
{error, {already_started, _}} ->
Expand Down
4 changes: 2 additions & 2 deletions lib/ssh/src/ssh_acceptor.erl
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ handle_connection(Address, Port, _Peer, Options, Socket, _MaxSessions, _NumSessi
handle_connection(Address, Port, Options0, Socket) ->
Options = ?PUT_INTERNAL_OPT([{user_pid, self()}
], Options0),
ssh_system_sup:start_subsystem(server,
ssh_system_sup:start_connection(server,
#address{address = Address,
port = Port,
profile = ?GET_OPT(profile,Options)
Expand Down Expand Up @@ -247,7 +247,7 @@ handle_error(Reason, ToAddress, ToPort, FromAddress, FromPort) ->

%%%----------------------------------------------------------------
number_of_connections(SysSupPid) ->
lists:foldl(fun({_Ref,_Pid,supervisor,[ssh_subsystem_sup]}, N) -> N+1;
lists:foldl(fun({_Ref,_Pid,supervisor,[ssh_connection_sup]}, N) -> N+1;
(_, N) -> N
end, 0, supervisor:which_children(SysSupPid)).

Expand Down
2 changes: 1 addition & 1 deletion lib/ssh/src/ssh_connect.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -269,5 +269,5 @@
suggest_window_size,
suggest_packet_size,
exec,
sub_system_supervisor
connection_supervisor
}).
20 changes: 10 additions & 10 deletions lib/ssh/src/ssh_connection.erl
Original file line number Diff line number Diff line change
Expand Up @@ -886,15 +886,15 @@ handle_msg(#ssh_msg_channel_open{channel_type = "forwarded-tcpip",
suggest_window_size = WinSz,
suggest_packet_size = PktSz,
options = Options,
sub_system_supervisor = SubSysSup
connection_supervisor = ConnectionSup
} = C,
client, _SSH) ->
{ReplyMsg, NextChId} =
case ssh_connection_handler:retrieve(C, {tcpip_forward,ConnectedHost,ConnectedPort}) of
{ok, {ConnectToHost,ConnectToPort}} ->
case gen_tcp:connect(ConnectToHost, ConnectToPort, [{active,false}, binary]) of
{ok,Sock} ->
{ok,Pid} = ssh_subsystem_sup:start_channel(client, SubSysSup, self(),
{ok,Pid} = ssh_connection_sup:start_channel(client, ConnectionSup, self(),
ssh_tcpip_forward_client, ChId,
[Sock], undefined, Options),
ssh_client_channel:cache_update(Cache,
Expand Down Expand Up @@ -944,7 +944,7 @@ handle_msg(#ssh_msg_channel_open{channel_type = "direct-tcpip",
suggest_window_size = WinSz,
suggest_packet_size = PktSz,
options = Options,
sub_system_supervisor = SubSysSup
connection_supervisor = ConnectionSup
} = C,
server, _SSH) ->
{ReplyMsg, NextChId} =
Expand All @@ -960,7 +960,7 @@ handle_msg(#ssh_msg_channel_open{channel_type = "direct-tcpip",
case gen_tcp:connect(binary_to_list(HostToConnect), PortToConnect,
[{active,false}, binary]) of
{ok,Sock} ->
{ok,Pid} = ssh_subsystem_sup:start_channel(server, SubSysSup, self(),
{ok,Pid} = ssh_connection_sup:start_channel(server, ConnectionSup, self(),
ssh_tcpip_forward_srv, ChId,
[Sock], undefined, Options),
ssh_client_channel:cache_update(Cache,
Expand Down Expand Up @@ -1192,8 +1192,8 @@ handle_msg(#ssh_msg_global_request{name = <<"tcpip-forward">>,
{[{connection_reply, request_failure_msg()}], Connection};

true ->
SubSysSup = ?GET_INTERNAL_OPT(subsystem_sup, Opts),
FwdSup = ssh_subsystem_sup:tcpip_fwd_supervisor(SubSysSup),
ConnectionSup = ?GET_INTERNAL_OPT(connection_sup, Opts),
FwdSup = ssh_connection_sup:tcpip_fwd_supervisor(ConnectionSup),
ConnPid = self(),
case ssh_tcpip_forward_acceptor:supervised_start(FwdSup,
{ListenAddrStr, ListenPort},
Expand Down Expand Up @@ -1423,22 +1423,22 @@ setup_session(#connection{channel_cache = Cache,
start_cli(#connection{options = Options,
cli_spec = CliSpec,
exec = Exec,
sub_system_supervisor = SubSysSup}, ChannelId) ->
connection_supervisor = ConnectionSup}, ChannelId) ->
case CliSpec of
no_cli ->
{error, cli_disabled};
{CbModule, Args} ->
ssh_subsystem_sup:start_channel(server, SubSysSup, self(), CbModule, ChannelId, Args, Exec, Options)
ssh_connection_sup:start_channel(server, ConnectionSup, self(), CbModule, ChannelId, Args, Exec, Options)
end.


start_subsystem(BinName, #connection{options = Options,
sub_system_supervisor = SubSysSup},
connection_supervisor = ConnectionSup},
#channel{local_id = ChannelId}, _ReplyMsg) ->
Name = binary_to_list(BinName),
case check_subsystem(Name, Options) of
{Callback, Opts} when is_atom(Callback), Callback =/= none ->
ssh_subsystem_sup:start_channel(server, SubSysSup, self(), Callback, ChannelId, Opts, undefined, Options);
ssh_connection_sup:start_channel(server, ConnectionSup, self(), Callback, ChannelId, Opts, undefined, Options);
{none, _} ->
{error, bad_subsystem};
{_, _} ->
Expand Down
20 changes: 10 additions & 10 deletions lib/ssh/src/ssh_connection_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ start_link(Role, Id, Socket, Options) ->
%% Announce the ConnectionRef to the system supervisor so it could
%% 1) initiate the socket handover, and
%% 2) be returned to whoever called for example ssh:connect; the Pid
%% returned from this function is "consumed" by the subsystem
%% returned from this function is "consumed" by the connection
%% supervisor.
?GET_INTERNAL_OPT(user_pid,Options) ! {new_connection_ref, Id, Pid},
{ok, Pid};
Expand Down Expand Up @@ -197,8 +197,8 @@ open_channel(ConnectionHandler,

%% . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
start_channel(ConnectionHandler, CallbackModule, ChannelId, Args, Exec) ->
{ok, {SubSysSup,Role,Opts}} = call(ConnectionHandler, get_misc),
ssh_subsystem_sup:start_channel(Role, SubSysSup,
{ok, {ConnectionSup,Role,Opts}} = call(ConnectionHandler, get_misc),
ssh_connection_sup:start_channel(Role, ConnectionSup,
ConnectionHandler, CallbackModule, ChannelId,
Args, Exec, Opts).

Expand Down Expand Up @@ -418,7 +418,7 @@ init_connection_record(Role, Socket, Opts) ->
suggest_packet_size = PktSz,
requests = [],
options = Opts,
sub_system_supervisor = ?GET_INTERNAL_OPT(subsystem_sup, Opts)
connection_supervisor = ?GET_INTERNAL_OPT(connection_sup, Opts)
},
case Role of
server ->
Expand Down Expand Up @@ -1022,8 +1022,8 @@ handle_event({call,From}, {eof, ChannelId}, StateName, D0)

handle_event({call,From}, get_misc, StateName,
#data{connection_state = #connection{options = Opts}} = D) when ?CONNECTED(StateName) ->
SubSysSup = ?GET_INTERNAL_OPT(subsystem_sup, Opts),
Reply = {ok, {SubSysSup, ?role(StateName), Opts}},
ConnectionSup = ?GET_INTERNAL_OPT(connection_sup, Opts),
Reply = {ok, {ConnectionSup, ?role(StateName), Opts}},
{keep_state, D, [{reply,From,Reply}]};

handle_event({call,From},
Expand Down Expand Up @@ -1286,9 +1286,9 @@ handle_event(info, check_cache, _, D) ->
handle_event(info, {fwd_connect_received, Sock, ChId, ChanCB}, StateName, #data{connection_state = Connection}) ->
#connection{options = Options,
channel_cache = Cache,
sub_system_supervisor = SubSysSup} = Connection,
connection_supervisor = ConnectionSup} = Connection,
Channel = ssh_client_channel:cache_lookup(Cache, ChId),
{ok,Pid} = ssh_subsystem_sup:start_channel(?role(StateName), SubSysSup, self(), ChanCB, ChId, [Sock], undefined, Options),
{ok,Pid} = ssh_connection_sup:start_channel(?role(StateName), ConnectionSup, self(), ChanCB, ChId, [Sock], undefined, Options),
ssh_client_channel:cache_update(Cache, Channel#channel{user=Pid}),
gen_tcp:controlling_process(Sock, Pid),
inet:setopts(Sock, [{active,once}]),
Expand All @@ -1297,8 +1297,8 @@ handle_event(info, {fwd_connect_received, Sock, ChId, ChanCB}, StateName, #data{
handle_event({call,From},
{handle_direct_tcpip, ListenHost, ListenPort, ConnectToHost, ConnectToPort, _Timeout},
_StateName,
#data{connection_state = #connection{sub_system_supervisor=SubSysSup}}) ->
case ssh_tcpip_forward_acceptor:supervised_start(ssh_subsystem_sup:tcpip_fwd_supervisor(SubSysSup),
#data{connection_state = #connection{connection_supervisor=ConnectionSup}}) ->
case ssh_tcpip_forward_acceptor:supervised_start(ssh_connection_sup:tcpip_fwd_supervisor(ConnectionSup),
{ListenHost, ListenPort},
{ConnectToHost, ConnectToPort},
"direct-tcpip", ssh_tcpip_forward_client,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
%%
%%
%%----------------------------------------------------------------------
%% Purpose: The ssh subsystem supervisor
%% Purpose: The ssh connection supervisor
%%----------------------------------------------------------------------

-module(ssh_subsystem_sup).
-module(ssh_connection_sup).
-moduledoc false.

-behaviour(supervisor).
Expand Down Expand Up @@ -52,52 +52,47 @@ start_channel(Role, SupPid, ConnRef, Callback, Id, Args, Exec, Opts) ->
ChannelSup = channel_supervisor(SupPid),
ssh_channel_sup:start_child(Role, ChannelSup, ConnRef, Callback, Id, Args, Exec, Opts).

tcpip_fwd_supervisor(SubSysSup) ->
find_child(tcpip_forward_acceptor_sup, SubSysSup).
tcpip_fwd_supervisor(ConnectionSup) ->
find_child(tcpip_forward_acceptor_sup, ConnectionSup).


%%%=========================================================================
%%% Supervisor callback
%%%=========================================================================
init([Role, Id, Socket, Options]) ->
ssh_lib:set_label(Role, {subsystem_sup, Socket}),
SubSysSup = self(),
ssh_lib:set_label(Role, {connection_sup, Socket}),
ConnectionSup = self(),
SupFlags = #{strategy => one_for_all,
auto_shutdown => any_significant,
intensity => 0,
period => 3600
},
ChildSpecs = [#{id => connection,
restart => temporary,
type => worker,
significant => true,
start => {ssh_connection_handler,
start_link,
[Role, Id, Socket,
?PUT_INTERNAL_OPT([
{subsystem_sup, SubSysSup}
], Options)
]
}
},
#{id => channel_sup,
restart => temporary,
type => supervisor,
start => {ssh_channel_sup, start_link, [Options]}
},
period => 3600},
ChildSpecs =
[#{id => connection,
restart => temporary,
type => worker,
significant => true,
start => {ssh_connection_handler,
start_link,
[Role, Id, Socket,
?PUT_INTERNAL_OPT([{connection_sup, ConnectionSup}], Options)]}
},
#{id => channel_sup,
restart => temporary,
type => supervisor,
start => {ssh_channel_sup, start_link, [Options]}
},

#{id => tcpip_forward_acceptor_sup,
restart => temporary,
type => supervisor,
start => {ssh_tcpip_forward_acceptor_sup, start_link, []}
}
],
#{id => tcpip_forward_acceptor_sup,
restart => temporary,
type => supervisor,
start => {ssh_tcpip_forward_acceptor_sup, start_link, []}
}],
{ok, {SupFlags,ChildSpecs}}.

%%%=========================================================================
%%% Internal functions
%%%=========================================================================
channel_supervisor(SubSysSup) -> find_child(channel_sup, SubSysSup).
channel_supervisor(ConnectionSup) -> find_child(channel_sup, ConnectionSup).

find_child(Id, Sup) when is_pid(Sup) ->
try
Expand Down
10 changes: 5 additions & 5 deletions lib/ssh/src/ssh_info.erl
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ format_sup(server, {{{ssh_system_sup,LocalAddress},Pid,supervisor,[ssh_system_su
io_lib:nl() % Separate system supervisors by an empty line
];
format_sup(client,
{{Ref,SubSysSup,supervisor,[ssh_subsystem_sup]}, _SubSysSpec,
{{Ref,ConnSup,supervisor,[ssh_connection_sup]}, _ConnSupSpec,
[{{connection,ConnPid,worker,[ssh_connection_handler]}, _ConnSpec}
| Children]
},
Expand All @@ -143,23 +143,23 @@ format_sup(client,
"~sConnectionRef=~s, subsys_sup=~s~n",
[indent(Indent), local_addr(ConnPid),
indent(Indent), peer_addr(ConnPid), peer_version(client,ConnPid),
indent(Indent), print_pid(ConnPid), print_pid(SubSysSup)
indent(Indent), print_pid(ConnPid), print_pid(ConnSup)
]),
walk_tree(client,
[{H,{connref,ConnPid},Cs} || {H,_,Cs} <- Children],
?inc(Indent)),
io_lib:nl() % Separate sub system supervisors by an empty line
];
format_sup(server,
{{Ref,SubSysSup,supervisor,[ssh_subsystem_sup]}, _SubSysSpec,
[{{connection,ConnPid,worker,[ssh_connection_handler]}, _ConnSpec}
{{Ref,ConnSup,supervisor,[ssh_connection_sup]}, _ConnSupSpec,
[{{connection,ConnPid,worker,[ssh_connection_handler]}, _ConnSpec}
| Children]
},
Indent) when is_reference(Ref) ->
[io_lib:format("~sRemote: ~s (Version: ~s)~n"
"~sConnectionRef=~s, subsys_sup=~s~n",
[indent(Indent), peer_addr(ConnPid), peer_version(server,ConnPid),
indent(Indent), print_pid(ConnPid), print_pid(SubSysSup)
indent(Indent), print_pid(ConnPid), print_pid(ConnSup)
]),
walk_tree(server,
[{H,{connref,ConnPid},Cs} || {H,_,Cs} <- Children],
Expand Down
18 changes: 9 additions & 9 deletions lib/ssh/src/ssh_system_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
stop_listener/1,
stop_system/1,
start_system/2,
start_subsystem/4,
start_connection/4,
get_daemon_listen_address/1,
addresses/1,
get_options/2,
Expand Down Expand Up @@ -94,33 +94,33 @@ get_daemon_listen_address(SystemSup) ->
end.

%%%----------------------------------------------------------------
%%% Start the subsystem child. It is a significant child of the system
%%% Start the connection child. It is a significant child of the system
%%% supervisor (callback = this module) for server and non-significant
%%% child of sshc_sup for client
start_subsystem(Role = client, _, Socket, Options) ->
do_start_subsystem(Role, sup(client), false, Socket, Options);
start_subsystem(Role = server, Address=#address{}, Socket, Options) ->
start_connection(Role = client, _, Socket, Options) ->
do_start_connection(Role, sup(client), false, Socket, Options);
start_connection(Role = server, Address=#address{}, Socket, Options) ->
case get_system_sup(Address, Options) of
{ok, SysPid} ->
do_start_subsystem(Role, SysPid, true, Socket, Options);
do_start_connection(Role, SysPid, true, Socket, Options);
Others ->
Others
end.

do_start_subsystem(Role, SupPid, Significant, Socket, Options0) ->
do_start_connection(Role, SupPid, Significant, Socket, Options0) ->
Id = make_ref(),
Options = ?PUT_INTERNAL_OPT([{user_pid, self()}], Options0),
case supervisor:start_child(SupPid,
#{id => Id,
start => {ssh_subsystem_sup, start_link,
start => {ssh_connection_sup, start_link,
[Role,Id,Socket,Options]
},
restart => temporary,
significant => Significant,
type => supervisor
})
of
{ok,_SubSysPid} ->
{ok,_ConnectionSupPid} ->
try
receive
{new_connection_ref, Id, ConnPid} ->
Expand Down
Loading

0 comments on commit 532436e

Please sign in to comment.