Innopolis University DevOps Playground
Skip to content
Snippets Groups Projects
ejabberd_app.erl 4.58 KiB
Newer Older
%%%----------------------------------------------------------------------
%%% File    : ejabberd_app.erl
%%% Author  : Alexey Shchepin <alexey@process-one.net>
Badlop's avatar
 
Badlop committed
%%% Purpose : ejabberd's application callback module
%%% Created : 31 Jan 2003 by Alexey Shchepin <alexey@process-one.net>
%%%
%%%
Badlop's avatar
 
Badlop committed
%%% ejabberd, Copyright (C) 2002-2009   ProcessOne
%%%
%%% 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.
Badlop's avatar
 
Badlop committed
%%%
%%% 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
%%%
%%%----------------------------------------------------------------------

-module(ejabberd_app).
-author('alexey@process-one.net').

-behaviour(application).

Badlop's avatar
 
Badlop committed
-export([start/2, prep_stop/1, stop/1, init/0]).

-include("ejabberd.hrl").

Badlop's avatar
 
Badlop committed

%%%
%%% Application API
%%%

Alexey Shchepin's avatar
 
Alexey Shchepin committed
start(normal, _Args) ->
Alexey Shchepin's avatar
 
Alexey Shchepin committed
    application:start(sasl),
    randoms:start(),
    db_init(),
    sha:start(),
    catch ssl:start(),
Badlop's avatar
 
Badlop committed
    start(),
    translate:start(),
    acl:start(),
Alexey Shchepin's avatar
 
Alexey Shchepin committed
    ejabberd_ctl:init(),
    gen_mod:start(),
    ejabberd_config:start(),
Alexey Shchepin's avatar
 
Alexey Shchepin committed
    connect_nodes(),
Alexey Shchepin's avatar
 
Alexey Shchepin committed
    Sup = ejabberd_sup:start_link(),
    ejabberd_auth:start(),
Alexey Shchepin's avatar
 
Alexey Shchepin committed
    cyrsasl:start(),
Alexey Shchepin's avatar
 
Alexey Shchepin committed
    % Profiling
    %eprof:start(),
    %eprof:profile([self()]),
    %fprof:trace(start, "/tmp/fprof"),
Badlop's avatar
 
Badlop committed
    maybe_add_nameservers(),
Badlop's avatar
 
Badlop committed
    start_modules(),
    Sup;
start(_, _) ->
    {error, badarg}.

Badlop's avatar
 
Badlop committed
%% Prepare the application for termination.
%% This function is called when an application is about to be stopped, 
%% before shutting down the processes of the application.
prep_stop(State) ->
    stop_modules(),
    State.

%% All the processes were killed when this function is called
stop(_State) ->
Badlop's avatar
 
Badlop committed

%%%
%%% Internal functions
%%%

start() ->
Alexey Shchepin's avatar
 
Alexey Shchepin committed
    spawn_link(?MODULE, init, []).

init() ->
    register(ejabberd, self()),
    %erlang:system_flag(fullsweep_after, 0),
Alexey Shchepin's avatar
 
Alexey Shchepin committed
    %error_logger:logfile({open, ?LOG_PATH}),
Alexey Shchepin's avatar
 
Alexey Shchepin committed
    LogPath =
	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,
Alexey Shchepin's avatar
 
Alexey Shchepin committed
    error_logger:add_report_handler(ejabberd_logger_h, LogPath),
Alexey Shchepin's avatar
 
Alexey Shchepin committed
    erl_ddll:load_driver(ejabberd:get_so_path(), tls_drv),
Alexey Shchepin's avatar
 
Alexey Shchepin committed
    case erl_ddll:load_driver(ejabberd:get_so_path(), expat_erl) of
	ok -> ok;
	{error, already_loaded} -> ok
    end,
    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).

Badlop's avatar
 
Badlop committed
%% Start all the modules in all the hosts
start_modules() ->
Alexey Shchepin's avatar
 
Alexey Shchepin committed
    lists:foreach(
      fun(Host) ->
Alexey Shchepin's avatar
 
Alexey Shchepin committed
	      case ejabberd_config:get_local_option({modules, Host}) of
Alexey Shchepin's avatar
 
Alexey Shchepin committed
		  undefined ->
		      ok;
		  Modules ->
		      lists:foreach(
			fun({Module, Args}) ->
				gen_mod:start_module(Host, Module, Args)
			end, Modules)
	      end
      end, ?MYHOSTS).
Badlop's avatar
 
Badlop committed
%% 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}) ->
Badlop's avatar
 
Badlop committed
				gen_mod:stop_module_keep_config(Host, Module)
Badlop's avatar
 
Badlop committed
			end, Modules)
	      end
      end, ?MYHOSTS).

Alexey Shchepin's avatar
 
Alexey Shchepin committed
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.

Badlop's avatar
 
Badlop committed

%% 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() ->
    IPTs = win32_dns:get_nameservers(),
Badlop's avatar
 
Badlop committed
    ?INFO_MSG("Adding machine's DNS IPs to Erlang system:~n~p", [IPTs]),
    lists:foreach(fun(IPT) -> inet_db:add_ns(IPT) end, IPTs).