Home | History | Annotate | Line # | Download | only in kern
kern_descrip.c revision 1.153.2.6
      1 /*	$NetBSD: kern_descrip.c,v 1.153.2.6 2007/07/09 11:27:14 ad 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. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)kern_descrip.c	8.8 (Berkeley) 2/14/95
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: kern_descrip.c,v 1.153.2.6 2007/07/09 11:27:14 ad Exp $");
     41 
     42 #include <sys/param.h>
     43 #include <sys/systm.h>
     44 #include <sys/filedesc.h>
     45 #include <sys/kernel.h>
     46 #include <sys/vnode.h>
     47 #include <sys/proc.h>
     48 #include <sys/file.h>
     49 #include <sys/namei.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 #include <sys/event.h>
     62 #include <sys/kauth.h>
     63 
     64 #include <sys/mount.h>
     65 #include <sys/syscallargs.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 POOL_INIT(file_pool, sizeof(struct file), 0, 0, 0, "filepl",
     73     &pool_allocator_nointr, IPL_NONE);
     74 POOL_INIT(cwdi_pool, sizeof(struct cwdinfo), 0, 0, 0, "cwdipl",
     75     &pool_allocator_nointr, IPL_NONE);
     76 POOL_INIT(filedesc0_pool, sizeof(struct filedesc0), 0, 0, 0, "fdescpl",
     77     &pool_allocator_nointr, IPL_NONE);
     78 
     79 /* Global file list lock */
     80 kmutex_t filelist_lock;
     81 
     82 MALLOC_DEFINE(M_FILE, "file", "Open file structure");
     83 MALLOC_DEFINE(M_FILEDESC, "file desc", "Open file descriptor table");
     84 MALLOC_DEFINE(M_IOCTLOPS, "ioctlops", "ioctl data buffer");
     85 
     86 static inline int
     87 find_next_zero(uint32_t *bitmap, int want, u_int bits)
     88 {
     89 	int i, off, maxoff;
     90 	uint32_t sub;
     91 
     92 	if (want > bits)
     93 		return -1;
     94 
     95 	off = want >> NDENTRYSHIFT;
     96 	i = want & NDENTRYMASK;
     97 	if (i) {
     98 		sub = bitmap[off] | ((u_int)~0 >> (NDENTRIES - i));
     99 		if (sub != ~0)
    100 			goto found;
    101 		off++;
    102 	}
    103 
    104 	maxoff = NDLOSLOTS(bits);
    105 	while (off < maxoff) {
    106 		if ((sub = bitmap[off]) != ~0)
    107 			goto found;
    108 		off++;
    109 	}
    110 
    111 	return (-1);
    112 
    113  found:
    114 	return (off << NDENTRYSHIFT) + ffs(~sub) - 1;
    115 }
    116 
    117 static int
    118 find_last_set(struct filedesc *fd, int last)
    119 {
    120 	int off, i;
    121 	struct file **ofiles = fd->fd_ofiles;
    122 	uint32_t *bitmap = fd->fd_lomap;
    123 
    124 	off = (last - 1) >> NDENTRYSHIFT;
    125 
    126 	while (off >= 0 && !bitmap[off])
    127 		off--;
    128 
    129 	if (off < 0)
    130 		return (-1);
    131 
    132 	i = ((off + 1) << NDENTRYSHIFT) - 1;
    133 	if (i >= last)
    134 		i = last - 1;
    135 
    136 	while (i > 0 && ofiles[i] == NULL)
    137 		i--;
    138 
    139 	return (i);
    140 }
    141 
    142 static inline void
    143 fd_used(struct filedesc *fdp, int fd)
    144 {
    145 	u_int off = fd >> NDENTRYSHIFT;
    146 
    147 	KASSERT(rw_write_held(&fdp->fd_lock));
    148 	KDASSERT((fdp->fd_lomap[off] & (1 << (fd & NDENTRYMASK))) == 0);
    149 
    150 	fdp->fd_lomap[off] |= 1 << (fd & NDENTRYMASK);
    151 	if (fdp->fd_lomap[off] == ~0) {
    152 		KDASSERT((fdp->fd_himap[off >> NDENTRYSHIFT] &
    153 		    (1 << (off & NDENTRYMASK))) == 0);
    154 		fdp->fd_himap[off >> NDENTRYSHIFT] |= 1 << (off & NDENTRYMASK);
    155 	}
    156 
    157 	if (fd > fdp->fd_lastfile)
    158 		fdp->fd_lastfile = fd;
    159 }
    160 
    161 static inline void
    162 fd_unused(struct filedesc *fdp, int fd)
    163 {
    164 	u_int off = fd >> NDENTRYSHIFT;
    165 
    166 	KASSERT(rw_write_held(&fdp->fd_lock));
    167 	if (fd < fdp->fd_freefile)
    168 		fdp->fd_freefile = fd;
    169 
    170 	if (fdp->fd_lomap[off] == ~0) {
    171 		KDASSERT((fdp->fd_himap[off >> NDENTRYSHIFT] &
    172 		    (1 << (off & NDENTRYMASK))) != 0);
    173 		fdp->fd_himap[off >> NDENTRYSHIFT] &=
    174 		    ~(1 << (off & NDENTRYMASK));
    175 	}
    176 	KDASSERT((fdp->fd_lomap[off] & (1 << (fd & NDENTRYMASK))) != 0);
    177 	fdp->fd_lomap[off] &= ~(1 << (fd & NDENTRYMASK));
    178 
    179 #ifdef DIAGNOSTIC
    180 	if (fd > fdp->fd_lastfile)
    181 		panic("fd_unused: fd_lastfile inconsistent");
    182 #endif
    183 	if (fd == fdp->fd_lastfile)
    184 		fdp->fd_lastfile = find_last_set(fdp, fd);
    185 }
    186 
    187 /*
    188  * Lookup the file structure corresponding to a file descriptor
    189  * and return it locked.
    190  * Note: typical usage is: `fp = fd_getfile(..); FILE_USE(fp);'
    191  * The locking strategy has been optimised for this case, i.e.
    192  * fd_getfile() returns the file locked while FILE_USE() will increment
    193  * the file's use count and unlock.
    194  */
    195 struct file *
    196 fd_getfile(struct filedesc *fdp, int fd)
    197 {
    198 	struct file *fp;
    199 
    200 	rw_enter(&fdp->fd_lock, RW_READER);
    201 	if ((u_int) fd >= fdp->fd_nfiles || (fp = fdp->fd_ofiles[fd]) == NULL) {
    202 		rw_exit(&fdp->fd_lock);
    203 		return (NULL);
    204 	}
    205 
    206 	mutex_enter(&fp->f_lock);
    207 	if (FILE_IS_USABLE(fp) == 0) {
    208 		mutex_exit(&fp->f_lock);
    209 		rw_exit(&fdp->fd_lock);
    210 		return (NULL);
    211 	}
    212 	rw_exit(&fdp->fd_lock);
    213 
    214 	return (fp);
    215 }
    216 
    217 /*
    218  * Common code for dup, dup2, and fcntl(F_DUPFD).
    219  */
    220 static int
    221 finishdup(struct lwp *l, int old, int new, register_t *retval)
    222 {
    223 	struct filedesc	*fdp;
    224 	struct file	*fp, *delfp;
    225 
    226 	fdp = l->l_proc->p_fd;
    227 
    228 	/*
    229 	 * If there is a file in the new slot, remember it so we
    230 	 * can close it after we've finished the dup.  We need
    231 	 * to do it after the dup is finished, since closing
    232 	 * the file may block.
    233 	 *
    234 	 * Note: `old' is already used for us.
    235 	 * Note: Caller already marked `new' slot "used".
    236 	 */
    237 	rw_enter(&fdp->fd_lock, RW_WRITER);
    238 	delfp = fdp->fd_ofiles[new];
    239 
    240 	fp = fdp->fd_ofiles[old];
    241 	KDASSERT(fp != NULL);
    242 	fdp->fd_ofiles[new] = fp;
    243 	fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
    244 	rw_exit(&fdp->fd_lock);
    245 
    246 	*retval = new;
    247 	mutex_enter(&fp->f_lock);
    248 	fp->f_count++;
    249 	FILE_UNUSE_HAVELOCK(fp, l);
    250 
    251 	if (delfp != NULL) {
    252 		mutex_enter(&delfp->f_lock);
    253 		FILE_USE(delfp);
    254 		if (new < fdp->fd_knlistsize)
    255 			knote_fdclose(l, new);
    256 		(void) closef(delfp, l);
    257 	}
    258 	return (0);
    259 }
    260 
    261 /*
    262  * Initialize the descriptor system.
    263  */
    264 void
    265 filedesc_init(void)
    266 {
    267 
    268 	mutex_init(&filelist_lock, MUTEX_DEFAULT, IPL_NONE);
    269 }
    270 
    271 /*
    272  * System calls on descriptors.
    273  */
    274 
    275 /*
    276  * Duplicate a file descriptor.
    277  */
    278 /* ARGSUSED */
    279 int
    280 sys_dup(struct lwp *l, void *v, register_t *retval)
    281 {
    282 	struct sys_dup_args /* {
    283 		syscallarg(int)	fd;
    284 	} */ *uap = v;
    285 	struct file	*fp;
    286 	struct filedesc	*fdp;
    287 	struct proc	*p;
    288 	int		old, new, error;
    289 
    290 	p = l->l_proc;
    291 	fdp = p->p_fd;
    292 	old = SCARG(uap, fd);
    293 
    294  restart:
    295 	if ((fp = fd_getfile(fdp, old)) == NULL)
    296 		return (EBADF);
    297 
    298 	FILE_USE(fp);
    299 
    300 	if ((error = fdalloc(p, 0, &new)) != 0) {
    301 		if (error == ENOSPC) {
    302 			fdexpand(p);
    303 			FILE_UNUSE(fp, l);
    304 			goto restart;
    305 		}
    306 		FILE_UNUSE(fp, l);
    307 		return (error);
    308 	}
    309 
    310 	/* finishdup() will unuse the descriptors for us */
    311 	return (finishdup(l, old, new, retval));
    312 }
    313 
    314 /*
    315  * Duplicate a file descriptor to a particular value.
    316  */
    317 /* ARGSUSED */
    318 int
    319 sys_dup2(struct lwp *l, void *v, register_t *retval)
    320 {
    321 	struct sys_dup2_args /* {
    322 		syscallarg(int)	from;
    323 		syscallarg(int)	to;
    324 	} */ *uap = v;
    325 	struct file	*fp;
    326 	struct filedesc	*fdp;
    327 	struct proc	*p;
    328 	int		old, new, i, error;
    329 
    330 	p = l->l_proc;
    331 	fdp = p->p_fd;
    332 	old = SCARG(uap, from);
    333 	new = SCARG(uap, to);
    334 
    335  restart:
    336 	if ((fp = fd_getfile(fdp, old)) == NULL)
    337 		return (EBADF);
    338 
    339 	if ((u_int)new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
    340 	    (u_int)new >= maxfiles) {
    341 		mutex_exit(&fp->f_lock);
    342 		return (EBADF);
    343 	}
    344 
    345 	if (old == new) {
    346 		mutex_exit(&fp->f_lock);
    347 		*retval = new;
    348 		return (0);
    349 	}
    350 
    351 	FILE_USE(fp);
    352 
    353 	if (new >= fdp->fd_nfiles) {
    354 		if ((error = fdalloc(p, new, &i)) != 0) {
    355 			if (error == ENOSPC) {
    356 				fdexpand(p);
    357 				FILE_UNUSE(fp, l);
    358 				goto restart;
    359 			}
    360 			FILE_UNUSE(fp, l);
    361 			return (error);
    362 		}
    363 		if (new != i)
    364 			panic("dup2: fdalloc");
    365 	} else {
    366 		rw_enter(&fdp->fd_lock, RW_WRITER);
    367 		/*
    368 		 * Mark `new' slot "used" only if it was empty.
    369 		 */
    370 		if (fdp->fd_ofiles[new] == NULL)
    371 			fd_used(fdp, new);
    372 		rw_exit(&fdp->fd_lock);
    373 	}
    374 
    375 	/*
    376 	 * finishdup() will close the file that's in the `new'
    377 	 * slot, if there's one there.
    378 	 */
    379 
    380 	/* finishdup() will unuse the descriptors for us */
    381 	return (finishdup(l, old, new, retval));
    382 }
    383 
    384 /*
    385  * fcntl call which is being passed to the file's fs.
    386  */
    387 static int
    388 fcntl_forfs(int fd, struct lwp *l, int cmd, void *arg)
    389 {
    390 	struct file	*fp;
    391 	struct filedesc	*fdp;
    392 	int		error;
    393 	u_int		size;
    394 	void		*data, *memp;
    395 #define STK_PARAMS	128
    396 	char		stkbuf[STK_PARAMS];
    397 
    398 	/* fd's value was validated in sys_fcntl before calling this routine */
    399 	fdp = l->l_proc->p_fd;
    400 	fp = fdp->fd_ofiles[fd];
    401 
    402 	if ((fp->f_flag & (FREAD | FWRITE)) == 0)
    403 		return (EBADF);
    404 
    405 	/*
    406 	 * Interpret high order word to find amount of data to be
    407 	 * copied to/from the user's address space.
    408 	 */
    409 	size = (size_t)F_PARAM_LEN(cmd);
    410 	if (size > F_PARAM_MAX)
    411 		return (EINVAL);
    412 	memp = NULL;
    413 	if (size > sizeof(stkbuf)) {
    414 		memp = malloc((u_long)size, M_IOCTLOPS, M_WAITOK);
    415 		data = memp;
    416 	} else
    417 		data = stkbuf;
    418 	if (cmd & F_FSIN) {
    419 		if (size) {
    420 			error = copyin(arg, data, size);
    421 			if (error) {
    422 				if (memp)
    423 					free(memp, M_IOCTLOPS);
    424 				return (error);
    425 			}
    426 		} else
    427 			*(void **)data = arg;
    428 	} else if ((cmd & F_FSOUT) && size)
    429 		/*
    430 		 * Zero the buffer so the user always
    431 		 * gets back something deterministic.
    432 		 */
    433 		memset(data, 0, size);
    434 	else if (cmd & F_FSVOID)
    435 		*(void **)data = arg;
    436 
    437 
    438 	error = (*fp->f_ops->fo_fcntl)(fp, cmd, data, l);
    439 
    440 	/*
    441 	 * Copy any data to user, size was
    442 	 * already set and checked above.
    443 	 */
    444 	if (error == 0 && (cmd & F_FSOUT) && size)
    445 		error = copyout(data, arg, size);
    446 	if (memp)
    447 		free(memp, M_IOCTLOPS);
    448 	return (error);
    449 }
    450 
    451 int
    452 do_fcntl_lock(struct lwp *l, int fd, int cmd, struct flock *fl)
    453 {
    454 	struct file *fp;
    455 	struct vnode *vp;
    456 	struct proc *p = l->l_proc;
    457 	int error, flg;
    458 
    459 	if ((fp = fd_getfile(p->p_fd, fd)) == NULL)
    460 		return (EBADF);
    461 
    462 	FILE_USE(fp);
    463 
    464 	if (fp->f_type != DTYPE_VNODE) {
    465 		error = EINVAL;
    466 		goto out;
    467 	}
    468 	vp = (struct vnode *)fp->f_data;
    469 	if (fl->l_whence == SEEK_CUR)
    470 		fl->l_start += fp->f_offset;
    471 
    472 	flg = F_POSIX;
    473 
    474 	switch (cmd) {
    475 
    476 	case F_SETLKW:
    477 		flg |= F_WAIT;
    478 		/* Fall into F_SETLK */
    479 
    480 	case F_SETLK:
    481 		switch (fl->l_type) {
    482 		case F_RDLCK:
    483 			if ((fp->f_flag & FREAD) == 0) {
    484 				error = EBADF;
    485 				goto out;
    486 			}
    487 			p->p_flag |= PK_ADVLOCK;
    488 			error = VOP_ADVLOCK(vp, p, F_SETLK, fl, flg);
    489 			goto out;
    490 
    491 		case F_WRLCK:
    492 			if ((fp->f_flag & FWRITE) == 0) {
    493 				error = EBADF;
    494 				goto out;
    495 			}
    496 			p->p_flag |= PK_ADVLOCK;
    497 			error = VOP_ADVLOCK(vp, p, F_SETLK, fl, flg);
    498 			goto out;
    499 
    500 		case F_UNLCK:
    501 			error = VOP_ADVLOCK(vp, p, F_UNLCK, fl, F_POSIX);
    502 			goto out;
    503 
    504 		default:
    505 			error = EINVAL;
    506 			goto out;
    507 		}
    508 
    509 	case F_GETLK:
    510 		if (fl->l_type != F_RDLCK &&
    511 		    fl->l_type != F_WRLCK &&
    512 		    fl->l_type != F_UNLCK) {
    513 			error = EINVAL;
    514 			goto out;
    515 		}
    516 		error = VOP_ADVLOCK(vp, p, F_GETLK, fl, F_POSIX);
    517 		break;
    518 
    519 	default:
    520 		error = EINVAL;
    521 		break;
    522 	}
    523 
    524     out:
    525 	FILE_UNUSE(fp, l);
    526 	return error;
    527 }
    528 
    529 /*
    530  * The file control system call.
    531  */
    532 /* ARGSUSED */
    533 int
    534 sys_fcntl(struct lwp *l, void *v, register_t *retval)
    535 {
    536 	struct sys_fcntl_args /* {
    537 		syscallarg(int)		fd;
    538 		syscallarg(int)		cmd;
    539 		syscallarg(void *)	arg;
    540 	} */ *uap = v;
    541 	struct filedesc *fdp;
    542 	struct file	*fp;
    543 	struct proc	*p;
    544 	int		fd, i, tmp, error, cmd, newmin;
    545 	struct flock	fl;
    546 
    547 	p = l->l_proc;
    548 	fd = SCARG(uap, fd);
    549 	cmd = SCARG(uap, cmd);
    550 	fdp = p->p_fd;
    551 	error = 0;
    552 
    553 	switch (cmd) {
    554 	case F_CLOSEM:
    555 		if (fd < 0)
    556 			return EBADF;
    557 		while (fdp->fd_lastfile >= fd)
    558 			fdrelease(l, fdp->fd_lastfile);
    559 		return 0;
    560 
    561 	case F_MAXFD:
    562 		*retval = fdp->fd_lastfile;
    563 		return 0;
    564 
    565 	case F_SETLKW:
    566 	case F_SETLK:
    567 	case F_GETLK:
    568 		error = copyin(SCARG(uap, arg), &fl, sizeof(fl));
    569 		if (error)
    570 			return error;
    571 		error = do_fcntl_lock(l, fd, cmd, &fl);
    572 		if (cmd == F_GETLK && error == 0)
    573 			error = copyout(&fl, SCARG(uap, arg), sizeof(fl));
    574 		return error;
    575 
    576 	default:
    577 		/* Handled below */
    578 		break;
    579 	}
    580 
    581  restart:
    582 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    583 		return (EBADF);
    584 
    585 	FILE_USE(fp);
    586 
    587 	if ((cmd & F_FSCTL)) {
    588 		error = fcntl_forfs(fd, l, cmd, SCARG(uap, arg));
    589 		goto out;
    590 	}
    591 
    592 	switch (cmd) {
    593 
    594 	case F_DUPFD:
    595 		newmin = (long)SCARG(uap, arg);
    596 		if ((u_int)newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
    597 		    (u_int)newmin >= maxfiles) {
    598 			error = EINVAL;
    599 			goto out;
    600 		}
    601 		if ((error = fdalloc(p, newmin, &i)) != 0) {
    602 			if (error == ENOSPC) {
    603 				fdexpand(p);
    604 				FILE_UNUSE(fp, l);
    605 				goto restart;
    606 			}
    607 			goto out;
    608 		}
    609 
    610 		/* finishdup() will unuse the descriptors for us */
    611 		return (finishdup(l, fd, i, retval));
    612 
    613 	case F_GETFD:
    614 		*retval = fdp->fd_ofileflags[fd] & UF_EXCLOSE ? 1 : 0;
    615 		break;
    616 
    617 	case F_SETFD:
    618 		if ((long)SCARG(uap, arg) & 1)
    619 			fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
    620 		else
    621 			fdp->fd_ofileflags[fd] &= ~UF_EXCLOSE;
    622 		break;
    623 
    624 	case F_GETFL:
    625 		*retval = OFLAGS(fp->f_flag);
    626 		break;
    627 
    628 	case F_SETFL:
    629 		tmp = FFLAGS((long)SCARG(uap, arg)) & FCNTLFLAGS;
    630 		error = (*fp->f_ops->fo_fcntl)(fp, F_SETFL, &tmp, l);
    631 		if (error)
    632 			break;
    633 		i = tmp ^ fp->f_flag;
    634 		if (i & FNONBLOCK) {
    635 			int flgs = tmp & FNONBLOCK;
    636 			error = (*fp->f_ops->fo_ioctl)(fp, FIONBIO, &flgs, l);
    637 			if (error)
    638 				goto reset_fcntl;
    639 		}
    640 		if (i & FASYNC) {
    641 			int flgs = tmp & FASYNC;
    642 			error = (*fp->f_ops->fo_ioctl)(fp, FIOASYNC, &flgs, l);
    643 			if (error) {
    644 				if (i & FNONBLOCK) {
    645 					tmp = fp->f_flag & FNONBLOCK;
    646 					(void)(*fp->f_ops->fo_ioctl)(fp,
    647 						FIONBIO, &tmp, l);
    648 				}
    649 				goto reset_fcntl;
    650 			}
    651 		}
    652 		fp->f_flag = (fp->f_flag & ~FCNTLFLAGS) | tmp;
    653 		break;
    654 	    reset_fcntl:
    655 		(void)(*fp->f_ops->fo_fcntl)(fp, F_SETFL, &fp->f_flag, l);
    656 		break;
    657 
    658 	case F_GETOWN:
    659 		error = (*fp->f_ops->fo_ioctl)(fp, FIOGETOWN, &tmp, l);
    660 		*retval = tmp;
    661 		break;
    662 
    663 	case F_SETOWN:
    664 		tmp = (int)(intptr_t) SCARG(uap, arg);
    665 		error = (*fp->f_ops->fo_ioctl)(fp, FIOSETOWN, &tmp, l);
    666 		break;
    667 
    668 	default:
    669 		error = EINVAL;
    670 	}
    671 
    672  out:
    673 	FILE_UNUSE(fp, l);
    674 	return (error);
    675 }
    676 
    677 void
    678 fdremove(struct filedesc *fdp, int fd)
    679 {
    680 
    681 	rw_enter(&fdp->fd_lock, RW_WRITER);
    682 	fdp->fd_ofiles[fd] = NULL;
    683 	fd_unused(fdp, fd);
    684 	rw_exit(&fdp->fd_lock);
    685 }
    686 
    687 int
    688 fdrelease(struct lwp *l, int fd)
    689 {
    690 	struct proc *p = l->l_proc;
    691 	struct filedesc	*fdp;
    692 	struct file	**fpp, *fp;
    693 
    694 	fdp = p->p_fd;
    695 	rw_enter(&fdp->fd_lock, RW_WRITER);
    696 	if (fd < 0 || fd > fdp->fd_lastfile)
    697 		goto badf;
    698 	fpp = &fdp->fd_ofiles[fd];
    699 	fp = *fpp;
    700 	if (fp == NULL)
    701 		goto badf;
    702 
    703 	mutex_enter(&fp->f_lock);
    704 	if (!FILE_IS_USABLE(fp)) {
    705 		mutex_exit(&fp->f_lock);
    706 		goto badf;
    707 	}
    708 
    709 	FILE_USE(fp);
    710 
    711 	*fpp = NULL;
    712 	fdp->fd_ofileflags[fd] = 0;
    713 	fd_unused(fdp, fd);
    714 	rw_exit(&fdp->fd_lock);
    715 	if (fd < fdp->fd_knlistsize)
    716 		knote_fdclose(l, fd);
    717 	return (closef(fp, l));
    718 
    719 badf:
    720 	rw_exit(&fdp->fd_lock);
    721 	return (EBADF);
    722 }
    723 
    724 /*
    725  * Close a file descriptor.
    726  */
    727 /* ARGSUSED */
    728 int
    729 sys_close(struct lwp *l, void *v, register_t *retval)
    730 {
    731 	struct sys_close_args /* {
    732 		syscallarg(int)	fd;
    733 	} */ *uap = v;
    734 	int		fd;
    735 	struct filedesc	*fdp;
    736 	struct proc *p;
    737 
    738 	p = l->l_proc;
    739 	fd = SCARG(uap, fd);
    740 	fdp = p->p_fd;
    741 
    742 #if 0
    743 	if (fd_getfile(fdp, fd) == NULL)
    744 		return (EBADF);
    745 #endif
    746 
    747 	return (fdrelease(l, fd));
    748 }
    749 
    750 /*
    751  * Return status information about a file descriptor.
    752  * Common function for compat code.
    753  */
    754 int
    755 do_sys_fstat(struct lwp *l, int fd, struct stat *sb)
    756 {
    757 	struct file	*fp;
    758 	int		error;
    759 
    760 	fp = fd_getfile(l->l_proc->p_fd, fd);
    761 	if (fp == NULL)
    762 		return EBADF;
    763 
    764 	FILE_USE(fp);
    765 	error = (*fp->f_ops->fo_stat)(fp, sb, l);
    766 	FILE_UNUSE(fp, l);
    767 
    768 	return error;
    769 }
    770 
    771 /*
    772  * Return status information about a file descriptor.
    773  */
    774 /* ARGSUSED */
    775 int
    776 sys___fstat30(struct lwp *l, void *v, register_t *retval)
    777 {
    778 	struct sys___fstat30_args /* {
    779 		syscallarg(int)			fd;
    780 		syscallarg(struct stat *)	sb;
    781 	} */ *uap = v;
    782 	struct stat	sb;
    783 	int		error;
    784 
    785 	error = do_sys_fstat(l, SCARG(uap, fd), &sb);
    786 
    787 	if (error == 0)
    788 		error = copyout(&sb, SCARG(uap, sb), sizeof(sb));
    789 
    790 	return (error);
    791 }
    792 
    793 /*
    794  * Return pathconf information about a file descriptor.
    795  */
    796 /* ARGSUSED */
    797 int
    798 sys_fpathconf(struct lwp *l, void *v, register_t *retval)
    799 {
    800 	struct sys_fpathconf_args /* {
    801 		syscallarg(int)	fd;
    802 		syscallarg(int)	name;
    803 	} */ *uap = v;
    804 	int		fd;
    805 	struct filedesc	*fdp;
    806 	struct file	*fp;
    807 	struct proc 	*p;
    808 	struct vnode	*vp;
    809 	int		error;
    810 
    811 	p = l->l_proc;
    812 	fd = SCARG(uap, fd);
    813 	fdp = p->p_fd;
    814 	error = 0;
    815 
    816 	if ((fp = fd_getfile(fdp, fd)) == NULL)
    817 		return (EBADF);
    818 
    819 	FILE_USE(fp);
    820 
    821 	switch (fp->f_type) {
    822 
    823 	case DTYPE_SOCKET:
    824 	case DTYPE_PIPE:
    825 		if (SCARG(uap, name) != _PC_PIPE_BUF)
    826 			error = EINVAL;
    827 		else
    828 			*retval = PIPE_BUF;
    829 		break;
    830 
    831 	case DTYPE_VNODE:
    832 		vp = (struct vnode *)fp->f_data;
    833 		error = VOP_PATHCONF(vp, SCARG(uap, name), retval);
    834 		break;
    835 
    836 	case DTYPE_KQUEUE:
    837 		error = EINVAL;
    838 		break;
    839 
    840 	default:
    841 		error = EOPNOTSUPP;
    842 		break;
    843 	}
    844 
    845 	FILE_UNUSE(fp, l);
    846 	return (error);
    847 }
    848 
    849 /*
    850  * Allocate a file descriptor for the process.
    851  */
    852 int	fdexpanded;		/* XXX: what else uses this? */
    853 
    854 int
    855 fdalloc(struct proc *p, int want, int *result)
    856 {
    857 	struct filedesc	*fdp;
    858 	int i, lim, last, error;
    859 	u_int off, new;
    860 
    861 	fdp = p->p_fd;
    862 	rw_enter(&fdp->fd_lock, RW_WRITER);
    863 
    864 	/*
    865 	 * Search for a free descriptor starting at the higher
    866 	 * of want or fd_freefile.  If that fails, consider
    867 	 * expanding the ofile array.
    868 	 */
    869 	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfiles);
    870 	last = min(fdp->fd_nfiles, lim);
    871  again:
    872 	if ((i = want) < fdp->fd_freefile)
    873 		i = fdp->fd_freefile;
    874 	off = i >> NDENTRYSHIFT;
    875 	new = find_next_zero(fdp->fd_himap, off,
    876 	    (last + NDENTRIES - 1) >> NDENTRYSHIFT);
    877 	if (new != -1) {
    878 		i = find_next_zero(&fdp->fd_lomap[new],
    879 		    new > off ? 0 : i & NDENTRYMASK, NDENTRIES);
    880 		if (i == -1) {
    881 			/*
    882 			 * free file descriptor in this block was
    883 			 * below want, try again with higher want.
    884 			 */
    885 			want = (new + 1) << NDENTRYSHIFT;
    886 			goto again;
    887 		}
    888 		i += (new << NDENTRYSHIFT);
    889 		if (i < last) {
    890 			if (fdp->fd_ofiles[i] == NULL) {
    891 				fd_used(fdp, i);
    892 				if (want <= fdp->fd_freefile)
    893 					fdp->fd_freefile = i;
    894 				*result = i;
    895 				error = 0;
    896 				goto out;
    897 			}
    898 		}
    899 	}
    900 
    901 	/* No space in current array.  Expand or let the caller do it. */
    902 	error = (fdp->fd_nfiles >= lim) ? EMFILE : ENOSPC;
    903 
    904 out:
    905 	rw_exit(&fdp->fd_lock);
    906 	return (error);
    907 }
    908 
    909 void
    910 fdexpand(struct proc *p)
    911 {
    912 	struct filedesc	*fdp;
    913 	int		i, numfiles, oldnfiles;
    914 	struct file	**newofile;
    915 	char		*newofileflags;
    916 	uint32_t	*newhimap = NULL, *newlomap = NULL;
    917 
    918 	fdp = p->p_fd;
    919 
    920 restart:
    921 	oldnfiles = fdp->fd_nfiles;
    922 
    923 	if (oldnfiles < NDEXTENT)
    924 		numfiles = NDEXTENT;
    925 	else
    926 		numfiles = 2 * oldnfiles;
    927 
    928 	newofile = malloc(numfiles * OFILESIZE, M_FILEDESC, M_WAITOK);
    929 	if (NDHISLOTS(numfiles) > NDHISLOTS(oldnfiles)) {
    930 		newhimap = malloc(NDHISLOTS(numfiles) * sizeof(uint32_t),
    931 		    M_FILEDESC, M_WAITOK);
    932 		newlomap = malloc(NDLOSLOTS(numfiles) * sizeof(uint32_t),
    933 		    M_FILEDESC, M_WAITOK);
    934 	}
    935 
    936 	rw_enter(&fdp->fd_lock, RW_WRITER);
    937 	/* lock fdp */
    938 	if (fdp->fd_nfiles != oldnfiles) {
    939 		/* fdp changed; retry */
    940 		rw_exit(&fdp->fd_lock);
    941 		free(newofile, M_FILEDESC);
    942 		if (newhimap != NULL) free(newhimap, M_FILEDESC);
    943 		if (newlomap != NULL) free(newlomap, M_FILEDESC);
    944 		goto restart;
    945 	}
    946 
    947 	newofileflags = (char *) &newofile[numfiles];
    948 	/*
    949 	 * Copy the existing ofile and ofileflags arrays
    950 	 * and zero the new portion of each array.
    951 	 */
    952 	memcpy(newofile, fdp->fd_ofiles,
    953 	    (i = sizeof(struct file *) * fdp->fd_nfiles));
    954 	memset((char *)newofile + i, 0,
    955 	    numfiles * sizeof(struct file *) - i);
    956 	memcpy(newofileflags, fdp->fd_ofileflags,
    957 	    (i = sizeof(char) * fdp->fd_nfiles));
    958 	memset(newofileflags + i, 0, numfiles * sizeof(char) - i);
    959 	if (oldnfiles > NDFILE)
    960 		free(fdp->fd_ofiles, M_FILEDESC);
    961 
    962 	if (NDHISLOTS(numfiles) > NDHISLOTS(oldnfiles)) {
    963 		memcpy(newhimap, fdp->fd_himap,
    964 		    (i = NDHISLOTS(oldnfiles) * sizeof(uint32_t)));
    965 		memset((char *)newhimap + i, 0,
    966 		    NDHISLOTS(numfiles) * sizeof(uint32_t) - i);
    967 
    968 		memcpy(newlomap, fdp->fd_lomap,
    969 		    (i = NDLOSLOTS(oldnfiles) * sizeof(uint32_t)));
    970 		memset((char *)newlomap + i, 0,
    971 		    NDLOSLOTS(numfiles) * sizeof(uint32_t) - i);
    972 
    973 		if (NDHISLOTS(oldnfiles) > NDHISLOTS(NDFILE)) {
    974 			free(fdp->fd_himap, M_FILEDESC);
    975 			free(fdp->fd_lomap, M_FILEDESC);
    976 		}
    977 		fdp->fd_himap = newhimap;
    978 		fdp->fd_lomap = newlomap;
    979 	}
    980 
    981 	fdp->fd_ofiles = newofile;
    982 	fdp->fd_ofileflags = newofileflags;
    983 	fdp->fd_nfiles = numfiles;
    984 
    985 	rw_exit(&fdp->fd_lock);
    986 
    987 	fdexpanded++;
    988 }
    989 
    990 /*
    991  * Create a new open file structure and allocate
    992  * a file descriptor for the process that refers to it.
    993  */
    994 int
    995 falloc(struct lwp *l, struct file **resultfp, int *resultfd)
    996 {
    997 	struct filedesc	*fdp;
    998 	struct file	*fp, *fq;
    999 	struct proc	*p;
   1000 	int		error, i;
   1001 
   1002 	p = l->l_proc;
   1003 	fdp = p->p_fd;
   1004 
   1005  restart:
   1006 	if ((error = fdalloc(p, 0, &i)) != 0) {
   1007 		if (error == ENOSPC) {
   1008 			fdexpand(p);
   1009 			goto restart;
   1010 		}
   1011 		return (error);
   1012 	}
   1013 
   1014 	fp = pool_get(&file_pool, PR_WAITOK);
   1015 	memset(fp, 0, sizeof(struct file));
   1016 	mutex_init(&fp->f_lock, MUTEX_DEFAULT, IPL_NONE);
   1017 	mutex_enter(&filelist_lock);
   1018 	if (nfiles >= maxfiles) {
   1019 		tablefull("file", "increase kern.maxfiles or MAXFILES");
   1020 		mutex_exit(&filelist_lock);
   1021 		rw_enter(&fdp->fd_lock, RW_WRITER);
   1022 		fd_unused(fdp, i);
   1023 		rw_exit(&fdp->fd_lock);
   1024 		mutex_destroy(&fp->f_lock);
   1025 		pool_put(&file_pool, fp);
   1026 		return (ENFILE);
   1027 	}
   1028 	/*
   1029 	 * Allocate a new file descriptor.
   1030 	 * If the process has file descriptor zero open, add to the list
   1031 	 * of open files at that point, otherwise put it at the front of
   1032 	 * the list of open files.
   1033 	 */
   1034 	nfiles++;
   1035 	fp->f_iflags = FIF_LARVAL;
   1036 	cv_init(&fp->f_cv, "closef");
   1037 	rw_enter(&fdp->fd_lock, RW_WRITER);	/* XXXAD check order */
   1038 	if ((fq = fdp->fd_ofiles[0]) != NULL) {
   1039 		LIST_INSERT_AFTER(fq, fp, f_list);
   1040 	} else {
   1041 		LIST_INSERT_HEAD(&filehead, fp, f_list);
   1042 	}
   1043 	KDASSERT(fdp->fd_ofiles[i] == NULL);
   1044 	fdp->fd_ofiles[i] = fp;
   1045 	fp->f_count = 1;
   1046 	fp->f_cred = l->l_cred;
   1047 	kauth_cred_hold(fp->f_cred);
   1048 	if (resultfp) {
   1049 		fp->f_usecount = 1;
   1050 		*resultfp = fp;
   1051 	}
   1052 	mutex_exit(&filelist_lock);
   1053 	rw_exit(&fdp->fd_lock);
   1054 	if (resultfd)
   1055 		*resultfd = i;
   1056 
   1057 	return (0);
   1058 }
   1059 
   1060 /*
   1061  * Free a file descriptor.
   1062  */
   1063 void
   1064 ffree(struct file *fp)
   1065 {
   1066 	kauth_cred_t cred;
   1067 
   1068 #ifdef DIAGNOSTIC
   1069 	if (fp->f_usecount)
   1070 		panic("ffree");
   1071 #endif
   1072 
   1073 	mutex_enter(&filelist_lock);
   1074 	LIST_REMOVE(fp, f_list);
   1075 	cred = fp->f_cred;
   1076 #ifdef DIAGNOSTIC
   1077 	fp->f_cred = NULL;
   1078 	fp->f_count = 0; /* What's the point? */
   1079 #endif
   1080 	nfiles--;
   1081 	mutex_exit(&filelist_lock);
   1082 	mutex_destroy(&fp->f_lock);
   1083 	cv_destroy(&fp->f_cv);
   1084 	pool_put(&file_pool, fp);
   1085 	kauth_cred_free(cred);
   1086 }
   1087 
   1088 /*
   1089  * Create an initial cwdinfo structure, using the same current and root
   1090  * directories as p.
   1091  */
   1092 struct cwdinfo *
   1093 cwdinit(struct proc *p)
   1094 {
   1095 	struct cwdinfo *cwdi;
   1096 	struct cwdinfo *copy;
   1097 
   1098 	cwdi = pool_get(&cwdi_pool, PR_WAITOK);
   1099 	copy = p->p_cwdi;
   1100 
   1101 	rw_init(&cwdi->cwdi_lock);
   1102 
   1103 	rw_enter(&copy->cwdi_lock, RW_READER);
   1104 	cwdi->cwdi_cdir = p->p_cwdi->cwdi_cdir;
   1105 	if (cwdi->cwdi_cdir)
   1106 		VREF(cwdi->cwdi_cdir);
   1107 	cwdi->cwdi_rdir = p->p_cwdi->cwdi_rdir;
   1108 	if (cwdi->cwdi_rdir)
   1109 		VREF(cwdi->cwdi_rdir);
   1110 	cwdi->cwdi_edir = p->p_cwdi->cwdi_edir;
   1111 	if (cwdi->cwdi_edir)
   1112 		VREF(cwdi->cwdi_edir);
   1113 	cwdi->cwdi_cmask =  p->p_cwdi->cwdi_cmask;
   1114 	cwdi->cwdi_refcnt = 1;
   1115 	rw_exit(&copy->cwdi_lock);
   1116 
   1117 	return (cwdi);
   1118 }
   1119 
   1120 /*
   1121  * Make p2 share p1's cwdinfo.
   1122  */
   1123 void
   1124 cwdshare(struct proc *p1, struct proc *p2)
   1125 {
   1126 	struct cwdinfo *cwdi = p1->p_cwdi;
   1127 
   1128 	rw_enter(&cwdi->cwdi_lock, RW_WRITER);
   1129 	cwdi->cwdi_refcnt++;
   1130 	rw_exit(&cwdi->cwdi_lock);
   1131 	p2->p_cwdi = cwdi;
   1132 }
   1133 
   1134 /*
   1135  * Make this process not share its cwdinfo structure, maintaining
   1136  * all cwdinfo state.
   1137  */
   1138 void
   1139 cwdunshare(struct proc *p)
   1140 {
   1141 	struct cwdinfo *oldcwdi, *newcwdi;
   1142 
   1143 	if (p->p_cwdi->cwdi_refcnt == 1)
   1144 		return;
   1145 
   1146 	newcwdi = cwdinit(p);
   1147 	oldcwdi = p->p_cwdi;
   1148 	p->p_cwdi = newcwdi;
   1149 	cwdfree(oldcwdi);
   1150 }
   1151 
   1152 /*
   1153  * Release a cwdinfo structure.
   1154  */
   1155 void
   1156 cwdfree(struct cwdinfo *cwdi)
   1157 {
   1158 	int n;
   1159 
   1160 	rw_enter(&cwdi->cwdi_lock, RW_WRITER);
   1161 	n = --cwdi->cwdi_refcnt;
   1162 	rw_exit(&cwdi->cwdi_lock);
   1163 	if (n > 0)
   1164 		return;
   1165 
   1166 	vrele(cwdi->cwdi_cdir);
   1167 	if (cwdi->cwdi_rdir)
   1168 		vrele(cwdi->cwdi_rdir);
   1169 	if (cwdi->cwdi_edir)
   1170 		vrele(cwdi->cwdi_edir);
   1171 	rw_destroy(&cwdi->cwdi_lock);
   1172 	pool_put(&cwdi_pool, cwdi);
   1173 }
   1174 
   1175 /*
   1176  * Create an initial filedesc structure, using the same current and root
   1177  * directories as p.
   1178  */
   1179 struct filedesc *
   1180 fdinit(struct proc *p)
   1181 {
   1182 	struct filedesc0 *newfdp;
   1183 
   1184 	newfdp = pool_get(&filedesc0_pool, PR_WAITOK);
   1185 	memset(newfdp, 0, sizeof(struct filedesc0));
   1186 
   1187 	fdinit1(newfdp);
   1188 
   1189 	return (&newfdp->fd_fd);
   1190 }
   1191 
   1192 /*
   1193  * Initialize a file descriptor table.
   1194  */
   1195 void
   1196 fdinit1(struct filedesc0 *newfdp)
   1197 {
   1198 
   1199 	newfdp->fd_fd.fd_refcnt = 1;
   1200 	newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
   1201 	newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
   1202 	newfdp->fd_fd.fd_nfiles = NDFILE;
   1203 	newfdp->fd_fd.fd_knlistsize = -1;
   1204 	newfdp->fd_fd.fd_himap = newfdp->fd_dhimap;
   1205 	newfdp->fd_fd.fd_lomap = newfdp->fd_dlomap;
   1206 	newfdp->fd_fd.fd_lastfile = -1;
   1207 	rw_init(&newfdp->fd_fd.fd_lock);
   1208 }
   1209 
   1210 /*
   1211  * Make p2 share p1's filedesc structure.
   1212  */
   1213 void
   1214 fdshare(struct proc *p1, struct proc *p2)
   1215 {
   1216 	struct filedesc *fdp = p1->p_fd;
   1217 
   1218 	rw_enter(&fdp->fd_lock, RW_WRITER);
   1219 	p2->p_fd = fdp;
   1220 	fdp->fd_refcnt++;
   1221 	rw_exit(&fdp->fd_lock);
   1222 }
   1223 
   1224 /*
   1225  * Make this process not share its filedesc structure, maintaining
   1226  * all file descriptor state.
   1227  */
   1228 void
   1229 fdunshare(struct lwp *l)
   1230 {
   1231 	struct proc *p = l->l_proc;
   1232 	struct filedesc *newfd;
   1233 
   1234 	if (p->p_fd->fd_refcnt == 1)
   1235 		return;
   1236 
   1237 	newfd = fdcopy(p);
   1238 	fdfree(l);
   1239 	p->p_fd = newfd;
   1240 }
   1241 
   1242 /*
   1243  * Clear a process's fd table.
   1244  */
   1245 void
   1246 fdclear(struct lwp *l)
   1247 {
   1248 	struct proc *p = l->l_proc;
   1249 	struct filedesc *newfd;
   1250 
   1251 	newfd = fdinit(p);
   1252 	fdfree(l);
   1253 	p->p_fd = newfd;
   1254 }
   1255 
   1256 /*
   1257  * Copy a filedesc structure.
   1258  */
   1259 struct filedesc *
   1260 fdcopy(struct proc *p)
   1261 {
   1262 	struct filedesc	*newfdp, *fdp;
   1263 	struct file	**fpp, **nfpp;
   1264 	int		i, numfiles, lastfile;
   1265 
   1266 	fdp = p->p_fd;
   1267 	newfdp = pool_get(&filedesc0_pool, PR_WAITOK);
   1268 	newfdp->fd_refcnt = 1;
   1269 	rw_init(&newfdp->fd_lock);
   1270 
   1271 restart:
   1272 	numfiles = fdp->fd_nfiles;
   1273 	lastfile = fdp->fd_lastfile;
   1274 
   1275 	/*
   1276 	 * If the number of open files fits in the internal arrays
   1277 	 * of the open file structure, use them, otherwise allocate
   1278 	 * additional memory for the number of descriptors currently
   1279 	 * in use.
   1280 	 */
   1281 	if (lastfile < NDFILE) {
   1282 		i = NDFILE;
   1283 	} else {
   1284 		/*
   1285 		 * Compute the smallest multiple of NDEXTENT needed
   1286 		 * for the file descriptors currently in use,
   1287 		 * allowing the table to shrink.
   1288 		 */
   1289 		i = numfiles;
   1290 		while (i >= 2 * NDEXTENT && i > lastfile * 2)
   1291 			i /= 2;
   1292 		newfdp->fd_ofiles = malloc(i * OFILESIZE, M_FILEDESC, M_WAITOK);
   1293 	}
   1294 	if (NDHISLOTS(i) > NDHISLOTS(NDFILE)) {
   1295 		newfdp->fd_himap = malloc(NDHISLOTS(i) * sizeof(uint32_t),
   1296 		    M_FILEDESC, M_WAITOK);
   1297 		newfdp->fd_lomap = malloc(NDLOSLOTS(i) * sizeof(uint32_t),
   1298 		    M_FILEDESC, M_WAITOK);
   1299 	}
   1300 
   1301 	rw_enter(&fdp->fd_lock, RW_READER);
   1302 	if (numfiles != fdp->fd_nfiles || lastfile != fdp->fd_lastfile) {
   1303 		rw_exit(&fdp->fd_lock);
   1304 		if (i > NDFILE)
   1305 			free(newfdp->fd_ofiles, M_FILEDESC);
   1306 		if (NDHISLOTS(i) > NDHISLOTS(NDFILE)) {
   1307 			free(newfdp->fd_himap, M_FILEDESC);
   1308 			free(newfdp->fd_lomap, M_FILEDESC);
   1309 		}
   1310 		goto restart;
   1311 	}
   1312 
   1313 	if (lastfile < NDFILE) {
   1314 		newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
   1315 		newfdp->fd_ofileflags =
   1316 		    ((struct filedesc0 *) newfdp)->fd_dfileflags;
   1317 	} else {
   1318 		newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
   1319 	}
   1320 	if (NDHISLOTS(i) <= NDHISLOTS(NDFILE)) {
   1321 		newfdp->fd_himap =
   1322 		    ((struct filedesc0 *) newfdp)->fd_dhimap;
   1323 		newfdp->fd_lomap =
   1324 		    ((struct filedesc0 *) newfdp)->fd_dlomap;
   1325 	}
   1326 
   1327 	newfdp->fd_nfiles = i;
   1328 	newfdp->fd_lastfile = lastfile;
   1329 	newfdp->fd_freefile = fdp->fd_freefile;
   1330 
   1331 	/* Clear the entries that will not be copied over.
   1332 	 * Avoid calling memset with 0 size (i.e. when
   1333 	 * lastfile == i-1 */
   1334 	if (lastfile < (i-1))
   1335 		memset(newfdp->fd_ofiles + lastfile + 1, 0,
   1336 		    (i - lastfile - 1) * sizeof(struct file **));
   1337 	memcpy(newfdp->fd_ofileflags, fdp->fd_ofileflags, i * sizeof(char));
   1338 	if (i < NDENTRIES * NDENTRIES)
   1339 		i = NDENTRIES * NDENTRIES; /* size of inlined bitmaps */
   1340 	memcpy(newfdp->fd_himap, fdp->fd_himap, NDHISLOTS(i)*sizeof(uint32_t));
   1341 	memcpy(newfdp->fd_lomap, fdp->fd_lomap, NDLOSLOTS(i)*sizeof(uint32_t));
   1342 
   1343 	fpp = fdp->fd_ofiles;
   1344 	nfpp = newfdp->fd_ofiles;
   1345 	for (i = 0; i <= lastfile; i++, fpp++, nfpp++) {
   1346 		if ((*nfpp = *fpp) == NULL)
   1347 			continue;
   1348 
   1349 		if ((*fpp)->f_type == DTYPE_KQUEUE)
   1350 			/* kq descriptors cannot be copied. */
   1351 			fdremove(newfdp, i);
   1352 		else {
   1353 			mutex_enter(&(*fpp)->f_lock);
   1354 			(*fpp)->f_count++;
   1355 			mutex_exit(&(*fpp)->f_lock);
   1356 		}
   1357 	}
   1358 
   1359 	rw_exit(&fdp->fd_lock);
   1360 
   1361 	newfdp->fd_knlist = NULL;
   1362 	newfdp->fd_knlistsize = -1;
   1363 	newfdp->fd_knhash = NULL;
   1364 	newfdp->fd_knhashmask = 0;
   1365 
   1366 	return (newfdp);
   1367 }
   1368 
   1369 /*
   1370  * Release a filedesc structure.
   1371  */
   1372 void
   1373 fdfree(struct lwp *l)
   1374 {
   1375 	struct proc	*p = l->l_proc;
   1376 	struct filedesc	*fdp;
   1377 	struct file	**fpp, *fp;
   1378 	int		i;
   1379 
   1380 	fdp = p->p_fd;
   1381 	rw_enter(&fdp->fd_lock, RW_WRITER);
   1382 	i = --fdp->fd_refcnt;
   1383 	rw_exit(&fdp->fd_lock);
   1384 	if (i > 0)
   1385 		return;
   1386 
   1387 	rw_destroy(&fdp->fd_lock);
   1388 	fpp = fdp->fd_ofiles;
   1389 	for (i = fdp->fd_lastfile; i >= 0; i--, fpp++) {
   1390 		fp = *fpp;
   1391 		if (fp != NULL) {
   1392 			*fpp = NULL;
   1393 			mutex_enter(&fp->f_lock);
   1394 			FILE_USE(fp);
   1395 			if ((fdp->fd_lastfile - i) < fdp->fd_knlistsize)
   1396 				knote_fdclose(l, fdp->fd_lastfile - i);
   1397 			(void) closef(fp, l);
   1398 		}
   1399 	}
   1400 	p->p_fd = NULL;
   1401 	if (fdp->fd_nfiles > NDFILE)
   1402 		free(fdp->fd_ofiles, M_FILEDESC);
   1403 	if (NDHISLOTS(fdp->fd_nfiles) > NDHISLOTS(NDFILE)) {
   1404 		free(fdp->fd_himap, M_FILEDESC);
   1405 		free(fdp->fd_lomap, M_FILEDESC);
   1406 	}
   1407 	if (fdp->fd_knlist)
   1408 		free(fdp->fd_knlist, M_KEVENT);
   1409 	if (fdp->fd_knhash)
   1410 		hashdone(fdp->fd_knhash, M_KEVENT);
   1411 	pool_put(&filedesc0_pool, fdp);
   1412 }
   1413 
   1414 /*
   1415  * Internal form of close.
   1416  * Decrement reference count on file structure.
   1417  * Note: p may be NULL when closing a file
   1418  * that was being passed in a message.
   1419  *
   1420  * Note: we expect the caller is holding a usecount, and expects us
   1421  * to drop it (the caller thinks the file is going away forever).
   1422  */
   1423 int
   1424 closef(struct file *fp, struct lwp *l)
   1425 {
   1426 	struct proc	*p = l ? l->l_proc : NULL;
   1427 	struct vnode	*vp;
   1428 	struct flock	lf;
   1429 	int		error;
   1430 
   1431 	if (fp == NULL)
   1432 		return (0);
   1433 
   1434 	/*
   1435 	 * POSIX record locking dictates that any close releases ALL
   1436 	 * locks owned by this process.  This is handled by setting
   1437 	 * a flag in the unlock to free ONLY locks obeying POSIX
   1438 	 * semantics, and not to free BSD-style file locks.
   1439 	 * If the descriptor was in a message, POSIX-style locks
   1440 	 * aren't passed with the descriptor.
   1441 	 */
   1442 	if (p && (p->p_flag & PK_ADVLOCK) && fp->f_type == DTYPE_VNODE) {
   1443 		lf.l_whence = SEEK_SET;
   1444 		lf.l_start = 0;
   1445 		lf.l_len = 0;
   1446 		lf.l_type = F_UNLCK;
   1447 		vp = (struct vnode *)fp->f_data;
   1448 		(void) VOP_ADVLOCK(vp, p, F_UNLCK, &lf, F_POSIX);
   1449 	}
   1450 
   1451 	/*
   1452 	 * If WANTCLOSE is set, then the reference count on the file
   1453 	 * is 0, but there were multiple users of the file.  This can
   1454 	 * happen if a filedesc structure is shared by multiple
   1455 	 * processes.
   1456 	 */
   1457 	mutex_enter(&fp->f_lock);
   1458 	if (fp->f_iflags & FIF_WANTCLOSE) {
   1459 		/*
   1460 		 * Another user of the file is already closing, and is
   1461 		 * simply waiting for other users of the file to drain.
   1462 		 * Release our usecount, and wake up the closer if it
   1463 		 * is the only remaining use.
   1464 		 */
   1465 #ifdef DIAGNOSTIC
   1466 		if (fp->f_count != 0)
   1467 			panic("closef: wantclose and count != 0");
   1468 		if (fp->f_usecount < 2)
   1469 			panic("closef: wantclose and usecount < 2");
   1470 #endif
   1471 		if (--fp->f_usecount == 1)
   1472 			cv_broadcast(&fp->f_cv);
   1473 		mutex_exit(&fp->f_lock);
   1474 		return (0);
   1475 	} else {
   1476 		/*
   1477 		 * Decrement the reference count.  If we were not the
   1478 		 * last reference, then release our use and just
   1479 		 * return.
   1480 		 */
   1481 		if (--fp->f_count > 0) {
   1482 #ifdef DIAGNOSTIC
   1483 			if (fp->f_usecount < 1)
   1484 				panic("closef: no wantclose and usecount < 1");
   1485 #endif
   1486 			fp->f_usecount--;
   1487 			mutex_exit(&fp->f_lock);
   1488 			return (0);
   1489 		}
   1490 	}
   1491 
   1492 	/*
   1493 	 * The reference count is now 0.  However, there may be
   1494 	 * multiple potential users of this file.  This can happen
   1495 	 * if multiple processes shared a single filedesc structure.
   1496 	 *
   1497 	 * Notify these potential users that the file is closing.
   1498 	 * This will prevent them from adding additional uses to
   1499 	 * the file.
   1500 	 */
   1501 	fp->f_iflags |= FIF_WANTCLOSE;
   1502 
   1503 	/*
   1504 	 * We expect the caller to add a use to the file.  So, if we
   1505 	 * are the last user, usecount will be 1.  If it is not, we
   1506 	 * must wait for the usecount to drain.  When it drains back
   1507 	 * to 1, we will be awakened so that we may proceed with the
   1508 	 * close.
   1509 	 */
   1510 #ifdef DIAGNOSTIC
   1511 	if (fp->f_usecount < 1)
   1512 		panic("closef: usecount < 1");
   1513 #endif
   1514 	while (fp->f_usecount > 1)
   1515 		cv_wait(&fp->f_cv, &fp->f_lock);
   1516 #ifdef DIAGNOSTIC
   1517 	if (fp->f_usecount != 1)
   1518 		panic("closef: usecount != 1");
   1519 #endif
   1520 
   1521 	mutex_exit(&fp->f_lock);
   1522 	if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
   1523 		lf.l_whence = SEEK_SET;
   1524 		lf.l_start = 0;
   1525 		lf.l_len = 0;
   1526 		lf.l_type = F_UNLCK;
   1527 		vp = (struct vnode *)fp->f_data;
   1528 		(void) VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
   1529 	}
   1530 	if (fp->f_ops)
   1531 		error = (*fp->f_ops->fo_close)(fp, l);
   1532 	else
   1533 		error = 0;
   1534 
   1535 	/* Nothing references the file now, drop the final use (us). */
   1536 	fp->f_usecount--;
   1537 
   1538 	ffree(fp);
   1539 	return (error);
   1540 }
   1541 
   1542 /*
   1543  * Apply an advisory lock on a file descriptor.
   1544  *
   1545  * Just attempt to get a record lock of the requested type on
   1546  * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
   1547  */
   1548 /* ARGSUSED */
   1549 int
   1550 sys_flock(struct lwp *l, void *v, register_t *retval)
   1551 {
   1552 	struct sys_flock_args /* {
   1553 		syscallarg(int)	fd;
   1554 		syscallarg(int)	how;
   1555 	} */ *uap = v;
   1556 	int		fd, how, error;
   1557 	struct proc	*p;
   1558 	struct filedesc	*fdp;
   1559 	struct file	*fp;
   1560 	struct vnode	*vp;
   1561 	struct flock	lf;
   1562 
   1563 	p = l->l_proc;
   1564 	fd = SCARG(uap, fd);
   1565 	how = SCARG(uap, how);
   1566 	fdp = p->p_fd;
   1567 	error = 0;
   1568 
   1569 	if ((fp = fd_getfile(fdp, fd)) == NULL)
   1570 		return (EBADF);
   1571 
   1572 	FILE_USE(fp);
   1573 
   1574 	if (fp->f_type != DTYPE_VNODE) {
   1575 		error = EOPNOTSUPP;
   1576 		goto out;
   1577 	}
   1578 
   1579 	vp = (struct vnode *)fp->f_data;
   1580 	lf.l_whence = SEEK_SET;
   1581 	lf.l_start = 0;
   1582 	lf.l_len = 0;
   1583 	if (how & LOCK_UN) {
   1584 		lf.l_type = F_UNLCK;
   1585 		fp->f_flag &= ~FHASLOCK;
   1586 		error = VOP_ADVLOCK(vp, fp, F_UNLCK, &lf, F_FLOCK);
   1587 		goto out;
   1588 	}
   1589 	if (how & LOCK_EX)
   1590 		lf.l_type = F_WRLCK;
   1591 	else if (how & LOCK_SH)
   1592 		lf.l_type = F_RDLCK;
   1593 	else {
   1594 		error = EINVAL;
   1595 		goto out;
   1596 	}
   1597 	fp->f_flag |= FHASLOCK;
   1598 	if (how & LOCK_NB)
   1599 		error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf, F_FLOCK);
   1600 	else
   1601 		error = VOP_ADVLOCK(vp, fp, F_SETLK, &lf,
   1602 		    F_FLOCK|F_WAIT);
   1603  out:
   1604 	FILE_UNUSE(fp, l);
   1605 	return (error);
   1606 }
   1607 
   1608 /* ARGSUSED */
   1609 int
   1610 sys_posix_fadvise(struct lwp *l, void *v, register_t *retval)
   1611 {
   1612 	const struct sys_posix_fadvise_args /* {
   1613 		syscallarg(int) fd;
   1614 		syscallarg(off_t) offset;
   1615 		syscallarg(off_t) len;
   1616 		syscallarg(int) advice;
   1617 	} */ *uap = v;
   1618 	const int fd = SCARG(uap, fd);
   1619 	const int advice = SCARG(uap, advice);
   1620 	struct proc *p = l->l_proc;
   1621 	struct file *fp;
   1622 	int error = 0;
   1623 
   1624 	fp = fd_getfile(p->p_fd, fd);
   1625 	if (fp == NULL) {
   1626 		error = EBADF;
   1627 		goto out;
   1628 	}
   1629 	FILE_USE(fp);
   1630 
   1631 	if (fp->f_type != DTYPE_VNODE) {
   1632 		if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) {
   1633 			error = ESPIPE;
   1634 		} else {
   1635 			error = EOPNOTSUPP;
   1636 		}
   1637 		goto out;
   1638 	}
   1639 
   1640 	switch (advice) {
   1641 	case POSIX_FADV_NORMAL:
   1642 	case POSIX_FADV_RANDOM:
   1643 	case POSIX_FADV_SEQUENTIAL:
   1644 		KASSERT(POSIX_FADV_NORMAL == UVM_ADV_NORMAL);
   1645 		KASSERT(POSIX_FADV_RANDOM == UVM_ADV_RANDOM);
   1646 		KASSERT(POSIX_FADV_SEQUENTIAL == UVM_ADV_SEQUENTIAL);
   1647 
   1648 		/*
   1649 		 * we ignore offset and size.
   1650 		 */
   1651 
   1652 		fp->f_advice = advice;
   1653 		break;
   1654 
   1655 	case POSIX_FADV_WILLNEED:
   1656 	case POSIX_FADV_DONTNEED:
   1657 	case POSIX_FADV_NOREUSE:
   1658 
   1659 		/*
   1660 		 * not implemented yet.
   1661 		 */
   1662 
   1663 		break;
   1664 	default:
   1665 		error = EINVAL;
   1666 		break;
   1667 	}
   1668 out:
   1669 	if (fp != NULL) {
   1670 		FILE_UNUSE(fp, l);
   1671 	}
   1672 	*retval = error;
   1673 	return 0;
   1674 }
   1675 
   1676 /*
   1677  * File Descriptor pseudo-device driver (/dev/fd/).
   1678  *
   1679  * Opening minor device N dup()s the file (if any) connected to file
   1680  * descriptor N belonging to the calling process.  Note that this driver
   1681  * consists of only the ``open()'' routine, because all subsequent
   1682  * references to this file will be direct to the other driver.
   1683  */
   1684 /* ARGSUSED */
   1685 static int
   1686 filedescopen(dev_t dev, int mode, int type, struct lwp *l)
   1687 {
   1688 
   1689 	/*
   1690 	 * XXX Kludge: set dupfd to contain the value of the
   1691 	 * the file descriptor being sought for duplication. The error
   1692 	 * return ensures that the vnode for this device will be released
   1693 	 * by vn_open. Open will detect this special error and take the
   1694 	 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
   1695 	 * will simply report the error.
   1696 	 */
   1697 	l->l_dupfd = minor(dev);	/* XXX */
   1698 	return EDUPFD;
   1699 }
   1700 
   1701 const struct cdevsw filedesc_cdevsw = {
   1702 	filedescopen, noclose, noread, nowrite, noioctl,
   1703 	    nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
   1704 };
   1705 
   1706 /*
   1707  * Duplicate the specified descriptor to a free descriptor.
   1708  *
   1709  * 'indx' has been fdalloc'ed (and will be fdremove'ed on error) by the caller.
   1710  */
   1711 int
   1712 dupfdopen(struct lwp *l, int indx, int dfd, int mode, int error)
   1713 {
   1714 	struct proc	*p = l->l_proc;
   1715 	struct filedesc *fdp;
   1716 	struct file	*wfp;
   1717 
   1718 	fdp = p->p_fd;
   1719 
   1720 	/* should be cleared by the caller */
   1721 	KASSERT(fdp->fd_ofiles[indx] == NULL);
   1722 
   1723 	/*
   1724 	 * If the to-be-dup'd fd number is greater than the allowed number
   1725 	 * of file descriptors, or the fd to be dup'd has already been
   1726 	 * closed, reject.
   1727 	 */
   1728 
   1729 	/*
   1730 	 * Note, in the case of indx == dfd, fd_getfile below returns NULL.
   1731 	 */
   1732 	if ((wfp = fd_getfile(fdp, dfd)) == NULL)
   1733 		return (EBADF);
   1734 
   1735 	FILE_USE(wfp);
   1736 
   1737 	/*
   1738 	 * There are two cases of interest here.
   1739 	 *
   1740 	 * For EDUPFD simply dup (dfd) to file descriptor
   1741 	 * (indx) and return.
   1742 	 *
   1743 	 * For EMOVEFD steal away the file structure from (dfd) and
   1744 	 * store it in (indx).  (dfd) is effectively closed by
   1745 	 * this operation.
   1746 	 *
   1747 	 * Any other error code is just returned.
   1748 	 */
   1749 	switch (error) {
   1750 	case EDUPFD:
   1751 		/*
   1752 		 * Check that the mode the file is being opened for is a
   1753 		 * subset of the mode of the existing descriptor.
   1754 		 */
   1755 		if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
   1756 			FILE_UNUSE(wfp, l);
   1757 			return (EACCES);
   1758 		}
   1759 		rw_enter(&fdp->fd_lock, RW_WRITER);
   1760 		fdp->fd_ofiles[indx] = wfp;
   1761 		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
   1762 		rw_exit(&fdp->fd_lock);
   1763 		mutex_enter(&wfp->f_lock);
   1764 		wfp->f_count++;
   1765 		/* 'indx' has been fd_used'ed by caller */
   1766 		FILE_UNUSE_HAVELOCK(wfp, l);
   1767 		return (0);
   1768 
   1769 	case EMOVEFD:
   1770 		/*
   1771 		 * Steal away the file pointer from dfd, and stuff it into indx.
   1772 		 */
   1773 		rw_enter(&fdp->fd_lock, RW_WRITER);
   1774 		fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
   1775 		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
   1776 		fdp->fd_ofiles[dfd] = NULL;
   1777 		fdp->fd_ofileflags[dfd] = 0;
   1778 		/*
   1779 		 * Complete the clean up of the filedesc structure by
   1780 		 * recomputing the various hints.
   1781 		 */
   1782 		/* 'indx' has been fd_used'ed by caller */
   1783 		fd_unused(fdp, dfd);
   1784 		rw_exit(&fdp->fd_lock);
   1785 		FILE_UNUSE(wfp, l);
   1786 		return (0);
   1787 
   1788 	default:
   1789 		FILE_UNUSE(wfp, l);
   1790 		return (error);
   1791 	}
   1792 	/* NOTREACHED */
   1793 }
   1794 
   1795 /*
   1796  * Close any files on exec?
   1797  */
   1798 void
   1799 fdcloseexec(struct lwp *l)
   1800 {
   1801 	struct proc	*p = l->l_proc;
   1802 	struct filedesc *fdp;
   1803 	int		fd;
   1804 
   1805 	fdunshare(l);
   1806 	cwdunshare(p);
   1807 
   1808 	if (p->p_cwdi->cwdi_edir)
   1809 		vrele(p->p_cwdi->cwdi_edir);
   1810 
   1811 	fdp = p->p_fd;
   1812 	for (fd = 0; fd <= fdp->fd_lastfile; fd++)
   1813 		if (fdp->fd_ofileflags[fd] & UF_EXCLOSE)
   1814 			(void) fdrelease(l, fd);
   1815 }
   1816 
   1817 /*
   1818  * It is unsafe for set[ug]id processes to be started with file
   1819  * descriptors 0..2 closed, as these descriptors are given implicit
   1820  * significance in the Standard C library.  fdcheckstd() will create a
   1821  * descriptor referencing /dev/null for each of stdin, stdout, and
   1822  * stderr that is not already open.
   1823  */
   1824 #define CHECK_UPTO 3
   1825 int
   1826 fdcheckstd(struct lwp *l)
   1827 {
   1828 	struct proc *p;
   1829 	struct nameidata nd;
   1830 	struct filedesc *fdp;
   1831 	struct file *fp;
   1832 	struct file *devnullfp = NULL;	/* Quell compiler warning */
   1833 	struct proc *pp;
   1834 	register_t retval;
   1835 	int fd, i, error, flags = FREAD|FWRITE, devnull = -1;
   1836 	char closed[CHECK_UPTO * 3 + 1], which[3 + 1];
   1837 
   1838 	p = l->l_proc;
   1839 	closed[0] = '\0';
   1840 	if ((fdp = p->p_fd) == NULL)
   1841 		return (0);
   1842 	for (i = 0; i < CHECK_UPTO; i++) {
   1843 		if (fdp->fd_ofiles[i] != NULL)
   1844 			continue;
   1845 		snprintf(which, sizeof(which), ",%d", i);
   1846 		strlcat(closed, which, sizeof(closed));
   1847 		if (devnullfp == NULL) {
   1848 			if ((error = falloc(l, &fp, &fd)) != 0)
   1849 				return (error);
   1850 			NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/null",
   1851 			    l);
   1852 			if ((error = vn_open(&nd, flags, 0)) != 0) {
   1853 				FILE_UNUSE(fp, l);
   1854 				ffree(fp);
   1855 				fdremove(p->p_fd, fd);
   1856 				return (error);
   1857 			}
   1858 			fp->f_data = nd.ni_vp;
   1859 			fp->f_flag = flags;
   1860 			fp->f_ops = &vnops;
   1861 			fp->f_type = DTYPE_VNODE;
   1862 			VOP_UNLOCK(nd.ni_vp, 0);
   1863 			devnull = fd;
   1864 			devnullfp = fp;
   1865 			FILE_SET_MATURE(fp);
   1866 		} else {
   1867 restart:
   1868 			if ((error = fdalloc(p, 0, &fd)) != 0) {
   1869 				if (error == ENOSPC) {
   1870 					fdexpand(p);
   1871 					goto restart;
   1872 				}
   1873 				return (error);
   1874 			}
   1875 
   1876 			mutex_enter(&devnullfp->f_lock);
   1877 			FILE_USE(devnullfp);
   1878 			/* finishdup() will unuse the descriptors for us */
   1879 			if ((error = finishdup(l, devnull, fd, &retval)) != 0)
   1880 				return (error);
   1881 		}
   1882 	}
   1883 	if (devnullfp)
   1884 		FILE_UNUSE(devnullfp, l);
   1885 	if (closed[0] != '\0') {
   1886 		mutex_enter(&proclist_lock);
   1887 		pp = p->p_pptr;
   1888 		mutex_enter(&pp->p_mutex);
   1889 		log(LOG_WARNING, "set{u,g}id pid %d (%s) "
   1890 		    "was invoked by uid %d ppid %d (%s) "
   1891 		    "with fd %s closed\n",
   1892 		    p->p_pid, p->p_comm, kauth_cred_geteuid(pp->p_cred),
   1893 		    pp->p_pid, pp->p_comm, &closed[1]);
   1894 		mutex_exit(&pp->p_mutex);
   1895 		mutex_exit(&proclist_lock);
   1896 	}
   1897 	return (0);
   1898 }
   1899 #undef CHECK_UPTO
   1900 
   1901 /*
   1902  * Sets descriptor owner. If the owner is a process, 'pgid'
   1903  * is set to positive value, process ID. If the owner is process group,
   1904  * 'pgid' is set to -pg_id.
   1905  */
   1906 int
   1907 fsetown(struct proc *p, pid_t *pgid, int cmd, const void *data)
   1908 {
   1909 	int id = *(const int *)data;
   1910 	int error;
   1911 
   1912 	switch (cmd) {
   1913 	case TIOCSPGRP:
   1914 		if (id < 0)
   1915 			return (EINVAL);
   1916 		id = -id;
   1917 		break;
   1918 	default:
   1919 		break;
   1920 	}
   1921 
   1922 	if (id > 0 && !pfind(id))
   1923 		return (ESRCH);
   1924 	else if (id < 0 && (error = pgid_in_session(p, -id)))
   1925 		return (error);
   1926 
   1927 	*pgid = id;
   1928 	return (0);
   1929 }
   1930 
   1931 /*
   1932  * Return descriptor owner information. If the value is positive,
   1933  * it's process ID. If it's negative, it's process group ID and
   1934  * needs the sign removed before use.
   1935  */
   1936 int
   1937 fgetown(struct proc *p, pid_t pgid, int cmd, void *data)
   1938 {
   1939 	switch (cmd) {
   1940 	case TIOCGPGRP:
   1941 		*(int *)data = -pgid;
   1942 		break;
   1943 	default:
   1944 		*(int *)data = pgid;
   1945 		break;
   1946 	}
   1947 	return (0);
   1948 }
   1949 
   1950 /*
   1951  * Send signal to descriptor owner, either process or process group.
   1952  */
   1953 void
   1954 fownsignal(pid_t pgid, int signo, int code, int band, void *fdescdata)
   1955 {
   1956 	struct proc *p1;
   1957 	struct pgrp *pgrp;
   1958 	ksiginfo_t ksi;
   1959 
   1960 	KSI_INIT(&ksi);
   1961 	ksi.ksi_signo = signo;
   1962 	ksi.ksi_code = code;
   1963 	ksi.ksi_band = band;
   1964 
   1965 	/*
   1966 	 * Since we may be called from an interrupt context, we must use
   1967 	 * the proclist_mutex.
   1968 	 */
   1969 	mutex_enter(&proclist_mutex);
   1970 	if (pgid > 0 && (p1 = p_find(pgid, PFIND_LOCKED)))
   1971 		kpsignal(p1, &ksi, fdescdata);
   1972 	else if (pgid < 0 && (pgrp = pg_find(-pgid, PFIND_LOCKED)))
   1973 		kpgsignal(pgrp, &ksi, fdescdata, 0);
   1974 	mutex_exit(&proclist_mutex);
   1975 }
   1976 
   1977 int
   1978 fdclone(struct lwp *l, struct file *fp, int fd, int flag,
   1979     const struct fileops *fops, void *data)
   1980 {
   1981 	fp->f_flag = flag;
   1982 	fp->f_type = DTYPE_MISC;
   1983 	fp->f_ops = fops;
   1984 	fp->f_data = data;
   1985 
   1986 	l->l_dupfd = fd;
   1987 
   1988 	FILE_SET_MATURE(fp);
   1989 	FILE_UNUSE(fp, l);
   1990 	return EMOVEFD;
   1991 }
   1992 
   1993 /* ARGSUSED */
   1994 int
   1995 fnullop_fcntl(struct file *fp, u_int cmd, void *data, struct lwp *l)
   1996 {
   1997 
   1998 	if (cmd == F_SETFL)
   1999 		return 0;
   2000 
   2001 	return EOPNOTSUPP;
   2002 }
   2003 
   2004 /* ARGSUSED */
   2005 int
   2006 fnullop_poll(struct file *fp, int which, struct lwp *l)
   2007 {
   2008 
   2009 	return 0;
   2010 }
   2011 
   2012 
   2013 /* ARGSUSED */
   2014 int
   2015 fnullop_kqfilter(struct file *fp, struct knote *kn)
   2016 {
   2017 
   2018 	return 0;
   2019 }
   2020 
   2021 /* ARGSUSED */
   2022 int
   2023 fbadop_stat(struct file *fp, struct stat *sb, struct lwp *l)
   2024 {
   2025 
   2026 	return EOPNOTSUPP;
   2027 }
   2028