#include <sys/timerfd.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
int
timerfd_create(int clockid, int flags)
{
int oflags = O_RDWR;
int fd;
if (flags & ~(TFD_NONBLOCK | TFD_CLOEXEC)) {
errno = EINVAL;
return (-1);
}
if (flags & TFD_NONBLOCK)
oflags |= O_NONBLOCK;
if (flags & TFD_CLOEXEC)
oflags |= O_CLOEXEC;
if ((fd = open("/dev/timerfd", oflags)) < 0)
return (-1);
if (ioctl(fd, TIMERFDIOC_CREATE, clockid) != 0) {
(void) close(fd);
return (-1);
}
return (fd);
}
int
timerfd_settime(int fd, int flags, const struct itimerspec *new_value,
struct itimerspec *old_value)
{
timerfd_settime_t st;
int rval;
if (flags & ~(TFD_TIMER_ABSTIME | TFD_TIMER_CANCEL_ON_SET)) {
errno = EINVAL;
return (-1);
}
st.tfd_settime_flags = flags;
st.tfd_settime_value = (uint64_t)(uintptr_t)new_value;
st.tfd_settime_ovalue = (uint64_t)(uintptr_t)old_value;
rval = ioctl(fd, TIMERFDIOC_SETTIME, &st);
if (rval == -1 && errno == ENOTTY) {
errno = EINVAL;
}
return (rval);
}
int
timerfd_gettime(int fd, struct itimerspec *curr_value)
{
int rval = ioctl(fd, TIMERFDIOC_GETTIME, curr_value);
if (rval == -1 && errno == ENOTTY) {
errno = EINVAL;
}
return (rval);
}