gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / tools / testing / selftests / timens / procfs.c
blob7f14f0fdac84090326972b83b2a18331c252edd1
1 // SPDX-License-Identifier: GPL-2.0
2 #define _GNU_SOURCE
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <math.h>
6 #include <sched.h>
7 #include <stdio.h>
8 #include <stdbool.h>
9 #include <stdlib.h>
10 #include <sys/stat.h>
11 #include <sys/syscall.h>
12 #include <sys/types.h>
13 #include <time.h>
14 #include <unistd.h>
16 #include "log.h"
17 #include "timens.h"
20 * Test shouldn't be run for a day, so add 10 days to child
21 * time and check parent's time to be in the same day.
23 #define MAX_TEST_TIME_SEC (60*5)
24 #define DAY_IN_SEC (60*60*24)
25 #define TEN_DAYS_IN_SEC (10*DAY_IN_SEC)
27 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
29 static int child_ns, parent_ns;
31 static int switch_ns(int fd)
33 if (setns(fd, CLONE_NEWTIME))
34 return pr_perror("setns()");
36 return 0;
39 static int init_namespaces(void)
41 char path[] = "/proc/self/ns/time_for_children";
42 struct stat st1, st2;
44 parent_ns = open(path, O_RDONLY);
45 if (parent_ns <= 0)
46 return pr_perror("Unable to open %s", path);
48 if (fstat(parent_ns, &st1))
49 return pr_perror("Unable to stat the parent timens");
51 if (unshare_timens())
52 return -1;
54 child_ns = open(path, O_RDONLY);
55 if (child_ns <= 0)
56 return pr_perror("Unable to open %s", path);
58 if (fstat(child_ns, &st2))
59 return pr_perror("Unable to stat the timens");
61 if (st1.st_ino == st2.st_ino)
62 return pr_err("The same child_ns after CLONE_NEWTIME");
64 if (_settime(CLOCK_BOOTTIME, TEN_DAYS_IN_SEC))
65 return -1;
67 return 0;
70 static int read_proc_uptime(struct timespec *uptime)
72 unsigned long up_sec, up_nsec;
73 FILE *proc;
75 proc = fopen("/proc/uptime", "r");
76 if (proc == NULL) {
77 pr_perror("Unable to open /proc/uptime");
78 return -1;
81 if (fscanf(proc, "%lu.%02lu", &up_sec, &up_nsec) != 2) {
82 if (errno) {
83 pr_perror("fscanf");
84 return -errno;
86 pr_err("failed to parse /proc/uptime");
87 return -1;
89 fclose(proc);
91 uptime->tv_sec = up_sec;
92 uptime->tv_nsec = up_nsec;
93 return 0;
96 static int check_uptime(void)
98 struct timespec uptime_new, uptime_old;
99 time_t uptime_expected;
100 double prec = MAX_TEST_TIME_SEC;
102 if (switch_ns(parent_ns))
103 return pr_err("switch_ns(%d)", parent_ns);
105 if (read_proc_uptime(&uptime_old))
106 return 1;
108 if (switch_ns(child_ns))
109 return pr_err("switch_ns(%d)", child_ns);
111 if (read_proc_uptime(&uptime_new))
112 return 1;
114 uptime_expected = uptime_old.tv_sec + TEN_DAYS_IN_SEC;
115 if (fabs(difftime(uptime_new.tv_sec, uptime_expected)) > prec) {
116 pr_fail("uptime in /proc/uptime: old %ld, new %ld [%ld]",
117 uptime_old.tv_sec, uptime_new.tv_sec,
118 uptime_old.tv_sec + TEN_DAYS_IN_SEC);
119 return 1;
122 ksft_test_result_pass("Passed for /proc/uptime\n");
123 return 0;
126 int main(int argc, char *argv[])
128 int ret = 0;
130 nscheck();
132 ksft_set_plan(1);
134 if (init_namespaces())
135 return 1;
137 ret |= check_uptime();
139 if (ret)
140 ksft_exit_fail();
141 ksft_exit_pass();
142 return ret;