aboutsummaryrefslogtreecommitdiffstats
path: root/lib/kernel/src/erl_distribution.erl
blob: e97c9db81f2f1bc9a296e10c6b1da5c1519903c2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 1996-2009. All Rights Reserved.
%% 
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%% 
%% %CopyrightEnd%
%%
-module(erl_distribution).

-behaviour(supervisor).

-export([start_link/0,start_link/1,init/1,start/1,stop/0]).

-define(DBG,erlang:display([?MODULE,?LINE])).

%% Called during system start-up.

start_link() ->
    case catch start_p() of
	{ok,Args} ->
	    start_link(Args);
	_ ->
	    ignore
    end.

%% Called from net_kernel:start/1 to start distribution after the
%% system has already started.

start(Args) ->
    C = {net_sup_dynamic, {?MODULE,start_link,[Args]}, permanent,
	 1000, supervisor, [erl_distribution]},
    supervisor:start_child(kernel_sup, C).

%% Stop distribution.

stop() ->
    case supervisor:terminate_child(kernel_sup, net_sup_dynamic) of
	ok ->
	    supervisor:delete_child(kernel_sup, net_sup_dynamic);
	Error ->
	    case whereis(net_sup) of
		Pid when is_pid(Pid) ->
		    %% Dist. started through -sname | -name flags
		    {error, not_allowed};
		_ ->
		    Error
	    end
    end.

%%%
%%% Internal helper functions.
%%%

%% Helper start function.

start_link(Args) ->
    supervisor:start_link({local,net_sup}, ?MODULE, Args).

init(NetArgs) ->
    Epmd = 
	case init:get_argument(no_epmd) of
	    {ok, [[]]} ->
		[];
	    _ ->
		EpmdMod = net_kernel:epmd_module(),
		[{EpmdMod,{EpmdMod,start_link,[]},
		  permanent,2000,worker,[EpmdMod]}]
	end,
    Auth = {auth,{auth,start_link,[]},permanent,2000,worker,[auth]},
    Kernel = {net_kernel,{net_kernel,start_link,[NetArgs]},
	      permanent,2000,worker,[net_kernel]},
    EarlySpecs = net_kernel:protocol_childspecs(),
    {ok,{{one_for_all,0,1}, EarlySpecs ++ Epmd ++ [Auth,Kernel]}}.

start_p() ->
    sname(),
    lname(),
    false.

sname() ->
    case init:get_argument(sname) of
	{ok,[[Name]]} ->
	    throw({ok,[list_to_atom(Name),shortnames|ticktime()]});
	_ ->
	    false
    end.

lname() ->
    case init:get_argument(name) of
	{ok,[[Name]]} ->
	    throw({ok,[list_to_atom(Name),longnames|ticktime()]});
	_ ->
	    false
    end.

ticktime() ->
    %% catch, in case the system was started with boot file start_old,
    %% i.e. running without the application_controller.
    %% Time is given in seconds. The net_kernel tick time is
    %% Time/4 milliseconds.
    case catch application:get_env(net_ticktime) of
	{ok, Value} when is_integer(Value), Value > 0 ->
	    [Value * 250]; %% i.e. 1000 / 4 = 250 ms.
	_ ->
	    []
    end.