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);
328 /* zero event mask is legal */
329 if ((levents & (LINUX_EPOLL_EVRD | LINUX_EPOLL_EVWR)) == 0) {
330 EV_SET(kevent++, fd, EVFILT_READ, EV_ADD|EV_DISABLE, 0, 0, 0);
334 if ((levents & ~(LINUX_EPOLL_EVSUP)) != 0) {
338 KASSERT(pem != NULL, ("epoll proc emuldata not found.\n"));
339 KASSERT(pem->epoll != NULL, ("epoll proc epolldata not found.\n"));
341 LINUX_PEM_XLOCK(pem);
342 if ((pem->flags & LINUX_XUNSUP_EPOLL) == 0) {
343 pem->flags |= LINUX_XUNSUP_EPOLL;
344 LINUX_PEM_XUNLOCK(pem);
345 linux_msg(td, "epoll_ctl unsupported flags: 0x%x",
348 LINUX_PEM_XUNLOCK(pem);
356 * Structure converting function from kevent to epoll. In a case
357 * this is called on error in registration we store the error in
358 * event->data and pick it up later in linux_epoll_ctl().
361 kevent_to_epoll(struct kevent *kevent, struct epoll_event *l_event)
364 if ((kevent->flags & EV_ERROR) != 0) {
365 l_event->events = LINUX_EPOLLERR;
369 /* XXX EPOLLPRI, EPOLLHUP */
370 switch (kevent->filter) {
372 l_event->events = LINUX_EPOLLIN;
373 if ((kevent->flags & EV_EOF) != 0)
374 l_event->events |= LINUX_EPOLLRDHUP;
377 l_event->events = LINUX_EPOLLOUT;
383 * Copyout callback used by kevent. This converts kevent
384 * events to epoll events and copies them back to the
385 * userspace. This is also called on error on registering
389 epoll_kev_copyout(void *arg, struct kevent *kevp, int count)
391 struct epoll_copyout_args *args;
392 struct linux_pemuldata *pem;
393 struct epoll_emuldata *emd;
394 struct epoll_event *eep;
397 args = (struct epoll_copyout_args*) arg;
398 eep = malloc(sizeof(*eep) * count, M_EPOLL, M_WAITOK | M_ZERO);
400 pem = pem_find(args->p);
401 KASSERT(pem != NULL, ("epoll proc emuldata not found.\n"));
402 LINUX_PEM_SLOCK(pem);
404 KASSERT(emd != NULL, ("epoll proc epolldata not found.\n"));
406 for (i = 0; i < count; i++) {
407 kevent_to_epoll(&kevp[i], &eep[i]);
410 KASSERT(fd <= emd->fdc, ("epoll user data vector"
411 " is too small.\n"));
412 eep[i].data = emd->udata[fd];
414 LINUX_PEM_SUNLOCK(pem);
416 error = copyout(eep, args->leventlist, count * sizeof(*eep));
418 args->leventlist += count;
419 args->count += count;
420 } else if (args->error == 0)
428 * Copyin callback used by kevent. This copies already
429 * converted filters from kernel memory to the kevent
430 * internal kernel memory. Hence the memcpy instead of
434 epoll_kev_copyin(void *arg, struct kevent *kevp, int count)
436 struct epoll_copyin_args *args;
438 args = (struct epoll_copyin_args*) arg;
440 memcpy(kevp, args->changelist, count * sizeof(*kevp));
441 args->changelist += count;
447 * Load epoll filter, convert it to kevent filter
448 * and load it into kevent subsystem.
451 linux_epoll_ctl(struct thread *td, struct linux_epoll_ctl_args *args)
453 struct file *epfp, *fp;
454 struct epoll_copyin_args ciargs;
455 struct kevent kev[2];
456 struct kevent_copyops k_ops = { &ciargs,
459 struct epoll_event le;
464 if (args->op != LINUX_EPOLL_CTL_DEL) {
465 error = copyin(args->event, &le, sizeof(le));
470 error = fget(td, args->epfd,
471 cap_rights_init(&rights, CAP_KQUEUE_CHANGE), &epfp);
474 if (epfp->f_type != DTYPE_KQUEUE) {
479 /* Protect user data vector from incorrectly supplied fd. */
480 error = fget(td, args->fd, cap_rights_init(&rights, CAP_POLL_EVENT), &fp);
484 /* Linux disallows spying on himself */
490 ciargs.changelist = kev;
492 if (args->op != LINUX_EPOLL_CTL_DEL) {
493 error = epoll_to_kevent(td, args->fd, &le, kev, &nchanges);
499 case LINUX_EPOLL_CTL_MOD:
500 error = epoll_delete_all_events(td, epfp, args->fd);
505 case LINUX_EPOLL_CTL_ADD:
506 if (epoll_fd_registered(td, epfp, args->fd)) {
512 case LINUX_EPOLL_CTL_DEL:
513 /* CTL_DEL means unregister this fd with this epoll */
514 error = epoll_delete_all_events(td, epfp, args->fd);
522 epoll_fd_install(td, args->fd, le.data);
524 error = kern_kevent_fp(td, epfp, nchanges, 0, &k_ops, NULL);
535 * Wait for a filter to be triggered on the epoll file descriptor.
538 linux_epoll_wait_common(struct thread *td, int epfd, struct epoll_event *events,
539 int maxevents, int timeout, sigset_t *uset)
541 struct epoll_copyout_args coargs;
542 struct kevent_copyops k_ops = { &coargs,
545 struct timespec ts, *tsp;
551 if (maxevents <= 0 || maxevents > LINUX_MAX_EVENTS)
554 error = fget(td, epfd,
555 cap_rights_init(&rights, CAP_KQUEUE_EVENT), &epfp);
558 if (epfp->f_type != DTYPE_KQUEUE) {
563 error = kern_sigprocmask(td, SIG_SETMASK, uset,
567 td->td_pflags |= TDP_OLDMASK;
569 * Make sure that ast() is called on return to
570 * usermode and TDP_OLDMASK is cleared, restoring old
574 td->td_flags |= TDF_ASTPENDING;
579 coargs.leventlist = events;
580 coargs.p = td->td_proc;
585 * Linux epoll_wait(2) man page states that timeout of -1 causes caller
586 * to block indefinitely. Real implementation does it if any negative
587 * timeout value is passed.
590 /* Convert from milliseconds to timespec. */
591 ts.tv_sec = timeout / 1000;
592 ts.tv_nsec = (timeout % 1000) * 1000000;
598 error = kern_kevent_fp(td, epfp, 0, maxevents, &k_ops, tsp);
599 if (error == 0 && coargs.error != 0)
600 error = coargs.error;
603 * kern_kevent might return ENOMEM which is not expected from epoll_wait.
604 * Maybe we should translate that but I don't think it matters at all.
607 td->td_retval[0] = coargs.count;
610 error = kern_sigprocmask(td, SIG_SETMASK, &omask,
617 #ifdef LINUX_LEGACY_SYSCALLS
619 linux_epoll_wait(struct thread *td, struct linux_epoll_wait_args *args)
622 return (linux_epoll_wait_common(td, args->epfd, args->events,
623 args->maxevents, args->timeout, NULL));
628 linux_epoll_pwait(struct thread *td, struct linux_epoll_pwait_args *args)
630 sigset_t mask, *pmask;
634 if (args->mask != NULL) {
635 if (args->sigsetsize != sizeof(l_sigset_t))
637 error = copyin(args->mask, &lmask, sizeof(l_sigset_t));
640 linux_to_bsd_sigset(&lmask, &mask);
644 return (linux_epoll_wait_common(td, args->epfd, args->events,
645 args->maxevents, args->timeout, pmask));
649 epoll_register_kevent(struct thread *td, struct file *epfp, int fd, int filter,
652 struct epoll_copyin_args ciargs;
654 struct kevent_copyops k_ops = { &ciargs,
658 ciargs.changelist = &kev;
659 EV_SET(&kev, fd, filter, flags, 0, 0, 0);
661 return (kern_kevent_fp(td, epfp, 1, 0, &k_ops, NULL));
665 epoll_fd_registered(struct thread *td, struct file *epfp, int fd)
668 * Set empty filter flags to avoid accidental modification of already
669 * registered events. In the case of event re-registration:
670 * 1. If event does not exists kevent() does nothing and returns ENOENT
671 * 2. If event does exists, it's enabled/disabled state is preserved
672 * but fflags, data and udata fields are overwritten. So we can not
673 * set socket lowats and store user's context pointer in udata.
675 if (epoll_register_kevent(td, epfp, fd, EVFILT_READ, 0) != ENOENT ||
676 epoll_register_kevent(td, epfp, fd, EVFILT_WRITE, 0) != ENOENT)
683 epoll_delete_all_events(struct thread *td, struct file *epfp, int fd)
687 error1 = epoll_register_kevent(td, epfp, fd, EVFILT_READ, EV_DELETE);
688 error2 = epoll_register_kevent(td, epfp, fd, EVFILT_WRITE, EV_DELETE);
690 /* return 0 if at least one result positive */
691 return (error1 == 0 ? 0 : error2);
695 eventfd_create(struct thread *td, uint32_t initval, int flags)
697 struct filedesc *fdp;
700 int fflags, fd, error;
703 if ((flags & LINUX_O_CLOEXEC) != 0)
706 fdp = td->td_proc->p_fd;
707 error = falloc(td, &fp, &fd, fflags);
711 efd = malloc(sizeof(*efd), M_EPOLL, M_WAITOK | M_ZERO);
712 efd->efd_flags = flags;
713 efd->efd_count = initval;
714 mtx_init(&efd->efd_lock, "eventfd", NULL, MTX_DEF);
716 knlist_init_mtx(&efd->efd_sel.si_note, &efd->efd_lock);
718 fflags = FREAD | FWRITE;
719 if ((flags & LINUX_O_NONBLOCK) != 0)
722 finit(fp, fflags, DTYPE_LINUXEFD, efd, &eventfdops);
725 td->td_retval[0] = fd;
729 #ifdef LINUX_LEGACY_SYSCALLS
731 linux_eventfd(struct thread *td, struct linux_eventfd_args *args)
734 return (eventfd_create(td, args->initval, 0));
739 linux_eventfd2(struct thread *td, struct linux_eventfd2_args *args)
742 if ((args->flags & ~(LINUX_O_CLOEXEC|LINUX_O_NONBLOCK|LINUX_EFD_SEMAPHORE)) != 0)
745 return (eventfd_create(td, args->initval, args->flags));
749 eventfd_close(struct file *fp, struct thread *td)
754 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
757 seldrain(&efd->efd_sel);
758 knlist_destroy(&efd->efd_sel.si_note);
760 fp->f_ops = &badfileops;
761 mtx_destroy(&efd->efd_lock);
768 eventfd_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
769 int flags, struct thread *td)
776 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
779 if (uio->uio_resid < sizeof(eventfd_t))
783 mtx_lock(&efd->efd_lock);
785 if (efd->efd_count == 0) {
786 if ((fp->f_flag & FNONBLOCK) != 0) {
787 mtx_unlock(&efd->efd_lock);
790 error = mtx_sleep(&efd->efd_count, &efd->efd_lock, PCATCH, "lefdrd", 0);
795 if ((efd->efd_flags & LINUX_EFD_SEMAPHORE) != 0) {
799 count = efd->efd_count;
802 KNOTE_LOCKED(&efd->efd_sel.si_note, 0);
803 selwakeup(&efd->efd_sel);
804 wakeup(&efd->efd_count);
805 mtx_unlock(&efd->efd_lock);
806 error = uiomove(&count, sizeof(eventfd_t), uio);
808 mtx_unlock(&efd->efd_lock);
814 eventfd_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
815 int flags, struct thread *td)
822 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
825 if (uio->uio_resid < sizeof(eventfd_t))
828 error = uiomove(&count, sizeof(eventfd_t), uio);
831 if (count == UINT64_MAX)
834 mtx_lock(&efd->efd_lock);
836 if (UINT64_MAX - efd->efd_count <= count) {
837 if ((fp->f_flag & FNONBLOCK) != 0) {
838 mtx_unlock(&efd->efd_lock);
839 /* Do not not return the number of bytes written */
840 uio->uio_resid += sizeof(eventfd_t);
843 error = mtx_sleep(&efd->efd_count, &efd->efd_lock,
844 PCATCH, "lefdwr", 0);
849 efd->efd_count += count;
850 KNOTE_LOCKED(&efd->efd_sel.si_note, 0);
851 selwakeup(&efd->efd_sel);
852 wakeup(&efd->efd_count);
854 mtx_unlock(&efd->efd_lock);
860 eventfd_poll(struct file *fp, int events, struct ucred *active_cred,
867 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
870 mtx_lock(&efd->efd_lock);
871 if ((events & (POLLIN|POLLRDNORM)) && efd->efd_count > 0)
872 revents |= events & (POLLIN|POLLRDNORM);
873 if ((events & (POLLOUT|POLLWRNORM)) && UINT64_MAX - 1 > efd->efd_count)
874 revents |= events & (POLLOUT|POLLWRNORM);
876 selrecord(td, &efd->efd_sel);
877 mtx_unlock(&efd->efd_lock);
884 eventfd_kqfilter(struct file *fp, struct knote *kn)
889 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
892 mtx_lock(&efd->efd_lock);
893 switch (kn->kn_filter) {
895 kn->kn_fop = &eventfd_rfiltops;
898 kn->kn_fop = &eventfd_wfiltops;
901 mtx_unlock(&efd->efd_lock);
906 knlist_add(&efd->efd_sel.si_note, kn, 1);
907 mtx_unlock(&efd->efd_lock);
913 filt_eventfddetach(struct knote *kn)
915 struct eventfd *efd = kn->kn_hook;
917 mtx_lock(&efd->efd_lock);
918 knlist_remove(&efd->efd_sel.si_note, kn, 1);
919 mtx_unlock(&efd->efd_lock);
924 filt_eventfdread(struct knote *kn, long hint)
926 struct eventfd *efd = kn->kn_hook;
929 mtx_assert(&efd->efd_lock, MA_OWNED);
930 ret = (efd->efd_count > 0);
937 filt_eventfdwrite(struct knote *kn, long hint)
939 struct eventfd *efd = kn->kn_hook;
942 mtx_assert(&efd->efd_lock, MA_OWNED);
943 ret = (UINT64_MAX - 1 > efd->efd_count);
950 eventfd_ioctl(struct file *fp, u_long cmd, void *data,
951 struct ucred *active_cred, struct thread *td)
954 if (fp->f_data == NULL || (fp->f_type != DTYPE_LINUXEFD &&
955 fp->f_type != DTYPE_LINUXTFD))
962 atomic_set_int(&fp->f_flag, FNONBLOCK);
964 atomic_clear_int(&fp->f_flag, FNONBLOCK);
974 eventfd_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
983 eventfd_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
986 kif->kf_type = KF_TYPE_UNKNOWN;
991 linux_timerfd_create(struct thread *td, struct linux_timerfd_create_args *args)
993 struct filedesc *fdp;
997 int fflags, fd, error;
999 if ((args->flags & ~LINUX_TFD_CREATE_FLAGS) != 0)
1002 error = linux_to_native_clockid(&clockid, args->clockid);
1005 if (clockid != CLOCK_REALTIME && clockid != CLOCK_MONOTONIC)
1009 if ((args->flags & LINUX_TFD_CLOEXEC) != 0)
1010 fflags |= O_CLOEXEC;
1012 fdp = td->td_proc->p_fd;
1013 error = falloc(td, &fp, &fd, fflags);
1017 tfd = malloc(sizeof(*tfd), M_EPOLL, M_WAITOK | M_ZERO);
1018 tfd->tfd_clockid = clockid;
1019 mtx_init(&tfd->tfd_lock, "timerfd", NULL, MTX_DEF);
1021 callout_init_mtx(&tfd->tfd_callout, &tfd->tfd_lock, 0);
1022 knlist_init_mtx(&tfd->tfd_sel.si_note, &tfd->tfd_lock);
1025 if ((args->flags & LINUX_O_NONBLOCK) != 0)
1026 fflags |= FNONBLOCK;
1028 finit(fp, fflags, DTYPE_LINUXTFD, tfd, &timerfdops);
1031 td->td_retval[0] = fd;
1036 timerfd_close(struct file *fp, struct thread *td)
1038 struct timerfd *tfd;
1041 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
1044 timespecclear(&tfd->tfd_time.it_value);
1045 timespecclear(&tfd->tfd_time.it_interval);
1047 mtx_lock(&tfd->tfd_lock);
1048 callout_drain(&tfd->tfd_callout);
1049 mtx_unlock(&tfd->tfd_lock);
1051 seldrain(&tfd->tfd_sel);
1052 knlist_destroy(&tfd->tfd_sel.si_note);
1054 fp->f_ops = &badfileops;
1055 mtx_destroy(&tfd->tfd_lock);
1062 timerfd_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
1063 int flags, struct thread *td)
1065 struct timerfd *tfd;
1070 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
1073 if (uio->uio_resid < sizeof(timerfd_t))
1077 mtx_lock(&tfd->tfd_lock);
1079 if (tfd->tfd_canceled) {
1081 mtx_unlock(&tfd->tfd_lock);
1084 if (tfd->tfd_count == 0) {
1085 if ((fp->f_flag & FNONBLOCK) != 0) {
1086 mtx_unlock(&tfd->tfd_lock);
1089 error = mtx_sleep(&tfd->tfd_count, &tfd->tfd_lock, PCATCH, "ltfdrd", 0);
1094 count = tfd->tfd_count;
1096 mtx_unlock(&tfd->tfd_lock);
1097 error = uiomove(&count, sizeof(timerfd_t), uio);
1099 mtx_unlock(&tfd->tfd_lock);
1105 timerfd_poll(struct file *fp, int events, struct ucred *active_cred,
1108 struct timerfd *tfd;
1112 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
1115 mtx_lock(&tfd->tfd_lock);
1116 if ((events & (POLLIN|POLLRDNORM)) && tfd->tfd_count > 0)
1117 revents |= events & (POLLIN|POLLRDNORM);
1119 selrecord(td, &tfd->tfd_sel);
1120 mtx_unlock(&tfd->tfd_lock);
1127 timerfd_kqfilter(struct file *fp, struct knote *kn)
1129 struct timerfd *tfd;
1132 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL)
1135 if (kn->kn_filter == EVFILT_READ)
1136 kn->kn_fop = &timerfd_rfiltops;
1141 knlist_add(&tfd->tfd_sel.si_note, kn, 0);
1147 filt_timerfddetach(struct knote *kn)
1149 struct timerfd *tfd = kn->kn_hook;
1151 mtx_lock(&tfd->tfd_lock);
1152 knlist_remove(&tfd->tfd_sel.si_note, kn, 1);
1153 mtx_unlock(&tfd->tfd_lock);
1158 filt_timerfdread(struct knote *kn, long hint)
1160 struct timerfd *tfd = kn->kn_hook;
1162 return (tfd->tfd_count > 0);
1167 timerfd_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
1176 timerfd_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
1179 kif->kf_type = KF_TYPE_UNKNOWN;
1184 linux_timerfd_clocktime(struct timerfd *tfd, struct timespec *ts)
1187 if (tfd->tfd_clockid == CLOCK_REALTIME)
1189 else /* CLOCK_MONOTONIC */
1194 linux_timerfd_curval(struct timerfd *tfd, struct itimerspec *ots)
1196 struct timespec cts;
1198 linux_timerfd_clocktime(tfd, &cts);
1199 *ots = tfd->tfd_time;
1200 if (ots->it_value.tv_sec != 0 || ots->it_value.tv_nsec != 0) {
1201 timespecsub(&ots->it_value, &cts, &ots->it_value);
1202 if (ots->it_value.tv_sec < 0 ||
1203 (ots->it_value.tv_sec == 0 &&
1204 ots->it_value.tv_nsec == 0)) {
1205 ots->it_value.tv_sec = 0;
1206 ots->it_value.tv_nsec = 1;
1212 linux_timerfd_gettime(struct thread *td, struct linux_timerfd_gettime_args *args)
1214 struct l_itimerspec lots;
1215 struct itimerspec ots;
1216 struct timerfd *tfd;
1220 error = fget(td, args->fd, &cap_read_rights, &fp);
1224 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL) {
1229 mtx_lock(&tfd->tfd_lock);
1230 linux_timerfd_curval(tfd, &ots);
1231 mtx_unlock(&tfd->tfd_lock);
1233 error = native_to_linux_itimerspec(&lots, &ots);
1235 error = copyout(&lots, args->old_value, sizeof(lots));
1243 linux_timerfd_settime(struct thread *td, struct linux_timerfd_settime_args *args)
1245 struct l_itimerspec lots;
1246 struct itimerspec nts, ots;
1247 struct timespec cts, ts;
1248 struct timerfd *tfd;
1253 if ((args->flags & ~LINUX_TFD_SETTIME_FLAGS) != 0)
1256 error = copyin(args->new_value, &lots, sizeof(lots));
1259 error = linux_to_native_itimerspec(&nts, &lots);
1263 error = fget(td, args->fd, &cap_write_rights, &fp);
1267 if (fp->f_type != DTYPE_LINUXTFD || tfd == NULL) {
1272 mtx_lock(&tfd->tfd_lock);
1273 if (!timespecisset(&nts.it_value))
1274 timespecclear(&nts.it_interval);
1275 if (args->old_value != NULL)
1276 linux_timerfd_curval(tfd, &ots);
1278 tfd->tfd_time = nts;
1279 if (timespecisset(&nts.it_value)) {
1280 linux_timerfd_clocktime(tfd, &cts);
1282 if ((args->flags & LINUX_TFD_TIMER_ABSTIME) == 0) {
1283 timespecadd(&tfd->tfd_time.it_value, &cts,
1284 &tfd->tfd_time.it_value);
1286 timespecsub(&ts, &cts, &ts);
1288 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1289 callout_reset(&tfd->tfd_callout, tvtohz(&tv),
1290 linux_timerfd_expire, tfd);
1291 tfd->tfd_canceled = false;
1293 tfd->tfd_canceled = true;
1294 callout_stop(&tfd->tfd_callout);
1296 mtx_unlock(&tfd->tfd_lock);
1298 if (args->old_value != NULL) {
1299 error = native_to_linux_itimerspec(&lots, &ots);
1301 error = copyout(&lots, args->old_value, sizeof(lots));
1310 linux_timerfd_expire(void *arg)
1312 struct timespec cts, ts;
1314 struct timerfd *tfd;
1316 tfd = (struct timerfd *)arg;
1318 linux_timerfd_clocktime(tfd, &cts);
1319 if (timespeccmp(&cts, &tfd->tfd_time.it_value, >=)) {
1320 if (timespecisset(&tfd->tfd_time.it_interval))
1321 timespecadd(&tfd->tfd_time.it_value,
1322 &tfd->tfd_time.it_interval,
1323 &tfd->tfd_time.it_value);
1325 /* single shot timer */
1326 timespecclear(&tfd->tfd_time.it_value);
1327 if (timespecisset(&tfd->tfd_time.it_value)) {
1328 timespecsub(&tfd->tfd_time.it_value, &cts, &ts);
1329 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1330 callout_reset(&tfd->tfd_callout, tvtohz(&tv),
1331 linux_timerfd_expire, tfd);
1334 KNOTE_LOCKED(&tfd->tfd_sel.si_note, 0);
1335 selwakeup(&tfd->tfd_sel);
1336 wakeup(&tfd->tfd_count);
1337 } else if (timespecisset(&tfd->tfd_time.it_value)) {
1338 timespecsub(&tfd->tfd_time.it_value, &cts, &ts);
1339 TIMESPEC_TO_TIMEVAL(&tv, &ts);
1340 callout_reset(&tfd->tfd_callout, tvtohz(&tv),
1341 linux_timerfd_expire, tfd);