Put more stuff under supervision

This commit is contained in:
Evgeniy Khramtsov 2017-02-26 15:10:59 +03:00
parent 7decd58aaa
commit fadcc85553
12 changed files with 252 additions and 60 deletions

View File

@ -26,14 +26,20 @@
-module(cyrsasl).
-author('alexey@process-one.net').
-behaviour(gen_server).
-export([start/0, register_mechanism/3, listmech/1,
-export([start_link/0, register_mechanism/3, listmech/1,
server_new/7, server_start/3, server_step/2,
get_mech/1, format_error/2]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-include("ejabberd.hrl").
-include("logger.hrl").
-record(state, {}).
-record(sasl_mechanism,
{mechanism = <<"">> :: mechanism() | '$1',
module :: atom(),
@ -74,10 +80,15 @@
-export_type([mechanism/0, mechanisms/0, sasl_mechanism/0, error_reason/0,
sasl_state/0, sasl_return/0, sasl_property/0]).
-callback start(list()) -> any().
-callback stop() -> any().
-callback mech_new(binary(), fun(), fun(), fun()) -> any().
-callback mech_step(any(), binary()) -> sasl_return().
start() ->
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
init([]) ->
ets:new(sasl_mechanism,
[named_table, public,
{keypos, #sasl_mechanism.mechanism}]),
@ -86,7 +97,27 @@ start() ->
cyrsasl_scram:start([]),
cyrsasl_anonymous:start([]),
cyrsasl_oauth:start([]),
ok.
{ok, #state{}}.
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
cyrsasl_plain:stop(),
cyrsasl_digest:stop(),
cyrsasl_scram:stop(),
cyrsasl_anonymous:stop(),
cyrsasl_oauth:stop().
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
-spec format_error(mechanism() | sasl_state(), error_reason()) -> {atom(), binary()}.
format_error(_, unsupported_mechanism) ->

View File

@ -26,7 +26,9 @@
-module(ejabberd_admin).
-author('mickael.remond@process-one.net').
-export([start/0, stop/0,
-behaviour(gen_server).
-export([start_link/0,
%% Server
status/0, reopen_log/0, rotate_log/0,
set_loglevel/1,
@ -54,17 +56,40 @@
restore/1, % Still used by some modules
get_commands_spec/0
]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-include("ejabberd.hrl").
-include("logger.hrl").
-include("ejabberd_commands.hrl").
start() ->
ejabberd_commands:register_commands(get_commands_spec()).
-record(state, {}).
stop() ->
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
init([]) ->
process_flag(trap_exit, true),
ejabberd_commands:register_commands(get_commands_spec()),
{ok, #state{}}.
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ejabberd_commands:unregister_commands(get_commands_spec()).
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%%
%%% ejabberd commands
%%%

View File

@ -46,22 +46,20 @@ start(normal, _Args) ->
start_elixir_application(),
ejabberd:check_app(ejabberd),
db_init(),
translate:start(),
ejabberd_access_permissions:start_link(),
ejabberd_ctl:init(),
ejabberd_commands:init(),
ejabberd_admin:start(),
setup_if_elixir_conf_used(),
ejabberd_config:start(),
set_settings_from_config(),
maybe_add_nameservers(),
cyrsasl:start(),
connect_nodes(),
Sup = ejabberd_sup:start_link(),
ext_mod:start(),
register_elixir_config_hooks(),
?INFO_MSG("ejabberd ~s is started in the node ~p", [?VERSION, node()]),
Sup;
case ejabberd_sup:start_link() of
{ok, SupPid} ->
register_elixir_config_hooks(),
?INFO_MSG("ejabberd ~s is started in the node ~p",
[?VERSION, node()]),
{ok, SupPid};
Err ->
Err
end;
start(_, _) ->
{error, badarg}.
@ -70,10 +68,8 @@ start(_, _) ->
%% before shutting down the processes of the application.
prep_stop(State) ->
ejabberd_listener:stop_listeners(),
ejabberd_admin:stop(),
ejabberd_sm:stop(),
gen_mod:stop_modules(),
timer:sleep(5000),
State.
%% All the processes were killed when this function is called

View File

@ -210,9 +210,11 @@
-module(ejabberd_commands).
-author('badlop@process-one.net').
-behaviour(gen_server).
-define(DEFAULT_VERSION, 1000000).
-export([init/0,
-export([start_link/0,
list_commands/0,
list_commands/1,
get_command_format/1,
@ -238,6 +240,9 @@
get_commands_definition/1,
execute_command2/3,
execute_command2/4]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-include("ejabberd_commands.hrl").
-include("ejabberd.hrl").
@ -246,6 +251,8 @@
-define(POLICY_ACCESS, '$policy').
-record(state, {}).
get_commands_spec() ->
[
#ejabberd_commands{name = gen_html_doc_for_commands, tags = [documentation],
@ -276,7 +283,11 @@ get_commands_spec() ->
result_desc = "0 if command failed, 1 when succedded",
args_example = ["/home/me/docs/api.html", "mod_admin", "java,json"],
result_example = ok}].
init() ->
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
init([]) ->
try mnesia:transform_table(ejabberd_commands, ignore,
record_info(fields, ejabberd_commands))
catch exit:{aborted, {no_exists, _}} -> ok
@ -288,7 +299,24 @@ init() ->
{type, bag}]),
mnesia:add_table_copy(ejabberd_commands, node(), ram_copies),
register_commands(get_commands_spec()),
ejabberd_access_permissions:register_permission_addon(?MODULE, fun permission_addon/0).
ejabberd_access_permissions:register_permission_addon(?MODULE, fun permission_addon/0),
{ok, #state{}}.
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
-spec register_commands([ejabberd_commands()]) -> ok.

View File

@ -46,11 +46,15 @@
-module(ejabberd_ctl).
-behaviour(ejabberd_config).
-behaviour(gen_server).
-author('alexey@process-one.net').
-export([start/0, init/0, process/1, process2/2,
-export([start/0, start_link/0, process/1, process2/2,
register_commands/3, unregister_commands/3,
opt_type/1]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-include("ejabberd_ctl.hrl").
-include("ejabberd_commands.hrl").
@ -59,6 +63,7 @@
-define(DEFAULT_VERSION, 1000000).
-record(state, {}).
%%-----------------------------
%% Module
@ -103,10 +108,29 @@ start() ->
end,
halt(Status).
init() ->
ets:new(ejabberd_ctl_cmds, [named_table, set, public]),
ets:new(ejabberd_ctl_host_cmds, [named_table, set, public]).
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
init([]) ->
ets:new(ejabberd_ctl_cmds, [named_table, set, public]),
ets:new(ejabberd_ctl_host_cmds, [named_table, set, public]),
{ok, #state{}}.
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%-----------------------------
%% ejabberdctl Command managment

View File

@ -28,7 +28,7 @@
host_of_route/1, is_my_route/1, is_my_host/1, get_all_routes/0]).
%% gen_server callbacks
-export([init/1, handle_cast/2, handle_call/3, handle_info/2,
terminate/2, code_change/3]).
terminate/2, code_change/3, start_link/0]).
-include("ejabberd.hrl").
-include("ejabberd_router.hrl").
@ -40,14 +40,19 @@
%%%===================================================================
%%% API
%%%===================================================================
-spec init() -> ok | {error, any()}.
init() ->
case gen_server:start_link({local, ?MODULE}, ?MODULE, [], []) of
{ok, _Pid} ->
ok;
Err ->
Err
Spec = {?MODULE, {?MODULE, start_link, []},
transient, 5000, worker, [?MODULE]},
case supervisor:start_child(ejabberd_backend_sup, Spec) of
{ok, _Pid} -> ok;
Err -> Err
end.
-spec start_link() -> {ok, pid()} | {error, any()}.
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
register_route(Domain, ServerHost, LocalHint, undefined) ->
F = fun () ->
mnesia:write(#route{domain = Domain,

View File

@ -38,7 +38,7 @@
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
terminate/2, code_change/3, start_link/0]).
-include("ejabberd.hrl").
-include("ejabberd_sm.hrl").
@ -51,13 +51,17 @@
%%%===================================================================
-spec init() -> ok | {error, any()}.
init() ->
case gen_server:start_link({local, ?MODULE}, ?MODULE, [], []) of
{ok, _Pid} ->
ok;
Err ->
Err
Spec = {?MODULE, {?MODULE, start_link, []},
transient, 5000, worker, [?MODULE]},
case supervisor:start_child(ejabberd_backend_sup, Spec) of
{ok, _Pid} -> ok;
Err -> Err
end.
-spec start_link() -> {ok, pid()} | {error, any()}.
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
-spec set_session(#session{}) -> ok.
set_session(Session) ->
mnesia:dirty_write(Session).

View File

@ -128,12 +128,33 @@ init([]) ->
permanent, 5000, worker, [ejabberd_sm]},
GenModSupervisor = {ejabberd_gen_mod_sup, {gen_mod, start_link, []},
permanent, infinity, supervisor, [gen_mod]},
ExtMod = {ext_mod, {ext_mod, start_link, []},
permanent, 5000, worker, [ext_mod]},
Auth = {ejabberd_auth, {ejabberd_auth, start_link, []},
permanent, 5000, worker, [ejabberd_auth]},
OAuth = {ejabberd_oauth, {ejabberd_oauth, start_link, []},
permanent, 5000, worker, [ejabberd_oauth]},
Translation = {translate, {translate, start_link, []},
permanent, 5000, worker, [translate]},
AccessPerms = {ejabberd_access_permissions,
{ejabberd_access_permissions, start_link, []},
permanent, 5000, worker, [ejabberd_access_permissions]},
Ctl = {ejabberd_ctl, {ejabberd_ctl, start_link, []},
permanent, 5000, worker, [ejabberd_ctl]},
Commands = {ejabberd_commands, {ejabberd_commands, start_link, []},
permanent, 5000, worker, [ejabberd_commands]},
Admin = {ejabberd_admin, {ejabberd_admin, start_link, []},
permanent, 5000, worker, [ejabberd_admin]},
CyrSASL = {cyrsasl, {cyrsasl, start_link, []},
permanent, 5000, worker, [cyrsasl]},
{ok, {{one_for_one, 10, 1},
[Hooks,
CyrSASL,
Translation,
AccessPerms,
Ctl,
Commands,
Admin,
Listener,
SystemMonitor,
S2S,
@ -153,5 +174,6 @@ init([]) ->
Local,
SM,
GenModSupervisor,
ExtMod,
Auth,
OAuth]}}.

View File

@ -26,9 +26,10 @@
-module(ext_mod).
-behaviour(ejabberd_config).
-behaviour(gen_server).
-author("Christophe Romain <christophe.romain@process-one.net>").
-export([start/0, stop/0, update/0, check/1,
-export([start_link/0, update/0, check/1,
available_command/0, available/0, available/1,
installed_command/0, installed/0, installed/1,
install/1, uninstall/1, upgrade/0, upgrade/1,
@ -37,22 +38,45 @@
-export([compile_erlang_file/2, compile_elixir_file/2]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-include("ejabberd_commands.hrl").
-include("logger.hrl").
-define(REPOS, "https://github.com/processone/ejabberd-contrib").
%% -- ejabberd init and commands
-record(state, {}).
start() ->
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
init([]) ->
process_flag(trap_exit, true),
[code:add_patha(module_ebin_dir(Module))
|| {Module, _} <- installed()],
p1_http:start(),
ejabberd_commands:register_commands(get_commands_spec()).
ejabberd_commands:register_commands(get_commands_spec()),
{ok, #state{}}.
stop() ->
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ejabberd_commands:unregister_commands(get_commands_spec()).
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%% -- ejabberd commands
get_commands_spec() ->
[#ejabberd_commands{name = modules_update_specs,
tags = [admin,modules],

View File

@ -29,7 +29,7 @@
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
terminate/2, code_change/3, start_link/0]).
-include("logger.hrl").
@ -42,14 +42,19 @@
%%%===================================================================
%%% API
%%%===================================================================
-spec init() -> ok | {error, any()}.
init() ->
case gen_server:start_link({local, ?MODULE}, ?MODULE, [], []) of
{ok, _Pid} ->
ok;
Err ->
Err
Spec = {?MODULE, {?MODULE, start_link, []},
transient, 5000, worker, [?MODULE]},
case supervisor:start_child(ejabberd_backend_sup, Spec) of
{ok, _Pid} -> ok;
Err -> Err
end.
-spec start_link() -> {ok, pid()} | {error, any()}.
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
open_session(SID, Pid) ->
Session = #bosh{sid = SID, timestamp = p1_time_compat:timestamp(), pid = Pid},
lists:foreach(

View File

@ -37,7 +37,7 @@
-export([set_affiliation/6, set_affiliations/4, get_affiliation/5,
get_affiliations/3, search_affiliation/4]).
%% gen_server callbacks
-export([init/1, handle_cast/2, handle_call/3, handle_info/2,
-export([start_link/2, init/1, handle_cast/2, handle_call/3, handle_info/2,
terminate/2, code_change/3]).
-include("mod_muc.hrl").
@ -51,14 +51,17 @@
%%% API
%%%===================================================================
init(Host, Opts) ->
Name = gen_mod:get_module_proc(Host, ?MODULE),
case gen_server:start_link({local, Name}, ?MODULE, [Host, Opts], []) of
{ok, _Pid} ->
ok;
Err ->
Err
Spec = {?MODULE, {?MODULE, start_link, [Host, Opts]},
transient, 5000, worker, [?MODULE]},
case supervisor:start_child(ejabberd_backend_sup, Spec) of
{ok, _Pid} -> ok;
Err -> Err
end.
start_link(Host, Opts) ->
Name = gen_mod:get_module_proc(Host, ?MODULE),
gen_server:start_link({local, Name}, ?MODULE, [Host, Opts], []).
store_room(_LServer, Host, Name, Opts) ->
F = fun () ->
mnesia:write(#muc_room{name_host = {Name, Host},

View File

@ -27,13 +27,22 @@
-author('alexey@process-one.net').
-export([start/0, load_dir/1, load_file/2,
translate/2]).
-behaviour(gen_server).
-export([start_link/0, load_dir/1, load_file/2, translate/2]).
%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-include("ejabberd.hrl").
-include("logger.hrl").
start() ->
-record(state, {}).
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
init([]) ->
ets:new(translations, [named_table, public]),
Dir = case os:getenv("EJABBERD_MSGS_PATH") of
false ->
@ -44,8 +53,24 @@ start() ->
Path -> Path
end,
load_dir(iolist_to_binary(Dir)),
{ok, #state{}}.
handle_call(_Request, _From, State) ->
Reply = ok,
{reply, Reply, State}.
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, _State) ->
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
-spec load_dir(binary()) -> ok.
load_dir(Dir) ->