2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2007 Roman Divacky
5 * Copyright (c) 2014 Dmitry Chagin
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "opt_compat.h"
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/imgact.h>
38 #include <sys/kernel.h>
39 #include <sys/limits.h>
41 #include <sys/mutex.h>
42 #include <sys/callout.h>
43 #include <sys/capsicum.h>
44 #include <sys/types.h>
47 #include <sys/filedesc.h>
48 #include <sys/filio.h>
49 #include <sys/errno.h>
50 #include <sys/event.h>
53 #include <sys/selinfo.h>
54 #include <sys/specialfd.h>
56 #include <sys/syscallsubr.h>
57 #include <sys/timespec.h>
58 #include <sys/eventfd.h>
61 #include <machine/../linux32/linux.h>
62 #include <machine/../linux32/linux32_proto.h>
64 #include <machine/../linux/linux.h>
65 #include <machine/../linux/linux_proto.h>
68 #include <compat/linux/linux_emul.h>
69 #include <compat/linux/linux_event.h>
70 #include <compat/linux/linux_file.h>
71 #include <compat/linux/linux_timer.h>
72 #include <compat/linux/linux_util.h>
74 typedef uint64_t epoll_udata_t;
80 #if defined(__amd64__)
81 __attribute__((packed))
85 #define LINUX_MAX_EVENTS (INT_MAX / sizeof(struct epoll_event))
87 static int epoll_to_kevent(struct thread *td, int fd,
88 struct epoll_event *l_event, struct kevent *kevent,
90 static void kevent_to_epoll(struct kevent *kevent, struct epoll_event *l_event);
91 static int epoll_kev_copyout(void *arg, struct kevent *kevp, int count);
92 static int epoll_kev_copyin(void *arg, struct kevent *kevp, int count);
93 static int epoll_register_kevent(struct thread *td, struct file *epfp,
94 int fd, int filter, unsigned int flags);
95 static int epoll_fd_registered(struct thread *td, struct file *epfp,
97 static int epoll_delete_all_events(struct thread *td, struct file *epfp,
100 struct epoll_copyin_args {
101 struct kevent *changelist;
104 struct epoll_copyout_args {
105 struct epoll_event *leventlist;
112 typedef uint64_t timerfd_t;
114 static fo_rdwr_t timerfd_read;
115 static fo_ioctl_t timerfd_ioctl;
116 static fo_poll_t timerfd_poll;
117 static fo_kqfilter_t timerfd_kqfilter;
118 static fo_stat_t timerfd_stat;
119 static fo_close_t timerfd_close;
120 static fo_fill_kinfo_t timerfd_fill_kinfo;
122 static struct fileops timerfdops = {
123 .fo_read = timerfd_read,
124 .fo_write = invfo_rdwr,
125 .fo_truncate = invfo_truncate,
126 .fo_ioctl = timerfd_ioctl,
127 .fo_poll = timerfd_poll,
128 .fo_kqfilter = timerfd_kqfilter,
129 .fo_stat = timerfd_stat,
130 .fo_close = timerfd_close,
131 .fo_chmod = invfo_chmod,
132 .fo_chown = invfo_chown,
133 .fo_sendfile = invfo_sendfile,
134 .fo_fill_kinfo = timerfd_fill_kinfo,
135 .fo_flags = DFLAG_PASSABLE
138 static void filt_timerfddetach(struct knote *kn);
139 static int filt_timerfdread(struct knote *kn, long hint);
141 static struct filterops timerfd_rfiltops = {
143 .f_detach = filt_timerfddetach,
144 .f_event = filt_timerfdread
148 clockid_t tfd_clockid;
149 struct itimerspec tfd_time;
150 struct callout tfd_callout;
153 struct selinfo tfd_sel;
157 static void linux_timerfd_expire(void *);
158 static void linux_timerfd_curval(struct timerfd *, struct itimerspec *);
161 epoll_create_common(struct thread *td, int flags)
164 return (kern_kqueue(td, flags, NULL));
167 #ifdef LINUX_LEGACY_SYSCALLS
169 linux_epoll_create(struct thread *td, struct linux_epoll_create_args *args)
173 * args->size is unused. Linux just tests it
174 * and then forgets it as well.
179 return (epoll_create_common(td, 0));
184 linux_epoll_create1(struct thread *td, struct linux_epoll_create1_args *args)
188 if ((args->flags & ~(LINUX_O_CLOEXEC)) != 0)
192 if ((args->flags & LINUX_O_CLOEXEC) != 0)
195 return (epoll_create_common(td, flags));
198 /* Structure converting function from epoll to kevent. */
200 epoll_to_kevent(struct thread *td, int fd, struct epoll_event *l_event,
201 struct kevent *kevent, int *nkevents)
203 uint32_t levents = l_event->events;
204 struct linux_pemuldata *pem;
206 unsigned short kev_flags = EV_ADD | EV_ENABLE;
208 /* flags related to how event is registered */
209 if ((levents & LINUX_EPOLLONESHOT) != 0)
210 kev_flags |= EV_DISPATCH;
211 if ((levents & LINUX_EPOLLET) != 0)
212 kev_flags |= EV_CLEAR;
213 if ((levents & LINUX_EPOLLERR) != 0)
214 kev_flags |= EV_ERROR;
215 if ((levents & LINUX_EPOLLRDHUP) != 0)
218 /* flags related to what event is registered */
219 if ((levents & LINUX_EPOLL_EVRD) != 0) {
220 EV_SET(kevent, fd, EVFILT_READ, kev_flags, 0, 0, 0);
221 kevent->ext[0] = l_event->data;
225 if ((levents & LINUX_EPOLL_EVWR) != 0) {
226 EV_SET(kevent, fd, EVFILT_WRITE, kev_flags, 0, 0, 0);
227 kevent->ext[0] = l_event->data;
231 /* zero event mask is legal */
232 if ((levents & (LINUX_EPOLL_EVRD | LINUX_EPOLL_EVWR)) == 0) {
233 EV_SET(kevent++, fd, EVFILT_READ, EV_ADD|EV_DISABLE, 0, 0, 0);
237 if ((levents & ~(LINUX_EPOLL_EVSUP)) != 0) {
241 KASSERT(pem != NULL, ("epoll proc emuldata not found.\n"));
243 LINUX_PEM_XLOCK(pem);
244 if ((pem->flags & LINUX_XUNSUP_EPOLL) == 0) {
245 pem->flags |= LINUX_XUNSUP_EPOLL;
246 LINUX_PEM_XUNLOCK(pem);
247 linux_msg(td, "epoll_ctl unsupported flags: 0x%x",
250 LINUX_PEM_XUNLOCK(pem);
258 * Structure converting function from kevent to epoll. In a case
259 * this is called on error in registration we store the error in
260 * event->data and pick it up later in linux_epoll_ctl().
263 kevent_to_epoll(struct kevent *kevent, struct epoll_event *l_event)
266 l_event->data = kevent->ext[0];
268 if ((kevent->flags & EV_ERROR) != 0) {
269 l_event->events = LINUX_EPOLLERR;
273 /* XXX EPOLLPRI, EPOLLHUP */
274 switch (kevent->filter) {
276 l_event->events = LINUX_EPOLLIN;
277 if ((kevent->flags & EV_EOF) != 0)
278 l_event->events |= LINUX_EPOLLRDHUP;
281 l_event->events = LINUX_EPOLLOUT;
287 * Copyout callback used by kevent. This converts kevent
288 * events to epoll events and copies them back to the
289 * userspace. This is also called on error on registering
293 epoll_kev_copyout(void *arg, struct kevent *kevp, int count)
295 struct epoll_copyout_args *args;
296 struct epoll_event *eep;
299 args = (struct epoll_copyout_args*) arg;
300 eep = malloc(sizeof(*eep) * count, M_EPOLL, M_WAITOK | M_ZERO);
302 for (i = 0; i < count; i++)
303 kevent_to_epoll(&kevp[i], &eep[i]);
305 error = copyout(eep, args->leventlist, count * sizeof(*eep));
307 args->leventlist += count;
308 args->count += count;
309 } else if (args->error == 0)
317 * Copyin callback used by kevent. This copies already
318 * converted filters from kernel memory to the kevent
319 * internal kernel memory. Hence the memcpy instead of
323 epoll_kev_copyin(void *arg, struct kevent *kevp, int count)
325 struct epoll_copyin_args *args;
327 args = (struct epoll_copyin_args*) arg;
329 memcpy(kevp, args->changelist, count * sizeof(*kevp));
330 args->changelist += count;
336 * Load epoll filter, convert it to kevent filter
337 * and load it into kevent subsystem.
340 linux_epoll_ctl(struct thread *td, struct linux_epoll_ctl_args *args)
342 struct file *epfp, *fp;
343 struct epoll_copyin_args ciargs;
344 struct kevent kev[2];
345 struct kevent_copyops k_ops = { &ciargs,
348 struct epoll_event le;
353 if (args->op != LINUX_EPOLL_CTL_DEL) {
354 error = copyin(args->event, &le, sizeof(le));
359 error = fget(td, args->epfd,
360 cap_rights_init_one(&rights, CAP_KQUEUE_CHANGE), &epfp);
363 if (epfp->f_type != DTYPE_KQUEUE) {
368 /* Protect user data vector from incorrectly supplied fd. */
369 error = fget(td, args->fd,
370 cap_rights_init_one(&rights, CAP_POLL_EVENT), &fp);
374 /* Linux disallows spying on himself */
380 ciargs.changelist = kev;
382 if (args->op != LINUX_EPOLL_CTL_DEL) {
383 error = epoll_to_kevent(td, args->fd, &le, kev, &nchanges);
389 case LINUX_EPOLL_CTL_MOD:
390 error = epoll_delete_all_events(td, epfp, args->fd);
395 case LINUX_EPOLL_CTL_ADD:
396 if (epoll_fd_registered(td, epfp, args->fd)) {
402 case LINUX_EPOLL_CTL_DEL:
403 /* CTL_DEL means unregister this fd with this epoll */
404 error = epoll_delete_all_events(td, epfp, args->fd);
412 error = kern_kevent_fp(td, epfp, nchanges, 0, &k_ops, NULL);
423 * Wait for a filter to be triggered on the epoll file descriptor.
426 linux_epoll_wait_common(struct thread *td, int epfd, struct epoll_event *events,
427 int maxevents, int timeout, sigset_t *uset)
429 struct epoll_copyout_args coargs;
430 struct kevent_copyops k_ops = { &coargs,
433 struct timespec ts, *tsp;
439 if (maxevents <= 0 || maxevents > LINUX_MAX_EVENTS)
442 error = fget(td, epfd,
443 cap_rights_init_one(&rights, CAP_KQUEUE_EVENT), &epfp);
446 if (epfp->f_type != DTYPE_KQUEUE) {
451 error = kern_sigprocmask(td, SIG_SETMASK, uset,
455 td->td_pflags |= TDP_OLDMASK;
457 * Make sure that ast() is called on return to
458 * usermode and TDP_OLDMASK is cleared, restoring old
462 td->td_flags |= TDF_ASTPENDING;
466 coargs.leventlist = events;
467 coargs.p = td->td_proc;
472 * Linux epoll_wait(2) man page states that timeout of -1 causes caller
473 * to block indefinitely. Real implementation does it if any negative
474 * timeout value is passed.
477 /* Convert from milliseconds to timespec. */
478 ts.tv_sec = timeout / 1000;
479 ts.tv_nsec = (timeout % 1000) * 1000000;
485 error = kern_kevent_fp(td, epfp, 0, maxevents, &k_ops, tsp);
486 if (error == 0 && coargs.error != 0)
487 error = coargs.error;
490 * kern_kevent might return ENOMEM which is not expected from epoll_wait.
491 * Maybe we should translate that but I don't think it matters at all.
494 td->td_retval[0] = coargs.count;
497 error = kern_sigprocmask(td, SIG_SETMASK, &omask,
504 #ifdef LINUX_LEGACY_SYSCALLS
506 linux_epoll_wait(struct thread *td, struct linux_epoll_wait_args *args)
509 return (linux_epoll_wait_common(td, args->epfd, args->events,
510 args->maxevents, args->timeout, NULL));
515 linux_epoll_pwait(struct thread *td, struct linux_epoll_pwait_args *args)
517 sigset_t mask, *pmask;
521 if (args->mask != NULL) {
522 if (args->sigsetsize != sizeof(l_sigset_t))
524 error = copyin(args->mask, &lmask, sizeof(l_sigset_t));
527 linux_to_bsd_sigset(&lmask, &mask);
531 return (linux_epoll_wait_common(td, args->epfd, args->events,
532 args->maxevents, args->timeout, pmask));
536 epoll_register_kevent(struct thread *td, struct file *epfp, int fd, int filter,
539 struct epoll_copyin_args ciargs;
541 struct kevent_copyops k_ops = { &ciargs,
545 ciargs.changelist = &kev;
546 EV_SET(&kev, fd, filter, flags, 0, 0, 0);
548 return (kern_kevent_fp(td, epfp, 1, 0, &k_ops, NULL));
552 epoll_fd_registered(struct thread *td, struct file *epfp, int fd)
555 * Set empty filter flags to avoid accidental modification of already
556 * registered events. In the case of event re-registration:
557 * 1. If event does not exists kevent() does nothing and returns ENOENT
558 * 2. If event does exists, it's enabled/disabled state is preserved
559 * but fflags, data and udata fields are overwritten. So we can not
560 * set socket lowats and store user's context pointer in udata.
562 if (epoll_register_kevent(td, epfp, fd, EVFILT_READ, 0) != ENOENT ||
563 epoll_register_kevent(td, epfp, fd, EVFILT_WRITE, 0) != ENOENT)
570 epoll_delete_all_events(struct thread *td, struct file *epfp, int fd)
574 error1 = epoll_register_kevent(td, epfp, fd, EVFILT_READ, EV_DELETE);
575 error2 = epoll_register_kevent(td, epfp, fd, EVFILT_WRITE, EV_DELETE);
577 /* return 0 if at least one result positive */
578 return (error1 == 0 ? 0 : error2);
581 #ifdef LINUX_LEGACY_SYSCALLS
583 linux_eventfd(struct thread *td, struct linux_eventfd_args *args)
585 struct specialfd_eventfd ae;
587 bzero(&ae, sizeof(ae));
588 ae.initval = args->initval;
589 return (kern_specialfd(td, SPECIALFD_EVENTFD, &ae));
594 linux_eventfd2(struct thread *td, struct linux_eventfd2_args *args)
596 struct specialfd_eventfd ae;
599 if ((args->flags & ~(LINUX_O_CLOEXEC | LINUX_O_NONBLOCK |
600 LINUX_EFD_SEMAPHORE)) != 0)
603 if ((args->flags & LINUX_O_CLOEXEC) != 0)
604 flags |= EFD_CLOEXEC;
605 if ((args->flags & LINUX_O_NONBLOCK) != 0)
606 flags |= EFD_NONBLOCK;
607 if ((args->flags & LINUX_EFD_SEMAPHORE) != 0)
608 flags |= EFD_SEMAPHORE;
610 bzero(&ae, sizeof(ae));
612 ae.initval = args->initval;
613 return (kern_specialfd(td, SPECIALFD_EVENTFD, &ae));
617 linux_timerfd_create(struct thread *td, struct linux_timerfd_create_args *args)
619 struct filedesc *fdp;
623 int fflags, fd, error;
625 if ((args->flags & ~LINUX_TFD_CREATE_FLAGS) != 0)
628 error = linux_to_native_clockid(&clockid, args->clockid);
631 if (clockid != CLOCK_REALTIME && clockid != CLOCK_MONOTONIC)
635 if ((args->flags & LINUX_TFD_CLOEXEC) != 0)
638 fdp = td->td_proc->p_fd;
639 error = falloc(td, &fp, &fd, fflags);
643 tfd = malloc(sizeof(*tfd), M_EPOLL, M_WAITOK | M_ZERO);
644 tfd->tfd_clockid = clockid;
645 mtx_init(&tfd->tfd_lock, "timerfd", NULL, MTX_DEF);
647 callout_init_mtx(&tfd->tfd_callout, &tfd->tfd_lock, 0);
648 knlist_init_mtx(&tfd->tfd_sel.si_note, &tfd->tfd_lock);
651 if ((args->flags & LINUX_O_NONBLOCK) != 0)
654 finit(fp, fflags, DTYPE_LINUXTFD, tfd, &timerfdops);
657 td->td_retval[0] = fd;
662 timerfd_close(struct file *fp, struct thread *td)
667 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
670 timespecclear(&tfd->tfd_time.it_value);
671 timespecclear(&tfd->tfd_time.it_interval);
673 mtx_lock(&tfd->tfd_lock);
674 callout_drain(&tfd->tfd_callout);
675 mtx_unlock(&tfd->tfd_lock);
677 seldrain(&tfd->tfd_sel);
678 knlist_destroy(&tfd->tfd_sel.si_note);
680 fp->f_ops = &badfileops;
681 mtx_destroy(&tfd->tfd_lock);
688 timerfd_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
689 int flags, struct thread *td)
696 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
699 if (uio->uio_resid < sizeof(timerfd_t))
703 mtx_lock(&tfd->tfd_lock);
705 if (tfd->tfd_canceled) {
707 mtx_unlock(&tfd->tfd_lock);
710 if (tfd->tfd_count == 0) {
711 if ((fp->f_flag & FNONBLOCK) != 0) {
712 mtx_unlock(&tfd->tfd_lock);
715 error = mtx_sleep(&tfd->tfd_count, &tfd->tfd_lock, PCATCH, "ltfdrd", 0);
720 count = tfd->tfd_count;
722 mtx_unlock(&tfd->tfd_lock);
723 error = uiomove(&count, sizeof(timerfd_t), uio);
725 mtx_unlock(&tfd->tfd_lock);
731 timerfd_poll(struct file *fp, int events, struct ucred *active_cred,
738 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
741 mtx_lock(&tfd->tfd_lock);
742 if ((events & (POLLIN|POLLRDNORM)) && tfd->tfd_count > 0)
743 revents |= events & (POLLIN|POLLRDNORM);
745 selrecord(td, &tfd->tfd_sel);
746 mtx_unlock(&tfd->tfd_lock);
752 timerfd_kqfilter(struct file *fp, struct knote *kn)
757 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
760 if (kn->kn_filter == EVFILT_READ)
761 kn->kn_fop = &timerfd_rfiltops;
766 knlist_add(&tfd->tfd_sel.si_note, kn, 0);
772 filt_timerfddetach(struct knote *kn)
774 struct timerfd *tfd = kn->kn_hook;
776 mtx_lock(&tfd->tfd_lock);
777 knlist_remove(&tfd->tfd_sel.si_note, kn, 1);
778 mtx_unlock(&tfd->tfd_lock);
782 filt_timerfdread(struct knote *kn, long hint)
784 struct timerfd *tfd = kn->kn_hook;
786 return (tfd->tfd_count > 0);
790 timerfd_ioctl(struct file *fp, u_long cmd, void *data,
791 struct ucred *active_cred, struct thread *td)
794 if (fp->f_data == NULL || fp->f_type != DTYPE_LINUXTFD)
807 timerfd_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
815 timerfd_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
818 kif->kf_type = KF_TYPE_UNKNOWN;
823 linux_timerfd_clocktime(struct timerfd *tfd, struct timespec *ts)
826 if (tfd->tfd_clockid == CLOCK_REALTIME)
828 else /* CLOCK_MONOTONIC */
833 linux_timerfd_curval(struct timerfd *tfd, struct itimerspec *ots)
837 linux_timerfd_clocktime(tfd, &cts);
838 *ots = tfd->tfd_time;
839 if (ots->it_value.tv_sec != 0 || ots->it_value.tv_nsec != 0) {
840 timespecsub(&ots->it_value, &cts, &ots->it_value);
841 if (ots->it_value.tv_sec < 0 ||
842 (ots->it_value.tv_sec == 0 &&
843 ots->it_value.tv_nsec == 0)) {
844 ots->it_value.tv_sec = 0;
845 ots->it_value.tv_nsec = 1;
851 linux_timerfd_gettime(struct thread *td, struct linux_timerfd_gettime_args *args)
853 struct l_itimerspec lots;
854 struct itimerspec ots;
859 error = fget(td, args->fd, &cap_read_rights, &fp);
863 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL) {
868 mtx_lock(&tfd->tfd_lock);
869 linux_timerfd_curval(tfd, &ots);
870 mtx_unlock(&tfd->tfd_lock);
872 error = native_to_linux_itimerspec(&lots, &ots);
874 error = copyout(&lots, args->old_value, sizeof(lots));
882 linux_timerfd_settime(struct thread *td, struct linux_timerfd_settime_args *args)
884 struct l_itimerspec lots;
885 struct itimerspec nts, ots;
886 struct timespec cts, ts;
892 if ((args->flags & ~LINUX_TFD_SETTIME_FLAGS) != 0)
895 error = copyin(args->new_value, &lots, sizeof(lots));
898 error = linux_to_native_itimerspec(&nts, &lots);
902 error = fget(td, args->fd, &cap_write_rights, &fp);
906 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL) {
911 mtx_lock(&tfd->tfd_lock);
912 if (!timespecisset(&nts.it_value))
913 timespecclear(&nts.it_interval);
914 if (args->old_value != NULL)
915 linux_timerfd_curval(tfd, &ots);
919 if (timespecisset(&nts.it_value)) {
920 linux_timerfd_clocktime(tfd, &cts);
922 if ((args->flags & LINUX_TFD_TIMER_ABSTIME) == 0) {
923 timespecadd(&tfd->tfd_time.it_value, &cts,
924 &tfd->tfd_time.it_value);
926 timespecsub(&ts, &cts, &ts);
928 TIMESPEC_TO_TIMEVAL(&tv, &ts);
929 callout_reset(&tfd->tfd_callout, tvtohz(&tv),
930 linux_timerfd_expire, tfd);
931 tfd->tfd_canceled = false;
933 tfd->tfd_canceled = true;
934 callout_stop(&tfd->tfd_callout);
936 mtx_unlock(&tfd->tfd_lock);
938 if (args->old_value != NULL) {
939 error = native_to_linux_itimerspec(&lots, &ots);
941 error = copyout(&lots, args->old_value, sizeof(lots));
950 linux_timerfd_expire(void *arg)
952 struct timespec cts, ts;
956 tfd = (struct timerfd *)arg;
958 linux_timerfd_clocktime(tfd, &cts);
959 if (timespeccmp(&cts, &tfd->tfd_time.it_value, >=)) {
960 if (timespecisset(&tfd->tfd_time.it_interval))
961 timespecadd(&tfd->tfd_time.it_value,
962 &tfd->tfd_time.it_interval,
963 &tfd->tfd_time.it_value);
965 /* single shot timer */
966 timespecclear(&tfd->tfd_time.it_value);
967 if (timespecisset(&tfd->tfd_time.it_value)) {
968 timespecsub(&tfd->tfd_time.it_value, &cts, &ts);
969 TIMESPEC_TO_TIMEVAL(&tv, &ts);
970 callout_reset(&tfd->tfd_callout, tvtohz(&tv),
971 linux_timerfd_expire, tfd);
974 KNOTE_LOCKED(&tfd->tfd_sel.si_note, 0);
975 selwakeup(&tfd->tfd_sel);
976 wakeup(&tfd->tfd_count);
977 } else if (timespecisset(&tfd->tfd_time.it_value)) {
978 timespecsub(&tfd->tfd_time.it_value, &cts, &ts);
979 TIMESPEC_TO_TIMEVAL(&tv, &ts);
980 callout_reset(&tfd->tfd_callout, tvtohz(&tv),
981 linux_timerfd_expire, tfd);