f8bec47
From: Peter Lemenkov <lemenkov@gmail.com>
f8bec47
Date: Thu, 19 May 2016 16:04:56 +0300
f8bec47
Subject: [PATCH] Remove excessive sd_notify code
f8bec47
f8bec47
Signed-off-by: Peter Lemenkov <lemenkov@gmail.com>
f8bec47
f8bec47
diff --git a/src/rabbit.erl b/src/rabbit.erl
1d1d3c2
index 2b043178a..81ddc4e45 100644
f8bec47
--- a/src/rabbit.erl
f8bec47
+++ b/src/rabbit.erl
1d1d3c2
@@ -349,123 +349,9 @@ broker_start() ->
f8bec47
     Plugins = rabbit_plugins:setup(),
f8bec47
     ToBeLoaded = Plugins ++ ?APPS,
f8bec47
     start_apps(ToBeLoaded),
f8bec47
-    maybe_sd_notify(),
1d1d3c2
     ok = rabbit_lager:broker_is_started(),
1d1d3c2
     ok = log_broker_started(rabbit_plugins:strictly_plugins(rabbit_plugins:active())).
f8bec47
 
f8bec47
-%% Try to send systemd ready notification if it makes sense in the
f8bec47
-%% current environment. standard_error is used intentionally in all
f8bec47
-%% logging statements, so all this messages will end in systemd
f8bec47
-%% journal.
f8bec47
-maybe_sd_notify() ->
f8bec47
-    case sd_notify_ready() of
f8bec47
-        false ->
f8bec47
-            io:format(standard_error, "systemd READY notification failed, beware of timeouts~n", []);
f8bec47
-        _ ->
f8bec47
-            ok
f8bec47
-    end.
f8bec47
-
f8bec47
-sd_notify_ready() ->
f8bec47
-    case {os:type(), os:getenv("NOTIFY_SOCKET")} of
f8bec47
-        {{win32, _}, _} ->
f8bec47
-            true;
f8bec47
-        {_, [_|_]} -> %% Non-empty NOTIFY_SOCKET, give it a try
f8bec47
-            sd_notify_legacy() orelse sd_notify_socat();
f8bec47
-        _ ->
f8bec47
-            true
f8bec47
-    end.
f8bec47
-
f8bec47
-sd_notify_data() ->
f8bec47
-    "READY=1\nSTATUS=Initialized\nMAINPID=" ++ os:getpid() ++ "\n".
f8bec47
-
f8bec47
-sd_notify_legacy() ->
f8bec47
-    case code:load_file(sd_notify) of
f8bec47
-        {module, sd_notify} ->
f8bec47
-            SDNotify = sd_notify,
f8bec47
-            SDNotify:sd_notify(0, sd_notify_data()),
f8bec47
-            true;
f8bec47
-        {error, _} ->
f8bec47
-            false
f8bec47
-    end.
f8bec47
-
f8bec47
-%% socat(1) is the most portable way the sd_notify could be
f8bec47
-%% implemented in erlang, without introducing some NIF. Currently the
f8bec47
-%% following issues prevent us from implementing it in a more
f8bec47
-%% reasonable way:
f8bec47
-%% - systemd-notify(1) is unstable for non-root users
f8bec47
-%% - erlang doesn't support unix domain sockets.
f8bec47
-%%
f8bec47
-%% Some details on how we ended with such a solution:
f8bec47
-%%   https://github.com/rabbitmq/rabbitmq-server/issues/664
f8bec47
-sd_notify_socat() ->
f8bec47
-    case sd_current_unit() of
f8bec47
-        {ok, Unit} ->
f8bec47
-            io:format(standard_error, "systemd unit for activation check: \"~s\"~n", [Unit]),
f8bec47
-            sd_notify_socat(Unit);
f8bec47
-        _ ->
f8bec47
-            false
f8bec47
-    end.
f8bec47
-
f8bec47
-socat_socket_arg("@" ++ AbstractUnixSocket) ->
f8bec47
-    "abstract-sendto:" ++ AbstractUnixSocket;
f8bec47
-socat_socket_arg(UnixSocket) ->
f8bec47
-    "unix-sendto:" ++ UnixSocket.
f8bec47
-
f8bec47
-sd_open_port() ->
f8bec47
-    open_port(
f8bec47
-      {spawn_executable, os:find_executable("socat")},
f8bec47
-      [{args, [socat_socket_arg(os:getenv("NOTIFY_SOCKET")), "STDIO"]},
f8bec47
-       use_stdio, out]).
f8bec47
-
f8bec47
-sd_notify_socat(Unit) ->
2dece13
-    try sd_open_port() of
f8bec47
-        Port ->
f8bec47
-            Port ! {self(), {command, sd_notify_data()}},
f8bec47
-            Result = sd_wait_activation(Port, Unit),
f8bec47
-            port_close(Port),
f8bec47
-            Result
2dece13
-    catch
2dece13
-        Class:Reason ->
2dece13
-            io:format(standard_error, "Failed to start socat ~p:~p~n", [Class, Reason]),
2dece13
-            false
f8bec47
-    end.
f8bec47
-
f8bec47
-sd_current_unit() ->
1d1d3c2
-    CmdOut = os:cmd("ps -o unit= -p " ++ os:getpid()),
1d1d3c2
-    case catch re:run(CmdOut, "([-.@0-9a-zA-Z]+)", [unicode, {capture, all_but_first, list}]) of
f8bec47
-        {'EXIT', _} ->
f8bec47
-            error;
f8bec47
-        {match, [Unit]} ->
f8bec47
-            {ok, Unit};
f8bec47
-        _ ->
f8bec47
-            error
f8bec47
-    end.
f8bec47
-
f8bec47
-sd_wait_activation(Port, Unit) ->
f8bec47
-    case os:find_executable("systemctl") of
f8bec47
-        false ->
f8bec47
-            io:format(standard_error, "'systemctl' unavailable, falling back to sleep~n", []),
f8bec47
-            timer:sleep(5000),
f8bec47
-            true;
f8bec47
-        _ ->
f8bec47
-            sd_wait_activation(Port, Unit, 10)
f8bec47
-    end.
f8bec47
-
f8bec47
-sd_wait_activation(_, _, 0) ->
f8bec47
-    io:format(standard_error, "Service still in 'activating' state, bailing out~n", []),
f8bec47
-    false;
f8bec47
-sd_wait_activation(Port, Unit, AttemptsLeft) ->
1d1d3c2
-    case os:cmd("systemctl show --property=ActiveState -- '" ++ Unit ++ "'") of
f8bec47
-        "ActiveState=activating\n" ->
f8bec47
-            timer:sleep(1000),
f8bec47
-            sd_wait_activation(Port, Unit, AttemptsLeft - 1);
f8bec47
-        "ActiveState=" ++ _ ->
f8bec47
-            true;
f8bec47
-        _ = Err->
f8bec47
-            io:format(standard_error, "Unexpected status from systemd ~p~n", [Err]),
f8bec47
-            false
f8bec47
-    end.
f8bec47
-
f8bec47
 start_it(StartFun) ->
f8bec47
     Marker = spawn_link(fun() -> receive stop -> ok end end),
f8bec47
     case catch register(rabbit_boot, Marker) of