2 * Copyright (c) 2005 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1982, 1986, 1989, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
37 * (c) UNIX System Laboratories, Inc.
38 * All or some portions of this file are derived from material licensed
39 * to the University of California by American Telephone and Telegraph
40 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
41 * the permission of UNIX System Laboratories, Inc.
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * @(#)kern_descrip.c 8.6 (Berkeley) 4/19/94
72 * $FreeBSD: src/sys/kern/kern_descrip.c,v 1.81.2.19 2004/02/28 00:43:31 tegge Exp $
73 * $DragonFly: src/sys/kern/kern_descrip.c,v 1.78 2007/02/18 16:15:23 corecode Exp $
76 #include "opt_compat.h"
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/malloc.h>
80 #include <sys/sysproto.h>
82 #include <sys/device.h>
83 #include <sys/filedesc.h>
84 #include <sys/kernel.h>
85 #include <sys/sysctl.h>
86 #include <sys/vnode.h>
88 #include <sys/nlookup.h>
91 #include <sys/filio.h>
92 #include <sys/fcntl.h>
93 #include <sys/unistd.h>
94 #include <sys/resourcevar.h>
95 #include <sys/event.h>
96 #include <sys/kern_syscall.h>
97 #include <sys/kcore.h>
98 #include <sys/kinfo.h>
101 #include <vm/vm_extern.h>
103 #include <sys/thread2.h>
104 #include <sys/file2.h>
105 #include <sys/spinlock2.h>
107 static void fsetfd_locked(struct filedesc
*fdp
, struct file
*fp
, int fd
);
108 static void fdreserve_locked (struct filedesc
*fdp
, int fd0
, int incr
);
109 static struct file
*funsetfd_locked (struct filedesc
*fdp
, int fd
);
110 static int checkfpclosed(struct filedesc
*fdp
, int fd
, struct file
*fp
);
111 static void ffree(struct file
*fp
);
113 static MALLOC_DEFINE(M_FILEDESC
, "file desc", "Open file descriptor table");
114 static MALLOC_DEFINE(M_FILEDESC_TO_LEADER
, "file desc to leader",
115 "file desc to leader structures");
116 MALLOC_DEFINE(M_FILE
, "file", "Open file structure");
117 static MALLOC_DEFINE(M_SIGIO
, "sigio", "sigio structures");
119 static d_open_t fdopen
;
122 #define CDEV_MAJOR 22
123 static struct dev_ops fildesc_ops
= {
124 { "FD", CDEV_MAJOR
, 0 },
128 static int badfo_readwrite (struct file
*fp
, struct uio
*uio
,
129 struct ucred
*cred
, int flags
);
130 static int badfo_ioctl (struct file
*fp
, u_long com
, caddr_t data
,
132 static int badfo_poll (struct file
*fp
, int events
, struct ucred
*cred
);
133 static int badfo_kqfilter (struct file
*fp
, struct knote
*kn
);
134 static int badfo_stat (struct file
*fp
, struct stat
*sb
, struct ucred
*cred
);
135 static int badfo_close (struct file
*fp
);
136 static int badfo_shutdown (struct file
*fp
, int how
);
139 * Descriptor management.
141 static struct filelist filehead
= LIST_HEAD_INITIALIZER(&filehead
);
142 static struct spinlock filehead_spin
= SPINLOCK_INITIALIZER(&filehead_spin
);
143 static int nfiles
; /* actual number of open files */
147 * Fixup fd_freefile and fd_lastfile after a descriptor has been cleared.
149 * MPSAFE - must be called with fdp->fd_spin exclusively held
153 fdfixup_locked(struct filedesc
*fdp
, int fd
)
155 if (fd
< fdp
->fd_freefile
) {
156 fdp
->fd_freefile
= fd
;
158 while (fdp
->fd_lastfile
>= 0 &&
159 fdp
->fd_files
[fdp
->fd_lastfile
].fp
== NULL
&&
160 fdp
->fd_files
[fdp
->fd_lastfile
].reserved
== 0
167 * System calls on descriptors.
172 sys_getdtablesize(struct getdtablesize_args
*uap
)
174 struct proc
*p
= curproc
;
175 struct plimit
*limit
= p
->p_limit
;
177 spin_lock_rd(&limit
->p_spin
);
179 min((int)limit
->pl_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfilesperproc
);
180 spin_unlock_rd(&limit
->p_spin
);
185 * Duplicate a file descriptor to a particular value.
187 * note: keep in mind that a potential race condition exists when closing
188 * descriptors from a shared descriptor table (via rfork).
193 sys_dup2(struct dup2_args
*uap
)
197 error
= kern_dup(DUP_FIXED
, uap
->from
, uap
->to
, uap
->sysmsg_fds
);
203 * Duplicate a file descriptor.
208 sys_dup(struct dup_args
*uap
)
212 error
= kern_dup(DUP_VARIABLE
, uap
->fd
, 0, uap
->sysmsg_fds
);
218 * MPALMOSTSAFE - acquires mplock for fp operations
221 kern_fcntl(int fd
, int cmd
, union fcntl_dat
*dat
, struct ucred
*cred
)
223 struct thread
*td
= curthread
;
224 struct proc
*p
= td
->td_proc
;
229 int tmp
, error
, flg
= F_POSIX
;
234 * Operations on file descriptors that do not require a file pointer.
238 error
= fgetfdflags(p
->p_fd
, fd
, &tmp
);
240 dat
->fc_cloexec
= (tmp
& UF_EXCLOSE
) ? FD_CLOEXEC
: 0;
244 if (dat
->fc_cloexec
& FD_CLOEXEC
)
245 error
= fsetfdflags(p
->p_fd
, fd
, UF_EXCLOSE
);
247 error
= fclrfdflags(p
->p_fd
, fd
, UF_EXCLOSE
);
251 error
= kern_dup(DUP_VARIABLE
, fd
, newmin
, &dat
->fc_fd
);
258 * Operations on file pointers
260 if ((fp
= holdfp(p
->p_fd
, fd
, -1)) == NULL
)
266 dat
->fc_flags
= OFLAGS(fp
->f_flag
);
271 oflags
= fp
->f_flag
& FCNTLFLAGS
;
272 fp
->f_flag
&= ~FCNTLFLAGS
;
273 fp
->f_flag
|= FFLAGS(dat
->fc_flags
& ~O_ACCMODE
) & FCNTLFLAGS
;
275 if ((fp
->f_flag
^ oflags
) & FASYNC
) {
276 tmp
= fp
->f_flag
& FASYNC
;
277 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, cred
);
280 fp
->f_flag
= (fp
->f_flag
& ~FCNTLFLAGS
) | oflags
;
284 error
= fo_ioctl(fp
, FIOGETOWN
, (caddr_t
)&dat
->fc_owner
, cred
);
288 error
= fo_ioctl(fp
, FIOSETOWN
, (caddr_t
)&dat
->fc_owner
, cred
);
293 /* Fall into F_SETLK */
296 if (fp
->f_type
!= DTYPE_VNODE
) {
300 vp
= (struct vnode
*)fp
->f_data
;
303 * copyin/lockop may block
305 if (dat
->fc_flock
.l_whence
== SEEK_CUR
)
306 dat
->fc_flock
.l_start
+= fp
->f_offset
;
308 switch (dat
->fc_flock
.l_type
) {
310 if ((fp
->f_flag
& FREAD
) == 0) {
314 p
->p_leader
->p_flag
|= P_ADVLOCK
;
315 error
= VOP_ADVLOCK(vp
, (caddr_t
)p
->p_leader
, F_SETLK
,
316 &dat
->fc_flock
, flg
);
319 if ((fp
->f_flag
& FWRITE
) == 0) {
323 p
->p_leader
->p_flag
|= P_ADVLOCK
;
324 error
= VOP_ADVLOCK(vp
, (caddr_t
)p
->p_leader
, F_SETLK
,
325 &dat
->fc_flock
, flg
);
328 error
= VOP_ADVLOCK(vp
, (caddr_t
)p
->p_leader
, F_UNLCK
,
329 &dat
->fc_flock
, F_POSIX
);
337 * It is possible to race a close() on the descriptor while
338 * we were blocked getting the lock. If this occurs the
339 * close might not have caught the lock.
341 if (checkfpclosed(p
->p_fd
, fd
, fp
)) {
342 dat
->fc_flock
.l_whence
= SEEK_SET
;
343 dat
->fc_flock
.l_start
= 0;
344 dat
->fc_flock
.l_len
= 0;
345 dat
->fc_flock
.l_type
= F_UNLCK
;
346 (void) VOP_ADVLOCK(vp
, (caddr_t
)p
->p_leader
,
347 F_UNLCK
, &dat
->fc_flock
, F_POSIX
);
352 if (fp
->f_type
!= DTYPE_VNODE
) {
356 vp
= (struct vnode
*)fp
->f_data
;
358 * copyin/lockop may block
360 if (dat
->fc_flock
.l_type
!= F_RDLCK
&&
361 dat
->fc_flock
.l_type
!= F_WRLCK
&&
362 dat
->fc_flock
.l_type
!= F_UNLCK
) {
366 if (dat
->fc_flock
.l_whence
== SEEK_CUR
)
367 dat
->fc_flock
.l_start
+= fp
->f_offset
;
368 error
= VOP_ADVLOCK(vp
, (caddr_t
)p
->p_leader
, F_GETLK
,
369 &dat
->fc_flock
, F_POSIX
);
382 * The file control system call.
387 sys_fcntl(struct fcntl_args
*uap
)
394 dat
.fc_fd
= uap
->arg
;
397 dat
.fc_cloexec
= uap
->arg
;
400 dat
.fc_flags
= uap
->arg
;
403 dat
.fc_owner
= uap
->arg
;
408 error
= copyin((caddr_t
)uap
->arg
, &dat
.fc_flock
,
409 sizeof(struct flock
));
415 error
= kern_fcntl(uap
->fd
, uap
->cmd
, &dat
, curproc
->p_ucred
);
420 uap
->sysmsg_result
= dat
.fc_fd
;
423 uap
->sysmsg_result
= dat
.fc_cloexec
;
426 uap
->sysmsg_result
= dat
.fc_flags
;
429 uap
->sysmsg_result
= dat
.fc_owner
;
431 error
= copyout(&dat
.fc_flock
, (caddr_t
)uap
->arg
,
432 sizeof(struct flock
));
441 * Common code for dup, dup2, and fcntl(F_DUPFD).
443 * The type flag can be either DUP_FIXED or DUP_VARIABLE. DUP_FIXED tells
444 * kern_dup() to destructively dup over an existing file descriptor if new
445 * is already open. DUP_VARIABLE tells kern_dup() to find the lowest
446 * unused file descriptor that is greater than or equal to new.
451 kern_dup(enum dup_type type
, int old
, int new, int *res
)
453 struct thread
*td
= curthread
;
454 struct proc
*p
= td
->td_proc
;
455 struct filedesc
*fdp
= p
->p_fd
;
463 * Verify that we have a valid descriptor to dup from and
464 * possibly to dup to.
467 spin_lock_wr(&fdp
->fd_spin
);
468 if (new < 0 || new > p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
469 new >= maxfilesperproc
) {
470 spin_unlock_wr(&fdp
->fd_spin
);
473 if ((unsigned)old
>= fdp
->fd_nfiles
|| fdp
->fd_files
[old
].fp
== NULL
) {
474 spin_unlock_wr(&fdp
->fd_spin
);
477 if (type
== DUP_FIXED
&& old
== new) {
479 spin_unlock_wr(&fdp
->fd_spin
);
482 fp
= fdp
->fd_files
[old
].fp
;
483 oldflags
= fdp
->fd_files
[old
].fileflags
;
484 fhold(fp
); /* MPSAFE - can be called with a spinlock held */
487 * Allocate a new descriptor if DUP_VARIABLE, or expand the table
488 * if the requested descriptor is beyond the current table size.
490 * This can block. Retry if the source descriptor no longer matches
491 * or if our expectation in the expansion case races.
493 * If we are not expanding or allocating a new decriptor, then reset
494 * the target descriptor to a reserved state so we have a uniform
495 * setup for the next code block.
497 if (type
== DUP_VARIABLE
|| new >= fdp
->fd_nfiles
) {
498 spin_unlock_wr(&fdp
->fd_spin
);
499 error
= fdalloc(p
, new, &newfd
);
500 spin_lock_wr(&fdp
->fd_spin
);
502 spin_unlock_wr(&fdp
->fd_spin
);
509 if (old
>= fdp
->fd_nfiles
|| fdp
->fd_files
[old
].fp
!= fp
) {
510 fsetfd_locked(fdp
, NULL
, newfd
);
511 spin_unlock_wr(&fdp
->fd_spin
);
516 * Check for expansion race
518 if (type
!= DUP_VARIABLE
&& new != newfd
) {
519 fsetfd_locked(fdp
, NULL
, newfd
);
520 spin_unlock_wr(&fdp
->fd_spin
);
525 * Check for ripout, newfd reused old (this case probably
529 fsetfd_locked(fdp
, NULL
, newfd
);
530 spin_unlock_wr(&fdp
->fd_spin
);
537 if (fdp
->fd_files
[new].reserved
) {
538 spin_unlock_wr(&fdp
->fd_spin
);
540 kprintf("Warning: dup(): target descriptor %d is reserved, waiting for it to be resolved\n", new);
541 tsleep(fdp
, 0, "fdres", hz
);
546 * If the target descriptor was never allocated we have
547 * to allocate it. If it was we have to clean out the
548 * old descriptor. delfp inherits the ref from the
551 delfp
= fdp
->fd_files
[new].fp
;
552 fdp
->fd_files
[new].fp
= NULL
;
553 fdp
->fd_files
[new].reserved
= 1;
555 fdreserve_locked(fdp
, new, 1);
556 if (new > fdp
->fd_lastfile
)
557 fdp
->fd_lastfile
= new;
563 * NOTE: still holding an exclusive spinlock
567 * If a descriptor is being overwritten we may hve to tell
568 * fdfree() to sleep to ensure that all relevant process
569 * leaders can be traversed in closef().
571 if (delfp
!= NULL
&& p
->p_fdtol
!= NULL
) {
572 fdp
->fd_holdleaderscount
++;
577 KASSERT(delfp
== NULL
|| type
== DUP_FIXED
,
578 ("dup() picked an open file"));
581 * Duplicate the source descriptor, update lastfile. If the new
582 * descriptor was not allocated and we aren't replacing an existing
583 * descriptor we have to mark the descriptor as being in use.
585 * The fd_files[] array inherits fp's hold reference.
587 fsetfd_locked(fdp
, fp
, new);
588 fdp
->fd_files
[new].fileflags
= oldflags
& ~UF_EXCLOSE
;
589 spin_unlock_wr(&fdp
->fd_spin
);
594 * If we dup'd over a valid file, we now own the reference to it
595 * and must dispose of it using closef() semantics (as if a
596 * close() were performed on it).
599 (void)closef(delfp
, td
);
601 spin_lock_wr(&fdp
->fd_spin
);
602 fdp
->fd_holdleaderscount
--;
603 if (fdp
->fd_holdleaderscount
== 0 &&
604 fdp
->fd_holdleaderswakeup
!= 0) {
605 fdp
->fd_holdleaderswakeup
= 0;
606 spin_unlock_wr(&fdp
->fd_spin
);
607 wakeup(&fdp
->fd_holdleaderscount
);
609 spin_unlock_wr(&fdp
->fd_spin
);
617 * If sigio is on the list associated with a process or process group,
618 * disable signalling from the device, remove sigio from the list and
622 funsetown(struct sigio
*sigio
)
627 *(sigio
->sio_myref
) = NULL
;
629 if (sigio
->sio_pgid
< 0) {
630 SLIST_REMOVE(&sigio
->sio_pgrp
->pg_sigiolst
, sigio
,
632 } else /* if ((*sigiop)->sio_pgid > 0) */ {
633 SLIST_REMOVE(&sigio
->sio_proc
->p_sigiolst
, sigio
,
636 crfree(sigio
->sio_ucred
);
637 kfree(sigio
, M_SIGIO
);
640 /* Free a list of sigio structures. */
642 funsetownlst(struct sigiolst
*sigiolst
)
646 while ((sigio
= SLIST_FIRST(sigiolst
)) != NULL
)
651 * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg).
653 * After permission checking, add a sigio structure to the sigio list for
654 * the process or process group.
657 fsetown(pid_t pgid
, struct sigio
**sigiop
)
673 * Policy - Don't allow a process to FSETOWN a process
674 * in another session.
676 * Remove this test to allow maximum flexibility or
677 * restrict FSETOWN to the current process or process
678 * group for maximum safety.
680 if (proc
->p_session
!= curproc
->p_session
)
684 } else /* if (pgid < 0) */ {
685 pgrp
= pgfind(-pgid
);
690 * Policy - Don't allow a process to FSETOWN a process
691 * in another session.
693 * Remove this test to allow maximum flexibility or
694 * restrict FSETOWN to the current process or process
695 * group for maximum safety.
697 if (pgrp
->pg_session
!= curproc
->p_session
)
703 sigio
= kmalloc(sizeof(struct sigio
), M_SIGIO
, M_WAITOK
);
705 SLIST_INSERT_HEAD(&proc
->p_sigiolst
, sigio
, sio_pgsigio
);
706 sigio
->sio_proc
= proc
;
708 SLIST_INSERT_HEAD(&pgrp
->pg_sigiolst
, sigio
, sio_pgsigio
);
709 sigio
->sio_pgrp
= pgrp
;
711 sigio
->sio_pgid
= pgid
;
712 sigio
->sio_ucred
= crhold(curproc
->p_ucred
);
713 /* It would be convenient if p_ruid was in ucred. */
714 sigio
->sio_ruid
= curproc
->p_ucred
->cr_ruid
;
715 sigio
->sio_myref
= sigiop
;
723 * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
726 fgetown(struct sigio
*sigio
)
728 return (sigio
!= NULL
? sigio
->sio_pgid
: 0);
732 * Close many file descriptors.
737 sys_closefrom(struct closefrom_args
*uap
)
739 return(kern_closefrom(uap
->fd
));
743 * Close all file descriptors greater then or equal to fd
748 kern_closefrom(int fd
)
750 struct thread
*td
= curthread
;
751 struct proc
*p
= td
->td_proc
;
752 struct filedesc
*fdp
;
761 * NOTE: This function will skip unassociated descriptors and
762 * reserved descriptors that have not yet been assigned.
763 * fd_lastfile can change as a side effect of kern_close().
765 spin_lock_wr(&fdp
->fd_spin
);
766 while (fd
<= fdp
->fd_lastfile
) {
767 if (fdp
->fd_files
[fd
].fp
!= NULL
) {
768 spin_unlock_wr(&fdp
->fd_spin
);
769 /* ok if this races another close */
770 if (kern_close(fd
) == EINTR
)
772 spin_lock_wr(&fdp
->fd_spin
);
776 spin_unlock_wr(&fdp
->fd_spin
);
781 * Close a file descriptor.
786 sys_close(struct close_args
*uap
)
788 return(kern_close(uap
->fd
));
792 * MPALMOSTSAFE - acquires mplock around knote_fdclose() calls
797 struct thread
*td
= curthread
;
798 struct proc
*p
= td
->td_proc
;
799 struct filedesc
*fdp
;
807 spin_lock_wr(&fdp
->fd_spin
);
808 if ((fp
= funsetfd_locked(fdp
, fd
)) == NULL
) {
809 spin_unlock_wr(&fdp
->fd_spin
);
813 if (p
->p_fdtol
!= NULL
) {
815 * Ask fdfree() to sleep to ensure that all relevant
816 * process leaders can be traversed in closef().
818 fdp
->fd_holdleaderscount
++;
823 * we now hold the fp reference that used to be owned by the descriptor
826 spin_unlock_wr(&fdp
->fd_spin
);
827 if (fd
< fdp
->fd_knlistsize
) {
829 if (fd
< fdp
->fd_knlistsize
)
830 knote_fdclose(p
, fd
);
833 error
= closef(fp
, td
);
835 spin_lock_wr(&fdp
->fd_spin
);
836 fdp
->fd_holdleaderscount
--;
837 if (fdp
->fd_holdleaderscount
== 0 &&
838 fdp
->fd_holdleaderswakeup
!= 0) {
839 fdp
->fd_holdleaderswakeup
= 0;
840 spin_unlock_wr(&fdp
->fd_spin
);
841 wakeup(&fdp
->fd_holdleaderscount
);
843 spin_unlock_wr(&fdp
->fd_spin
);
850 * shutdown_args(int fd, int how)
853 kern_shutdown(int fd
, int how
)
855 struct thread
*td
= curthread
;
856 struct proc
*p
= td
->td_proc
;
862 if ((fp
= holdfp(p
->p_fd
, fd
, -1)) == NULL
)
864 error
= fo_shutdown(fp
, how
);
871 sys_shutdown(struct shutdown_args
*uap
)
875 error
= kern_shutdown(uap
->s
, uap
->how
);
881 kern_fstat(int fd
, struct stat
*ub
)
883 struct thread
*td
= curthread
;
884 struct proc
*p
= td
->td_proc
;
890 if ((fp
= holdfp(p
->p_fd
, fd
, -1)) == NULL
)
892 error
= fo_stat(fp
, ub
, p
->p_ucred
);
899 * Return status information about a file descriptor.
902 sys_fstat(struct fstat_args
*uap
)
907 error
= kern_fstat(uap
->fd
, &st
);
910 error
= copyout(&st
, uap
->sb
, sizeof(st
));
915 * Return pathconf information about a file descriptor.
919 sys_fpathconf(struct fpathconf_args
*uap
)
921 struct thread
*td
= curthread
;
922 struct proc
*p
= td
->td_proc
;
929 if ((fp
= holdfp(p
->p_fd
, uap
->fd
, -1)) == NULL
)
932 switch (fp
->f_type
) {
935 if (uap
->name
!= _PC_PIPE_BUF
) {
938 uap
->sysmsg_result
= PIPE_BUF
;
944 vp
= (struct vnode
*)fp
->f_data
;
945 error
= VOP_PATHCONF(vp
, uap
->name
, uap
->sysmsg_fds
);
956 SYSCTL_INT(_debug
, OID_AUTO
, fdexpand
, CTLFLAG_RD
, &fdexpand
, 0, "");
959 * Grow the file table so it can hold through descriptor (want).
961 * The fdp's spinlock must be held exclusively on entry and may be held
962 * exclusively on return. The spinlock may be cycled by the routine.
967 fdgrow_locked(struct filedesc
*fdp
, int want
)
969 struct fdnode
*newfiles
;
970 struct fdnode
*oldfiles
;
975 /* nf has to be of the form 2^n - 1 */
977 } while (nf
<= want
);
979 spin_unlock_wr(&fdp
->fd_spin
);
980 newfiles
= kmalloc(nf
* sizeof(struct fdnode
), M_FILEDESC
, M_WAITOK
);
981 spin_lock_wr(&fdp
->fd_spin
);
984 * We could have raced another extend while we were not holding
987 if (fdp
->fd_nfiles
>= nf
) {
988 spin_unlock_wr(&fdp
->fd_spin
);
989 kfree(newfiles
, M_FILEDESC
);
990 spin_lock_wr(&fdp
->fd_spin
);
994 * Copy the existing ofile and ofileflags arrays
995 * and zero the new portion of each array.
997 extra
= nf
- fdp
->fd_nfiles
;
998 bcopy(fdp
->fd_files
, newfiles
, fdp
->fd_nfiles
* sizeof(struct fdnode
));
999 bzero(&newfiles
[fdp
->fd_nfiles
], extra
* sizeof(struct fdnode
));
1001 oldfiles
= fdp
->fd_files
;
1002 fdp
->fd_files
= newfiles
;
1003 fdp
->fd_nfiles
= nf
;
1005 if (oldfiles
!= fdp
->fd_builtin_files
) {
1006 spin_unlock_wr(&fdp
->fd_spin
);
1007 kfree(oldfiles
, M_FILEDESC
);
1008 spin_lock_wr(&fdp
->fd_spin
);
1014 * Number of nodes in right subtree, including the root.
1017 right_subtree_size(int n
)
1019 return (n
^ (n
| (n
+ 1)));
1026 right_ancestor(int n
)
1028 return (n
| (n
+ 1));
1035 left_ancestor(int n
)
1037 return ((n
& (n
+ 1)) - 1);
1041 * Traverse the in-place binary tree buttom-up adjusting the allocation
1042 * count so scans can determine where free descriptors are located.
1044 * MPSAFE - caller must be holding an exclusive spinlock on fdp
1048 fdreserve_locked(struct filedesc
*fdp
, int fd
, int incr
)
1051 fdp
->fd_files
[fd
].allocated
+= incr
;
1052 KKASSERT(fdp
->fd_files
[fd
].allocated
>= 0);
1053 fd
= left_ancestor(fd
);
1058 * Reserve a file descriptor for the process. If no error occurs, the
1059 * caller MUST at some point call fsetfd() or assign a file pointer
1060 * or dispose of the reservation.
1065 fdalloc(struct proc
*p
, int want
, int *result
)
1067 struct filedesc
*fdp
= p
->p_fd
;
1068 int fd
, rsize
, rsum
, node
, lim
;
1070 spin_lock_rd(&p
->p_limit
->p_spin
);
1071 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfilesperproc
);
1072 spin_unlock_rd(&p
->p_limit
->p_spin
);
1075 spin_lock_wr(&fdp
->fd_spin
);
1076 if (want
>= fdp
->fd_nfiles
)
1077 fdgrow_locked(fdp
, want
);
1080 * Search for a free descriptor starting at the higher
1081 * of want or fd_freefile. If that fails, consider
1082 * expanding the ofile array.
1084 * NOTE! the 'allocated' field is a cumulative recursive allocation
1085 * count. If we happen to see a value of 0 then we can shortcut
1086 * our search. Otherwise we run through through the tree going
1087 * down branches we know have free descriptor(s) until we hit a
1088 * leaf node. The leaf node will be free but will not necessarily
1089 * have an allocated field of 0.
1092 /* move up the tree looking for a subtree with a free node */
1093 for (fd
= max(want
, fdp
->fd_freefile
); fd
< min(fdp
->fd_nfiles
, lim
);
1094 fd
= right_ancestor(fd
)) {
1095 if (fdp
->fd_files
[fd
].allocated
== 0)
1098 rsize
= right_subtree_size(fd
);
1099 if (fdp
->fd_files
[fd
].allocated
== rsize
)
1100 continue; /* right subtree full */
1103 * Free fd is in the right subtree of the tree rooted at fd.
1104 * Call that subtree R. Look for the smallest (leftmost)
1105 * subtree of R with an unallocated fd: continue moving
1106 * down the left branch until encountering a full left
1107 * subtree, then move to the right.
1109 for (rsum
= 0, rsize
/= 2; rsize
> 0; rsize
/= 2) {
1111 rsum
+= fdp
->fd_files
[node
].allocated
;
1112 if (fdp
->fd_files
[fd
].allocated
== rsum
+ rsize
) {
1113 fd
= node
; /* move to the right */
1114 if (fdp
->fd_files
[node
].allocated
== 0)
1123 * No space in current array. Expand?
1125 if (fdp
->fd_nfiles
>= lim
) {
1126 spin_unlock_wr(&fdp
->fd_spin
);
1129 fdgrow_locked(fdp
, want
);
1133 KKASSERT(fd
< fdp
->fd_nfiles
);
1134 if (fd
> fdp
->fd_lastfile
)
1135 fdp
->fd_lastfile
= fd
;
1136 if (want
<= fdp
->fd_freefile
)
1137 fdp
->fd_freefile
= fd
;
1139 KKASSERT(fdp
->fd_files
[fd
].fp
== NULL
);
1140 KKASSERT(fdp
->fd_files
[fd
].reserved
== 0);
1141 fdp
->fd_files
[fd
].fileflags
= 0;
1142 fdp
->fd_files
[fd
].reserved
= 1;
1143 fdreserve_locked(fdp
, fd
, 1);
1144 spin_unlock_wr(&fdp
->fd_spin
);
1149 * Check to see whether n user file descriptors
1150 * are available to the process p.
1155 fdavail(struct proc
*p
, int n
)
1157 struct filedesc
*fdp
= p
->p_fd
;
1158 struct fdnode
*fdnode
;
1161 spin_lock_rd(&p
->p_limit
->p_spin
);
1162 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfilesperproc
);
1163 spin_unlock_rd(&p
->p_limit
->p_spin
);
1165 spin_lock_rd(&fdp
->fd_spin
);
1166 if ((i
= lim
- fdp
->fd_nfiles
) > 0 && (n
-= i
) <= 0) {
1167 spin_unlock_rd(&fdp
->fd_spin
);
1170 last
= min(fdp
->fd_nfiles
, lim
);
1171 fdnode
= &fdp
->fd_files
[fdp
->fd_freefile
];
1172 for (i
= last
- fdp
->fd_freefile
; --i
>= 0; ++fdnode
) {
1173 if (fdnode
->fp
== NULL
&& --n
<= 0) {
1174 spin_unlock_rd(&fdp
->fd_spin
);
1178 spin_unlock_rd(&fdp
->fd_spin
);
1184 * Create a new open file structure and reserve a file decriptor
1185 * for the process that refers to it.
1187 * Root creds are checked using p, or assumed if p is NULL. If
1188 * resultfd is non-NULL then p must also be non-NULL. No file
1189 * descriptor is reserved if resultfd is NULL.
1191 * A file pointer with a refcount of 1 is returned. Note that the
1192 * file pointer is NOT associated with the descriptor. If falloc
1193 * returns success, fsetfd() MUST be called to either associate the
1194 * file pointer or clear the reservation.
1199 falloc(struct proc
*p
, struct file
**resultfp
, int *resultfd
)
1201 static struct timeval lastfail
;
1209 * Handle filetable full issues and root overfill.
1211 if (nfiles
>= maxfiles
- maxfilesrootres
&&
1212 ((p
&& p
->p_ucred
->cr_ruid
!= 0) || nfiles
>= maxfiles
)) {
1213 if (ppsratecheck(&lastfail
, &curfail
, 1)) {
1214 kprintf("kern.maxfiles limit exceeded by uid %d, please see tuning(7).\n",
1215 (p
? p
->p_ucred
->cr_ruid
: -1));
1222 * Allocate a new file descriptor.
1224 fp
= kmalloc(sizeof(struct file
), M_FILE
, M_WAITOK
| M_ZERO
);
1225 spin_init(&fp
->f_spin
);
1227 fp
->f_ops
= &badfileops
;
1230 fp
->f_cred
= crhold(p
->p_ucred
);
1232 fp
->f_cred
= crhold(proc0
.p_ucred
);
1233 spin_lock_wr(&filehead_spin
);
1235 LIST_INSERT_HEAD(&filehead
, fp
, f_list
);
1236 spin_unlock_wr(&filehead_spin
);
1238 if ((error
= fdalloc(p
, 0, resultfd
)) != 0) {
1255 checkfpclosed(struct filedesc
*fdp
, int fd
, struct file
*fp
)
1259 spin_lock_rd(&fdp
->fd_spin
);
1260 if ((unsigned) fd
>= fdp
->fd_nfiles
|| fp
!= fdp
->fd_files
[fd
].fp
)
1264 spin_unlock_rd(&fdp
->fd_spin
);
1269 * Associate a file pointer with a previously reserved file descriptor.
1270 * This function always succeeds.
1272 * If fp is NULL, the file descriptor is returned to the pool.
1276 * MPSAFE (exclusive spinlock must be held on call)
1279 fsetfd_locked(struct filedesc
*fdp
, struct file
*fp
, int fd
)
1281 KKASSERT((unsigned)fd
< fdp
->fd_nfiles
);
1282 KKASSERT(fdp
->fd_files
[fd
].reserved
!= 0);
1285 fdp
->fd_files
[fd
].fp
= fp
;
1286 fdp
->fd_files
[fd
].reserved
= 0;
1287 if (fp
->f_type
== DTYPE_KQUEUE
) {
1288 if (fdp
->fd_knlistsize
< 0)
1289 fdp
->fd_knlistsize
= 0;
1292 fdp
->fd_files
[fd
].reserved
= 0;
1293 fdreserve_locked(fdp
, fd
, -1);
1294 fdfixup_locked(fdp
, fd
);
1302 fsetfd(struct proc
*p
, struct file
*fp
, int fd
)
1304 struct filedesc
*fdp
= p
->p_fd
;
1306 spin_lock_wr(&fdp
->fd_spin
);
1307 fsetfd_locked(fdp
, fp
, fd
);
1308 spin_unlock_wr(&fdp
->fd_spin
);
1312 * MPSAFE (exclusive spinlock must be held on call)
1316 funsetfd_locked(struct filedesc
*fdp
, int fd
)
1320 if ((unsigned)fd
>= fdp
->fd_nfiles
)
1322 if ((fp
= fdp
->fd_files
[fd
].fp
) == NULL
)
1324 fdp
->fd_files
[fd
].fp
= NULL
;
1325 fdp
->fd_files
[fd
].fileflags
= 0;
1327 fdreserve_locked(fdp
, fd
, -1);
1328 fdfixup_locked(fdp
, fd
);
1336 fgetfdflags(struct filedesc
*fdp
, int fd
, int *flagsp
)
1340 spin_lock_rd(&fdp
->fd_spin
);
1341 if (((u_int
)fd
) >= fdp
->fd_nfiles
) {
1343 } else if (fdp
->fd_files
[fd
].fp
== NULL
) {
1346 *flagsp
= fdp
->fd_files
[fd
].fileflags
;
1349 spin_unlock_rd(&fdp
->fd_spin
);
1357 fsetfdflags(struct filedesc
*fdp
, int fd
, int add_flags
)
1361 spin_lock_wr(&fdp
->fd_spin
);
1362 if (((u_int
)fd
) >= fdp
->fd_nfiles
) {
1364 } else if (fdp
->fd_files
[fd
].fp
== NULL
) {
1367 fdp
->fd_files
[fd
].fileflags
|= add_flags
;
1370 spin_unlock_wr(&fdp
->fd_spin
);
1378 fclrfdflags(struct filedesc
*fdp
, int fd
, int rem_flags
)
1382 spin_lock_wr(&fdp
->fd_spin
);
1383 if (((u_int
)fd
) >= fdp
->fd_nfiles
) {
1385 } else if (fdp
->fd_files
[fd
].fp
== NULL
) {
1388 fdp
->fd_files
[fd
].fileflags
&= ~rem_flags
;
1391 spin_unlock_wr(&fdp
->fd_spin
);
1396 fsetcred(struct file
*fp
, struct ucred
*cr
)
1404 * Free a file descriptor.
1408 ffree(struct file
*fp
)
1410 KASSERT((fp
->f_count
== 0), ("ffree: fp_fcount not 0!"));
1411 spin_lock_wr(&filehead_spin
);
1412 LIST_REMOVE(fp
, f_list
);
1414 spin_unlock_wr(&filehead_spin
);
1416 if (fp
->f_nchandle
.ncp
)
1417 cache_drop(&fp
->f_nchandle
);
1422 * called from init_main, initialize filedesc0 for proc0.
1425 fdinit_bootstrap(struct proc
*p0
, struct filedesc
*fdp0
, int cmask
)
1429 fdp0
->fd_refcnt
= 1;
1430 fdp0
->fd_cmask
= cmask
;
1431 fdp0
->fd_files
= fdp0
->fd_builtin_files
;
1432 fdp0
->fd_nfiles
= NDFILE
;
1433 fdp0
->fd_lastfile
= -1;
1434 spin_init(&fdp0
->fd_spin
);
1438 * Build a new filedesc structure.
1443 fdinit(struct proc
*p
)
1445 struct filedesc
*newfdp
;
1446 struct filedesc
*fdp
= p
->p_fd
;
1448 newfdp
= kmalloc(sizeof(struct filedesc
), M_FILEDESC
, M_WAITOK
|M_ZERO
);
1449 spin_lock_rd(&fdp
->fd_spin
);
1451 newfdp
->fd_cdir
= fdp
->fd_cdir
;
1452 vref(newfdp
->fd_cdir
);
1453 cache_copy(&fdp
->fd_ncdir
, &newfdp
->fd_ncdir
);
1457 * rdir may not be set in e.g. proc0 or anything vm_fork'd off of
1458 * proc0, but should unconditionally exist in other processes.
1461 newfdp
->fd_rdir
= fdp
->fd_rdir
;
1462 vref(newfdp
->fd_rdir
);
1463 cache_copy(&fdp
->fd_nrdir
, &newfdp
->fd_nrdir
);
1466 newfdp
->fd_jdir
= fdp
->fd_jdir
;
1467 vref(newfdp
->fd_jdir
);
1468 cache_copy(&fdp
->fd_njdir
, &newfdp
->fd_njdir
);
1470 spin_unlock_rd(&fdp
->fd_spin
);
1472 /* Create the file descriptor table. */
1473 newfdp
->fd_refcnt
= 1;
1474 newfdp
->fd_cmask
= cmask
;
1475 newfdp
->fd_files
= newfdp
->fd_builtin_files
;
1476 newfdp
->fd_nfiles
= NDFILE
;
1477 newfdp
->fd_knlistsize
= -1;
1478 newfdp
->fd_lastfile
= -1;
1479 spin_init(&newfdp
->fd_spin
);
1485 * Share a filedesc structure.
1490 fdshare(struct proc
*p
)
1492 struct filedesc
*fdp
;
1495 spin_lock_wr(&fdp
->fd_spin
);
1497 spin_unlock_wr(&fdp
->fd_spin
);
1502 * Copy a filedesc structure.
1507 fdcopy(struct proc
*p
)
1509 struct filedesc
*fdp
= p
->p_fd
;
1510 struct filedesc
*newfdp
;
1511 struct fdnode
*fdnode
;
1516 * Certain daemons might not have file descriptors.
1522 * Allocate the new filedesc and fd_files[] array. This can race
1523 * with operations by other threads on the fdp so we have to be
1526 newfdp
= kmalloc(sizeof(struct filedesc
), M_FILEDESC
, M_WAITOK
| M_ZERO
);
1528 spin_lock_rd(&fdp
->fd_spin
);
1529 if (fdp
->fd_lastfile
< NDFILE
) {
1530 newfdp
->fd_files
= newfdp
->fd_builtin_files
;
1534 * We have to allocate (N^2-1) entries for our in-place
1535 * binary tree. Allow the table to shrink.
1539 while (ni
> fdp
->fd_lastfile
&& ni
> NDFILE
) {
1543 spin_unlock_rd(&fdp
->fd_spin
);
1544 newfdp
->fd_files
= kmalloc(i
* sizeof(struct fdnode
),
1545 M_FILEDESC
, M_WAITOK
| M_ZERO
);
1548 * Check for race, retry
1550 spin_lock_rd(&fdp
->fd_spin
);
1551 if (i
<= fdp
->fd_lastfile
) {
1552 spin_unlock_rd(&fdp
->fd_spin
);
1553 kfree(newfdp
->fd_files
, M_FILEDESC
);
1559 * Dup the remaining fields. vref() and cache_hold() can be
1560 * safely called while holding the read spinlock on fdp.
1562 * The read spinlock on fdp is still being held.
1564 * NOTE: vref and cache_hold calls for the case where the vnode
1565 * or cache entry already has at least one ref may be called
1566 * while holding spin locks.
1568 if ((newfdp
->fd_cdir
= fdp
->fd_cdir
) != NULL
) {
1569 vref(newfdp
->fd_cdir
);
1570 cache_copy(&fdp
->fd_ncdir
, &newfdp
->fd_ncdir
);
1573 * We must check for fd_rdir here, at least for now because
1574 * the init process is created before we have access to the
1575 * rootvode to take a reference to it.
1577 if ((newfdp
->fd_rdir
= fdp
->fd_rdir
) != NULL
) {
1578 vref(newfdp
->fd_rdir
);
1579 cache_copy(&fdp
->fd_nrdir
, &newfdp
->fd_nrdir
);
1581 if ((newfdp
->fd_jdir
= fdp
->fd_jdir
) != NULL
) {
1582 vref(newfdp
->fd_jdir
);
1583 cache_copy(&fdp
->fd_njdir
, &newfdp
->fd_njdir
);
1585 newfdp
->fd_refcnt
= 1;
1586 newfdp
->fd_nfiles
= i
;
1587 newfdp
->fd_lastfile
= fdp
->fd_lastfile
;
1588 newfdp
->fd_freefile
= fdp
->fd_freefile
;
1589 newfdp
->fd_cmask
= fdp
->fd_cmask
;
1590 newfdp
->fd_knlist
= NULL
;
1591 newfdp
->fd_knlistsize
= -1;
1592 newfdp
->fd_knhash
= NULL
;
1593 newfdp
->fd_knhashmask
= 0;
1594 spin_init(&newfdp
->fd_spin
);
1597 * Copy the descriptor table through (i). This also copies the
1598 * allocation state. Then go through and ref the file pointers
1599 * and clean up any KQ descriptors.
1601 * kq descriptors cannot be copied. Since we haven't ref'd the
1602 * copied files yet we can ignore the return value from funsetfd().
1604 * The read spinlock on fdp is still being held.
1606 bcopy(fdp
->fd_files
, newfdp
->fd_files
, i
* sizeof(struct fdnode
));
1607 for (i
= 0 ; i
< newfdp
->fd_nfiles
; ++i
) {
1608 fdnode
= &newfdp
->fd_files
[i
];
1609 if (fdnode
->reserved
) {
1610 fdreserve_locked(newfdp
, i
, -1);
1611 fdnode
->reserved
= 0;
1612 fdfixup_locked(newfdp
, i
);
1613 } else if (fdnode
->fp
) {
1614 if (fdnode
->fp
->f_type
== DTYPE_KQUEUE
) {
1615 (void)funsetfd_locked(newfdp
, i
);
1621 spin_unlock_rd(&fdp
->fd_spin
);
1626 * Release a filedesc structure.
1628 * NOT MPSAFE (MPSAFE for refs > 1, but the final cleanup code is not MPSAFE)
1631 fdfree(struct proc
*p
)
1633 /* Take any thread of p */
1634 struct thread
*td
= FIRST_LWP_IN_PROC(p
)->lwp_thread
;
1635 struct filedesc
*fdp
= p
->p_fd
;
1636 struct fdnode
*fdnode
;
1638 struct filedesc_to_leader
*fdtol
;
1643 /* Certain daemons might not have file descriptors. */
1648 * Severe messing around to follow
1650 spin_lock_wr(&fdp
->fd_spin
);
1652 /* Check for special need to clear POSIX style locks */
1654 if (fdtol
!= NULL
) {
1655 KASSERT(fdtol
->fdl_refcount
> 0,
1656 ("filedesc_to_refcount botch: fdl_refcount=%d",
1657 fdtol
->fdl_refcount
));
1658 if (fdtol
->fdl_refcount
== 1 &&
1659 (p
->p_leader
->p_flag
& P_ADVLOCK
) != 0) {
1660 for (i
= 0; i
<= fdp
->fd_lastfile
; ++i
) {
1661 fdnode
= &fdp
->fd_files
[i
];
1662 if (fdnode
->fp
== NULL
||
1663 fdnode
->fp
->f_type
!= DTYPE_VNODE
) {
1668 spin_unlock_wr(&fdp
->fd_spin
);
1670 lf
.l_whence
= SEEK_SET
;
1673 lf
.l_type
= F_UNLCK
;
1674 vp
= (struct vnode
*)fp
->f_data
;
1675 (void) VOP_ADVLOCK(vp
,
1676 (caddr_t
)p
->p_leader
,
1681 spin_lock_wr(&fdp
->fd_spin
);
1685 if (fdtol
->fdl_refcount
== 1) {
1686 if (fdp
->fd_holdleaderscount
> 0 &&
1687 (p
->p_leader
->p_flag
& P_ADVLOCK
) != 0) {
1689 * close() or do_dup() has cleared a reference
1690 * in a shared file descriptor table.
1692 fdp
->fd_holdleaderswakeup
= 1;
1693 msleep(&fdp
->fd_holdleaderscount
,
1694 &fdp
->fd_spin
, 0, "fdlhold", 0);
1697 if (fdtol
->fdl_holdcount
> 0) {
1699 * Ensure that fdtol->fdl_leader
1700 * remains valid in closef().
1702 fdtol
->fdl_wakeup
= 1;
1703 msleep(fdtol
, &fdp
->fd_spin
, 0, "fdlhold", 0);
1707 fdtol
->fdl_refcount
--;
1708 if (fdtol
->fdl_refcount
== 0 &&
1709 fdtol
->fdl_holdcount
== 0) {
1710 fdtol
->fdl_next
->fdl_prev
= fdtol
->fdl_prev
;
1711 fdtol
->fdl_prev
->fdl_next
= fdtol
->fdl_next
;
1716 if (fdtol
!= NULL
) {
1717 spin_unlock_wr(&fdp
->fd_spin
);
1718 kfree(fdtol
, M_FILEDESC_TO_LEADER
);
1719 spin_lock_wr(&fdp
->fd_spin
);
1722 if (--fdp
->fd_refcnt
> 0) {
1723 spin_unlock_wr(&fdp
->fd_spin
);
1726 spin_unlock_wr(&fdp
->fd_spin
);
1729 * we are the last reference to the structure, we can
1730 * safely assume it will not change out from under us.
1732 for (i
= 0; i
<= fdp
->fd_lastfile
; ++i
) {
1733 if (fdp
->fd_files
[i
].fp
)
1734 closef(fdp
->fd_files
[i
].fp
, td
);
1736 if (fdp
->fd_files
!= fdp
->fd_builtin_files
)
1737 kfree(fdp
->fd_files
, M_FILEDESC
);
1739 cache_drop(&fdp
->fd_ncdir
);
1740 vrele(fdp
->fd_cdir
);
1743 cache_drop(&fdp
->fd_nrdir
);
1744 vrele(fdp
->fd_rdir
);
1747 cache_drop(&fdp
->fd_njdir
);
1748 vrele(fdp
->fd_jdir
);
1751 kfree(fdp
->fd_knlist
, M_KQUEUE
);
1753 kfree(fdp
->fd_knhash
, M_KQUEUE
);
1754 kfree(fdp
, M_FILEDESC
);
1758 * Retrieve and reference the file pointer associated with a descriptor.
1763 holdfp(struct filedesc
*fdp
, int fd
, int flag
)
1767 spin_lock_rd(&fdp
->fd_spin
);
1768 if (((u_int
)fd
) >= fdp
->fd_nfiles
) {
1772 if ((fp
= fdp
->fd_files
[fd
].fp
) == NULL
)
1774 if ((fp
->f_flag
& flag
) == 0 && flag
!= -1) {
1780 spin_unlock_rd(&fdp
->fd_spin
);
1785 * holdsock() - load the struct file pointer associated
1786 * with a socket into *fpp. If an error occurs, non-zero
1787 * will be returned and *fpp will be set to NULL.
1792 holdsock(struct filedesc
*fdp
, int fd
, struct file
**fpp
)
1797 spin_lock_rd(&fdp
->fd_spin
);
1798 if ((unsigned)fd
>= fdp
->fd_nfiles
) {
1803 if ((fp
= fdp
->fd_files
[fd
].fp
) == NULL
) {
1807 if (fp
->f_type
!= DTYPE_SOCKET
) {
1814 spin_unlock_rd(&fdp
->fd_spin
);
1820 * Convert a user file descriptor to a held file pointer.
1825 holdvnode(struct filedesc
*fdp
, int fd
, struct file
**fpp
)
1830 spin_lock_rd(&fdp
->fd_spin
);
1831 if ((unsigned)fd
>= fdp
->fd_nfiles
) {
1836 if ((fp
= fdp
->fd_files
[fd
].fp
) == NULL
) {
1840 if (fp
->f_type
!= DTYPE_VNODE
&& fp
->f_type
!= DTYPE_FIFO
) {
1847 spin_unlock_rd(&fdp
->fd_spin
);
1853 * For setugid programs, we don't want to people to use that setugidness
1854 * to generate error messages which write to a file which otherwise would
1855 * otherwise be off-limits to the process.
1857 * This is a gross hack to plug the hole. A better solution would involve
1858 * a special vop or other form of generalized access control mechanism. We
1859 * go ahead and just reject all procfs file systems accesses as dangerous.
1861 * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
1862 * sufficient. We also don't for check setugidness since we know we are.
1865 is_unsafe(struct file
*fp
)
1867 if (fp
->f_type
== DTYPE_VNODE
&&
1868 ((struct vnode
*)(fp
->f_data
))->v_tag
== VT_PROCFS
)
1874 * Make this setguid thing safe, if at all possible.
1876 * NOT MPSAFE - scans fdp without spinlocks, calls knote_fdclose()
1879 setugidsafety(struct proc
*p
)
1881 /* Take any thread of p */
1882 struct thread
*td
= FIRST_LWP_IN_PROC(p
)->lwp_thread
;
1883 struct filedesc
*fdp
= p
->p_fd
;
1886 /* Certain daemons might not have file descriptors. */
1891 * note: fdp->fd_files may be reallocated out from under us while
1892 * we are blocked in a close. Be careful!
1894 for (i
= 0; i
<= fdp
->fd_lastfile
; i
++) {
1897 if (fdp
->fd_files
[i
].fp
&& is_unsafe(fdp
->fd_files
[i
].fp
)) {
1900 if (i
< fdp
->fd_knlistsize
)
1901 knote_fdclose(p
, i
);
1903 * NULL-out descriptor prior to close to avoid
1904 * a race while close blocks.
1906 if ((fp
= funsetfd_locked(fdp
, i
)) != NULL
)
1913 * Close any files on exec?
1915 * NOT MPSAFE - scans fdp without spinlocks, calls knote_fdclose()
1918 fdcloseexec(struct proc
*p
)
1920 /* Take any thread of p */
1921 struct thread
*td
= FIRST_LWP_IN_PROC(p
)->lwp_thread
;
1922 struct filedesc
*fdp
= p
->p_fd
;
1925 /* Certain daemons might not have file descriptors. */
1930 * We cannot cache fd_files since operations may block and rip
1931 * them out from under us.
1933 for (i
= 0; i
<= fdp
->fd_lastfile
; i
++) {
1934 if (fdp
->fd_files
[i
].fp
!= NULL
&&
1935 (fdp
->fd_files
[i
].fileflags
& UF_EXCLOSE
)) {
1938 if (i
< fdp
->fd_knlistsize
)
1939 knote_fdclose(p
, i
);
1941 * NULL-out descriptor prior to close to avoid
1942 * a race while close blocks.
1944 if ((fp
= funsetfd_locked(fdp
, i
)) != NULL
)
1951 * It is unsafe for set[ug]id processes to be started with file
1952 * descriptors 0..2 closed, as these descriptors are given implicit
1953 * significance in the Standard C library. fdcheckstd() will create a
1954 * descriptor referencing /dev/null for each of stdin, stdout, and
1955 * stderr that is not already open.
1957 * NOT MPSAFE - calls falloc, vn_open, etc
1960 fdcheckstd(struct proc
*p
)
1962 struct nlookupdata nd
;
1963 struct filedesc
*fdp
;
1966 int i
, error
, flags
, devnull
;
1973 for (i
= 0; i
< 3; i
++) {
1974 if (fdp
->fd_files
[i
].fp
!= NULL
)
1977 if ((error
= falloc(p
, &fp
, &devnull
)) != 0)
1980 error
= nlookup_init(&nd
, "/dev/null", UIO_SYSSPACE
,
1981 NLC_FOLLOW
|NLC_LOCKVP
);
1982 flags
= FREAD
| FWRITE
;
1984 error
= vn_open(&nd
, fp
, flags
, 0);
1986 fsetfd(p
, fp
, devnull
);
1988 fsetfd(p
, NULL
, devnull
);
1993 KKASSERT(i
== devnull
);
1995 error
= kern_dup(DUP_FIXED
, devnull
, i
, &retval
);
2004 * Internal form of close.
2005 * Decrement reference count on file structure.
2006 * Note: td and/or p may be NULL when closing a file
2007 * that was being passed in a message.
2009 * MPALMOSTSAFE - acquires mplock for VOP operations
2012 closef(struct file
*fp
, struct thread
*td
)
2016 struct filedesc_to_leader
*fdtol
;
2023 p
= NULL
; /* allow no proc association */
2025 p
= td
->td_proc
; /* can also be NULL */
2028 * POSIX record locking dictates that any close releases ALL
2029 * locks owned by this process. This is handled by setting
2030 * a flag in the unlock to free ONLY locks obeying POSIX
2031 * semantics, and not to free BSD-style file locks.
2032 * If the descriptor was in a message, POSIX-style locks
2033 * aren't passed with the descriptor.
2035 if (p
!= NULL
&& fp
->f_type
== DTYPE_VNODE
&&
2036 (((struct vnode
*)fp
->f_data
)->v_flag
& VMAYHAVELOCKS
)
2039 if ((p
->p_leader
->p_flag
& P_ADVLOCK
) != 0) {
2040 lf
.l_whence
= SEEK_SET
;
2043 lf
.l_type
= F_UNLCK
;
2044 vp
= (struct vnode
*)fp
->f_data
;
2045 (void) VOP_ADVLOCK(vp
, (caddr_t
)p
->p_leader
, F_UNLCK
,
2049 if (fdtol
!= NULL
) {
2051 * Handle special case where file descriptor table
2052 * is shared between multiple process leaders.
2054 for (fdtol
= fdtol
->fdl_next
;
2055 fdtol
!= p
->p_fdtol
;
2056 fdtol
= fdtol
->fdl_next
) {
2057 if ((fdtol
->fdl_leader
->p_flag
&
2060 fdtol
->fdl_holdcount
++;
2061 lf
.l_whence
= SEEK_SET
;
2064 lf
.l_type
= F_UNLCK
;
2065 vp
= (struct vnode
*)fp
->f_data
;
2066 (void) VOP_ADVLOCK(vp
,
2067 (caddr_t
)fdtol
->fdl_leader
,
2068 F_UNLCK
, &lf
, F_POSIX
);
2069 fdtol
->fdl_holdcount
--;
2070 if (fdtol
->fdl_holdcount
== 0 &&
2071 fdtol
->fdl_wakeup
!= 0) {
2072 fdtol
->fdl_wakeup
= 0;
2085 * fhold() can only be called if f_count is already at least 1 (i.e. the
2086 * caller of fhold() already has a reference to the file pointer in some
2089 * This is a rare case where callers are allowed to hold spinlocks, so
2090 * we can't ourselves. Since we are not obtaining the fp spinlock,
2091 * we have to use an atomic lock to interlock against fdrop().
2094 fhold(struct file
*fp
)
2096 atomic_add_int(&fp
->f_count
, 1);
2100 * A spinlock is required to handle 1->0 transitions on f_count. We have
2101 * to use atomic_sub_int so as not to race the atomic_add_int in fhold().
2103 * MPALMOSTSAFE - acquires mplock for final close sequence
2106 fdrop(struct file
*fp
)
2112 spin_lock_wr(&fp
->f_spin
);
2113 atomic_subtract_int(&fp
->f_count
, 1);
2114 if (fp
->f_count
> 0) {
2115 spin_unlock_wr(&fp
->f_spin
);
2118 spin_unlock_wr(&fp
->f_spin
);
2123 * The last reference has gone away, we own the fp structure free
2126 if (fp
->f_count
< 0)
2127 panic("fdrop: count < 0");
2128 if ((fp
->f_flag
& FHASLOCK
) && fp
->f_type
== DTYPE_VNODE
&&
2129 (((struct vnode
*)fp
->f_data
)->v_flag
& VMAYHAVELOCKS
)
2131 lf
.l_whence
= SEEK_SET
;
2134 lf
.l_type
= F_UNLCK
;
2135 vp
= (struct vnode
*)fp
->f_data
;
2136 (void) VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_UNLCK
, &lf
, 0);
2138 if (fp
->f_ops
!= &badfileops
)
2139 error
= fo_close(fp
);
2148 * Apply an advisory lock on a file descriptor.
2150 * Just attempt to get a record lock of the requested type on
2151 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
2154 sys_flock(struct flock_args
*uap
)
2156 struct proc
*p
= curproc
;
2162 if ((fp
= holdfp(p
->p_fd
, uap
->fd
, -1)) == NULL
)
2164 if (fp
->f_type
!= DTYPE_VNODE
) {
2168 vp
= (struct vnode
*)fp
->f_data
;
2169 lf
.l_whence
= SEEK_SET
;
2172 if (uap
->how
& LOCK_UN
) {
2173 lf
.l_type
= F_UNLCK
;
2174 fp
->f_flag
&= ~FHASLOCK
;
2175 error
= VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_UNLCK
, &lf
, 0);
2178 if (uap
->how
& LOCK_EX
)
2179 lf
.l_type
= F_WRLCK
;
2180 else if (uap
->how
& LOCK_SH
)
2181 lf
.l_type
= F_RDLCK
;
2186 fp
->f_flag
|= FHASLOCK
;
2187 if (uap
->how
& LOCK_NB
)
2188 error
= VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, 0);
2190 error
= VOP_ADVLOCK(vp
, (caddr_t
)fp
, F_SETLK
, &lf
, F_WAIT
);
2197 * File Descriptor pseudo-device driver (/dev/fd/).
2199 * Opening minor device N dup()s the file (if any) connected to file
2200 * descriptor N belonging to the calling process. Note that this driver
2201 * consists of only the ``open()'' routine, because all subsequent
2202 * references to this file will be direct to the other driver.
2206 fdopen(struct dev_open_args
*ap
)
2208 thread_t td
= curthread
;
2210 KKASSERT(td
->td_lwp
!= NULL
);
2213 * XXX Kludge: set curlwp->lwp_dupfd to contain the value of the
2214 * the file descriptor being sought for duplication. The error
2215 * return ensures that the vnode for this device will be released
2216 * by vn_open. Open will detect this special error and take the
2217 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
2218 * will simply report the error.
2220 td
->td_lwp
->lwp_dupfd
= minor(ap
->a_head
.a_dev
);
2225 * The caller has reserved the file descriptor dfd for us. On success we
2226 * must fsetfd() it. On failure the caller will clean it up.
2228 * NOT MPSAFE - isn't getting spinlocks, possibly other things
2231 dupfdopen(struct proc
*p
, int dfd
, int sfd
, int mode
, int error
)
2233 struct filedesc
*fdp
= p
->p_fd
;
2237 if ((wfp
= holdfp(fdp
, sfd
, -1)) == NULL
)
2241 * There are two cases of interest here.
2243 * For ENODEV simply dup sfd to file descriptor dfd and return.
2245 * For ENXIO steal away the file structure from sfd and store it
2246 * dfd. sfd is effectively closed by this operation.
2248 * Any other error code is just returned.
2253 * Check that the mode the file is being opened for is a
2254 * subset of the mode of the existing descriptor.
2256 if (((mode
& (FREAD
|FWRITE
)) | wfp
->f_flag
) != wfp
->f_flag
)
2258 fdp
->fd_files
[dfd
].fileflags
= fdp
->fd_files
[sfd
].fileflags
;
2259 fsetfd(p
, wfp
, dfd
);
2264 * Steal away the file pointer from dfd, and stuff it into indx.
2266 fdp
->fd_files
[dfd
].fileflags
= fdp
->fd_files
[sfd
].fileflags
;
2267 fsetfd(p
, wfp
, dfd
);
2268 if ((xfp
= funsetfd_locked(fdp
, sfd
)) != NULL
)
2270 KKASSERT(xfp
== wfp
); /* XXX MP RACE */
2281 * NOT MPSAFE - I think these refer to a common file descriptor table
2282 * and we need to spinlock that to link fdtol in.
2284 struct filedesc_to_leader
*
2285 filedesc_to_leader_alloc(struct filedesc_to_leader
*old
,
2286 struct proc
*leader
)
2288 struct filedesc_to_leader
*fdtol
;
2290 fdtol
= kmalloc(sizeof(struct filedesc_to_leader
),
2291 M_FILEDESC_TO_LEADER
, M_WAITOK
);
2292 fdtol
->fdl_refcount
= 1;
2293 fdtol
->fdl_holdcount
= 0;
2294 fdtol
->fdl_wakeup
= 0;
2295 fdtol
->fdl_leader
= leader
;
2297 fdtol
->fdl_next
= old
->fdl_next
;
2298 fdtol
->fdl_prev
= old
;
2299 old
->fdl_next
= fdtol
;
2300 fdtol
->fdl_next
->fdl_prev
= fdtol
;
2302 fdtol
->fdl_next
= fdtol
;
2303 fdtol
->fdl_prev
= fdtol
;
2309 * Scan all file pointers in the system. The callback is made with
2310 * both the master list spinlock held and the fp spinlock held,
2315 * WARNING: both the filehead spinlock and the file pointer spinlock are
2316 * held exclusively when the callback is made. The file pointer is not
2320 allfiles_scan_exclusive(int (*callback
)(struct file
*, void *), void *data
)
2325 spin_lock_wr(&filehead_spin
);
2326 LIST_FOREACH(fp
, &filehead
, f_list
) {
2327 spin_lock_wr(&fp
->f_spin
);
2328 res
= callback(fp
, data
);
2329 spin_unlock_wr(&fp
->f_spin
);
2333 spin_unlock_wr(&filehead_spin
);
2337 * Get file structures.
2339 * NOT MPSAFE - process list scan, SYSCTL_OUT (probably not mpsafe)
2342 struct sysctl_kern_file_info
{
2345 struct sysctl_req
*req
;
2348 static int sysctl_kern_file_callback(struct proc
*p
, void *data
);
2351 sysctl_kern_file(SYSCTL_HANDLER_ARGS
)
2353 struct sysctl_kern_file_info info
;
2356 * Note: because the number of file descriptors is calculated
2357 * in different ways for sizing vs returning the data,
2358 * there is information leakage from the first loop. However,
2359 * it is of a similar order of magnitude to the leakage from
2360 * global system statistics such as kern.openfiles.
2362 * When just doing a count, note that we cannot just count
2363 * the elements and add f_count via the filehead list because
2364 * threaded processes share their descriptor table and f_count might
2365 * still be '1' in that case.
2367 * Since the SYSCTL op can block, we must hold the process to
2368 * prevent it being ripped out from under us either in the
2369 * file descriptor loop or in the greater LIST_FOREACH. The
2370 * process may be in varying states of disrepair. If the process
2371 * is in SZOMB we may have caught it just as it is being removed
2372 * from the allproc list, we must skip it in that case to maintain
2373 * an unbroken chain through the allproc list.
2378 allproc_scan(sysctl_kern_file_callback
, &info
);
2381 * When just calculating the size, overestimate a bit to try to
2382 * prevent system activity from causing the buffer-fill call
2385 if (req
->oldptr
== NULL
) {
2386 info
.count
= (info
.count
+ 16) + (info
.count
/ 10);
2387 info
.error
= SYSCTL_OUT(req
, NULL
,
2388 info
.count
* sizeof(struct kinfo_file
));
2390 return (info
.error
);
2394 sysctl_kern_file_callback(struct proc
*p
, void *data
)
2396 struct sysctl_kern_file_info
*info
= data
;
2397 struct kinfo_file kf
;
2398 struct filedesc
*fdp
;
2403 if (p
->p_stat
== SIDL
|| p
->p_stat
== SZOMB
)
2405 if (!PRISON_CHECK(info
->req
->td
->td_proc
->p_ucred
, p
->p_ucred
) != 0)
2407 if ((fdp
= p
->p_fd
) == NULL
)
2409 spin_lock_rd(&fdp
->fd_spin
);
2410 for (n
= 0; n
< fdp
->fd_nfiles
; ++n
) {
2411 if ((fp
= fdp
->fd_files
[n
].fp
) == NULL
)
2413 if (info
->req
->oldptr
== NULL
) {
2416 uid
= p
->p_ucred
? p
->p_ucred
->cr_uid
: -1;
2417 kcore_make_file(&kf
, fp
, p
->p_pid
, uid
, n
);
2418 spin_unlock_rd(&fdp
->fd_spin
);
2419 info
->error
= SYSCTL_OUT(info
->req
, &kf
, sizeof(kf
));
2420 spin_lock_rd(&fdp
->fd_spin
);
2425 spin_unlock_rd(&fdp
->fd_spin
);
2431 SYSCTL_PROC(_kern
, KERN_FILE
, file
, CTLTYPE_OPAQUE
|CTLFLAG_RD
,
2432 0, 0, sysctl_kern_file
, "S,file", "Entire file table");
2434 SYSCTL_INT(_kern
, KERN_MAXFILESPERPROC
, maxfilesperproc
, CTLFLAG_RW
,
2435 &maxfilesperproc
, 0, "Maximum files allowed open per process");
2437 SYSCTL_INT(_kern
, KERN_MAXFILES
, maxfiles
, CTLFLAG_RW
,
2438 &maxfiles
, 0, "Maximum number of files");
2440 SYSCTL_INT(_kern
, OID_AUTO
, maxfilesrootres
, CTLFLAG_RW
,
2441 &maxfilesrootres
, 0, "Descriptors reserved for root use");
2443 SYSCTL_INT(_kern
, OID_AUTO
, openfiles
, CTLFLAG_RD
,
2444 &nfiles
, 0, "System-wide number of open files");
2447 fildesc_drvinit(void *unused
)
2451 dev_ops_add(&fildesc_ops
, 0, 0);
2452 for (fd
= 0; fd
< NUMFDESC
; fd
++) {
2453 make_dev(&fildesc_ops
, fd
,
2454 UID_BIN
, GID_BIN
, 0666, "fd/%d", fd
);
2456 make_dev(&fildesc_ops
, 0, UID_ROOT
, GID_WHEEL
, 0666, "stdin");
2457 make_dev(&fildesc_ops
, 1, UID_ROOT
, GID_WHEEL
, 0666, "stdout");
2458 make_dev(&fildesc_ops
, 2, UID_ROOT
, GID_WHEEL
, 0666, "stderr");
2464 struct fileops badfileops
= {
2465 .fo_read
= badfo_readwrite
,
2466 .fo_write
= badfo_readwrite
,
2467 .fo_ioctl
= badfo_ioctl
,
2468 .fo_poll
= badfo_poll
,
2469 .fo_kqfilter
= badfo_kqfilter
,
2470 .fo_stat
= badfo_stat
,
2471 .fo_close
= badfo_close
,
2472 .fo_shutdown
= badfo_shutdown
2492 badfo_ioctl(struct file
*fp
, u_long com
, caddr_t data
, struct ucred
*cred
)
2501 badfo_poll(struct file
*fp
, int events
, struct ucred
*cred
)
2510 badfo_kqfilter(struct file
*fp
, struct knote
*kn
)
2516 badfo_stat(struct file
*fp
, struct stat
*sb
, struct ucred
*cred
)
2525 badfo_close(struct file
*fp
)
2534 badfo_shutdown(struct file
*fp
, int how
)
2543 nofo_shutdown(struct file
*fp
, int how
)
2545 return (EOPNOTSUPP
);
2548 SYSINIT(fildescdev
,SI_SUB_DRIVERS
,SI_ORDER_MIDDLE
+CDEV_MAJOR
,
2549 fildesc_drvinit
,NULL
)