~rdoering/ubuntu/karmic/erlang/fix-535090

« back to all changes in this revision

Viewing changes to lib/ssh/src/sshd_sup.erl

  • Committer: Bazaar Package Importer
  • Author(s): Sergei Golovan
  • Date: 2009-02-15 16:42:52 UTC
  • mfrom: (3.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090215164252-q5x4rcf8a5pbesb1
Tags: 1:12.b.5-dfsg-2
Upload to unstable after lenny is released.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
%%<copyright>
 
2
%% <year>2008-2008</year>
 
3
%% <holder>Ericsson AB, All Rights Reserved</holder>
 
4
%%</copyright>
 
5
%%<legalnotice>
 
6
%% The contents of this file are subject to the Erlang Public License,
 
7
%% Version 1.1, (the "License"); you may not use this file except in
 
8
%% compliance with the License. You should have received a copy of the
 
9
%% Erlang Public License along with this software. If not, it can be
 
10
%% retrieved online at http://www.erlang.org/.
 
11
%%
 
12
%% Software distributed under the License is distributed on an "AS IS"
 
13
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 
14
%% the License for the specific language governing rights and limitations
 
15
%% under the License.
 
16
%%
 
17
%% The Initial Developer of the Original Code is Ericsson AB.
 
18
%%</legalnotice>
 
19
%%
 
20
%%----------------------------------------------------------------------
 
21
%% Purpose: The top supervisor for ssh servers hangs under 
 
22
%%          ssh_sup.
 
23
%%----------------------------------------------------------------------
 
24
 
 
25
-module(sshd_sup).
 
26
 
 
27
-behaviour(supervisor).
 
28
 
 
29
-export([start_link/1, start_child/1, stop_child/1,
 
30
         stop_child/2, system_name/1]).
 
31
 
 
32
%% Supervisor callback
 
33
-export([init/1]).
 
34
 
 
35
%%%=========================================================================
 
36
%%%  API
 
37
%%%=========================================================================
 
38
start_link(Servers) ->
 
39
    supervisor:start_link({local, ?MODULE}, ?MODULE, [Servers]).
 
40
 
 
41
start_child(ServerOpts) ->
 
42
    Address = proplists:get_value(address, ServerOpts),
 
43
    Port = proplists:get_value(port, ServerOpts),
 
44
    case ssh_system_sup:system_supervisor(Address, Port) of
 
45
       undefined ->
 
46
            Spec =  child_spec(Address, Port, ServerOpts),    
 
47
            supervisor:start_child(?MODULE, Spec);
 
48
        Pid ->
 
49
            AccPid = ssh_system_sup:acceptor_supervisor(Pid),
 
50
            ssh_acceptor_sup:start_child(AccPid, ServerOpts)
 
51
    end.
 
52
 
 
53
stop_child(Name) ->
 
54
    case supervisor:terminate_child(?MODULE, Name) of
 
55
        ok ->
 
56
            supervisor:delete_child(?MODULE, Name);
 
57
        Error ->
 
58
            Error
 
59
    end.
 
60
 
 
61
stop_child(Address, Port) ->
 
62
    Name = id(Address, Port),
 
63
    stop_child(Name).
 
64
 
 
65
system_name(SysSup) ->
 
66
    Children = supervisor:which_children(sshd_sup),
 
67
    system_name(SysSup, Children).
 
68
 
 
69
%%%=========================================================================
 
70
%%%  Supervisor callback
 
71
%%%=========================================================================
 
72
init([Servers]) ->
 
73
    RestartStrategy = one_for_one,
 
74
    MaxR = 10,
 
75
    MaxT = 3600,
 
76
    Fun = fun(ServerOpts) -> 
 
77
                  Address = proplists:get_value(address, ServerOpts),
 
78
                  Port = proplists:get_value(port, ServerOpts),
 
79
                  child_spec(Address, Port, ServerOpts) 
 
80
          end,
 
81
    Children = lists:map(Fun, Servers),
 
82
    {ok, {{RestartStrategy, MaxR, MaxT}, Children}}.
 
83
 
 
84
%%%=========================================================================
 
85
%%%  Internal functions
 
86
%%%=========================================================================
 
87
child_spec(Address, Port, ServerOpts) ->
 
88
    Name = id(Address, Port),
 
89
    StartFunc = {ssh_system_sup, start_link, [ServerOpts]},
 
90
    Restart = transient, 
 
91
    Shutdown = infinity,
 
92
    Modules = [ssh_system_sup],
 
93
    Type = supervisor,
 
94
    {Name, StartFunc, Restart, Shutdown, Type, Modules}.
 
95
 
 
96
id(Address, Port) ->
 
97
    {server, ssh_system_sup, Address, Port}.
 
98
 
 
99
system_name([], _ ) ->
 
100
    undefined;
 
101
system_name(SysSup, [{Name, SysSup, _, _} | _]) ->
 
102
    Name;
 
103
system_name(SysSup, [_ | Rest]) ->
 
104
    system_name(SysSup, Rest).