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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2014-03-13 12:29:21 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2014 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').
|
2002-11-18 21:39:47 +01:00
|
|
|
|
2013-07-06 18:11:01 +02:00
|
|
|
-export([start/0, stop/0, start_app/1, start_app/2,
|
|
|
|
get_pid_file/0, check_app/1]).
|
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() ->
|
2009-05-08 00:46:51 +02:00
|
|
|
%%ejabberd_cover:start(),
|
2003-02-01 21:21:28 +01:00
|
|
|
application:start(ejabberd).
|
2002-11-20 21:19:20 +01:00
|
|
|
|
2003-02-01 21:21:28 +01:00
|
|
|
stop() ->
|
|
|
|
application:stop(ejabberd).
|
2009-05-08 00:46:51 +02:00
|
|
|
%%ejabberd_cover:stop().
|
2002-11-23 21:55:05 +01:00
|
|
|
|
2009-08-24 23:21:04 +02:00
|
|
|
%% @spec () -> false | string()
|
|
|
|
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).
|
|
|
|
|
|
|
|
check_app(App) ->
|
|
|
|
StartFlag = not is_loaded(),
|
|
|
|
spawn(fun() -> check_app_modules(App, StartFlag) end),
|
|
|
|
ok.
|
|
|
|
|
|
|
|
is_loaded() ->
|
|
|
|
Apps = application:which_applications(),
|
|
|
|
lists:keymember(ejabberd, 1, Apps).
|
|
|
|
|
|
|
|
start_app(App, Type, StartFlag) when not is_list(App) ->
|
|
|
|
start_app([App], Type, StartFlag);
|
|
|
|
start_app([App|Apps], Type, StartFlag) ->
|
2013-04-08 11:12:54 +02:00
|
|
|
case application:start(App) of
|
|
|
|
ok ->
|
2013-07-06 18:11:01 +02:00
|
|
|
spawn(fun() -> check_app_modules(App, StartFlag) end),
|
|
|
|
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(
|
|
|
|
"failed to start application '~p': "
|
|
|
|
"circular dependency on '~p' detected",
|
|
|
|
[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;
|
|
|
|
Err ->
|
2013-07-06 18:11:01 +02:00
|
|
|
Reason = io_lib:format("failed to start application '~p': ~p",
|
|
|
|
[App, Err]),
|
|
|
|
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) ->
|
|
|
|
{A, B, C} = now(),
|
|
|
|
random:seed(A, B, C),
|
|
|
|
sleep(5000),
|
|
|
|
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(
|
|
|
|
"couldn't find module ~s "
|
|
|
|
"needed for application '~p'",
|
|
|
|
[File, App]),
|
|
|
|
exit_or_halt(Reason, StartFlag);
|
|
|
|
_ ->
|
|
|
|
sleep(10)
|
|
|
|
end
|
|
|
|
end, Mods);
|
|
|
|
_ ->
|
|
|
|
%% No modules? This is strange
|
|
|
|
ok
|
|
|
|
end.
|
|
|
|
|
|
|
|
exit_or_halt(Reason, StartFlag) ->
|
|
|
|
?CRITICAL_MSG(Reason, []),
|
|
|
|
if StartFlag ->
|
|
|
|
%% Wait for the critical message is written in the console/log
|
|
|
|
timer:sleep(1000),
|
|
|
|
halt(string:substr(lists:flatten(Reason), 1, 199));
|
|
|
|
true ->
|
|
|
|
erlang:error(application_start_failed)
|
|
|
|
end.
|
|
|
|
|
|
|
|
sleep(N) ->
|
|
|
|
timer:sleep(random:uniform(N)).
|
|
|
|
|
|
|
|
get_module_file(App, Mod) ->
|
|
|
|
BaseName = atom_to_list(Mod),
|
|
|
|
case code:lib_dir(App, ebin) of
|
|
|
|
{error, _} ->
|
|
|
|
BaseName;
|
|
|
|
Dir ->
|
|
|
|
filename:join([Dir, BaseName ++ ".beam"])
|
|
|
|
end.
|