ac3a84
From 82e78c509c9eab33c1dbf2bc445f91b67b2f118f Mon Sep 17 00:00:00 2001
ac3a84
From: Yu Watanabe <watanabe.yu+github@gmail.com>
ac3a84
Date: Wed, 16 Nov 2022 03:08:22 +0900
ac3a84
Subject: [PATCH] core/unit: drop dependency to the unit being merged
ac3a84
ac3a84
Fixes a bug in 15ed3c3a188cf7fa5a60ae508fc7a3ed048d2220.
ac3a84
ac3a84
Fixes #24990. Also, hopefully fixes #24577.
ac3a84
ac3a84
(cherry picked from commit c8b3b524134539846917269ddd644ee93a35623f)
ac3a84
ac3a84
Related: #2160477
ac3a84
---
ac3a84
 src/core/unit.c | 21 +++++++++++++--------
ac3a84
 1 file changed, 13 insertions(+), 8 deletions(-)
ac3a84
ac3a84
diff --git a/src/core/unit.c b/src/core/unit.c
ac3a84
index 4cde5c4b4e..ae9f688ad2 100644
ac3a84
--- a/src/core/unit.c
ac3a84
+++ b/src/core/unit.c
ac3a84
@@ -1043,10 +1043,10 @@ static int unit_add_dependency_hashmap(
ac3a84
         return unit_per_dependency_type_hashmap_update(per_type, other, origin_mask, destination_mask);
ac3a84
 }
ac3a84
 
ac3a84
-static void unit_merge_dependencies(
ac3a84
-                Unit *u,
ac3a84
-                Unit *other) {
ac3a84
-
ac3a84
+static void unit_merge_dependencies(Unit *u, Unit *other) {
ac3a84
+        Hashmap *deps;
ac3a84
+        void *dt; /* Actually of type UnitDependency, except that we don't bother casting it here,
ac3a84
+                   * since the hashmaps all want it as void pointer. */
ac3a84
         int r;
ac3a84
 
ac3a84
         assert(u);
ac3a84
@@ -1055,12 +1055,19 @@ static void unit_merge_dependencies(
ac3a84
         if (u == other)
ac3a84
                 return;
ac3a84
 
ac3a84
+        /* First, remove dependency to other. */
ac3a84
+        HASHMAP_FOREACH_KEY(deps, dt, u->dependencies) {
ac3a84
+                if (hashmap_remove(deps, other))
ac3a84
+                        unit_maybe_warn_about_dependency(u, other->id, UNIT_DEPENDENCY_FROM_PTR(dt));
ac3a84
+
ac3a84
+                if (hashmap_isempty(deps))
ac3a84
+                        hashmap_free(hashmap_remove(u->dependencies, dt));
ac3a84
+        }
ac3a84
+
ac3a84
         for (;;) {
ac3a84
                 _cleanup_(hashmap_freep) Hashmap *other_deps = NULL;
ac3a84
                 UnitDependencyInfo di_back;
ac3a84
                 Unit *back;
ac3a84
-                void *dt; /* Actually of type UnitDependency, except that we don't bother casting it here,
ac3a84
-                           * since the hashmaps all want it as void pointer. */
ac3a84
 
ac3a84
                 /* Let's focus on one dependency type at a time, that 'other' has defined. */
ac3a84
                 other_deps = hashmap_steal_first_key_and_value(other->dependencies, &dt);
ac3a84
@@ -1102,8 +1109,6 @@ static void unit_merge_dependencies(
ac3a84
                  * them per type wholesale. */
ac3a84
                 r = hashmap_put(u->dependencies, dt, other_deps);
ac3a84
                 if (r == -EEXIST) {
ac3a84
-                        Hashmap *deps;
ac3a84
-
ac3a84
                         /* The target unit already has dependencies of this type, let's then merge this individually. */
ac3a84
 
ac3a84
                         assert_se(deps = hashmap_get(u->dependencies, dt));