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>
75 * epoll defines 'struct epoll_event' with the field 'data' as 64 bits
76 * on all architectures. But on 32 bit architectures BSD 'struct kevent' only
77 * has 32 bit opaque pointer as 'udata' field. So we can't pass epoll supplied
78 * data verbatuim. Therefore we allocate 64-bit memory block to pass
79 * user supplied data for every file descriptor.
82 typedef uint64_t epoll_udata_t;
84 struct epoll_emuldata {
85 uint32_t fdc; /* epoll udata max index */
86 epoll_udata_t udata[1]; /* epoll user data vector */
89 #define EPOLL_DEF_SZ 16
90 #define EPOLL_SIZE(fdn) \
91 (sizeof(struct epoll_emuldata)+(fdn) * sizeof(epoll_udata_t))
97 #if defined(__amd64__)
98 __attribute__((packed))
102 #define LINUX_MAX_EVENTS (INT_MAX / sizeof(struct epoll_event))
104 static void epoll_fd_install(struct thread *td, int fd, epoll_udata_t udata);
105 static int epoll_to_kevent(struct thread *td, int fd,
106 struct epoll_event *l_event, struct kevent *kevent,
108 static void kevent_to_epoll(struct kevent *kevent, struct epoll_event *l_event);
109 static int epoll_kev_copyout(void *arg, struct kevent *kevp, int count);
110 static int epoll_kev_copyin(void *arg, struct kevent *kevp, int count);
111 static int epoll_register_kevent(struct thread *td, struct file *epfp,
112 int fd, int filter, unsigned int flags);
113 static int epoll_fd_registered(struct thread *td, struct file *epfp,
115 static int epoll_delete_all_events(struct thread *td, struct file *epfp,
118 struct epoll_copyin_args {
119 struct kevent *changelist;
122 struct epoll_copyout_args {
123 struct epoll_event *leventlist;
130 typedef uint64_t timerfd_t;
132 static fo_rdwr_t timerfd_read;
133 static fo_ioctl_t timerfd_ioctl;
134 static fo_poll_t timerfd_poll;
135 static fo_kqfilter_t timerfd_kqfilter;
136 static fo_stat_t timerfd_stat;
137 static fo_close_t timerfd_close;
138 static fo_fill_kinfo_t timerfd_fill_kinfo;
140 static struct fileops timerfdops = {
141 .fo_read = timerfd_read,
142 .fo_write = invfo_rdwr,
143 .fo_truncate = invfo_truncate,
144 .fo_ioctl = timerfd_ioctl,
145 .fo_poll = timerfd_poll,
146 .fo_kqfilter = timerfd_kqfilter,
147 .fo_stat = timerfd_stat,
148 .fo_close = timerfd_close,
149 .fo_chmod = invfo_chmod,
150 .fo_chown = invfo_chown,
151 .fo_sendfile = invfo_sendfile,
152 .fo_fill_kinfo = timerfd_fill_kinfo,
153 .fo_flags = DFLAG_PASSABLE
156 static void filt_timerfddetach(struct knote *kn);
157 static int filt_timerfdread(struct knote *kn, long hint);
159 static struct filterops timerfd_rfiltops = {
161 .f_detach = filt_timerfddetach,
162 .f_event = filt_timerfdread
166 clockid_t tfd_clockid;
167 struct itimerspec tfd_time;
168 struct callout tfd_callout;
171 struct selinfo tfd_sel;
175 static void linux_timerfd_expire(void *);
176 static void linux_timerfd_curval(struct timerfd *, struct itimerspec *);
179 epoll_fd_install(struct thread *td, int fd, epoll_udata_t udata)
181 struct linux_pemuldata *pem;
182 struct epoll_emuldata *emd;
188 KASSERT(pem != NULL, ("epoll proc emuldata not found.\n"));
190 LINUX_PEM_XLOCK(pem);
191 if (pem->epoll == NULL) {
192 emd = malloc(EPOLL_SIZE(fd), M_EPOLL, M_WAITOK);
198 emd = realloc(emd, EPOLL_SIZE(fd), M_EPOLL, M_WAITOK);
203 emd->udata[fd] = udata;
204 LINUX_PEM_XUNLOCK(pem);
208 epoll_create_common(struct thread *td, int flags)
212 error = kern_kqueue(td, flags, NULL);
216 epoll_fd_install(td, EPOLL_DEF_SZ, 0);
221 #ifdef LINUX_LEGACY_SYSCALLS
223 linux_epoll_create(struct thread *td, struct linux_epoll_create_args *args)
227 * args->size is unused. Linux just tests it
228 * and then forgets it as well.
233 return (epoll_create_common(td, 0));
238 linux_epoll_create1(struct thread *td, struct linux_epoll_create1_args *args)
242 if ((args->flags & ~(LINUX_O_CLOEXEC)) != 0)
246 if ((args->flags & LINUX_O_CLOEXEC) != 0)
249 return (epoll_create_common(td, flags));
252 /* Structure converting function from epoll to kevent. */
254 epoll_to_kevent(struct thread *td, int fd, struct epoll_event *l_event,
255 struct kevent *kevent, int *nkevents)
257 uint32_t levents = l_event->events;
258 struct linux_pemuldata *pem;
260 unsigned short kev_flags = EV_ADD | EV_ENABLE;
262 /* flags related to how event is registered */
263 if ((levents & LINUX_EPOLLONESHOT) != 0)
264 kev_flags |= EV_DISPATCH;
265 if ((levents & LINUX_EPOLLET) != 0)
266 kev_flags |= EV_CLEAR;
267 if ((levents & LINUX_EPOLLERR) != 0)
268 kev_flags |= EV_ERROR;
269 if ((levents & LINUX_EPOLLRDHUP) != 0)
272 /* flags related to what event is registered */
273 if ((levents & LINUX_EPOLL_EVRD) != 0) {
274 EV_SET(kevent++, fd, EVFILT_READ, kev_flags, 0, 0, 0);
277 if ((levents & LINUX_EPOLL_EVWR) != 0) {
278 EV_SET(kevent++, fd, EVFILT_WRITE, kev_flags, 0, 0, 0);
281 /* zero event mask is legal */
282 if ((levents & (LINUX_EPOLL_EVRD | LINUX_EPOLL_EVWR)) == 0) {
283 EV_SET(kevent++, fd, EVFILT_READ, EV_ADD|EV_DISABLE, 0, 0, 0);
287 if ((levents & ~(LINUX_EPOLL_EVSUP)) != 0) {
291 KASSERT(pem != NULL, ("epoll proc emuldata not found.\n"));
292 KASSERT(pem->epoll != NULL, ("epoll proc epolldata not found.\n"));
294 LINUX_PEM_XLOCK(pem);
295 if ((pem->flags & LINUX_XUNSUP_EPOLL) == 0) {
296 pem->flags |= LINUX_XUNSUP_EPOLL;
297 LINUX_PEM_XUNLOCK(pem);
298 linux_msg(td, "epoll_ctl unsupported flags: 0x%x",
301 LINUX_PEM_XUNLOCK(pem);
309 * Structure converting function from kevent to epoll. In a case
310 * this is called on error in registration we store the error in
311 * event->data and pick it up later in linux_epoll_ctl().
314 kevent_to_epoll(struct kevent *kevent, struct epoll_event *l_event)
317 if ((kevent->flags & EV_ERROR) != 0) {
318 l_event->events = LINUX_EPOLLERR;
322 /* XXX EPOLLPRI, EPOLLHUP */
323 switch (kevent->filter) {
325 l_event->events = LINUX_EPOLLIN;
326 if ((kevent->flags & EV_EOF) != 0)
327 l_event->events |= LINUX_EPOLLRDHUP;
330 l_event->events = LINUX_EPOLLOUT;
336 * Copyout callback used by kevent. This converts kevent
337 * events to epoll events and copies them back to the
338 * userspace. This is also called on error on registering
342 epoll_kev_copyout(void *arg, struct kevent *kevp, int count)
344 struct epoll_copyout_args *args;
345 struct linux_pemuldata *pem;
346 struct epoll_emuldata *emd;
347 struct epoll_event *eep;
350 args = (struct epoll_copyout_args*) arg;
351 eep = malloc(sizeof(*eep) * count, M_EPOLL, M_WAITOK | M_ZERO);
353 pem = pem_find(args->p);
354 KASSERT(pem != NULL, ("epoll proc emuldata not found.\n"));
355 LINUX_PEM_SLOCK(pem);
357 KASSERT(emd != NULL, ("epoll proc epolldata not found.\n"));
359 for (i = 0; i < count; i++) {
360 kevent_to_epoll(&kevp[i], &eep[i]);
363 KASSERT(fd <= emd->fdc, ("epoll user data vector"
364 " is too small.\n"));
365 eep[i].data = emd->udata[fd];
367 LINUX_PEM_SUNLOCK(pem);
369 error = copyout(eep, args->leventlist, count * sizeof(*eep));
371 args->leventlist += count;
372 args->count += count;
373 } else if (args->error == 0)
381 * Copyin callback used by kevent. This copies already
382 * converted filters from kernel memory to the kevent
383 * internal kernel memory. Hence the memcpy instead of
387 epoll_kev_copyin(void *arg, struct kevent *kevp, int count)
389 struct epoll_copyin_args *args;
391 args = (struct epoll_copyin_args*) arg;
393 memcpy(kevp, args->changelist, count * sizeof(*kevp));
394 args->changelist += count;
400 * Load epoll filter, convert it to kevent filter
401 * and load it into kevent subsystem.
404 linux_epoll_ctl(struct thread *td, struct linux_epoll_ctl_args *args)
406 struct file *epfp, *fp;
407 struct epoll_copyin_args ciargs;
408 struct kevent kev[2];
409 struct kevent_copyops k_ops = { &ciargs,
412 struct epoll_event le;
417 if (args->op != LINUX_EPOLL_CTL_DEL) {
418 error = copyin(args->event, &le, sizeof(le));
423 error = fget(td, args->epfd,
424 cap_rights_init_one(&rights, CAP_KQUEUE_CHANGE), &epfp);
427 if (epfp->f_type != DTYPE_KQUEUE) {
432 /* Protect user data vector from incorrectly supplied fd. */
433 error = fget(td, args->fd,
434 cap_rights_init_one(&rights, CAP_POLL_EVENT), &fp);
438 /* Linux disallows spying on himself */
444 ciargs.changelist = kev;
446 if (args->op != LINUX_EPOLL_CTL_DEL) {
447 error = epoll_to_kevent(td, args->fd, &le, kev, &nchanges);
453 case LINUX_EPOLL_CTL_MOD:
454 error = epoll_delete_all_events(td, epfp, args->fd);
459 case LINUX_EPOLL_CTL_ADD:
460 if (epoll_fd_registered(td, epfp, args->fd)) {
466 case LINUX_EPOLL_CTL_DEL:
467 /* CTL_DEL means unregister this fd with this epoll */
468 error = epoll_delete_all_events(td, epfp, args->fd);
476 epoll_fd_install(td, args->fd, le.data);
478 error = kern_kevent_fp(td, epfp, nchanges, 0, &k_ops, NULL);
489 * Wait for a filter to be triggered on the epoll file descriptor.
492 linux_epoll_wait_common(struct thread *td, int epfd, struct epoll_event *events,
493 int maxevents, int timeout, sigset_t *uset)
495 struct epoll_copyout_args coargs;
496 struct kevent_copyops k_ops = { &coargs,
499 struct timespec ts, *tsp;
505 if (maxevents <= 0 || maxevents > LINUX_MAX_EVENTS)
508 error = fget(td, epfd,
509 cap_rights_init_one(&rights, CAP_KQUEUE_EVENT), &epfp);
512 if (epfp->f_type != DTYPE_KQUEUE) {
517 error = kern_sigprocmask(td, SIG_SETMASK, uset,
521 td->td_pflags |= TDP_OLDMASK;
523 * Make sure that ast() is called on return to
524 * usermode and TDP_OLDMASK is cleared, restoring old
528 td->td_flags |= TDF_ASTPENDING;
532 coargs.leventlist = events;
533 coargs.p = td->td_proc;
538 * Linux epoll_wait(2) man page states that timeout of -1 causes caller
539 * to block indefinitely. Real implementation does it if any negative
540 * timeout value is passed.
543 /* Convert from milliseconds to timespec. */
544 ts.tv_sec = timeout / 1000;
545 ts.tv_nsec = (timeout % 1000) * 1000000;
551 error = kern_kevent_fp(td, epfp, 0, maxevents, &k_ops, tsp);
552 if (error == 0 && coargs.error != 0)
553 error = coargs.error;
556 * kern_kevent might return ENOMEM which is not expected from epoll_wait.
557 * Maybe we should translate that but I don't think it matters at all.
560 td->td_retval[0] = coargs.count;
563 error = kern_sigprocmask(td, SIG_SETMASK, &omask,
570 #ifdef LINUX_LEGACY_SYSCALLS
572 linux_epoll_wait(struct thread *td, struct linux_epoll_wait_args *args)
575 return (linux_epoll_wait_common(td, args->epfd, args->events,
576 args->maxevents, args->timeout, NULL));
581 linux_epoll_pwait(struct thread *td, struct linux_epoll_pwait_args *args)
583 sigset_t mask, *pmask;
587 if (args->mask != NULL) {
588 if (args->sigsetsize != sizeof(l_sigset_t))
590 error = copyin(args->mask, &lmask, sizeof(l_sigset_t));
593 linux_to_bsd_sigset(&lmask, &mask);
597 return (linux_epoll_wait_common(td, args->epfd, args->events,
598 args->maxevents, args->timeout, pmask));
602 epoll_register_kevent(struct thread *td, struct file *epfp, int fd, int filter,
605 struct epoll_copyin_args ciargs;
607 struct kevent_copyops k_ops = { &ciargs,
611 ciargs.changelist = &kev;
612 EV_SET(&kev, fd, filter, flags, 0, 0, 0);
614 return (kern_kevent_fp(td, epfp, 1, 0, &k_ops, NULL));
618 epoll_fd_registered(struct thread *td, struct file *epfp, int fd)
621 * Set empty filter flags to avoid accidental modification of already
622 * registered events. In the case of event re-registration:
623 * 1. If event does not exists kevent() does nothing and returns ENOENT
624 * 2. If event does exists, it's enabled/disabled state is preserved
625 * but fflags, data and udata fields are overwritten. So we can not
626 * set socket lowats and store user's context pointer in udata.
628 if (epoll_register_kevent(td, epfp, fd, EVFILT_READ, 0) != ENOENT ||
629 epoll_register_kevent(td, epfp, fd, EVFILT_WRITE, 0) != ENOENT)
636 epoll_delete_all_events(struct thread *td, struct file *epfp, int fd)
640 error1 = epoll_register_kevent(td, epfp, fd, EVFILT_READ, EV_DELETE);
641 error2 = epoll_register_kevent(td, epfp, fd, EVFILT_WRITE, EV_DELETE);
643 /* return 0 if at least one result positive */
644 return (error1 == 0 ? 0 : error2);
647 #ifdef LINUX_LEGACY_SYSCALLS
649 linux_eventfd(struct thread *td, struct linux_eventfd_args *args)
651 struct specialfd_eventfd ae;
653 bzero(&ae, sizeof(ae));
654 ae.initval = args->initval;
655 return (kern_specialfd(td, SPECIALFD_EVENTFD, &ae));
660 linux_eventfd2(struct thread *td, struct linux_eventfd2_args *args)
662 struct specialfd_eventfd ae;
665 if ((args->flags & ~(LINUX_O_CLOEXEC | LINUX_O_NONBLOCK |
666 LINUX_EFD_SEMAPHORE)) != 0)
669 if ((args->flags & LINUX_O_CLOEXEC) != 0)
670 flags |= EFD_CLOEXEC;
671 if ((args->flags & LINUX_O_NONBLOCK) != 0)
672 flags |= EFD_NONBLOCK;
673 if ((args->flags & LINUX_EFD_SEMAPHORE) != 0)
674 flags |= EFD_SEMAPHORE;
676 bzero(&ae, sizeof(ae));
678 ae.initval = args->initval;
679 return (kern_specialfd(td, SPECIALFD_EVENTFD, &ae));
683 linux_timerfd_create(struct thread *td, struct linux_timerfd_create_args *args)
685 struct filedesc *fdp;
689 int fflags, fd, error;
691 if ((args->flags & ~LINUX_TFD_CREATE_FLAGS) != 0)
694 error = linux_to_native_clockid(&clockid, args->clockid);
697 if (clockid != CLOCK_REALTIME && clockid != CLOCK_MONOTONIC)
701 if ((args->flags & LINUX_TFD_CLOEXEC) != 0)
704 fdp = td->td_proc->p_fd;
705 error = falloc(td, &fp, &fd, fflags);
709 tfd = malloc(sizeof(*tfd), M_EPOLL, M_WAITOK | M_ZERO);
710 tfd->tfd_clockid = clockid;
711 mtx_init(&tfd->tfd_lock, "timerfd", NULL, MTX_DEF);
713 callout_init_mtx(&tfd->tfd_callout, &tfd->tfd_lock, 0);
714 knlist_init_mtx(&tfd->tfd_sel.si_note, &tfd->tfd_lock);
717 if ((args->flags & LINUX_O_NONBLOCK) != 0)
720 finit(fp, fflags, DTYPE_LINUXTFD, tfd, &timerfdops);
723 td->td_retval[0] = fd;
728 timerfd_close(struct file *fp, struct thread *td)
733 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
736 timespecclear(&tfd->tfd_time.it_value);
737 timespecclear(&tfd->tfd_time.it_interval);
739 mtx_lock(&tfd->tfd_lock);
740 callout_drain(&tfd->tfd_callout);
741 mtx_unlock(&tfd->tfd_lock);
743 seldrain(&tfd->tfd_sel);
744 knlist_destroy(&tfd->tfd_sel.si_note);
746 fp->f_ops = &badfileops;
747 mtx_destroy(&tfd->tfd_lock);
754 timerfd_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
755 int flags, struct thread *td)
762 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
765 if (uio->uio_resid < sizeof(timerfd_t))
769 mtx_lock(&tfd->tfd_lock);
771 if (tfd->tfd_canceled) {
773 mtx_unlock(&tfd->tfd_lock);
776 if (tfd->tfd_count == 0) {
777 if ((fp->f_flag & FNONBLOCK) != 0) {
778 mtx_unlock(&tfd->tfd_lock);
781 error = mtx_sleep(&tfd->tfd_count, &tfd->tfd_lock, PCATCH, "ltfdrd", 0);
786 count = tfd->tfd_count;
788 mtx_unlock(&tfd->tfd_lock);
789 error = uiomove(&count, sizeof(timerfd_t), uio);
791 mtx_unlock(&tfd->tfd_lock);
797 timerfd_poll(struct file *fp, int events, struct ucred *active_cred,
804 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
807 mtx_lock(&tfd->tfd_lock);
808 if ((events & (POLLIN|POLLRDNORM)) && tfd->tfd_count > 0)
809 revents |= events & (POLLIN|POLLRDNORM);
811 selrecord(td, &tfd->tfd_sel);
812 mtx_unlock(&tfd->tfd_lock);
818 timerfd_kqfilter(struct file *fp, struct knote *kn)
823 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
826 if (kn->kn_filter == EVFILT_READ)
827 kn->kn_fop = &timerfd_rfiltops;
832 knlist_add(&tfd->tfd_sel.si_note, kn, 0);
838 filt_timerfddetach(struct knote *kn)
840 struct timerfd *tfd = kn->kn_hook;
842 mtx_lock(&tfd->tfd_lock);
843 knlist_remove(&tfd->tfd_sel.si_note, kn, 1);
844 mtx_unlock(&tfd->tfd_lock);
848 filt_timerfdread(struct knote *kn, long hint)
850 struct timerfd *tfd = kn->kn_hook;
852 return (tfd->tfd_count > 0);
856 timerfd_ioctl(struct file *fp, u_long cmd, void *data,
857 struct ucred *active_cred, struct thread *td)
860 if (fp->f_data == NULL || fp->f_type != DTYPE_LINUXTFD)
873 timerfd_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
881 timerfd_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
884 kif->kf_type = KF_TYPE_UNKNOWN;
889 linux_timerfd_clocktime(struct timerfd *tfd, struct timespec *ts)
892 if (tfd->tfd_clockid == CLOCK_REALTIME)
894 else /* CLOCK_MONOTONIC */
899 linux_timerfd_curval(struct timerfd *tfd, struct itimerspec *ots)
903 linux_timerfd_clocktime(tfd, &cts);
904 *ots = tfd->tfd_time;
905 if (ots->it_value.tv_sec != 0 || ots->it_value.tv_nsec != 0) {
906 timespecsub(&ots->it_value, &cts, &ots->it_value);
907 if (ots->it_value.tv_sec < 0 ||
908 (ots->it_value.tv_sec == 0 &&
909 ots->it_value.tv_nsec == 0)) {
910 ots->it_value.tv_sec = 0;
911 ots->it_value.tv_nsec = 1;
917 linux_timerfd_gettime(struct thread *td, struct linux_timerfd_gettime_args *args)
919 struct l_itimerspec lots;
920 struct itimerspec ots;
925 error = fget(td, args->fd, &cap_read_rights, &fp);
929 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL) {
934 mtx_lock(&tfd->tfd_lock);
935 linux_timerfd_curval(tfd, &ots);
936 mtx_unlock(&tfd->tfd_lock);
938 error = native_to_linux_itimerspec(&lots, &ots);
940 error = copyout(&lots, args->old_value, sizeof(lots));
948 linux_timerfd_settime(struct thread *td, struct linux_timerfd_settime_args *args)
950 struct l_itimerspec lots;
951 struct itimerspec nts, ots;
952 struct timespec cts, ts;
958 if ((args->flags & ~LINUX_TFD_SETTIME_FLAGS) != 0)
961 error = copyin(args->new_value, &lots, sizeof(lots));
964 error = linux_to_native_itimerspec(&nts, &lots);
968 error = fget(td, args->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);
980 if (args->old_value != NULL)
981 linux_timerfd_curval(tfd, &ots);
984 if (timespecisset(&nts.it_value)) {
985 linux_timerfd_clocktime(tfd, &cts);
987 if ((args->flags & LINUX_TFD_TIMER_ABSTIME) == 0) {
988 timespecadd(&tfd->tfd_time.it_value, &cts,
989 &tfd->tfd_time.it_value);
991 timespecsub(&ts, &cts, &ts);
993 TIMESPEC_TO_TIMEVAL(&tv, &ts);
994 callout_reset(&tfd->tfd_callout, tvtohz(&tv),
995 linux_timerfd_expire, tfd);
996 tfd->tfd_canceled = false;
998 tfd->tfd_canceled = true;
999 callout_stop(&tfd->tfd_callout);
1001 mtx_unlock(&tfd->tfd_lock);
1003 if (args->old_value != NULL) {
1004 error = native_to_linux_itimerspec(&lots, &ots);
1006 error = copyout(&lots, args->old_value, sizeof(lots));
1015 linux_timerfd_expire(void *arg)
1017 struct timespec cts, ts;
1019 struct timerfd *tfd;
1021 tfd = (struct timerfd *)arg;
1023 linux_timerfd_clocktime(tfd, &cts);
1024 if (timespeccmp(&cts, &tfd->tfd_time.it_value, >=)) {
1025 if (timespecisset(&tfd->tfd_time.it_interval))
1026 timespecadd(&tfd->tfd_time.it_value,
1027 &tfd->tfd_time.it_interval,
1028 &tfd->tfd_time.it_value);
1030 /* single shot timer */
1031 timespecclear(&tfd->tfd_time.it_value);
1032 if (timespecisset(&tfd->tfd_time.it_value)) {
1033 timespecsub(&tfd->tfd_time.it_value, &cts, &ts);
1034 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1035 callout_reset(&tfd->tfd_callout, tvtohz(&tv),
1036 linux_timerfd_expire, tfd);
1039 KNOTE_LOCKED(&tfd->tfd_sel.si_note, 0);
1040 selwakeup(&tfd->tfd_sel);
1041 wakeup(&tfd->tfd_count);
1042 } else if (timespecisset(&tfd->tfd_time.it_value)) {
1043 timespecsub(&tfd->tfd_time.it_value, &cts, &ts);
1044 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1045 callout_reset(&tfd->tfd_callout, tvtohz(&tv),
1046 linux_timerfd_expire, tfd);