Merge branch 'locking-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / tools / testing / selftests / bpf / cgroup_helpers.c
blobf3bca3ade0f3de74b4397db83f90628c96af6514
1 // SPDX-License-Identifier: GPL-2.0
2 #define _GNU_SOURCE
3 #include <sched.h>
4 #include <sys/mount.h>
5 #include <sys/stat.h>
6 #include <sys/types.h>
7 #include <linux/limits.h>
8 #include <stdio.h>
9 #include <linux/sched.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12 #include <ftw.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)
35 /**
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)) {
51 log_err("unshare");
52 return 1;
55 if (mount("none", "/", NULL, MS_REC | MS_PRIVATE, NULL)) {
56 log_err("mount fakeroot");
57 return 1;
60 if (mount("none", CGROUP_MOUNT_PATH, "cgroup2", 0, NULL) && errno != EBUSY) {
61 log_err("mount cgroup2");
62 return 1;
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");
70 return 1;
73 return 0;
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);
81 return 0;
85 static int join_cgroup_from_top(char *cgroup_path)
87 char cgroup_procs_path[PATH_MAX + 1];
88 pid_t pid = getpid();
89 int fd, rc = 0;
91 snprintf(cgroup_procs_path, sizeof(cgroup_procs_path),
92 "%s/cgroup.procs", cgroup_path);
94 fd = open(cgroup_procs_path, O_WRONLY);
95 if (fd < 0) {
96 log_err("Opening Cgroup Procs: %s", cgroup_procs_path);
97 return 1;
100 if (dprintf(fd, "%d\n", pid) < 0) {
101 log_err("Joining Cgroup");
102 rc = 1;
105 close(fd);
106 return rc;
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
133 * directory.
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];
163 int fd;
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);
168 return 0;
171 fd = open(cgroup_path, O_RDONLY);
172 if (fd < 0) {
173 log_err("Opening Cgroup");
174 return 0;
177 return fd;