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/param.h>
30 #include <sys/callout.h>
31 #include <sys/capsicum.h>
32 #include <sys/errno.h>
33 #include <sys/event.h>
34 #include <sys/eventfd.h>
36 #include <sys/filedesc.h>
37 #include <sys/filio.h>
38 #include <sys/limits.h>
40 #include <sys/mutex.h>
43 #include <sys/selinfo.h>
44 #include <sys/specialfd.h>
46 #include <sys/syscallsubr.h>
47 #include <sys/timespec.h>
51 #include <machine/../linux32/linux.h>
52 #include <machine/../linux32/linux32_proto.h>
54 #include <machine/../linux/linux.h>
55 #include <machine/../linux/linux_proto.h>
58 #include <compat/linux/linux_emul.h>
59 #include <compat/linux/linux_event.h>
60 #include <compat/linux/linux_file.h>
61 #include <compat/linux/linux_signal.h>
62 #include <compat/linux/linux_time.h>
63 #include <compat/linux/linux_util.h>
65 typedef uint64_t epoll_udata_t;
71 #if defined(__amd64__)
72 __attribute__((packed))
76 #define LINUX_MAX_EVENTS (INT_MAX / sizeof(struct epoll_event))
78 static int epoll_to_kevent(struct thread *td, int fd,
79 struct epoll_event *l_event, struct kevent *kevent,
81 static void kevent_to_epoll(struct kevent *kevent, struct epoll_event *l_event);
82 static int epoll_kev_copyout(void *arg, struct kevent *kevp, int count);
83 static int epoll_kev_copyin(void *arg, struct kevent *kevp, int count);
84 static int epoll_register_kevent(struct thread *td, struct file *epfp,
85 int fd, int filter, unsigned int flags);
86 static int epoll_fd_registered(struct thread *td, struct file *epfp,
88 static int epoll_delete_all_events(struct thread *td, struct file *epfp,
91 struct epoll_copyin_args {
92 struct kevent *changelist;
95 struct epoll_copyout_args {
96 struct epoll_event *leventlist;
103 typedef uint64_t timerfd_t;
105 static fo_rdwr_t timerfd_read;
106 static fo_ioctl_t timerfd_ioctl;
107 static fo_poll_t timerfd_poll;
108 static fo_kqfilter_t timerfd_kqfilter;
109 static fo_stat_t timerfd_stat;
110 static fo_close_t timerfd_close;
111 static fo_fill_kinfo_t timerfd_fill_kinfo;
113 static struct fileops timerfdops = {
114 .fo_read = timerfd_read,
115 .fo_write = invfo_rdwr,
116 .fo_truncate = invfo_truncate,
117 .fo_ioctl = timerfd_ioctl,
118 .fo_poll = timerfd_poll,
119 .fo_kqfilter = timerfd_kqfilter,
120 .fo_stat = timerfd_stat,
121 .fo_close = timerfd_close,
122 .fo_chmod = invfo_chmod,
123 .fo_chown = invfo_chown,
124 .fo_sendfile = invfo_sendfile,
125 .fo_fill_kinfo = timerfd_fill_kinfo,
126 .fo_flags = DFLAG_PASSABLE
129 static void filt_timerfddetach(struct knote *kn);
130 static int filt_timerfdread(struct knote *kn, long hint);
132 static struct filterops timerfd_rfiltops = {
134 .f_detach = filt_timerfddetach,
135 .f_event = filt_timerfdread
139 clockid_t tfd_clockid;
140 struct itimerspec tfd_time;
141 struct callout tfd_callout;
144 struct selinfo tfd_sel;
148 static void linux_timerfd_expire(void *);
149 static void linux_timerfd_curval(struct timerfd *, struct itimerspec *);
152 epoll_create_common(struct thread *td, int flags)
155 return (kern_kqueue(td, flags, NULL));
158 #ifdef LINUX_LEGACY_SYSCALLS
160 linux_epoll_create(struct thread *td, struct linux_epoll_create_args *args)
164 * args->size is unused. Linux just tests it
165 * and then forgets it as well.
170 return (epoll_create_common(td, 0));
175 linux_epoll_create1(struct thread *td, struct linux_epoll_create1_args *args)
179 if ((args->flags & ~(LINUX_O_CLOEXEC)) != 0)
183 if ((args->flags & LINUX_O_CLOEXEC) != 0)
186 return (epoll_create_common(td, flags));
189 /* Structure converting function from epoll to kevent. */
191 epoll_to_kevent(struct thread *td, int fd, struct epoll_event *l_event,
192 struct kevent *kevent, int *nkevents)
194 uint32_t levents = l_event->events;
195 struct linux_pemuldata *pem;
197 unsigned short kev_flags = EV_ADD | EV_ENABLE;
199 /* flags related to how event is registered */
200 if ((levents & LINUX_EPOLLONESHOT) != 0)
201 kev_flags |= EV_DISPATCH;
202 if ((levents & LINUX_EPOLLET) != 0)
203 kev_flags |= EV_CLEAR;
204 if ((levents & LINUX_EPOLLERR) != 0)
205 kev_flags |= EV_ERROR;
206 if ((levents & LINUX_EPOLLRDHUP) != 0)
209 /* flags related to what event is registered */
210 if ((levents & LINUX_EPOLL_EVRD) != 0) {
211 EV_SET(kevent, fd, EVFILT_READ, kev_flags, 0, 0, 0);
212 kevent->ext[0] = l_event->data;
216 if ((levents & LINUX_EPOLL_EVWR) != 0) {
217 EV_SET(kevent, fd, EVFILT_WRITE, kev_flags, 0, 0, 0);
218 kevent->ext[0] = l_event->data;
222 /* zero event mask is legal */
223 if ((levents & (LINUX_EPOLL_EVRD | LINUX_EPOLL_EVWR)) == 0) {
224 EV_SET(kevent++, fd, EVFILT_READ, EV_ADD|EV_DISABLE, 0, 0, 0);
228 if ((levents & ~(LINUX_EPOLL_EVSUP)) != 0) {
232 KASSERT(pem != NULL, ("epoll proc emuldata not found.\n"));
234 LINUX_PEM_XLOCK(pem);
235 if ((pem->flags & LINUX_XUNSUP_EPOLL) == 0) {
236 pem->flags |= LINUX_XUNSUP_EPOLL;
237 LINUX_PEM_XUNLOCK(pem);
238 linux_msg(td, "epoll_ctl unsupported flags: 0x%x",
241 LINUX_PEM_XUNLOCK(pem);
249 * Structure converting function from kevent to epoll. In a case
250 * this is called on error in registration we store the error in
251 * event->data and pick it up later in linux_epoll_ctl().
254 kevent_to_epoll(struct kevent *kevent, struct epoll_event *l_event)
257 l_event->data = kevent->ext[0];
259 if ((kevent->flags & EV_ERROR) != 0) {
260 l_event->events = LINUX_EPOLLERR;
264 /* XXX EPOLLPRI, EPOLLHUP */
265 switch (kevent->filter) {
267 l_event->events = LINUX_EPOLLIN;
268 if ((kevent->flags & EV_EOF) != 0)
269 l_event->events |= LINUX_EPOLLRDHUP;
272 l_event->events = LINUX_EPOLLOUT;
278 * Copyout callback used by kevent. This converts kevent
279 * events to epoll events and copies them back to the
280 * userspace. This is also called on error on registering
284 epoll_kev_copyout(void *arg, struct kevent *kevp, int count)
286 struct epoll_copyout_args *args;
287 struct epoll_event *eep;
290 args = (struct epoll_copyout_args*) arg;
291 eep = malloc(sizeof(*eep) * count, M_EPOLL, M_WAITOK | M_ZERO);
293 for (i = 0; i < count; i++)
294 kevent_to_epoll(&kevp[i], &eep[i]);
296 error = copyout(eep, args->leventlist, count * sizeof(*eep));
298 args->leventlist += count;
299 args->count += count;
300 } else if (args->error == 0)
308 * Copyin callback used by kevent. This copies already
309 * converted filters from kernel memory to the kevent
310 * internal kernel memory. Hence the memcpy instead of
314 epoll_kev_copyin(void *arg, struct kevent *kevp, int count)
316 struct epoll_copyin_args *args;
318 args = (struct epoll_copyin_args*) arg;
320 memcpy(kevp, args->changelist, count * sizeof(*kevp));
321 args->changelist += count;
327 * Load epoll filter, convert it to kevent filter
328 * and load it into kevent subsystem.
331 linux_epoll_ctl(struct thread *td, struct linux_epoll_ctl_args *args)
333 struct file *epfp, *fp;
334 struct epoll_copyin_args ciargs;
335 struct kevent kev[2];
336 struct kevent_copyops k_ops = { &ciargs,
339 struct epoll_event le;
344 if (args->op != LINUX_EPOLL_CTL_DEL) {
345 error = copyin(args->event, &le, sizeof(le));
350 error = fget(td, args->epfd,
351 cap_rights_init_one(&rights, CAP_KQUEUE_CHANGE), &epfp);
354 if (epfp->f_type != DTYPE_KQUEUE) {
359 /* Protect user data vector from incorrectly supplied fd. */
360 error = fget(td, args->fd,
361 cap_rights_init_one(&rights, CAP_POLL_EVENT), &fp);
365 /* Linux disallows spying on himself */
371 ciargs.changelist = kev;
373 if (args->op != LINUX_EPOLL_CTL_DEL) {
374 error = epoll_to_kevent(td, args->fd, &le, kev, &nchanges);
380 case LINUX_EPOLL_CTL_MOD:
381 error = epoll_delete_all_events(td, epfp, args->fd);
386 case LINUX_EPOLL_CTL_ADD:
387 if (epoll_fd_registered(td, epfp, args->fd)) {
393 case LINUX_EPOLL_CTL_DEL:
394 /* CTL_DEL means unregister this fd with this epoll */
395 error = epoll_delete_all_events(td, epfp, args->fd);
403 error = kern_kevent_fp(td, epfp, nchanges, 0, &k_ops, NULL);
414 * Wait for a filter to be triggered on the epoll file descriptor.
418 linux_epoll_wait_ts(struct thread *td, int epfd, struct epoll_event *events,
419 int maxevents, struct timespec *tsp, sigset_t *uset)
421 struct epoll_copyout_args coargs;
422 struct kevent_copyops k_ops = { &coargs,
430 if (maxevents <= 0 || maxevents > LINUX_MAX_EVENTS)
433 error = fget(td, epfd,
434 cap_rights_init_one(&rights, CAP_KQUEUE_EVENT), &epfp);
437 if (epfp->f_type != DTYPE_KQUEUE) {
442 error = kern_sigprocmask(td, SIG_SETMASK, uset,
446 td->td_pflags |= TDP_OLDMASK;
448 * Make sure that ast() is called on return to
449 * usermode and TDP_OLDMASK is cleared, restoring old
452 ast_sched(td, TDA_SIGSUSPEND);
455 coargs.leventlist = events;
456 coargs.p = td->td_proc;
460 error = kern_kevent_fp(td, epfp, 0, maxevents, &k_ops, tsp);
461 if (error == 0 && coargs.error != 0)
462 error = coargs.error;
465 * kern_kevent might return ENOMEM which is not expected from epoll_wait.
466 * Maybe we should translate that but I don't think it matters at all.
469 td->td_retval[0] = coargs.count;
472 error = kern_sigprocmask(td, SIG_SETMASK, &omask,
480 linux_epoll_wait_common(struct thread *td, int epfd, struct epoll_event *events,
481 int maxevents, int timeout, sigset_t *uset)
483 struct timespec ts, *tsp;
486 * Linux epoll_wait(2) man page states that timeout of -1 causes caller
487 * to block indefinitely. Real implementation does it if any negative
488 * timeout value is passed.
491 /* Convert from milliseconds to timespec. */
492 ts.tv_sec = timeout / 1000;
493 ts.tv_nsec = (timeout % 1000) * 1000000;
498 return (linux_epoll_wait_ts(td, epfd, events, maxevents, tsp, uset));
502 #ifdef LINUX_LEGACY_SYSCALLS
504 linux_epoll_wait(struct thread *td, struct linux_epoll_wait_args *args)
507 return (linux_epoll_wait_common(td, args->epfd, args->events,
508 args->maxevents, args->timeout, NULL));
513 linux_epoll_pwait(struct thread *td, struct linux_epoll_pwait_args *args)
515 sigset_t mask, *pmask;
518 error = linux_copyin_sigset(td, args->mask, sizeof(l_sigset_t),
523 return (linux_epoll_wait_common(td, args->epfd, args->events,
524 args->maxevents, args->timeout, pmask));
527 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
529 linux_epoll_pwait2_64(struct thread *td, struct linux_epoll_pwait2_64_args *args)
531 struct timespec ts, *tsa;
532 sigset_t mask, *pmask;
535 error = linux_copyin_sigset(td, args->mask, sizeof(l_sigset_t),
541 error = linux_get_timespec64(&ts, args->timeout);
548 return (linux_epoll_wait_ts(td, args->epfd, args->events,
549 args->maxevents, tsa, pmask));
553 linux_epoll_pwait2(struct thread *td, struct linux_epoll_pwait2_args *args)
555 struct timespec ts, *tsa;
556 sigset_t mask, *pmask;
559 error = linux_copyin_sigset(td, args->mask, sizeof(l_sigset_t),
565 error = linux_get_timespec(&ts, args->timeout);
572 return (linux_epoll_wait_ts(td, args->epfd, args->events,
573 args->maxevents, tsa, pmask));
575 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
578 epoll_register_kevent(struct thread *td, struct file *epfp, int fd, int filter,
581 struct epoll_copyin_args ciargs;
583 struct kevent_copyops k_ops = { &ciargs,
587 ciargs.changelist = &kev;
588 EV_SET(&kev, fd, filter, flags, 0, 0, 0);
590 return (kern_kevent_fp(td, epfp, 1, 0, &k_ops, NULL));
594 epoll_fd_registered(struct thread *td, struct file *epfp, int fd)
597 * Set empty filter flags to avoid accidental modification of already
598 * registered events. In the case of event re-registration:
599 * 1. If event does not exists kevent() does nothing and returns ENOENT
600 * 2. If event does exists, it's enabled/disabled state is preserved
601 * but fflags, data and udata fields are overwritten. So we can not
602 * set socket lowats and store user's context pointer in udata.
604 if (epoll_register_kevent(td, epfp, fd, EVFILT_READ, 0) != ENOENT ||
605 epoll_register_kevent(td, epfp, fd, EVFILT_WRITE, 0) != ENOENT)
612 epoll_delete_all_events(struct thread *td, struct file *epfp, int fd)
616 error1 = epoll_register_kevent(td, epfp, fd, EVFILT_READ, EV_DELETE);
617 error2 = epoll_register_kevent(td, epfp, fd, EVFILT_WRITE, EV_DELETE);
619 /* return 0 if at least one result positive */
620 return (error1 == 0 ? 0 : error2);
623 #ifdef LINUX_LEGACY_SYSCALLS
625 linux_eventfd(struct thread *td, struct linux_eventfd_args *args)
627 struct specialfd_eventfd ae;
629 bzero(&ae, sizeof(ae));
630 ae.initval = args->initval;
631 return (kern_specialfd(td, SPECIALFD_EVENTFD, &ae));
636 linux_eventfd2(struct thread *td, struct linux_eventfd2_args *args)
638 struct specialfd_eventfd ae;
641 if ((args->flags & ~(LINUX_O_CLOEXEC | LINUX_O_NONBLOCK |
642 LINUX_EFD_SEMAPHORE)) != 0)
645 if ((args->flags & LINUX_O_CLOEXEC) != 0)
646 flags |= EFD_CLOEXEC;
647 if ((args->flags & LINUX_O_NONBLOCK) != 0)
648 flags |= EFD_NONBLOCK;
649 if ((args->flags & LINUX_EFD_SEMAPHORE) != 0)
650 flags |= EFD_SEMAPHORE;
652 bzero(&ae, sizeof(ae));
654 ae.initval = args->initval;
655 return (kern_specialfd(td, SPECIALFD_EVENTFD, &ae));
659 linux_timerfd_create(struct thread *td, struct linux_timerfd_create_args *args)
664 int fflags, fd, error;
666 if ((args->flags & ~LINUX_TFD_CREATE_FLAGS) != 0)
669 error = linux_to_native_clockid(&clockid, args->clockid);
672 if (clockid != CLOCK_REALTIME && clockid != CLOCK_MONOTONIC)
676 if ((args->flags & LINUX_TFD_CLOEXEC) != 0)
679 error = falloc(td, &fp, &fd, fflags);
683 tfd = malloc(sizeof(*tfd), M_EPOLL, M_WAITOK | M_ZERO);
684 tfd->tfd_clockid = clockid;
685 mtx_init(&tfd->tfd_lock, "timerfd", NULL, MTX_DEF);
687 callout_init_mtx(&tfd->tfd_callout, &tfd->tfd_lock, 0);
688 knlist_init_mtx(&tfd->tfd_sel.si_note, &tfd->tfd_lock);
691 if ((args->flags & LINUX_O_NONBLOCK) != 0)
694 finit(fp, fflags, DTYPE_LINUXTFD, tfd, &timerfdops);
697 td->td_retval[0] = fd;
702 timerfd_close(struct file *fp, struct thread *td)
707 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
710 timespecclear(&tfd->tfd_time.it_value);
711 timespecclear(&tfd->tfd_time.it_interval);
713 callout_drain(&tfd->tfd_callout);
715 seldrain(&tfd->tfd_sel);
716 knlist_destroy(&tfd->tfd_sel.si_note);
718 fp->f_ops = &badfileops;
719 mtx_destroy(&tfd->tfd_lock);
726 timerfd_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
727 int flags, struct thread *td)
734 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
737 if (uio->uio_resid < sizeof(timerfd_t))
741 mtx_lock(&tfd->tfd_lock);
743 if (tfd->tfd_canceled) {
745 mtx_unlock(&tfd->tfd_lock);
748 if (tfd->tfd_count == 0) {
749 if ((fp->f_flag & FNONBLOCK) != 0) {
750 mtx_unlock(&tfd->tfd_lock);
753 error = mtx_sleep(&tfd->tfd_count, &tfd->tfd_lock, PCATCH, "ltfdrd", 0);
758 count = tfd->tfd_count;
760 mtx_unlock(&tfd->tfd_lock);
761 error = uiomove(&count, sizeof(timerfd_t), uio);
763 mtx_unlock(&tfd->tfd_lock);
769 timerfd_poll(struct file *fp, int events, struct ucred *active_cred,
776 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
779 mtx_lock(&tfd->tfd_lock);
780 if ((events & (POLLIN|POLLRDNORM)) && tfd->tfd_count > 0)
781 revents |= events & (POLLIN|POLLRDNORM);
783 selrecord(td, &tfd->tfd_sel);
784 mtx_unlock(&tfd->tfd_lock);
790 timerfd_kqfilter(struct file *fp, struct knote *kn)
795 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
798 if (kn->kn_filter == EVFILT_READ)
799 kn->kn_fop = &timerfd_rfiltops;
804 knlist_add(&tfd->tfd_sel.si_note, kn, 0);
810 filt_timerfddetach(struct knote *kn)
812 struct timerfd *tfd = kn->kn_hook;
814 mtx_lock(&tfd->tfd_lock);
815 knlist_remove(&tfd->tfd_sel.si_note, kn, 1);
816 mtx_unlock(&tfd->tfd_lock);
820 filt_timerfdread(struct knote *kn, long hint)
822 struct timerfd *tfd = kn->kn_hook;
824 return (tfd->tfd_count > 0);
828 timerfd_ioctl(struct file *fp, u_long cmd, void *data,
829 struct ucred *active_cred, struct thread *td)
832 if (fp->f_data == NULL || fp->f_type != DTYPE_LINUXTFD)
845 timerfd_stat(struct file *fp, struct stat *st, struct ucred *active_cred)
852 timerfd_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
855 kif->kf_type = KF_TYPE_UNKNOWN;
860 linux_timerfd_clocktime(struct timerfd *tfd, struct timespec *ts)
863 if (tfd->tfd_clockid == CLOCK_REALTIME)
865 else /* CLOCK_MONOTONIC */
870 linux_timerfd_curval(struct timerfd *tfd, struct itimerspec *ots)
874 linux_timerfd_clocktime(tfd, &cts);
875 *ots = tfd->tfd_time;
876 if (ots->it_value.tv_sec != 0 || ots->it_value.tv_nsec != 0) {
877 timespecsub(&ots->it_value, &cts, &ots->it_value);
878 if (ots->it_value.tv_sec < 0 ||
879 (ots->it_value.tv_sec == 0 &&
880 ots->it_value.tv_nsec == 0)) {
881 ots->it_value.tv_sec = 0;
882 ots->it_value.tv_nsec = 1;
888 linux_timerfd_gettime_common(struct thread *td, int fd, struct itimerspec *ots)
894 error = fget(td, fd, &cap_read_rights, &fp);
898 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL) {
903 mtx_lock(&tfd->tfd_lock);
904 linux_timerfd_curval(tfd, ots);
905 mtx_unlock(&tfd->tfd_lock);
913 linux_timerfd_gettime(struct thread *td, struct linux_timerfd_gettime_args *args)
915 struct l_itimerspec lots;
916 struct itimerspec ots;
919 error = linux_timerfd_gettime_common(td, args->fd, &ots);
922 error = native_to_linux_itimerspec(&lots, &ots);
924 error = copyout(&lots, args->old_value, sizeof(lots));
928 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
930 linux_timerfd_gettime64(struct thread *td, struct linux_timerfd_gettime64_args *args)
932 struct l_itimerspec64 lots;
933 struct itimerspec ots;
936 error = linux_timerfd_gettime_common(td, args->fd, &ots);
939 error = native_to_linux_itimerspec64(&lots, &ots);
941 error = copyout(&lots, args->old_value, sizeof(lots));
947 linux_timerfd_settime_common(struct thread *td, int fd, int flags,
948 struct itimerspec *nts, struct itimerspec *oval)
950 struct timespec cts, ts;
956 if ((flags & ~LINUX_TFD_SETTIME_FLAGS) != 0)
959 error = fget(td, fd, &cap_write_rights, &fp);
963 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL) {
968 mtx_lock(&tfd->tfd_lock);
969 if (!timespecisset(&nts->it_value))
970 timespecclear(&nts->it_interval);
972 linux_timerfd_curval(tfd, oval);
974 bcopy(nts, &tfd->tfd_time, sizeof(*nts));
976 if (timespecisset(&nts->it_value)) {
977 linux_timerfd_clocktime(tfd, &cts);
979 if ((flags & LINUX_TFD_TIMER_ABSTIME) == 0) {
980 timespecadd(&tfd->tfd_time.it_value, &cts,
981 &tfd->tfd_time.it_value);
983 timespecsub(&ts, &cts, &ts);
985 TIMESPEC_TO_TIMEVAL(&tv, &ts);
986 callout_reset(&tfd->tfd_callout, tvtohz(&tv),
987 linux_timerfd_expire, tfd);
988 tfd->tfd_canceled = false;
990 tfd->tfd_canceled = true;
991 callout_stop(&tfd->tfd_callout);
993 mtx_unlock(&tfd->tfd_lock);
1001 linux_timerfd_settime(struct thread *td, struct linux_timerfd_settime_args *args)
1003 struct l_itimerspec lots;
1004 struct itimerspec nts, ots, *pots;
1007 error = copyin(args->new_value, &lots, sizeof(lots));
1010 error = linux_to_native_itimerspec(&nts, &lots);
1013 pots = (args->old_value != NULL ? &ots : NULL);
1014 error = linux_timerfd_settime_common(td, args->fd, args->flags,
1016 if (error == 0 && args->old_value != NULL) {
1017 error = native_to_linux_itimerspec(&lots, &ots);
1019 error = copyout(&lots, args->old_value, sizeof(lots));
1024 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1026 linux_timerfd_settime64(struct thread *td, struct linux_timerfd_settime64_args *args)
1028 struct l_itimerspec64 lots;
1029 struct itimerspec nts, ots, *pots;
1032 error = copyin(args->new_value, &lots, sizeof(lots));
1035 error = linux_to_native_itimerspec64(&nts, &lots);
1038 pots = (args->old_value != NULL ? &ots : NULL);
1039 error = linux_timerfd_settime_common(td, args->fd, args->flags,
1041 if (error == 0 && args->old_value != NULL) {
1042 error = native_to_linux_itimerspec64(&lots, &ots);
1044 error = copyout(&lots, args->old_value, sizeof(lots));
1051 linux_timerfd_expire(void *arg)
1053 struct timespec cts, ts;
1055 struct timerfd *tfd;
1057 tfd = (struct timerfd *)arg;
1059 linux_timerfd_clocktime(tfd, &cts);
1060 if (timespeccmp(&cts, &tfd->tfd_time.it_value, >=)) {
1061 if (timespecisset(&tfd->tfd_time.it_interval))
1062 timespecadd(&tfd->tfd_time.it_value,
1063 &tfd->tfd_time.it_interval,
1064 &tfd->tfd_time.it_value);
1066 /* single shot timer */
1067 timespecclear(&tfd->tfd_time.it_value);
1068 if (timespecisset(&tfd->tfd_time.it_value)) {
1069 timespecsub(&tfd->tfd_time.it_value, &cts, &ts);
1070 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1071 callout_reset(&tfd->tfd_callout, tvtohz(&tv),
1072 linux_timerfd_expire, tfd);
1075 KNOTE_LOCKED(&tfd->tfd_sel.si_note, 0);
1076 selwakeup(&tfd->tfd_sel);
1077 wakeup(&tfd->tfd_count);
1078 } else if (timespecisset(&tfd->tfd_time.it_value)) {
1079 timespecsub(&tfd->tfd_time.it_value, &cts, &ts);
1080 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1081 callout_reset(&tfd->tfd_callout, tvtohz(&tv),
1082 linux_timerfd_expire, tfd);