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