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