From c35755fb878af58b80ac62a501a75f79c90a3763 Mon Sep 17 00:00:00 2001 From: Jouke Witteveen Date: Tue, 22 Nov 2016 17:39:56 +0100 Subject: [PATCH 1/2] service: introduce protocol error type Introduce a SERVICE_FAILURE_PROTOCOL error type for when a service does not follow the protocol. This error type is used when a pid file is expected, but not delivered. --- src/core/service.c | 9 +++++---- src/core/service.h | 1 + 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/core/service.c b/src/core/service.c index 9ad4cf5070..47368edd0a 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -2585,9 +2585,9 @@ static void service_notify_cgroup_empty_event(Unit *u) { service_unwatch_pid_file(s); if (s->state == SERVICE_START) - service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); + service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_PROTOCOL); else - service_enter_stop(s, SERVICE_FAILURE_RESOURCES); + service_enter_stop(s, SERVICE_FAILURE_PROTOCOL); } break; @@ -2825,7 +2825,7 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) { if (!has_start_post && r < 0) { r = service_demand_pid_file(s); if (r < 0 || !cgroup_good(s)) - service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); + service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_PROTOCOL); break; } } else @@ -2847,7 +2847,7 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) { if (r < 0) { r = service_demand_pid_file(s); if (r < 0 || !cgroup_good(s)) - service_enter_stop(s, SERVICE_FAILURE_RESOURCES); + service_enter_stop(s, SERVICE_FAILURE_PROTOCOL); break; } } else @@ -3384,6 +3384,7 @@ DEFINE_STRING_TABLE_LOOKUP(notify_state, NotifyState); static const char* const service_result_table[_SERVICE_RESULT_MAX] = { [SERVICE_SUCCESS] = "success", [SERVICE_FAILURE_RESOURCES] = "resources", + [SERVICE_FAILURE_PROTOCOL] = "protocol", [SERVICE_FAILURE_TIMEOUT] = "timeout", [SERVICE_FAILURE_EXIT_CODE] = "exit-code", [SERVICE_FAILURE_SIGNAL] = "signal", diff --git a/src/core/service.h b/src/core/service.h index 2869144fcb..278cc1ceb8 100644 --- a/src/core/service.h +++ b/src/core/service.h @@ -81,6 +81,7 @@ typedef enum NotifyState { typedef enum ServiceResult { SERVICE_SUCCESS, SERVICE_FAILURE_RESOURCES, /* a bit of a misnomer, just our catch-all error for errnos we didn't expect */ + SERVICE_FAILURE_PROTOCOL, SERVICE_FAILURE_TIMEOUT, SERVICE_FAILURE_EXIT_CODE, SERVICE_FAILURE_SIGNAL, From 3d474ef7a687e2052aa303e0f95893b2fc610475 Mon Sep 17 00:00:00 2001 From: Jouke Witteveen Date: Sat, 1 Oct 2016 14:06:48 +0200 Subject: [PATCH 2/2] service: fix main processes exit behavior for type notify services Before this commit, when the main process of a Type=notify service exits the service would enter a running state without passing through the startup post state. This meant ExecStartPost= from being executed and allowed follow-up units to start too early (before the ready notification). Additionally, when RemainAfterExit=yes is used on a Type=notify service, the exit status of the main process would be disregarded. After this commit, an unsuccessful exit of the main process of a Type=notify service puts the unit in a failed state. A successful exit is inconsequential in case RemainAfterExit=yes. Otherwise, when no ready notification has been received, the unit is put in a failed state because it has never been active. When all processes in the cgroup of a Type=notify service are gone and no ready notification has been received yet, the unit is also put in a failed state. --- src/core/service.c | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/core/service.c b/src/core/service.c index 47368edd0a..180854b57c 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -1694,7 +1694,9 @@ static void service_enter_running(Service *s, ServiceResult f) { service_arm_timer(s, usec_add(UNIT(s)->active_enter_timestamp.monotonic, s->runtime_max_usec)); } - } else if (s->remain_after_exit) + } else if (f != SERVICE_SUCCESS) + service_enter_signal(s, SERVICE_FINAL_SIGTERM, f); + else if (s->remain_after_exit) service_set_state(s, SERVICE_EXITED); else service_enter_stop(s, SERVICE_SUCCESS); @@ -2578,9 +2580,11 @@ static void service_notify_cgroup_empty_event(Unit *u) { case SERVICE_START: case SERVICE_START_POST: - /* If we were hoping for the daemon to write its PID file, - * we can give up now. */ - if (s->pid_file_pathspec) { + if (s->type == SERVICE_NOTIFY) + /* No chance of getting a ready notification anymore */ + service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_PROTOCOL); + else if (s->pid_file_pathspec) { + /* Give up hoping for the daemon to write its PID file */ log_unit_warning(u, "Daemon never wrote its PID file. Failing."); service_unwatch_pid_file(s); @@ -2721,6 +2725,16 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) { else service_enter_signal(s, SERVICE_FINAL_SIGTERM, f); break; + } else if (s->type == SERVICE_NOTIFY) { + /* Only enter running through a notification, so that the + * SERVICE_START state signifies that no ready notification + * has been received */ + if (f != SERVICE_SUCCESS) + service_enter_signal(s, SERVICE_FINAL_SIGTERM, f); + else if (!s->remain_after_exit) + /* The service has never been active */ + service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_PROTOCOL); + break; } /* Fall through */