tools/testing/selftests/bpf/cgroup_helpers.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/cgroup_helpers.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/cgroup_helpers.c- Extension
.c- Size
- 19484 bytes
- Lines
- 752
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
sched.hsys/mount.hsys/stat.hsys/types.hsys/xattr.hlinux/limits.hstdio.hstdlib.hlinux/sched.hfcntl.hunistd.hftw.hcgroup_helpers.hbpf_util.h
Detected Declarations
function __enable_controllersfunction enable_controllersfunction __write_cgroup_filefunction write_cgroup_filefunction write_cgroup_file_parentfunction setup_cgroup_environmentfunction nftwfuncfunction join_cgroup_from_topfunction join_cgroupfunction join_root_cgroupfunction join_parent_cgroupfunction set_cgroup_xattrfunction __cleanup_cgroup_environmentfunction cleanup_cgroup_environmentfunction get_root_cgroupfunction remove_cgroupfunction remove_cgroup_pidfunction create_and_get_cgroupfunction get_cgroup_id_from_pathfunction get_cgroup_idfunction cgroup_setup_and_joinfunction setup_classid_environmentfunction set_classidfunction join_classidfunction cleanup_classid_environmentfunction get_classid_cgroup_idfunction get_cgroup1_hierarchy_idfunction open_classid
Annotated Snippet
if (fd < 0) {
log_err("Opening cgroup.controllers: %s", path);
return 1;
}
len = read(fd, enable, sizeof(enable) - 1);
if (len < 0) {
close(fd);
log_err("Reading cgroup.controllers: %s", path);
return 1;
} else if (len == 0) { /* No controllers to enable */
close(fd);
return 0;
}
enable[len] = 0;
close(fd);
} else {
strscpy(enable, controllers);
}
snprintf(path, sizeof(path), "%s/cgroup.subtree_control", cgroup_path);
cfd = open(path, O_RDWR);
if (cfd < 0) {
log_err("Opening cgroup.subtree_control: %s", path);
return 1;
}
for (c = strtok_r(enable, " ", &c2); c; c = strtok_r(NULL, " ", &c2)) {
if (dprintf(cfd, "+%s\n", c) <= 0) {
log_err("Enabling controller %s: %s", c, path);
close(cfd);
return 1;
}
}
close(cfd);
return 0;
}
/**
* enable_controllers() - Enable cgroup v2 controllers
* @relative_path: The cgroup path, relative to the workdir
* @controllers: List of controllers to enable in cgroup.controllers format
*
*
* Enable given cgroup v2 controllers, if @controllers is NULL, enable all
* available controllers.
*
* If successful, 0 is returned.
*/
int enable_controllers(const char *relative_path, const char *controllers)
{
char cgroup_path[PATH_MAX + 1];
format_cgroup_path(cgroup_path, relative_path);
return __enable_controllers(cgroup_path, controllers);
}
static int __write_cgroup_file(const char *cgroup_path, const char *file,
const char *buf)
{
char file_path[PATH_MAX + 1];
int fd;
snprintf(file_path, sizeof(file_path), "%s/%s", cgroup_path, file);
fd = open(file_path, O_RDWR);
if (fd < 0) {
log_err("Opening %s", file_path);
return 1;
}
if (dprintf(fd, "%s", buf) <= 0) {
log_err("Writing to %s", file_path);
close(fd);
return 1;
}
close(fd);
return 0;
}
/**
* write_cgroup_file() - Write to a cgroup file
* @relative_path: The cgroup path, relative to the workdir
* @file: The name of the file in cgroupfs to write to
* @buf: Buffer to write to the file
*
* Write to a file in the given cgroup's directory.
*
* If successful, 0 is returned.
*/
int write_cgroup_file(const char *relative_path, const char *file,
const char *buf)
Annotation
- Immediate include surface: `sched.h`, `sys/mount.h`, `sys/stat.h`, `sys/types.h`, `sys/xattr.h`, `linux/limits.h`, `stdio.h`, `stdlib.h`.
- Detected declarations: `function __enable_controllers`, `function enable_controllers`, `function __write_cgroup_file`, `function write_cgroup_file`, `function write_cgroup_file_parent`, `function setup_cgroup_environment`, `function nftwfunc`, `function join_cgroup_from_top`, `function join_cgroup`, `function join_root_cgroup`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.