kern_descrip.c revision 74523
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 74523 2001-03-20 13:26:13Z phk $
40 */
41
42#include "opt_compat.h"
43#include <sys/param.h>
44#include <sys/systm.h>
45#include <sys/sysproto.h>
46#include <sys/conf.h>
47#include <sys/filedesc.h>
48#include <sys/kernel.h>
49#include <sys/sysctl.h>
50#include <sys/vnode.h>
51#include <sys/proc.h>
52#include <sys/file.h>
53#include <sys/stat.h>
54#include <sys/filio.h>
55#include <sys/fcntl.h>
56#include <sys/malloc.h>
57#include <sys/unistd.h>
58#include <sys/resourcevar.h>
59#include <sys/event.h>
60
61#include <vm/vm.h>
62#include <vm/vm_extern.h>
63
64static MALLOC_DEFINE(M_FILEDESC, "file desc", "Open file descriptor table");
65MALLOC_DEFINE(M_FILE, "file", "Open file structure");
66static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures");
67
68static	 d_open_t  fdopen;
69#define NUMFDESC 64
70
71#define CDEV_MAJOR 22
72static struct cdevsw fildesc_cdevsw = {
73	/* open */	fdopen,
74	/* close */	noclose,
75	/* read */	noread,
76	/* write */	nowrite,
77	/* ioctl */	noioctl,
78	/* poll */	nopoll,
79	/* mmap */	nommap,
80	/* strategy */	nostrategy,
81	/* name */	"FD",
82	/* maj */	CDEV_MAJOR,
83	/* dump */	nodump,
84	/* psize */	nopsize,
85	/* flags */	0,
86	/* bmaj */	-1
87};
88
89static int do_dup __P((struct filedesc *fdp, int old, int new, register_t *retval, struct proc *p));
90static int badfo_readwrite __P((struct file *fp, struct uio *uio,
91    struct ucred *cred, int flags, struct proc *p));
92static int badfo_ioctl __P((struct file *fp, u_long com, caddr_t data,
93    struct proc *p));
94static int badfo_poll __P((struct file *fp, int events,
95    struct ucred *cred, struct proc *p));
96static int badfo_kqfilter __P((struct file *fp, struct knote *kn));
97static int badfo_stat __P((struct file *fp, struct stat *sb, struct proc *p));
98static int badfo_close __P((struct file *fp, struct proc *p));
99
100/*
101 * Descriptor management.
102 */
103struct filelist filehead;	/* head of list of open files */
104int nfiles;			/* actual number of open files */
105extern int cmask;
106
107/*
108 * System calls on descriptors.
109 */
110#ifndef _SYS_SYSPROTO_H_
111struct getdtablesize_args {
112	int	dummy;
113};
114#endif
115/* ARGSUSED */
116int
117getdtablesize(p, uap)
118	struct proc *p;
119	struct getdtablesize_args *uap;
120{
121
122	p->p_retval[0] =
123	    min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
124	return (0);
125}
126
127/*
128 * Duplicate a file descriptor to a particular value.
129 *
130 * note: keep in mind that a potential race condition exists when closing
131 * descriptors from a shared descriptor table (via rfork).
132 */
133#ifndef _SYS_SYSPROTO_H_
134struct dup2_args {
135	u_int	from;
136	u_int	to;
137};
138#endif
139/* ARGSUSED */
140int
141dup2(p, uap)
142	struct proc *p;
143	struct dup2_args *uap;
144{
145	register struct filedesc *fdp = p->p_fd;
146	register u_int old = uap->from, new = uap->to;
147	int i, error;
148
149retry:
150	if (old >= fdp->fd_nfiles ||
151	    fdp->fd_ofiles[old] == NULL ||
152	    new >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
153	    new >= maxfilesperproc) {
154		return (EBADF);
155	}
156	if (old == new) {
157		p->p_retval[0] = new;
158		return (0);
159	}
160	if (new >= fdp->fd_nfiles) {
161		if ((error = fdalloc(p, new, &i)))
162			return (error);
163		if (new != i)
164			panic("dup2: fdalloc");
165		/*
166		 * fdalloc() may block, retest everything.
167		 */
168		goto retry;
169	}
170	return (do_dup(fdp, (int)old, (int)new, p->p_retval, p));
171}
172
173/*
174 * Duplicate a file descriptor.
175 */
176#ifndef _SYS_SYSPROTO_H_
177struct dup_args {
178	u_int	fd;
179};
180#endif
181/* ARGSUSED */
182int
183dup(p, uap)
184	struct proc *p;
185	struct dup_args *uap;
186{
187	register struct filedesc *fdp;
188	u_int old;
189	int new, error;
190
191	old = uap->fd;
192	fdp = p->p_fd;
193	if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL)
194		return (EBADF);
195	if ((error = fdalloc(p, 0, &new)))
196		return (error);
197	return (do_dup(fdp, (int)old, new, p->p_retval, p));
198}
199
200/*
201 * The file control system call.
202 */
203#ifndef _SYS_SYSPROTO_H_
204struct fcntl_args {
205	int	fd;
206	int	cmd;
207	long	arg;
208};
209#endif
210/* ARGSUSED */
211int
212fcntl(p, uap)
213	struct proc *p;
214	register struct fcntl_args *uap;
215{
216	register struct filedesc *fdp = p->p_fd;
217	register struct file *fp;
218	register char *pop;
219	struct vnode *vp;
220	int i, tmp, error, flg = F_POSIX;
221	struct flock fl;
222	u_int newmin;
223
224	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
225	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
226		return (EBADF);
227	pop = &fdp->fd_ofileflags[uap->fd];
228
229	switch (uap->cmd) {
230	case F_DUPFD:
231		newmin = uap->arg;
232		if (newmin >= p->p_rlimit[RLIMIT_NOFILE].rlim_cur ||
233		    newmin >= maxfilesperproc)
234			return (EINVAL);
235		if ((error = fdalloc(p, newmin, &i)))
236			return (error);
237		return (do_dup(fdp, uap->fd, i, p->p_retval, p));
238
239	case F_GETFD:
240		p->p_retval[0] = *pop & 1;
241		return (0);
242
243	case F_SETFD:
244		*pop = (*pop &~ 1) | (uap->arg & 1);
245		return (0);
246
247	case F_GETFL:
248		p->p_retval[0] = OFLAGS(fp->f_flag);
249		return (0);
250
251	case F_SETFL:
252		fhold(fp);
253		fp->f_flag &= ~FCNTLFLAGS;
254		fp->f_flag |= FFLAGS(uap->arg & ~O_ACCMODE) & FCNTLFLAGS;
255		tmp = fp->f_flag & FNONBLOCK;
256		error = fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, p);
257		if (error) {
258			fdrop(fp, p);
259			return (error);
260		}
261		tmp = fp->f_flag & FASYNC;
262		error = fo_ioctl(fp, FIOASYNC, (caddr_t)&tmp, p);
263		if (!error) {
264			fdrop(fp, p);
265			return (0);
266		}
267		fp->f_flag &= ~FNONBLOCK;
268		tmp = 0;
269		(void)fo_ioctl(fp, FIONBIO, (caddr_t)&tmp, p);
270		fdrop(fp, p);
271		return (error);
272
273	case F_GETOWN:
274		fhold(fp);
275		error = fo_ioctl(fp, FIOGETOWN, (caddr_t)p->p_retval, p);
276		fdrop(fp, p);
277		return(error);
278
279	case F_SETOWN:
280		fhold(fp);
281		error = fo_ioctl(fp, FIOSETOWN, (caddr_t)&uap->arg, p);
282		fdrop(fp, p);
283		return(error);
284
285	case F_SETLKW:
286		flg |= F_WAIT;
287		/* Fall into F_SETLK */
288
289	case F_SETLK:
290		if (fp->f_type != DTYPE_VNODE)
291			return (EBADF);
292		vp = (struct vnode *)fp->f_data;
293
294		/*
295		 * copyin/lockop may block
296		 */
297		fhold(fp);
298		/* Copy in the lock structure */
299		error = copyin((caddr_t)(intptr_t)uap->arg, (caddr_t)&fl,
300		    sizeof(fl));
301		if (error) {
302			fdrop(fp, p);
303			return (error);
304		}
305		if (fl.l_whence == SEEK_CUR)
306			fl.l_start += fp->f_offset;
307
308		switch (fl.l_type) {
309		case F_RDLCK:
310			if ((fp->f_flag & FREAD) == 0) {
311				error = EBADF;
312				break;
313			}
314			p->p_flag |= P_ADVLOCK;
315			error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
316			    &fl, flg);
317			break;
318		case F_WRLCK:
319			if ((fp->f_flag & FWRITE) == 0) {
320				error = EBADF;
321				break;
322			}
323			p->p_flag |= P_ADVLOCK;
324			error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
325			    &fl, flg);
326			break;
327		case F_UNLCK:
328			error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
329				&fl, F_POSIX);
330			break;
331		default:
332			error = EINVAL;
333			break;
334		}
335		fdrop(fp, p);
336		return(error);
337
338	case F_GETLK:
339		if (fp->f_type != DTYPE_VNODE)
340			return (EBADF);
341		vp = (struct vnode *)fp->f_data;
342		/*
343		 * copyin/lockop may block
344		 */
345		fhold(fp);
346		/* Copy in the lock structure */
347		error = copyin((caddr_t)(intptr_t)uap->arg, (caddr_t)&fl,
348		    sizeof(fl));
349		if (error) {
350			fdrop(fp, p);
351			return (error);
352		}
353		if (fl.l_type != F_RDLCK && fl.l_type != F_WRLCK &&
354		    fl.l_type != F_UNLCK) {
355			fdrop(fp, p);
356			return (EINVAL);
357		}
358		if (fl.l_whence == SEEK_CUR)
359			fl.l_start += fp->f_offset;
360		error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK,
361			    &fl, F_POSIX);
362		fdrop(fp, p);
363		if (error == 0) {
364			error = copyout((caddr_t)&fl,
365				    (caddr_t)(intptr_t)uap->arg, sizeof(fl));
366		}
367		return(error);
368	default:
369		return (EINVAL);
370	}
371	/* NOTREACHED */
372}
373
374/*
375 * Common code for dup, dup2, and fcntl(F_DUPFD).
376 */
377static int
378do_dup(fdp, old, new, retval, p)
379	register struct filedesc *fdp;
380	register int old, new;
381	register_t *retval;
382	struct proc *p;
383{
384	struct file *fp;
385	struct file *delfp;
386
387	/*
388	 * Save info on the descriptor being overwritten.  We have
389	 * to do the unmap now, but we cannot close it without
390	 * introducing an ownership race for the slot.
391	 */
392	delfp = fdp->fd_ofiles[new];
393#if 0
394	if (delfp && (fdp->fd_ofileflags[new] & UF_MAPPED))
395		(void) munmapfd(p, new);
396#endif
397
398	/*
399	 * Duplicate the source descriptor, update lastfile
400	 */
401	fp = fdp->fd_ofiles[old];
402	fdp->fd_ofiles[new] = fp;
403	fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
404	fhold(fp);
405	if (new > fdp->fd_lastfile)
406		fdp->fd_lastfile = new;
407	*retval = new;
408
409	/*
410	 * If we dup'd over a valid file, we now own the reference to it
411	 * and must dispose of it using closef() semantics (as if a
412	 * close() were performed on it).
413	 */
414	if (delfp)
415		(void) closef(delfp, p);
416	return (0);
417}
418
419/*
420 * If sigio is on the list associated with a process or process group,
421 * disable signalling from the device, remove sigio from the list and
422 * free sigio.
423 */
424void
425funsetown(sigio)
426	struct sigio *sigio;
427{
428	int s;
429
430	if (sigio == NULL)
431		return;
432	s = splhigh();
433	*(sigio->sio_myref) = NULL;
434	splx(s);
435	if (sigio->sio_pgid < 0) {
436		SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio,
437			     sigio, sio_pgsigio);
438	} else /* if ((*sigiop)->sio_pgid > 0) */ {
439		SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio,
440			     sigio, sio_pgsigio);
441	}
442	crfree(sigio->sio_ucred);
443	FREE(sigio, M_SIGIO);
444}
445
446/* Free a list of sigio structures. */
447void
448funsetownlst(sigiolst)
449	struct sigiolst *sigiolst;
450{
451	struct sigio *sigio;
452
453	while ((sigio = SLIST_FIRST(sigiolst)) != NULL)
454		funsetown(sigio);
455}
456
457/*
458 * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg).
459 *
460 * After permission checking, add a sigio structure to the sigio list for
461 * the process or process group.
462 */
463int
464fsetown(pgid, sigiop)
465	pid_t pgid;
466	struct sigio **sigiop;
467{
468	struct proc *proc;
469	struct pgrp *pgrp;
470	struct sigio *sigio;
471	int s;
472
473	if (pgid == 0) {
474		funsetown(*sigiop);
475		return (0);
476	}
477	if (pgid > 0) {
478		proc = pfind(pgid);
479		if (proc == NULL)
480			return (ESRCH);
481
482		/*
483		 * Policy - Don't allow a process to FSETOWN a process
484		 * in another session.
485		 *
486		 * Remove this test to allow maximum flexibility or
487		 * restrict FSETOWN to the current process or process
488		 * group for maximum safety.
489		 */
490		if (proc->p_session != curproc->p_session)
491			return (EPERM);
492
493		pgrp = NULL;
494	} else /* if (pgid < 0) */ {
495		pgrp = pgfind(-pgid);
496		if (pgrp == NULL)
497			return (ESRCH);
498
499		/*
500		 * Policy - Don't allow a process to FSETOWN a process
501		 * in another session.
502		 *
503		 * Remove this test to allow maximum flexibility or
504		 * restrict FSETOWN to the current process or process
505		 * group for maximum safety.
506		 */
507		if (pgrp->pg_session != curproc->p_session)
508			return (EPERM);
509
510		proc = NULL;
511	}
512	funsetown(*sigiop);
513	MALLOC(sigio, struct sigio *, sizeof(struct sigio), M_SIGIO, M_WAITOK);
514	if (pgid > 0) {
515		SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio);
516		sigio->sio_proc = proc;
517	} else {
518		SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
519		sigio->sio_pgrp = pgrp;
520	}
521	sigio->sio_pgid = pgid;
522	crhold(curproc->p_ucred);
523	sigio->sio_ucred = curproc->p_ucred;
524	/* It would be convenient if p_ruid was in ucred. */
525	sigio->sio_ruid = curproc->p_cred->p_ruid;
526	sigio->sio_myref = sigiop;
527	s = splhigh();
528	*sigiop = sigio;
529	splx(s);
530	return (0);
531}
532
533/*
534 * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
535 */
536pid_t
537fgetown(sigio)
538	struct sigio *sigio;
539{
540	return (sigio != NULL ? sigio->sio_pgid : 0);
541}
542
543/*
544 * Close a file descriptor.
545 */
546#ifndef _SYS_SYSPROTO_H_
547struct close_args {
548        int     fd;
549};
550#endif
551/* ARGSUSED */
552int
553close(p, uap)
554	struct proc *p;
555	struct close_args *uap;
556{
557	register struct filedesc *fdp = p->p_fd;
558	register struct file *fp;
559	register int fd = uap->fd;
560
561	if ((unsigned)fd >= fdp->fd_nfiles ||
562	    (fp = fdp->fd_ofiles[fd]) == NULL)
563		return (EBADF);
564#if 0
565	if (fdp->fd_ofileflags[fd] & UF_MAPPED)
566		(void) munmapfd(p, fd);
567#endif
568	fdp->fd_ofiles[fd] = NULL;
569	fdp->fd_ofileflags[fd] = 0;
570
571	/*
572	 * we now hold the fp reference that used to be owned by the descriptor
573	 * array.
574	 */
575	while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
576		fdp->fd_lastfile--;
577	if (fd < fdp->fd_freefile)
578		fdp->fd_freefile = fd;
579	if (fd < fdp->fd_knlistsize)
580		knote_fdclose(p, fd);
581	return (closef(fp, p));
582}
583
584#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
585/*
586 * Return status information about a file descriptor.
587 */
588#ifndef _SYS_SYSPROTO_H_
589struct ofstat_args {
590	int	fd;
591	struct	ostat *sb;
592};
593#endif
594/* ARGSUSED */
595int
596ofstat(p, uap)
597	struct proc *p;
598	register struct ofstat_args *uap;
599{
600	register struct filedesc *fdp = p->p_fd;
601	register struct file *fp;
602	struct stat ub;
603	struct ostat oub;
604	int error;
605
606	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
607	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
608		return (EBADF);
609	fhold(fp);
610	error = fo_stat(fp, &ub, p);
611	if (error == 0) {
612		cvtstat(&ub, &oub);
613		error = copyout((caddr_t)&oub, (caddr_t)uap->sb, sizeof (oub));
614	}
615	fdrop(fp, p);
616	return (error);
617}
618#endif /* COMPAT_43 || COMPAT_SUNOS */
619
620/*
621 * Return status information about a file descriptor.
622 */
623#ifndef _SYS_SYSPROTO_H_
624struct fstat_args {
625	int	fd;
626	struct	stat *sb;
627};
628#endif
629/* ARGSUSED */
630int
631fstat(p, uap)
632	struct proc *p;
633	register struct fstat_args *uap;
634{
635	register struct filedesc *fdp = p->p_fd;
636	register struct file *fp;
637	struct stat ub;
638	int error;
639
640	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
641	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
642		return (EBADF);
643	fhold(fp);
644	error = fo_stat(fp, &ub, p);
645	if (error == 0)
646		error = copyout((caddr_t)&ub, (caddr_t)uap->sb, sizeof (ub));
647	fdrop(fp, p);
648	return (error);
649}
650
651/*
652 * Return status information about a file descriptor.
653 */
654#ifndef _SYS_SYSPROTO_H_
655struct nfstat_args {
656	int	fd;
657	struct	nstat *sb;
658};
659#endif
660/* ARGSUSED */
661int
662nfstat(p, uap)
663	struct proc *p;
664	register struct nfstat_args *uap;
665{
666	register struct filedesc *fdp = p->p_fd;
667	register struct file *fp;
668	struct stat ub;
669	struct nstat nub;
670	int error;
671
672	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
673	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
674		return (EBADF);
675	fhold(fp);
676	error = fo_stat(fp, &ub, p);
677	if (error == 0) {
678		cvtnstat(&ub, &nub);
679		error = copyout((caddr_t)&nub, (caddr_t)uap->sb, sizeof (nub));
680	}
681	fdrop(fp, p);
682	return (error);
683}
684
685/*
686 * Return pathconf information about a file descriptor.
687 */
688#ifndef _SYS_SYSPROTO_H_
689struct fpathconf_args {
690	int	fd;
691	int	name;
692};
693#endif
694/* ARGSUSED */
695int
696fpathconf(p, uap)
697	struct proc *p;
698	register struct fpathconf_args *uap;
699{
700	struct filedesc *fdp = p->p_fd;
701	struct file *fp;
702	struct vnode *vp;
703	int error = 0;
704
705	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
706	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
707		return (EBADF);
708
709	fhold(fp);
710
711	switch (fp->f_type) {
712	case DTYPE_PIPE:
713	case DTYPE_SOCKET:
714		if (uap->name != _PC_PIPE_BUF)
715			return (EINVAL);
716		p->p_retval[0] = PIPE_BUF;
717		error = 0;
718		break;
719	case DTYPE_FIFO:
720	case DTYPE_VNODE:
721		vp = (struct vnode *)fp->f_data;
722		error = VOP_PATHCONF(vp, uap->name, p->p_retval);
723		break;
724	default:
725		error = EOPNOTSUPP;
726		break;
727	}
728	fdrop(fp, p);
729	return(error);
730}
731
732/*
733 * Allocate a file descriptor for the process.
734 */
735static int fdexpand;
736SYSCTL_INT(_debug, OID_AUTO, fdexpand, CTLFLAG_RD, &fdexpand, 0, "");
737
738int
739fdalloc(p, want, result)
740	struct proc *p;
741	int want;
742	int *result;
743{
744	register struct filedesc *fdp = p->p_fd;
745	register int i;
746	int lim, last, nfiles;
747	struct file **newofile;
748	char *newofileflags;
749
750	/*
751	 * Search for a free descriptor starting at the higher
752	 * of want or fd_freefile.  If that fails, consider
753	 * expanding the ofile array.
754	 */
755	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
756	for (;;) {
757		last = min(fdp->fd_nfiles, lim);
758		if ((i = want) < fdp->fd_freefile)
759			i = fdp->fd_freefile;
760		for (; i < last; i++) {
761			if (fdp->fd_ofiles[i] == NULL) {
762				fdp->fd_ofileflags[i] = 0;
763				if (i > fdp->fd_lastfile)
764					fdp->fd_lastfile = i;
765				if (want <= fdp->fd_freefile)
766					fdp->fd_freefile = i;
767				*result = i;
768				return (0);
769			}
770		}
771
772		/*
773		 * No space in current array.  Expand?
774		 */
775		if (fdp->fd_nfiles >= lim)
776			return (EMFILE);
777		if (fdp->fd_nfiles < NDEXTENT)
778			nfiles = NDEXTENT;
779		else
780			nfiles = 2 * fdp->fd_nfiles;
781		MALLOC(newofile, struct file **, nfiles * OFILESIZE,
782		    M_FILEDESC, M_WAITOK);
783
784		/*
785		 * deal with file-table extend race that might have occured
786		 * when malloc was blocked.
787		 */
788		if (fdp->fd_nfiles >= nfiles) {
789			FREE(newofile, M_FILEDESC);
790			continue;
791		}
792		newofileflags = (char *) &newofile[nfiles];
793		/*
794		 * Copy the existing ofile and ofileflags arrays
795		 * and zero the new portion of each array.
796		 */
797		bcopy(fdp->fd_ofiles, newofile,
798			(i = sizeof(struct file *) * fdp->fd_nfiles));
799		bzero((char *)newofile + i, nfiles * sizeof(struct file *) - i);
800		bcopy(fdp->fd_ofileflags, newofileflags,
801			(i = sizeof(char) * fdp->fd_nfiles));
802		bzero(newofileflags + i, nfiles * sizeof(char) - i);
803		if (fdp->fd_nfiles > NDFILE)
804			FREE(fdp->fd_ofiles, M_FILEDESC);
805		fdp->fd_ofiles = newofile;
806		fdp->fd_ofileflags = newofileflags;
807		fdp->fd_nfiles = nfiles;
808		fdexpand++;
809	}
810	return (0);
811}
812
813/*
814 * Check to see whether n user file descriptors
815 * are available to the process p.
816 */
817int
818fdavail(p, n)
819	struct proc *p;
820	register int n;
821{
822	register struct filedesc *fdp = p->p_fd;
823	register struct file **fpp;
824	register int i, lim, last;
825
826	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
827	if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
828		return (1);
829
830	last = min(fdp->fd_nfiles, lim);
831	fpp = &fdp->fd_ofiles[fdp->fd_freefile];
832	for (i = last - fdp->fd_freefile; --i >= 0; fpp++) {
833		if (*fpp == NULL && --n <= 0)
834			return (1);
835	}
836	return (0);
837}
838
839/*
840 * Create a new open file structure and allocate
841 * a file decriptor for the process that refers to it.
842 */
843int
844falloc(p, resultfp, resultfd)
845	register struct proc *p;
846	struct file **resultfp;
847	int *resultfd;
848{
849	register struct file *fp, *fq;
850	int error, i;
851
852	if (nfiles >= maxfiles) {
853		tablefull("file");
854		return (ENFILE);
855	}
856	/*
857	 * Allocate a new file descriptor.
858	 * If the process has file descriptor zero open, add to the list
859	 * of open files at that point, otherwise put it at the front of
860	 * the list of open files.
861	 */
862	nfiles++;
863	MALLOC(fp, struct file *, sizeof(struct file), M_FILE, M_WAITOK | M_ZERO);
864
865	/*
866	 * wait until after malloc (which may have blocked) returns before
867	 * allocating the slot, else a race might have shrunk it if we had
868	 * allocated it before the malloc.
869	 */
870	if ((error = fdalloc(p, 0, &i))) {
871		nfiles--;
872		FREE(fp, M_FILE);
873		return (error);
874	}
875	fp->f_count = 1;
876	fp->f_cred = p->p_ucred;
877	fp->f_ops = &badfileops;
878	fp->f_seqcount = 1;
879	crhold(fp->f_cred);
880	if ((fq = p->p_fd->fd_ofiles[0])) {
881		LIST_INSERT_AFTER(fq, fp, f_list);
882	} else {
883		LIST_INSERT_HEAD(&filehead, fp, f_list);
884	}
885	p->p_fd->fd_ofiles[i] = fp;
886	if (resultfp)
887		*resultfp = fp;
888	if (resultfd)
889		*resultfd = i;
890	return (0);
891}
892
893/*
894 * Free a file descriptor.
895 */
896void
897ffree(fp)
898	register struct file *fp;
899{
900	KASSERT((fp->f_count == 0), ("ffree: fp_fcount not 0!"));
901	LIST_REMOVE(fp, f_list);
902	crfree(fp->f_cred);
903	nfiles--;
904	FREE(fp, M_FILE);
905}
906
907/*
908 * Build a new filedesc structure.
909 */
910struct filedesc *
911fdinit(p)
912	struct proc *p;
913{
914	register struct filedesc0 *newfdp;
915	register struct filedesc *fdp = p->p_fd;
916
917	MALLOC(newfdp, struct filedesc0 *, sizeof(struct filedesc0),
918	    M_FILEDESC, M_WAITOK | M_ZERO);
919	newfdp->fd_fd.fd_cdir = fdp->fd_cdir;
920	if (newfdp->fd_fd.fd_cdir)
921		VREF(newfdp->fd_fd.fd_cdir);
922	newfdp->fd_fd.fd_rdir = fdp->fd_rdir;
923	if (newfdp->fd_fd.fd_rdir)
924		VREF(newfdp->fd_fd.fd_rdir);
925	newfdp->fd_fd.fd_jdir = fdp->fd_jdir;
926	if (newfdp->fd_fd.fd_jdir)
927		VREF(newfdp->fd_fd.fd_jdir);
928
929	/* Create the file descriptor table. */
930	newfdp->fd_fd.fd_refcnt = 1;
931	newfdp->fd_fd.fd_cmask = cmask;
932	newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
933	newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
934	newfdp->fd_fd.fd_nfiles = NDFILE;
935	newfdp->fd_fd.fd_knlistsize = -1;
936
937	return (&newfdp->fd_fd);
938}
939
940/*
941 * Share a filedesc structure.
942 */
943struct filedesc *
944fdshare(p)
945	struct proc *p;
946{
947	p->p_fd->fd_refcnt++;
948	return (p->p_fd);
949}
950
951/*
952 * Copy a filedesc structure.
953 */
954struct filedesc *
955fdcopy(p)
956	struct proc *p;
957{
958	register struct filedesc *newfdp, *fdp = p->p_fd;
959	register struct file **fpp;
960	register int i;
961
962	/* Certain daemons might not have file descriptors. */
963	if (fdp == NULL)
964		return (NULL);
965
966	MALLOC(newfdp, struct filedesc *, sizeof(struct filedesc0),
967	    M_FILEDESC, M_WAITOK);
968	bcopy(fdp, newfdp, sizeof(struct filedesc));
969	if (newfdp->fd_cdir)
970		VREF(newfdp->fd_cdir);
971	if (newfdp->fd_rdir)
972		VREF(newfdp->fd_rdir);
973	if (newfdp->fd_jdir)
974		VREF(newfdp->fd_jdir);
975	newfdp->fd_refcnt = 1;
976
977	/*
978	 * If the number of open files fits in the internal arrays
979	 * of the open file structure, use them, otherwise allocate
980	 * additional memory for the number of descriptors currently
981	 * in use.
982	 */
983	if (newfdp->fd_lastfile < NDFILE) {
984		newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
985		newfdp->fd_ofileflags =
986		    ((struct filedesc0 *) newfdp)->fd_dfileflags;
987		i = NDFILE;
988	} else {
989		/*
990		 * Compute the smallest multiple of NDEXTENT needed
991		 * for the file descriptors currently in use,
992		 * allowing the table to shrink.
993		 */
994		i = newfdp->fd_nfiles;
995		while (i > 2 * NDEXTENT && i > newfdp->fd_lastfile * 2)
996			i /= 2;
997		MALLOC(newfdp->fd_ofiles, struct file **, i * OFILESIZE,
998		    M_FILEDESC, M_WAITOK);
999		newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
1000	}
1001	newfdp->fd_nfiles = i;
1002	bcopy(fdp->fd_ofiles, newfdp->fd_ofiles, i * sizeof(struct file **));
1003	bcopy(fdp->fd_ofileflags, newfdp->fd_ofileflags, i * sizeof(char));
1004
1005	/*
1006	 * kq descriptors cannot be copied.
1007	 */
1008	if (newfdp->fd_knlistsize != -1) {
1009		fpp = newfdp->fd_ofiles;
1010		for (i = newfdp->fd_lastfile; i-- >= 0; fpp++) {
1011			if (*fpp != NULL && (*fpp)->f_type == DTYPE_KQUEUE)
1012				*fpp = NULL;
1013		}
1014		newfdp->fd_knlist = NULL;
1015		newfdp->fd_knlistsize = -1;
1016		newfdp->fd_knhash = NULL;
1017		newfdp->fd_knhashmask = 0;
1018	}
1019
1020	fpp = newfdp->fd_ofiles;
1021	for (i = newfdp->fd_lastfile; i-- >= 0; fpp++) {
1022		if (*fpp != NULL)
1023			fhold(*fpp);
1024	}
1025	return (newfdp);
1026}
1027
1028/*
1029 * Release a filedesc structure.
1030 */
1031void
1032fdfree(p)
1033	struct proc *p;
1034{
1035	register struct filedesc *fdp = p->p_fd;
1036	struct file **fpp;
1037	register int i;
1038
1039	/* Certain daemons might not have file descriptors. */
1040	if (fdp == NULL)
1041		return;
1042
1043	if (--fdp->fd_refcnt > 0)
1044		return;
1045	/*
1046	 * we are the last reference to the structure, we can
1047	 * safely assume it will not change out from under us.
1048	 */
1049	fpp = fdp->fd_ofiles;
1050	for (i = fdp->fd_lastfile; i-- >= 0; fpp++) {
1051		if (*fpp)
1052			(void) closef(*fpp, p);
1053	}
1054	if (fdp->fd_nfiles > NDFILE)
1055		FREE(fdp->fd_ofiles, M_FILEDESC);
1056	if (fdp->fd_cdir)
1057		vrele(fdp->fd_cdir);
1058	if (fdp->fd_rdir)
1059		vrele(fdp->fd_rdir);
1060	if (fdp->fd_jdir)
1061		vrele(fdp->fd_jdir);
1062	if (fdp->fd_knlist)
1063		FREE(fdp->fd_knlist, M_TEMP);
1064	if (fdp->fd_knhash)
1065		FREE(fdp->fd_knhash, M_TEMP);
1066	FREE(fdp, M_FILEDESC);
1067}
1068
1069/*
1070 * For setugid programs, we don't want to people to use that setugidness
1071 * to generate error messages which write to a file which otherwise would
1072 * otherwise be off-limits to the process.
1073 *
1074 * This is a gross hack to plug the hole.  A better solution would involve
1075 * a special vop or other form of generalized access control mechanism.  We
1076 * go ahead and just reject all procfs file systems accesses as dangerous.
1077 *
1078 * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
1079 * sufficient.  We also don't for check setugidness since we know we are.
1080 */
1081static int
1082is_unsafe(struct file *fp)
1083{
1084	if (fp->f_type == DTYPE_VNODE &&
1085	    ((struct vnode *)(fp->f_data))->v_tag == VT_PROCFS)
1086		return (1);
1087	return (0);
1088}
1089
1090/*
1091 * Make this setguid thing safe, if at all possible.
1092 */
1093void
1094setugidsafety(p)
1095	struct proc *p;
1096{
1097	struct filedesc *fdp = p->p_fd;
1098	register int i;
1099
1100	/* Certain daemons might not have file descriptors. */
1101	if (fdp == NULL)
1102		return;
1103
1104	/*
1105	 * note: fdp->fd_ofiles may be reallocated out from under us while
1106	 * we are blocked in a close.  Be careful!
1107	 */
1108	for (i = 0; i <= fdp->fd_lastfile; i++) {
1109		if (i > 2)
1110			break;
1111		if (fdp->fd_ofiles[i] && is_unsafe(fdp->fd_ofiles[i])) {
1112			struct file *fp;
1113
1114#if 0
1115			if ((fdp->fd_ofileflags[i] & UF_MAPPED) != 0)
1116				(void) munmapfd(p, i);
1117#endif
1118			if (i < fdp->fd_knlistsize)
1119				knote_fdclose(p, i);
1120			/*
1121			 * NULL-out descriptor prior to close to avoid
1122			 * a race while close blocks.
1123			 */
1124			fp = fdp->fd_ofiles[i];
1125			fdp->fd_ofiles[i] = NULL;
1126			fdp->fd_ofileflags[i] = 0;
1127			if (i < fdp->fd_freefile)
1128				fdp->fd_freefile = i;
1129			(void) closef(fp, p);
1130		}
1131	}
1132	while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
1133		fdp->fd_lastfile--;
1134}
1135
1136/*
1137 * Close any files on exec?
1138 */
1139void
1140fdcloseexec(p)
1141	struct proc *p;
1142{
1143	struct filedesc *fdp = p->p_fd;
1144	register int i;
1145
1146	/* Certain daemons might not have file descriptors. */
1147	if (fdp == NULL)
1148		return;
1149
1150	/*
1151	 * We cannot cache fd_ofiles or fd_ofileflags since operations
1152	 * may block and rip them out from under us.
1153	 */
1154	for (i = 0; i <= fdp->fd_lastfile; i++) {
1155		if (fdp->fd_ofiles[i] != NULL &&
1156		    (fdp->fd_ofileflags[i] & UF_EXCLOSE)) {
1157			struct file *fp;
1158
1159#if 0
1160			if (fdp->fd_ofileflags[i] & UF_MAPPED)
1161				(void) munmapfd(p, i);
1162#endif
1163			if (i < fdp->fd_knlistsize)
1164				knote_fdclose(p, i);
1165			/*
1166			 * NULL-out descriptor prior to close to avoid
1167			 * a race while close blocks.
1168			 */
1169			fp = fdp->fd_ofiles[i];
1170			fdp->fd_ofiles[i] = NULL;
1171			fdp->fd_ofileflags[i] = 0;
1172			if (i < fdp->fd_freefile)
1173				fdp->fd_freefile = i;
1174			(void) closef(fp, p);
1175		}
1176	}
1177	while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
1178		fdp->fd_lastfile--;
1179}
1180
1181/*
1182 * Internal form of close.
1183 * Decrement reference count on file structure.
1184 * Note: p may be NULL when closing a file
1185 * that was being passed in a message.
1186 */
1187int
1188closef(fp, p)
1189	register struct file *fp;
1190	register struct proc *p;
1191{
1192	struct vnode *vp;
1193	struct flock lf;
1194
1195	if (fp == NULL)
1196		return (0);
1197	/*
1198	 * POSIX record locking dictates that any close releases ALL
1199	 * locks owned by this process.  This is handled by setting
1200	 * a flag in the unlock to free ONLY locks obeying POSIX
1201	 * semantics, and not to free BSD-style file locks.
1202	 * If the descriptor was in a message, POSIX-style locks
1203	 * aren't passed with the descriptor.
1204	 */
1205	if (p && (p->p_flag & P_ADVLOCK) && fp->f_type == DTYPE_VNODE) {
1206		lf.l_whence = SEEK_SET;
1207		lf.l_start = 0;
1208		lf.l_len = 0;
1209		lf.l_type = F_UNLCK;
1210		vp = (struct vnode *)fp->f_data;
1211		(void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK, &lf, F_POSIX);
1212	}
1213	return (fdrop(fp, p));
1214}
1215
1216int
1217fdrop(fp, p)
1218	struct file *fp;
1219	struct proc *p;
1220{
1221	struct flock lf;
1222	struct vnode *vp;
1223	int error;
1224
1225	if (--fp->f_count > 0)
1226		return (0);
1227	if (fp->f_count < 0)
1228		panic("fdrop: count < 0");
1229	if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
1230		lf.l_whence = SEEK_SET;
1231		lf.l_start = 0;
1232		lf.l_len = 0;
1233		lf.l_type = F_UNLCK;
1234		vp = (struct vnode *)fp->f_data;
1235		(void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
1236	}
1237	if (fp->f_ops != &badfileops)
1238		error = fo_close(fp, p);
1239	else
1240		error = 0;
1241	ffree(fp);
1242	return (error);
1243}
1244
1245/*
1246 * Apply an advisory lock on a file descriptor.
1247 *
1248 * Just attempt to get a record lock of the requested type on
1249 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
1250 */
1251#ifndef _SYS_SYSPROTO_H_
1252struct flock_args {
1253	int	fd;
1254	int	how;
1255};
1256#endif
1257/* ARGSUSED */
1258int
1259flock(p, uap)
1260	struct proc *p;
1261	register struct flock_args *uap;
1262{
1263	register struct filedesc *fdp = p->p_fd;
1264	register struct file *fp;
1265	struct vnode *vp;
1266	struct flock lf;
1267
1268	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
1269	    (fp = fdp->fd_ofiles[uap->fd]) == NULL)
1270		return (EBADF);
1271	if (fp->f_type != DTYPE_VNODE)
1272		return (EOPNOTSUPP);
1273	vp = (struct vnode *)fp->f_data;
1274	lf.l_whence = SEEK_SET;
1275	lf.l_start = 0;
1276	lf.l_len = 0;
1277	if (uap->how & LOCK_UN) {
1278		lf.l_type = F_UNLCK;
1279		fp->f_flag &= ~FHASLOCK;
1280		return (VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK));
1281	}
1282	if (uap->how & LOCK_EX)
1283		lf.l_type = F_WRLCK;
1284	else if (uap->how & LOCK_SH)
1285		lf.l_type = F_RDLCK;
1286	else
1287		return (EBADF);
1288	fp->f_flag |= FHASLOCK;
1289	if (uap->how & LOCK_NB)
1290		return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK));
1291	return (VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT));
1292}
1293
1294/*
1295 * File Descriptor pseudo-device driver (/dev/fd/).
1296 *
1297 * Opening minor device N dup()s the file (if any) connected to file
1298 * descriptor N belonging to the calling process.  Note that this driver
1299 * consists of only the ``open()'' routine, because all subsequent
1300 * references to this file will be direct to the other driver.
1301 */
1302/* ARGSUSED */
1303static int
1304fdopen(dev, mode, type, p)
1305	dev_t dev;
1306	int mode, type;
1307	struct proc *p;
1308{
1309
1310	/*
1311	 * XXX Kludge: set curproc->p_dupfd to contain the value of the
1312	 * the file descriptor being sought for duplication. The error
1313	 * return ensures that the vnode for this device will be released
1314	 * by vn_open. Open will detect this special error and take the
1315	 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
1316	 * will simply report the error.
1317	 */
1318	p->p_dupfd = dev2unit(dev);
1319	return (ENODEV);
1320}
1321
1322/*
1323 * Duplicate the specified descriptor to a free descriptor.
1324 */
1325int
1326dupfdopen(p, fdp, indx, dfd, mode, error)
1327	struct proc *p;
1328	struct filedesc *fdp;
1329	int indx, dfd;
1330	int mode;
1331	int error;
1332{
1333	register struct file *wfp;
1334	struct file *fp;
1335
1336	/*
1337	 * If the to-be-dup'd fd number is greater than the allowed number
1338	 * of file descriptors, or the fd to be dup'd has already been
1339	 * closed, then reject.
1340	 */
1341	if ((u_int)dfd >= fdp->fd_nfiles ||
1342	    (wfp = fdp->fd_ofiles[dfd]) == NULL) {
1343		return (EBADF);
1344	}
1345
1346	/*
1347	 * There are two cases of interest here.
1348	 *
1349	 * For ENODEV simply dup (dfd) to file descriptor
1350	 * (indx) and return.
1351	 *
1352	 * For ENXIO steal away the file structure from (dfd) and
1353	 * store it in (indx).  (dfd) is effectively closed by
1354	 * this operation.
1355	 *
1356	 * Any other error code is just returned.
1357	 */
1358	switch (error) {
1359	case ENODEV:
1360		/*
1361		 * Check that the mode the file is being opened for is a
1362		 * subset of the mode of the existing descriptor.
1363		 */
1364		if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag)
1365			return (EACCES);
1366		fp = fdp->fd_ofiles[indx];
1367#if 0
1368		if (fp && fdp->fd_ofileflags[indx] & UF_MAPPED)
1369			(void) munmapfd(p, indx);
1370#endif
1371		fdp->fd_ofiles[indx] = wfp;
1372		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1373		fhold(wfp);
1374		if (indx > fdp->fd_lastfile)
1375			fdp->fd_lastfile = indx;
1376		/*
1377		 * we now own the reference to fp that the ofiles[] array
1378		 * used to own.  Release it.
1379		 */
1380		if (fp)
1381			fdrop(fp, p);
1382		return (0);
1383
1384	case ENXIO:
1385		/*
1386		 * Steal away the file pointer from dfd, and stuff it into indx.
1387		 */
1388		fp = fdp->fd_ofiles[indx];
1389#if 0
1390		if (fp && fdp->fd_ofileflags[indx] & UF_MAPPED)
1391			(void) munmapfd(p, indx);
1392#endif
1393		fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
1394		fdp->fd_ofiles[dfd] = NULL;
1395		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1396		fdp->fd_ofileflags[dfd] = 0;
1397
1398		/*
1399		 * we now own the reference to fp that the ofiles[] array
1400		 * used to own.  Release it.
1401		 */
1402		if (fp)
1403			fdrop(fp, p);
1404		/*
1405		 * Complete the clean up of the filedesc structure by
1406		 * recomputing the various hints.
1407		 */
1408		if (indx > fdp->fd_lastfile) {
1409			fdp->fd_lastfile = indx;
1410		} else {
1411			while (fdp->fd_lastfile > 0 &&
1412			   fdp->fd_ofiles[fdp->fd_lastfile] == NULL) {
1413				fdp->fd_lastfile--;
1414			}
1415			if (dfd < fdp->fd_freefile)
1416				fdp->fd_freefile = dfd;
1417		}
1418		return (0);
1419
1420	default:
1421		return (error);
1422	}
1423	/* NOTREACHED */
1424}
1425
1426/*
1427 * Get file structures.
1428 */
1429static int
1430sysctl_kern_file(SYSCTL_HANDLER_ARGS)
1431{
1432	int error;
1433	struct file *fp;
1434
1435	if (!req->oldptr) {
1436		/*
1437		 * overestimate by 10 files
1438		 */
1439		return (SYSCTL_OUT(req, 0, sizeof(filehead) +
1440				(nfiles + 10) * sizeof(struct file)));
1441	}
1442
1443	error = SYSCTL_OUT(req, (caddr_t)&filehead, sizeof(filehead));
1444	if (error)
1445		return (error);
1446
1447	/*
1448	 * followed by an array of file structures
1449	 */
1450	LIST_FOREACH(fp, &filehead, f_list) {
1451		error = SYSCTL_OUT(req, (caddr_t)fp, sizeof (struct file));
1452		if (error)
1453			return (error);
1454	}
1455	return (0);
1456}
1457
1458SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
1459    0, 0, sysctl_kern_file, "S,file", "Entire file table");
1460
1461SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
1462    &maxfilesperproc, 0, "Maximum files allowed open per process");
1463
1464SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
1465    &maxfiles, 0, "Maximum number of files");
1466
1467SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD,
1468    &nfiles, 0, "System-wide number of open files");
1469
1470static void
1471fildesc_clone(void *arg, char *name, int namelen, dev_t *dev)
1472{
1473	int u;
1474
1475	if (*dev != NODEV)
1476		return;
1477	if (dev_stdclone(name, NULL, "fd/", &u) != 1)
1478		return;
1479	if (u <= 2)
1480		return;
1481	/* Don't clone higher than it makes sense */
1482	if (u >= maxfilesperproc)
1483		return;
1484	/* And don't clone higher than our minors will support */
1485	if (u > 0xffffff)
1486		return;
1487	u = unit2minor(u);
1488	*dev = make_dev(&fildesc_cdevsw, u, UID_BIN, GID_BIN, 0666, name);
1489	return;
1490}
1491
1492static void
1493fildesc_drvinit(void *unused)
1494{
1495	dev_t dev;
1496
1497	dev = make_dev(&fildesc_cdevsw, 0, UID_BIN, GID_BIN, 0666, "fd/0");
1498	make_dev_alias(dev, "stdin");
1499	dev = make_dev(&fildesc_cdevsw, 1, UID_BIN, GID_BIN, 0666, "fd/1");
1500	make_dev_alias(dev, "stdout");
1501	dev = make_dev(&fildesc_cdevsw, 2, UID_BIN, GID_BIN, 0666, "fd/2");
1502	make_dev_alias(dev, "stderr");
1503	EVENTHANDLER_REGISTER(dev_clone, fildesc_clone, 0, 1000);
1504	if (!devfs_present) {
1505		int fd;
1506
1507		for (fd = 3; fd < NUMFDESC; fd++)
1508			make_dev(&fildesc_cdevsw, fd, UID_BIN, GID_BIN, 0666,
1509			    "fd/%d", fd);
1510	}
1511}
1512
1513struct fileops badfileops = {
1514	badfo_readwrite,
1515	badfo_readwrite,
1516	badfo_ioctl,
1517	badfo_poll,
1518	badfo_kqfilter,
1519	badfo_stat,
1520	badfo_close
1521};
1522
1523static int
1524badfo_readwrite(fp, uio, cred, flags, p)
1525	struct file *fp;
1526	struct uio *uio;
1527	struct ucred *cred;
1528	struct proc *p;
1529	int flags;
1530{
1531
1532	return (EBADF);
1533}
1534
1535static int
1536badfo_ioctl(fp, com, data, p)
1537	struct file *fp;
1538	u_long com;
1539	caddr_t data;
1540	struct proc *p;
1541{
1542
1543	return (EBADF);
1544}
1545
1546static int
1547badfo_poll(fp, events, cred, p)
1548	struct file *fp;
1549	int events;
1550	struct ucred *cred;
1551	struct proc *p;
1552{
1553
1554	return (0);
1555}
1556
1557static int
1558badfo_kqfilter(fp, kn)
1559	struct file *fp;
1560	struct knote *kn;
1561{
1562
1563	return (0);
1564}
1565
1566static int
1567badfo_stat(fp, sb, p)
1568	struct file *fp;
1569	struct stat *sb;
1570	struct proc *p;
1571{
1572
1573	return (EBADF);
1574}
1575
1576static int
1577badfo_close(fp, p)
1578	struct file *fp;
1579	struct proc *p;
1580{
1581
1582	return (EBADF);
1583}
1584
1585SYSINIT(fildescdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,
1586					fildesc_drvinit,NULL)
1587