Home | History | Annotate | Line # | Download | only in kern
kern_descrip.c revision 1.72.2.13
      1 /*	$NetBSD: kern_descrip.c,v 1.72.2.13 2002/09/17 21:21:59 nathanw 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.8 (Berkeley) 2/14/95
     41  */
     42 
     43 #include <sys/cdefs.h>
     44 __KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.72.2.13 2002/09/17 21:21:59 nathanw Exp $");
     45 
     46 #include <sys/param.h>
     47 #include <sys/systm.h>
     48 #include <sys/filedesc.h>
     49 #include <sys/kernel.h>
     50 #include <sys/vnode.h>
     51 #include <sys/proc.h>
     52 #include <sys/file.h>
     53 #include <sys/namei.h>
     54 #include <sys/socket.h>
     55 #include <sys/socketvar.h>
     56 #include <sys/stat.h>
     57 #include <sys/ioctl.h>
     58 #include <sys/fcntl.h>
     59 #include <sys/malloc.h>
     60 #include <sys/pool.h>
     61 #include <sys/syslog.h>
     62 #include <sys/unistd.h>
     63 #include <sys/resourcevar.h>
     64 #include <sys/conf.h>
     65 
     66 #include <sys/mount.h>
     67 #include <sys/sa.h>
     68 #include <sys/syscallargs.h>
     69 
     70 /*
     71  * Descriptor management.
     72  */
     73 struct filelist	filehead;	/* head of list of open files */
     74 int		nfiles;		/* actual number of open files */
     75 struct pool	file_pool;	/* memory pool for file structures */
     76 struct pool	cwdi_pool;	/* memory pool for cwdinfo structures */
     77 struct pool	filedesc0_pool;	/* memory pool for filedesc0 structures */
     78 
     79 static __inline void	fd_used(struct filedesc *, int);
     80 static __inline void	fd_unused(struct filedesc *, int);
     81 int			finishdup(struct proc *, int, int, register_t *);
     82 int			fcntl_forfs(int, struct proc *, int, void *);
     83 
     84 dev_type_open(filedescopen);
     85 
     86 const struct cdevsw filedesc_cdevsw = {
     87 	filedescopen, noclose, noread, nowrite, noioctl,
     88 	nostop, notty, nopoll, nommap,
     89 };
     90 
     91 static __inline void
     92 fd_used(struct filedesc *fdp, int fd)
     93 {
     94 
     95 	if (fd > fdp->fd_lastfile)
     96 		fdp->fd_lastfile = fd;
     97 }
     98 
     99 static __inline void
    100 fd_unused(struct filedesc *fdp, int fd)
    101 {
    102 
    103 	if (fd < fdp->fd_freefile)
    104 		fdp->fd_freefile = fd;
    105 #ifdef DIAGNOSTIC
    106 	if (fd > fdp->fd_lastfile)
    107 		panic("fd_unused: fd_lastfile inconsistent");
    108 #endif
    109 	if (fd == fdp->fd_lastfile) {
    110 		do {
    111 			fd--;
    112 		} while (fd >= 0 && fdp->fd_ofiles[fd] == NULL);
    113 		fdp->fd_lastfile = fd;
    114 	}
    115 }
    116 
    117 struct file *
    118 fd_getfile(struct filedesc *fdp, int fd)
    119 {
    120 	struct file *fp;
    121 
    122 	if ((u_int) fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL)
    123 		return (NULL);
    124 
    125 	if (FILE_IS_USABLE(fp) == 0)
    126 		return (NULL);
    127 
    128 	return (fp);
    129 }
    130 
    131 /*
    132  * System calls on descriptors.
    133  */
    134 
    135 /*
    136  * Duplicate a file descriptor.
    137  */
    138 /* ARGSUSED */
    139 int
    140 sys_dup(struct lwp *l, void *v, register_t *retval)
    141 {
    142 	struct sys_dup_args /* {
    143 		syscallarg(int)	fd;
    144 	} */ *uap = v;
    145 	struct file	*fp;
    146 	struct filedesc	*fdp;
    147 	struct proc	*p;
    148 	int		old, new, error;
    149 
    150 	p = l->l_proc;
    151 	fdp = p->p_fd;
    152 	old = SCARG(uap, fd);
    153 
    154  restart:
    155 	if ((fp = fd_getfile(fdp, old)) == NULL)
    156 		return (EBADF);
    157 
    158 	FILE_USE(fp);
    159 
    160 	if ((error = fdalloc(p, 0, &new)) != 0) {
    161 		if (error == ENOSPC) {
    162 			fdexpand(p);
    163 			FILE_UNUSE(fp, p);
    164 			goto restart;
    165 		}
    166 		FILE_UNUSE(fp, p);
    167 		return (error);
    168 	}
    169 
    170 	/* finishdup() will unuse the descriptors for us */
    171 	return (finishdup(p, old, new, retval));
    172 }
    173 
    174 /*
    175  * Duplicate a file descriptor to a particular value.
    176  */
    177 /* ARGSUSED */
    178 int
    179 sys_dup2(struct lwp *l, void *v, register_t *retval)
    180 {
    181 	struct sys_dup2_args /* {
    182 		syscallarg(int)	from;
    183 		syscallarg(int)	to;
    184 	} */ *uap = v;
    185 	struct file	*fp;
    186 	struct filedesc	*fdp;
    187 	struct proc	*p;
    188 	int		old, new, i, error;
    189 
    190 	p = l->l_proc;
    191 	fdp = p->p_fd;
    192 	old = SCARG(uap, from);
    193 	new = SCARG(uap, to);
    194 
    195  restart:
    196 	if ((fp = fd_getfile(fdp, old)) == NULL)
    197 		return (EBADF);
    198 
    199 	if ((u_int)new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
    200 	    (u_int)new >= maxfiles)
    201 		return (EBADF);
    202 
    203 	if (old == new) {
    204 		*retval = new;
    205 		return (0);
    206 	}
    207 
    208 	FILE_USE(fp);
    209 
    210 	if (new >= fdp->fd_nfiles) {
    211 		if ((error = fdalloc(p, new, &i)) != 0) {
    212 			if (error == ENOSPC) {
    213 				fdexpand(p);
    214 				FILE_UNUSE(fp, p);
    215 				goto restart;
    216 			}
    217 			FILE_UNUSE(fp, p);
    218 			return (error);
    219 		}
    220 		if (new != i)
    221 			panic("dup2: fdalloc");
    222 	}
    223 
    224 	/*
    225 	 * finishdup() will close the file that's in the `new'
    226 	 * slot, if there's one there.
    227 	 */
    228 
    229 	/* finishdup() will unuse the descriptors for us */
    230 	return (finishdup(p, old, new, retval));
    231 }
    232 
    233 /*
    234  * The file control system call.
    235  */
    236 /* ARGSUSED */
    237 int
    238 sys_fcntl(struct lwp *l, void *v, register_t *retval)
    239 {
    240 	struct sys_fcntl_args /* {
    241 		syscallarg(int)		fd;
    242 		syscallarg(int)		cmd;
    243 		syscallarg(void *)	arg;
    244 	} */ *uap = v;
    245 	struct filedesc *fdp;
    246 	struct file	*fp;
    247 	struct proc	*p;
    248 	struct vnode	*vp;
    249 	int		fd, i, tmp, error, flg, cmd, newmin;
    250 	struct flock	fl;
    251 
    252 	p = l->l_proc;
    253 	fd = SCARG(uap, fd);
    254 	fdp = p->p_fd;
    255 	error = 0;
    256 	flg = F_POSIX;
    257 
    258  restart:
    259 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    260 		return (EBADF);
    261 
    262 	FILE_USE(fp);
    263 
    264 	cmd = SCARG(uap, cmd);
    265 	if ((cmd & F_FSCTL)) {
    266 		error = fcntl_forfs(fd, p, cmd, SCARG(uap, arg));
    267 		goto out;
    268 	}
    269 
    270 	switch (cmd) {
    271 
    272 	case F_DUPFD:
    273 		newmin = (long)SCARG(uap, arg);
    274 		if ((u_int)newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
    275 		    (u_int)newmin >= maxfiles) {
    276 			error = EINVAL;
    277 			goto out;
    278 		}
    279 		if ((error = fdalloc(p, newmin, &i)) != 0) {
    280 			if (error == ENOSPC) {
    281 				fdexpand(p);
    282 				FILE_UNUSE(fp, p);
    283 				goto restart;
    284 			}
    285 			goto out;
    286 		}
    287 
    288 		/* finishdup() will unuse the descriptors for us */
    289 		return (finishdup(p, fd, i, retval));
    290 
    291 	case F_GETFD:
    292 		*retval = fdp->fd_ofileflags[fd] & UF_EXCLOSE ? 1 : 0;
    293 		break;
    294 
    295 	case F_SETFD:
    296 		if ((long)SCARG(uap, arg) & 1)
    297 			fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
    298 		else
    299 			fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
    300 		break;
    301 
    302 	case F_GETFL:
    303 		*retval = OFLAGS(fp->f_flag);
    304 		break;
    305 
    306 	case F_SETFL:
    307 		tmp = FFLAGS((long)SCARG(uap, arg)) & FCNTLFLAGS;
    308 		error = (*fp->f_ops->fo_fcntl)(fp, F_SETFL, (caddr_t)&tmp, p);
    309 		if (error)
    310 			goto out;
    311 		fp->f_flag &= ~FCNTLFLAGS;
    312 		fp->f_flag |= tmp;
    313 		tmp = fp->f_flag & FNONBLOCK;
    314 		error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
    315 		if (error)
    316 			goto out;
    317 		tmp = fp->f_flag & FASYNC;
    318 		error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, (caddr_t)&tmp, p);
    319 		if (error == 0)
    320 			goto out;
    321 		fp->f_flag &= ~FNONBLOCK;
    322 		tmp = 0;
    323 		(void) (*fp->f_ops->fo_ioctl)(fp, FIONBIO, (caddr_t)&tmp, p);
    324 		break;
    325 
    326 	case F_GETOWN:
    327 		if (fp->f_type == DTYPE_SOCKET) {
    328 			*retval = ((struct socket *)fp->f_data)->so_pgid;
    329 			goto out;
    330 		}
    331 		error = (*fp->f_ops->fo_ioctl)
    332 			(fp, TIOCGPGRP, (caddr_t)&tmp, p);
    333 		*retval = -tmp;
    334 		break;
    335 
    336 	case F_SETOWN:
    337 		if (fp->f_type == DTYPE_SOCKET) {
    338 			((struct socket *)fp->f_data)->so_pgid =
    339 			    (long)SCARG(uap, arg);
    340 			goto out;
    341 		}
    342 		if ((long)SCARG(uap, arg) <= 0) {
    343 			tmp = (-(long)SCARG(uap, arg));
    344 		} else {
    345 			struct proc *p1 = pfind((long)SCARG(uap, arg));
    346 			if (p1 == 0) {
    347 				error = ESRCH;
    348 				goto out;
    349 			}
    350 			tmp = (long)p1->p_pgrp->pg_id;
    351 		}
    352 		error = (*fp->f_ops->fo_ioctl)
    353 		    (fp, TIOCSPGRP, (caddr_t)&tmp, p);
    354 		break;
    355 
    356 	case F_SETLKW:
    357 		flg |= F_WAIT;
    358 		/* Fall into F_SETLK */
    359 
    360 	case F_SETLK:
    361 		if (fp->f_type != DTYPE_VNODE) {
    362 			error = EINVAL;
    363 			goto out;
    364 		}
    365 		vp = (struct vnode *)fp->f_data;
    366 		/* Copy in the lock structure */
    367 		error = copyin((caddr_t)SCARG(uap, arg), (caddr_t)&fl,
    368 		    sizeof(fl));
    369 		if (error)
    370 			goto out;
    371 		if (fl.l_whence == SEEK_CUR)
    372 			fl.l_start += fp->f_offset;
    373 		switch (fl.l_type) {
    374 		case F_RDLCK:
    375 			if ((fp->f_flag & FREAD) == 0) {
    376 				error = EBADF;
    377 				goto out;
    378 			}
    379 			p->p_flag |= P_ADVLOCK;
    380 			error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg);
    381 			goto out;
    382 
    383 		case F_WRLCK:
    384 			if ((fp->f_flag & FWRITE) == 0) {
    385 				error = EBADF;
    386 				goto out;
    387 			}
    388 			p->p_flag |= P_ADVLOCK;
    389 			error = VOP_ADVLOCK(vp, (caddr_t)p, F_SETLK, &fl, flg);
    390 			goto out;
    391 
    392 		case F_UNLCK:
    393 			error = VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &fl,
    394 			    F_POSIX);
    395 			goto out;
    396 
    397 		default:
    398 			error = EINVAL;
    399 			goto out;
    400 		}
    401 
    402 	case F_GETLK:
    403 		if (fp->f_type != DTYPE_VNODE) {
    404 			error = EINVAL;
    405 			goto out;
    406 		}
    407 		vp = (struct vnode *)fp->f_data;
    408 		/* Copy in the lock structure */
    409 		error = copyin((caddr_t)SCARG(uap, arg), (caddr_t)&fl,
    410 		    sizeof(fl));
    411 		if (error)
    412 			goto out;
    413 		if (fl.l_whence == SEEK_CUR)
    414 			fl.l_start += fp->f_offset;
    415 		if (fl.l_type != F_RDLCK &&
    416 		    fl.l_type != F_WRLCK &&
    417 		    fl.l_type != F_UNLCK) {
    418 			error = EINVAL;
    419 			goto out;
    420 		}
    421 		error = VOP_ADVLOCK(vp, (caddr_t)p, F_GETLK, &fl, F_POSIX);
    422 		if (error)
    423 			goto out;
    424 		error = copyout((caddr_t)&fl, (caddr_t)SCARG(uap, arg),
    425 		    sizeof(fl));
    426 		break;
    427 
    428 	default:
    429 		error = EINVAL;
    430 	}
    431 
    432  out:
    433 	FILE_UNUSE(fp, p);
    434 	return (error);
    435 }
    436 
    437 /*
    438  * Common code for dup, dup2, and fcntl(F_DUPFD).
    439  */
    440 int
    441 finishdup(struct proc *p, int old, int new, register_t *retval)
    442 {
    443 	struct filedesc	*fdp;
    444 	struct file	*fp, *delfp;
    445 
    446 	fdp = p->p_fd;
    447 
    448 	/*
    449 	 * If there is a file in the new slot, remember it so we
    450 	 * can close it after we've finished the dup.  We need
    451 	 * to do it after the dup is finished, since closing
    452 	 * the file may block.
    453 	 *
    454 	 * Note: `old' is already used for us.
    455 	 */
    456 	delfp = fdp->fd_ofiles[new];
    457 
    458 	fp = fdp->fd_ofiles[old];
    459 	fdp->fd_ofiles[new] = fp;
    460 	fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
    461 	fp->f_count++;
    462 	/*
    463 	 * Note, don't have to mark it "used" in the table if there
    464 	 * was already a file in the `new' slot.
    465 	 */
    466 	if (delfp == NULL)
    467 		fd_used(fdp, new);
    468 	*retval = new;
    469 	FILE_UNUSE(fp, p);
    470 
    471 	if (delfp != NULL) {
    472 		FILE_USE(delfp);
    473 		(void) closef(delfp, p);
    474 	}
    475 	return (0);
    476 }
    477 
    478 void
    479 fdremove(struct filedesc *fdp, int fd)
    480 {
    481 
    482 	fdp->fd_ofiles[fd] = NULL;
    483 	fd_unused(fdp, fd);
    484 }
    485 
    486 int
    487 fdrelease(struct proc *p, int fd)
    488 {
    489 	struct filedesc	*fdp;
    490 	struct file	**fpp, *fp;
    491 
    492 	fdp = p->p_fd;
    493 	fpp = &fdp->fd_ofiles[fd];
    494 	fp = *fpp;
    495 	if (fp == NULL)
    496 		return (EBADF);
    497 
    498 	FILE_USE(fp);
    499 
    500 	*fpp = NULL;
    501 	fdp->fd_ofileflags[fd] = 0;
    502 	fd_unused(fdp, fd);
    503 	return (closef(fp, p));
    504 }
    505 
    506 /*
    507  * Close a file descriptor.
    508  */
    509 /* ARGSUSED */
    510 int
    511 sys_close(struct lwp *l, void *v, register_t *retval)
    512 {
    513 	struct sys_close_args /* {
    514 		syscallarg(int)	fd;
    515 	} */ *uap = v;
    516 	int		fd;
    517 	struct filedesc	*fdp;
    518 	struct proc *p;
    519 	struct file	*fp;
    520 
    521 	p = l->l_proc;
    522 	fd = SCARG(uap, fd);
    523 	fdp = p->p_fd;
    524 
    525 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    526 		return (EBADF);
    527 
    528 	return (fdrelease(p, fd));
    529 }
    530 
    531 /*
    532  * Return status information about a file descriptor.
    533  */
    534 /* ARGSUSED */
    535 int
    536 sys___fstat13(struct lwp *l, void *v, register_t *retval)
    537 {
    538 	struct sys___fstat13_args /* {
    539 		syscallarg(int)			fd;
    540 		syscallarg(struct stat *)	sb;
    541 	} */ *uap = v;
    542 	int		fd;
    543 	struct filedesc	*fdp;
    544 	struct file	*fp;
    545 	struct proc	*p;
    546 	struct stat	ub;
    547 	int		error;
    548 
    549 	p = l->l_proc;
    550 	fd = SCARG(uap, fd);
    551 	fdp = p->p_fd;
    552 
    553 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    554 		return (EBADF);
    555 
    556 	FILE_USE(fp);
    557 	error = (*fp->f_ops->fo_stat)(fp, &ub, p);
    558 	FILE_UNUSE(fp, p);
    559 
    560 	if (error == 0)
    561 		error = copyout(&ub, SCARG(uap, sb), sizeof(ub));
    562 
    563 	return (error);
    564 }
    565 
    566 /*
    567  * Return pathconf information about a file descriptor.
    568  */
    569 /* ARGSUSED */
    570 int
    571 sys_fpathconf(struct lwp *l, void *v, register_t *retval)
    572 {
    573 	struct sys_fpathconf_args /* {
    574 		syscallarg(int)	fd;
    575 		syscallarg(int)	name;
    576 	} */ *uap = v;
    577 	int		fd;
    578 	struct filedesc	*fdp;
    579 	struct file	*fp;
    580 	struct proc 	*p;
    581 	struct vnode	*vp;
    582 	int		error;
    583 
    584 	p = l->l_proc;
    585 	fd = SCARG(uap, fd);
    586 	fdp = p->p_fd;
    587 	error = 0;
    588 
    589 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    590 		return (EBADF);
    591 
    592 	FILE_USE(fp);
    593 
    594 	switch (fp->f_type) {
    595 
    596 	case DTYPE_SOCKET:
    597 	case DTYPE_PIPE:
    598 		if (SCARG(uap, name) != _PC_PIPE_BUF)
    599 			error = EINVAL;
    600 		else
    601 			*retval = PIPE_BUF;
    602 		break;
    603 
    604 	case DTYPE_VNODE:
    605 		vp = (struct vnode *)fp->f_data;
    606 		error = VOP_PATHCONF(vp, SCARG(uap, name), retval);
    607 		break;
    608 
    609 	default:
    610 		error = EOPNOTSUPP;
    611 		break;
    612 	}
    613 
    614 	FILE_UNUSE(fp, p);
    615 	return (error);
    616 }
    617 
    618 /*
    619  * Allocate a file descriptor for the process.
    620  */
    621 int	fdexpanded;		/* XXX: what else uses this? */
    622 
    623 int
    624 fdalloc(struct proc *p, int want, int *result)
    625 {
    626 	struct filedesc	*fdp;
    627 	int i, lim, last;
    628 
    629 	fdp = p->p_fd;
    630 
    631 	/*
    632 	 * Search for a free descriptor starting at the higher
    633 	 * of want or fd_freefile.  If that fails, consider
    634 	 * expanding the ofile array.
    635 	 */
    636 	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
    637 	last = min(fdp->fd_nfiles, lim);
    638 	if ((i = want) < fdp->fd_freefile)
    639 		i = fdp->fd_freefile;
    640 	for (; i < last; i++) {
    641 		if (fdp->fd_ofiles[i] == NULL) {
    642 			fd_used(fdp, i);
    643 			if (want <= fdp->fd_freefile)
    644 				fdp->fd_freefile = i;
    645 			*result = i;
    646 			return (0);
    647 		}
    648 	}
    649 
    650 	/* No space in current array.  Expand? */
    651 	if (fdp->fd_nfiles >= lim)
    652 		return (EMFILE);
    653 
    654 	/* Let the caller do it. */
    655 	return (ENOSPC);
    656 }
    657 
    658 void
    659 fdexpand(struct proc *p)
    660 {
    661 	struct filedesc	*fdp;
    662 	int		i, nfiles;
    663 	struct file	**newofile;
    664 	char		*newofileflags;
    665 
    666 	fdp = p->p_fd;
    667 
    668 	if (fdp->fd_nfiles < NDEXTENT)
    669 		nfiles = NDEXTENT;
    670 	else
    671 		nfiles = 2 * fdp->fd_nfiles;
    672 	newofile = malloc(nfiles * OFILESIZE, M_FILEDESC, M_WAITOK);
    673 	newofileflags = (char *) &newofile[nfiles];
    674 	/*
    675 	 * Copy the existing ofile and ofileflags arrays
    676 	 * and zero the new portion of each array.
    677 	 */
    678 	memcpy(newofile, fdp->fd_ofiles,
    679 	    (i = sizeof(struct file *) * fdp->fd_nfiles));
    680 	memset((char *)newofile + i, 0,
    681 	    nfiles * sizeof(struct file *) - i);
    682 	memcpy(newofileflags, fdp->fd_ofileflags,
    683 	    (i = sizeof(char) * fdp->fd_nfiles));
    684 	memset(newofileflags + i, 0, nfiles * sizeof(char) - i);
    685 	if (fdp->fd_nfiles > NDFILE)
    686 		free(fdp->fd_ofiles, M_FILEDESC);
    687 	fdp->fd_ofiles = newofile;
    688 	fdp->fd_ofileflags = newofileflags;
    689 	fdp->fd_nfiles = nfiles;
    690 	fdexpanded++;
    691 }
    692 
    693 /*
    694  * Check to see whether n user file descriptors
    695  * are available to the process p.
    696  */
    697 int
    698 fdavail(struct proc *p, int n)
    699 {
    700 	struct filedesc	*fdp;
    701 	struct file	**fpp;
    702 	int		i, lim;
    703 
    704 	fdp = p->p_fd;
    705 	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
    706 	if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
    707 		return (1);
    708 	fpp = &fdp->fd_ofiles[fdp->fd_freefile];
    709 	for (i = min(lim,fdp->fd_nfiles) - fdp->fd_freefile; --i >= 0; fpp++)
    710 		if (*fpp == NULL && --n <= 0)
    711 			return (1);
    712 	return (0);
    713 }
    714 
    715 /*
    716  * Initialize the data structures necessary for managing files.
    717  */
    718 void
    719 finit(void)
    720 {
    721 
    722 	pool_init(&file_pool, sizeof(struct file), 0, 0, 0, "filepl",
    723 	    &pool_allocator_nointr);
    724 	pool_init(&cwdi_pool, sizeof(struct cwdinfo), 0, 0, 0, "cwdipl",
    725 	    &pool_allocator_nointr);
    726 	pool_init(&filedesc0_pool, sizeof(struct filedesc0), 0, 0, 0, "fdescpl",
    727 	    &pool_allocator_nointr);
    728 }
    729 
    730 /*
    731  * Create a new open file structure and allocate
    732  * a file decriptor for the process that refers to it.
    733  */
    734 int
    735 falloc(struct proc *p, struct file **resultfp, int *resultfd)
    736 {
    737 	struct file	*fp, *fq;
    738 	int		error, i;
    739 
    740  restart:
    741 	if ((error = fdalloc(p, 0, &i)) != 0) {
    742 		if (error == ENOSPC) {
    743 			fdexpand(p);
    744 			goto restart;
    745 		}
    746 		return (error);
    747 	}
    748 	if (nfiles >= maxfiles) {
    749 		tablefull("file", "increase kern.maxfiles or MAXFILES");
    750 		return (ENFILE);
    751 	}
    752 	/*
    753 	 * Allocate a new file descriptor.
    754 	 * If the process has file descriptor zero open, add to the list
    755 	 * of open files at that point, otherwise put it at the front of
    756 	 * the list of open files.
    757 	 */
    758 	nfiles++;
    759 	fp = pool_get(&file_pool, PR_WAITOK);
    760 	memset(fp, 0, sizeof(struct file));
    761 	fp->f_iflags = FIF_LARVAL;
    762 	if ((fq = p->p_fd->fd_ofiles[0]) != NULL) {
    763 		LIST_INSERT_AFTER(fq, fp, f_list);
    764 	} else {
    765 		LIST_INSERT_HEAD(&filehead, fp, f_list);
    766 	}
    767 	p->p_fd->fd_ofiles[i] = fp;
    768 	fp->f_count = 1;
    769 	fp->f_cred = p->p_ucred;
    770 	crhold(fp->f_cred);
    771 	if (resultfp) {
    772 		FILE_USE(fp);
    773 		*resultfp = fp;
    774 	}
    775 	if (resultfd)
    776 		*resultfd = i;
    777 	return (0);
    778 }
    779 
    780 /*
    781  * Free a file descriptor.
    782  */
    783 void
    784 ffree(struct file *fp)
    785 {
    786 
    787 #ifdef DIAGNOSTIC
    788 	if (fp->f_usecount)
    789 		panic("ffree");
    790 #endif
    791 
    792 	LIST_REMOVE(fp, f_list);
    793 	crfree(fp->f_cred);
    794 #ifdef DIAGNOSTIC
    795 	fp->f_count = 0;
    796 #endif
    797 	nfiles--;
    798 	pool_put(&file_pool, fp);
    799 }
    800 
    801 /*
    802  * Create an initial cwdinfo structure, using the same current and root
    803  * directories as p.
    804  */
    805 struct cwdinfo *
    806 cwdinit(struct proc *p)
    807 {
    808 	struct cwdinfo *cwdi;
    809 
    810 	cwdi = pool_get(&cwdi_pool, PR_WAITOK);
    811 
    812 	cwdi->cwdi_cdir = p->p_cwdi->cwdi_cdir;
    813 	if (cwdi->cwdi_cdir)
    814 		VREF(cwdi->cwdi_cdir);
    815 	cwdi->cwdi_rdir = p->p_cwdi->cwdi_rdir;
    816 	if (cwdi->cwdi_rdir)
    817 		VREF(cwdi->cwdi_rdir);
    818 	cwdi->cwdi_cmask =  p->p_cwdi->cwdi_cmask;
    819 	cwdi->cwdi_refcnt = 1;
    820 
    821 	return (cwdi);
    822 }
    823 
    824 /*
    825  * Make p2 share p1's cwdinfo.
    826  */
    827 void
    828 cwdshare(struct proc *p1, struct proc *p2)
    829 {
    830 
    831 	p2->p_cwdi = p1->p_cwdi;
    832 	p1->p_cwdi->cwdi_refcnt++;
    833 }
    834 
    835 /*
    836  * Make this process not share its cwdinfo structure, maintaining
    837  * all cwdinfo state.
    838  */
    839 void
    840 cwdunshare(struct proc *p)
    841 {
    842 	struct cwdinfo *newcwdi;
    843 
    844 	if (p->p_cwdi->cwdi_refcnt == 1)
    845 		return;
    846 
    847 	newcwdi = cwdinit(p);
    848 	cwdfree(p);
    849 	p->p_cwdi = newcwdi;
    850 }
    851 
    852 /*
    853  * Release a cwdinfo structure.
    854  */
    855 void
    856 cwdfree(struct proc *p)
    857 {
    858 	struct cwdinfo *cwdi;
    859 
    860 	cwdi = p->p_cwdi;
    861 	if (--cwdi->cwdi_refcnt > 0)
    862 		return;
    863 
    864 	p->p_cwdi = NULL;
    865 
    866 	vrele(cwdi->cwdi_cdir);
    867 	if (cwdi->cwdi_rdir)
    868 		vrele(cwdi->cwdi_rdir);
    869 	pool_put(&cwdi_pool, cwdi);
    870 }
    871 
    872 /*
    873  * Create an initial filedesc structure, using the same current and root
    874  * directories as p.
    875  */
    876 struct filedesc *
    877 fdinit(struct proc *p)
    878 {
    879 	struct filedesc0 *newfdp;
    880 
    881 	newfdp = pool_get(&filedesc0_pool, PR_WAITOK);
    882 	memset(newfdp, 0, sizeof(struct filedesc0));
    883 
    884 	fdinit1(newfdp);
    885 
    886 	return (&newfdp->fd_fd);
    887 }
    888 
    889 /*
    890  * Initialize a file descriptor table.
    891  */
    892 void
    893 fdinit1(struct filedesc0 *newfdp)
    894 {
    895 
    896 	newfdp->fd_fd.fd_refcnt = 1;
    897 	newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
    898 	newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
    899 	newfdp->fd_fd.fd_nfiles = NDFILE;
    900 }
    901 
    902 /*
    903  * Make p2 share p1's filedesc structure.
    904  */
    905 void
    906 fdshare(struct proc *p1, struct proc *p2)
    907 {
    908 
    909 	p2->p_fd = p1->p_fd;
    910 	p1->p_fd->fd_refcnt++;
    911 }
    912 
    913 /*
    914  * Make this process not share its filedesc structure, maintaining
    915  * all file descriptor state.
    916  */
    917 void
    918 fdunshare(struct proc *p)
    919 {
    920 	struct filedesc *newfd;
    921 
    922 	if (p->p_fd->fd_refcnt == 1)
    923 		return;
    924 
    925 	newfd = fdcopy(p);
    926 	fdfree(p);
    927 	p->p_fd = newfd;
    928 }
    929 
    930 /*
    931  * Clear a process's fd table.
    932  */
    933 void
    934 fdclear(struct proc *p)
    935 {
    936 	struct filedesc *newfd;
    937 
    938 	newfd = fdinit(p);
    939 	fdfree(p);
    940 	p->p_fd = newfd;
    941 }
    942 
    943 /*
    944  * Copy a filedesc structure.
    945  */
    946 struct filedesc *
    947 fdcopy(struct proc *p)
    948 {
    949 	struct filedesc	*newfdp, *fdp;
    950 	struct file	**fpp;
    951 	int		i;
    952 
    953 	fdp = p->p_fd;
    954 	newfdp = pool_get(&filedesc0_pool, PR_WAITOK);
    955 	memcpy(newfdp, fdp, sizeof(struct filedesc));
    956 	newfdp->fd_refcnt = 1;
    957 
    958 	/*
    959 	 * If the number of open files fits in the internal arrays
    960 	 * of the open file structure, use them, otherwise allocate
    961 	 * additional memory for the number of descriptors currently
    962 	 * in use.
    963 	 */
    964 	if (newfdp->fd_lastfile < NDFILE) {
    965 		newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
    966 		newfdp->fd_ofileflags =
    967 		    ((struct filedesc0 *) newfdp)->fd_dfileflags;
    968 		i = NDFILE;
    969 	} else {
    970 		/*
    971 		 * Compute the smallest multiple of NDEXTENT needed
    972 		 * for the file descriptors currently in use,
    973 		 * allowing the table to shrink.
    974 		 */
    975 		i = newfdp->fd_nfiles;
    976 		while (i >= 2 * NDEXTENT && i > newfdp->fd_lastfile * 2)
    977 			i /= 2;
    978 		newfdp->fd_ofiles = malloc(i * OFILESIZE, M_FILEDESC, M_WAITOK);
    979 		newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
    980 	}
    981 	newfdp->fd_nfiles = i;
    982 	memcpy(newfdp->fd_ofiles, fdp->fd_ofiles, i * sizeof(struct file **));
    983 	memcpy(newfdp->fd_ofileflags, fdp->fd_ofileflags, i * sizeof(char));
    984 	fpp = newfdp->fd_ofiles;
    985 	for (i = newfdp->fd_lastfile; i >= 0; i--, fpp++)
    986 		if (*fpp != NULL)
    987 			(*fpp)->f_count++;
    988 	return (newfdp);
    989 }
    990 
    991 /*
    992  * Release a filedesc structure.
    993  */
    994 void
    995 fdfree(struct proc *p)
    996 {
    997 	struct filedesc	*fdp;
    998 	struct file	**fpp, *fp;
    999 	int		i;
   1000 
   1001 	fdp = p->p_fd;
   1002 	if (--fdp->fd_refcnt > 0)
   1003 		return;
   1004 	fpp = fdp->fd_ofiles;
   1005 	for (i = fdp->fd_lastfile; i >= 0; i--, fpp++) {
   1006 		fp = *fpp;
   1007 		if (fp != NULL) {
   1008 			*fpp = NULL;
   1009 			FILE_USE(fp);
   1010 			(void) closef(fp, p);
   1011 		}
   1012 	}
   1013 	p->p_fd = NULL;
   1014 	if (fdp->fd_nfiles > NDFILE)
   1015 		free(fdp->fd_ofiles, M_FILEDESC);
   1016 	pool_put(&filedesc0_pool, fdp);
   1017 }
   1018 
   1019 /*
   1020  * Internal form of close.
   1021  * Decrement reference count on file structure.
   1022  * Note: p may be NULL when closing a file
   1023  * that was being passed in a message.
   1024  *
   1025  * Note: we expect the caller is holding a usecount, and expects us
   1026  * to drop it (the caller thinks the file is going away forever).
   1027  */
   1028 int
   1029 closef(struct file *fp, struct proc *p)
   1030 {
   1031 	struct vnode	*vp;
   1032 	struct flock	lf;
   1033 	int		error;
   1034 
   1035 	if (fp == NULL)
   1036 		return (0);
   1037 
   1038 	/*
   1039 	 * POSIX record locking dictates that any close releases ALL
   1040 	 * locks owned by this process.  This is handled by setting
   1041 	 * a flag in the unlock to free ONLY locks obeying POSIX
   1042 	 * semantics, and not to free BSD-style file locks.
   1043 	 * If the descriptor was in a message, POSIX-style locks
   1044 	 * aren't passed with the descriptor.
   1045 	 */
   1046 	if (p && (p->p_flag & P_ADVLOCK) && fp->f_type == DTYPE_VNODE) {
   1047 		lf.l_whence = SEEK_SET;
   1048 		lf.l_start = 0;
   1049 		lf.l_len = 0;
   1050 		lf.l_type = F_UNLCK;
   1051 		vp = (struct vnode *)fp->f_data;
   1052 		(void) VOP_ADVLOCK(vp, (caddr_t)p, F_UNLCK, &lf, F_POSIX);
   1053 	}
   1054 
   1055 	/*
   1056 	 * If WANTCLOSE is set, then the reference count on the file
   1057 	 * is 0, but there were multiple users of the file.  This can
   1058 	 * happen if a filedesc structure is shared by multiple
   1059 	 * processes.
   1060 	 */
   1061 	if (fp->f_iflags & FIF_WANTCLOSE) {
   1062 		/*
   1063 		 * Another user of the file is already closing, and is
   1064 		 * simply waiting for other users of the file to drain.
   1065 		 * Release our usecount, and wake up the closer if it
   1066 		 * is the only remaining use.
   1067 		 */
   1068 #ifdef DIAGNOSTIC
   1069 		if (fp->f_count != 0)
   1070 			panic("closef: wantclose and count != 0");
   1071 		if (fp->f_usecount < 2)
   1072 			panic("closef: wantclose and usecount < 2");
   1073 #endif
   1074 		if (--fp->f_usecount == 1)
   1075 			wakeup(&fp->f_usecount);
   1076 		return (0);
   1077 	} else {
   1078 		/*
   1079 		 * Decrement the reference count.  If we were not the
   1080 		 * last reference, then release our use and just
   1081 		 * return.
   1082 		 */
   1083 		if (--fp->f_count > 0) {
   1084 #ifdef DIAGNOSTIC
   1085 			if (fp->f_usecount < 1)
   1086 				panic("closef: no wantclose and usecount < 1");
   1087 #endif
   1088 			fp->f_usecount--;
   1089 			return (0);
   1090 		}
   1091 		if (fp->f_count < 0)
   1092 			panic("closef: count < 0");
   1093 	}
   1094 
   1095 	/*
   1096 	 * The reference count is now 0.  However, there may be
   1097 	 * multiple potential users of this file.  This can happen
   1098 	 * if multiple processes shared a single filedesc structure.
   1099 	 *
   1100 	 * Notify these potential users that the file is closing.
   1101 	 * This will prevent them from adding additional uses to
   1102 	 * the file.
   1103 	 */
   1104 	fp->f_iflags |= FIF_WANTCLOSE;
   1105 
   1106 	/*
   1107 	 * We expect the caller to add a use to the file.  So, if we
   1108 	 * are the last user, usecount will be 1.  If it is not, we
   1109 	 * must wait for the usecount to drain.  When it drains back
   1110 	 * to 1, we will be awakened so that we may proceed with the
   1111 	 * close.
   1112 	 */
   1113 #ifdef DIAGNOSTIC
   1114 	if (fp->f_usecount < 1)
   1115 		panic("closef: usecount < 1");
   1116 #endif
   1117 	while (fp->f_usecount > 1)
   1118 		(void) tsleep(&fp->f_usecount, PRIBIO, "closef", 0);
   1119 #ifdef DIAGNOSTIC
   1120 	if (fp->f_usecount != 1)
   1121 		panic("closef: usecount != 1");
   1122 #endif
   1123 
   1124 	if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
   1125 		lf.l_whence = SEEK_SET;
   1126 		lf.l_start = 0;
   1127 		lf.l_len = 0;
   1128 		lf.l_type = F_UNLCK;
   1129 		vp = (struct vnode *)fp->f_data;
   1130 		(void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
   1131 	}
   1132 	if (fp->f_ops)
   1133 		error = (*fp->f_ops->fo_close)(fp, p);
   1134 	else
   1135 		error = 0;
   1136 
   1137 	/* Nothing references the file now, drop the final use (us). */
   1138 	fp->f_usecount--;
   1139 
   1140 	ffree(fp);
   1141 	return (error);
   1142 }
   1143 
   1144 /*
   1145  * Apply an advisory lock on a file descriptor.
   1146  *
   1147  * Just attempt to get a record lock of the requested type on
   1148  * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
   1149  */
   1150 /* ARGSUSED */
   1151 int
   1152 sys_flock(struct lwp *l, void *v, register_t *retval)
   1153 {
   1154 	struct sys_flock_args /* {
   1155 		syscallarg(int)	fd;
   1156 		syscallarg(int)	how;
   1157 	} */ *uap = v;
   1158 	int		fd, how, error;
   1159 	struct proc	*p;
   1160 	struct filedesc	*fdp;
   1161 	struct file	*fp;
   1162 	struct vnode	*vp;
   1163 	struct flock	lf;
   1164 
   1165 	p = l->l_proc;
   1166 	fd = SCARG(uap, fd);
   1167 	how = SCARG(uap, how);
   1168 	fdp = p->p_fd;
   1169 	error = 0;
   1170 
   1171 	if ((fp = fd_getfile(fdp, fd)) == NULL)
   1172 		return (EBADF);
   1173 
   1174 	FILE_USE(fp);
   1175 
   1176 	if (fp->f_type != DTYPE_VNODE) {
   1177 		error = EOPNOTSUPP;
   1178 		goto out;
   1179 	}
   1180 
   1181 	vp = (struct vnode *)fp->f_data;
   1182 	lf.l_whence = SEEK_SET;
   1183 	lf.l_start = 0;
   1184 	lf.l_len = 0;
   1185 	if (how & LOCK_UN) {
   1186 		lf.l_type = F_UNLCK;
   1187 		fp->f_flag &= ~FHASLOCK;
   1188 		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
   1189 		goto out;
   1190 	}
   1191 	if (how & LOCK_EX)
   1192 		lf.l_type = F_WRLCK;
   1193 	else if (how & LOCK_SH)
   1194 		lf.l_type = F_RDLCK;
   1195 	else {
   1196 		error = EINVAL;
   1197 		goto out;
   1198 	}
   1199 	fp->f_flag |= FHASLOCK;
   1200 	if (how & LOCK_NB)
   1201 		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK);
   1202 	else
   1203 		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
   1204 		    F_FLOCK|F_WAIT);
   1205  out:
   1206 	FILE_UNUSE(fp, p);
   1207 	return (error);
   1208 }
   1209 
   1210 /*
   1211  * File Descriptor pseudo-device driver (/dev/fd/).
   1212  *
   1213  * Opening minor device N dup()s the file (if any) connected to file
   1214  * descriptor N belonging to the calling process.  Note that this driver
   1215  * consists of only the ``open()'' routine, because all subsequent
   1216  * references to this file will be direct to the other driver.
   1217  */
   1218 /* ARGSUSED */
   1219 int
   1220 filedescopen(dev_t dev, int mode, int type, struct proc *p)
   1221 {
   1222 
   1223 	/*
   1224 	 * XXX Kludge: set p->p_dupfd to contain the value of the
   1225 	 * the file descriptor being sought for duplication. The error
   1226 	 * return ensures that the vnode for this device will be released
   1227 	 * by vn_open. Open will detect this special error and take the
   1228 	 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
   1229 	 * will simply report the error.
   1230 	 */
   1231 	p->p_dupfd = minor(dev);
   1232 	return (ENODEV);
   1233 }
   1234 
   1235 /*
   1236  * Duplicate the specified descriptor to a free descriptor.
   1237  */
   1238 int
   1239 dupfdopen(struct proc *p, int indx, int dfd, int mode, int error)
   1240 {
   1241 	struct filedesc	*fdp;
   1242 	struct file	*wfp, *fp;
   1243 
   1244 	fdp = p->p_fd;
   1245 	/*
   1246 	 * If the to-be-dup'd fd number is greater than the allowed number
   1247 	 * of file descriptors, or the fd to be dup'd has already been
   1248 	 * closed, reject.  Note, check for new == old is necessary as
   1249 	 * falloc could allocate an already closed to-be-dup'd descriptor
   1250 	 * as the new descriptor.
   1251 	 */
   1252 	fp = fdp->fd_ofiles[indx];
   1253 
   1254 	if ((wfp = fd_getfile(fdp, dfd)) == NULL)
   1255 		return (EBADF);
   1256 
   1257 	if (fp == wfp)
   1258 		return (EBADF);
   1259 
   1260 	FILE_USE(wfp);
   1261 
   1262 	/*
   1263 	 * There are two cases of interest here.
   1264 	 *
   1265 	 * For ENODEV simply dup (dfd) to file descriptor
   1266 	 * (indx) and return.
   1267 	 *
   1268 	 * For ENXIO steal away the file structure from (dfd) and
   1269 	 * store it in (indx).  (dfd) is effectively closed by
   1270 	 * this operation.
   1271 	 *
   1272 	 * Any other error code is just returned.
   1273 	 */
   1274 	switch (error) {
   1275 	case ENODEV:
   1276 		/*
   1277 		 * Check that the mode the file is being opened for is a
   1278 		 * subset of the mode of the existing descriptor.
   1279 		 */
   1280 		if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
   1281 			FILE_UNUSE(wfp, p);
   1282 			return (EACCES);
   1283 		}
   1284 		fdp->fd_ofiles[indx] = wfp;
   1285 		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
   1286 		wfp->f_count++;
   1287 		fd_used(fdp, indx);
   1288 		FILE_UNUSE(wfp, p);
   1289 		return (0);
   1290 
   1291 	case ENXIO:
   1292 		/*
   1293 		 * Steal away the file pointer from dfd, and stuff it into indx.
   1294 		 */
   1295 		fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
   1296 		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
   1297 		fdp->fd_ofiles[dfd] = NULL;
   1298 		fdp->fd_ofileflags[dfd] = 0;
   1299 		/*
   1300 		 * Complete the clean up of the filedesc structure by
   1301 		 * recomputing the various hints.
   1302 		 */
   1303 		fd_used(fdp, indx);
   1304 		fd_unused(fdp, dfd);
   1305 		FILE_UNUSE(wfp, p);
   1306 		return (0);
   1307 
   1308 	default:
   1309 		FILE_UNUSE(wfp, p);
   1310 		return (error);
   1311 	}
   1312 	/* NOTREACHED */
   1313 }
   1314 
   1315 /*
   1316  * fcntl call which is being passed to the file's fs.
   1317  */
   1318 int
   1319 fcntl_forfs(int fd, struct proc *p, int cmd, void *arg)
   1320 {
   1321 	struct file	*fp;
   1322 	struct filedesc	*fdp;
   1323 	int		error;
   1324 	u_int		size;
   1325 	caddr_t		data, memp;
   1326 #define STK_PARAMS	128
   1327 	char		stkbuf[STK_PARAMS];
   1328 
   1329 	/* fd's value was validated in sys_fcntl before calling this routine */
   1330 	fdp = p->p_fd;
   1331 	fp = fdp->fd_ofiles[fd];
   1332 
   1333 	if ((fp->f_flag & (FREAD | FWRITE)) == 0)
   1334 		return (EBADF);
   1335 
   1336 	/*
   1337 	 * Interpret high order word to find amount of data to be
   1338 	 * copied to/from the user's address space.
   1339 	 */
   1340 	size = (size_t)F_PARAM_LEN(cmd);
   1341 	if (size > F_PARAM_MAX)
   1342 		return (EINVAL);
   1343 	memp = NULL;
   1344 	if (size > sizeof(stkbuf)) {
   1345 		memp = (caddr_t)malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
   1346 		data = memp;
   1347 	} else
   1348 		data = stkbuf;
   1349 	if (cmd & F_FSIN) {
   1350 		if (size) {
   1351 			error = copyin(arg, data, size);
   1352 			if (error) {
   1353 				if (memp)
   1354 					free(memp, M_IOCTLOPS);
   1355 				return (error);
   1356 			}
   1357 		} else
   1358 			*(caddr_t *)data = arg;
   1359 	} else if ((cmd & F_FSOUT) && size)
   1360 		/*
   1361 		 * Zero the buffer so the user always
   1362 		 * gets back something deterministic.
   1363 		 */
   1364 		memset(data, 0, size);
   1365 	else if (cmd & F_FSVOID)
   1366 		*(caddr_t *)data = arg;
   1367 
   1368 
   1369 	error = (*fp->f_ops->fo_fcntl)(fp, cmd, data, p);
   1370 
   1371 	/*
   1372 	 * Copy any data to user, size was
   1373 	 * already set and checked above.
   1374 	 */
   1375 	if (error == 0 && (cmd & F_FSOUT) && size)
   1376 		error = copyout(data, arg, size);
   1377 	if (memp)
   1378 		free(memp, M_IOCTLOPS);
   1379 	return (error);
   1380 }
   1381 
   1382 /*
   1383  * Close any files on exec?
   1384  */
   1385 void
   1386 fdcloseexec(struct proc *p)
   1387 {
   1388 	struct filedesc	*fdp;
   1389 	int		fd;
   1390 
   1391 	fdunshare(p);
   1392 	cwdunshare(p);
   1393 
   1394 	fdp = p->p_fd;
   1395 	for (fd = 0; fd <= fdp->fd_lastfile; fd++)
   1396 		if (fdp->fd_ofileflags[fd] & UF_EXCLOSE)
   1397 			(void) fdrelease(p, fd);
   1398 }
   1399 
   1400 /*
   1401  * It is unsafe for set[ug]id processes to be started with file
   1402  * descriptors 0..2 closed, as these descriptors are given implicit
   1403  * significance in the Standard C library.  fdcheckstd() will create a
   1404  * descriptor referencing /dev/null for each of stdin, stdout, and
   1405  * stderr that is not already open.
   1406  */
   1407 #define CHECK_UPTO 3
   1408 int
   1409 fdcheckstd(p)
   1410 	struct proc *p;
   1411 {
   1412 	struct nameidata nd;
   1413 	struct filedesc *fdp;
   1414 	struct file *fp;
   1415 	struct file *devnullfp;
   1416 	struct proc *pp;
   1417 	register_t retval;
   1418 	int fd, i, error, flags = FREAD|FWRITE, devnull = -1;
   1419 	char closed[CHECK_UPTO * 3 + 1], which[3 + 1];
   1420 
   1421 	closed[0] = '\0';
   1422 	if ((fdp = p->p_fd) == NULL)
   1423 		return (0);
   1424 	for (i = 0; i < CHECK_UPTO; i++) {
   1425 		if (fdp->fd_ofiles[i] != NULL)
   1426 			continue;
   1427 		snprintf(which, sizeof(which), ",%d", i);
   1428 		strcat(closed, which);
   1429 		if (devnull < 0) {
   1430 			if ((error = falloc(p, &fp, &fd)) != 0)
   1431 				return (error);
   1432 			NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/null",
   1433 			    p);
   1434 			if ((error = vn_open(&nd, flags, 0)) != 0) {
   1435 				FILE_UNUSE(fp, p);
   1436 				ffree(fp);
   1437 				fdremove(p->p_fd, fd);
   1438 				return (error);
   1439 			}
   1440 			fp->f_data = (caddr_t)nd.ni_vp;
   1441 			fp->f_flag = flags;
   1442 			fp->f_ops = &vnops;
   1443 			fp->f_type = DTYPE_VNODE;
   1444 			VOP_UNLOCK(nd.ni_vp, 0);
   1445 			devnull = fd;
   1446 			devnullfp = fp;
   1447 			FILE_SET_MATURE(fp);
   1448 			FILE_UNUSE(fp, p);
   1449 		} else {
   1450 restart:
   1451 			if ((error = fdalloc(p, 0, &fd)) != 0) {
   1452 				if (error == ENOSPC) {
   1453 					fdexpand(p);
   1454 					goto restart;
   1455 				}
   1456 				return (error);
   1457 			}
   1458 
   1459 			FILE_USE(devnullfp);
   1460 			/* finishdup() will unuse the descriptors for us */
   1461 			if ((error = finishdup(p, devnull, fd, &retval)) != 0)
   1462 				return (error);
   1463 		}
   1464 	}
   1465 	if (closed[0] != '\0') {
   1466 		pp = p->p_pptr;
   1467 		log(LOG_WARNING, "set{u,g}id pid %d (%s) "
   1468 		    "was invoked by uid %d ppid %d (%s) "
   1469 		    "with fd %s closed\n",
   1470 		    p->p_pid, p->p_comm, pp->p_ucred->cr_uid,
   1471 		    pp->p_pid, pp->p_comm, &closed[1]);
   1472 	}
   1473 	return (0);
   1474 }
   1475 #undef CHECK_UPTO
   1476