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