1570af302Sopenharmony_ci#include <sys/timerfd.h>
2570af302Sopenharmony_ci#include <errno.h>
3570af302Sopenharmony_ci#include "syscall.h"
4570af302Sopenharmony_ci
5570af302Sopenharmony_ci#define IS32BIT(x) !((x)+0x80000000ULL>>32)
6570af302Sopenharmony_ci
7570af302Sopenharmony_ciint timerfd_create(int clockid, int flags)
8570af302Sopenharmony_ci{
9570af302Sopenharmony_ci	return syscall(SYS_timerfd_create, clockid, flags);
10570af302Sopenharmony_ci}
11570af302Sopenharmony_ci
12570af302Sopenharmony_ciint timerfd_settime(int fd, int flags, const struct itimerspec *new, struct itimerspec *old)
13570af302Sopenharmony_ci{
14570af302Sopenharmony_ci#ifdef SYS_timerfd_settime64
15570af302Sopenharmony_ci	time_t is = new->it_interval.tv_sec, vs = new->it_value.tv_sec;
16570af302Sopenharmony_ci	long ins = new->it_interval.tv_nsec, vns = new->it_value.tv_nsec;
17570af302Sopenharmony_ci	int r = -ENOSYS;
18570af302Sopenharmony_ci	if (SYS_timerfd_settime == SYS_timerfd_settime64
19570af302Sopenharmony_ci	    || !IS32BIT(is) || !IS32BIT(vs) || (sizeof(time_t)>4 && old))
20570af302Sopenharmony_ci		r = __syscall(SYS_timerfd_settime64, fd, flags,
21570af302Sopenharmony_ci			((long long[]){is, ins, vs, vns}), old);
22570af302Sopenharmony_ci	if (SYS_timerfd_settime == SYS_timerfd_settime64 || r!=-ENOSYS)
23570af302Sopenharmony_ci		return __syscall_ret(r);
24570af302Sopenharmony_ci	if (!IS32BIT(is) || !IS32BIT(vs))
25570af302Sopenharmony_ci		return __syscall_ret(-ENOTSUP);
26570af302Sopenharmony_ci	long old32[4];
27570af302Sopenharmony_ci	r = __syscall(SYS_timerfd_settime, fd, flags,
28570af302Sopenharmony_ci		((long[]){is, ins, vs, vns}), old32);
29570af302Sopenharmony_ci	if (!r && old) {
30570af302Sopenharmony_ci		old->it_interval.tv_sec = old32[0];
31570af302Sopenharmony_ci		old->it_interval.tv_nsec = old32[1];
32570af302Sopenharmony_ci		old->it_value.tv_sec = old32[2];
33570af302Sopenharmony_ci		old->it_value.tv_nsec = old32[3];
34570af302Sopenharmony_ci	}
35570af302Sopenharmony_ci	return __syscall_ret(r);
36570af302Sopenharmony_ci#endif
37570af302Sopenharmony_ci	return syscall(SYS_timerfd_settime, fd, flags, new, old);
38570af302Sopenharmony_ci}
39570af302Sopenharmony_ci
40570af302Sopenharmony_ciint timerfd_gettime(int fd, struct itimerspec *cur)
41570af302Sopenharmony_ci{
42570af302Sopenharmony_ci#ifdef SYS_timerfd_gettime64
43570af302Sopenharmony_ci	int r = -ENOSYS;
44570af302Sopenharmony_ci	if (sizeof(time_t) > 4)
45570af302Sopenharmony_ci		r = __syscall(SYS_timerfd_gettime64, fd, cur);
46570af302Sopenharmony_ci	if (SYS_timerfd_gettime == SYS_timerfd_gettime64 || r!=-ENOSYS)
47570af302Sopenharmony_ci		return __syscall_ret(r);
48570af302Sopenharmony_ci	long cur32[4];
49570af302Sopenharmony_ci	r = __syscall(SYS_timerfd_gettime, fd, cur32);
50570af302Sopenharmony_ci	if (!r) {
51570af302Sopenharmony_ci		cur->it_interval.tv_sec = cur32[0];
52570af302Sopenharmony_ci		cur->it_interval.tv_nsec = cur32[1];
53570af302Sopenharmony_ci		cur->it_value.tv_sec = cur32[2];
54570af302Sopenharmony_ci		cur->it_value.tv_nsec = cur32[3];
55570af302Sopenharmony_ci	}
56570af302Sopenharmony_ci	return __syscall_ret(r);
57570af302Sopenharmony_ci#endif
58570af302Sopenharmony_ci	return syscall(SYS_timerfd_gettime, fd, cur);
59570af302Sopenharmony_ci}
60