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