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 * setup_cgroup_environment() - Setup the cgroup environment
39 * After calling this function, cleanup_cgroup_environment should be called
40 * once testing is complete.
42 * This function will print an error to stderr and return 1 if it is unable
43 * to setup the cgroup environment. If setup is successful, 0 is returned.
45 int setup_cgroup_environment(void)
47 char cgroup_workdir[PATH_MAX + 1];
49 format_cgroup_path(cgroup_workdir, "");
51 if (unshare(CLONE_NEWNS)) {
56 if (mount("none", "/", NULL, MS_REC | MS_PRIVATE, NULL)) {
57 log_err("mount fakeroot");
61 if (mount("none", CGROUP_MOUNT_PATH, "cgroup2", 0, NULL) && errno != EBUSY) {
62 log_err("mount cgroup2");
66 /* Cleanup existing failed runs, now that the environment is setup */
67 cleanup_cgroup_environment();
69 if (mkdir(cgroup_workdir, 0777) && errno != EEXIST) {
70 log_err("mkdir cgroup work dir");
77 static int nftwfunc(const char *filename, const struct stat *statptr,
78 int fileflags, struct FTW *pfwt)
80 if ((fileflags & FTW_D) && rmdir(filename))
81 log_err("Removing cgroup: %s", filename);
86 static int join_cgroup_from_top(char *cgroup_path)
88 char cgroup_procs_path[PATH_MAX + 1];
92 snprintf(cgroup_procs_path, sizeof(cgroup_procs_path),
93 "%s/cgroup.procs", cgroup_path);
95 fd = open(cgroup_procs_path, O_WRONLY);
97 log_err("Opening Cgroup Procs: %s", cgroup_procs_path);
101 if (dprintf(fd, "%d\n", pid) < 0) {
102 log_err("Joining Cgroup");
111 * join_cgroup() - Join a cgroup
112 * @path: The cgroup path, relative to the workdir, to join
114 * This function expects a cgroup to already be created, relative to the cgroup
115 * work dir, and it joins it. For example, passing "/my-cgroup" as the path
116 * would actually put the calling process into the cgroup
117 * "/cgroup-test-work-dir/my-cgroup"
119 * On success, it returns 0, otherwise on failure it returns 1.
121 int join_cgroup(const char *path)
123 char cgroup_path[PATH_MAX + 1];
125 format_cgroup_path(cgroup_path, path);
126 return join_cgroup_from_top(cgroup_path);
130 * cleanup_cgroup_environment() - Cleanup Cgroup Testing Environment
132 * This is an idempotent function to delete all temporary cgroups that
133 * have been created during the test, including the cgroup testing work
136 * At call time, it moves the calling process to the root cgroup, and then
137 * runs the deletion process. It is idempotent, and should not fail, unless
138 * a process is lingering.
140 * On failure, it will print an error to stderr, and try to continue.
142 void cleanup_cgroup_environment(void)
144 char cgroup_workdir[PATH_MAX + 1];
146 format_cgroup_path(cgroup_workdir, "");
147 join_cgroup_from_top(CGROUP_MOUNT_PATH);
148 nftw(cgroup_workdir, nftwfunc, WALK_FD_LIMIT, FTW_DEPTH | FTW_MOUNT);
152 * create_and_get_cgroup() - Create a cgroup, relative to workdir, and get the FD
153 * @path: The cgroup path, relative to the workdir, to join
155 * This function creates a cgroup under the top level workdir and returns the
156 * file descriptor. It is idempotent.
158 * On success, it returns the file descriptor. On failure it returns 0.
159 * If there is a failure, it prints the error to stderr.
161 int create_and_get_cgroup(const char *path)
163 char cgroup_path[PATH_MAX + 1];
166 format_cgroup_path(cgroup_path, path);
167 if (mkdir(cgroup_path, 0777) && errno != EEXIST) {
168 log_err("mkdiring cgroup %s .. %s", path, cgroup_path);
172 fd = open(cgroup_path, O_RDONLY);
174 log_err("Opening Cgroup");
182 * get_cgroup_id() - Get cgroup id for a particular cgroup path
183 * @path: The cgroup path, relative to the workdir, to join
185 * On success, it returns the cgroup id. On failure it returns 0,
186 * which is an invalid cgroup id.
187 * If there is a failure, it prints the error to stderr.
189 unsigned long long get_cgroup_id(const char *path)
191 int dirfd, err, flags, mount_id, fhsize;
193 unsigned long long cgid;
194 unsigned char raw_bytes[8];
196 char cgroup_workdir[PATH_MAX + 1];
197 struct file_handle *fhp, *fhp2;
198 unsigned long long ret = 0;
200 format_cgroup_path(cgroup_workdir, path);
204 fhsize = sizeof(*fhp);
205 fhp = calloc(1, fhsize);
210 err = name_to_handle_at(dirfd, cgroup_workdir, fhp, &mount_id, flags);
211 if (err >= 0 || fhp->handle_bytes != 8) {
212 log_err("name_to_handle_at");
216 fhsize = sizeof(struct file_handle) + fhp->handle_bytes;
217 fhp2 = realloc(fhp, fhsize);
222 err = name_to_handle_at(dirfd, cgroup_workdir, fhp2, &mount_id, flags);
225 log_err("name_to_handle_at");
229 memcpy(id.raw_bytes, fhp->f_handle, 8);