2003-02-01 21:21:28 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_app.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2008-02-28 01:30:23 +01:00
|
|
|
%%% Purpose : ejabberd's application callback module
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 31 Jan 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2015-01-08 17:34:43 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2015 ProcessOne
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
2003-02-01 21:21:28 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_app).
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-02-01 21:21:28 +01:00
|
|
|
|
|
|
|
-behaviour(application).
|
|
|
|
|
2013-06-27 10:58:56 +02:00
|
|
|
-export([start_modules/0,start/2, prep_stop/1, stop/1, init/0]).
|
2003-02-01 21:21:28 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2008-02-28 01:30:23 +01:00
|
|
|
|
|
|
|
%%%
|
|
|
|
%%% Application API
|
|
|
|
%%%
|
|
|
|
|
2003-10-18 21:15:12 +02:00
|
|
|
start(normal, _Args) ->
|
2013-06-27 10:58:56 +02:00
|
|
|
ejabberd_logger:start(),
|
2009-08-24 23:21:04 +02:00
|
|
|
write_pid_file(),
|
2013-04-08 11:12:54 +02:00
|
|
|
start_apps(),
|
2013-07-06 18:11:01 +02:00
|
|
|
ejabberd:check_app(ejabberd),
|
2003-02-01 21:21:28 +01:00
|
|
|
randoms:start(),
|
|
|
|
db_init(),
|
2008-10-25 00:07:38 +02:00
|
|
|
start(),
|
2003-02-01 21:21:28 +01:00
|
|
|
translate:start(),
|
2006-02-20 05:07:42 +01:00
|
|
|
ejabberd_ctl:init(),
|
2008-10-12 13:56:07 +02:00
|
|
|
ejabberd_commands:init(),
|
|
|
|
ejabberd_admin:start(),
|
2003-02-01 21:21:28 +01:00
|
|
|
gen_mod:start(),
|
2015-03-11 14:14:28 +01:00
|
|
|
ext_mod:start(),
|
2003-02-01 21:21:28 +01:00
|
|
|
ejabberd_config:start(),
|
2013-08-12 14:25:05 +02:00
|
|
|
set_loglevel_from_config(),
|
|
|
|
acl:start(),
|
|
|
|
shaper:start(),
|
2006-11-07 03:08:51 +01:00
|
|
|
connect_nodes(),
|
2005-07-13 05:24:13 +02:00
|
|
|
Sup = ejabberd_sup:start_link(),
|
2006-10-25 09:56:49 +02:00
|
|
|
ejabberd_rdbms:start(),
|
2012-11-06 16:58:08 +01:00
|
|
|
ejabberd_riak_sup:start(),
|
2015-03-09 14:41:13 +01:00
|
|
|
ejabberd_sm:start(),
|
2003-02-01 21:21:28 +01:00
|
|
|
ejabberd_auth:start(),
|
2003-03-09 21:46:47 +01:00
|
|
|
cyrsasl:start(),
|
2003-07-20 22:35:35 +02:00
|
|
|
% Profiling
|
2009-05-08 00:46:51 +02:00
|
|
|
%ejabberd_debug:eprof_start(),
|
2009-05-11 18:48:56 +02:00
|
|
|
%ejabberd_debug:fprof_start(),
|
2009-03-05 21:03:18 +01:00
|
|
|
maybe_add_nameservers(),
|
2008-02-28 01:30:23 +01:00
|
|
|
start_modules(),
|
2008-10-12 13:11:29 +02:00
|
|
|
ejabberd_listener:start_listeners(),
|
2008-10-25 00:07:38 +02:00
|
|
|
?INFO_MSG("ejabberd ~s is started in the node ~p", [?VERSION, node()]),
|
2003-02-01 21:21:28 +01:00
|
|
|
Sup;
|
|
|
|
start(_, _) ->
|
|
|
|
{error, badarg}.
|
|
|
|
|
2008-02-28 01:30:23 +01:00
|
|
|
%% Prepare the application for termination.
|
2008-10-25 00:07:38 +02:00
|
|
|
%% This function is called when an application is about to be stopped,
|
2008-02-28 01:30:23 +01:00
|
|
|
%% before shutting down the processes of the application.
|
|
|
|
prep_stop(State) ->
|
2012-07-25 13:02:21 +02:00
|
|
|
ejabberd_listener:stop_listeners(),
|
2008-02-28 01:30:23 +01:00
|
|
|
stop_modules(),
|
2008-10-12 13:56:07 +02:00
|
|
|
ejabberd_admin:stop(),
|
2010-01-03 01:35:51 +01:00
|
|
|
broadcast_c2s_shutdown(),
|
|
|
|
timer:sleep(5000),
|
2008-02-28 01:30:23 +01:00
|
|
|
State.
|
|
|
|
|
|
|
|
%% All the processes were killed when this function is called
|
|
|
|
stop(_State) ->
|
2008-10-25 00:07:38 +02:00
|
|
|
?INFO_MSG("ejabberd ~s is stopped in the node ~p", [?VERSION, node()]),
|
2009-08-24 23:21:04 +02:00
|
|
|
delete_pid_file(),
|
2010-05-28 00:48:04 +02:00
|
|
|
%%ejabberd_debug:stop(),
|
2003-02-01 21:21:28 +01:00
|
|
|
ok.
|
|
|
|
|
2008-02-28 01:30:23 +01:00
|
|
|
|
|
|
|
%%%
|
|
|
|
%%% Internal functions
|
|
|
|
%%%
|
|
|
|
|
2003-02-01 21:21:28 +01:00
|
|
|
start() ->
|
2003-07-20 22:35:35 +02:00
|
|
|
spawn_link(?MODULE, init, []).
|
2003-02-01 21:21:28 +01:00
|
|
|
|
|
|
|
init() ->
|
|
|
|
register(ejabberd, self()),
|
2013-04-08 11:12:54 +02:00
|
|
|
loop().
|
2003-02-01 21:21:28 +01:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
loop() ->
|
2003-02-01 21:21:28 +01:00
|
|
|
receive
|
|
|
|
_ ->
|
2013-04-08 11:12:54 +02:00
|
|
|
loop()
|
2003-02-01 21:21:28 +01:00
|
|
|
end.
|
|
|
|
|
|
|
|
db_init() ->
|
2014-06-12 11:00:22 +02:00
|
|
|
MyNode = node(),
|
|
|
|
DbNodes = mnesia:system_info(db_nodes),
|
|
|
|
case lists:member(MyNode, DbNodes) of
|
|
|
|
true ->
|
|
|
|
ok;
|
|
|
|
false ->
|
|
|
|
?CRITICAL_MSG("Node name mismatch: I'm [~s], "
|
|
|
|
"the database is owned by ~p", [MyNode, DbNodes]),
|
|
|
|
?CRITICAL_MSG("Either set ERLANG_NODE in ejabberdctl.cfg "
|
|
|
|
"or change node name in Mnesia", []),
|
|
|
|
erlang:error(node_name_mismatch)
|
|
|
|
end,
|
2003-02-01 21:21:28 +01:00
|
|
|
case mnesia:system_info(extra_db_nodes) of
|
|
|
|
[] ->
|
|
|
|
mnesia:create_schema([node()]);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
2013-07-06 18:11:01 +02:00
|
|
|
ejabberd:start_app(mnesia, permanent),
|
2003-02-01 21:21:28 +01:00
|
|
|
mnesia:wait_for_tables(mnesia:system_info(local_tables), infinity).
|
|
|
|
|
2008-02-28 01:30:23 +01:00
|
|
|
%% Start all the modules in all the hosts
|
|
|
|
start_modules() ->
|
2005-06-20 05:18:13 +02:00
|
|
|
lists:foreach(
|
|
|
|
fun(Host) ->
|
2013-08-12 14:25:05 +02:00
|
|
|
Modules = ejabberd_config:get_option(
|
2013-03-14 10:33:02 +01:00
|
|
|
{modules, Host},
|
|
|
|
fun(Mods) ->
|
|
|
|
lists:map(
|
|
|
|
fun({M, A}) when is_atom(M), is_list(A) ->
|
|
|
|
{M, A}
|
|
|
|
end, Mods)
|
|
|
|
end, []),
|
|
|
|
lists:foreach(
|
|
|
|
fun({Module, Args}) ->
|
|
|
|
gen_mod:start_module(Host, Module, Args)
|
|
|
|
end, Modules)
|
2005-06-20 05:18:13 +02:00
|
|
|
end, ?MYHOSTS).
|
2003-02-01 21:21:28 +01:00
|
|
|
|
2008-02-28 01:30:23 +01:00
|
|
|
%% Stop all the modules in all the hosts
|
|
|
|
stop_modules() ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(Host) ->
|
2013-08-12 14:25:05 +02:00
|
|
|
Modules = ejabberd_config:get_option(
|
2013-03-14 10:33:02 +01:00
|
|
|
{modules, Host},
|
|
|
|
fun(Mods) ->
|
|
|
|
lists:map(
|
|
|
|
fun({M, A}) when is_atom(M), is_list(A) ->
|
|
|
|
{M, A}
|
|
|
|
end, Mods)
|
|
|
|
end, []),
|
|
|
|
lists:foreach(
|
|
|
|
fun({Module, _Args}) ->
|
|
|
|
gen_mod:stop_module_keep_config(Host, Module)
|
|
|
|
end, Modules)
|
2008-02-28 01:30:23 +01:00
|
|
|
end, ?MYHOSTS).
|
|
|
|
|
2006-11-07 03:08:51 +01:00
|
|
|
connect_nodes() ->
|
2013-08-12 14:25:05 +02:00
|
|
|
Nodes = ejabberd_config:get_option(
|
2013-03-14 10:33:02 +01:00
|
|
|
cluster_nodes,
|
|
|
|
fun(Ns) ->
|
|
|
|
true = lists:all(fun is_atom/1, Ns),
|
|
|
|
Ns
|
|
|
|
end, []),
|
|
|
|
lists:foreach(fun(Node) ->
|
|
|
|
net_kernel:connect_node(Node)
|
|
|
|
end, Nodes).
|
2006-11-07 03:08:51 +01:00
|
|
|
|
2009-03-05 21:03:18 +01:00
|
|
|
%% If ejabberd is running on some Windows machine, get nameservers and add to Erlang
|
|
|
|
maybe_add_nameservers() ->
|
|
|
|
case os:type() of
|
|
|
|
{win32, _} -> add_windows_nameservers();
|
|
|
|
_ -> ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
add_windows_nameservers() ->
|
2009-03-11 19:36:27 +01:00
|
|
|
IPTs = win32_dns:get_nameservers(),
|
2009-03-05 21:03:18 +01:00
|
|
|
?INFO_MSG("Adding machine's DNS IPs to Erlang system:~n~p", [IPTs]),
|
|
|
|
lists:foreach(fun(IPT) -> inet_db:add_ns(IPT) end, IPTs).
|
2009-08-24 23:21:04 +02:00
|
|
|
|
|
|
|
|
2010-01-03 01:35:51 +01:00
|
|
|
broadcast_c2s_shutdown() ->
|
2014-05-08 19:28:12 +02:00
|
|
|
Children = ejabberd_sm:get_all_pids(),
|
2010-01-03 01:35:51 +01:00
|
|
|
lists:foreach(
|
2014-05-08 19:28:12 +02:00
|
|
|
fun(C2SPid) when node(C2SPid) == node() ->
|
|
|
|
C2SPid ! system_shutdown;
|
|
|
|
(_) ->
|
|
|
|
ok
|
2010-01-03 01:35:51 +01:00
|
|
|
end, Children).
|
|
|
|
|
2009-08-24 23:21:04 +02:00
|
|
|
%%%
|
|
|
|
%%% PID file
|
|
|
|
%%%
|
|
|
|
|
|
|
|
write_pid_file() ->
|
|
|
|
case ejabberd:get_pid_file() of
|
|
|
|
false ->
|
|
|
|
ok;
|
|
|
|
PidFilename ->
|
|
|
|
write_pid_file(os:getpid(), PidFilename)
|
|
|
|
end.
|
|
|
|
|
|
|
|
write_pid_file(Pid, PidFilename) ->
|
|
|
|
case file:open(PidFilename, [write]) of
|
|
|
|
{ok, Fd} ->
|
|
|
|
io:format(Fd, "~s~n", [Pid]),
|
|
|
|
file:close(Fd);
|
|
|
|
{error, Reason} ->
|
|
|
|
?ERROR_MSG("Cannot write PID file ~s~nReason: ~p", [PidFilename, Reason]),
|
|
|
|
throw({cannot_write_pid_file, PidFilename, Reason})
|
|
|
|
end.
|
|
|
|
|
|
|
|
delete_pid_file() ->
|
|
|
|
case ejabberd:get_pid_file() of
|
|
|
|
false ->
|
|
|
|
ok;
|
|
|
|
PidFilename ->
|
|
|
|
file:delete(PidFilename)
|
|
|
|
end.
|
2013-04-08 11:12:54 +02:00
|
|
|
|
2013-08-12 14:25:05 +02:00
|
|
|
set_loglevel_from_config() ->
|
|
|
|
Level = ejabberd_config:get_option(
|
|
|
|
loglevel,
|
|
|
|
fun(P) when P>=0, P=<5 -> P end,
|
|
|
|
4),
|
|
|
|
ejabberd_logger:set(Level).
|
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
start_apps() ->
|
|
|
|
ejabberd:start_app(sasl),
|
|
|
|
ejabberd:start_app(ssl),
|
2013-08-12 14:25:05 +02:00
|
|
|
ejabberd:start_app(p1_yaml),
|
2013-04-08 11:12:54 +02:00
|
|
|
ejabberd:start_app(p1_tls),
|
|
|
|
ejabberd:start_app(p1_xml),
|
|
|
|
ejabberd:start_app(p1_stringprep),
|
|
|
|
ejabberd:start_app(p1_zlib),
|
|
|
|
ejabberd:start_app(p1_cache_tab).
|