ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
923a60
From ff80bfd94181327a5f8e0fbd70b9b7afe0c5545c 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 13:12:43 +0100
923a60
Subject: [PATCH] pid1: include the source unit in UnitRef
923a60
923a60
No functional change.
923a60
923a60
The source unit manages the reference. It allocates the UnitRef structure and
923a60
registers it in the target unit, and then the reference must be destroyed
923a60
before the source unit is destroyed. Thus, is should be OK to include the
923a60
pointer to the source unit, it should be live as long as the reference exists.
923a60
923a60
v2:
923a60
- rename refs to refs_by_target
923a60
923a60
(cherry picked from commit 7f7d01ed5804afef220ebdb29f22d8177d0d3a5c)
923a60
923a60
Related: #1718953
923a60
---
923a60
 src/core/busname.c       |  2 +-
923a60
 src/core/dbus-manager.c  |  2 +-
923a60
 src/core/dbus-unit.c     |  2 +-
923a60
 src/core/load-fragment.c |  6 +++---
923a60
 src/core/service.c       |  2 +-
923a60
 src/core/slice.c         |  2 +-
923a60
 src/core/socket.c        |  4 ++--
923a60
 src/core/unit.c          | 35 ++++++++++++++++++-----------------
923a60
 src/core/unit.h          | 12 ++++++------
923a60
 9 files changed, 34 insertions(+), 33 deletions(-)
923a60
923a60
diff --git a/src/core/busname.c b/src/core/busname.c
923a60
index a5e659049d..97886f1e05 100644
923a60
--- a/src/core/busname.c
923a60
+++ b/src/core/busname.c
923a60
@@ -175,7 +175,7 @@ static int busname_add_extras(BusName *n) {
923a60
                         if (r < 0)
923a60
                                 return r;
923a60
 
923a60
-                        unit_ref_set(&n->service, x);
923a60
+                        unit_ref_set(&n->service, u, x);
923a60
                 }
923a60
 
923a60
                 r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(n->service), true);
923a60
diff --git a/src/core/dbus-manager.c b/src/core/dbus-manager.c
923a60
index 1766163b33..8267d44e1a 100644
923a60
--- a/src/core/dbus-manager.c
923a60
+++ b/src/core/dbus-manager.c
923a60
@@ -649,7 +649,7 @@ static int transient_unit_from_message(
923a60
             u->fragment_path ||
923a60
             u->source_path ||
923a60
             !strv_isempty(u->dropin_paths) ||
923a60
-            u->refs ||
923a60
+            u->refs_by_target ||
923a60
             set_size(u->dependencies[UNIT_REFERENCED_BY]) > 0)
923a60
                 return sd_bus_error_setf(error, BUS_ERROR_UNIT_EXISTS, "Unit %s already exists.", name);
923a60
 
923a60
diff --git a/src/core/dbus-unit.c b/src/core/dbus-unit.c
923a60
index f0f75e01b0..77073308c8 100644
923a60
--- a/src/core/dbus-unit.c
923a60
+++ b/src/core/dbus-unit.c
923a60
@@ -967,7 +967,7 @@ static int bus_unit_set_transient_property(
923a60
                                 return -EINVAL;
923a60
 
923a60
                         if (mode != UNIT_CHECK) {
923a60
-                                unit_ref_set(&u->slice, slice);
923a60
+                                unit_ref_set(&u->slice, u, slice);
923a60
                                 unit_write_drop_in_private_format(u, mode, name, "Slice=%s\n", s);
923a60
                         }
923a60
                 }
923a60
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
923a60
index f3d0851fe2..1721fea8f3 100644
923a60
--- a/src/core/load-fragment.c
923a60
+++ b/src/core/load-fragment.c
923a60
@@ -1836,7 +1836,7 @@ int config_parse_socket_service(
923a60
                 return 0;
923a60
         }
923a60
 
923a60
-        unit_ref_set(&s->service, x);
923a60
+        unit_ref_set(&s->service, UNIT(s), x);
923a60
 
923a60
         return 0;
923a60
 }
923a60
@@ -2006,7 +2006,7 @@ int config_parse_busname_service(
923a60
                 return 0;
923a60
         }
923a60
 
923a60
-        unit_ref_set(&n->service, x);
923a60
+        unit_ref_set(&n->service, UNIT(n), x);
923a60
 
923a60
         return 0;
923a60
 }
923a60
@@ -2933,7 +2933,7 @@ int config_parse_unit_slice(
923a60
                 return 0;
923a60
         }
923a60
 
923a60
-        unit_ref_set(&u->slice, slice);
923a60
+        unit_ref_set(&u->slice, u, slice);
923a60
         return 0;
923a60
 }
923a60
 
923a60
diff --git a/src/core/service.c b/src/core/service.c
923a60
index 69ec916f2d..eaa588863f 100644
923a60
--- a/src/core/service.c
923a60
+++ b/src/core/service.c
923a60
@@ -3299,7 +3299,7 @@ int service_set_socket_fd(Service *s, int fd, Socket *sock, bool selinux_context
923a60
         s->socket_fd = fd;
923a60
         s->socket_fd_selinux_context_net = selinux_context_net;
923a60
 
923a60
-        unit_ref_set(&s->accept_socket, UNIT(sock));
923a60
+        unit_ref_set(&s->accept_socket, UNIT(s), UNIT(sock));
923a60
 
923a60
         return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
923a60
 }
923a60
diff --git a/src/core/slice.c b/src/core/slice.c
923a60
index 1cce3e1217..0985a65286 100644
923a60
--- a/src/core/slice.c
923a60
+++ b/src/core/slice.c
923a60
@@ -76,7 +76,7 @@ static int slice_add_parent_slice(Slice *s) {
923a60
         if (r < 0)
923a60
                 return r;
923a60
 
923a60
-        unit_ref_set(&UNIT(s)->slice, parent);
923a60
+        unit_ref_set(&UNIT(s)->slice, UNIT(s), parent);
923a60
         return 0;
923a60
 }
923a60
 
923a60
diff --git a/src/core/socket.c b/src/core/socket.c
923a60
index 3e4cdd467f..8489575de6 100644
923a60
--- a/src/core/socket.c
923a60
+++ b/src/core/socket.c
923a60
@@ -208,7 +208,7 @@ int socket_instantiate_service(Socket *s) {
923a60
                 return r;
923a60
 
923a60
         u->no_gc = true;
923a60
-        unit_ref_set(&s->service, u);
923a60
+        unit_ref_set(&s->service, UNIT(s), u);
923a60
 
923a60
         return unit_add_two_dependencies(UNIT(s), UNIT_BEFORE, UNIT_TRIGGERS, u, false);
923a60
 }
923a60
@@ -313,7 +313,7 @@ static int socket_add_extras(Socket *s) {
923a60
                         if (r < 0)
923a60
                                 return r;
923a60
 
923a60
-                        unit_ref_set(&s->service, x);
923a60
+                        unit_ref_set(&s->service, u, x);
923a60
                 }
923a60
 
923a60
                 r = unit_add_two_dependencies(u, UNIT_BEFORE, UNIT_TRIGGERS, UNIT_DEREF(s->service), true);
923a60
diff --git a/src/core/unit.c b/src/core/unit.c
923a60
index 1b8ec9a20e..5376ef862f 100644
923a60
--- a/src/core/unit.c
923a60
+++ b/src/core/unit.c
923a60
@@ -315,7 +315,7 @@ bool unit_may_gc(Unit *u) {
923a60
         if (u->no_gc)
923a60
                 return false;
923a60
 
923a60
-        if (u->refs)
923a60
+        if (u->refs_by_target)
923a60
                 return false;
923a60
 
923a60
         if (UNIT_VTABLE(u)->may_gc && !UNIT_VTABLE(u)->may_gc(u))
923a60
@@ -553,9 +553,8 @@ void unit_free(Unit *u) {
923a60
         condition_free_list(u->asserts);
923a60
 
923a60
         unit_ref_unset(&u->slice);
923a60
-
923a60
-        while (u->refs)
923a60
-                unit_ref_unset(u->refs);
923a60
+        while (u->refs_by_target)
923a60
+                unit_ref_unset(u->refs_by_target);
923a60
 
923a60
         free(u);
923a60
 }
923a60
@@ -737,8 +736,8 @@ int unit_merge(Unit *u, Unit *other) {
923a60
                 return r;
923a60
 
923a60
         /* Redirect all references */
923a60
-        while (other->refs)
923a60
-                unit_ref_set(other->refs, u);
923a60
+        while (other->refs_by_target)
923a60
+                unit_ref_set(other->refs_by_target, other->refs_by_target->source, u);
923a60
 
923a60
         /* Merge dependencies */
923a60
         for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
923a60
@@ -2493,7 +2492,7 @@ int unit_add_default_slice(Unit *u, CGroupContext *c) {
923a60
         if (r < 0)
923a60
                 return r;
923a60
 
923a60
-        unit_ref_set(&u->slice, slice);
923a60
+        unit_ref_set(&u->slice, u, slice);
923a60
         return 0;
923a60
 }
923a60
 
923a60
@@ -3130,30 +3129,32 @@ int unit_get_unit_file_preset(Unit *u) {
923a60
         return u->unit_file_preset;
923a60
 }
923a60
 
923a60
-Unit* unit_ref_set(UnitRef *ref, Unit *u) {
923a60
+Unit* unit_ref_set(UnitRef *ref, Unit *source, Unit *target) {
923a60
         assert(ref);
923a60
-        assert(u);
923a60
+        assert(source);
923a60
+        assert(target);
923a60
 
923a60
-        if (ref->unit)
923a60
+        if (ref->target)
923a60
                 unit_ref_unset(ref);
923a60
 
923a60
-        ref->unit = u;
923a60
-        LIST_PREPEND(refs, u->refs, ref);
923a60
-        return u;
923a60
+        ref->source = source;
923a60
+        ref->target = target;
923a60
+        LIST_PREPEND(refs_by_target, target->refs_by_target, ref);
923a60
+        return target;
923a60
 }
923a60
 
923a60
 void unit_ref_unset(UnitRef *ref) {
923a60
         assert(ref);
923a60
 
923a60
-        if (!ref->unit)
923a60
+        if (!ref->target)
923a60
                 return;
923a60
 
923a60
         /* We are about to drop a reference to the unit, make sure the garbage collection has a look at it as it might
923a60
          * be unreferenced now. */
923a60
-        unit_add_to_gc_queue(ref->unit);
923a60
+        unit_add_to_gc_queue(ref->target);
923a60
 
923a60
-        LIST_REMOVE(refs, ref->unit->refs, ref);
923a60
-        ref->unit = NULL;
923a60
+        LIST_REMOVE(refs_by_target, ref->target->refs_by_target, ref);
923a60
+        ref->source = ref->target = NULL;
923a60
 }
923a60
 
923a60
 int unit_patch_contexts(Unit *u) {
923a60
diff --git a/src/core/unit.h b/src/core/unit.h
923a60
index 3f411a1793..a6e21d60ce 100644
923a60
--- a/src/core/unit.h
923a60
+++ b/src/core/unit.h
923a60
@@ -84,8 +84,8 @@ struct UnitRef {
923a60
          * that we can merge two units if necessary and correct all
923a60
          * references to them */
923a60
 
923a60
-        Unit* unit;
923a60
-        LIST_FIELDS(UnitRef, refs);
923a60
+        Unit *source, *target;
923a60
+        LIST_FIELDS(UnitRef, refs_by_target);
923a60
 };
923a60
 
923a60
 struct Unit {
923a60
@@ -125,7 +125,7 @@ struct Unit {
923a60
         char *job_timeout_reboot_arg;
923a60
 
923a60
         /* References to this */
923a60
-        LIST_HEAD(UnitRef, refs);
923a60
+        LIST_HEAD(UnitRef, refs_by_target);
923a60
 
923a60
         /* Conditions to check */
923a60
         LIST_HEAD(Condition, conditions);
923a60
@@ -591,11 +591,11 @@ void unit_trigger_notify(Unit *u);
923a60
 UnitFileState unit_get_unit_file_state(Unit *u);
923a60
 int unit_get_unit_file_preset(Unit *u);
923a60
 
923a60
-Unit* unit_ref_set(UnitRef *ref, Unit *u);
923a60
+Unit* unit_ref_set(UnitRef *ref, Unit *source, Unit *target);
923a60
 void unit_ref_unset(UnitRef *ref);
923a60
 
923a60
-#define UNIT_DEREF(ref) ((ref).unit)
923a60
-#define UNIT_ISSET(ref) (!!(ref).unit)
923a60
+#define UNIT_DEREF(ref) ((ref).target)
923a60
+#define UNIT_ISSET(ref) (!!(ref).target)
923a60
 
923a60
 int unit_patch_contexts(Unit *u);
923a60