1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2008-2018. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20%%
21%%----------------------------------------------------------------------
22%% Purpose: The ssh subsystem supervisor
23%%----------------------------------------------------------------------
24
25-module(ssh_subsystem_sup).
26
27-behaviour(supervisor).
28
29-include("ssh.hrl").
30
31-export([start_link/5,
32	 connection_supervisor/1,
33	 channel_supervisor/1,
34         tcpip_fwd_supervisor/1,
35         start_channel/8
36	]).
37
38%% Supervisor callback
39-export([init/1]).
40
41%%%=========================================================================
42%%%  API
43%%%=========================================================================
44start_link(Role, Address, Port, Profile, Options) ->
45    supervisor:start_link(?MODULE, [Role, Address, Port, Profile, Options]).
46
47connection_supervisor(SupPid) ->
48    Children = supervisor:which_children(SupPid),
49    ssh_connection_sup(Children).
50
51channel_supervisor(SupPid) when is_pid(SupPid) ->
52    Children = supervisor:which_children(SupPid),
53    ssh_channel_sup(Children).
54
55tcpip_fwd_supervisor(SupPid) when is_pid(SupPid) ->
56    Children = supervisor:which_children(SupPid),
57    tcpip_fwd_sup(Children).
58
59start_channel(Role, SupPid, ConnRef, Callback, Id, Args, Exec, Opts) ->
60    ChannelSup = channel_supervisor(SupPid),
61    ssh_channel_sup:start_child(Role, ChannelSup, ConnRef, Callback, Id, Args, Exec, Opts).
62
63%%%=========================================================================
64%%%  Supervisor callback
65%%%=========================================================================
66init([Role, Address, Port, Profile, Options]) ->
67    SupFlags = #{strategy  => one_for_all,
68                 intensity =>    0,
69                 period    => 3600
70                },
71    ChildSpecs = child_specs(Role, Address, Port, Profile, Options),
72    {ok, {SupFlags,ChildSpecs}}.
73
74%%%=========================================================================
75%%%  Internal functions
76%%%=========================================================================
77child_specs(Role, Address, Port, Profile, Options) ->
78    [ssh_channel_child_spec(Role, Address, Port, Profile, Options),
79     ssh_connection_child_spec(Role, Address, Port, Profile, Options),
80     ssh_tcpip_forward_acceptor_child_spec()].
81
82ssh_connection_child_spec(Role, Address, Port, _Profile, Options) ->
83    #{id       => id(Role, ssh_connection_sup, Address, Port),
84      start    => {ssh_connection_sup, start_link, [Options]},
85      restart  => temporary,
86      type     => supervisor
87     }.
88
89ssh_channel_child_spec(Role, Address, Port, _Profile, Options) ->
90    #{id       => id(Role, ssh_channel_sup, Address, Port),
91      start    => {ssh_channel_sup, start_link, [Options]},
92      restart  => temporary,
93      type     => supervisor
94     }.
95
96ssh_tcpip_forward_acceptor_child_spec() ->
97    #{id       => make_ref(),
98      start    => {ssh_tcpip_forward_acceptor_sup, start_link, []},
99      restart  => temporary,
100      type     => supervisor
101     }.
102
103
104id(Role, Sup, Address, Port) ->
105    {Role, Sup, Address, Port}.
106
107ssh_connection_sup([{_, Child, _, [ssh_connection_sup]} | _]) ->
108    Child;
109ssh_connection_sup([_ | Rest]) ->
110    ssh_connection_sup(Rest).
111
112ssh_channel_sup([{_, Child, _, [ssh_channel_sup]} | _]) ->
113    Child;
114ssh_channel_sup([_ | Rest]) ->
115    ssh_channel_sup(Rest).
116
117tcpip_fwd_sup([{_, Child, _, [ssh_tcpip_forward_acceptor_sup]} | _]) ->
118    Child;
119tcpip_fwd_sup([_ | Rest]) ->
120    tcpip_fwd_sup(Rest).
121
122