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