]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/core/target.c
tree-wide: drop _pure_ + _const_ from local, static functions
[thirdparty/systemd.git] / src / core / target.c
index 421a304c73da421b14a962ab2cb35e962e23a248..8f2a331f92b9df10cfb1867bb4efb78315df91c3 100644 (file)
@@ -1,4 +1,4 @@
-/* SPDX-License-Identifier: LGPL-2.1+ */
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
 #include "dbus-target.h"
 #include "dbus-unit.h"
@@ -31,40 +31,35 @@ static void target_set_state(Target *t, TargetState state) {
                           target_state_to_string(old_state),
                           target_state_to_string(state));
 
-        unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], 0);
+        unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], /* reload_success = */ true);
 }
 
 static int target_add_default_dependencies(Target *t) {
-
-        static const UnitDependency deps[] = {
-                UNIT_REQUIRES,
-                UNIT_REQUISITE,
-                UNIT_WANTS,
-                UNIT_BINDS_TO,
-                UNIT_PART_OF
-        };
-
-        int r;
-        unsigned k;
+        _cleanup_free_ Unit **others = NULL;
+        int r, n_others;
 
         assert(t);
 
         if (!UNIT(t)->default_dependencies)
                 return 0;
 
-        /* Imply ordering for requirement dependencies on target units. Note that when the user created a contradicting
-         * ordering manually we won't add anything in here to make sure we don't create a loop. */
-
-        for (k = 0; k < ELEMENTSOF(deps); k++) {
-                Unit *other;
-                Iterator i;
-                void *v;
-
-                HASHMAP_FOREACH_KEY(v, other, UNIT(t)->dependencies[deps[k]], i) {
-                        r = unit_add_default_target_dependency(other, UNIT(t));
-                        if (r < 0)
-                                return r;
-                }
+        /* Imply ordering for requirement dependencies on target units. Note that when the user created a
+         * contradicting ordering manually we won't add anything in here to make sure we don't create a
+         * loop.
+         *
+         * Note that quite likely iterating through these dependencies will add new dependencies, which
+         * conflicts with the hashmap-based iteration logic. Hence, instead of iterating through the
+         * dependencies and acting on them as we go, first take an "atomic snapshot" of sorts and iterate
+         * through that. */
+
+        n_others = unit_get_dependency_array(UNIT(t), UNIT_ATOM_ADD_DEFAULT_TARGET_DEPENDENCY_QUEUE, &others);
+        if (n_others < 0)
+                return n_others;
+
+        for (int i = 0; i < n_others; i++) {
+                r = unit_add_default_target_dependency(others[i], UNIT(t));
+                if (r < 0)
+                        return r;
         }
 
         if (unit_has_name(UNIT(t), SPECIAL_SHUTDOWN_TARGET))
@@ -80,18 +75,15 @@ static int target_load(Unit *u) {
 
         assert(t);
 
-        r = unit_load_fragment_and_dropin(u);
+        r = unit_load_fragment_and_dropin(u, true);
         if (r < 0)
                 return r;
 
-        /* This is a new unit? Then let's add in some extras */
-        if (u->load_state == UNIT_LOADED) {
-                r = target_add_default_dependencies(t);
-                if (r < 0)
-                        return r;
-        }
+        if (u->load_state != UNIT_LOADED)
+                return 0;
 
-        return 0;
+        /* This is a new unit? Then let's add in some extras */
+        return target_add_default_dependencies(t);
 }
 
 static int target_coldplug(Unit *u) {
@@ -156,6 +148,7 @@ static int target_serialize(Unit *u, FILE *f, FDSet *fds) {
 static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
         Target *s = TARGET(u);
 
+        assert(s);
         assert(u);
         assert(key);
         assert(value);
@@ -176,13 +169,13 @@ static int target_deserialize_item(Unit *u, const char *key, const char *value,
         return 0;
 }
 
-_pure_ static UnitActiveState target_active_state(Unit *u) {
+static UnitActiveState target_active_state(Unit *u) {
         assert(u);
 
         return state_translation_table[TARGET(u)->state];
 }
 
-_pure_ static const char *target_sub_state_to_string(Unit *u) {
+static const char *target_sub_state_to_string(Unit *u) {
         assert(u);
 
         return target_state_to_string(TARGET(u)->state);
@@ -196,6 +189,8 @@ const UnitVTable target_vtable = {
                 "Target\0"
                 "Install\0",
 
+        .can_fail = true,
+
         .load = target_load,
         .coldplug = target_coldplug,
 
@@ -210,8 +205,6 @@ const UnitVTable target_vtable = {
         .active_state = target_active_state,
         .sub_state_to_string = target_sub_state_to_string,
 
-        .bus_vtable = bus_target_vtable,
-
         .status_message_formats = {
                 .finished_start_job = {
                         [JOB_DONE]       = "Reached target %s.",