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