1 // SPDX-License-Identifier: GPL-2.0
5 #include <sys/syscall.h>
18 int run_test(int clockid
, struct timespec now
)
20 struct itimerspec new_value
;
25 if (check_skip(clockid
))
28 for (i
= 0; i
< 2; i
++) {
29 struct sigevent sevp
= {.sigev_notify
= SIGEV_NONE
};
32 new_value
.it_value
.tv_sec
= 3600;
33 new_value
.it_value
.tv_nsec
= 0;
34 new_value
.it_interval
.tv_sec
= 1;
35 new_value
.it_interval
.tv_nsec
= 0;
38 new_value
.it_value
.tv_sec
+= now
.tv_sec
;
39 new_value
.it_value
.tv_nsec
+= now
.tv_nsec
;
42 if (timer_create(clockid
, &sevp
, &fd
) == -1) {
43 if (errno
== ENOSYS
) {
44 ksft_test_result_skip("Posix Clocks & timers are supported\n");
47 return pr_perror("timerfd_create");
51 flags
|= TIMER_ABSTIME
;
52 if (timer_settime(fd
, flags
, &new_value
, NULL
) == -1)
53 return pr_perror("timerfd_settime");
55 if (timer_gettime(fd
, &new_value
) == -1)
56 return pr_perror("timerfd_gettime");
58 elapsed
= new_value
.it_value
.tv_sec
;
59 if (llabs(elapsed
- 3600) > 60) {
60 ksft_test_result_fail("clockid: %d elapsed: %lld\n",
66 ksft_test_result_pass("clockid=%d\n", clockid
);
71 int main(int argc
, char *argv
[])
73 int ret
, status
, len
, fd
;
76 struct timespec btime_now
, mtime_now
;
80 check_supported_timers();
84 clock_gettime(CLOCK_MONOTONIC
, &mtime_now
);
85 clock_gettime(CLOCK_BOOTTIME
, &btime_now
);
90 len
= snprintf(buf
, sizeof(buf
), "%d %d 0\n%d %d 0",
91 CLOCK_MONOTONIC
, 70 * 24 * 3600,
92 CLOCK_BOOTTIME
, 9 * 24 * 3600);
93 fd
= open("/proc/self/timens_offsets", O_WRONLY
);
95 return pr_perror("/proc/self/timens_offsets");
97 if (write(fd
, buf
, len
) != len
)
98 return pr_perror("/proc/self/timens_offsets");
101 mtime_now
.tv_sec
+= 70 * 24 * 3600;
102 btime_now
.tv_sec
+= 9 * 24 * 3600;
106 return pr_perror("Unable to fork");
109 ret
|= run_test(CLOCK_BOOTTIME
, btime_now
);
110 ret
|= run_test(CLOCK_MONOTONIC
, mtime_now
);
111 ret
|= run_test(CLOCK_BOOTTIME_ALARM
, btime_now
);
119 if (waitpid(pid
, &status
, 0) != pid
)
120 return pr_perror("Unable to wait the child process");
122 if (WIFEXITED(status
))
123 return WEXITSTATUS(status
);