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