summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon MacMullen <simon@rabbitmq.com>2014-03-13 12:45:54 +0000
committerSimon MacMullen <simon@rabbitmq.com>2014-03-13 12:45:54 +0000
commita384b76234a95da979ed407d3a8d4c4c9598c651 (patch)
treee84fbc1ca8c6d37e1828b31de9d23fbe1c5bdac9
parentcb53413987e483ae65f25dbd1664f89a69f704dc (diff)
downloadrabbitmq-server-a384b76234a95da979ed407d3a8d4c4c9598c651.tar.gz
Check if dist port is available since otherwise we will create a horrific error message. Also refactor a touch.
-rwxr-xr-xscripts/rabbitmq-server2
-rw-r--r--src/rabbit_prelaunch.erl56
2 files changed, 38 insertions, 20 deletions
diff --git a/scripts/rabbitmq-server b/scripts/rabbitmq-server
index 7372a01a..2f89abdb 100755
--- a/scripts/rabbitmq-server
+++ b/scripts/rabbitmq-server
@@ -85,7 +85,7 @@ case "$(uname -s)" in
fi
esac
-export RABBITMQ_CONFIG_FILE
+export RABBITMQ_CONFIG_FILE RABBITMQ_DIST_PORT
RABBITMQ_EBIN_ROOT="${RABBITMQ_HOME}/ebin"
${ERL_DIR}erl -pa "$RABBITMQ_EBIN_ROOT" \
diff --git a/src/rabbit_prelaunch.erl b/src/rabbit_prelaunch.erl
index 0fdb91c4..5068d244 100644
--- a/src/rabbit_prelaunch.erl
+++ b/src/rabbit_prelaunch.erl
@@ -39,8 +39,10 @@
start() ->
[NodeStr] = init:get_plain_arguments(),
- ok = duplicate_node_check(NodeStr),
- rabbit_misc:quit(0),
+ {ok, NodeHost} = duplicate_node_check(NodeStr),
+ ok = dist_port_set_check(),
+ ok = dist_port_use_check(NodeHost),
+ rabbit_misc:quit(?DIST_PORT_NOT_CONFIGURED),
ok.
stop() ->
@@ -63,26 +65,42 @@ duplicate_node_check(NodeStr) ->
[NodeName, NodeHost]),
io:format(rabbit_nodes:diagnostics([Node]) ++ "~n"),
rabbit_misc:quit(?ERROR_CODE);
- false -> ok
- end,
- case file:consult(os:getenv("RABBITMQ_CONFIG_FILE") ++ ".config") of
- {ok, [Config]} ->
- Kernel = proplists:get_value(kernel, Config, []),
- case {proplists:get_value(inet_dist_listen_min, Kernel),
- proplists:get_value(inet_dist_listen_max, Kernel)}
- of
- {undefined, undefined} ->
- rabbit_misc:quit(?DIST_PORT_NOT_CONFIGURED);
- _ ->
- rabbit_misc:quit(?DIST_PORT_CONFIGURED)
- end;
- {error, _} ->
- %% TODO can we present errors more nicely here
- %% than after -config has failed?
- rabbit_misc:quit(?DIST_PORT_NOT_CONFIGURED)
+ false -> {ok, NodeHost}
end;
{error, EpmdReason} ->
io:format("ERROR: epmd error for host ~s: ~s~n",
[NodeHost, rabbit_misc:format_inet_error(EpmdReason)]),
rabbit_misc:quit(?ERROR_CODE)
end.
+
+dist_port_set_check() ->
+ case file:consult(os:getenv("RABBITMQ_CONFIG_FILE") ++ ".config") of
+ {ok, [Config]} ->
+ Kernel = proplists:get_value(kernel, Config, []),
+ case {proplists:get_value(inet_dist_listen_min, Kernel, none),
+ proplists:get_value(inet_dist_listen_max, Kernel, none)} of
+ {none, none} -> ok;
+ _ -> rabbit_misc:quit(?DIST_PORT_CONFIGURED)
+ end;
+ {error, _} ->
+ %% TODO can we present errors more nicely here
+ %% than after -config has failed?
+ ok
+ end.
+
+dist_port_use_check(NodeHost) ->
+ Port = list_to_integer(os:getenv("RABBITMQ_DIST_PORT")),
+ case gen_tcp:listen(Port, [inet]) of
+ {ok, Sock} -> gen_tcp:close(Sock);
+ {error, _} -> dist_port_use_check_fail(Port, NodeHost)
+ end.
+
+dist_port_use_check_fail(Port, Host) ->
+ {ok, Names} = rabbit_nodes:names(Host),
+ case [N || {N, P} <- Names, P =:= Port] of
+ [] -> io:format("ERROR: distribution port ~b in use on ~s "
+ "(by non-Erlang process?)~n", [Port, Host]);
+ [Name] -> io:format("ERROR: distribution port ~b in use by ~s@~s~n",
+ [Port, Name, Host])
+ end,
+ rabbit_misc:quit(?ERROR_CODE).