Home | History | Annotate | Line # | Download | only in kern
sys_generic.c revision 1.1.1.3
      1  1.1.1.2  fvdl /*
      2  1.1.1.2  fvdl  * Copyright (c) 1982, 1986, 1989, 1993
      3  1.1.1.2  fvdl  *	The Regents of the University of California.  All rights reserved.
      4  1.1.1.2  fvdl  * (c) UNIX System Laboratories, Inc.
      5  1.1.1.2  fvdl  * All or some portions of this file are derived from material licensed
      6  1.1.1.2  fvdl  * to the University of California by American Telephone and Telegraph
      7  1.1.1.2  fvdl  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
      8  1.1.1.2  fvdl  * the permission of UNIX System Laboratories, Inc.
      9  1.1.1.2  fvdl  *
     10  1.1.1.2  fvdl  * Redistribution and use in source and binary forms, with or without
     11  1.1.1.2  fvdl  * modification, are permitted provided that the following conditions
     12  1.1.1.2  fvdl  * are met:
     13  1.1.1.2  fvdl  * 1. Redistributions of source code must retain the above copyright
     14  1.1.1.2  fvdl  *    notice, this list of conditions and the following disclaimer.
     15  1.1.1.2  fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1.1.2  fvdl  *    notice, this list of conditions and the following disclaimer in the
     17  1.1.1.2  fvdl  *    documentation and/or other materials provided with the distribution.
     18  1.1.1.2  fvdl  * 3. All advertising materials mentioning features or use of this software
     19  1.1.1.2  fvdl  *    must display the following acknowledgement:
     20  1.1.1.2  fvdl  *	This product includes software developed by the University of
     21  1.1.1.2  fvdl  *	California, Berkeley and its contributors.
     22  1.1.1.2  fvdl  * 4. Neither the name of the University nor the names of its contributors
     23  1.1.1.2  fvdl  *    may be used to endorse or promote products derived from this software
     24  1.1.1.2  fvdl  *    without specific prior written permission.
     25  1.1.1.2  fvdl  *
     26  1.1.1.2  fvdl  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.1.1.2  fvdl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.1.1.2  fvdl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.1.1.2  fvdl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.1.1.2  fvdl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.1.1.2  fvdl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.1.1.2  fvdl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.1.1.2  fvdl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.1.1.2  fvdl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.1.1.2  fvdl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.1.1.2  fvdl  * SUCH DAMAGE.
     37  1.1.1.2  fvdl  *
     38  1.1.1.3  fvdl  *	@(#)sys_generic.c	8.9 (Berkeley) 2/14/95
     39  1.1.1.2  fvdl  */
     40  1.1.1.2  fvdl 
     41  1.1.1.2  fvdl #include <sys/param.h>
     42  1.1.1.2  fvdl #include <sys/systm.h>
     43  1.1.1.2  fvdl #include <sys/filedesc.h>
     44  1.1.1.2  fvdl #include <sys/ioctl.h>
     45  1.1.1.2  fvdl #include <sys/file.h>
     46  1.1.1.2  fvdl #include <sys/proc.h>
     47  1.1.1.2  fvdl #include <sys/socketvar.h>
     48  1.1.1.2  fvdl #include <sys/uio.h>
     49  1.1.1.2  fvdl #include <sys/kernel.h>
     50  1.1.1.2  fvdl #include <sys/stat.h>
     51  1.1.1.2  fvdl #include <sys/malloc.h>
     52  1.1.1.2  fvdl #ifdef KTRACE
     53  1.1.1.2  fvdl #include <sys/ktrace.h>
     54  1.1.1.2  fvdl #endif
     55  1.1.1.2  fvdl 
     56  1.1.1.3  fvdl #include <sys/mount.h>
     57  1.1.1.3  fvdl #include <sys/syscallargs.h>
     58  1.1.1.3  fvdl 
     59  1.1.1.2  fvdl /*
     60  1.1.1.2  fvdl  * Read system call.
     61  1.1.1.2  fvdl  */
     62  1.1.1.2  fvdl /* ARGSUSED */
     63  1.1.1.3  fvdl int
     64  1.1.1.2  fvdl read(p, uap, retval)
     65  1.1.1.2  fvdl 	struct proc *p;
     66  1.1.1.3  fvdl 	register struct read_args /* {
     67  1.1.1.3  fvdl 		syscallarg(int) fd;
     68  1.1.1.3  fvdl 		syscallarg(char *) buf;
     69  1.1.1.3  fvdl 		syscallarg(u_int) nbyte;
     70  1.1.1.3  fvdl 	} */ *uap;
     71  1.1.1.3  fvdl 	register_t *retval;
     72  1.1.1.2  fvdl {
     73  1.1.1.2  fvdl 	register struct file *fp;
     74  1.1.1.2  fvdl 	register struct filedesc *fdp = p->p_fd;
     75  1.1.1.2  fvdl 	struct uio auio;
     76  1.1.1.2  fvdl 	struct iovec aiov;
     77  1.1.1.2  fvdl 	long cnt, error = 0;
     78  1.1.1.2  fvdl #ifdef KTRACE
     79  1.1.1.2  fvdl 	struct iovec ktriov;
     80  1.1.1.2  fvdl #endif
     81  1.1.1.2  fvdl 
     82  1.1.1.3  fvdl 	if (((u_int)SCARG(uap, fd)) >= fdp->fd_nfiles ||
     83  1.1.1.3  fvdl 	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
     84  1.1.1.2  fvdl 	    (fp->f_flag & FREAD) == 0)
     85  1.1.1.2  fvdl 		return (EBADF);
     86  1.1.1.3  fvdl 	aiov.iov_base = (caddr_t)SCARG(uap, buf);
     87  1.1.1.3  fvdl 	aiov.iov_len = SCARG(uap, nbyte);
     88  1.1.1.2  fvdl 	auio.uio_iov = &aiov;
     89  1.1.1.2  fvdl 	auio.uio_iovcnt = 1;
     90  1.1.1.3  fvdl 	auio.uio_resid = SCARG(uap, nbyte);
     91  1.1.1.2  fvdl 	auio.uio_rw = UIO_READ;
     92  1.1.1.2  fvdl 	auio.uio_segflg = UIO_USERSPACE;
     93  1.1.1.2  fvdl 	auio.uio_procp = p;
     94  1.1.1.2  fvdl #ifdef KTRACE
     95  1.1.1.2  fvdl 	/*
     96  1.1.1.2  fvdl 	 * if tracing, save a copy of iovec
     97  1.1.1.2  fvdl 	 */
     98  1.1.1.2  fvdl 	if (KTRPOINT(p, KTR_GENIO))
     99  1.1.1.2  fvdl 		ktriov = aiov;
    100  1.1.1.2  fvdl #endif
    101  1.1.1.3  fvdl 	cnt = SCARG(uap, nbyte);
    102  1.1.1.2  fvdl 	if (error = (*fp->f_ops->fo_read)(fp, &auio, fp->f_cred))
    103  1.1.1.2  fvdl 		if (auio.uio_resid != cnt && (error == ERESTART ||
    104  1.1.1.2  fvdl 		    error == EINTR || error == EWOULDBLOCK))
    105  1.1.1.2  fvdl 			error = 0;
    106  1.1.1.2  fvdl 	cnt -= auio.uio_resid;
    107  1.1.1.2  fvdl #ifdef KTRACE
    108  1.1.1.2  fvdl 	if (KTRPOINT(p, KTR_GENIO) && error == 0)
    109  1.1.1.3  fvdl 		ktrgenio(p->p_tracep, SCARG(uap, fd), UIO_READ, &ktriov,
    110  1.1.1.3  fvdl 		    cnt, error);
    111  1.1.1.2  fvdl #endif
    112  1.1.1.2  fvdl 	*retval = cnt;
    113  1.1.1.2  fvdl 	return (error);
    114  1.1.1.2  fvdl }
    115  1.1.1.2  fvdl 
    116  1.1.1.2  fvdl /*
    117  1.1.1.2  fvdl  * Scatter read system call.
    118  1.1.1.2  fvdl  */
    119  1.1.1.3  fvdl int
    120  1.1.1.2  fvdl readv(p, uap, retval)
    121  1.1.1.2  fvdl 	struct proc *p;
    122  1.1.1.3  fvdl 	register struct readv_args /* {
    123  1.1.1.3  fvdl 		syscallarg(int) fd;
    124  1.1.1.3  fvdl 		syscallarg(struct iovec *) iovp;
    125  1.1.1.3  fvdl 		syscallarg(u_int) iovcnt;
    126  1.1.1.3  fvdl 	} */ *uap;
    127  1.1.1.3  fvdl 	register_t *retval;
    128  1.1.1.2  fvdl {
    129  1.1.1.2  fvdl 	register struct file *fp;
    130  1.1.1.2  fvdl 	register struct filedesc *fdp = p->p_fd;
    131  1.1.1.2  fvdl 	struct uio auio;
    132  1.1.1.2  fvdl 	register struct iovec *iov;
    133  1.1.1.2  fvdl 	struct iovec *needfree;
    134  1.1.1.2  fvdl 	struct iovec aiov[UIO_SMALLIOV];
    135  1.1.1.2  fvdl 	long i, cnt, error = 0;
    136  1.1.1.2  fvdl 	u_int iovlen;
    137  1.1.1.2  fvdl #ifdef KTRACE
    138  1.1.1.2  fvdl 	struct iovec *ktriov = NULL;
    139  1.1.1.2  fvdl #endif
    140  1.1.1.2  fvdl 
    141  1.1.1.3  fvdl 	if (((u_int)SCARG(uap, fd)) >= fdp->fd_nfiles ||
    142  1.1.1.3  fvdl 	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
    143  1.1.1.2  fvdl 	    (fp->f_flag & FREAD) == 0)
    144  1.1.1.2  fvdl 		return (EBADF);
    145  1.1.1.2  fvdl 	/* note: can't use iovlen until iovcnt is validated */
    146  1.1.1.3  fvdl 	iovlen = SCARG(uap, iovcnt) * sizeof (struct iovec);
    147  1.1.1.3  fvdl 	if (SCARG(uap, iovcnt) > UIO_SMALLIOV) {
    148  1.1.1.3  fvdl 		if (SCARG(uap, iovcnt) > UIO_MAXIOV)
    149  1.1.1.2  fvdl 			return (EINVAL);
    150  1.1.1.2  fvdl 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
    151  1.1.1.2  fvdl 		needfree = iov;
    152  1.1.1.2  fvdl 	} else {
    153  1.1.1.2  fvdl 		iov = aiov;
    154  1.1.1.2  fvdl 		needfree = NULL;
    155  1.1.1.2  fvdl 	}
    156  1.1.1.2  fvdl 	auio.uio_iov = iov;
    157  1.1.1.3  fvdl 	auio.uio_iovcnt = SCARG(uap, iovcnt);
    158  1.1.1.2  fvdl 	auio.uio_rw = UIO_READ;
    159  1.1.1.2  fvdl 	auio.uio_segflg = UIO_USERSPACE;
    160  1.1.1.2  fvdl 	auio.uio_procp = p;
    161  1.1.1.3  fvdl 	if (error = copyin((caddr_t)SCARG(uap, iovp), (caddr_t)iov, iovlen))
    162  1.1.1.2  fvdl 		goto done;
    163  1.1.1.2  fvdl 	auio.uio_resid = 0;
    164  1.1.1.3  fvdl 	for (i = 0; i < SCARG(uap, iovcnt); i++) {
    165  1.1.1.3  fvdl 		if (auio.uio_resid + iov->iov_len < auio.uio_resid) {
    166  1.1.1.2  fvdl 			error = EINVAL;
    167  1.1.1.2  fvdl 			goto done;
    168  1.1.1.2  fvdl 		}
    169  1.1.1.2  fvdl 		auio.uio_resid += iov->iov_len;
    170  1.1.1.2  fvdl 		iov++;
    171  1.1.1.2  fvdl 	}
    172  1.1.1.2  fvdl #ifdef KTRACE
    173  1.1.1.2  fvdl 	/*
    174  1.1.1.2  fvdl 	 * if tracing, save a copy of iovec
    175  1.1.1.2  fvdl 	 */
    176  1.1.1.2  fvdl 	if (KTRPOINT(p, KTR_GENIO))  {
    177  1.1.1.2  fvdl 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
    178  1.1.1.2  fvdl 		bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
    179  1.1.1.2  fvdl 	}
    180  1.1.1.2  fvdl #endif
    181  1.1.1.2  fvdl 	cnt = auio.uio_resid;
    182  1.1.1.2  fvdl 	if (error = (*fp->f_ops->fo_read)(fp, &auio, fp->f_cred))
    183  1.1.1.2  fvdl 		if (auio.uio_resid != cnt && (error == ERESTART ||
    184  1.1.1.2  fvdl 		    error == EINTR || error == EWOULDBLOCK))
    185  1.1.1.2  fvdl 			error = 0;
    186  1.1.1.2  fvdl 	cnt -= auio.uio_resid;
    187  1.1.1.2  fvdl #ifdef KTRACE
    188  1.1.1.2  fvdl 	if (ktriov != NULL) {
    189  1.1.1.2  fvdl 		if (error == 0)
    190  1.1.1.3  fvdl 			ktrgenio(p->p_tracep, SCARG(uap, fd), UIO_READ, ktriov,
    191  1.1.1.2  fvdl 			    cnt, error);
    192  1.1.1.2  fvdl 		FREE(ktriov, M_TEMP);
    193  1.1.1.2  fvdl 	}
    194  1.1.1.2  fvdl #endif
    195  1.1.1.2  fvdl 	*retval = cnt;
    196  1.1.1.2  fvdl done:
    197  1.1.1.2  fvdl 	if (needfree)
    198  1.1.1.2  fvdl 		FREE(needfree, M_IOV);
    199  1.1.1.2  fvdl 	return (error);
    200  1.1.1.2  fvdl }
    201  1.1.1.2  fvdl 
    202  1.1.1.2  fvdl /*
    203  1.1.1.2  fvdl  * Write system call
    204  1.1.1.2  fvdl  */
    205  1.1.1.3  fvdl int
    206  1.1.1.2  fvdl write(p, uap, retval)
    207  1.1.1.2  fvdl 	struct proc *p;
    208  1.1.1.3  fvdl 	register struct write_args /* {
    209  1.1.1.3  fvdl 		syscallarg(int) fd;
    210  1.1.1.3  fvdl 		syscallarg(char *) buf;
    211  1.1.1.3  fvdl 		syscallarg(u_int) nbyte;
    212  1.1.1.3  fvdl 	} */ *uap;
    213  1.1.1.3  fvdl 	register_t *retval;
    214  1.1.1.2  fvdl {
    215  1.1.1.2  fvdl 	register struct file *fp;
    216  1.1.1.2  fvdl 	register struct filedesc *fdp = p->p_fd;
    217  1.1.1.2  fvdl 	struct uio auio;
    218  1.1.1.2  fvdl 	struct iovec aiov;
    219  1.1.1.2  fvdl 	long cnt, error = 0;
    220  1.1.1.2  fvdl #ifdef KTRACE
    221  1.1.1.2  fvdl 	struct iovec ktriov;
    222  1.1.1.2  fvdl #endif
    223  1.1.1.2  fvdl 
    224  1.1.1.3  fvdl 	if (((u_int)SCARG(uap, fd)) >= fdp->fd_nfiles ||
    225  1.1.1.3  fvdl 	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
    226  1.1.1.2  fvdl 	    (fp->f_flag & FWRITE) == 0)
    227  1.1.1.2  fvdl 		return (EBADF);
    228  1.1.1.3  fvdl 	aiov.iov_base = (caddr_t)SCARG(uap, buf);
    229  1.1.1.3  fvdl 	aiov.iov_len = SCARG(uap, nbyte);
    230  1.1.1.2  fvdl 	auio.uio_iov = &aiov;
    231  1.1.1.2  fvdl 	auio.uio_iovcnt = 1;
    232  1.1.1.3  fvdl 	auio.uio_resid = SCARG(uap, nbyte);
    233  1.1.1.2  fvdl 	auio.uio_rw = UIO_WRITE;
    234  1.1.1.2  fvdl 	auio.uio_segflg = UIO_USERSPACE;
    235  1.1.1.2  fvdl 	auio.uio_procp = p;
    236  1.1.1.2  fvdl #ifdef KTRACE
    237  1.1.1.2  fvdl 	/*
    238  1.1.1.2  fvdl 	 * if tracing, save a copy of iovec
    239  1.1.1.2  fvdl 	 */
    240  1.1.1.2  fvdl 	if (KTRPOINT(p, KTR_GENIO))
    241  1.1.1.2  fvdl 		ktriov = aiov;
    242  1.1.1.2  fvdl #endif
    243  1.1.1.3  fvdl 	cnt = SCARG(uap, nbyte);
    244  1.1.1.2  fvdl 	if (error = (*fp->f_ops->fo_write)(fp, &auio, fp->f_cred)) {
    245  1.1.1.2  fvdl 		if (auio.uio_resid != cnt && (error == ERESTART ||
    246  1.1.1.2  fvdl 		    error == EINTR || error == EWOULDBLOCK))
    247  1.1.1.2  fvdl 			error = 0;
    248  1.1.1.2  fvdl 		if (error == EPIPE)
    249  1.1.1.2  fvdl 			psignal(p, SIGPIPE);
    250  1.1.1.2  fvdl 	}
    251  1.1.1.2  fvdl 	cnt -= auio.uio_resid;
    252  1.1.1.2  fvdl #ifdef KTRACE
    253  1.1.1.2  fvdl 	if (KTRPOINT(p, KTR_GENIO) && error == 0)
    254  1.1.1.3  fvdl 		ktrgenio(p->p_tracep, SCARG(uap, fd), UIO_WRITE,
    255  1.1.1.2  fvdl 		    &ktriov, cnt, error);
    256  1.1.1.2  fvdl #endif
    257  1.1.1.2  fvdl 	*retval = cnt;
    258  1.1.1.2  fvdl 	return (error);
    259  1.1.1.2  fvdl }
    260  1.1.1.2  fvdl 
    261  1.1.1.2  fvdl /*
    262  1.1.1.2  fvdl  * Gather write system call
    263  1.1.1.2  fvdl  */
    264  1.1.1.3  fvdl int
    265  1.1.1.2  fvdl writev(p, uap, retval)
    266  1.1.1.2  fvdl 	struct proc *p;
    267  1.1.1.3  fvdl 	register struct writev_args /* {
    268  1.1.1.3  fvdl 		syscallarg(int) fd;
    269  1.1.1.3  fvdl 		syscallarg(struct iovec *) iovp;
    270  1.1.1.3  fvdl 		syscallarg(u_int) iovcnt;
    271  1.1.1.3  fvdl 	} */ *uap;
    272  1.1.1.3  fvdl 	register_t *retval;
    273  1.1.1.2  fvdl {
    274  1.1.1.2  fvdl 	register struct file *fp;
    275  1.1.1.2  fvdl 	register struct filedesc *fdp = p->p_fd;
    276  1.1.1.2  fvdl 	struct uio auio;
    277  1.1.1.2  fvdl 	register struct iovec *iov;
    278  1.1.1.2  fvdl 	struct iovec *needfree;
    279  1.1.1.2  fvdl 	struct iovec aiov[UIO_SMALLIOV];
    280  1.1.1.2  fvdl 	long i, cnt, error = 0;
    281  1.1.1.2  fvdl 	u_int iovlen;
    282  1.1.1.2  fvdl #ifdef KTRACE
    283  1.1.1.2  fvdl 	struct iovec *ktriov = NULL;
    284  1.1.1.2  fvdl #endif
    285  1.1.1.2  fvdl 
    286  1.1.1.3  fvdl 	if (((u_int)SCARG(uap, fd)) >= fdp->fd_nfiles ||
    287  1.1.1.3  fvdl 	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL ||
    288  1.1.1.2  fvdl 	    (fp->f_flag & FWRITE) == 0)
    289  1.1.1.2  fvdl 		return (EBADF);
    290  1.1.1.2  fvdl 	/* note: can't use iovlen until iovcnt is validated */
    291  1.1.1.3  fvdl 	iovlen = SCARG(uap, iovcnt) * sizeof (struct iovec);
    292  1.1.1.3  fvdl 	if (SCARG(uap, iovcnt) > UIO_SMALLIOV) {
    293  1.1.1.3  fvdl 		if (SCARG(uap, iovcnt) > UIO_MAXIOV)
    294  1.1.1.2  fvdl 			return (EINVAL);
    295  1.1.1.2  fvdl 		MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK);
    296  1.1.1.2  fvdl 		needfree = iov;
    297  1.1.1.2  fvdl 	} else {
    298  1.1.1.2  fvdl 		iov = aiov;
    299  1.1.1.2  fvdl 		needfree = NULL;
    300  1.1.1.2  fvdl 	}
    301  1.1.1.2  fvdl 	auio.uio_iov = iov;
    302  1.1.1.3  fvdl 	auio.uio_iovcnt = SCARG(uap, iovcnt);
    303  1.1.1.2  fvdl 	auio.uio_rw = UIO_WRITE;
    304  1.1.1.2  fvdl 	auio.uio_segflg = UIO_USERSPACE;
    305  1.1.1.2  fvdl 	auio.uio_procp = p;
    306  1.1.1.3  fvdl 	if (error = copyin((caddr_t)SCARG(uap, iovp), (caddr_t)iov, iovlen))
    307  1.1.1.2  fvdl 		goto done;
    308  1.1.1.2  fvdl 	auio.uio_resid = 0;
    309  1.1.1.3  fvdl 	for (i = 0; i < SCARG(uap, iovcnt); i++) {
    310  1.1.1.3  fvdl 		if (auio.uio_resid + iov->iov_len < auio.uio_resid) {
    311  1.1.1.2  fvdl 			error = EINVAL;
    312  1.1.1.2  fvdl 			goto done;
    313  1.1.1.2  fvdl 		}
    314  1.1.1.2  fvdl 		auio.uio_resid += iov->iov_len;
    315  1.1.1.2  fvdl 		iov++;
    316  1.1.1.2  fvdl 	}
    317  1.1.1.2  fvdl #ifdef KTRACE
    318  1.1.1.2  fvdl 	/*
    319  1.1.1.2  fvdl 	 * if tracing, save a copy of iovec
    320  1.1.1.2  fvdl 	 */
    321  1.1.1.2  fvdl 	if (KTRPOINT(p, KTR_GENIO))  {
    322  1.1.1.2  fvdl 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
    323  1.1.1.2  fvdl 		bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
    324  1.1.1.2  fvdl 	}
    325  1.1.1.2  fvdl #endif
    326  1.1.1.2  fvdl 	cnt = auio.uio_resid;
    327  1.1.1.2  fvdl 	if (error = (*fp->f_ops->fo_write)(fp, &auio, fp->f_cred)) {
    328  1.1.1.2  fvdl 		if (auio.uio_resid != cnt && (error == ERESTART ||
    329  1.1.1.2  fvdl 		    error == EINTR || error == EWOULDBLOCK))
    330  1.1.1.2  fvdl 			error = 0;
    331  1.1.1.2  fvdl 		if (error == EPIPE)
    332  1.1.1.2  fvdl 			psignal(p, SIGPIPE);
    333  1.1.1.2  fvdl 	}
    334  1.1.1.2  fvdl 	cnt -= auio.uio_resid;
    335  1.1.1.2  fvdl #ifdef KTRACE
    336  1.1.1.2  fvdl 	if (ktriov != NULL) {
    337  1.1.1.2  fvdl 		if (error == 0)
    338  1.1.1.3  fvdl 			ktrgenio(p->p_tracep, SCARG(uap, fd), UIO_WRITE,
    339  1.1.1.2  fvdl 				ktriov, cnt, error);
    340  1.1.1.2  fvdl 		FREE(ktriov, M_TEMP);
    341  1.1.1.2  fvdl 	}
    342  1.1.1.2  fvdl #endif
    343  1.1.1.2  fvdl 	*retval = cnt;
    344  1.1.1.2  fvdl done:
    345  1.1.1.2  fvdl 	if (needfree)
    346  1.1.1.2  fvdl 		FREE(needfree, M_IOV);
    347  1.1.1.2  fvdl 	return (error);
    348  1.1.1.2  fvdl }
    349  1.1.1.2  fvdl 
    350  1.1.1.2  fvdl /*
    351  1.1.1.2  fvdl  * Ioctl system call
    352  1.1.1.2  fvdl  */
    353  1.1.1.2  fvdl /* ARGSUSED */
    354  1.1.1.3  fvdl int
    355  1.1.1.2  fvdl ioctl(p, uap, retval)
    356  1.1.1.2  fvdl 	struct proc *p;
    357  1.1.1.3  fvdl 	register struct ioctl_args /* {
    358  1.1.1.3  fvdl 		syscallarg(int) fd;
    359  1.1.1.3  fvdl 		syscallarg(u_long) com;
    360  1.1.1.3  fvdl 		syscallarg(caddr_t) data;
    361  1.1.1.3  fvdl 	} */ *uap;
    362  1.1.1.3  fvdl 	register_t *retval;
    363  1.1.1.2  fvdl {
    364  1.1.1.2  fvdl 	register struct file *fp;
    365  1.1.1.2  fvdl 	register struct filedesc *fdp;
    366  1.1.1.3  fvdl 	register u_long com;
    367  1.1.1.3  fvdl 	register int error;
    368  1.1.1.2  fvdl 	register u_int size;
    369  1.1.1.2  fvdl 	caddr_t data, memp;
    370  1.1.1.2  fvdl 	int tmp;
    371  1.1.1.2  fvdl #define STK_PARAMS	128
    372  1.1.1.2  fvdl 	char stkbuf[STK_PARAMS];
    373  1.1.1.2  fvdl 
    374  1.1.1.2  fvdl 	fdp = p->p_fd;
    375  1.1.1.3  fvdl 	if ((u_int)SCARG(uap, fd) >= fdp->fd_nfiles ||
    376  1.1.1.3  fvdl 	    (fp = fdp->fd_ofiles[SCARG(uap, fd)]) == NULL)
    377  1.1.1.2  fvdl 		return (EBADF);
    378  1.1.1.2  fvdl 
    379  1.1.1.2  fvdl 	if ((fp->f_flag & (FREAD | FWRITE)) == 0)
    380  1.1.1.2  fvdl 		return (EBADF);
    381  1.1.1.2  fvdl 
    382  1.1.1.3  fvdl 	switch (com = SCARG(uap, com)) {
    383  1.1.1.2  fvdl 	case FIONCLEX:
    384  1.1.1.3  fvdl 		fdp->fd_ofileflags[SCARG(uap, fd)] &= ~UF_EXCLOSE;
    385  1.1.1.2  fvdl 		return (0);
    386  1.1.1.2  fvdl 	case FIOCLEX:
    387  1.1.1.3  fvdl 		fdp->fd_ofileflags[SCARG(uap, fd)] |= UF_EXCLOSE;
    388  1.1.1.2  fvdl 		return (0);
    389  1.1.1.2  fvdl 	}
    390  1.1.1.2  fvdl 
    391  1.1.1.2  fvdl 	/*
    392  1.1.1.2  fvdl 	 * Interpret high order word to find amount of data to be
    393  1.1.1.2  fvdl 	 * copied to/from the user's address space.
    394  1.1.1.2  fvdl 	 */
    395  1.1.1.2  fvdl 	size = IOCPARM_LEN(com);
    396  1.1.1.2  fvdl 	if (size > IOCPARM_MAX)
    397  1.1.1.2  fvdl 		return (ENOTTY);
    398  1.1.1.2  fvdl 	memp = NULL;
    399  1.1.1.2  fvdl 	if (size > sizeof (stkbuf)) {
    400  1.1.1.2  fvdl 		memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
    401  1.1.1.2  fvdl 		data = memp;
    402  1.1.1.2  fvdl 	} else
    403  1.1.1.2  fvdl 		data = stkbuf;
    404  1.1.1.2  fvdl 	if (com&IOC_IN) {
    405  1.1.1.2  fvdl 		if (size) {
    406  1.1.1.3  fvdl 			error = copyin(SCARG(uap, data), data, (u_int)size);
    407  1.1.1.2  fvdl 			if (error) {
    408  1.1.1.2  fvdl 				if (memp)
    409  1.1.1.2  fvdl 					free(memp, M_IOCTLOPS);
    410  1.1.1.2  fvdl 				return (error);
    411  1.1.1.2  fvdl 			}
    412  1.1.1.2  fvdl 		} else
    413  1.1.1.3  fvdl 			*(caddr_t *)data = SCARG(uap, data);
    414  1.1.1.2  fvdl 	} else if ((com&IOC_OUT) && size)
    415  1.1.1.2  fvdl 		/*
    416  1.1.1.2  fvdl 		 * Zero the buffer so the user always
    417  1.1.1.2  fvdl 		 * gets back something deterministic.
    418  1.1.1.2  fvdl 		 */
    419  1.1.1.2  fvdl 		bzero(data, size);
    420  1.1.1.2  fvdl 	else if (com&IOC_VOID)
    421  1.1.1.3  fvdl 		*(caddr_t *)data = SCARG(uap, data);
    422  1.1.1.2  fvdl 
    423  1.1.1.2  fvdl 	switch (com) {
    424  1.1.1.2  fvdl 
    425  1.1.1.2  fvdl 	case FIONBIO:
    426  1.1.1.2  fvdl 		if (tmp = *(int *)data)
    427  1.1.1.2  fvdl 			fp->f_flag |= FNONBLOCK;
    428  1.1.1.2  fvdl 		else
    429  1.1.1.2  fvdl 			fp->f_flag &= ~FNONBLOCK;
    430  1.1.1.2  fvdl 		error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
    431  1.1.1.2  fvdl 		break;
    432  1.1.1.2  fvdl 
    433  1.1.1.2  fvdl 	case FIOASYNC:
    434  1.1.1.2  fvdl 		if (tmp = *(int *)data)
    435  1.1.1.2  fvdl 			fp->f_flag |= FASYNC;
    436  1.1.1.2  fvdl 		else
    437  1.1.1.2  fvdl 			fp->f_flag &= ~FASYNC;
    438  1.1.1.2  fvdl 		error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
    439  1.1.1.2  fvdl 		break;
    440  1.1.1.2  fvdl 
    441  1.1.1.2  fvdl 	case FIOSETOWN:
    442  1.1.1.2  fvdl 		tmp = *(int *)data;
    443  1.1.1.2  fvdl 		if (fp->f_type == DTYPE_SOCKET) {
    444  1.1.1.2  fvdl 			((struct socket *)fp->f_data)->so_pgid = tmp;
    445  1.1.1.2  fvdl 			error = 0;
    446  1.1.1.2  fvdl 			break;
    447  1.1.1.2  fvdl 		}
    448  1.1.1.2  fvdl 		if (tmp <= 0) {
    449  1.1.1.2  fvdl 			tmp = -tmp;
    450  1.1.1.2  fvdl 		} else {
    451  1.1.1.2  fvdl 			struct proc *p1 = pfind(tmp);
    452  1.1.1.2  fvdl 			if (p1 == 0) {
    453  1.1.1.2  fvdl 				error = ESRCH;
    454  1.1.1.2  fvdl 				break;
    455  1.1.1.2  fvdl 			}
    456  1.1.1.2  fvdl 			tmp = p1->p_pgrp->pg_id;
    457  1.1.1.2  fvdl 		}
    458  1.1.1.2  fvdl 		error = (*fp->f_ops->fo_ioctl)
    459  1.1.1.3  fvdl 			(fp, TIOCSPGRP, (caddr_t)&tmp, p);
    460  1.1.1.2  fvdl 		break;
    461  1.1.1.2  fvdl 
    462  1.1.1.2  fvdl 	case FIOGETOWN:
    463  1.1.1.2  fvdl 		if (fp->f_type == DTYPE_SOCKET) {
    464  1.1.1.2  fvdl 			error = 0;
    465  1.1.1.2  fvdl 			*(int *)data = ((struct socket *)fp->f_data)->so_pgid;
    466  1.1.1.2  fvdl 			break;
    467  1.1.1.2  fvdl 		}
    468  1.1.1.3  fvdl 		error = (*fp->f_ops->fo_ioctl)(fp, TIOCGPGRP, data, p);
    469  1.1.1.2  fvdl 		*(int *)data = -*(int *)data;
    470  1.1.1.2  fvdl 		break;
    471  1.1.1.2  fvdl 
    472  1.1.1.2  fvdl 	default:
    473  1.1.1.2  fvdl 		error = (*fp->f_ops->fo_ioctl)(fp, com, data, p);
    474  1.1.1.2  fvdl 		/*
    475  1.1.1.2  fvdl 		 * Copy any data to user, size was
    476  1.1.1.2  fvdl 		 * already set and checked above.
    477  1.1.1.2  fvdl 		 */
    478  1.1.1.2  fvdl 		if (error == 0 && (com&IOC_OUT) && size)
    479  1.1.1.3  fvdl 			error = copyout(data, SCARG(uap, data), (u_int)size);
    480  1.1.1.2  fvdl 		break;
    481  1.1.1.2  fvdl 	}
    482  1.1.1.2  fvdl 	if (memp)
    483  1.1.1.2  fvdl 		free(memp, M_IOCTLOPS);
    484  1.1.1.2  fvdl 	return (error);
    485  1.1.1.2  fvdl }
    486  1.1.1.2  fvdl 
    487  1.1.1.2  fvdl int	selwait, nselcoll;
    488  1.1.1.2  fvdl 
    489  1.1.1.2  fvdl /*
    490  1.1.1.2  fvdl  * Select system call.
    491  1.1.1.2  fvdl  */
    492  1.1.1.3  fvdl int
    493  1.1.1.2  fvdl select(p, uap, retval)
    494  1.1.1.2  fvdl 	register struct proc *p;
    495  1.1.1.3  fvdl 	register struct select_args /* {
    496  1.1.1.3  fvdl 		syscallarg(u_int) nd;
    497  1.1.1.3  fvdl 		syscallarg(fd_set *) in;
    498  1.1.1.3  fvdl 		syscallarg(fd_set *) ou;
    499  1.1.1.3  fvdl 		syscallarg(fd_set *) ex;
    500  1.1.1.3  fvdl 		syscallarg(struct timeval *) tv;
    501  1.1.1.3  fvdl 	} */ *uap;
    502  1.1.1.3  fvdl 	register_t *retval;
    503  1.1.1.2  fvdl {
    504  1.1.1.2  fvdl 	fd_set ibits[3], obits[3];
    505  1.1.1.2  fvdl 	struct timeval atv;
    506  1.1.1.3  fvdl 	int s, ncoll, error, timo = 0;
    507  1.1.1.2  fvdl 	u_int ni;
    508  1.1.1.2  fvdl 
    509  1.1.1.2  fvdl 	bzero((caddr_t)ibits, sizeof(ibits));
    510  1.1.1.2  fvdl 	bzero((caddr_t)obits, sizeof(obits));
    511  1.1.1.3  fvdl 	if (SCARG(uap, nd) > FD_SETSIZE)
    512  1.1.1.2  fvdl 		return (EINVAL);
    513  1.1.1.3  fvdl 	if (SCARG(uap, nd) > p->p_fd->fd_nfiles) {
    514  1.1.1.3  fvdl 		/* forgiving; slightly wrong */
    515  1.1.1.3  fvdl 		SCARG(uap, nd) = p->p_fd->fd_nfiles;
    516  1.1.1.3  fvdl 	}
    517  1.1.1.3  fvdl 	ni = howmany(SCARG(uap, nd), NFDBITS) * sizeof(fd_mask);
    518  1.1.1.2  fvdl 
    519  1.1.1.2  fvdl #define	getbits(name, x) \
    520  1.1.1.3  fvdl 	if (SCARG(uap, name) && (error = copyin((caddr_t)SCARG(uap, name), \
    521  1.1.1.3  fvdl 	    (caddr_t)&ibits[x], ni))) \
    522  1.1.1.2  fvdl 		goto done;
    523  1.1.1.2  fvdl 	getbits(in, 0);
    524  1.1.1.2  fvdl 	getbits(ou, 1);
    525  1.1.1.2  fvdl 	getbits(ex, 2);
    526  1.1.1.2  fvdl #undef	getbits
    527  1.1.1.2  fvdl 
    528  1.1.1.3  fvdl 	if (SCARG(uap, tv)) {
    529  1.1.1.3  fvdl 		error = copyin((caddr_t)SCARG(uap, tv), (caddr_t)&atv,
    530  1.1.1.2  fvdl 			sizeof (atv));
    531  1.1.1.2  fvdl 		if (error)
    532  1.1.1.2  fvdl 			goto done;
    533  1.1.1.2  fvdl 		if (itimerfix(&atv)) {
    534  1.1.1.2  fvdl 			error = EINVAL;
    535  1.1.1.2  fvdl 			goto done;
    536  1.1.1.2  fvdl 		}
    537  1.1.1.2  fvdl 		s = splclock();
    538  1.1.1.2  fvdl 		timevaladd(&atv, (struct timeval *)&time);
    539  1.1.1.2  fvdl 		splx(s);
    540  1.1.1.3  fvdl 	}
    541  1.1.1.2  fvdl retry:
    542  1.1.1.2  fvdl 	ncoll = nselcoll;
    543  1.1.1.2  fvdl 	p->p_flag |= P_SELECT;
    544  1.1.1.3  fvdl 	error = selscan(p, ibits, obits, SCARG(uap, nd), retval);
    545  1.1.1.2  fvdl 	if (error || *retval)
    546  1.1.1.2  fvdl 		goto done;
    547  1.1.1.2  fvdl 	s = splhigh();
    548  1.1.1.3  fvdl 	if (SCARG(uap, tv)) {
    549  1.1.1.3  fvdl 		if (timercmp(&time, &atv, >=)) {
    550  1.1.1.3  fvdl 			splx(s);
    551  1.1.1.3  fvdl 			goto done;
    552  1.1.1.3  fvdl 		}
    553  1.1.1.3  fvdl 		/*
    554  1.1.1.3  fvdl 		 * If poll wait was tiny, this could be zero; we will
    555  1.1.1.3  fvdl 		 * have to round it up to avoid sleeping forever.  If
    556  1.1.1.3  fvdl 		 * we retry below, the timercmp above will get us out.
    557  1.1.1.3  fvdl 		 * Note that if wait was 0, the timercmp will prevent
    558  1.1.1.3  fvdl 		 * us from getting here the first time.
    559  1.1.1.3  fvdl 		 */
    560  1.1.1.3  fvdl 		timo = hzto(&atv);
    561  1.1.1.3  fvdl 		if (timo == 0)
    562  1.1.1.3  fvdl 			timo = 1;
    563  1.1.1.2  fvdl 	}
    564  1.1.1.2  fvdl 	if ((p->p_flag & P_SELECT) == 0 || nselcoll != ncoll) {
    565  1.1.1.2  fvdl 		splx(s);
    566  1.1.1.2  fvdl 		goto retry;
    567  1.1.1.2  fvdl 	}
    568  1.1.1.2  fvdl 	p->p_flag &= ~P_SELECT;
    569  1.1.1.2  fvdl 	error = tsleep((caddr_t)&selwait, PSOCK | PCATCH, "select", timo);
    570  1.1.1.2  fvdl 	splx(s);
    571  1.1.1.2  fvdl 	if (error == 0)
    572  1.1.1.2  fvdl 		goto retry;
    573  1.1.1.2  fvdl done:
    574  1.1.1.2  fvdl 	p->p_flag &= ~P_SELECT;
    575  1.1.1.2  fvdl 	/* select is not restarted after signals... */
    576  1.1.1.2  fvdl 	if (error == ERESTART)
    577  1.1.1.2  fvdl 		error = EINTR;
    578  1.1.1.2  fvdl 	if (error == EWOULDBLOCK)
    579  1.1.1.2  fvdl 		error = 0;
    580  1.1.1.2  fvdl #define	putbits(name, x) \
    581  1.1.1.3  fvdl 	if (SCARG(uap, name) && (error2 = copyout((caddr_t)&obits[x], \
    582  1.1.1.3  fvdl 	    (caddr_t)SCARG(uap, name), ni))) \
    583  1.1.1.2  fvdl 		error = error2;
    584  1.1.1.2  fvdl 	if (error == 0) {
    585  1.1.1.2  fvdl 		int error2;
    586  1.1.1.2  fvdl 
    587  1.1.1.2  fvdl 		putbits(in, 0);
    588  1.1.1.2  fvdl 		putbits(ou, 1);
    589  1.1.1.2  fvdl 		putbits(ex, 2);
    590  1.1.1.2  fvdl #undef putbits
    591  1.1.1.2  fvdl 	}
    592  1.1.1.2  fvdl 	return (error);
    593  1.1.1.2  fvdl }
    594  1.1.1.2  fvdl 
    595  1.1.1.3  fvdl int
    596  1.1.1.2  fvdl selscan(p, ibits, obits, nfd, retval)
    597  1.1.1.2  fvdl 	struct proc *p;
    598  1.1.1.2  fvdl 	fd_set *ibits, *obits;
    599  1.1.1.3  fvdl 	int nfd;
    600  1.1.1.3  fvdl 	register_t *retval;
    601  1.1.1.2  fvdl {
    602  1.1.1.2  fvdl 	register struct filedesc *fdp = p->p_fd;
    603  1.1.1.2  fvdl 	register int msk, i, j, fd;
    604  1.1.1.2  fvdl 	register fd_mask bits;
    605  1.1.1.2  fvdl 	struct file *fp;
    606  1.1.1.2  fvdl 	int n = 0;
    607  1.1.1.2  fvdl 	static int flag[3] = { FREAD, FWRITE, 0 };
    608  1.1.1.2  fvdl 
    609  1.1.1.2  fvdl 	for (msk = 0; msk < 3; msk++) {
    610  1.1.1.2  fvdl 		for (i = 0; i < nfd; i += NFDBITS) {
    611  1.1.1.2  fvdl 			bits = ibits[msk].fds_bits[i/NFDBITS];
    612  1.1.1.2  fvdl 			while ((j = ffs(bits)) && (fd = i + --j) < nfd) {
    613  1.1.1.2  fvdl 				bits &= ~(1 << j);
    614  1.1.1.2  fvdl 				fp = fdp->fd_ofiles[fd];
    615  1.1.1.2  fvdl 				if (fp == NULL)
    616  1.1.1.2  fvdl 					return (EBADF);
    617  1.1.1.2  fvdl 				if ((*fp->f_ops->fo_select)(fp, flag[msk], p)) {
    618  1.1.1.2  fvdl 					FD_SET(fd, &obits[msk]);
    619  1.1.1.2  fvdl 					n++;
    620  1.1.1.2  fvdl 				}
    621  1.1.1.2  fvdl 			}
    622  1.1.1.2  fvdl 		}
    623  1.1.1.2  fvdl 	}
    624  1.1.1.2  fvdl 	*retval = n;
    625  1.1.1.2  fvdl 	return (0);
    626  1.1.1.2  fvdl }
    627  1.1.1.2  fvdl 
    628  1.1.1.2  fvdl /*ARGSUSED*/
    629  1.1.1.3  fvdl int
    630  1.1.1.2  fvdl seltrue(dev, flag, p)
    631  1.1.1.2  fvdl 	dev_t dev;
    632  1.1.1.2  fvdl 	int flag;
    633  1.1.1.2  fvdl 	struct proc *p;
    634  1.1.1.2  fvdl {
    635  1.1.1.2  fvdl 
    636  1.1.1.2  fvdl 	return (1);
    637  1.1.1.2  fvdl }
    638  1.1.1.2  fvdl 
    639  1.1.1.2  fvdl /*
    640  1.1.1.2  fvdl  * Record a select request.
    641  1.1.1.2  fvdl  */
    642  1.1.1.2  fvdl void
    643  1.1.1.2  fvdl selrecord(selector, sip)
    644  1.1.1.2  fvdl 	struct proc *selector;
    645  1.1.1.2  fvdl 	struct selinfo *sip;
    646  1.1.1.2  fvdl {
    647  1.1.1.2  fvdl 	struct proc *p;
    648  1.1.1.2  fvdl 	pid_t mypid;
    649  1.1.1.2  fvdl 
    650  1.1.1.2  fvdl 	mypid = selector->p_pid;
    651  1.1.1.2  fvdl 	if (sip->si_pid == mypid)
    652  1.1.1.2  fvdl 		return;
    653  1.1.1.2  fvdl 	if (sip->si_pid && (p = pfind(sip->si_pid)) &&
    654  1.1.1.2  fvdl 	    p->p_wchan == (caddr_t)&selwait)
    655  1.1.1.2  fvdl 		sip->si_flags |= SI_COLL;
    656  1.1.1.2  fvdl 	else
    657  1.1.1.2  fvdl 		sip->si_pid = mypid;
    658  1.1.1.2  fvdl }
    659  1.1.1.2  fvdl 
    660  1.1.1.2  fvdl /*
    661  1.1.1.2  fvdl  * Do a wakeup when a selectable event occurs.
    662  1.1.1.2  fvdl  */
    663  1.1.1.2  fvdl void
    664  1.1.1.2  fvdl selwakeup(sip)
    665  1.1.1.2  fvdl 	register struct selinfo *sip;
    666  1.1.1.2  fvdl {
    667  1.1.1.2  fvdl 	register struct proc *p;
    668  1.1.1.2  fvdl 	int s;
    669  1.1.1.2  fvdl 
    670  1.1.1.2  fvdl 	if (sip->si_pid == 0)
    671  1.1.1.2  fvdl 		return;
    672  1.1.1.2  fvdl 	if (sip->si_flags & SI_COLL) {
    673  1.1.1.2  fvdl 		nselcoll++;
    674  1.1.1.2  fvdl 		sip->si_flags &= ~SI_COLL;
    675  1.1.1.2  fvdl 		wakeup((caddr_t)&selwait);
    676  1.1.1.2  fvdl 	}
    677  1.1.1.2  fvdl 	p = pfind(sip->si_pid);
    678  1.1.1.2  fvdl 	sip->si_pid = 0;
    679  1.1.1.2  fvdl 	if (p != NULL) {
    680  1.1.1.2  fvdl 		s = splhigh();
    681  1.1.1.2  fvdl 		if (p->p_wchan == (caddr_t)&selwait) {
    682  1.1.1.2  fvdl 			if (p->p_stat == SSLEEP)
    683  1.1.1.2  fvdl 				setrunnable(p);
    684  1.1.1.2  fvdl 			else
    685  1.1.1.2  fvdl 				unsleep(p);
    686  1.1.1.2  fvdl 		} else if (p->p_flag & P_SELECT)
    687  1.1.1.2  fvdl 			p->p_flag &= ~P_SELECT;
    688  1.1.1.2  fvdl 		splx(s);
    689  1.1.1.2  fvdl 	}
    690  1.1.1.2  fvdl }
    691