From 0124d292b5d0a9e0d38bc54ba751b14271dc2c8b Mon Sep 17 00:00:00 2001 From: Evgeniy Khramtsov Date: Fri, 24 Feb 2017 16:31:39 +0300 Subject: [PATCH] Add gen_mod:is_loaded_elsewhere/2 --- src/gen_mod.erl | 11 ++++++++++- src/mod_fail2ban.erl | 10 +--------- src/mod_proxy65.erl | 7 ++++++- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/gen_mod.erl b/src/gen_mod.erl index 63bfa314b..ef000635d 100644 --- a/src/gen_mod.erl +++ b/src/gen_mod.erl @@ -37,7 +37,7 @@ get_opt/4, get_opt_host/3, opt_type/1, is_equal_opt/5, get_module_opt/4, get_module_opt/5, get_module_opt_host/3, loaded_modules/1, loaded_modules_with_opts/1, - get_hosts/2, get_module_proc/2, is_loaded/2, + get_hosts/2, get_module_proc/2, is_loaded/2, is_loaded_elsewhere/2, start_modules/0, start_modules/1, stop_modules/0, stop_modules/1, db_mod/2, db_mod/3, ram_db_mod/2, ram_db_mod/3, db_type/2, db_type/3, ram_db_type/2, ram_db_type/3]). @@ -622,6 +622,15 @@ get_module_proc(Host, Base) -> is_loaded(Host, Module) -> ets:member(ejabberd_modules, {Module, Host}). +-spec is_loaded_elsewhere(binary(), atom()) -> boolean(). +is_loaded_elsewhere(Host, Module) -> + ets:select_count( + ejabberd_modules, + ets:fun2ms( + fun(#ejabberd_module{module_host = {Mod, H}}) -> + (Mod == Module) and (H /= Host) + end)) /= 0. + -spec config_reloaded() -> ok. config_reloaded() -> lists:foreach( diff --git a/src/mod_fail2ban.erl b/src/mod_fail2ban.erl index b6f889fc9..4d5e5fdb4 100644 --- a/src/mod_fail2ban.erl +++ b/src/mod_fail2ban.erl @@ -150,7 +150,7 @@ handle_info(_Info, State) -> terminate(_Reason, #state{host = Host}) -> ejabberd_hooks:delete(c2s_auth_result, Host, ?MODULE, c2s_auth_result, 100), ejabberd_hooks:delete(c2s_stream_started, Host, ?MODULE, c2s_stream_started, 100), - case is_loaded_at_other_hosts(Host) of + case gen_mod:is_loaded_elsewhere(Host, ?MODULE) of true -> ok; false -> @@ -184,14 +184,6 @@ is_whitelisted(Host, Addr) -> none), acl:match_rule(Host, Access, Addr) == allow. -is_loaded_at_other_hosts(Host) -> - lists:any( - fun(VHost) when VHost == Host -> - false; - (VHost) -> - gen_mod:is_loaded(VHost, ?MODULE) - end, ?MYHOSTS). - seconds_to_now(Secs) -> {Secs div 1000000, Secs rem 1000000, 0}. diff --git a/src/mod_proxy65.erl b/src/mod_proxy65.erl index d7793115e..53f708340 100644 --- a/src/mod_proxy65.erl +++ b/src/mod_proxy65.erl @@ -63,7 +63,12 @@ start(Host, Opts) -> end. stop(Host) -> - mod_proxy65_service:delete_listener(Host), + case gen_mod:is_loaded_elsewhere(Host, ?MODULE) of + false -> + mod_proxy65_service:delete_listener(Host); + true -> + ok + end, Proc = gen_mod:get_module_proc(Host, ?PROCNAME), supervisor:terminate_child(ejabberd_gen_mod_sup, Proc), supervisor:delete_child(ejabberd_gen_mod_sup, Proc). -- 2.40.0