803fb7
From 77f4e582d0f381391594e6f8a7b6767d572d96f7 Mon Sep 17 00:00:00 2001
803fb7
From: Lennart Poettering <lennart@poettering.net>
803fb7
Date: Tue, 19 May 2015 18:13:22 +0200
803fb7
Subject: [PATCH] core: when propagating restart requests due to deps,
803fb7
 downgrade restart to try-restart
803fb7
803fb7
Previously, if a service A depended on a service B via Requires=, and A
803fb7
was not running and B restarted this would trigger a start of A as well,
803fb7
since the restart was propagated as restart independently of the state
803fb7
of A.
803fb7
803fb7
This patch ensures that a restart of B would be propagated as a
803fb7
try-restart to A, thus not changing its state if it isn't up.
803fb7
803fb7
http://lists.freedesktop.org/archives/systemd-devel/2015-May/032061.html
803fb7
(cherry picked from commit c6497ccb7153af9a1252c48918e380b5134314de)
803fb7
803fb7
Resolves: #1436021
803fb7
---
803fb7
 src/core/job.c         | 28 ++++++++++++++--------------
803fb7
 src/core/job.h         |  2 +-
803fb7
 src/core/manager.c     |  2 +-
803fb7
 src/core/transaction.c | 11 ++++++++---
803fb7
 4 files changed, 24 insertions(+), 19 deletions(-)
803fb7
803fb7
diff --git a/src/core/job.c b/src/core/job.c
803fb7
index 703286496..1617e24c0 100644
803fb7
--- a/src/core/job.c
803fb7
+++ b/src/core/job.c
803fb7
@@ -392,38 +392,38 @@ bool job_type_is_redundant(JobType a, UnitActiveState b) {
803fb7
         }
803fb7
 }
803fb7
 
803fb7
-void job_type_collapse(JobType *t, Unit *u) {
803fb7
+JobType job_type_collapse(JobType t, Unit *u) {
803fb7
         UnitActiveState s;
803fb7
 
803fb7
-        switch (*t) {
803fb7
+        switch (t) {
803fb7
 
803fb7
         case JOB_TRY_RESTART:
803fb7
                 s = unit_active_state(u);
803fb7
                 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s))
803fb7
-                        *t = JOB_NOP;
803fb7
-                else
803fb7
-                        *t = JOB_RESTART;
803fb7
-                break;
803fb7
+                        return JOB_NOP;
803fb7
+
803fb7
+                return JOB_RESTART;
803fb7
 
803fb7
         case JOB_RELOAD_OR_START:
803fb7
                 s = unit_active_state(u);
803fb7
                 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(s))
803fb7
-                        *t = JOB_START;
803fb7
-                else
803fb7
-                        *t = JOB_RELOAD;
803fb7
-                break;
803fb7
+                        return JOB_START;
803fb7
+
803fb7
+                return JOB_RELOAD;
803fb7
 
803fb7
         default:
803fb7
-                ;
803fb7
+                return t;
803fb7
         }
803fb7
 }
803fb7
 
803fb7
 int job_type_merge_and_collapse(JobType *a, JobType b, Unit *u) {
803fb7
-        JobType t = job_type_lookup_merge(*a, b);
803fb7
+        JobType t;
803fb7
+
803fb7
+        t = job_type_lookup_merge(*a, b);
803fb7
         if (t < 0)
803fb7
                 return -EEXIST;
803fb7
-        *a = t;
803fb7
-        job_type_collapse(a, u);
803fb7
+
803fb7
+        *a = job_type_collapse(t, u);
803fb7
         return 0;
803fb7
 }
803fb7
 
803fb7
diff --git a/src/core/job.h b/src/core/job.h
803fb7
index e4191ee77..ce81607de 100644
803fb7
--- a/src/core/job.h
803fb7
+++ b/src/core/job.h
803fb7
@@ -210,7 +210,7 @@ bool job_type_is_redundant(JobType a, UnitActiveState b) _pure_;
803fb7
 
803fb7
 /* Collapses a state-dependent job type into a simpler type by observing
803fb7
  * the state of the unit which it is going to be applied to. */
803fb7
-void job_type_collapse(JobType *t, Unit *u);
803fb7
+JobType job_type_collapse(JobType t, Unit *u);
803fb7
 
803fb7
 int job_type_merge_and_collapse(JobType *a, JobType b, Unit *u);
803fb7
 
803fb7
diff --git a/src/core/manager.c b/src/core/manager.c
803fb7
index 8bd80e687..287cf6a74 100644
803fb7
--- a/src/core/manager.c
803fb7
+++ b/src/core/manager.c
803fb7
@@ -1303,7 +1303,7 @@ int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool ove
803fb7
                        "Trying to enqueue job %s/%s/%s", unit->id,
803fb7
                        job_type_to_string(type), job_mode_to_string(mode));
803fb7
 
803fb7
-        job_type_collapse(&type, unit);
803fb7
+        type = job_type_collapse(type, unit);
803fb7
 
803fb7
         tr = transaction_new(mode == JOB_REPLACE_IRREVERSIBLY);
803fb7
         if (!tr)
803fb7
diff --git a/src/core/transaction.c b/src/core/transaction.c
803fb7
index 428b7671b..34df15718 100644
803fb7
--- a/src/core/transaction.c
803fb7
+++ b/src/core/transaction.c
803fb7
@@ -855,8 +855,7 @@ int transaction_add_job_and_dependencies(
803fb7
         /*           by ? job_type_to_string(by->type) : "NA"); */
803fb7
 
803fb7
         if (!IN_SET(unit->load_state, UNIT_LOADED, UNIT_ERROR, UNIT_NOT_FOUND, UNIT_MASKED))
803fb7
-                return sd_bus_error_setf(e, BUS_ERROR_LOAD_FAILED,
803fb7
-                                         "Unit %s is not loaded properly.", unit->id);
803fb7
+                return sd_bus_error_setf(e, BUS_ERROR_LOAD_FAILED, "Unit %s is not loaded properly.", unit->id);
803fb7
 
803fb7
         if (type != JOB_STOP) {
803fb7
                 r = bus_unit_check_load_state(unit, e);
803fb7
@@ -1014,12 +1013,18 @@ int transaction_add_job_and_dependencies(
803fb7
                                 UNIT_CONSISTS_OF,
803fb7
                         };
803fb7
 
803fb7
+                        JobType ptype;
803fb7
                         unsigned j;
803fb7
 
803fb7
+                        /* We propagate STOP as STOP, but RESTART only
803fb7
+                         * as TRY_RESTART, in order not to start
803fb7
+                         * dependencies that are not around. */
803fb7
+                        ptype = type == JOB_RESTART ? JOB_TRY_RESTART : type;
803fb7
+
803fb7
                         for (j = 0; j < ELEMENTSOF(propagate_deps); j++)
803fb7
                                 SET_FOREACH(dep, ret->unit->dependencies[propagate_deps[j]], i) {
803fb7
 
803fb7
-                                        r = transaction_add_job_and_dependencies(tr, type, dep, ret, true, override, false, false, ignore_order, e);
803fb7
+                                        r = transaction_add_job_and_dependencies(tr, job_type_collapse(ptype, dep), dep, ret, true, override, false, false, ignore_order, e);
803fb7
                                         if (r < 0) {
803fb7
                                                 if (r != -EBADR)
803fb7
                                                         goto fail;