24
1
mirror of https://github.com/processone/ejabberd.git synced 2024-06-02 21:17:12 +02:00

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). -module(cyrsasl).
-author('alexey@process-one.net'). -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, server_new/7, server_start/3, server_step/2,
get_mech/1, format_error/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("ejabberd.hrl").
-include("logger.hrl"). -include("logger.hrl").
-record(state, {}).
-record(sasl_mechanism, -record(sasl_mechanism,
{mechanism = <<"">> :: mechanism() | '$1', {mechanism = <<"">> :: mechanism() | '$1',
module :: atom(), module :: atom(),
@ -74,10 +80,15 @@
-export_type([mechanism/0, mechanisms/0, sasl_mechanism/0, error_reason/0, -export_type([mechanism/0, mechanisms/0, sasl_mechanism/0, error_reason/0,
sasl_state/0, sasl_return/0, sasl_property/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_new(binary(), fun(), fun(), fun()) -> any().
-callback mech_step(any(), binary()) -> sasl_return(). -callback mech_step(any(), binary()) -> sasl_return().
start() -> start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
init([]) ->
ets:new(sasl_mechanism, ets:new(sasl_mechanism,
[named_table, public, [named_table, public,
{keypos, #sasl_mechanism.mechanism}]), {keypos, #sasl_mechanism.mechanism}]),
@ -86,7 +97,27 @@ start() ->
cyrsasl_scram:start([]), cyrsasl_scram:start([]),
cyrsasl_anonymous:start([]), cyrsasl_anonymous:start([]),
cyrsasl_oauth: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()}. -spec format_error(mechanism() | sasl_state(), error_reason()) -> {atom(), binary()}.
format_error(_, unsupported_mechanism) -> format_error(_, unsupported_mechanism) ->

View File

@ -26,7 +26,9 @@
-module(ejabberd_admin). -module(ejabberd_admin).
-author('mickael.remond@process-one.net'). -author('mickael.remond@process-one.net').
-export([start/0, stop/0, -behaviour(gen_server).
-export([start_link/0,
%% Server %% Server
status/0, reopen_log/0, rotate_log/0, status/0, reopen_log/0, rotate_log/0,
set_loglevel/1, set_loglevel/1,
@ -54,17 +56,40 @@
restore/1, % Still used by some modules restore/1, % Still used by some modules
get_commands_spec/0 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("ejabberd.hrl").
-include("logger.hrl"). -include("logger.hrl").
-include("ejabberd_commands.hrl"). -include("ejabberd_commands.hrl").
start() -> -record(state, {}).
ejabberd_commands:register_commands(get_commands_spec()).
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()). ejabberd_commands:unregister_commands(get_commands_spec()).
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%% %%%
%%% ejabberd commands %%% ejabberd commands
%%% %%%

View File

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

View File

@ -210,9 +210,11 @@
-module(ejabberd_commands). -module(ejabberd_commands).
-author('badlop@process-one.net'). -author('badlop@process-one.net').
-behaviour(gen_server).
-define(DEFAULT_VERSION, 1000000). -define(DEFAULT_VERSION, 1000000).
-export([init/0, -export([start_link/0,
list_commands/0, list_commands/0,
list_commands/1, list_commands/1,
get_command_format/1, get_command_format/1,
@ -238,6 +240,9 @@
get_commands_definition/1, get_commands_definition/1,
execute_command2/3, execute_command2/3,
execute_command2/4]). 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_commands.hrl").
-include("ejabberd.hrl"). -include("ejabberd.hrl").
@ -246,6 +251,8 @@
-define(POLICY_ACCESS, '$policy'). -define(POLICY_ACCESS, '$policy').
-record(state, {}).
get_commands_spec() -> get_commands_spec() ->
[ [
#ejabberd_commands{name = gen_html_doc_for_commands, tags = [documentation], #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", result_desc = "0 if command failed, 1 when succedded",
args_example = ["/home/me/docs/api.html", "mod_admin", "java,json"], args_example = ["/home/me/docs/api.html", "mod_admin", "java,json"],
result_example = ok}]. result_example = ok}].
init() ->
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
init([]) ->
try mnesia:transform_table(ejabberd_commands, ignore, try mnesia:transform_table(ejabberd_commands, ignore,
record_info(fields, ejabberd_commands)) record_info(fields, ejabberd_commands))
catch exit:{aborted, {no_exists, _}} -> ok catch exit:{aborted, {no_exists, _}} -> ok
@ -288,7 +299,24 @@ init() ->
{type, bag}]), {type, bag}]),
mnesia:add_table_copy(ejabberd_commands, node(), ram_copies), mnesia:add_table_copy(ejabberd_commands, node(), ram_copies),
register_commands(get_commands_spec()), 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. -spec register_commands([ejabberd_commands()]) -> ok.

View File

@ -46,11 +46,15 @@
-module(ejabberd_ctl). -module(ejabberd_ctl).
-behaviour(ejabberd_config). -behaviour(ejabberd_config).
-behaviour(gen_server).
-author('alexey@process-one.net'). -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, register_commands/3, unregister_commands/3,
opt_type/1]). 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_ctl.hrl").
-include("ejabberd_commands.hrl"). -include("ejabberd_commands.hrl").
@ -59,6 +63,7 @@
-define(DEFAULT_VERSION, 1000000). -define(DEFAULT_VERSION, 1000000).
-record(state, {}).
%%----------------------------- %%-----------------------------
%% Module %% Module
@ -103,10 +108,29 @@ start() ->
end, end,
halt(Status). halt(Status).
init() -> start_link() ->
ets:new(ejabberd_ctl_cmds, [named_table, set, public]), gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
ets:new(ejabberd_ctl_host_cmds, [named_table, set, public]).
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 %% 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]). host_of_route/1, is_my_route/1, is_my_host/1, get_all_routes/0]).
%% gen_server callbacks %% gen_server callbacks
-export([init/1, handle_cast/2, handle_call/3, handle_info/2, -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.hrl").
-include("ejabberd_router.hrl"). -include("ejabberd_router.hrl").
@ -40,14 +40,19 @@
%%%=================================================================== %%%===================================================================
%%% API %%% API
%%%=================================================================== %%%===================================================================
-spec init() -> ok | {error, any()}.
init() -> init() ->
case gen_server:start_link({local, ?MODULE}, ?MODULE, [], []) of Spec = {?MODULE, {?MODULE, start_link, []},
{ok, _Pid} -> transient, 5000, worker, [?MODULE]},
ok; case supervisor:start_child(ejabberd_backend_sup, Spec) of
Err -> {ok, _Pid} -> ok;
Err Err -> Err
end. end.
-spec start_link() -> {ok, pid()} | {error, any()}.
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
register_route(Domain, ServerHost, LocalHint, undefined) -> register_route(Domain, ServerHost, LocalHint, undefined) ->
F = fun () -> F = fun () ->
mnesia:write(#route{domain = Domain, mnesia:write(#route{domain = Domain,

View File

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

View File

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

View File

@ -26,9 +26,10 @@
-module(ext_mod). -module(ext_mod).
-behaviour(ejabberd_config). -behaviour(ejabberd_config).
-behaviour(gen_server).
-author("Christophe Romain <christophe.romain@process-one.net>"). -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, available_command/0, available/0, available/1,
installed_command/0, installed/0, installed/1, installed_command/0, installed/0, installed/1,
install/1, uninstall/1, upgrade/0, upgrade/1, install/1, uninstall/1, upgrade/0, upgrade/1,
@ -37,22 +38,45 @@
-export([compile_erlang_file/2, compile_elixir_file/2]). -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("ejabberd_commands.hrl").
-include("logger.hrl"). -include("logger.hrl").
-define(REPOS, "https://github.com/processone/ejabberd-contrib"). -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)) [code:add_patha(module_ebin_dir(Module))
|| {Module, _} <- installed()], || {Module, _} <- installed()],
p1_http:start(), 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()). ejabberd_commands:unregister_commands(get_commands_spec()).
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%% -- ejabberd commands
get_commands_spec() -> get_commands_spec() ->
[#ejabberd_commands{name = modules_update_specs, [#ejabberd_commands{name = modules_update_specs,
tags = [admin,modules], tags = [admin,modules],

View File

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

View File

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

View File

@ -27,13 +27,22 @@
-author('alexey@process-one.net'). -author('alexey@process-one.net').
-export([start/0, load_dir/1, load_file/2, -behaviour(gen_server).
translate/2]).
-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("ejabberd.hrl").
-include("logger.hrl"). -include("logger.hrl").
start() -> -record(state, {}).
start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).
init([]) ->
ets:new(translations, [named_table, public]), ets:new(translations, [named_table, public]),
Dir = case os:getenv("EJABBERD_MSGS_PATH") of Dir = case os:getenv("EJABBERD_MSGS_PATH") of
false -> false ->
@ -44,8 +53,24 @@ start() ->
Path -> Path Path -> Path
end, end,
load_dir(iolist_to_binary(Dir)), 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. ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
-spec load_dir(binary()) -> ok. -spec load_dir(binary()) -> ok.
load_dir(Dir) -> load_dir(Dir) ->