2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 "opt_compat.h"
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/imgact.h>
37 #include <sys/kernel.h>
38 #include <sys/limits.h>
40 #include <sys/mutex.h>
41 #include <sys/callout.h>
42 #include <sys/capsicum.h>
43 #include <sys/types.h>
46 #include <sys/filedesc.h>
47 #include <sys/filio.h>
48 #include <sys/errno.h>
49 #include <sys/event.h>
52 #include <sys/selinfo.h>
53 #include <sys/specialfd.h>
55 #include <sys/syscallsubr.h>
56 #include <sys/timespec.h>
57 #include <sys/eventfd.h>
60 #include <machine/../linux32/linux.h>
61 #include <machine/../linux32/linux32_proto.h>
63 #include <machine/../linux/linux.h>
64 #include <machine/../linux/linux_proto.h>
67 #include <compat/linux/linux_emul.h>
68 #include <compat/linux/linux_event.h>
69 #include <compat/linux/linux_file.h>
70 #include <compat/linux/linux_signal.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.
427 linux_epoll_wait_ts(struct thread *td, int epfd, struct epoll_event *events,
428 int maxevents, struct timespec *tsp, sigset_t *uset)
430 struct epoll_copyout_args coargs;
431 struct kevent_copyops k_ops = { &coargs,
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
461 ast_sched(td, TDA_SIGSUSPEND);
464 coargs.leventlist = events;
465 coargs.p = td->td_proc;
469 error = kern_kevent_fp(td, epfp, 0, maxevents, &k_ops, tsp);
470 if (error == 0 && coargs.error != 0)
471 error = coargs.error;
474 * kern_kevent might return ENOMEM which is not expected from epoll_wait.
475 * Maybe we should translate that but I don't think it matters at all.
478 td->td_retval[0] = coargs.count;
481 error = kern_sigprocmask(td, SIG_SETMASK, &omask,
489 linux_epoll_wait_common(struct thread *td, int epfd, struct epoll_event *events,
490 int maxevents, int timeout, sigset_t *uset)
492 struct timespec ts, *tsp;
495 * Linux epoll_wait(2) man page states that timeout of -1 causes caller
496 * to block indefinitely. Real implementation does it if any negative
497 * timeout value is passed.
500 /* Convert from milliseconds to timespec. */
501 ts.tv_sec = timeout / 1000;
502 ts.tv_nsec = (timeout % 1000) * 1000000;
507 return (linux_epoll_wait_ts(td, epfd, events, maxevents, tsp, uset));
511 #ifdef LINUX_LEGACY_SYSCALLS
513 linux_epoll_wait(struct thread *td, struct linux_epoll_wait_args *args)
516 return (linux_epoll_wait_common(td, args->epfd, args->events,
517 args->maxevents, args->timeout, NULL));
522 linux_epoll_pwait(struct thread *td, struct linux_epoll_pwait_args *args)
524 sigset_t mask, *pmask;
527 error = linux_copyin_sigset(td, args->mask, sizeof(l_sigset_t),
532 return (linux_epoll_wait_common(td, args->epfd, args->events,
533 args->maxevents, args->timeout, pmask));
536 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
538 linux_epoll_pwait2_64(struct thread *td, struct linux_epoll_pwait2_64_args *args)
540 struct timespec ts, *tsa;
541 sigset_t mask, *pmask;
544 error = linux_copyin_sigset(td, args->mask, sizeof(l_sigset_t),
550 error = linux_get_timespec64(&ts, args->timeout);
557 return (linux_epoll_wait_ts(td, args->epfd, args->events,
558 args->maxevents, tsa, pmask));
562 linux_epoll_pwait2(struct thread *td, struct linux_epoll_pwait2_args *args)
564 struct timespec ts, *tsa;
565 sigset_t mask, *pmask;
568 error = linux_copyin_sigset(td, args->mask, sizeof(l_sigset_t),
574 error = linux_get_timespec(&ts, args->timeout);
581 return (linux_epoll_wait_ts(td, args->epfd, args->events,
582 args->maxevents, tsa, pmask));
584 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
587 epoll_register_kevent(struct thread *td, struct file *epfp, int fd, int filter,
590 struct epoll_copyin_args ciargs;
592 struct kevent_copyops k_ops = { &ciargs,
596 ciargs.changelist = &kev;
597 EV_SET(&kev, fd, filter, flags, 0, 0, 0);
599 return (kern_kevent_fp(td, epfp, 1, 0, &k_ops, NULL));
603 epoll_fd_registered(struct thread *td, struct file *epfp, int fd)
606 * Set empty filter flags to avoid accidental modification of already
607 * registered events. In the case of event re-registration:
608 * 1. If event does not exists kevent() does nothing and returns ENOENT
609 * 2. If event does exists, it's enabled/disabled state is preserved
610 * but fflags, data and udata fields are overwritten. So we can not
611 * set socket lowats and store user's context pointer in udata.
613 if (epoll_register_kevent(td, epfp, fd, EVFILT_READ, 0) != ENOENT ||
614 epoll_register_kevent(td, epfp, fd, EVFILT_WRITE, 0) != ENOENT)
621 epoll_delete_all_events(struct thread *td, struct file *epfp, int fd)
625 error1 = epoll_register_kevent(td, epfp, fd, EVFILT_READ, EV_DELETE);
626 error2 = epoll_register_kevent(td, epfp, fd, EVFILT_WRITE, EV_DELETE);
628 /* return 0 if at least one result positive */
629 return (error1 == 0 ? 0 : error2);
632 #ifdef LINUX_LEGACY_SYSCALLS
634 linux_eventfd(struct thread *td, struct linux_eventfd_args *args)
636 struct specialfd_eventfd ae;
638 bzero(&ae, sizeof(ae));
639 ae.initval = args->initval;
640 return (kern_specialfd(td, SPECIALFD_EVENTFD, &ae));
645 linux_eventfd2(struct thread *td, struct linux_eventfd2_args *args)
647 struct specialfd_eventfd ae;
650 if ((args->flags & ~(LINUX_O_CLOEXEC | LINUX_O_NONBLOCK |
651 LINUX_EFD_SEMAPHORE)) != 0)
654 if ((args->flags & LINUX_O_CLOEXEC) != 0)
655 flags |= EFD_CLOEXEC;
656 if ((args->flags & LINUX_O_NONBLOCK) != 0)
657 flags |= EFD_NONBLOCK;
658 if ((args->flags & LINUX_EFD_SEMAPHORE) != 0)
659 flags |= EFD_SEMAPHORE;
661 bzero(&ae, sizeof(ae));
663 ae.initval = args->initval;
664 return (kern_specialfd(td, SPECIALFD_EVENTFD, &ae));
668 linux_timerfd_create(struct thread *td, struct linux_timerfd_create_args *args)
673 int fflags, fd, error;
675 if ((args->flags & ~LINUX_TFD_CREATE_FLAGS) != 0)
678 error = linux_to_native_clockid(&clockid, args->clockid);
681 if (clockid != CLOCK_REALTIME && clockid != CLOCK_MONOTONIC)
685 if ((args->flags & LINUX_TFD_CLOEXEC) != 0)
688 error = falloc(td, &fp, &fd, fflags);
692 tfd = malloc(sizeof(*tfd), M_EPOLL, M_WAITOK | M_ZERO);
693 tfd->tfd_clockid = clockid;
694 mtx_init(&tfd->tfd_lock, "timerfd", NULL, MTX_DEF);
696 callout_init_mtx(&tfd->tfd_callout, &tfd->tfd_lock, 0);
697 knlist_init_mtx(&tfd->tfd_sel.si_note, &tfd->tfd_lock);
700 if ((args->flags & LINUX_O_NONBLOCK) != 0)
703 finit(fp, fflags, DTYPE_LINUXTFD, tfd, &timerfdops);
706 td->td_retval[0] = fd;
711 timerfd_close(struct file *fp, struct thread *td)
716 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
719 timespecclear(&tfd->tfd_time.it_value);
720 timespecclear(&tfd->tfd_time.it_interval);
722 callout_drain(&tfd->tfd_callout);
724 seldrain(&tfd->tfd_sel);
725 knlist_destroy(&tfd->tfd_sel.si_note);
727 fp->f_ops = &badfileops;
728 mtx_destroy(&tfd->tfd_lock);
735 timerfd_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
736 int flags, struct thread *td)
743 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
746 if (uio->uio_resid < sizeof(timerfd_t))
750 mtx_lock(&tfd->tfd_lock);
752 if (tfd->tfd_canceled) {
754 mtx_unlock(&tfd->tfd_lock);
757 if (tfd->tfd_count == 0) {
758 if ((fp->f_flag & FNONBLOCK) != 0) {
759 mtx_unlock(&tfd->tfd_lock);
762 error = mtx_sleep(&tfd->tfd_count, &tfd->tfd_lock, PCATCH, "ltfdrd", 0);
767 count = tfd->tfd_count;
769 mtx_unlock(&tfd->tfd_lock);
770 error = uiomove(&count, sizeof(timerfd_t), uio);
772 mtx_unlock(&tfd->tfd_lock);
778 timerfd_poll(struct file *fp, int events, struct ucred *active_cred,
785 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
788 mtx_lock(&tfd->tfd_lock);
789 if ((events & (POLLIN|POLLRDNORM)) && tfd->tfd_count > 0)
790 revents |= events & (POLLIN|POLLRDNORM);
792 selrecord(td, &tfd->tfd_sel);
793 mtx_unlock(&tfd->tfd_lock);
799 timerfd_kqfilter(struct file *fp, struct knote *kn)
804 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
807 if (kn->kn_filter == EVFILT_READ)
808 kn->kn_fop = &timerfd_rfiltops;
813 knlist_add(&tfd->tfd_sel.si_note, kn, 0);
819 filt_timerfddetach(struct knote *kn)
821 struct timerfd *tfd = kn->kn_hook;
823 mtx_lock(&tfd->tfd_lock);
824 knlist_remove(&tfd->tfd_sel.si_note, kn, 1);
825 mtx_unlock(&tfd->tfd_lock);
829 filt_timerfdread(struct knote *kn, long hint)
831 struct timerfd *tfd = kn->kn_hook;
833 return (tfd->tfd_count > 0);
837 timerfd_ioctl(struct file *fp, u_long cmd, void *data,
838 struct ucred *active_cred, struct thread *td)
841 if (fp->f_data == NULL || fp->f_type != DTYPE_LINUXTFD)
854 timerfd_stat(struct file *fp, struct stat *st, struct ucred *active_cred)
861 timerfd_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
864 kif->kf_type = KF_TYPE_UNKNOWN;
869 linux_timerfd_clocktime(struct timerfd *tfd, struct timespec *ts)
872 if (tfd->tfd_clockid == CLOCK_REALTIME)
874 else /* CLOCK_MONOTONIC */
879 linux_timerfd_curval(struct timerfd *tfd, struct itimerspec *ots)
883 linux_timerfd_clocktime(tfd, &cts);
884 *ots = tfd->tfd_time;
885 if (ots->it_value.tv_sec != 0 || ots->it_value.tv_nsec != 0) {
886 timespecsub(&ots->it_value, &cts, &ots->it_value);
887 if (ots->it_value.tv_sec < 0 ||
888 (ots->it_value.tv_sec == 0 &&
889 ots->it_value.tv_nsec == 0)) {
890 ots->it_value.tv_sec = 0;
891 ots->it_value.tv_nsec = 1;
897 linux_timerfd_gettime_common(struct thread *td, int fd, struct itimerspec *ots)
903 error = fget(td, fd, &cap_read_rights, &fp);
907 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL) {
912 mtx_lock(&tfd->tfd_lock);
913 linux_timerfd_curval(tfd, ots);
914 mtx_unlock(&tfd->tfd_lock);
922 linux_timerfd_gettime(struct thread *td, struct linux_timerfd_gettime_args *args)
924 struct l_itimerspec lots;
925 struct itimerspec ots;
928 error = linux_timerfd_gettime_common(td, args->fd, &ots);
931 error = native_to_linux_itimerspec(&lots, &ots);
933 error = copyout(&lots, args->old_value, sizeof(lots));
937 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
939 linux_timerfd_gettime64(struct thread *td, struct linux_timerfd_gettime64_args *args)
941 struct l_itimerspec64 lots;
942 struct itimerspec ots;
945 error = linux_timerfd_gettime_common(td, args->fd, &ots);
948 error = native_to_linux_itimerspec64(&lots, &ots);
950 error = copyout(&lots, args->old_value, sizeof(lots));
956 linux_timerfd_settime_common(struct thread *td, int fd, int flags,
957 struct itimerspec *nts, struct itimerspec *oval)
959 struct timespec cts, ts;
965 if ((flags & ~LINUX_TFD_SETTIME_FLAGS) != 0)
968 error = fget(td, fd, &cap_write_rights, &fp);
972 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL) {
977 mtx_lock(&tfd->tfd_lock);
978 if (!timespecisset(&nts->it_value))
979 timespecclear(&nts->it_interval);
981 linux_timerfd_curval(tfd, oval);
983 bcopy(nts, &tfd->tfd_time, sizeof(*nts));
985 if (timespecisset(&nts->it_value)) {
986 linux_timerfd_clocktime(tfd, &cts);
988 if ((flags & LINUX_TFD_TIMER_ABSTIME) == 0) {
989 timespecadd(&tfd->tfd_time.it_value, &cts,
990 &tfd->tfd_time.it_value);
992 timespecsub(&ts, &cts, &ts);
994 TIMESPEC_TO_TIMEVAL(&tv, &ts);
995 callout_reset(&tfd->tfd_callout, tvtohz(&tv),
996 linux_timerfd_expire, tfd);
997 tfd->tfd_canceled = false;
999 tfd->tfd_canceled = true;
1000 callout_stop(&tfd->tfd_callout);
1002 mtx_unlock(&tfd->tfd_lock);
1010 linux_timerfd_settime(struct thread *td, struct linux_timerfd_settime_args *args)
1012 struct l_itimerspec lots;
1013 struct itimerspec nts, ots, *pots;
1016 error = copyin(args->new_value, &lots, sizeof(lots));
1019 error = linux_to_native_itimerspec(&nts, &lots);
1022 pots = (args->old_value != NULL ? &ots : NULL);
1023 error = linux_timerfd_settime_common(td, args->fd, args->flags,
1025 if (error == 0 && args->old_value != NULL) {
1026 error = native_to_linux_itimerspec(&lots, &ots);
1028 error = copyout(&lots, args->old_value, sizeof(lots));
1033 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1035 linux_timerfd_settime64(struct thread *td, struct linux_timerfd_settime64_args *args)
1037 struct l_itimerspec64 lots;
1038 struct itimerspec nts, ots, *pots;
1041 error = copyin(args->new_value, &lots, sizeof(lots));
1044 error = linux_to_native_itimerspec64(&nts, &lots);
1047 pots = (args->old_value != NULL ? &ots : NULL);
1048 error = linux_timerfd_settime_common(td, args->fd, args->flags,
1050 if (error == 0 && args->old_value != NULL) {
1051 error = native_to_linux_itimerspec64(&lots, &ots);
1053 error = copyout(&lots, args->old_value, sizeof(lots));
1060 linux_timerfd_expire(void *arg)
1062 struct timespec cts, ts;
1064 struct timerfd *tfd;
1066 tfd = (struct timerfd *)arg;
1068 linux_timerfd_clocktime(tfd, &cts);
1069 if (timespeccmp(&cts, &tfd->tfd_time.it_value, >=)) {
1070 if (timespecisset(&tfd->tfd_time.it_interval))
1071 timespecadd(&tfd->tfd_time.it_value,
1072 &tfd->tfd_time.it_interval,
1073 &tfd->tfd_time.it_value);
1075 /* single shot timer */
1076 timespecclear(&tfd->tfd_time.it_value);
1077 if (timespecisset(&tfd->tfd_time.it_value)) {
1078 timespecsub(&tfd->tfd_time.it_value, &cts, &ts);
1079 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1080 callout_reset(&tfd->tfd_callout, tvtohz(&tv),
1081 linux_timerfd_expire, tfd);
1084 KNOTE_LOCKED(&tfd->tfd_sel.si_note, 0);
1085 selwakeup(&tfd->tfd_sel);
1086 wakeup(&tfd->tfd_count);
1087 } else if (timespecisset(&tfd->tfd_time.it_value)) {
1088 timespecsub(&tfd->tfd_time.it_value, &cts, &ts);
1089 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1090 callout_reset(&tfd->tfd_callout, tvtohz(&tv),
1091 linux_timerfd_expire, tfd);