1 /*-
2 * Copyright (c) 2007 Roman Divacky
3 * Copyright (c) 2014 Dmitry Chagin
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include "opt_compat.h"
32
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>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/capability.h>
41 #include <sys/types.h>
42 #include <sys/user.h>
43 #include <sys/file.h>
44 #include <sys/filedesc.h>
45 #include <sys/errno.h>
46 #include <sys/event.h>
47 #include <sys/poll.h>
48 #include <sys/proc.h>
49 #include <sys/selinfo.h>
50 #include <sys/sx.h>
51 #include <sys/syscallsubr.h>
52 #include <sys/timespec.h>
53
54 #ifdef COMPAT_LINUX32
55 #include <machine/../linux32/linux.h>
56 #include <machine/../linux32/linux32_proto.h>
57 #else
58 #include <machine/../linux/linux.h>
59 #include <machine/../linux/linux_proto.h>
60 #endif
61
62 #include <compat/linux/linux_emul.h>
63 #include <compat/linux/linux_event.h>
64 #include <compat/linux/linux_file.h>
65 #include <compat/linux/linux_util.h>
66
67 /*
68 * epoll defines 'struct epoll_event' with the field 'data' as 64 bits
69 * on all architectures. But on 32 bit architectures BSD 'struct kevent' only
70 * has 32 bit opaque pointer as 'udata' field. So we can't pass epoll supplied
71 * data verbatuim. Therefore we allocate 64-bit memory block to pass
72 * user supplied data for every file descriptor.
73 */
74
75 typedef uint64_t epoll_udata_t;
76
77 struct epoll_emuldata {
78 uint32_t fdc; /* epoll udata max index */
79 epoll_udata_t udata[1]; /* epoll user data vector */
80 };
81
82 #define EPOLL_DEF_SZ 16
83 #define EPOLL_SIZE(fdn) \
84 (sizeof(struct epoll_emuldata)+(fdn) * sizeof(epoll_udata_t))
85
86 struct epoll_event {
87 uint32_t events;
88 epoll_udata_t data;
89 }
90 #if defined(__amd64__)
91 __attribute__((packed))
92 #endif
93 ;
94
95 #define LINUX_MAX_EVENTS (INT_MAX / sizeof(struct epoll_event))
96
97 static void epoll_fd_install(struct thread *td, int fd, epoll_udata_t udata);
98 static int epoll_to_kevent(struct thread *td, struct file *epfp,
99 int fd, struct epoll_event *l_event, int *kev_flags,
100 struct kevent *kevent, int *nkevents);
101 static void kevent_to_epoll(struct kevent *kevent, struct epoll_event *l_event);
102 static int epoll_kev_copyout(void *arg, void *kevp, int count);
103 static int epoll_kev_copyin(void *arg, void *kevp, int count);
104 static int epoll_delete_event(struct thread *td, struct file *epfp,
105 int fd, int filter);
106 static int epoll_delete_all_events(struct thread *td, struct file *epfp,
107 int fd);
108
109 struct epoll_copyin_args {
110 struct kevent *changelist;
111 };
112
113 struct epoll_copyout_args {
114 struct epoll_event *leventlist;
115 struct proc *p;
116 uint32_t count;
117 int error;
118 };
119
120 /* eventfd */
121 typedef uint64_t eventfd_t;
122
123 static fo_rdwr_t eventfd_read;
124 static fo_rdwr_t eventfd_write;
125 static fo_truncate_t eventfd_truncate;
126 static fo_ioctl_t eventfd_ioctl;
127 static fo_poll_t eventfd_poll;
128 static fo_kqfilter_t eventfd_kqfilter;
129 static fo_stat_t eventfd_stat;
130 static fo_close_t eventfd_close;
131 static fo_fill_kinfo_t eventfd_fill_kinfo;
132
133 static struct fileops eventfdops = {
134 .fo_read = eventfd_read,
135 .fo_write = eventfd_write,
136 .fo_truncate = eventfd_truncate,
137 .fo_ioctl = eventfd_ioctl,
138 .fo_poll = eventfd_poll,
139 .fo_kqfilter = eventfd_kqfilter,
140 .fo_stat = eventfd_stat,
141 .fo_close = eventfd_close,
142 .fo_chmod = invfo_chmod,
143 .fo_chown = invfo_chown,
144 .fo_sendfile = invfo_sendfile,
145 .fo_fill_kinfo = eventfd_fill_kinfo,
146 .fo_flags = DFLAG_PASSABLE
147 };
148
149 static void filt_eventfddetach(struct knote *kn);
150 static int filt_eventfdread(struct knote *kn, long hint);
151 static int filt_eventfdwrite(struct knote *kn, long hint);
152
153 static struct filterops eventfd_rfiltops = {
154 .f_isfd = 1,
155 .f_detach = filt_eventfddetach,
156 .f_event = filt_eventfdread
157 };
158 static struct filterops eventfd_wfiltops = {
159 .f_isfd = 1,
160 .f_detach = filt_eventfddetach,
161 .f_event = filt_eventfdwrite
162 };
163
164 struct eventfd {
165 eventfd_t efd_count;
166 uint32_t efd_flags;
167 struct selinfo efd_sel;
168 struct mtx efd_lock;
169 };
170
171 static int eventfd_create(struct thread *td, uint32_t initval, int flags);
172
173
174 static void
epoll_fd_install(struct thread * td,int fd,epoll_udata_t udata)175 epoll_fd_install(struct thread *td, int fd, epoll_udata_t udata)
176 {
177 struct linux_pemuldata *pem;
178 struct epoll_emuldata *emd;
179 struct proc *p;
180
181 p = td->td_proc;
182
183 pem = pem_find(p);
184 KASSERT(pem != NULL, ("epoll proc emuldata not found.\n"));
185
186 LINUX_PEM_XLOCK(pem);
187 if (pem->epoll == NULL) {
188 emd = malloc(EPOLL_SIZE(fd), M_EPOLL, M_WAITOK);
189 emd->fdc = fd;
190 pem->epoll = emd;
191 } else {
192 emd = pem->epoll;
193 if (fd > emd->fdc) {
194 emd = realloc(emd, EPOLL_SIZE(fd), M_EPOLL, M_WAITOK);
195 emd->fdc = fd;
196 pem->epoll = emd;
197 }
198 }
199 emd->udata[fd] = udata;
200 LINUX_PEM_XUNLOCK(pem);
201 }
202
203 static int
epoll_create_common(struct thread * td,int flags)204 epoll_create_common(struct thread *td, int flags)
205 {
206 int error;
207
208 error = kern_kqueue(td, flags, NULL);
209 if (error)
210 return (error);
211
212 epoll_fd_install(td, EPOLL_DEF_SZ, 0);
213
214 return (0);
215 }
216
217 int
linux_epoll_create(struct thread * td,struct linux_epoll_create_args * args)218 linux_epoll_create(struct thread *td, struct linux_epoll_create_args *args)
219 {
220
221 /*
222 * args->size is unused. Linux just tests it
223 * and then forgets it as well.
224 */
225 if (args->size <= 0)
226 return (EINVAL);
227
228 return (epoll_create_common(td, 0));
229 }
230
231 int
linux_epoll_create1(struct thread * td,struct linux_epoll_create1_args * args)232 linux_epoll_create1(struct thread *td, struct linux_epoll_create1_args *args)
233 {
234 int flags;
235
236 if ((args->flags & ~(LINUX_O_CLOEXEC)) != 0)
237 return (EINVAL);
238
239 flags = 0;
240 if ((args->flags & LINUX_O_CLOEXEC) != 0)
241 flags |= O_CLOEXEC;
242
243 return (epoll_create_common(td, flags));
244 }
245
246 /* Structure converting function from epoll to kevent. */
247 static int
epoll_to_kevent(struct thread * td,struct file * epfp,int fd,struct epoll_event * l_event,int * kev_flags,struct kevent * kevent,int * nkevents)248 epoll_to_kevent(struct thread *td, struct file *epfp,
249 int fd, struct epoll_event *l_event, int *kev_flags,
250 struct kevent *kevent, int *nkevents)
251 {
252 uint32_t levents = l_event->events;
253 struct linux_pemuldata *pem;
254 struct proc *p;
255
256 /* flags related to how event is registered */
257 if ((levents & LINUX_EPOLLONESHOT) != 0)
258 *kev_flags |= EV_ONESHOT;
259 if ((levents & LINUX_EPOLLET) != 0)
260 *kev_flags |= EV_CLEAR;
261 if ((levents & LINUX_EPOLLERR) != 0)
262 *kev_flags |= EV_ERROR;
263 if ((levents & LINUX_EPOLLRDHUP) != 0)
264 *kev_flags |= EV_EOF;
265
266 /* flags related to what event is registered */
267 if ((levents & LINUX_EPOLL_EVRD) != 0) {
268 EV_SET(kevent++, fd, EVFILT_READ, *kev_flags, 0, 0, 0);
269 ++(*nkevents);
270 }
271 if ((levents & LINUX_EPOLL_EVWR) != 0) {
272 EV_SET(kevent++, fd, EVFILT_WRITE, *kev_flags, 0, 0, 0);
273 ++(*nkevents);
274 }
275
276 if ((levents & ~(LINUX_EPOLL_EVSUP)) != 0) {
277 p = td->td_proc;
278
279 pem = pem_find(p);
280 KASSERT(pem != NULL, ("epoll proc emuldata not found.\n"));
281 KASSERT(pem->epoll != NULL, ("epoll proc epolldata not found.\n"));
282
283 LINUX_PEM_XLOCK(pem);
284 if ((pem->flags & LINUX_XUNSUP_EPOLL) == 0) {
285 pem->flags |= LINUX_XUNSUP_EPOLL;
286 LINUX_PEM_XUNLOCK(pem);
287 linux_msg(td, "epoll_ctl unsupported flags: 0x%x\n",
288 levents);
289 } else
290 LINUX_PEM_XUNLOCK(pem);
291 return (EINVAL);
292 }
293
294 return (0);
295 }
296
297 /*
298 * Structure converting function from kevent to epoll. In a case
299 * this is called on error in registration we store the error in
300 * event->data and pick it up later in linux_epoll_ctl().
301 */
302 static void
kevent_to_epoll(struct kevent * kevent,struct epoll_event * l_event)303 kevent_to_epoll(struct kevent *kevent, struct epoll_event *l_event)
304 {
305
306 if ((kevent->flags & EV_ERROR) != 0) {
307 l_event->events = LINUX_EPOLLERR;
308 return;
309 }
310
311 switch (kevent->filter) {
312 case EVFILT_READ:
313 l_event->events = LINUX_EPOLLIN|LINUX_EPOLLRDNORM|LINUX_EPOLLPRI;
314 if ((kevent->flags & EV_EOF) != 0)
315 l_event->events |= LINUX_EPOLLRDHUP;
316 break;
317 case EVFILT_WRITE:
318 l_event->events = LINUX_EPOLLOUT|LINUX_EPOLLWRNORM;
319 break;
320 }
321 }
322
323 /*
324 * Copyout callback used by kevent. This converts kevent
325 * events to epoll events and copies them back to the
326 * userspace. This is also called on error on registering
327 * of the filter.
328 */
329 static int
epoll_kev_copyout(void * arg,void * _kevp,int count)330 epoll_kev_copyout(void *arg, void *_kevp, int count)
331 {
332 struct epoll_copyout_args *args;
333 struct linux_pemuldata *pem;
334 struct epoll_emuldata *emd;
335 struct epoll_event *eep;
336 struct kevent *kevp;
337 int error, fd, i;
338
339 kevp = _kevp;
340 args = (struct epoll_copyout_args*) arg;
341 eep = malloc(sizeof(*eep) * count, M_EPOLL, M_WAITOK | M_ZERO);
342
343 pem = pem_find(args->p);
344 KASSERT(pem != NULL, ("epoll proc emuldata not found.\n"));
345 LINUX_PEM_SLOCK(pem);
346 emd = pem->epoll;
347 KASSERT(emd != NULL, ("epoll proc epolldata not found.\n"));
348
349 for (i = 0; i < count; i++) {
350 kevent_to_epoll(&kevp[i], &eep[i]);
351
352 fd = kevp[i].ident;
353 KASSERT(fd <= emd->fdc, ("epoll user data vector"
354 " is too small.\n"));
355 eep[i].data = emd->udata[fd];
356 }
357 LINUX_PEM_SUNLOCK(pem);
358
359 error = copyout(eep, args->leventlist, count * sizeof(*eep));
360 if (error == 0) {
361 args->leventlist += count;
362 args->count += count;
363 } else if (args->error == 0)
364 args->error = error;
365
366 free(eep, M_EPOLL);
367 return (error);
368 }
369
370 /*
371 * Copyin callback used by kevent. This copies already
372 * converted filters from kernel memory to the kevent
373 * internal kernel memory. Hence the memcpy instead of
374 * copyin.
375 */
376 static int
epoll_kev_copyin(void * arg,void * _kevp,int count)377 epoll_kev_copyin(void *arg, void *_kevp, int count)
378 {
379 struct epoll_copyin_args *args;
380 struct kevent *kevp;
381
382 kevp = _kevp;
383 args = (struct epoll_copyin_args*) arg;
384
385 memcpy(kevp, args->changelist, count * sizeof(*kevp));
386 args->changelist += count;
387
388 return (0);
389 }
390
391 /*
392 * Load epoll filter, convert it to kevent filter
393 * and load it into kevent subsystem.
394 */
395 int
linux_epoll_ctl(struct thread * td,struct linux_epoll_ctl_args * args)396 linux_epoll_ctl(struct thread *td, struct linux_epoll_ctl_args *args)
397 {
398 struct file *epfp, *fp;
399 struct epoll_copyin_args ciargs;
400 struct kevent kev[2];
401 struct kevent_copyops k_ops = { &ciargs,
402 NULL,
403 epoll_kev_copyin};
404 struct epoll_event le;
405 cap_rights_t rights;
406 int kev_flags;
407 int nchanges = 0;
408 int error;
409
410 if (args->op != LINUX_EPOLL_CTL_DEL) {
411 error = copyin(args->event, &le, sizeof(le));
412 if (error != 0)
413 return (error);
414 }
415
416 error = fget(td, args->epfd,
417 cap_rights_init(&rights, CAP_KQUEUE_CHANGE), &epfp);
418 if (error != 0)
419 return (error);
420 if (epfp->f_type != DTYPE_KQUEUE)
421 goto leave1;
422
423 /* Protect user data vector from incorrectly supplied fd. */
424 error = fget(td, args->fd, cap_rights_init(&rights, CAP_POLL_EVENT), &fp);
425 if (error != 0)
426 goto leave1;
427
428 /* Linux disallows spying on himself */
429 if (epfp == fp) {
430 error = EINVAL;
431 goto leave0;
432 }
433
434 ciargs.changelist = kev;
435
436 switch (args->op) {
437 case LINUX_EPOLL_CTL_MOD:
438 /*
439 * We don't memorize which events were set for this FD
440 * on this level, so just delete all we could have set:
441 * EVFILT_READ and EVFILT_WRITE, ignoring any errors
442 */
443 error = epoll_delete_all_events(td, epfp, args->fd);
444 if (error)
445 goto leave0;
446 /* FALLTHROUGH */
447
448 case LINUX_EPOLL_CTL_ADD:
449 kev_flags = EV_ADD | EV_ENABLE;
450 break;
451
452 case LINUX_EPOLL_CTL_DEL:
453 /* CTL_DEL means unregister this fd with this epoll */
454 error = epoll_delete_all_events(td, epfp, args->fd);
455 goto leave0;
456
457 default:
458 error = EINVAL;
459 goto leave0;
460 }
461
462 error = epoll_to_kevent(td, epfp, args->fd, &le, &kev_flags,
463 kev, &nchanges);
464 if (error)
465 goto leave0;
466
467 epoll_fd_install(td, args->fd, le.data);
468
469 error = kern_kevent_fp(td, epfp, nchanges, 0, &k_ops, NULL);
470
471 leave0:
472 fdrop(fp, td);
473
474 leave1:
475 fdrop(epfp, td);
476 return (error);
477 }
478
479 /*
480 * Wait for a filter to be triggered on the epoll file descriptor.
481 */
482 static int
linux_epoll_wait_common(struct thread * td,int epfd,struct epoll_event * events,int maxevents,int timeout,sigset_t * uset)483 linux_epoll_wait_common(struct thread *td, int epfd, struct epoll_event *events,
484 int maxevents, int timeout, sigset_t *uset)
485 {
486 struct file *epfp;
487 struct timespec ts, *tsp;
488 cap_rights_t rights;
489 struct epoll_copyout_args coargs;
490 struct kevent_copyops k_ops = { &coargs,
491 epoll_kev_copyout,
492 NULL};
493 int error;
494
495 if (maxevents <= 0 || maxevents > LINUX_MAX_EVENTS)
496 return (EINVAL);
497
498 if (uset != NULL) {
499 error = kern_sigprocmask(td, SIG_SETMASK, uset,
500 &td->td_oldsigmask, 0);
501 if (error != 0)
502 return (error);
503 td->td_pflags |= TDP_OLDMASK;
504 /*
505 * Make sure that ast() is called on return to
506 * usermode and TDP_OLDMASK is cleared, restoring old
507 * sigmask.
508 */
509 thread_lock(td);
510 td->td_flags |= TDF_ASTPENDING;
511 thread_unlock(td);
512 }
513
514 error = fget(td, epfd,
515 cap_rights_init(&rights, CAP_KQUEUE_EVENT), &epfp);
516 if (error != 0)
517 return (error);
518
519 coargs.leventlist = events;
520 coargs.p = td->td_proc;
521 coargs.count = 0;
522 coargs.error = 0;
523
524 if (timeout != -1) {
525 if (timeout < 0) {
526 error = EINVAL;
527 goto leave;
528 }
529 /* Convert from milliseconds to timespec. */
530 ts.tv_sec = timeout / 1000;
531 ts.tv_nsec = (timeout % 1000) * 1000000;
532 tsp = &ts;
533 } else {
534 tsp = NULL;
535 }
536
537 error = kern_kevent_fp(td, epfp, 0, maxevents, &k_ops, tsp);
538 if (error == 0 && coargs.error != 0)
539 error = coargs.error;
540
541 /*
542 * kern_kevent might return ENOMEM which is not expected from epoll_wait.
543 * Maybe we should translate that but I don't think it matters at all.
544 */
545 if (error == 0)
546 td->td_retval[0] = coargs.count;
547 leave:
548 fdrop(epfp, td);
549 return (error);
550 }
551
552 int
linux_epoll_wait(struct thread * td,struct linux_epoll_wait_args * args)553 linux_epoll_wait(struct thread *td, struct linux_epoll_wait_args *args)
554 {
555
556 return (linux_epoll_wait_common(td, args->epfd, args->events,
557 args->maxevents, args->timeout, NULL));
558 }
559
560 int
linux_epoll_pwait(struct thread * td,struct linux_epoll_pwait_args * args)561 linux_epoll_pwait(struct thread *td, struct linux_epoll_pwait_args *args)
562 {
563 sigset_t mask, *pmask;
564 l_sigset_t lmask;
565 int error;
566
567 if (args->mask != NULL) {
568 error = copyin(args->mask, &lmask, sizeof(l_sigset_t));
569 if (error != 0)
570 return (error);
571 linux_to_bsd_sigset(&lmask, &mask);
572 pmask = &mask;
573 } else
574 pmask = NULL;
575 return (linux_epoll_wait_common(td, args->epfd, args->events,
576 args->maxevents, args->timeout, pmask));
577 }
578
579 static int
epoll_delete_event(struct thread * td,struct file * epfp,int fd,int filter)580 epoll_delete_event(struct thread *td, struct file *epfp, int fd, int filter)
581 {
582 struct epoll_copyin_args ciargs;
583 struct kevent kev;
584 struct kevent_copyops k_ops = { &ciargs,
585 NULL,
586 epoll_kev_copyin};
587 int error;
588
589 ciargs.changelist = &kev;
590 EV_SET(&kev, fd, filter, EV_DELETE | EV_DISABLE, 0, 0, 0);
591
592 error = kern_kevent_fp(td, epfp, 1, 0, &k_ops, NULL);
593
594 /*
595 * here we ignore ENONT, because we don't keep track of events here
596 */
597 if (error == ENOENT)
598 error = 0;
599 return (error);
600 }
601
602 static int
epoll_delete_all_events(struct thread * td,struct file * epfp,int fd)603 epoll_delete_all_events(struct thread *td, struct file *epfp, int fd)
604 {
605 int error1, error2;
606
607 error1 = epoll_delete_event(td, epfp, fd, EVFILT_READ);
608 error2 = epoll_delete_event(td, epfp, fd, EVFILT_WRITE);
609
610 /* report any errors we got */
611 return (error1 == 0 ? error2 : error1);
612 }
613
614 static int
eventfd_create(struct thread * td,uint32_t initval,int flags)615 eventfd_create(struct thread *td, uint32_t initval, int flags)
616 {
617 struct filedesc *fdp;
618 struct eventfd *efd;
619 struct file *fp;
620 int fflags, fd, error;
621
622 fflags = 0;
623 if ((flags & LINUX_O_CLOEXEC) != 0)
624 fflags |= O_CLOEXEC;
625
626 fdp = td->td_proc->p_fd;
627 error = falloc(td, &fp, &fd, fflags);
628 if (error)
629 return (error);
630
631 efd = malloc(sizeof(*efd), M_EPOLL, M_WAITOK | M_ZERO);
632 efd->efd_flags = flags;
633 efd->efd_count = initval;
634 mtx_init(&efd->efd_lock, "eventfd", NULL, MTX_DEF);
635
636 knlist_init_mtx(&efd->efd_sel.si_note, &efd->efd_lock);
637
638 fflags = FREAD | FWRITE;
639 if ((flags & LINUX_O_NONBLOCK) != 0)
640 fflags |= FNONBLOCK;
641
642 finit(fp, fflags, DTYPE_LINUXEFD, efd, &eventfdops);
643 fdrop(fp, td);
644
645 td->td_retval[0] = fd;
646 return (error);
647 }
648
649 int
linux_eventfd(struct thread * td,struct linux_eventfd_args * args)650 linux_eventfd(struct thread *td, struct linux_eventfd_args *args)
651 {
652
653 return (eventfd_create(td, args->initval, 0));
654 }
655
656 int
linux_eventfd2(struct thread * td,struct linux_eventfd2_args * args)657 linux_eventfd2(struct thread *td, struct linux_eventfd2_args *args)
658 {
659
660 if ((args->flags & ~(LINUX_O_CLOEXEC|LINUX_O_NONBLOCK|LINUX_EFD_SEMAPHORE)) != 0)
661 return (EINVAL);
662
663 return (eventfd_create(td, args->initval, args->flags));
664 }
665
666 static int
eventfd_close(struct file * fp,struct thread * td)667 eventfd_close(struct file *fp, struct thread *td)
668 {
669 struct eventfd *efd;
670
671 efd = fp->f_data;
672 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
673 return (EBADF);
674
675 seldrain(&efd->efd_sel);
676 knlist_destroy(&efd->efd_sel.si_note);
677
678 fp->f_ops = &badfileops;
679 mtx_destroy(&efd->efd_lock);
680 free(efd, M_EPOLL);
681
682 return (0);
683 }
684
685 static int
eventfd_read(struct file * fp,struct uio * uio,struct ucred * active_cred,int flags,struct thread * td)686 eventfd_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
687 int flags, struct thread *td)
688 {
689 struct eventfd *efd;
690 eventfd_t count;
691 int error;
692
693 efd = fp->f_data;
694 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
695 return (EBADF);
696
697 if (uio->uio_resid < sizeof(eventfd_t))
698 return (EINVAL);
699
700 error = 0;
701 mtx_lock(&efd->efd_lock);
702 retry:
703 if (efd->efd_count == 0) {
704 if ((efd->efd_flags & LINUX_O_NONBLOCK) != 0) {
705 mtx_unlock(&efd->efd_lock);
706 return (EAGAIN);
707 }
708 error = mtx_sleep(&efd->efd_count, &efd->efd_lock, PCATCH, "lefdrd", 0);
709 if (error == 0)
710 goto retry;
711 }
712 if (error == 0) {
713 if ((efd->efd_flags & LINUX_EFD_SEMAPHORE) != 0) {
714 count = 1;
715 --efd->efd_count;
716 } else {
717 count = efd->efd_count;
718 efd->efd_count = 0;
719 }
720 KNOTE_LOCKED(&efd->efd_sel.si_note, 0);
721 selwakeup(&efd->efd_sel);
722 wakeup(&efd->efd_count);
723 mtx_unlock(&efd->efd_lock);
724 error = uiomove(&count, sizeof(eventfd_t), uio);
725 } else
726 mtx_unlock(&efd->efd_lock);
727
728 return (error);
729 }
730
731 static int
eventfd_write(struct file * fp,struct uio * uio,struct ucred * active_cred,int flags,struct thread * td)732 eventfd_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
733 int flags, struct thread *td)
734 {
735 struct eventfd *efd;
736 eventfd_t count;
737 int error;
738
739 efd = fp->f_data;
740 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
741 return (EBADF);
742
743 if (uio->uio_resid < sizeof(eventfd_t))
744 return (EINVAL);
745
746 error = uiomove(&count, sizeof(eventfd_t), uio);
747 if (error)
748 return (error);
749 if (count == UINT64_MAX)
750 return (EINVAL);
751
752 mtx_lock(&efd->efd_lock);
753 retry:
754 if (UINT64_MAX - efd->efd_count <= count) {
755 if ((efd->efd_flags & LINUX_O_NONBLOCK) != 0) {
756 mtx_unlock(&efd->efd_lock);
757 return (EAGAIN);
758 }
759 error = mtx_sleep(&efd->efd_count, &efd->efd_lock,
760 PCATCH, "lefdwr", 0);
761 if (error == 0)
762 goto retry;
763 }
764 if (error == 0) {
765 efd->efd_count += count;
766 KNOTE_LOCKED(&efd->efd_sel.si_note, 0);
767 selwakeup(&efd->efd_sel);
768 wakeup(&efd->efd_count);
769 }
770 mtx_unlock(&efd->efd_lock);
771
772 return (error);
773 }
774
775 static int
eventfd_poll(struct file * fp,int events,struct ucred * active_cred,struct thread * td)776 eventfd_poll(struct file *fp, int events, struct ucred *active_cred,
777 struct thread *td)
778 {
779 struct eventfd *efd;
780 int revents = 0;
781
782 efd = fp->f_data;
783 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
784 return (POLLERR);
785
786 mtx_lock(&efd->efd_lock);
787 if ((events & (POLLIN|POLLRDNORM)) && efd->efd_count > 0)
788 revents |= events & (POLLIN|POLLRDNORM);
789 if ((events & (POLLOUT|POLLWRNORM)) && UINT64_MAX - 1 > efd->efd_count)
790 revents |= events & (POLLOUT|POLLWRNORM);
791 if (revents == 0)
792 selrecord(td, &efd->efd_sel);
793 mtx_unlock(&efd->efd_lock);
794
795 return (revents);
796 }
797
798 /*ARGSUSED*/
799 static int
eventfd_kqfilter(struct file * fp,struct knote * kn)800 eventfd_kqfilter(struct file *fp, struct knote *kn)
801 {
802 struct eventfd *efd;
803
804 efd = fp->f_data;
805 if (fp->f_type != DTYPE_LINUXEFD || efd == NULL)
806 return (EINVAL);
807
808 mtx_lock(&efd->efd_lock);
809 switch (kn->kn_filter) {
810 case EVFILT_READ:
811 kn->kn_fop = &eventfd_rfiltops;
812 break;
813 case EVFILT_WRITE:
814 kn->kn_fop = &eventfd_wfiltops;
815 break;
816 default:
817 mtx_unlock(&efd->efd_lock);
818 return (EINVAL);
819 }
820
821 kn->kn_hook = efd;
822 knlist_add(&efd->efd_sel.si_note, kn, 1);
823 mtx_unlock(&efd->efd_lock);
824
825 return (0);
826 }
827
828 static void
filt_eventfddetach(struct knote * kn)829 filt_eventfddetach(struct knote *kn)
830 {
831 struct eventfd *efd = kn->kn_hook;
832
833 mtx_lock(&efd->efd_lock);
834 knlist_remove(&efd->efd_sel.si_note, kn, 1);
835 mtx_unlock(&efd->efd_lock);
836 }
837
838 /*ARGSUSED*/
839 static int
filt_eventfdread(struct knote * kn,long hint)840 filt_eventfdread(struct knote *kn, long hint)
841 {
842 struct eventfd *efd = kn->kn_hook;
843 int ret;
844
845 mtx_assert(&efd->efd_lock, MA_OWNED);
846 ret = (efd->efd_count > 0);
847
848 return (ret);
849 }
850
851 /*ARGSUSED*/
852 static int
filt_eventfdwrite(struct knote * kn,long hint)853 filt_eventfdwrite(struct knote *kn, long hint)
854 {
855 struct eventfd *efd = kn->kn_hook;
856 int ret;
857
858 mtx_assert(&efd->efd_lock, MA_OWNED);
859 ret = (UINT64_MAX - 1 > efd->efd_count);
860
861 return (ret);
862 }
863
864 /*ARGSUSED*/
865 static int
eventfd_truncate(struct file * fp,off_t length,struct ucred * active_cred,struct thread * td)866 eventfd_truncate(struct file *fp, off_t length, struct ucred *active_cred,
867 struct thread *td)
868 {
869
870 return (ENXIO);
871 }
872
873 /*ARGSUSED*/
874 static int
eventfd_ioctl(struct file * fp,u_long cmd,void * data,struct ucred * active_cred,struct thread * td)875 eventfd_ioctl(struct file *fp, u_long cmd, void *data,
876 struct ucred *active_cred, struct thread *td)
877 {
878
879 return (ENXIO);
880 }
881
882 /*ARGSUSED*/
883 static int
eventfd_stat(struct file * fp,struct stat * st,struct ucred * active_cred,struct thread * td)884 eventfd_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
885 struct thread *td)
886 {
887
888 return (ENXIO);
889 }
890
891 /*ARGSUSED*/
892 static int
eventfd_fill_kinfo(struct file * fp,struct kinfo_file * kif,struct filedesc * fdp)893 eventfd_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
894 {
895
896 kif->kf_type = KF_TYPE_UNKNOWN;
897 return (0);
898 }
899