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