2 * Copyright (c) 2007 Roman Divacky
3 * Copyright (c) 2014 Dmitry Chagin
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include "opt_compat.h"
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/imgact.h>
36 #include <sys/kernel.h>
37 #include <sys/limits.h>
39 #include <sys/mutex.h>
40 #include <sys/callout.h>
41 #include <sys/capsicum.h>
42 #include <sys/types.h>
45 #include <sys/filedesc.h>
46 #include <sys/filio.h>
47 #include <sys/errno.h>
48 #include <sys/event.h>
51 #include <sys/selinfo.h>
53 #include <sys/syscallsubr.h>
54 #include <sys/timespec.h>
57 #include <machine/../linux32/linux.h>
58 #include <machine/../linux32/linux32_proto.h>
60 #include <machine/../linux/linux.h>
61 #include <machine/../linux/linux_proto.h>
64 #include <compat/linux/linux_emul.h>
65 #include <compat/linux/linux_event.h>
66 #include <compat/linux/linux_file.h>
67 #include <compat/linux/linux_timer.h>
68 #include <compat/linux/linux_util.h>
71 * epoll defines 'struct epoll_event' with the field 'data' as 64 bits
72 * on all architectures. But on 32 bit architectures BSD 'struct kevent' only
73 * has 32 bit opaque pointer as 'udata' field. So we can't pass epoll supplied
74 * data verbatuim. Therefore we allocate 64-bit memory block to pass
75 * user supplied data for every file descriptor.
78 typedef uint64_t epoll_udata_t;
80 struct epoll_emuldata {
81 uint32_t fdc; /* epoll udata max index */
82 epoll_udata_t udata[1]; /* epoll user data vector */
85 #define EPOLL_DEF_SZ 16
86 #define EPOLL_SIZE(fdn) \
87 (sizeof(struct epoll_emuldata)+(fdn) * sizeof(epoll_udata_t))
93 #if defined(__amd64__)
94 __attribute__((packed))
98 #define LINUX_MAX_EVENTS (INT_MAX / sizeof(struct epoll_event))
100 static void epoll_fd_install(struct thread *td, int fd, epoll_udata_t udata);
101 static int epoll_to_kevent(struct thread *td, int fd,
102 struct epoll_event *l_event, struct kevent *kevent,
104 static void kevent_to_epoll(struct kevent *kevent, struct epoll_event *l_event);
105 static int epoll_kev_copyout(void *arg, struct kevent *kevp, int count);
106 static int epoll_kev_copyin(void *arg, struct kevent *kevp, int count);
107 static int epoll_register_kevent(struct thread *td, struct file *epfp,
108 int fd, int filter, unsigned int flags);
109 static int epoll_fd_registered(struct thread *td, struct file *epfp,
111 static int epoll_delete_all_events(struct thread *td, struct file *epfp,
114 struct epoll_copyin_args {
115 struct kevent *changelist;
118 struct epoll_copyout_args {
119 struct epoll_event *leventlist;
126 typedef uint64_t eventfd_t;
128 static fo_rdwr_t eventfd_read;
129 static fo_rdwr_t eventfd_write;
130 static fo_ioctl_t eventfd_ioctl;
131 static fo_poll_t eventfd_poll;
132 static fo_kqfilter_t eventfd_kqfilter;
133 static fo_stat_t eventfd_stat;
134 static fo_close_t eventfd_close;
135 static fo_fill_kinfo_t eventfd_fill_kinfo;
137 static struct fileops eventfdops = {
138 .fo_read = eventfd_read,
139 .fo_write = eventfd_write,
140 .fo_truncate = invfo_truncate,
141 .fo_ioctl = eventfd_ioctl,
142 .fo_poll = eventfd_poll,
143 .fo_kqfilter = eventfd_kqfilter,
144 .fo_stat = eventfd_stat,
145 .fo_close = eventfd_close,
146 .fo_chmod = invfo_chmod,
147 .fo_chown = invfo_chown,
148 .fo_sendfile = invfo_sendfile,
149 .fo_fill_kinfo = eventfd_fill_kinfo,
150 .fo_flags = DFLAG_PASSABLE
153 static void filt_eventfddetach(struct knote *kn);
154 static int filt_eventfdread(struct knote *kn, long hint);
155 static int filt_eventfdwrite(struct knote *kn, long hint);
157 static struct filterops eventfd_rfiltops = {
159 .f_detach = filt_eventfddetach,
160 .f_event = filt_eventfdread
162 static struct filterops eventfd_wfiltops = {
164 .f_detach = filt_eventfddetach,
165 .f_event = filt_eventfdwrite
169 typedef uint64_t timerfd_t;
171 static fo_rdwr_t timerfd_read;
172 static fo_poll_t timerfd_poll;
173 static fo_kqfilter_t timerfd_kqfilter;
174 static fo_stat_t timerfd_stat;
175 static fo_close_t timerfd_close;
176 static fo_fill_kinfo_t timerfd_fill_kinfo;
178 static struct fileops timerfdops = {
179 .fo_read = timerfd_read,
180 .fo_write = invfo_rdwr,
181 .fo_truncate = invfo_truncate,
182 .fo_ioctl = eventfd_ioctl,
183 .fo_poll = timerfd_poll,
184 .fo_kqfilter = timerfd_kqfilter,
185 .fo_stat = timerfd_stat,
186 .fo_close = timerfd_close,
187 .fo_chmod = invfo_chmod,
188 .fo_chown = invfo_chown,
189 .fo_sendfile = invfo_sendfile,
190 .fo_fill_kinfo = timerfd_fill_kinfo,
191 .fo_flags = DFLAG_PASSABLE
194 static void filt_timerfddetach(struct knote *kn);
195 static int filt_timerfdread(struct knote *kn, long hint);
197 static struct filterops timerfd_rfiltops = {
199 .f_detach = filt_timerfddetach,
200 .f_event = filt_timerfdread
206 struct selinfo efd_sel;
211 clockid_t tfd_clockid;
212 struct itimerspec tfd_time;
213 struct callout tfd_callout;
216 struct selinfo tfd_sel;
220 static int eventfd_create(struct thread *td, uint32_t initval, int flags);
221 static void linux_timerfd_expire(void *);
222 static void linux_timerfd_curval(struct timerfd *, struct itimerspec *);
226 epoll_fd_install(struct thread *td, int fd, epoll_udata_t udata)
228 struct linux_pemuldata *pem;
229 struct epoll_emuldata *emd;
235 KASSERT(pem != NULL, ("epoll proc emuldata not found.\n"));
237 LINUX_PEM_XLOCK(pem);
238 if (pem->epoll == NULL) {
239 emd = malloc(EPOLL_SIZE(fd), M_EPOLL, M_WAITOK);
245 emd = realloc(emd, EPOLL_SIZE(fd), M_EPOLL, M_WAITOK);
250 emd->udata[fd] = udata;
251 LINUX_PEM_XUNLOCK(pem);
255 epoll_create_common(struct thread *td, int flags)
259 error = kern_kqueue(td, flags, NULL);
263 epoll_fd_install(td, EPOLL_DEF_SZ, 0);
268 #ifdef LINUX_LEGACY_SYSCALLS
270 linux_epoll_create(struct thread *td, struct linux_epoll_create_args *args)
274 * args->size is unused. Linux just tests it
275 * and then forgets it as well.
280 return (epoll_create_common(td, 0));
285 linux_epoll_create1(struct thread *td, struct linux_epoll_create1_args *args)
289 if ((args->flags & ~(LINUX_O_CLOEXEC)) != 0)
293 if ((args->flags & LINUX_O_CLOEXEC) != 0)
296 return (epoll_create_common(td, flags));
299 /* Structure converting function from epoll to kevent. */
301 epoll_to_kevent(struct thread *td, int fd, struct epoll_event *l_event,
302 struct kevent *kevent, int *nkevents)
304 uint32_t levents = l_event->events;
305 struct linux_pemuldata *pem;
307 unsigned short kev_flags = EV_ADD | EV_ENABLE;
309 /* flags related to how event is registered */
310 if ((levents & LINUX_EPOLLONESHOT) != 0)
311 kev_flags |= EV_DISPATCH;
312 if ((levents & LINUX_EPOLLET) != 0)
313 kev_flags |= EV_CLEAR;
314 if ((levents & LINUX_EPOLLERR) != 0)
315 kev_flags |= EV_ERROR;
316 if ((levents & LINUX_EPOLLRDHUP) != 0)
319 /* flags related to what event is registered */
320 if ((levents & LINUX_EPOLL_EVRD) != 0) {
321 EV_SET(kevent++, fd, EVFILT_READ, kev_flags, 0, 0, 0);
324 if ((levents & LINUX_EPOLL_EVWR) != 0) {
325 EV_SET(kevent++, fd, EVFILT_WRITE, kev_flags, 0, 0, 0);
329 if ((levents & ~(LINUX_EPOLL_EVSUP)) != 0) {
333 KASSERT(pem != NULL, ("epoll proc emuldata not found.\n"));
334 KASSERT(pem->epoll != NULL, ("epoll proc epolldata not found.\n"));
336 LINUX_PEM_XLOCK(pem);
337 if ((pem->flags & LINUX_XUNSUP_EPOLL) == 0) {
338 pem->flags |= LINUX_XUNSUP_EPOLL;
339 LINUX_PEM_XUNLOCK(pem);
340 linux_msg(td, "epoll_ctl unsupported flags: 0x%x\n",
343 LINUX_PEM_XUNLOCK(pem);
351 * Structure converting function from kevent to epoll. In a case
352 * this is called on error in registration we store the error in
353 * event->data and pick it up later in linux_epoll_ctl().
356 kevent_to_epoll(struct kevent *kevent, struct epoll_event *l_event)
359 if ((kevent->flags & EV_ERROR) != 0) {
360 l_event->events = LINUX_EPOLLERR;
364 /* XXX EPOLLPRI, EPOLLHUP */
365 switch (kevent->filter) {
367 l_event->events = LINUX_EPOLLIN;
368 if ((kevent->flags & EV_EOF) != 0)
369 l_event->events |= LINUX_EPOLLRDHUP;
372 l_event->events = LINUX_EPOLLOUT;
378 * Copyout callback used by kevent. This converts kevent
379 * events to epoll events and copies them back to the
380 * userspace. This is also called on error on registering
384 epoll_kev_copyout(void *arg, struct kevent *kevp, int count)
386 struct epoll_copyout_args *args;
387 struct linux_pemuldata *pem;
388 struct epoll_emuldata *emd;
389 struct epoll_event *eep;
392 args = (struct epoll_copyout_args*) arg;
393 eep = malloc(sizeof(*eep) * count, M_EPOLL, M_WAITOK | M_ZERO);
395 pem = pem_find(args->p);
396 KASSERT(pem != NULL, ("epoll proc emuldata not found.\n"));
397 LINUX_PEM_SLOCK(pem);
399 KASSERT(emd != NULL, ("epoll proc epolldata not found.\n"));
401 for (i = 0; i < count; i++) {
402 kevent_to_epoll(&kevp[i], &eep[i]);
405 KASSERT(fd <= emd->fdc, ("epoll user data vector"
406 " is too small.\n"));
407 eep[i].data = emd->udata[fd];
409 LINUX_PEM_SUNLOCK(pem);
411 error = copyout(eep, args->leventlist, count * sizeof(*eep));
413 args->leventlist += count;
414 args->count += count;
415 } else if (args->error == 0)
423 * Copyin callback used by kevent. This copies already
424 * converted filters from kernel memory to the kevent
425 * internal kernel memory. Hence the memcpy instead of
429 epoll_kev_copyin(void *arg, struct kevent *kevp, int count)
431 struct epoll_copyin_args *args;
433 args = (struct epoll_copyin_args*) arg;
435 memcpy(kevp, args->changelist, count * sizeof(*kevp));
436 args->changelist += count;
442 * Load epoll filter, convert it to kevent filter
443 * and load it into kevent subsystem.
446 linux_epoll_ctl(struct thread *td, struct linux_epoll_ctl_args *args)
448 struct file *epfp, *fp;
449 struct epoll_copyin_args ciargs;
450 struct kevent kev[2];
451 struct kevent_copyops k_ops = { &ciargs,
454 struct epoll_event le;
459 if (args->op != LINUX_EPOLL_CTL_DEL) {
460 error = copyin(args->event, &le, sizeof(le));
465 error = fget(td, args->epfd,
466 cap_rights_init(&rights, CAP_KQUEUE_CHANGE), &epfp);
469 if (epfp->f_type != DTYPE_KQUEUE) {
474 /* Protect user data vector from incorrectly supplied fd. */
475 error = fget(td, args->fd, cap_rights_init(&rights, CAP_POLL_EVENT), &fp);
479 /* Linux disallows spying on himself */
485 ciargs.changelist = kev;
487 if (args->op != LINUX_EPOLL_CTL_DEL) {
488 error = epoll_to_kevent(td, args->fd, &le, kev, &nchanges);
494 case LINUX_EPOLL_CTL_MOD:
495 error = epoll_delete_all_events(td, epfp, args->fd);
500 case LINUX_EPOLL_CTL_ADD:
501 if (epoll_fd_registered(td, epfp, args->fd)) {
507 case LINUX_EPOLL_CTL_DEL:
508 /* CTL_DEL means unregister this fd with this epoll */
509 error = epoll_delete_all_events(td, epfp, args->fd);
517 epoll_fd_install(td, args->fd, le.data);
519 error = kern_kevent_fp(td, epfp, nchanges, 0, &k_ops, NULL);
530 * Wait for a filter to be triggered on the epoll file descriptor.
533 linux_epoll_wait_common(struct thread *td, int epfd, struct epoll_event *events,
534 int maxevents, int timeout, sigset_t *uset)
536 struct epoll_copyout_args coargs;
537 struct kevent_copyops k_ops = { &coargs,
540 struct timespec ts, *tsp;
546 if (maxevents <= 0 || maxevents > LINUX_MAX_EVENTS)
549 error = fget(td, epfd,
550 cap_rights_init(&rights, CAP_KQUEUE_EVENT), &epfp);
553 if (epfp->f_type != DTYPE_KQUEUE) {
558 error = kern_sigprocmask(td, SIG_SETMASK, uset,
562 td->td_pflags |= TDP_OLDMASK;
564 * Make sure that ast() is called on return to
565 * usermode and TDP_OLDMASK is cleared, restoring old
569 td->td_flags |= TDF_ASTPENDING;
574 coargs.leventlist = events;
575 coargs.p = td->td_proc;
584 /* Convert from milliseconds to timespec. */
585 ts.tv_sec = timeout / 1000;
586 ts.tv_nsec = (timeout % 1000) * 1000000;
592 error = kern_kevent_fp(td, epfp, 0, maxevents, &k_ops, tsp);
593 if (error == 0 && coargs.error != 0)
594 error = coargs.error;
597 * kern_kevent might return ENOMEM which is not expected from epoll_wait.
598 * Maybe we should translate that but I don't think it matters at all.
601 td->td_retval[0] = coargs.count;
605 error = kern_sigprocmask(td, SIG_SETMASK, &omask,
612 #ifdef LINUX_LEGACY_SYSCALLS
614 linux_epoll_wait(struct thread *td, struct linux_epoll_wait_args *args)
617 return (linux_epoll_wait_common(td, args->epfd, args->events,
618 args->maxevents, args->timeout, NULL));
623 linux_epoll_pwait(struct thread *td, struct linux_epoll_pwait_args *args)
625 sigset_t mask, *pmask;
629 if (args->mask != NULL) {
630 if (args->sigsetsize != sizeof(l_sigset_t))
632 error = copyin(args->mask, &lmask, sizeof(l_sigset_t));
635 linux_to_bsd_sigset(&lmask, &mask);
639 return (linux_epoll_wait_common(td, args->epfd, args->events,
640 args->maxevents, args->timeout, pmask));
644 epoll_register_kevent(struct thread *td, struct file *epfp, int fd, int filter,
647 struct epoll_copyin_args ciargs;
649 struct kevent_copyops k_ops = { &ciargs,
653 ciargs.changelist = &kev;
654 EV_SET(&kev, fd, filter, flags, 0, 0, 0);
656 return (kern_kevent_fp(td, epfp, 1, 0, &k_ops, NULL));
660 epoll_fd_registered(struct thread *td, struct file *epfp, int fd)
663 * Set empty filter flags to avoid accidental modification of already
664 * registered events. In the case of event re-registration:
665 * 1. If event does not exists kevent() does nothing and returns ENOENT
666 * 2. If event does exists, it's enabled/disabled state is preserved
667 * but fflags, data and udata fields are overwritten. So we can not
668 * set socket lowats and store user's context pointer in udata.
670 if (epoll_register_kevent(td, epfp, fd, EVFILT_READ, 0) != ENOENT ||
671 epoll_register_kevent(td, epfp, fd, EVFILT_WRITE, 0) != ENOENT)
678 epoll_delete_all_events(struct thread *td, struct file *epfp, int fd)
682 error1 = epoll_register_kevent(td, epfp, fd, EVFILT_READ, EV_DELETE);
683 error2 = epoll_register_kevent(td, epfp, fd, EVFILT_WRITE, EV_DELETE);
685 /* return 0 if at least one result positive */
686 return (error1 == 0 ? 0 : error2);
690 eventfd_create(struct thread *td, uint32_t initval, int flags)
692 struct filedesc *fdp;
695 int fflags, fd, error;
698 if ((flags & LINUX_O_CLOEXEC) != 0)
701 fdp = td->td_proc->p_fd;
702 error = falloc(td, &fp, &fd, fflags);
706 efd = malloc(sizeof(*efd), M_EPOLL, M_WAITOK | M_ZERO);
707 efd->efd_flags = flags;
708 efd->efd_count = initval;
709 mtx_init(&efd->efd_lock, "eventfd", NULL, MTX_DEF);
711 knlist_init_mtx(&efd->efd_sel.si_note, &efd->efd_lock);
713 fflags = FREAD | FWRITE;
714 if ((flags & LINUX_O_NONBLOCK) != 0)
717 finit(fp, fflags, DTYPE_LINUXEFD, efd, &eventfdops);
720 td->td_retval[0] = fd;
724 #ifdef LINUX_LEGACY_SYSCALLS
726 linux_eventfd(struct thread *td, struct linux_eventfd_args *args)
729 return (eventfd_create(td, args->initval, 0));
734 linux_eventfd2(struct thread *td, struct linux_eventfd2_args *args)
737 if ((args->flags & ~(LINUX_O_CLOEXEC|LINUX_O_NONBLOCK|LINUX_EFD_SEMAPHORE)) != 0)
740 return (eventfd_create(td, args->initval, args->flags));
744 eventfd_close(struct file *fp, struct thread *td)
749 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
752 seldrain(&efd->efd_sel);
753 knlist_destroy(&efd->efd_sel.si_note);
755 fp->f_ops = &badfileops;
756 mtx_destroy(&efd->efd_lock);
763 eventfd_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
764 int flags, struct thread *td)
771 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
774 if (uio->uio_resid < sizeof(eventfd_t))
778 mtx_lock(&efd->efd_lock);
780 if (efd->efd_count == 0) {
781 if ((fp->f_flag & FNONBLOCK) != 0) {
782 mtx_unlock(&efd->efd_lock);
785 error = mtx_sleep(&efd->efd_count, &efd->efd_lock, PCATCH, "lefdrd", 0);
790 if ((efd->efd_flags & LINUX_EFD_SEMAPHORE) != 0) {
794 count = efd->efd_count;
797 KNOTE_LOCKED(&efd->efd_sel.si_note, 0);
798 selwakeup(&efd->efd_sel);
799 wakeup(&efd->efd_count);
800 mtx_unlock(&efd->efd_lock);
801 error = uiomove(&count, sizeof(eventfd_t), uio);
803 mtx_unlock(&efd->efd_lock);
809 eventfd_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
810 int flags, struct thread *td)
817 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
820 if (uio->uio_resid < sizeof(eventfd_t))
823 error = uiomove(&count, sizeof(eventfd_t), uio);
826 if (count == UINT64_MAX)
829 mtx_lock(&efd->efd_lock);
831 if (UINT64_MAX - efd->efd_count <= count) {
832 if ((fp->f_flag & FNONBLOCK) != 0) {
833 mtx_unlock(&efd->efd_lock);
834 /* Do not not return the number of bytes written */
835 uio->uio_resid += sizeof(eventfd_t);
838 error = mtx_sleep(&efd->efd_count, &efd->efd_lock,
839 PCATCH, "lefdwr", 0);
844 efd->efd_count += count;
845 KNOTE_LOCKED(&efd->efd_sel.si_note, 0);
846 selwakeup(&efd->efd_sel);
847 wakeup(&efd->efd_count);
849 mtx_unlock(&efd->efd_lock);
855 eventfd_poll(struct file *fp, int events, struct ucred *active_cred,
862 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
865 mtx_lock(&efd->efd_lock);
866 if ((events & (POLLIN|POLLRDNORM)) && efd->efd_count > 0)
867 revents |= events & (POLLIN|POLLRDNORM);
868 if ((events & (POLLOUT|POLLWRNORM)) && UINT64_MAX - 1 > efd->efd_count)
869 revents |= events & (POLLOUT|POLLWRNORM);
871 selrecord(td, &efd->efd_sel);
872 mtx_unlock(&efd->efd_lock);
879 eventfd_kqfilter(struct file *fp, struct knote *kn)
884 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
887 mtx_lock(&efd->efd_lock);
888 switch (kn->kn_filter) {
890 kn->kn_fop = &eventfd_rfiltops;
893 kn->kn_fop = &eventfd_wfiltops;
896 mtx_unlock(&efd->efd_lock);
901 knlist_add(&efd->efd_sel.si_note, kn, 1);
902 mtx_unlock(&efd->efd_lock);
908 filt_eventfddetach(struct knote *kn)
910 struct eventfd *efd = kn->kn_hook;
912 mtx_lock(&efd->efd_lock);
913 knlist_remove(&efd->efd_sel.si_note, kn, 1);
914 mtx_unlock(&efd->efd_lock);
919 filt_eventfdread(struct knote *kn, long hint)
921 struct eventfd *efd = kn->kn_hook;
924 mtx_assert(&efd->efd_lock, MA_OWNED);
925 ret = (efd->efd_count > 0);
932 filt_eventfdwrite(struct knote *kn, long hint)
934 struct eventfd *efd = kn->kn_hook;
937 mtx_assert(&efd->efd_lock, MA_OWNED);
938 ret = (UINT64_MAX - 1 > efd->efd_count);
945 eventfd_ioctl(struct file *fp, u_long cmd, void *data,
946 struct ucred *active_cred, struct thread *td)
949 if (fp->f_data == NULL || (fp->f_type != DTYPE_LINUXEFD &&
950 fp->f_type != DTYPE_LINUXTFD))
957 atomic_set_int(&fp->f_flag, FNONBLOCK);
959 atomic_clear_int(&fp->f_flag, FNONBLOCK);
969 eventfd_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
978 eventfd_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
981 kif->kf_type = KF_TYPE_UNKNOWN;
986 linux_timerfd_create(struct thread *td, struct linux_timerfd_create_args *args)
988 struct filedesc *fdp;
992 int fflags, fd, error;
994 if ((args->flags & ~LINUX_TFD_CREATE_FLAGS) != 0)
997 error = linux_to_native_clockid(&clockid, args->clockid);
1000 if (clockid != CLOCK_REALTIME && clockid != CLOCK_MONOTONIC)
1004 if ((args->flags & LINUX_TFD_CLOEXEC) != 0)
1005 fflags |= O_CLOEXEC;
1007 fdp = td->td_proc->p_fd;
1008 error = falloc(td, &fp, &fd, fflags);
1012 tfd = malloc(sizeof(*tfd), M_EPOLL, M_WAITOK | M_ZERO);
1013 tfd->tfd_clockid = clockid;
1014 mtx_init(&tfd->tfd_lock, "timerfd", NULL, MTX_DEF);
1016 callout_init_mtx(&tfd->tfd_callout, &tfd->tfd_lock, 0);
1017 knlist_init_mtx(&tfd->tfd_sel.si_note, &tfd->tfd_lock);
1020 if ((args->flags & LINUX_O_NONBLOCK) != 0)
1021 fflags |= FNONBLOCK;
1023 finit(fp, fflags, DTYPE_LINUXTFD, tfd, &timerfdops);
1026 td->td_retval[0] = fd;
1031 timerfd_close(struct file *fp, struct thread *td)
1033 struct timerfd *tfd;
1036 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
1039 timespecclear(&tfd->tfd_time.it_value);
1040 timespecclear(&tfd->tfd_time.it_interval);
1042 mtx_lock(&tfd->tfd_lock);
1043 callout_drain(&tfd->tfd_callout);
1044 mtx_unlock(&tfd->tfd_lock);
1046 seldrain(&tfd->tfd_sel);
1047 knlist_destroy(&tfd->tfd_sel.si_note);
1049 fp->f_ops = &badfileops;
1050 mtx_destroy(&tfd->tfd_lock);
1057 timerfd_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
1058 int flags, struct thread *td)
1060 struct timerfd *tfd;
1065 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
1068 if (uio->uio_resid < sizeof(timerfd_t))
1072 mtx_lock(&tfd->tfd_lock);
1074 if (tfd->tfd_canceled) {
1076 mtx_unlock(&tfd->tfd_lock);
1079 if (tfd->tfd_count == 0) {
1080 if ((fp->f_flag & FNONBLOCK) != 0) {
1081 mtx_unlock(&tfd->tfd_lock);
1084 error = mtx_sleep(&tfd->tfd_count, &tfd->tfd_lock, PCATCH, "ltfdrd", 0);
1089 count = tfd->tfd_count;
1091 mtx_unlock(&tfd->tfd_lock);
1092 error = uiomove(&count, sizeof(timerfd_t), uio);
1094 mtx_unlock(&tfd->tfd_lock);
1100 timerfd_poll(struct file *fp, int events, struct ucred *active_cred,
1103 struct timerfd *tfd;
1107 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
1110 mtx_lock(&tfd->tfd_lock);
1111 if ((events & (POLLIN|POLLRDNORM)) && tfd->tfd_count > 0)
1112 revents |= events & (POLLIN|POLLRDNORM);
1114 selrecord(td, &tfd->tfd_sel);
1115 mtx_unlock(&tfd->tfd_lock);
1122 timerfd_kqfilter(struct file *fp, struct knote *kn)
1124 struct timerfd *tfd;
1127 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
1130 if (kn->kn_filter == EVFILT_READ)
1131 kn->kn_fop = &timerfd_rfiltops;
1136 knlist_add(&tfd->tfd_sel.si_note, kn, 0);
1142 filt_timerfddetach(struct knote *kn)
1144 struct timerfd *tfd = kn->kn_hook;
1146 mtx_lock(&tfd->tfd_lock);
1147 knlist_remove(&tfd->tfd_sel.si_note, kn, 1);
1148 mtx_unlock(&tfd->tfd_lock);
1153 filt_timerfdread(struct knote *kn, long hint)
1155 struct timerfd *tfd = kn->kn_hook;
1157 return (tfd->tfd_count > 0);
1162 timerfd_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
1171 timerfd_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
1174 kif->kf_type = KF_TYPE_UNKNOWN;
1179 linux_timerfd_clocktime(struct timerfd *tfd, struct timespec *ts)
1182 if (tfd->tfd_clockid == CLOCK_REALTIME)
1184 else /* CLOCK_MONOTONIC */
1189 linux_timerfd_curval(struct timerfd *tfd, struct itimerspec *ots)
1191 struct timespec cts;
1193 linux_timerfd_clocktime(tfd, &cts);
1194 *ots = tfd->tfd_time;
1195 if (ots->it_value.tv_sec != 0 || ots->it_value.tv_nsec != 0) {
1196 timespecsub(&ots->it_value, &cts, &ots->it_value);
1197 if (ots->it_value.tv_sec < 0 ||
1198 (ots->it_value.tv_sec == 0 &&
1199 ots->it_value.tv_nsec == 0)) {
1200 ots->it_value.tv_sec = 0;
1201 ots->it_value.tv_nsec = 1;
1207 linux_timerfd_gettime(struct thread *td, struct linux_timerfd_gettime_args *args)
1209 struct l_itimerspec lots;
1210 struct itimerspec ots;
1211 struct timerfd *tfd;
1215 error = fget(td, args->fd, &cap_read_rights, &fp);
1219 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL) {
1224 mtx_lock(&tfd->tfd_lock);
1225 linux_timerfd_curval(tfd, &ots);
1226 mtx_unlock(&tfd->tfd_lock);
1228 error = native_to_linux_itimerspec(&lots, &ots);
1230 error = copyout(&lots, args->old_value, sizeof(lots));
1238 linux_timerfd_settime(struct thread *td, struct linux_timerfd_settime_args *args)
1240 struct l_itimerspec lots;
1241 struct itimerspec nts, ots;
1242 struct timespec cts, ts;
1243 struct timerfd *tfd;
1248 if ((args->flags & ~LINUX_TFD_SETTIME_FLAGS) != 0)
1251 error = copyin(args->new_value, &lots, sizeof(lots));
1254 error = linux_to_native_itimerspec(&nts, &lots);
1258 error = fget(td, args->fd, &cap_write_rights, &fp);
1262 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL) {
1267 mtx_lock(&tfd->tfd_lock);
1268 if (!timespecisset(&nts.it_value))
1269 timespecclear(&nts.it_interval);
1270 if (args->old_value != NULL)
1271 linux_timerfd_curval(tfd, &ots);
1273 tfd->tfd_time = nts;
1274 if (timespecisset(&nts.it_value)) {
1275 linux_timerfd_clocktime(tfd, &cts);
1277 if ((args->flags & LINUX_TFD_TIMER_ABSTIME) == 0) {
1278 timespecadd(&tfd->tfd_time.it_value, &cts,
1279 &tfd->tfd_time.it_value);
1281 timespecsub(&ts, &cts, &ts);
1283 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1284 callout_reset(&tfd->tfd_callout, tvtohz(&tv),
1285 linux_timerfd_expire, tfd);
1286 tfd->tfd_canceled = false;
1288 tfd->tfd_canceled = true;
1289 callout_stop(&tfd->tfd_callout);
1291 mtx_unlock(&tfd->tfd_lock);
1293 if (args->old_value != NULL) {
1294 error = native_to_linux_itimerspec(&lots, &ots);
1296 error = copyout(&lots, args->old_value, sizeof(lots));
1305 linux_timerfd_expire(void *arg)
1307 struct timespec cts, ts;
1309 struct timerfd *tfd;
1311 tfd = (struct timerfd *)arg;
1313 linux_timerfd_clocktime(tfd, &cts);
1314 if (timespeccmp(&cts, &tfd->tfd_time.it_value, >=)) {
1315 if (timespecisset(&tfd->tfd_time.it_interval))
1316 timespecadd(&tfd->tfd_time.it_value,
1317 &tfd->tfd_time.it_interval,
1318 &tfd->tfd_time.it_value);
1320 /* single shot timer */
1321 timespecclear(&tfd->tfd_time.it_value);
1322 if (timespecisset(&tfd->tfd_time.it_value)) {
1323 timespecsub(&tfd->tfd_time.it_value, &cts, &ts);
1324 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1325 callout_reset(&tfd->tfd_callout, tvtohz(&tv),
1326 linux_timerfd_expire, tfd);
1329 KNOTE_LOCKED(&tfd->tfd_sel.si_note, 0);
1330 selwakeup(&tfd->tfd_sel);
1331 wakeup(&tfd->tfd_count);
1332 } else if (timespecisset(&tfd->tfd_time.it_value)) {
1333 timespecsub(&tfd->tfd_time.it_value, &cts, &ts);
1334 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1335 callout_reset(&tfd->tfd_callout, tvtohz(&tv),
1336 linux_timerfd_expire, tfd);