Skip to content

Commit f05ccc9

Browse files
authored
Merge pull request #1174 from giuseppe/rootless-use-current-cgroupfs
cgroup: cgroupfs attempt new sibling cgroup
2 parents 954da39 + ba6c957 commit f05ccc9

File tree

4 files changed

+53
-10
lines changed

4 files changed

+53
-10
lines changed

src/libcrun/cgroup-cgroupfs.c

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,14 @@
4040
static char *
4141
make_cgroup_path (const char *path, const char *id)
4242
{
43-
const char *cgroup_path = path;
4443
char *ret;
4544

46-
if (cgroup_path == NULL)
45+
if (path == NULL)
4746
xasprintf (&ret, "/%s", id);
48-
else if (cgroup_path[0] == '/')
49-
ret = xstrdup (cgroup_path);
47+
else if (path[0] == '/')
48+
ret = xstrdup (path);
5049
else
51-
xasprintf (&ret, "/%s", cgroup_path);
50+
xasprintf (&ret, "/%s", path);
5251

5352
return ret;
5453
}
@@ -87,6 +86,23 @@ libcrun_precreate_cgroup_cgroupfs (struct libcrun_cgroup_args *args, int *dirfd,
8786
return 0;
8887
}
8988

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+
90106
static int
91107
libcrun_cgroup_enter_cgroupfs (struct libcrun_cgroup_args *args, struct libcrun_cgroup_status *out, libcrun_error_t *err)
92108
{
@@ -105,7 +121,30 @@ libcrun_cgroup_enter_cgroupfs (struct libcrun_cgroup_args *args, struct libcrun_
105121

106122
ret = enable_controllers (out->path, err);
107123
if (UNLIKELY (ret < 0))
108-
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+
}
109148
}
110149

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

src/libcrun/cgroup-utils.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ move_process_to_cgroup (pid_t pid, const char *subsystem, const char *path, libc
151151
}
152152

153153
int
154-
libcrun_get_current_unified_cgroup (char **path, libcrun_error_t *err)
154+
libcrun_get_current_unified_cgroup (char **path, bool absolute, libcrun_error_t *err)
155155
{
156156
cleanup_free char *content = NULL;
157157
size_t content_size;
@@ -172,7 +172,11 @@ libcrun_get_current_unified_cgroup (char **path, libcrun_error_t *err)
172172
return crun_make_error (err, 0, "cannot parse /proc/self/cgroup");
173173
*to = '\0';
174174

175-
return append_paths (path, err, CGROUP_ROOT, from, NULL);
175+
if (absolute)
176+
return append_paths (path, err, CGROUP_ROOT, from, NULL);
177+
178+
*path = xstrdup (from);
179+
return 0;
176180
}
177181

178182
#ifndef CGROUP2_SUPER_MAGIC

src/libcrun/cgroup-utils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ int libcrun_move_process_to_cgroup (pid_t pid, pid_t init_pid, char *path, libcr
2626

2727
int libcrun_cgroups_create_symlinks (int dirfd, libcrun_error_t *err);
2828

29-
int libcrun_get_current_unified_cgroup (char **path, libcrun_error_t *err);
29+
int libcrun_get_current_unified_cgroup (char **path, bool absolute, libcrun_error_t *err);
3030

3131
int libcrun_get_cgroup_mode (libcrun_error_t *err);
3232

src/libcrun/linux.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2546,7 +2546,7 @@ libcrun_set_mounts (struct container_entrypoint_s *entrypoint_args, libcrun_cont
25462546
if (cgroup_mode == CGROUP_MODE_UNIFIED)
25472547
{
25482548
/* Read the cgroup path before we enter the cgroupns. */
2549-
ret = libcrun_get_current_unified_cgroup (&unified_cgroup_path, err);
2549+
ret = libcrun_get_current_unified_cgroup (&unified_cgroup_path, true, err);
25502550
if (UNLIKELY (ret < 0))
25512551
return ret;
25522552
}

0 commit comments

Comments
 (0)