1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #define _GNU_SOURCE
3*4882a593Smuzhiyun #include <sched.h>
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun #include <linux/unistd.h>
6*4882a593Smuzhiyun #include <linux/futex.h>
7*4882a593Smuzhiyun #include <stdio.h>
8*4882a593Smuzhiyun #include <string.h>
9*4882a593Smuzhiyun #include <sys/syscall.h>
10*4882a593Smuzhiyun #include <sys/types.h>
11*4882a593Smuzhiyun #include <sys/wait.h>
12*4882a593Smuzhiyun #include <time.h>
13*4882a593Smuzhiyun #include <unistd.h>
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #include "log.h"
16*4882a593Smuzhiyun #include "timens.h"
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #define NSEC_PER_SEC 1000000000ULL
19*4882a593Smuzhiyun
run_test(int clockid)20*4882a593Smuzhiyun static int run_test(int clockid)
21*4882a593Smuzhiyun {
22*4882a593Smuzhiyun int futex_op = FUTEX_WAIT_BITSET;
23*4882a593Smuzhiyun struct timespec timeout, end;
24*4882a593Smuzhiyun int val = 0;
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun if (clockid == CLOCK_REALTIME)
27*4882a593Smuzhiyun futex_op |= FUTEX_CLOCK_REALTIME;
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun clock_gettime(clockid, &timeout);
30*4882a593Smuzhiyun timeout.tv_nsec += NSEC_PER_SEC / 10; // 100ms
31*4882a593Smuzhiyun if (timeout.tv_nsec > NSEC_PER_SEC) {
32*4882a593Smuzhiyun timeout.tv_sec++;
33*4882a593Smuzhiyun timeout.tv_nsec -= NSEC_PER_SEC;
34*4882a593Smuzhiyun }
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun if (syscall(__NR_futex, &val, futex_op, 0,
37*4882a593Smuzhiyun &timeout, 0, FUTEX_BITSET_MATCH_ANY) >= 0) {
38*4882a593Smuzhiyun ksft_test_result_fail("futex didn't return ETIMEDOUT\n");
39*4882a593Smuzhiyun return 1;
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun if (errno != ETIMEDOUT) {
43*4882a593Smuzhiyun ksft_test_result_fail("futex didn't return ETIMEDOUT: %s\n",
44*4882a593Smuzhiyun strerror(errno));
45*4882a593Smuzhiyun return 1;
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun clock_gettime(clockid, &end);
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun if (end.tv_sec < timeout.tv_sec ||
51*4882a593Smuzhiyun (end.tv_sec == timeout.tv_sec && end.tv_nsec < timeout.tv_nsec)) {
52*4882a593Smuzhiyun ksft_test_result_fail("futex slept less than 100ms\n");
53*4882a593Smuzhiyun return 1;
54*4882a593Smuzhiyun }
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun ksft_test_result_pass("futex with the %d clockid\n", clockid);
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun return 0;
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun
main(int argc,char * argv[])62*4882a593Smuzhiyun int main(int argc, char *argv[])
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun int status, len, fd;
65*4882a593Smuzhiyun char buf[4096];
66*4882a593Smuzhiyun pid_t pid;
67*4882a593Smuzhiyun struct timespec mtime_now;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun nscheck();
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun ksft_set_plan(2);
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun clock_gettime(CLOCK_MONOTONIC, &mtime_now);
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun if (unshare_timens())
76*4882a593Smuzhiyun return 1;
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun len = snprintf(buf, sizeof(buf), "%d %d 0",
79*4882a593Smuzhiyun CLOCK_MONOTONIC, 70 * 24 * 3600);
80*4882a593Smuzhiyun fd = open("/proc/self/timens_offsets", O_WRONLY);
81*4882a593Smuzhiyun if (fd < 0)
82*4882a593Smuzhiyun return pr_perror("/proc/self/timens_offsets");
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun if (write(fd, buf, len) != len)
85*4882a593Smuzhiyun return pr_perror("/proc/self/timens_offsets");
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun close(fd);
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun pid = fork();
90*4882a593Smuzhiyun if (pid < 0)
91*4882a593Smuzhiyun return pr_perror("Unable to fork");
92*4882a593Smuzhiyun if (pid == 0) {
93*4882a593Smuzhiyun int ret = 0;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun ret |= run_test(CLOCK_REALTIME);
96*4882a593Smuzhiyun ret |= run_test(CLOCK_MONOTONIC);
97*4882a593Smuzhiyun if (ret)
98*4882a593Smuzhiyun ksft_exit_fail();
99*4882a593Smuzhiyun ksft_exit_pass();
100*4882a593Smuzhiyun return 0;
101*4882a593Smuzhiyun }
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun if (waitpid(pid, &status, 0) != pid)
104*4882a593Smuzhiyun return pr_perror("Unable to wait the child process");
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun if (WIFEXITED(status))
107*4882a593Smuzhiyun return WEXITSTATUS(status);
108*4882a593Smuzhiyun
109*4882a593Smuzhiyun return 1;
110*4882a593Smuzhiyun }
111