2002-11-18 21:39:47 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%% Purpose : ejabberd wrapper: start / stop
|
|
|
|
%%% Created : 16 Nov 2002 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2024-01-22 16:40:01 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2024 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
|
|
|
%%%
|
2002-11-18 21:39:47 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd).
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2018-03-29 11:14:31 +02:00
|
|
|
-compile({no_auto_import, [{halt, 0}]}).
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2022-11-04 11:34:45 +01:00
|
|
|
-protocol({rfc, 6122}).
|
|
|
|
-protocol({rfc, 7590}).
|
2024-11-08 19:27:22 +01:00
|
|
|
-protocol({xep, 4, '2.9', '0.5.0', "complete", ""}).
|
|
|
|
-protocol({xep, 59, '1.0', '2.1.0', "complete", ""}).
|
|
|
|
-protocol({xep, 82, '1.1.1', '2.1.0', "complete", ""}).
|
|
|
|
-protocol({xep, 86, '1.0', '0.5.0', "complete", ""}).
|
|
|
|
-protocol({xep, 106, '1.1', '0.5.0', "complete", ""}).
|
|
|
|
-protocol({xep, 170, '1.0', '17.12', "complete", ""}).
|
|
|
|
-protocol({xep, 178, '1.1', '17.03', "complete", ""}).
|
|
|
|
-protocol({xep, 205, '1.0', '1.1.2', "complete", ""}).
|
|
|
|
-protocol({xep, 368, '1.1.0', '17.09', "complete", ""}).
|
|
|
|
-protocol({xep, 386, '0.3.0', '24.02', "complete", ""}).
|
|
|
|
-protocol({xep, 388, '0.4.0', '24.02', "complete", ""}).
|
|
|
|
-protocol({xep, 424, '0.4.0', '24.02', "complete", ""}).
|
|
|
|
-protocol({xep, 440, '0.4.0', '24.02', "complete", ""}).
|
|
|
|
-protocol({xep, 474, '0.3.0', '24.02', "complete", ""}).
|
|
|
|
-protocol({xep, 485, '0.2.0', '24.02', "complete", "mod_pubsub_serverinfo in ejabberd-contrib.git"}).
|
2015-06-22 13:11:11 +02:00
|
|
|
|
2018-03-29 11:14:31 +02:00
|
|
|
-export([start/0, stop/0, halt/0, start_app/1, start_app/2,
|
2019-06-14 11:33:26 +02:00
|
|
|
get_pid_file/0, check_apps/0, module_name/1, is_loaded/0]).
|
2013-04-08 11:12:54 +02:00
|
|
|
|
|
|
|
-include("logger.hrl").
|
2003-01-14 18:38:03 +01:00
|
|
|
|
2002-11-18 21:39:47 +01:00
|
|
|
start() ->
|
2024-07-05 09:57:07 +02:00
|
|
|
case application:ensure_all_started(ejabberd) of
|
2024-07-05 10:25:59 +02:00
|
|
|
{error, Err} -> error_logger:error_msg("Failed to start ejabberd application: ~p", [Err]);
|
2024-07-05 09:57:07 +02:00
|
|
|
Ok -> Ok
|
|
|
|
end.
|
2002-11-20 21:19:20 +01:00
|
|
|
|
2003-02-01 21:21:28 +01:00
|
|
|
stop() ->
|
|
|
|
application:stop(ejabberd).
|
2002-11-23 21:55:05 +01:00
|
|
|
|
2018-03-29 11:14:31 +02:00
|
|
|
halt() ->
|
Replace lager with built-in new logging API
This change requires Erlang/OTP-21.0 or higher.
The commit also deprecates the following options:
- log_rotate_date
- log_rate_limit
Furthermore, these options have no effect. The logger now fully
relies on log_rotate_size, that cannot be 0 anymore.
The loglevel option now accepts levels in literal formats.
Those are: none, emergency, alert, critical, error, warning, notice, info, debug.
Old integer values (0-5) are still supported and automatically converted
into literal format.
2019-10-18 18:12:32 +02:00
|
|
|
ejabberd_logger:flush(),
|
2018-03-29 11:14:31 +02:00
|
|
|
erlang:halt(1, [{flush, true}]).
|
|
|
|
|
2022-05-12 17:25:11 +02:00
|
|
|
-spec get_pid_file() -> false | string().
|
2009-08-24 23:21:04 +02:00
|
|
|
get_pid_file() ->
|
|
|
|
case os:getenv("EJABBERD_PID_PATH") of
|
|
|
|
false ->
|
|
|
|
false;
|
|
|
|
"" ->
|
|
|
|
false;
|
|
|
|
Path ->
|
|
|
|
Path
|
|
|
|
end.
|
2013-04-08 11:12:54 +02:00
|
|
|
|
2013-07-06 18:11:01 +02:00
|
|
|
start_app(App) ->
|
|
|
|
start_app(App, temporary).
|
|
|
|
|
|
|
|
start_app(App, Type) ->
|
|
|
|
StartFlag = not is_loaded(),
|
|
|
|
start_app(App, Type, StartFlag).
|
|
|
|
|
|
|
|
is_loaded() ->
|
|
|
|
Apps = application:which_applications(),
|
|
|
|
lists:keymember(ejabberd, 1, Apps).
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
start_app(App, Type, StartFlag) when is_atom(App) ->
|
2013-07-06 18:11:01 +02:00
|
|
|
start_app([App], Type, StartFlag);
|
|
|
|
start_app([App|Apps], Type, StartFlag) ->
|
2015-07-30 12:57:59 +02:00
|
|
|
case application:start(App,Type) of
|
2013-04-08 11:12:54 +02:00
|
|
|
ok ->
|
2013-07-06 18:11:01 +02:00
|
|
|
start_app(Apps, Type, StartFlag);
|
2013-04-08 11:12:54 +02:00
|
|
|
{error, {already_started, _}} ->
|
2013-07-06 18:11:01 +02:00
|
|
|
start_app(Apps, Type, StartFlag);
|
2013-04-08 11:12:54 +02:00
|
|
|
{error, {not_started, DepApp}} ->
|
|
|
|
case lists:member(DepApp, [App|Apps]) of
|
|
|
|
true ->
|
2013-07-06 18:11:01 +02:00
|
|
|
Reason = io_lib:format(
|
2019-09-23 14:17:20 +02:00
|
|
|
"Failed to start Erlang application '~ts': "
|
|
|
|
"circular dependency with '~ts' detected",
|
2013-07-06 18:11:01 +02:00
|
|
|
[App, DepApp]),
|
|
|
|
exit_or_halt(Reason, StartFlag);
|
2013-04-08 11:12:54 +02:00
|
|
|
false ->
|
2013-07-06 18:11:01 +02:00
|
|
|
start_app([DepApp,App|Apps], Type, StartFlag)
|
2013-04-08 11:12:54 +02:00
|
|
|
end;
|
2019-06-14 11:33:26 +02:00
|
|
|
{error, Why} ->
|
|
|
|
Reason = io_lib:format(
|
2019-09-23 14:17:20 +02:00
|
|
|
"Failed to start Erlang application '~ts': ~ts. ~ts",
|
2019-06-14 11:33:26 +02:00
|
|
|
[App, format_error(Why), hint()]),
|
2013-07-06 18:11:01 +02:00
|
|
|
exit_or_halt(Reason, StartFlag)
|
2013-04-08 11:12:54 +02:00
|
|
|
end;
|
2013-07-06 18:11:01 +02:00
|
|
|
start_app([], _Type, _StartFlag) ->
|
2013-04-08 11:12:54 +02:00
|
|
|
ok.
|
2013-07-06 18:11:01 +02:00
|
|
|
|
|
|
|
check_app_modules(App, StartFlag) ->
|
|
|
|
case application:get_key(App, modules) of
|
|
|
|
{ok, Mods} ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(Mod) ->
|
|
|
|
case code:which(Mod) of
|
|
|
|
non_existing ->
|
|
|
|
File = get_module_file(App, Mod),
|
|
|
|
Reason = io_lib:format(
|
2019-09-23 14:17:20 +02:00
|
|
|
"Couldn't find file ~ts needed "
|
|
|
|
"for Erlang application '~ts'. ~ts",
|
2019-06-14 11:33:26 +02:00
|
|
|
[File, App, hint()]),
|
2013-07-06 18:11:01 +02:00
|
|
|
exit_or_halt(Reason, StartFlag);
|
|
|
|
_ ->
|
2019-06-14 11:33:26 +02:00
|
|
|
ok
|
2013-07-06 18:11:01 +02:00
|
|
|
end
|
|
|
|
end, Mods);
|
|
|
|
_ ->
|
|
|
|
%% No modules? This is strange
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
2019-06-14 11:33:26 +02:00
|
|
|
check_apps() ->
|
|
|
|
spawn(
|
|
|
|
fun() ->
|
|
|
|
Apps = [ejabberd |
|
|
|
|
[App || {App, _, _} <- application:which_applications(),
|
2023-12-19 20:20:48 +01:00
|
|
|
App /= ejabberd, App /= hex]],
|
2019-09-23 14:17:20 +02:00
|
|
|
?DEBUG("Checking consistency of applications: ~ts",
|
2019-06-14 11:33:26 +02:00
|
|
|
[misc:join_atoms(Apps, <<", ">>)]),
|
|
|
|
misc:peach(
|
|
|
|
fun(App) ->
|
|
|
|
check_app_modules(App, true)
|
|
|
|
end, Apps),
|
|
|
|
?DEBUG("All applications are intact", []),
|
|
|
|
lists:foreach(fun erlang:garbage_collect/1, processes())
|
|
|
|
end).
|
|
|
|
|
|
|
|
-spec exit_or_halt(iodata(), boolean()) -> no_return().
|
2013-07-06 18:11:01 +02:00
|
|
|
exit_or_halt(Reason, StartFlag) ->
|
|
|
|
?CRITICAL_MSG(Reason, []),
|
|
|
|
if StartFlag ->
|
|
|
|
%% Wait for the critical message is written in the console/log
|
2018-03-29 11:14:31 +02:00
|
|
|
halt();
|
2013-07-06 18:11:01 +02:00
|
|
|
true ->
|
|
|
|
erlang:error(application_start_failed)
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_module_file(App, Mod) ->
|
|
|
|
BaseName = atom_to_list(Mod),
|
2024-03-29 20:49:08 +01:00
|
|
|
case code:lib_dir(App) of
|
2013-07-06 18:11:01 +02:00
|
|
|
{error, _} ->
|
|
|
|
BaseName;
|
|
|
|
Dir ->
|
2024-03-29 20:49:08 +01:00
|
|
|
filename:join([Dir, "ebin", BaseName ++ ".beam"])
|
2013-07-06 18:11:01 +02:00
|
|
|
end.
|
2017-10-31 11:04:32 +01:00
|
|
|
|
2017-10-31 11:25:01 +01:00
|
|
|
module_name([Dir, _, <<H,_/binary>> | _] = Mod) when H >= 65, H =< 90 ->
|
2017-10-31 11:04:32 +01:00
|
|
|
Module = str:join([elixir_name(M) || M<-tl(Mod)], <<>>),
|
|
|
|
Prefix = case elixir_name(Dir) of
|
|
|
|
<<"Ejabberd">> -> <<"Elixir.Ejabberd.">>;
|
|
|
|
Lib -> <<"Elixir.Ejabberd.", Lib/binary, ".">>
|
|
|
|
end,
|
|
|
|
misc:binary_to_atom(<<Prefix/binary, Module/binary>>);
|
|
|
|
module_name([<<"ejabberd">> | _] = Mod) ->
|
2017-10-31 12:06:40 +01:00
|
|
|
Module = str:join([erlang_name(M) || M<-Mod], $_),
|
|
|
|
misc:binary_to_atom(Module);
|
2017-10-31 11:04:32 +01:00
|
|
|
module_name(Mod) when is_list(Mod) ->
|
2017-10-31 12:06:40 +01:00
|
|
|
Module = str:join([erlang_name(M) || M<-tl(Mod)], $_),
|
|
|
|
misc:binary_to_atom(Module).
|
2017-10-31 11:04:32 +01:00
|
|
|
|
2017-10-31 12:06:40 +01:00
|
|
|
elixir_name(Atom) when is_atom(Atom) ->
|
|
|
|
elixir_name(misc:atom_to_binary(Atom));
|
2017-10-31 11:04:32 +01:00
|
|
|
elixir_name(<<H,T/binary>>) when H >= 65, H =< 90 ->
|
|
|
|
<<H, T/binary>>;
|
|
|
|
elixir_name(<<H,T/binary>>) ->
|
|
|
|
<<(H-32), T/binary>>.
|
2017-10-31 12:06:40 +01:00
|
|
|
|
|
|
|
erlang_name(Atom) when is_atom(Atom) ->
|
|
|
|
misc:atom_to_binary(Atom);
|
|
|
|
erlang_name(Bin) when is_binary(Bin) ->
|
|
|
|
Bin.
|
2019-06-14 11:33:26 +02:00
|
|
|
|
|
|
|
format_error({Reason, File}) when is_list(Reason), is_list(File) ->
|
|
|
|
Reason ++ ": " ++ File;
|
|
|
|
format_error(Term) ->
|
|
|
|
io_lib:format("~p", [Term]).
|
|
|
|
|
|
|
|
hint() ->
|
|
|
|
"This usually means that ejabberd or Erlang "
|
|
|
|
"was compiled/installed incorrectly.".
|