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