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