tools/testing/selftests/timens/procfs.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/timens/procfs.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/timens/procfs.c- Extension
.c- Size
- 3989 bytes
- Lines
- 200
- 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.hmath.hsched.hstdio.hstdbool.hstdlib.hsys/stat.hsys/syscall.hsys/types.htime.hunistd.hlog.htimens.h
Detected Declarations
function switch_nsfunction init_namespacesfunction read_proc_uptimefunction read_proc_stat_btimefunction check_uptimefunction check_stat_btimefunction main
Annotated Snippet
if (errno) {
pr_perror("fscanf");
return -errno;
}
pr_err("failed to parse /proc/uptime");
return -1;
}
fclose(proc);
uptime->tv_sec = up_sec;
uptime->tv_nsec = up_nsec;
return 0;
}
static int read_proc_stat_btime(unsigned long long *boottime_sec)
{
FILE *proc;
char line_buf[2048];
proc = fopen("/proc/stat", "r");
if (proc == NULL) {
pr_perror("Unable to open /proc/stat");
return -1;
}
while (fgets(line_buf, 2048, proc)) {
if (sscanf(line_buf, "btime %llu", boottime_sec) != 1)
continue;
fclose(proc);
return 0;
}
if (errno) {
pr_perror("fscanf");
fclose(proc);
return -errno;
}
pr_err("failed to parse /proc/stat");
fclose(proc);
return -1;
}
static int check_uptime(void)
{
struct timespec uptime_new, uptime_old;
time_t uptime_expected;
double prec = MAX_TEST_TIME_SEC;
if (switch_ns(parent_ns))
return pr_err("switch_ns(%d)", parent_ns);
if (read_proc_uptime(&uptime_old))
return 1;
if (switch_ns(child_ns))
return pr_err("switch_ns(%d)", child_ns);
if (read_proc_uptime(&uptime_new))
return 1;
uptime_expected = uptime_old.tv_sec + TEN_DAYS_IN_SEC;
if (fabs(difftime(uptime_new.tv_sec, uptime_expected)) > prec) {
pr_fail("uptime in /proc/uptime: old %ld, new %ld [%ld]",
uptime_old.tv_sec, uptime_new.tv_sec,
uptime_old.tv_sec + TEN_DAYS_IN_SEC);
return 1;
}
ksft_test_result_pass("Passed for /proc/uptime\n");
return 0;
}
static int check_stat_btime(void)
{
unsigned long long btime_new, btime_old;
unsigned long long btime_expected;
if (switch_ns(parent_ns))
return pr_err("switch_ns(%d)", parent_ns);
if (read_proc_stat_btime(&btime_old))
return 1;
if (switch_ns(child_ns))
return pr_err("switch_ns(%d)", child_ns);
if (read_proc_stat_btime(&btime_new))
return 1;
btime_expected = btime_old - TEN_DAYS_IN_SEC;
if (btime_new != btime_expected) {
Annotation
- Immediate include surface: `errno.h`, `fcntl.h`, `math.h`, `sched.h`, `stdio.h`, `stdbool.h`, `stdlib.h`, `sys/stat.h`.
- Detected declarations: `function switch_ns`, `function init_namespaces`, `function read_proc_uptime`, `function read_proc_stat_btime`, `function check_uptime`, `function check_stat_btime`, `function main`.
- 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.