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