|
12 | 12 | #include <unistd.h> |
13 | 13 | #include <ftw.h> |
14 | 14 |
|
15 | | - |
16 | 15 | #include "cgroup_helpers.h" |
17 | 16 |
|
18 | 17 | /* |
19 | 18 | * 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 |
22 | | - * |
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. |
| 19 | + * we create a new mount namespace, and cgroup namespace. The cgroupv2 |
| 20 | + * root is mounted at CGROUP_MOUNT_PATH. Unfortunately, most people don't |
| 21 | + * have cgroupv2 enabled at this point in time. It's easier to create our |
| 22 | + * own mount namespace and manage it ourselves. We assume /mnt exists. |
25 | 23 | * |
26 | | - * We assume /mnt exists. |
| 24 | + * Related cgroupv1 helpers are named *classid*(), since we only use the |
| 25 | + * net_cls controller for tagging net_cls.classid. We assume the default |
| 26 | + * mount under /sys/fs/cgroup/net_cls exists which should be the case for |
| 27 | + * the vast majority of users. |
27 | 28 | */ |
28 | 29 |
|
29 | 30 | #define WALK_FD_LIMIT 16 |
| 31 | + |
30 | 32 | #define CGROUP_MOUNT_PATH "/mnt" |
| 33 | +#define NETCLS_MOUNT_PATH "/sys/fs/cgroup/net_cls" |
31 | 34 | #define CGROUP_WORK_DIR "/cgroup-test-work-dir" |
| 35 | + |
32 | 36 | #define format_cgroup_path(buf, path) \ |
33 | 37 | snprintf(buf, sizeof(buf), "%s%s%s", CGROUP_MOUNT_PATH, \ |
34 | 38 | CGROUP_WORK_DIR, path) |
35 | 39 |
|
| 40 | +#define format_classid_path(buf) \ |
| 41 | + snprintf(buf, sizeof(buf), "%s%s", NETCLS_MOUNT_PATH, \ |
| 42 | + CGROUP_WORK_DIR) |
| 43 | + |
36 | 44 | /** |
37 | 45 | * enable_all_controllers() - Enable all available cgroup v2 controllers |
38 | 46 | * |
@@ -139,8 +147,7 @@ static int nftwfunc(const char *filename, const struct stat *statptr, |
139 | 147 | return 0; |
140 | 148 | } |
141 | 149 |
|
142 | | - |
143 | | -static int join_cgroup_from_top(char *cgroup_path) |
| 150 | +static int join_cgroup_from_top(const char *cgroup_path) |
144 | 151 | { |
145 | 152 | char cgroup_procs_path[PATH_MAX + 1]; |
146 | 153 | pid_t pid = getpid(); |
@@ -313,3 +320,96 @@ int cgroup_setup_and_join(const char *path) { |
313 | 320 | } |
314 | 321 | return cg_fd; |
315 | 322 | } |
| 323 | + |
| 324 | +/** |
| 325 | + * setup_classid_environment() - Setup the cgroupv1 net_cls environment |
| 326 | + * |
| 327 | + * After calling this function, cleanup_classid_environment should be called |
| 328 | + * once testing is complete. |
| 329 | + * |
| 330 | + * This function will print an error to stderr and return 1 if it is unable |
| 331 | + * to setup the cgroup environment. If setup is successful, 0 is returned. |
| 332 | + */ |
| 333 | +int setup_classid_environment(void) |
| 334 | +{ |
| 335 | + char cgroup_workdir[PATH_MAX + 1]; |
| 336 | + |
| 337 | + format_classid_path(cgroup_workdir); |
| 338 | + cleanup_classid_environment(); |
| 339 | + |
| 340 | + if (mkdir(cgroup_workdir, 0777) && errno != EEXIST) { |
| 341 | + log_err("mkdir cgroup work dir"); |
| 342 | + return 1; |
| 343 | + } |
| 344 | + |
| 345 | + return 0; |
| 346 | +} |
| 347 | + |
| 348 | +/** |
| 349 | + * set_classid() - Set a cgroupv1 net_cls classid |
| 350 | + * @id: the numeric classid |
| 351 | + * |
| 352 | + * Writes the passed classid into the cgroup work dir's net_cls.classid |
| 353 | + * file in order to later on trigger socket tagging. |
| 354 | + * |
| 355 | + * On success, it returns 0, otherwise on failure it returns 1. If there |
| 356 | + * is a failure, it prints the error to stderr. |
| 357 | + */ |
| 358 | +int set_classid(unsigned int id) |
| 359 | +{ |
| 360 | + char cgroup_workdir[PATH_MAX - 42]; |
| 361 | + char cgroup_classid_path[PATH_MAX + 1]; |
| 362 | + int fd, rc = 0; |
| 363 | + |
| 364 | + format_classid_path(cgroup_workdir); |
| 365 | + snprintf(cgroup_classid_path, sizeof(cgroup_classid_path), |
| 366 | + "%s/net_cls.classid", cgroup_workdir); |
| 367 | + |
| 368 | + fd = open(cgroup_classid_path, O_WRONLY); |
| 369 | + if (fd < 0) { |
| 370 | + log_err("Opening cgroup classid: %s", cgroup_classid_path); |
| 371 | + return 1; |
| 372 | + } |
| 373 | + |
| 374 | + if (dprintf(fd, "%u\n", id) < 0) { |
| 375 | + log_err("Setting cgroup classid"); |
| 376 | + rc = 1; |
| 377 | + } |
| 378 | + |
| 379 | + close(fd); |
| 380 | + return rc; |
| 381 | +} |
| 382 | + |
| 383 | +/** |
| 384 | + * join_classid() - Join a cgroupv1 net_cls classid |
| 385 | + * |
| 386 | + * This function expects the cgroup work dir to be already created, as we |
| 387 | + * join it here. This causes the process sockets to be tagged with the given |
| 388 | + * net_cls classid. |
| 389 | + * |
| 390 | + * On success, it returns 0, otherwise on failure it returns 1. |
| 391 | + */ |
| 392 | +int join_classid(void) |
| 393 | +{ |
| 394 | + char cgroup_workdir[PATH_MAX + 1]; |
| 395 | + |
| 396 | + format_classid_path(cgroup_workdir); |
| 397 | + return join_cgroup_from_top(cgroup_workdir); |
| 398 | +} |
| 399 | + |
| 400 | +/** |
| 401 | + * cleanup_classid_environment() - Cleanup the cgroupv1 net_cls environment |
| 402 | + * |
| 403 | + * At call time, it moves the calling process to the root cgroup, and then |
| 404 | + * runs the deletion process. |
| 405 | + * |
| 406 | + * On failure, it will print an error to stderr, and try to continue. |
| 407 | + */ |
| 408 | +void cleanup_classid_environment(void) |
| 409 | +{ |
| 410 | + char cgroup_workdir[PATH_MAX + 1]; |
| 411 | + |
| 412 | + format_classid_path(cgroup_workdir); |
| 413 | + join_cgroup_from_top(NETCLS_MOUNT_PATH); |
| 414 | + nftw(cgroup_workdir, nftwfunc, WALK_FD_LIMIT, FTW_DEPTH | FTW_MOUNT); |
| 415 | +} |
0 commit comments