1 // SPDX-License-Identifier: GPL-2.0
5 #include <sys/timerfd.h>
6 #include <sys/syscall.h>
18 static int tclock_gettime(clock_t clockid
, struct timespec
*now
)
20 if (clockid
== CLOCK_BOOTTIME_ALARM
)
21 clockid
= CLOCK_BOOTTIME
;
22 return clock_gettime(clockid
, now
);
25 int run_test(int clockid
, struct timespec now
)
27 struct itimerspec new_value
;
31 if (check_skip(clockid
))
34 if (tclock_gettime(clockid
, &now
))
35 return pr_perror("clock_gettime(%d)", clockid
);
37 for (i
= 0; i
< 2; i
++) {
40 new_value
.it_value
.tv_sec
= 3600;
41 new_value
.it_value
.tv_nsec
= 0;
42 new_value
.it_interval
.tv_sec
= 1;
43 new_value
.it_interval
.tv_nsec
= 0;
46 new_value
.it_value
.tv_sec
+= now
.tv_sec
;
47 new_value
.it_value
.tv_nsec
+= now
.tv_nsec
;
50 fd
= timerfd_create(clockid
, 0);
52 return pr_perror("timerfd_create(%d)", clockid
);
55 flags
|= TFD_TIMER_ABSTIME
;
57 if (timerfd_settime(fd
, flags
, &new_value
, NULL
))
58 return pr_perror("timerfd_settime(%d)", clockid
);
60 if (timerfd_gettime(fd
, &new_value
))
61 return pr_perror("timerfd_gettime(%d)", clockid
);
63 elapsed
= new_value
.it_value
.tv_sec
;
64 if (llabs(elapsed
- 3600) > 60) {
65 ksft_test_result_fail("clockid: %d elapsed: %lld\n",
73 ksft_test_result_pass("clockid=%d\n", clockid
);
78 int main(int argc
, char *argv
[])
80 int ret
, status
, len
, fd
;
83 struct timespec btime_now
, mtime_now
;
87 check_supported_timers();
91 clock_gettime(CLOCK_MONOTONIC
, &mtime_now
);
92 clock_gettime(CLOCK_BOOTTIME
, &btime_now
);
97 len
= snprintf(buf
, sizeof(buf
), "%d %d 0\n%d %d 0",
98 CLOCK_MONOTONIC
, 70 * 24 * 3600,
99 CLOCK_BOOTTIME
, 9 * 24 * 3600);
100 fd
= open("/proc/self/timens_offsets", O_WRONLY
);
102 return pr_perror("/proc/self/timens_offsets");
104 if (write(fd
, buf
, len
) != len
)
105 return pr_perror("/proc/self/timens_offsets");
108 mtime_now
.tv_sec
+= 70 * 24 * 3600;
109 btime_now
.tv_sec
+= 9 * 24 * 3600;
113 return pr_perror("Unable to fork");
116 ret
|= run_test(CLOCK_BOOTTIME
, btime_now
);
117 ret
|= run_test(CLOCK_MONOTONIC
, mtime_now
);
118 ret
|= run_test(CLOCK_BOOTTIME_ALARM
, btime_now
);
126 if (waitpid(pid
, &status
, 0) != pid
)
127 return pr_perror("Unable to wait the child process");
129 if (WIFEXITED(status
))
130 return WEXITSTATUS(status
);