chiark / gitweb /
cgroup: simplify how instantiated units are mapped to cgroups
authorLennart Poettering <lennart@poettering.net>
Thu, 11 Jul 2013 16:47:20 +0000 (18:47 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 11 Jul 2013 16:49:53 +0000 (18:49 +0200)
Previously for an instantiated unit foo@bar.service we created a cgroup
foo@.service/foo@bar.service, in order to place all instances of the
same template inside the same subtree. As we now implicitly add all
instantiated units into one per-template slice we don't need this
complexity anymore, and instance units can map directly to the cgroups
of their full name.

src/core/unit.c
src/shared/cgroup-util.c
src/test/test-cgroup-util.c

index bfde08d..8645285 100644 (file)
@@ -1991,7 +1991,7 @@ char *unit_dbus_path(Unit *u) {
 }
 
 char *unit_default_cgroup_path(Unit *u) {
-        _cleanup_free_ char *escaped_instance = NULL, *slice = NULL;
+        _cleanup_free_ char *escaped = NULL, *slice = NULL;
         int r;
 
         assert(u);
@@ -2005,28 +2005,14 @@ char *unit_default_cgroup_path(Unit *u) {
                         return NULL;
         }
 
-        escaped_instance = cg_escape(u->id);
-        if (!escaped_instance)
+        escaped = cg_escape(u->id);
+        if (!escaped)
                 return NULL;
 
-        if (u->instance) {
-                _cleanup_free_ char *t = NULL, *escaped_template = NULL;
-
-                t = unit_name_template(u->id);
-                if (!t)
-                        return NULL;
-
-                escaped_template = cg_escape(t);
-                if (!escaped_template)
-                        return NULL;
-
-                return strjoin(u->manager->cgroup_root, "/",
-                               slice ? slice : "", slice ? "/" : "",
-                               escaped_template, "/", escaped_instance, NULL);
-        } else
-                return strjoin(u->manager->cgroup_root, "/",
-                               slice ? slice : "", slice ? "/" : "",
-                               escaped_instance, NULL);
+        if (slice)
+                return strjoin(u->manager->cgroup_root, "/", slice, "/", escaped, NULL);
+        else
+                return strjoin(u->manager->cgroup_root, "/", escaped, NULL);
 }
 
 int unit_add_default_slice(Unit *u) {
index 8f3c64f..1d545e0 100644 (file)
@@ -1126,7 +1126,7 @@ int cg_pid_get_path_shifted(pid_t pid, char **root, char **cgroup) {
 }
 
 int cg_path_decode_unit(const char *cgroup, char **unit){
-        char *p, *e, *c, *s, *k;
+        char *e, *c, *s;
 
         assert(cgroup);
         assert(unit);
@@ -1135,28 +1135,10 @@ int cg_path_decode_unit(const char *cgroup, char **unit){
         c = strndupa(cgroup, e - cgroup);
         c = cg_unescape(c);
 
-        /* Could this be a valid unit name? */
-        if (!unit_name_is_valid(c, true))
+        if (!unit_name_is_valid(c, false))
                 return -EINVAL;
 
-        if (!unit_name_is_template(c))
-                s = strdup(c);
-        else {
-                if (*e != '/')
-                        return -EINVAL;
-
-                e += strspn(e, "/");
-
-                p = strchrnul(e, '/');
-                k = strndupa(e, p - e);
-                k = cg_unescape(k);
-
-                if (!unit_name_is_valid(k, false))
-                        return -EINVAL;
-
-                s = strdup(k);
-        }
-
+        s = strdup(c);
         if (!s)
                 return -ENOMEM;
 
index ed2c6ae..295bb02 100644 (file)
@@ -32,12 +32,15 @@ static void check_p_d_u(const char *path, int code, const char *result) {
 }
 
 static void test_path_decode_unit(void) {
-        check_p_d_u("getty@.service/getty@tty2.service", 0, "getty@tty2.service");
-        check_p_d_u("getty@.service/getty@tty2.service/xxx", 0, "getty@tty2.service");
+        check_p_d_u("getty@tty2.service", 0, "getty@tty2.service");
+        check_p_d_u("getty@tty2.service/", 0, "getty@tty2.service");
+        check_p_d_u("getty@tty2.service/xxx", 0, "getty@tty2.service");
         check_p_d_u("getty@.service/", -EINVAL, NULL);
         check_p_d_u("getty@.service", -EINVAL, NULL);
         check_p_d_u("getty.service", 0, "getty.service");
         check_p_d_u("getty", -EINVAL, NULL);
+        check_p_d_u("getty/waldo", -EINVAL, NULL);
+        check_p_d_u("_cpu.service", 0, "cpu.service");
 }
 
 static void check_p_g_u(const char *path, int code, const char *result) {
@@ -49,13 +52,14 @@ static void check_p_g_u(const char *path, int code, const char *result) {
 
 static void test_path_get_unit(void) {
         check_p_g_u("/system.slice/foobar.service/sdfdsaf", 0, "foobar.service");
-        check_p_g_u("/system.slice/getty@.service/getty@tty5.service", 0, "getty@tty5.service");
-        check_p_g_u("/system.slice/getty@.service/getty@tty5.service/aaa/bbb", 0, "getty@tty5.service");
-        check_p_g_u("/system.slice/getty@.service/getty@tty5.service/", 0, "getty@tty5.service");
+        check_p_g_u("/system.slice/getty@tty5.service", 0, "getty@tty5.service");
+        check_p_g_u("/system.slice/getty@tty5.service/aaa/bbb", 0, "getty@tty5.service");
+        check_p_g_u("/system.slice/getty@tty5.service/", 0, "getty@tty5.service");
         check_p_g_u("/system.slice/getty@tty6.service/tty5", 0, "getty@tty6.service");
         check_p_g_u("sadfdsafsda", -EINVAL, NULL);
-        check_p_g_u("/system.slice/getty####@tty6.service/tty5", -EINVAL, NULL);
+        check_p_g_u("/system.slice/getty####@tty6.service/xxx", -EINVAL, NULL);
         check_p_g_u("/system.slice/system-waldo.slice/foobar.service/sdfdsaf", 0, "foobar.service");
+        check_p_g_u("/system.slice/system-waldo.slice/_cpu.service/sdfdsaf", 0, "cpu.service");
 }
 
 static void check_p_g_u_u(const char *path, int code, const char *result) {
@@ -71,10 +75,11 @@ static void test_path_get_user_unit(void) {
         check_p_g_u_u("/user.slice/user-1002.slice/session-2.scope/foobar.service/waldo", 0, "foobar.service");
         check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar.service/waldo/uuuux", 0, "foobar.service");
         check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/waldo/waldo/uuuux", -EINVAL, NULL);
-        check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar@.service/foobar@pie.service/pa/po", 0, "foobar@pie.service");
-        check_p_g_u_u("/session-2.scope/foobar@.service/foobar@pie.service/pa/po", 0, "foobar@pie.service");
-        check_p_g_u_u("/xyz.slice/xyz-waldo.slice/session-77.scope/foobar@.service/foobar@pie.service/pa/po", 0, "foobar@pie.service");
+        check_p_g_u_u("/user.slice/user-1000.slice/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
+        check_p_g_u_u("/session-2.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
+        check_p_g_u_u("/xyz.slice/xyz-waldo.slice/session-77.scope/foobar@pie.service/pa/po", 0, "foobar@pie.service");
         check_p_g_u_u("/meh.service", -ENOENT, NULL);
+        check_p_g_u_u("/session-3.scope/_cpu.service", 0, "cpu.service");
 }
 
 static void check_p_g_s(const char *path, int code, const char *result) {