c2dfb7
From 81c3f90d41c973a18e157c1106926711815adc0e Mon Sep 17 00:00:00 2001
c2dfb7
From: Lennart Poettering <lennart@poettering.net>
c2dfb7
Date: Mon, 10 Dec 2018 20:56:57 +0100
c2dfb7
Subject: [PATCH] core: split out all logic that updates a Job on a unit's
c2dfb7
 unit_notify() invocation
c2dfb7
c2dfb7
Just some refactoring, no change in behaviour.
c2dfb7
c2dfb7
(cherry picked from commit 16c74914d233ec93012d77e5f93cf90e42939669)
c2dfb7
c2dfb7
Related: #1737283
c2dfb7
---
c2dfb7
 src/core/unit.c | 136 +++++++++++++++++++++++++-----------------------
c2dfb7
 1 file changed, 71 insertions(+), 65 deletions(-)
c2dfb7
c2dfb7
diff --git a/src/core/unit.c b/src/core/unit.c
c2dfb7
index f5e251123d..a4865c1da5 100644
c2dfb7
--- a/src/core/unit.c
c2dfb7
+++ b/src/core/unit.c
c2dfb7
@@ -2225,6 +2225,73 @@ static void unit_update_on_console(Unit *u) {
c2dfb7
                 manager_unref_console(u->manager);
c2dfb7
 }
c2dfb7
 
c2dfb7
+static bool unit_process_job(Job *j, UnitActiveState ns, UnitNotifyFlags flags) {
c2dfb7
+        bool unexpected = false;
c2dfb7
+
c2dfb7
+        assert(j);
c2dfb7
+
c2dfb7
+        if (j->state == JOB_WAITING)
c2dfb7
+
c2dfb7
+                /* So we reached a different state for this job. Let's see if we can run it now if it failed previously
c2dfb7
+                 * due to EAGAIN. */
c2dfb7
+                job_add_to_run_queue(j);
c2dfb7
+
c2dfb7
+        /* Let's check whether the unit's new state constitutes a finished job, or maybe contradicts a running job and
c2dfb7
+         * hence needs to invalidate jobs. */
c2dfb7
+
c2dfb7
+        switch (j->type) {
c2dfb7
+
c2dfb7
+        case JOB_START:
c2dfb7
+        case JOB_VERIFY_ACTIVE:
c2dfb7
+
c2dfb7
+                if (UNIT_IS_ACTIVE_OR_RELOADING(ns))
c2dfb7
+                        job_finish_and_invalidate(j, JOB_DONE, true, false);
c2dfb7
+                else if (j->state == JOB_RUNNING && ns != UNIT_ACTIVATING) {
c2dfb7
+                        unexpected = true;
c2dfb7
+
c2dfb7
+                        if (UNIT_IS_INACTIVE_OR_FAILED(ns))
c2dfb7
+                                job_finish_and_invalidate(j, ns == UNIT_FAILED ? JOB_FAILED : JOB_DONE, true, false);
c2dfb7
+                }
c2dfb7
+
c2dfb7
+                break;
c2dfb7
+
c2dfb7
+        case JOB_RELOAD:
c2dfb7
+        case JOB_RELOAD_OR_START:
c2dfb7
+        case JOB_TRY_RELOAD:
c2dfb7
+
c2dfb7
+                if (j->state == JOB_RUNNING) {
c2dfb7
+                        if (ns == UNIT_ACTIVE)
c2dfb7
+                                job_finish_and_invalidate(j, (flags & UNIT_NOTIFY_RELOAD_FAILURE) ? JOB_FAILED : JOB_DONE, true, false);
c2dfb7
+                        else if (!IN_SET(ns, UNIT_ACTIVATING, UNIT_RELOADING)) {
c2dfb7
+                                unexpected = true;
c2dfb7
+
c2dfb7
+                                if (UNIT_IS_INACTIVE_OR_FAILED(ns))
c2dfb7
+                                        job_finish_and_invalidate(j, ns == UNIT_FAILED ? JOB_FAILED : JOB_DONE, true, false);
c2dfb7
+                        }
c2dfb7
+                }
c2dfb7
+
c2dfb7
+                break;
c2dfb7
+
c2dfb7
+        case JOB_STOP:
c2dfb7
+        case JOB_RESTART:
c2dfb7
+        case JOB_TRY_RESTART:
c2dfb7
+
c2dfb7
+                if (UNIT_IS_INACTIVE_OR_FAILED(ns))
c2dfb7
+                        job_finish_and_invalidate(j, JOB_DONE, true, false);
c2dfb7
+                else if (j->state == JOB_RUNNING && ns != UNIT_DEACTIVATING) {
c2dfb7
+                        unexpected = true;
c2dfb7
+                        job_finish_and_invalidate(j, JOB_FAILED, true, false);
c2dfb7
+                }
c2dfb7
+
c2dfb7
+                break;
c2dfb7
+
c2dfb7
+        default:
c2dfb7
+                assert_not_reached("Job type unknown");
c2dfb7
+        }
c2dfb7
+
c2dfb7
+        return unexpected;
c2dfb7
+}
c2dfb7
+
c2dfb7
 void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, UnitNotifyFlags flags) {
c2dfb7
         bool unexpected;
c2dfb7
         Manager *m;
c2dfb7
@@ -2265,71 +2332,10 @@ void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, UnitNotifyFlag
c2dfb7
 
c2dfb7
         unit_update_on_console(u);
c2dfb7
 
c2dfb7
-        if (u->job) {
c2dfb7
-                unexpected = false;
c2dfb7
-
c2dfb7
-                if (u->job->state == JOB_WAITING)
c2dfb7
-
c2dfb7
-                        /* So we reached a different state for this
c2dfb7
-                         * job. Let's see if we can run it now if it
c2dfb7
-                         * failed previously due to EAGAIN. */
c2dfb7
-                        job_add_to_run_queue(u->job);
c2dfb7
-
c2dfb7
-                /* Let's check whether this state change constitutes a
c2dfb7
-                 * finished job, or maybe contradicts a running job and
c2dfb7
-                 * hence needs to invalidate jobs. */
c2dfb7
-
c2dfb7
-                switch (u->job->type) {
c2dfb7
-
c2dfb7
-                case JOB_START:
c2dfb7
-                case JOB_VERIFY_ACTIVE:
c2dfb7
-
c2dfb7
-                        if (UNIT_IS_ACTIVE_OR_RELOADING(ns))
c2dfb7
-                                job_finish_and_invalidate(u->job, JOB_DONE, true, false);
c2dfb7
-                        else if (u->job->state == JOB_RUNNING && ns != UNIT_ACTIVATING) {
c2dfb7
-                                unexpected = true;
c2dfb7
-
c2dfb7
-                                if (UNIT_IS_INACTIVE_OR_FAILED(ns))
c2dfb7
-                                        job_finish_and_invalidate(u->job, ns == UNIT_FAILED ? JOB_FAILED : JOB_DONE, true, false);
c2dfb7
-                        }
c2dfb7
-
c2dfb7
-                        break;
c2dfb7
-
c2dfb7
-                case JOB_RELOAD:
c2dfb7
-                case JOB_RELOAD_OR_START:
c2dfb7
-                case JOB_TRY_RELOAD:
c2dfb7
-
c2dfb7
-                        if (u->job->state == JOB_RUNNING) {
c2dfb7
-                                if (ns == UNIT_ACTIVE)
c2dfb7
-                                        job_finish_and_invalidate(u->job, (flags & UNIT_NOTIFY_RELOAD_FAILURE) ? JOB_FAILED : JOB_DONE, true, false);
c2dfb7
-                                else if (!IN_SET(ns, UNIT_ACTIVATING, UNIT_RELOADING)) {
c2dfb7
-                                        unexpected = true;
c2dfb7
-
c2dfb7
-                                        if (UNIT_IS_INACTIVE_OR_FAILED(ns))
c2dfb7
-                                                job_finish_and_invalidate(u->job, ns == UNIT_FAILED ? JOB_FAILED : JOB_DONE, true, false);
c2dfb7
-                                }
c2dfb7
-                        }
c2dfb7
-
c2dfb7
-                        break;
c2dfb7
-
c2dfb7
-                case JOB_STOP:
c2dfb7
-                case JOB_RESTART:
c2dfb7
-                case JOB_TRY_RESTART:
c2dfb7
-
c2dfb7
-                        if (UNIT_IS_INACTIVE_OR_FAILED(ns))
c2dfb7
-                                job_finish_and_invalidate(u->job, JOB_DONE, true, false);
c2dfb7
-                        else if (u->job->state == JOB_RUNNING && ns != UNIT_DEACTIVATING) {
c2dfb7
-                                unexpected = true;
c2dfb7
-                                job_finish_and_invalidate(u->job, JOB_FAILED, true, false);
c2dfb7
-                        }
c2dfb7
-
c2dfb7
-                        break;
c2dfb7
-
c2dfb7
-                default:
c2dfb7
-                        assert_not_reached("Job type unknown");
c2dfb7
-                }
c2dfb7
-
c2dfb7
-        } else
c2dfb7
+        /* Let's propagate state changes to the job */
c2dfb7
+        if (u->job)
c2dfb7
+                unexpected = unit_process_job(u->job, ns, flags);
c2dfb7
+        else
c2dfb7
                 unexpected = true;
c2dfb7
 
c2dfb7
         if (!MANAGER_IS_RELOADING(m)) {