xref: /freebsd/sys/kern/kern_descrip.c (revision 4b2eaea43fec8e8792be611dea204071a10b655a)
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)kern_descrip.c	8.6 (Berkeley) 4/19/94
39  * $FreeBSD$
40  */
41 
42 #include "opt_compat.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/syscallsubr.h>
47 #include <sys/sysproto.h>
48 #include <sys/conf.h>
49 #include <sys/filedesc.h>
50 #include <sys/lock.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 #include <sys/mutex.h>
54 #include <sys/sysctl.h>
55 #include <sys/vnode.h>
56 #include <sys/mount.h>
57 #include <sys/proc.h>
58 #include <sys/namei.h>
59 #include <sys/file.h>
60 #include <sys/stat.h>
61 #include <sys/filio.h>
62 #include <sys/fcntl.h>
63 #include <sys/unistd.h>
64 #include <sys/resourcevar.h>
65 #include <sys/event.h>
66 #include <sys/sx.h>
67 #include <sys/socketvar.h>
68 #include <sys/signalvar.h>
69 
70 #include <machine/limits.h>
71 
72 #include <vm/vm.h>
73 #include <vm/vm_extern.h>
74 #include <vm/uma.h>
75 
76 static MALLOC_DEFINE(M_FILEDESC, "file desc", "Open file descriptor table");
77 static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures");
78 
79 uma_zone_t file_zone;
80 
81 static	 d_open_t  fdopen;
82 #define	NUMFDESC 64
83 
84 #define	CDEV_MAJOR 22
85 static struct cdevsw fildesc_cdevsw = {
86 	/* open */	fdopen,
87 	/* close */	noclose,
88 	/* read */	noread,
89 	/* write */	nowrite,
90 	/* ioctl */	noioctl,
91 	/* poll */	nopoll,
92 	/* mmap */	nommap,
93 	/* strategy */	nostrategy,
94 	/* name */	"FD",
95 	/* maj */	CDEV_MAJOR,
96 	/* dump */	nodump,
97 	/* psize */	nopsize,
98 	/* flags */	0,
99 };
100 
101 /* How to treat 'new' parameter when allocating a fd for do_dup(). */
102 enum dup_type { DUP_VARIABLE, DUP_FIXED };
103 
104 static int do_dup(struct thread *td, enum dup_type type, int old, int new,
105     register_t *retval);
106 
107 /*
108  * Descriptor management.
109  */
110 struct filelist filehead;	/* head of list of open files */
111 int nfiles;			/* actual number of open files */
112 extern int cmask;
113 struct sx filelist_lock;	/* sx to protect filelist */
114 struct mtx sigio_lock;		/* mtx to protect pointers to sigio */
115 
116 /*
117  * System calls on descriptors.
118  */
119 #ifndef _SYS_SYSPROTO_H_
120 struct getdtablesize_args {
121 	int	dummy;
122 };
123 #endif
124 /*
125  * MPSAFE
126  */
127 /* ARGSUSED */
128 int
129 getdtablesize(td, uap)
130 	struct thread *td;
131 	struct getdtablesize_args *uap;
132 {
133 	struct proc *p = td->td_proc;
134 
135 	mtx_lock(&Giant);
136 	td->td_retval[0] =
137 	    min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
138 	mtx_unlock(&Giant);
139 	return (0);
140 }
141 
142 /*
143  * Duplicate a file descriptor to a particular value.
144  *
145  * note: keep in mind that a potential race condition exists when closing
146  * descriptors from a shared descriptor table (via rfork).
147  */
148 #ifndef _SYS_SYSPROTO_H_
149 struct dup2_args {
150 	u_int	from;
151 	u_int	to;
152 };
153 #endif
154 /*
155  * MPSAFE
156  */
157 /* ARGSUSED */
158 int
159 dup2(td, uap)
160 	struct thread *td;
161 	struct dup2_args *uap;
162 {
163 
164 	return (do_dup(td, DUP_FIXED, (int)uap->from, (int)uap->to,
165 		    td->td_retval));
166 }
167 
168 /*
169  * Duplicate a file descriptor.
170  */
171 #ifndef _SYS_SYSPROTO_H_
172 struct dup_args {
173 	u_int	fd;
174 };
175 #endif
176 /*
177  * MPSAFE
178  */
179 /* ARGSUSED */
180 int
181 dup(td, uap)
182 	struct thread *td;
183 	struct dup_args *uap;
184 {
185 
186 	return (do_dup(td, DUP_VARIABLE, (int)uap->fd, 0, td->td_retval));
187 }
188 
189 /*
190  * The file control system call.
191  */
192 #ifndef _SYS_SYSPROTO_H_
193 struct fcntl_args {
194 	int	fd;
195 	int	cmd;
196 	long	arg;
197 };
198 #endif
199 /*
200  * MPSAFE
201  */
202 /* ARGSUSED */
203 int
204 fcntl(td, uap)
205 	struct thread *td;
206 	struct fcntl_args *uap;
207 {
208 	struct flock fl;
209 	intptr_t arg;
210 	int error;
211 
212 	error = 0;
213 	switch (uap->cmd) {
214 	case F_GETLK:
215 	case F_SETLK:
216 	case F_SETLKW:
217 		error = copyin((void *)(intptr_t)uap->arg, &fl, sizeof(fl));
218 		arg = (intptr_t)&fl;
219 		break;
220 	default:
221 		arg = uap->arg;
222 		break;
223 	}
224 	if (error)
225 		return (error);
226 	error = kern_fcntl(td, uap->fd, uap->cmd, arg);
227 	if (error)
228 		return (error);
229 	if (uap->cmd == F_GETLK)
230 		error = copyout(&fl, (void *)(intptr_t)uap->arg, sizeof(fl));
231 	return (error);
232 }
233 
234 int
235 kern_fcntl(struct thread *td, int fd, int cmd, intptr_t arg)
236 {
237 	struct filedesc *fdp;
238 	struct flock *flp;
239 	struct file *fp;
240 	struct proc *p;
241 	char *pop;
242 	struct vnode *vp;
243 	u_int newmin;
244 	int error, flg, tmp;
245 
246 	error = 0;
247 	flg = F_POSIX;
248 	p = td->td_proc;
249 	fdp = p->p_fd;
250 	mtx_lock(&Giant);
251 	FILEDESC_LOCK(fdp);
252 	if ((unsigned)fd >= fdp->fd_nfiles ||
253 	    (fp = fdp->fd_ofiles[fd]) == NULL) {
254 		FILEDESC_UNLOCK(fdp);
255 		error = EBADF;
256 		goto done2;
257 	}
258 	pop = &fdp->fd_ofileflags[fd];
259 
260 	switch (cmd) {
261 	case F_DUPFD:
262 		FILEDESC_UNLOCK(fdp);
263 		newmin = arg;
264 		if (newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
265 		    newmin >= maxfilesperproc) {
266 			error = EINVAL;
267 			break;
268 		}
269 		error = do_dup(td, DUP_VARIABLE, fd, newmin, td->td_retval);
270 		break;
271 
272 	case F_GETFD:
273 		td->td_retval[0] = (*pop & UF_EXCLOSE) ? FD_CLOEXEC : 0;
274 		FILEDESC_UNLOCK(fdp);
275 		break;
276 
277 	case F_SETFD:
278 		*pop = (*pop &~ UF_EXCLOSE) |
279 		    (arg & FD_CLOEXEC ? UF_EXCLOSE : 0);
280 		FILEDESC_UNLOCK(fdp);
281 		break;
282 
283 	case F_GETFL:
284 		FILE_LOCK(fp);
285 		FILEDESC_UNLOCK(fdp);
286 		td->td_retval[0] = OFLAGS(fp->f_flag);
287 		FILE_UNLOCK(fp);
288 		break;
289 
290 	case F_SETFL:
291 		FILE_LOCK(fp);
292 		FILEDESC_UNLOCK(fdp);
293 		fhold_locked(fp);
294 		fp->f_flag &= ~FCNTLFLAGS;
295 		fp->f_flag |= FFLAGS(arg & ~O_ACCMODE) & FCNTLFLAGS;
296 		FILE_UNLOCK(fp);
297 		tmp = fp->f_flag & FNONBLOCK;
298 		error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
299 		if (error) {
300 			fdrop(fp, td);
301 			break;
302 		}
303 		tmp = fp->f_flag & FASYNC;
304 		error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td);
305 		if (error == 0) {
306 			fdrop(fp, td);
307 			break;
308 		}
309 		FILE_LOCK(fp);
310 		fp->f_flag &= ~FNONBLOCK;
311 		FILE_UNLOCK(fp);
312 		tmp = 0;
313 		(void)fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
314 		fdrop(fp, td);
315 		break;
316 
317 	case F_GETOWN:
318 		fhold(fp);
319 		FILEDESC_UNLOCK(fdp);
320 		error = fo_ioctl(fp, FIOGETOWN, &tmp, td->td_ucred, td);
321 		if (error == 0)
322 			td->td_retval[0] = tmp;
323 		fdrop(fp, td);
324 		break;
325 
326 	case F_SETOWN:
327 		fhold(fp);
328 		FILEDESC_UNLOCK(fdp);
329 		tmp = arg;
330 		error = fo_ioctl(fp, FIOSETOWN, &tmp, td->td_ucred, td);
331 		fdrop(fp, td);
332 		break;
333 
334 	case F_SETLKW:
335 		flg |= F_WAIT;
336 		/* FALLTHROUGH F_SETLK */
337 
338 	case F_SETLK:
339 		if (fp->f_type != DTYPE_VNODE) {
340 			FILEDESC_UNLOCK(fdp);
341 			error = EBADF;
342 			break;
343 		}
344 
345 		flp = (struct flock *)arg;
346 		if (flp->l_whence == SEEK_CUR) {
347 			if (fp->f_offset < 0 ||
348 			    (flp->l_start > 0 &&
349 			     fp->f_offset > OFF_MAX - flp->l_start)) {
350 				FILEDESC_UNLOCK(fdp);
351 				error = EOVERFLOW;
352 				break;
353 			}
354 			flp->l_start += fp->f_offset;
355 		}
356 
357 		/*
358 		 * VOP_ADVLOCK() may block.
359 		 */
360 		fhold(fp);
361 		FILEDESC_UNLOCK(fdp);
362 		vp = fp->f_data;
363 
364 		switch (flp->l_type) {
365 		case F_RDLCK:
366 			if ((fp->f_flag & FREAD) == 0) {
367 				error = EBADF;
368 				break;
369 			}
370 			PROC_LOCK(p);
371 			p->p_flag |= P_ADVLOCK;
372 			PROC_UNLOCK(p);
373 			error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
374 			    flp, flg);
375 			break;
376 		case F_WRLCK:
377 			if ((fp->f_flag & FWRITE) == 0) {
378 				error = EBADF;
379 				break;
380 			}
381 			PROC_LOCK(p);
382 			p->p_flag |= P_ADVLOCK;
383 			PROC_UNLOCK(p);
384 			error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
385 			    flp, flg);
386 			break;
387 		case F_UNLCK:
388 			error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
389 			    flp, F_POSIX);
390 			break;
391 		default:
392 			error = EINVAL;
393 			break;
394 		}
395 		fdrop(fp, td);
396 		break;
397 
398 	case F_GETLK:
399 		if (fp->f_type != DTYPE_VNODE) {
400 			FILEDESC_UNLOCK(fdp);
401 			error = EBADF;
402 			break;
403 		}
404 		flp = (struct flock *)arg;
405 		if (flp->l_type != F_RDLCK && flp->l_type != F_WRLCK &&
406 		    flp->l_type != F_UNLCK) {
407 			FILEDESC_UNLOCK(fdp);
408 			error = EINVAL;
409 			break;
410 		}
411 		if (flp->l_whence == SEEK_CUR) {
412 			if ((flp->l_start > 0 &&
413 			    fp->f_offset > OFF_MAX - flp->l_start) ||
414 			    (flp->l_start < 0 &&
415 			     fp->f_offset < OFF_MIN - flp->l_start)) {
416 				FILEDESC_UNLOCK(fdp);
417 				error = EOVERFLOW;
418 				break;
419 			}
420 			flp->l_start += fp->f_offset;
421 		}
422 		/*
423 		 * VOP_ADVLOCK() may block.
424 		 */
425 		fhold(fp);
426 		FILEDESC_UNLOCK(fdp);
427 		vp = fp->f_data;
428 		error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK, flp,
429 		    F_POSIX);
430 		fdrop(fp, td);
431 		break;
432 	default:
433 		FILEDESC_UNLOCK(fdp);
434 		error = EINVAL;
435 		break;
436 	}
437 done2:
438 	mtx_unlock(&Giant);
439 	return (error);
440 }
441 
442 /*
443  * Common code for dup, dup2, and fcntl(F_DUPFD).
444  * filedesc must be locked, but will be unlocked as a side effect.
445  */
446 static int
447 do_dup(td, type, old, new, retval)
448 	enum dup_type type;
449 	int old, new;
450 	register_t *retval;
451 	struct thread *td;
452 {
453 	struct filedesc *fdp;
454 	struct proc *p;
455 	struct file *fp;
456 	struct file *delfp;
457 	int error, newfd;
458 
459 	p = td->td_proc;
460 	fdp = p->p_fd;
461 
462 	/*
463 	 * Verify we have a valid descriptor to dup from and possibly to
464 	 * dup to.
465 	 */
466 	if (old < 0 || new < 0 || new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
467 	    new >= maxfilesperproc)
468 		return (EBADF);
469 	FILEDESC_LOCK(fdp);
470 	if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL) {
471 		FILEDESC_UNLOCK(fdp);
472 		return (EBADF);
473 	}
474 	if (type == DUP_FIXED && old == new) {
475 		*retval = new;
476 		FILEDESC_UNLOCK(fdp);
477 		return (0);
478 	}
479 	fp = fdp->fd_ofiles[old];
480 	fhold(fp);
481 
482 	/*
483 	 * Expand the table for the new descriptor if needed.  This may
484 	 * block and drop and reacquire the filedesc lock.
485 	 */
486 	if (type == DUP_VARIABLE || new >= fdp->fd_nfiles) {
487 		error = fdalloc(td, new, &newfd);
488 		if (error) {
489 			FILEDESC_UNLOCK(fdp);
490 			fdrop(fp, td);
491 			return (error);
492 		}
493 	}
494 	if (type == DUP_VARIABLE)
495 		new = newfd;
496 
497 	/*
498 	 * If the old file changed out from under us then treat it as a
499 	 * bad file descriptor.  Userland should do its own locking to
500 	 * avoid this case.
501 	 */
502 	if (fdp->fd_ofiles[old] != fp) {
503 		if (fdp->fd_ofiles[new] == NULL) {
504 			if (new < fdp->fd_freefile)
505 				fdp->fd_freefile = new;
506 			while (fdp->fd_lastfile > 0 &&
507 			    fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
508 				fdp->fd_lastfile--;
509 		}
510 		FILEDESC_UNLOCK(fdp);
511 		fdrop(fp, td);
512 		return (EBADF);
513 	}
514 	KASSERT(old != new, ("new fd is same as old"));
515 
516 	/*
517 	 * Save info on the descriptor being overwritten.  We have
518 	 * to do the unmap now, but we cannot close it without
519 	 * introducing an ownership race for the slot.
520 	 */
521 	delfp = fdp->fd_ofiles[new];
522 	KASSERT(delfp == NULL || type == DUP_FIXED,
523 	    ("dup() picked an open file"));
524 #if 0
525 	if (delfp && (fdp->fd_ofileflags[new] & UF_MAPPED))
526 		(void) munmapfd(td, new);
527 #endif
528 
529 	/*
530 	 * Duplicate the source descriptor, update lastfile
531 	 */
532 	fdp->fd_ofiles[new] = fp;
533  	fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
534 	if (new > fdp->fd_lastfile)
535 		fdp->fd_lastfile = new;
536 	FILEDESC_UNLOCK(fdp);
537 	*retval = new;
538 
539 	/*
540 	 * If we dup'd over a valid file, we now own the reference to it
541 	 * and must dispose of it using closef() semantics (as if a
542 	 * close() were performed on it).
543 	 */
544 	if (delfp) {
545 		mtx_lock(&Giant);
546 		(void) closef(delfp, td);
547 		mtx_unlock(&Giant);
548 	}
549 	return (0);
550 }
551 
552 /*
553  * If sigio is on the list associated with a process or process group,
554  * disable signalling from the device, remove sigio from the list and
555  * free sigio.
556  */
557 void
558 funsetown(sigiop)
559 	struct sigio **sigiop;
560 {
561 	struct sigio *sigio;
562 
563 	SIGIO_LOCK();
564 	sigio = *sigiop;
565 	if (sigio == NULL) {
566 		SIGIO_UNLOCK();
567 		return;
568 	}
569 	*(sigio->sio_myref) = NULL;
570 	if ((sigio)->sio_pgid < 0) {
571 		struct pgrp *pg = (sigio)->sio_pgrp;
572 		PGRP_LOCK(pg);
573 		SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio,
574 			     sigio, sio_pgsigio);
575 		PGRP_UNLOCK(pg);
576 	} else {
577 		struct proc *p = (sigio)->sio_proc;
578 		PROC_LOCK(p);
579 		SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio,
580 			     sigio, sio_pgsigio);
581 		PROC_UNLOCK(p);
582 	}
583 	SIGIO_UNLOCK();
584 	crfree(sigio->sio_ucred);
585 	FREE(sigio, M_SIGIO);
586 }
587 
588 /*
589  * Free a list of sigio structures.
590  * We only need to lock the SIGIO_LOCK because we have made ourselves
591  * inaccessable to callers of fsetown and therefore do not need to lock
592  * the proc or pgrp struct for the list manipulation.
593  */
594 void
595 funsetownlst(sigiolst)
596 	struct sigiolst *sigiolst;
597 {
598 	struct proc *p;
599 	struct pgrp *pg;
600 	struct sigio *sigio;
601 
602 	sigio = SLIST_FIRST(sigiolst);
603 	if (sigio == NULL)
604 		return;
605 	p = NULL;
606 	pg = NULL;
607 
608 	/*
609 	 * Every entry of the list should belong
610 	 * to a single proc or pgrp.
611 	 */
612 	if (sigio->sio_pgid < 0) {
613 		pg = sigio->sio_pgrp;
614 		PGRP_LOCK_ASSERT(pg, MA_NOTOWNED);
615 	} else /* if (sigio->sio_pgid > 0) */ {
616 		p = sigio->sio_proc;
617 		PROC_LOCK_ASSERT(p, MA_NOTOWNED);
618 	}
619 
620 	SIGIO_LOCK();
621 	while ((sigio = SLIST_FIRST(sigiolst)) != NULL) {
622 		*(sigio->sio_myref) = NULL;
623 		if (pg != NULL) {
624 			KASSERT(sigio->sio_pgid < 0,
625 			    ("Proc sigio in pgrp sigio list"));
626 			KASSERT(sigio->sio_pgrp == pg,
627 			    ("Bogus pgrp in sigio list"));
628 			PGRP_LOCK(pg);
629 			SLIST_REMOVE(&pg->pg_sigiolst, sigio, sigio,
630 			    sio_pgsigio);
631 			PGRP_UNLOCK(pg);
632 		} else /* if (p != NULL) */ {
633 			KASSERT(sigio->sio_pgid > 0,
634 			    ("Pgrp sigio in proc sigio list"));
635 			KASSERT(sigio->sio_proc == p,
636 			    ("Bogus proc in sigio list"));
637 			PROC_LOCK(p);
638 			SLIST_REMOVE(&p->p_sigiolst, sigio, sigio,
639 			    sio_pgsigio);
640 			PROC_UNLOCK(p);
641 		}
642 		SIGIO_UNLOCK();
643 		crfree(sigio->sio_ucred);
644 		FREE(sigio, M_SIGIO);
645 		SIGIO_LOCK();
646 	}
647 	SIGIO_UNLOCK();
648 }
649 
650 /*
651  * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg).
652  *
653  * After permission checking, add a sigio structure to the sigio list for
654  * the process or process group.
655  */
656 int
657 fsetown(pgid, sigiop)
658 	pid_t pgid;
659 	struct sigio **sigiop;
660 {
661 	struct proc *proc;
662 	struct pgrp *pgrp;
663 	struct sigio *sigio;
664 	int ret;
665 
666 	if (pgid == 0) {
667 		funsetown(sigiop);
668 		return (0);
669 	}
670 
671 	ret = 0;
672 
673 	/* Allocate and fill in the new sigio out of locks. */
674 	MALLOC(sigio, struct sigio *, sizeof(struct sigio), M_SIGIO, 0);
675 	sigio->sio_pgid = pgid;
676 	sigio->sio_ucred = crhold(curthread->td_ucred);
677 	sigio->sio_myref = sigiop;
678 
679 	sx_slock(&proctree_lock);
680 	if (pgid > 0) {
681 		proc = pfind(pgid);
682 		if (proc == NULL) {
683 			ret = ESRCH;
684 			goto fail;
685 		}
686 
687 		/*
688 		 * Policy - Don't allow a process to FSETOWN a process
689 		 * in another session.
690 		 *
691 		 * Remove this test to allow maximum flexibility or
692 		 * restrict FSETOWN to the current process or process
693 		 * group for maximum safety.
694 		 */
695 		PROC_UNLOCK(proc);
696 		if (proc->p_session != curthread->td_proc->p_session) {
697 			ret = EPERM;
698 			goto fail;
699 		}
700 
701 		pgrp = NULL;
702 	} else /* if (pgid < 0) */ {
703 		pgrp = pgfind(-pgid);
704 		if (pgrp == NULL) {
705 			ret = ESRCH;
706 			goto fail;
707 		}
708 		PGRP_UNLOCK(pgrp);
709 
710 		/*
711 		 * Policy - Don't allow a process to FSETOWN a process
712 		 * in another session.
713 		 *
714 		 * Remove this test to allow maximum flexibility or
715 		 * restrict FSETOWN to the current process or process
716 		 * group for maximum safety.
717 		 */
718 		if (pgrp->pg_session != curthread->td_proc->p_session) {
719 			ret = EPERM;
720 			goto fail;
721 		}
722 
723 		proc = NULL;
724 	}
725 	funsetown(sigiop);
726 	if (pgid > 0) {
727 		PROC_LOCK(proc);
728 		/*
729 		 * Since funsetownlst() is called without the proctree
730 		 * locked, we need to check for P_WEXIT.
731 		 * XXX: is ESRCH correct?
732 		 */
733 		if ((proc->p_flag & P_WEXIT) != 0) {
734 			PROC_UNLOCK(proc);
735 			ret = ESRCH;
736 			goto fail;
737 		}
738 		SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio);
739 		sigio->sio_proc = proc;
740 		PROC_UNLOCK(proc);
741 	} else {
742 		PGRP_LOCK(pgrp);
743 		SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
744 		sigio->sio_pgrp = pgrp;
745 		PGRP_UNLOCK(pgrp);
746 	}
747 	sx_sunlock(&proctree_lock);
748 	SIGIO_LOCK();
749 	*sigiop = sigio;
750 	SIGIO_UNLOCK();
751 	return (0);
752 
753 fail:
754 	sx_sunlock(&proctree_lock);
755 	crfree(sigio->sio_ucred);
756 	FREE(sigio, M_SIGIO);
757 	return (ret);
758 }
759 
760 /*
761  * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
762  */
763 pid_t
764 fgetown(sigiop)
765 	struct sigio **sigiop;
766 {
767 	pid_t pgid;
768 
769 	SIGIO_LOCK();
770 	pgid = (*sigiop != NULL) ? (*sigiop)->sio_pgid : 0;
771 	SIGIO_UNLOCK();
772 	return (pgid);
773 }
774 
775 /*
776  * Close a file descriptor.
777  */
778 #ifndef _SYS_SYSPROTO_H_
779 struct close_args {
780         int     fd;
781 };
782 #endif
783 /*
784  * MPSAFE
785  */
786 /* ARGSUSED */
787 int
788 close(td, uap)
789 	struct thread *td;
790 	struct close_args *uap;
791 {
792 	struct filedesc *fdp;
793 	struct file *fp;
794 	int fd, error;
795 
796 	fd = uap->fd;
797 	error = 0;
798 	fdp = td->td_proc->p_fd;
799 	mtx_lock(&Giant);
800 	FILEDESC_LOCK(fdp);
801 	if ((unsigned)fd >= fdp->fd_nfiles ||
802 	    (fp = fdp->fd_ofiles[fd]) == NULL) {
803 		FILEDESC_UNLOCK(fdp);
804 		error = EBADF;
805 		goto done2;
806 	}
807 #if 0
808 	if (fdp->fd_ofileflags[fd] & UF_MAPPED)
809 		(void) munmapfd(td, fd);
810 #endif
811 	fdp->fd_ofiles[fd] = NULL;
812 	fdp->fd_ofileflags[fd] = 0;
813 
814 	/*
815 	 * we now hold the fp reference that used to be owned by the descriptor
816 	 * array.
817 	 */
818 	while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
819 		fdp->fd_lastfile--;
820 	if (fd < fdp->fd_freefile)
821 		fdp->fd_freefile = fd;
822 	if (fd < fdp->fd_knlistsize) {
823 		FILEDESC_UNLOCK(fdp);
824 		knote_fdclose(td, fd);
825 	} else
826 		FILEDESC_UNLOCK(fdp);
827 
828 	error = closef(fp, td);
829 done2:
830 	mtx_unlock(&Giant);
831 	return (error);
832 }
833 
834 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
835 /*
836  * Return status information about a file descriptor.
837  */
838 #ifndef _SYS_SYSPROTO_H_
839 struct ofstat_args {
840 	int	fd;
841 	struct	ostat *sb;
842 };
843 #endif
844 /*
845  * MPSAFE
846  */
847 /* ARGSUSED */
848 int
849 ofstat(td, uap)
850 	struct thread *td;
851 	struct ofstat_args *uap;
852 {
853 	struct file *fp;
854 	struct stat ub;
855 	struct ostat oub;
856 	int error;
857 
858 	mtx_lock(&Giant);
859 	if ((error = fget(td, uap->fd, &fp)) != 0)
860 		goto done2;
861 	error = fo_stat(fp, &ub, td->td_ucred, td);
862 	if (error == 0) {
863 		cvtstat(&ub, &oub);
864 		error = copyout(&oub, uap->sb, sizeof(oub));
865 	}
866 	fdrop(fp, td);
867 done2:
868 	mtx_unlock(&Giant);
869 	return (error);
870 }
871 #endif /* COMPAT_43 || COMPAT_SUNOS */
872 
873 /*
874  * Return status information about a file descriptor.
875  */
876 #ifndef _SYS_SYSPROTO_H_
877 struct fstat_args {
878 	int	fd;
879 	struct	stat *sb;
880 };
881 #endif
882 /*
883  * MPSAFE
884  */
885 /* ARGSUSED */
886 int
887 fstat(td, uap)
888 	struct thread *td;
889 	struct fstat_args *uap;
890 {
891 	struct file *fp;
892 	struct stat ub;
893 	int error;
894 
895 	mtx_lock(&Giant);
896 	if ((error = fget(td, uap->fd, &fp)) != 0)
897 		goto done2;
898 	error = fo_stat(fp, &ub, td->td_ucred, td);
899 	if (error == 0)
900 		error = copyout(&ub, uap->sb, sizeof(ub));
901 	fdrop(fp, td);
902 done2:
903 	mtx_unlock(&Giant);
904 	return (error);
905 }
906 
907 /*
908  * Return status information about a file descriptor.
909  */
910 #ifndef _SYS_SYSPROTO_H_
911 struct nfstat_args {
912 	int	fd;
913 	struct	nstat *sb;
914 };
915 #endif
916 /*
917  * MPSAFE
918  */
919 /* ARGSUSED */
920 int
921 nfstat(td, uap)
922 	struct thread *td;
923 	struct nfstat_args *uap;
924 {
925 	struct file *fp;
926 	struct stat ub;
927 	struct nstat nub;
928 	int error;
929 
930 	mtx_lock(&Giant);
931 	if ((error = fget(td, uap->fd, &fp)) != 0)
932 		goto done2;
933 	error = fo_stat(fp, &ub, td->td_ucred, td);
934 	if (error == 0) {
935 		cvtnstat(&ub, &nub);
936 		error = copyout(&nub, uap->sb, sizeof(nub));
937 	}
938 	fdrop(fp, td);
939 done2:
940 	mtx_unlock(&Giant);
941 	return (error);
942 }
943 
944 /*
945  * Return pathconf information about a file descriptor.
946  */
947 #ifndef _SYS_SYSPROTO_H_
948 struct fpathconf_args {
949 	int	fd;
950 	int	name;
951 };
952 #endif
953 /*
954  * MPSAFE
955  */
956 /* ARGSUSED */
957 int
958 fpathconf(td, uap)
959 	struct thread *td;
960 	struct fpathconf_args *uap;
961 {
962 	struct file *fp;
963 	struct vnode *vp;
964 	int error;
965 
966 	if ((error = fget(td, uap->fd, &fp)) != 0)
967 		return (error);
968 
969 	/* If asynchronous I/O is available, it works for all descriptors. */
970 	if (uap->name == _PC_ASYNC_IO) {
971 		td->td_retval[0] = async_io_version;
972 		goto out;
973 	}
974 	switch (fp->f_type) {
975 	case DTYPE_PIPE:
976 	case DTYPE_SOCKET:
977 		if (uap->name != _PC_PIPE_BUF) {
978 			error = EINVAL;
979 		} else {
980 			td->td_retval[0] = PIPE_BUF;
981 			error = 0;
982 		}
983 		break;
984 	case DTYPE_FIFO:
985 	case DTYPE_VNODE:
986 		vp = fp->f_data;
987 		mtx_lock(&Giant);
988 		error = VOP_PATHCONF(vp, uap->name, td->td_retval);
989 		mtx_unlock(&Giant);
990 		break;
991 	default:
992 		error = EOPNOTSUPP;
993 		break;
994 	}
995 out:
996 	fdrop(fp, td);
997 	return (error);
998 }
999 
1000 /*
1001  * Allocate a file descriptor for the process.
1002  */
1003 static int fdexpand;
1004 SYSCTL_INT(_debug, OID_AUTO, fdexpand, CTLFLAG_RD, &fdexpand, 0, "");
1005 
1006 int
1007 fdalloc(td, want, result)
1008 	struct thread *td;
1009 	int want;
1010 	int *result;
1011 {
1012 	struct proc *p = td->td_proc;
1013 	struct filedesc *fdp = td->td_proc->p_fd;
1014 	int i;
1015 	int lim, last, nfiles;
1016 	struct file **newofile, **oldofile;
1017 	char *newofileflags;
1018 
1019 	FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
1020 
1021 	/*
1022 	 * Search for a free descriptor starting at the higher
1023 	 * of want or fd_freefile.  If that fails, consider
1024 	 * expanding the ofile array.
1025 	 */
1026 	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
1027 	for (;;) {
1028 		last = min(fdp->fd_nfiles, lim);
1029 		i = max(want, fdp->fd_freefile);
1030 		for (; i < last; i++) {
1031 			if (fdp->fd_ofiles[i] == NULL) {
1032 				fdp->fd_ofileflags[i] = 0;
1033 				if (i > fdp->fd_lastfile)
1034 					fdp->fd_lastfile = i;
1035 				if (want <= fdp->fd_freefile)
1036 					fdp->fd_freefile = i;
1037 				*result = i;
1038 				return (0);
1039 			}
1040 		}
1041 
1042 		/*
1043 		 * No space in current array.  Expand?
1044 		 */
1045 		if (i >= lim)
1046 			return (EMFILE);
1047 		if (fdp->fd_nfiles < NDEXTENT)
1048 			nfiles = NDEXTENT;
1049 		else
1050 			nfiles = 2 * fdp->fd_nfiles;
1051 		while (nfiles < want)
1052 			nfiles <<= 1;
1053 		FILEDESC_UNLOCK(fdp);
1054 		/*
1055 		 * XXX malloc() calls uma_large_malloc() for sizes larger
1056 		 * than KMEM_ZMAX bytes. uma_large_malloc() requires Giant.
1057 		 */
1058 		mtx_lock(&Giant);
1059 		newofile = malloc(nfiles * OFILESIZE, M_FILEDESC, 0);
1060 		mtx_unlock(&Giant);
1061 
1062 		/*
1063 		 * Deal with file-table extend race that might have
1064 		 * occurred while filedesc was unlocked.
1065 		 */
1066 		FILEDESC_LOCK(fdp);
1067 		if (fdp->fd_nfiles >= nfiles) {
1068 			/* XXX uma_large_free() needs Giant. */
1069 			FILEDESC_UNLOCK(fdp);
1070 			mtx_lock(&Giant);
1071 			free(newofile, M_FILEDESC);
1072 			mtx_unlock(&Giant);
1073 			FILEDESC_LOCK(fdp);
1074 			continue;
1075 		}
1076 		newofileflags = (char *) &newofile[nfiles];
1077 		/*
1078 		 * Copy the existing ofile and ofileflags arrays
1079 		 * and zero the new portion of each array.
1080 		 */
1081 		i = fdp->fd_nfiles * sizeof(struct file *);
1082 		bcopy(fdp->fd_ofiles, newofile,	i);
1083 		bzero((char *)newofile + i,
1084 		    nfiles * sizeof(struct file *) - i);
1085 		i = fdp->fd_nfiles * sizeof(char);
1086 		bcopy(fdp->fd_ofileflags, newofileflags, i);
1087 		bzero(newofileflags + i, nfiles * sizeof(char) - i);
1088 		if (fdp->fd_nfiles > NDFILE)
1089 			oldofile = fdp->fd_ofiles;
1090 		else
1091 			oldofile = NULL;
1092 		fdp->fd_ofiles = newofile;
1093 		fdp->fd_ofileflags = newofileflags;
1094 		fdp->fd_nfiles = nfiles;
1095 		fdexpand++;
1096 		if (oldofile != NULL) {
1097 			/* XXX uma_large_free() needs Giant. */
1098 			FILEDESC_UNLOCK(fdp);
1099 			mtx_lock(&Giant);
1100 			free(oldofile, M_FILEDESC);
1101 			mtx_unlock(&Giant);
1102 			FILEDESC_LOCK(fdp);
1103 		}
1104 	}
1105 	return (0);
1106 }
1107 
1108 /*
1109  * Check to see whether n user file descriptors
1110  * are available to the process p.
1111  */
1112 int
1113 fdavail(td, n)
1114 	struct thread *td;
1115 	int n;
1116 {
1117 	struct proc *p = td->td_proc;
1118 	struct filedesc *fdp = td->td_proc->p_fd;
1119 	struct file **fpp;
1120 	int i, lim, last;
1121 
1122 	FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
1123 
1124 	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
1125 	if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
1126 		return (1);
1127 	last = min(fdp->fd_nfiles, lim);
1128 	fpp = &fdp->fd_ofiles[fdp->fd_freefile];
1129 	for (i = last - fdp->fd_freefile; --i >= 0; fpp++) {
1130 		if (*fpp == NULL && --n <= 0)
1131 			return (1);
1132 	}
1133 	return (0);
1134 }
1135 
1136 /*
1137  * Create a new open file structure and allocate
1138  * a file decriptor for the process that refers to it.
1139  */
1140 int
1141 falloc(td, resultfp, resultfd)
1142 	struct thread *td;
1143 	struct file **resultfp;
1144 	int *resultfd;
1145 {
1146 	struct proc *p = td->td_proc;
1147 	struct file *fp, *fq;
1148 	int error, i;
1149 
1150 	fp = uma_zalloc(file_zone, M_ZERO);
1151 	sx_xlock(&filelist_lock);
1152 	if (nfiles >= maxfiles) {
1153 		sx_xunlock(&filelist_lock);
1154 		uma_zfree(file_zone, fp);
1155 		tablefull("file");
1156 		return (ENFILE);
1157 	}
1158 	nfiles++;
1159 
1160 	/*
1161 	 * If the process has file descriptor zero open, add the new file
1162 	 * descriptor to the list of open files at that point, otherwise
1163 	 * put it at the front of the list of open files.
1164 	 */
1165 	fp->f_mtxp = mtx_pool_alloc();
1166 	fp->f_gcflag = 0;
1167 	fp->f_count = 1;
1168 	fp->f_cred = crhold(td->td_ucred);
1169 	fp->f_ops = &badfileops;
1170 	fp->f_seqcount = 1;
1171 	FILEDESC_LOCK(p->p_fd);
1172 	if ((fq = p->p_fd->fd_ofiles[0])) {
1173 		LIST_INSERT_AFTER(fq, fp, f_list);
1174 	} else {
1175 		LIST_INSERT_HEAD(&filehead, fp, f_list);
1176 	}
1177 	sx_xunlock(&filelist_lock);
1178 	if ((error = fdalloc(td, 0, &i))) {
1179 		FILEDESC_UNLOCK(p->p_fd);
1180 		fdrop(fp, td);
1181 		return (error);
1182 	}
1183 	p->p_fd->fd_ofiles[i] = fp;
1184 	FILEDESC_UNLOCK(p->p_fd);
1185 	if (resultfp)
1186 		*resultfp = fp;
1187 	if (resultfd)
1188 		*resultfd = i;
1189 	return (0);
1190 }
1191 
1192 /*
1193  * Free a file descriptor.
1194  */
1195 void
1196 ffree(fp)
1197 	struct file *fp;
1198 {
1199 
1200 	KASSERT(fp->f_count == 0, ("ffree: fp_fcount not 0!"));
1201 	sx_xlock(&filelist_lock);
1202 	LIST_REMOVE(fp, f_list);
1203 	nfiles--;
1204 	sx_xunlock(&filelist_lock);
1205 	crfree(fp->f_cred);
1206 	uma_zfree(file_zone, fp);
1207 }
1208 
1209 /*
1210  * Build a new filedesc structure from another.
1211  * Copy the current, root, and jail root vnode references.
1212  */
1213 struct filedesc *
1214 fdinit(fdp)
1215 	struct filedesc *fdp;
1216 {
1217 	struct filedesc0 *newfdp;
1218 
1219 	MALLOC(newfdp, struct filedesc0 *, sizeof(struct filedesc0),
1220 	    M_FILEDESC, M_ZERO);
1221 	mtx_init(&newfdp->fd_fd.fd_mtx, FILEDESC_LOCK_DESC, NULL, MTX_DEF);
1222 	newfdp->fd_fd.fd_cdir = fdp->fd_cdir;
1223 	if (newfdp->fd_fd.fd_cdir)
1224 		VREF(newfdp->fd_fd.fd_cdir);
1225 	newfdp->fd_fd.fd_rdir = fdp->fd_rdir;
1226 	if (newfdp->fd_fd.fd_rdir)
1227 		VREF(newfdp->fd_fd.fd_rdir);
1228 	newfdp->fd_fd.fd_jdir = fdp->fd_jdir;
1229 	if (newfdp->fd_fd.fd_jdir)
1230 		VREF(newfdp->fd_fd.fd_jdir);
1231 
1232 	/* Create the file descriptor table. */
1233 	newfdp->fd_fd.fd_refcnt = 1;
1234 	newfdp->fd_fd.fd_cmask = cmask;
1235 	newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
1236 	newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
1237 	newfdp->fd_fd.fd_nfiles = NDFILE;
1238 	newfdp->fd_fd.fd_knlistsize = -1;
1239 	return (&newfdp->fd_fd);
1240 }
1241 
1242 /*
1243  * Share a filedesc structure.
1244  */
1245 struct filedesc *
1246 fdshare(fdp)
1247 	struct filedesc *fdp;
1248 {
1249 	FILEDESC_LOCK(fdp);
1250 	fdp->fd_refcnt++;
1251 	FILEDESC_UNLOCK(fdp);
1252 	return (fdp);
1253 }
1254 
1255 /*
1256  * Copy a filedesc structure.
1257  * A NULL pointer in returns a NULL reference, this is to ease callers,
1258  * not catch errors.
1259  */
1260 struct filedesc *
1261 fdcopy(fdp)
1262 	struct filedesc *fdp;
1263 {
1264 	struct filedesc *newfdp;
1265 	struct file **fpp;
1266 	int i, j;
1267 
1268 	/* Certain daemons might not have file descriptors. */
1269 	if (fdp == NULL)
1270 		return (NULL);
1271 
1272 	FILEDESC_LOCK_ASSERT(fdp, MA_OWNED);
1273 
1274 	FILEDESC_UNLOCK(fdp);
1275 	MALLOC(newfdp, struct filedesc *, sizeof(struct filedesc0),
1276 	    M_FILEDESC, 0);
1277 	FILEDESC_LOCK(fdp);
1278 	bcopy(fdp, newfdp, sizeof(struct filedesc));
1279 	FILEDESC_UNLOCK(fdp);
1280 	bzero(&newfdp->fd_mtx, sizeof(newfdp->fd_mtx));
1281 	mtx_init(&newfdp->fd_mtx, FILEDESC_LOCK_DESC, NULL, MTX_DEF);
1282 	if (newfdp->fd_cdir)
1283 		VREF(newfdp->fd_cdir);
1284 	if (newfdp->fd_rdir)
1285 		VREF(newfdp->fd_rdir);
1286 	if (newfdp->fd_jdir)
1287 		VREF(newfdp->fd_jdir);
1288 	newfdp->fd_refcnt = 1;
1289 
1290 	/*
1291 	 * If the number of open files fits in the internal arrays
1292 	 * of the open file structure, use them, otherwise allocate
1293 	 * additional memory for the number of descriptors currently
1294 	 * in use.
1295 	 */
1296 	FILEDESC_LOCK(fdp);
1297 	newfdp->fd_lastfile = fdp->fd_lastfile;
1298 	newfdp->fd_nfiles = fdp->fd_nfiles;
1299 	if (newfdp->fd_lastfile < NDFILE) {
1300 		newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
1301 		newfdp->fd_ofileflags =
1302 		    ((struct filedesc0 *) newfdp)->fd_dfileflags;
1303 		i = NDFILE;
1304 	} else {
1305 		/*
1306 		 * Compute the smallest multiple of NDEXTENT needed
1307 		 * for the file descriptors currently in use,
1308 		 * allowing the table to shrink.
1309 		 */
1310 retry:
1311 		i = newfdp->fd_nfiles;
1312 		while (i > 2 * NDEXTENT && i > newfdp->fd_lastfile * 2)
1313 			i /= 2;
1314 		FILEDESC_UNLOCK(fdp);
1315 		MALLOC(newfdp->fd_ofiles, struct file **, i * OFILESIZE,
1316 		    M_FILEDESC, 0);
1317 		FILEDESC_LOCK(fdp);
1318 		newfdp->fd_lastfile = fdp->fd_lastfile;
1319 		newfdp->fd_nfiles = fdp->fd_nfiles;
1320 		j = newfdp->fd_nfiles;
1321 		while (j > 2 * NDEXTENT && j > newfdp->fd_lastfile * 2)
1322 			j /= 2;
1323 		if (i != j) {
1324 			/*
1325 			 * The size of the original table has changed.
1326 			 * Go over once again.
1327 			 */
1328 			FILEDESC_UNLOCK(fdp);
1329 			FREE(newfdp->fd_ofiles, M_FILEDESC);
1330 			FILEDESC_LOCK(fdp);
1331 			newfdp->fd_lastfile = fdp->fd_lastfile;
1332 			newfdp->fd_nfiles = fdp->fd_nfiles;
1333 			goto retry;
1334 		}
1335 		newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
1336 	}
1337 	newfdp->fd_nfiles = i;
1338 	bcopy(fdp->fd_ofiles, newfdp->fd_ofiles, i * sizeof(struct file **));
1339 	bcopy(fdp->fd_ofileflags, newfdp->fd_ofileflags, i * sizeof(char));
1340 
1341 	/*
1342 	 * kq descriptors cannot be copied.
1343 	 */
1344 	if (newfdp->fd_knlistsize != -1) {
1345 		fpp = &newfdp->fd_ofiles[newfdp->fd_lastfile];
1346 		for (i = newfdp->fd_lastfile; i >= 0; i--, fpp--) {
1347 			if (*fpp != NULL && (*fpp)->f_type == DTYPE_KQUEUE) {
1348 				*fpp = NULL;
1349 				if (i < newfdp->fd_freefile)
1350 					newfdp->fd_freefile = i;
1351 			}
1352 			if (*fpp == NULL && i == newfdp->fd_lastfile && i > 0)
1353 				newfdp->fd_lastfile--;
1354 		}
1355 		newfdp->fd_knlist = NULL;
1356 		newfdp->fd_knlistsize = -1;
1357 		newfdp->fd_knhash = NULL;
1358 		newfdp->fd_knhashmask = 0;
1359 	}
1360 
1361 	fpp = newfdp->fd_ofiles;
1362 	for (i = newfdp->fd_lastfile; i-- >= 0; fpp++) {
1363 		if (*fpp != NULL)
1364 			fhold(*fpp);
1365 	}
1366 	return (newfdp);
1367 }
1368 
1369 /*
1370  * Release a filedesc structure.
1371  */
1372 void
1373 fdfree(td)
1374 	struct thread *td;
1375 {
1376 	struct filedesc *fdp;
1377 	struct file **fpp;
1378 	int i;
1379 
1380 	/* Certain daemons might not have file descriptors. */
1381 	fdp = td->td_proc->p_fd;
1382 	if (fdp == NULL)
1383 		return;
1384 
1385 	FILEDESC_LOCK(fdp);
1386 	if (--fdp->fd_refcnt > 0) {
1387 		FILEDESC_UNLOCK(fdp);
1388 		return;
1389 	}
1390 
1391 	/*
1392 	 * We are the last reference to the structure, so we can
1393 	 * safely assume it will not change out from under us.
1394 	 */
1395 	FILEDESC_UNLOCK(fdp);
1396 	fpp = fdp->fd_ofiles;
1397 	for (i = fdp->fd_lastfile; i-- >= 0; fpp++) {
1398 		if (*fpp)
1399 			(void) closef(*fpp, td);
1400 	}
1401 	td->td_proc->p_fd = NULL;
1402 	if (fdp->fd_nfiles > NDFILE)
1403 		FREE(fdp->fd_ofiles, M_FILEDESC);
1404 	if (fdp->fd_cdir)
1405 		vrele(fdp->fd_cdir);
1406 	if (fdp->fd_rdir)
1407 		vrele(fdp->fd_rdir);
1408 	if (fdp->fd_jdir)
1409 		vrele(fdp->fd_jdir);
1410 	if (fdp->fd_knlist)
1411 		FREE(fdp->fd_knlist, M_KQUEUE);
1412 	if (fdp->fd_knhash)
1413 		FREE(fdp->fd_knhash, M_KQUEUE);
1414 	mtx_destroy(&fdp->fd_mtx);
1415 	FREE(fdp, M_FILEDESC);
1416 }
1417 
1418 /*
1419  * For setugid programs, we don't want to people to use that setugidness
1420  * to generate error messages which write to a file which otherwise would
1421  * otherwise be off-limits to the process.  We check for filesystems where
1422  * the vnode can change out from under us after execve (like [lin]procfs).
1423  *
1424  * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
1425  * sufficient.  We also don't for check setugidness since we know we are.
1426  */
1427 static int
1428 is_unsafe(struct file *fp)
1429 {
1430 	if (fp->f_type == DTYPE_VNODE) {
1431 		struct vnode *vp = fp->f_data;
1432 
1433 		if ((vp->v_vflag & VV_PROCDEP) != 0)
1434 			return (1);
1435 	}
1436 	return (0);
1437 }
1438 
1439 /*
1440  * Make this setguid thing safe, if at all possible.
1441  */
1442 void
1443 setugidsafety(td)
1444 	struct thread *td;
1445 {
1446 	struct filedesc *fdp;
1447 	int i;
1448 
1449 	/* Certain daemons might not have file descriptors. */
1450 	fdp = td->td_proc->p_fd;
1451 	if (fdp == NULL)
1452 		return;
1453 
1454 	/*
1455 	 * Note: fdp->fd_ofiles may be reallocated out from under us while
1456 	 * we are blocked in a close.  Be careful!
1457 	 */
1458 	FILEDESC_LOCK(fdp);
1459 	for (i = 0; i <= fdp->fd_lastfile; i++) {
1460 		if (i > 2)
1461 			break;
1462 		if (fdp->fd_ofiles[i] && is_unsafe(fdp->fd_ofiles[i])) {
1463 			struct file *fp;
1464 
1465 #if 0
1466 			if ((fdp->fd_ofileflags[i] & UF_MAPPED) != 0)
1467 				(void) munmapfd(td, i);
1468 #endif
1469 			if (i < fdp->fd_knlistsize) {
1470 				FILEDESC_UNLOCK(fdp);
1471 				knote_fdclose(td, i);
1472 				FILEDESC_LOCK(fdp);
1473 			}
1474 			/*
1475 			 * NULL-out descriptor prior to close to avoid
1476 			 * a race while close blocks.
1477 			 */
1478 			fp = fdp->fd_ofiles[i];
1479 			fdp->fd_ofiles[i] = NULL;
1480 			fdp->fd_ofileflags[i] = 0;
1481 			if (i < fdp->fd_freefile)
1482 				fdp->fd_freefile = i;
1483 			FILEDESC_UNLOCK(fdp);
1484 			(void) closef(fp, td);
1485 			FILEDESC_LOCK(fdp);
1486 		}
1487 	}
1488 	while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
1489 		fdp->fd_lastfile--;
1490 	FILEDESC_UNLOCK(fdp);
1491 }
1492 
1493 /*
1494  * Close any files on exec?
1495  */
1496 void
1497 fdcloseexec(td)
1498 	struct thread *td;
1499 {
1500 	struct filedesc *fdp;
1501 	int i;
1502 
1503 	/* Certain daemons might not have file descriptors. */
1504 	fdp = td->td_proc->p_fd;
1505 	if (fdp == NULL)
1506 		return;
1507 
1508 	FILEDESC_LOCK(fdp);
1509 
1510 	/*
1511 	 * We cannot cache fd_ofiles or fd_ofileflags since operations
1512 	 * may block and rip them out from under us.
1513 	 */
1514 	for (i = 0; i <= fdp->fd_lastfile; i++) {
1515 		if (fdp->fd_ofiles[i] != NULL &&
1516 		    (fdp->fd_ofileflags[i] & UF_EXCLOSE)) {
1517 			struct file *fp;
1518 
1519 #if 0
1520 			if (fdp->fd_ofileflags[i] & UF_MAPPED)
1521 				(void) munmapfd(td, i);
1522 #endif
1523 			if (i < fdp->fd_knlistsize) {
1524 				FILEDESC_UNLOCK(fdp);
1525 				knote_fdclose(td, i);
1526 				FILEDESC_LOCK(fdp);
1527 			}
1528 			/*
1529 			 * NULL-out descriptor prior to close to avoid
1530 			 * a race while close blocks.
1531 			 */
1532 			fp = fdp->fd_ofiles[i];
1533 			fdp->fd_ofiles[i] = NULL;
1534 			fdp->fd_ofileflags[i] = 0;
1535 			if (i < fdp->fd_freefile)
1536 				fdp->fd_freefile = i;
1537 			FILEDESC_UNLOCK(fdp);
1538 			(void) closef(fp, td);
1539 			FILEDESC_LOCK(fdp);
1540 		}
1541 	}
1542 	while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
1543 		fdp->fd_lastfile--;
1544 	FILEDESC_UNLOCK(fdp);
1545 }
1546 
1547 /*
1548  * It is unsafe for set[ug]id processes to be started with file
1549  * descriptors 0..2 closed, as these descriptors are given implicit
1550  * significance in the Standard C library.  fdcheckstd() will create a
1551  * descriptor referencing /dev/null for each of stdin, stdout, and
1552  * stderr that is not already open.
1553  */
1554 int
1555 fdcheckstd(td)
1556 	struct thread *td;
1557 {
1558 	struct nameidata nd;
1559 	struct filedesc *fdp;
1560 	struct file *fp;
1561 	register_t retval;
1562 	int fd, i, error, flags, devnull;
1563 
1564 	fdp = td->td_proc->p_fd;
1565 	if (fdp == NULL)
1566 		return (0);
1567 	devnull = -1;
1568 	error = 0;
1569 	for (i = 0; i < 3; i++) {
1570 		if (fdp->fd_ofiles[i] != NULL)
1571 			continue;
1572 		if (devnull < 0) {
1573 			error = falloc(td, &fp, &fd);
1574 			if (error != 0)
1575 				break;
1576 			KASSERT(fd == i, ("oof, we didn't get our fd"));
1577 			NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/dev/null",
1578 			    td);
1579 			flags = FREAD | FWRITE;
1580 			error = vn_open(&nd, &flags, 0);
1581 			if (error != 0) {
1582 				FILEDESC_LOCK(fdp);
1583 				fdp->fd_ofiles[fd] = NULL;
1584 				FILEDESC_UNLOCK(fdp);
1585 				fdrop(fp, td);
1586 				break;
1587 			}
1588 			NDFREE(&nd, NDF_ONLY_PNBUF);
1589 			fp->f_data = nd.ni_vp;
1590 			fp->f_flag = flags;
1591 			fp->f_ops = &vnops;
1592 			fp->f_type = DTYPE_VNODE;
1593 			VOP_UNLOCK(nd.ni_vp, 0, td);
1594 			devnull = fd;
1595 		} else {
1596 			error = do_dup(td, DUP_FIXED, devnull, i, &retval);
1597 			if (error != 0)
1598 				break;
1599 		}
1600 	}
1601 	return (error);
1602 }
1603 
1604 /*
1605  * Internal form of close.
1606  * Decrement reference count on file structure.
1607  * Note: td may be NULL when closing a file
1608  * that was being passed in a message.
1609  */
1610 int
1611 closef(fp, td)
1612 	struct file *fp;
1613 	struct thread *td;
1614 {
1615 	struct vnode *vp;
1616 	struct flock lf;
1617 
1618 	if (fp == NULL)
1619 		return (0);
1620 	/*
1621 	 * POSIX record locking dictates that any close releases ALL
1622 	 * locks owned by this process.  This is handled by setting
1623 	 * a flag in the unlock to free ONLY locks obeying POSIX
1624 	 * semantics, and not to free BSD-style file locks.
1625 	 * If the descriptor was in a message, POSIX-style locks
1626 	 * aren't passed with the descriptor.
1627 	 */
1628 	if (td && (td->td_proc->p_flag & P_ADVLOCK) &&
1629 	    fp->f_type == DTYPE_VNODE) {
1630 		lf.l_whence = SEEK_SET;
1631 		lf.l_start = 0;
1632 		lf.l_len = 0;
1633 		lf.l_type = F_UNLCK;
1634 		vp = fp->f_data;
1635 		(void) VOP_ADVLOCK(vp, (caddr_t)td->td_proc->p_leader,
1636 		    F_UNLCK, &lf, F_POSIX);
1637 	}
1638 	return (fdrop(fp, td));
1639 }
1640 
1641 /*
1642  * Drop reference on struct file passed in, may call closef if the
1643  * reference hits zero.
1644  */
1645 int
1646 fdrop(fp, td)
1647 	struct file *fp;
1648 	struct thread *td;
1649 {
1650 
1651 	FILE_LOCK(fp);
1652 	return (fdrop_locked(fp, td));
1653 }
1654 
1655 /*
1656  * Extract the file pointer associated with the specified descriptor for
1657  * the current user process.
1658  *
1659  * If the descriptor doesn't exist, EBADF is returned.
1660  *
1661  * If the descriptor exists but doesn't match 'flags' then
1662  * return EBADF for read attempts and EINVAL for write attempts.
1663  *
1664  * If 'hold' is set (non-zero) the file's refcount will be bumped on return.
1665  * It should be droped with fdrop().
1666  * If it is not set, then the refcount will not be bumped however the
1667  * thread's filedesc struct will be returned locked (for fgetsock).
1668  *
1669  * If an error occured the non-zero error is returned and *fpp is set to NULL.
1670  * Otherwise *fpp is set and zero is returned.
1671  */
1672 static __inline int
1673 _fget(struct thread *td, int fd, struct file **fpp, int flags, int hold)
1674 {
1675 	struct filedesc *fdp;
1676 	struct file *fp;
1677 
1678 	*fpp = NULL;
1679 	if (td == NULL || (fdp = td->td_proc->p_fd) == NULL)
1680 		return (EBADF);
1681 	FILEDESC_LOCK(fdp);
1682 	if ((fp = fget_locked(fdp, fd)) == NULL || fp->f_ops == &badfileops) {
1683 		FILEDESC_UNLOCK(fdp);
1684 		return (EBADF);
1685 	}
1686 
1687 	/*
1688 	 * Note: FREAD failures returns EBADF to maintain backwards
1689 	 * compatibility with what routines returned before.
1690 	 *
1691 	 * Only one flag, or 0, may be specified.
1692 	 */
1693 	if (flags == FREAD && (fp->f_flag & FREAD) == 0) {
1694 		FILEDESC_UNLOCK(fdp);
1695 		return (EBADF);
1696 	}
1697 	if (flags == FWRITE && (fp->f_flag & FWRITE) == 0) {
1698 		FILEDESC_UNLOCK(fdp);
1699 		return (EINVAL);
1700 	}
1701 	if (hold) {
1702 		fhold(fp);
1703 		FILEDESC_UNLOCK(fdp);
1704 	}
1705 	*fpp = fp;
1706 	return (0);
1707 }
1708 
1709 int
1710 fget(struct thread *td, int fd, struct file **fpp)
1711 {
1712 
1713 	return(_fget(td, fd, fpp, 0, 1));
1714 }
1715 
1716 int
1717 fget_read(struct thread *td, int fd, struct file **fpp)
1718 {
1719 
1720 	return(_fget(td, fd, fpp, FREAD, 1));
1721 }
1722 
1723 int
1724 fget_write(struct thread *td, int fd, struct file **fpp)
1725 {
1726 
1727 	return(_fget(td, fd, fpp, FWRITE, 1));
1728 }
1729 
1730 /*
1731  * Like fget() but loads the underlying vnode, or returns an error if
1732  * the descriptor does not represent a vnode.  Note that pipes use vnodes
1733  * but never have VM objects (so VOP_GETVOBJECT() calls will return an
1734  * error).  The returned vnode will be vref()d.
1735  */
1736 static __inline int
1737 _fgetvp(struct thread *td, int fd, struct vnode **vpp, int flags)
1738 {
1739 	struct file *fp;
1740 	int error;
1741 
1742 	*vpp = NULL;
1743 	if ((error = _fget(td, fd, &fp, 0, 0)) != 0)
1744 		return (error);
1745 	if (fp->f_type != DTYPE_VNODE && fp->f_type != DTYPE_FIFO) {
1746 		error = EINVAL;
1747 	} else {
1748 		*vpp = fp->f_data;
1749 		vref(*vpp);
1750 	}
1751 	FILEDESC_UNLOCK(td->td_proc->p_fd);
1752 	return (error);
1753 }
1754 
1755 int
1756 fgetvp(struct thread *td, int fd, struct vnode **vpp)
1757 {
1758 
1759 	return (_fgetvp(td, fd, vpp, 0));
1760 }
1761 
1762 int
1763 fgetvp_read(struct thread *td, int fd, struct vnode **vpp)
1764 {
1765 
1766 	return (_fgetvp(td, fd, vpp, FREAD));
1767 }
1768 
1769 int
1770 fgetvp_write(struct thread *td, int fd, struct vnode **vpp)
1771 {
1772 
1773 	return (_fgetvp(td, fd, vpp, FWRITE));
1774 }
1775 
1776 /*
1777  * Like fget() but loads the underlying socket, or returns an error if
1778  * the descriptor does not represent a socket.
1779  *
1780  * We bump the ref count on the returned socket.  XXX Also obtain the SX
1781  * lock in the future.
1782  */
1783 int
1784 fgetsock(struct thread *td, int fd, struct socket **spp, u_int *fflagp)
1785 {
1786 	struct file *fp;
1787 	int error;
1788 
1789 	*spp = NULL;
1790 	if (fflagp != NULL)
1791 		*fflagp = 0;
1792 	if ((error = _fget(td, fd, &fp, 0, 0)) != 0)
1793 		return (error);
1794 	if (fp->f_type != DTYPE_SOCKET) {
1795 		error = ENOTSOCK;
1796 	} else {
1797 		*spp = fp->f_data;
1798 		if (fflagp)
1799 			*fflagp = fp->f_flag;
1800 		soref(*spp);
1801 	}
1802 	FILEDESC_UNLOCK(td->td_proc->p_fd);
1803 	return (error);
1804 }
1805 
1806 /*
1807  * Drop the reference count on the the socket and XXX release the SX lock in
1808  * the future.  The last reference closes the socket.
1809  */
1810 void
1811 fputsock(struct socket *so)
1812 {
1813 
1814 	sorele(so);
1815 }
1816 
1817 /*
1818  * Drop reference on struct file passed in, may call closef if the
1819  * reference hits zero.
1820  * Expects struct file locked, and will unlock it.
1821  */
1822 int
1823 fdrop_locked(fp, td)
1824 	struct file *fp;
1825 	struct thread *td;
1826 {
1827 	struct flock lf;
1828 	struct vnode *vp;
1829 	int error;
1830 
1831 	FILE_LOCK_ASSERT(fp, MA_OWNED);
1832 
1833 	if (--fp->f_count > 0) {
1834 		FILE_UNLOCK(fp);
1835 		return (0);
1836 	}
1837 	mtx_lock(&Giant);
1838 	if (fp->f_count < 0)
1839 		panic("fdrop: count < 0");
1840 	if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
1841 		lf.l_whence = SEEK_SET;
1842 		lf.l_start = 0;
1843 		lf.l_len = 0;
1844 		lf.l_type = F_UNLCK;
1845 		vp = fp->f_data;
1846 		FILE_UNLOCK(fp);
1847 		(void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
1848 	} else
1849 		FILE_UNLOCK(fp);
1850 	if (fp->f_ops != &badfileops)
1851 		error = fo_close(fp, td);
1852 	else
1853 		error = 0;
1854 	ffree(fp);
1855 	mtx_unlock(&Giant);
1856 	return (error);
1857 }
1858 
1859 /*
1860  * Apply an advisory lock on a file descriptor.
1861  *
1862  * Just attempt to get a record lock of the requested type on
1863  * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
1864  */
1865 #ifndef _SYS_SYSPROTO_H_
1866 struct flock_args {
1867 	int	fd;
1868 	int	how;
1869 };
1870 #endif
1871 /*
1872  * MPSAFE
1873  */
1874 /* ARGSUSED */
1875 int
1876 flock(td, uap)
1877 	struct thread *td;
1878 	struct flock_args *uap;
1879 {
1880 	struct file *fp;
1881 	struct vnode *vp;
1882 	struct flock lf;
1883 	int error;
1884 
1885 	if ((error = fget(td, uap->fd, &fp)) != 0)
1886 		return (error);
1887 	if (fp->f_type != DTYPE_VNODE) {
1888 		fdrop(fp, td);
1889 		return (EOPNOTSUPP);
1890 	}
1891 
1892 	mtx_lock(&Giant);
1893 	vp = fp->f_data;
1894 	lf.l_whence = SEEK_SET;
1895 	lf.l_start = 0;
1896 	lf.l_len = 0;
1897 	if (uap->how & LOCK_UN) {
1898 		lf.l_type = F_UNLCK;
1899 		FILE_LOCK(fp);
1900 		fp->f_flag &= ~FHASLOCK;
1901 		FILE_UNLOCK(fp);
1902 		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
1903 		goto done2;
1904 	}
1905 	if (uap->how & LOCK_EX)
1906 		lf.l_type = F_WRLCK;
1907 	else if (uap->how & LOCK_SH)
1908 		lf.l_type = F_RDLCK;
1909 	else {
1910 		error = EBADF;
1911 		goto done2;
1912 	}
1913 	FILE_LOCK(fp);
1914 	fp->f_flag |= FHASLOCK;
1915 	FILE_UNLOCK(fp);
1916 	error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
1917 	    (uap->how & LOCK_NB) ? F_FLOCK : F_FLOCK | F_WAIT);
1918 done2:
1919 	fdrop(fp, td);
1920 	mtx_unlock(&Giant);
1921 	return (error);
1922 }
1923 
1924 /*
1925  * File Descriptor pseudo-device driver (/dev/fd/).
1926  *
1927  * Opening minor device N dup()s the file (if any) connected to file
1928  * descriptor N belonging to the calling process.  Note that this driver
1929  * consists of only the ``open()'' routine, because all subsequent
1930  * references to this file will be direct to the other driver.
1931  */
1932 /* ARGSUSED */
1933 static int
1934 fdopen(dev, mode, type, td)
1935 	dev_t dev;
1936 	int mode, type;
1937 	struct thread *td;
1938 {
1939 
1940 	/*
1941 	 * XXX Kludge: set curthread->td_dupfd to contain the value of the
1942 	 * the file descriptor being sought for duplication. The error
1943 	 * return ensures that the vnode for this device will be released
1944 	 * by vn_open. Open will detect this special error and take the
1945 	 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
1946 	 * will simply report the error.
1947 	 */
1948 	td->td_dupfd = dev2unit(dev);
1949 	return (ENODEV);
1950 }
1951 
1952 /*
1953  * Duplicate the specified descriptor to a free descriptor.
1954  */
1955 int
1956 dupfdopen(td, fdp, indx, dfd, mode, error)
1957 	struct thread *td;
1958 	struct filedesc *fdp;
1959 	int indx, dfd;
1960 	int mode;
1961 	int error;
1962 {
1963 	struct file *wfp;
1964 	struct file *fp;
1965 
1966 	/*
1967 	 * If the to-be-dup'd fd number is greater than the allowed number
1968 	 * of file descriptors, or the fd to be dup'd has already been
1969 	 * closed, then reject.
1970 	 */
1971 	FILEDESC_LOCK(fdp);
1972 	if (dfd < 0 || dfd >= fdp->fd_nfiles ||
1973 	    (wfp = fdp->fd_ofiles[dfd]) == NULL) {
1974 		FILEDESC_UNLOCK(fdp);
1975 		return (EBADF);
1976 	}
1977 
1978 	/*
1979 	 * There are two cases of interest here.
1980 	 *
1981 	 * For ENODEV simply dup (dfd) to file descriptor
1982 	 * (indx) and return.
1983 	 *
1984 	 * For ENXIO steal away the file structure from (dfd) and
1985 	 * store it in (indx).  (dfd) is effectively closed by
1986 	 * this operation.
1987 	 *
1988 	 * Any other error code is just returned.
1989 	 */
1990 	switch (error) {
1991 	case ENODEV:
1992 		/*
1993 		 * Check that the mode the file is being opened for is a
1994 		 * subset of the mode of the existing descriptor.
1995 		 */
1996 		FILE_LOCK(wfp);
1997 		if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
1998 			FILE_UNLOCK(wfp);
1999 			FILEDESC_UNLOCK(fdp);
2000 			return (EACCES);
2001 		}
2002 		fp = fdp->fd_ofiles[indx];
2003 #if 0
2004 		if (fp && fdp->fd_ofileflags[indx] & UF_MAPPED)
2005 			(void) munmapfd(td, indx);
2006 #endif
2007 		fdp->fd_ofiles[indx] = wfp;
2008 		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
2009 		fhold_locked(wfp);
2010 		FILE_UNLOCK(wfp);
2011 		if (indx > fdp->fd_lastfile)
2012 			fdp->fd_lastfile = indx;
2013 		if (fp != NULL)
2014 			FILE_LOCK(fp);
2015 		FILEDESC_UNLOCK(fdp);
2016 		/*
2017 		 * We now own the reference to fp that the ofiles[] array
2018 		 * used to own.  Release it.
2019 		 */
2020 		if (fp != NULL)
2021 			fdrop_locked(fp, td);
2022 		return (0);
2023 
2024 	case ENXIO:
2025 		/*
2026 		 * Steal away the file pointer from dfd and stuff it into indx.
2027 		 */
2028 		fp = fdp->fd_ofiles[indx];
2029 #if 0
2030 		if (fp && fdp->fd_ofileflags[indx] & UF_MAPPED)
2031 			(void) munmapfd(td, indx);
2032 #endif
2033 		fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
2034 		fdp->fd_ofiles[dfd] = NULL;
2035 		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
2036 		fdp->fd_ofileflags[dfd] = 0;
2037 
2038 		/*
2039 		 * Complete the clean up of the filedesc structure by
2040 		 * recomputing the various hints.
2041 		 */
2042 		if (indx > fdp->fd_lastfile) {
2043 			fdp->fd_lastfile = indx;
2044 		} else {
2045 			while (fdp->fd_lastfile > 0 &&
2046 			   fdp->fd_ofiles[fdp->fd_lastfile] == NULL) {
2047 				fdp->fd_lastfile--;
2048 			}
2049 			if (dfd < fdp->fd_freefile)
2050 				fdp->fd_freefile = dfd;
2051 		}
2052 		if (fp != NULL)
2053 			FILE_LOCK(fp);
2054 		FILEDESC_UNLOCK(fdp);
2055 
2056 		/*
2057 		 * we now own the reference to fp that the ofiles[] array
2058 		 * used to own.  Release it.
2059 		 */
2060 		if (fp != NULL)
2061 			fdrop_locked(fp, td);
2062 		return (0);
2063 
2064 	default:
2065 		FILEDESC_UNLOCK(fdp);
2066 		return (error);
2067 	}
2068 	/* NOTREACHED */
2069 }
2070 
2071 /*
2072  * Get file structures.
2073  */
2074 static int
2075 sysctl_kern_file(SYSCTL_HANDLER_ARGS)
2076 {
2077 	struct xfile xf;
2078 	struct filedesc *fdp;
2079 	struct file *fp;
2080 	struct proc *p;
2081 	int error, n;
2082 
2083 	sysctl_wire_old_buffer(req, 0);
2084 	if (req->oldptr == NULL) {
2085 		n = 16;		/* A slight overestimate. */
2086 		sx_slock(&filelist_lock);
2087 		LIST_FOREACH(fp, &filehead, f_list) {
2088 			/*
2089 			 * We should grab the lock, but this is an
2090 			 * estimate, so does it really matter?
2091 			 */
2092 			/* mtx_lock(fp->f_mtxp); */
2093 			n += fp->f_count;
2094 			/* mtx_unlock(f->f_mtxp); */
2095 		}
2096 		sx_sunlock(&filelist_lock);
2097 		return (SYSCTL_OUT(req, 0, n * sizeof(xf)));
2098 	}
2099 	error = 0;
2100 	bzero(&xf, sizeof(xf));
2101 	xf.xf_size = sizeof(xf);
2102 	sx_slock(&allproc_lock);
2103 	LIST_FOREACH(p, &allproc, p_list) {
2104 		PROC_LOCK(p);
2105 		xf.xf_pid = p->p_pid;
2106 		xf.xf_uid = p->p_ucred->cr_uid;
2107 		if ((fdp = p->p_fd) == NULL) {
2108 			PROC_UNLOCK(p);
2109 			continue;
2110 		}
2111 		FILEDESC_LOCK(fdp);
2112 		for (n = 0; n < fdp->fd_nfiles; ++n) {
2113 			if ((fp = fdp->fd_ofiles[n]) == NULL)
2114 				continue;
2115 			xf.xf_fd = n;
2116 			xf.xf_file = fp;
2117 			xf.xf_data = fp->f_data;
2118 			xf.xf_type = fp->f_type;
2119 			xf.xf_count = fp->f_count;
2120 			xf.xf_msgcount = fp->f_msgcount;
2121 			xf.xf_offset = fp->f_offset;
2122 			xf.xf_flag = fp->f_flag;
2123 			error = SYSCTL_OUT(req, &xf, sizeof(xf));
2124 			if (error)
2125 				break;
2126 		}
2127 		FILEDESC_UNLOCK(fdp);
2128 		PROC_UNLOCK(p);
2129 		if (error)
2130 			break;
2131 	}
2132 	sx_sunlock(&allproc_lock);
2133 	return (error);
2134 }
2135 
2136 SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
2137     0, 0, sysctl_kern_file, "S,xfile", "Entire file table");
2138 
2139 SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
2140     &maxfilesperproc, 0, "Maximum files allowed open per process");
2141 
2142 SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
2143     &maxfiles, 0, "Maximum number of files");
2144 
2145 SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD,
2146     &nfiles, 0, "System-wide number of open files");
2147 
2148 static void
2149 fildesc_drvinit(void *unused)
2150 {
2151 	dev_t dev;
2152 
2153 	dev = make_dev(&fildesc_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666, "fd/0");
2154 	make_dev_alias(dev, "stdin");
2155 	dev = make_dev(&fildesc_cdevsw, 1, UID_ROOT, GID_WHEEL, 0666, "fd/1");
2156 	make_dev_alias(dev, "stdout");
2157 	dev = make_dev(&fildesc_cdevsw, 2, UID_ROOT, GID_WHEEL, 0666, "fd/2");
2158 	make_dev_alias(dev, "stderr");
2159 #ifdef NODEVFS
2160 	{
2161 	int fd;
2162 
2163 	for (fd = 3; fd < NUMFDESC; fd++)
2164 		make_dev(&fildesc_cdevsw, fd, UID_ROOT, GID_WHEEL,
2165 		    0666, "fd/%d", fd);
2166 	}
2167 #endif
2168 }
2169 
2170 static fo_rdwr_t	badfo_readwrite;
2171 static fo_ioctl_t	badfo_ioctl;
2172 static fo_poll_t	badfo_poll;
2173 static fo_kqfilter_t	badfo_kqfilter;
2174 static fo_stat_t	badfo_stat;
2175 static fo_close_t	badfo_close;
2176 
2177 struct fileops badfileops = {
2178 	badfo_readwrite,
2179 	badfo_readwrite,
2180 	badfo_ioctl,
2181 	badfo_poll,
2182 	badfo_kqfilter,
2183 	badfo_stat,
2184 	badfo_close
2185 };
2186 
2187 static int
2188 badfo_readwrite(fp, uio, active_cred, flags, td)
2189 	struct file *fp;
2190 	struct uio *uio;
2191 	struct ucred *active_cred;
2192 	struct thread *td;
2193 	int flags;
2194 {
2195 
2196 	return (EBADF);
2197 }
2198 
2199 static int
2200 badfo_ioctl(fp, com, data, active_cred, td)
2201 	struct file *fp;
2202 	u_long com;
2203 	void *data;
2204 	struct ucred *active_cred;
2205 	struct thread *td;
2206 {
2207 
2208 	return (EBADF);
2209 }
2210 
2211 static int
2212 badfo_poll(fp, events, active_cred, td)
2213 	struct file *fp;
2214 	int events;
2215 	struct ucred *active_cred;
2216 	struct thread *td;
2217 {
2218 
2219 	return (0);
2220 }
2221 
2222 static int
2223 badfo_kqfilter(fp, kn)
2224 	struct file *fp;
2225 	struct knote *kn;
2226 {
2227 
2228 	return (0);
2229 }
2230 
2231 static int
2232 badfo_stat(fp, sb, active_cred, td)
2233 	struct file *fp;
2234 	struct stat *sb;
2235 	struct ucred *active_cred;
2236 	struct thread *td;
2237 {
2238 
2239 	return (EBADF);
2240 }
2241 
2242 static int
2243 badfo_close(fp, td)
2244 	struct file *fp;
2245 	struct thread *td;
2246 {
2247 
2248 	return (EBADF);
2249 }
2250 
2251 SYSINIT(fildescdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,
2252 					fildesc_drvinit,NULL)
2253 
2254 static void filelistinit(void *);
2255 SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, filelistinit, NULL)
2256 
2257 /* ARGSUSED*/
2258 static void
2259 filelistinit(dummy)
2260 	void *dummy;
2261 {
2262 
2263 	file_zone = uma_zcreate("Files", sizeof(struct file), NULL, NULL,
2264 	    NULL, NULL, UMA_ALIGN_PTR, 0);
2265 	sx_init(&filelist_lock, "filelist lock");
2266 	mtx_init(&sigio_lock, "sigio lock", NULL, MTX_DEF);
2267 }
2268