From c2f3480897b51dd9b2137f1eeddb4c851e9c6e3b Mon Sep 17 00:00:00 2001 From: Michal Schmidt Date: Mon, 5 Mar 2012 22:47:54 +0100 Subject: [PATCH] socket: fail the socket if the service keeps dying on start If the service reaches the start limit, mark the sockets that activate it as failed (with the result code 'service-broken'). This way the sockets won't act as tarpits for clients connecting to them. --- src/service.c | 10 ++++++---- src/socket.c | 12 ++++++++---- src/socket.h | 3 ++- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/service.c b/src/service.c index ec2725a7..5764f5f8 100644 --- a/src/service.c +++ b/src/service.c @@ -1436,7 +1436,7 @@ static int service_search_main_pid(Service *s) { return 0; } -static void service_notify_sockets_dead(Service *s) { +static void service_notify_sockets_dead(Service *s, bool broken) { Iterator i; Unit *u; @@ -1449,7 +1449,7 @@ static void service_notify_sockets_dead(Service *s) { SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) if (u->type == UNIT_SOCKET) - socket_notify_service_dead(SOCKET(u)); + socket_notify_service_dead(SOCKET(u), broken); return; } @@ -1511,7 +1511,7 @@ static void service_set_state(Service *s, ServiceState state) { state == SERVICE_FINAL_SIGKILL || state == SERVICE_FAILED || state == SERVICE_AUTO_RESTART) - service_notify_sockets_dead(s); + service_notify_sockets_dead(s, false); if (state != SERVICE_START_PRE && state != SERVICE_START && @@ -2402,8 +2402,10 @@ static int service_start(Unit *u) { /* Make sure we don't enter a busy loop of some kind. */ r = service_start_limit_test(s); - if (r < 0) + if (r < 0) { + service_notify_sockets_dead(s, true); return r; + } s->result = SERVICE_SUCCESS; s->reload_result = SERVICE_SUCCESS; diff --git a/src/socket.c b/src/socket.c index 15a517be..6bcc1297 100644 --- a/src/socket.c +++ b/src/socket.c @@ -2038,7 +2038,7 @@ int socket_collect_fds(Socket *s, int **fds, unsigned *n_fds) { return 0; } -void socket_notify_service_dead(Socket *s) { +void socket_notify_service_dead(Socket *s, bool broken) { assert(s); /* The service is dead. Dang! @@ -2047,8 +2047,11 @@ void socket_notify_service_dead(Socket *s) { * services. */ if (s->state == SOCKET_RUNNING) { - log_debug("%s got notified about service death.", UNIT(s)->id); - socket_enter_listening(s); + log_debug("%s got notified about service death (broken: %s)", UNIT(s)->id, yes_no(broken)); + if (broken) + socket_enter_stop_pre(s, SOCKET_FAILURE_SERVICE_BROKEN); + else + socket_enter_listening(s); } } @@ -2156,7 +2159,8 @@ static const char* const socket_result_table[_SOCKET_RESULT_MAX] = { [SOCKET_FAILURE_TIMEOUT] = "timeout", [SOCKET_FAILURE_EXIT_CODE] = "exit-code", [SOCKET_FAILURE_SIGNAL] = "signal", - [SOCKET_FAILURE_CORE_DUMP] = "core-dump" + [SOCKET_FAILURE_CORE_DUMP] = "core-dump", + [SOCKET_FAILURE_SERVICE_BROKEN] = "service-broken" }; DEFINE_STRING_TABLE_LOOKUP(socket_result, SocketResult); diff --git a/src/socket.h b/src/socket.h index d242796a..1f4f23c9 100644 --- a/src/socket.h +++ b/src/socket.h @@ -72,6 +72,7 @@ typedef enum SocketResult { SOCKET_FAILURE_EXIT_CODE, SOCKET_FAILURE_SIGNAL, SOCKET_FAILURE_CORE_DUMP, + SOCKET_FAILURE_SERVICE_BROKEN, _SOCKET_RESULT_MAX, _SOCKET_RESULT_INVALID = -1 } SocketResult; @@ -148,7 +149,7 @@ struct Socket { int socket_collect_fds(Socket *s, int **fds, unsigned *n_fds); /* Called from the service when it shut down */ -void socket_notify_service_dead(Socket *s); +void socket_notify_service_dead(Socket *s, bool broken); /* Called from the mount code figure out if a mount is a dependency of * any of the sockets of this socket */ -- 2.39.5