chiark / gitweb /
do not change console to non-unicode for LANG=C
[elogind.git] / src / shared / cgroup-util.c
index f0d0d4855b28cfc38d4d6f1cfdd9e38ad4d812bb..075260783cb001fca0753fc3cdd302d1f6e2f070 100644 (file)
@@ -37,6 +37,7 @@
 #include "path-util.h"
 #include "strv.h"
 #include "unit-name.h"
+#include "fileio.h"
 
 int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) {
         char *fs;
@@ -676,9 +677,9 @@ int cg_delete(const char *controller, const char *path) {
 }
 
 int cg_attach(const char *controller, const char *path, pid_t pid) {
-        char *fs;
+        _cleanup_free_ char *fs = NULL;
+        char c[DECIMAL_STR_MAX(pid_t) + 2];
         int r;
-        char c[32];
 
         assert(controller);
         assert(path);
@@ -692,16 +693,18 @@ int cg_attach(const char *controller, const char *path, pid_t pid) {
                 pid = getpid();
 
         snprintf(c, sizeof(c), "%lu\n", (unsigned long) pid);
-        char_array_0(c);
-
-        r = write_one_line_file(fs, c);
-        free(fs);
 
-        return r;
+        return write_string_file(fs, c);
 }
 
-int cg_set_group_access(const char *controller, const char *path, mode_t mode, uid_t uid, gid_t gid) {
-        char *fs;
+int cg_set_group_access(
+                const char *controller,
+                const char *path,
+                mode_t mode,
+                uid_t uid,
+                gid_t gid) {
+
+        _cleanup_free_ char *fs = NULL;
         int r;
 
         assert(controller);
@@ -714,14 +717,18 @@ int cg_set_group_access(const char *controller, const char *path, mode_t mode, u
         if (r < 0)
                 return r;
 
-        r = chmod_and_chown(fs, mode, uid, gid);
-        free(fs);
-
-        return r;
+        return chmod_and_chown(fs, mode, uid, gid);
 }
 
-int cg_set_task_access(const char *controller, const char *path, mode_t mode, uid_t uid, gid_t gid, int sticky) {
-        char *fs;
+int cg_set_task_access(
+                const char *controller,
+                const char *path,
+                mode_t mode,
+                uid_t uid,
+                gid_t gid,
+                int sticky) {
+
+        _cleanup_free_ char *fs = NULL, *procs = NULL;
         int r;
 
         assert(controller);
@@ -748,10 +755,8 @@ int cg_set_task_access(const char *controller, const char *path, mode_t mode, ui
                  * mode from the file itself */
 
                 r = lstat(fs, &st);
-                if (r < 0) {
-                        free(fs);
+                if (r < 0)
                         return -errno;
-                }
 
                 if (mode == (mode_t) -1)
                         /* No mode set, we just shall set the sticky bit */
@@ -762,9 +767,15 @@ int cg_set_task_access(const char *controller, const char *path, mode_t mode, ui
         }
 
         r = chmod_and_chown(fs, mode, uid, gid);
-        free(fs);
+        if (r < 0)
+                return r;
 
-        return r;
+        /* Always keep values for "cgroup.procs" in sync with "tasks" */
+        r = cg_get_path(controller, path, "cgroup.procs", &procs);
+        if (r < 0)
+                return r;
+
+        return chmod_and_chown(procs, mode, uid, gid);
 }
 
 int cg_get_by_pid(const char *controller, pid_t pid, char **path) {
@@ -811,7 +822,7 @@ int cg_get_by_pid(const char *controller, pid_t pid, char **path) {
                         continue;
 
                 l++;
-                if (strncmp(l, controller, cs) != 0)
+                if (!strneq(l, controller, cs))
                         continue;
 
                 if (l[cs] != ':')
@@ -856,7 +867,8 @@ int cg_install_release_agent(const char *controller, const char *agent) {
                         goto finish;
                 }
 
-                if ((r = write_one_line_file(fs, line)) < 0)
+                r = write_string_file(fs, line);
+                if (r < 0)
                         goto finish;
 
         } else if (!streq(sc, agent)) {
@@ -877,7 +889,7 @@ int cg_install_release_agent(const char *controller, const char *agent) {
         sc = strstrip(contents);
 
         if (streq(sc, "0")) {
-                if ((r = write_one_line_file(fs, "1\n")) < 0)
+                if ((r = write_string_file(fs, "1\n")) < 0)
                         goto finish;
 
                 r = 1;
@@ -990,6 +1002,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);
@@ -1007,7 +1021,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) {
@@ -1024,29 +1038,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;
 }
@@ -1129,8 +1148,6 @@ int cg_get_user_path(char **path) {
 char **cg_shorten_controllers(char **controllers) {
         char **f, **t;
 
-        controllers = strv_uniq(controllers);
-
         if (!controllers)
                 return controllers;
 
@@ -1156,11 +1173,11 @@ char **cg_shorten_controllers(char **controllers) {
         }
 
         *t = NULL;
-        return controllers;
+        return strv_uniq(controllers);
 }
 
 int cg_pid_get_cgroup(pid_t pid, char **root, char **cgroup) {
-        char *cg_process, *cg_init, *p;
+        char *cg_process, *cg_init, *p, *q;
         int r;
 
         assert(pid >= 0);
@@ -1183,11 +1200,8 @@ int cg_pid_get_cgroup(pid_t pid, char **root, char **cgroup) {
         else if (streq(cg_init, "/"))
                 cg_init[0] = 0;
 
-        if (startswith(cg_process, cg_init))
-                p = cg_process + strlen(cg_init);
-        else
-                p = cg_process;
-
+        q = startswith(cg_process, cg_init);
+        p = q ? q : cg_process;
         free(cg_init);
 
         if (cgroup) {
@@ -1211,82 +1225,153 @@ int cg_pid_get_cgroup(pid_t pid, char **root, char **cgroup) {
         return 0;
 }
 
-static int instance_unit_from_cgroup(char *cgroup){
-        char *at;
+/* non-static only for testing purposes */
+int cg_cgroup_to_unit(const char *cgroup, char **unit){
+        char *p, *e, *c, *s, *k;
 
         assert(cgroup);
+        assert(unit);
 
-        at = strstr(cgroup, "@.");
-        if (at) {
-                /* This is a templated service */
+        e = strchrnul(cgroup, '/');
+        c = strndupa(cgroup, e - cgroup);
 
-                char *i;
-                char _cleanup_free_ *i2 = NULL, *s = NULL;
+        /* Could this be a valid unit name? */
+        if (!unit_name_is_valid(c, true))
+                return -EINVAL;
 
-                i = strchr(at, '/');
-                if (!i || !i[1]) /* disallow empty instances */
+        if (!unit_name_is_template(c))
+                s = strdup(c);
+        else {
+                if (*e != '/')
                         return -EINVAL;
 
-                s = strndup(at + 1, i - at - 1);
-                i2 = strdup(i + 1);
-                if (!s || !i2)
-                        return -ENOMEM;
+                e += strspn(e, "/");
+                p = strchrnul(e, '/');
 
-                strcpy(at + 1, i2);
-                strcat(at + 1, s);
+                /* Don't allow empty instance strings */
+                if (p == e)
+                        return -EINVAL;
+
+                k = strndupa(e, p - e);
+
+                s = unit_name_replace_instance(c, k);
         }
 
+        if (!s)
+                return -ENOMEM;
+
+        *unit = s;
         return 0;
 }
 
-/* non-static only for testing purposes */
-int cgroup_to_unit(char *cgroup, char **unit){
+int cg_path_get_unit(const char *path, char **unit) {
+        const char *e;
+
+        assert(path);
+        assert(unit);
+
+        e = path_startswith(path, "/system/");
+        if (!e)
+                return -ENOENT;
+
+        return cg_cgroup_to_unit(e, unit);
+}
+
+int cg_pid_get_unit(pid_t pid, char **unit) {
+        char _cleanup_free_ *cgroup = NULL;
         int r;
-        char *p;
 
-        assert(cgroup);
         assert(unit);
 
-        r = instance_unit_from_cgroup(cgroup);
+        r = cg_pid_get_cgroup(pid, NULL, &cgroup);
         if (r < 0)
                 return r;
 
-        p = strrchr(cgroup, '/');
-        assert(p);
+        return cg_path_get_unit(cgroup, unit);
+}
 
-        r = unit_name_is_valid(p + 1, true);
-        if (!r)
-                return -EINVAL;
+static const char *skip_label(const char *e) {
+        assert(e);
 
-        *unit = strdup(p + 1);
-        if (!*unit)
-                return -ENOMEM;
+        e += strspn(e, "/");
+        e = strchr(e, '/');
+        if (!e)
+                return NULL;
 
-        return 0;
+        e += strspn(e, "/");
+        return e;
 }
 
-static int cg_pid_get(const char *prefix, pid_t pid, char **unit) {
-        int r;
+int cg_path_get_user_unit(const char *path, char **unit) {
+        const char *e;
+
+        assert(path);
+        assert(unit);
+
+        /* We always have to parse the path from the beginning as unit
+         * cgroups might have arbitrary child cgroups and we shouldn't get
+         * confused by those */
+
+        e = path_startswith(path, "/user/");
+        if (!e)
+                return -ENOENT;
+
+        /* Skip the user name */
+        e = skip_label(e);
+        if (!e)
+                return -ENOENT;
+
+        /* Skip the session ID */
+        e = skip_label(e);
+        if (!e)
+                return -ENOENT;
+
+        /* Skip the systemd cgroup */
+        e = skip_label(e);
+        if (!e)
+                return -ENOENT;
+
+        return cg_cgroup_to_unit(e, unit);
+}
+
+int cg_pid_get_user_unit(pid_t pid, char **unit) {
         char _cleanup_free_ *cgroup = NULL;
+        int r;
 
-        assert(pid >= 0);
         assert(unit);
 
         r = cg_pid_get_cgroup(pid, NULL, &cgroup);
         if (r < 0)
                 return r;
 
-        if (!startswith(cgroup, prefix))
-                return -ENOENT;
-
-        r = cgroup_to_unit(cgroup, unit);
-        return r;
+        return cg_path_get_user_unit(cgroup, unit);
 }
 
-int cg_pid_get_unit(pid_t pid, char **unit) {
-        return cg_pid_get("/system/", pid, unit);
-}
+int cg_controller_from_attr(const char *attr, char **controller) {
+        const char *dot;
+        char *c;
 
-int cg_pid_get_user_unit(pid_t pid, char **unit) {
-        return cg_pid_get("/user/", pid, unit);
+        assert(attr);
+        assert(controller);
+
+        if (!filename_is_safe(attr))
+                return -EINVAL;
+
+        dot = strchr(attr, '.');
+        if (!dot) {
+                *controller = NULL;
+                return 0;
+        }
+
+        c = strndup(attr, dot - attr);
+        if (!c)
+                return -ENOMEM;
+
+        if (!filename_is_safe(c)) {
+                free(c);
+                return -EINVAL;
+        }
+
+        *controller = c;
+        return 1;
 }