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