summaryrefslogtreecommitdiff
path: root/src/shared
diff options
context:
space:
mode:
Diffstat (limited to 'src/shared')
-rw-r--r--src/shared/cgroup-util.c51
-rw-r--r--src/shared/cgroup-util.h2
2 files changed, 51 insertions, 2 deletions
diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c
index 05d026a587..9cbc64a541 100644
--- a/src/shared/cgroup-util.c
+++ b/src/shared/cgroup-util.c
@@ -1227,11 +1227,11 @@ int cg_path_decode_unit(const char *cgroup, char **unit){
}
static const char *skip_slices(const char *p) {
- size_t n;
-
/* Skips over all slice assignments */
for (;;) {
+ size_t n;
+
p += strspn(p, "/");
n = strcspn(p, "/");
@@ -1475,6 +1475,53 @@ int cg_pid_get_owner_uid(pid_t pid, uid_t *uid) {
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;
diff --git a/src/shared/cgroup-util.h b/src/shared/cgroup-util.h
index 0485c11ade..2d00bb3fff 100644
--- a/src/shared/cgroup-util.h
+++ b/src/shared/cgroup-util.h
@@ -90,6 +90,7 @@ int cg_path_get_owner_uid(const char *path, uid_t *uid);
int cg_path_get_unit(const char *path, char **unit);
int cg_path_get_user_unit(const char *path, char **unit);
int cg_path_get_machine_name(const char *path, char **machine);
+int cg_path_get_slice(const char *path, char **slice);
int cg_pid_get_path_shifted(pid_t pid, char **root, char **cgroup);
@@ -98,6 +99,7 @@ int cg_pid_get_owner_uid(pid_t pid, uid_t *uid);
int cg_pid_get_unit(pid_t pid, char **unit);
int cg_pid_get_user_unit(pid_t pid, char **unit);
int cg_pid_get_machine_name(pid_t pid, char **machine);
+int cg_pid_get_slice(pid_t pid, char **slice);
int cg_path_decode_unit(const char *cgroup, char **unit);