naccyde / rpms / systemd

Forked from rpms/systemd a year ago
Clone
923a60
From 703cc4991049cdf3ad3506e432cda982b3b3b007 Mon Sep 17 00:00:00 2001
923a60
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
923a60
Date: Tue, 13 Feb 2018 14:37:11 +0100
923a60
Subject: [PATCH] pid1: fix collection of cycles of units which reference one
923a60
 another
923a60
923a60
A .socket will reference a .service unit, by registering a UnitRef with the
923a60
.service unit. If this .service unit has the .socket unit listed in Wants or
923a60
Sockets or such, a cycle will be created. We would not free this cycle
923a60
properly, because we treated any unit with non-empty refs as uncollectable. To
923a60
solve this issue, treats refs with UnitRef in u->refs_by_target similarly to
923a60
the refs in u->dependencies, and check if the "other" unit is known to be
923a60
needed. If it is not needed, do not treat the reference from it as preventing
923a60
the unit we are looking at from being freed.
923a60
923a60
(cherry picked from commit 2641f02e23ac7d5385db7f932aff221a063f245e)
923a60
923a60
Resolves: #1718953
923a60
---
923a60
 src/core/manager.c | 14 ++++++++++++++
923a60
 src/core/unit.c    |  9 +++++----
923a60
 2 files changed, 19 insertions(+), 4 deletions(-)
923a60
923a60
diff --git a/src/core/manager.c b/src/core/manager.c
923a60
index 9dfdd67860..fdbb3c0fd9 100644
923a60
--- a/src/core/manager.c
923a60
+++ b/src/core/manager.c
923a60
@@ -888,6 +888,20 @@ static void unit_gc_sweep(Unit *u, unsigned gc_marker) {
923a60
                         is_bad = false;
923a60
         }
923a60
 
923a60
+        if (u->refs_by_target) {
923a60
+                const UnitRef *ref;
923a60
+
923a60
+                LIST_FOREACH(refs_by_target, ref, u->refs_by_target) {
923a60
+                        unit_gc_sweep(ref->source, gc_marker);
923a60
+
923a60
+                        if (ref->source->gc_marker == gc_marker + GC_OFFSET_GOOD)
923a60
+                                goto good;
923a60
+
923a60
+                        if (ref->source->gc_marker != gc_marker + GC_OFFSET_BAD)
923a60
+                                is_bad = false;
923a60
+                }
923a60
+        }
923a60
+
923a60
         if (is_bad)
923a60
                 goto bad;
923a60
 
923a60
diff --git a/src/core/unit.c b/src/core/unit.c
923a60
index 5376ef862f..2204be26d2 100644
923a60
--- a/src/core/unit.c
923a60
+++ b/src/core/unit.c
923a60
@@ -288,7 +288,11 @@ bool unit_may_gc(Unit *u) {
923a60
 
923a60
         /* Checks whether the unit is ready to be unloaded for garbage collection.
923a60
          * Returns true when the unit may be collected, and false if there's some
923a60
-         * reason to keep it loaded. */
923a60
+         * reason to keep it loaded.
923a60
+         *
923a60
+         * References from other units are *not* checked here. Instead, this is done
923a60
+         * in unit_gc_sweep(), but using markers to properly collect dependency loops.
923a60
+         */
923a60
 
923a60
         if (u->job)
923a60
                 return false;
923a60
@@ -315,9 +319,6 @@ bool unit_may_gc(Unit *u) {
923a60
         if (u->no_gc)
923a60
                 return false;
923a60
 
923a60
-        if (u->refs_by_target)
923a60
-                return false;
923a60
-
923a60
         if (UNIT_VTABLE(u)->may_gc && !UNIT_VTABLE(u)->may_gc(u))
923a60
                 return false;
923a60