1 // SPDX-License-Identifier: GPL-2.0
7 #include <linux/limits.h>
10 #include <linux/sched.h>
16 #include "cgroup_helpers.h"
19 * To avoid relying on the system setup, when setup_cgroup_env is called
20 * we create a new mount namespace, and cgroup namespace. The cgroup2
21 * root is mounted at CGROUP_MOUNT_PATH
23 * Unfortunately, most people don't have cgroupv2 enabled at this point in time.
24 * It's easier to create our own mount namespace and manage it ourselves.
26 * We assume /mnt exists.
29 #define WALK_FD_LIMIT 16
30 #define CGROUP_MOUNT_PATH "/mnt"
31 #define CGROUP_WORK_DIR "/cgroup-test-work-dir"
32 #define format_cgroup_path(buf, path) \
33 snprintf(buf, sizeof(buf), "%s%s%s", CGROUP_MOUNT_PATH, \
34 CGROUP_WORK_DIR, path)
37 * enable_all_controllers() - Enable all available cgroup v2 controllers
39 * Enable all available cgroup v2 controllers in order to increase
42 * If successful, 0 is returned.
44 static int enable_all_controllers(char *cgroup_path
)
46 char path
[PATH_MAX
+ 1];
52 snprintf(path
, sizeof(path
), "%s/cgroup.controllers", cgroup_path
);
53 fd
= open(path
, O_RDONLY
);
55 log_err("Opening cgroup.controllers: %s", path
);
59 len
= read(fd
, buf
, sizeof(buf
) - 1);
62 log_err("Reading cgroup.controllers: %s", path
);
68 /* No controllers available? We're probably on cgroup v1. */
72 snprintf(path
, sizeof(path
), "%s/cgroup.subtree_control", cgroup_path
);
73 cfd
= open(path
, O_RDWR
);
75 log_err("Opening cgroup.subtree_control: %s", path
);
79 for (c
= strtok_r(buf
, " ", &c2
); c
; c
= strtok_r(NULL
, " ", &c2
)) {
80 if (dprintf(cfd
, "+%s\n", c
) <= 0) {
81 log_err("Enabling controller %s: %s", c
, path
);
91 * setup_cgroup_environment() - Setup the cgroup environment
93 * After calling this function, cleanup_cgroup_environment should be called
94 * once testing is complete.
96 * This function will print an error to stderr and return 1 if it is unable
97 * to setup the cgroup environment. If setup is successful, 0 is returned.
99 int setup_cgroup_environment(void)
101 char cgroup_workdir
[PATH_MAX
- 24];
103 format_cgroup_path(cgroup_workdir
, "");
105 if (unshare(CLONE_NEWNS
)) {
110 if (mount("none", "/", NULL
, MS_REC
| MS_PRIVATE
, NULL
)) {
111 log_err("mount fakeroot");
115 if (mount("none", CGROUP_MOUNT_PATH
, "cgroup2", 0, NULL
) && errno
!= EBUSY
) {
116 log_err("mount cgroup2");
120 /* Cleanup existing failed runs, now that the environment is setup */
121 cleanup_cgroup_environment();
123 if (mkdir(cgroup_workdir
, 0777) && errno
!= EEXIST
) {
124 log_err("mkdir cgroup work dir");
128 if (enable_all_controllers(cgroup_workdir
))
134 static int nftwfunc(const char *filename
, const struct stat
*statptr
,
135 int fileflags
, struct FTW
*pfwt
)
137 if ((fileflags
& FTW_D
) && rmdir(filename
))
138 log_err("Removing cgroup: %s", filename
);
143 static int join_cgroup_from_top(char *cgroup_path
)
145 char cgroup_procs_path
[PATH_MAX
+ 1];
146 pid_t pid
= getpid();
149 snprintf(cgroup_procs_path
, sizeof(cgroup_procs_path
),
150 "%s/cgroup.procs", cgroup_path
);
152 fd
= open(cgroup_procs_path
, O_WRONLY
);
154 log_err("Opening Cgroup Procs: %s", cgroup_procs_path
);
158 if (dprintf(fd
, "%d\n", pid
) < 0) {
159 log_err("Joining Cgroup");
168 * join_cgroup() - Join a cgroup
169 * @path: The cgroup path, relative to the workdir, to join
171 * This function expects a cgroup to already be created, relative to the cgroup
172 * work dir, and it joins it. For example, passing "/my-cgroup" as the path
173 * would actually put the calling process into the cgroup
174 * "/cgroup-test-work-dir/my-cgroup"
176 * On success, it returns 0, otherwise on failure it returns 1.
178 int join_cgroup(const char *path
)
180 char cgroup_path
[PATH_MAX
+ 1];
182 format_cgroup_path(cgroup_path
, path
);
183 return join_cgroup_from_top(cgroup_path
);
187 * cleanup_cgroup_environment() - Cleanup Cgroup Testing Environment
189 * This is an idempotent function to delete all temporary cgroups that
190 * have been created during the test, including the cgroup testing work
193 * At call time, it moves the calling process to the root cgroup, and then
194 * runs the deletion process. It is idempotent, and should not fail, unless
195 * a process is lingering.
197 * On failure, it will print an error to stderr, and try to continue.
199 void cleanup_cgroup_environment(void)
201 char cgroup_workdir
[PATH_MAX
+ 1];
203 format_cgroup_path(cgroup_workdir
, "");
204 join_cgroup_from_top(CGROUP_MOUNT_PATH
);
205 nftw(cgroup_workdir
, nftwfunc
, WALK_FD_LIMIT
, FTW_DEPTH
| FTW_MOUNT
);
209 * create_and_get_cgroup() - Create a cgroup, relative to workdir, and get the FD
210 * @path: The cgroup path, relative to the workdir, to join
212 * This function creates a cgroup under the top level workdir and returns the
213 * file descriptor. It is idempotent.
215 * On success, it returns the file descriptor. On failure it returns -1.
216 * If there is a failure, it prints the error to stderr.
218 int create_and_get_cgroup(const char *path
)
220 char cgroup_path
[PATH_MAX
+ 1];
223 format_cgroup_path(cgroup_path
, path
);
224 if (mkdir(cgroup_path
, 0777) && errno
!= EEXIST
) {
225 log_err("mkdiring cgroup %s .. %s", path
, cgroup_path
);
229 fd
= open(cgroup_path
, O_RDONLY
);
231 log_err("Opening Cgroup");
239 * get_cgroup_id() - Get cgroup id for a particular cgroup path
240 * @path: The cgroup path, relative to the workdir, to join
242 * On success, it returns the cgroup id. On failure it returns 0,
243 * which is an invalid cgroup id.
244 * If there is a failure, it prints the error to stderr.
246 unsigned long long get_cgroup_id(const char *path
)
248 int dirfd
, err
, flags
, mount_id
, fhsize
;
250 unsigned long long cgid
;
251 unsigned char raw_bytes
[8];
253 char cgroup_workdir
[PATH_MAX
+ 1];
254 struct file_handle
*fhp
, *fhp2
;
255 unsigned long long ret
= 0;
257 format_cgroup_path(cgroup_workdir
, path
);
261 fhsize
= sizeof(*fhp
);
262 fhp
= calloc(1, fhsize
);
267 err
= name_to_handle_at(dirfd
, cgroup_workdir
, fhp
, &mount_id
, flags
);
268 if (err
>= 0 || fhp
->handle_bytes
!= 8) {
269 log_err("name_to_handle_at");
273 fhsize
= sizeof(struct file_handle
) + fhp
->handle_bytes
;
274 fhp2
= realloc(fhp
, fhsize
);
279 err
= name_to_handle_at(dirfd
, cgroup_workdir
, fhp2
, &mount_id
, flags
);
282 log_err("name_to_handle_at");
286 memcpy(id
.raw_bytes
, fhp
->f_handle
, 8);
294 int cgroup_setup_and_join(const char *path
) {
297 if (setup_cgroup_environment()) {
298 fprintf(stderr
, "Failed to setup cgroup environment\n");
302 cg_fd
= create_and_get_cgroup(path
);
304 fprintf(stderr
, "Failed to create test cgroup\n");
305 cleanup_cgroup_environment();
309 if (join_cgroup(path
)) {
310 fprintf(stderr
, "Failed to join cgroup\n");
311 cleanup_cgroup_environment();