Home | History | Annotate | Line # | Download | only in kern
sys_generic.c revision 1.70
      1  1.70        pk /*	$NetBSD: sys_generic.c,v 1.70 2003/02/23 14:37:34 pk Exp $	*/
      2  1.15       cgd 
      3  1.15       cgd /*
      4  1.15       cgd  * Copyright (c) 1982, 1986, 1989, 1993
      5  1.15       cgd  *	The Regents of the University of California.  All rights reserved.
      6  1.15       cgd  * (c) UNIX System Laboratories, Inc.
      7  1.15       cgd  * All or some portions of this file are derived from material licensed
      8  1.15       cgd  * to the University of California by American Telephone and Telegraph
      9  1.15       cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  1.15       cgd  * the permission of UNIX System Laboratories, Inc.
     11  1.15       cgd  *
     12  1.15       cgd  * Redistribution and use in source and binary forms, with or without
     13  1.15       cgd  * modification, are permitted provided that the following conditions
     14  1.15       cgd  * are met:
     15  1.15       cgd  * 1. Redistributions of source code must retain the above copyright
     16  1.15       cgd  *    notice, this list of conditions and the following disclaimer.
     17  1.15       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.15       cgd  *    notice, this list of conditions and the following disclaimer in the
     19  1.15       cgd  *    documentation and/or other materials provided with the distribution.
     20  1.15       cgd  * 3. All advertising materials mentioning features or use of this software
     21  1.15       cgd  *    must display the following acknowledgement:
     22  1.15       cgd  *	This product includes software developed by the University of
     23  1.15       cgd  *	California, Berkeley and its contributors.
     24  1.15       cgd  * 4. Neither the name of the University nor the names of its contributors
     25  1.15       cgd  *    may be used to endorse or promote products derived from this software
     26  1.15       cgd  *    without specific prior written permission.
     27  1.15       cgd  *
     28  1.15       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  1.15       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  1.15       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  1.15       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  1.15       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  1.15       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  1.15       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  1.15       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  1.15       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  1.15       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  1.15       cgd  * SUCH DAMAGE.
     39  1.15       cgd  *
     40  1.36      fvdl  *	@(#)sys_generic.c	8.9 (Berkeley) 2/14/95
     41  1.15       cgd  */
     42  1.59     lukem 
     43  1.59     lukem #include <sys/cdefs.h>
     44  1.70        pk __KERNEL_RCSID(0, "$NetBSD: sys_generic.c,v 1.70 2003/02/23 14:37:34 pk Exp $");
     45  1.37   thorpej 
     46  1.37   thorpej #include "opt_ktrace.h"
     47  1.15       cgd 
     48  1.15       cgd #include <sys/param.h>
     49  1.15       cgd #include <sys/systm.h>
     50  1.15       cgd #include <sys/filedesc.h>
     51  1.15       cgd #include <sys/ioctl.h>
     52  1.15       cgd #include <sys/file.h>
     53  1.15       cgd #include <sys/proc.h>
     54  1.15       cgd #include <sys/socketvar.h>
     55  1.22  christos #include <sys/signalvar.h>
     56  1.15       cgd #include <sys/uio.h>
     57  1.15       cgd #include <sys/kernel.h>
     58  1.15       cgd #include <sys/stat.h>
     59  1.15       cgd #include <sys/malloc.h>
     60  1.28   mycroft #include <sys/poll.h>
     61  1.15       cgd #ifdef KTRACE
     62  1.15       cgd #include <sys/ktrace.h>
     63  1.15       cgd #endif
     64  1.15       cgd 
     65  1.16       cgd #include <sys/mount.h>
     66  1.69   thorpej #include <sys/sa.h>
     67  1.16       cgd #include <sys/syscallargs.h>
     68  1.22  christos 
     69  1.25   mycroft int selscan __P((struct proc *, fd_mask *, fd_mask *, int, register_t *));
     70  1.28   mycroft int pollscan __P((struct proc *, struct pollfd *, int, register_t *));
     71  1.22  christos 
     72  1.15       cgd /*
     73  1.15       cgd  * Read system call.
     74  1.15       cgd  */
     75  1.15       cgd /* ARGSUSED */
     76  1.22  christos int
     77  1.69   thorpej sys_read(struct lwp *l, void *v, register_t *retval)
     78  1.20   thorpej {
     79  1.47  augustss 	struct sys_read_args /* {
     80  1.53     lukem 		syscallarg(int)		fd;
     81  1.53     lukem 		syscallarg(void *)	buf;
     82  1.53     lukem 		syscallarg(size_t)	nbyte;
     83  1.20   thorpej 	} */ *uap = v;
     84  1.53     lukem 	int		fd;
     85  1.53     lukem 	struct file	*fp;
     86  1.69   thorpej 	struct proc	*p;
     87  1.53     lukem 	struct filedesc	*fdp;
     88  1.39   thorpej 
     89  1.53     lukem 	fd = SCARG(uap, fd);
     90  1.69   thorpej 	p = l->l_proc;
     91  1.53     lukem 	fdp = p->p_fd;
     92  1.56   thorpej 
     93  1.56   thorpej 	if ((fp = fd_getfile(fdp, fd)) == NULL)
     94  1.56   thorpej 		return (EBADF);
     95  1.56   thorpej 
     96  1.70        pk 	if ((fp->f_flag & FREAD) == 0) {
     97  1.70        pk 		simple_unlock(&fp->f_slock);
     98  1.39   thorpej 		return (EBADF);
     99  1.70        pk 	}
    100  1.39   thorpej 
    101  1.45   thorpej 	FILE_USE(fp);
    102  1.45   thorpej 
    103  1.45   thorpej 	/* dofileread() will unuse the descriptor for us */
    104  1.39   thorpej 	return (dofileread(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
    105  1.39   thorpej 	    &fp->f_offset, FOF_UPDATE_OFFSET, retval));
    106  1.39   thorpej }
    107  1.39   thorpej 
    108  1.39   thorpej int
    109  1.53     lukem dofileread(struct proc *p, int fd, struct file *fp, void *buf, size_t nbyte,
    110  1.53     lukem 	off_t *offset, int flags, register_t *retval)
    111  1.53     lukem {
    112  1.53     lukem 	struct uio	auio;
    113  1.53     lukem 	struct iovec	aiov;
    114  1.64   thorpej 	size_t		cnt;
    115  1.64   thorpej 	int		error;
    116  1.15       cgd #ifdef KTRACE
    117  1.53     lukem 	struct iovec	ktriov;
    118  1.15       cgd #endif
    119  1.53     lukem 	error = 0;
    120  1.15       cgd 
    121  1.39   thorpej 	aiov.iov_base = (caddr_t)buf;
    122  1.39   thorpej 	aiov.iov_len = nbyte;
    123  1.15       cgd 	auio.uio_iov = &aiov;
    124  1.15       cgd 	auio.uio_iovcnt = 1;
    125  1.39   thorpej 	auio.uio_resid = nbyte;
    126  1.15       cgd 	auio.uio_rw = UIO_READ;
    127  1.15       cgd 	auio.uio_segflg = UIO_USERSPACE;
    128  1.15       cgd 	auio.uio_procp = p;
    129  1.40   thorpej 
    130  1.40   thorpej 	/*
    131  1.40   thorpej 	 * Reads return ssize_t because -1 is returned on error.  Therefore
    132  1.40   thorpej 	 * we must restrict the length to SSIZE_MAX to avoid garbage return
    133  1.40   thorpej 	 * values.
    134  1.40   thorpej 	 */
    135  1.45   thorpej 	if (auio.uio_resid > SSIZE_MAX) {
    136  1.45   thorpej 		error = EINVAL;
    137  1.45   thorpej 		goto out;
    138  1.45   thorpej 	}
    139  1.40   thorpej 
    140  1.15       cgd #ifdef KTRACE
    141  1.15       cgd 	/*
    142  1.15       cgd 	 * if tracing, save a copy of iovec
    143  1.15       cgd 	 */
    144  1.15       cgd 	if (KTRPOINT(p, KTR_GENIO))
    145  1.15       cgd 		ktriov = aiov;
    146  1.15       cgd #endif
    147  1.38   thorpej 	cnt = auio.uio_resid;
    148  1.39   thorpej 	error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
    149  1.22  christos 	if (error)
    150  1.15       cgd 		if (auio.uio_resid != cnt && (error == ERESTART ||
    151  1.15       cgd 		    error == EINTR || error == EWOULDBLOCK))
    152  1.15       cgd 			error = 0;
    153  1.15       cgd 	cnt -= auio.uio_resid;
    154  1.15       cgd #ifdef KTRACE
    155  1.15       cgd 	if (KTRPOINT(p, KTR_GENIO) && error == 0)
    156  1.48  sommerfe 		ktrgenio(p, fd, UIO_READ, &ktriov, cnt, error);
    157  1.15       cgd #endif
    158  1.15       cgd 	*retval = cnt;
    159  1.45   thorpej  out:
    160  1.45   thorpej 	FILE_UNUSE(fp, p);
    161  1.15       cgd 	return (error);
    162  1.15       cgd }
    163  1.15       cgd 
    164  1.15       cgd /*
    165  1.15       cgd  * Scatter read system call.
    166  1.15       cgd  */
    167  1.22  christos int
    168  1.69   thorpej sys_readv(struct lwp *l, void *v, register_t *retval)
    169  1.20   thorpej {
    170  1.47  augustss 	struct sys_readv_args /* {
    171  1.53     lukem 		syscallarg(int)				fd;
    172  1.53     lukem 		syscallarg(const struct iovec *)	iovp;
    173  1.53     lukem 		syscallarg(int)				iovcnt;
    174  1.20   thorpej 	} */ *uap = v;
    175  1.53     lukem 	int		fd;
    176  1.53     lukem 	struct file	*fp;
    177  1.69   thorpej 	struct proc	*p;
    178  1.53     lukem 	struct filedesc	*fdp;
    179  1.39   thorpej 
    180  1.53     lukem 	fd = SCARG(uap, fd);
    181  1.69   thorpej 	p = l->l_proc;
    182  1.53     lukem 	fdp = p->p_fd;
    183  1.56   thorpej 
    184  1.56   thorpej 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    185  1.56   thorpej 		return (EBADF);
    186  1.56   thorpej 
    187  1.70        pk 	if ((fp->f_flag & FREAD) == 0) {
    188  1.70        pk 		simple_unlock(&fp->f_slock);
    189  1.39   thorpej 		return (EBADF);
    190  1.70        pk 	}
    191  1.39   thorpej 
    192  1.45   thorpej 	FILE_USE(fp);
    193  1.45   thorpej 
    194  1.45   thorpej 	/* dofilereadv() will unuse the descriptor for us */
    195  1.39   thorpej 	return (dofilereadv(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
    196  1.39   thorpej 	    &fp->f_offset, FOF_UPDATE_OFFSET, retval));
    197  1.39   thorpej }
    198  1.39   thorpej 
    199  1.39   thorpej int
    200  1.53     lukem dofilereadv(struct proc *p, int fd, struct file *fp, const struct iovec *iovp,
    201  1.53     lukem 	int iovcnt, off_t *offset, int flags, register_t *retval)
    202  1.53     lukem {
    203  1.53     lukem 	struct uio	auio;
    204  1.53     lukem 	struct iovec	*iov, *needfree, aiov[UIO_SMALLIOV];
    205  1.64   thorpej 	int		i, error;
    206  1.64   thorpej 	size_t		cnt;
    207  1.53     lukem 	u_int		iovlen;
    208  1.15       cgd #ifdef KTRACE
    209  1.53     lukem 	struct iovec	*ktriov;
    210  1.15       cgd #endif
    211  1.15       cgd 
    212  1.53     lukem 	error = 0;
    213  1.53     lukem #ifdef KTRACE
    214  1.53     lukem 	ktriov = NULL;
    215  1.53     lukem #endif
    216  1.15       cgd 	/* note: can't use iovlen until iovcnt is validated */
    217  1.42     perry 	iovlen = iovcnt * sizeof(struct iovec);
    218  1.34   mycroft 	if ((u_int)iovcnt > UIO_SMALLIOV) {
    219  1.45   thorpej 		if ((u_int)iovcnt > IOV_MAX) {
    220  1.45   thorpej 			error = EINVAL;
    221  1.45   thorpej 			goto out;
    222  1.45   thorpej 		}
    223  1.50   thorpej 		iov = malloc(iovlen, M_IOV, M_WAITOK);
    224  1.15       cgd 		needfree = iov;
    225  1.41    kleink 	} else if ((u_int)iovcnt > 0) {
    226  1.15       cgd 		iov = aiov;
    227  1.15       cgd 		needfree = NULL;
    228  1.45   thorpej 	} else {
    229  1.45   thorpej 		error = EINVAL;
    230  1.45   thorpej 		goto out;
    231  1.45   thorpej 	}
    232  1.41    kleink 
    233  1.15       cgd 	auio.uio_iov = iov;
    234  1.34   mycroft 	auio.uio_iovcnt = iovcnt;
    235  1.15       cgd 	auio.uio_rw = UIO_READ;
    236  1.15       cgd 	auio.uio_segflg = UIO_USERSPACE;
    237  1.15       cgd 	auio.uio_procp = p;
    238  1.39   thorpej 	error = copyin(iovp, iov, iovlen);
    239  1.22  christos 	if (error)
    240  1.15       cgd 		goto done;
    241  1.15       cgd 	auio.uio_resid = 0;
    242  1.34   mycroft 	for (i = 0; i < iovcnt; i++) {
    243  1.15       cgd 		auio.uio_resid += iov->iov_len;
    244  1.40   thorpej 		/*
    245  1.40   thorpej 		 * Reads return ssize_t because -1 is returned on error.
    246  1.40   thorpej 		 * Therefore we must restrict the length to SSIZE_MAX to
    247  1.40   thorpej 		 * avoid garbage return values.
    248  1.40   thorpej 		 */
    249  1.40   thorpej 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    250  1.15       cgd 			error = EINVAL;
    251  1.15       cgd 			goto done;
    252  1.15       cgd 		}
    253  1.15       cgd 		iov++;
    254  1.15       cgd 	}
    255  1.15       cgd #ifdef KTRACE
    256  1.15       cgd 	/*
    257  1.15       cgd 	 * if tracing, save a copy of iovec
    258  1.15       cgd 	 */
    259  1.15       cgd 	if (KTRPOINT(p, KTR_GENIO))  {
    260  1.50   thorpej 		ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
    261  1.44     perry 		memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
    262  1.15       cgd 	}
    263  1.15       cgd #endif
    264  1.15       cgd 	cnt = auio.uio_resid;
    265  1.39   thorpej 	error = (*fp->f_ops->fo_read)(fp, offset, &auio, fp->f_cred, flags);
    266  1.22  christos 	if (error)
    267  1.15       cgd 		if (auio.uio_resid != cnt && (error == ERESTART ||
    268  1.15       cgd 		    error == EINTR || error == EWOULDBLOCK))
    269  1.15       cgd 			error = 0;
    270  1.15       cgd 	cnt -= auio.uio_resid;
    271  1.15       cgd #ifdef KTRACE
    272  1.58     itohy 	if (ktriov != NULL) {
    273  1.58     itohy 		if (error == 0)
    274  1.48  sommerfe 			ktrgenio(p, fd, UIO_READ, ktriov, cnt, error);
    275  1.50   thorpej 		free(ktriov, M_TEMP);
    276  1.15       cgd 	}
    277  1.15       cgd #endif
    278  1.15       cgd 	*retval = cnt;
    279  1.45   thorpej  done:
    280  1.15       cgd 	if (needfree)
    281  1.50   thorpej 		free(needfree, M_IOV);
    282  1.45   thorpej  out:
    283  1.45   thorpej 	FILE_UNUSE(fp, p);
    284  1.15       cgd 	return (error);
    285  1.15       cgd }
    286  1.15       cgd 
    287  1.15       cgd /*
    288  1.15       cgd  * Write system call
    289  1.15       cgd  */
    290  1.22  christos int
    291  1.69   thorpej sys_write(struct lwp *l, void *v, register_t *retval)
    292  1.20   thorpej {
    293  1.47  augustss 	struct sys_write_args /* {
    294  1.53     lukem 		syscallarg(int)			fd;
    295  1.53     lukem 		syscallarg(const void *)	buf;
    296  1.53     lukem 		syscallarg(size_t)		nbyte;
    297  1.20   thorpej 	} */ *uap = v;
    298  1.53     lukem 	int		fd;
    299  1.53     lukem 	struct file	*fp;
    300  1.69   thorpej 	struct proc	*p;
    301  1.53     lukem 	struct filedesc	*fdp;
    302  1.39   thorpej 
    303  1.53     lukem 	fd = SCARG(uap, fd);
    304  1.69   thorpej 	p = l->l_proc;
    305  1.53     lukem 	fdp = p->p_fd;
    306  1.56   thorpej 
    307  1.56   thorpej 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    308  1.56   thorpej 		return (EBADF);
    309  1.56   thorpej 
    310  1.70        pk 	if ((fp->f_flag & FWRITE) == 0) {
    311  1.70        pk 		simple_unlock(&fp->f_slock);
    312  1.39   thorpej 		return (EBADF);
    313  1.70        pk 	}
    314  1.39   thorpej 
    315  1.45   thorpej 	FILE_USE(fp);
    316  1.45   thorpej 
    317  1.45   thorpej 	/* dofilewrite() will unuse the descriptor for us */
    318  1.39   thorpej 	return (dofilewrite(p, fd, fp, SCARG(uap, buf), SCARG(uap, nbyte),
    319  1.39   thorpej 	    &fp->f_offset, FOF_UPDATE_OFFSET, retval));
    320  1.39   thorpej }
    321  1.39   thorpej 
    322  1.39   thorpej int
    323  1.53     lukem dofilewrite(struct proc *p, int fd, struct file *fp, const void *buf,
    324  1.53     lukem 	size_t nbyte, off_t *offset, int flags, register_t *retval)
    325  1.53     lukem {
    326  1.53     lukem 	struct uio	auio;
    327  1.53     lukem 	struct iovec	aiov;
    328  1.64   thorpej 	size_t		cnt;
    329  1.64   thorpej 	int		error;
    330  1.15       cgd #ifdef KTRACE
    331  1.53     lukem 	struct iovec	ktriov;
    332  1.15       cgd #endif
    333  1.15       cgd 
    334  1.53     lukem 	error = 0;
    335  1.39   thorpej 	aiov.iov_base = (caddr_t)buf;		/* XXX kills const */
    336  1.39   thorpej 	aiov.iov_len = nbyte;
    337  1.15       cgd 	auio.uio_iov = &aiov;
    338  1.15       cgd 	auio.uio_iovcnt = 1;
    339  1.39   thorpej 	auio.uio_resid = nbyte;
    340  1.15       cgd 	auio.uio_rw = UIO_WRITE;
    341  1.15       cgd 	auio.uio_segflg = UIO_USERSPACE;
    342  1.15       cgd 	auio.uio_procp = p;
    343  1.40   thorpej 
    344  1.40   thorpej 	/*
    345  1.40   thorpej 	 * Writes return ssize_t because -1 is returned on error.  Therefore
    346  1.40   thorpej 	 * we must restrict the length to SSIZE_MAX to avoid garbage return
    347  1.40   thorpej 	 * values.
    348  1.40   thorpej 	 */
    349  1.45   thorpej 	if (auio.uio_resid > SSIZE_MAX) {
    350  1.45   thorpej 		error = EINVAL;
    351  1.45   thorpej 		goto out;
    352  1.45   thorpej 	}
    353  1.40   thorpej 
    354  1.15       cgd #ifdef KTRACE
    355  1.15       cgd 	/*
    356  1.15       cgd 	 * if tracing, save a copy of iovec
    357  1.15       cgd 	 */
    358  1.15       cgd 	if (KTRPOINT(p, KTR_GENIO))
    359  1.15       cgd 		ktriov = aiov;
    360  1.15       cgd #endif
    361  1.38   thorpej 	cnt = auio.uio_resid;
    362  1.39   thorpej 	error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
    363  1.22  christos 	if (error) {
    364  1.15       cgd 		if (auio.uio_resid != cnt && (error == ERESTART ||
    365  1.15       cgd 		    error == EINTR || error == EWOULDBLOCK))
    366  1.15       cgd 			error = 0;
    367  1.15       cgd 		if (error == EPIPE)
    368  1.15       cgd 			psignal(p, SIGPIPE);
    369  1.15       cgd 	}
    370  1.15       cgd 	cnt -= auio.uio_resid;
    371  1.15       cgd #ifdef KTRACE
    372  1.15       cgd 	if (KTRPOINT(p, KTR_GENIO) && error == 0)
    373  1.48  sommerfe 		ktrgenio(p, fd, UIO_WRITE, &ktriov, cnt, error);
    374  1.15       cgd #endif
    375  1.15       cgd 	*retval = cnt;
    376  1.45   thorpej  out:
    377  1.45   thorpej 	FILE_UNUSE(fp, p);
    378  1.15       cgd 	return (error);
    379  1.15       cgd }
    380  1.15       cgd 
    381  1.15       cgd /*
    382  1.15       cgd  * Gather write system call
    383  1.15       cgd  */
    384  1.22  christos int
    385  1.69   thorpej sys_writev(struct lwp *l, void *v, register_t *retval)
    386  1.20   thorpej {
    387  1.47  augustss 	struct sys_writev_args /* {
    388  1.53     lukem 		syscallarg(int)				fd;
    389  1.53     lukem 		syscallarg(const struct iovec *)	iovp;
    390  1.53     lukem 		syscallarg(int)				iovcnt;
    391  1.20   thorpej 	} */ *uap = v;
    392  1.53     lukem 	int		fd;
    393  1.53     lukem 	struct file	*fp;
    394  1.69   thorpej 	struct proc	*p;
    395  1.53     lukem 	struct filedesc	*fdp;
    396  1.39   thorpej 
    397  1.53     lukem 	fd = SCARG(uap, fd);
    398  1.69   thorpej 	p = l->l_proc;
    399  1.53     lukem 	fdp = p->p_fd;
    400  1.56   thorpej 
    401  1.56   thorpej 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    402  1.56   thorpej 		return (EBADF);
    403  1.56   thorpej 
    404  1.70        pk 	if ((fp->f_flag & FWRITE) == 0) {
    405  1.70        pk 		simple_unlock(&fp->f_slock);
    406  1.39   thorpej 		return (EBADF);
    407  1.70        pk 	}
    408  1.39   thorpej 
    409  1.45   thorpej 	FILE_USE(fp);
    410  1.45   thorpej 
    411  1.45   thorpej 	/* dofilewritev() will unuse the descriptor for us */
    412  1.39   thorpej 	return (dofilewritev(p, fd, fp, SCARG(uap, iovp), SCARG(uap, iovcnt),
    413  1.39   thorpej 	    &fp->f_offset, FOF_UPDATE_OFFSET, retval));
    414  1.39   thorpej }
    415  1.39   thorpej 
    416  1.39   thorpej int
    417  1.53     lukem dofilewritev(struct proc *p, int fd, struct file *fp, const struct iovec *iovp,
    418  1.53     lukem 	int iovcnt, off_t *offset, int flags, register_t *retval)
    419  1.53     lukem {
    420  1.53     lukem 	struct uio	auio;
    421  1.53     lukem 	struct iovec	*iov, *needfree, aiov[UIO_SMALLIOV];
    422  1.64   thorpej 	int		i, error;
    423  1.64   thorpej 	size_t		cnt;
    424  1.53     lukem 	u_int		iovlen;
    425  1.15       cgd #ifdef KTRACE
    426  1.53     lukem 	struct iovec	*ktriov;
    427  1.15       cgd #endif
    428  1.15       cgd 
    429  1.53     lukem 	error = 0;
    430  1.53     lukem #ifdef KTRACE
    431  1.53     lukem 	ktriov = NULL;
    432  1.53     lukem #endif
    433  1.15       cgd 	/* note: can't use iovlen until iovcnt is validated */
    434  1.42     perry 	iovlen = iovcnt * sizeof(struct iovec);
    435  1.34   mycroft 	if ((u_int)iovcnt > UIO_SMALLIOV) {
    436  1.62  jdolecek 		if ((u_int)iovcnt > IOV_MAX) {
    437  1.62  jdolecek 			error = EINVAL;
    438  1.62  jdolecek 			goto out;
    439  1.62  jdolecek 		}
    440  1.50   thorpej 		iov = malloc(iovlen, M_IOV, M_WAITOK);
    441  1.15       cgd 		needfree = iov;
    442  1.41    kleink 	} else if ((u_int)iovcnt > 0) {
    443  1.15       cgd 		iov = aiov;
    444  1.15       cgd 		needfree = NULL;
    445  1.45   thorpej 	} else {
    446  1.45   thorpej 		error = EINVAL;
    447  1.45   thorpej 		goto out;
    448  1.45   thorpej 	}
    449  1.41    kleink 
    450  1.15       cgd 	auio.uio_iov = iov;
    451  1.34   mycroft 	auio.uio_iovcnt = iovcnt;
    452  1.15       cgd 	auio.uio_rw = UIO_WRITE;
    453  1.15       cgd 	auio.uio_segflg = UIO_USERSPACE;
    454  1.15       cgd 	auio.uio_procp = p;
    455  1.39   thorpej 	error = copyin(iovp, iov, iovlen);
    456  1.22  christos 	if (error)
    457  1.15       cgd 		goto done;
    458  1.15       cgd 	auio.uio_resid = 0;
    459  1.34   mycroft 	for (i = 0; i < iovcnt; i++) {
    460  1.15       cgd 		auio.uio_resid += iov->iov_len;
    461  1.40   thorpej 		/*
    462  1.40   thorpej 		 * Writes return ssize_t because -1 is returned on error.
    463  1.40   thorpej 		 * Therefore we must restrict the length to SSIZE_MAX to
    464  1.40   thorpej 		 * avoid garbage return values.
    465  1.40   thorpej 		 */
    466  1.40   thorpej 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    467  1.15       cgd 			error = EINVAL;
    468  1.15       cgd 			goto done;
    469  1.15       cgd 		}
    470  1.15       cgd 		iov++;
    471  1.15       cgd 	}
    472  1.15       cgd #ifdef KTRACE
    473  1.15       cgd 	/*
    474  1.15       cgd 	 * if tracing, save a copy of iovec
    475  1.15       cgd 	 */
    476  1.15       cgd 	if (KTRPOINT(p, KTR_GENIO))  {
    477  1.50   thorpej 		ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
    478  1.44     perry 		memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
    479  1.15       cgd 	}
    480  1.15       cgd #endif
    481  1.15       cgd 	cnt = auio.uio_resid;
    482  1.39   thorpej 	error = (*fp->f_ops->fo_write)(fp, offset, &auio, fp->f_cred, flags);
    483  1.22  christos 	if (error) {
    484  1.15       cgd 		if (auio.uio_resid != cnt && (error == ERESTART ||
    485  1.15       cgd 		    error == EINTR || error == EWOULDBLOCK))
    486  1.15       cgd 			error = 0;
    487  1.15       cgd 		if (error == EPIPE)
    488  1.15       cgd 			psignal(p, SIGPIPE);
    489  1.15       cgd 	}
    490  1.15       cgd 	cnt -= auio.uio_resid;
    491  1.15       cgd #ifdef KTRACE
    492  1.34   mycroft 	if (KTRPOINT(p, KTR_GENIO))
    493  1.34   mycroft 		if (error == 0) {
    494  1.48  sommerfe 			ktrgenio(p, fd, UIO_WRITE, ktriov, cnt, error);
    495  1.50   thorpej 		free(ktriov, M_TEMP);
    496  1.15       cgd 	}
    497  1.15       cgd #endif
    498  1.15       cgd 	*retval = cnt;
    499  1.45   thorpej  done:
    500  1.15       cgd 	if (needfree)
    501  1.50   thorpej 		free(needfree, M_IOV);
    502  1.45   thorpej  out:
    503  1.45   thorpej 	FILE_UNUSE(fp, p);
    504  1.15       cgd 	return (error);
    505  1.15       cgd }
    506  1.15       cgd 
    507  1.15       cgd /*
    508  1.15       cgd  * Ioctl system call
    509  1.15       cgd  */
    510  1.15       cgd /* ARGSUSED */
    511  1.22  christos int
    512  1.69   thorpej sys_ioctl(struct lwp *l, void *v, register_t *retval)
    513  1.20   thorpej {
    514  1.47  augustss 	struct sys_ioctl_args /* {
    515  1.53     lukem 		syscallarg(int)		fd;
    516  1.53     lukem 		syscallarg(u_long)	com;
    517  1.53     lukem 		syscallarg(caddr_t)	data;
    518  1.20   thorpej 	} */ *uap = v;
    519  1.53     lukem 	struct file	*fp;
    520  1.69   thorpej 	struct proc	*p;
    521  1.53     lukem 	struct filedesc	*fdp;
    522  1.53     lukem 	u_long		com;
    523  1.53     lukem 	int		error;
    524  1.53     lukem 	u_int		size;
    525  1.53     lukem 	caddr_t		data, memp;
    526  1.53     lukem 	int		tmp;
    527  1.53     lukem #define	STK_PARAMS	128
    528  1.53     lukem 	u_long		stkbuf[STK_PARAMS/sizeof(u_long)];
    529  1.15       cgd 
    530  1.53     lukem 	error = 0;
    531  1.69   thorpej 	p = l->l_proc;
    532  1.15       cgd 	fdp = p->p_fd;
    533  1.56   thorpej 
    534  1.56   thorpej 	if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
    535  1.15       cgd 		return (EBADF);
    536  1.15       cgd 
    537  1.45   thorpej 	FILE_USE(fp);
    538  1.45   thorpej 
    539  1.45   thorpej 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
    540  1.45   thorpej 		error = EBADF;
    541  1.65       scw 		com = 0;
    542  1.45   thorpej 		goto out;
    543  1.45   thorpej 	}
    544  1.15       cgd 
    545  1.16       cgd 	switch (com = SCARG(uap, com)) {
    546  1.15       cgd 	case FIONCLEX:
    547  1.16       cgd 		fdp->fd_ofileflags[SCARG(uap, fd)] &= ~UF_EXCLOSE;
    548  1.45   thorpej 		goto out;
    549  1.45   thorpej 
    550  1.15       cgd 	case FIOCLEX:
    551  1.16       cgd 		fdp->fd_ofileflags[SCARG(uap, fd)] |= UF_EXCLOSE;
    552  1.45   thorpej 		goto out;
    553  1.15       cgd 	}
    554  1.15       cgd 
    555  1.15       cgd 	/*
    556  1.15       cgd 	 * Interpret high order word to find amount of data to be
    557  1.15       cgd 	 * copied to/from the user's address space.
    558  1.15       cgd 	 */
    559  1.15       cgd 	size = IOCPARM_LEN(com);
    560  1.45   thorpej 	if (size > IOCPARM_MAX) {
    561  1.45   thorpej 		error = ENOTTY;
    562  1.45   thorpej 		goto out;
    563  1.45   thorpej 	}
    564  1.15       cgd 	memp = NULL;
    565  1.42     perry 	if (size > sizeof(stkbuf)) {
    566  1.15       cgd 		memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
    567  1.15       cgd 		data = memp;
    568  1.15       cgd 	} else
    569  1.46   darrenr 		data = (caddr_t)stkbuf;
    570  1.15       cgd 	if (com&IOC_IN) {
    571  1.15       cgd 		if (size) {
    572  1.31       cgd 			error = copyin(SCARG(uap, data), data, size);
    573  1.15       cgd 			if (error) {
    574  1.15       cgd 				if (memp)
    575  1.15       cgd 					free(memp, M_IOCTLOPS);
    576  1.45   thorpej 				goto out;
    577  1.15       cgd 			}
    578  1.15       cgd 		} else
    579  1.16       cgd 			*(caddr_t *)data = SCARG(uap, data);
    580  1.15       cgd 	} else if ((com&IOC_OUT) && size)
    581  1.15       cgd 		/*
    582  1.15       cgd 		 * Zero the buffer so the user always
    583  1.15       cgd 		 * gets back something deterministic.
    584  1.15       cgd 		 */
    585  1.44     perry 		memset(data, 0, size);
    586  1.15       cgd 	else if (com&IOC_VOID)
    587  1.16       cgd 		*(caddr_t *)data = SCARG(uap, data);
    588  1.15       cgd 
    589  1.15       cgd 	switch (com) {
    590  1.15       cgd 
    591  1.15       cgd 	case FIONBIO:
    592  1.22  christos 		if ((tmp = *(int *)data) != 0)
    593  1.15       cgd 			fp->f_flag |= FNONBLOCK;
    594  1.15       cgd 		else
    595  1.15       cgd 			fp->f_flag &= ~FNONBLOCK;
    596  1.15       cgd 		error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
    597  1.15       cgd 		break;
    598  1.15       cgd 
    599  1.15       cgd 	case FIOASYNC:
    600  1.22  christos 		if ((tmp = *(int *)data) != 0)
    601  1.15       cgd 			fp->f_flag |= FASYNC;
    602  1.15       cgd 		else
    603  1.15       cgd 			fp->f_flag &= ~FASYNC;
    604  1.15       cgd 		error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
    605  1.15       cgd 		break;
    606  1.15       cgd 
    607  1.15       cgd 	case FIOSETOWN:
    608  1.15       cgd 		tmp = *(int *)data;
    609  1.15       cgd 		if (fp->f_type == DTYPE_SOCKET) {
    610  1.15       cgd 			((struct socket *)fp->f_data)->so_pgid = tmp;
    611  1.15       cgd 			error = 0;
    612  1.15       cgd 			break;
    613  1.15       cgd 		}
    614  1.15       cgd 		if (tmp <= 0) {
    615  1.15       cgd 			tmp = -tmp;
    616  1.15       cgd 		} else {
    617  1.15       cgd 			struct proc *p1 = pfind(tmp);
    618  1.15       cgd 			if (p1 == 0) {
    619  1.15       cgd 				error = ESRCH;
    620  1.15       cgd 				break;
    621  1.15       cgd 			}
    622  1.15       cgd 			tmp = p1->p_pgrp->pg_id;
    623  1.15       cgd 		}
    624  1.15       cgd 		error = (*fp->f_ops->fo_ioctl)
    625  1.24       cgd 			(fp, TIOCSPGRP, (caddr_t)&tmp, p);
    626  1.15       cgd 		break;
    627  1.15       cgd 
    628  1.15       cgd 	case FIOGETOWN:
    629  1.15       cgd 		if (fp->f_type == DTYPE_SOCKET) {
    630  1.15       cgd 			error = 0;
    631  1.15       cgd 			*(int *)data = ((struct socket *)fp->f_data)->so_pgid;
    632  1.15       cgd 			break;
    633  1.15       cgd 		}
    634  1.17       cgd 		error = (*fp->f_ops->fo_ioctl)(fp, TIOCGPGRP, data, p);
    635  1.55     lukem 		if (error == 0)
    636  1.55     lukem 			*(int *)data = -*(int *)data;
    637  1.15       cgd 		break;
    638  1.15       cgd 
    639  1.15       cgd 	default:
    640  1.15       cgd 		error = (*fp->f_ops->fo_ioctl)(fp, com, data, p);
    641  1.15       cgd 		/*
    642  1.15       cgd 		 * Copy any data to user, size was
    643  1.15       cgd 		 * already set and checked above.
    644  1.15       cgd 		 */
    645  1.15       cgd 		if (error == 0 && (com&IOC_OUT) && size)
    646  1.31       cgd 			error = copyout(data, SCARG(uap, data), size);
    647  1.15       cgd 		break;
    648  1.15       cgd 	}
    649  1.15       cgd 	if (memp)
    650  1.15       cgd 		free(memp, M_IOCTLOPS);
    651  1.45   thorpej  out:
    652  1.45   thorpej 	FILE_UNUSE(fp, p);
    653  1.61    atatat 	switch (error) {
    654  1.61    atatat 	case -1:
    655  1.61    atatat 		printf("sys_ioctl: _IO%s%s('%c', %lu, %lu) returned -1: "
    656  1.61    atatat 		    "pid=%d comm=%s\n",
    657  1.61    atatat 		    (com & IOC_IN) ? "W" : "", (com & IOC_OUT) ? "R" : "",
    658  1.61    atatat 		    (char)IOCGROUP(com), (com & 0xff), IOCPARM_LEN(com),
    659  1.61    atatat 		    p->p_pid, p->p_comm);
    660  1.61    atatat 		/* FALLTHROUGH */
    661  1.61    atatat 	case EPASSTHROUGH:
    662  1.61    atatat 		error = ENOTTY;
    663  1.61    atatat 		/* FALLTHROUGH */
    664  1.61    atatat 	default:
    665  1.61    atatat 		return (error);
    666  1.61    atatat 	}
    667  1.15       cgd }
    668  1.15       cgd 
    669  1.15       cgd int	selwait, nselcoll;
    670  1.15       cgd 
    671  1.15       cgd /*
    672  1.15       cgd  * Select system call.
    673  1.15       cgd  */
    674  1.22  christos int
    675  1.69   thorpej sys_select(struct lwp *l, void *v, register_t *retval)
    676  1.20   thorpej {
    677  1.47  augustss 	struct sys_select_args /* {
    678  1.53     lukem 		syscallarg(int)			nd;
    679  1.53     lukem 		syscallarg(fd_set *)		in;
    680  1.53     lukem 		syscallarg(fd_set *)		ou;
    681  1.53     lukem 		syscallarg(fd_set *)		ex;
    682  1.53     lukem 		syscallarg(struct timeval *)	tv;
    683  1.20   thorpej 	} */ *uap = v;
    684  1.69   thorpej 	struct proc	*p;
    685  1.53     lukem 	caddr_t		bits;
    686  1.53     lukem 	char		smallbits[howmany(FD_SETSIZE, NFDBITS) *
    687  1.53     lukem 			    sizeof(fd_mask) * 6];
    688  1.53     lukem 	struct		timeval atv;
    689  1.53     lukem 	int		s, ncoll, error, timo;
    690  1.53     lukem 	size_t		ni;
    691  1.15       cgd 
    692  1.53     lukem 	error = 0;
    693  1.69   thorpej 	p = l->l_proc;
    694  1.35   thorpej 	if (SCARG(uap, nd) < 0)
    695  1.35   thorpej 		return (EINVAL);
    696  1.16       cgd 	if (SCARG(uap, nd) > p->p_fd->fd_nfiles) {
    697  1.16       cgd 		/* forgiving; slightly wrong */
    698  1.16       cgd 		SCARG(uap, nd) = p->p_fd->fd_nfiles;
    699  1.16       cgd 	}
    700  1.16       cgd 	ni = howmany(SCARG(uap, nd), NFDBITS) * sizeof(fd_mask);
    701  1.27   mycroft 	if (ni * 6 > sizeof(smallbits))
    702  1.25   mycroft 		bits = malloc(ni * 6, M_TEMP, M_WAITOK);
    703  1.25   mycroft 	else
    704  1.26       cgd 		bits = smallbits;
    705  1.15       cgd 
    706  1.53     lukem #define	getbits(name, x)						\
    707  1.53     lukem 	if (SCARG(uap, name)) {						\
    708  1.53     lukem 		error = copyin(SCARG(uap, name), bits + ni * x, ni);	\
    709  1.53     lukem 		if (error)						\
    710  1.53     lukem 			goto done;					\
    711  1.53     lukem 	} else								\
    712  1.44     perry 		memset(bits + ni * x, 0, ni);
    713  1.15       cgd 	getbits(in, 0);
    714  1.15       cgd 	getbits(ou, 1);
    715  1.15       cgd 	getbits(ex, 2);
    716  1.15       cgd #undef	getbits
    717  1.15       cgd 
    718  1.65       scw 	timo = 0;
    719  1.16       cgd 	if (SCARG(uap, tv)) {
    720  1.31       cgd 		error = copyin(SCARG(uap, tv), (caddr_t)&atv,
    721  1.42     perry 			sizeof(atv));
    722  1.15       cgd 		if (error)
    723  1.15       cgd 			goto done;
    724  1.15       cgd 		if (itimerfix(&atv)) {
    725  1.15       cgd 			error = EINVAL;
    726  1.15       cgd 			goto done;
    727  1.15       cgd 		}
    728  1.15       cgd 		s = splclock();
    729  1.19   mycroft 		timeradd(&atv, &time, &atv);
    730  1.15       cgd 		splx(s);
    731  1.65       scw 	}
    732  1.65       scw 
    733  1.53     lukem  retry:
    734  1.15       cgd 	ncoll = nselcoll;
    735  1.69   thorpej 	l->l_flag |= L_SELECT;
    736  1.25   mycroft 	error = selscan(p, (fd_mask *)(bits + ni * 0),
    737  1.25   mycroft 			   (fd_mask *)(bits + ni * 3), SCARG(uap, nd), retval);
    738  1.15       cgd 	if (error || *retval)
    739  1.15       cgd 		goto done;
    740  1.49   thorpej 	if (SCARG(uap, tv)) {
    741  1.49   thorpej 		/*
    742  1.49   thorpej 		 * We have to recalculate the timeout on every retry.
    743  1.49   thorpej 		 */
    744  1.49   thorpej 		timo = hzto(&atv);
    745  1.49   thorpej 		if (timo <= 0)
    746  1.49   thorpej 			goto done;
    747  1.49   thorpej 	}
    748  1.52   thorpej 	s = splsched();
    749  1.69   thorpej 	if ((l->l_flag & L_SELECT) == 0 || nselcoll != ncoll) {
    750  1.15       cgd 		splx(s);
    751  1.15       cgd 		goto retry;
    752  1.15       cgd 	}
    753  1.69   thorpej 	l->l_flag &= ~L_SELECT;
    754  1.15       cgd 	error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
    755  1.15       cgd 	splx(s);
    756  1.15       cgd 	if (error == 0)
    757  1.15       cgd 		goto retry;
    758  1.53     lukem  done:
    759  1.69   thorpej 	l->l_flag &= ~L_SELECT;
    760  1.15       cgd 	/* select is not restarted after signals... */
    761  1.15       cgd 	if (error == ERESTART)
    762  1.15       cgd 		error = EINTR;
    763  1.15       cgd 	if (error == EWOULDBLOCK)
    764  1.15       cgd 		error = 0;
    765  1.27   mycroft 	if (error == 0) {
    766  1.53     lukem 
    767  1.53     lukem #define	putbits(name, x)						\
    768  1.53     lukem 		if (SCARG(uap, name)) {					\
    769  1.31       cgd 			error = copyout(bits + ni * x, SCARG(uap, name), ni); \
    770  1.53     lukem 			if (error)					\
    771  1.53     lukem 				goto out;				\
    772  1.27   mycroft 		}
    773  1.25   mycroft 		putbits(in, 3);
    774  1.25   mycroft 		putbits(ou, 4);
    775  1.25   mycroft 		putbits(ex, 5);
    776  1.15       cgd #undef putbits
    777  1.15       cgd 	}
    778  1.53     lukem  out:
    779  1.27   mycroft 	if (ni * 6 > sizeof(smallbits))
    780  1.25   mycroft 		free(bits, M_TEMP);
    781  1.15       cgd 	return (error);
    782  1.15       cgd }
    783  1.15       cgd 
    784  1.22  christos int
    785  1.53     lukem selscan(struct proc *p, fd_mask *ibitp, fd_mask *obitp, int nfd,
    786  1.53     lukem 	register_t *retval)
    787  1.53     lukem {
    788  1.53     lukem 	struct filedesc	*fdp;
    789  1.53     lukem 	int		msk, i, j, fd, n;
    790  1.53     lukem 	fd_mask		ibits, obits;
    791  1.53     lukem 	struct file	*fp;
    792  1.63  jdolecek 	static const int flag[3] = { POLLRDNORM | POLLHUP | POLLERR,
    793  1.28   mycroft 			       POLLWRNORM | POLLHUP | POLLERR,
    794  1.28   mycroft 			       POLLRDBAND };
    795  1.15       cgd 
    796  1.53     lukem 	fdp = p->p_fd;
    797  1.53     lukem 	n = 0;
    798  1.15       cgd 	for (msk = 0; msk < 3; msk++) {
    799  1.15       cgd 		for (i = 0; i < nfd; i += NFDBITS) {
    800  1.25   mycroft 			ibits = *ibitp++;
    801  1.25   mycroft 			obits = 0;
    802  1.25   mycroft 			while ((j = ffs(ibits)) && (fd = i + --j) < nfd) {
    803  1.25   mycroft 				ibits &= ~(1 << j);
    804  1.56   thorpej 				if ((fp = fd_getfile(fdp, fd)) == NULL)
    805  1.15       cgd 					return (EBADF);
    806  1.45   thorpej 				FILE_USE(fp);
    807  1.28   mycroft 				if ((*fp->f_ops->fo_poll)(fp, flag[msk], p)) {
    808  1.25   mycroft 					obits |= (1 << j);
    809  1.15       cgd 					n++;
    810  1.15       cgd 				}
    811  1.45   thorpej 				FILE_UNUSE(fp, p);
    812  1.15       cgd 			}
    813  1.25   mycroft 			*obitp++ = obits;
    814  1.15       cgd 		}
    815  1.15       cgd 	}
    816  1.15       cgd 	*retval = n;
    817  1.15       cgd 	return (0);
    818  1.15       cgd }
    819  1.15       cgd 
    820  1.28   mycroft /*
    821  1.28   mycroft  * Poll system call.
    822  1.28   mycroft  */
    823  1.28   mycroft int
    824  1.69   thorpej sys_poll(struct lwp *l, void *v, register_t *retval)
    825  1.28   mycroft {
    826  1.47  augustss 	struct sys_poll_args /* {
    827  1.53     lukem 		syscallarg(struct pollfd *)	fds;
    828  1.53     lukem 		syscallarg(u_int)		nfds;
    829  1.53     lukem 		syscallarg(int)			timeout;
    830  1.28   mycroft 	} */ *uap = v;
    831  1.69   thorpej 	struct proc	*p;
    832  1.53     lukem 	caddr_t		bits;
    833  1.53     lukem 	char		smallbits[32 * sizeof(struct pollfd)];
    834  1.53     lukem 	struct timeval	atv;
    835  1.53     lukem 	int		s, ncoll, error, timo;
    836  1.53     lukem 	size_t		ni;
    837  1.28   mycroft 
    838  1.53     lukem 	error = 0;
    839  1.69   thorpej 	p = l->l_proc;
    840  1.28   mycroft 	if (SCARG(uap, nfds) > p->p_fd->fd_nfiles) {
    841  1.28   mycroft 		/* forgiving; slightly wrong */
    842  1.28   mycroft 		SCARG(uap, nfds) = p->p_fd->fd_nfiles;
    843  1.28   mycroft 	}
    844  1.28   mycroft 	ni = SCARG(uap, nfds) * sizeof(struct pollfd);
    845  1.28   mycroft 	if (ni > sizeof(smallbits))
    846  1.28   mycroft 		bits = malloc(ni, M_TEMP, M_WAITOK);
    847  1.28   mycroft 	else
    848  1.28   mycroft 		bits = smallbits;
    849  1.28   mycroft 
    850  1.31       cgd 	error = copyin(SCARG(uap, fds), bits, ni);
    851  1.28   mycroft 	if (error)
    852  1.28   mycroft 		goto done;
    853  1.28   mycroft 
    854  1.65       scw 	timo = 0;
    855  1.30   mycroft 	if (SCARG(uap, timeout) != INFTIM) {
    856  1.28   mycroft 		atv.tv_sec = SCARG(uap, timeout) / 1000;
    857  1.28   mycroft 		atv.tv_usec = (SCARG(uap, timeout) % 1000) * 1000;
    858  1.28   mycroft 		if (itimerfix(&atv)) {
    859  1.28   mycroft 			error = EINVAL;
    860  1.28   mycroft 			goto done;
    861  1.28   mycroft 		}
    862  1.28   mycroft 		s = splclock();
    863  1.28   mycroft 		timeradd(&atv, &time, &atv);
    864  1.28   mycroft 		splx(s);
    865  1.65       scw 	}
    866  1.65       scw 
    867  1.53     lukem  retry:
    868  1.28   mycroft 	ncoll = nselcoll;
    869  1.69   thorpej 	l->l_flag |= L_SELECT;
    870  1.28   mycroft 	error = pollscan(p, (struct pollfd *)bits, SCARG(uap, nfds), retval);
    871  1.28   mycroft 	if (error || *retval)
    872  1.28   mycroft 		goto done;
    873  1.49   thorpej 	if (SCARG(uap, timeout) != INFTIM) {
    874  1.49   thorpej 		/*
    875  1.49   thorpej 		 * We have to recalculate the timeout on every retry.
    876  1.49   thorpej 		 */
    877  1.49   thorpej 		timo = hzto(&atv);
    878  1.49   thorpej 		if (timo <= 0)
    879  1.49   thorpej 			goto done;
    880  1.49   thorpej 	}
    881  1.52   thorpej 	s = splsched();
    882  1.69   thorpej 	if ((l->l_flag & L_SELECT) == 0 || nselcoll != ncoll) {
    883  1.28   mycroft 		splx(s);
    884  1.28   mycroft 		goto retry;
    885  1.28   mycroft 	}
    886  1.69   thorpej 	l->l_flag &= ~L_SELECT;
    887  1.28   mycroft 	error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
    888  1.28   mycroft 	splx(s);
    889  1.28   mycroft 	if (error == 0)
    890  1.28   mycroft 		goto retry;
    891  1.53     lukem  done:
    892  1.69   thorpej 	l->l_flag &= ~L_SELECT;
    893  1.28   mycroft 	/* poll is not restarted after signals... */
    894  1.28   mycroft 	if (error == ERESTART)
    895  1.28   mycroft 		error = EINTR;
    896  1.28   mycroft 	if (error == EWOULDBLOCK)
    897  1.28   mycroft 		error = 0;
    898  1.28   mycroft 	if (error == 0) {
    899  1.31       cgd 		error = copyout(bits, SCARG(uap, fds), ni);
    900  1.28   mycroft 		if (error)
    901  1.28   mycroft 			goto out;
    902  1.28   mycroft 	}
    903  1.53     lukem  out:
    904  1.28   mycroft 	if (ni > sizeof(smallbits))
    905  1.28   mycroft 		free(bits, M_TEMP);
    906  1.28   mycroft 	return (error);
    907  1.28   mycroft }
    908  1.28   mycroft 
    909  1.28   mycroft int
    910  1.53     lukem pollscan(struct proc *p, struct pollfd *fds, int nfd, register_t *retval)
    911  1.53     lukem {
    912  1.53     lukem 	struct filedesc	*fdp;
    913  1.53     lukem 	int		i, n;
    914  1.53     lukem 	struct file	*fp;
    915  1.28   mycroft 
    916  1.53     lukem 	fdp = p->p_fd;
    917  1.54     lukem 	n = 0;
    918  1.28   mycroft 	for (i = 0; i < nfd; i++, fds++) {
    919  1.60  christos 		if (fds->fd >= fdp->fd_nfiles) {
    920  1.28   mycroft 			fds->revents = POLLNVAL;
    921  1.28   mycroft 			n++;
    922  1.60  christos 		} else if (fds->fd < 0) {
    923  1.60  christos 			fds->revents = 0;
    924  1.28   mycroft 		} else {
    925  1.56   thorpej 			if ((fp = fd_getfile(fdp, fds->fd)) == NULL) {
    926  1.32       mrg 				fds->revents = POLLNVAL;
    927  1.28   mycroft 				n++;
    928  1.32       mrg 			} else {
    929  1.45   thorpej 				FILE_USE(fp);
    930  1.32       mrg 				fds->revents = (*fp->f_ops->fo_poll)(fp,
    931  1.32       mrg 				    fds->events | POLLERR | POLLHUP, p);
    932  1.32       mrg 				if (fds->revents != 0)
    933  1.32       mrg 					n++;
    934  1.45   thorpej 				FILE_UNUSE(fp, p);
    935  1.32       mrg 			}
    936  1.28   mycroft 		}
    937  1.28   mycroft 	}
    938  1.28   mycroft 	*retval = n;
    939  1.28   mycroft 	return (0);
    940  1.28   mycroft }
    941  1.28   mycroft 
    942  1.15       cgd /*ARGSUSED*/
    943  1.22  christos int
    944  1.53     lukem seltrue(dev_t dev, int events, struct proc *p)
    945  1.15       cgd {
    946  1.15       cgd 
    947  1.28   mycroft 	return (events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
    948  1.15       cgd }
    949  1.15       cgd 
    950  1.15       cgd /*
    951  1.15       cgd  * Record a select request.
    952  1.15       cgd  */
    953  1.15       cgd void
    954  1.53     lukem selrecord(struct proc *selector, struct selinfo *sip)
    955  1.15       cgd {
    956  1.69   thorpej 	struct lwp	*l;
    957  1.53     lukem 	struct proc	*p;
    958  1.53     lukem 	pid_t		mypid;
    959  1.69   thorpej 	int		collision;
    960  1.15       cgd 
    961  1.15       cgd 	mypid = selector->p_pid;
    962  1.66  christos 	if (sip->sel_pid == mypid)
    963  1.15       cgd 		return;
    964  1.69   thorpej 	collision = 0;
    965  1.69   thorpej 	if (sip->sel_pid && (p = pfind(sip->sel_pid))) {
    966  1.69   thorpej 		for (l = LIST_FIRST(&p->p_lwps); l != NULL;
    967  1.69   thorpej 		     l = LIST_NEXT(l, l_sibling)) {
    968  1.69   thorpej 			if (l->l_wchan == (caddr_t)&selwait) {
    969  1.69   thorpej 				collision = 1;
    970  1.69   thorpej 				sip->sel_flags |= SI_COLL;
    971  1.69   thorpej 			}
    972  1.69   thorpej 		}
    973  1.69   thorpej 	}
    974  1.69   thorpej 
    975  1.69   thorpej 	if (collision == 0)
    976  1.66  christos 		sip->sel_pid = mypid;
    977  1.15       cgd }
    978  1.15       cgd 
    979  1.15       cgd /*
    980  1.15       cgd  * Do a wakeup when a selectable event occurs.
    981  1.15       cgd  */
    982  1.15       cgd void
    983  1.15       cgd selwakeup(sip)
    984  1.47  augustss 	struct selinfo *sip;
    985  1.15       cgd {
    986  1.69   thorpej 	struct lwp *l;
    987  1.47  augustss 	struct proc *p;
    988  1.15       cgd 	int s;
    989  1.15       cgd 
    990  1.66  christos 	if (sip->sel_pid == 0)
    991  1.15       cgd 		return;
    992  1.66  christos 	if (sip->sel_flags & SI_COLL) {
    993  1.67  jdolecek 		sip->sel_pid = 0;
    994  1.15       cgd 		nselcoll++;
    995  1.66  christos 		sip->sel_flags &= ~SI_COLL;
    996  1.15       cgd 		wakeup((caddr_t)&selwait);
    997  1.67  jdolecek 		return;
    998  1.15       cgd 	}
    999  1.66  christos 	p = pfind(sip->sel_pid);
   1000  1.66  christos 	sip->sel_pid = 0;
   1001  1.15       cgd 	if (p != NULL) {
   1002  1.69   thorpej 		for (l = LIST_FIRST(&p->p_lwps); l != NULL;
   1003  1.69   thorpej 		     l = LIST_NEXT(l, l_sibling)) {
   1004  1.69   thorpej 			SCHED_LOCK(s);
   1005  1.69   thorpej 			if (l->l_wchan == (caddr_t)&selwait) {
   1006  1.69   thorpej 				if (l->l_stat == LSSLEEP)
   1007  1.69   thorpej 					setrunnable(l);
   1008  1.69   thorpej 				else
   1009  1.69   thorpej 					unsleep(l);
   1010  1.69   thorpej 			} else if (l->l_flag & L_SELECT)
   1011  1.69   thorpej 				l->l_flag &= ~L_SELECT;
   1012  1.69   thorpej 			SCHED_UNLOCK(s);
   1013  1.69   thorpej 		}
   1014  1.15       cgd 	}
   1015  1.15       cgd }
   1016