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%%----------------------------------------------------------------------
23%% Purpose: The acceptor supervisor for ssh servers hangs under
24%%          ssh_system_sup.
25%%----------------------------------------------------------------------
26
27-module(ssh_acceptor_sup).
28-behaviour(supervisor).
29
30-include("ssh.hrl").
31
32-export([start_link/4, start_child/5, stop_child/4]).
33
34%% Supervisor callback
35-export([init/1]).
36
37-define(DEFAULT_TIMEOUT, 50000).
38
39%%%=========================================================================
40%%%  API
41%%%=========================================================================
42start_link(Address, Port, Profile, Options) ->
43    supervisor:start_link(?MODULE, [Address, Port, Profile, Options]).
44
45start_child(AccSup, Address, Port, Profile, Options) ->
46    Spec = child_spec(Address, Port, Profile, Options),
47    case supervisor:start_child(AccSup, Spec) of
48	{error, already_present} ->
49            %% Is this ever called?
50	    stop_child(AccSup, Address, Port, Profile),
51	    supervisor:start_child(AccSup, Spec);
52	Reply ->
53            %% Reply = {ok,SystemSupPid} when the user calls ssh:daemon
54            %% after having called ssh:stop_listening
55	    Reply
56    end.
57
58stop_child(AccSup, Address, Port, Profile) ->
59    Name = id(Address, Port, Profile),
60    case supervisor:terminate_child(AccSup, Name) of
61        ok ->
62            supervisor:delete_child(AccSup, Name);
63        Error ->
64            Error
65    end.
66
67%%%=========================================================================
68%%%  Supervisor callback
69%%%=========================================================================
70init([Address, Port, Profile, Options]) ->
71    %% Initial start of ssh_acceptor_sup for this port or new start after
72    %% ssh:stop_daemon
73    SupFlags = #{strategy  => one_for_one,
74                 intensity =>   10,
75                 period    => 3600
76                },
77    ChildSpecs = [child_spec(Address, Port, Profile, Options)],
78    {ok, {SupFlags,ChildSpecs}}.
79
80%%%=========================================================================
81%%%  Internal functions
82%%%=========================================================================
83child_spec(Address, Port, Profile, Options) ->
84    Timeout = ?GET_INTERNAL_OPT(timeout, Options, ?DEFAULT_TIMEOUT),
85    #{id       => id(Address, Port, Profile),
86      start    => {ssh_acceptor, start_link, [Port, Address, Options, Timeout]},
87      restart  => transient % because a crashed listener could be replaced by a new one
88     }.
89
90id(Address, Port, Profile) ->
91    {ssh_acceptor_sup, Address, Port, Profile}.
92
93