1
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 <stdlib.h>
10#include <linux/sched.h>
11#include <fcntl.h>
12#include <unistd.h>
13#include <ftw.h>
14
15
16#include "cgroup_helpers.h"
17
18
19
20
21
22
23
24
25
26
27
28
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)
35
36
37
38
39
40
41
42
43
44static int enable_all_controllers(char *cgroup_path)
45{
46 char path[PATH_MAX + 1];
47 char buf[PATH_MAX];
48 char *c, *c2;
49 int fd, cfd;
50 ssize_t len;
51
52 snprintf(path, sizeof(path), "%s/cgroup.controllers", cgroup_path);
53 fd = open(path, O_RDONLY);
54 if (fd < 0) {
55 log_err("Opening cgroup.controllers: %s", path);
56 return 1;
57 }
58
59 len = read(fd, buf, sizeof(buf) - 1);
60 if (len < 0) {
61 close(fd);
62 log_err("Reading cgroup.controllers: %s", path);
63 return 1;
64 }
65 buf[len] = 0;
66 close(fd);
67
68
69 if (len == 0)
70 return 0;
71
72 snprintf(path, sizeof(path), "%s/cgroup.subtree_control", cgroup_path);
73 cfd = open(path, O_RDWR);
74 if (cfd < 0) {
75 log_err("Opening cgroup.subtree_control: %s", path);
76 return 1;
77 }
78
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);
82 close(cfd);
83 return 1;
84 }
85 }
86 close(cfd);
87 return 0;
88}
89
90
91
92
93
94
95
96
97
98
99int setup_cgroup_environment(void)
100{
101 char cgroup_workdir[PATH_MAX - 24];
102
103 format_cgroup_path(cgroup_workdir, "");
104
105 if (unshare(CLONE_NEWNS)) {
106 log_err("unshare");
107 return 1;
108 }
109
110 if (mount("none", "/", NULL, MS_REC | MS_PRIVATE, NULL)) {
111 log_err("mount fakeroot");
112 return 1;
113 }
114
115 if (mount("none", CGROUP_MOUNT_PATH, "cgroup2", 0, NULL) && errno != EBUSY) {
116 log_err("mount cgroup2");
117 return 1;
118 }
119
120
121 cleanup_cgroup_environment();
122
123 if (mkdir(cgroup_workdir, 0777) && errno != EEXIST) {
124 log_err("mkdir cgroup work dir");
125 return 1;
126 }
127
128 if (enable_all_controllers(cgroup_workdir))
129 return 1;
130
131 return 0;
132}
133
134static int nftwfunc(const char *filename, const struct stat *statptr,
135 int fileflags, struct FTW *pfwt)
136{
137 if ((fileflags & FTW_D) && rmdir(filename))
138 log_err("Removing cgroup: %s", filename);
139 return 0;
140}
141
142
143static int join_cgroup_from_top(char *cgroup_path)
144{
145 char cgroup_procs_path[PATH_MAX + 1];
146 pid_t pid = getpid();
147 int fd, rc = 0;
148
149 snprintf(cgroup_procs_path, sizeof(cgroup_procs_path),
150 "%s/cgroup.procs", cgroup_path);
151
152 fd = open(cgroup_procs_path, O_WRONLY);
153 if (fd < 0) {
154 log_err("Opening Cgroup Procs: %s", cgroup_procs_path);
155 return 1;
156 }
157
158 if (dprintf(fd, "%d\n", pid) < 0) {
159 log_err("Joining Cgroup");
160 rc = 1;
161 }
162
163 close(fd);
164 return rc;
165}
166
167
168
169
170
171
172
173
174
175
176
177
178int join_cgroup(const char *path)
179{
180 char cgroup_path[PATH_MAX + 1];
181
182 format_cgroup_path(cgroup_path, path);
183 return join_cgroup_from_top(cgroup_path);
184}
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199void cleanup_cgroup_environment(void)
200{
201 char cgroup_workdir[PATH_MAX + 1];
202
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);
206}
207
208
209
210
211
212
213
214
215
216
217
218int create_and_get_cgroup(const char *path)
219{
220 char cgroup_path[PATH_MAX + 1];
221 int fd;
222
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);
226 return -1;
227 }
228
229 fd = open(cgroup_path, O_RDONLY);
230 if (fd < 0) {
231 log_err("Opening Cgroup");
232 return -1;
233 }
234
235 return fd;
236}
237
238
239
240
241
242
243
244
245
246unsigned long long get_cgroup_id(const char *path)
247{
248 int dirfd, err, flags, mount_id, fhsize;
249 union {
250 unsigned long long cgid;
251 unsigned char raw_bytes[8];
252 } id;
253 char cgroup_workdir[PATH_MAX + 1];
254 struct file_handle *fhp, *fhp2;
255 unsigned long long ret = 0;
256
257 format_cgroup_path(cgroup_workdir, path);
258
259 dirfd = AT_FDCWD;
260 flags = 0;
261 fhsize = sizeof(*fhp);
262 fhp = calloc(1, fhsize);
263 if (!fhp) {
264 log_err("calloc");
265 return 0;
266 }
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");
270 goto free_mem;
271 }
272
273 fhsize = sizeof(struct file_handle) + fhp->handle_bytes;
274 fhp2 = realloc(fhp, fhsize);
275 if (!fhp2) {
276 log_err("realloc");
277 goto free_mem;
278 }
279 err = name_to_handle_at(dirfd, cgroup_workdir, fhp2, &mount_id, flags);
280 fhp = fhp2;
281 if (err < 0) {
282 log_err("name_to_handle_at");
283 goto free_mem;
284 }
285
286 memcpy(id.raw_bytes, fhp->f_handle, 8);
287 ret = id.cgid;
288
289free_mem:
290 free(fhp);
291 return ret;
292}
293
294int cgroup_setup_and_join(const char *path) {
295 int cg_fd;
296
297 if (setup_cgroup_environment()) {
298 fprintf(stderr, "Failed to setup cgroup environment\n");
299 return -EINVAL;
300 }
301
302 cg_fd = create_and_get_cgroup(path);
303 if (cg_fd < 0) {
304 fprintf(stderr, "Failed to create test cgroup\n");
305 cleanup_cgroup_environment();
306 return cg_fd;
307 }
308
309 if (join_cgroup(path)) {
310 fprintf(stderr, "Failed to join cgroup\n");
311 cleanup_cgroup_environment();
312 return -EINVAL;
313 }
314 return cg_fd;
315}
316