From 28f66ddd7c3d39bbc694a32a396fc1c40296983e Mon Sep 17 00:00:00 2001 From: Evgeniy Khramtsov Date: Tue, 14 Feb 2017 12:39:26 +0300 Subject: [PATCH] Attach modules to gen_mod's supervisor --- include/bosh.hrl | 2 -- src/ejabberd_bosh.erl | 2 +- src/gen_mod.erl | 39 ++++++++++++++++++++++++++++++---- src/mod_announce.erl | 27 ++++++----------------- src/mod_bosh.erl | 4 ++-- src/mod_caps.erl | 18 +++------------- src/mod_carboncopy.erl | 1 - src/mod_delegation.erl | 15 ++----------- src/mod_echo.erl | 38 +++++++++------------------------ src/mod_fail2ban.erl | 16 +++----------- src/mod_http_fileserver.erl | 33 +++------------------------- src/mod_http_upload.erl | 32 ++++++++-------------------- src/mod_http_upload_quota.erl | 34 ++++++++--------------------- src/mod_irc.erl | 25 ++++------------------ src/mod_mix.erl | 17 +++------------ src/mod_muc.erl | 23 +++++--------------- src/mod_muc_log.erl | 19 ++++------------- src/mod_multicast.erl | 20 +++-------------- src/mod_offline.erl | 24 ++++++--------------- src/mod_ping.erl | 17 +++------------ src/mod_privilege.erl | 15 ++----------- src/mod_pubsub.erl | 17 +++------------ src/mod_shared_roster_ldap.erl | 17 +++------------ src/mod_sip_registrar.erl | 4 +++- src/mod_vcard.erl | 21 +++--------------- 25 files changed, 125 insertions(+), 355 deletions(-) diff --git a/include/bosh.hrl b/include/bosh.hrl index 6468efac4..3f9095e58 100644 --- a/include/bosh.hrl +++ b/include/bosh.hrl @@ -47,5 +47,3 @@ -define(HEADER(CType), [CType, ?AC_ALLOW_ORIGIN, ?AC_ALLOW_HEADERS]). - --define(PROCNAME, ejabberd_mod_bosh). diff --git a/src/ejabberd_bosh.erl b/src/ejabberd_bosh.erl index b38de2515..91641523e 100644 --- a/src/ejabberd_bosh.erl +++ b/src/ejabberd_bosh.erl @@ -123,7 +123,7 @@ start(#body{attrs = Attrs} = Body, IP, SID) -> XMPPDomain = get_attr(to, Attrs), - SupervisorProc = gen_mod:get_module_proc(XMPPDomain, ?PROCNAME), + SupervisorProc = gen_mod:get_module_proc(XMPPDomain, mod_bosh), case catch supervisor:start_child(SupervisorProc, [Body, IP, SID]) of diff --git a/src/gen_mod.erl b/src/gen_mod.erl index 872ae8589..578b3e7e0 100644 --- a/src/gen_mod.erl +++ b/src/gen_mod.erl @@ -26,9 +26,12 @@ -module(gen_mod). -behaviour(ejabberd_config). +-behaviour(supervisor). -author('alexey@process-one.net'). +-export([init/1, start_link/0, start_child/3, start_child/4, + stop_child/1, stop_child/2]). -export([start/0, start_module/2, start_module/3, stop_module/2, stop_module_keep_config/2, get_opt/3, get_opt/4, get_opt_host/3, opt_type/1, @@ -59,14 +62,43 @@ -export_type([opts/0]). -export_type([db_type/0]). -%%behaviour_info(callbacks) -> [{start, 2}, {stop, 1}]; -%%behaviour_info(_Other) -> undefined. +-ifndef(GEN_SERVER). +-define(GEN_SERVER, gen_server). +-endif. start() -> + Spec = {ejabberd_gen_mod_sup, {?MODULE, start_link, []}, + permanent, infinity, supervisor, [?MODULE]}, + supervisor:start_child(ejabberd_sup, Spec). + +start_link() -> + supervisor:start_link({local, ejabberd_gen_mod_sup}, ?MODULE, []). + +init([]) -> ets:new(ejabberd_modules, [named_table, public, {keypos, #ejabberd_module.module_host}]), - ok. + {ok, {{one_for_one, 10, 1}, []}}. + +-spec start_child(module(), binary() | global, opts()) -> ok | {error, any()}. +start_child(Mod, Host, Opts) -> + start_child(Mod, Host, Opts, get_module_proc(Host, Mod)). + +-spec start_child(module(), binary() | global, opts(), atom()) -> ok | {error, any()}. +start_child(Mod, Host, Opts, Proc) -> + Spec = {Proc, {?GEN_SERVER, start_link, + [{local, Proc}, Mod, [Host, Opts], []]}, + transient, 2000, worker, [Mod]}, + supervisor:start_child(ejabberd_gen_mod_sup, Spec). + +-spec stop_child(module(), binary() | global) -> ok. +stop_child(Mod, Host) -> + stop_child(get_module_proc(Host, Mod)). + +-spec stop_child(atom()) -> ok | {error, any()}. +stop_child(Proc) -> + supervisor:terminate_child(ejabberd_gen_mod_sup, Proc), + supervisor:delete_child(ejabberd_gen_mod_sup, Proc). -spec start_modules() -> any(). @@ -500,7 +532,6 @@ get_hosts(Opts, Prefix) -> end. -spec get_module_proc(binary(), {frontend, atom()} | atom()) -> atom(). - get_module_proc(Host, {frontend, Base}) -> get_module_proc(<<"frontend_", Host/binary>>, Base); get_module_proc(Host, Base) -> diff --git a/src/mod_announce.erl b/src/mod_announce.erl index e22a42223..c21adfb00 100644 --- a/src/mod_announce.erl +++ b/src/mod_announce.erl @@ -37,7 +37,7 @@ disco_features/5, disco_items/5, depends/2, send_announcement_to_all/3, announce_commands/4, announce_items/4, mod_opt_type/1]). --export([start_link/2, init/1, handle_call/3, handle_cast/2, +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). -include("ejabberd.hrl"). @@ -56,34 +56,19 @@ -record(state, {host :: binary()}). --define(PROCNAME, ejabberd_announce). - -define(NS_ADMINL(Sub), [<<"http:">>, <<"jabber.org">>, <<"protocol">>, <<"admin">>, <>]). tokenize(Node) -> str:tokens(Node, <<"/#">>). -%%==================================================================== -%% API -%%==================================================================== -start_link(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - gen_server:start_link({local, Proc}, ?MODULE, [Host, Opts], []). - %%==================================================================== %% gen_mod callbacks %%==================================================================== start(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - Spec = {Proc, {?MODULE, start_link, [Host, Opts]}, - transient, 2000, worker, [?MODULE]}, - supervisor:start_child(ejabberd_sup, Spec). + gen_mod:start_child(?MODULE, Host, Opts). stop(Host) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - supervisor:terminate_child(ejabberd_sup, Proc), - supervisor:delete_child(ejabberd_sup, Proc), - ok. + gen_mod:stop_child(?MODULE, Host). depends(_Host, _Opts) -> [{mod_adhoc, hard}]. @@ -155,7 +140,7 @@ code_change(_OldVsn, State, _Extra) -> %% Announcing via messages to a custom resource -spec announce(jid(), jid(), stanza()) -> ok | stop. announce(From, #jid{luser = <<>>} = To, #message{} = Packet) -> - Proc = gen_mod:get_module_proc(To#jid.lserver, ?PROCNAME), + Proc = gen_mod:get_module_proc(To#jid.lserver, ?MODULE), Res = case To#jid.lresource of <<"announce/all">> -> gen_server:cast(Proc, {announce_all, From, To, Packet}); @@ -539,7 +524,7 @@ handle_adhoc_form(From, #jid{lserver = LServer} = To, Packet = #message{type = headline, body = xmpp:mk_text(Body), subject = xmpp:mk_text(Subject)}, - Proc = gen_mod:get_module_proc(LServer, ?PROCNAME), + Proc = gen_mod:get_module_proc(LServer, ?MODULE), case {Node, Body} of {?NS_ADMIN_DELETE_MOTD, _} -> if Confirm -> @@ -560,7 +545,7 @@ handle_adhoc_form(From, #jid{lserver = LServer} = To, %% Throw an error and give him/her a chance to send message again. {error, xmpp:err_not_acceptable( <<"No body provided for announce message">>, Lang)}; - %% Now send the packet to ?PROCNAME. + %% Now send the packet to ?MODULE. %% We don't use direct announce_* functions because it %% leads to large delay in response and queries processing {?NS_ADMIN_ANNOUNCE, _} -> diff --git a/src/mod_bosh.erl b/src/mod_bosh.erl index 92ce6bc10..fd33d4b79 100644 --- a/src/mod_bosh.erl +++ b/src/mod_bosh.erl @@ -92,14 +92,14 @@ start(Host, Opts) -> start_jiffy(Opts), Mod = gen_mod:ram_db_mod(global, ?MODULE), Mod:init(), - TmpSup = gen_mod:get_module_proc(Host, ?PROCNAME), + TmpSup = gen_mod:get_module_proc(Host, ?MODULE), TmpSupSpec = {TmpSup, {ejabberd_tmp_sup, start_link, [TmpSup, ejabberd_bosh]}, permanent, infinity, supervisor, [ejabberd_tmp_sup]}, supervisor:start_child(ejabberd_sup, TmpSupSpec). stop(Host) -> - TmpSup = gen_mod:get_module_proc(Host, ?PROCNAME), + TmpSup = gen_mod:get_module_proc(Host, ?MODULE), supervisor:terminate_child(ejabberd_sup, TmpSup), supervisor:delete_child(ejabberd_sup, TmpSup). diff --git a/src/mod_caps.erl b/src/mod_caps.erl index c001a696c..85c301f6c 100644 --- a/src/mod_caps.erl +++ b/src/mod_caps.erl @@ -41,7 +41,7 @@ get_user_caps/2, import_start/2, import_stop/2]). %% gen_mod callbacks --export([start/2, start_link/2, stop/1, depends/2]). +-export([start/2, stop/1, depends/2]). %% gen_server callbacks -export([init/1, handle_info/2, handle_call/3, @@ -56,8 +56,6 @@ -include("xmpp.hrl"). -include("mod_caps.hrl"). --define(PROCNAME, ejabberd_mod_caps). - -define(BAD_HASH_LIFETIME, 600). -record(state, {host = <<"">> :: binary()}). @@ -69,21 +67,11 @@ -callback caps_write(binary(), {binary(), binary()}, non_neg_integer() | [binary()]) -> any(). -start_link(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - gen_server:start_link({local, Proc}, ?MODULE, - [Host, Opts], []). - start(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - ChildSpec = {Proc, {?MODULE, start_link, [Host, Opts]}, - transient, 1000, worker, [?MODULE]}, - supervisor:start_child(ejabberd_sup, ChildSpec). + gen_mod:start_child(?MODULE, Host, Opts). stop(Host) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - supervisor:terminate_child(ejabberd_sup, Proc), - supervisor:delete_child(ejabberd_sup, Proc). + gen_mod:stop_child(?MODULE, Host). -spec get_features(binary(), nothing | caps()) -> [binary()]. get_features(_Host, nothing) -> []; diff --git a/src/mod_carboncopy.erl b/src/mod_carboncopy.erl index 202c7005a..0c782b728 100644 --- a/src/mod_carboncopy.erl +++ b/src/mod_carboncopy.erl @@ -42,7 +42,6 @@ -include("ejabberd.hrl"). -include("logger.hrl"). -include("xmpp.hrl"). --define(PROCNAME, ?MODULE). -type direction() :: sent | received. diff --git a/src/mod_delegation.erl b/src/mod_delegation.erl index 9abf5a45f..2037bfa34 100644 --- a/src/mod_delegation.erl +++ b/src/mod_delegation.erl @@ -31,7 +31,6 @@ -behaviour(gen_mod). %% API --export([start_link/2]). -export([start/2, stop/1, mod_opt_type/1, depends/2]). %% gen_server callbacks -export([init/1, handle_call/3, handle_cast/2, handle_info/2, @@ -52,21 +51,11 @@ %%%=================================================================== %%% API %%%=================================================================== -start_link(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?MODULE), - gen_server:start_link({local, Proc}, ?MODULE, [Host, Opts], []). - start(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?MODULE), - PingSpec = {Proc, {?MODULE, start_link, [Host, Opts]}, - transient, 2000, worker, [?MODULE]}, - supervisor:start_child(ejabberd_sup, PingSpec). + gen_mod:start_child(?MODULE, Host, Opts). stop(Host) -> - Proc = gen_mod:get_module_proc(Host, ?MODULE), - supervisor:terminate_child(ejabberd_sup, Proc), - supervisor:delete_child(ejabberd_sup, Proc), - ok. + gen_mod:stop_child(?MODULE, Host). mod_opt_type(iqdisc) -> fun gen_iq_handler:check_type/1; mod_opt_type(namespaces) -> validate_fun(); diff --git a/src/mod_echo.erl b/src/mod_echo.erl index 80e41494b..7c8722fce 100644 --- a/src/mod_echo.erl +++ b/src/mod_echo.erl @@ -32,8 +32,7 @@ -behaviour(gen_mod). %% API --export([start_link/2, start/2, stop/1, - do_client_version/3]). +-export([start/2, stop/1, do_client_version/3]). -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3, @@ -46,31 +45,20 @@ -record(state, {host = <<"">> :: binary()}). --define(PROCNAME, ejabberd_mod_echo). - %%==================================================================== -%% API +%% gen_mod API %%==================================================================== -%%-------------------------------------------------------------------- -%% Function: start_link() -> {ok,Pid} | ignore | {error,Error} -%% Description: Starts the server -%%-------------------------------------------------------------------- -start_link(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - gen_server:start_link({local, Proc}, ?MODULE, - [Host, Opts], []). - start(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - ChildSpec = {Proc, {?MODULE, start_link, [Host, Opts]}, - transient, 1000, worker, [?MODULE]}, - supervisor:start_child(ejabberd_sup, ChildSpec). + gen_mod:start_child(?MODULE, Host, Opts). stop(Host) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - supervisor:terminate_child(ejabberd_sup, Proc), - supervisor:delete_child(ejabberd_sup, Proc), - ok. + gen_mod:stop_child(?MODULE, Host). + +depends(_Host, _Opts) -> + []. + +mod_opt_type(host) -> fun iolist_to_binary/1; +mod_opt_type(_) -> [host]. %%==================================================================== %% gen_server callbacks @@ -190,9 +178,3 @@ do_client_version(enabled, From, To) -> after 5000 -> % Timeout in miliseconds: 5 seconds [] end. - -depends(_Host, _Opts) -> - []. - -mod_opt_type(host) -> fun iolist_to_binary/1; -mod_opt_type(_) -> [host]. diff --git a/src/mod_fail2ban.erl b/src/mod_fail2ban.erl index d76b2f990..94d4d89e2 100644 --- a/src/mod_fail2ban.erl +++ b/src/mod_fail2ban.erl @@ -28,7 +28,7 @@ -behaviour(gen_server). %% API --export([start_link/2, start/2, stop/1, c2s_auth_result/3, +-export([start/2, stop/1, c2s_auth_result/3, c2s_stream_started/2]). -export([init/1, handle_call/3, handle_cast/2, @@ -49,10 +49,6 @@ %%%=================================================================== %%% API %%%=================================================================== -start_link(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?MODULE), - gen_server:start_link({local, Proc}, ?MODULE, [Host, Opts], []). - -spec c2s_auth_result(ejabberd_c2s:state(), boolean(), binary()) -> ejabberd_c2s:state() | {stop, ejabberd_c2s:state()}. c2s_auth_result(#{ip := {Addr, _}, lserver := LServer} = State, false, _User) -> @@ -111,16 +107,10 @@ c2s_stream_started(#{ip := {Addr, _}} = State, _) -> %%==================================================================== start(Host, Opts) -> catch ets:new(failed_auth, [named_table, public]), - Proc = gen_mod:get_module_proc(Host, ?MODULE), - ChildSpec = {Proc, {?MODULE, start_link, [Host, Opts]}, - transient, 1000, worker, [?MODULE]}, - supervisor:start_child(ejabberd_sup, ChildSpec). + gen_mod:start_child(?MODULE, Host, Opts). stop(Host) -> - Proc = gen_mod:get_module_proc(Host, ?MODULE), - supervisor:terminate_child(ejabberd_sup, Proc), - supervisor:delete_child(ejabberd_sup, Proc), - ok. + gen_mod:stop_child(?MODULE, Host). depends(_Host, _Opts) -> []. diff --git a/src/mod_http_fileserver.erl b/src/mod_http_fileserver.erl index 7c95d96bd..143ab03f5 100644 --- a/src/mod_http_fileserver.erl +++ b/src/mod_http_fileserver.erl @@ -33,9 +33,6 @@ %% gen_mod callbacks -export([start/2, stop/1]). -%% API --export([start_link/2]). - %% gen_server callbacks -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). @@ -58,8 +55,6 @@ directory_indices, custom_headers, default_content_type, content_types = [], user_access = none}). --define(PROCNAME, ejabberd_mod_http_fileserver). - %% Response is {DataSize, Code, [{HeaderKey, HeaderValue}], Data} -define(HTTP_ERR_FILE_NOT_FOUND, {-1, 404, [], <<"Not found">>}). @@ -90,36 +85,14 @@ %%==================================================================== start(Host, Opts) -> - Proc = get_proc_name(Host), - ChildSpec = - {Proc, - {?MODULE, start_link, [Host, Opts]}, - transient, % if process crashes abruptly, it gets restarted - 1000, - worker, - [?MODULE]}, - supervisor:start_child(ejabberd_sup, ChildSpec). + gen_mod:start_child(?MODULE, Host, Opts). stop(Host) -> - Proc = get_proc_name(Host), - supervisor:terminate_child(ejabberd_sup, Proc), - supervisor:delete_child(ejabberd_sup, Proc), - ok. + gen_mod:stop_child(?MODULE, Host). depends(_Host, _Opts) -> []. -%%==================================================================== -%% API -%%==================================================================== -%%-------------------------------------------------------------------- -%% Function: start_link() -> {ok,Pid} | ignore | {error,Error} -%% Description: Starts the server -%%-------------------------------------------------------------------- -start_link(Host, Opts) -> - Proc = get_proc_name(Host), - gen_server:start_link({local, Proc}, ?MODULE, [Host, Opts], []). - %%==================================================================== %% gen_server callbacks %%==================================================================== @@ -459,7 +432,7 @@ find_header(Header, Headers, Default) -> %% Utilities %%---------------------------------------------------------------------- -get_proc_name(Host) -> gen_mod:get_module_proc(Host, ?PROCNAME). +get_proc_name(Host) -> gen_mod:get_module_proc(Host, ?MODULE). join([], _) -> <<"">>; diff --git a/src/mod_http_upload.erl b/src/mod_http_upload.erl index 55efc1ab0..c32c189e1 100644 --- a/src/mod_http_upload.erl +++ b/src/mod_http_upload.erl @@ -28,10 +28,11 @@ -protocol({xep, 363, '0.1'}). +-ifndef(GEN_SERVER). -define(GEN_SERVER, gen_server). +-endif. -define(SERVICE_REQUEST_TIMEOUT, 5000). % 5 seconds. -define(SLOT_TIMEOUT, 18000000). % 5 hours. --define(PROCNAME, ?MODULE). -define(FORMAT(Error), file:format_error(Error)). -define(URL_ENC(URL), binary_to_list(ejabberd_http:url_encode(URL))). -define(ADDR_TO_STR(IP), ejabberd_config:may_hide_data(jlib:ip_to_list(IP))). @@ -65,8 +66,7 @@ -behaviour(gen_mod). %% gen_mod/supervisor callbacks. --export([start_link/3, - start/2, +-export([start/2, stop/1, depends/2, mod_opt_type/1]). @@ -124,13 +124,6 @@ %%-------------------------------------------------------------------- %% gen_mod/supervisor callbacks. %%-------------------------------------------------------------------- - --spec start_link(binary(), atom(), gen_mod:opts()) - -> {ok, pid()} | ignore | {error, _}. - -start_link(ServerHost, Proc, Opts) -> - ?GEN_SERVER:start_link({local, Proc}, ?MODULE, {ServerHost, Opts}, []). - -spec start(binary(), gen_mod:opts()) -> {ok, _} | {ok, _, _} | {error, _}. start(ServerHost, Opts) -> @@ -143,14 +136,8 @@ start(ServerHost, Opts) -> false -> ok end, - Proc = get_proc_name(ServerHost, ?PROCNAME), - Spec = {Proc, - {?MODULE, start_link, [ServerHost, Proc, Opts]}, - permanent, - 3000, - worker, - [?MODULE]}, - supervisor:start_child(ejabberd_sup, Spec). + Proc = get_proc_name(ServerHost, ?MODULE), + gen_mod:start_child(?MODULE, ServerHost, Opts, Proc). -spec stop(binary()) -> ok. @@ -164,9 +151,8 @@ stop(ServerHost) -> false -> ok end, - Proc = get_proc_name(ServerHost, ?PROCNAME), - supervisor:terminate_child(ejabberd_sup, Proc), - supervisor:delete_child(ejabberd_sup, Proc). + Proc = get_proc_name(ServerHost, ?MODULE), + gen_mod:stop_child(Proc). -spec mod_opt_type(atom()) -> fun((term()) -> term()) | [atom()]. @@ -230,7 +216,7 @@ depends(_Host, _Opts) -> -spec init({binary(), gen_mod:opts()}) -> {ok, state()}. -init({ServerHost, Opts}) -> +init([ServerHost, Opts]) -> process_flag(trap_exit, true), Host = gen_mod:get_opt_host(ServerHost, Opts, <<"upload.@HOST@">>), Name = gen_mod:get_opt(name, Opts, @@ -750,7 +736,7 @@ parse_http_request(#request{host = Host, path = Path}) -> true -> {Host, Path} end, - {gen_mod:get_module_proc(ProcURL, ?PROCNAME), Slot}. + {gen_mod:get_module_proc(ProcURL, ?MODULE), Slot}. -spec store_file(binary(), binary(), integer() | undefined, diff --git a/src/mod_http_upload_quota.erl b/src/mod_http_upload_quota.erl index c7a697743..ee0ea9d1d 100644 --- a/src/mod_http_upload_quota.erl +++ b/src/mod_http_upload_quota.erl @@ -26,8 +26,9 @@ -module(mod_http_upload_quota). -author('holger@zedat.fu-berlin.de'). +-ifndef(GEN_SERVER). -define(GEN_SERVER, gen_server). --define(PROCNAME, ?MODULE). +-endif. -define(TIMEOUT, timer:hours(24)). -define(INITIAL_TIMEOUT, timer:minutes(10)). -define(FORMAT(Error), file:format_error(Error)). @@ -36,8 +37,7 @@ -behaviour(gen_mod). %% gen_mod/supervisor callbacks. --export([start_link/3, - start/2, +-export([start/2, stop/1, depends/2, mod_opt_type/1]). @@ -71,31 +71,17 @@ %%-------------------------------------------------------------------- %% gen_mod/supervisor callbacks. %%-------------------------------------------------------------------- - --spec start_link(binary(), atom(), gen_mod:opts()) - -> {ok, pid()} | ignore | {error, _}. - -start_link(ServerHost, Proc, Opts) -> - ?GEN_SERVER:start_link({local, Proc}, ?MODULE, {ServerHost, Opts}, []). - -spec start(binary(), gen_mod:opts()) -> {ok, _} | {ok, _, _} | {error, _}. start(ServerHost, Opts) -> - Proc = mod_http_upload:get_proc_name(ServerHost, ?PROCNAME), - Spec = {Proc, - {?MODULE, start_link, [ServerHost, Proc, Opts]}, - permanent, - 3000, - worker, - [?MODULE]}, - supervisor:start_child(ejabberd_sup, Spec). + Proc = mod_http_upload:get_proc_name(ServerHost, ?MODULE), + gen_mod:start_child(?MODULE, ServerHost, Opts, Proc). -spec stop(binary()) -> ok. stop(ServerHost) -> - Proc = mod_http_upload:get_proc_name(ServerHost, ?PROCNAME), - supervisor:terminate_child(ejabberd_sup, Proc), - supervisor:delete_child(ejabberd_sup, Proc). + Proc = mod_http_upload:get_proc_name(ServerHost, ?MODULE), + gen_mod:stop_child(Proc). -spec mod_opt_type(atom()) -> fun((term()) -> term()) | [atom()]. @@ -119,9 +105,7 @@ depends(_Host, _Opts) -> %% gen_server callbacks. %%-------------------------------------------------------------------- --spec init({binary(), gen_mod:opts()}) -> {ok, state()}. - -init({ServerHost, Opts}) -> +init([ServerHost, Opts]) -> process_flag(trap_exit, true), AccessSoftQuota = gen_mod:get_opt(access_soft_quota, Opts, fun acl:shaper_rules_validator/1, @@ -268,7 +252,7 @@ code_change(_OldVsn, #state{server_host = ServerHost} = State, _Extra) -> handle_slot_request(allow, #jid{lserver = ServerHost} = JID, Path, Size, _Lang) -> - Proc = mod_http_upload:get_proc_name(ServerHost, ?PROCNAME), + Proc = mod_http_upload:get_proc_name(ServerHost, ?MODULE), ?GEN_SERVER:cast(Proc, {handle_slot_request, JID, Path, Size}), allow; handle_slot_request(Acc, _JID, _Path, _Size, _Lang) -> Acc. diff --git a/src/mod_irc.erl b/src/mod_irc.erl index 25c7430b2..dcb603317 100644 --- a/src/mod_irc.erl +++ b/src/mod_irc.erl @@ -32,7 +32,7 @@ -behaviour(gen_mod). %% API --export([start_link/2, start/2, stop/1, export/1, import/1, +-export([start/2, stop/1, export/1, import/1, import/3, closed_connection/3, get_connection_params/3, data_to_binary/2, process_disco_info/1, process_disco_items/1, process_register/1, process_vcard/1, process_command/1]). @@ -62,38 +62,21 @@ server_host = <<"">> :: binary(), access = all :: atom()}). --define(PROCNAME, ejabberd_mod_irc). - -callback init(binary(), gen_mod:opts()) -> any(). -callback import(binary(), #irc_custom{}) -> ok | pass. -callback get_data(binary(), binary(), jid()) -> error | empty | irc_data(). -callback set_data(binary(), binary(), jid(), irc_data()) -> {atomic, any()}. %%==================================================================== -%% API +%% gen_mod API %%==================================================================== -%%-------------------------------------------------------------------- -%% Function: start_link() -> {ok,Pid} | ignore | {error,Error} -%% Description: Starts the server -%%-------------------------------------------------------------------- -start_link(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - gen_server:start_link({local, Proc}, ?MODULE, - [Host, Opts], []). - start(Host, Opts) -> start_supervisor(Host), - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - ChildSpec = {Proc, {?MODULE, start_link, [Host, Opts]}, - transient, 1000, worker, [?MODULE]}, - supervisor:start_child(ejabberd_sup, ChildSpec). + gen_mod:start_child(?MODULE, Host, Opts). stop(Host) -> stop_supervisor(Host), - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - supervisor:terminate_child(ejabberd_sup, Proc), - supervisor:delete_child(ejabberd_sup, Proc), - ok. + gen_mod:stop_child(?MODULE, Host). depends(_Host, _Opts) -> []. diff --git a/src/mod_mix.erl b/src/mod_mix.erl index 9282d4a16..6ee796fc6 100644 --- a/src/mod_mix.erl +++ b/src/mod_mix.erl @@ -28,7 +28,7 @@ -behaviour(gen_mod). %% API --export([start_link/2, start/2, stop/1, process_iq/1, +-export([start/2, stop/1, process_iq/1, disco_items/5, disco_identity/5, disco_info/5, disco_features/5, mod_opt_type/1, depends/2]). @@ -39,7 +39,6 @@ -include("logger.hrl"). -include("xmpp.hrl"). --define(PROCNAME, ejabberd_mod_mix). -define(NODES, [?NS_MIX_NODES_MESSAGES, ?NS_MIX_NODES_PRESENCE, ?NS_MIX_NODES_PARTICIPANTS, @@ -52,21 +51,11 @@ %%%=================================================================== %%% API %%%=================================================================== -start_link(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - gen_server:start_link({local, Proc}, ?MODULE, [Host, Opts], []). - start(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - ChildSpec = {Proc, {?MODULE, start_link, [Host, Opts]}, - transient, 5000, worker, [?MODULE]}, - supervisor:start_child(ejabberd_sup, ChildSpec). + gen_mod:start_child(?MODULE, Host, Opts). stop(Host) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - supervisor:terminate_child(ejabberd_sup, Proc), - supervisor:delete_child(ejabberd_sup, Proc), - ok. + gen_mod:stop_child(?MODULE, Host). -spec disco_features({error, stanza_error()} | {result, [binary()]} | empty, jid(), jid(), binary(), binary()) -> {result, [binary()]}. diff --git a/src/mod_muc.erl b/src/mod_muc.erl index 9f39b17fd..046b691fa 100644 --- a/src/mod_muc.erl +++ b/src/mod_muc.erl @@ -34,8 +34,7 @@ -behaviour(gen_mod). %% API --export([start_link/2, - start/2, +-export([start/2, stop/1, room_destroyed/4, store_room/4, @@ -83,8 +82,6 @@ default_room_opts = [] :: list(), room_shaper = none :: shaper:shaper()}). --define(PROCNAME, ejabberd_mod_muc). - -type muc_room_opts() :: [{atom(), any()}]. -callback init(binary(), gen_mod:opts()) -> any(). -callback import(binary(), binary(), [binary()]) -> ok. @@ -109,22 +106,12 @@ %%==================================================================== %% API %%==================================================================== -start_link(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - gen_server:start_link({local, Proc}, ?MODULE, - [Host, Opts], []). - start(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - ChildSpec = {Proc, {?MODULE, start_link, [Host, Opts]}, - transient, 1000, worker, [?MODULE]}, - supervisor:start_child(ejabberd_sup, ChildSpec). + gen_mod:start_child(?MODULE, Host, Opts). stop(Host) -> Rooms = shutdown_rooms(Host), - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - supervisor:terminate_child(ejabberd_sup, Proc), - supervisor:delete_child(ejabberd_sup, Proc), + gen_mod:stop_child(?MODULE, Host), {wait, Rooms}. depends(_Host, _Opts) -> @@ -150,7 +137,7 @@ shutdown_rooms(Host) -> %% In this case, the mod_muc process died before the room processes %% So the message sending must be catched room_destroyed(Host, Room, Pid, ServerHost) -> - catch gen_mod:get_module_proc(ServerHost, ?PROCNAME) ! + catch gen_mod:get_module_proc(ServerHost, ?MODULE) ! {room_destroyed, {Room, Host}, Pid}, ok. @@ -158,7 +145,7 @@ room_destroyed(Host, Room, Pid, ServerHost) -> %% If Opts = default, the default room options are used. %% Else use the passed options as defined in mod_muc_room. create_room(Host, Name, From, Nick, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), + Proc = gen_mod:get_module_proc(Host, ?MODULE), gen_server:call(Proc, {create, Name, From, Nick, Opts}). store_room(ServerHost, Host, Name, Opts) -> diff --git a/src/mod_muc_log.erl b/src/mod_muc_log.erl index fb9d22328..916deef24 100644 --- a/src/mod_muc_log.erl +++ b/src/mod_muc_log.erl @@ -36,7 +36,7 @@ -behaviour(gen_mod). %% API --export([start_link/2, start/2, stop/1, transform_module_options/1, +-export([start/2, stop/1, transform_module_options/1, check_access_log/2, add_to_log/5]). -export([init/1, handle_call/3, handle_cast/2, @@ -50,7 +50,6 @@ -include("mod_muc_room.hrl"). -define(T(Text), translate:translate(Lang, Text)). --define(PROCNAME, ejabberd_mod_muc_log). -record(room, {jid, title, subject, subject_author, config}). -define(PLAINTEXT_CO, <<"ZZCZZ">>). @@ -73,21 +72,11 @@ %%==================================================================== %% API %%==================================================================== -start_link(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - gen_server:start_link({local, Proc}, ?MODULE, [Host, Opts], []). - start(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - ChildSpec = {Proc, {?MODULE, start_link, [Host, Opts]}, - transient, 1000, worker, [?MODULE]}, - supervisor:start_child(ejabberd_sup, ChildSpec). + gen_mod:start_child(?MODULE, Host, Opts). stop(Host) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - supervisor:terminate_child(ejabberd_sup, Proc), - supervisor:delete_child(ejabberd_sup, Proc), - ok. + gen_mod:stop_child(?MODULE, Host). add_to_log(Host, Type, Data, Room, Opts) -> gen_server:cast(get_proc_name(Host), @@ -1185,7 +1174,7 @@ get_room_state(RoomPid) -> R. get_proc_name(Host) -> - gen_mod:get_module_proc(Host, ?PROCNAME). + gen_mod:get_module_proc(Host, ?MODULE). calc_hour_offset(TimeHere) -> TimeZero = calendar:universal_time(), diff --git a/src/mod_multicast.erl b/src/mod_multicast.erl index 7e8e21c31..8948131fd 100644 --- a/src/mod_multicast.erl +++ b/src/mod_multicast.erl @@ -34,7 +34,7 @@ -behaviour(gen_mod). %% API --export([start_link/2, start/2, stop/1]). +-export([start/2, stop/1]). %% gen_server callbacks -export([init/1, handle_info/2, handle_call/3, @@ -97,8 +97,6 @@ -define(VERSION_MULTICAST, <<"$Revision: 440 $ ">>). --define(PROCNAME, ejabberd_mod_multicast). - -define(PURGE_PROCNAME, ejabberd_mod_multicast_purgeloop). @@ -118,23 +116,11 @@ -define(DEFAULT_LIMIT_REMOTE_PRESENCE, 20). -start_link(LServerS, Opts) -> - Proc = gen_mod:get_module_proc(LServerS, ?PROCNAME), - gen_server:start_link({local, Proc}, ?MODULE, - [LServerS, Opts], []). - start(LServerS, Opts) -> - Proc = gen_mod:get_module_proc(LServerS, ?PROCNAME), - ChildSpec = {Proc, - {?MODULE, start_link, [LServerS, Opts]}, temporary, - 1000, worker, [?MODULE]}, - supervisor:start_child(ejabberd_sup, ChildSpec). + gen_mod:start_child(?MODULE, LServerS, Opts). stop(LServerS) -> - Proc = gen_mod:get_module_proc(LServerS, ?PROCNAME), - supervisor:terminate_child(ejabberd_sup, Proc), - supervisor:delete_child(ejabberd_sup, Proc), - ok. + gen_mod:stop_child(?MODULE, LServerS). %%==================================================================== %% gen_server callbacks diff --git a/src/mod_offline.erl b/src/mod_offline.erl index 082386514..9ed9ab91f 100644 --- a/src/mod_offline.erl +++ b/src/mod_offline.erl @@ -33,13 +33,14 @@ -protocol({xep, 160, '1.0'}). -protocol({xep, 334, '0.2'}). --define(GEN_SERVER, p1_server). +-ifndef(GEN_SERVER). +-define(GEN_SERVER, gen_server). +-endif. -behaviour(?GEN_SERVER). -behaviour(gen_mod). -export([start/2, - start_link/2, stop/1, store_packet/4, store_offline_msg/5, @@ -83,8 +84,6 @@ -include("mod_offline.hrl"). --define(PROCNAME, ejabberd_offline). - -define(OFFLINE_TABLE_LOCK_THRESHOLD, 1000). %% default value for the maximum number of user messages @@ -112,22 +111,11 @@ -callback remove_all_messages(binary(), binary()) -> {atomic, any()}. -callback count_messages(binary(), binary()) -> non_neg_integer(). -start_link(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - ?GEN_SERVER:start_link({local, Proc}, ?MODULE, - [Host, Opts], []). - start(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - ChildSpec = {Proc, {?MODULE, start_link, [Host, Opts]}, - transient, 1000, worker, [?MODULE]}, - supervisor:start_child(ejabberd_sup, ChildSpec). + gen_mod:start_child(?MODULE, Host, Opts). stop(Host) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - supervisor:terminate_child(ejabberd_sup, Proc), - supervisor:delete_child(ejabberd_sup, Proc), - ok. + gen_mod:stop_child(?MODULE, Host). depends(_Host, _Opts) -> []. @@ -478,7 +466,7 @@ store_packet(Acc, From, To, Packet) -> NewPacket -> TimeStamp = p1_time_compat:timestamp(), Expire = find_x_expire(TimeStamp, NewPacket), - gen_mod:get_module_proc(To#jid.lserver, ?PROCNAME) ! + gen_mod:get_module_proc(To#jid.lserver, ?MODULE) ! #offline_msg{us = {LUser, LServer}, timestamp = TimeStamp, expire = Expire, diff --git a/src/mod_ping.erl b/src/mod_ping.erl index 1c1d24850..fd88c12dc 100644 --- a/src/mod_ping.erl +++ b/src/mod_ping.erl @@ -45,7 +45,7 @@ -define(DEFAULT_PING_INTERVAL, 60). %% API --export([start_link/2, start_ping/2, stop_ping/2]). +-export([start_ping/2, stop_ping/2]). %% gen_mod callbacks -export([start/2, stop/1]). @@ -68,11 +68,6 @@ %%==================================================================== %% API %%==================================================================== -start_link(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?MODULE), - gen_server:start_link({local, Proc}, ?MODULE, - [Host, Opts], []). - -spec start_ping(binary(), jid()) -> ok. start_ping(Host, JID) -> Proc = gen_mod:get_module_proc(Host, ?MODULE), @@ -87,16 +82,10 @@ stop_ping(Host, JID) -> %% gen_mod callbacks %%==================================================================== start(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?MODULE), - PingSpec = {Proc, {?MODULE, start_link, [Host, Opts]}, - transient, 2000, worker, [?MODULE]}, - supervisor:start_child(?SUPERVISOR, PingSpec). + gen_mod:start_child(?MODULE, Host, Opts). stop(Host) -> - Proc = gen_mod:get_module_proc(Host, ?MODULE), - supervisor:terminate_child(ejabberd_sup, Proc), - supervisor:delete_child(?SUPERVISOR, Proc), - ok. + gen_mod:stop_child(?MODULE, Host). %%==================================================================== %% gen_server callbacks diff --git a/src/mod_privilege.erl b/src/mod_privilege.erl index 3783470b2..c65119d4c 100644 --- a/src/mod_privilege.erl +++ b/src/mod_privilege.erl @@ -31,7 +31,6 @@ -behaviour(gen_mod). %% API --export([start_link/2]). -export([start/2, stop/1, mod_opt_type/1, depends/2]). %% gen_server callbacks -export([init/1, handle_call/3, handle_cast/2, handle_info/2, @@ -50,21 +49,11 @@ %%%=================================================================== %%% API %%%=================================================================== -start_link(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?MODULE), - gen_server:start_link({local, Proc}, ?MODULE, [Host, Opts], []). - start(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?MODULE), - PingSpec = {Proc, {?MODULE, start_link, [Host, Opts]}, - transient, 2000, worker, [?MODULE]}, - supervisor:start_child(ejabberd_sup, PingSpec). + gen_mod:start_child(?MODULE, Host, Opts). stop(Host) -> - Proc = gen_mod:get_module_proc(Host, ?MODULE), - supervisor:terminate_child(ejabberd_sup, Proc), - supervisor:delete_child(ejabberd_sup, Proc), - ok. + gen_mod:stop_child(?MODULE, Host). mod_opt_type(roster) -> v_roster(); mod_opt_type(message) -> v_message(); diff --git a/src/mod_pubsub.erl b/src/mod_pubsub.erl index 8acfdb7ce..90d2a758f 100644 --- a/src/mod_pubsub.erl +++ b/src/mod_pubsub.erl @@ -86,13 +86,12 @@ err_unsupported_access_model/0]). %% API and gen_server callbacks --export([start_link/2, start/2, stop/1, init/1, +-export([start/2, stop/1, init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3, depends/2]). -export([send_loop/1, mod_opt_type/1]). --define(PROCNAME, ejabberd_mod_pubsub). -define(LOOPNAME, ejabberd_mod_pubsub_loop). %%==================================================================== @@ -219,21 +218,11 @@ ). -start_link(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - gen_server:start_link({local, Proc}, ?MODULE, [Host, Opts], []). - start(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - ChildSpec = {Proc, {?MODULE, start_link, [Host, Opts]}, - transient, 1000, worker, [?MODULE]}, - supervisor:start_child(ejabberd_sup, ChildSpec). + gen_mod:start_child(?MODULE, Host, Opts). stop(Host) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - supervisor:terminate_child(ejabberd_sup, Proc), - supervisor:delete_child(ejabberd_sup, Proc), - ok. + gen_mod:stop_child(?MODULE, Host). %%==================================================================== %% gen_server callbacks diff --git a/src/mod_shared_roster_ldap.erl b/src/mod_shared_roster_ldap.erl index feb958105..47ba9f08c 100644 --- a/src/mod_shared_roster_ldap.erl +++ b/src/mod_shared_roster_ldap.erl @@ -33,7 +33,7 @@ -behaviour(gen_mod). %% API --export([start_link/2, start/2, stop/1]). +-export([start/2, stop/1]). %% gen_server callbacks -export([init/1, handle_call/3, handle_cast/2, @@ -90,22 +90,11 @@ %%==================================================================== %% API %%==================================================================== -start_link(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?MODULE), - gen_server:start_link({local, Proc}, ?MODULE, - [Host, Opts], []). - start(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?MODULE), - ChildSpec = {Proc, {?MODULE, start_link, [Host, Opts]}, - permanent, 1000, worker, [?MODULE]}, - supervisor:start_child(ejabberd_sup, ChildSpec). + gen_mod:start_child(?MODULE, Host, Opts). stop(Host) -> - Proc = gen_mod:get_module_proc(Host, ?MODULE), - supervisor:terminate_child(ejabberd_sup, Proc), - supervisor:delete_child(ejabberd_sup, Proc), - ok. + gen_mod:stop_child(?MODULE, Host). depends(_Host, _Opts) -> [{mod_roster, hard}]. diff --git a/src/mod_sip_registrar.erl b/src/mod_sip_registrar.erl index a6535c0f1..1cc4dc3a1 100644 --- a/src/mod_sip_registrar.erl +++ b/src/mod_sip_registrar.erl @@ -24,7 +24,9 @@ %%%------------------------------------------------------------------- -module(mod_sip_registrar). --define(GEN_SERVER, p1_server). +-ifndef(GEN_SERVER). +-define(GEN_SERVER, gen_server). +-endif. -behaviour(?GEN_SERVER). %% API diff --git a/src/mod_vcard.erl b/src/mod_vcard.erl index f05de693f..72dc4a2b9 100644 --- a/src/mod_vcard.erl +++ b/src/mod_vcard.erl @@ -39,7 +39,7 @@ depends/2, process_search/1, process_vcard/1, get_vcard/2, disco_items/5, disco_features/5, disco_identity/5, decode_iq_subel/1, mod_opt_type/1, set_vcard/3, make_vcard_search/4]). --export([start_link/2, init/1, handle_call/3, handle_cast/2, +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]). -include("ejabberd.hrl"). @@ -49,8 +49,6 @@ -define(JUD_MATCHES, 30). --define(PROCNAME, ejabberd_mod_vcard). - -callback init(binary(), gen_mod:opts()) -> any(). -callback stop(binary()) -> any(). -callback import(binary(), binary(), [binary()]) -> ok. @@ -66,27 +64,14 @@ -record(state, {host :: binary(), server_host :: binary()}). -%%==================================================================== -%% API -%%==================================================================== -start_link(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - gen_server:start_link({local, Proc}, ?MODULE, [Host, Opts], []). - %%==================================================================== %% gen_mod callbacks %%==================================================================== start(Host, Opts) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - Spec = {Proc, {?MODULE, start_link, [Host, Opts]}, - transient, 2000, worker, [?MODULE]}, - supervisor:start_child(ejabberd_sup, Spec). + gen_mod:start_child(?MODULE, Host, Opts). stop(Host) -> - Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - supervisor:terminate_child(ejabberd_sup, Proc), - supervisor:delete_child(ejabberd_sup, Proc), - ok. + gen_mod:stop_child(?MODULE, Host). %%==================================================================== %% gen_server callbacks