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