cgroups: simplify current cgroup retrieval on pure unified cgroup layouts

parent c41ab8e4
...@@ -3417,41 +3417,39 @@ static int cg_hybrid_init(struct cgroup_ops *ops, bool relative, bool unprivileg ...@@ -3417,41 +3417,39 @@ static int cg_hybrid_init(struct cgroup_ops *ops, bool relative, bool unprivileg
} }
/* Get current cgroup from /proc/self/cgroup for the cgroupfs v2 hierarchy. */ /* Get current cgroup from /proc/self/cgroup for the cgroupfs v2 hierarchy. */
static char *cg_unified_get_current_cgroup(bool relative) static char *current_cgroup_unified(bool relative)
{ {
__do_free char *basecginfo = NULL, *copy = NULL; __do_free char *cgroup_info = NULL;
char *base_cgroup; char *it;
if (!relative && (geteuid() == 0)) if (!relative && (geteuid() == 0))
basecginfo = read_file_at(-EBADF, "/proc/1/cgroup", PROTECT_OPEN, 0); cgroup_info = read_file_at(-EBADF, "/proc/1/cgroup", PROTECT_OPEN, 0);
else else
basecginfo = read_file_at(-EBADF, "/proc/self/cgroup", PROTECT_OPEN, 0); cgroup_info = read_file_at(-EBADF, "/proc/self/cgroup", PROTECT_OPEN, 0);
if (!basecginfo) if (!cgroup_info)
return NULL; return NULL;
base_cgroup = strstr(basecginfo, "0::/"); lxc_iterate_parts(it, cgroup_info, "\n") {
if (!base_cgroup) if (*it != '0')
return NULL; continue;
base_cgroup = base_cgroup + 3; it += STRLITERALLEN("0::");
copy = copy_to_eol(base_cgroup);
if (!copy)
return NULL;
trim(copy);
if (!relative) { if (!abspath(it))
base_cgroup = prune_init_scope(copy); return log_error(NULL, "Corrupt cgroup info for %s process", relative ? "current" : "init");
if (!base_cgroup)
return NULL;
} else {
base_cgroup = copy;
}
if (abspath(base_cgroup)) /* remove init.scope */
base_cgroup = deabs(base_cgroup); if (!relative)
it = prune_init_scope(it);
/* create a relative path */
it = deabs(it);
return strdup(it);
}
/* We're allowing base_cgroup to be "". */ return log_error(NULL, "Failed to retrieve current cgroup for %s process",
return strdup(base_cgroup); relative ? "current" : "init");
} }
static int cg_unified_init(struct cgroup_ops *ops, bool relative, static int cg_unified_init(struct cgroup_ops *ops, bool relative,
...@@ -3460,7 +3458,7 @@ static int cg_unified_init(struct cgroup_ops *ops, bool relative, ...@@ -3460,7 +3458,7 @@ static int cg_unified_init(struct cgroup_ops *ops, bool relative,
__do_free char *base_cgroup = NULL; __do_free char *base_cgroup = NULL;
int ret; int ret;
base_cgroup = cg_unified_get_current_cgroup(relative); base_cgroup = current_cgroup_unified(relative);
if (!base_cgroup) if (!base_cgroup)
return ret_errno(EINVAL); return ret_errno(EINVAL);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment