Home | History | Annotate | Line # | Download | only in kern
kern_descrip.c revision 1.22
      1  1.22      cgd /*	$NetBSD: kern_descrip.c,v 1.22 1994/06/29 06:32:23 cgd Exp $	*/
      2  1.22      cgd 
      3  1.16      cgd /*
      4  1.17      cgd  * Copyright (c) 1982, 1986, 1989, 1991, 1993
      5  1.17      cgd  *	The Regents of the University of California.  All rights reserved.
      6  1.16      cgd  * (c) UNIX System Laboratories, Inc.
      7  1.16      cgd  * All or some portions of this file are derived from material licensed
      8  1.16      cgd  * to the University of California by American Telephone and Telegraph
      9  1.16      cgd  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
     10  1.16      cgd  * the permission of UNIX System Laboratories, Inc.
     11  1.16      cgd  *
     12  1.16      cgd  * Redistribution and use in source and binary forms, with or without
     13  1.16      cgd  * modification, are permitted provided that the following conditions
     14  1.16      cgd  * are met:
     15  1.16      cgd  * 1. Redistributions of source code must retain the above copyright
     16  1.16      cgd  *    notice, this list of conditions and the following disclaimer.
     17  1.16      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.16      cgd  *    notice, this list of conditions and the following disclaimer in the
     19  1.16      cgd  *    documentation and/or other materials provided with the distribution.
     20  1.16      cgd  * 3. All advertising materials mentioning features or use of this software
     21  1.16      cgd  *    must display the following acknowledgement:
     22  1.16      cgd  *	This product includes software developed by the University of
     23  1.16      cgd  *	California, Berkeley and its contributors.
     24  1.16      cgd  * 4. Neither the name of the University nor the names of its contributors
     25  1.16      cgd  *    may be used to endorse or promote products derived from this software
     26  1.16      cgd  *    without specific prior written permission.
     27  1.16      cgd  *
     28  1.16      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     29  1.16      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     30  1.16      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     31  1.16      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     32  1.16      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     33  1.16      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     34  1.16      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     35  1.16      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     36  1.16      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     37  1.16      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     38  1.16      cgd  * SUCH DAMAGE.
     39  1.16      cgd  *
     40  1.22      cgd  *	@(#)kern_descrip.c	8.6 (Berkeley) 4/19/94
     41  1.16      cgd  */
     42  1.16      cgd 
     43  1.16      cgd #include <sys/param.h>
     44  1.16      cgd #include <sys/systm.h>
     45  1.16      cgd #include <sys/filedesc.h>
     46  1.16      cgd #include <sys/kernel.h>
     47  1.16      cgd #include <sys/vnode.h>
     48  1.16      cgd #include <sys/proc.h>
     49  1.16      cgd #include <sys/file.h>
     50  1.16      cgd #include <sys/socket.h>
     51  1.16      cgd #include <sys/socketvar.h>
     52  1.16      cgd #include <sys/stat.h>
     53  1.16      cgd #include <sys/ioctl.h>
     54  1.16      cgd #include <sys/fcntl.h>
     55  1.16      cgd #include <sys/malloc.h>
     56  1.16      cgd #include <sys/syslog.h>
     57  1.17      cgd #include <sys/unistd.h>
     58  1.16      cgd #include <sys/resourcevar.h>
     59  1.16      cgd 
     60  1.16      cgd /*
     61  1.16      cgd  * Descriptor management.
     62  1.16      cgd  */
     63  1.16      cgd struct file *filehead;	/* head of list of open files */
     64  1.16      cgd int nfiles;		/* actual number of open files */
     65  1.16      cgd 
     66  1.16      cgd /*
     67  1.16      cgd  * System calls on descriptors.
     68  1.16      cgd  */
     69  1.18      cgd 
     70  1.21  mycroft #if defined(COMPAT_43) || defined(COMPAT_SUNOS) || defined(COMPAT_ULTRIX) || defined(COMPAT_HPUX)
     71  1.16      cgd /* ARGSUSED */
     72  1.18      cgd ogetdtablesize(p, uap, retval)
     73  1.16      cgd 	struct proc *p;
     74  1.17      cgd 	void *uap;
     75  1.16      cgd 	int *retval;
     76  1.16      cgd {
     77  1.16      cgd 
     78  1.17      cgd 	*retval = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
     79  1.16      cgd 	return (0);
     80  1.16      cgd }
     81  1.18      cgd #endif
     82  1.16      cgd 
     83  1.16      cgd /*
     84  1.16      cgd  * Duplicate a file descriptor.
     85  1.16      cgd  */
     86  1.16      cgd struct dup_args {
     87  1.17      cgd 	u_int	fd;
     88  1.16      cgd };
     89  1.16      cgd /* ARGSUSED */
     90  1.16      cgd dup(p, uap, retval)
     91  1.16      cgd 	struct proc *p;
     92  1.16      cgd 	struct dup_args *uap;
     93  1.16      cgd 	int *retval;
     94  1.16      cgd {
     95  1.17      cgd 	register struct filedesc *fdp;
     96  1.17      cgd 	u_int old;
     97  1.17      cgd 	int new, error;
     98  1.16      cgd 
     99  1.17      cgd 	old = uap->fd;
    100  1.16      cgd 	/*
    101  1.16      cgd 	 * XXX Compatibility
    102  1.16      cgd 	 */
    103  1.17      cgd 	if (old &~ 077) { uap->fd &= 077; return (dup2(p, uap, retval)); }
    104  1.16      cgd 
    105  1.17      cgd 	fdp = p->p_fd;
    106  1.17      cgd 	if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL)
    107  1.16      cgd 		return (EBADF);
    108  1.17      cgd 	if (error = fdalloc(p, 0, &new))
    109  1.16      cgd 		return (error);
    110  1.17      cgd 	return (finishdup(fdp, (int)old, new, retval));
    111  1.16      cgd }
    112  1.16      cgd 
    113  1.16      cgd /*
    114  1.16      cgd  * Duplicate a file descriptor to a particular value.
    115  1.16      cgd  */
    116  1.16      cgd struct dup2_args {
    117  1.16      cgd 	u_int	from;
    118  1.16      cgd 	u_int	to;
    119  1.16      cgd };
    120  1.16      cgd /* ARGSUSED */
    121  1.16      cgd dup2(p, uap, retval)
    122  1.16      cgd 	struct proc *p;
    123  1.16      cgd 	struct dup2_args *uap;
    124  1.16      cgd 	int *retval;
    125  1.16      cgd {
    126  1.16      cgd 	register struct filedesc *fdp = p->p_fd;
    127  1.16      cgd 	register u_int old = uap->from, new = uap->to;
    128  1.16      cgd 	int i, error;
    129  1.16      cgd 
    130  1.16      cgd 	if (old >= fdp->fd_nfiles ||
    131  1.17      cgd 	    fdp->fd_ofiles[old] == NULL ||
    132  1.16      cgd 	    new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
    133  1.17      cgd 	    new >= maxfiles)
    134  1.16      cgd 		return (EBADF);
    135  1.17      cgd 	if (old == new) {
    136  1.17      cgd 		*retval = new;
    137  1.16      cgd 		return (0);
    138  1.17      cgd 	}
    139  1.16      cgd 	if (new >= fdp->fd_nfiles) {
    140  1.16      cgd 		if (error = fdalloc(p, new, &i))
    141  1.16      cgd 			return (error);
    142  1.16      cgd 		if (new != i)
    143  1.16      cgd 			panic("dup2: fdalloc");
    144  1.16      cgd 	} else if (fdp->fd_ofiles[new]) {
    145  1.16      cgd 		if (fdp->fd_ofileflags[new] & UF_MAPPED)
    146  1.16      cgd 			(void) munmapfd(p, new);
    147  1.16      cgd 		/*
    148  1.16      cgd 		 * dup2() must succeed even if the close has an error.
    149  1.16      cgd 		 */
    150  1.16      cgd 		(void) closef(fdp->fd_ofiles[new], p);
    151  1.16      cgd 	}
    152  1.17      cgd 	return (finishdup(fdp, (int)old, (int)new, retval));
    153  1.16      cgd }
    154  1.16      cgd 
    155  1.16      cgd /*
    156  1.16      cgd  * The file control system call.
    157  1.16      cgd  */
    158  1.16      cgd struct fcntl_args {
    159  1.16      cgd 	int	fd;
    160  1.16      cgd 	int	cmd;
    161  1.16      cgd 	int	arg;
    162  1.16      cgd };
    163  1.16      cgd /* ARGSUSED */
    164  1.16      cgd fcntl(p, uap, retval)
    165  1.16      cgd 	struct proc *p;
    166  1.16      cgd 	register struct fcntl_args *uap;
    167  1.16      cgd 	int *retval;
    168  1.16      cgd {
    169  1.16      cgd 	register struct filedesc *fdp = p->p_fd;
    170  1.16      cgd 	register struct file *fp;
    171  1.16      cgd 	register char *pop;
    172  1.16      cgd 	struct vnode *vp;
    173  1.16      cgd 	int i, tmp, error, flg = F_POSIX;
    174  1.16      cgd 	struct flock fl;
    175  1.17      cgd 	u_int newmin;
    176  1.16      cgd 
    177  1.16      cgd 	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
    178  1.16      cgd 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
    179  1.16      cgd 		return (EBADF);
    180  1.16      cgd 	pop = &fdp->fd_ofileflags[uap->fd];
    181  1.17      cgd 	switch (uap->cmd) {
    182  1.17      cgd 
    183  1.16      cgd 	case F_DUPFD:
    184  1.17      cgd 		newmin = uap->arg;
    185  1.17      cgd 		if (newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
    186  1.17      cgd 		    newmin >= maxfiles)
    187  1.16      cgd 			return (EINVAL);
    188  1.17      cgd 		if (error = fdalloc(p, newmin, &i))
    189  1.16      cgd 			return (error);
    190  1.17      cgd 		return (finishdup(fdp, uap->fd, i, retval));
    191  1.16      cgd 
    192  1.16      cgd 	case F_GETFD:
    193  1.16      cgd 		*retval = *pop & 1;
    194  1.16      cgd 		return (0);
    195  1.16      cgd 
    196  1.16      cgd 	case F_SETFD:
    197  1.16      cgd 		*pop = (*pop &~ 1) | (uap->arg & 1);
    198  1.16      cgd 		return (0);
    199  1.16      cgd 
    200  1.16      cgd 	case F_GETFL:
    201  1.16      cgd 		*retval = OFLAGS(fp->f_flag);
    202  1.16      cgd 		return (0);
    203  1.16      cgd 
    204  1.16      cgd 	case F_SETFL:
    205  1.16      cgd 		fp->f_flag &= ~FCNTLFLAGS;
    206  1.16      cgd 		fp->f_flag |= FFLAGS(uap->arg) & FCNTLFLAGS;
    207  1.16      cgd 		tmp = fp->f_flag & FNONBLOCK;
    208  1.16      cgd 		error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
    209  1.16      cgd 		if (error)
    210  1.16      cgd 			return (error);
    211  1.16      cgd 		tmp = fp->f_flag & FASYNC;
    212  1.16      cgd 		error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
    213  1.16      cgd 		if (!error)
    214  1.16      cgd 			return (0);
    215  1.16      cgd 		fp->f_flag &= ~FNONBLOCK;
    216  1.16      cgd 		tmp = 0;
    217  1.16      cgd 		(void) (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
    218  1.16      cgd 		return (error);
    219  1.16      cgd 
    220  1.16      cgd 	case F_GETOWN:
    221  1.16      cgd 		if (fp->f_type == DTYPE_SOCKET) {
    222  1.16      cgd 			*retval = ((struct socket *)fp->f_data)->so_pgid;
    223  1.16      cgd 			return (0);
    224  1.16      cgd 		}
    225  1.16      cgd 		error = (*fp->f_ops->fo_ioctl)
    226  1.16      cgd 			(fp, (int)TIOCGPGRP, (caddr_t)retval, p);
    227  1.16      cgd 		*retval = -*retval;
    228  1.16      cgd 		return (error);
    229  1.16      cgd 
    230  1.16      cgd 	case F_SETOWN:
    231  1.16      cgd 		if (fp->f_type == DTYPE_SOCKET) {
    232  1.16      cgd 			((struct socket *)fp->f_data)->so_pgid = uap->arg;
    233  1.16      cgd 			return (0);
    234  1.16      cgd 		}
    235  1.16      cgd 		if (uap->arg <= 0) {
    236  1.16      cgd 			uap->arg = -uap->arg;
    237  1.16      cgd 		} else {
    238  1.16      cgd 			struct proc *p1 = pfind(uap->arg);
    239  1.16      cgd 			if (p1 == 0)
    240  1.16      cgd 				return (ESRCH);
    241  1.16      cgd 			uap->arg = p1->p_pgrp->pg_id;
    242  1.16      cgd 		}
    243  1.16      cgd 		return ((*fp->f_ops->fo_ioctl)
    244  1.16      cgd 			(fp, (int)TIOCSPGRP, (caddr_t)&uap->arg, p));
    245  1.16      cgd 
    246  1.16      cgd 	case F_SETLKW:
    247  1.16      cgd 		flg |= F_WAIT;
    248  1.16      cgd 		/* Fall into F_SETLK */
    249  1.16      cgd 
    250  1.16      cgd 	case F_SETLK:
    251  1.16      cgd 		if (fp->f_type != DTYPE_VNODE)
    252  1.16      cgd 			return (EBADF);
    253  1.16      cgd 		vp = (struct vnode *)fp->f_data;
    254  1.16      cgd 		/* Copy in the lock structure */
    255  1.16      cgd 		error = copyin((caddr_t)uap->arg, (caddr_t)&fl, sizeof (fl));
    256  1.16      cgd 		if (error)
    257  1.16      cgd 			return (error);
    258  1.16      cgd 		if (fl.l_whence == SEEK_CUR)
    259  1.16      cgd 			fl.l_start += fp->f_offset;
    260  1.16      cgd 		switch (fl.l_type) {
    261  1.16      cgd 
    262  1.16      cgd 		case F_RDLCK:
    263  1.16      cgd 			if ((fp->f_flag & FREAD) == 0)
    264  1.16      cgd 				return (EBADF);
    265  1.16      cgd 			p->p_flag |= P_ADVLOCK;
    266  1.16      cgd 			return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
    267  1.16      cgd 
    268  1.16      cgd 		case F_WRLCK:
    269  1.16      cgd 			if ((fp->f_flag & FWRITE) == 0)
    270  1.16      cgd 				return (EBADF);
    271  1.16      cgd 			p->p_flag |= P_ADVLOCK;
    272  1.16      cgd 			return (VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg));
    273  1.16      cgd 
    274  1.16      cgd 		case F_UNLCK:
    275  1.16      cgd 			return (VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &fl,
    276  1.16      cgd 				F_POSIX));
    277  1.16      cgd 
    278  1.16      cgd 		default:
    279  1.16      cgd 			return (EINVAL);
    280  1.16      cgd 		}
    281  1.16      cgd 
    282  1.16      cgd 	case F_GETLK:
    283  1.16      cgd 		if (fp->f_type != DTYPE_VNODE)
    284  1.16      cgd 			return (EBADF);
    285  1.16      cgd 		vp = (struct vnode *)fp->f_data;
    286  1.16      cgd 		/* Copy in the lock structure */
    287  1.16      cgd 		error = copyin((caddr_t)uap->arg, (caddr_t)&fl, sizeof (fl));
    288  1.16      cgd 		if (error)
    289  1.16      cgd 			return (error);
    290  1.16      cgd 		if (fl.l_whence == SEEK_CUR)
    291  1.16      cgd 			fl.l_start += fp->f_offset;
    292  1.16      cgd 		if (error = VOP_ADVLOCK(vp, (caddr_t)p, F_GETLK, &fl, F_POSIX))
    293  1.16      cgd 			return (error);
    294  1.16      cgd 		return (copyout((caddr_t)&fl, (caddr_t)uap->arg, sizeof (fl)));
    295  1.16      cgd 
    296  1.16      cgd 	default:
    297  1.16      cgd 		return (EINVAL);
    298  1.16      cgd 	}
    299  1.16      cgd 	/* NOTREACHED */
    300  1.16      cgd }
    301  1.16      cgd 
    302  1.16      cgd /*
    303  1.17      cgd  * Common code for dup, dup2, and fcntl(F_DUPFD).
    304  1.17      cgd  */
    305  1.17      cgd int
    306  1.17      cgd finishdup(fdp, old, new, retval)
    307  1.17      cgd 	register struct filedesc *fdp;
    308  1.17      cgd 	register int old, new, *retval;
    309  1.17      cgd {
    310  1.17      cgd 	register struct file *fp;
    311  1.17      cgd 
    312  1.17      cgd 	fp = fdp->fd_ofiles[old];
    313  1.17      cgd 	fdp->fd_ofiles[new] = fp;
    314  1.17      cgd 	fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
    315  1.17      cgd 	fp->f_count++;
    316  1.17      cgd 	if (new > fdp->fd_lastfile)
    317  1.17      cgd 		fdp->fd_lastfile = new;
    318  1.17      cgd 	*retval = new;
    319  1.17      cgd 	return (0);
    320  1.17      cgd }
    321  1.17      cgd 
    322  1.17      cgd /*
    323  1.16      cgd  * Close a file descriptor.
    324  1.16      cgd  */
    325  1.16      cgd struct close_args {
    326  1.16      cgd 	int	fd;
    327  1.16      cgd };
    328  1.16      cgd /* ARGSUSED */
    329  1.16      cgd close(p, uap, retval)
    330  1.16      cgd 	struct proc *p;
    331  1.16      cgd 	struct close_args *uap;
    332  1.16      cgd 	int *retval;
    333  1.16      cgd {
    334  1.16      cgd 	register struct filedesc *fdp = p->p_fd;
    335  1.16      cgd 	register struct file *fp;
    336  1.16      cgd 	register int fd = uap->fd;
    337  1.16      cgd 	register u_char *pf;
    338  1.16      cgd 
    339  1.16      cgd 	if ((unsigned)fd >= fdp->fd_nfiles ||
    340  1.16      cgd 	    (fp = fdp->fd_ofiles[fd]) == NULL)
    341  1.16      cgd 		return (EBADF);
    342  1.16      cgd 	pf = (u_char *)&fdp->fd_ofileflags[fd];
    343  1.16      cgd 	if (*pf & UF_MAPPED)
    344  1.16      cgd 		(void) munmapfd(p, fd);
    345  1.16      cgd 	fdp->fd_ofiles[fd] = NULL;
    346  1.16      cgd 	while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
    347  1.16      cgd 		fdp->fd_lastfile--;
    348  1.16      cgd 	if (fd < fdp->fd_freefile)
    349  1.16      cgd 		fdp->fd_freefile = fd;
    350  1.16      cgd 	*pf = 0;
    351  1.16      cgd 	return (closef(fp, p));
    352  1.16      cgd }
    353  1.16      cgd 
    354  1.17      cgd #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
    355  1.16      cgd /*
    356  1.16      cgd  * Return status information about a file descriptor.
    357  1.16      cgd  */
    358  1.16      cgd struct ofstat_args {
    359  1.16      cgd 	int	fd;
    360  1.17      cgd 	struct	ostat *sb;
    361  1.16      cgd };
    362  1.16      cgd /* ARGSUSED */
    363  1.16      cgd ofstat(p, uap, retval)
    364  1.16      cgd 	struct proc *p;
    365  1.16      cgd 	register struct ofstat_args *uap;
    366  1.16      cgd 	int *retval;
    367  1.16      cgd {
    368  1.16      cgd 	register struct filedesc *fdp = p->p_fd;
    369  1.16      cgd 	register struct file *fp;
    370  1.17      cgd 	struct stat ub;
    371  1.17      cgd 	struct ostat oub;
    372  1.16      cgd 	int error;
    373  1.16      cgd 
    374  1.16      cgd 	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
    375  1.16      cgd 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
    376  1.16      cgd 		return (EBADF);
    377  1.16      cgd 	switch (fp->f_type) {
    378  1.16      cgd 
    379  1.16      cgd 	case DTYPE_VNODE:
    380  1.17      cgd 		error = vn_stat((struct vnode *)fp->f_data, &ub, p);
    381  1.16      cgd 		break;
    382  1.16      cgd 
    383  1.16      cgd 	case DTYPE_SOCKET:
    384  1.17      cgd 		error = soo_stat((struct socket *)fp->f_data, &ub);
    385  1.16      cgd 		break;
    386  1.16      cgd 
    387  1.16      cgd 	default:
    388  1.16      cgd 		panic("ofstat");
    389  1.16      cgd 		/*NOTREACHED*/
    390  1.16      cgd 	}
    391  1.17      cgd 	cvtstat(&ub, &oub);
    392  1.17      cgd 	if (error == 0)
    393  1.17      cgd 		error = copyout((caddr_t)&oub, (caddr_t)uap->sb, sizeof (oub));
    394  1.16      cgd 	return (error);
    395  1.16      cgd }
    396  1.17      cgd #endif /* COMPAT_43 || COMPAT_SUNOS */
    397  1.16      cgd 
    398  1.17      cgd /*
    399  1.17      cgd  * Return status information about a file descriptor.
    400  1.17      cgd  */
    401  1.16      cgd struct fstat_args {
    402  1.16      cgd 	int	fd;
    403  1.17      cgd 	struct	stat *sb;
    404  1.16      cgd };
    405  1.16      cgd /* ARGSUSED */
    406  1.16      cgd fstat(p, uap, retval)
    407  1.16      cgd 	struct proc *p;
    408  1.16      cgd 	register struct fstat_args *uap;
    409  1.16      cgd 	int *retval;
    410  1.16      cgd {
    411  1.16      cgd 	register struct filedesc *fdp = p->p_fd;
    412  1.16      cgd 	register struct file *fp;
    413  1.16      cgd 	struct stat ub;
    414  1.16      cgd 	int error;
    415  1.16      cgd 
    416  1.16      cgd 	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
    417  1.16      cgd 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
    418  1.16      cgd 		return (EBADF);
    419  1.16      cgd 	switch (fp->f_type) {
    420  1.16      cgd 
    421  1.16      cgd 	case DTYPE_VNODE:
    422  1.16      cgd 		error = vn_stat((struct vnode *)fp->f_data, &ub, p);
    423  1.16      cgd 		break;
    424  1.16      cgd 
    425  1.16      cgd 	case DTYPE_SOCKET:
    426  1.16      cgd 		error = soo_stat((struct socket *)fp->f_data, &ub);
    427  1.16      cgd 		break;
    428  1.16      cgd 
    429  1.16      cgd 	default:
    430  1.16      cgd 		panic("fstat");
    431  1.16      cgd 		/*NOTREACHED*/
    432  1.16      cgd 	}
    433  1.16      cgd 	if (error == 0)
    434  1.16      cgd 		error = copyout((caddr_t)&ub, (caddr_t)uap->sb, sizeof (ub));
    435  1.16      cgd 	return (error);
    436  1.16      cgd }
    437  1.16      cgd 
    438  1.16      cgd /*
    439  1.16      cgd  * Return pathconf information about a file descriptor.
    440  1.16      cgd  */
    441  1.16      cgd struct fpathconf_args {
    442  1.16      cgd 	int	fd;
    443  1.16      cgd 	int	name;
    444  1.16      cgd };
    445  1.16      cgd /* ARGSUSED */
    446  1.16      cgd fpathconf(p, uap, retval)
    447  1.16      cgd 	struct proc *p;
    448  1.16      cgd 	register struct fpathconf_args *uap;
    449  1.16      cgd 	int *retval;
    450  1.16      cgd {
    451  1.17      cgd 	struct filedesc *fdp = p->p_fd;
    452  1.17      cgd 	struct file *fp;
    453  1.17      cgd 	struct vnode *vp;
    454  1.17      cgd 
    455  1.17      cgd 	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
    456  1.17      cgd 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
    457  1.17      cgd 		return (EBADF);
    458  1.17      cgd 	switch (fp->f_type) {
    459  1.16      cgd 
    460  1.17      cgd 	case DTYPE_SOCKET:
    461  1.17      cgd 		if (uap->name != _PC_PIPE_BUF)
    462  1.17      cgd 			return (EINVAL);
    463  1.17      cgd 		*retval = PIPE_BUF;
    464  1.17      cgd 		return (0);
    465  1.17      cgd 
    466  1.17      cgd 	case DTYPE_VNODE:
    467  1.17      cgd 		vp = (struct vnode *)fp->f_data;
    468  1.17      cgd #ifdef notyet
    469  1.17      cgd 		return (VOP_PATHCONF(vp, uap->name, retval));
    470  1.17      cgd #else
    471  1.17      cgd 		return (ENOSYS);
    472  1.17      cgd #endif
    473  1.17      cgd 
    474  1.17      cgd 	default:
    475  1.17      cgd 		panic("fpathconf");
    476  1.17      cgd 	}
    477  1.17      cgd 	/*NOTREACHED*/
    478  1.16      cgd }
    479  1.16      cgd 
    480  1.16      cgd /*
    481  1.16      cgd  * Allocate a file descriptor for the process.
    482  1.16      cgd  */
    483  1.16      cgd int fdexpand;
    484  1.16      cgd 
    485  1.16      cgd fdalloc(p, want, result)
    486  1.16      cgd 	struct proc *p;
    487  1.16      cgd 	int want;
    488  1.16      cgd 	int *result;
    489  1.16      cgd {
    490  1.16      cgd 	register struct filedesc *fdp = p->p_fd;
    491  1.16      cgd 	register int i;
    492  1.16      cgd 	int lim, last, nfiles;
    493  1.16      cgd 	struct file **newofile;
    494  1.16      cgd 	char *newofileflags;
    495  1.16      cgd 
    496  1.16      cgd 	/*
    497  1.16      cgd 	 * Search for a free descriptor starting at the higher
    498  1.16      cgd 	 * of want or fd_freefile.  If that fails, consider
    499  1.16      cgd 	 * expanding the ofile array.
    500  1.16      cgd 	 */
    501  1.17      cgd 	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
    502  1.16      cgd 	for (;;) {
    503  1.16      cgd 		last = min(fdp->fd_nfiles, lim);
    504  1.16      cgd 		if ((i = want) < fdp->fd_freefile)
    505  1.16      cgd 			i = fdp->fd_freefile;
    506  1.16      cgd 		for (; i < last; i++) {
    507  1.16      cgd 			if (fdp->fd_ofiles[i] == NULL) {
    508  1.16      cgd 				fdp->fd_ofileflags[i] = 0;
    509  1.16      cgd 				if (i > fdp->fd_lastfile)
    510  1.16      cgd 					fdp->fd_lastfile = i;
    511  1.16      cgd 				if (want <= fdp->fd_freefile)
    512  1.16      cgd 					fdp->fd_freefile = i;
    513  1.16      cgd 				*result = i;
    514  1.16      cgd 				return (0);
    515  1.16      cgd 			}
    516  1.16      cgd 		}
    517  1.16      cgd 
    518  1.16      cgd 		/*
    519  1.16      cgd 		 * No space in current array.  Expand?
    520  1.16      cgd 		 */
    521  1.16      cgd 		if (fdp->fd_nfiles >= lim)
    522  1.16      cgd 			return (EMFILE);
    523  1.16      cgd 		if (fdp->fd_nfiles < NDEXTENT)
    524  1.16      cgd 			nfiles = NDEXTENT;
    525  1.16      cgd 		else
    526  1.16      cgd 			nfiles = 2 * fdp->fd_nfiles;
    527  1.16      cgd 		MALLOC(newofile, struct file **, nfiles * OFILESIZE,
    528  1.16      cgd 		    M_FILEDESC, M_WAITOK);
    529  1.16      cgd 		newofileflags = (char *) &newofile[nfiles];
    530  1.16      cgd 		/*
    531  1.16      cgd 		 * Copy the existing ofile and ofileflags arrays
    532  1.16      cgd 		 * and zero the new portion of each array.
    533  1.16      cgd 		 */
    534  1.16      cgd 		bcopy(fdp->fd_ofiles, newofile,
    535  1.16      cgd 			(i = sizeof(struct file *) * fdp->fd_nfiles));
    536  1.16      cgd 		bzero((char *)newofile + i, nfiles * sizeof(struct file *) - i);
    537  1.16      cgd 		bcopy(fdp->fd_ofileflags, newofileflags,
    538  1.16      cgd 			(i = sizeof(char) * fdp->fd_nfiles));
    539  1.16      cgd 		bzero(newofileflags + i, nfiles * sizeof(char) - i);
    540  1.16      cgd 		if (fdp->fd_nfiles > NDFILE)
    541  1.16      cgd 			FREE(fdp->fd_ofiles, M_FILEDESC);
    542  1.16      cgd 		fdp->fd_ofiles = newofile;
    543  1.16      cgd 		fdp->fd_ofileflags = newofileflags;
    544  1.16      cgd 		fdp->fd_nfiles = nfiles;
    545  1.16      cgd 		fdexpand++;
    546  1.16      cgd 	}
    547  1.16      cgd }
    548  1.16      cgd 
    549  1.16      cgd /*
    550  1.16      cgd  * Check to see whether n user file descriptors
    551  1.16      cgd  * are available to the process p.
    552  1.16      cgd  */
    553  1.16      cgd fdavail(p, n)
    554  1.16      cgd 	struct proc *p;
    555  1.16      cgd 	register int n;
    556  1.16      cgd {
    557  1.16      cgd 	register struct filedesc *fdp = p->p_fd;
    558  1.16      cgd 	register struct file **fpp;
    559  1.17      cgd 	register int i, lim;
    560  1.16      cgd 
    561  1.17      cgd 	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
    562  1.17      cgd 	if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
    563  1.16      cgd 		return (1);
    564  1.16      cgd 	fpp = &fdp->fd_ofiles[fdp->fd_freefile];
    565  1.16      cgd 	for (i = fdp->fd_nfiles - fdp->fd_freefile; --i >= 0; fpp++)
    566  1.16      cgd 		if (*fpp == NULL && --n <= 0)
    567  1.16      cgd 			return (1);
    568  1.16      cgd 	return (0);
    569  1.16      cgd }
    570  1.16      cgd 
    571  1.16      cgd /*
    572  1.16      cgd  * Create a new open file structure and allocate
    573  1.16      cgd  * a file decriptor for the process that refers to it.
    574  1.16      cgd  */
    575  1.16      cgd falloc(p, resultfp, resultfd)
    576  1.16      cgd 	register struct proc *p;
    577  1.16      cgd 	struct file **resultfp;
    578  1.16      cgd 	int *resultfd;
    579  1.16      cgd {
    580  1.16      cgd 	register struct file *fp, *fq, **fpp;
    581  1.16      cgd 	int error, i;
    582  1.16      cgd 
    583  1.16      cgd 	if (error = fdalloc(p, 0, &i))
    584  1.16      cgd 		return (error);
    585  1.16      cgd 	if (nfiles >= maxfiles) {
    586  1.16      cgd 		tablefull("file");
    587  1.16      cgd 		return (ENFILE);
    588  1.16      cgd 	}
    589  1.16      cgd 	/*
    590  1.16      cgd 	 * Allocate a new file descriptor.
    591  1.16      cgd 	 * If the process has file descriptor zero open, add to the list
    592  1.16      cgd 	 * of open files at that point, otherwise put it at the front of
    593  1.16      cgd 	 * the list of open files.
    594  1.16      cgd 	 */
    595  1.16      cgd 	nfiles++;
    596  1.16      cgd 	MALLOC(fp, struct file *, sizeof(struct file), M_FILE, M_WAITOK);
    597  1.17      cgd 	bzero(fp, sizeof(struct file));
    598  1.16      cgd 	if (fq = p->p_fd->fd_ofiles[0])
    599  1.16      cgd 		fpp = &fq->f_filef;
    600  1.16      cgd 	else
    601  1.16      cgd 		fpp = &filehead;
    602  1.16      cgd 	p->p_fd->fd_ofiles[i] = fp;
    603  1.16      cgd 	if (fq = *fpp)
    604  1.16      cgd 		fq->f_fileb = &fp->f_filef;
    605  1.16      cgd 	fp->f_filef = fq;
    606  1.16      cgd 	fp->f_fileb = fpp;
    607  1.16      cgd 	*fpp = fp;
    608  1.16      cgd 	fp->f_count = 1;
    609  1.16      cgd 	fp->f_cred = p->p_ucred;
    610  1.16      cgd 	crhold(fp->f_cred);
    611  1.16      cgd 	if (resultfp)
    612  1.16      cgd 		*resultfp = fp;
    613  1.16      cgd 	if (resultfd)
    614  1.16      cgd 		*resultfd = i;
    615  1.16      cgd 	return (0);
    616  1.16      cgd }
    617  1.16      cgd 
    618  1.16      cgd /*
    619  1.16      cgd  * Free a file descriptor.
    620  1.16      cgd  */
    621  1.16      cgd ffree(fp)
    622  1.16      cgd 	register struct file *fp;
    623  1.16      cgd {
    624  1.16      cgd 	register struct file *fq;
    625  1.16      cgd 
    626  1.16      cgd 	if (fq = fp->f_filef)
    627  1.16      cgd 		fq->f_fileb = fp->f_fileb;
    628  1.16      cgd 	*fp->f_fileb = fq;
    629  1.16      cgd 	crfree(fp->f_cred);
    630  1.16      cgd #ifdef DIAGNOSTIC
    631  1.16      cgd 	fp->f_filef = NULL;
    632  1.16      cgd 	fp->f_fileb = NULL;
    633  1.16      cgd 	fp->f_count = 0;
    634  1.16      cgd #endif
    635  1.16      cgd 	nfiles--;
    636  1.16      cgd 	FREE(fp, M_FILE);
    637  1.16      cgd }
    638  1.16      cgd 
    639  1.16      cgd /*
    640  1.16      cgd  * Copy a filedesc structure.
    641  1.16      cgd  */
    642  1.16      cgd struct filedesc *
    643  1.16      cgd fdcopy(p)
    644  1.16      cgd 	struct proc *p;
    645  1.16      cgd {
    646  1.16      cgd 	register struct filedesc *newfdp, *fdp = p->p_fd;
    647  1.16      cgd 	register struct file **fpp;
    648  1.16      cgd 	register int i;
    649  1.16      cgd 
    650  1.16      cgd 	MALLOC(newfdp, struct filedesc *, sizeof(struct filedesc0),
    651  1.16      cgd 	    M_FILEDESC, M_WAITOK);
    652  1.16      cgd 	bcopy(fdp, newfdp, sizeof(struct filedesc));
    653  1.16      cgd 	VREF(newfdp->fd_cdir);
    654  1.16      cgd 	if (newfdp->fd_rdir)
    655  1.16      cgd 		VREF(newfdp->fd_rdir);
    656  1.16      cgd 	newfdp->fd_refcnt = 1;
    657  1.16      cgd 
    658  1.16      cgd 	/*
    659  1.16      cgd 	 * If the number of open files fits in the internal arrays
    660  1.16      cgd 	 * of the open file structure, use them, otherwise allocate
    661  1.16      cgd 	 * additional memory for the number of descriptors currently
    662  1.16      cgd 	 * in use.
    663  1.16      cgd 	 */
    664  1.16      cgd 	if (newfdp->fd_lastfile < NDFILE) {
    665  1.16      cgd 		newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
    666  1.16      cgd 		newfdp->fd_ofileflags =
    667  1.16      cgd 		    ((struct filedesc0 *) newfdp)->fd_dfileflags;
    668  1.16      cgd 		i = NDFILE;
    669  1.16      cgd 	} else {
    670  1.16      cgd 		/*
    671  1.16      cgd 		 * Compute the smallest multiple of NDEXTENT needed
    672  1.16      cgd 		 * for the file descriptors currently in use,
    673  1.16      cgd 		 * allowing the table to shrink.
    674  1.16      cgd 		 */
    675  1.16      cgd 		i = newfdp->fd_nfiles;
    676  1.17      cgd 		while (i > 2 * NDEXTENT && i > newfdp->fd_lastfile * 2)
    677  1.16      cgd 			i /= 2;
    678  1.16      cgd 		MALLOC(newfdp->fd_ofiles, struct file **, i * OFILESIZE,
    679  1.16      cgd 		    M_FILEDESC, M_WAITOK);
    680  1.16      cgd 		newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
    681  1.16      cgd 	}
    682  1.16      cgd 	newfdp->fd_nfiles = i;
    683  1.16      cgd 	bcopy(fdp->fd_ofiles, newfdp->fd_ofiles, i * sizeof(struct file **));
    684  1.16      cgd 	bcopy(fdp->fd_ofileflags, newfdp->fd_ofileflags, i * sizeof(char));
    685  1.16      cgd 	fpp = newfdp->fd_ofiles;
    686  1.16      cgd 	for (i = newfdp->fd_lastfile; i-- >= 0; fpp++)
    687  1.16      cgd 		if (*fpp != NULL)
    688  1.16      cgd 			(*fpp)->f_count++;
    689  1.16      cgd 	return (newfdp);
    690  1.16      cgd }
    691  1.16      cgd 
    692  1.16      cgd /*
    693  1.16      cgd  * Release a filedesc structure.
    694  1.16      cgd  */
    695  1.16      cgd void
    696  1.16      cgd fdfree(p)
    697  1.16      cgd 	struct proc *p;
    698  1.16      cgd {
    699  1.16      cgd 	register struct filedesc *fdp = p->p_fd;
    700  1.16      cgd 	struct file **fpp;
    701  1.16      cgd 	register int i;
    702  1.16      cgd 
    703  1.16      cgd 	if (--fdp->fd_refcnt > 0)
    704  1.16      cgd 		return;
    705  1.16      cgd 	fpp = fdp->fd_ofiles;
    706  1.16      cgd 	for (i = fdp->fd_lastfile; i-- >= 0; fpp++)
    707  1.16      cgd 		if (*fpp)
    708  1.16      cgd 			(void) closef(*fpp, p);
    709  1.16      cgd 	if (fdp->fd_nfiles > NDFILE)
    710  1.16      cgd 		FREE(fdp->fd_ofiles, M_FILEDESC);
    711  1.16      cgd 	vrele(fdp->fd_cdir);
    712  1.16      cgd 	if (fdp->fd_rdir)
    713  1.16      cgd 		vrele(fdp->fd_rdir);
    714  1.16      cgd 	FREE(fdp, M_FILEDESC);
    715  1.16      cgd }
    716  1.16      cgd 
    717  1.16      cgd /*
    718  1.16      cgd  * Close any files on exec?
    719  1.16      cgd  */
    720  1.16      cgd void
    721  1.16      cgd fdcloseexec(p)
    722  1.16      cgd 	struct proc *p;
    723  1.16      cgd {
    724  1.16      cgd 	struct filedesc *fdp = p->p_fd;
    725  1.16      cgd 	struct file **fpp;
    726  1.16      cgd 	char *fdfp;
    727  1.16      cgd 	register int i;
    728  1.16      cgd 
    729  1.16      cgd 	fpp = fdp->fd_ofiles;
    730  1.16      cgd 	fdfp = fdp->fd_ofileflags;
    731  1.16      cgd 	for (i = 0; i <= fdp->fd_lastfile; i++, fpp++, fdfp++)
    732  1.16      cgd 		if (*fpp != NULL && (*fdfp & UF_EXCLOSE)) {
    733  1.16      cgd 			if (*fdfp & UF_MAPPED)
    734  1.16      cgd 				(void) munmapfd(p, i);
    735  1.16      cgd 			(void) closef(*fpp, p);
    736  1.16      cgd 			*fpp = NULL;
    737  1.16      cgd 			*fdfp = 0;
    738  1.16      cgd 			if (i < fdp->fd_freefile)
    739  1.16      cgd 				fdp->fd_freefile = i;
    740  1.16      cgd 		}
    741  1.16      cgd 	while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
    742  1.16      cgd 		fdp->fd_lastfile--;
    743  1.16      cgd }
    744  1.16      cgd 
    745  1.16      cgd /*
    746  1.16      cgd  * Internal form of close.
    747  1.16      cgd  * Decrement reference count on file structure.
    748  1.17      cgd  * Note: p may be NULL when closing a file
    749  1.17      cgd  * that was being passed in a message.
    750  1.16      cgd  */
    751  1.16      cgd closef(fp, p)
    752  1.16      cgd 	register struct file *fp;
    753  1.16      cgd 	register struct proc *p;
    754  1.16      cgd {
    755  1.16      cgd 	struct vnode *vp;
    756  1.16      cgd 	struct flock lf;
    757  1.16      cgd 	int error;
    758  1.16      cgd 
    759  1.16      cgd 	if (fp == NULL)
    760  1.16      cgd 		return (0);
    761  1.16      cgd 	/*
    762  1.16      cgd 	 * POSIX record locking dictates that any close releases ALL
    763  1.16      cgd 	 * locks owned by this process.  This is handled by setting
    764  1.16      cgd 	 * a flag in the unlock to free ONLY locks obeying POSIX
    765  1.16      cgd 	 * semantics, and not to free BSD-style file locks.
    766  1.17      cgd 	 * If the descriptor was in a message, POSIX-style locks
    767  1.17      cgd 	 * aren't passed with the descriptor.
    768  1.16      cgd 	 */
    769  1.16      cgd 	if (p && (p->p_flag & P_ADVLOCK) && fp->f_type == DTYPE_VNODE) {
    770  1.16      cgd 		lf.l_whence = SEEK_SET;
    771  1.16      cgd 		lf.l_start = 0;
    772  1.16      cgd 		lf.l_len = 0;
    773  1.16      cgd 		lf.l_type = F_UNLCK;
    774  1.16      cgd 		vp = (struct vnode *)fp->f_data;
    775  1.16      cgd 		(void) VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_POSIX);
    776  1.16      cgd 	}
    777  1.16      cgd 	if (--fp->f_count > 0)
    778  1.16      cgd 		return (0);
    779  1.16      cgd 	if (fp->f_count < 0)
    780  1.16      cgd 		panic("closef: count < 0");
    781  1.16      cgd 	if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
    782  1.16      cgd 		lf.l_whence = SEEK_SET;
    783  1.16      cgd 		lf.l_start = 0;
    784  1.16      cgd 		lf.l_len = 0;
    785  1.16      cgd 		lf.l_type = F_UNLCK;
    786  1.16      cgd 		vp = (struct vnode *)fp->f_data;
    787  1.16      cgd 		(void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
    788  1.16      cgd 	}
    789  1.17      cgd 	if (fp->f_ops)
    790  1.17      cgd 		error = (*fp->f_ops->fo_close)(fp, p);
    791  1.17      cgd 	else
    792  1.17      cgd 		error = 0;
    793  1.16      cgd 	ffree(fp);
    794  1.16      cgd 	return (error);
    795  1.16      cgd }
    796  1.16      cgd 
    797  1.16      cgd /*
    798  1.16      cgd  * Apply an advisory lock on a file descriptor.
    799  1.16      cgd  *
    800  1.16      cgd  * Just attempt to get a record lock of the requested type on
    801  1.16      cgd  * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
    802  1.16      cgd  */
    803  1.16      cgd struct flock_args {
    804  1.16      cgd 	int	fd;
    805  1.16      cgd 	int	how;
    806  1.16      cgd };
    807  1.16      cgd /* ARGSUSED */
    808  1.16      cgd flock(p, uap, retval)
    809  1.16      cgd 	struct proc *p;
    810  1.16      cgd 	register struct flock_args *uap;
    811  1.16      cgd 	int *retval;
    812  1.16      cgd {
    813  1.16      cgd 	register struct filedesc *fdp = p->p_fd;
    814  1.16      cgd 	register struct file *fp;
    815  1.16      cgd 	struct vnode *vp;
    816  1.16      cgd 	struct flock lf;
    817  1.16      cgd 
    818  1.16      cgd 	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
    819  1.16      cgd 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
    820  1.16      cgd 		return (EBADF);
    821  1.16      cgd 	if (fp->f_type != DTYPE_VNODE)
    822  1.16      cgd 		return (EOPNOTSUPP);
    823  1.16      cgd 	vp = (struct vnode *)fp->f_data;
    824  1.16      cgd 	lf.l_whence = SEEK_SET;
    825  1.16      cgd 	lf.l_start = 0;
    826  1.16      cgd 	lf.l_len = 0;
    827  1.16      cgd 	if (uap->how & LOCK_UN) {
    828  1.16      cgd 		lf.l_type = F_UNLCK;
    829  1.16      cgd 		fp->f_flag &= ~FHASLOCK;
    830  1.16      cgd 		return (VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK));
    831  1.16      cgd 	}
    832  1.16      cgd 	if (uap->how & LOCK_EX)
    833  1.16      cgd 		lf.l_type = F_WRLCK;
    834  1.16      cgd 	else if (uap->how & LOCK_SH)
    835  1.16      cgd 		lf.l_type = F_RDLCK;
    836  1.16      cgd 	else
    837  1.16      cgd 		return (EBADF);
    838  1.16      cgd 	fp->f_flag |= FHASLOCK;
    839  1.16      cgd 	if (uap->how & LOCK_NB)
    840  1.16      cgd 		return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK));
    841  1.16      cgd 	return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT));
    842  1.16      cgd }
    843  1.16      cgd 
    844  1.16      cgd /*
    845  1.16      cgd  * File Descriptor pseudo-device driver (/dev/fd/).
    846  1.16      cgd  *
    847  1.16      cgd  * Opening minor device N dup()s the file (if any) connected to file
    848  1.16      cgd  * descriptor N belonging to the calling process.  Note that this driver
    849  1.16      cgd  * consists of only the ``open()'' routine, because all subsequent
    850  1.16      cgd  * references to this file will be direct to the other driver.
    851  1.16      cgd  */
    852  1.16      cgd /* ARGSUSED */
    853  1.16      cgd fdopen(dev, mode, type, p)
    854  1.16      cgd 	dev_t dev;
    855  1.16      cgd 	int mode, type;
    856  1.16      cgd 	struct proc *p;
    857  1.16      cgd {
    858  1.16      cgd 
    859  1.16      cgd 	/*
    860  1.16      cgd 	 * XXX Kludge: set curproc->p_dupfd to contain the value of the
    861  1.16      cgd 	 * the file descriptor being sought for duplication. The error
    862  1.16      cgd 	 * return ensures that the vnode for this device will be released
    863  1.16      cgd 	 * by vn_open. Open will detect this special error and take the
    864  1.16      cgd 	 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
    865  1.16      cgd 	 * will simply report the error.
    866  1.16      cgd 	 */
    867  1.17      cgd 	p->p_dupfd = minor(dev);
    868  1.16      cgd 	return (ENODEV);
    869  1.16      cgd }
    870  1.16      cgd 
    871  1.16      cgd /*
    872  1.16      cgd  * Duplicate the specified descriptor to a free descriptor.
    873  1.16      cgd  */
    874  1.16      cgd dupfdopen(fdp, indx, dfd, mode, error)
    875  1.16      cgd 	register struct filedesc *fdp;
    876  1.16      cgd 	register int indx, dfd;
    877  1.16      cgd 	int mode;
    878  1.16      cgd 	int error;
    879  1.16      cgd {
    880  1.16      cgd 	register struct file *wfp;
    881  1.16      cgd 	struct file *fp;
    882  1.16      cgd 
    883  1.16      cgd 	/*
    884  1.16      cgd 	 * If the to-be-dup'd fd number is greater than the allowed number
    885  1.16      cgd 	 * of file descriptors, or the fd to be dup'd has already been
    886  1.16      cgd 	 * closed, reject.  Note, check for new == old is necessary as
    887  1.16      cgd 	 * falloc could allocate an already closed to-be-dup'd descriptor
    888  1.16      cgd 	 * as the new descriptor.
    889  1.16      cgd 	 */
    890  1.16      cgd 	fp = fdp->fd_ofiles[indx];
    891  1.16      cgd 	if ((u_int)dfd >= fdp->fd_nfiles ||
    892  1.16      cgd 	    (wfp = fdp->fd_ofiles[dfd]) == NULL || fp == wfp)
    893  1.16      cgd 		return (EBADF);
    894  1.16      cgd 
    895  1.16      cgd 	/*
    896  1.16      cgd 	 * There are two cases of interest here.
    897  1.16      cgd 	 *
    898  1.16      cgd 	 * For ENODEV simply dup (dfd) to file descriptor
    899  1.16      cgd 	 * (indx) and return.
    900  1.16      cgd 	 *
    901  1.16      cgd 	 * For ENXIO steal away the file structure from (dfd) and
    902  1.16      cgd 	 * store it in (indx).  (dfd) is effectively closed by
    903  1.16      cgd 	 * this operation.
    904  1.16      cgd 	 *
    905  1.16      cgd 	 * Any other error code is just returned.
    906  1.16      cgd 	 */
    907  1.16      cgd 	switch (error) {
    908  1.16      cgd 	case ENODEV:
    909  1.16      cgd 		/*
    910  1.17      cgd 		 * Check that the mode the file is being opened for is a
    911  1.17      cgd 		 * subset of the mode of the existing descriptor.
    912  1.16      cgd 		 */
    913  1.16      cgd 		if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag)
    914  1.16      cgd 			return (EACCES);
    915  1.16      cgd 		fdp->fd_ofiles[indx] = wfp;
    916  1.16      cgd 		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
    917  1.16      cgd 		wfp->f_count++;
    918  1.16      cgd 		if (indx > fdp->fd_lastfile)
    919  1.16      cgd 			fdp->fd_lastfile = indx;
    920  1.16      cgd 		return (0);
    921  1.16      cgd 
    922  1.16      cgd 	case ENXIO:
    923  1.16      cgd 		/*
    924  1.16      cgd 		 * Steal away the file pointer from dfd, and stuff it into indx.
    925  1.16      cgd 		 */
    926  1.16      cgd 		fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
    927  1.16      cgd 		fdp->fd_ofiles[dfd] = NULL;
    928  1.16      cgd 		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
    929  1.16      cgd 		fdp->fd_ofileflags[dfd] = 0;
    930  1.16      cgd 		/*
    931  1.17      cgd 		 * Complete the clean up of the filedesc structure by
    932  1.17      cgd 		 * recomputing the various hints.
    933  1.16      cgd 		 */
    934  1.16      cgd 		if (indx > fdp->fd_lastfile)
    935  1.16      cgd 			fdp->fd_lastfile = indx;
    936  1.16      cgd 		else
    937  1.16      cgd 			while (fdp->fd_lastfile > 0 &&
    938  1.16      cgd 			       fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
    939  1.16      cgd 				fdp->fd_lastfile--;
    940  1.16      cgd 			if (dfd < fdp->fd_freefile)
    941  1.16      cgd 				fdp->fd_freefile = dfd;
    942  1.16      cgd 		return (0);
    943  1.16      cgd 
    944  1.16      cgd 	default:
    945  1.16      cgd 		return (error);
    946  1.16      cgd 	}
    947  1.17      cgd 	/* NOTREACHED */
    948  1.16      cgd }
    949