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