1 // SPDX-License-Identifier: GPL-2.0
7 #include <linux/limits.h>
9 #include <linux/sched.h>
15 #include "cgroup_helpers.h"
18 * To avoid relying on the system setup, when setup_cgroup_env is called
19 * we create a new mount namespace, and cgroup namespace. The cgroup2
20 * root is mounted at CGROUP_MOUNT_PATH
22 * Unfortunately, most people don't have cgroupv2 enabled at this point in time.
23 * It's easier to create our own mount namespace and manage it ourselves.
25 * We assume /mnt exists.
28 #define WALK_FD_LIMIT 16
29 #define CGROUP_MOUNT_PATH "/mnt"
30 #define CGROUP_WORK_DIR "/cgroup-test-work-dir"
31 #define format_cgroup_path(buf, path) \
32 snprintf(buf, sizeof(buf), "%s%s%s", CGROUP_MOUNT_PATH, \
33 CGROUP_WORK_DIR, path)
36 * setup_cgroup_environment() - Setup the cgroup environment
38 * After calling this function, cleanup_cgroup_environment should be called
39 * once testing is complete.
41 * This function will print an error to stderr and return 1 if it is unable
42 * to setup the cgroup environment. If setup is successful, 0 is returned.
44 int setup_cgroup_environment(void)
46 char cgroup_workdir
[PATH_MAX
+ 1];
48 format_cgroup_path(cgroup_workdir
, "");
50 if (unshare(CLONE_NEWNS
)) {
55 if (mount("none", "/", NULL
, MS_REC
| MS_PRIVATE
, NULL
)) {
56 log_err("mount fakeroot");
60 if (mount("none", CGROUP_MOUNT_PATH
, "cgroup2", 0, NULL
) && errno
!= EBUSY
) {
61 log_err("mount cgroup2");
65 /* Cleanup existing failed runs, now that the environment is setup */
66 cleanup_cgroup_environment();
68 if (mkdir(cgroup_workdir
, 0777) && errno
!= EEXIST
) {
69 log_err("mkdir cgroup work dir");
76 static int nftwfunc(const char *filename
, const struct stat
*statptr
,
77 int fileflags
, struct FTW
*pfwt
)
79 if ((fileflags
& FTW_D
) && rmdir(filename
))
80 log_err("Removing cgroup: %s", filename
);
85 static int join_cgroup_from_top(char *cgroup_path
)
87 char cgroup_procs_path
[PATH_MAX
+ 1];
91 snprintf(cgroup_procs_path
, sizeof(cgroup_procs_path
),
92 "%s/cgroup.procs", cgroup_path
);
94 fd
= open(cgroup_procs_path
, O_WRONLY
);
96 log_err("Opening Cgroup Procs: %s", cgroup_procs_path
);
100 if (dprintf(fd
, "%d\n", pid
) < 0) {
101 log_err("Joining Cgroup");
110 * join_cgroup() - Join a cgroup
111 * @path: The cgroup path, relative to the workdir, to join
113 * This function expects a cgroup to already be created, relative to the cgroup
114 * work dir, and it joins it. For example, passing "/my-cgroup" as the path
115 * would actually put the calling process into the cgroup
116 * "/cgroup-test-work-dir/my-cgroup"
118 * On success, it returns 0, otherwise on failure it returns 1.
120 int join_cgroup(char *path
)
122 char cgroup_path
[PATH_MAX
+ 1];
124 format_cgroup_path(cgroup_path
, path
);
125 return join_cgroup_from_top(cgroup_path
);
129 * cleanup_cgroup_environment() - Cleanup Cgroup Testing Environment
131 * This is an idempotent function to delete all temporary cgroups that
132 * have been created during the test, including the cgroup testing work
135 * At call time, it moves the calling process to the root cgroup, and then
136 * runs the deletion process. It is idempotent, and should not fail, unless
137 * a process is lingering.
139 * On failure, it will print an error to stderr, and try to continue.
141 void cleanup_cgroup_environment(void)
143 char cgroup_workdir
[PATH_MAX
+ 1];
145 format_cgroup_path(cgroup_workdir
, "");
146 join_cgroup_from_top(CGROUP_MOUNT_PATH
);
147 nftw(cgroup_workdir
, nftwfunc
, WALK_FD_LIMIT
, FTW_DEPTH
| FTW_MOUNT
);
151 * create_and_get_cgroup() - Create a cgroup, relative to workdir, and get the FD
152 * @path: The cgroup path, relative to the workdir, to join
154 * This function creates a cgroup under the top level workdir and returns the
155 * file descriptor. It is idempotent.
157 * On success, it returns the file descriptor. On failure it returns 0.
158 * If there is a failure, it prints the error to stderr.
160 int create_and_get_cgroup(char *path
)
162 char cgroup_path
[PATH_MAX
+ 1];
165 format_cgroup_path(cgroup_path
, path
);
166 if (mkdir(cgroup_path
, 0777) && errno
!= EEXIST
) {
167 log_err("mkdiring cgroup %s .. %s", path
, cgroup_path
);
171 fd
= open(cgroup_path
, O_RDONLY
);
173 log_err("Opening Cgroup");