Home | History | Annotate | Line # | Download | only in kern
sys_generic.c revision 1.100.2.9
      1 /*	$NetBSD: sys_generic.c,v 1.100.2.9 2007/07/15 15:52:56 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2007 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     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. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1982, 1986, 1989, 1993
     41  *	The Regents of the University of California.  All rights reserved.
     42  * (c) UNIX System Laboratories, Inc.
     43  * All or some portions of this file are derived from material licensed
     44  * to the University of California by American Telephone and Telegraph
     45  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     46  * the permission of UNIX System Laboratories, Inc.
     47  *
     48  * Redistribution and use in source and binary forms, with or without
     49  * modification, are permitted provided that the following conditions
     50  * are met:
     51  * 1. Redistributions of source code must retain the above copyright
     52  *    notice, this list of conditions and the following disclaimer.
     53  * 2. Redistributions in binary form must reproduce the above copyright
     54  *    notice, this list of conditions and the following disclaimer in the
     55  *    documentation and/or other materials provided with the distribution.
     56  * 3. Neither the name of the University nor the names of its contributors
     57  *    may be used to endorse or promote products derived from this software
     58  *    without specific prior written permission.
     59  *
     60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     70  * SUCH DAMAGE.
     71  *
     72  *	@(#)sys_generic.c	8.9 (Berkeley) 2/14/95
     73  */
     74 
     75 /*
     76  * System calls relating to files.
     77  */
     78 
     79 #include <sys/cdefs.h>
     80 __KERNEL_RCSID(0, "$NetBSD: sys_generic.c,v 1.100.2.9 2007/07/15 15:52:56 ad Exp $");
     81 
     82 #include "opt_ktrace.h"
     83 
     84 #include <sys/param.h>
     85 #include <sys/systm.h>
     86 #include <sys/filedesc.h>
     87 #include <sys/ioctl.h>
     88 #include <sys/file.h>
     89 #include <sys/proc.h>
     90 #include <sys/socketvar.h>
     91 #include <sys/signalvar.h>
     92 #include <sys/uio.h>
     93 #include <sys/kernel.h>
     94 #include <sys/stat.h>
     95 #include <sys/kmem.h>
     96 #include <sys/poll.h>
     97 #include <sys/vnode.h>
     98 #include <sys/mount.h>
     99 #include <sys/syscallargs.h>
    100 #ifdef KTRACE
    101 #include <sys/ktrace.h>
    102 #endif
    103 
    104 #include <uvm/uvm_extern.h>
    105 
    106 /* Flags for lwp::l_selflag. */
    107 #define	SEL_RESET	0	/* awoken, interrupted, or not yet polling */
    108 #define	SEL_SCANNING	1	/* polling descriptors */
    109 #define	SEL_BLOCKING	2	/* about to block on select_cv */
    110 
    111 static int	selscan(lwp_t *, fd_mask *, fd_mask *, int, register_t *);
    112 static int	pollscan(lwp_t *, struct pollfd *, int, register_t *);
    113 static void	selclear(void);
    114 
    115 /* Global state for select()/poll(). */
    116 kmutex_t	select_lock;
    117 kcondvar_t	select_cv;
    118 int		nselcoll;
    119 
    120 /*
    121  * Read system call.
    122  */
    123 /* ARGSUSED */
    124 int
    125 sys_read(lwp_t *l, void *v, register_t *retval)
    126 {
    127 	struct sys_read_args /* {
    128 		syscallarg(int)		fd;
    129 		syscallarg(void *)	buf;
    130 		syscallarg(size_t)	nbyte;
    131 	} */ *uap = v;
    132 	int		fd;
    133 	struct file	*fp;
    134 	proc_t		*p;
    135 	struct filedesc	*fdp;
    136 
    137 	fd = SCARG(uap, fd);
    138 	p = l->l_proc;
    139 	fdp = p->p_fd;
    140 
    141 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    142 		return (EBADF);
    143 
    144 	if ((fp->f_flag & FREAD) == 0) {
    145 		mutex_exit(&fp->f_lock);
    146 		return (EBADF);
    147 	}
    148 
    149 	FILE_USE(fp);
    150 
    151 	/* dofileread() will unuse the descriptor for us */
    152 	return (dofileread(l, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
    153 	    &fp->f_offset, FOF_UPDATE_OFFSET, retval));
    154 }
    155 
    156 int
    157 dofileread(lwp_t *l, int fd, struct file *fp, void *buf, size_t nbyte,
    158 	off_t *offset, int flags, register_t *retval)
    159 {
    160 	struct iovec aiov;
    161 	struct uio auio;
    162 	proc_t *p;
    163 	struct vmspace *vm;
    164 	size_t cnt;
    165 	int error;
    166 #ifdef KTRACE
    167 	struct iovec	ktriov;
    168 #endif
    169 	p = l->l_proc;
    170 
    171 	error = proc_vmspace_getref(p, &vm);
    172 	if (error) {
    173 		goto out;
    174 	}
    175 
    176 	aiov.iov_base = (void *)buf;
    177 	aiov.iov_len = nbyte;
    178 	auio.uio_iov = &aiov;
    179 	auio.uio_iovcnt = 1;
    180 	auio.uio_resid = nbyte;
    181 	auio.uio_rw = UIO_READ;
    182 	auio.uio_vmspace = vm;
    183 
    184 	/*
    185 	 * Reads return ssize_t because -1 is returned on error.  Therefore
    186 	 * we must restrict the length to SSIZE_MAX to avoid garbage return
    187 	 * values.
    188 	 */
    189 	if (auio.uio_resid > SSIZE_MAX) {
    190 		error = EINVAL;
    191 		goto out;
    192 	}
    193 
    194 #ifdef KTRACE
    195 	/* In case we are tracing, save a copy of iovec */
    196 	ktriov = aiov;
    197 #endif
    198 	cnt = auio.uio_resid;
    199 	error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
    200 	if (error)
    201 		if (auio.uio_resid != cnt && (error == ERESTART ||
    202 		    error == EINTR || error == EWOULDBLOCK))
    203 			error = 0;
    204 	cnt -= auio.uio_resid;
    205 #ifdef KTRACE
    206 	if (KTRPOINT(p, KTR_GENIO) && error == 0)
    207 		ktrgenio(l, fd, UIO_READ, &ktriov, cnt, error);
    208 #endif
    209 	*retval = cnt;
    210  out:
    211 	FILE_UNUSE(fp, l);
    212 	uvmspace_free(vm);
    213 	return (error);
    214 }
    215 
    216 /*
    217  * Scatter read system call.
    218  */
    219 int
    220 sys_readv(lwp_t *l, void *v, register_t *retval)
    221 {
    222 	struct sys_readv_args /* {
    223 		syscallarg(int)				fd;
    224 		syscallarg(const struct iovec *)	iovp;
    225 		syscallarg(int)				iovcnt;
    226 	} */ *uap = v;
    227 
    228 	return do_filereadv(l, SCARG(uap, fd), SCARG(uap, iovp),
    229 	    SCARG(uap, iovcnt), NULL, FOF_UPDATE_OFFSET, retval);
    230 }
    231 
    232 int
    233 do_filereadv(struct lwp *l, int fd, const struct iovec *iovp, int iovcnt,
    234     off_t *offset, int flags, register_t *retval)
    235 {
    236 	struct proc	*p;
    237 	struct uio	auio;
    238 	struct iovec	*iov, *needfree = NULL, aiov[UIO_SMALLIOV];
    239 	struct vmspace	*vm;
    240 	int		i, error;
    241 	size_t		cnt;
    242 	u_int		iovlen;
    243 	struct file	*fp;
    244 	struct filedesc	*fdp;
    245 #ifdef KTRACE
    246 	struct iovec	*ktriov = NULL;
    247 #endif
    248 
    249 	if (iovcnt == 0)
    250 		return EINVAL;
    251 
    252 	p = l->l_proc;
    253 	fdp = p->p_fd;
    254 
    255 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    256 		return EBADF;
    257 
    258 	if ((fp->f_flag & FREAD) == 0) {
    259 		mutex_exit(&fp->f_lock);
    260 		return EBADF;
    261 	}
    262 
    263 	FILE_USE(fp);
    264 
    265 	if (offset == NULL)
    266 		offset = &fp->f_offset;
    267 	else {
    268 		struct vnode *vp = fp->f_data;
    269 		if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
    270 			error = ESPIPE;
    271 			goto out;
    272 		}
    273 		/*
    274 		 * Test that the device is seekable ?
    275 		 * XXX This works because no file systems actually
    276 		 * XXX take any action on the seek operation.
    277 		 */
    278 		error = VOP_SEEK(vp, fp->f_offset, *offset, fp->f_cred);
    279 		if (error != 0)
    280 			goto out;
    281 	}
    282 
    283 	error = proc_vmspace_getref(p, &vm);
    284 	if (error)
    285 		goto out;
    286 
    287 	iovlen = iovcnt * sizeof(struct iovec);
    288 	if (flags & FOF_IOV_SYSSPACE)
    289 		iov = __UNCONST(iovp);
    290 	else {
    291 		iov = aiov;
    292 		if ((u_int)iovcnt > UIO_SMALLIOV) {
    293 			if ((u_int)iovcnt > IOV_MAX) {
    294 				error = EINVAL;
    295 				goto out;
    296 			}
    297 			iov = kmem_alloc(iovlen, KM_SLEEP);
    298 			if (iov == NULL) {
    299 				error = ENOMEM;
    300 				goto out;
    301 			}
    302 			needfree = iov;
    303 		}
    304 		error = copyin(iovp, iov, iovlen);
    305 		if (error)
    306 			goto done;
    307 	}
    308 
    309 	auio.uio_iov = iov;
    310 	auio.uio_iovcnt = iovcnt;
    311 	auio.uio_rw = UIO_READ;
    312 	auio.uio_vmspace = vm;
    313 
    314 	auio.uio_resid = 0;
    315 	for (i = 0; i < iovcnt; i++, iov++) {
    316 		auio.uio_resid += iov->iov_len;
    317 		/*
    318 		 * Reads return ssize_t because -1 is returned on error.
    319 		 * Therefore we must restrict the length to SSIZE_MAX to
    320 		 * avoid garbage return values.
    321 		 */
    322 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    323 			error = EINVAL;
    324 			goto done;
    325 		}
    326 	}
    327 
    328 #ifdef KTRACE
    329 	/*
    330 	 * if tracing, save a copy of iovec
    331 	 */
    332 	if (KTRPOINT(p, KTR_GENIO))  {
    333 		ktriov = kmem_alloc(iovlen, KM_SLEEP);
    334 		if (ktriov != NULL)
    335 			memcpy(ktriov, auio.uio_iov, iovlen);
    336 	}
    337 #endif
    338 
    339 	cnt = auio.uio_resid;
    340 	error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
    341 	if (error)
    342 		if (auio.uio_resid != cnt && (error == ERESTART ||
    343 		    error == EINTR || error == EWOULDBLOCK))
    344 			error = 0;
    345 	cnt -= auio.uio_resid;
    346 	*retval = cnt;
    347 
    348 #ifdef KTRACE
    349 	if (ktriov != NULL) {
    350 		if (KTRPOINT(p, KTR_GENIO) && (error == 0))
    351 			ktrgenio(l, fd, UIO_READ, ktriov, cnt, error);
    352 		kmem_free(ktriov, iovlen);
    353 	}
    354 #endif
    355 
    356  done:
    357 	if (needfree)
    358 		kmem_free(needfree, iovlen);
    359  out:
    360 	FILE_UNUSE(fp, l);
    361 	uvmspace_free(vm);
    362 	return (error);
    363 }
    364 
    365 /*
    366  * Write system call
    367  */
    368 int
    369 sys_write(lwp_t *l, void *v, register_t *retval)
    370 {
    371 	struct sys_write_args /* {
    372 		syscallarg(int)			fd;
    373 		syscallarg(const void *)	buf;
    374 		syscallarg(size_t)		nbyte;
    375 	} */ *uap = v;
    376 	int		fd;
    377 	struct file	*fp;
    378 	proc_t		*p;
    379 	struct filedesc	*fdp;
    380 
    381 	fd = SCARG(uap, fd);
    382 	p = l->l_proc;
    383 	fdp = p->p_fd;
    384 
    385 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    386 		return (EBADF);
    387 
    388 	if ((fp->f_flag & FWRITE) == 0) {
    389 		mutex_exit(&fp->f_lock);
    390 		return (EBADF);
    391 	}
    392 
    393 	FILE_USE(fp);
    394 
    395 	/* dofilewrite() will unuse the descriptor for us */
    396 	return (dofilewrite(l, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
    397 	    &fp->f_offset, FOF_UPDATE_OFFSET, retval));
    398 }
    399 
    400 int
    401 dofilewrite(lwp_t *l, int fd, struct file *fp, const void *buf,
    402 	size_t nbyte, off_t *offset, int flags, register_t *retval)
    403 {
    404 	struct iovec aiov;
    405 	struct uio auio;
    406 	proc_t *p;
    407 	struct vmspace *vm;
    408 	size_t cnt;
    409 	int error;
    410 #ifdef KTRACE
    411 	struct iovec	ktriov;
    412 #endif
    413 
    414 	p = l->l_proc;
    415 	error = proc_vmspace_getref(p, &vm);
    416 	if (error) {
    417 		goto out;
    418 	}
    419 	aiov.iov_base = __UNCONST(buf);		/* XXXUNCONST kills const */
    420 	aiov.iov_len = nbyte;
    421 	auio.uio_iov = &aiov;
    422 	auio.uio_iovcnt = 1;
    423 	auio.uio_resid = nbyte;
    424 	auio.uio_rw = UIO_WRITE;
    425 	auio.uio_vmspace = vm;
    426 
    427 	/*
    428 	 * Writes return ssize_t because -1 is returned on error.  Therefore
    429 	 * we must restrict the length to SSIZE_MAX to avoid garbage return
    430 	 * values.
    431 	 */
    432 	if (auio.uio_resid > SSIZE_MAX) {
    433 		error = EINVAL;
    434 		goto out;
    435 	}
    436 
    437 #ifdef KTRACE
    438 	/* In case we are tracing, save a copy of iovec */
    439 	ktriov = aiov;
    440 #endif
    441 	cnt = auio.uio_resid;
    442 	error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
    443 	if (error) {
    444 		if (auio.uio_resid != cnt && (error == ERESTART ||
    445 		    error == EINTR || error == EWOULDBLOCK))
    446 			error = 0;
    447 		if (error == EPIPE) {
    448 			mutex_enter(&proclist_mutex);
    449 			psignal(p, SIGPIPE);
    450 			mutex_exit(&proclist_mutex);
    451 		}
    452 	}
    453 	cnt -= auio.uio_resid;
    454 #ifdef KTRACE
    455 	if (KTRPOINT(p, KTR_GENIO) && error == 0)
    456 		ktrgenio(l, fd, UIO_WRITE, &ktriov, cnt, error);
    457 #endif
    458 	*retval = cnt;
    459  out:
    460 	FILE_UNUSE(fp, l);
    461 	uvmspace_free(vm);
    462 	return (error);
    463 }
    464 
    465 /*
    466  * Gather write system call
    467  */
    468 int
    469 sys_writev(lwp_t *l, void *v, register_t *retval)
    470 {
    471 	struct sys_writev_args /* {
    472 		syscallarg(int)				fd;
    473 		syscallarg(const struct iovec *)	iovp;
    474 		syscallarg(int)				iovcnt;
    475 	} */ *uap = v;
    476 
    477 	return do_filewritev(l, SCARG(uap, fd), SCARG(uap, iovp),
    478 	    SCARG(uap, iovcnt), NULL, FOF_UPDATE_OFFSET, retval);
    479 }
    480 
    481 int
    482 do_filewritev(struct lwp *l, int fd, const struct iovec *iovp, int iovcnt,
    483     off_t *offset, int flags, register_t *retval)
    484 {
    485 	struct proc	*p;
    486 	struct uio	auio;
    487 	struct iovec	*iov, *needfree = NULL, aiov[UIO_SMALLIOV];
    488 	struct vmspace	*vm;
    489 	int		i, error;
    490 	size_t		cnt;
    491 	u_int		iovlen;
    492 	struct file	*fp;
    493 	struct filedesc	*fdp;
    494 #ifdef KTRACE
    495 	struct iovec	*ktriov = NULL;
    496 #endif
    497 
    498 	if (iovcnt == 0)
    499 		return EINVAL;
    500 
    501 	p = l->l_proc;
    502 	fdp = p->p_fd;
    503 
    504 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    505 		return EBADF;
    506 
    507 	if ((fp->f_flag & FWRITE) == 0) {
    508 		mutex_exit(&fp->f_lock);
    509 		return EBADF;
    510 	}
    511 
    512 	FILE_USE(fp);
    513 
    514 	if (offset == NULL)
    515 		offset = &fp->f_offset;
    516 	else {
    517 		struct vnode *vp = fp->f_data;
    518 		if (fp->f_type != DTYPE_VNODE || vp->v_type == VFIFO) {
    519 			error = ESPIPE;
    520 			goto out;
    521 		}
    522 		/*
    523 		 * Test that the device is seekable ?
    524 		 * XXX This works because no file systems actually
    525 		 * XXX take any action on the seek operation.
    526 		 */
    527 		error = VOP_SEEK(vp, fp->f_offset, *offset, fp->f_cred);
    528 		if (error != 0)
    529 			goto out;
    530 	}
    531 
    532 	error = proc_vmspace_getref(p, &vm);
    533 	if (error)
    534 		goto out;
    535 
    536 	iovlen = iovcnt * sizeof(struct iovec);
    537 	if (flags & FOF_IOV_SYSSPACE)
    538 		iov = __UNCONST(iovp);
    539 	else {
    540 		iov = aiov;
    541 		if ((u_int)iovcnt > UIO_SMALLIOV) {
    542 			if ((u_int)iovcnt > IOV_MAX) {
    543 				error = EINVAL;
    544 				goto out;
    545 			}
    546 			iov = kmem_alloc(iovlen, KM_SLEEP);
    547 			if (iov == NULL) {
    548 				error = ENOMEM;
    549 				goto out;
    550 			}
    551 			needfree = iov;
    552 		}
    553 		error = copyin(iovp, iov, iovlen);
    554 		if (error)
    555 			goto done;
    556 	}
    557 
    558 	auio.uio_iov = iov;
    559 	auio.uio_iovcnt = iovcnt;
    560 	auio.uio_rw = UIO_WRITE;
    561 	auio.uio_vmspace = vm;
    562 
    563 	auio.uio_resid = 0;
    564 	for (i = 0; i < iovcnt; i++, iov++) {
    565 		auio.uio_resid += iov->iov_len;
    566 		/*
    567 		 * Writes return ssize_t because -1 is returned on error.
    568 		 * Therefore we must restrict the length to SSIZE_MAX to
    569 		 * avoid garbage return values.
    570 		 */
    571 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    572 			error = EINVAL;
    573 			goto done;
    574 		}
    575 	}
    576 
    577 #ifdef KTRACE
    578 	/*
    579 	 * if tracing, save a copy of iovec
    580 	 */
    581 	if (KTRPOINT(p, KTR_GENIO))  {
    582 		ktriov = kmem_alloc(iovlen, KM_SLEEP);
    583 		if (ktriov != NULL)
    584 			memcpy(ktriov, auio.uio_iov, iovlen);
    585 	}
    586 #endif
    587 	cnt = auio.uio_resid;
    588 	error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
    589 	if (error) {
    590 		if (auio.uio_resid != cnt && (error == ERESTART ||
    591 		    error == EINTR || error == EWOULDBLOCK))
    592 			error = 0;
    593 		if (error == EPIPE) {
    594 			mutex_enter(&proclist_mutex);
    595 			psignal(p, SIGPIPE);
    596 			mutex_exit(&proclist_mutex);
    597 		}
    598 	}
    599 	cnt -= auio.uio_resid;
    600 	*retval = cnt;
    601 
    602 #ifdef KTRACE
    603 	if (ktriov != NULL) {
    604 		if (KTRPOINT(p, KTR_GENIO) && (error == 0))
    605 			ktrgenio(l, fd, UIO_WRITE, ktriov, cnt, error);
    606 		kmem_free(ktriov, iovlen);
    607 	}
    608 #endif
    609 
    610  done:
    611 	if (needfree)
    612 		kmem_free(needfree, iovlen);
    613  out:
    614 	FILE_UNUSE(fp, l);
    615 	uvmspace_free(vm);
    616 	return (error);
    617 }
    618 
    619 /*
    620  * Ioctl system call
    621  */
    622 /* ARGSUSED */
    623 int
    624 sys_ioctl(lwp_t *l, void *v, register_t *retval)
    625 {
    626 	struct sys_ioctl_args /* {
    627 		syscallarg(int)		fd;
    628 		syscallarg(u_long)	com;
    629 		syscallarg(void *)	data;
    630 	} */ *uap = v;
    631 	struct file	*fp;
    632 	proc_t		*p;
    633 	struct filedesc	*fdp;
    634 	u_long		com;
    635 	int		error;
    636 	u_int		size;
    637 	void 		*data, *memp;
    638 #define	STK_PARAMS	128
    639 	u_long		stkbuf[STK_PARAMS/sizeof(u_long)];
    640 
    641 	error = 0;
    642 	p = l->l_proc;
    643 	fdp = p->p_fd;
    644 
    645 	if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
    646 		return (EBADF);
    647 
    648 	FILE_USE(fp);
    649 
    650 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
    651 		error = EBADF;
    652 		com = 0;
    653 		goto out;
    654 	}
    655 
    656 	switch (com = SCARG(uap, com)) {
    657 	case FIONCLEX:
    658 		rw_enter(&fdp->fd_lock, RW_WRITER);
    659 		fdp->fd_ofileflags[SCARG(uap, fd)] &= ~UF_EXCLOSE;
    660 		rw_exit(&fdp->fd_lock);
    661 		goto out;
    662 
    663 	case FIOCLEX:
    664 		rw_enter(&fdp->fd_lock, RW_WRITER);
    665 		fdp->fd_ofileflags[SCARG(uap, fd)] |= UF_EXCLOSE;
    666 		rw_exit(&fdp->fd_lock);
    667 		goto out;
    668 	}
    669 
    670 	/*
    671 	 * Interpret high order word to find amount of data to be
    672 	 * copied to/from the user's address space.
    673 	 */
    674 	size = IOCPARM_LEN(com);
    675 	if (size > IOCPARM_MAX) {
    676 		error = ENOTTY;
    677 		goto out;
    678 	}
    679 	memp = NULL;
    680 	if (size > sizeof(stkbuf)) {
    681 		memp = kmem_alloc(size, KM_SLEEP);
    682 		data = memp;
    683 	} else
    684 		data = (void *)stkbuf;
    685 	if (com&IOC_IN) {
    686 		if (size) {
    687 			error = copyin(SCARG(uap, data), data, size);
    688 			if (error) {
    689 				if (memp)
    690 					kmem_free(memp, size);
    691 				goto out;
    692 			}
    693 #ifdef KTRACE
    694 			if (KTRPOINT(p, KTR_GENIO)) {
    695 				struct iovec iov;
    696 				iov.iov_base = SCARG(uap, data);
    697 				iov.iov_len = size;
    698 				ktrgenio(l, SCARG(uap, fd), UIO_WRITE, &iov,
    699 					size, 0);
    700 			}
    701 #endif
    702 		} else
    703 			*(void **)data = SCARG(uap, data);
    704 	} else if ((com&IOC_OUT) && size)
    705 		/*
    706 		 * Zero the buffer so the user always
    707 		 * gets back something deterministic.
    708 		 */
    709 		memset(data, 0, size);
    710 	else if (com&IOC_VOID)
    711 		*(void **)data = SCARG(uap, data);
    712 
    713 	switch (com) {
    714 
    715 	case FIONBIO:
    716 		mutex_enter(&fp->f_lock);
    717 		if (*(int *)data != 0)
    718 			fp->f_flag |= FNONBLOCK;
    719 		else
    720 			fp->f_flag &= ~FNONBLOCK;
    721 		mutex_exit(&fp->f_lock);
    722 		error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, data, l);
    723 		break;
    724 
    725 	case FIOASYNC:
    726 		mutex_enter(&fp->f_lock);
    727 		if (*(int *)data != 0)
    728 			fp->f_flag |= FASYNC;
    729 		else
    730 			fp->f_flag &= ~FASYNC;
    731 		mutex_exit(&fp->f_lock);
    732 		error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, data, l);
    733 		break;
    734 
    735 	default:
    736 		error = (*fp->f_ops->fo_ioctl)(fp, com, data, l);
    737 		/*
    738 		 * Copy any data to user, size was
    739 		 * already set and checked above.
    740 		 */
    741 		if (error == 0 && (com&IOC_OUT) && size) {
    742 			error = copyout(data, SCARG(uap, data), size);
    743 #ifdef KTRACE
    744 			if (KTRPOINT(p, KTR_GENIO)) {
    745 				struct iovec iov;
    746 				iov.iov_base = SCARG(uap, data);
    747 				iov.iov_len = size;
    748 				ktrgenio(l, SCARG(uap, fd), UIO_READ, &iov,
    749 					size, error);
    750 			}
    751 #endif
    752 		}
    753 		break;
    754 	}
    755 	if (memp)
    756 		kmem_free(memp, size);
    757  out:
    758 	FILE_UNUSE(fp, l);
    759 	switch (error) {
    760 	case -1:
    761 		printf("sys_ioctl: _IO%s%s('%c', %lu, %lu) returned -1: "
    762 		    "pid=%d comm=%s\n",
    763 		    (com & IOC_IN) ? "W" : "", (com & IOC_OUT) ? "R" : "",
    764 		    (char)IOCGROUP(com), (com & 0xff), IOCPARM_LEN(com),
    765 		    p->p_pid, p->p_comm);
    766 		/* FALLTHROUGH */
    767 	case EPASSTHROUGH:
    768 		error = ENOTTY;
    769 		/* FALLTHROUGH */
    770 	default:
    771 		return (error);
    772 	}
    773 }
    774 
    775 /*
    776  * Select system call.
    777  */
    778 int
    779 sys_pselect(lwp_t *l, void *v, register_t *retval)
    780 {
    781 	struct sys_pselect_args /* {
    782 		syscallarg(int)				nd;
    783 		syscallarg(fd_set *)			in;
    784 		syscallarg(fd_set *)			ou;
    785 		syscallarg(fd_set *)			ex;
    786 		syscallarg(const struct timespec *)	ts;
    787 		syscallarg(sigset_t *)			mask;
    788 	} */ * const uap = v;
    789 	struct timespec	ats;
    790 	struct timeval	atv, *tv = NULL;
    791 	sigset_t	amask, *mask = NULL;
    792 	int		error;
    793 
    794 	if (SCARG(uap, ts)) {
    795 		error = copyin(SCARG(uap, ts), &ats, sizeof(ats));
    796 		if (error)
    797 			return error;
    798 		atv.tv_sec = ats.tv_sec;
    799 		atv.tv_usec = ats.tv_nsec / 1000;
    800 		tv = &atv;
    801 	}
    802 	if (SCARG(uap, mask) != NULL) {
    803 		error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
    804 		if (error)
    805 			return error;
    806 		mask = &amask;
    807 	}
    808 
    809 	return selcommon(l, retval, SCARG(uap, nd), SCARG(uap, in),
    810 	    SCARG(uap, ou), SCARG(uap, ex), tv, mask);
    811 }
    812 
    813 int
    814 inittimeleft(struct timeval *tv, struct timeval *sleeptv)
    815 {
    816 	if (itimerfix(tv))
    817 		return -1;
    818 	getmicrouptime(sleeptv);
    819 	return 0;
    820 }
    821 
    822 int
    823 gettimeleft(struct timeval *tv, struct timeval *sleeptv)
    824 {
    825 	/*
    826 	 * We have to recalculate the timeout on every retry.
    827 	 */
    828 	struct timeval slepttv;
    829 	/*
    830 	 * reduce tv by elapsed time
    831 	 * based on monotonic time scale
    832 	 */
    833 	getmicrouptime(&slepttv);
    834 	timeradd(tv, sleeptv, tv);
    835 	timersub(tv, &slepttv, tv);
    836 	*sleeptv = slepttv;
    837 	return tvtohz(tv);
    838 }
    839 
    840 int
    841 sys_select(lwp_t *l, void *v, register_t *retval)
    842 {
    843 	struct sys_select_args /* {
    844 		syscallarg(int)			nd;
    845 		syscallarg(fd_set *)		in;
    846 		syscallarg(fd_set *)		ou;
    847 		syscallarg(fd_set *)		ex;
    848 		syscallarg(struct timeval *)	tv;
    849 	} */ * const uap = v;
    850 	struct timeval atv, *tv = NULL;
    851 	int error;
    852 
    853 	if (SCARG(uap, tv)) {
    854 		error = copyin(SCARG(uap, tv), (void *)&atv,
    855 			sizeof(atv));
    856 		if (error)
    857 			return error;
    858 		tv = &atv;
    859 	}
    860 
    861 	return selcommon(l, retval, SCARG(uap, nd), SCARG(uap, in),
    862 	    SCARG(uap, ou), SCARG(uap, ex), tv, NULL);
    863 }
    864 
    865 int
    866 selcommon(lwp_t *l, register_t *retval, int nd, fd_set *u_in,
    867 	  fd_set *u_ou, fd_set *u_ex, struct timeval *tv, sigset_t *mask)
    868 {
    869 	char		smallbits[howmany(FD_SETSIZE, NFDBITS) *
    870 			    sizeof(fd_mask) * 6];
    871 	proc_t		* const p = l->l_proc;
    872 	char 		*bits;
    873 	int		ncoll, error, timo;
    874 	size_t		ni;
    875 	sigset_t	oldmask;
    876 	struct timeval  sleeptv;
    877 
    878 	error = 0;
    879 	if (nd < 0)
    880 		return (EINVAL);
    881 	if (nd > p->p_fd->fd_nfiles) {
    882 		/* forgiving; slightly wrong */
    883 		nd = p->p_fd->fd_nfiles;
    884 	}
    885 	ni = howmany(nd, NFDBITS) * sizeof(fd_mask);
    886 	if (ni * 6 > sizeof(smallbits))
    887 		bits = kmem_alloc(ni * 6, KM_SLEEP);
    888 	else
    889 		bits = smallbits;
    890 
    891 #define	getbits(name, x)						\
    892 	if (u_ ## name) {						\
    893 		error = copyin(u_ ## name, bits + ni * x, ni);		\
    894 		if (error)						\
    895 			goto done;					\
    896 	} else								\
    897 		memset(bits + ni * x, 0, ni);
    898 	getbits(in, 0);
    899 	getbits(ou, 1);
    900 	getbits(ex, 2);
    901 #undef	getbits
    902 
    903 	timo = 0;
    904 	if (tv && inittimeleft(tv, &sleeptv) == -1) {
    905 		error = EINVAL;
    906 		goto done;
    907 	}
    908 
    909 	if (mask) {
    910 		sigminusset(&sigcantmask, mask);
    911 		mutex_enter(&p->p_smutex);
    912 		oldmask = l->l_sigmask;
    913 		l->l_sigmask = *mask;
    914 		mutex_exit(&p->p_smutex);
    915 	} else
    916 		oldmask = l->l_sigmask;	/* XXXgcc */
    917 
    918 	mutex_enter(&select_lock);
    919 	SLIST_INIT(&l->l_selwait);
    920 	for (;;) {
    921 	 	l->l_selflag = SEL_SCANNING;
    922 		ncoll = nselcoll;
    923  		mutex_exit(&select_lock);
    924 
    925 		error = selscan(l, (fd_mask *)(bits + ni * 0),
    926 		    (fd_mask *)(bits + ni * 3), nd, retval);
    927 
    928 		mutex_enter(&select_lock);
    929 		if (error || *retval)
    930 			break;
    931 		if (tv && (timo = gettimeleft(tv, &sleeptv)) <= 0)
    932 			break;
    933 		if (l->l_selflag != SEL_SCANNING || ncoll != nselcoll)
    934 			continue;
    935 		l->l_selflag = SEL_BLOCKING;
    936 		error = cv_timedwait_sig(&select_cv, &select_lock, timo);
    937 		if (error != 0)
    938 			break;
    939 	}
    940 	selclear();
    941 	mutex_exit(&select_lock);
    942 
    943 	if (mask) {
    944 		mutex_enter(&p->p_smutex);
    945 		l->l_sigmask = oldmask;
    946 		mutex_exit(&p->p_smutex);
    947 	}
    948 
    949  done:
    950 	/* select is not restarted after signals... */
    951 	if (error == ERESTART)
    952 		error = EINTR;
    953 	if (error == EWOULDBLOCK)
    954 		error = 0;
    955 	if (error == 0 && u_in != NULL)
    956 		error = copyout(bits + ni * 3, u_in, ni);
    957 	if (error == 0 && u_ou != NULL)
    958 		error = copyout(bits + ni * 4, u_ou, ni);
    959 	if (error == 0 && u_ex != NULL)
    960 		error = copyout(bits + ni * 5, u_ex, ni);
    961 	if (bits != smallbits)
    962 		kmem_free(bits, ni * 6);
    963 	return (error);
    964 }
    965 
    966 int
    967 selscan(lwp_t *l, fd_mask *ibitp, fd_mask *obitp, int nfd,
    968 	register_t *retval)
    969 {
    970 	static const int flag[3] = { POLLRDNORM | POLLHUP | POLLERR,
    971 			       POLLWRNORM | POLLHUP | POLLERR,
    972 			       POLLRDBAND };
    973 	proc_t *p = l->l_proc;
    974 	struct filedesc	*fdp;
    975 	int msk, i, j, fd, n;
    976 	fd_mask ibits, obits;
    977 	struct file *fp;
    978 
    979 	fdp = p->p_fd;
    980 	n = 0;
    981 	for (msk = 0; msk < 3; msk++) {
    982 		for (i = 0; i < nfd; i += NFDBITS) {
    983 			ibits = *ibitp++;
    984 			obits = 0;
    985 			while ((j = ffs(ibits)) && (fd = i + --j) < nfd) {
    986 				ibits &= ~(1 << j);
    987 				if ((fp = fd_getfile(fdp, fd)) == NULL)
    988 					return (EBADF);
    989 				FILE_USE(fp);
    990 				if ((*fp->f_ops->fo_poll)(fp, flag[msk], l)) {
    991 					obits |= (1 << j);
    992 					n++;
    993 				}
    994 				FILE_UNUSE(fp, l);
    995 			}
    996 			*obitp++ = obits;
    997 		}
    998 	}
    999 	*retval = n;
   1000 	return (0);
   1001 }
   1002 
   1003 /*
   1004  * Poll system call.
   1005  */
   1006 int
   1007 sys_poll(lwp_t *l, void *v, register_t *retval)
   1008 {
   1009 	struct sys_poll_args /* {
   1010 		syscallarg(struct pollfd *)	fds;
   1011 		syscallarg(u_int)		nfds;
   1012 		syscallarg(int)			timeout;
   1013 	} */ * const uap = v;
   1014 	struct timeval	atv, *tv = NULL;
   1015 
   1016 	if (SCARG(uap, timeout) != INFTIM) {
   1017 		atv.tv_sec = SCARG(uap, timeout) / 1000;
   1018 		atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000;
   1019 		tv = &atv;
   1020 	}
   1021 
   1022 	return pollcommon(l, retval, SCARG(uap, fds), SCARG(uap, nfds),
   1023 		tv, NULL);
   1024 }
   1025 
   1026 /*
   1027  * Poll system call.
   1028  */
   1029 int
   1030 sys_pollts(lwp_t *l, void *v, register_t *retval)
   1031 {
   1032 	struct sys_pollts_args /* {
   1033 		syscallarg(struct pollfd *)		fds;
   1034 		syscallarg(u_int)			nfds;
   1035 		syscallarg(const struct timespec *)	ts;
   1036 		syscallarg(const sigset_t *)		mask;
   1037 	} */ * const uap = v;
   1038 	struct timespec	ats;
   1039 	struct timeval	atv, *tv = NULL;
   1040 	sigset_t	amask, *mask = NULL;
   1041 	int		error;
   1042 
   1043 	if (SCARG(uap, ts)) {
   1044 		error = copyin(SCARG(uap, ts), &ats, sizeof(ats));
   1045 		if (error)
   1046 			return error;
   1047 		atv.tv_sec = ats.tv_sec;
   1048 		atv.tv_usec = ats.tv_nsec / 1000;
   1049 		tv = &atv;
   1050 	}
   1051 	if (SCARG(uap, mask)) {
   1052 		error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
   1053 		if (error)
   1054 			return error;
   1055 		mask = &amask;
   1056 	}
   1057 
   1058 	return pollcommon(l, retval, SCARG(uap, fds), SCARG(uap, nfds),
   1059 		tv, mask);
   1060 }
   1061 
   1062 int
   1063 pollcommon(lwp_t *l, register_t *retval,
   1064 	struct pollfd *u_fds, u_int nfds,
   1065 	struct timeval *tv, sigset_t *mask)
   1066 {
   1067 	char		smallbits[32 * sizeof(struct pollfd)];
   1068 	proc_t		* const p = l->l_proc;
   1069 	void *		bits;
   1070 	sigset_t	oldmask;
   1071 	int		ncoll, error, timo;
   1072 	size_t		ni;
   1073 	struct timeval	sleeptv;
   1074 
   1075 	if (nfds > p->p_fd->fd_nfiles) {
   1076 		/* forgiving; slightly wrong */
   1077 		nfds = p->p_fd->fd_nfiles;
   1078 	}
   1079 	ni = nfds * sizeof(struct pollfd);
   1080 	if (ni > sizeof(smallbits))
   1081 		bits = kmem_alloc(ni, KM_SLEEP);
   1082 	else
   1083 		bits = smallbits;
   1084 
   1085 	error = copyin(u_fds, bits, ni);
   1086 	if (error)
   1087 		goto done;
   1088 
   1089 	timo = 0;
   1090 	if (tv && inittimeleft(tv, &sleeptv) == -1) {
   1091 		error = EINVAL;
   1092 		goto done;
   1093 	}
   1094 
   1095 	if (mask) {
   1096 		sigminusset(&sigcantmask, mask);
   1097 		mutex_enter(&p->p_smutex);
   1098 		oldmask = l->l_sigmask;
   1099 		l->l_sigmask = *mask;
   1100 		mutex_exit(&p->p_smutex);
   1101 	} else
   1102 		oldmask = l->l_sigmask;	/* XXXgcc */
   1103 
   1104 	mutex_enter(&select_lock);
   1105 	SLIST_INIT(&l->l_selwait);
   1106 	for (;;) {
   1107 		ncoll = nselcoll;
   1108 		l->l_selflag = SEL_SCANNING;
   1109 		mutex_exit(&select_lock);
   1110 
   1111 		error = pollscan(l, (struct pollfd *)bits, nfds, retval);
   1112 
   1113 		mutex_enter(&select_lock);
   1114 		if (error || *retval)
   1115 			break;
   1116 		if (tv && (timo = gettimeleft(tv, &sleeptv)) <= 0)
   1117 			break;
   1118 		if (l->l_selflag != SEL_SCANNING || nselcoll != ncoll)
   1119 			continue;
   1120 		l->l_selflag = SEL_BLOCKING;
   1121 		error = cv_timedwait_sig(&select_cv, &select_lock, timo);
   1122 		if (error != 0)
   1123 			break;
   1124 	}
   1125 	selclear();
   1126 	mutex_exit(&select_lock);
   1127 
   1128 	if (mask) {
   1129 		mutex_enter(&p->p_smutex);
   1130 		l->l_sigmask = oldmask;
   1131 		mutex_exit(&p->p_smutex);
   1132 	}
   1133  done:
   1134 	/* poll is not restarted after signals... */
   1135 	if (error == ERESTART)
   1136 		error = EINTR;
   1137 	if (error == EWOULDBLOCK)
   1138 		error = 0;
   1139 	if (error == 0)
   1140 		error = copyout(bits, u_fds, ni);
   1141 	if (bits != smallbits)
   1142 		kmem_free(bits, ni);
   1143 	return (error);
   1144 }
   1145 
   1146 int
   1147 pollscan(lwp_t *l, struct pollfd *fds, int nfd, register_t *retval)
   1148 {
   1149 	proc_t		*p = l->l_proc;
   1150 	struct filedesc	*fdp;
   1151 	int		i, n;
   1152 	struct file	*fp;
   1153 
   1154 	fdp = p->p_fd;
   1155 	n = 0;
   1156 	for (i = 0; i < nfd; i++, fds++) {
   1157 		if (fds->fd >= fdp->fd_nfiles) {
   1158 			fds->revents = POLLNVAL;
   1159 			n++;
   1160 		} else if (fds->fd < 0) {
   1161 			fds->revents = 0;
   1162 		} else {
   1163 			if ((fp = fd_getfile(fdp, fds->fd)) == NULL) {
   1164 				fds->revents = POLLNVAL;
   1165 				n++;
   1166 			} else {
   1167 				FILE_USE(fp);
   1168 				fds->revents = (*fp->f_ops->fo_poll)(fp,
   1169 				    fds->events | POLLERR | POLLHUP, l);
   1170 				if (fds->revents != 0)
   1171 					n++;
   1172 				FILE_UNUSE(fp, l);
   1173 			}
   1174 		}
   1175 	}
   1176 	*retval = n;
   1177 	return (0);
   1178 }
   1179 
   1180 /*ARGSUSED*/
   1181 int
   1182 seltrue(dev_t dev, int events, lwp_t *l)
   1183 {
   1184 
   1185 	return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
   1186 }
   1187 
   1188 /*
   1189  * Record a select request.
   1190  */
   1191 void
   1192 selrecord(lwp_t *selector, struct selinfo *sip)
   1193 {
   1194 
   1195 	mutex_enter(&select_lock);
   1196 	if (sip->sel_lwp == NULL) {
   1197 		/* First named waiter, although there may be more. */
   1198 		sip->sel_lwp = selector;
   1199 		SLIST_INSERT_HEAD(&selector->l_selwait, sip, sel_chain);
   1200 	} else if (sip->sel_lwp != selector) {
   1201 		/* Multiple waiters. */
   1202 		sip->sel_collision = true;
   1203 	}
   1204 	mutex_exit(&select_lock);
   1205 }
   1206 
   1207 /*
   1208  * Do a wakeup when a selectable event occurs.
   1209  */
   1210 void
   1211 selwakeup(struct selinfo *sip)
   1212 {
   1213 	lwp_t *l;
   1214 
   1215 	mutex_enter(&select_lock);
   1216 	if (sip->sel_collision) {
   1217 		/* Multiple waiters - just notify everybody. */
   1218 		nselcoll++;
   1219 		sip->sel_collision = false;
   1220 		cv_broadcast(&select_cv);
   1221 	} else if (sip->sel_lwp != NULL) {
   1222 		/* Only one LWP waiting. */
   1223 		l = sip->sel_lwp;
   1224 		if (l->l_selflag == SEL_BLOCKING) {
   1225 			/*
   1226 			 * If it's sleeping, wake it up.  If not, it's
   1227 			 * already awake but hasn't yet removed itself
   1228 			 * from the selector.  We reset the state below
   1229 			 * so that we only attempt to do this once.
   1230 			 */
   1231 			lwp_lock(l);
   1232 			if (l->l_wchan == &select_cv) {
   1233 				/* lwp_unsleep() releases the LWP lock. */
   1234 				lwp_unsleep(l);
   1235 			} else
   1236 				lwp_unlock(l);
   1237 		} else {
   1238 			/*
   1239 			 * Not yet asleep.  Reset its state below so that
   1240 			 * it will go around again.
   1241 			 */
   1242 		}
   1243 		l->l_selflag = SEL_RESET;
   1244 	}
   1245 	mutex_exit(&select_lock);
   1246 }
   1247 
   1248 void
   1249 selnotify(struct selinfo *sip, long knhint)
   1250 {
   1251 
   1252 	selwakeup(sip);
   1253 	KNOTE(&sip->sel_klist, knhint);
   1254 }
   1255 
   1256 /*
   1257  * Remove an LWP from all objects that it is waiting for.
   1258  */
   1259 static void
   1260 selclear(void)
   1261 {
   1262 	struct selinfo *sip;
   1263 	lwp_t *l = curlwp;
   1264 
   1265 	KASSERT(mutex_owned(&select_lock));
   1266 
   1267 	SLIST_FOREACH(sip, &l->l_selwait, sel_chain) {
   1268 		KASSERT(sip->sel_lwp == l);
   1269 		sip->sel_lwp = NULL;
   1270 	}
   1271 }
   1272 
   1273 /*
   1274  * Initialize the select/poll system calls.
   1275  */
   1276 void
   1277 selsysinit(void)
   1278 {
   1279 
   1280 	mutex_init(&select_lock, MUTEX_DRIVER, IPL_VM);
   1281 	cv_init(&select_cv, "select");
   1282 }
   1283