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