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