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