chiark / gitweb /
efi: read microseconds from boot loader info instead of "ticks" magic
[elogind.git] / src / shared / cgroup-util.c
index 9dfab2eaacf8d9a8b5584127cbd29a763f620b02..acace52bc8daaa7ab2299e85f2108fec27e63762 100644 (file)
@@ -36,6 +36,7 @@
 #include "util.h"
 #include "path-util.h"
 #include "strv.h"
+#include "unit-name.h"
 
 int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) {
         char *fs;
@@ -989,6 +990,8 @@ int cg_split_spec(const char *spec, char **controller, char **path) {
         assert(spec);
 
         if (*spec == '/') {
+                if (!path_is_safe(spec))
+                        return -EINVAL;
 
                 if (path) {
                         t = strdup(spec);
@@ -1006,7 +1009,7 @@ int cg_split_spec(const char *spec, char **controller, char **path) {
 
         e = strchr(spec, ':');
         if (!e) {
-                if (strchr(spec, '/') || spec[0] == 0)
+                if (!filename_is_safe(spec))
                         return -EINVAL;
 
                 if (controller) {
@@ -1023,29 +1026,34 @@ int cg_split_spec(const char *spec, char **controller, char **path) {
                 return 0;
         }
 
-        if (e[1] != '/' || e == spec || memchr(spec, '/', e-spec))
+        t = strndup(spec, e-spec);
+        if (!t)
+                return -ENOMEM;
+        if (!filename_is_safe(t)) {
+                free(t);
                 return -EINVAL;
-
-        if (controller) {
-                t = strndup(spec, e-spec);
-                if (!t)
-                        return -ENOMEM;
-
         }
 
-        if (path) {
-                u = strdup(e+1);
-                if (!u) {
-                        free(t);
-                        return -ENOMEM;
-                }
+        u = strdup(e+1);
+        if (!u) {
+                free(t);
+                return -ENOMEM;
+        }
+        if (!path_is_safe(u)) {
+                free(t);
+                free(u);
+                return -EINVAL;
         }
 
         if (controller)
                 *controller = t;
+        else
+                free(t);
 
         if (path)
                 *path = u;
+        else
+                free(u);
 
         return 0;
 }
@@ -1210,10 +1218,63 @@ int cg_pid_get_cgroup(pid_t pid, char **root, char **cgroup) {
         return 0;
 }
 
-int cg_pid_get_unit(pid_t pid, char **unit) {
+static int instance_unit_from_cgroup(char *cgroup){
+        char *at;
+
+        assert(cgroup);
+
+        at = strstr(cgroup, "@.");
+        if (at) {
+                /* This is a templated service */
+
+                char *i;
+                char _cleanup_free_ *i2 = NULL, *s = NULL;
+
+                i = strchr(at, '/');
+                if (!i || !i[1]) /* disallow empty instances */
+                        return -EINVAL;
+
+                s = strndup(at + 1, i - at - 1);
+                i2 = strdup(i + 1);
+                if (!s || !i2)
+                        return -ENOMEM;
+
+                strcpy(at + 1, i2);
+                strcat(at + 1, s);
+        }
+
+        return 0;
+}
+
+/* non-static only for testing purposes */
+int cgroup_to_unit(char *cgroup, char **unit){
         int r;
-        char *cgroup, *p, *at, *b;
-        size_t k;
+        char *p;
+
+        assert(cgroup);
+        assert(unit);
+
+        r = instance_unit_from_cgroup(cgroup);
+        if (r < 0)
+                return r;
+
+        p = strrchr(cgroup, '/');
+        assert(p);
+
+        r = unit_name_is_valid(p + 1, true);
+        if (!r)
+                return -EINVAL;
+
+        *unit = strdup(p + 1);
+        if (!*unit)
+                return -ENOMEM;
+
+        return 0;
+}
+
+static int cg_pid_get(const char *prefix, pid_t pid, char **unit) {
+        int r;
+        char _cleanup_free_ *cgroup = NULL;
 
         assert(pid >= 0);
         assert(unit);
@@ -1222,43 +1283,46 @@ int cg_pid_get_unit(pid_t pid, char **unit) {
         if (r < 0)
                 return r;
 
-        if (!startswith(cgroup, "/system/")) {
-                free(cgroup);
+        if (!startswith(cgroup, prefix))
                 return -ENOENT;
-        }
 
-        p = cgroup + 8;
-        k = strcspn(p, "/");
+        r = cgroup_to_unit(cgroup, unit);
+        return r;
+}
 
-        at = memchr(p, '@', k);
-        if (at && at[1] == '.') {
-                size_t j;
+int cg_pid_get_unit(pid_t pid, char **unit) {
+        return cg_pid_get("/system/", pid, unit);
+}
 
-                /* This is a templated service */
-                if (p[k] != '/') {
-                        free(cgroup);
-                        return -EIO;
-                }
+int cg_pid_get_user_unit(pid_t pid, char **unit) {
+        return cg_pid_get("/user/", pid, unit);
+}
 
-                j = strcspn(p+k+1, "/");
+int cg_controller_from_attr(const char *attr, char **controller) {
+        const char *dot;
+        char *c;
 
-                b = malloc(k + j + 1);
+        assert(attr);
+        assert(controller);
 
-                if (b) {
-                        memcpy(b, p, at - p + 1);
-                        memcpy(b + (at - p) + 1, p + k + 1, j);
-                        memcpy(b + (at - p) + 1 + j, at + 1, k - (at - p) - 1);
-                        b[k+j] = 0;
-                }
-        } else
-                  b = strndup(p, k);
+        if (!filename_is_safe(attr))
+                return -EINVAL;
 
-        free(cgroup);
+        dot = strchr(attr, '.');
+        if (!dot) {
+                *controller = NULL;
+                return 0;
+        }
 
-        if (!b)
+        c = strndup(attr, dot - attr);
+        if (!c)
                 return -ENOMEM;
 
-        *unit = b;
-        return 0;
+        if (!filename_is_safe(c)) {
+                free(c);
+                return -EINVAL;
+        }
 
+        *controller = c;
+        return 1;
 }