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