Blob Blame History Raw
From 883e46f2e4255b92a338c9d4004a8b4740cdbcaf Mon Sep 17 00:00:00 2001
From: Yu Watanabe <watanabe.yu+github@gmail.com>
Date: Tue, 15 Nov 2022 22:59:01 +0900
Subject: [PATCH] core/unit: fix log message

As you can see in the below, the dropped dependency Before=issue-24990.service
is not logged, but the dependency Before=test1.service which is not owned by
the units generated by the TEST-26 is logged.

Before:
systemd[1]: issue-24990.service: Dependency After=test1.service dropped, merged into issue-24990.service
systemd[1]: issue-24990.service: Dependency Before=test1.service dropped, merged into issue-24990.service

After:
systemd[1]: issue-24990.service: Dependency After=test1.service is dropped, as test1.service is merged into issue-24990.service.
systemd[1]: issue-24990.service: Dependency Before=issue-24990.service in test1.service is dropped, as test1.service is merged into issue-24990.service.

(cherry picked from commit ed9911630e4bca844381d7caeb850dad9a9fa122)

Related: #2160477
---
 src/core/unit.c | 49 ++++++++++++++++++++++---------------------------
 1 file changed, 22 insertions(+), 27 deletions(-)

diff --git a/src/core/unit.c b/src/core/unit.c
index d1929bbf69..c319e99d71 100644
--- a/src/core/unit.c
+++ b/src/core/unit.c
@@ -936,29 +936,17 @@ static int unit_reserve_dependencies(Unit *u, Unit *other) {
         return 0;
 }
 
-static void unit_maybe_warn_about_dependency(
-                Unit *u,
-                const char *other_id,
-                UnitDependency dependency) {
-
-        assert(u);
-
+static bool unit_should_warn_about_dependency(UnitDependency dependency) {
         /* Only warn about some unit types */
-        if (!IN_SET(dependency,
-                    UNIT_CONFLICTS,
-                    UNIT_CONFLICTED_BY,
-                    UNIT_BEFORE,
-                    UNIT_AFTER,
-                    UNIT_ON_SUCCESS,
-                    UNIT_ON_FAILURE,
-                    UNIT_TRIGGERS,
-                    UNIT_TRIGGERED_BY))
-                return;
-
-        if (streq_ptr(u->id, other_id))
-                log_unit_warning(u, "Dependency %s=%s dropped", unit_dependency_to_string(dependency), u->id);
-        else
-                log_unit_warning(u, "Dependency %s=%s dropped, merged into %s", unit_dependency_to_string(dependency), strna(other_id), u->id);
+        return IN_SET(dependency,
+                      UNIT_CONFLICTS,
+                      UNIT_CONFLICTED_BY,
+                      UNIT_BEFORE,
+                      UNIT_AFTER,
+                      UNIT_ON_SUCCESS,
+                      UNIT_ON_FAILURE,
+                      UNIT_TRIGGERS,
+                      UNIT_TRIGGERED_BY);
 }
 
 static int unit_per_dependency_type_hashmap_update(
@@ -1056,8 +1044,10 @@ static void unit_merge_dependencies(Unit *u, Unit *other) {
 
         /* First, remove dependency to other. */
         HASHMAP_FOREACH_KEY(deps, dt, u->dependencies) {
-                if (hashmap_remove(deps, other))
-                        unit_maybe_warn_about_dependency(u, other->id, UNIT_DEPENDENCY_FROM_PTR(dt));
+                if (hashmap_remove(deps, other) && unit_should_warn_about_dependency(UNIT_DEPENDENCY_FROM_PTR(dt)))
+                        log_unit_warning(u, "Dependency %s=%s is dropped, as %s is merged into %s.",
+                                         unit_dependency_to_string(UNIT_DEPENDENCY_FROM_PTR(dt)),
+                                         other->id, other->id, u->id);
 
                 if (hashmap_isempty(deps))
                         hashmap_free(hashmap_remove(u->dependencies, dt));
@@ -1084,7 +1074,11 @@ static void unit_merge_dependencies(Unit *u, Unit *other) {
                         if (back == u) {
                                 /* This is a dependency pointing back to the unit we want to merge with?
                                  * Suppress it (but warn) */
-                                unit_maybe_warn_about_dependency(u, other->id, UNIT_DEPENDENCY_FROM_PTR(dt));
+                                if (unit_should_warn_about_dependency(UNIT_DEPENDENCY_FROM_PTR(dt)))
+                                        log_unit_warning(u, "Dependency %s=%s in %s is dropped, as %s is merged into %s.",
+                                                         unit_dependency_to_string(UNIT_DEPENDENCY_FROM_PTR(dt)),
+                                                         u->id, other->id, other->id, u->id);
+
                                 hashmap_remove(other_deps, back);
                                 continue;
                         }
@@ -3049,7 +3043,6 @@ int unit_add_dependency(
                 [UNIT_IN_SLICE]               = UNIT_SLICE_OF,
                 [UNIT_SLICE_OF]               = UNIT_IN_SLICE,
         };
-        Unit *original_u = u, *original_other = other;
         UnitDependencyAtom a;
         int r;
 
@@ -3068,7 +3061,9 @@ int unit_add_dependency(
 
         /* We won't allow dependencies on ourselves. We will not consider them an error however. */
         if (u == other) {
-                unit_maybe_warn_about_dependency(original_u, original_other->id, d);
+                if (unit_should_warn_about_dependency(d))
+                        log_unit_warning(u, "Dependency %s=%s is dropped.",
+                                         unit_dependency_to_string(d), u->id);
                 return 0;
         }