2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2007 Roman Divacky
5 * Copyright (c) 2014 Dmitry Chagin <dchagin@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/callout.h>
34 #include <sys/capsicum.h>
35 #include <sys/errno.h>
36 #include <sys/event.h>
37 #include <sys/eventfd.h>
39 #include <sys/filedesc.h>
40 #include <sys/filio.h>
41 #include <sys/limits.h>
43 #include <sys/mutex.h>
46 #include <sys/selinfo.h>
47 #include <sys/specialfd.h>
49 #include <sys/syscallsubr.h>
50 #include <sys/timespec.h>
54 #include <machine/../linux32/linux.h>
55 #include <machine/../linux32/linux32_proto.h>
57 #include <machine/../linux/linux.h>
58 #include <machine/../linux/linux_proto.h>
61 #include <compat/linux/linux_emul.h>
62 #include <compat/linux/linux_event.h>
63 #include <compat/linux/linux_file.h>
64 #include <compat/linux/linux_signal.h>
65 #include <compat/linux/linux_time.h>
66 #include <compat/linux/linux_util.h>
68 typedef uint64_t epoll_udata_t;
74 #if defined(__amd64__)
75 __attribute__((packed))
79 #define LINUX_MAX_EVENTS (INT_MAX / sizeof(struct epoll_event))
81 static int epoll_to_kevent(struct thread *td, int fd,
82 struct epoll_event *l_event, struct kevent *kevent,
84 static void kevent_to_epoll(struct kevent *kevent, struct epoll_event *l_event);
85 static int epoll_kev_copyout(void *arg, struct kevent *kevp, int count);
86 static int epoll_kev_copyin(void *arg, struct kevent *kevp, int count);
87 static int epoll_register_kevent(struct thread *td, struct file *epfp,
88 int fd, int filter, unsigned int flags);
89 static int epoll_fd_registered(struct thread *td, struct file *epfp,
91 static int epoll_delete_all_events(struct thread *td, struct file *epfp,
94 struct epoll_copyin_args {
95 struct kevent *changelist;
98 struct epoll_copyout_args {
99 struct epoll_event *leventlist;
106 typedef uint64_t timerfd_t;
108 static fo_rdwr_t timerfd_read;
109 static fo_ioctl_t timerfd_ioctl;
110 static fo_poll_t timerfd_poll;
111 static fo_kqfilter_t timerfd_kqfilter;
112 static fo_stat_t timerfd_stat;
113 static fo_close_t timerfd_close;
114 static fo_fill_kinfo_t timerfd_fill_kinfo;
116 static struct fileops timerfdops = {
117 .fo_read = timerfd_read,
118 .fo_write = invfo_rdwr,
119 .fo_truncate = invfo_truncate,
120 .fo_ioctl = timerfd_ioctl,
121 .fo_poll = timerfd_poll,
122 .fo_kqfilter = timerfd_kqfilter,
123 .fo_stat = timerfd_stat,
124 .fo_close = timerfd_close,
125 .fo_chmod = invfo_chmod,
126 .fo_chown = invfo_chown,
127 .fo_sendfile = invfo_sendfile,
128 .fo_fill_kinfo = timerfd_fill_kinfo,
129 .fo_flags = DFLAG_PASSABLE
132 static void filt_timerfddetach(struct knote *kn);
133 static int filt_timerfdread(struct knote *kn, long hint);
135 static struct filterops timerfd_rfiltops = {
137 .f_detach = filt_timerfddetach,
138 .f_event = filt_timerfdread
142 clockid_t tfd_clockid;
143 struct itimerspec tfd_time;
144 struct callout tfd_callout;
147 struct selinfo tfd_sel;
151 static void linux_timerfd_expire(void *);
152 static void linux_timerfd_curval(struct timerfd *, struct itimerspec *);
155 epoll_create_common(struct thread *td, int flags)
158 return (kern_kqueue(td, flags, NULL));
161 #ifdef LINUX_LEGACY_SYSCALLS
163 linux_epoll_create(struct thread *td, struct linux_epoll_create_args *args)
167 * args->size is unused. Linux just tests it
168 * and then forgets it as well.
173 return (epoll_create_common(td, 0));
178 linux_epoll_create1(struct thread *td, struct linux_epoll_create1_args *args)
182 if ((args->flags & ~(LINUX_O_CLOEXEC)) != 0)
186 if ((args->flags & LINUX_O_CLOEXEC) != 0)
189 return (epoll_create_common(td, flags));
192 /* Structure converting function from epoll to kevent. */
194 epoll_to_kevent(struct thread *td, int fd, struct epoll_event *l_event,
195 struct kevent *kevent, int *nkevents)
197 uint32_t levents = l_event->events;
198 struct linux_pemuldata *pem;
200 unsigned short kev_flags = EV_ADD | EV_ENABLE;
202 /* flags related to how event is registered */
203 if ((levents & LINUX_EPOLLONESHOT) != 0)
204 kev_flags |= EV_DISPATCH;
205 if ((levents & LINUX_EPOLLET) != 0)
206 kev_flags |= EV_CLEAR;
207 if ((levents & LINUX_EPOLLERR) != 0)
208 kev_flags |= EV_ERROR;
209 if ((levents & LINUX_EPOLLRDHUP) != 0)
212 /* flags related to what event is registered */
213 if ((levents & LINUX_EPOLL_EVRD) != 0) {
214 EV_SET(kevent, fd, EVFILT_READ, kev_flags, 0, 0, 0);
215 kevent->ext[0] = l_event->data;
219 if ((levents & LINUX_EPOLL_EVWR) != 0) {
220 EV_SET(kevent, fd, EVFILT_WRITE, kev_flags, 0, 0, 0);
221 kevent->ext[0] = l_event->data;
225 /* zero event mask is legal */
226 if ((levents & (LINUX_EPOLL_EVRD | LINUX_EPOLL_EVWR)) == 0) {
227 EV_SET(kevent++, fd, EVFILT_READ, EV_ADD|EV_DISABLE, 0, 0, 0);
231 if ((levents & ~(LINUX_EPOLL_EVSUP)) != 0) {
235 KASSERT(pem != NULL, ("epoll proc emuldata not found.\n"));
237 LINUX_PEM_XLOCK(pem);
238 if ((pem->flags & LINUX_XUNSUP_EPOLL) == 0) {
239 pem->flags |= LINUX_XUNSUP_EPOLL;
240 LINUX_PEM_XUNLOCK(pem);
241 linux_msg(td, "epoll_ctl unsupported flags: 0x%x",
244 LINUX_PEM_XUNLOCK(pem);
252 * Structure converting function from kevent to epoll. In a case
253 * this is called on error in registration we store the error in
254 * event->data and pick it up later in linux_epoll_ctl().
257 kevent_to_epoll(struct kevent *kevent, struct epoll_event *l_event)
260 l_event->data = kevent->ext[0];
262 if ((kevent->flags & EV_ERROR) != 0) {
263 l_event->events = LINUX_EPOLLERR;
267 /* XXX EPOLLPRI, EPOLLHUP */
268 switch (kevent->filter) {
270 l_event->events = LINUX_EPOLLIN;
271 if ((kevent->flags & EV_EOF) != 0)
272 l_event->events |= LINUX_EPOLLRDHUP;
275 l_event->events = LINUX_EPOLLOUT;
281 * Copyout callback used by kevent. This converts kevent
282 * events to epoll events and copies them back to the
283 * userspace. This is also called on error on registering
287 epoll_kev_copyout(void *arg, struct kevent *kevp, int count)
289 struct epoll_copyout_args *args;
290 struct epoll_event *eep;
293 args = (struct epoll_copyout_args*) arg;
294 eep = malloc(sizeof(*eep) * count, M_EPOLL, M_WAITOK | M_ZERO);
296 for (i = 0; i < count; i++)
297 kevent_to_epoll(&kevp[i], &eep[i]);
299 error = copyout(eep, args->leventlist, count * sizeof(*eep));
301 args->leventlist += count;
302 args->count += count;
303 } else if (args->error == 0)
311 * Copyin callback used by kevent. This copies already
312 * converted filters from kernel memory to the kevent
313 * internal kernel memory. Hence the memcpy instead of
317 epoll_kev_copyin(void *arg, struct kevent *kevp, int count)
319 struct epoll_copyin_args *args;
321 args = (struct epoll_copyin_args*) arg;
323 memcpy(kevp, args->changelist, count * sizeof(*kevp));
324 args->changelist += count;
330 * Load epoll filter, convert it to kevent filter
331 * and load it into kevent subsystem.
334 linux_epoll_ctl(struct thread *td, struct linux_epoll_ctl_args *args)
336 struct file *epfp, *fp;
337 struct epoll_copyin_args ciargs;
338 struct kevent kev[2];
339 struct kevent_copyops k_ops = { &ciargs,
342 struct epoll_event le;
347 if (args->op != LINUX_EPOLL_CTL_DEL) {
348 error = copyin(args->event, &le, sizeof(le));
353 error = fget(td, args->epfd,
354 cap_rights_init_one(&rights, CAP_KQUEUE_CHANGE), &epfp);
357 if (epfp->f_type != DTYPE_KQUEUE) {
362 /* Protect user data vector from incorrectly supplied fd. */
363 error = fget(td, args->fd,
364 cap_rights_init_one(&rights, CAP_POLL_EVENT), &fp);
368 /* Linux disallows spying on himself */
374 ciargs.changelist = kev;
376 if (args->op != LINUX_EPOLL_CTL_DEL) {
377 error = epoll_to_kevent(td, args->fd, &le, kev, &nchanges);
383 case LINUX_EPOLL_CTL_MOD:
384 error = epoll_delete_all_events(td, epfp, args->fd);
389 case LINUX_EPOLL_CTL_ADD:
390 if (epoll_fd_registered(td, epfp, args->fd)) {
396 case LINUX_EPOLL_CTL_DEL:
397 /* CTL_DEL means unregister this fd with this epoll */
398 error = epoll_delete_all_events(td, epfp, args->fd);
406 error = kern_kevent_fp(td, epfp, nchanges, 0, &k_ops, NULL);
417 * Wait for a filter to be triggered on the epoll file descriptor.
421 linux_epoll_wait_ts(struct thread *td, int epfd, struct epoll_event *events,
422 int maxevents, struct timespec *tsp, sigset_t *uset)
424 struct epoll_copyout_args coargs;
425 struct kevent_copyops k_ops = { &coargs,
433 if (maxevents <= 0 || maxevents > LINUX_MAX_EVENTS)
436 error = fget(td, epfd,
437 cap_rights_init_one(&rights, CAP_KQUEUE_EVENT), &epfp);
440 if (epfp->f_type != DTYPE_KQUEUE) {
445 error = kern_sigprocmask(td, SIG_SETMASK, uset,
449 td->td_pflags |= TDP_OLDMASK;
451 * Make sure that ast() is called on return to
452 * usermode and TDP_OLDMASK is cleared, restoring old
455 ast_sched(td, TDA_SIGSUSPEND);
458 coargs.leventlist = events;
459 coargs.p = td->td_proc;
463 error = kern_kevent_fp(td, epfp, 0, maxevents, &k_ops, tsp);
464 if (error == 0 && coargs.error != 0)
465 error = coargs.error;
468 * kern_kevent might return ENOMEM which is not expected from epoll_wait.
469 * Maybe we should translate that but I don't think it matters at all.
472 td->td_retval[0] = coargs.count;
475 error = kern_sigprocmask(td, SIG_SETMASK, &omask,
483 linux_epoll_wait_common(struct thread *td, int epfd, struct epoll_event *events,
484 int maxevents, int timeout, sigset_t *uset)
486 struct timespec ts, *tsp;
489 * Linux epoll_wait(2) man page states that timeout of -1 causes caller
490 * to block indefinitely. Real implementation does it if any negative
491 * timeout value is passed.
494 /* Convert from milliseconds to timespec. */
495 ts.tv_sec = timeout / 1000;
496 ts.tv_nsec = (timeout % 1000) * 1000000;
501 return (linux_epoll_wait_ts(td, epfd, events, maxevents, tsp, uset));
505 #ifdef LINUX_LEGACY_SYSCALLS
507 linux_epoll_wait(struct thread *td, struct linux_epoll_wait_args *args)
510 return (linux_epoll_wait_common(td, args->epfd, args->events,
511 args->maxevents, args->timeout, NULL));
516 linux_epoll_pwait(struct thread *td, struct linux_epoll_pwait_args *args)
518 sigset_t mask, *pmask;
521 error = linux_copyin_sigset(td, args->mask, sizeof(l_sigset_t),
526 return (linux_epoll_wait_common(td, args->epfd, args->events,
527 args->maxevents, args->timeout, pmask));
530 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
532 linux_epoll_pwait2_64(struct thread *td, struct linux_epoll_pwait2_64_args *args)
534 struct timespec ts, *tsa;
535 sigset_t mask, *pmask;
538 error = linux_copyin_sigset(td, args->mask, sizeof(l_sigset_t),
544 error = linux_get_timespec64(&ts, args->timeout);
551 return (linux_epoll_wait_ts(td, args->epfd, args->events,
552 args->maxevents, tsa, pmask));
556 linux_epoll_pwait2(struct thread *td, struct linux_epoll_pwait2_args *args)
558 struct timespec ts, *tsa;
559 sigset_t mask, *pmask;
562 error = linux_copyin_sigset(td, args->mask, sizeof(l_sigset_t),
568 error = linux_get_timespec(&ts, args->timeout);
575 return (linux_epoll_wait_ts(td, args->epfd, args->events,
576 args->maxevents, tsa, pmask));
578 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
581 epoll_register_kevent(struct thread *td, struct file *epfp, int fd, int filter,
584 struct epoll_copyin_args ciargs;
586 struct kevent_copyops k_ops = { &ciargs,
590 ciargs.changelist = &kev;
591 EV_SET(&kev, fd, filter, flags, 0, 0, 0);
593 return (kern_kevent_fp(td, epfp, 1, 0, &k_ops, NULL));
597 epoll_fd_registered(struct thread *td, struct file *epfp, int fd)
600 * Set empty filter flags to avoid accidental modification of already
601 * registered events. In the case of event re-registration:
602 * 1. If event does not exists kevent() does nothing and returns ENOENT
603 * 2. If event does exists, it's enabled/disabled state is preserved
604 * but fflags, data and udata fields are overwritten. So we can not
605 * set socket lowats and store user's context pointer in udata.
607 if (epoll_register_kevent(td, epfp, fd, EVFILT_READ, 0) != ENOENT ||
608 epoll_register_kevent(td, epfp, fd, EVFILT_WRITE, 0) != ENOENT)
615 epoll_delete_all_events(struct thread *td, struct file *epfp, int fd)
619 error1 = epoll_register_kevent(td, epfp, fd, EVFILT_READ, EV_DELETE);
620 error2 = epoll_register_kevent(td, epfp, fd, EVFILT_WRITE, EV_DELETE);
622 /* return 0 if at least one result positive */
623 return (error1 == 0 ? 0 : error2);
626 #ifdef LINUX_LEGACY_SYSCALLS
628 linux_eventfd(struct thread *td, struct linux_eventfd_args *args)
630 struct specialfd_eventfd ae;
632 bzero(&ae, sizeof(ae));
633 ae.initval = args->initval;
634 return (kern_specialfd(td, SPECIALFD_EVENTFD, &ae));
639 linux_eventfd2(struct thread *td, struct linux_eventfd2_args *args)
641 struct specialfd_eventfd ae;
644 if ((args->flags & ~(LINUX_O_CLOEXEC | LINUX_O_NONBLOCK |
645 LINUX_EFD_SEMAPHORE)) != 0)
648 if ((args->flags & LINUX_O_CLOEXEC) != 0)
649 flags |= EFD_CLOEXEC;
650 if ((args->flags & LINUX_O_NONBLOCK) != 0)
651 flags |= EFD_NONBLOCK;
652 if ((args->flags & LINUX_EFD_SEMAPHORE) != 0)
653 flags |= EFD_SEMAPHORE;
655 bzero(&ae, sizeof(ae));
657 ae.initval = args->initval;
658 return (kern_specialfd(td, SPECIALFD_EVENTFD, &ae));
662 linux_timerfd_create(struct thread *td, struct linux_timerfd_create_args *args)
667 int fflags, fd, error;
669 if ((args->flags & ~LINUX_TFD_CREATE_FLAGS) != 0)
672 error = linux_to_native_clockid(&clockid, args->clockid);
675 if (clockid != CLOCK_REALTIME && clockid != CLOCK_MONOTONIC)
679 if ((args->flags & LINUX_TFD_CLOEXEC) != 0)
682 error = falloc(td, &fp, &fd, fflags);
686 tfd = malloc(sizeof(*tfd), M_EPOLL, M_WAITOK | M_ZERO);
687 tfd->tfd_clockid = clockid;
688 mtx_init(&tfd->tfd_lock, "timerfd", NULL, MTX_DEF);
690 callout_init_mtx(&tfd->tfd_callout, &tfd->tfd_lock, 0);
691 knlist_init_mtx(&tfd->tfd_sel.si_note, &tfd->tfd_lock);
694 if ((args->flags & LINUX_O_NONBLOCK) != 0)
697 finit(fp, fflags, DTYPE_LINUXTFD, tfd, &timerfdops);
700 td->td_retval[0] = fd;
705 timerfd_close(struct file *fp, struct thread *td)
710 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
713 timespecclear(&tfd->tfd_time.it_value);
714 timespecclear(&tfd->tfd_time.it_interval);
716 callout_drain(&tfd->tfd_callout);
718 seldrain(&tfd->tfd_sel);
719 knlist_destroy(&tfd->tfd_sel.si_note);
721 fp->f_ops = &badfileops;
722 mtx_destroy(&tfd->tfd_lock);
729 timerfd_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
730 int flags, struct thread *td)
737 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
740 if (uio->uio_resid < sizeof(timerfd_t))
744 mtx_lock(&tfd->tfd_lock);
746 if (tfd->tfd_canceled) {
748 mtx_unlock(&tfd->tfd_lock);
751 if (tfd->tfd_count == 0) {
752 if ((fp->f_flag & FNONBLOCK) != 0) {
753 mtx_unlock(&tfd->tfd_lock);
756 error = mtx_sleep(&tfd->tfd_count, &tfd->tfd_lock, PCATCH, "ltfdrd", 0);
761 count = tfd->tfd_count;
763 mtx_unlock(&tfd->tfd_lock);
764 error = uiomove(&count, sizeof(timerfd_t), uio);
766 mtx_unlock(&tfd->tfd_lock);
772 timerfd_poll(struct file *fp, int events, struct ucred *active_cred,
779 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
782 mtx_lock(&tfd->tfd_lock);
783 if ((events & (POLLIN|POLLRDNORM)) && tfd->tfd_count > 0)
784 revents |= events & (POLLIN|POLLRDNORM);
786 selrecord(td, &tfd->tfd_sel);
787 mtx_unlock(&tfd->tfd_lock);
793 timerfd_kqfilter(struct file *fp, struct knote *kn)
798 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
801 if (kn->kn_filter == EVFILT_READ)
802 kn->kn_fop = &timerfd_rfiltops;
807 knlist_add(&tfd->tfd_sel.si_note, kn, 0);
813 filt_timerfddetach(struct knote *kn)
815 struct timerfd *tfd = kn->kn_hook;
817 mtx_lock(&tfd->tfd_lock);
818 knlist_remove(&tfd->tfd_sel.si_note, kn, 1);
819 mtx_unlock(&tfd->tfd_lock);
823 filt_timerfdread(struct knote *kn, long hint)
825 struct timerfd *tfd = kn->kn_hook;
827 return (tfd->tfd_count > 0);
831 timerfd_ioctl(struct file *fp, u_long cmd, void *data,
832 struct ucred *active_cred, struct thread *td)
835 if (fp->f_data == NULL || fp->f_type != DTYPE_LINUXTFD)
848 timerfd_stat(struct file *fp, struct stat *st, struct ucred *active_cred)
855 timerfd_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
858 kif->kf_type = KF_TYPE_UNKNOWN;
863 linux_timerfd_clocktime(struct timerfd *tfd, struct timespec *ts)
866 if (tfd->tfd_clockid == CLOCK_REALTIME)
868 else /* CLOCK_MONOTONIC */
873 linux_timerfd_curval(struct timerfd *tfd, struct itimerspec *ots)
877 linux_timerfd_clocktime(tfd, &cts);
878 *ots = tfd->tfd_time;
879 if (ots->it_value.tv_sec != 0 || ots->it_value.tv_nsec != 0) {
880 timespecsub(&ots->it_value, &cts, &ots->it_value);
881 if (ots->it_value.tv_sec < 0 ||
882 (ots->it_value.tv_sec == 0 &&
883 ots->it_value.tv_nsec == 0)) {
884 ots->it_value.tv_sec = 0;
885 ots->it_value.tv_nsec = 1;
891 linux_timerfd_gettime_common(struct thread *td, int fd, struct itimerspec *ots)
897 error = fget(td, fd, &cap_read_rights, &fp);
901 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL) {
906 mtx_lock(&tfd->tfd_lock);
907 linux_timerfd_curval(tfd, ots);
908 mtx_unlock(&tfd->tfd_lock);
916 linux_timerfd_gettime(struct thread *td, struct linux_timerfd_gettime_args *args)
918 struct l_itimerspec lots;
919 struct itimerspec ots;
922 error = linux_timerfd_gettime_common(td, args->fd, &ots);
925 error = native_to_linux_itimerspec(&lots, &ots);
927 error = copyout(&lots, args->old_value, sizeof(lots));
931 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
933 linux_timerfd_gettime64(struct thread *td, struct linux_timerfd_gettime64_args *args)
935 struct l_itimerspec64 lots;
936 struct itimerspec ots;
939 error = linux_timerfd_gettime_common(td, args->fd, &ots);
942 error = native_to_linux_itimerspec64(&lots, &ots);
944 error = copyout(&lots, args->old_value, sizeof(lots));
950 linux_timerfd_settime_common(struct thread *td, int fd, int flags,
951 struct itimerspec *nts, struct itimerspec *oval)
953 struct timespec cts, ts;
959 if ((flags & ~LINUX_TFD_SETTIME_FLAGS) != 0)
962 error = fget(td, fd, &cap_write_rights, &fp);
966 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL) {
971 mtx_lock(&tfd->tfd_lock);
972 if (!timespecisset(&nts->it_value))
973 timespecclear(&nts->it_interval);
975 linux_timerfd_curval(tfd, oval);
977 bcopy(nts, &tfd->tfd_time, sizeof(*nts));
979 if (timespecisset(&nts->it_value)) {
980 linux_timerfd_clocktime(tfd, &cts);
982 if ((flags & LINUX_TFD_TIMER_ABSTIME) == 0) {
983 timespecadd(&tfd->tfd_time.it_value, &cts,
984 &tfd->tfd_time.it_value);
986 timespecsub(&ts, &cts, &ts);
988 TIMESPEC_TO_TIMEVAL(&tv, &ts);
989 callout_reset(&tfd->tfd_callout, tvtohz(&tv),
990 linux_timerfd_expire, tfd);
991 tfd->tfd_canceled = false;
993 tfd->tfd_canceled = true;
994 callout_stop(&tfd->tfd_callout);
996 mtx_unlock(&tfd->tfd_lock);
1004 linux_timerfd_settime(struct thread *td, struct linux_timerfd_settime_args *args)
1006 struct l_itimerspec lots;
1007 struct itimerspec nts, ots, *pots;
1010 error = copyin(args->new_value, &lots, sizeof(lots));
1013 error = linux_to_native_itimerspec(&nts, &lots);
1016 pots = (args->old_value != NULL ? &ots : NULL);
1017 error = linux_timerfd_settime_common(td, args->fd, args->flags,
1019 if (error == 0 && args->old_value != NULL) {
1020 error = native_to_linux_itimerspec(&lots, &ots);
1022 error = copyout(&lots, args->old_value, sizeof(lots));
1027 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1029 linux_timerfd_settime64(struct thread *td, struct linux_timerfd_settime64_args *args)
1031 struct l_itimerspec64 lots;
1032 struct itimerspec nts, ots, *pots;
1035 error = copyin(args->new_value, &lots, sizeof(lots));
1038 error = linux_to_native_itimerspec64(&nts, &lots);
1041 pots = (args->old_value != NULL ? &ots : NULL);
1042 error = linux_timerfd_settime_common(td, args->fd, args->flags,
1044 if (error == 0 && args->old_value != NULL) {
1045 error = native_to_linux_itimerspec64(&lots, &ots);
1047 error = copyout(&lots, args->old_value, sizeof(lots));
1054 linux_timerfd_expire(void *arg)
1056 struct timespec cts, ts;
1058 struct timerfd *tfd;
1060 tfd = (struct timerfd *)arg;
1062 linux_timerfd_clocktime(tfd, &cts);
1063 if (timespeccmp(&cts, &tfd->tfd_time.it_value, >=)) {
1064 if (timespecisset(&tfd->tfd_time.it_interval))
1065 timespecadd(&tfd->tfd_time.it_value,
1066 &tfd->tfd_time.it_interval,
1067 &tfd->tfd_time.it_value);
1069 /* single shot timer */
1070 timespecclear(&tfd->tfd_time.it_value);
1071 if (timespecisset(&tfd->tfd_time.it_value)) {
1072 timespecsub(&tfd->tfd_time.it_value, &cts, &ts);
1073 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1074 callout_reset(&tfd->tfd_callout, tvtohz(&tv),
1075 linux_timerfd_expire, tfd);
1078 KNOTE_LOCKED(&tfd->tfd_sel.si_note, 0);
1079 selwakeup(&tfd->tfd_sel);
1080 wakeup(&tfd->tfd_count);
1081 } else if (timespecisset(&tfd->tfd_time.it_value)) {
1082 timespecsub(&tfd->tfd_time.it_value, &cts, &ts);
1083 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1084 callout_reset(&tfd->tfd_callout, tvtohz(&tv),
1085 linux_timerfd_expire, tfd);