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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2008-09-16 16:39:57 +02:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2008 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.
|
|
|
|
%%%
|
|
|
|
%%% 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., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
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).
|
|
|
|
|
2008-12-01 16:53:30 +01:00
|
|
|
-export([start_modules/0,start/2, get_log_path/0, prep_stop/1, stop/1, init/0]).
|
2003-02-01 21:21:28 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
2008-02-28 01:30:23 +01:00
|
|
|
|
|
|
|
%%%
|
|
|
|
%%% Application API
|
|
|
|
%%%
|
|
|
|
|
2003-10-18 21:15:12 +02:00
|
|
|
start(normal, _Args) ->
|
2006-12-04 17:07:44 +01:00
|
|
|
ejabberd_loglevel:set(4),
|
2003-07-20 22:35:35 +02:00
|
|
|
application:start(sasl),
|
2008-12-01 16:00:36 +01:00
|
|
|
application:start(exmpp),
|
2003-02-01 21:21:28 +01:00
|
|
|
randoms:start(),
|
|
|
|
db_init(),
|
|
|
|
sha:start(),
|
2008-12-01 16:53:30 +01:00
|
|
|
start(),
|
2003-02-01 21:21:28 +01:00
|
|
|
translate:start(),
|
|
|
|
acl:start(),
|
2006-02-20 05:07:42 +01:00
|
|
|
ejabberd_ctl:init(),
|
2008-10-13 12:11:19 +02:00
|
|
|
ejabberd_commands:init(),
|
|
|
|
ejabberd_admin:start(),
|
2003-02-01 21:21:28 +01:00
|
|
|
gen_mod:start(),
|
|
|
|
ejabberd_config:start(),
|
2008-02-27 21:15:25 +01:00
|
|
|
ejabberd_check:config(),
|
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(),
|
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
|
|
|
|
%eprof:start(),
|
|
|
|
%eprof:profile([self()]),
|
2003-10-18 21:15:12 +02:00
|
|
|
%fprof:trace(start, "/tmp/fprof"),
|
2008-02-28 01:30:23 +01:00
|
|
|
start_modules(),
|
2008-10-13 12:11:19 +02:00
|
|
|
ejabberd_listener:start_listeners(),
|
2008-12-01 16:53:30 +01: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-12-01 16:53:30 +01: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) ->
|
|
|
|
stop_modules(),
|
2008-10-13 12:11:19 +02:00
|
|
|
ejabberd_admin:stop(),
|
2008-02-28 01:30:23 +01:00
|
|
|
State.
|
|
|
|
|
|
|
|
%% All the processes were killed when this function is called
|
|
|
|
stop(_State) ->
|
2008-12-01 16:53:30 +01:00
|
|
|
?INFO_MSG("ejabberd ~s is stopped in the node ~p", [?VERSION, node()]),
|
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()),
|
|
|
|
%erlang:system_flag(fullsweep_after, 0),
|
2003-10-23 21:20:56 +02:00
|
|
|
%error_logger:logfile({open, ?LOG_PATH}),
|
2008-12-01 16:53:30 +01:00
|
|
|
LogPath = get_log_path(),
|
2003-12-28 21:59:21 +01:00
|
|
|
error_logger:add_report_handler(ejabberd_logger_h, LogPath),
|
2005-10-21 23:22:00 +02:00
|
|
|
erl_ddll:load_driver(ejabberd:get_so_path(), tls_drv),
|
2005-10-25 04:19:01 +02:00
|
|
|
case erl_ddll:load_driver(ejabberd:get_so_path(), expat_erl) of
|
|
|
|
ok -> ok;
|
|
|
|
{error, already_loaded} -> ok
|
|
|
|
end,
|
2003-02-01 21:21:28 +01:00
|
|
|
Port = open_port({spawn, expat_erl}, [binary]),
|
|
|
|
loop(Port).
|
|
|
|
|
|
|
|
|
|
|
|
loop(Port) ->
|
|
|
|
receive
|
|
|
|
_ ->
|
|
|
|
loop(Port)
|
|
|
|
end.
|
|
|
|
|
|
|
|
db_init() ->
|
|
|
|
case mnesia:system_info(extra_db_nodes) of
|
|
|
|
[] ->
|
|
|
|
mnesia:create_schema([node()]);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
|
|
|
mnesia:start(),
|
|
|
|
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) ->
|
2005-07-03 03:44:13 +02:00
|
|
|
case ejabberd_config:get_local_option({modules, Host}) of
|
2005-06-20 05:18:13 +02:00
|
|
|
undefined ->
|
|
|
|
ok;
|
|
|
|
Modules ->
|
|
|
|
lists:foreach(
|
|
|
|
fun({Module, Args}) ->
|
|
|
|
gen_mod:start_module(Host, Module, Args)
|
|
|
|
end, Modules)
|
|
|
|
end
|
|
|
|
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) ->
|
|
|
|
case ejabberd_config:get_local_option({modules, Host}) of
|
|
|
|
undefined ->
|
|
|
|
ok;
|
|
|
|
Modules ->
|
|
|
|
lists:foreach(
|
|
|
|
fun({Module, _Args}) ->
|
2008-09-16 16:39:57 +02:00
|
|
|
gen_mod:stop_module_keep_config(Host, Module)
|
2008-02-28 01:30:23 +01:00
|
|
|
end, Modules)
|
|
|
|
end
|
|
|
|
end, ?MYHOSTS).
|
|
|
|
|
2006-11-07 03:08:51 +01:00
|
|
|
connect_nodes() ->
|
|
|
|
case ejabberd_config:get_local_option(cluster_nodes) of
|
|
|
|
undefined ->
|
|
|
|
ok;
|
|
|
|
Nodes when is_list(Nodes) ->
|
|
|
|
lists:foreach(fun(Node) ->
|
|
|
|
net_kernel:connect_node(Node)
|
|
|
|
end, Nodes)
|
|
|
|
end.
|
|
|
|
|
2008-12-01 16:53:30 +01:00
|
|
|
%% @spec () -> string()
|
|
|
|
%% Returns the full path to the ejabberd log file.
|
|
|
|
%% It first checks for application configuration parameter 'log_path'.
|
|
|
|
%% If not defined it checks the environment variable EJABBERD_LOG_PATH.
|
|
|
|
%% And if that one is neither defined, returns the default value:
|
|
|
|
%% "ejabberd.log" in current directory.
|
|
|
|
get_log_path() ->
|
|
|
|
case application:get_env(log_path) of
|
|
|
|
{ok, Path} ->
|
|
|
|
Path;
|
|
|
|
undefined ->
|
|
|
|
case os:getenv("EJABBERD_LOG_PATH") of
|
|
|
|
false ->
|
|
|
|
?LOG_PATH;
|
|
|
|
Path ->
|
|
|
|
Path
|
|
|
|
end
|
|
|
|
end.
|