1%% 2%% %CopyrightBegin% 3%% 4%% Copyright Ericsson AB 1996-2016. 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%% Supervisor for the entire Mnesia application 23 24-module(mnesia_sup). 25 26-behaviour(supervisor). 27 28-export([start_link/1, init/1, start_event/0, kill/0]). 29 30start_link(Args) -> 31 supervisor:start_link({local,?MODULE}, ?MODULE, [Args]). 32 33%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 34%% supervisor callback functions 35 36init([[]]) -> 37 init(); 38init(BadArg) -> 39 {error, {badarg, BadArg}}. 40 41init() -> 42 Flags = {one_for_all, 0, 3600}, % Should be rest_for_one policy 43 44 Event = event_procs(), 45 Ext = ext_procs(), 46 Kernel = kernel_procs(), 47 48 {ok, {Flags, Event ++ Ext ++ Kernel}}. 49 50event_procs() -> 51 KillAfter = timer:seconds(30), 52 KA = mnesia_kernel_sup:supervisor_timeout(KillAfter), 53 E = mnesia_event, 54 [{E, {?MODULE, start_event, []}, permanent, KA, worker, [E, gen_event]}]. 55 56kernel_procs() -> 57 K = mnesia_kernel_sup, 58 KA = infinity, 59 [{K, {K, start, []}, permanent, KA, supervisor, [K, supervisor]}]. 60 61ext_procs() -> 62 K = mnesia_ext_sup, 63 KA = infinity, 64 [{K, {K, start, []}, permanent, KA, supervisor, [K, supervisor]}]. 65 66%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 67%% event handler 68 69start_event() -> 70 case gen_event:start_link({local, mnesia_event}) of 71 {ok, Pid} -> 72 case add_event_handler() of 73 ok -> 74 {ok, Pid}; 75 Error -> 76 Error 77 end; 78 Error -> 79 Error 80 end. 81 82add_event_handler() -> 83 Handler = mnesia_monitor:get_env(event_module), 84 gen_event:add_handler(mnesia_event, Handler, []). 85 86%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 87%% debug functions 88 89kill() -> 90 Mnesia = [mnesia_fallback | mnesia:ms()], 91 Kill = fun(Name) -> catch exit(whereis(Name), kill) end, 92 lists:foreach(Kill, Mnesia), 93 lists:foreach(fun ensure_dead/1, Mnesia), 94 timer:sleep(10), 95 case lists:keymember(mnesia, 1, application:which_applications()) of 96 true -> kill(); 97 false -> ok 98 end. 99 100ensure_dead(Name) -> 101 case whereis(Name) of 102 undefined -> 103 ok; 104 Pid when is_pid(Pid) -> 105 exit(Pid, kill), 106 timer:sleep(10), 107 ensure_dead(Name) 108 end. 109