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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2020-01-28 13:34:02 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2020 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).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-02-01 21:21:28 +01:00
|
|
|
|
|
|
|
-behaviour(application).
|
|
|
|
|
2017-07-06 14:47:00 +02:00
|
|
|
-export([start/2, prep_stop/1, stop/1]).
|
2003-02-01 21:21:28 +01:00
|
|
|
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2019-06-14 11:33:26 +02:00
|
|
|
-include("ejabberd_stacktrace.hrl").
|
2008-02-28 01:30:23 +01:00
|
|
|
|
|
|
|
%%%
|
|
|
|
%%% Application API
|
|
|
|
%%%
|
|
|
|
|
2003-10-18 21:15:12 +02:00
|
|
|
start(normal, _Args) ->
|
2019-06-14 11:33:26 +02:00
|
|
|
try
|
|
|
|
{T1, _} = statistics(wall_clock),
|
|
|
|
ejabberd_logger:start(),
|
|
|
|
write_pid_file(),
|
|
|
|
start_included_apps(),
|
|
|
|
start_elixir_application(),
|
|
|
|
setup_if_elixir_conf_used(),
|
|
|
|
case ejabberd_config:load() of
|
|
|
|
ok ->
|
|
|
|
ejabberd_mnesia:start(),
|
|
|
|
file_queue_init(),
|
|
|
|
maybe_add_nameservers(),
|
|
|
|
case ejabberd_sup:start_link() of
|
|
|
|
{ok, SupPid} ->
|
|
|
|
ejabberd_system_monitor:start(),
|
|
|
|
register_elixir_config_hooks(),
|
|
|
|
ejabberd_cluster:wait_for_sync(infinity),
|
|
|
|
ejabberd_hooks:run(ejabberd_started, []),
|
|
|
|
ejabberd:check_apps(),
|
|
|
|
{T2, _} = statistics(wall_clock),
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("ejabberd ~ts is started in the node ~p in ~.2fs",
|
2019-06-14 11:33:26 +02:00
|
|
|
[ejabberd_option:version(),
|
|
|
|
node(), (T2-T1)/1000]),
|
|
|
|
{ok, SupPid};
|
|
|
|
Err ->
|
|
|
|
?CRITICAL_MSG("Failed to start ejabberd application: ~p", [Err]),
|
|
|
|
ejabberd:halt()
|
|
|
|
end;
|
|
|
|
Err ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?CRITICAL_MSG("Failed to start ejabberd application: ~ts",
|
2019-06-14 11:33:26 +02:00
|
|
|
[ejabberd_config:format_error(Err)]),
|
|
|
|
ejabberd:halt()
|
|
|
|
end
|
|
|
|
catch throw:{?MODULE, Error} ->
|
|
|
|
?DEBUG("Failed to start ejabberd application: ~p", [Error]),
|
2018-03-29 11:14:31 +02:00
|
|
|
ejabberd:halt()
|
2017-02-26 13:10:59 +01:00
|
|
|
end;
|
2003-02-01 21:21:28 +01:00
|
|
|
start(_, _) ->
|
|
|
|
{error, badarg}.
|
|
|
|
|
2019-01-16 15:01:32 +01:00
|
|
|
start_included_apps() ->
|
|
|
|
{ok, Apps} = application:get_key(ejabberd, included_applications),
|
|
|
|
lists:foreach(
|
|
|
|
fun(mnesia) ->
|
|
|
|
ok;
|
2019-10-25 10:44:04 +02:00
|
|
|
(lager) ->
|
|
|
|
ok;
|
2019-01-16 15:40:10 +01:00
|
|
|
(os_mon)->
|
|
|
|
ok;
|
2019-01-16 15:01:32 +01:00
|
|
|
(App) ->
|
|
|
|
application:ensure_all_started(App)
|
|
|
|
end, Apps).
|
|
|
|
|
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) ->
|
2018-11-19 16:11:33 +01:00
|
|
|
ejabberd_hooks:run(ejabberd_stopping, []),
|
2019-07-29 09:46:20 +02:00
|
|
|
ejabberd_listener:stop(),
|
2019-07-26 10:40:19 +02:00
|
|
|
ejabberd_sm:stop(),
|
|
|
|
ejabberd_service:stop(),
|
|
|
|
ejabberd_s2s:stop(),
|
2019-07-29 09:46:20 +02:00
|
|
|
gen_mod:stop(),
|
2008-02-28 01:30:23 +01:00
|
|
|
State.
|
|
|
|
|
|
|
|
%% All the processes were killed when this function is called
|
|
|
|
stop(_State) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?INFO_MSG("ejabberd ~ts is stopped in the node ~p",
|
2019-06-14 11:33:26 +02:00
|
|
|
[ejabberd_option:version(), node()]),
|
|
|
|
delete_pid_file().
|
2008-02-28 01:30:23 +01:00
|
|
|
|
|
|
|
%%%
|
|
|
|
%%% Internal functions
|
|
|
|
%%%
|
|
|
|
|
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
|
|
|
|
|
|
|
%%%
|
|
|
|
%%% 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) ->
|
2019-09-23 14:17:20 +02:00
|
|
|
case file:write_file(PidFilename, io_lib:format("~ts~n", [Pid])) of
|
2019-06-14 11:33:26 +02:00
|
|
|
ok ->
|
|
|
|
ok;
|
|
|
|
{error, Reason} = Err ->
|
2019-09-23 14:17:20 +02:00
|
|
|
?CRITICAL_MSG("Cannot write PID file ~ts: ~ts",
|
2019-06-14 11:33:26 +02:00
|
|
|
[PidFilename, file:format_error(Reason)]),
|
|
|
|
throw({?MODULE, Err})
|
2009-08-24 23:21:04 +02:00
|
|
|
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
|
|
|
|
2017-03-10 13:12:43 +01:00
|
|
|
file_queue_init() ->
|
2019-06-14 11:33:26 +02:00
|
|
|
QueueDir = case ejabberd_option:queue_dir() of
|
2017-03-10 13:12:43 +01:00
|
|
|
undefined ->
|
2017-04-03 11:57:23 +02:00
|
|
|
MnesiaDir = mnesia:system_info(directory),
|
2017-03-10 13:12:43 +01:00
|
|
|
filename:join(MnesiaDir, "queue");
|
|
|
|
Path ->
|
|
|
|
Path
|
|
|
|
end,
|
2019-06-14 11:33:26 +02:00
|
|
|
case p1_queue:start(QueueDir) of
|
|
|
|
ok -> ok;
|
|
|
|
Err -> throw({?MODULE, Err})
|
|
|
|
end.
|
|
|
|
|
|
|
|
-ifdef(ELIXIR_ENABLED).
|
|
|
|
is_using_elixir_config() ->
|
|
|
|
Config = ejabberd_config:path(),
|
|
|
|
'Elixir.Ejabberd.ConfigUtil':is_elixir_config(Config).
|
2017-03-10 13:12:43 +01:00
|
|
|
|
2016-09-08 11:34:42 +02:00
|
|
|
setup_if_elixir_conf_used() ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case is_using_elixir_config() of
|
2016-09-08 11:34:42 +02:00
|
|
|
true -> 'Elixir.Ejabberd.Config.Store':start_link();
|
|
|
|
false -> ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
register_elixir_config_hooks() ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case is_using_elixir_config() of
|
2016-09-08 11:34:42 +02:00
|
|
|
true -> 'Elixir.Ejabberd.Config':start_hooks();
|
|
|
|
false -> ok
|
|
|
|
end.
|
|
|
|
|
2016-07-28 15:57:35 +02:00
|
|
|
start_elixir_application() ->
|
2019-06-14 11:33:26 +02:00
|
|
|
case application:ensure_started(elixir) of
|
|
|
|
ok -> ok;
|
|
|
|
{error, _Msg} -> ?ERROR_MSG("Elixir application not started.", [])
|
2016-09-08 16:29:19 +02:00
|
|
|
end.
|
2019-06-14 11:33:26 +02:00
|
|
|
-else.
|
|
|
|
setup_if_elixir_conf_used() -> ok.
|
|
|
|
register_elixir_config_hooks() -> ok.
|
|
|
|
start_elixir_application() -> ok.
|
|
|
|
-endif.
|