tools/testing/selftests/cgroup/lib/cgroup_util.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/cgroup/lib/cgroup_util.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/cgroup/lib/cgroup_util.c- Extension
.c- Size
- 12704 bytes
- Lines
- 680
- 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
errno.hfcntl.hlinux/limits.hpoll.hsignal.hstdio.hstdlib.hstring.hsys/inotify.hsys/stat.hsys/types.hsys/wait.hunistd.hcgroup_util.h../../clone3/clone3_selftests.h
Detected Declarations
function read_textfunction write_textfunction cg_readfunction cg_read_strcmpfunction cg_read_strcmp_waitfunction cg_read_strstrfunction cg_read_longfunction cg_read_long_fdfunction cg_read_key_longfunction cg_read_key_long_pollfunction cg_read_lcfunction cg_writefunction cg_openfunction cg_write_numericfunction cg_find_rootfunction cg_find_controller_rootfunction cg_find_unified_rootfunction cg_createfunction cg_wait_for_proc_countfunction cg_killallfunction cg_destroyfunction cg_enterfunction cg_enter_currentfunction cg_enter_current_threadfunction cg_runfunction clone_into_cgroupfunction clone_reapfunction dirfd_open_opathfunction clone_into_cgroup_run_nowaitfunction cg_run_nowaitfunction proc_mount_containsfunction cgroup_featurefunction proc_read_textfunction proc_read_strstrfunction clone_into_cgroup_run_waitfunction __prepare_for_waitfunction cg_prepare_for_waitfunction memcg_prepare_for_waitfunction cg_wait_for
Annotated Snippet
if (strcmp(type, "cgroup") == 0) {
if (!controller || !strstr(options, controller))
continue;
} else if (strcmp(type, "cgroup2") == 0) {
if (controller &&
cg_read_strstr(mount, "cgroup.controllers", controller))
continue;
} else {
continue;
}
strncpy(root, mount, len);
if (nsdelegate)
*nsdelegate = !!strstr(options, "nsdelegate");
return 0;
}
return -1;
}
int cg_find_controller_root(char *root, size_t len, const char *controller)
{
return cg_find_root(root, len, controller, NULL);
}
int cg_find_unified_root(char *root, size_t len, bool *nsdelegate)
{
return cg_find_root(root, len, NULL, nsdelegate);
}
int cg_create(const char *cgroup)
{
return mkdir(cgroup, 0755);
}
int cg_wait_for_proc_count(const char *cgroup, int count)
{
char buf[10 * BUF_SIZE] = {0};
int attempts;
char *ptr;
for (attempts = 10; attempts >= 0; attempts--) {
int nr = 0;
if (cg_read(cgroup, "cgroup.procs", buf, sizeof(buf)))
break;
for (ptr = buf; *ptr; ptr++)
if (*ptr == '\n')
nr++;
if (nr >= count)
return 0;
usleep(100000);
}
return -1;
}
int cg_killall(const char *cgroup)
{
char buf[BUF_SIZE];
char *ptr = buf;
/* If cgroup.kill exists use it. */
if (!cg_write(cgroup, "cgroup.kill", "1"))
return 0;
if (cg_read(cgroup, "cgroup.procs", buf, sizeof(buf)))
return -1;
while (ptr < buf + sizeof(buf)) {
int pid = strtol(ptr, &ptr, 10);
if (pid == 0)
break;
if (*ptr)
ptr++;
else
break;
if (kill(pid, SIGKILL))
return -1;
}
return 0;
}
int cg_destroy(const char *cgroup)
Annotation
- Immediate include surface: `errno.h`, `fcntl.h`, `linux/limits.h`, `poll.h`, `signal.h`, `stdio.h`, `stdlib.h`, `string.h`.
- Detected declarations: `function read_text`, `function write_text`, `function cg_read`, `function cg_read_strcmp`, `function cg_read_strcmp_wait`, `function cg_read_strstr`, `function cg_read_long`, `function cg_read_long_fd`, `function cg_read_key_long`, `function cg_read_key_long_poll`.
- 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.