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