-int cg_path_get_owner_uid(const char *path, uid_t *uid) {
- _cleanup_free_ char *slice = NULL;
- const char *e;
- char *s;
- int r;
-
- assert(path);
- assert(uid);
-
- r = cg_path_get_slice(path, &slice);
- if (r < 0)
- return r;
-
- e = startswith(slice, "user-");
- if (!e)
- return -ENOENT;
- if (!endswith(slice, ".slice"))
- return -ENOENT;
-
- s = strndupa(e, strlen(e) - 6);
- if (!s)
- return -ENOMEM;
-
- return parse_uid(s, uid);
-}
-
-int cg_pid_get_owner_uid(pid_t pid, uid_t *uid) {
- _cleanup_free_ char *cgroup = NULL;
- int r;
-
- assert(uid);
-
- r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
- if (r < 0)
- return r;
-
- return cg_path_get_owner_uid(cgroup, uid);
-}
-
-int cg_path_get_slice(const char *p, char **slice) {
- const char *e = NULL;
- size_t m = 0;
-
- assert(p);
- assert(slice);
-
- for (;;) {
- size_t n;
-
- p += strspn(p, "/");
-
- n = strcspn(p, "/");
- if (n <= 6 || memcmp(p + n - 6, ".slice", 6) != 0) {
- char *s;
-
- if (!e)
- return -ENOENT;
-
- s = strndup(e, m);
- if (!s)
- return -ENOMEM;
-
- *slice = s;
- return 0;
- }
-
- e = p;
- m = n;
-
- p += n;
- }
-}
-
-int cg_pid_get_slice(pid_t pid, char **slice) {
- _cleanup_free_ char *cgroup = NULL;
- int r;
-
- assert(slice);
-
- r = cg_pid_get_path_shifted(pid, NULL, &cgroup);
- if (r < 0)
- return r;
-
- return cg_path_get_slice(cgroup, slice);
-}
-
-int cg_controller_from_attr(const char *attr, char **controller) {
- const char *dot;
- char *c;
-
- 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 (!cg_controller_is_valid(c, false)) {
- free(c);
- return -EINVAL;
- }
-
- *controller = c;
- return 1;
-}
-