cyapa - Introduce better three-finger button emulation
[dragonfly.git] / sys / kern / sys_generic.c
blob1b9425417ba48a8f588466347346af56b88810f2
1 /*
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 * @(#)sys_generic.c 8.5 (Berkeley) 1/21/94
35 * $FreeBSD: src/sys/kern/sys_generic.c,v 1.55.2.10 2001/03/17 10:39:32 peter Exp $
38 #include "opt_ktrace.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sysproto.h>
43 #include <sys/event.h>
44 #include <sys/filedesc.h>
45 #include <sys/filio.h>
46 #include <sys/fcntl.h>
47 #include <sys/file.h>
48 #include <sys/proc.h>
49 #include <sys/signalvar.h>
50 #include <sys/socketvar.h>
51 #include <sys/uio.h>
52 #include <sys/kernel.h>
53 #include <sys/kern_syscall.h>
54 #include <sys/malloc.h>
55 #include <sys/mapped_ioctl.h>
56 #include <sys/poll.h>
57 #include <sys/queue.h>
58 #include <sys/resourcevar.h>
59 #include <sys/socketops.h>
60 #include <sys/sysctl.h>
61 #include <sys/sysent.h>
62 #include <sys/buf.h>
63 #ifdef KTRACE
64 #include <sys/ktrace.h>
65 #endif
66 #include <vm/vm.h>
67 #include <vm/vm_page.h>
69 #include <sys/file2.h>
70 #include <sys/mplock2.h>
71 #include <sys/spinlock2.h>
73 #include <machine/limits.h>
75 static MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
76 static MALLOC_DEFINE(M_IOCTLMAP, "ioctlmap", "mapped ioctl handler buffer");
77 static MALLOC_DEFINE(M_SELECT, "select", "select() buffer");
78 MALLOC_DEFINE(M_IOV, "iov", "large iov's");
80 typedef struct kfd_set {
81 fd_mask fds_bits[2];
82 } kfd_set;
84 enum select_copyin_states {
85 COPYIN_READ, COPYIN_WRITE, COPYIN_EXCEPT, COPYIN_DONE };
87 struct select_kevent_copyin_args {
88 kfd_set *read_set;
89 kfd_set *write_set;
90 kfd_set *except_set;
91 int active_set; /* One of select_copyin_states */
92 struct lwp *lwp; /* Pointer to our lwp */
93 int num_fds; /* Number of file descriptors (syscall arg) */
94 int proc_fds; /* Processed fd's (wraps) */
95 int error; /* Returned to userland */
98 struct poll_kevent_copyin_args {
99 struct lwp *lwp;
100 struct pollfd *fds;
101 int nfds;
102 int pfds;
103 int error;
106 static struct lwkt_token mioctl_token = LWKT_TOKEN_INITIALIZER(mioctl_token);
108 static int doselect(int nd, fd_set *in, fd_set *ou, fd_set *ex,
109 struct timespec *ts, int *res);
110 static int dopoll(int nfds, struct pollfd *fds, struct timespec *ts,
111 int *res);
112 static int dofileread(int, struct file *, struct uio *, int, size_t *);
113 static int dofilewrite(int, struct file *, struct uio *, int, size_t *);
116 * Read system call.
118 * MPSAFE
121 sys_read(struct read_args *uap)
123 struct thread *td = curthread;
124 struct uio auio;
125 struct iovec aiov;
126 int error;
128 if ((ssize_t)uap->nbyte < 0)
129 error = EINVAL;
131 aiov.iov_base = uap->buf;
132 aiov.iov_len = uap->nbyte;
133 auio.uio_iov = &aiov;
134 auio.uio_iovcnt = 1;
135 auio.uio_offset = -1;
136 auio.uio_resid = uap->nbyte;
137 auio.uio_rw = UIO_READ;
138 auio.uio_segflg = UIO_USERSPACE;
139 auio.uio_td = td;
141 error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_szresult);
142 return(error);
146 * Positioned (Pread) read system call
148 * MPSAFE
151 sys_extpread(struct extpread_args *uap)
153 struct thread *td = curthread;
154 struct uio auio;
155 struct iovec aiov;
156 int error;
157 int flags;
159 if ((ssize_t)uap->nbyte < 0)
160 return(EINVAL);
162 aiov.iov_base = uap->buf;
163 aiov.iov_len = uap->nbyte;
164 auio.uio_iov = &aiov;
165 auio.uio_iovcnt = 1;
166 auio.uio_offset = uap->offset;
167 auio.uio_resid = uap->nbyte;
168 auio.uio_rw = UIO_READ;
169 auio.uio_segflg = UIO_USERSPACE;
170 auio.uio_td = td;
172 flags = uap->flags & O_FMASK;
173 if (uap->offset != (off_t)-1)
174 flags |= O_FOFFSET;
176 error = kern_preadv(uap->fd, &auio, flags, &uap->sysmsg_szresult);
177 return(error);
181 * Scatter read system call.
183 * MPSAFE
186 sys_readv(struct readv_args *uap)
188 struct thread *td = curthread;
189 struct uio auio;
190 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
191 int error;
193 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
194 &auio.uio_resid);
195 if (error)
196 return (error);
197 auio.uio_iov = iov;
198 auio.uio_iovcnt = uap->iovcnt;
199 auio.uio_offset = -1;
200 auio.uio_rw = UIO_READ;
201 auio.uio_segflg = UIO_USERSPACE;
202 auio.uio_td = td;
204 error = kern_preadv(uap->fd, &auio, 0, &uap->sysmsg_szresult);
206 iovec_free(&iov, aiov);
207 return (error);
212 * Scatter positioned read system call.
214 * MPSAFE
217 sys_extpreadv(struct extpreadv_args *uap)
219 struct thread *td = curthread;
220 struct uio auio;
221 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
222 int error;
223 int flags;
225 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
226 &auio.uio_resid);
227 if (error)
228 return (error);
229 auio.uio_iov = iov;
230 auio.uio_iovcnt = uap->iovcnt;
231 auio.uio_offset = uap->offset;
232 auio.uio_rw = UIO_READ;
233 auio.uio_segflg = UIO_USERSPACE;
234 auio.uio_td = td;
236 flags = uap->flags & O_FMASK;
237 if (uap->offset != (off_t)-1)
238 flags |= O_FOFFSET;
240 error = kern_preadv(uap->fd, &auio, flags, &uap->sysmsg_szresult);
242 iovec_free(&iov, aiov);
243 return(error);
247 * MPSAFE
250 kern_preadv(int fd, struct uio *auio, int flags, size_t *res)
252 struct thread *td = curthread;
253 struct proc *p = td->td_proc;
254 struct file *fp;
255 int error;
257 KKASSERT(p);
259 fp = holdfp(p->p_fd, fd, FREAD);
260 if (fp == NULL)
261 return (EBADF);
262 if (flags & O_FOFFSET && fp->f_type != DTYPE_VNODE) {
263 error = ESPIPE;
264 } else {
265 error = dofileread(fd, fp, auio, flags, res);
267 fdrop(fp);
268 return(error);
272 * Common code for readv and preadv that reads data in
273 * from a file using the passed in uio, offset, and flags.
275 * MPALMOSTSAFE - ktrace needs help
277 static int
278 dofileread(int fd, struct file *fp, struct uio *auio, int flags, size_t *res)
280 int error;
281 size_t len;
282 #ifdef KTRACE
283 struct thread *td = curthread;
284 struct iovec *ktriov = NULL;
285 struct uio ktruio;
286 #endif
288 #ifdef KTRACE
290 * if tracing, save a copy of iovec
292 if (KTRPOINT(td, KTR_GENIO)) {
293 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
295 ktriov = kmalloc(iovlen, M_TEMP, M_WAITOK);
296 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
297 ktruio = *auio;
299 #endif
300 len = auio->uio_resid;
301 error = fo_read(fp, auio, fp->f_cred, flags);
302 if (error) {
303 if (auio->uio_resid != len && (error == ERESTART ||
304 error == EINTR || error == EWOULDBLOCK))
305 error = 0;
307 #ifdef KTRACE
308 if (ktriov != NULL) {
309 if (error == 0) {
310 ktruio.uio_iov = ktriov;
311 ktruio.uio_resid = len - auio->uio_resid;
312 get_mplock();
313 ktrgenio(td->td_lwp, fd, UIO_READ, &ktruio, error);
314 rel_mplock();
316 kfree(ktriov, M_TEMP);
318 #endif
319 if (error == 0)
320 *res = len - auio->uio_resid;
322 return(error);
326 * Write system call
328 * MPSAFE
331 sys_write(struct write_args *uap)
333 struct thread *td = curthread;
334 struct uio auio;
335 struct iovec aiov;
336 int error;
338 if ((ssize_t)uap->nbyte < 0)
339 error = EINVAL;
341 aiov.iov_base = (void *)(uintptr_t)uap->buf;
342 aiov.iov_len = uap->nbyte;
343 auio.uio_iov = &aiov;
344 auio.uio_iovcnt = 1;
345 auio.uio_offset = -1;
346 auio.uio_resid = uap->nbyte;
347 auio.uio_rw = UIO_WRITE;
348 auio.uio_segflg = UIO_USERSPACE;
349 auio.uio_td = td;
351 error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_szresult);
353 return(error);
357 * Pwrite system call
359 * MPSAFE
362 sys_extpwrite(struct extpwrite_args *uap)
364 struct thread *td = curthread;
365 struct uio auio;
366 struct iovec aiov;
367 int error;
368 int flags;
370 if ((ssize_t)uap->nbyte < 0)
371 error = EINVAL;
373 aiov.iov_base = (void *)(uintptr_t)uap->buf;
374 aiov.iov_len = uap->nbyte;
375 auio.uio_iov = &aiov;
376 auio.uio_iovcnt = 1;
377 auio.uio_offset = uap->offset;
378 auio.uio_resid = uap->nbyte;
379 auio.uio_rw = UIO_WRITE;
380 auio.uio_segflg = UIO_USERSPACE;
381 auio.uio_td = td;
383 flags = uap->flags & O_FMASK;
384 if (uap->offset != (off_t)-1)
385 flags |= O_FOFFSET;
386 error = kern_pwritev(uap->fd, &auio, flags, &uap->sysmsg_szresult);
387 return(error);
391 * MPSAFE
394 sys_writev(struct writev_args *uap)
396 struct thread *td = curthread;
397 struct uio auio;
398 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
399 int error;
401 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
402 &auio.uio_resid);
403 if (error)
404 return (error);
405 auio.uio_iov = iov;
406 auio.uio_iovcnt = uap->iovcnt;
407 auio.uio_offset = -1;
408 auio.uio_rw = UIO_WRITE;
409 auio.uio_segflg = UIO_USERSPACE;
410 auio.uio_td = td;
412 error = kern_pwritev(uap->fd, &auio, 0, &uap->sysmsg_szresult);
414 iovec_free(&iov, aiov);
415 return (error);
420 * Gather positioned write system call
422 * MPSAFE
425 sys_extpwritev(struct extpwritev_args *uap)
427 struct thread *td = curthread;
428 struct uio auio;
429 struct iovec aiov[UIO_SMALLIOV], *iov = NULL;
430 int error;
431 int flags;
433 error = iovec_copyin(uap->iovp, &iov, aiov, uap->iovcnt,
434 &auio.uio_resid);
435 if (error)
436 return (error);
437 auio.uio_iov = iov;
438 auio.uio_iovcnt = uap->iovcnt;
439 auio.uio_offset = uap->offset;
440 auio.uio_rw = UIO_WRITE;
441 auio.uio_segflg = UIO_USERSPACE;
442 auio.uio_td = td;
444 flags = uap->flags & O_FMASK;
445 if (uap->offset != (off_t)-1)
446 flags |= O_FOFFSET;
448 error = kern_pwritev(uap->fd, &auio, flags, &uap->sysmsg_szresult);
450 iovec_free(&iov, aiov);
451 return(error);
455 * MPSAFE
458 kern_pwritev(int fd, struct uio *auio, int flags, size_t *res)
460 struct thread *td = curthread;
461 struct proc *p = td->td_proc;
462 struct file *fp;
463 int error;
465 KKASSERT(p);
467 fp = holdfp(p->p_fd, fd, FWRITE);
468 if (fp == NULL)
469 return (EBADF);
470 else if ((flags & O_FOFFSET) && fp->f_type != DTYPE_VNODE) {
471 error = ESPIPE;
472 } else {
473 error = dofilewrite(fd, fp, auio, flags, res);
476 fdrop(fp);
477 return (error);
481 * Common code for writev and pwritev that writes data to
482 * a file using the passed in uio, offset, and flags.
484 * MPALMOSTSAFE - ktrace needs help
486 static int
487 dofilewrite(int fd, struct file *fp, struct uio *auio, int flags, size_t *res)
489 struct thread *td = curthread;
490 struct lwp *lp = td->td_lwp;
491 int error;
492 size_t len;
493 #ifdef KTRACE
494 struct iovec *ktriov = NULL;
495 struct uio ktruio;
496 #endif
498 #ifdef KTRACE
500 * if tracing, save a copy of iovec and uio
502 if (KTRPOINT(td, KTR_GENIO)) {
503 int iovlen = auio->uio_iovcnt * sizeof(struct iovec);
505 ktriov = kmalloc(iovlen, M_TEMP, M_WAITOK);
506 bcopy((caddr_t)auio->uio_iov, (caddr_t)ktriov, iovlen);
507 ktruio = *auio;
509 #endif
510 len = auio->uio_resid;
511 error = fo_write(fp, auio, fp->f_cred, flags);
512 if (error) {
513 if (auio->uio_resid != len && (error == ERESTART ||
514 error == EINTR || error == EWOULDBLOCK))
515 error = 0;
516 /* Socket layer is responsible for issuing SIGPIPE. */
517 if (error == EPIPE && fp->f_type != DTYPE_SOCKET)
518 lwpsignal(lp->lwp_proc, lp, SIGPIPE);
520 #ifdef KTRACE
521 if (ktriov != NULL) {
522 if (error == 0) {
523 ktruio.uio_iov = ktriov;
524 ktruio.uio_resid = len - auio->uio_resid;
525 get_mplock();
526 ktrgenio(lp, fd, UIO_WRITE, &ktruio, error);
527 rel_mplock();
529 kfree(ktriov, M_TEMP);
531 #endif
532 if (error == 0)
533 *res = len - auio->uio_resid;
535 return(error);
539 * Ioctl system call
541 * MPSAFE
544 sys_ioctl(struct ioctl_args *uap)
546 int error;
548 error = mapped_ioctl(uap->fd, uap->com, uap->data, NULL, &uap->sysmsg);
549 return (error);
552 struct ioctl_map_entry {
553 const char *subsys;
554 struct ioctl_map_range *cmd_ranges;
555 LIST_ENTRY(ioctl_map_entry) entries;
559 * The true heart of all ioctl syscall handlers (native, emulation).
560 * If map != NULL, it will be searched for a matching entry for com,
561 * and appropriate conversions/conversion functions will be utilized.
563 * MPSAFE
566 mapped_ioctl(int fd, u_long com, caddr_t uspc_data, struct ioctl_map *map,
567 struct sysmsg *msg)
569 struct thread *td = curthread;
570 struct proc *p = td->td_proc;
571 struct ucred *cred;
572 struct file *fp;
573 struct ioctl_map_range *iomc = NULL;
574 int error;
575 u_int size;
576 u_long ocom = com;
577 caddr_t data, memp;
578 int tmp;
579 #define STK_PARAMS 128
580 union {
581 char stkbuf[STK_PARAMS];
582 long align;
583 } ubuf;
585 KKASSERT(p);
586 cred = td->td_ucred;
588 fp = holdfp(p->p_fd, fd, FREAD|FWRITE);
589 if (fp == NULL)
590 return(EBADF);
592 if (map != NULL) { /* obey translation map */
593 u_long maskcmd;
594 struct ioctl_map_entry *e;
596 maskcmd = com & map->mask;
598 lwkt_gettoken(&mioctl_token);
599 LIST_FOREACH(e, &map->mapping, entries) {
600 for (iomc = e->cmd_ranges; iomc->start != 0 ||
601 iomc->maptocmd != 0 || iomc->wrapfunc != NULL ||
602 iomc->mapfunc != NULL;
603 iomc++) {
604 if (maskcmd >= iomc->start &&
605 maskcmd <= iomc->end)
606 break;
609 /* Did we find a match? */
610 if (iomc->start != 0 || iomc->maptocmd != 0 ||
611 iomc->wrapfunc != NULL || iomc->mapfunc != NULL)
612 break;
614 lwkt_reltoken(&mioctl_token);
616 if (iomc == NULL ||
617 (iomc->start == 0 && iomc->maptocmd == 0
618 && iomc->wrapfunc == NULL && iomc->mapfunc == NULL)) {
619 kprintf("%s: 'ioctl' fd=%d, cmd=0x%lx ('%c',%d) not implemented\n",
620 map->sys, fd, maskcmd,
621 (int)((maskcmd >> 8) & 0xff),
622 (int)(maskcmd & 0xff));
623 error = EINVAL;
624 goto done;
628 * If it's a non-range one to one mapping, maptocmd should be
629 * correct. If it's a ranged one to one mapping, we pass the
630 * original value of com, and for a range mapped to a different
631 * range, we always need a mapping function to translate the
632 * ioctl to our native ioctl. Ex. 6500-65ff <-> 9500-95ff
634 if (iomc->start == iomc->end && iomc->maptocmd == iomc->maptoend) {
635 com = iomc->maptocmd;
636 } else if (iomc->start == iomc->maptocmd && iomc->end == iomc->maptoend) {
637 if (iomc->mapfunc != NULL)
638 com = iomc->mapfunc(iomc->start, iomc->end,
639 iomc->start, iomc->end,
640 com, com);
641 } else {
642 if (iomc->mapfunc != NULL) {
643 com = iomc->mapfunc(iomc->start, iomc->end,
644 iomc->maptocmd, iomc->maptoend,
645 com, ocom);
646 } else {
647 kprintf("%s: Invalid mapping for fd=%d, cmd=%#lx ('%c',%d)\n",
648 map->sys, fd, maskcmd,
649 (int)((maskcmd >> 8) & 0xff),
650 (int)(maskcmd & 0xff));
651 error = EINVAL;
652 goto done;
657 switch (com) {
658 case FIONCLEX:
659 error = fclrfdflags(p->p_fd, fd, UF_EXCLOSE);
660 goto done;
661 case FIOCLEX:
662 error = fsetfdflags(p->p_fd, fd, UF_EXCLOSE);
663 goto done;
667 * Interpret high order word to find amount of data to be
668 * copied to/from the user's address space.
670 size = IOCPARM_LEN(com);
671 if (size > IOCPARM_MAX) {
672 error = ENOTTY;
673 goto done;
676 if ((com & IOC_VOID) == 0 && size > sizeof(ubuf.stkbuf)) {
677 memp = kmalloc(size, M_IOCTLOPS, M_WAITOK);
678 data = memp;
679 } else {
680 memp = NULL;
681 data = ubuf.stkbuf;
683 if (com & IOC_VOID) {
684 *(caddr_t *)data = uspc_data;
685 } else if (com & IOC_IN) {
686 if (size != 0) {
687 error = copyin(uspc_data, data, (size_t)size);
688 if (error)
689 goto done;
690 } else {
691 *(caddr_t *)data = uspc_data;
693 } else if ((com & IOC_OUT) != 0 && size) {
695 * Zero the buffer so the user always
696 * gets back something deterministic.
698 bzero(data, (size_t)size);
701 switch (com) {
702 case FIONBIO:
703 if ((tmp = *(int *)data))
704 atomic_set_int(&fp->f_flag, FNONBLOCK);
705 else
706 atomic_clear_int(&fp->f_flag, FNONBLOCK);
707 error = 0;
708 break;
710 case FIOASYNC:
711 if ((tmp = *(int *)data))
712 atomic_set_int(&fp->f_flag, FASYNC);
713 else
714 atomic_clear_int(&fp->f_flag, FASYNC);
715 error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, cred, msg);
716 break;
718 default:
720 * If there is a override function,
721 * call it instead of directly routing the call
723 if (map != NULL && iomc->wrapfunc != NULL)
724 error = iomc->wrapfunc(fp, com, ocom, data, cred);
725 else
726 error = fo_ioctl(fp, com, data, cred, msg);
728 * Copy any data to user, size was
729 * already set and checked above.
731 if (error == 0 && (com & IOC_OUT) != 0 && size != 0)
732 error = copyout(data, uspc_data, (size_t)size);
733 break;
735 done:
736 if (memp != NULL)
737 kfree(memp, M_IOCTLOPS);
738 fdrop(fp);
739 return(error);
743 * MPSAFE
746 mapped_ioctl_register_handler(struct ioctl_map_handler *he)
748 struct ioctl_map_entry *ne;
750 KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL &&
751 he->subsys != NULL && *he->subsys != '\0');
753 ne = kmalloc(sizeof(struct ioctl_map_entry), M_IOCTLMAP,
754 M_WAITOK | M_ZERO);
756 ne->subsys = he->subsys;
757 ne->cmd_ranges = he->cmd_ranges;
759 lwkt_gettoken(&mioctl_token);
760 LIST_INSERT_HEAD(&he->map->mapping, ne, entries);
761 lwkt_reltoken(&mioctl_token);
763 return(0);
767 * MPSAFE
770 mapped_ioctl_unregister_handler(struct ioctl_map_handler *he)
772 struct ioctl_map_entry *ne;
773 int error = EINVAL;
775 KKASSERT(he != NULL && he->map != NULL && he->cmd_ranges != NULL);
777 lwkt_gettoken(&mioctl_token);
778 LIST_FOREACH(ne, &he->map->mapping, entries) {
779 if (ne->cmd_ranges == he->cmd_ranges) {
780 LIST_REMOVE(ne, entries);
781 kfree(ne, M_IOCTLMAP);
782 error = 0;
783 break;
786 lwkt_reltoken(&mioctl_token);
787 return(error);
790 static int nselcoll; /* Select collisions since boot */
791 int selwait;
792 SYSCTL_INT(_kern, OID_AUTO, nselcoll, CTLFLAG_RD, &nselcoll, 0, "");
793 static int nseldebug;
794 SYSCTL_INT(_kern, OID_AUTO, nseldebug, CTLFLAG_RW, &nseldebug, 0, "");
797 * Select system call.
799 * MPSAFE
802 sys_select(struct select_args *uap)
804 struct timeval ktv;
805 struct timespec *ktsp, kts;
806 int error;
809 * Get timeout if any.
811 if (uap->tv != NULL) {
812 error = copyin(uap->tv, &ktv, sizeof (ktv));
813 if (error)
814 return (error);
815 TIMEVAL_TO_TIMESPEC(&ktv, &kts);
816 ktsp = &kts;
817 } else {
818 ktsp = NULL;
822 * Do real work.
824 error = doselect(uap->nd, uap->in, uap->ou, uap->ex, ktsp,
825 &uap->sysmsg_result);
827 return (error);
832 * Pselect system call.
835 sys_pselect(struct pselect_args *uap)
837 struct thread *td = curthread;
838 struct lwp *lp = td->td_lwp;
839 struct timespec *ktsp, kts;
840 sigset_t sigmask;
841 int error;
844 * Get timeout if any.
846 if (uap->ts != NULL) {
847 error = copyin(uap->ts, &kts, sizeof (kts));
848 if (error)
849 return (error);
850 ktsp = &kts;
851 } else {
852 ktsp = NULL;
856 * Install temporary signal mask if any provided.
858 if (uap->sigmask != NULL) {
859 error = copyin(uap->sigmask, &sigmask, sizeof(sigmask));
860 if (error)
861 return (error);
862 lwkt_gettoken(&lp->lwp_proc->p_token);
863 lp->lwp_oldsigmask = lp->lwp_sigmask;
864 SIG_CANTMASK(sigmask);
865 lp->lwp_sigmask = sigmask;
866 lwkt_reltoken(&lp->lwp_proc->p_token);
870 * Do real job.
872 error = doselect(uap->nd, uap->in, uap->ou, uap->ex, ktsp,
873 &uap->sysmsg_result);
875 if (uap->sigmask != NULL) {
876 lwkt_gettoken(&lp->lwp_proc->p_token);
877 /* doselect() responsible for turning ERESTART into EINTR */
878 KKASSERT(error != ERESTART);
879 if (error == EINTR) {
881 * We can't restore the previous signal mask now
882 * because it could block the signal that interrupted
883 * us. So make a note to restore it after executing
884 * the handler.
886 lp->lwp_flags |= LWP_OLDMASK;
887 } else {
889 * No handler to run. Restore previous mask immediately.
891 lp->lwp_sigmask = lp->lwp_oldsigmask;
893 lwkt_reltoken(&lp->lwp_proc->p_token);
896 return (error);
899 static int
900 select_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
902 struct select_kevent_copyin_args *skap = NULL;
903 struct kevent *kev;
904 int fd;
905 kfd_set *fdp = NULL;
906 short filter = 0;
907 u_int fflags = 0;
909 skap = (struct select_kevent_copyin_args *)arg;
911 if (*events == maxevents)
912 return (0);
914 while (skap->active_set < COPYIN_DONE) {
915 switch (skap->active_set) {
916 case COPYIN_READ:
918 * Register descriptors for the read filter
920 fdp = skap->read_set;
921 filter = EVFILT_READ;
922 fflags = NOTE_OLDAPI;
923 if (fdp)
924 break;
925 ++skap->active_set;
926 skap->proc_fds = 0;
927 /* fall through */
928 case COPYIN_WRITE:
930 * Register descriptors for the write filter
932 fdp = skap->write_set;
933 filter = EVFILT_WRITE;
934 fflags = NOTE_OLDAPI;
935 if (fdp)
936 break;
937 ++skap->active_set;
938 skap->proc_fds = 0;
939 /* fall through */
940 case COPYIN_EXCEPT:
942 * Register descriptors for the exception filter
944 fdp = skap->except_set;
945 filter = EVFILT_EXCEPT;
946 fflags = NOTE_OLDAPI | NOTE_OOB;
947 if (fdp)
948 break;
949 ++skap->active_set;
950 skap->proc_fds = 0;
951 /* fall through */
952 case COPYIN_DONE:
954 * Nothing left to register
956 return(0);
957 /* NOT REACHED */
960 while (skap->proc_fds < skap->num_fds) {
961 fd = skap->proc_fds;
962 if (FD_ISSET(fd, fdp)) {
963 kev = &kevp[*events];
964 EV_SET(kev, fd, filter,
965 EV_ADD|EV_ENABLE,
966 fflags, 0,
967 (void *)(uintptr_t)
968 skap->lwp->lwp_kqueue_serial);
969 FD_CLR(fd, fdp);
970 ++*events;
972 if (nseldebug)
973 kprintf("select fd %d filter %d serial %d\n",
974 fd, filter, skap->lwp->lwp_kqueue_serial);
976 ++skap->proc_fds;
977 if (*events == maxevents)
978 return (0);
980 skap->active_set++;
981 skap->proc_fds = 0;
984 return (0);
987 static int
988 select_copyout(void *arg, struct kevent *kevp, int count, int *res)
990 struct select_kevent_copyin_args *skap;
991 struct kevent kev;
992 int i = 0;
994 skap = (struct select_kevent_copyin_args *)arg;
996 for (i = 0; i < count; ++i) {
998 * Filter out and delete spurious events
1000 if ((u_int)(uintptr_t)kevp[i].udata !=
1001 skap->lwp->lwp_kqueue_serial) {
1002 kev = kevp[i];
1003 kev.flags = EV_DISABLE|EV_DELETE;
1004 kqueue_register(&skap->lwp->lwp_kqueue, &kev);
1005 if (nseldebug)
1006 kprintf("select fd %ju mismatched serial %d\n",
1007 (uintmax_t)kevp[i].ident,
1008 skap->lwp->lwp_kqueue_serial);
1009 continue;
1013 * Handle errors
1015 if (kevp[i].flags & EV_ERROR) {
1016 int error = kevp[i].data;
1018 switch (error) {
1019 case EBADF:
1021 * A bad file descriptor is considered a
1022 * fatal error for select, bail out.
1024 skap->error = error;
1025 *res = -1;
1026 return error;
1028 default:
1030 * Select silently swallows any unknown errors
1031 * for descriptors in the read or write sets.
1033 * ALWAYS filter out EOPNOTSUPP errors from
1034 * filters (at least until all filters support
1035 * EVFILT_EXCEPT)
1037 * We also filter out ENODEV since dev_dkqfilter
1038 * returns ENODEV if EOPNOTSUPP is returned in an
1039 * inner call.
1041 * XXX: fix this
1043 if (kevp[i].filter != EVFILT_READ &&
1044 kevp[i].filter != EVFILT_WRITE &&
1045 error != EOPNOTSUPP &&
1046 error != ENODEV) {
1047 skap->error = error;
1048 *res = -1;
1049 return error;
1051 break;
1053 if (nseldebug)
1054 kprintf("select fd %ju filter %d error %d\n",
1055 (uintmax_t)kevp[i].ident,
1056 kevp[i].filter, error);
1057 continue;
1060 switch (kevp[i].filter) {
1061 case EVFILT_READ:
1062 FD_SET(kevp[i].ident, skap->read_set);
1063 break;
1064 case EVFILT_WRITE:
1065 FD_SET(kevp[i].ident, skap->write_set);
1066 break;
1067 case EVFILT_EXCEPT:
1068 FD_SET(kevp[i].ident, skap->except_set);
1069 break;
1072 ++*res;
1075 return (0);
1079 * Copy select bits in from userland. Allocate kernel memory if the
1080 * set is large.
1082 static int
1083 getbits(int bytes, fd_set *in_set, kfd_set **out_set, kfd_set *tmp_set)
1085 int error;
1087 if (in_set) {
1088 if (bytes < sizeof(*tmp_set))
1089 *out_set = tmp_set;
1090 else
1091 *out_set = kmalloc(bytes, M_SELECT, M_WAITOK);
1092 error = copyin(in_set, *out_set, bytes);
1093 } else {
1094 *out_set = NULL;
1095 error = 0;
1097 return (error);
1101 * Copy returned select bits back out to userland.
1103 static int
1104 putbits(int bytes, kfd_set *in_set, fd_set *out_set)
1106 int error;
1108 if (in_set) {
1109 error = copyout(in_set, out_set, bytes);
1110 } else {
1111 error = 0;
1113 return (error);
1116 static int
1117 dotimeout_only(struct timespec *ts)
1119 return(nanosleep1(ts, NULL));
1123 * Common code for sys_select() and sys_pselect().
1125 * in, out and ex are userland pointers. ts must point to validated
1126 * kernel-side timeout value or NULL for infinite timeout. res must
1127 * point to syscall return value.
1129 static int
1130 doselect(int nd, fd_set *read, fd_set *write, fd_set *except,
1131 struct timespec *ts, int *res)
1133 struct proc *p = curproc;
1134 struct select_kevent_copyin_args *kap, ka;
1135 int bytes, error;
1136 kfd_set read_tmp;
1137 kfd_set write_tmp;
1138 kfd_set except_tmp;
1140 *res = 0;
1141 if (nd < 0)
1142 return (EINVAL);
1143 if (nd == 0 && ts)
1144 return (dotimeout_only(ts));
1146 if (nd > p->p_fd->fd_nfiles) /* limit kmalloc */
1147 nd = p->p_fd->fd_nfiles;
1149 kap = &ka;
1150 kap->lwp = curthread->td_lwp;
1151 kap->num_fds = nd;
1152 kap->proc_fds = 0;
1153 kap->error = 0;
1154 kap->active_set = COPYIN_READ;
1157 * Calculate bytes based on the number of __fd_mask[] array entries
1158 * multiplied by the size of __fd_mask.
1160 bytes = howmany(nd, __NFDBITS) * sizeof(__fd_mask);
1162 /* kap->read_set = NULL; not needed */
1163 kap->write_set = NULL;
1164 kap->except_set = NULL;
1166 error = getbits(bytes, read, &kap->read_set, &read_tmp);
1167 if (error == 0)
1168 error = getbits(bytes, write, &kap->write_set, &write_tmp);
1169 if (error == 0)
1170 error = getbits(bytes, except, &kap->except_set, &except_tmp);
1171 if (error)
1172 goto done;
1175 * NOTE: Make sure the max events passed to kern_kevent() is
1176 * effectively unlimited. (nd * 3) accomplishes this.
1178 * (*res) continues to increment as returned events are
1179 * loaded in.
1181 error = kern_kevent(&kap->lwp->lwp_kqueue, 0x7FFFFFFF, res, kap,
1182 select_copyin, select_copyout, ts);
1183 if (error == 0)
1184 error = putbits(bytes, kap->read_set, read);
1185 if (error == 0)
1186 error = putbits(bytes, kap->write_set, write);
1187 if (error == 0)
1188 error = putbits(bytes, kap->except_set, except);
1191 * An error from an individual event that should be passed
1192 * back to userland (EBADF)
1194 if (kap->error)
1195 error = kap->error;
1198 * Clean up.
1200 done:
1201 if (kap->read_set && kap->read_set != &read_tmp)
1202 kfree(kap->read_set, M_SELECT);
1203 if (kap->write_set && kap->write_set != &write_tmp)
1204 kfree(kap->write_set, M_SELECT);
1205 if (kap->except_set && kap->except_set != &except_tmp)
1206 kfree(kap->except_set, M_SELECT);
1208 kap->lwp->lwp_kqueue_serial += kap->num_fds;
1210 return (error);
1214 * Poll system call.
1216 * MPSAFE
1219 sys_poll(struct poll_args *uap)
1221 struct timespec ts, *tsp;
1222 int error;
1224 if (uap->timeout != INFTIM) {
1225 ts.tv_sec = uap->timeout / 1000;
1226 ts.tv_nsec = (uap->timeout % 1000) * 1000 * 1000;
1227 tsp = &ts;
1228 } else {
1229 tsp = NULL;
1232 error = dopoll(uap->nfds, uap->fds, tsp, &uap->sysmsg_result);
1234 return (error);
1237 static int
1238 poll_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
1240 struct poll_kevent_copyin_args *pkap;
1241 struct pollfd *pfd;
1242 struct kevent *kev;
1243 int kev_count;
1245 pkap = (struct poll_kevent_copyin_args *)arg;
1247 while (pkap->pfds < pkap->nfds) {
1248 pfd = &pkap->fds[pkap->pfds];
1250 /* Clear return events */
1251 pfd->revents = 0;
1253 /* Do not check if fd is equal to -1 */
1254 if (pfd->fd == -1) {
1255 ++pkap->pfds;
1256 continue;
1259 kev_count = 0;
1260 if (pfd->events & (POLLIN | POLLRDNORM))
1261 kev_count++;
1262 if (pfd->events & (POLLOUT | POLLWRNORM))
1263 kev_count++;
1264 if (pfd->events & (POLLPRI | POLLRDBAND))
1265 kev_count++;
1267 if (*events + kev_count > maxevents)
1268 return (0);
1271 * NOTE: A combined serial number and poll array index is
1272 * stored in kev->udata.
1274 kev = &kevp[*events];
1275 if (pfd->events & (POLLIN | POLLRDNORM)) {
1276 EV_SET(kev++, pfd->fd, EVFILT_READ, EV_ADD|EV_ENABLE,
1277 NOTE_OLDAPI, 0, (void *)(uintptr_t)
1278 (pkap->lwp->lwp_kqueue_serial + pkap->pfds));
1280 if (pfd->events & (POLLOUT | POLLWRNORM)) {
1281 EV_SET(kev++, pfd->fd, EVFILT_WRITE, EV_ADD|EV_ENABLE,
1282 NOTE_OLDAPI, 0, (void *)(uintptr_t)
1283 (pkap->lwp->lwp_kqueue_serial + pkap->pfds));
1285 if (pfd->events & (POLLPRI | POLLRDBAND)) {
1286 EV_SET(kev++, pfd->fd, EVFILT_EXCEPT, EV_ADD|EV_ENABLE,
1287 NOTE_OLDAPI | NOTE_OOB, 0,
1288 (void *)(uintptr_t)
1289 (pkap->lwp->lwp_kqueue_serial + pkap->pfds));
1292 if (nseldebug) {
1293 kprintf("poll index %d/%d fd %d events %08x serial %d\n",
1294 pkap->pfds, pkap->nfds-1, pfd->fd, pfd->events,
1295 pkap->lwp->lwp_kqueue_serial);
1298 ++pkap->pfds;
1299 (*events) += kev_count;
1302 return (0);
1305 static int
1306 poll_copyout(void *arg, struct kevent *kevp, int count, int *res)
1308 struct poll_kevent_copyin_args *pkap;
1309 struct pollfd *pfd;
1310 struct kevent kev;
1311 int count_res;
1312 int i;
1313 u_int pi;
1315 pkap = (struct poll_kevent_copyin_args *)arg;
1317 for (i = 0; i < count; ++i) {
1319 * Extract the poll array index and delete spurious events.
1320 * We can easily tell if the serial number is incorrect
1321 * by checking whether the extracted index is out of range.
1323 pi = (u_int)(uintptr_t)kevp[i].udata -
1324 (u_int)pkap->lwp->lwp_kqueue_serial;
1326 if (pi >= pkap->nfds) {
1327 kev = kevp[i];
1328 kev.flags = EV_DISABLE|EV_DELETE;
1329 kqueue_register(&pkap->lwp->lwp_kqueue, &kev);
1330 if (nseldebug)
1331 kprintf("poll index %d out of range against serial %d\n",
1332 pi, pkap->lwp->lwp_kqueue_serial);
1333 continue;
1335 pfd = &pkap->fds[pi];
1336 if (kevp[i].ident == pfd->fd) {
1338 * A single descriptor may generate an error against
1339 * more than one filter, make sure to set the
1340 * appropriate flags but do not increment (*res)
1341 * more than once.
1343 count_res = (pfd->revents == 0);
1344 if (kevp[i].flags & EV_ERROR) {
1345 switch(kevp[i].data) {
1346 case EBADF:
1347 case POLLNVAL:
1348 /* Bad file descriptor */
1349 if (count_res)
1350 ++*res;
1351 pfd->revents |= POLLNVAL;
1352 break;
1353 default:
1355 * Poll silently swallows any unknown
1356 * errors except in the case of POLLPRI
1357 * (OOB/urgent data).
1359 * ALWAYS filter out EOPNOTSUPP errors
1360 * from filters, common applications
1361 * set POLLPRI|POLLRDBAND and most
1362 * filters do not support EVFILT_EXCEPT.
1364 * We also filter out ENODEV since dev_dkqfilter
1365 * returns ENODEV if EOPNOTSUPP is returned in an
1366 * inner call.
1368 * XXX: fix this
1370 if (kevp[i].filter != EVFILT_READ &&
1371 kevp[i].filter != EVFILT_WRITE &&
1372 kevp[i].data != EOPNOTSUPP &&
1373 kevp[i].data != ENODEV) {
1374 if (count_res == 0)
1375 ++*res;
1376 pfd->revents |= POLLERR;
1378 break;
1380 if (nseldebug) {
1381 kprintf("poll index %d fd %d "
1382 "filter %d error %jd\n",
1383 pi, pfd->fd,
1384 kevp[i].filter,
1385 (intmax_t)kevp[i].data);
1387 continue;
1390 switch (kevp[i].filter) {
1391 case EVFILT_READ:
1392 #if 0
1394 * NODATA on the read side can indicate a
1395 * half-closed situation and not necessarily
1396 * a disconnect, so depend on the user
1397 * issuing a read() and getting 0 bytes back.
1399 if (kevp[i].flags & EV_NODATA)
1400 pfd->revents |= POLLHUP;
1401 #endif
1402 if ((kevp[i].flags & EV_EOF) &&
1403 kevp[i].fflags != 0)
1404 pfd->revents |= POLLERR;
1405 if (pfd->events & POLLIN)
1406 pfd->revents |= POLLIN;
1407 if (pfd->events & POLLRDNORM)
1408 pfd->revents |= POLLRDNORM;
1409 break;
1410 case EVFILT_WRITE:
1412 * As per the OpenGroup POLLHUP is mutually
1413 * exclusive with the writability flags. I
1414 * consider this a bit broken but...
1416 * In this case a disconnect is implied even
1417 * for a half-closed (write side) situation.
1419 if (kevp[i].flags & EV_EOF) {
1420 pfd->revents |= POLLHUP;
1421 if (kevp[i].fflags != 0)
1422 pfd->revents |= POLLERR;
1423 } else {
1424 if (pfd->events & POLLOUT)
1425 pfd->revents |= POLLOUT;
1426 if (pfd->events & POLLWRNORM)
1427 pfd->revents |= POLLWRNORM;
1429 break;
1430 case EVFILT_EXCEPT:
1432 * EV_NODATA should never be tagged for this
1433 * filter.
1435 if (pfd->events & POLLPRI)
1436 pfd->revents |= POLLPRI;
1437 if (pfd->events & POLLRDBAND)
1438 pfd->revents |= POLLRDBAND;
1439 break;
1442 if (nseldebug) {
1443 kprintf("poll index %d/%d fd %d revents %08x\n",
1444 pi, pkap->nfds, pfd->fd, pfd->revents);
1447 if (count_res && pfd->revents)
1448 ++*res;
1449 } else {
1450 if (nseldebug) {
1451 kprintf("poll index %d mismatch %ju/%d\n",
1452 pi, (uintmax_t)kevp[i].ident, pfd->fd);
1457 return (0);
1460 static int
1461 dopoll(int nfds, struct pollfd *fds, struct timespec *ts, int *res)
1463 struct poll_kevent_copyin_args ka;
1464 struct pollfd sfds[64];
1465 int bytes;
1466 int error;
1468 *res = 0;
1469 if (nfds < 0)
1470 return (EINVAL);
1472 if (nfds == 0 && ts)
1473 return (dotimeout_only(ts));
1476 * This is a bit arbitrary but we need to limit internal kmallocs.
1478 if (nfds > maxfilesperproc * 2)
1479 nfds = maxfilesperproc * 2;
1480 bytes = sizeof(struct pollfd) * nfds;
1482 ka.lwp = curthread->td_lwp;
1483 ka.nfds = nfds;
1484 ka.pfds = 0;
1485 ka.error = 0;
1487 if (ka.nfds < 64)
1488 ka.fds = sfds;
1489 else
1490 ka.fds = kmalloc(bytes, M_SELECT, M_WAITOK);
1492 error = copyin(fds, ka.fds, bytes);
1493 if (error == 0)
1494 error = kern_kevent(&ka.lwp->lwp_kqueue, 0x7FFFFFFF, res, &ka,
1495 poll_copyin, poll_copyout, ts);
1497 if (error == 0)
1498 error = copyout(ka.fds, fds, bytes);
1500 if (ka.fds != sfds)
1501 kfree(ka.fds, M_SELECT);
1503 ka.lwp->lwp_kqueue_serial += nfds;
1505 return (error);
1508 static int
1509 socket_wait_copyin(void *arg, struct kevent *kevp, int maxevents, int *events)
1511 return (0);
1514 static int
1515 socket_wait_copyout(void *arg, struct kevent *kevp, int count, int *res)
1517 ++*res;
1518 return (0);
1521 extern struct fileops socketops;
1524 * NOTE: Callers of socket_wait() must already have a reference on the
1525 * socket.
1528 socket_wait(struct socket *so, struct timespec *ts, int *res)
1530 struct thread *td = curthread;
1531 struct file *fp;
1532 struct kqueue kq;
1533 struct kevent kev;
1534 int error, fd;
1536 if ((error = falloc(td->td_lwp, &fp, &fd)) != 0)
1537 return (error);
1539 fp->f_type = DTYPE_SOCKET;
1540 fp->f_flag = FREAD | FWRITE;
1541 fp->f_ops = &socketops;
1542 fp->f_data = so;
1543 fsetfd(td->td_lwp->lwp_proc->p_fd, fp, fd);
1545 kqueue_init(&kq, td->td_lwp->lwp_proc->p_fd);
1546 EV_SET(&kev, fd, EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, NULL);
1547 if ((error = kqueue_register(&kq, &kev)) != 0) {
1548 fdrop(fp);
1549 return (error);
1552 error = kern_kevent(&kq, 1, res, NULL, socket_wait_copyin,
1553 socket_wait_copyout, ts);
1555 EV_SET(&kev, fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
1556 kqueue_register(&kq, &kev);
1557 fp->f_ops = &badfileops;
1558 fdrop(fp);
1560 return (error);
1564 * OpenBSD poll system call.
1565 * XXX this isn't quite a true representation.. OpenBSD uses select ops.
1567 * MPSAFE
1570 sys_openbsd_poll(struct openbsd_poll_args *uap)
1572 return (sys_poll((struct poll_args *)uap));
1575 /*ARGSUSED*/
1577 seltrue(cdev_t dev, int events)
1579 return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));