summaryrefslogtreecommitdiff
path: root/src/core/unit.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2015-08-28 17:36:39 +0200
committerLennart Poettering <lennart@poettering.net>2015-08-31 13:20:43 +0200
commitd79200e26ee39d4b451f95e876fc4595df51fe51 (patch)
tree189d23d4acbd47ad7ab0c4711c1eb087bf07f3be /src/core/unit.c
parent35b7ff80e29524cb01f881ca6d52c669970c88f1 (diff)
unit: unify how we assing slices to units
This adds a new call unit_set_slice(), and simplifies unit_add_default_slice(). THis should make our code a bit more robust and simpler.
Diffstat (limited to 'src/core/unit.c')
-rw-r--r--src/core/unit.c35
1 files changed, 31 insertions, 4 deletions
diff --git a/src/core/unit.c b/src/core/unit.c
index 2ad49fd50b..9afb5736d7 100644
--- a/src/core/unit.c
+++ b/src/core/unit.c
@@ -2424,14 +2424,42 @@ char *unit_default_cgroup_path(Unit *u) {
return strjoin(u->manager->cgroup_root, "/", escaped, NULL);
}
-int unit_add_default_slice(Unit *u, CGroupContext *c) {
+int unit_set_slice(Unit *u, Unit *slice) {
+ assert(u);
+ assert(slice);
+
+ /* Sets the unit slice if it has not been set before. Is extra
+ * careful, to only allow this for units that actually have a
+ * cgroup context. Also, we don't allow to set this for slices
+ * (since the parent slice is derived from the name). Make
+ * sure the unit we set is actually a slice. */
+
+ if (!UNIT_HAS_CGROUP_CONTEXT(u))
+ return -EOPNOTSUPP;
+
+ if (u->type == UNIT_SLICE)
+ return -EINVAL;
+
+ if (slice->type != UNIT_SLICE)
+ return -EINVAL;
+
+ if (UNIT_DEREF(u->slice) == slice)
+ return 0;
+
+ if (UNIT_ISSET(u->slice))
+ return -EBUSY;
+
+ unit_ref_set(&u->slice, slice);
+ return 1;
+}
+
+int unit_set_default_slice(Unit *u) {
_cleanup_free_ char *b = NULL;
const char *slice_name;
Unit *slice;
int r;
assert(u);
- assert(c);
if (UNIT_ISSET(u->slice))
return 0;
@@ -2471,8 +2499,7 @@ int unit_add_default_slice(Unit *u, CGroupContext *c) {
if (r < 0)
return r;
- unit_ref_set(&u->slice, slice);
- return 0;
+ return unit_set_slice(u, slice);
}
const char *unit_slice_name(Unit *u) {