chiark / gitweb /
target: add default unit ordering deps from the unit not the target
[elogind.git] / src / unit.c
index bca4d97993c9acae1411e6aed0530aaa5c1d53b2..d28a0a8cf7637b461ebbe8334bc6f82a22e7ce46 100644 (file)
@@ -725,6 +725,42 @@ int unit_load_fragment_and_dropin_optional(Unit *u) {
         return 0;
 }
 
+static int unit_add_one_default_dependency(Unit *u, Unit *target) {
+        assert(u);
+        assert(target);
+
+        if (target->meta.type != UNIT_TARGET)
+                return 0;
+
+        /* Don't create loops */
+        if (set_get(target->meta.dependencies[UNIT_BEFORE], u))
+                return 0;
+
+        return unit_add_dependency(target, UNIT_AFTER, u, true);
+}
+
+static int unit_add_default_dependencies(Unit *u) {
+        Unit *other;
+        Iterator i;
+        int r;
+
+        assert(u);
+
+        SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
+                if ((r = unit_add_one_default_dependency(u, other)) < 0)
+                        return r;
+
+        SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY_OVERRIDABLE], i)
+                if ((r = unit_add_one_default_dependency(u, other)) < 0)
+                        return r;
+
+        SET_FOREACH(other, u->meta.dependencies[UNIT_WANTED_BY], i)
+                if ((r = unit_add_one_default_dependency(u, other)) < 0)
+                        return r;
+
+        return 0;
+}
+
 int unit_load(Unit *u) {
         int r;
 
@@ -750,6 +786,11 @@ int unit_load(Unit *u) {
                 goto fail;
         }
 
+        if (u->meta.load_state == UNIT_LOADED &&
+            u->meta.default_dependencies)
+                if ((r = unit_add_default_dependencies(u)) < 0)
+                        goto fail;
+
         assert((u->meta.load_state != UNIT_MERGED) == !u->meta.merged_into);
 
         unit_add_to_dbus_queue(unit_follow_merge(u));
@@ -1084,7 +1125,7 @@ void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns) {
          * something is already activated. */
 
         if (unexpected && u->meta.manager->n_deserializing <= 0) {
-                if (UNIT_IS_INACTIVE_OR_DEACTIVATING(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns))
+                if (UNIT_IS_INACTIVE_OR_FAILED(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns))
                         retroactively_start_dependencies(u);
                 else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns))
                         retroactively_stop_dependencies(u);
@@ -2125,6 +2166,20 @@ Unit *unit_following(Unit *u) {
         return NULL;
 }
 
+bool unit_pending_inactive(Unit *u) {
+        assert(u);
+
+        /* Returns true if the unit is inactive or going down */
+
+        if (UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)))
+                return true;
+
+        if (u->meta.job && u->meta.job->type == JOB_STOP)
+                return true;
+
+        return false;
+}
+
 static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
         [UNIT_STUB] = "stub",
         [UNIT_LOADED] = "loaded",