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