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 *);
225 epoll_fd_install(struct thread *td, int fd, epoll_udata_t udata)
227 struct linux_pemuldata *pem;
228 struct epoll_emuldata *emd;
234 KASSERT(pem != NULL, ("epoll proc emuldata not found.\n"));
236 LINUX_PEM_XLOCK(pem);
237 if (pem->epoll == NULL) {
238 emd = malloc(EPOLL_SIZE(fd), M_EPOLL, M_WAITOK);
244 emd = realloc(emd, EPOLL_SIZE(fd), M_EPOLL, M_WAITOK);
249 emd->udata[fd] = udata;
250 LINUX_PEM_XUNLOCK(pem);
254 epoll_create_common(struct thread *td, int flags)
258 error = kern_kqueue(td, flags, NULL);
262 epoll_fd_install(td, EPOLL_DEF_SZ, 0);
267 #ifdef LINUX_LEGACY_SYSCALLS
269 linux_epoll_create(struct thread *td, struct linux_epoll_create_args *args)
273 * args->size is unused. Linux just tests it
274 * and then forgets it as well.
279 return (epoll_create_common(td, 0));
284 linux_epoll_create1(struct thread *td, struct linux_epoll_create1_args *args)
288 if ((args->flags & ~(LINUX_O_CLOEXEC)) != 0)
292 if ((args->flags & LINUX_O_CLOEXEC) != 0)
295 return (epoll_create_common(td, flags));
298 /* Structure converting function from epoll to kevent. */
300 epoll_to_kevent(struct thread *td, int fd, struct epoll_event *l_event,
301 struct kevent *kevent, int *nkevents)
303 uint32_t levents = l_event->events;
304 struct linux_pemuldata *pem;
306 unsigned short kev_flags = EV_ADD | EV_ENABLE;
308 /* flags related to how event is registered */
309 if ((levents & LINUX_EPOLLONESHOT) != 0)
310 kev_flags |= EV_DISPATCH;
311 if ((levents & LINUX_EPOLLET) != 0)
312 kev_flags |= EV_CLEAR;
313 if ((levents & LINUX_EPOLLERR) != 0)
314 kev_flags |= EV_ERROR;
315 if ((levents & LINUX_EPOLLRDHUP) != 0)
318 /* flags related to what event is registered */
319 if ((levents & LINUX_EPOLL_EVRD) != 0) {
320 EV_SET(kevent++, fd, EVFILT_READ, kev_flags, 0, 0, 0);
323 if ((levents & LINUX_EPOLL_EVWR) != 0) {
324 EV_SET(kevent++, fd, EVFILT_WRITE, kev_flags, 0, 0, 0);
327 /* zero event mask is legal */
328 if ((levents & (LINUX_EPOLL_EVRD | LINUX_EPOLL_EVWR)) == 0) {
329 EV_SET(kevent++, fd, EVFILT_READ, EV_ADD|EV_DISABLE, 0, 0, 0);
333 if ((levents & ~(LINUX_EPOLL_EVSUP)) != 0) {
337 KASSERT(pem != NULL, ("epoll proc emuldata not found.\n"));
338 KASSERT(pem->epoll != NULL, ("epoll proc epolldata not found.\n"));
340 LINUX_PEM_XLOCK(pem);
341 if ((pem->flags & LINUX_XUNSUP_EPOLL) == 0) {
342 pem->flags |= LINUX_XUNSUP_EPOLL;
343 LINUX_PEM_XUNLOCK(pem);
344 linux_msg(td, "epoll_ctl unsupported flags: 0x%x",
347 LINUX_PEM_XUNLOCK(pem);
355 * Structure converting function from kevent to epoll. In a case
356 * this is called on error in registration we store the error in
357 * event->data and pick it up later in linux_epoll_ctl().
360 kevent_to_epoll(struct kevent *kevent, struct epoll_event *l_event)
363 if ((kevent->flags & EV_ERROR) != 0) {
364 l_event->events = LINUX_EPOLLERR;
368 /* XXX EPOLLPRI, EPOLLHUP */
369 switch (kevent->filter) {
371 l_event->events = LINUX_EPOLLIN;
372 if ((kevent->flags & EV_EOF) != 0)
373 l_event->events |= LINUX_EPOLLRDHUP;
376 l_event->events = LINUX_EPOLLOUT;
382 * Copyout callback used by kevent. This converts kevent
383 * events to epoll events and copies them back to the
384 * userspace. This is also called on error on registering
388 epoll_kev_copyout(void *arg, struct kevent *kevp, int count)
390 struct epoll_copyout_args *args;
391 struct linux_pemuldata *pem;
392 struct epoll_emuldata *emd;
393 struct epoll_event *eep;
396 args = (struct epoll_copyout_args*) arg;
397 eep = malloc(sizeof(*eep) * count, M_EPOLL, M_WAITOK | M_ZERO);
399 pem = pem_find(args->p);
400 KASSERT(pem != NULL, ("epoll proc emuldata not found.\n"));
401 LINUX_PEM_SLOCK(pem);
403 KASSERT(emd != NULL, ("epoll proc epolldata not found.\n"));
405 for (i = 0; i < count; i++) {
406 kevent_to_epoll(&kevp[i], &eep[i]);
409 KASSERT(fd <= emd->fdc, ("epoll user data vector"
410 " is too small.\n"));
411 eep[i].data = emd->udata[fd];
413 LINUX_PEM_SUNLOCK(pem);
415 error = copyout(eep, args->leventlist, count * sizeof(*eep));
417 args->leventlist += count;
418 args->count += count;
419 } else if (args->error == 0)
427 * Copyin callback used by kevent. This copies already
428 * converted filters from kernel memory to the kevent
429 * internal kernel memory. Hence the memcpy instead of
433 epoll_kev_copyin(void *arg, struct kevent *kevp, int count)
435 struct epoll_copyin_args *args;
437 args = (struct epoll_copyin_args*) arg;
439 memcpy(kevp, args->changelist, count * sizeof(*kevp));
440 args->changelist += count;
446 * Load epoll filter, convert it to kevent filter
447 * and load it into kevent subsystem.
450 linux_epoll_ctl(struct thread *td, struct linux_epoll_ctl_args *args)
452 struct file *epfp, *fp;
453 struct epoll_copyin_args ciargs;
454 struct kevent kev[2];
455 struct kevent_copyops k_ops = { &ciargs,
458 struct epoll_event le;
463 if (args->op != LINUX_EPOLL_CTL_DEL) {
464 error = copyin(args->event, &le, sizeof(le));
469 error = fget(td, args->epfd,
470 cap_rights_init(&rights, CAP_KQUEUE_CHANGE), &epfp);
473 if (epfp->f_type != DTYPE_KQUEUE) {
478 /* Protect user data vector from incorrectly supplied fd. */
479 error = fget(td, args->fd, cap_rights_init(&rights, CAP_POLL_EVENT), &fp);
483 /* Linux disallows spying on himself */
489 ciargs.changelist = kev;
491 if (args->op != LINUX_EPOLL_CTL_DEL) {
492 error = epoll_to_kevent(td, args->fd, &le, kev, &nchanges);
498 case LINUX_EPOLL_CTL_MOD:
499 error = epoll_delete_all_events(td, epfp, args->fd);
504 case LINUX_EPOLL_CTL_ADD:
505 if (epoll_fd_registered(td, epfp, args->fd)) {
511 case LINUX_EPOLL_CTL_DEL:
512 /* CTL_DEL means unregister this fd with this epoll */
513 error = epoll_delete_all_events(td, epfp, args->fd);
521 epoll_fd_install(td, args->fd, le.data);
523 error = kern_kevent_fp(td, epfp, nchanges, 0, &k_ops, NULL);
534 * Wait for a filter to be triggered on the epoll file descriptor.
537 linux_epoll_wait_common(struct thread *td, int epfd, struct epoll_event *events,
538 int maxevents, int timeout, sigset_t *uset)
540 struct epoll_copyout_args coargs;
541 struct kevent_copyops k_ops = { &coargs,
544 struct timespec ts, *tsp;
550 if (maxevents <= 0 || maxevents > LINUX_MAX_EVENTS)
553 error = fget(td, epfd,
554 cap_rights_init(&rights, CAP_KQUEUE_EVENT), &epfp);
557 if (epfp->f_type != DTYPE_KQUEUE) {
562 error = kern_sigprocmask(td, SIG_SETMASK, uset,
566 td->td_pflags |= TDP_OLDMASK;
568 * Make sure that ast() is called on return to
569 * usermode and TDP_OLDMASK is cleared, restoring old
573 td->td_flags |= TDF_ASTPENDING;
577 coargs.leventlist = events;
578 coargs.p = td->td_proc;
583 * Linux epoll_wait(2) man page states that timeout of -1 causes caller
584 * to block indefinitely. Real implementation does it if any negative
585 * timeout value is passed.
588 /* Convert from milliseconds to timespec. */
589 ts.tv_sec = timeout / 1000;
590 ts.tv_nsec = (timeout % 1000) * 1000000;
596 error = kern_kevent_fp(td, epfp, 0, maxevents, &k_ops, tsp);
597 if (error == 0 && coargs.error != 0)
598 error = coargs.error;
601 * kern_kevent might return ENOMEM which is not expected from epoll_wait.
602 * Maybe we should translate that but I don't think it matters at all.
605 td->td_retval[0] = coargs.count;
608 error = kern_sigprocmask(td, SIG_SETMASK, &omask,
615 #ifdef LINUX_LEGACY_SYSCALLS
617 linux_epoll_wait(struct thread *td, struct linux_epoll_wait_args *args)
620 return (linux_epoll_wait_common(td, args->epfd, args->events,
621 args->maxevents, args->timeout, NULL));
626 linux_epoll_pwait(struct thread *td, struct linux_epoll_pwait_args *args)
628 sigset_t mask, *pmask;
632 if (args->mask != NULL) {
633 if (args->sigsetsize != sizeof(l_sigset_t))
635 error = copyin(args->mask, &lmask, sizeof(l_sigset_t));
638 linux_to_bsd_sigset(&lmask, &mask);
642 return (linux_epoll_wait_common(td, args->epfd, args->events,
643 args->maxevents, args->timeout, pmask));
647 epoll_register_kevent(struct thread *td, struct file *epfp, int fd, int filter,
650 struct epoll_copyin_args ciargs;
652 struct kevent_copyops k_ops = { &ciargs,
656 ciargs.changelist = &kev;
657 EV_SET(&kev, fd, filter, flags, 0, 0, 0);
659 return (kern_kevent_fp(td, epfp, 1, 0, &k_ops, NULL));
663 epoll_fd_registered(struct thread *td, struct file *epfp, int fd)
666 * Set empty filter flags to avoid accidental modification of already
667 * registered events. In the case of event re-registration:
668 * 1. If event does not exists kevent() does nothing and returns ENOENT
669 * 2. If event does exists, it's enabled/disabled state is preserved
670 * but fflags, data and udata fields are overwritten. So we can not
671 * set socket lowats and store user's context pointer in udata.
673 if (epoll_register_kevent(td, epfp, fd, EVFILT_READ, 0) != ENOENT ||
674 epoll_register_kevent(td, epfp, fd, EVFILT_WRITE, 0) != ENOENT)
681 epoll_delete_all_events(struct thread *td, struct file *epfp, int fd)
685 error1 = epoll_register_kevent(td, epfp, fd, EVFILT_READ, EV_DELETE);
686 error2 = epoll_register_kevent(td, epfp, fd, EVFILT_WRITE, EV_DELETE);
688 /* return 0 if at least one result positive */
689 return (error1 == 0 ? 0 : error2);
693 eventfd_create(struct thread *td, uint32_t initval, int flags)
695 struct filedesc *fdp;
698 int fflags, fd, error;
701 if ((flags & LINUX_O_CLOEXEC) != 0)
704 fdp = td->td_proc->p_fd;
705 error = falloc(td, &fp, &fd, fflags);
709 efd = malloc(sizeof(*efd), M_EPOLL, M_WAITOK | M_ZERO);
710 efd->efd_flags = flags;
711 efd->efd_count = initval;
712 mtx_init(&efd->efd_lock, "eventfd", NULL, MTX_DEF);
714 knlist_init_mtx(&efd->efd_sel.si_note, &efd->efd_lock);
716 fflags = FREAD | FWRITE;
717 if ((flags & LINUX_O_NONBLOCK) != 0)
720 finit(fp, fflags, DTYPE_LINUXEFD, efd, &eventfdops);
723 td->td_retval[0] = fd;
727 #ifdef LINUX_LEGACY_SYSCALLS
729 linux_eventfd(struct thread *td, struct linux_eventfd_args *args)
732 return (eventfd_create(td, args->initval, 0));
737 linux_eventfd2(struct thread *td, struct linux_eventfd2_args *args)
740 if ((args->flags & ~(LINUX_O_CLOEXEC|LINUX_O_NONBLOCK|LINUX_EFD_SEMAPHORE)) != 0)
743 return (eventfd_create(td, args->initval, args->flags));
747 eventfd_close(struct file *fp, struct thread *td)
752 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
755 seldrain(&efd->efd_sel);
756 knlist_destroy(&efd->efd_sel.si_note);
758 fp->f_ops = &badfileops;
759 mtx_destroy(&efd->efd_lock);
766 eventfd_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
767 int flags, struct thread *td)
774 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
777 if (uio->uio_resid < sizeof(eventfd_t))
781 mtx_lock(&efd->efd_lock);
783 if (efd->efd_count == 0) {
784 if ((fp->f_flag & FNONBLOCK) != 0) {
785 mtx_unlock(&efd->efd_lock);
788 error = mtx_sleep(&efd->efd_count, &efd->efd_lock, PCATCH, "lefdrd", 0);
793 if ((efd->efd_flags & LINUX_EFD_SEMAPHORE) != 0) {
797 count = efd->efd_count;
800 KNOTE_LOCKED(&efd->efd_sel.si_note, 0);
801 selwakeup(&efd->efd_sel);
802 wakeup(&efd->efd_count);
803 mtx_unlock(&efd->efd_lock);
804 error = uiomove(&count, sizeof(eventfd_t), uio);
806 mtx_unlock(&efd->efd_lock);
812 eventfd_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
813 int flags, struct thread *td)
820 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
823 if (uio->uio_resid < sizeof(eventfd_t))
826 error = uiomove(&count, sizeof(eventfd_t), uio);
829 if (count == UINT64_MAX)
832 mtx_lock(&efd->efd_lock);
834 if (UINT64_MAX - efd->efd_count <= count) {
835 if ((fp->f_flag & FNONBLOCK) != 0) {
836 mtx_unlock(&efd->efd_lock);
837 /* Do not not return the number of bytes written */
838 uio->uio_resid += sizeof(eventfd_t);
841 error = mtx_sleep(&efd->efd_count, &efd->efd_lock,
842 PCATCH, "lefdwr", 0);
847 efd->efd_count += count;
848 KNOTE_LOCKED(&efd->efd_sel.si_note, 0);
849 selwakeup(&efd->efd_sel);
850 wakeup(&efd->efd_count);
852 mtx_unlock(&efd->efd_lock);
858 eventfd_poll(struct file *fp, int events, struct ucred *active_cred,
865 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
868 mtx_lock(&efd->efd_lock);
869 if ((events & (POLLIN|POLLRDNORM)) && efd->efd_count > 0)
870 revents |= events & (POLLIN|POLLRDNORM);
871 if ((events & (POLLOUT|POLLWRNORM)) && UINT64_MAX - 1 > efd->efd_count)
872 revents |= events & (POLLOUT|POLLWRNORM);
874 selrecord(td, &efd->efd_sel);
875 mtx_unlock(&efd->efd_lock);
882 eventfd_kqfilter(struct file *fp, struct knote *kn)
887 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
890 mtx_lock(&efd->efd_lock);
891 switch (kn->kn_filter) {
893 kn->kn_fop = &eventfd_rfiltops;
896 kn->kn_fop = &eventfd_wfiltops;
899 mtx_unlock(&efd->efd_lock);
904 knlist_add(&efd->efd_sel.si_note, kn, 1);
905 mtx_unlock(&efd->efd_lock);
911 filt_eventfddetach(struct knote *kn)
913 struct eventfd *efd = kn->kn_hook;
915 mtx_lock(&efd->efd_lock);
916 knlist_remove(&efd->efd_sel.si_note, kn, 1);
917 mtx_unlock(&efd->efd_lock);
922 filt_eventfdread(struct knote *kn, long hint)
924 struct eventfd *efd = kn->kn_hook;
927 mtx_assert(&efd->efd_lock, MA_OWNED);
928 ret = (efd->efd_count > 0);
935 filt_eventfdwrite(struct knote *kn, long hint)
937 struct eventfd *efd = kn->kn_hook;
940 mtx_assert(&efd->efd_lock, MA_OWNED);
941 ret = (UINT64_MAX - 1 > efd->efd_count);
948 eventfd_ioctl(struct file *fp, u_long cmd, void *data,
949 struct ucred *active_cred, struct thread *td)
952 if (fp->f_data == NULL || (fp->f_type != DTYPE_LINUXEFD &&
953 fp->f_type != DTYPE_LINUXTFD))
960 atomic_set_int(&fp->f_flag, FNONBLOCK);
962 atomic_clear_int(&fp->f_flag, FNONBLOCK);
972 eventfd_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
981 eventfd_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
984 kif->kf_type = KF_TYPE_UNKNOWN;
989 linux_timerfd_create(struct thread *td, struct linux_timerfd_create_args *args)
991 struct filedesc *fdp;
995 int fflags, fd, error;
997 if ((args->flags & ~LINUX_TFD_CREATE_FLAGS) != 0)
1000 error = linux_to_native_clockid(&clockid, args->clockid);
1003 if (clockid != CLOCK_REALTIME && clockid != CLOCK_MONOTONIC)
1007 if ((args->flags & LINUX_TFD_CLOEXEC) != 0)
1008 fflags |= O_CLOEXEC;
1010 fdp = td->td_proc->p_fd;
1011 error = falloc(td, &fp, &fd, fflags);
1015 tfd = malloc(sizeof(*tfd), M_EPOLL, M_WAITOK | M_ZERO);
1016 tfd->tfd_clockid = clockid;
1017 mtx_init(&tfd->tfd_lock, "timerfd", NULL, MTX_DEF);
1019 callout_init_mtx(&tfd->tfd_callout, &tfd->tfd_lock, 0);
1020 knlist_init_mtx(&tfd->tfd_sel.si_note, &tfd->tfd_lock);
1023 if ((args->flags & LINUX_O_NONBLOCK) != 0)
1024 fflags |= FNONBLOCK;
1026 finit(fp, fflags, DTYPE_LINUXTFD, tfd, &timerfdops);
1029 td->td_retval[0] = fd;
1034 timerfd_close(struct file *fp, struct thread *td)
1036 struct timerfd *tfd;
1039 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
1042 timespecclear(&tfd->tfd_time.it_value);
1043 timespecclear(&tfd->tfd_time.it_interval);
1045 mtx_lock(&tfd->tfd_lock);
1046 callout_drain(&tfd->tfd_callout);
1047 mtx_unlock(&tfd->tfd_lock);
1049 seldrain(&tfd->tfd_sel);
1050 knlist_destroy(&tfd->tfd_sel.si_note);
1052 fp->f_ops = &badfileops;
1053 mtx_destroy(&tfd->tfd_lock);
1060 timerfd_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
1061 int flags, struct thread *td)
1063 struct timerfd *tfd;
1068 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
1071 if (uio->uio_resid < sizeof(timerfd_t))
1075 mtx_lock(&tfd->tfd_lock);
1077 if (tfd->tfd_canceled) {
1079 mtx_unlock(&tfd->tfd_lock);
1082 if (tfd->tfd_count == 0) {
1083 if ((fp->f_flag & FNONBLOCK) != 0) {
1084 mtx_unlock(&tfd->tfd_lock);
1087 error = mtx_sleep(&tfd->tfd_count, &tfd->tfd_lock, PCATCH, "ltfdrd", 0);
1092 count = tfd->tfd_count;
1094 mtx_unlock(&tfd->tfd_lock);
1095 error = uiomove(&count, sizeof(timerfd_t), uio);
1097 mtx_unlock(&tfd->tfd_lock);
1103 timerfd_poll(struct file *fp, int events, struct ucred *active_cred,
1106 struct timerfd *tfd;
1110 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
1113 mtx_lock(&tfd->tfd_lock);
1114 if ((events & (POLLIN|POLLRDNORM)) && tfd->tfd_count > 0)
1115 revents |= events & (POLLIN|POLLRDNORM);
1117 selrecord(td, &tfd->tfd_sel);
1118 mtx_unlock(&tfd->tfd_lock);
1125 timerfd_kqfilter(struct file *fp, struct knote *kn)
1127 struct timerfd *tfd;
1130 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
1133 if (kn->kn_filter == EVFILT_READ)
1134 kn->kn_fop = &timerfd_rfiltops;
1139 knlist_add(&tfd->tfd_sel.si_note, kn, 0);
1145 filt_timerfddetach(struct knote *kn)
1147 struct timerfd *tfd = kn->kn_hook;
1149 mtx_lock(&tfd->tfd_lock);
1150 knlist_remove(&tfd->tfd_sel.si_note, kn, 1);
1151 mtx_unlock(&tfd->tfd_lock);
1156 filt_timerfdread(struct knote *kn, long hint)
1158 struct timerfd *tfd = kn->kn_hook;
1160 return (tfd->tfd_count > 0);
1165 timerfd_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
1174 timerfd_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
1177 kif->kf_type = KF_TYPE_UNKNOWN;
1182 linux_timerfd_clocktime(struct timerfd *tfd, struct timespec *ts)
1185 if (tfd->tfd_clockid == CLOCK_REALTIME)
1187 else /* CLOCK_MONOTONIC */
1192 linux_timerfd_curval(struct timerfd *tfd, struct itimerspec *ots)
1194 struct timespec cts;
1196 linux_timerfd_clocktime(tfd, &cts);
1197 *ots = tfd->tfd_time;
1198 if (ots->it_value.tv_sec != 0 || ots->it_value.tv_nsec != 0) {
1199 timespecsub(&ots->it_value, &cts, &ots->it_value);
1200 if (ots->it_value.tv_sec < 0 ||
1201 (ots->it_value.tv_sec == 0 &&
1202 ots->it_value.tv_nsec == 0)) {
1203 ots->it_value.tv_sec = 0;
1204 ots->it_value.tv_nsec = 1;
1210 linux_timerfd_gettime(struct thread *td, struct linux_timerfd_gettime_args *args)
1212 struct l_itimerspec lots;
1213 struct itimerspec ots;
1214 struct timerfd *tfd;
1218 error = fget(td, args->fd, &cap_read_rights, &fp);
1222 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL) {
1227 mtx_lock(&tfd->tfd_lock);
1228 linux_timerfd_curval(tfd, &ots);
1229 mtx_unlock(&tfd->tfd_lock);
1231 error = native_to_linux_itimerspec(&lots, &ots);
1233 error = copyout(&lots, args->old_value, sizeof(lots));
1241 linux_timerfd_settime(struct thread *td, struct linux_timerfd_settime_args *args)
1243 struct l_itimerspec lots;
1244 struct itimerspec nts, ots;
1245 struct timespec cts, ts;
1246 struct timerfd *tfd;
1251 if ((args->flags & ~LINUX_TFD_SETTIME_FLAGS) != 0)
1254 error = copyin(args->new_value, &lots, sizeof(lots));
1257 error = linux_to_native_itimerspec(&nts, &lots);
1261 error = fget(td, args->fd, &cap_write_rights, &fp);
1265 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL) {
1270 mtx_lock(&tfd->tfd_lock);
1271 if (!timespecisset(&nts.it_value))
1272 timespecclear(&nts.it_interval);
1273 if (args->old_value != NULL)
1274 linux_timerfd_curval(tfd, &ots);
1276 tfd->tfd_time = nts;
1277 if (timespecisset(&nts.it_value)) {
1278 linux_timerfd_clocktime(tfd, &cts);
1280 if ((args->flags & LINUX_TFD_TIMER_ABSTIME) == 0) {
1281 timespecadd(&tfd->tfd_time.it_value, &cts,
1282 &tfd->tfd_time.it_value);
1284 timespecsub(&ts, &cts, &ts);
1286 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1287 callout_reset(&tfd->tfd_callout, tvtohz(&tv),
1288 linux_timerfd_expire, tfd);
1289 tfd->tfd_canceled = false;
1291 tfd->tfd_canceled = true;
1292 callout_stop(&tfd->tfd_callout);
1294 mtx_unlock(&tfd->tfd_lock);
1296 if (args->old_value != NULL) {
1297 error = native_to_linux_itimerspec(&lots, &ots);
1299 error = copyout(&lots, args->old_value, sizeof(lots));
1308 linux_timerfd_expire(void *arg)
1310 struct timespec cts, ts;
1312 struct timerfd *tfd;
1314 tfd = (struct timerfd *)arg;
1316 linux_timerfd_clocktime(tfd, &cts);
1317 if (timespeccmp(&cts, &tfd->tfd_time.it_value, >=)) {
1318 if (timespecisset(&tfd->tfd_time.it_interval))
1319 timespecadd(&tfd->tfd_time.it_value,
1320 &tfd->tfd_time.it_interval,
1321 &tfd->tfd_time.it_value);
1323 /* single shot timer */
1324 timespecclear(&tfd->tfd_time.it_value);
1325 if (timespecisset(&tfd->tfd_time.it_value)) {
1326 timespecsub(&tfd->tfd_time.it_value, &cts, &ts);
1327 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1328 callout_reset(&tfd->tfd_callout, tvtohz(&tv),
1329 linux_timerfd_expire, tfd);
1332 KNOTE_LOCKED(&tfd->tfd_sel.si_note, 0);
1333 selwakeup(&tfd->tfd_sel);
1334 wakeup(&tfd->tfd_count);
1335 } else if (timespecisset(&tfd->tfd_time.it_value)) {
1336 timespecsub(&tfd->tfd_time.it_value, &cts, &ts);
1337 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1338 callout_reset(&tfd->tfd_callout, tvtohz(&tv),
1339 linux_timerfd_expire, tfd);