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