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