1 // SPDX-License-Identifier: GPL-2.0
2 #define _GNU_SOURCE
3 #include <sched.h>
4 
5 #include <sys/syscall.h>
6 #include <sys/types.h>
7 #include <sys/wait.h>
8 #include <time.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <stdint.h>
13 #include <signal.h>
14 #include <time.h>
15 
16 #include "log.h"
17 #include "timens.h"
18 
19 int run_test(int clockid, struct timespec now)
20 {
21 	struct itimerspec new_value;
22 	long long elapsed;
23 	timer_t fd;
24 	int i;
25 
26 	for (i = 0; i < 2; i++) {
27 		struct sigevent sevp = {.sigev_notify = SIGEV_NONE};
28 		int flags = 0;
29 
30 		new_value.it_value.tv_sec = 3600;
31 		new_value.it_value.tv_nsec = 0;
32 		new_value.it_interval.tv_sec = 1;
33 		new_value.it_interval.tv_nsec = 0;
34 
35 		if (i == 1) {
36 			new_value.it_value.tv_sec += now.tv_sec;
37 			new_value.it_value.tv_nsec += now.tv_nsec;
38 		}
39 
40 		if (timer_create(clockid, &sevp, &fd) == -1) {
41 			if (errno == ENOSYS) {
42 				ksft_test_result_skip("Posix Clocks & timers are supported\n");
43 				return 0;
44 			}
45 			return pr_perror("timerfd_create");
46 		}
47 
48 		if (i == 1)
49 			flags |= TIMER_ABSTIME;
50 		if (timer_settime(fd, flags, &new_value, NULL) == -1)
51 			return pr_perror("timerfd_settime");
52 
53 		if (timer_gettime(fd, &new_value) == -1)
54 			return pr_perror("timerfd_gettime");
55 
56 		elapsed = new_value.it_value.tv_sec;
57 		if (abs(elapsed - 3600) > 60) {
58 			ksft_test_result_fail("clockid: %d elapsed: %lld\n",
59 					      clockid, elapsed);
60 			return 1;
61 		}
62 	}
63 
64 	ksft_test_result_pass("clockid=%d\n", clockid);
65 
66 	return 0;
67 }
68 
69 int main(int argc, char *argv[])
70 {
71 	int ret, status, len, fd;
72 	char buf[4096];
73 	pid_t pid;
74 	struct timespec btime_now, mtime_now;
75 
76 	nscheck();
77 
78 	ksft_set_plan(3);
79 
80 	clock_gettime(CLOCK_MONOTONIC, &mtime_now);
81 	clock_gettime(CLOCK_BOOTTIME, &btime_now);
82 
83 	if (unshare_timens())
84 		return 1;
85 
86 	len = snprintf(buf, sizeof(buf), "%d %d 0\n%d %d 0",
87 			CLOCK_MONOTONIC, 70 * 24 * 3600,
88 			CLOCK_BOOTTIME, 9 * 24 * 3600);
89 	fd = open("/proc/self/timens_offsets", O_WRONLY);
90 	if (fd < 0)
91 		return pr_perror("/proc/self/timens_offsets");
92 
93 	if (write(fd, buf, len) != len)
94 		return pr_perror("/proc/self/timens_offsets");
95 
96 	close(fd);
97 	mtime_now.tv_sec += 70 * 24 * 3600;
98 	btime_now.tv_sec += 9 * 24 * 3600;
99 
100 	pid = fork();
101 	if (pid < 0)
102 		return pr_perror("Unable to fork");
103 	if (pid == 0) {
104 		ret = 0;
105 		ret |= run_test(CLOCK_BOOTTIME, btime_now);
106 		ret |= run_test(CLOCK_MONOTONIC, mtime_now);
107 		ret |= run_test(CLOCK_BOOTTIME_ALARM, btime_now);
108 
109 		if (ret)
110 			ksft_exit_fail();
111 		ksft_exit_pass();
112 		return ret;
113 	}
114 
115 	if (waitpid(pid, &status, 0) != pid)
116 		return pr_perror("Unable to wait the child process");
117 
118 	if (WIFEXITED(status))
119 		return WEXITSTATUS(status);
120 
121 	return 1;
122 }
123