149adf
From 71c1119e2fe9bc57e0efb07f784f616a98763028 Mon Sep 17 00:00:00 2001
149adf
From: =?UTF-8?q?Renaud=20M=C3=A9trich?= <rmetrich@redhat.com>
149adf
Date: Fri, 2 Oct 2020 17:30:35 +0200
149adf
Subject: [PATCH] unit: don't emit PropertiesChanged signal if adding a
149adf
 dependency to a unit is a no-op
149adf
149adf
(cherry-picked from commit 5177cb0a9add4ae568cff6e6f7c2b3c77760c343)
149adf
149adf
Related: #1793527
149adf
---
149adf
 src/core/unit.c | 16 +++++++++++++---
149adf
 1 file changed, 13 insertions(+), 3 deletions(-)
149adf
149adf
diff --git a/src/core/unit.c b/src/core/unit.c
149adf
index 18b1b898fd..e07c34bfc5 100644
149adf
--- a/src/core/unit.c
149adf
+++ b/src/core/unit.c
149adf
@@ -2260,6 +2260,9 @@ int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_referen
149adf
         };
149adf
         int r, q = 0, v = 0, w = 0;
149adf
         Unit *orig_u = u, *orig_other = other;
149adf
+        /* Helper to know whether sending a notification is necessary or not:
149adf
+         * if the dependency is already there, no need to notify! */
149adf
+        bool noop = true;
149adf
 
149adf
         assert(u);
149adf
         assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
149adf
@@ -2298,13 +2301,16 @@ int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_referen
149adf
         q = set_put(u->dependencies[d], other);
149adf
         if (q < 0)
149adf
                 return q;
149adf
+        else if (q > 0)
149adf
+                noop = false;
149adf
 
149adf
         if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID && inverse_table[d] != d) {
149adf
                 v = set_put(other->dependencies[inverse_table[d]], u);
149adf
                 if (v < 0) {
149adf
                         r = v;
149adf
                         goto fail;
149adf
-                }
149adf
+                } else if (v > 0)
149adf
+                        noop = false;
149adf
         }
149adf
 
149adf
         if (add_reference) {
149adf
@@ -2312,14 +2318,18 @@ int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_referen
149adf
                 if (w < 0) {
149adf
                         r = w;
149adf
                         goto fail;
149adf
-                }
149adf
+                } else if (w > 0)
149adf
+                        noop = false;
149adf
 
149adf
                 r = set_put(other->dependencies[UNIT_REFERENCED_BY], u);
149adf
                 if (r < 0)
149adf
                         goto fail;
149adf
+                else if (r > 0)
149adf
+                        noop = false;
149adf
         }
149adf
 
149adf
-        unit_add_to_dbus_queue(u);
149adf
+        if (!noop)
149adf
+                unit_add_to_dbus_queue(u);
149adf
         return 0;
149adf
 
149adf
 fail: