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