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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
%%
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 1996-2013. 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(heart).
-compile(no_native).
% 'no_native' as part of a crude fix to make init:restart/0 work by clearing
% all hipe inter-module information (hipe_mfa_info's in hipe_bif0.c).
%%%--------------------------------------------------------------------
%%% This is a rewrite of pre_heart from BS.3.
%%%
%%% The purpose of this process-module is to act as a supervisor
%%% of the entire erlang-system. This 'heart' beats with a frequence
%%% satisfying an external port program *not* reboot the entire
%%% system. If however the erlang-emulator would hang, a reboot is
%%% then needed.
%%%
%%% It recognizes the flag '-heart'
%%%--------------------------------------------------------------------
-export([start/0, init/2,
set_cmd/1, clear_cmd/0, get_cmd/0,
set_callback/2, clear_callback/0, get_callback/0,
cycle/0]).
-define(START_ACK, 1).
-define(HEART_BEAT, 2).
-define(SHUT_DOWN, 3).
-define(SET_CMD, 4).
-define(CLEAR_CMD, 5).
-define(GET_CMD, 6).
-define(HEART_CMD, 7).
-define(PREPARING_CRASH, 8). % Used in beam vm
-define(TIMEOUT, 5000).
-define(CYCLE_TIMEOUT, 10000).
-define(HEART_PORT_NAME, heart_port).
-record(state,{port :: port(),
cmd :: [] | binary(),
callback :: 'undefined' | {module(), function()}}).
%%---------------------------------------------------------------------
-spec start() -> 'ignore' | {'error', term()} | {'ok', pid()}.
start() ->
case whereis(heart) of
undefined ->
%% As heart survives a init:restart/0 the Parent
%% of heart must be init.
%% The init process is responsible to create a link
%% to heart.
Pid = spawn(?MODULE, init, [self(), whereis(init)]),
wait_for_init_ack(Pid);
Pid ->
{ok, Pid}
end.
wait_for_init_ack(From) ->
receive
{ok, From} = Ok ->
Ok;
{no_heart, From} ->
ignore;
{Error, From} ->
{error, Error}
end.
-spec init(pid(), pid()) -> {'no_heart', pid()} | {'start_error', pid()}.
init(Starter, Parent) ->
process_flag(trap_exit, true),
process_flag(priority, max),
register(?MODULE, self()),
case catch start_portprogram() of
{ok, Port} ->
Starter ! {ok, self()},
loop(Parent, #state{port = Port, cmd = []});
no_heart ->
Starter ! {no_heart, self()};
error ->
Starter ! {start_error, self()}
end.
-spec set_cmd(Cmd) -> 'ok' | {'error', {'bad_cmd', Cmd}} when
Cmd :: string().
set_cmd(Cmd) ->
?MODULE ! {self(), set_cmd, Cmd},
wait().
-spec get_cmd() -> {ok, Cmd} when
Cmd :: string().
get_cmd() ->
?MODULE ! {self(), get_cmd},
wait().
-spec clear_cmd() -> ok.
clear_cmd() ->
?MODULE ! {self(), clear_cmd},
wait().
-spec set_callback(Module,Function) -> 'ok' | {'error', {'bad_callback', {Module, Function}}} when
Module :: module(),
Function :: function().
set_callback(Module, Function) ->
?MODULE ! {self(), set_callback, {Module,Function}},
wait().
-spec get_callback() -> {'ok', Callback} | 'none' when
Callback :: {module(), function()}.
get_callback() ->
?MODULE ! {self(), get_callback},
wait().
-spec clear_callback() -> ok.
clear_callback() ->
?MODULE ! {self(), clear_callback},
wait().
%%% Should be used solely by the release handler!!!!!!!
-spec cycle() -> 'ok' | {'error', term()}.
cycle() ->
?MODULE ! {self(), cycle},
wait().
wait() ->
receive
{?MODULE, Res} ->
Res
end.
start_portprogram() ->
check_start_heart(),
HeartCmd = "heart -pid " ++ os:getpid() ++ " " ++ get_heart_timeouts(),
try open_port({spawn, HeartCmd}, [{packet, 2}]) of
Port when is_port(Port) ->
case wait_ack(Port) of
ok ->
%% register port so the vm can find it if need be
register(?HEART_PORT_NAME, Port),
{ok, Port};
{error, Reason} ->
report_problem({{port_problem, Reason},
{heart, start_portprogram, []}}),
error
end
catch
_:Reason ->
report_problem({{open_port, Reason},
{heart, start_portprogram, []}}),
error
end.
get_heart_timeouts() ->
HeartOpts = case os:getenv("HEART_BEAT_TIMEOUT") of
false -> "";
H when is_list(H) ->
"-ht " ++ H
end,
HeartOpts ++ case os:getenv("HEART_BEAT_BOOT_DELAY") of
false -> "";
W when is_list(W) ->
" -wt " ++ W
end.
check_start_heart() ->
case init:get_argument(heart) of
{ok, [[]]} ->
ok;
error ->
throw(no_heart);
{ok, [[X|_]|_]} ->
report_problem({{bad_heart_flag, list_to_atom(X)},
{heart, check_start_heart, []}}),
throw(error)
end.
wait_ack(Port) ->
receive
{Port, {data, [?START_ACK]}} ->
ok;
{'EXIT', Port, badsig} -> % Since this is not synchronous, skip it!
wait_ack(Port);
{'EXIT', Port, Reason} -> % The port really terminated.
{error, Reason}
end.
loop(Parent, #state{port=Port}=S) ->
_ = send_heart_beat(S),
receive
{From, set_cmd, NewCmd0} ->
Enc = file:native_name_encoding(),
case catch unicode:characters_to_binary(NewCmd0,Enc,Enc) of
NewCmd when is_binary(NewCmd), byte_size(NewCmd) < 2047 ->
_ = send_heart_cmd(Port, NewCmd),
_ = wait_ack(Port),
From ! {?MODULE, ok},
loop(Parent, S#state{cmd=NewCmd});
_ ->
From ! {?MODULE, {error, {bad_cmd, NewCmd0}}},
loop(Parent, S)
end;
{From, clear_cmd} ->
From ! {?MODULE, ok},
_ = send_heart_cmd(Port, []),
_ = wait_ack(Port),
loop(Parent, S#state{cmd = []});
{From, get_cmd} ->
From ! {?MODULE, get_heart_cmd(Port)},
loop(Parent, S);
{From, set_callback, Callback} ->
case Callback of
{M,F} when is_atom(M), is_atom(F) ->
From ! {?MODULE, ok},
loop(Parent, S#state{callback=Callback});
_ ->
From ! {?MODULE, {error, {bad_callback, Callback}}},
loop(Parent, S)
end;
{From, get_callback} ->
Res = case S#state.callback of
undefined -> none;
Cb -> {ok, Cb}
end,
From ! {?MODULE, Res},
loop(Parent, S);
{From, clear_callback} ->
From ! {?MODULE, ok},
loop(Parent, S#state{callback=undefined});
{From, cycle} ->
%% Calls back to loop
do_cycle_port_program(From, Parent, S);
{'EXIT', Parent, shutdown} ->
no_reboot_shutdown(Port);
{'EXIT', Parent, Reason} ->
exit(Port, Reason),
exit(Reason);
{'EXIT', Port, badsig} -> % we can ignore badsig-messages!
loop(Parent, S);
{'EXIT', Port, _Reason} ->
exit({port_terminated, {?MODULE, loop, [Parent, S]}});
_ ->
loop(Parent, S)
after
?TIMEOUT ->
loop(Parent, S)
end.
-spec no_reboot_shutdown(port()) -> no_return().
no_reboot_shutdown(Port) ->
_ = send_shutdown(Port),
receive
{'EXIT', Port, Reason} when Reason =/= badsig ->
exit(normal)
end.
do_cycle_port_program(Caller, Parent, #state{port=Port} = S) ->
unregister(?HEART_PORT_NAME),
case catch start_portprogram() of
{ok, NewPort} ->
_ = send_shutdown(Port),
receive
{'EXIT', Port, _Reason} ->
_ = send_heart_cmd(NewPort, S#state.cmd),
Caller ! {?MODULE, ok},
loop(Parent, S#state{port=NewPort})
after
?CYCLE_TIMEOUT ->
%% Huh! Two heart port programs running...
%% well, the old one has to be sick not to respond
%% so we'll settle for the new one...
_ = send_heart_cmd(NewPort, S#state.cmd),
Caller ! {?MODULE, {error, stop_error}},
loop(Parent, S#state{port=NewPort})
end;
no_heart ->
Caller ! {?MODULE, {error, no_heart}},
loop(Parent, S);
error ->
Caller ! {?MODULE, {error, start_error}},
loop(Parent, S)
end.
%% "Beates" the heart once.
send_heart_beat(#state{port=Port, callback=Cb}) ->
ok = check_callback(Cb),
Port ! {self(), {command, [?HEART_BEAT]}}.
%% Set a new HEART_COMMAND.
send_heart_cmd(Port, []) ->
Port ! {self(), {command, [?CLEAR_CMD]}};
send_heart_cmd(Port, Cmd) ->
Port ! {self(), {command, [?SET_CMD|Cmd]}}.
get_heart_cmd(Port) ->
Port ! {self(), {command, [?GET_CMD]}},
receive
{Port, {data, [?HEART_CMD | Cmd]}} ->
{ok, Cmd}
end.
%% validate system by performing a check before the heartbeat
%% return 'ok' if everything is alright.
%% Terminate if with reason if something is a miss.
%% It is fine to timeout in the callback, in fact that is the intention
%% if something goes wront -> no heartbeat.
check_callback(Callback) ->
case Callback of
undefined -> ok;
{M,F} ->
erlang:apply(M,F,[])
end.
%% Sends shutdown command to the port.
send_shutdown(Port) -> Port ! {self(), {command, [?SHUT_DOWN]}}.
%% We must report using erlang:display/1 since we don't know whether
%% there is an error_logger available or not.
report_problem(Error) ->
erlang:display(Error).
|