661545
From 273d69011bf2a8abfcef71e33e6b6ae3323dfc34 Mon Sep 17 00:00:00 2001
661545
From: Jouke Witteveen <j.witteveen@gmail.com>
661545
Date: Sat, 1 Oct 2016 14:06:48 +0200
661545
Subject: [PATCH] service: fix main processes exit behavior for type notify
661545
 services
661545
661545
Before this commit, when the main process of a Type=notify service exits the
661545
service would enter a running state without passing through the startup post
661545
state. This meant ExecStartPost= from being executed and allowed follow-up
661545
units to start too early (before the ready notification).
661545
Additionally, when RemainAfterExit=yes is used on a Type=notify service, the
661545
exit status of the main process would be disregarded.
661545
661545
After this commit, an unsuccessful exit of the main process of a Type=notify
661545
service puts the unit in a failed state. A successful exit is inconsequential
661545
in case RemainAfterExit=yes. Otherwise, when no ready notification has been
661545
received, the unit is put in a failed state because it has never been active.
661545
When all processes in the cgroup of a Type=notify service are gone and no ready
661545
notification has been received yet, the unit is also put in a failed state.
661545
661545
(cherry picked from commit 3d474ef7a687e2052aa303e0f95893b2fc610475)
661545
661545
Related: #1733998
661545
---
661545
 src/core/service.c | 24 +++++++++++++++++++-----
661545
 1 file changed, 19 insertions(+), 5 deletions(-)
661545
661545
diff --git a/src/core/service.c b/src/core/service.c
661545
index 84a5aeb072..b6f302ec33 100644
661545
--- a/src/core/service.c
661545
+++ b/src/core/service.c
661545
@@ -1531,7 +1531,9 @@ static void service_enter_running(Service *s, ServiceResult f) {
661545
                 else
661545
                         service_set_state(s, SERVICE_RUNNING);
661545
 
661545
-        } else if (s->remain_after_exit)
661545
+        } else if (f != SERVICE_SUCCESS)
661545
+                service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
661545
+        else if (s->remain_after_exit)
661545
                 service_set_state(s, SERVICE_EXITED);
661545
         else
661545
                 service_enter_stop(s, SERVICE_SUCCESS);
661545
@@ -2561,10 +2563,12 @@ static void service_notify_cgroup_empty_event(Unit *u) {
661545
 
661545
         case SERVICE_START:
661545
         case SERVICE_START_POST:
661545
-                /* If we were hoping for the daemon to write its PID file,
661545
-                 * we can give up now. */
661545
-                if (s->pid_file_pathspec) {
661545
-                        log_unit_warning(u->id, "%s never wrote its PID file. Failing.", UNIT(s)->id);
661545
+                if (s->type == SERVICE_NOTIFY)
661545
+                        /* No chance of getting a ready notification anymore */
661545
+                        service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_PROTOCOL);
661545
+                else if (s->pid_file_pathspec) {
661545
+                        /* Give up hoping for the daemon to write its PID file */
661545
+                        log_unit_warning(u->id, "Daemon never wrote its PID file. Failing.");
661545
 
661545
                         service_unwatch_pid_file(s);
661545
                         if (s->state == SERVICE_START)
661545
@@ -2699,6 +2703,16 @@ static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
661545
                                         else
661545
                                                 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
661545
                                         break;
661545
+                                } else if (s->type == SERVICE_NOTIFY) {
661545
+                                        /* Only enter running through a notification, so that the
661545
+                                         * SERVICE_START state signifies that no ready notification
661545
+                                         * has been received */
661545
+                                        if (f != SERVICE_SUCCESS)
661545
+                                                service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
661545
+                                        else if (!s->remain_after_exit)
661545
+                                                /* The service has never been active */
661545
+                                                service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_PROTOCOL);
661545
+                                        break;
661545
                                 }
661545
 
661545
                                 /* Fall through */