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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)kern_descrip.c 8.6 (Berkeley) 4/19/94
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "opt_capsicum.h"
41 #include "opt_compat.h"
43 #include "opt_ktrace.h"
44 #include "opt_procdesc.h"
46 #include <sys/param.h>
47 #include <sys/systm.h>
49 #include <sys/capability.h>
51 #include <sys/domain.h>
52 #include <sys/fcntl.h>
54 #include <sys/filedesc.h>
55 #include <sys/filio.h>
57 #include <sys/kernel.h>
58 #include <sys/limits.h>
60 #include <sys/malloc.h>
61 #include <sys/mount.h>
62 #include <sys/mqueue.h>
63 #include <sys/mutex.h>
64 #include <sys/namei.h>
65 #include <sys/selinfo.h>
69 #include <sys/procdesc.h>
70 #include <sys/protosw.h>
71 #include <sys/racct.h>
72 #include <sys/resourcevar.h>
73 #include <sys/signalvar.h>
74 #include <sys/socketvar.h>
77 #include <sys/syscallsubr.h>
78 #include <sys/sysctl.h>
79 #include <sys/sysproto.h>
81 #include <sys/unistd.h>
83 #include <sys/unpcb.h>
85 #include <sys/vnode.h>
87 #include <sys/ktrace.h>
92 #include <netinet/in.h>
93 #include <netinet/in_pcb.h>
95 #include <security/audit/audit.h>
102 static MALLOC_DEFINE(M_FILEDESC, "filedesc", "Open file descriptor table");
103 static MALLOC_DEFINE(M_FILEDESC_TO_LEADER, "filedesc_to_leader",
104 "file desc to leader structures");
105 static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures");
107 static uma_zone_t file_zone;
110 /* Flags for do_dup() */
111 #define DUP_FIXED 0x1 /* Force fixed allocation */
112 #define DUP_FCNTL 0x2 /* fcntl()-style errors */
114 static int do_dup(struct thread *td, int flags, int old, int new,
116 static int fd_first_free(struct filedesc *, int, int);
117 static int fd_last_used(struct filedesc *, int, int);
118 static void fdgrowtable(struct filedesc *, int);
119 static void fdunused(struct filedesc *fdp, int fd);
120 static void fdused(struct filedesc *fdp, int fd);
121 static int fill_vnode_info(struct vnode *vp, struct kinfo_file *kif);
122 static int fill_socket_info(struct socket *so, struct kinfo_file *kif);
123 static int fill_pts_info(struct tty *tp, struct kinfo_file *kif);
124 static int fill_pipe_info(struct pipe *pi, struct kinfo_file *kif);
125 static int fill_procdesc_info(struct procdesc *pdp,
126 struct kinfo_file *kif);
129 * A process is initially started out with NDFILE descriptors stored within
130 * this structure, selected to be enough for typical applications based on
131 * the historical limit of 20 open files (and the usage of descriptors by
132 * shells). If these descriptors are exhausted, a larger descriptor table
133 * may be allocated, up to a process' resource limit; the internal arrays
137 #define NDSLOTSIZE sizeof(NDSLOTTYPE)
138 #define NDENTRIES (NDSLOTSIZE * __CHAR_BIT)
139 #define NDSLOT(x) ((x) / NDENTRIES)
140 #define NDBIT(x) ((NDSLOTTYPE)1 << ((x) % NDENTRIES))
141 #define NDSLOTS(x) (((x) + NDENTRIES - 1) / NDENTRIES)
144 * Storage required per open file descriptor.
146 #define OFILESIZE (sizeof(struct file *) + sizeof(char))
149 * Storage to hold unused ofiles that need to be reclaimed.
152 struct file **ft_table;
153 SLIST_ENTRY(freetable) ft_next;
157 * Basic allocation of descriptors:
158 * one of the above, plus arrays for NDFILE descriptors.
161 struct filedesc fd_fd;
163 * ofiles which need to be reclaimed on free.
165 SLIST_HEAD(,freetable) fd_free;
167 * These arrays are used when the number of open files is
168 * <= NDFILE, and are then pointed to by the pointers above.
170 struct file *fd_dfiles[NDFILE];
171 char fd_dfileflags[NDFILE];
172 NDSLOTTYPE fd_dmap[NDSLOTS(NDFILE)];
176 * Descriptor management.
178 volatile int openfiles; /* actual number of open files */
179 struct mtx sigio_lock; /* mtx to protect pointers to sigio */
180 void (*mq_fdclose)(struct thread *td, int fd, struct file *fp);
182 /* A mutex to protect the association between a proc and filedesc. */
183 static struct mtx fdesc_mtx;
186 * Find the first zero bit in the given bitmap, starting at low and not
187 * exceeding size - 1.
190 fd_first_free(struct filedesc *fdp, int low, int size)
192 NDSLOTTYPE *map = fdp->fd_map;
200 if (low % NDENTRIES) {
201 mask = ~(~(NDSLOTTYPE)0 >> (NDENTRIES - (low % NDENTRIES)));
202 if ((mask &= ~map[off]) != 0UL)
203 return (off * NDENTRIES + ffsl(mask) - 1);
206 for (maxoff = NDSLOTS(size); off < maxoff; ++off)
207 if (map[off] != ~0UL)
208 return (off * NDENTRIES + ffsl(~map[off]) - 1);
213 * Find the highest non-zero bit in the given bitmap, starting at low and
214 * not exceeding size - 1.
217 fd_last_used(struct filedesc *fdp, int low, int size)
219 NDSLOTTYPE *map = fdp->fd_map;
227 if (size % NDENTRIES) {
228 mask = ~(~(NDSLOTTYPE)0 << (size % NDENTRIES));
229 if ((mask &= map[off]) != 0)
230 return (off * NDENTRIES + flsl(mask) - 1);
233 for (minoff = NDSLOT(low); off >= minoff; --off)
235 return (off * NDENTRIES + flsl(map[off]) - 1);
240 fdisused(struct filedesc *fdp, int fd)
242 KASSERT(fd >= 0 && fd < fdp->fd_nfiles,
243 ("file descriptor %d out of range (0, %d)", fd, fdp->fd_nfiles));
244 return ((fdp->fd_map[NDSLOT(fd)] & NDBIT(fd)) != 0);
248 * Mark a file descriptor as used.
251 fdused(struct filedesc *fdp, int fd)
254 FILEDESC_XLOCK_ASSERT(fdp);
255 KASSERT(!fdisused(fdp, fd),
256 ("fd already used"));
258 fdp->fd_map[NDSLOT(fd)] |= NDBIT(fd);
259 if (fd > fdp->fd_lastfile)
260 fdp->fd_lastfile = fd;
261 if (fd == fdp->fd_freefile)
262 fdp->fd_freefile = fd_first_free(fdp, fd, fdp->fd_nfiles);
266 * Mark a file descriptor as unused.
269 fdunused(struct filedesc *fdp, int fd)
272 FILEDESC_XLOCK_ASSERT(fdp);
273 KASSERT(fdisused(fdp, fd),
274 ("fd is already unused"));
275 KASSERT(fdp->fd_ofiles[fd] == NULL,
276 ("fd is still in use"));
278 fdp->fd_map[NDSLOT(fd)] &= ~NDBIT(fd);
279 if (fd < fdp->fd_freefile)
280 fdp->fd_freefile = fd;
281 if (fd == fdp->fd_lastfile)
282 fdp->fd_lastfile = fd_last_used(fdp, 0, fd);
286 * System calls on descriptors.
288 #ifndef _SYS_SYSPROTO_H_
289 struct getdtablesize_args {
295 sys_getdtablesize(struct thread *td, struct getdtablesize_args *uap)
297 struct proc *p = td->td_proc;
302 min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
303 lim = racct_get_limit(td->td_proc, RACCT_NOFILE);
305 if (lim < td->td_retval[0])
306 td->td_retval[0] = lim;
311 * Duplicate a file descriptor to a particular value.
313 * Note: keep in mind that a potential race condition exists when closing
314 * descriptors from a shared descriptor table (via rfork).
316 #ifndef _SYS_SYSPROTO_H_
324 sys_dup2(struct thread *td, struct dup2_args *uap)
327 return (do_dup(td, DUP_FIXED, (int)uap->from, (int)uap->to,
332 * Duplicate a file descriptor.
334 #ifndef _SYS_SYSPROTO_H_
341 sys_dup(struct thread *td, struct dup_args *uap)
344 return (do_dup(td, 0, (int)uap->fd, 0, td->td_retval));
348 * The file control system call.
350 #ifndef _SYS_SYSPROTO_H_
359 sys_fcntl(struct thread *td, struct fcntl_args *uap)
374 * Convert old flock structure to new.
376 error = copyin((void *)(intptr_t)uap->arg, &ofl, sizeof(ofl));
377 fl.l_start = ofl.l_start;
378 fl.l_len = ofl.l_len;
379 fl.l_pid = ofl.l_pid;
380 fl.l_type = ofl.l_type;
381 fl.l_whence = ofl.l_whence;
401 error = copyin((void *)(intptr_t)uap->arg, &fl, sizeof(fl));
410 error = kern_fcntl(td, uap->fd, cmd, arg);
413 if (uap->cmd == F_OGETLK) {
414 ofl.l_start = fl.l_start;
415 ofl.l_len = fl.l_len;
416 ofl.l_pid = fl.l_pid;
417 ofl.l_type = fl.l_type;
418 ofl.l_whence = fl.l_whence;
419 error = copyout(&ofl, (void *)(intptr_t)uap->arg, sizeof(ofl));
420 } else if (uap->cmd == F_GETLK) {
421 error = copyout(&fl, (void *)(intptr_t)uap->arg, sizeof(fl));
426 static inline struct file *
427 fdtofp(int fd, struct filedesc *fdp)
431 FILEDESC_LOCK_ASSERT(fdp);
432 if ((unsigned)fd >= fdp->fd_nfiles ||
433 (fp = fdp->fd_ofiles[fd]) == NULL)
439 fdunwrap(int fd, cap_rights_t rights, struct filedesc *fdp, struct file **fpp)
442 *fpp = fdtofp(fd, fdp);
447 if ((*fpp)->f_type == DTYPE_CAPABILITY) {
448 int err = cap_funwrap(*fpp, rights, fpp);
454 #endif /* CAPABILITIES */
459 kern_fcntl(struct thread *td, int fd, int cmd, intptr_t arg)
461 struct filedesc *fdp;
481 error = do_dup(td, DUP_FCNTL, fd, tmp, td->td_retval);
486 error = do_dup(td, DUP_FIXED, fd, tmp, td->td_retval);
491 if ((fp = fdtofp(fd, fdp)) == NULL) {
492 FILEDESC_SUNLOCK(fdp);
496 pop = &fdp->fd_ofileflags[fd];
497 td->td_retval[0] = (*pop & UF_EXCLOSE) ? FD_CLOEXEC : 0;
498 FILEDESC_SUNLOCK(fdp);
503 if ((fp = fdtofp(fd, fdp)) == NULL) {
504 FILEDESC_XUNLOCK(fdp);
508 pop = &fdp->fd_ofileflags[fd];
509 *pop = (*pop &~ UF_EXCLOSE) |
510 (arg & FD_CLOEXEC ? UF_EXCLOSE : 0);
511 FILEDESC_XUNLOCK(fdp);
516 error = fdunwrap(fd, CAP_FCNTL, fdp, &fp);
518 FILEDESC_SUNLOCK(fdp);
521 td->td_retval[0] = OFLAGS(fp->f_flag);
522 FILEDESC_SUNLOCK(fdp);
527 error = fdunwrap(fd, CAP_FCNTL, fdp, &fp);
529 FILEDESC_SUNLOCK(fdp);
533 FILEDESC_SUNLOCK(fdp);
535 tmp = flg = fp->f_flag;
537 tmp |= FFLAGS(arg & ~O_ACCMODE) & FCNTLFLAGS;
538 } while(atomic_cmpset_int(&fp->f_flag, flg, tmp) == 0);
539 tmp = fp->f_flag & FNONBLOCK;
540 error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
545 tmp = fp->f_flag & FASYNC;
546 error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td);
551 atomic_clear_int(&fp->f_flag, FNONBLOCK);
553 (void)fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
559 error = fdunwrap(fd, CAP_FCNTL, fdp, &fp);
561 FILEDESC_SUNLOCK(fdp);
565 FILEDESC_SUNLOCK(fdp);
566 error = fo_ioctl(fp, FIOGETOWN, &tmp, td->td_ucred, td);
568 td->td_retval[0] = tmp;
574 error = fdunwrap(fd, CAP_FCNTL, fdp, &fp);
576 FILEDESC_SUNLOCK(fdp);
580 FILEDESC_SUNLOCK(fdp);
582 error = fo_ioctl(fp, FIOSETOWN, &tmp, td->td_ucred, td);
587 error = priv_check(td, PRIV_NFS_LOCKD);
595 /* FALLTHROUGH F_SETLK */
600 error = fdunwrap(fd, CAP_FLOCK, fdp, &fp);
602 FILEDESC_SUNLOCK(fdp);
605 if (fp->f_type != DTYPE_VNODE) {
606 FILEDESC_SUNLOCK(fdp);
610 flp = (struct flock *)arg;
611 if (flp->l_whence == SEEK_CUR) {
612 if (fp->f_offset < 0 ||
614 fp->f_offset > OFF_MAX - flp->l_start)) {
615 FILEDESC_SUNLOCK(fdp);
619 flp->l_start += fp->f_offset;
623 * VOP_ADVLOCK() may block.
626 FILEDESC_SUNLOCK(fdp);
628 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
629 switch (flp->l_type) {
631 if ((fp->f_flag & FREAD) == 0) {
635 PROC_LOCK(p->p_leader);
636 p->p_leader->p_flag |= P_ADVLOCK;
637 PROC_UNLOCK(p->p_leader);
638 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
642 if ((fp->f_flag & FWRITE) == 0) {
646 PROC_LOCK(p->p_leader);
647 p->p_leader->p_flag |= P_ADVLOCK;
648 PROC_UNLOCK(p->p_leader);
649 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
653 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
658 * Temporary api for testing remote lock
661 if (flg != F_REMOTE) {
665 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
666 F_UNLCKSYS, flp, flg);
672 VFS_UNLOCK_GIANT(vfslocked);
674 /* Check for race with close */
676 if ((unsigned) fd >= fdp->fd_nfiles ||
677 fp != fdp->fd_ofiles[fd]) {
678 FILEDESC_SUNLOCK(fdp);
679 flp->l_whence = SEEK_SET;
682 flp->l_type = F_UNLCK;
683 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
684 (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
685 F_UNLCK, flp, F_POSIX);
686 VFS_UNLOCK_GIANT(vfslocked);
689 FILEDESC_SUNLOCK(fdp);
695 error = fdunwrap(fd, CAP_FLOCK, fdp, &fp);
697 FILEDESC_SUNLOCK(fdp);
700 if (fp->f_type != DTYPE_VNODE) {
701 FILEDESC_SUNLOCK(fdp);
705 flp = (struct flock *)arg;
706 if (flp->l_type != F_RDLCK && flp->l_type != F_WRLCK &&
707 flp->l_type != F_UNLCK) {
708 FILEDESC_SUNLOCK(fdp);
712 if (flp->l_whence == SEEK_CUR) {
713 if ((flp->l_start > 0 &&
714 fp->f_offset > OFF_MAX - flp->l_start) ||
716 fp->f_offset < OFF_MIN - flp->l_start)) {
717 FILEDESC_SUNLOCK(fdp);
721 flp->l_start += fp->f_offset;
724 * VOP_ADVLOCK() may block.
727 FILEDESC_SUNLOCK(fdp);
729 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
730 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK, flp,
732 VFS_UNLOCK_GIANT(vfslocked);
738 arg = arg ? 128 * 1024: 0;
742 if ((fp = fdtofp(fd, fdp)) == NULL) {
743 FILEDESC_SUNLOCK(fdp);
747 if (fp->f_type != DTYPE_VNODE) {
748 FILEDESC_SUNLOCK(fdp);
753 FILEDESC_SUNLOCK(fdp);
756 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
757 error = vn_lock(vp, LK_SHARED);
759 goto readahead_vnlock_fail;
760 bsize = fp->f_vnode->v_mount->mnt_stat.f_iosize;
762 fp->f_seqcount = (arg + bsize - 1) / bsize;
764 new = old = fp->f_flag;
766 } while (!atomic_cmpset_rel_int(&fp->f_flag, old, new));
767 readahead_vnlock_fail:
768 VFS_UNLOCK_GIANT(vfslocked);
772 new = old = fp->f_flag;
774 } while (!atomic_cmpset_rel_int(&fp->f_flag, old, new));
783 VFS_UNLOCK_GIANT(vfslocked);
788 * Common code for dup, dup2, fcntl(F_DUPFD) and fcntl(F_DUP2FD).
791 do_dup(struct thread *td, int flags, int old, int new,
794 struct filedesc *fdp;
798 int error, holdleaders, maxfd;
804 * Verify we have a valid descriptor to dup from and possibly to
805 * dup to. Unlike dup() and dup2(), fcntl()'s F_DUPFD should
806 * return EINVAL when the new descriptor is out of bounds.
811 return (flags & DUP_FCNTL ? EINVAL : EBADF);
813 maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
816 return (flags & DUP_FCNTL ? EINVAL : EMFILE);
819 if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL) {
820 FILEDESC_XUNLOCK(fdp);
823 if (flags & DUP_FIXED && old == new) {
825 FILEDESC_XUNLOCK(fdp);
828 fp = fdp->fd_ofiles[old];
832 * If the caller specified a file descriptor, make sure the file
833 * table is large enough to hold it, and grab it. Otherwise, just
834 * allocate a new descriptor the usual way. Since the filedesc
835 * lock may be temporarily dropped in the process, we have to look
838 if (flags & DUP_FIXED) {
839 if (new >= fdp->fd_nfiles) {
841 * The resource limits are here instead of e.g. fdalloc(),
842 * because the file descriptor table may be shared between
843 * processes, so we can't really use racct_add()/racct_sub().
844 * Instead of counting the number of actually allocated
845 * descriptors, just put the limit on the size of the file
850 error = racct_set(p, RACCT_NOFILE, new + 1);
853 FILEDESC_XUNLOCK(fdp);
858 fdgrowtable(fdp, new + 1);
860 if (fdp->fd_ofiles[new] == NULL)
863 if ((error = fdalloc(td, new, &new)) != 0) {
864 FILEDESC_XUNLOCK(fdp);
871 * If the old file changed out from under us then treat it as a
872 * bad file descriptor. Userland should do its own locking to
875 if (fdp->fd_ofiles[old] != fp) {
876 /* we've allocated a descriptor which we won't use */
877 if (fdp->fd_ofiles[new] == NULL)
879 FILEDESC_XUNLOCK(fdp);
884 ("new fd is same as old"));
887 * Save info on the descriptor being overwritten. We cannot close
888 * it without introducing an ownership race for the slot, since we
889 * need to drop the filedesc lock to call closef().
891 * XXX this duplicates parts of close().
893 delfp = fdp->fd_ofiles[new];
896 if (td->td_proc->p_fdtol != NULL) {
898 * Ask fdfree() to sleep to ensure that all relevant
899 * process leaders can be traversed in closef().
901 fdp->fd_holdleaderscount++;
907 * Duplicate the source descriptor
909 fdp->fd_ofiles[new] = fp;
910 fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
911 if (new > fdp->fd_lastfile)
912 fdp->fd_lastfile = new;
916 * If we dup'd over a valid file, we now own the reference to it
917 * and must dispose of it using closef() semantics (as if a
918 * close() were performed on it).
920 * XXX this duplicates parts of close().
923 knote_fdclose(td, new);
924 if (delfp->f_type == DTYPE_MQUEUE)
925 mq_fdclose(td, new, delfp);
926 FILEDESC_XUNLOCK(fdp);
927 (void) closef(delfp, td);
930 fdp->fd_holdleaderscount--;
931 if (fdp->fd_holdleaderscount == 0 &&
932 fdp->fd_holdleaderswakeup != 0) {
933 fdp->fd_holdleaderswakeup = 0;
934 wakeup(&fdp->fd_holdleaderscount);
936 FILEDESC_XUNLOCK(fdp);
939 FILEDESC_XUNLOCK(fdp);
945 * If sigio is on the list associated with a process or process group,
946 * disable signalling from the device, remove sigio from the list and
950 funsetown(struct sigio **sigiop)
960 *(sigio->sio_myref) = NULL;
961 if ((sigio)->sio_pgid < 0) {
962 struct pgrp *pg = (sigio)->sio_pgrp;
964 SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio,
968 struct proc *p = (sigio)->sio_proc;
970 SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio,
975 crfree(sigio->sio_ucred);
976 free(sigio, M_SIGIO);
980 * Free a list of sigio structures.
981 * We only need to lock the SIGIO_LOCK because we have made ourselves
982 * inaccessible to callers of fsetown and therefore do not need to lock
983 * the proc or pgrp struct for the list manipulation.
986 funsetownlst(struct sigiolst *sigiolst)
992 sigio = SLIST_FIRST(sigiolst);
999 * Every entry of the list should belong
1000 * to a single proc or pgrp.
1002 if (sigio->sio_pgid < 0) {
1003 pg = sigio->sio_pgrp;
1004 PGRP_LOCK_ASSERT(pg, MA_NOTOWNED);
1005 } else /* if (sigio->sio_pgid > 0) */ {
1006 p = sigio->sio_proc;
1007 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
1011 while ((sigio = SLIST_FIRST(sigiolst)) != NULL) {
1012 *(sigio->sio_myref) = NULL;
1014 KASSERT(sigio->sio_pgid < 0,
1015 ("Proc sigio in pgrp sigio list"));
1016 KASSERT(sigio->sio_pgrp == pg,
1017 ("Bogus pgrp in sigio list"));
1019 SLIST_REMOVE(&pg->pg_sigiolst, sigio, sigio,
1022 } else /* if (p != NULL) */ {
1023 KASSERT(sigio->sio_pgid > 0,
1024 ("Pgrp sigio in proc sigio list"));
1025 KASSERT(sigio->sio_proc == p,
1026 ("Bogus proc in sigio list"));
1028 SLIST_REMOVE(&p->p_sigiolst, sigio, sigio,
1033 crfree(sigio->sio_ucred);
1034 free(sigio, M_SIGIO);
1041 * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg).
1043 * After permission checking, add a sigio structure to the sigio list for
1044 * the process or process group.
1047 fsetown(pid_t pgid, struct sigio **sigiop)
1051 struct sigio *sigio;
1061 /* Allocate and fill in the new sigio out of locks. */
1062 sigio = malloc(sizeof(struct sigio), M_SIGIO, M_WAITOK);
1063 sigio->sio_pgid = pgid;
1064 sigio->sio_ucred = crhold(curthread->td_ucred);
1065 sigio->sio_myref = sigiop;
1067 sx_slock(&proctree_lock);
1076 * Policy - Don't allow a process to FSETOWN a process
1077 * in another session.
1079 * Remove this test to allow maximum flexibility or
1080 * restrict FSETOWN to the current process or process
1081 * group for maximum safety.
1084 if (proc->p_session != curthread->td_proc->p_session) {
1090 } else /* if (pgid < 0) */ {
1091 pgrp = pgfind(-pgid);
1099 * Policy - Don't allow a process to FSETOWN a process
1100 * in another session.
1102 * Remove this test to allow maximum flexibility or
1103 * restrict FSETOWN to the current process or process
1104 * group for maximum safety.
1106 if (pgrp->pg_session != curthread->td_proc->p_session) {
1117 * Since funsetownlst() is called without the proctree
1118 * locked, we need to check for P_WEXIT.
1119 * XXX: is ESRCH correct?
1121 if ((proc->p_flag & P_WEXIT) != 0) {
1126 SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio);
1127 sigio->sio_proc = proc;
1131 SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
1132 sigio->sio_pgrp = pgrp;
1135 sx_sunlock(&proctree_lock);
1142 sx_sunlock(&proctree_lock);
1143 crfree(sigio->sio_ucred);
1144 free(sigio, M_SIGIO);
1149 * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
1153 struct sigio **sigiop;
1158 pgid = (*sigiop != NULL) ? (*sigiop)->sio_pgid : 0;
1164 * Close a file descriptor.
1166 #ifndef _SYS_SYSPROTO_H_
1175 struct close_args *uap;
1178 return (kern_close(td, uap->fd));
1186 struct filedesc *fdp;
1187 struct file *fp, *fp_object;
1193 fdp = td->td_proc->p_fd;
1195 AUDIT_SYSCLOSE(td, fd);
1197 FILEDESC_XLOCK(fdp);
1198 if ((unsigned)fd >= fdp->fd_nfiles ||
1199 (fp = fdp->fd_ofiles[fd]) == NULL) {
1200 FILEDESC_XUNLOCK(fdp);
1203 fdp->fd_ofiles[fd] = NULL;
1204 fdp->fd_ofileflags[fd] = 0;
1206 if (td->td_proc->p_fdtol != NULL) {
1208 * Ask fdfree() to sleep to ensure that all relevant
1209 * process leaders can be traversed in closef().
1211 fdp->fd_holdleaderscount++;
1216 * We now hold the fp reference that used to be owned by the
1217 * descriptor array. We have to unlock the FILEDESC *AFTER*
1218 * knote_fdclose to prevent a race of the fd getting opened, a knote
1219 * added, and deleteing a knote for the new fd.
1221 knote_fdclose(td, fd);
1224 * When we're closing an fd with a capability, we need to notify
1225 * mqueue if the underlying object is of type mqueue.
1227 (void)cap_funwrap(fp, 0, &fp_object);
1228 if (fp_object->f_type == DTYPE_MQUEUE)
1229 mq_fdclose(td, fd, fp_object);
1230 FILEDESC_XUNLOCK(fdp);
1232 error = closef(fp, td);
1234 FILEDESC_XLOCK(fdp);
1235 fdp->fd_holdleaderscount--;
1236 if (fdp->fd_holdleaderscount == 0 &&
1237 fdp->fd_holdleaderswakeup != 0) {
1238 fdp->fd_holdleaderswakeup = 0;
1239 wakeup(&fdp->fd_holdleaderscount);
1241 FILEDESC_XUNLOCK(fdp);
1247 * Close open file descriptors.
1249 #ifndef _SYS_SYSPROTO_H_
1250 struct closefrom_args {
1256 sys_closefrom(struct thread *td, struct closefrom_args *uap)
1258 struct filedesc *fdp;
1261 fdp = td->td_proc->p_fd;
1262 AUDIT_ARG_FD(uap->lowfd);
1265 * Treat negative starting file descriptor values identical to
1266 * closefrom(0) which closes all files.
1270 FILEDESC_SLOCK(fdp);
1271 for (fd = uap->lowfd; fd < fdp->fd_nfiles; fd++) {
1272 if (fdp->fd_ofiles[fd] != NULL) {
1273 FILEDESC_SUNLOCK(fdp);
1274 (void)kern_close(td, fd);
1275 FILEDESC_SLOCK(fdp);
1278 FILEDESC_SUNLOCK(fdp);
1282 #if defined(COMPAT_43)
1284 * Return status information about a file descriptor.
1286 #ifndef _SYS_SYSPROTO_H_
1287 struct ofstat_args {
1294 ofstat(struct thread *td, struct ofstat_args *uap)
1300 error = kern_fstat(td, uap->fd, &ub);
1303 error = copyout(&oub, uap->sb, sizeof(oub));
1307 #endif /* COMPAT_43 */
1310 * Return status information about a file descriptor.
1312 #ifndef _SYS_SYSPROTO_H_
1320 sys_fstat(struct thread *td, struct fstat_args *uap)
1325 error = kern_fstat(td, uap->fd, &ub);
1327 error = copyout(&ub, uap->sb, sizeof(ub));
1332 kern_fstat(struct thread *td, int fd, struct stat *sbp)
1339 if ((error = fget(td, fd, CAP_FSTAT, &fp)) != 0)
1342 AUDIT_ARG_FILE(td->td_proc, fp);
1344 error = fo_stat(fp, sbp, td->td_ucred, td);
1347 if (error == 0 && KTRPOINT(td, KTR_STRUCT))
1354 * Return status information about a file descriptor.
1356 #ifndef _SYS_SYSPROTO_H_
1357 struct nfstat_args {
1364 sys_nfstat(struct thread *td, struct nfstat_args *uap)
1370 error = kern_fstat(td, uap->fd, &ub);
1372 cvtnstat(&ub, &nub);
1373 error = copyout(&nub, uap->sb, sizeof(nub));
1379 * Return pathconf information about a file descriptor.
1381 #ifndef _SYS_SYSPROTO_H_
1382 struct fpathconf_args {
1389 sys_fpathconf(struct thread *td, struct fpathconf_args *uap)
1395 if ((error = fget(td, uap->fd, CAP_FPATHCONF, &fp)) != 0)
1398 /* If asynchronous I/O is available, it works for all descriptors. */
1399 if (uap->name == _PC_ASYNC_IO) {
1400 td->td_retval[0] = async_io_version;
1406 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1407 vn_lock(vp, LK_SHARED | LK_RETRY);
1408 error = VOP_PATHCONF(vp, uap->name, td->td_retval);
1410 VFS_UNLOCK_GIANT(vfslocked);
1411 } else if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) {
1412 if (uap->name != _PC_PIPE_BUF) {
1415 td->td_retval[0] = PIPE_BUF;
1427 * Grow the file table to accomodate (at least) nfd descriptors. This may
1428 * block and drop the filedesc lock, but it will reacquire it before
1432 fdgrowtable(struct filedesc *fdp, int nfd)
1434 struct filedesc0 *fdp0;
1435 struct freetable *fo;
1436 struct file **ntable;
1437 struct file **otable;
1439 int nnfiles, onfiles;
1442 FILEDESC_XLOCK_ASSERT(fdp);
1444 KASSERT(fdp->fd_nfiles > 0,
1445 ("zero-length file table"));
1447 /* compute the size of the new table */
1448 onfiles = fdp->fd_nfiles;
1449 nnfiles = NDSLOTS(nfd) * NDENTRIES; /* round up */
1450 if (nnfiles <= onfiles)
1451 /* the table is already large enough */
1454 /* allocate a new table and (if required) new bitmaps */
1455 FILEDESC_XUNLOCK(fdp);
1456 ntable = malloc((nnfiles * OFILESIZE) + sizeof(struct freetable),
1457 M_FILEDESC, M_ZERO | M_WAITOK);
1458 nfileflags = (char *)&ntable[nnfiles];
1459 if (NDSLOTS(nnfiles) > NDSLOTS(onfiles))
1460 nmap = malloc(NDSLOTS(nnfiles) * NDSLOTSIZE,
1461 M_FILEDESC, M_ZERO | M_WAITOK);
1464 FILEDESC_XLOCK(fdp);
1467 * We now have new tables ready to go. Since we dropped the
1468 * filedesc lock to call malloc(), watch out for a race.
1470 onfiles = fdp->fd_nfiles;
1471 if (onfiles >= nnfiles) {
1472 /* we lost the race, but that's OK */
1473 free(ntable, M_FILEDESC);
1475 free(nmap, M_FILEDESC);
1478 bcopy(fdp->fd_ofiles, ntable, onfiles * sizeof(*ntable));
1479 bcopy(fdp->fd_ofileflags, nfileflags, onfiles);
1480 otable = fdp->fd_ofiles;
1481 fdp->fd_ofileflags = nfileflags;
1482 fdp->fd_ofiles = ntable;
1484 * We must preserve ofiles until the process exits because we can't
1485 * be certain that no threads have references to the old table via
1488 if (onfiles > NDFILE) {
1489 fo = (struct freetable *)&otable[onfiles];
1490 fdp0 = (struct filedesc0 *)fdp;
1491 fo->ft_table = otable;
1492 SLIST_INSERT_HEAD(&fdp0->fd_free, fo, ft_next);
1494 if (NDSLOTS(nnfiles) > NDSLOTS(onfiles)) {
1495 bcopy(fdp->fd_map, nmap, NDSLOTS(onfiles) * sizeof(*nmap));
1496 if (NDSLOTS(onfiles) > NDSLOTS(NDFILE))
1497 free(fdp->fd_map, M_FILEDESC);
1500 fdp->fd_nfiles = nnfiles;
1504 * Allocate a file descriptor for the process.
1507 fdalloc(struct thread *td, int minfd, int *result)
1509 struct proc *p = td->td_proc;
1510 struct filedesc *fdp = p->p_fd;
1516 FILEDESC_XLOCK_ASSERT(fdp);
1518 if (fdp->fd_freefile > minfd)
1519 minfd = fdp->fd_freefile;
1522 maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
1526 * Search the bitmap for a free descriptor. If none is found, try
1527 * to grow the file table. Keep at it until we either get a file
1528 * descriptor or run into process or system limits; fdgrowtable()
1529 * may drop the filedesc lock, so we're in a race.
1532 fd = fd_first_free(fdp, minfd, fdp->fd_nfiles);
1535 if (fd < fdp->fd_nfiles)
1539 error = racct_set(p, RACCT_NOFILE, min(fdp->fd_nfiles * 2, maxfd));
1544 fdgrowtable(fdp, min(fdp->fd_nfiles * 2, maxfd));
1548 * Perform some sanity checks, then mark the file descriptor as
1549 * used and return it to the caller.
1551 KASSERT(!fdisused(fdp, fd),
1552 ("fd_first_free() returned non-free descriptor"));
1553 KASSERT(fdp->fd_ofiles[fd] == NULL,
1554 ("free descriptor isn't"));
1555 fdp->fd_ofileflags[fd] = 0; /* XXX needed? */
1562 * Check to see whether n user file descriptors are available to the process
1566 fdavail(struct thread *td, int n)
1568 struct proc *p = td->td_proc;
1569 struct filedesc *fdp = td->td_proc->p_fd;
1573 FILEDESC_LOCK_ASSERT(fdp);
1576 * XXX: This is only called from uipc_usrreq.c:unp_externalize();
1577 * call racct_add() from there instead of dealing with containers
1581 lim = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
1583 if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
1585 last = min(fdp->fd_nfiles, lim);
1586 fpp = &fdp->fd_ofiles[fdp->fd_freefile];
1587 for (i = last - fdp->fd_freefile; --i >= 0; fpp++) {
1588 if (*fpp == NULL && --n <= 0)
1595 * Create a new open file structure and allocate a file decriptor for the
1596 * process that refers to it. We add one reference to the file for the
1597 * descriptor table and one reference for resultfp. This is to prevent us
1598 * being preempted and the entry in the descriptor table closed after we
1599 * release the FILEDESC lock.
1602 falloc(struct thread *td, struct file **resultfp, int *resultfd, int flags)
1607 error = falloc_noinstall(td, &fp);
1609 return (error); /* no reference held on error */
1611 error = finstall(td, fp, &fd, flags);
1613 fdrop(fp, td); /* one reference (fp only) */
1617 if (resultfp != NULL)
1618 *resultfp = fp; /* copy out result */
1620 fdrop(fp, td); /* release local reference */
1622 if (resultfd != NULL)
1629 * Create a new open file structure without allocating a file descriptor.
1632 falloc_noinstall(struct thread *td, struct file **resultfp)
1635 int maxuserfiles = maxfiles - (maxfiles / 20);
1636 static struct timeval lastfail;
1639 KASSERT(resultfp != NULL, ("%s: resultfp == NULL", __func__));
1641 if ((openfiles >= maxuserfiles &&
1642 priv_check(td, PRIV_MAXFILES) != 0) ||
1643 openfiles >= maxfiles) {
1644 if (ppsratecheck(&lastfail, &curfail, 1)) {
1645 printf("kern.maxfiles limit exceeded by uid %i, "
1646 "please see tuning(7).\n", td->td_ucred->cr_ruid);
1650 atomic_add_int(&openfiles, 1);
1651 fp = uma_zalloc(file_zone, M_WAITOK | M_ZERO);
1652 refcount_init(&fp->f_count, 1);
1653 fp->f_cred = crhold(td->td_ucred);
1654 fp->f_ops = &badfileops;
1662 * Install a file in a file descriptor table.
1665 finstall(struct thread *td, struct file *fp, int *fd, int flags)
1667 struct filedesc *fdp = td->td_proc->p_fd;
1670 KASSERT(fd != NULL, ("%s: fd == NULL", __func__));
1671 KASSERT(fp != NULL, ("%s: fp == NULL", __func__));
1673 FILEDESC_XLOCK(fdp);
1674 if ((error = fdalloc(td, 0, fd))) {
1675 FILEDESC_XUNLOCK(fdp);
1679 fdp->fd_ofiles[*fd] = fp;
1680 if ((flags & O_CLOEXEC) != 0)
1681 fdp->fd_ofileflags[*fd] |= UF_EXCLOSE;
1682 FILEDESC_XUNLOCK(fdp);
1687 * Build a new filedesc structure from another.
1688 * Copy the current, root, and jail root vnode references.
1691 fdinit(struct filedesc *fdp)
1693 struct filedesc0 *newfdp;
1695 newfdp = malloc(sizeof *newfdp, M_FILEDESC, M_WAITOK | M_ZERO);
1696 FILEDESC_LOCK_INIT(&newfdp->fd_fd);
1698 FILEDESC_XLOCK(fdp);
1699 newfdp->fd_fd.fd_cdir = fdp->fd_cdir;
1700 if (newfdp->fd_fd.fd_cdir)
1701 VREF(newfdp->fd_fd.fd_cdir);
1702 newfdp->fd_fd.fd_rdir = fdp->fd_rdir;
1703 if (newfdp->fd_fd.fd_rdir)
1704 VREF(newfdp->fd_fd.fd_rdir);
1705 newfdp->fd_fd.fd_jdir = fdp->fd_jdir;
1706 if (newfdp->fd_fd.fd_jdir)
1707 VREF(newfdp->fd_fd.fd_jdir);
1708 FILEDESC_XUNLOCK(fdp);
1711 /* Create the file descriptor table. */
1712 newfdp->fd_fd.fd_refcnt = 1;
1713 newfdp->fd_fd.fd_holdcnt = 1;
1714 newfdp->fd_fd.fd_cmask = CMASK;
1715 newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
1716 newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
1717 newfdp->fd_fd.fd_nfiles = NDFILE;
1718 newfdp->fd_fd.fd_map = newfdp->fd_dmap;
1719 newfdp->fd_fd.fd_lastfile = -1;
1720 return (&newfdp->fd_fd);
1723 static struct filedesc *
1724 fdhold(struct proc *p)
1726 struct filedesc *fdp;
1728 mtx_lock(&fdesc_mtx);
1732 mtx_unlock(&fdesc_mtx);
1737 fddrop(struct filedesc *fdp)
1739 struct filedesc0 *fdp0;
1740 struct freetable *ft;
1743 mtx_lock(&fdesc_mtx);
1744 i = --fdp->fd_holdcnt;
1745 mtx_unlock(&fdesc_mtx);
1749 FILEDESC_LOCK_DESTROY(fdp);
1750 fdp0 = (struct filedesc0 *)fdp;
1751 while ((ft = SLIST_FIRST(&fdp0->fd_free)) != NULL) {
1752 SLIST_REMOVE_HEAD(&fdp0->fd_free, ft_next);
1753 free(ft->ft_table, M_FILEDESC);
1755 free(fdp, M_FILEDESC);
1759 * Share a filedesc structure.
1762 fdshare(struct filedesc *fdp)
1765 FILEDESC_XLOCK(fdp);
1767 FILEDESC_XUNLOCK(fdp);
1772 * Unshare a filedesc structure, if necessary by making a copy
1775 fdunshare(struct proc *p, struct thread *td)
1778 FILEDESC_XLOCK(p->p_fd);
1779 if (p->p_fd->fd_refcnt > 1) {
1780 struct filedesc *tmp;
1782 FILEDESC_XUNLOCK(p->p_fd);
1783 tmp = fdcopy(p->p_fd);
1787 FILEDESC_XUNLOCK(p->p_fd);
1791 * Copy a filedesc structure. A NULL pointer in returns a NULL reference,
1792 * this is to ease callers, not catch errors.
1795 fdcopy(struct filedesc *fdp)
1797 struct filedesc *newfdp;
1800 /* Certain daemons might not have file descriptors. */
1804 newfdp = fdinit(fdp);
1805 FILEDESC_SLOCK(fdp);
1806 while (fdp->fd_lastfile >= newfdp->fd_nfiles) {
1807 FILEDESC_SUNLOCK(fdp);
1808 FILEDESC_XLOCK(newfdp);
1809 fdgrowtable(newfdp, fdp->fd_lastfile + 1);
1810 FILEDESC_XUNLOCK(newfdp);
1811 FILEDESC_SLOCK(fdp);
1813 /* copy all passable descriptors (i.e. not kqueue) */
1814 newfdp->fd_freefile = -1;
1815 for (i = 0; i <= fdp->fd_lastfile; ++i) {
1816 if (fdisused(fdp, i) &&
1817 (fdp->fd_ofiles[i]->f_ops->fo_flags & DFLAG_PASSABLE) &&
1818 fdp->fd_ofiles[i]->f_ops != &badfileops) {
1819 newfdp->fd_ofiles[i] = fdp->fd_ofiles[i];
1820 newfdp->fd_ofileflags[i] = fdp->fd_ofileflags[i];
1821 fhold(newfdp->fd_ofiles[i]);
1822 newfdp->fd_lastfile = i;
1824 if (newfdp->fd_freefile == -1)
1825 newfdp->fd_freefile = i;
1828 newfdp->fd_cmask = fdp->fd_cmask;
1829 FILEDESC_SUNLOCK(fdp);
1830 FILEDESC_XLOCK(newfdp);
1831 for (i = 0; i <= newfdp->fd_lastfile; ++i)
1832 if (newfdp->fd_ofiles[i] != NULL)
1834 if (newfdp->fd_freefile == -1)
1835 newfdp->fd_freefile = i;
1836 FILEDESC_XUNLOCK(newfdp);
1841 * Release a filedesc structure.
1844 fdfree(struct thread *td)
1846 struct filedesc *fdp;
1849 struct filedesc_to_leader *fdtol;
1851 struct vnode *cdir, *jdir, *rdir, *vp;
1854 /* Certain daemons might not have file descriptors. */
1855 fdp = td->td_proc->p_fd;
1860 PROC_LOCK(td->td_proc);
1861 racct_set(td->td_proc, RACCT_NOFILE, 0);
1862 PROC_UNLOCK(td->td_proc);
1865 /* Check for special need to clear POSIX style locks */
1866 fdtol = td->td_proc->p_fdtol;
1867 if (fdtol != NULL) {
1868 FILEDESC_XLOCK(fdp);
1869 KASSERT(fdtol->fdl_refcount > 0,
1870 ("filedesc_to_refcount botch: fdl_refcount=%d",
1871 fdtol->fdl_refcount));
1872 if (fdtol->fdl_refcount == 1 &&
1873 (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1874 for (i = 0, fpp = fdp->fd_ofiles;
1875 i <= fdp->fd_lastfile;
1878 (*fpp)->f_type != DTYPE_VNODE)
1882 FILEDESC_XUNLOCK(fdp);
1883 lf.l_whence = SEEK_SET;
1886 lf.l_type = F_UNLCK;
1888 locked = VFS_LOCK_GIANT(vp->v_mount);
1889 (void) VOP_ADVLOCK(vp,
1890 (caddr_t)td->td_proc->
1895 VFS_UNLOCK_GIANT(locked);
1896 FILEDESC_XLOCK(fdp);
1898 fpp = fdp->fd_ofiles + i;
1902 if (fdtol->fdl_refcount == 1) {
1903 if (fdp->fd_holdleaderscount > 0 &&
1904 (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1906 * close() or do_dup() has cleared a reference
1907 * in a shared file descriptor table.
1909 fdp->fd_holdleaderswakeup = 1;
1910 sx_sleep(&fdp->fd_holdleaderscount,
1911 FILEDESC_LOCK(fdp), PLOCK, "fdlhold", 0);
1914 if (fdtol->fdl_holdcount > 0) {
1916 * Ensure that fdtol->fdl_leader remains
1917 * valid in closef().
1919 fdtol->fdl_wakeup = 1;
1920 sx_sleep(fdtol, FILEDESC_LOCK(fdp), PLOCK,
1925 fdtol->fdl_refcount--;
1926 if (fdtol->fdl_refcount == 0 &&
1927 fdtol->fdl_holdcount == 0) {
1928 fdtol->fdl_next->fdl_prev = fdtol->fdl_prev;
1929 fdtol->fdl_prev->fdl_next = fdtol->fdl_next;
1932 td->td_proc->p_fdtol = NULL;
1933 FILEDESC_XUNLOCK(fdp);
1935 free(fdtol, M_FILEDESC_TO_LEADER);
1937 FILEDESC_XLOCK(fdp);
1938 i = --fdp->fd_refcnt;
1939 FILEDESC_XUNLOCK(fdp);
1943 fpp = fdp->fd_ofiles;
1944 for (i = fdp->fd_lastfile; i-- >= 0; fpp++) {
1946 FILEDESC_XLOCK(fdp);
1949 FILEDESC_XUNLOCK(fdp);
1950 (void) closef(fp, td);
1953 FILEDESC_XLOCK(fdp);
1955 /* XXX This should happen earlier. */
1956 mtx_lock(&fdesc_mtx);
1957 td->td_proc->p_fd = NULL;
1958 mtx_unlock(&fdesc_mtx);
1960 if (fdp->fd_nfiles > NDFILE)
1961 free(fdp->fd_ofiles, M_FILEDESC);
1962 if (NDSLOTS(fdp->fd_nfiles) > NDSLOTS(NDFILE))
1963 free(fdp->fd_map, M_FILEDESC);
1967 cdir = fdp->fd_cdir;
1968 fdp->fd_cdir = NULL;
1969 rdir = fdp->fd_rdir;
1970 fdp->fd_rdir = NULL;
1971 jdir = fdp->fd_jdir;
1972 fdp->fd_jdir = NULL;
1973 FILEDESC_XUNLOCK(fdp);
1976 locked = VFS_LOCK_GIANT(cdir->v_mount);
1978 VFS_UNLOCK_GIANT(locked);
1981 locked = VFS_LOCK_GIANT(rdir->v_mount);
1983 VFS_UNLOCK_GIANT(locked);
1986 locked = VFS_LOCK_GIANT(jdir->v_mount);
1988 VFS_UNLOCK_GIANT(locked);
1995 * For setugid programs, we don't want to people to use that setugidness
1996 * to generate error messages which write to a file which otherwise would
1997 * otherwise be off-limits to the process. We check for filesystems where
1998 * the vnode can change out from under us after execve (like [lin]procfs).
2000 * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
2001 * sufficient. We also don't check for setugidness since we know we are.
2004 is_unsafe(struct file *fp)
2006 if (fp->f_type == DTYPE_VNODE) {
2007 struct vnode *vp = fp->f_vnode;
2009 if ((vp->v_vflag & VV_PROCDEP) != 0)
2016 * Make this setguid thing safe, if at all possible.
2019 setugidsafety(struct thread *td)
2021 struct filedesc *fdp;
2024 /* Certain daemons might not have file descriptors. */
2025 fdp = td->td_proc->p_fd;
2030 * Note: fdp->fd_ofiles may be reallocated out from under us while
2031 * we are blocked in a close. Be careful!
2033 FILEDESC_XLOCK(fdp);
2034 for (i = 0; i <= fdp->fd_lastfile; i++) {
2037 if (fdp->fd_ofiles[i] && is_unsafe(fdp->fd_ofiles[i])) {
2040 knote_fdclose(td, i);
2042 * NULL-out descriptor prior to close to avoid
2043 * a race while close blocks.
2045 fp = fdp->fd_ofiles[i];
2046 fdp->fd_ofiles[i] = NULL;
2047 fdp->fd_ofileflags[i] = 0;
2049 FILEDESC_XUNLOCK(fdp);
2050 (void) closef(fp, td);
2051 FILEDESC_XLOCK(fdp);
2054 FILEDESC_XUNLOCK(fdp);
2058 * If a specific file object occupies a specific file descriptor, close the
2059 * file descriptor entry and drop a reference on the file object. This is a
2060 * convenience function to handle a subsequent error in a function that calls
2061 * falloc() that handles the race that another thread might have closed the
2062 * file descriptor out from under the thread creating the file object.
2065 fdclose(struct filedesc *fdp, struct file *fp, int idx, struct thread *td)
2068 FILEDESC_XLOCK(fdp);
2069 if (fdp->fd_ofiles[idx] == fp) {
2070 fdp->fd_ofiles[idx] = NULL;
2072 FILEDESC_XUNLOCK(fdp);
2075 FILEDESC_XUNLOCK(fdp);
2079 * Close any files on exec?
2082 fdcloseexec(struct thread *td)
2084 struct filedesc *fdp;
2087 /* Certain daemons might not have file descriptors. */
2088 fdp = td->td_proc->p_fd;
2092 FILEDESC_XLOCK(fdp);
2095 * We cannot cache fd_ofiles or fd_ofileflags since operations
2096 * may block and rip them out from under us.
2098 for (i = 0; i <= fdp->fd_lastfile; i++) {
2099 if (fdp->fd_ofiles[i] != NULL &&
2100 (fdp->fd_ofiles[i]->f_type == DTYPE_MQUEUE ||
2101 (fdp->fd_ofileflags[i] & UF_EXCLOSE))) {
2104 knote_fdclose(td, i);
2106 * NULL-out descriptor prior to close to avoid
2107 * a race while close blocks.
2109 fp = fdp->fd_ofiles[i];
2110 fdp->fd_ofiles[i] = NULL;
2111 fdp->fd_ofileflags[i] = 0;
2113 if (fp->f_type == DTYPE_MQUEUE)
2114 mq_fdclose(td, i, fp);
2115 FILEDESC_XUNLOCK(fdp);
2116 (void) closef(fp, td);
2117 FILEDESC_XLOCK(fdp);
2120 FILEDESC_XUNLOCK(fdp);
2124 * It is unsafe for set[ug]id processes to be started with file
2125 * descriptors 0..2 closed, as these descriptors are given implicit
2126 * significance in the Standard C library. fdcheckstd() will create a
2127 * descriptor referencing /dev/null for each of stdin, stdout, and
2128 * stderr that is not already open.
2131 fdcheckstd(struct thread *td)
2133 struct filedesc *fdp;
2134 register_t retval, save;
2135 int i, error, devnull;
2137 fdp = td->td_proc->p_fd;
2140 KASSERT(fdp->fd_refcnt == 1, ("the fdtable should not be shared"));
2143 for (i = 0; i < 3; i++) {
2144 if (fdp->fd_ofiles[i] != NULL)
2147 save = td->td_retval[0];
2148 error = kern_open(td, "/dev/null", UIO_SYSSPACE,
2150 devnull = td->td_retval[0];
2151 td->td_retval[0] = save;
2154 KASSERT(devnull == i, ("oof, we didn't get our fd"));
2156 error = do_dup(td, DUP_FIXED, devnull, i, &retval);
2165 * Internal form of close. Decrement reference count on file structure.
2166 * Note: td may be NULL when closing a file that was being passed in a
2169 * XXXRW: Giant is not required for the caller, but often will be held; this
2170 * makes it moderately likely the Giant will be recursed in the VFS case.
2173 closef(struct file *fp, struct thread *td)
2177 struct filedesc_to_leader *fdtol;
2178 struct filedesc *fdp;
2179 struct file *fp_object;
2182 * POSIX record locking dictates that any close releases ALL
2183 * locks owned by this process. This is handled by setting
2184 * a flag in the unlock to free ONLY locks obeying POSIX
2185 * semantics, and not to free BSD-style file locks.
2186 * If the descriptor was in a message, POSIX-style locks
2187 * aren't passed with the descriptor, and the thread pointer
2188 * will be NULL. Callers should be careful only to pass a
2189 * NULL thread pointer when there really is no owning
2190 * context that might have locks, or the locks will be
2193 * If this is a capability, we do lock processing under the underlying
2194 * node, not the capability itself.
2196 (void)cap_funwrap(fp, 0, &fp_object);
2197 if ((fp_object->f_type == DTYPE_VNODE) && (td != NULL)) {
2200 vp = fp_object->f_vnode;
2201 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2202 if ((td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
2203 lf.l_whence = SEEK_SET;
2206 lf.l_type = F_UNLCK;
2207 (void) VOP_ADVLOCK(vp, (caddr_t)td->td_proc->p_leader,
2208 F_UNLCK, &lf, F_POSIX);
2210 fdtol = td->td_proc->p_fdtol;
2211 if (fdtol != NULL) {
2213 * Handle special case where file descriptor table is
2214 * shared between multiple process leaders.
2216 fdp = td->td_proc->p_fd;
2217 FILEDESC_XLOCK(fdp);
2218 for (fdtol = fdtol->fdl_next;
2219 fdtol != td->td_proc->p_fdtol;
2220 fdtol = fdtol->fdl_next) {
2221 if ((fdtol->fdl_leader->p_flag &
2224 fdtol->fdl_holdcount++;
2225 FILEDESC_XUNLOCK(fdp);
2226 lf.l_whence = SEEK_SET;
2229 lf.l_type = F_UNLCK;
2230 vp = fp_object->f_vnode;
2231 (void) VOP_ADVLOCK(vp,
2232 (caddr_t)fdtol->fdl_leader,
2233 F_UNLCK, &lf, F_POSIX);
2234 FILEDESC_XLOCK(fdp);
2235 fdtol->fdl_holdcount--;
2236 if (fdtol->fdl_holdcount == 0 &&
2237 fdtol->fdl_wakeup != 0) {
2238 fdtol->fdl_wakeup = 0;
2242 FILEDESC_XUNLOCK(fdp);
2244 VFS_UNLOCK_GIANT(vfslocked);
2246 return (fdrop(fp, td));
2250 * Initialize the file pointer with the specified properties.
2252 * The ops are set with release semantics to be certain that the flags, type,
2253 * and data are visible when ops is. This is to prevent ops methods from being
2254 * called with bad data.
2257 finit(struct file *fp, u_int flag, short type, void *data, struct fileops *ops)
2262 atomic_store_rel_ptr((volatile uintptr_t *)&fp->f_ops, (uintptr_t)ops);
2266 fget_unlocked(struct filedesc *fdp, int fd)
2271 if (fd < 0 || fd >= fdp->fd_nfiles)
2274 * Fetch the descriptor locklessly. We avoid fdrop() races by
2275 * never raising a refcount above 0. To accomplish this we have
2276 * to use a cmpset loop rather than an atomic_add. The descriptor
2277 * must be re-verified once we acquire a reference to be certain
2278 * that the identity is still correct and we did not lose a race
2279 * due to preemption.
2282 fp = fdp->fd_ofiles[fd];
2285 count = fp->f_count;
2289 * Use an acquire barrier to prevent caching of fd_ofiles
2290 * so it is refreshed for verification.
2292 if (atomic_cmpset_acq_int(&fp->f_count, count, count + 1) != 1)
2294 if (fp == fdp->fd_ofiles[fd])
2296 fdrop(fp, curthread);
2303 * Extract the file pointer associated with the specified descriptor for the
2304 * current user process.
2306 * If the descriptor doesn't exist or doesn't match 'flags', EBADF is
2309 * If the FGET_GETCAP flag is set, the capability itself will be returned.
2310 * Calling _fget() with FGET_GETCAP on a non-capability will return EINVAL.
2311 * Otherwise, if the file is a capability, its rights will be checked against
2312 * the capability rights mask, and if successful, the object will be unwrapped.
2314 * If an error occured the non-zero error is returned and *fpp is set to
2315 * NULL. Otherwise *fpp is held and set and zero is returned. Caller is
2316 * responsible for fdrop().
2318 #define FGET_GETCAP 0x00000001
2320 _fget(struct thread *td, int fd, struct file **fpp, int flags,
2321 cap_rights_t needrights, cap_rights_t *haverightsp, u_char *maxprotp,
2324 struct filedesc *fdp;
2327 struct file *fp_fromcap;
2332 if (td == NULL || (fdp = td->td_proc->p_fd) == NULL)
2334 if ((fp = fget_unlocked(fdp, fd)) == NULL)
2336 if (fp->f_ops == &badfileops) {
2343 * If this is a capability, what rights does it have?
2345 if (haverightsp != NULL) {
2346 if (fp->f_type == DTYPE_CAPABILITY)
2347 *haverightsp = cap_rights(fp);
2349 *haverightsp = CAP_MASK_VALID;
2353 * If a capability has been requested, return the capability directly.
2354 * Otherwise, check capability rights, extract the underlying object,
2355 * and check its access flags.
2357 if (fget_flags & FGET_GETCAP) {
2358 if (fp->f_type != DTYPE_CAPABILITY) {
2363 if (maxprotp == NULL)
2364 error = cap_funwrap(fp, needrights, &fp_fromcap);
2366 error = cap_funwrap_mmap(fp, needrights, maxprotp,
2374 * If we've unwrapped a file, drop the original capability
2375 * and hold the new descriptor. fp after this point refers to
2376 * the actual (unwrapped) object, not the capability.
2378 if (fp != fp_fromcap) {
2384 #else /* !CAPABILITIES */
2385 KASSERT(fp->f_type != DTYPE_CAPABILITY,
2386 ("%s: saw capability", __func__));
2387 if (maxprotp != NULL)
2388 *maxprotp = VM_PROT_ALL;
2389 #endif /* CAPABILITIES */
2392 * FREAD and FWRITE failure return EBADF as per POSIX.
2394 * Only one flag, or 0, may be specified.
2396 if ((flags == FREAD && (fp->f_flag & FREAD) == 0) ||
2397 (flags == FWRITE && (fp->f_flag & FWRITE) == 0)) {
2406 fget(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
2409 return(_fget(td, fd, fpp, 0, rights, NULL, NULL, 0));
2413 fget_mmap(struct thread *td, int fd, cap_rights_t rights, u_char *maxprotp,
2417 return (_fget(td, fd, fpp, 0, rights, NULL, maxprotp, 0));
2421 fget_read(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
2424 return(_fget(td, fd, fpp, FREAD, rights, NULL, NULL, 0));
2428 fget_write(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
2431 return (_fget(td, fd, fpp, FWRITE, rights, NULL, NULL, 0));
2435 * Unlike the other fget() calls, which accept and check capability rights
2436 * but never return capabilities, fgetcap() returns the capability but doesn't
2437 * check capability rights.
2440 fgetcap(struct thread *td, int fd, struct file **fpp)
2443 return (_fget(td, fd, fpp, 0, 0, NULL, NULL, FGET_GETCAP));
2448 * Like fget() but loads the underlying vnode, or returns an error if the
2449 * descriptor does not represent a vnode. Note that pipes use vnodes but
2450 * never have VM objects. The returned vnode will be vref()'d.
2452 * XXX: what about the unused flags ?
2455 _fgetvp(struct thread *td, int fd, int flags, cap_rights_t needrights,
2456 cap_rights_t *haverightsp, struct vnode **vpp)
2462 if ((error = _fget(td, fd, &fp, flags, needrights, haverightsp,
2465 if (fp->f_vnode == NULL) {
2477 fgetvp(struct thread *td, int fd, cap_rights_t rights, struct vnode **vpp)
2480 return (_fgetvp(td, fd, 0, rights, NULL, vpp));
2484 fgetvp_rights(struct thread *td, int fd, cap_rights_t need, cap_rights_t *have,
2487 return (_fgetvp(td, fd, 0, need, have, vpp));
2491 fgetvp_read(struct thread *td, int fd, cap_rights_t rights, struct vnode **vpp)
2494 return (_fgetvp(td, fd, FREAD, rights, NULL, vpp));
2499 fgetvp_write(struct thread *td, int fd, cap_rights_t rights,
2503 return (_fgetvp(td, fd, FWRITE, rights, NULL, vpp));
2508 * Like fget() but loads the underlying socket, or returns an error if the
2509 * descriptor does not represent a socket.
2511 * We bump the ref count on the returned socket. XXX Also obtain the SX lock
2514 * Note: fgetsock() and fputsock() are deprecated, as consumers should rely
2515 * on their file descriptor reference to prevent the socket from being free'd
2519 fgetsock(struct thread *td, int fd, cap_rights_t rights, struct socket **spp,
2528 if ((error = _fget(td, fd, &fp, 0, rights, NULL, NULL, 0)) != 0)
2530 if (fp->f_type != DTYPE_SOCKET) {
2535 *fflagp = fp->f_flag;
2546 * Drop the reference count on the socket and XXX release the SX lock in the
2547 * future. The last reference closes the socket.
2549 * Note: fputsock() is deprecated, see comment for fgetsock().
2552 fputsock(struct socket *so)
2557 CURVNET_SET(so->so_vnet);
2563 * Handle the last reference to a file being closed.
2565 * No special capability handling here, as the capability's fo_close will run
2566 * instead of the object here, and perform any necessary drop on the object.
2569 _fdrop(struct file *fp, struct thread *td)
2574 if (fp->f_count != 0)
2575 panic("fdrop: count %d", fp->f_count);
2576 if (fp->f_ops != &badfileops)
2577 error = fo_close(fp, td);
2579 * The f_cdevpriv cannot be assigned non-NULL value while we
2580 * are destroying the file.
2582 if (fp->f_cdevpriv != NULL)
2584 atomic_subtract_int(&openfiles, 1);
2586 uma_zfree(file_zone, fp);
2592 * Apply an advisory lock on a file descriptor.
2594 * Just attempt to get a record lock of the requested type on the entire file
2595 * (l_whence = SEEK_SET, l_start = 0, l_len = 0).
2597 #ifndef _SYS_SYSPROTO_H_
2605 sys_flock(struct thread *td, struct flock_args *uap)
2613 if ((error = fget(td, uap->fd, CAP_FLOCK, &fp)) != 0)
2615 if (fp->f_type != DTYPE_VNODE) {
2617 return (EOPNOTSUPP);
2621 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2622 lf.l_whence = SEEK_SET;
2625 if (uap->how & LOCK_UN) {
2626 lf.l_type = F_UNLCK;
2627 atomic_clear_int(&fp->f_flag, FHASLOCK);
2628 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
2631 if (uap->how & LOCK_EX)
2632 lf.l_type = F_WRLCK;
2633 else if (uap->how & LOCK_SH)
2634 lf.l_type = F_RDLCK;
2639 atomic_set_int(&fp->f_flag, FHASLOCK);
2640 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
2641 (uap->how & LOCK_NB) ? F_FLOCK : F_FLOCK | F_WAIT);
2644 VFS_UNLOCK_GIANT(vfslocked);
2648 * Duplicate the specified descriptor to a free descriptor.
2651 dupfdopen(struct thread *td, struct filedesc *fdp, int indx, int dfd, int mode, int error)
2657 * If the to-be-dup'd fd number is greater than the allowed number
2658 * of file descriptors, or the fd to be dup'd has already been
2659 * closed, then reject.
2661 FILEDESC_XLOCK(fdp);
2662 if (dfd < 0 || dfd >= fdp->fd_nfiles ||
2663 (wfp = fdp->fd_ofiles[dfd]) == NULL) {
2664 FILEDESC_XUNLOCK(fdp);
2669 * There are two cases of interest here.
2671 * For ENODEV simply dup (dfd) to file descriptor (indx) and return.
2673 * For ENXIO steal away the file structure from (dfd) and store it in
2674 * (indx). (dfd) is effectively closed by this operation.
2676 * Any other error code is just returned.
2681 * Check that the mode the file is being opened for is a
2682 * subset of the mode of the existing descriptor.
2684 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
2685 FILEDESC_XUNLOCK(fdp);
2688 fp = fdp->fd_ofiles[indx];
2689 fdp->fd_ofiles[indx] = wfp;
2690 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
2694 FILEDESC_XUNLOCK(fdp);
2697 * We now own the reference to fp that the ofiles[]
2698 * array used to own. Release it.
2705 * Steal away the file pointer from dfd and stuff it into indx.
2707 fp = fdp->fd_ofiles[indx];
2708 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
2709 fdp->fd_ofiles[dfd] = NULL;
2710 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
2711 fdp->fd_ofileflags[dfd] = 0;
2715 FILEDESC_XUNLOCK(fdp);
2718 * We now own the reference to fp that the ofiles[] array
2719 * used to own. Release it.
2726 FILEDESC_XUNLOCK(fdp);
2733 * Scan all active processes and prisons to see if any of them have a current
2734 * or root directory of `olddp'. If so, replace them with the new mount point.
2737 mountcheckdirs(struct vnode *olddp, struct vnode *newdp)
2739 struct filedesc *fdp;
2744 if (vrefcnt(olddp) == 1)
2747 sx_slock(&allproc_lock);
2748 FOREACH_PROC_IN_SYSTEM(p) {
2752 FILEDESC_XLOCK(fdp);
2753 if (fdp->fd_cdir == olddp) {
2755 fdp->fd_cdir = newdp;
2758 if (fdp->fd_rdir == olddp) {
2760 fdp->fd_rdir = newdp;
2763 if (fdp->fd_jdir == olddp) {
2765 fdp->fd_jdir = newdp;
2768 FILEDESC_XUNLOCK(fdp);
2771 sx_sunlock(&allproc_lock);
2772 if (rootvnode == olddp) {
2777 mtx_lock(&prison0.pr_mtx);
2778 if (prison0.pr_root == olddp) {
2780 prison0.pr_root = newdp;
2783 mtx_unlock(&prison0.pr_mtx);
2784 sx_slock(&allprison_lock);
2785 TAILQ_FOREACH(pr, &allprison, pr_list) {
2786 mtx_lock(&pr->pr_mtx);
2787 if (pr->pr_root == olddp) {
2789 pr->pr_root = newdp;
2792 mtx_unlock(&pr->pr_mtx);
2794 sx_sunlock(&allprison_lock);
2799 struct filedesc_to_leader *
2800 filedesc_to_leader_alloc(struct filedesc_to_leader *old, struct filedesc *fdp, struct proc *leader)
2802 struct filedesc_to_leader *fdtol;
2804 fdtol = malloc(sizeof(struct filedesc_to_leader),
2805 M_FILEDESC_TO_LEADER,
2807 fdtol->fdl_refcount = 1;
2808 fdtol->fdl_holdcount = 0;
2809 fdtol->fdl_wakeup = 0;
2810 fdtol->fdl_leader = leader;
2812 FILEDESC_XLOCK(fdp);
2813 fdtol->fdl_next = old->fdl_next;
2814 fdtol->fdl_prev = old;
2815 old->fdl_next = fdtol;
2816 fdtol->fdl_next->fdl_prev = fdtol;
2817 FILEDESC_XUNLOCK(fdp);
2819 fdtol->fdl_next = fdtol;
2820 fdtol->fdl_prev = fdtol;
2826 * Get file structures globally.
2829 sysctl_kern_file(SYSCTL_HANDLER_ARGS)
2832 struct filedesc *fdp;
2837 error = sysctl_wire_old_buffer(req, 0);
2840 if (req->oldptr == NULL) {
2842 sx_slock(&allproc_lock);
2843 FOREACH_PROC_IN_SYSTEM(p) {
2844 if (p->p_state == PRS_NEW)
2849 /* overestimates sparse tables. */
2850 if (fdp->fd_lastfile > 0)
2851 n += fdp->fd_lastfile;
2854 sx_sunlock(&allproc_lock);
2855 return (SYSCTL_OUT(req, 0, n * sizeof(xf)));
2858 bzero(&xf, sizeof(xf));
2859 xf.xf_size = sizeof(xf);
2860 sx_slock(&allproc_lock);
2861 FOREACH_PROC_IN_SYSTEM(p) {
2863 if (p->p_state == PRS_NEW) {
2867 if (p_cansee(req->td, p) != 0) {
2871 xf.xf_pid = p->p_pid;
2872 xf.xf_uid = p->p_ucred->cr_uid;
2877 FILEDESC_SLOCK(fdp);
2878 for (n = 0; fdp->fd_refcnt > 0 && n < fdp->fd_nfiles; ++n) {
2879 if ((fp = fdp->fd_ofiles[n]) == NULL)
2883 xf.xf_data = fp->f_data;
2884 xf.xf_vnode = fp->f_vnode;
2885 xf.xf_type = fp->f_type;
2886 xf.xf_count = fp->f_count;
2888 xf.xf_offset = fp->f_offset;
2889 xf.xf_flag = fp->f_flag;
2890 error = SYSCTL_OUT(req, &xf, sizeof(xf));
2894 FILEDESC_SUNLOCK(fdp);
2899 sx_sunlock(&allproc_lock);
2903 SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
2904 0, 0, sysctl_kern_file, "S,xfile", "Entire file table");
2906 #ifdef KINFO_OFILE_SIZE
2907 CTASSERT(sizeof(struct kinfo_ofile) == KINFO_OFILE_SIZE);
2910 #ifdef COMPAT_FREEBSD7
2912 export_vnode_for_osysctl(struct vnode *vp, int type,
2913 struct kinfo_ofile *kif, struct filedesc *fdp, struct sysctl_req *req)
2916 char *fullpath, *freepath;
2919 bzero(kif, sizeof(*kif));
2920 kif->kf_structsize = sizeof(*kif);
2924 kif->kf_type = KF_TYPE_VNODE;
2925 /* This function only handles directories. */
2926 if (vp->v_type != VDIR) {
2930 kif->kf_vnode_type = KF_VTYPE_VDIR;
2933 * This is not a true file descriptor, so we set a bogus refcount
2934 * and offset to indicate these fields should be ignored.
2936 kif->kf_ref_count = -1;
2937 kif->kf_offset = -1;
2941 FILEDESC_SUNLOCK(fdp);
2942 vn_fullpath(curthread, vp, &fullpath, &freepath);
2943 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2945 VFS_UNLOCK_GIANT(vfslocked);
2946 strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path));
2947 if (freepath != NULL)
2948 free(freepath, M_TEMP);
2949 error = SYSCTL_OUT(req, kif, sizeof(*kif));
2950 FILEDESC_SLOCK(fdp);
2955 * Get per-process file descriptors for use by procstat(1), et al.
2958 sysctl_kern_proc_ofiledesc(SYSCTL_HANDLER_ARGS)
2960 char *fullpath, *freepath;
2961 struct kinfo_ofile *kif;
2962 struct filedesc *fdp;
2963 int error, i, *name;
2972 if ((p = pfind((pid_t)name[0])) == NULL)
2974 if ((error = p_candebug(curthread, p))) {
2982 kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK);
2983 FILEDESC_SLOCK(fdp);
2984 if (fdp->fd_cdir != NULL)
2985 export_vnode_for_osysctl(fdp->fd_cdir, KF_FD_TYPE_CWD, kif,
2987 if (fdp->fd_rdir != NULL)
2988 export_vnode_for_osysctl(fdp->fd_rdir, KF_FD_TYPE_ROOT, kif,
2990 if (fdp->fd_jdir != NULL)
2991 export_vnode_for_osysctl(fdp->fd_jdir, KF_FD_TYPE_JAIL, kif,
2993 for (i = 0; i < fdp->fd_nfiles; i++) {
2994 if ((fp = fdp->fd_ofiles[i]) == NULL)
2996 bzero(kif, sizeof(*kif));
2997 kif->kf_structsize = sizeof(*kif);
3005 * When reporting a capability, most fields will be from the
3006 * underlying object, but do mark as a capability. With
3007 * ofiledesc, we don't have a field to export the cap_rights_t,
3008 * but we do with the new filedesc.
3010 if (fp->f_type == DTYPE_CAPABILITY) {
3011 kif->kf_flags |= KF_FLAG_CAPABILITY;
3012 (void)cap_funwrap(fp, 0, &fp);
3015 KASSERT(fp->f_type != DTYPE_CAPABILITY,
3016 ("sysctl_kern_proc_ofiledesc: saw capability"));
3018 switch (fp->f_type) {
3020 kif->kf_type = KF_TYPE_VNODE;
3025 kif->kf_type = KF_TYPE_SOCKET;
3030 kif->kf_type = KF_TYPE_PIPE;
3034 kif->kf_type = KF_TYPE_FIFO;
3039 kif->kf_type = KF_TYPE_KQUEUE;
3043 kif->kf_type = KF_TYPE_CRYPTO;
3047 kif->kf_type = KF_TYPE_MQUEUE;
3051 kif->kf_type = KF_TYPE_SHM;
3055 kif->kf_type = KF_TYPE_SEM;
3059 kif->kf_type = KF_TYPE_PTS;
3064 case DTYPE_PROCDESC:
3065 kif->kf_type = KF_TYPE_PROCDESC;
3070 kif->kf_type = KF_TYPE_UNKNOWN;
3073 kif->kf_ref_count = fp->f_count;
3074 if (fp->f_flag & FREAD)
3075 kif->kf_flags |= KF_FLAG_READ;
3076 if (fp->f_flag & FWRITE)
3077 kif->kf_flags |= KF_FLAG_WRITE;
3078 if (fp->f_flag & FAPPEND)
3079 kif->kf_flags |= KF_FLAG_APPEND;
3080 if (fp->f_flag & FASYNC)
3081 kif->kf_flags |= KF_FLAG_ASYNC;
3082 if (fp->f_flag & FFSYNC)
3083 kif->kf_flags |= KF_FLAG_FSYNC;
3084 if (fp->f_flag & FNONBLOCK)
3085 kif->kf_flags |= KF_FLAG_NONBLOCK;
3086 if (fp->f_flag & O_DIRECT)
3087 kif->kf_flags |= KF_FLAG_DIRECT;
3088 if (fp->f_flag & FHASLOCK)
3089 kif->kf_flags |= KF_FLAG_HASLOCK;
3090 kif->kf_offset = fp->f_offset;
3093 switch (vp->v_type) {
3095 kif->kf_vnode_type = KF_VTYPE_VNON;
3098 kif->kf_vnode_type = KF_VTYPE_VREG;
3101 kif->kf_vnode_type = KF_VTYPE_VDIR;
3104 kif->kf_vnode_type = KF_VTYPE_VBLK;
3107 kif->kf_vnode_type = KF_VTYPE_VCHR;
3110 kif->kf_vnode_type = KF_VTYPE_VLNK;
3113 kif->kf_vnode_type = KF_VTYPE_VSOCK;
3116 kif->kf_vnode_type = KF_VTYPE_VFIFO;
3119 kif->kf_vnode_type = KF_VTYPE_VBAD;
3122 kif->kf_vnode_type = KF_VTYPE_UNKNOWN;
3126 * It is OK to drop the filedesc lock here as we will
3127 * re-validate and re-evaluate its properties when
3128 * the loop continues.
3132 FILEDESC_SUNLOCK(fdp);
3133 vn_fullpath(curthread, vp, &fullpath, &freepath);
3134 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3136 VFS_UNLOCK_GIANT(vfslocked);
3137 strlcpy(kif->kf_path, fullpath,
3138 sizeof(kif->kf_path));
3139 if (freepath != NULL)
3140 free(freepath, M_TEMP);
3141 FILEDESC_SLOCK(fdp);
3144 struct sockaddr *sa;
3146 if (so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa)
3147 == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) {
3148 bcopy(sa, &kif->kf_sa_local, sa->sa_len);
3151 if (so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa)
3152 == 0 && sa->sa_len <= sizeof(kif->kf_sa_peer)) {
3153 bcopy(sa, &kif->kf_sa_peer, sa->sa_len);
3156 kif->kf_sock_domain =
3157 so->so_proto->pr_domain->dom_family;
3158 kif->kf_sock_type = so->so_type;
3159 kif->kf_sock_protocol = so->so_proto->pr_protocol;
3162 strlcpy(kif->kf_path, tty_devname(tp),
3163 sizeof(kif->kf_path));
3165 error = SYSCTL_OUT(req, kif, sizeof(*kif));
3169 FILEDESC_SUNLOCK(fdp);
3175 static SYSCTL_NODE(_kern_proc, KERN_PROC_OFILEDESC, ofiledesc, CTLFLAG_RD,
3176 sysctl_kern_proc_ofiledesc, "Process ofiledesc entries");
3177 #endif /* COMPAT_FREEBSD7 */
3179 #ifdef KINFO_FILE_SIZE
3180 CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE);
3184 export_fd_for_sysctl(void *data, int type, int fd, int fflags, int refcnt,
3185 int64_t offset, struct kinfo_file *kif, struct sysctl_req *req)
3190 } fflags_table[] = {
3191 { FAPPEND, KF_FLAG_APPEND },
3192 { FASYNC, KF_FLAG_ASYNC },
3193 { FFSYNC, KF_FLAG_FSYNC },
3194 { FHASLOCK, KF_FLAG_HASLOCK },
3195 { FNONBLOCK, KF_FLAG_NONBLOCK },
3196 { FREAD, KF_FLAG_READ },
3197 { FWRITE, KF_FLAG_WRITE },
3198 { O_CREAT, KF_FLAG_CREAT },
3199 { O_DIRECT, KF_FLAG_DIRECT },
3200 { O_EXCL, KF_FLAG_EXCL },
3201 { O_EXEC, KF_FLAG_EXEC },
3202 { O_EXLOCK, KF_FLAG_EXLOCK },
3203 { O_NOFOLLOW, KF_FLAG_NOFOLLOW },
3204 { O_SHLOCK, KF_FLAG_SHLOCK },
3205 { O_TRUNC, KF_FLAG_TRUNC }
3207 #define NFFLAGS (sizeof(fflags_table) / sizeof(*fflags_table))
3209 int error, vfslocked;
3212 bzero(kif, sizeof(*kif));
3216 vp = (struct vnode *)data;
3217 error = fill_vnode_info(vp, kif);
3218 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3220 VFS_UNLOCK_GIANT(vfslocked);
3222 case KF_TYPE_SOCKET:
3223 error = fill_socket_info((struct socket *)data, kif);
3226 error = fill_pipe_info((struct pipe *)data, kif);
3229 error = fill_pts_info((struct tty *)data, kif);
3231 case KF_TYPE_PROCDESC:
3232 error = fill_procdesc_info((struct procdesc *)data, kif);
3238 kif->kf_status |= KF_ATTR_VALID;
3241 * Translate file access flags.
3243 for (i = 0; i < NFFLAGS; i++)
3244 if (fflags & fflags_table[i].fflag)
3245 kif->kf_flags |= fflags_table[i].kf_fflag;
3247 kif->kf_type = type;
3248 kif->kf_ref_count = refcnt;
3249 kif->kf_offset = offset;
3250 /* Pack record size down */
3251 kif->kf_structsize = offsetof(struct kinfo_file, kf_path) +
3252 strlen(kif->kf_path) + 1;
3253 kif->kf_structsize = roundup(kif->kf_structsize, sizeof(uint64_t));
3254 error = SYSCTL_OUT(req, kif, kif->kf_structsize);
3259 * Get per-process file descriptors for use by procstat(1), et al.
3262 sysctl_kern_proc_filedesc(SYSCTL_HANDLER_ARGS)
3265 struct filedesc *fdp;
3266 struct kinfo_file *kif;
3268 struct vnode *cttyvp, *textvp, *tracevp;
3272 int error, i, *name;
3273 int type, refcnt, fflags;
3276 if ((p = pfind((pid_t)name[0])) == NULL)
3278 if ((error = p_candebug(curthread, p))) {
3283 tracevp = p->p_tracevp;
3284 if (tracevp != NULL)
3287 textvp = p->p_textvp;
3290 /* Controlling tty. */
3292 if (p->p_pgrp != NULL && p->p_pgrp->pg_session != NULL) {
3293 cttyvp = p->p_pgrp->pg_session->s_ttyvp;
3299 kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK);
3300 if (tracevp != NULL)
3301 export_fd_for_sysctl(tracevp, KF_TYPE_VNODE, KF_FD_TYPE_TRACE,
3302 FREAD | FWRITE, -1, -1, kif, req);
3304 export_fd_for_sysctl(textvp, KF_TYPE_VNODE, KF_FD_TYPE_TEXT,
3305 FREAD, -1, -1, kif, req);
3307 export_fd_for_sysctl(cttyvp, KF_TYPE_VNODE, KF_FD_TYPE_CTTY,
3308 FREAD | FWRITE, -1, -1, kif, req);
3311 FILEDESC_SLOCK(fdp);
3312 /* working directory */
3313 if (fdp->fd_cdir != NULL) {
3315 data = fdp->fd_cdir;
3316 FILEDESC_SUNLOCK(fdp);
3317 export_fd_for_sysctl(data, KF_TYPE_VNODE, KF_FD_TYPE_CWD,
3318 FREAD, -1, -1, kif, req);
3319 FILEDESC_SLOCK(fdp);
3321 /* root directory */
3322 if (fdp->fd_rdir != NULL) {
3324 data = fdp->fd_rdir;
3325 FILEDESC_SUNLOCK(fdp);
3326 export_fd_for_sysctl(data, KF_TYPE_VNODE, KF_FD_TYPE_ROOT,
3327 FREAD, -1, -1, kif, req);
3328 FILEDESC_SLOCK(fdp);
3330 /* jail directory */
3331 if (fdp->fd_jdir != NULL) {
3333 data = fdp->fd_jdir;
3334 FILEDESC_SUNLOCK(fdp);
3335 export_fd_for_sysctl(data, KF_TYPE_VNODE, KF_FD_TYPE_JAIL,
3336 FREAD, -1, -1, kif, req);
3337 FILEDESC_SLOCK(fdp);
3339 for (i = 0; i < fdp->fd_nfiles; i++) {
3340 if ((fp = fdp->fd_ofiles[i]) == NULL)
3346 * When reporting a capability, most fields will be from the
3347 * underlying object, but do mark as a capability and export
3348 * the capability rights mask.
3350 if (fp->f_type == DTYPE_CAPABILITY) {
3351 kif->kf_flags |= KF_FLAG_CAPABILITY;
3352 kif->kf_cap_rights = cap_rights(fp);
3353 (void)cap_funwrap(fp, 0, &fp);
3355 #else /* !CAPABILITIES */
3356 KASSERT(fp->f_type != DTYPE_CAPABILITY,
3357 ("sysctl_kern_proc_filedesc: saw capability"));
3359 switch (fp->f_type) {
3361 type = KF_TYPE_VNODE;
3367 type = KF_TYPE_SOCKET;
3372 type = KF_TYPE_PIPE;
3377 type = KF_TYPE_FIFO;
3383 type = KF_TYPE_KQUEUE;
3387 type = KF_TYPE_CRYPTO;
3391 type = KF_TYPE_MQUEUE;
3408 case DTYPE_PROCDESC:
3409 type = KF_TYPE_PROCDESC;
3415 type = KF_TYPE_UNKNOWN;
3418 refcnt = fp->f_count;
3419 fflags = fp->f_flag;
3420 offset = fp->f_offset;
3423 * Create sysctl entry.
3424 * It is OK to drop the filedesc lock here as we will
3425 * re-validate and re-evaluate its properties when
3426 * the loop continues.
3428 oldidx = req->oldidx;
3429 if (type == KF_TYPE_VNODE || type == KF_TYPE_FIFO)
3430 FILEDESC_SUNLOCK(fdp);
3431 error = export_fd_for_sysctl(data, type, i,
3432 fflags, refcnt, offset, kif, req);
3433 if (type == KF_TYPE_VNODE || type == KF_TYPE_FIFO)
3434 FILEDESC_SLOCK(fdp);
3436 if (error == ENOMEM) {
3438 * The hack to keep the ABI of sysctl
3439 * kern.proc.filedesc intact, but not
3440 * to account a partially copied
3441 * kinfo_file into the oldidx.
3443 req->oldidx = oldidx;
3449 FILEDESC_SUNLOCK(fdp);
3458 vntype_to_kinfo(int vtype)
3463 } vtypes_table[] = {
3464 { VBAD, KF_VTYPE_VBAD },
3465 { VBLK, KF_VTYPE_VBLK },
3466 { VCHR, KF_VTYPE_VCHR },
3467 { VDIR, KF_VTYPE_VDIR },
3468 { VFIFO, KF_VTYPE_VFIFO },
3469 { VLNK, KF_VTYPE_VLNK },
3470 { VNON, KF_VTYPE_VNON },
3471 { VREG, KF_VTYPE_VREG },
3472 { VSOCK, KF_VTYPE_VSOCK }
3474 #define NVTYPES (sizeof(vtypes_table) / sizeof(*vtypes_table))
3478 * Perform vtype translation.
3480 for (i = 0; i < NVTYPES; i++)
3481 if (vtypes_table[i].vtype == vtype)
3484 return (vtypes_table[i].kf_vtype);
3486 return (KF_VTYPE_UNKNOWN);
3490 fill_vnode_info(struct vnode *vp, struct kinfo_file *kif)
3493 char *fullpath, *freepath;
3494 int error, vfslocked;
3498 kif->kf_vnode_type = vntype_to_kinfo(vp->v_type);
3501 error = vn_fullpath(curthread, vp, &fullpath, &freepath);
3503 strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path));
3505 if (freepath != NULL)
3506 free(freepath, M_TEMP);
3509 * Retrieve vnode attributes.
3511 va.va_fsid = VNOVAL;
3513 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3514 vn_lock(vp, LK_SHARED | LK_RETRY);
3515 error = VOP_GETATTR(vp, &va, curthread->td_ucred);
3517 VFS_UNLOCK_GIANT(vfslocked);
3520 if (va.va_fsid != VNOVAL)
3521 kif->kf_un.kf_file.kf_file_fsid = va.va_fsid;
3523 kif->kf_un.kf_file.kf_file_fsid =
3524 vp->v_mount->mnt_stat.f_fsid.val[0];
3525 kif->kf_un.kf_file.kf_file_fileid = va.va_fileid;
3526 kif->kf_un.kf_file.kf_file_mode = MAKEIMODE(va.va_type, va.va_mode);
3527 kif->kf_un.kf_file.kf_file_size = va.va_size;
3528 kif->kf_un.kf_file.kf_file_rdev = va.va_rdev;
3533 fill_socket_info(struct socket *so, struct kinfo_file *kif)
3535 struct sockaddr *sa;
3536 struct inpcb *inpcb;
3537 struct unpcb *unpcb;
3542 kif->kf_sock_domain = so->so_proto->pr_domain->dom_family;
3543 kif->kf_sock_type = so->so_type;
3544 kif->kf_sock_protocol = so->so_proto->pr_protocol;
3545 kif->kf_un.kf_sock.kf_sock_pcb = (uintptr_t)so->so_pcb;
3546 switch(kif->kf_sock_domain) {
3549 if (kif->kf_sock_protocol == IPPROTO_TCP) {
3550 if (so->so_pcb != NULL) {
3551 inpcb = (struct inpcb *)(so->so_pcb);
3552 kif->kf_un.kf_sock.kf_sock_inpcb =
3553 (uintptr_t)inpcb->inp_ppcb;
3558 if (so->so_pcb != NULL) {
3559 unpcb = (struct unpcb *)(so->so_pcb);
3560 if (unpcb->unp_conn) {
3561 kif->kf_un.kf_sock.kf_sock_unpconn =
3562 (uintptr_t)unpcb->unp_conn;
3563 kif->kf_un.kf_sock.kf_sock_rcv_sb_state =
3564 so->so_rcv.sb_state;
3565 kif->kf_un.kf_sock.kf_sock_snd_sb_state =
3566 so->so_snd.sb_state;
3571 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
3572 if (error == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) {
3573 bcopy(sa, &kif->kf_sa_local, sa->sa_len);
3576 error = so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa);
3577 if (error == 0 && sa->sa_len <= sizeof(kif->kf_sa_peer)) {
3578 bcopy(sa, &kif->kf_sa_peer, sa->sa_len);
3581 strncpy(kif->kf_path, so->so_proto->pr_domain->dom_name,
3582 sizeof(kif->kf_path));
3587 fill_pts_info(struct tty *tp, struct kinfo_file *kif)
3592 kif->kf_un.kf_pts.kf_pts_dev = tty_udev(tp);
3593 strlcpy(kif->kf_path, tty_devname(tp), sizeof(kif->kf_path));
3598 fill_pipe_info(struct pipe *pi, struct kinfo_file *kif)
3603 kif->kf_un.kf_pipe.kf_pipe_addr = (uintptr_t)pi;
3604 kif->kf_un.kf_pipe.kf_pipe_peer = (uintptr_t)pi->pipe_peer;
3605 kif->kf_un.kf_pipe.kf_pipe_buffer_cnt = pi->pipe_buffer.cnt;
3610 fill_procdesc_info(struct procdesc *pdp, struct kinfo_file *kif)
3615 kif->kf_un.kf_proc.kf_pid = pdp->pd_pid;
3619 static SYSCTL_NODE(_kern_proc, KERN_PROC_FILEDESC, filedesc, CTLFLAG_RD,
3620 sysctl_kern_proc_filedesc, "Process filedesc entries");
3624 * For the purposes of debugging, generate a human-readable string for the
3628 file_type_to_name(short type)
3658 * For the purposes of debugging, identify a process (if any, perhaps one of
3659 * many) that references the passed file in its file descriptor array. Return
3662 static struct proc *
3663 file_to_first_proc(struct file *fp)
3665 struct filedesc *fdp;
3669 FOREACH_PROC_IN_SYSTEM(p) {
3670 if (p->p_state == PRS_NEW)
3675 for (n = 0; n < fdp->fd_nfiles; n++) {
3676 if (fp == fdp->fd_ofiles[n])
3684 db_print_file(struct file *fp, int header)
3689 db_printf("%8s %4s %8s %8s %4s %5s %6s %8s %5s %12s\n",
3690 "File", "Type", "Data", "Flag", "GCFl", "Count",
3691 "MCount", "Vnode", "FPID", "FCmd");
3692 p = file_to_first_proc(fp);
3693 db_printf("%8p %4s %8p %08x %04x %5d %6d %8p %5d %12s\n", fp,
3694 file_type_to_name(fp->f_type), fp->f_data, fp->f_flag,
3695 0, fp->f_count, 0, fp->f_vnode,
3696 p != NULL ? p->p_pid : -1, p != NULL ? p->p_comm : "-");
3699 DB_SHOW_COMMAND(file, db_show_file)
3704 db_printf("usage: show file <addr>\n");
3707 fp = (struct file *)addr;
3708 db_print_file(fp, 1);
3711 DB_SHOW_COMMAND(files, db_show_files)
3713 struct filedesc *fdp;
3720 FOREACH_PROC_IN_SYSTEM(p) {
3721 if (p->p_state == PRS_NEW)
3723 if ((fdp = p->p_fd) == NULL)
3725 for (n = 0; n < fdp->fd_nfiles; ++n) {
3726 if ((fp = fdp->fd_ofiles[n]) == NULL)
3728 db_print_file(fp, header);
3735 SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
3736 &maxfilesperproc, 0, "Maximum files allowed open per process");
3738 SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
3739 &maxfiles, 0, "Maximum number of files");
3741 SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD,
3742 __DEVOLATILE(int *, &openfiles), 0, "System-wide number of open files");
3746 filelistinit(void *dummy)
3749 file_zone = uma_zcreate("Files", sizeof(struct file), NULL, NULL,
3750 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
3751 mtx_init(&sigio_lock, "sigio lock", NULL, MTX_DEF);
3752 mtx_init(&fdesc_mtx, "fdesc", NULL, MTX_DEF);
3754 SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, filelistinit, NULL);
3756 /*-------------------------------------------------------------------*/
3759 badfo_readwrite(struct file *fp, struct uio *uio, struct ucred *active_cred, int flags, struct thread *td)
3766 badfo_truncate(struct file *fp, off_t length, struct ucred *active_cred, struct thread *td)
3773 badfo_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred, struct thread *td)
3780 badfo_poll(struct file *fp, int events, struct ucred *active_cred, struct thread *td)
3787 badfo_kqfilter(struct file *fp, struct knote *kn)
3794 badfo_stat(struct file *fp, struct stat *sb, struct ucred *active_cred, struct thread *td)
3801 badfo_close(struct file *fp, struct thread *td)
3808 badfo_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
3816 badfo_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
3823 struct fileops badfileops = {
3824 .fo_read = badfo_readwrite,
3825 .fo_write = badfo_readwrite,
3826 .fo_truncate = badfo_truncate,
3827 .fo_ioctl = badfo_ioctl,
3828 .fo_poll = badfo_poll,
3829 .fo_kqfilter = badfo_kqfilter,
3830 .fo_stat = badfo_stat,
3831 .fo_close = badfo_close,
3832 .fo_chmod = badfo_chmod,
3833 .fo_chown = badfo_chown,
3837 invfo_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
3845 invfo_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
3852 /*-------------------------------------------------------------------*/
3855 * File Descriptor pseudo-device driver (/dev/fd/).
3857 * Opening minor device N dup()s the file (if any) connected to file
3858 * descriptor N belonging to the calling process. Note that this driver
3859 * consists of only the ``open()'' routine, because all subsequent
3860 * references to this file will be direct to the other driver.
3862 * XXX: we could give this one a cloning event handler if necessary.
3867 fdopen(struct cdev *dev, int mode, int type, struct thread *td)
3871 * XXX Kludge: set curthread->td_dupfd to contain the value of the
3872 * the file descriptor being sought for duplication. The error
3873 * return ensures that the vnode for this device will be released
3874 * by vn_open. Open will detect this special error and take the
3875 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
3876 * will simply report the error.
3878 td->td_dupfd = dev2unit(dev);
3882 static struct cdevsw fildesc_cdevsw = {
3883 .d_version = D_VERSION,
3889 fildesc_drvinit(void *unused)
3893 dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 0, NULL,
3894 UID_ROOT, GID_WHEEL, 0666, "fd/0");
3895 make_dev_alias(dev, "stdin");
3896 dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 1, NULL,
3897 UID_ROOT, GID_WHEEL, 0666, "fd/1");
3898 make_dev_alias(dev, "stdout");
3899 dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 2, NULL,
3900 UID_ROOT, GID_WHEEL, 0666, "fd/2");
3901 make_dev_alias(dev, "stderr");
3904 SYSINIT(fildescdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, fildesc_drvinit, NULL);