naccyde / rpms / systemd

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