valeriyvdovin / rpms / systemd

Forked from rpms/systemd 4 years ago
Clone

Blame SOURCES/0378-core-split-out-all-logic-that-updates-a-Job-on-a-uni.patch

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