richardphibel / rpms / systemd

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