Skip to content

Commit ba6c957

Browse files
committed
cgroup: cgroupfs attempt new sibling cgroup
when using cgroupfs, if there was no cgroup path specified in the OCI configuration and using the absolute path failed, e.g. because we are rootless and don't have access to it, then attempt to create a sibling cgroup. Closes: #1173 Signed-off-by: Giuseppe Scrivano <[email protected]>
1 parent 74dc9b4 commit ba6c957

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

src/libcrun/cgroup-cgroupfs.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,23 @@ libcrun_precreate_cgroup_cgroupfs (struct libcrun_cgroup_args *args, int *dirfd,
8686
return 0;
8787
}
8888

89+
static int
90+
make_new_sibling_cgroup (char **out, const char *id, libcrun_error_t *err)
91+
{
92+
cleanup_free char *current_cgroup = NULL;
93+
char *dir;
94+
int ret;
95+
96+
ret = libcrun_get_current_unified_cgroup (&current_cgroup, false, err);
97+
if (UNLIKELY (ret < 0))
98+
return ret;
99+
100+
dir = dirname (current_cgroup);
101+
102+
append_paths (out, err, dir, id, NULL);
103+
return 0;
104+
}
105+
89106
static int
90107
libcrun_cgroup_enter_cgroupfs (struct libcrun_cgroup_args *args, struct libcrun_cgroup_status *out, libcrun_error_t *err)
91108
{
@@ -104,7 +121,30 @@ libcrun_cgroup_enter_cgroupfs (struct libcrun_cgroup_args *args, struct libcrun_
104121

105122
ret = enable_controllers (out->path, err);
106123
if (UNLIKELY (ret < 0))
107-
return ret;
124+
{
125+
/* If the generated path cannot be used attempt to create the new cgroup
126+
as a sibling of the current one. */
127+
if (args->cgroup_path == NULL)
128+
{
129+
libcrun_error_t tmp_err = NULL;
130+
int tmp_ret;
131+
132+
free (out->path);
133+
out->path = NULL;
134+
135+
tmp_ret = make_new_sibling_cgroup (&out->path, args->id, &tmp_err);
136+
if (UNLIKELY (tmp_ret < 0))
137+
{
138+
crun_error_release (&tmp_err);
139+
return ret;
140+
}
141+
142+
crun_error_release (err);
143+
ret = enable_controllers (out->path, err);
144+
}
145+
if (UNLIKELY (ret < 0))
146+
return ret;
147+
}
108148
}
109149

110150
/* The cgroup was already joined, nothing more left to do. */

0 commit comments

Comments
 (0)