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