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