tools/lib/api/fs/cgroup.c
Source file repositories/reference/linux-study-clean/tools/lib/api/fs/cgroup.c
File Facts
- System
- Linux kernel
- Corpus path
tools/lib/api/fs/cgroup.c- Extension
.c- Size
- 2169 bytes
- Lines
- 108
- 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
linux/stringify.hsys/types.hsys/stat.hfcntl.hstdio.hstdlib.hstring.hfs.h
Detected Declarations
struct cgroupfs_cache_entryfunction cgroupfs_find_mountpoint
Annotated Snippet
struct cgroupfs_cache_entry {
char subsys[32];
char mountpoint[PATH_MAX];
};
/* just cache last used one */
static struct cgroupfs_cache_entry *cached;
int cgroupfs_find_mountpoint(char *buf, size_t maxlen, const char *subsys)
{
FILE *fp;
char *line = NULL;
size_t len = 0;
char *p, *path;
char mountpoint[PATH_MAX];
if (cached && !strcmp(cached->subsys, subsys)) {
if (strlen(cached->mountpoint) < maxlen) {
strcpy(buf, cached->mountpoint);
return 0;
}
return -1;
}
fp = fopen("/proc/mounts", "r");
if (!fp)
return -1;
/*
* in order to handle split hierarchy, we need to scan /proc/mounts
* and inspect every cgroupfs mount point to find one that has
* the given subsystem. If we found v1, just use it. If not we can
* use v2 path as a fallback.
*/
mountpoint[0] = '\0';
/*
* The /proc/mounts has the follow format:
*
* <devname> <mount point> <fs type> <options> ...
*
*/
while (getline(&line, &len, fp) != -1) {
/* skip devname */
p = strchr(line, ' ');
if (p == NULL)
continue;
/* save the mount point */
path = ++p;
p = strchr(p, ' ');
if (p == NULL)
continue;
*p++ = '\0';
/* check filesystem type */
if (strncmp(p, "cgroup", 6))
continue;
if (p[6] == '2') {
/* save cgroup v2 path */
strcpy(mountpoint, path);
continue;
}
/* now we have cgroup v1, check the options for subsystem */
p += 7;
p = strstr(p, subsys);
if (p == NULL)
continue;
/* sanity check: it should be separated by a space or a comma */
if (!strchr(" ,", p[-1]) || !strchr(" ,", p[strlen(subsys)]))
continue;
strcpy(mountpoint, path);
break;
}
free(line);
fclose(fp);
if (!cached)
cached = calloc(1, sizeof(*cached));
if (cached) {
strncpy(cached->subsys, subsys, sizeof(cached->subsys) - 1);
strcpy(cached->mountpoint, mountpoint);
}
Annotation
- Immediate include surface: `linux/stringify.h`, `sys/types.h`, `sys/stat.h`, `fcntl.h`, `stdio.h`, `stdlib.h`, `string.h`, `fs.h`.
- Detected declarations: `struct cgroupfs_cache_entry`, `function cgroupfs_find_mountpoint`.
- 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.