From 47537aa90160d02f9ba108b267c351c34adf6404 Mon Sep 17 00:00:00 2001 From: Mickael Remond Date: Mon, 6 Apr 2015 11:36:16 +0200 Subject: [PATCH] Make sure Mnesia dir environment and log file are list, not binary This is useful for Elixir configuration, as binary is the more natural data type. Closes #514 --- src/ejabberd_app.erl | 1 + src/ejabberd_config.erl | 19 ++++++++++++++++++- src/ejabberd_logger.erl | 4 +++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/ejabberd_app.erl b/src/ejabberd_app.erl index 4c821cd98..cd217ddde 100644 --- a/src/ejabberd_app.erl +++ b/src/ejabberd_app.erl @@ -110,6 +110,7 @@ loop() -> end. db_init() -> + ejabberd_config:env_binary_to_list(mnesia, dir), MyNode = node(), DbNodes = mnesia:system_info(db_nodes), case lists:member(MyNode, DbNodes) of diff --git a/src/ejabberd_config.erl b/src/ejabberd_config.erl index 022590bc5..5d1df5056 100644 --- a/src/ejabberd_config.erl +++ b/src/ejabberd_config.erl @@ -35,7 +35,8 @@ get_version/0, get_myhosts/0, get_mylang/0, prepare_opt_val/4, convert_table_to_binary/5, transform_options/1, collect_options/1, - convert_to_yaml/1, convert_to_yaml/2]). + convert_to_yaml/1, convert_to_yaml/2, + env_binary_to_list/2]). -include("ejabberd.hrl"). -include("logger.hrl"). @@ -167,6 +168,22 @@ convert_to_yaml(File, Output) -> file:write_file(FileName, Data) end. +%% Some Erlang apps expects env parameters to be list and not binary. +%% For example, Mnesia is not able to start if mnesia dir is passed as a binary. +%% However, binary is most common on Elixir, so it is easy to make a setup mistake. +-spec env_binary_to_list(atom(), atom()) -> {ok, any()}|undefined. +env_binary_to_list(Application, Parameter) -> + %% Application need to be loaded to allow setting parameters + application:load(Application), + case application:get_env(Application, Parameter) of + {ok, Val} when is_binary(Val) -> + BVal = binary_to_list(Val), + application:set_env(Application, Parameter, BVal), + {ok, BVal}; + Other -> + Other + end. + %% @doc Read an ejabberd configuration file and return the terms. %% Input is an absolute or relative path to an ejabberd config file. %% Returns a list of plain terms, diff --git a/src/ejabberd_logger.erl b/src/ejabberd_logger.erl index 59beca16d..a00ac9942 100644 --- a/src/ejabberd_logger.erl +++ b/src/ejabberd_logger.erl @@ -46,8 +46,10 @@ %% 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. +%% Note: If the directory where to place the ejabberd log file to not exist, +%% it is not created and no log file will be generated. get_log_path() -> - case application:get_env(ejabberd, log_path) of + case ejabberd_config:env_binary_to_list(ejabberd, log_path) of {ok, Path} -> Path; undefined ->