xref: /freebsd/sys/kern/kern_descrip.c (revision 41466b50c1d5bfd1cf6adaae547a579a75d7c04e)
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$
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 
64 #include <machine/limits.h>
65 
66 #include <vm/vm.h>
67 #include <vm/vm_extern.h>
68 
69 static MALLOC_DEFINE(M_FILEDESC, "file desc", "Open file descriptor table");
70 MALLOC_DEFINE(M_FILE, "file", "Open file structure");
71 static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures");
72 
73 static	 d_open_t  fdopen;
74 #define NUMFDESC 64
75 
76 #define CDEV_MAJOR 22
77 static 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 
93 static int do_dup __P((struct filedesc *fdp, int old, int new, register_t *retval, struct thread *td));
94 static int badfo_readwrite __P((struct file *fp, struct uio *uio,
95     struct ucred *cred, int flags, struct thread *td));
96 static int badfo_ioctl __P((struct file *fp, u_long com, caddr_t data,
97     struct thread *td));
98 static int badfo_poll __P((struct file *fp, int events,
99     struct ucred *cred, struct thread *td));
100 static int badfo_kqfilter __P((struct file *fp, struct knote *kn));
101 static int badfo_stat __P((struct file *fp, struct stat *sb, struct thread *td));
102 static int badfo_close __P((struct file *fp, struct thread *td));
103 
104 /*
105  * Descriptor management.
106  */
107 struct filelist filehead;	/* head of list of open files */
108 int nfiles;			/* actual number of open files */
109 extern int cmask;
110 
111 /*
112  * System calls on descriptors.
113  */
114 #ifndef _SYS_SYSPROTO_H_
115 struct getdtablesize_args {
116 	int	dummy;
117 };
118 #endif
119 /*
120  * MPSAFE
121  */
122 /* ARGSUSED */
123 int
124 getdtablesize(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_
144 struct dup2_args {
145 	u_int	from;
146 	u_int	to;
147 };
148 #endif
149 /*
150  * MPSAFE
151  */
152 /* ARGSUSED */
153 int
154 dup2(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);
164 retry:
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);
188 done2:
189 	mtx_unlock(&Giant);
190 	return(error);
191 }
192 
193 /*
194  * Duplicate a file descriptor.
195  */
196 #ifndef _SYS_SYSPROTO_H_
197 struct dup_args {
198 	u_int	fd;
199 };
200 #endif
201 /*
202  * MPSAFE
203  */
204 /* ARGSUSED */
205 int
206 dup(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);
224 done2:
225 	mtx_unlock(&Giant);
226 	return (error);
227 }
228 
229 /*
230  * The file control system call.
231  */
232 #ifndef _SYS_SYSPROTO_H_
233 struct fcntl_args {
234 	int	fd;
235 	int	cmd;
236 	long	arg;
237 };
238 #endif
239 /*
240  * MPSAFE
241  */
242 /* ARGSUSED */
243 int
244 fcntl(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 	}
435 done2:
436 	mtx_unlock(&Giant);
437 	return (error);
438 }
439 
440 /*
441  * Common code for dup, dup2, and fcntl(F_DUPFD).
442  */
443 static int
444 do_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  */
490 void
491 funsetown(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. */
513 void
514 funsetownlst(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  */
529 int
530 fsetown(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 	sigio->sio_ucred = crhold(curthread->td_proc->p_ucred);
592 	sigio->sio_myref = sigiop;
593 	s = splhigh();
594 	*sigiop = sigio;
595 	splx(s);
596 	return (0);
597 }
598 
599 /*
600  * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
601  */
602 pid_t
603 fgetown(sigio)
604 	struct sigio *sigio;
605 {
606 	return (sigio != NULL ? sigio->sio_pgid : 0);
607 }
608 
609 /*
610  * Close a file descriptor.
611  */
612 #ifndef _SYS_SYSPROTO_H_
613 struct close_args {
614         int     fd;
615 };
616 #endif
617 /*
618  * MPSAFE
619  */
620 /* ARGSUSED */
621 int
622 close(td, uap)
623 	struct thread *td;
624 	struct close_args *uap;
625 {
626 	register struct filedesc *fdp;
627 	register struct file *fp;
628 	register int fd = uap->fd;
629 	int error = 0;
630 
631 	mtx_lock(&Giant);
632 	fdp = td->td_proc->p_fd;
633 	if ((unsigned)fd >= fdp->fd_nfiles ||
634 	    (fp = fdp->fd_ofiles[fd]) == NULL) {
635 		error = EBADF;
636 		goto done2;
637 	}
638 #if 0
639 	if (fdp->fd_ofileflags[fd] & UF_MAPPED)
640 		(void) munmapfd(td, fd);
641 #endif
642 	fdp->fd_ofiles[fd] = NULL;
643 	fdp->fd_ofileflags[fd] = 0;
644 
645 	/*
646 	 * we now hold the fp reference that used to be owned by the descriptor
647 	 * array.
648 	 */
649 	while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
650 		fdp->fd_lastfile--;
651 	if (fd < fdp->fd_freefile)
652 		fdp->fd_freefile = fd;
653 	if (fd < fdp->fd_knlistsize)
654 		knote_fdclose(td, fd);
655 	error = closef(fp, td);
656 done2:
657 	mtx_unlock(&Giant);
658 	return(error);
659 }
660 
661 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
662 /*
663  * Return status information about a file descriptor.
664  */
665 #ifndef _SYS_SYSPROTO_H_
666 struct ofstat_args {
667 	int	fd;
668 	struct	ostat *sb;
669 };
670 #endif
671 /*
672  * MPSAFE
673  */
674 /* ARGSUSED */
675 int
676 ofstat(td, uap)
677 	struct thread *td;
678 	register struct ofstat_args *uap;
679 {
680 	register struct filedesc *fdp = td->td_proc->p_fd;
681 	register struct file *fp;
682 	struct stat ub;
683 	struct ostat oub;
684 	int error;
685 
686 	mtx_lock(&Giant);
687 
688 	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
689 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL) {
690 		error = EBADF;
691 		goto done2;
692 	}
693 	fhold(fp);
694 	error = fo_stat(fp, &ub, td);
695 	if (error == 0) {
696 		cvtstat(&ub, &oub);
697 		error = copyout((caddr_t)&oub, (caddr_t)uap->sb, sizeof (oub));
698 	}
699 	fdrop(fp, td);
700 done2:
701 	mtx_unlock(&Giant);
702 	return (error);
703 }
704 #endif /* COMPAT_43 || COMPAT_SUNOS */
705 
706 /*
707  * Return status information about a file descriptor.
708  */
709 #ifndef _SYS_SYSPROTO_H_
710 struct fstat_args {
711 	int	fd;
712 	struct	stat *sb;
713 };
714 #endif
715 /*
716  * MPSAFE
717  */
718 /* ARGSUSED */
719 int
720 fstat(td, uap)
721 	struct thread *td;
722 	register struct fstat_args *uap;
723 {
724 	register struct filedesc *fdp;
725 	register struct file *fp;
726 	struct stat ub;
727 	int error;
728 
729 	mtx_lock(&Giant);
730 	fdp = td->td_proc->p_fd;
731 
732 	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
733 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL) {
734 		error = EBADF;
735 		goto done2;
736 	}
737 	fhold(fp);
738 	error = fo_stat(fp, &ub, td);
739 	if (error == 0)
740 		error = copyout((caddr_t)&ub, (caddr_t)uap->sb, sizeof (ub));
741 	fdrop(fp, td);
742 done2:
743 	mtx_unlock(&Giant);
744 	return (error);
745 }
746 
747 /*
748  * Return status information about a file descriptor.
749  */
750 #ifndef _SYS_SYSPROTO_H_
751 struct nfstat_args {
752 	int	fd;
753 	struct	nstat *sb;
754 };
755 #endif
756 /*
757  * MPSAFE
758  */
759 /* ARGSUSED */
760 int
761 nfstat(td, uap)
762 	struct thread *td;
763 	register struct nfstat_args *uap;
764 {
765 	register struct filedesc *fdp;
766 	register struct file *fp;
767 	struct stat ub;
768 	struct nstat nub;
769 	int error;
770 
771 	mtx_lock(&Giant);
772 
773 	fdp = td->td_proc->p_fd;
774 	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
775 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL) {
776 		error = EBADF;
777 		goto done2;
778 	}
779 	fhold(fp);
780 	error = fo_stat(fp, &ub, td);
781 	if (error == 0) {
782 		cvtnstat(&ub, &nub);
783 		error = copyout((caddr_t)&nub, (caddr_t)uap->sb, sizeof (nub));
784 	}
785 	fdrop(fp, td);
786 done2:
787 	mtx_unlock(&Giant);
788 	return (error);
789 }
790 
791 /*
792  * Return pathconf information about a file descriptor.
793  */
794 #ifndef _SYS_SYSPROTO_H_
795 struct fpathconf_args {
796 	int	fd;
797 	int	name;
798 };
799 #endif
800 /*
801  * MPSAFE
802  */
803 /* ARGSUSED */
804 int
805 fpathconf(td, uap)
806 	struct thread *td;
807 	register struct fpathconf_args *uap;
808 {
809 	struct filedesc *fdp;
810 	struct file *fp;
811 	struct vnode *vp;
812 	int error = 0;
813 
814 	mtx_lock(&Giant);
815 	fdp = td->td_proc->p_fd;
816 
817 	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
818 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL) {
819 		error = EBADF;
820 		goto done2;
821 	}
822 
823 	fhold(fp);
824 
825 	switch (fp->f_type) {
826 	case DTYPE_PIPE:
827 	case DTYPE_SOCKET:
828 		if (uap->name != _PC_PIPE_BUF) {
829 			error = EINVAL;
830 			goto done2;
831 		}
832 		td->td_retval[0] = PIPE_BUF;
833 		error = 0;
834 		break;
835 	case DTYPE_FIFO:
836 	case DTYPE_VNODE:
837 		vp = (struct vnode *)fp->f_data;
838 		error = VOP_PATHCONF(vp, uap->name, td->td_retval);
839 		break;
840 	default:
841 		error = EOPNOTSUPP;
842 		break;
843 	}
844 	fdrop(fp, td);
845 done2:
846 	mtx_unlock(&Giant);
847 	return(error);
848 }
849 
850 /*
851  * Allocate a file descriptor for the process.
852  */
853 static int fdexpand;
854 SYSCTL_INT(_debug, OID_AUTO, fdexpand, CTLFLAG_RD, &fdexpand, 0, "");
855 
856 int
857 fdalloc(td, want, result)
858 	struct thread *td;
859 	int want;
860 	int *result;
861 {
862 	struct proc *p = td->td_proc;
863 	register struct filedesc *fdp = td->td_proc->p_fd;
864 	register int i;
865 	int lim, last, nfiles;
866 	struct file **newofile;
867 	char *newofileflags;
868 
869 	/*
870 	 * Search for a free descriptor starting at the higher
871 	 * of want or fd_freefile.  If that fails, consider
872 	 * expanding the ofile array.
873 	 */
874 	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
875 	for (;;) {
876 		last = min(fdp->fd_nfiles, lim);
877 		if ((i = want) < fdp->fd_freefile)
878 			i = fdp->fd_freefile;
879 		for (; i < last; i++) {
880 			if (fdp->fd_ofiles[i] == NULL) {
881 				fdp->fd_ofileflags[i] = 0;
882 				if (i > fdp->fd_lastfile)
883 					fdp->fd_lastfile = i;
884 				if (want <= fdp->fd_freefile)
885 					fdp->fd_freefile = i;
886 				*result = i;
887 				return (0);
888 			}
889 		}
890 
891 		/*
892 		 * No space in current array.  Expand?
893 		 */
894 		if (fdp->fd_nfiles >= lim)
895 			return (EMFILE);
896 		if (fdp->fd_nfiles < NDEXTENT)
897 			nfiles = NDEXTENT;
898 		else
899 			nfiles = 2 * fdp->fd_nfiles;
900 		MALLOC(newofile, struct file **, nfiles * OFILESIZE,
901 		    M_FILEDESC, M_WAITOK);
902 
903 		/*
904 		 * deal with file-table extend race that might have occured
905 		 * when malloc was blocked.
906 		 */
907 		if (fdp->fd_nfiles >= nfiles) {
908 			FREE(newofile, M_FILEDESC);
909 			continue;
910 		}
911 		newofileflags = (char *) &newofile[nfiles];
912 		/*
913 		 * Copy the existing ofile and ofileflags arrays
914 		 * and zero the new portion of each array.
915 		 */
916 		bcopy(fdp->fd_ofiles, newofile,
917 			(i = sizeof(struct file *) * fdp->fd_nfiles));
918 		bzero((char *)newofile + i, nfiles * sizeof(struct file *) - i);
919 		bcopy(fdp->fd_ofileflags, newofileflags,
920 			(i = sizeof(char) * fdp->fd_nfiles));
921 		bzero(newofileflags + i, nfiles * sizeof(char) - i);
922 		if (fdp->fd_nfiles > NDFILE)
923 			FREE(fdp->fd_ofiles, M_FILEDESC);
924 		fdp->fd_ofiles = newofile;
925 		fdp->fd_ofileflags = newofileflags;
926 		fdp->fd_nfiles = nfiles;
927 		fdexpand++;
928 	}
929 	return (0);
930 }
931 
932 /*
933  * Check to see whether n user file descriptors
934  * are available to the process p.
935  */
936 int
937 fdavail(td, n)
938 	struct thread *td;
939 	register int n;
940 {
941 	struct proc *p = td->td_proc;
942 	register struct filedesc *fdp = td->td_proc->p_fd;
943 	register struct file **fpp;
944 	register int i, lim, last;
945 
946 	lim = min((int)p->p_rlimit[RLIMIT_NOFILE].rlim_cur, maxfilesperproc);
947 	if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
948 		return (1);
949 
950 	last = min(fdp->fd_nfiles, lim);
951 	fpp = &fdp->fd_ofiles[fdp->fd_freefile];
952 	for (i = last - fdp->fd_freefile; --i >= 0; fpp++) {
953 		if (*fpp == NULL && --n <= 0)
954 			return (1);
955 	}
956 	return (0);
957 }
958 
959 /*
960  * Create a new open file structure and allocate
961  * a file decriptor for the process that refers to it.
962  */
963 int
964 falloc(td, resultfp, resultfd)
965 	register struct thread *td;
966 	struct file **resultfp;
967 	int *resultfd;
968 {
969 	struct proc *p = td->td_proc;
970 	register struct file *fp, *fq;
971 	int error, i;
972 
973 	if (nfiles >= maxfiles) {
974 		tablefull("file");
975 		return (ENFILE);
976 	}
977 	/*
978 	 * Allocate a new file descriptor.
979 	 * If the process has file descriptor zero open, add to the list
980 	 * of open files at that point, otherwise put it at the front of
981 	 * the list of open files.
982 	 */
983 	nfiles++;
984 	MALLOC(fp, struct file *, sizeof(struct file), M_FILE, M_WAITOK | M_ZERO);
985 
986 	/*
987 	 * wait until after malloc (which may have blocked) returns before
988 	 * allocating the slot, else a race might have shrunk it if we had
989 	 * allocated it before the malloc.
990 	 */
991 	if ((error = fdalloc(td, 0, &i))) {
992 		nfiles--;
993 		FREE(fp, M_FILE);
994 		return (error);
995 	}
996 	fp->f_count = 1;
997 	fp->f_cred = crhold(p->p_ucred);
998 	fp->f_ops = &badfileops;
999 	fp->f_seqcount = 1;
1000 	if ((fq = p->p_fd->fd_ofiles[0])) {
1001 		LIST_INSERT_AFTER(fq, fp, f_list);
1002 	} else {
1003 		LIST_INSERT_HEAD(&filehead, fp, f_list);
1004 	}
1005 	p->p_fd->fd_ofiles[i] = fp;
1006 	if (resultfp)
1007 		*resultfp = fp;
1008 	if (resultfd)
1009 		*resultfd = i;
1010 	return (0);
1011 }
1012 
1013 /*
1014  * Free a file descriptor.
1015  */
1016 void
1017 ffree(fp)
1018 	register struct file *fp;
1019 {
1020 	KASSERT((fp->f_count == 0), ("ffree: fp_fcount not 0!"));
1021 	LIST_REMOVE(fp, f_list);
1022 	crfree(fp->f_cred);
1023 	nfiles--;
1024 	FREE(fp, M_FILE);
1025 }
1026 
1027 /*
1028  * Build a new filedesc structure.
1029  */
1030 struct filedesc *
1031 fdinit(td)
1032 	struct thread *td;
1033 {
1034 	register struct filedesc0 *newfdp;
1035 	register struct filedesc *fdp = td->td_proc->p_fd;
1036 
1037 	MALLOC(newfdp, struct filedesc0 *, sizeof(struct filedesc0),
1038 	    M_FILEDESC, M_WAITOK | M_ZERO);
1039 	newfdp->fd_fd.fd_cdir = fdp->fd_cdir;
1040 	if (newfdp->fd_fd.fd_cdir)
1041 		VREF(newfdp->fd_fd.fd_cdir);
1042 	newfdp->fd_fd.fd_rdir = fdp->fd_rdir;
1043 	if (newfdp->fd_fd.fd_rdir)
1044 		VREF(newfdp->fd_fd.fd_rdir);
1045 	newfdp->fd_fd.fd_jdir = fdp->fd_jdir;
1046 	if (newfdp->fd_fd.fd_jdir)
1047 		VREF(newfdp->fd_fd.fd_jdir);
1048 
1049 	/* Create the file descriptor table. */
1050 	newfdp->fd_fd.fd_refcnt = 1;
1051 	newfdp->fd_fd.fd_cmask = cmask;
1052 	newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
1053 	newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
1054 	newfdp->fd_fd.fd_nfiles = NDFILE;
1055 	newfdp->fd_fd.fd_knlistsize = -1;
1056 
1057 	return (&newfdp->fd_fd);
1058 }
1059 
1060 /*
1061  * Share a filedesc structure.
1062  */
1063 struct filedesc *
1064 fdshare(p)
1065 	struct proc *p;
1066 {
1067 	p->p_fd->fd_refcnt++;
1068 	return (p->p_fd);
1069 }
1070 
1071 /*
1072  * Copy a filedesc structure.
1073  */
1074 struct filedesc *
1075 fdcopy(td)
1076 	struct thread *td;
1077 {
1078 	register struct filedesc *newfdp, *fdp = td->td_proc->p_fd;
1079 	register struct file **fpp;
1080 	register int i;
1081 
1082 	/* Certain daemons might not have file descriptors. */
1083 	if (fdp == NULL)
1084 		return (NULL);
1085 
1086 	MALLOC(newfdp, struct filedesc *, sizeof(struct filedesc0),
1087 	    M_FILEDESC, M_WAITOK);
1088 	bcopy(fdp, newfdp, sizeof(struct filedesc));
1089 	if (newfdp->fd_cdir)
1090 		VREF(newfdp->fd_cdir);
1091 	if (newfdp->fd_rdir)
1092 		VREF(newfdp->fd_rdir);
1093 	if (newfdp->fd_jdir)
1094 		VREF(newfdp->fd_jdir);
1095 	newfdp->fd_refcnt = 1;
1096 
1097 	/*
1098 	 * If the number of open files fits in the internal arrays
1099 	 * of the open file structure, use them, otherwise allocate
1100 	 * additional memory for the number of descriptors currently
1101 	 * in use.
1102 	 */
1103 	if (newfdp->fd_lastfile < NDFILE) {
1104 		newfdp->fd_ofiles = ((struct filedesc0 *) newfdp)->fd_dfiles;
1105 		newfdp->fd_ofileflags =
1106 		    ((struct filedesc0 *) newfdp)->fd_dfileflags;
1107 		i = NDFILE;
1108 	} else {
1109 		/*
1110 		 * Compute the smallest multiple of NDEXTENT needed
1111 		 * for the file descriptors currently in use,
1112 		 * allowing the table to shrink.
1113 		 */
1114 		i = newfdp->fd_nfiles;
1115 		while (i > 2 * NDEXTENT && i > newfdp->fd_lastfile * 2)
1116 			i /= 2;
1117 		MALLOC(newfdp->fd_ofiles, struct file **, i * OFILESIZE,
1118 		    M_FILEDESC, M_WAITOK);
1119 		newfdp->fd_ofileflags = (char *) &newfdp->fd_ofiles[i];
1120 	}
1121 	newfdp->fd_nfiles = i;
1122 	bcopy(fdp->fd_ofiles, newfdp->fd_ofiles, i * sizeof(struct file **));
1123 	bcopy(fdp->fd_ofileflags, newfdp->fd_ofileflags, i * sizeof(char));
1124 
1125 	/*
1126 	 * kq descriptors cannot be copied.
1127 	 */
1128 	if (newfdp->fd_knlistsize != -1) {
1129 		fpp = newfdp->fd_ofiles;
1130 		for (i = newfdp->fd_lastfile; i-- >= 0; fpp++) {
1131 			if (*fpp != NULL && (*fpp)->f_type == DTYPE_KQUEUE)
1132 				*fpp = NULL;
1133 		}
1134 		newfdp->fd_knlist = NULL;
1135 		newfdp->fd_knlistsize = -1;
1136 		newfdp->fd_knhash = NULL;
1137 		newfdp->fd_knhashmask = 0;
1138 	}
1139 
1140 	fpp = newfdp->fd_ofiles;
1141 	for (i = newfdp->fd_lastfile; i-- >= 0; fpp++) {
1142 		if (*fpp != NULL)
1143 			fhold(*fpp);
1144 	}
1145 	return (newfdp);
1146 }
1147 
1148 /*
1149  * Release a filedesc structure.
1150  */
1151 void
1152 fdfree(td)
1153 	struct thread *td;
1154 {
1155 	register struct filedesc *fdp = td->td_proc->p_fd;
1156 	struct file **fpp;
1157 	register int i;
1158 
1159 	/* Certain daemons might not have file descriptors. */
1160 	if (fdp == NULL)
1161 		return;
1162 
1163 	if (--fdp->fd_refcnt > 0)
1164 		return;
1165 	/*
1166 	 * we are the last reference to the structure, we can
1167 	 * safely assume it will not change out from under us.
1168 	 */
1169 	fpp = fdp->fd_ofiles;
1170 	for (i = fdp->fd_lastfile; i-- >= 0; fpp++) {
1171 		if (*fpp)
1172 			(void) closef(*fpp, td);
1173 	}
1174 	if (fdp->fd_nfiles > NDFILE)
1175 		FREE(fdp->fd_ofiles, M_FILEDESC);
1176 	if (fdp->fd_cdir)
1177 		vrele(fdp->fd_cdir);
1178 	if (fdp->fd_rdir)
1179 		vrele(fdp->fd_rdir);
1180 	if (fdp->fd_jdir)
1181 		vrele(fdp->fd_jdir);
1182 	if (fdp->fd_knlist)
1183 		FREE(fdp->fd_knlist, M_KQUEUE);
1184 	if (fdp->fd_knhash)
1185 		FREE(fdp->fd_knhash, M_KQUEUE);
1186 	FREE(fdp, M_FILEDESC);
1187 }
1188 
1189 /*
1190  * For setugid programs, we don't want to people to use that setugidness
1191  * to generate error messages which write to a file which otherwise would
1192  * otherwise be off-limits to the process.
1193  *
1194  * This is a gross hack to plug the hole.  A better solution would involve
1195  * a special vop or other form of generalized access control mechanism.  We
1196  * go ahead and just reject all procfs file systems accesses as dangerous.
1197  *
1198  * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
1199  * sufficient.  We also don't for check setugidness since we know we are.
1200  */
1201 static int
1202 is_unsafe(struct file *fp)
1203 {
1204 	if (fp->f_type == DTYPE_VNODE &&
1205 	    ((struct vnode *)(fp->f_data))->v_tag == VT_PROCFS)
1206 		return (1);
1207 	return (0);
1208 }
1209 
1210 /*
1211  * Make this setguid thing safe, if at all possible.
1212  */
1213 void
1214 setugidsafety(td)
1215 	struct thread *td;
1216 {
1217 	struct filedesc *fdp = td->td_proc->p_fd;
1218 	register int i;
1219 
1220 	/* Certain daemons might not have file descriptors. */
1221 	if (fdp == NULL)
1222 		return;
1223 
1224 	/*
1225 	 * note: fdp->fd_ofiles may be reallocated out from under us while
1226 	 * we are blocked in a close.  Be careful!
1227 	 */
1228 	for (i = 0; i <= fdp->fd_lastfile; i++) {
1229 		if (i > 2)
1230 			break;
1231 		if (fdp->fd_ofiles[i] && is_unsafe(fdp->fd_ofiles[i])) {
1232 			struct file *fp;
1233 
1234 #if 0
1235 			if ((fdp->fd_ofileflags[i] & UF_MAPPED) != 0)
1236 				(void) munmapfd(td, i);
1237 #endif
1238 			if (i < fdp->fd_knlistsize)
1239 				knote_fdclose(td, i);
1240 			/*
1241 			 * NULL-out descriptor prior to close to avoid
1242 			 * a race while close blocks.
1243 			 */
1244 			fp = fdp->fd_ofiles[i];
1245 			fdp->fd_ofiles[i] = NULL;
1246 			fdp->fd_ofileflags[i] = 0;
1247 			if (i < fdp->fd_freefile)
1248 				fdp->fd_freefile = i;
1249 			(void) closef(fp, td);
1250 		}
1251 	}
1252 	while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
1253 		fdp->fd_lastfile--;
1254 }
1255 
1256 /*
1257  * Close any files on exec?
1258  */
1259 void
1260 fdcloseexec(td)
1261 	struct thread *td;
1262 {
1263 	struct filedesc *fdp = td->td_proc->p_fd;
1264 	register int i;
1265 
1266 	/* Certain daemons might not have file descriptors. */
1267 	if (fdp == NULL)
1268 		return;
1269 
1270 	/*
1271 	 * We cannot cache fd_ofiles or fd_ofileflags since operations
1272 	 * may block and rip them out from under us.
1273 	 */
1274 	for (i = 0; i <= fdp->fd_lastfile; i++) {
1275 		if (fdp->fd_ofiles[i] != NULL &&
1276 		    (fdp->fd_ofileflags[i] & UF_EXCLOSE)) {
1277 			struct file *fp;
1278 
1279 #if 0
1280 			if (fdp->fd_ofileflags[i] & UF_MAPPED)
1281 				(void) munmapfd(td, i);
1282 #endif
1283 			if (i < fdp->fd_knlistsize)
1284 				knote_fdclose(td, i);
1285 			/*
1286 			 * NULL-out descriptor prior to close to avoid
1287 			 * a race while close blocks.
1288 			 */
1289 			fp = fdp->fd_ofiles[i];
1290 			fdp->fd_ofiles[i] = NULL;
1291 			fdp->fd_ofileflags[i] = 0;
1292 			if (i < fdp->fd_freefile)
1293 				fdp->fd_freefile = i;
1294 			(void) closef(fp, td);
1295 		}
1296 	}
1297 	while (fdp->fd_lastfile > 0 && fdp->fd_ofiles[fdp->fd_lastfile] == NULL)
1298 		fdp->fd_lastfile--;
1299 }
1300 
1301 /*
1302  * Internal form of close.
1303  * Decrement reference count on file structure.
1304  * Note: td may be NULL when closing a file
1305  * that was being passed in a message.
1306  */
1307 int
1308 closef(fp, td)
1309 	register struct file *fp;
1310 	register struct thread *td;
1311 {
1312 	struct vnode *vp;
1313 	struct flock lf;
1314 
1315 	if (fp == NULL)
1316 		return (0);
1317 	/*
1318 	 * POSIX record locking dictates that any close releases ALL
1319 	 * locks owned by this process.  This is handled by setting
1320 	 * a flag in the unlock to free ONLY locks obeying POSIX
1321 	 * semantics, and not to free BSD-style file locks.
1322 	 * If the descriptor was in a message, POSIX-style locks
1323 	 * aren't passed with the descriptor.
1324 	 */
1325 	if (td && (td->td_proc->p_flag & P_ADVLOCK) &&
1326 	    fp->f_type == DTYPE_VNODE) {
1327 		lf.l_whence = SEEK_SET;
1328 		lf.l_start = 0;
1329 		lf.l_len = 0;
1330 		lf.l_type = F_UNLCK;
1331 		vp = (struct vnode *)fp->f_data;
1332 		(void) VOP_ADVLOCK(vp, (caddr_t)td->td_proc->p_leader,
1333 		    F_UNLCK, &lf, F_POSIX);
1334 	}
1335 	return (fdrop(fp, td));
1336 }
1337 
1338 int
1339 fdrop(fp, td)
1340 	struct file *fp;
1341 	struct thread *td;
1342 {
1343 	struct flock lf;
1344 	struct vnode *vp;
1345 	int error;
1346 
1347 	if (--fp->f_count > 0)
1348 		return (0);
1349 	if (fp->f_count < 0)
1350 		panic("fdrop: count < 0");
1351 	if ((fp->f_flag & FHASLOCK) && fp->f_type == DTYPE_VNODE) {
1352 		lf.l_whence = SEEK_SET;
1353 		lf.l_start = 0;
1354 		lf.l_len = 0;
1355 		lf.l_type = F_UNLCK;
1356 		vp = (struct vnode *)fp->f_data;
1357 		(void) VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
1358 	}
1359 	if (fp->f_ops != &badfileops)
1360 		error = fo_close(fp, td);
1361 	else
1362 		error = 0;
1363 	ffree(fp);
1364 	return (error);
1365 }
1366 
1367 /*
1368  * Apply an advisory lock on a file descriptor.
1369  *
1370  * Just attempt to get a record lock of the requested type on
1371  * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
1372  */
1373 #ifndef _SYS_SYSPROTO_H_
1374 struct flock_args {
1375 	int	fd;
1376 	int	how;
1377 };
1378 #endif
1379 /*
1380  * MPSAFE
1381  */
1382 /* ARGSUSED */
1383 int
1384 flock(td, uap)
1385 	struct thread *td;
1386 	register struct flock_args *uap;
1387 {
1388 	register struct filedesc *fdp = td->td_proc->p_fd;
1389 	register struct file *fp;
1390 	struct vnode *vp;
1391 	struct flock lf;
1392 	int error;
1393 
1394 	mtx_lock(&Giant);
1395 
1396 	if ((unsigned)uap->fd >= fdp->fd_nfiles ||
1397 	    (fp = fdp->fd_ofiles[uap->fd]) == NULL) {
1398 		error = EBADF;
1399 		goto done2;
1400 	}
1401 	if (fp->f_type != DTYPE_VNODE) {
1402 		error = EOPNOTSUPP;
1403 		goto done2;
1404 	}
1405 	vp = (struct vnode *)fp->f_data;
1406 	lf.l_whence = SEEK_SET;
1407 	lf.l_start = 0;
1408 	lf.l_len = 0;
1409 	if (uap->how & LOCK_UN) {
1410 		lf.l_type = F_UNLCK;
1411 		fp->f_flag &= ~FHASLOCK;
1412 		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
1413 		goto done2;
1414 	}
1415 	if (uap->how & LOCK_EX)
1416 		lf.l_type = F_WRLCK;
1417 	else if (uap->how & LOCK_SH)
1418 		lf.l_type = F_RDLCK;
1419 	else {
1420 		error = EBADF;
1421 		goto done2;
1422 	}
1423 	fp->f_flag |= FHASLOCK;
1424 	if (uap->how & LOCK_NB)
1425 		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK);
1426 	else
1427 		error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf, F_FLOCK|F_WAIT);
1428 done2:
1429 	mtx_unlock(&Giant);
1430 	return (error);
1431 }
1432 
1433 /*
1434  * File Descriptor pseudo-device driver (/dev/fd/).
1435  *
1436  * Opening minor device N dup()s the file (if any) connected to file
1437  * descriptor N belonging to the calling process.  Note that this driver
1438  * consists of only the ``open()'' routine, because all subsequent
1439  * references to this file will be direct to the other driver.
1440  */
1441 /* ARGSUSED */
1442 static int
1443 fdopen(dev, mode, type, td)
1444 	dev_t dev;
1445 	int mode, type;
1446 	struct thread *td;
1447 {
1448 
1449 	/*
1450 	 * XXX Kludge: set curthread->td_dupfd to contain the value of the
1451 	 * the file descriptor being sought for duplication. The error
1452 	 * return ensures that the vnode for this device will be released
1453 	 * by vn_open. Open will detect this special error and take the
1454 	 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
1455 	 * will simply report the error.
1456 	 */
1457 	td->td_dupfd = dev2unit(dev);
1458 	return (ENODEV);
1459 }
1460 
1461 /*
1462  * Duplicate the specified descriptor to a free descriptor.
1463  */
1464 int
1465 dupfdopen(td, fdp, indx, dfd, mode, error)
1466 	struct thread *td;
1467 	struct filedesc *fdp;
1468 	int indx, dfd;
1469 	int mode;
1470 	int error;
1471 {
1472 	register struct file *wfp;
1473 	struct file *fp;
1474 
1475 	/*
1476 	 * If the to-be-dup'd fd number is greater than the allowed number
1477 	 * of file descriptors, or the fd to be dup'd has already been
1478 	 * closed, then reject.
1479 	 */
1480 	if ((u_int)dfd >= fdp->fd_nfiles ||
1481 	    (wfp = fdp->fd_ofiles[dfd]) == NULL) {
1482 		return (EBADF);
1483 	}
1484 
1485 	/*
1486 	 * There are two cases of interest here.
1487 	 *
1488 	 * For ENODEV simply dup (dfd) to file descriptor
1489 	 * (indx) and return.
1490 	 *
1491 	 * For ENXIO steal away the file structure from (dfd) and
1492 	 * store it in (indx).  (dfd) is effectively closed by
1493 	 * this operation.
1494 	 *
1495 	 * Any other error code is just returned.
1496 	 */
1497 	switch (error) {
1498 	case ENODEV:
1499 		/*
1500 		 * Check that the mode the file is being opened for is a
1501 		 * subset of the mode of the existing descriptor.
1502 		 */
1503 		if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag)
1504 			return (EACCES);
1505 		fp = fdp->fd_ofiles[indx];
1506 #if 0
1507 		if (fp && fdp->fd_ofileflags[indx] & UF_MAPPED)
1508 			(void) munmapfd(td, indx);
1509 #endif
1510 		fdp->fd_ofiles[indx] = wfp;
1511 		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1512 		fhold(wfp);
1513 		if (indx > fdp->fd_lastfile)
1514 			fdp->fd_lastfile = indx;
1515 		/*
1516 		 * we now own the reference to fp that the ofiles[] array
1517 		 * used to own.  Release it.
1518 		 */
1519 		if (fp)
1520 			fdrop(fp, td);
1521 		return (0);
1522 
1523 	case ENXIO:
1524 		/*
1525 		 * Steal away the file pointer from dfd, and stuff it into indx.
1526 		 */
1527 		fp = fdp->fd_ofiles[indx];
1528 #if 0
1529 		if (fp && fdp->fd_ofileflags[indx] & UF_MAPPED)
1530 			(void) munmapfd(td, indx);
1531 #endif
1532 		fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
1533 		fdp->fd_ofiles[dfd] = NULL;
1534 		fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
1535 		fdp->fd_ofileflags[dfd] = 0;
1536 
1537 		/*
1538 		 * we now own the reference to fp that the ofiles[] array
1539 		 * used to own.  Release it.
1540 		 */
1541 		if (fp)
1542 			fdrop(fp, td);
1543 		/*
1544 		 * Complete the clean up of the filedesc structure by
1545 		 * recomputing the various hints.
1546 		 */
1547 		if (indx > fdp->fd_lastfile) {
1548 			fdp->fd_lastfile = indx;
1549 		} else {
1550 			while (fdp->fd_lastfile > 0 &&
1551 			   fdp->fd_ofiles[fdp->fd_lastfile] == NULL) {
1552 				fdp->fd_lastfile--;
1553 			}
1554 			if (dfd < fdp->fd_freefile)
1555 				fdp->fd_freefile = dfd;
1556 		}
1557 		return (0);
1558 
1559 	default:
1560 		return (error);
1561 	}
1562 	/* NOTREACHED */
1563 }
1564 
1565 /*
1566  * Get file structures.
1567  */
1568 static int
1569 sysctl_kern_file(SYSCTL_HANDLER_ARGS)
1570 {
1571 	int error;
1572 	struct file *fp;
1573 
1574 	if (!req->oldptr) {
1575 		/*
1576 		 * overestimate by 10 files
1577 		 */
1578 		return (SYSCTL_OUT(req, 0, sizeof(filehead) +
1579 				(nfiles + 10) * sizeof(struct file)));
1580 	}
1581 
1582 	error = SYSCTL_OUT(req, (caddr_t)&filehead, sizeof(filehead));
1583 	if (error)
1584 		return (error);
1585 
1586 	/*
1587 	 * followed by an array of file structures
1588 	 */
1589 	LIST_FOREACH(fp, &filehead, f_list) {
1590 		error = SYSCTL_OUT(req, (caddr_t)fp, sizeof (struct file));
1591 		if (error)
1592 			return (error);
1593 	}
1594 	return (0);
1595 }
1596 
1597 SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
1598     0, 0, sysctl_kern_file, "S,file", "Entire file table");
1599 
1600 SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
1601     &maxfilesperproc, 0, "Maximum files allowed open per process");
1602 
1603 SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
1604     &maxfiles, 0, "Maximum number of files");
1605 
1606 SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD,
1607     &nfiles, 0, "System-wide number of open files");
1608 
1609 static void
1610 fildesc_drvinit(void *unused)
1611 {
1612 	dev_t dev;
1613 
1614 	dev = make_dev(&fildesc_cdevsw, 0, UID_BIN, GID_BIN, 0666, "fd/0");
1615 	make_dev_alias(dev, "stdin");
1616 	dev = make_dev(&fildesc_cdevsw, 1, UID_BIN, GID_BIN, 0666, "fd/1");
1617 	make_dev_alias(dev, "stdout");
1618 	dev = make_dev(&fildesc_cdevsw, 2, UID_BIN, GID_BIN, 0666, "fd/2");
1619 	make_dev_alias(dev, "stderr");
1620 	if (!devfs_present) {
1621 		int fd;
1622 
1623 		for (fd = 3; fd < NUMFDESC; fd++)
1624 			make_dev(&fildesc_cdevsw, fd, UID_BIN, GID_BIN, 0666,
1625 			    "fd/%d", fd);
1626 	}
1627 }
1628 
1629 struct fileops badfileops = {
1630 	badfo_readwrite,
1631 	badfo_readwrite,
1632 	badfo_ioctl,
1633 	badfo_poll,
1634 	badfo_kqfilter,
1635 	badfo_stat,
1636 	badfo_close
1637 };
1638 
1639 static int
1640 badfo_readwrite(fp, uio, cred, flags, td)
1641 	struct file *fp;
1642 	struct uio *uio;
1643 	struct ucred *cred;
1644 	struct thread *td;
1645 	int flags;
1646 {
1647 
1648 	return (EBADF);
1649 }
1650 
1651 static int
1652 badfo_ioctl(fp, com, data, td)
1653 	struct file *fp;
1654 	u_long com;
1655 	caddr_t data;
1656 	struct thread *td;
1657 {
1658 
1659 	return (EBADF);
1660 }
1661 
1662 static int
1663 badfo_poll(fp, events, cred, td)
1664 	struct file *fp;
1665 	int events;
1666 	struct ucred *cred;
1667 	struct thread *td;
1668 {
1669 
1670 	return (0);
1671 }
1672 
1673 static int
1674 badfo_kqfilter(fp, kn)
1675 	struct file *fp;
1676 	struct knote *kn;
1677 {
1678 
1679 	return (0);
1680 }
1681 
1682 static int
1683 badfo_stat(fp, sb, td)
1684 	struct file *fp;
1685 	struct stat *sb;
1686 	struct thread *td;
1687 {
1688 
1689 	return (EBADF);
1690 }
1691 
1692 static int
1693 badfo_close(fp, td)
1694 	struct file *fp;
1695 	struct thread *td;
1696 {
1697 
1698 	return (EBADF);
1699 }
1700 
1701 SYSINIT(fildescdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR,
1702 					fildesc_drvinit,NULL)
1703