-
Notifications
You must be signed in to change notification settings - Fork 3
/
kernel.erl
299 lines (274 loc) · 9.69 KB
/
kernel.erl
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1996-2010. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%%
%% %CopyrightEnd%
%%
-module(kernel).
-behaviour(supervisor).
%% External exports
-export([start/2, init/1, stop/1]).
-export([config_change/3]).
%%%-----------------------------------------------------------------
%%% The kernel is the first application started.
%%% Callback functions for the kernel application.
%%%-----------------------------------------------------------------
start(_, []) ->
case supervisor:start_link({local, kernel_sup}, kernel, []) of
{ok, Pid} ->
Type = get_error_logger_type(),
error_logger:swap_handler(Type),
{ok, Pid, []};
Error -> Error
end.
stop(_State) ->
ok.
%%-------------------------------------------------------------------
%% Some configuration parameters for kernel are changed
%%-------------------------------------------------------------------
config_change(Changed, New, Removed) ->
do_distribution_change(Changed, New, Removed),
do_global_groups_change(Changed, New, Removed),
ok.
get_error_logger_type() ->
case application:get_env(kernel, error_logger) of
{ok, tty} -> tty;
{ok, {file, File}} when is_list(File) -> {logfile, File};
{ok, false} -> false;
{ok, silent} -> silent;
undefined -> tty; % default value
{ok, Bad} -> exit({bad_config, {kernel, {error_logger, Bad}}})
end.
%%%-----------------------------------------------------------------
%%% The process structure in kernel is as shown in the figure.
%%%
%%% ---------------
%%% | kernel_sup (A)|
%%% ---------------
%%% |
%%% -------------------------------
%%% | | |
%%% <std services> ------------- -------------
%%% (file,code, | erl_dist (A)| | safe_sup (1)|
%%% rpc, ...) ------------- -------------
%%% | |
%%% (net_kernel, (disk_log, pg2,
%%% auth, ...) ...)
%%%
%%% The rectangular boxes are supervisors. All supervisors except
%%% for kernel_safe_sup terminates the enitre erlang node if any of
%%% their children dies. Any child that can't be restarted in case
%%% of failure must be placed under one of these supervisors. Any
%%% other child must be placed under safe_sup. These children may
%%% be restarted. Be aware that if a child is restarted the old state
%%% and all data will be lost.
%%%-----------------------------------------------------------------
%%% Callback functions for the kernel_sup supervisor.
%%%-----------------------------------------------------------------
init([]) ->
SupFlags = {one_for_all, 0, 1},
Config = {kernel_config,
{kernel_config, start_link, []},
permanent, 2000, worker, [kernel_config]},
Code = {code_server,
{code, start_link, get_code_args()},
permanent, 2000, worker, [code]},
File = {file_server_2,
{file_server, start_link, []},
permanent, 2000, worker,
[file, file_server, file_io_server, prim_file]},
StdError = {standard_error,
{standard_error, start_link, []},
temporary, 2000, supervisor, [user_sup]},
User = {user,
{user_sup, start, []},
temporary, 2000, supervisor, [user_sup]},
case init:get_argument(mode) of
{ok, [["minimal"]]} ->
SafeSupervisor = {kernel_safe_sup,
{supervisor, start_link,
[{local, kernel_safe_sup}, ?MODULE, safe]},
permanent, infinity, supervisor, [?MODULE]},
{ok, {SupFlags,
[File, Code, StdError, User,
Config, SafeSupervisor]}};
_ ->
Rpc = {rex, {rpc, start_link, []},
permanent, 2000, worker, [rpc]},
Global = {global_name_server, {global, start_link, []},
permanent, 2000, worker, [global]},
SGrp = {s_group, {s_group,start_link,[]},
permanent, 2000, worker, [s_group]},
InetDb = {inet_db, {inet_db, start_link, []},
permanent, 2000, worker, [inet_db]},
NetSup = {net_sup, {erl_distribution, start_link, []},
permanent, infinity, supervisor,[erl_distribution]},
DistAC = start_dist_ac(),
Timer = start_timer(),
SafeSupervisor = {kernel_safe_sup,
{supervisor, start_link,
[{local, kernel_safe_sup}, ?MODULE, safe]},
permanent, infinity, supervisor, [?MODULE]},
{ok, {SupFlags,
[Rpc, Global, InetDb | DistAC] ++
[NetSup, SGrp, File, Code,
StdError, User, Config, SafeSupervisor] ++ Timer}}
end;
init(safe) ->
SupFlags = {one_for_one, 4, 3600},
Boot = start_boot_server(),
DiskLog = start_disk_log(),
Pg2 = start_pg2(),
%% Run the on_load handlers for all modules that have been
%% loaded so far. Running them at this point means that
%% on_load handlers can safely call kernel processes
%% (and in particular call code:priv_dir/1 or code:lib_dir/1).
init:run_on_load_handlers(),
{ok, {SupFlags, Boot ++ DiskLog ++ Pg2}}.
get_code_args() ->
case init:get_argument(nostick) of
{ok, [[]]} -> [[nostick]];
_ -> []
end.
start_dist_ac() ->
Spec = [{dist_ac,{dist_ac,start_link,[]},permanent,2000,worker,[dist_ac]}],
case application:get_env(kernel, start_dist_ac) of
{ok, true} -> Spec;
{ok, false} -> [];
undefined ->
case application:get_env(kernel, distributed) of
{ok, _} -> Spec;
_ -> []
end
end.
start_boot_server() ->
case application:get_env(kernel, start_boot_server) of
{ok, true} ->
Args = get_boot_args(),
[{boot_server, {erl_boot_server, start_link, [Args]}, permanent,
1000, worker, [erl_boot_server]}];
_ ->
[]
end.
get_boot_args() ->
case application:get_env(kernel, boot_server_slaves) of
{ok, Slaves} -> Slaves;
_ -> []
end.
start_disk_log() ->
case application:get_env(kernel, start_disk_log) of
{ok, true} ->
[{disk_log_server,
{disk_log_server, start_link, []},
permanent, 2000, worker, [disk_log_server]},
{disk_log_sup, {disk_log_sup, start_link, []}, permanent,
1000, supervisor, [disk_log_sup]}];
_ ->
[]
end.
start_pg2() ->
case application:get_env(kernel, start_pg2) of
{ok, true} ->
[{pg2, {pg2, start_link, []}, permanent, 1000, worker, [pg2]}];
_ ->
[]
end.
start_timer() ->
case application:get_env(kernel, start_timer) of
{ok, true} ->
[{timer_server, {timer, start_link, []}, permanent, 1000, worker,
[timer]}];
_ ->
[]
end.
%%-----------------------------------------------------------------
%% The change of the distributed parameter is taken care of here
%%-----------------------------------------------------------------
do_distribution_change(Changed, New, Removed) ->
%% check if the distributed parameter is changed. It is not allowed
%% to make a local application to a distributed one, or vice versa.
case is_dist_changed(Changed, New, Removed) of
%%{changed, new, removed}
{false, false, false} ->
ok;
{C, false, false} ->
%% At last, update the parameter.
gen_server:call(dist_ac, {distribution_changed, C}, infinity);
{false, _, false} ->
error_logger:error_report("Distribution not changed: "
"Not allowed to add the 'distributed' "
"parameter."),
{error, {distribution_not_changed, "Not allowed to add the "
"'distributed' parameter"}};
{false, false, _} ->
error_logger:error_report("Distribution not changed: "
"Not allowed to remove the "
"distribution parameter."),
{error, {distribution_not_changed, "Not allowed to remove the "
"'distributed' parameter"}}
end.
%%-----------------------------------------------------------------
%% Check if distribution is changed in someway.
%%-----------------------------------------------------------------
is_dist_changed(Changed, New, Removed) ->
C = case lists:keyfind(distributed, 1, Changed) of
false ->
false;
{distributed, NewDistC} ->
NewDistC
end,
N = case lists:keyfind(distributed, 1, New) of
false ->
false;
{distributed, NewDistN} ->
NewDistN
end,
R = lists:member(distributed, Removed),
{C, N, R}.
%%-----------------------------------------------------------------
%% The change of the global_groups parameter is taken care of here
%%-----------------------------------------------------------------
do_global_groups_change(Changed, New, Removed) ->
%% check if the global_groups parameter is changed.
case is_gg_changed(Changed, New, Removed) of
%%{changed, new, removed}
{false, false, false} ->
ok;
{C, false, false} ->
%% At last, update the parameter.
s_group:s_groups_changed(C);
{false, N, false} ->
s_group:s_groups_added(N);
{false, false, R} ->
s_group:s_groups_removed(R)
end.
%%-----------------------------------------------------------------
%% Check if global_groups is changed in someway.
%%-----------------------------------------------------------------
is_gg_changed(Changed, New, Removed) ->
C = case lists:keyfind(s_groups, 1, Changed) of
false ->
false;
{s_groups, NewDistC} ->
NewDistC
end,
N = case lists:keyfind(s_groups, 1, New) of
false ->
false;
{s_groups, NewDistN} ->
NewDistN
end,
R = lists:member(s_groups, Removed),
{C, N, R}.