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