[systemd-devel] [PATCH 1/2] systemd: added new dependency StoppedBy

Michal Sekletar msekleta at redhat.com
Tue Jul 3 03:04:00 PDT 2012


This should address TODO item "new dependency type to "group" services
in a target". Semantic of new dependency is as follows. Once configured
it creates dependency which will cause that all dependent units get
stopped if unit they all depend on is stopped or restarted.
Usual use case would be configuring StoppedBy=some.target in template unit
file and WantedBy=some.target in [Install] section and enabling desired
number of instances. In this case starting one instance won't pull in target
but stopping or starting target(in case of WantedBy is properly configured)
will cause stop/start of all instances.
---
 src/core/load-fragment-gperf.gperf.m4 |    1 +
 src/core/transaction.c                |   12 ++++++++++++
 src/core/unit.c                       |    3 ++-
 src/core/unit.h                       |    2 ++
 4 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/src/core/load-fragment-gperf.gperf.m4 b/src/core/load-fragment-gperf.gperf.m4
index f5e9b70..6824b5d 100644
--- a/src/core/load-fragment-gperf.gperf.m4
+++ b/src/core/load-fragment-gperf.gperf.m4
@@ -105,6 +105,7 @@ Unit.After,                      config_parse_unit_deps,             UNIT_AFTER,
 Unit.OnFailure,                  config_parse_unit_deps,             UNIT_ON_FAILURE,               0
 Unit.PropagateReloadTo,          config_parse_unit_deps,             UNIT_PROPAGATE_RELOAD_TO,      0
 Unit.PropagateReloadFrom,        config_parse_unit_deps,             UNIT_PROPAGATE_RELOAD_FROM,    0
+Unit.StoppedBy,                  config_parse_unit_deps,             UNIT_STOPPED_BY,               0
 Unit.RequiresMountsFor,          config_parse_unit_requires_mounts_for, 0,                          offsetof(Unit, requires_mounts_for)
 Unit.StopWhenUnneeded,           config_parse_bool,                  0,                             offsetof(Unit, stop_when_unneeded)
 Unit.RefuseManualStart,          config_parse_bool,                  0,                             offsetof(Unit, refuse_manual_start)
diff --git a/src/core/transaction.c b/src/core/transaction.c
index 44fdc06..b82fbb0 100644
--- a/src/core/transaction.c
+++ b/src/core/transaction.c
@@ -994,6 +994,18 @@ int transaction_add_job_and_dependencies(
                                                 dbus_error_free(e);
                                 }
                         }
+
+                        SET_FOREACH(dep, ret->unit->dependencies[UNIT_STOPS], i) {
+                                r = transaction_add_job_and_dependencies(tr, type, dep, ret, true, override, false, false, ignore_order, e);
+                                if (r < 0) {
+                                        if (r != -EBADR)
+                                                goto fail;
+
+                                        if (e)
+                                                dbus_error_free(e);
+                                }
+                        }
+
                 }
 
                 if (type == JOB_RELOAD) {
diff --git a/src/core/unit.c b/src/core/unit.c
index 64e26b7..6d10413 100644
--- a/src/core/unit.c
+++ b/src/core/unit.c
@@ -1610,7 +1610,8 @@ int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_referen
                 [UNIT_TRIGGERS] = UNIT_TRIGGERED_BY,
                 [UNIT_TRIGGERED_BY] = UNIT_TRIGGERS,
                 [UNIT_PROPAGATE_RELOAD_TO] = UNIT_PROPAGATE_RELOAD_FROM,
-                [UNIT_PROPAGATE_RELOAD_FROM] = UNIT_PROPAGATE_RELOAD_TO
+                [UNIT_PROPAGATE_RELOAD_FROM] = UNIT_PROPAGATE_RELOAD_TO,
+                [UNIT_STOPPED_BY] = UNIT_STOPS
         };
         int r, q = 0, v = 0, w = 0;
 
diff --git a/src/core/unit.h b/src/core/unit.h
index cfb38d0..d4cf2eb 100644
--- a/src/core/unit.h
+++ b/src/core/unit.h
@@ -102,12 +102,14 @@ enum UnitDependency {
         UNIT_REQUISITE_OVERRIDABLE,
         UNIT_WANTS,
         UNIT_BIND_TO,
+        UNIT_STOPPED_BY,
 
         /* Inverse of the above */
         UNIT_REQUIRED_BY,             /* inverse of 'requires' and 'requisite' is 'required_by' */
         UNIT_REQUIRED_BY_OVERRIDABLE, /* inverse of 'requires_overridable' and 'requisite_overridable' is 'soft_required_by' */
         UNIT_WANTED_BY,               /* inverse of 'wants' */
         UNIT_BOUND_BY,                /* inverse of 'bind_to' */
+        UNIT_STOPS,                   /* inverse of 'stopped_by' */
 
         /* Negative dependencies */
         UNIT_CONFLICTS,               /* inverse of 'conflicts' is 'conflicted_by' */
-- 
1.7.10.4



More information about the systemd-devel mailing list