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>
62 #include <sys/mount.h>
63 #include <sys/mqueue.h>
64 #include <sys/mutex.h>
65 #include <sys/namei.h>
66 #include <sys/selinfo.h>
70 #include <sys/procdesc.h>
71 #include <sys/protosw.h>
72 #include <sys/racct.h>
73 #include <sys/resourcevar.h>
74 #include <sys/signalvar.h>
75 #include <sys/socketvar.h>
78 #include <sys/syscallsubr.h>
79 #include <sys/sysctl.h>
80 #include <sys/sysproto.h>
82 #include <sys/unistd.h>
84 #include <sys/unpcb.h>
86 #include <sys/vnode.h>
88 #include <sys/ktrace.h>
93 #include <netinet/in.h>
94 #include <netinet/in_pcb.h>
96 #include <security/audit/audit.h>
103 static MALLOC_DEFINE(M_FILEDESC, "filedesc", "Open file descriptor table");
104 static MALLOC_DEFINE(M_FILEDESC_TO_LEADER, "filedesc_to_leader",
105 "file desc to leader structures");
106 static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures");
108 MALLOC_DECLARE(M_FADVISE);
110 static uma_zone_t file_zone;
113 /* Flags for do_dup() */
114 #define DUP_FIXED 0x1 /* Force fixed allocation */
115 #define DUP_FCNTL 0x2 /* fcntl()-style errors */
117 static int closefp(struct filedesc *fdp, int fd, struct file *fp,
119 static int do_dup(struct thread *td, int flags, int old, int new,
121 static int fd_first_free(struct filedesc *, int, int);
122 static int fd_last_used(struct filedesc *, int, int);
123 static void fdgrowtable(struct filedesc *, int);
124 static void fdunused(struct filedesc *fdp, int fd);
125 static void fdused(struct filedesc *fdp, int fd);
126 static int fill_vnode_info(struct vnode *vp, struct kinfo_file *kif);
127 static int fill_socket_info(struct socket *so, struct kinfo_file *kif);
128 static int fill_pts_info(struct tty *tp, struct kinfo_file *kif);
129 static int fill_pipe_info(struct pipe *pi, struct kinfo_file *kif);
130 static int fill_procdesc_info(struct procdesc *pdp,
131 struct kinfo_file *kif);
132 static int fill_shm_info(struct file *fp, struct kinfo_file *kif);
135 * A process is initially started out with NDFILE descriptors stored within
136 * this structure, selected to be enough for typical applications based on
137 * the historical limit of 20 open files (and the usage of descriptors by
138 * shells). If these descriptors are exhausted, a larger descriptor table
139 * may be allocated, up to a process' resource limit; the internal arrays
143 #define NDSLOTSIZE sizeof(NDSLOTTYPE)
144 #define NDENTRIES (NDSLOTSIZE * __CHAR_BIT)
145 #define NDSLOT(x) ((x) / NDENTRIES)
146 #define NDBIT(x) ((NDSLOTTYPE)1 << ((x) % NDENTRIES))
147 #define NDSLOTS(x) (((x) + NDENTRIES - 1) / NDENTRIES)
150 * Storage required per open file descriptor.
152 #define OFILESIZE (sizeof(struct file *) + sizeof(char))
155 * Storage to hold unused ofiles that need to be reclaimed.
158 struct file **ft_table;
159 SLIST_ENTRY(freetable) ft_next;
163 * Basic allocation of descriptors:
164 * one of the above, plus arrays for NDFILE descriptors.
167 struct filedesc fd_fd;
169 * ofiles which need to be reclaimed on free.
171 SLIST_HEAD(,freetable) fd_free;
173 * These arrays are used when the number of open files is
174 * <= NDFILE, and are then pointed to by the pointers above.
176 struct file *fd_dfiles[NDFILE];
177 char fd_dfileflags[NDFILE];
178 NDSLOTTYPE fd_dmap[NDSLOTS(NDFILE)];
182 * Descriptor management.
184 volatile int openfiles; /* actual number of open files */
185 struct mtx sigio_lock; /* mtx to protect pointers to sigio */
186 void (*mq_fdclose)(struct thread *td, int fd, struct file *fp);
188 /* A mutex to protect the association between a proc and filedesc. */
189 static struct mtx fdesc_mtx;
192 * If low >= size, just return low. Otherwise find the first zero bit in the
193 * given bitmap, starting at low and not exceeding size - 1. Return size if
197 fd_first_free(struct filedesc *fdp, int low, int size)
199 NDSLOTTYPE *map = fdp->fd_map;
207 if (low % NDENTRIES) {
208 mask = ~(~(NDSLOTTYPE)0 >> (NDENTRIES - (low % NDENTRIES)));
209 if ((mask &= ~map[off]) != 0UL)
210 return (off * NDENTRIES + ffsl(mask) - 1);
213 for (maxoff = NDSLOTS(size); off < maxoff; ++off)
214 if (map[off] != ~0UL)
215 return (off * NDENTRIES + ffsl(~map[off]) - 1);
220 * Find the highest non-zero bit in the given bitmap, starting at low and
221 * not exceeding size - 1.
224 fd_last_used(struct filedesc *fdp, int low, int size)
226 NDSLOTTYPE *map = fdp->fd_map;
234 if (size % NDENTRIES) {
235 mask = ~(~(NDSLOTTYPE)0 << (size % NDENTRIES));
236 if ((mask &= map[off]) != 0)
237 return (off * NDENTRIES + flsl(mask) - 1);
240 for (minoff = NDSLOT(low); off >= minoff; --off)
242 return (off * NDENTRIES + flsl(map[off]) - 1);
247 fdisused(struct filedesc *fdp, int fd)
249 KASSERT((unsigned int)fd < fdp->fd_nfiles,
250 ("file descriptor %d out of range (0, %d)", fd, fdp->fd_nfiles));
251 return ((fdp->fd_map[NDSLOT(fd)] & NDBIT(fd)) != 0);
255 * Mark a file descriptor as used.
258 fdused(struct filedesc *fdp, int fd)
261 FILEDESC_XLOCK_ASSERT(fdp);
262 KASSERT(!fdisused(fdp, fd),
263 ("fd already used"));
265 fdp->fd_map[NDSLOT(fd)] |= NDBIT(fd);
266 if (fd > fdp->fd_lastfile)
267 fdp->fd_lastfile = fd;
268 if (fd == fdp->fd_freefile)
269 fdp->fd_freefile = fd_first_free(fdp, fd, fdp->fd_nfiles);
273 * Mark a file descriptor as unused.
276 fdunused(struct filedesc *fdp, int fd)
279 FILEDESC_XLOCK_ASSERT(fdp);
280 KASSERT(fdisused(fdp, fd),
281 ("fd is already unused"));
282 KASSERT(fdp->fd_ofiles[fd] == NULL,
283 ("fd is still in use"));
285 fdp->fd_map[NDSLOT(fd)] &= ~NDBIT(fd);
286 if (fd < fdp->fd_freefile)
287 fdp->fd_freefile = fd;
288 if (fd == fdp->fd_lastfile)
289 fdp->fd_lastfile = fd_last_used(fdp, 0, fd);
293 * System calls on descriptors.
295 #ifndef _SYS_SYSPROTO_H_
296 struct getdtablesize_args {
302 sys_getdtablesize(struct thread *td, struct getdtablesize_args *uap)
304 struct proc *p = td->td_proc;
309 min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
310 lim = racct_get_limit(td->td_proc, RACCT_NOFILE);
312 if (lim < td->td_retval[0])
313 td->td_retval[0] = lim;
318 * Duplicate a file descriptor to a particular value.
320 * Note: keep in mind that a potential race condition exists when closing
321 * descriptors from a shared descriptor table (via rfork).
323 #ifndef _SYS_SYSPROTO_H_
331 sys_dup2(struct thread *td, struct dup2_args *uap)
334 return (do_dup(td, DUP_FIXED, (int)uap->from, (int)uap->to,
339 * Duplicate a file descriptor.
341 #ifndef _SYS_SYSPROTO_H_
348 sys_dup(struct thread *td, struct dup_args *uap)
351 return (do_dup(td, 0, (int)uap->fd, 0, td->td_retval));
355 * The file control system call.
357 #ifndef _SYS_SYSPROTO_H_
366 sys_fcntl(struct thread *td, struct fcntl_args *uap)
381 * Convert old flock structure to new.
383 error = copyin((void *)(intptr_t)uap->arg, &ofl, sizeof(ofl));
384 fl.l_start = ofl.l_start;
385 fl.l_len = ofl.l_len;
386 fl.l_pid = ofl.l_pid;
387 fl.l_type = ofl.l_type;
388 fl.l_whence = ofl.l_whence;
408 error = copyin((void *)(intptr_t)uap->arg, &fl, sizeof(fl));
417 error = kern_fcntl(td, uap->fd, cmd, arg);
420 if (uap->cmd == F_OGETLK) {
421 ofl.l_start = fl.l_start;
422 ofl.l_len = fl.l_len;
423 ofl.l_pid = fl.l_pid;
424 ofl.l_type = fl.l_type;
425 ofl.l_whence = fl.l_whence;
426 error = copyout(&ofl, (void *)(intptr_t)uap->arg, sizeof(ofl));
427 } else if (uap->cmd == F_GETLK) {
428 error = copyout(&fl, (void *)(intptr_t)uap->arg, sizeof(fl));
433 static inline struct file *
434 fdtofp(int fd, struct filedesc *fdp)
437 FILEDESC_LOCK_ASSERT(fdp);
439 if ((unsigned)fd >= fdp->fd_nfiles)
442 return (fdp->fd_ofiles[fd]);
446 fdunwrap(int fd, cap_rights_t rights, struct filedesc *fdp, struct file **fpp)
449 *fpp = fdtofp(fd, fdp);
454 if ((*fpp)->f_type == DTYPE_CAPABILITY) {
455 int err = cap_funwrap(*fpp, rights, fpp);
461 #endif /* CAPABILITIES */
466 kern_fcntl(struct thread *td, int fd, int cmd, intptr_t arg)
468 struct filedesc *fdp;
488 error = do_dup(td, DUP_FCNTL, fd, tmp, td->td_retval);
493 error = do_dup(td, DUP_FIXED, fd, tmp, td->td_retval);
498 if ((fp = fdtofp(fd, fdp)) == NULL) {
499 FILEDESC_SUNLOCK(fdp);
503 pop = &fdp->fd_ofileflags[fd];
504 td->td_retval[0] = (*pop & UF_EXCLOSE) ? FD_CLOEXEC : 0;
505 FILEDESC_SUNLOCK(fdp);
510 if ((fp = fdtofp(fd, fdp)) == NULL) {
511 FILEDESC_XUNLOCK(fdp);
515 pop = &fdp->fd_ofileflags[fd];
516 *pop = (*pop &~ UF_EXCLOSE) |
517 (arg & FD_CLOEXEC ? UF_EXCLOSE : 0);
518 FILEDESC_XUNLOCK(fdp);
523 error = fdunwrap(fd, CAP_FCNTL, fdp, &fp);
525 FILEDESC_SUNLOCK(fdp);
528 td->td_retval[0] = OFLAGS(fp->f_flag);
529 FILEDESC_SUNLOCK(fdp);
534 error = fdunwrap(fd, CAP_FCNTL, fdp, &fp);
536 FILEDESC_SUNLOCK(fdp);
540 FILEDESC_SUNLOCK(fdp);
542 tmp = flg = fp->f_flag;
544 tmp |= FFLAGS(arg & ~O_ACCMODE) & FCNTLFLAGS;
545 } while(atomic_cmpset_int(&fp->f_flag, flg, tmp) == 0);
546 tmp = fp->f_flag & FNONBLOCK;
547 error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
552 tmp = fp->f_flag & FASYNC;
553 error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td);
558 atomic_clear_int(&fp->f_flag, FNONBLOCK);
560 (void)fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
566 error = fdunwrap(fd, CAP_FCNTL, fdp, &fp);
568 FILEDESC_SUNLOCK(fdp);
572 FILEDESC_SUNLOCK(fdp);
573 error = fo_ioctl(fp, FIOGETOWN, &tmp, td->td_ucred, td);
575 td->td_retval[0] = tmp;
581 error = fdunwrap(fd, CAP_FCNTL, fdp, &fp);
583 FILEDESC_SUNLOCK(fdp);
587 FILEDESC_SUNLOCK(fdp);
589 error = fo_ioctl(fp, FIOSETOWN, &tmp, td->td_ucred, td);
594 error = priv_check(td, PRIV_NFS_LOCKD);
602 /* FALLTHROUGH F_SETLK */
607 error = fdunwrap(fd, CAP_FLOCK, fdp, &fp);
609 FILEDESC_SUNLOCK(fdp);
612 if (fp->f_type != DTYPE_VNODE) {
613 FILEDESC_SUNLOCK(fdp);
617 flp = (struct flock *)arg;
618 if (flp->l_whence == SEEK_CUR) {
619 if (fp->f_offset < 0 ||
621 fp->f_offset > OFF_MAX - flp->l_start)) {
622 FILEDESC_SUNLOCK(fdp);
626 flp->l_start += fp->f_offset;
630 * VOP_ADVLOCK() may block.
633 FILEDESC_SUNLOCK(fdp);
635 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
636 switch (flp->l_type) {
638 if ((fp->f_flag & FREAD) == 0) {
642 PROC_LOCK(p->p_leader);
643 p->p_leader->p_flag |= P_ADVLOCK;
644 PROC_UNLOCK(p->p_leader);
645 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
649 if ((fp->f_flag & FWRITE) == 0) {
653 PROC_LOCK(p->p_leader);
654 p->p_leader->p_flag |= P_ADVLOCK;
655 PROC_UNLOCK(p->p_leader);
656 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
660 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
665 * Temporary api for testing remote lock
668 if (flg != F_REMOTE) {
672 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
673 F_UNLCKSYS, flp, flg);
679 VFS_UNLOCK_GIANT(vfslocked);
681 /* Check for race with close */
683 if ((unsigned) fd >= fdp->fd_nfiles ||
684 fp != fdp->fd_ofiles[fd]) {
685 FILEDESC_SUNLOCK(fdp);
686 flp->l_whence = SEEK_SET;
689 flp->l_type = F_UNLCK;
690 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
691 (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
692 F_UNLCK, flp, F_POSIX);
693 VFS_UNLOCK_GIANT(vfslocked);
696 FILEDESC_SUNLOCK(fdp);
702 error = fdunwrap(fd, CAP_FLOCK, fdp, &fp);
704 FILEDESC_SUNLOCK(fdp);
707 if (fp->f_type != DTYPE_VNODE) {
708 FILEDESC_SUNLOCK(fdp);
712 flp = (struct flock *)arg;
713 if (flp->l_type != F_RDLCK && flp->l_type != F_WRLCK &&
714 flp->l_type != F_UNLCK) {
715 FILEDESC_SUNLOCK(fdp);
719 if (flp->l_whence == SEEK_CUR) {
720 if ((flp->l_start > 0 &&
721 fp->f_offset > OFF_MAX - flp->l_start) ||
723 fp->f_offset < OFF_MIN - flp->l_start)) {
724 FILEDESC_SUNLOCK(fdp);
728 flp->l_start += fp->f_offset;
731 * VOP_ADVLOCK() may block.
734 FILEDESC_SUNLOCK(fdp);
736 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
737 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK, flp,
739 VFS_UNLOCK_GIANT(vfslocked);
745 arg = arg ? 128 * 1024: 0;
749 if ((fp = fdtofp(fd, fdp)) == NULL) {
750 FILEDESC_SUNLOCK(fdp);
754 if (fp->f_type != DTYPE_VNODE) {
755 FILEDESC_SUNLOCK(fdp);
760 FILEDESC_SUNLOCK(fdp);
763 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
764 error = vn_lock(vp, LK_SHARED);
766 goto readahead_vnlock_fail;
767 bsize = fp->f_vnode->v_mount->mnt_stat.f_iosize;
769 fp->f_seqcount = (arg + bsize - 1) / bsize;
771 new = old = fp->f_flag;
773 } while (!atomic_cmpset_rel_int(&fp->f_flag, old, new));
774 readahead_vnlock_fail:
775 VFS_UNLOCK_GIANT(vfslocked);
779 new = old = fp->f_flag;
781 } while (!atomic_cmpset_rel_int(&fp->f_flag, old, new));
790 VFS_UNLOCK_GIANT(vfslocked);
795 * Common code for dup, dup2, fcntl(F_DUPFD) and fcntl(F_DUP2FD).
798 do_dup(struct thread *td, int flags, int old, int new,
801 struct filedesc *fdp;
811 * Verify we have a valid descriptor to dup from and possibly to
812 * dup to. Unlike dup() and dup2(), fcntl()'s F_DUPFD should
813 * return EINVAL when the new descriptor is out of bounds.
818 return (flags & DUP_FCNTL ? EINVAL : EBADF);
820 maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
823 return (flags & DUP_FCNTL ? EINVAL : EBADF);
826 if (old >= fdp->fd_nfiles || fdp->fd_ofiles[old] == NULL) {
827 FILEDESC_XUNLOCK(fdp);
830 if (flags & DUP_FIXED && old == new) {
832 FILEDESC_XUNLOCK(fdp);
835 fp = fdp->fd_ofiles[old];
839 * If the caller specified a file descriptor, make sure the file
840 * table is large enough to hold it, and grab it. Otherwise, just
841 * allocate a new descriptor the usual way. Since the filedesc
842 * lock may be temporarily dropped in the process, we have to look
845 if (flags & DUP_FIXED) {
846 if (new >= fdp->fd_nfiles) {
848 * The resource limits are here instead of e.g.
849 * fdalloc(), because the file descriptor table may be
850 * shared between processes, so we can't really use
851 * racct_add()/racct_sub(). Instead of counting the
852 * number of actually allocated descriptors, just put
853 * the limit on the size of the file descriptor table.
857 error = racct_set(p, RACCT_NOFILE, new + 1);
860 FILEDESC_XUNLOCK(fdp);
865 fdgrowtable(fdp, new + 1);
867 if (fdp->fd_ofiles[new] == NULL)
870 if ((error = fdalloc(td, new, &new)) != 0) {
871 FILEDESC_XUNLOCK(fdp);
877 KASSERT(fp == fdp->fd_ofiles[old], ("old fd has been modified"));
878 KASSERT(old != new, ("new fd is same as old"));
880 delfp = fdp->fd_ofiles[new];
882 * Duplicate the source descriptor.
884 fdp->fd_ofiles[new] = fp;
885 fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] &~ UF_EXCLOSE;
886 if (new > fdp->fd_lastfile)
887 fdp->fd_lastfile = new;
891 (void) closefp(fdp, new, delfp, td);
892 /* closefp() drops the FILEDESC lock for us. */
894 FILEDESC_XUNLOCK(fdp);
901 * If sigio is on the list associated with a process or process group,
902 * disable signalling from the device, remove sigio from the list and
906 funsetown(struct sigio **sigiop)
916 *(sigio->sio_myref) = NULL;
917 if ((sigio)->sio_pgid < 0) {
918 struct pgrp *pg = (sigio)->sio_pgrp;
920 SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio,
924 struct proc *p = (sigio)->sio_proc;
926 SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio,
931 crfree(sigio->sio_ucred);
932 free(sigio, M_SIGIO);
936 * Free a list of sigio structures.
937 * We only need to lock the SIGIO_LOCK because we have made ourselves
938 * inaccessible to callers of fsetown and therefore do not need to lock
939 * the proc or pgrp struct for the list manipulation.
942 funsetownlst(struct sigiolst *sigiolst)
948 sigio = SLIST_FIRST(sigiolst);
955 * Every entry of the list should belong
956 * to a single proc or pgrp.
958 if (sigio->sio_pgid < 0) {
959 pg = sigio->sio_pgrp;
960 PGRP_LOCK_ASSERT(pg, MA_NOTOWNED);
961 } else /* if (sigio->sio_pgid > 0) */ {
963 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
967 while ((sigio = SLIST_FIRST(sigiolst)) != NULL) {
968 *(sigio->sio_myref) = NULL;
970 KASSERT(sigio->sio_pgid < 0,
971 ("Proc sigio in pgrp sigio list"));
972 KASSERT(sigio->sio_pgrp == pg,
973 ("Bogus pgrp in sigio list"));
975 SLIST_REMOVE(&pg->pg_sigiolst, sigio, sigio,
978 } else /* if (p != NULL) */ {
979 KASSERT(sigio->sio_pgid > 0,
980 ("Pgrp sigio in proc sigio list"));
981 KASSERT(sigio->sio_proc == p,
982 ("Bogus proc in sigio list"));
984 SLIST_REMOVE(&p->p_sigiolst, sigio, sigio,
989 crfree(sigio->sio_ucred);
990 free(sigio, M_SIGIO);
997 * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg).
999 * After permission checking, add a sigio structure to the sigio list for
1000 * the process or process group.
1003 fsetown(pid_t pgid, struct sigio **sigiop)
1007 struct sigio *sigio;
1017 /* Allocate and fill in the new sigio out of locks. */
1018 sigio = malloc(sizeof(struct sigio), M_SIGIO, M_WAITOK);
1019 sigio->sio_pgid = pgid;
1020 sigio->sio_ucred = crhold(curthread->td_ucred);
1021 sigio->sio_myref = sigiop;
1023 sx_slock(&proctree_lock);
1032 * Policy - Don't allow a process to FSETOWN a process
1033 * in another session.
1035 * Remove this test to allow maximum flexibility or
1036 * restrict FSETOWN to the current process or process
1037 * group for maximum safety.
1040 if (proc->p_session != curthread->td_proc->p_session) {
1046 } else /* if (pgid < 0) */ {
1047 pgrp = pgfind(-pgid);
1055 * Policy - Don't allow a process to FSETOWN a process
1056 * in another session.
1058 * Remove this test to allow maximum flexibility or
1059 * restrict FSETOWN to the current process or process
1060 * group for maximum safety.
1062 if (pgrp->pg_session != curthread->td_proc->p_session) {
1073 * Since funsetownlst() is called without the proctree
1074 * locked, we need to check for P_WEXIT.
1075 * XXX: is ESRCH correct?
1077 if ((proc->p_flag & P_WEXIT) != 0) {
1082 SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio);
1083 sigio->sio_proc = proc;
1087 SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
1088 sigio->sio_pgrp = pgrp;
1091 sx_sunlock(&proctree_lock);
1098 sx_sunlock(&proctree_lock);
1099 crfree(sigio->sio_ucred);
1100 free(sigio, M_SIGIO);
1105 * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
1109 struct sigio **sigiop;
1114 pgid = (*sigiop != NULL) ? (*sigiop)->sio_pgid : 0;
1120 * Function drops the filedesc lock on return.
1123 closefp(struct filedesc *fdp, int fd, struct file *fp, struct thread *td)
1125 struct file *fp_object;
1126 int error, holdleaders;
1128 FILEDESC_XLOCK_ASSERT(fdp);
1130 if (td->td_proc->p_fdtol != NULL) {
1132 * Ask fdfree() to sleep to ensure that all relevant
1133 * process leaders can be traversed in closef().
1135 fdp->fd_holdleaderscount++;
1142 * We now hold the fp reference that used to be owned by the
1143 * descriptor array. We have to unlock the FILEDESC *AFTER*
1144 * knote_fdclose to prevent a race of the fd getting opened, a knote
1145 * added, and deleteing a knote for the new fd.
1147 knote_fdclose(td, fd);
1150 * When we're closing an fd with a capability, we need to notify
1151 * mqueue if the underlying object is of type mqueue.
1153 (void)cap_funwrap(fp, 0, &fp_object);
1154 if (fp_object->f_type == DTYPE_MQUEUE)
1155 mq_fdclose(td, fd, fp_object);
1156 FILEDESC_XUNLOCK(fdp);
1158 error = closef(fp, td);
1160 FILEDESC_XLOCK(fdp);
1161 fdp->fd_holdleaderscount--;
1162 if (fdp->fd_holdleaderscount == 0 &&
1163 fdp->fd_holdleaderswakeup != 0) {
1164 fdp->fd_holdleaderswakeup = 0;
1165 wakeup(&fdp->fd_holdleaderscount);
1167 FILEDESC_XUNLOCK(fdp);
1173 * Close a file descriptor.
1175 #ifndef _SYS_SYSPROTO_H_
1184 struct close_args *uap;
1187 return (kern_close(td, uap->fd));
1195 struct filedesc *fdp;
1198 fdp = td->td_proc->p_fd;
1200 AUDIT_SYSCLOSE(td, fd);
1202 FILEDESC_XLOCK(fdp);
1203 if ((unsigned)fd >= fdp->fd_nfiles ||
1204 (fp = fdp->fd_ofiles[fd]) == NULL) {
1205 FILEDESC_XUNLOCK(fdp);
1208 fdp->fd_ofiles[fd] = NULL;
1209 fdp->fd_ofileflags[fd] = 0;
1212 /* closefp() drops the FILEDESC lock for us. */
1213 return (closefp(fdp, fd, fp, td));
1217 * Close open file descriptors.
1219 #ifndef _SYS_SYSPROTO_H_
1220 struct closefrom_args {
1226 sys_closefrom(struct thread *td, struct closefrom_args *uap)
1228 struct filedesc *fdp;
1231 fdp = td->td_proc->p_fd;
1232 AUDIT_ARG_FD(uap->lowfd);
1235 * Treat negative starting file descriptor values identical to
1236 * closefrom(0) which closes all files.
1240 FILEDESC_SLOCK(fdp);
1241 for (fd = uap->lowfd; fd < fdp->fd_nfiles; fd++) {
1242 if (fdp->fd_ofiles[fd] != NULL) {
1243 FILEDESC_SUNLOCK(fdp);
1244 (void)kern_close(td, fd);
1245 FILEDESC_SLOCK(fdp);
1248 FILEDESC_SUNLOCK(fdp);
1252 #if defined(COMPAT_43)
1254 * Return status information about a file descriptor.
1256 #ifndef _SYS_SYSPROTO_H_
1257 struct ofstat_args {
1264 ofstat(struct thread *td, struct ofstat_args *uap)
1270 error = kern_fstat(td, uap->fd, &ub);
1273 error = copyout(&oub, uap->sb, sizeof(oub));
1277 #endif /* COMPAT_43 */
1280 * Return status information about a file descriptor.
1282 #ifndef _SYS_SYSPROTO_H_
1290 sys_fstat(struct thread *td, struct fstat_args *uap)
1295 error = kern_fstat(td, uap->fd, &ub);
1297 error = copyout(&ub, uap->sb, sizeof(ub));
1302 kern_fstat(struct thread *td, int fd, struct stat *sbp)
1309 if ((error = fget(td, fd, CAP_FSTAT, &fp)) != 0)
1312 AUDIT_ARG_FILE(td->td_proc, fp);
1314 error = fo_stat(fp, sbp, td->td_ucred, td);
1317 if (error == 0 && KTRPOINT(td, KTR_STRUCT))
1324 * Return status information about a file descriptor.
1326 #ifndef _SYS_SYSPROTO_H_
1327 struct nfstat_args {
1334 sys_nfstat(struct thread *td, struct nfstat_args *uap)
1340 error = kern_fstat(td, uap->fd, &ub);
1342 cvtnstat(&ub, &nub);
1343 error = copyout(&nub, uap->sb, sizeof(nub));
1349 * Return pathconf information about a file descriptor.
1351 #ifndef _SYS_SYSPROTO_H_
1352 struct fpathconf_args {
1359 sys_fpathconf(struct thread *td, struct fpathconf_args *uap)
1365 if ((error = fget(td, uap->fd, CAP_FPATHCONF, &fp)) != 0)
1368 /* If asynchronous I/O is available, it works for all descriptors. */
1369 if (uap->name == _PC_ASYNC_IO) {
1370 td->td_retval[0] = async_io_version;
1376 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1377 vn_lock(vp, LK_SHARED | LK_RETRY);
1378 error = VOP_PATHCONF(vp, uap->name, td->td_retval);
1380 VFS_UNLOCK_GIANT(vfslocked);
1381 } else if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) {
1382 if (uap->name != _PC_PIPE_BUF) {
1385 td->td_retval[0] = PIPE_BUF;
1397 * Grow the file table to accomodate (at least) nfd descriptors.
1400 fdgrowtable(struct filedesc *fdp, int nfd)
1402 struct filedesc0 *fdp0;
1403 struct freetable *fo;
1404 struct file **ntable;
1405 struct file **otable;
1407 int nnfiles, onfiles;
1410 FILEDESC_XLOCK_ASSERT(fdp);
1412 KASSERT(fdp->fd_nfiles > 0,
1413 ("zero-length file table"));
1415 /* compute the size of the new table */
1416 onfiles = fdp->fd_nfiles;
1417 nnfiles = NDSLOTS(nfd) * NDENTRIES; /* round up */
1418 if (nnfiles <= onfiles)
1419 /* the table is already large enough */
1422 /* allocate a new table and (if required) new bitmaps */
1423 ntable = malloc((nnfiles * OFILESIZE) + sizeof(struct freetable),
1424 M_FILEDESC, M_ZERO | M_WAITOK);
1425 nfileflags = (char *)&ntable[nnfiles];
1426 if (NDSLOTS(nnfiles) > NDSLOTS(onfiles))
1427 nmap = malloc(NDSLOTS(nnfiles) * NDSLOTSIZE,
1428 M_FILEDESC, M_ZERO | M_WAITOK);
1432 bcopy(fdp->fd_ofiles, ntable, onfiles * sizeof(*ntable));
1433 bcopy(fdp->fd_ofileflags, nfileflags, onfiles);
1434 otable = fdp->fd_ofiles;
1435 fdp->fd_ofileflags = nfileflags;
1436 fdp->fd_ofiles = ntable;
1438 * We must preserve ofiles until the process exits because we can't
1439 * be certain that no threads have references to the old table via
1442 if (onfiles > NDFILE) {
1443 fo = (struct freetable *)&otable[onfiles];
1444 fdp0 = (struct filedesc0 *)fdp;
1445 fo->ft_table = otable;
1446 SLIST_INSERT_HEAD(&fdp0->fd_free, fo, ft_next);
1448 if (NDSLOTS(nnfiles) > NDSLOTS(onfiles)) {
1449 bcopy(fdp->fd_map, nmap, NDSLOTS(onfiles) * sizeof(*nmap));
1450 if (NDSLOTS(onfiles) > NDSLOTS(NDFILE))
1451 free(fdp->fd_map, M_FILEDESC);
1454 fdp->fd_nfiles = nnfiles;
1458 * Allocate a file descriptor for the process.
1461 fdalloc(struct thread *td, int minfd, int *result)
1463 struct proc *p = td->td_proc;
1464 struct filedesc *fdp = p->p_fd;
1465 int fd = -1, maxfd, allocfd;
1470 FILEDESC_XLOCK_ASSERT(fdp);
1472 if (fdp->fd_freefile > minfd)
1473 minfd = fdp->fd_freefile;
1476 maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
1480 * Search the bitmap for a free descriptor starting at minfd.
1481 * If none is found, grow the file table.
1483 fd = fd_first_free(fdp, minfd, fdp->fd_nfiles);
1486 if (fd >= fdp->fd_nfiles) {
1487 allocfd = min(fd * 2, maxfd);
1490 error = racct_set(p, RACCT_NOFILE, allocfd);
1496 * fd is already equal to first free descriptor >= minfd, so
1497 * we only need to grow the table and we are done.
1499 fdgrowtable(fdp, allocfd);
1503 * Perform some sanity checks, then mark the file descriptor as
1504 * used and return it to the caller.
1506 KASSERT((unsigned int)fd < min(maxfd, fdp->fd_nfiles),
1507 ("invalid descriptor %d", fd));
1508 KASSERT(!fdisused(fdp, fd),
1509 ("fd_first_free() returned non-free descriptor"));
1510 KASSERT(fdp->fd_ofiles[fd] == NULL, ("file descriptor isn't free"));
1511 KASSERT(fdp->fd_ofileflags[fd] == 0, ("file flags are set"));
1518 * Check to see whether n user file descriptors are available to the process
1522 fdavail(struct thread *td, int n)
1524 struct proc *p = td->td_proc;
1525 struct filedesc *fdp = td->td_proc->p_fd;
1528 FILEDESC_LOCK_ASSERT(fdp);
1531 * XXX: This is only called from uipc_usrreq.c:unp_externalize();
1532 * call racct_add() from there instead of dealing with containers
1536 lim = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
1538 if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
1540 last = min(fdp->fd_nfiles, lim);
1541 for (i = fdp->fd_freefile; i < last; i++) {
1542 if (fdp->fd_ofiles[i] == NULL && --n <= 0)
1549 * Create a new open file structure and allocate a file decriptor for the
1550 * process that refers to it. We add one reference to the file for the
1551 * descriptor table and one reference for resultfp. This is to prevent us
1552 * being preempted and the entry in the descriptor table closed after we
1553 * release the FILEDESC lock.
1556 falloc(struct thread *td, struct file **resultfp, int *resultfd, int flags)
1561 error = falloc_noinstall(td, &fp);
1563 return (error); /* no reference held on error */
1565 error = finstall(td, fp, &fd, flags);
1567 fdrop(fp, td); /* one reference (fp only) */
1571 if (resultfp != NULL)
1572 *resultfp = fp; /* copy out result */
1574 fdrop(fp, td); /* release local reference */
1576 if (resultfd != NULL)
1583 * Create a new open file structure without allocating a file descriptor.
1586 falloc_noinstall(struct thread *td, struct file **resultfp)
1589 int maxuserfiles = maxfiles - (maxfiles / 20);
1590 static struct timeval lastfail;
1593 KASSERT(resultfp != NULL, ("%s: resultfp == NULL", __func__));
1595 if ((openfiles >= maxuserfiles &&
1596 priv_check(td, PRIV_MAXFILES) != 0) ||
1597 openfiles >= maxfiles) {
1598 if (ppsratecheck(&lastfail, &curfail, 1)) {
1599 printf("kern.maxfiles limit exceeded by uid %i, "
1600 "please see tuning(7).\n", td->td_ucred->cr_ruid);
1604 atomic_add_int(&openfiles, 1);
1605 fp = uma_zalloc(file_zone, M_WAITOK | M_ZERO);
1606 refcount_init(&fp->f_count, 1);
1607 fp->f_cred = crhold(td->td_ucred);
1608 fp->f_ops = &badfileops;
1616 * Install a file in a file descriptor table.
1619 finstall(struct thread *td, struct file *fp, int *fd, int flags)
1621 struct filedesc *fdp = td->td_proc->p_fd;
1624 KASSERT(fd != NULL, ("%s: fd == NULL", __func__));
1625 KASSERT(fp != NULL, ("%s: fp == NULL", __func__));
1627 FILEDESC_XLOCK(fdp);
1628 if ((error = fdalloc(td, 0, fd))) {
1629 FILEDESC_XUNLOCK(fdp);
1633 fdp->fd_ofiles[*fd] = fp;
1634 if ((flags & O_CLOEXEC) != 0)
1635 fdp->fd_ofileflags[*fd] |= UF_EXCLOSE;
1636 FILEDESC_XUNLOCK(fdp);
1641 * Build a new filedesc structure from another.
1642 * Copy the current, root, and jail root vnode references.
1645 fdinit(struct filedesc *fdp)
1647 struct filedesc0 *newfdp;
1649 newfdp = malloc(sizeof *newfdp, M_FILEDESC, M_WAITOK | M_ZERO);
1650 FILEDESC_LOCK_INIT(&newfdp->fd_fd);
1652 FILEDESC_XLOCK(fdp);
1653 newfdp->fd_fd.fd_cdir = fdp->fd_cdir;
1654 if (newfdp->fd_fd.fd_cdir)
1655 VREF(newfdp->fd_fd.fd_cdir);
1656 newfdp->fd_fd.fd_rdir = fdp->fd_rdir;
1657 if (newfdp->fd_fd.fd_rdir)
1658 VREF(newfdp->fd_fd.fd_rdir);
1659 newfdp->fd_fd.fd_jdir = fdp->fd_jdir;
1660 if (newfdp->fd_fd.fd_jdir)
1661 VREF(newfdp->fd_fd.fd_jdir);
1662 FILEDESC_XUNLOCK(fdp);
1665 /* Create the file descriptor table. */
1666 newfdp->fd_fd.fd_refcnt = 1;
1667 newfdp->fd_fd.fd_holdcnt = 1;
1668 newfdp->fd_fd.fd_cmask = CMASK;
1669 newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
1670 newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
1671 newfdp->fd_fd.fd_nfiles = NDFILE;
1672 newfdp->fd_fd.fd_map = newfdp->fd_dmap;
1673 newfdp->fd_fd.fd_lastfile = -1;
1674 return (&newfdp->fd_fd);
1677 static struct filedesc *
1678 fdhold(struct proc *p)
1680 struct filedesc *fdp;
1682 mtx_lock(&fdesc_mtx);
1686 mtx_unlock(&fdesc_mtx);
1691 fddrop(struct filedesc *fdp)
1693 struct filedesc0 *fdp0;
1694 struct freetable *ft;
1697 mtx_lock(&fdesc_mtx);
1698 i = --fdp->fd_holdcnt;
1699 mtx_unlock(&fdesc_mtx);
1703 FILEDESC_LOCK_DESTROY(fdp);
1704 fdp0 = (struct filedesc0 *)fdp;
1705 while ((ft = SLIST_FIRST(&fdp0->fd_free)) != NULL) {
1706 SLIST_REMOVE_HEAD(&fdp0->fd_free, ft_next);
1707 free(ft->ft_table, M_FILEDESC);
1709 free(fdp, M_FILEDESC);
1713 * Share a filedesc structure.
1716 fdshare(struct filedesc *fdp)
1719 FILEDESC_XLOCK(fdp);
1721 FILEDESC_XUNLOCK(fdp);
1726 * Unshare a filedesc structure, if necessary by making a copy
1729 fdunshare(struct proc *p, struct thread *td)
1732 FILEDESC_XLOCK(p->p_fd);
1733 if (p->p_fd->fd_refcnt > 1) {
1734 struct filedesc *tmp;
1736 FILEDESC_XUNLOCK(p->p_fd);
1737 tmp = fdcopy(p->p_fd);
1741 FILEDESC_XUNLOCK(p->p_fd);
1745 * Copy a filedesc structure. A NULL pointer in returns a NULL reference,
1746 * this is to ease callers, not catch errors.
1749 fdcopy(struct filedesc *fdp)
1751 struct filedesc *newfdp;
1754 /* Certain daemons might not have file descriptors. */
1758 newfdp = fdinit(fdp);
1759 FILEDESC_SLOCK(fdp);
1760 while (fdp->fd_lastfile >= newfdp->fd_nfiles) {
1761 FILEDESC_SUNLOCK(fdp);
1762 FILEDESC_XLOCK(newfdp);
1763 fdgrowtable(newfdp, fdp->fd_lastfile + 1);
1764 FILEDESC_XUNLOCK(newfdp);
1765 FILEDESC_SLOCK(fdp);
1767 /* copy all passable descriptors (i.e. not kqueue) */
1768 newfdp->fd_freefile = -1;
1769 for (i = 0; i <= fdp->fd_lastfile; ++i) {
1770 if (fdisused(fdp, i) &&
1771 (fdp->fd_ofiles[i]->f_ops->fo_flags & DFLAG_PASSABLE) &&
1772 fdp->fd_ofiles[i]->f_ops != &badfileops) {
1773 newfdp->fd_ofiles[i] = fdp->fd_ofiles[i];
1774 newfdp->fd_ofileflags[i] = fdp->fd_ofileflags[i];
1775 fhold(newfdp->fd_ofiles[i]);
1776 newfdp->fd_lastfile = i;
1778 if (newfdp->fd_freefile == -1)
1779 newfdp->fd_freefile = i;
1782 newfdp->fd_cmask = fdp->fd_cmask;
1783 FILEDESC_SUNLOCK(fdp);
1784 FILEDESC_XLOCK(newfdp);
1785 for (i = 0; i <= newfdp->fd_lastfile; ++i)
1786 if (newfdp->fd_ofiles[i] != NULL)
1788 if (newfdp->fd_freefile == -1)
1789 newfdp->fd_freefile = i;
1790 FILEDESC_XUNLOCK(newfdp);
1795 * Release a filedesc structure.
1798 fdfree(struct thread *td)
1800 struct filedesc *fdp;
1802 struct filedesc_to_leader *fdtol;
1804 struct vnode *cdir, *jdir, *rdir, *vp;
1807 /* Certain daemons might not have file descriptors. */
1808 fdp = td->td_proc->p_fd;
1813 PROC_LOCK(td->td_proc);
1814 racct_set(td->td_proc, RACCT_NOFILE, 0);
1815 PROC_UNLOCK(td->td_proc);
1818 /* Check for special need to clear POSIX style locks */
1819 fdtol = td->td_proc->p_fdtol;
1820 if (fdtol != NULL) {
1821 FILEDESC_XLOCK(fdp);
1822 KASSERT(fdtol->fdl_refcount > 0,
1823 ("filedesc_to_refcount botch: fdl_refcount=%d",
1824 fdtol->fdl_refcount));
1825 if (fdtol->fdl_refcount == 1 &&
1826 (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1827 for (i = 0; i <= fdp->fd_lastfile; i++) {
1828 fp = fdp->fd_ofiles[i];
1829 if (fp == NULL || fp->f_type != DTYPE_VNODE)
1832 FILEDESC_XUNLOCK(fdp);
1833 lf.l_whence = SEEK_SET;
1836 lf.l_type = F_UNLCK;
1838 locked = VFS_LOCK_GIANT(vp->v_mount);
1839 (void) VOP_ADVLOCK(vp,
1840 (caddr_t)td->td_proc->
1845 VFS_UNLOCK_GIANT(locked);
1846 FILEDESC_XLOCK(fdp);
1851 if (fdtol->fdl_refcount == 1) {
1852 if (fdp->fd_holdleaderscount > 0 &&
1853 (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1855 * close() or do_dup() has cleared a reference
1856 * in a shared file descriptor table.
1858 fdp->fd_holdleaderswakeup = 1;
1859 sx_sleep(&fdp->fd_holdleaderscount,
1860 FILEDESC_LOCK(fdp), PLOCK, "fdlhold", 0);
1863 if (fdtol->fdl_holdcount > 0) {
1865 * Ensure that fdtol->fdl_leader remains
1866 * valid in closef().
1868 fdtol->fdl_wakeup = 1;
1869 sx_sleep(fdtol, FILEDESC_LOCK(fdp), PLOCK,
1874 fdtol->fdl_refcount--;
1875 if (fdtol->fdl_refcount == 0 &&
1876 fdtol->fdl_holdcount == 0) {
1877 fdtol->fdl_next->fdl_prev = fdtol->fdl_prev;
1878 fdtol->fdl_prev->fdl_next = fdtol->fdl_next;
1881 td->td_proc->p_fdtol = NULL;
1882 FILEDESC_XUNLOCK(fdp);
1884 free(fdtol, M_FILEDESC_TO_LEADER);
1886 FILEDESC_XLOCK(fdp);
1887 i = --fdp->fd_refcnt;
1888 FILEDESC_XUNLOCK(fdp);
1892 for (i = 0; i <= fdp->fd_lastfile; i++) {
1893 fp = fdp->fd_ofiles[i];
1895 FILEDESC_XLOCK(fdp);
1896 fdp->fd_ofiles[i] = NULL;
1897 FILEDESC_XUNLOCK(fdp);
1898 (void) closef(fp, td);
1901 FILEDESC_XLOCK(fdp);
1903 /* XXX This should happen earlier. */
1904 mtx_lock(&fdesc_mtx);
1905 td->td_proc->p_fd = NULL;
1906 mtx_unlock(&fdesc_mtx);
1908 if (fdp->fd_nfiles > NDFILE)
1909 free(fdp->fd_ofiles, M_FILEDESC);
1910 if (NDSLOTS(fdp->fd_nfiles) > NDSLOTS(NDFILE))
1911 free(fdp->fd_map, M_FILEDESC);
1915 cdir = fdp->fd_cdir;
1916 fdp->fd_cdir = NULL;
1917 rdir = fdp->fd_rdir;
1918 fdp->fd_rdir = NULL;
1919 jdir = fdp->fd_jdir;
1920 fdp->fd_jdir = NULL;
1921 FILEDESC_XUNLOCK(fdp);
1924 locked = VFS_LOCK_GIANT(cdir->v_mount);
1926 VFS_UNLOCK_GIANT(locked);
1929 locked = VFS_LOCK_GIANT(rdir->v_mount);
1931 VFS_UNLOCK_GIANT(locked);
1934 locked = VFS_LOCK_GIANT(jdir->v_mount);
1936 VFS_UNLOCK_GIANT(locked);
1943 * For setugid programs, we don't want to people to use that setugidness
1944 * to generate error messages which write to a file which otherwise would
1945 * otherwise be off-limits to the process. We check for filesystems where
1946 * the vnode can change out from under us after execve (like [lin]procfs).
1948 * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
1949 * sufficient. We also don't check for setugidness since we know we are.
1952 is_unsafe(struct file *fp)
1954 if (fp->f_type == DTYPE_VNODE) {
1955 struct vnode *vp = fp->f_vnode;
1957 if ((vp->v_vflag & VV_PROCDEP) != 0)
1964 * Make this setguid thing safe, if at all possible.
1967 setugidsafety(struct thread *td)
1969 struct filedesc *fdp;
1972 /* Certain daemons might not have file descriptors. */
1973 fdp = td->td_proc->p_fd;
1978 * Note: fdp->fd_ofiles may be reallocated out from under us while
1979 * we are blocked in a close. Be careful!
1981 FILEDESC_XLOCK(fdp);
1982 for (i = 0; i <= fdp->fd_lastfile; i++) {
1985 if (fdp->fd_ofiles[i] && is_unsafe(fdp->fd_ofiles[i])) {
1988 knote_fdclose(td, i);
1990 * NULL-out descriptor prior to close to avoid
1991 * a race while close blocks.
1993 fp = fdp->fd_ofiles[i];
1994 fdp->fd_ofiles[i] = NULL;
1995 fdp->fd_ofileflags[i] = 0;
1997 FILEDESC_XUNLOCK(fdp);
1998 (void) closef(fp, td);
1999 FILEDESC_XLOCK(fdp);
2002 FILEDESC_XUNLOCK(fdp);
2006 * If a specific file object occupies a specific file descriptor, close the
2007 * file descriptor entry and drop a reference on the file object. This is a
2008 * convenience function to handle a subsequent error in a function that calls
2009 * falloc() that handles the race that another thread might have closed the
2010 * file descriptor out from under the thread creating the file object.
2013 fdclose(struct filedesc *fdp, struct file *fp, int idx, struct thread *td)
2016 FILEDESC_XLOCK(fdp);
2017 if (fdp->fd_ofiles[idx] == fp) {
2018 fdp->fd_ofiles[idx] = NULL;
2020 FILEDESC_XUNLOCK(fdp);
2023 FILEDESC_XUNLOCK(fdp);
2027 * Close any files on exec?
2030 fdcloseexec(struct thread *td)
2032 struct filedesc *fdp;
2035 /* Certain daemons might not have file descriptors. */
2036 fdp = td->td_proc->p_fd;
2040 FILEDESC_XLOCK(fdp);
2043 * We cannot cache fd_ofiles or fd_ofileflags since operations
2044 * may block and rip them out from under us.
2046 for (i = 0; i <= fdp->fd_lastfile; i++) {
2047 if (fdp->fd_ofiles[i] != NULL &&
2048 (fdp->fd_ofiles[i]->f_type == DTYPE_MQUEUE ||
2049 (fdp->fd_ofileflags[i] & UF_EXCLOSE))) {
2052 knote_fdclose(td, i);
2054 * NULL-out descriptor prior to close to avoid
2055 * a race while close blocks.
2057 fp = fdp->fd_ofiles[i];
2058 fdp->fd_ofiles[i] = NULL;
2059 fdp->fd_ofileflags[i] = 0;
2061 if (fp->f_type == DTYPE_MQUEUE)
2062 mq_fdclose(td, i, fp);
2063 FILEDESC_XUNLOCK(fdp);
2064 (void) closef(fp, td);
2065 FILEDESC_XLOCK(fdp);
2068 FILEDESC_XUNLOCK(fdp);
2072 * It is unsafe for set[ug]id processes to be started with file
2073 * descriptors 0..2 closed, as these descriptors are given implicit
2074 * significance in the Standard C library. fdcheckstd() will create a
2075 * descriptor referencing /dev/null for each of stdin, stdout, and
2076 * stderr that is not already open.
2079 fdcheckstd(struct thread *td)
2081 struct filedesc *fdp;
2082 register_t retval, save;
2083 int i, error, devnull;
2085 fdp = td->td_proc->p_fd;
2088 KASSERT(fdp->fd_refcnt == 1, ("the fdtable should not be shared"));
2091 for (i = 0; i < 3; i++) {
2092 if (fdp->fd_ofiles[i] != NULL)
2095 save = td->td_retval[0];
2096 error = kern_open(td, "/dev/null", UIO_SYSSPACE,
2098 devnull = td->td_retval[0];
2099 td->td_retval[0] = save;
2102 KASSERT(devnull == i, ("oof, we didn't get our fd"));
2104 error = do_dup(td, DUP_FIXED, devnull, i, &retval);
2113 * Internal form of close. Decrement reference count on file structure.
2114 * Note: td may be NULL when closing a file that was being passed in a
2117 * XXXRW: Giant is not required for the caller, but often will be held; this
2118 * makes it moderately likely the Giant will be recursed in the VFS case.
2121 closef(struct file *fp, struct thread *td)
2125 struct filedesc_to_leader *fdtol;
2126 struct filedesc *fdp;
2127 struct file *fp_object;
2130 * POSIX record locking dictates that any close releases ALL
2131 * locks owned by this process. This is handled by setting
2132 * a flag in the unlock to free ONLY locks obeying POSIX
2133 * semantics, and not to free BSD-style file locks.
2134 * If the descriptor was in a message, POSIX-style locks
2135 * aren't passed with the descriptor, and the thread pointer
2136 * will be NULL. Callers should be careful only to pass a
2137 * NULL thread pointer when there really is no owning
2138 * context that might have locks, or the locks will be
2141 * If this is a capability, we do lock processing under the underlying
2142 * node, not the capability itself.
2144 (void)cap_funwrap(fp, 0, &fp_object);
2145 if ((fp_object->f_type == DTYPE_VNODE) && (td != NULL)) {
2148 vp = fp_object->f_vnode;
2149 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2150 if ((td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
2151 lf.l_whence = SEEK_SET;
2154 lf.l_type = F_UNLCK;
2155 (void) VOP_ADVLOCK(vp, (caddr_t)td->td_proc->p_leader,
2156 F_UNLCK, &lf, F_POSIX);
2158 fdtol = td->td_proc->p_fdtol;
2159 if (fdtol != NULL) {
2161 * Handle special case where file descriptor table is
2162 * shared between multiple process leaders.
2164 fdp = td->td_proc->p_fd;
2165 FILEDESC_XLOCK(fdp);
2166 for (fdtol = fdtol->fdl_next;
2167 fdtol != td->td_proc->p_fdtol;
2168 fdtol = fdtol->fdl_next) {
2169 if ((fdtol->fdl_leader->p_flag &
2172 fdtol->fdl_holdcount++;
2173 FILEDESC_XUNLOCK(fdp);
2174 lf.l_whence = SEEK_SET;
2177 lf.l_type = F_UNLCK;
2178 vp = fp_object->f_vnode;
2179 (void) VOP_ADVLOCK(vp,
2180 (caddr_t)fdtol->fdl_leader,
2181 F_UNLCK, &lf, F_POSIX);
2182 FILEDESC_XLOCK(fdp);
2183 fdtol->fdl_holdcount--;
2184 if (fdtol->fdl_holdcount == 0 &&
2185 fdtol->fdl_wakeup != 0) {
2186 fdtol->fdl_wakeup = 0;
2190 FILEDESC_XUNLOCK(fdp);
2192 VFS_UNLOCK_GIANT(vfslocked);
2194 return (fdrop(fp, td));
2198 * Initialize the file pointer with the specified properties.
2200 * The ops are set with release semantics to be certain that the flags, type,
2201 * and data are visible when ops is. This is to prevent ops methods from being
2202 * called with bad data.
2205 finit(struct file *fp, u_int flag, short type, void *data, struct fileops *ops)
2210 atomic_store_rel_ptr((volatile uintptr_t *)&fp->f_ops, (uintptr_t)ops);
2214 fget_unlocked(struct filedesc *fdp, int fd)
2219 if ((unsigned int)fd >= fdp->fd_nfiles)
2222 * Fetch the descriptor locklessly. We avoid fdrop() races by
2223 * never raising a refcount above 0. To accomplish this we have
2224 * to use a cmpset loop rather than an atomic_add. The descriptor
2225 * must be re-verified once we acquire a reference to be certain
2226 * that the identity is still correct and we did not lose a race
2227 * due to preemption.
2230 fp = fdp->fd_ofiles[fd];
2233 count = fp->f_count;
2237 * Use an acquire barrier to prevent caching of fd_ofiles
2238 * so it is refreshed for verification.
2240 if (atomic_cmpset_acq_int(&fp->f_count, count, count + 1) != 1)
2242 if (fp == fdp->fd_ofiles[fd])
2244 fdrop(fp, curthread);
2251 * Extract the file pointer associated with the specified descriptor for the
2252 * current user process.
2254 * If the descriptor doesn't exist or doesn't match 'flags', EBADF is
2257 * If the FGET_GETCAP flag is set, the capability itself will be returned.
2258 * Calling _fget() with FGET_GETCAP on a non-capability will return EINVAL.
2259 * Otherwise, if the file is a capability, its rights will be checked against
2260 * the capability rights mask, and if successful, the object will be unwrapped.
2262 * If an error occured the non-zero error is returned and *fpp is set to
2263 * NULL. Otherwise *fpp is held and set and zero is returned. Caller is
2264 * responsible for fdrop().
2266 #define FGET_GETCAP 0x00000001
2268 _fget(struct thread *td, int fd, struct file **fpp, int flags,
2269 cap_rights_t needrights, cap_rights_t *haverightsp, u_char *maxprotp,
2272 struct filedesc *fdp;
2275 struct file *fp_fromcap;
2280 if (td == NULL || (fdp = td->td_proc->p_fd) == NULL)
2282 if ((fp = fget_unlocked(fdp, fd)) == NULL)
2284 if (fp->f_ops == &badfileops) {
2291 * If this is a capability, what rights does it have?
2293 if (haverightsp != NULL) {
2294 if (fp->f_type == DTYPE_CAPABILITY)
2295 *haverightsp = cap_rights(fp);
2297 *haverightsp = CAP_MASK_VALID;
2301 * If a capability has been requested, return the capability directly.
2302 * Otherwise, check capability rights, extract the underlying object,
2303 * and check its access flags.
2305 if (fget_flags & FGET_GETCAP) {
2306 if (fp->f_type != DTYPE_CAPABILITY) {
2311 if (maxprotp == NULL)
2312 error = cap_funwrap(fp, needrights, &fp_fromcap);
2314 error = cap_funwrap_mmap(fp, needrights, maxprotp,
2322 * If we've unwrapped a file, drop the original capability
2323 * and hold the new descriptor. fp after this point refers to
2324 * the actual (unwrapped) object, not the capability.
2326 if (fp != fp_fromcap) {
2332 #else /* !CAPABILITIES */
2333 KASSERT(fp->f_type != DTYPE_CAPABILITY,
2334 ("%s: saw capability", __func__));
2335 if (maxprotp != NULL)
2336 *maxprotp = VM_PROT_ALL;
2337 #endif /* CAPABILITIES */
2340 * FREAD and FWRITE failure return EBADF as per POSIX.
2342 * Only one flag, or 0, may be specified.
2344 if ((flags == FREAD && (fp->f_flag & FREAD) == 0) ||
2345 (flags == FWRITE && (fp->f_flag & FWRITE) == 0)) {
2354 fget(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
2357 return(_fget(td, fd, fpp, 0, rights, NULL, NULL, 0));
2361 fget_mmap(struct thread *td, int fd, cap_rights_t rights, u_char *maxprotp,
2365 return (_fget(td, fd, fpp, 0, rights, NULL, maxprotp, 0));
2369 fget_read(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
2372 return(_fget(td, fd, fpp, FREAD, rights, NULL, NULL, 0));
2376 fget_write(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
2379 return (_fget(td, fd, fpp, FWRITE, rights, NULL, NULL, 0));
2383 * Unlike the other fget() calls, which accept and check capability rights
2384 * but never return capabilities, fgetcap() returns the capability but doesn't
2385 * check capability rights.
2388 fgetcap(struct thread *td, int fd, struct file **fpp)
2391 return (_fget(td, fd, fpp, 0, 0, NULL, NULL, FGET_GETCAP));
2396 * Like fget() but loads the underlying vnode, or returns an error if the
2397 * descriptor does not represent a vnode. Note that pipes use vnodes but
2398 * never have VM objects. The returned vnode will be vref()'d.
2400 * XXX: what about the unused flags ?
2403 _fgetvp(struct thread *td, int fd, int flags, cap_rights_t needrights,
2404 cap_rights_t *haverightsp, struct vnode **vpp)
2410 if ((error = _fget(td, fd, &fp, flags, needrights, haverightsp,
2413 if (fp->f_vnode == NULL) {
2425 fgetvp(struct thread *td, int fd, cap_rights_t rights, struct vnode **vpp)
2428 return (_fgetvp(td, fd, 0, rights, NULL, vpp));
2432 fgetvp_rights(struct thread *td, int fd, cap_rights_t need, cap_rights_t *have,
2435 return (_fgetvp(td, fd, 0, need, have, vpp));
2439 fgetvp_read(struct thread *td, int fd, cap_rights_t rights, struct vnode **vpp)
2442 return (_fgetvp(td, fd, FREAD, rights, NULL, vpp));
2447 fgetvp_write(struct thread *td, int fd, cap_rights_t rights,
2451 return (_fgetvp(td, fd, FWRITE, rights, NULL, vpp));
2456 * Like fget() but loads the underlying socket, or returns an error if the
2457 * descriptor does not represent a socket.
2459 * We bump the ref count on the returned socket. XXX Also obtain the SX lock
2462 * Note: fgetsock() and fputsock() are deprecated, as consumers should rely
2463 * on their file descriptor reference to prevent the socket from being free'd
2467 fgetsock(struct thread *td, int fd, cap_rights_t rights, struct socket **spp,
2476 if ((error = _fget(td, fd, &fp, 0, rights, NULL, NULL, 0)) != 0)
2478 if (fp->f_type != DTYPE_SOCKET) {
2483 *fflagp = fp->f_flag;
2494 * Drop the reference count on the socket and XXX release the SX lock in the
2495 * future. The last reference closes the socket.
2497 * Note: fputsock() is deprecated, see comment for fgetsock().
2500 fputsock(struct socket *so)
2505 CURVNET_SET(so->so_vnet);
2511 * Handle the last reference to a file being closed.
2513 * No special capability handling here, as the capability's fo_close will run
2514 * instead of the object here, and perform any necessary drop on the object.
2517 _fdrop(struct file *fp, struct thread *td)
2522 if (fp->f_count != 0)
2523 panic("fdrop: count %d", fp->f_count);
2524 if (fp->f_ops != &badfileops)
2525 error = fo_close(fp, td);
2526 atomic_subtract_int(&openfiles, 1);
2528 free(fp->f_advice, M_FADVISE);
2529 uma_zfree(file_zone, fp);
2535 * Apply an advisory lock on a file descriptor.
2537 * Just attempt to get a record lock of the requested type on the entire file
2538 * (l_whence = SEEK_SET, l_start = 0, l_len = 0).
2540 #ifndef _SYS_SYSPROTO_H_
2548 sys_flock(struct thread *td, struct flock_args *uap)
2556 if ((error = fget(td, uap->fd, CAP_FLOCK, &fp)) != 0)
2558 if (fp->f_type != DTYPE_VNODE) {
2560 return (EOPNOTSUPP);
2564 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2565 lf.l_whence = SEEK_SET;
2568 if (uap->how & LOCK_UN) {
2569 lf.l_type = F_UNLCK;
2570 atomic_clear_int(&fp->f_flag, FHASLOCK);
2571 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
2574 if (uap->how & LOCK_EX)
2575 lf.l_type = F_WRLCK;
2576 else if (uap->how & LOCK_SH)
2577 lf.l_type = F_RDLCK;
2582 atomic_set_int(&fp->f_flag, FHASLOCK);
2583 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
2584 (uap->how & LOCK_NB) ? F_FLOCK : F_FLOCK | F_WAIT);
2587 VFS_UNLOCK_GIANT(vfslocked);
2591 * Duplicate the specified descriptor to a free descriptor.
2594 dupfdopen(struct thread *td, struct filedesc *fdp, int indx, int dfd, int mode, int error)
2600 * If the to-be-dup'd fd number is greater than the allowed number
2601 * of file descriptors, or the fd to be dup'd has already been
2602 * closed, then reject.
2604 FILEDESC_XLOCK(fdp);
2605 if ((unsigned int)dfd >= fdp->fd_nfiles ||
2606 (wfp = fdp->fd_ofiles[dfd]) == NULL) {
2607 FILEDESC_XUNLOCK(fdp);
2612 * There are two cases of interest here.
2614 * For ENODEV simply dup (dfd) to file descriptor (indx) and return.
2616 * For ENXIO steal away the file structure from (dfd) and store it in
2617 * (indx). (dfd) is effectively closed by this operation.
2619 * Any other error code is just returned.
2624 * Check that the mode the file is being opened for is a
2625 * subset of the mode of the existing descriptor.
2627 if (((mode & (FREAD|FWRITE)) | wfp->f_flag) != wfp->f_flag) {
2628 FILEDESC_XUNLOCK(fdp);
2631 fp = fdp->fd_ofiles[indx];
2632 fdp->fd_ofiles[indx] = wfp;
2633 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
2637 FILEDESC_XUNLOCK(fdp);
2640 * We now own the reference to fp that the ofiles[]
2641 * array used to own. Release it.
2648 * Steal away the file pointer from dfd and stuff it into indx.
2650 fp = fdp->fd_ofiles[indx];
2651 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
2652 fdp->fd_ofiles[dfd] = NULL;
2653 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
2654 fdp->fd_ofileflags[dfd] = 0;
2658 FILEDESC_XUNLOCK(fdp);
2661 * We now own the reference to fp that the ofiles[] array
2662 * used to own. Release it.
2669 FILEDESC_XUNLOCK(fdp);
2676 * Scan all active processes and prisons to see if any of them have a current
2677 * or root directory of `olddp'. If so, replace them with the new mount point.
2680 mountcheckdirs(struct vnode *olddp, struct vnode *newdp)
2682 struct filedesc *fdp;
2687 if (vrefcnt(olddp) == 1)
2690 sx_slock(&allproc_lock);
2691 FOREACH_PROC_IN_SYSTEM(p) {
2695 FILEDESC_XLOCK(fdp);
2696 if (fdp->fd_cdir == olddp) {
2698 fdp->fd_cdir = newdp;
2701 if (fdp->fd_rdir == olddp) {
2703 fdp->fd_rdir = newdp;
2706 if (fdp->fd_jdir == olddp) {
2708 fdp->fd_jdir = newdp;
2711 FILEDESC_XUNLOCK(fdp);
2714 sx_sunlock(&allproc_lock);
2715 if (rootvnode == olddp) {
2720 mtx_lock(&prison0.pr_mtx);
2721 if (prison0.pr_root == olddp) {
2723 prison0.pr_root = newdp;
2726 mtx_unlock(&prison0.pr_mtx);
2727 sx_slock(&allprison_lock);
2728 TAILQ_FOREACH(pr, &allprison, pr_list) {
2729 mtx_lock(&pr->pr_mtx);
2730 if (pr->pr_root == olddp) {
2732 pr->pr_root = newdp;
2735 mtx_unlock(&pr->pr_mtx);
2737 sx_sunlock(&allprison_lock);
2742 struct filedesc_to_leader *
2743 filedesc_to_leader_alloc(struct filedesc_to_leader *old, struct filedesc *fdp, struct proc *leader)
2745 struct filedesc_to_leader *fdtol;
2747 fdtol = malloc(sizeof(struct filedesc_to_leader),
2748 M_FILEDESC_TO_LEADER,
2750 fdtol->fdl_refcount = 1;
2751 fdtol->fdl_holdcount = 0;
2752 fdtol->fdl_wakeup = 0;
2753 fdtol->fdl_leader = leader;
2755 FILEDESC_XLOCK(fdp);
2756 fdtol->fdl_next = old->fdl_next;
2757 fdtol->fdl_prev = old;
2758 old->fdl_next = fdtol;
2759 fdtol->fdl_next->fdl_prev = fdtol;
2760 FILEDESC_XUNLOCK(fdp);
2762 fdtol->fdl_next = fdtol;
2763 fdtol->fdl_prev = fdtol;
2769 * Get file structures globally.
2772 sysctl_kern_file(SYSCTL_HANDLER_ARGS)
2775 struct filedesc *fdp;
2780 error = sysctl_wire_old_buffer(req, 0);
2783 if (req->oldptr == NULL) {
2785 sx_slock(&allproc_lock);
2786 FOREACH_PROC_IN_SYSTEM(p) {
2787 if (p->p_state == PRS_NEW)
2792 /* overestimates sparse tables. */
2793 if (fdp->fd_lastfile > 0)
2794 n += fdp->fd_lastfile;
2797 sx_sunlock(&allproc_lock);
2798 return (SYSCTL_OUT(req, 0, n * sizeof(xf)));
2801 bzero(&xf, sizeof(xf));
2802 xf.xf_size = sizeof(xf);
2803 sx_slock(&allproc_lock);
2804 FOREACH_PROC_IN_SYSTEM(p) {
2806 if (p->p_state == PRS_NEW) {
2810 if (p_cansee(req->td, p) != 0) {
2814 xf.xf_pid = p->p_pid;
2815 xf.xf_uid = p->p_ucred->cr_uid;
2820 FILEDESC_SLOCK(fdp);
2821 for (n = 0; fdp->fd_refcnt > 0 && n < fdp->fd_nfiles; ++n) {
2822 if ((fp = fdp->fd_ofiles[n]) == NULL)
2826 xf.xf_data = fp->f_data;
2827 xf.xf_vnode = fp->f_vnode;
2828 xf.xf_type = fp->f_type;
2829 xf.xf_count = fp->f_count;
2831 xf.xf_offset = fp->f_offset;
2832 xf.xf_flag = fp->f_flag;
2833 error = SYSCTL_OUT(req, &xf, sizeof(xf));
2837 FILEDESC_SUNLOCK(fdp);
2842 sx_sunlock(&allproc_lock);
2846 SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
2847 0, 0, sysctl_kern_file, "S,xfile", "Entire file table");
2849 #ifdef KINFO_OFILE_SIZE
2850 CTASSERT(sizeof(struct kinfo_ofile) == KINFO_OFILE_SIZE);
2853 #ifdef COMPAT_FREEBSD7
2855 export_vnode_for_osysctl(struct vnode *vp, int type,
2856 struct kinfo_ofile *kif, struct filedesc *fdp, struct sysctl_req *req)
2859 char *fullpath, *freepath;
2862 bzero(kif, sizeof(*kif));
2863 kif->kf_structsize = sizeof(*kif);
2867 kif->kf_type = KF_TYPE_VNODE;
2868 /* This function only handles directories. */
2869 if (vp->v_type != VDIR) {
2873 kif->kf_vnode_type = KF_VTYPE_VDIR;
2876 * This is not a true file descriptor, so we set a bogus refcount
2877 * and offset to indicate these fields should be ignored.
2879 kif->kf_ref_count = -1;
2880 kif->kf_offset = -1;
2884 FILEDESC_SUNLOCK(fdp);
2885 vn_fullpath(curthread, vp, &fullpath, &freepath);
2886 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2888 VFS_UNLOCK_GIANT(vfslocked);
2889 strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path));
2890 if (freepath != NULL)
2891 free(freepath, M_TEMP);
2892 error = SYSCTL_OUT(req, kif, sizeof(*kif));
2893 FILEDESC_SLOCK(fdp);
2898 * Get per-process file descriptors for use by procstat(1), et al.
2901 sysctl_kern_proc_ofiledesc(SYSCTL_HANDLER_ARGS)
2903 char *fullpath, *freepath;
2904 struct kinfo_ofile *kif;
2905 struct filedesc *fdp;
2906 int error, i, *name;
2907 struct shmfd *shmfd;
2916 if ((p = pfind((pid_t)name[0])) == NULL)
2918 if ((error = p_candebug(curthread, p))) {
2926 kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK);
2927 FILEDESC_SLOCK(fdp);
2928 if (fdp->fd_cdir != NULL)
2929 export_vnode_for_osysctl(fdp->fd_cdir, KF_FD_TYPE_CWD, kif,
2931 if (fdp->fd_rdir != NULL)
2932 export_vnode_for_osysctl(fdp->fd_rdir, KF_FD_TYPE_ROOT, kif,
2934 if (fdp->fd_jdir != NULL)
2935 export_vnode_for_osysctl(fdp->fd_jdir, KF_FD_TYPE_JAIL, kif,
2937 for (i = 0; i < fdp->fd_nfiles; i++) {
2938 if ((fp = fdp->fd_ofiles[i]) == NULL)
2940 bzero(kif, sizeof(*kif));
2941 kif->kf_structsize = sizeof(*kif);
2950 * When reporting a capability, most fields will be from the
2951 * underlying object, but do mark as a capability. With
2952 * ofiledesc, we don't have a field to export the cap_rights_t,
2953 * but we do with the new filedesc.
2955 if (fp->f_type == DTYPE_CAPABILITY) {
2956 kif->kf_flags |= KF_FLAG_CAPABILITY;
2957 (void)cap_funwrap(fp, 0, &fp);
2960 KASSERT(fp->f_type != DTYPE_CAPABILITY,
2961 ("sysctl_kern_proc_ofiledesc: saw capability"));
2963 switch (fp->f_type) {
2965 kif->kf_type = KF_TYPE_VNODE;
2970 kif->kf_type = KF_TYPE_SOCKET;
2975 kif->kf_type = KF_TYPE_PIPE;
2979 kif->kf_type = KF_TYPE_FIFO;
2984 kif->kf_type = KF_TYPE_KQUEUE;
2988 kif->kf_type = KF_TYPE_CRYPTO;
2992 kif->kf_type = KF_TYPE_MQUEUE;
2996 kif->kf_type = KF_TYPE_SHM;
3001 kif->kf_type = KF_TYPE_SEM;
3005 kif->kf_type = KF_TYPE_PTS;
3010 case DTYPE_PROCDESC:
3011 kif->kf_type = KF_TYPE_PROCDESC;
3016 kif->kf_type = KF_TYPE_UNKNOWN;
3019 kif->kf_ref_count = fp->f_count;
3020 if (fp->f_flag & FREAD)
3021 kif->kf_flags |= KF_FLAG_READ;
3022 if (fp->f_flag & FWRITE)
3023 kif->kf_flags |= KF_FLAG_WRITE;
3024 if (fp->f_flag & FAPPEND)
3025 kif->kf_flags |= KF_FLAG_APPEND;
3026 if (fp->f_flag & FASYNC)
3027 kif->kf_flags |= KF_FLAG_ASYNC;
3028 if (fp->f_flag & FFSYNC)
3029 kif->kf_flags |= KF_FLAG_FSYNC;
3030 if (fp->f_flag & FNONBLOCK)
3031 kif->kf_flags |= KF_FLAG_NONBLOCK;
3032 if (fp->f_flag & O_DIRECT)
3033 kif->kf_flags |= KF_FLAG_DIRECT;
3034 if (fp->f_flag & FHASLOCK)
3035 kif->kf_flags |= KF_FLAG_HASLOCK;
3036 kif->kf_offset = fp->f_offset;
3039 switch (vp->v_type) {
3041 kif->kf_vnode_type = KF_VTYPE_VNON;
3044 kif->kf_vnode_type = KF_VTYPE_VREG;
3047 kif->kf_vnode_type = KF_VTYPE_VDIR;
3050 kif->kf_vnode_type = KF_VTYPE_VBLK;
3053 kif->kf_vnode_type = KF_VTYPE_VCHR;
3056 kif->kf_vnode_type = KF_VTYPE_VLNK;
3059 kif->kf_vnode_type = KF_VTYPE_VSOCK;
3062 kif->kf_vnode_type = KF_VTYPE_VFIFO;
3065 kif->kf_vnode_type = KF_VTYPE_VBAD;
3068 kif->kf_vnode_type = KF_VTYPE_UNKNOWN;
3072 * It is OK to drop the filedesc lock here as we will
3073 * re-validate and re-evaluate its properties when
3074 * the loop continues.
3078 FILEDESC_SUNLOCK(fdp);
3079 vn_fullpath(curthread, vp, &fullpath, &freepath);
3080 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3082 VFS_UNLOCK_GIANT(vfslocked);
3083 strlcpy(kif->kf_path, fullpath,
3084 sizeof(kif->kf_path));
3085 if (freepath != NULL)
3086 free(freepath, M_TEMP);
3087 FILEDESC_SLOCK(fdp);
3090 struct sockaddr *sa;
3092 if (so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa)
3093 == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) {
3094 bcopy(sa, &kif->kf_sa_local, sa->sa_len);
3097 if (so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa)
3098 == 0 && sa->sa_len <= sizeof(kif->kf_sa_peer)) {
3099 bcopy(sa, &kif->kf_sa_peer, sa->sa_len);
3102 kif->kf_sock_domain =
3103 so->so_proto->pr_domain->dom_family;
3104 kif->kf_sock_type = so->so_type;
3105 kif->kf_sock_protocol = so->so_proto->pr_protocol;
3108 strlcpy(kif->kf_path, tty_devname(tp),
3109 sizeof(kif->kf_path));
3112 shm_path(shmfd, kif->kf_path, sizeof(kif->kf_path));
3113 error = SYSCTL_OUT(req, kif, sizeof(*kif));
3117 FILEDESC_SUNLOCK(fdp);
3123 static SYSCTL_NODE(_kern_proc, KERN_PROC_OFILEDESC, ofiledesc, CTLFLAG_RD,
3124 sysctl_kern_proc_ofiledesc, "Process ofiledesc entries");
3125 #endif /* COMPAT_FREEBSD7 */
3127 #ifdef KINFO_FILE_SIZE
3128 CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE);
3132 export_fd_for_sysctl(void *data, int type, int fd, int fflags, int refcnt,
3133 int64_t offset, int fd_is_cap, cap_rights_t fd_cap_rights,
3134 struct kinfo_file *kif, struct sysctl_req *req)
3139 } fflags_table[] = {
3140 { FAPPEND, KF_FLAG_APPEND },
3141 { FASYNC, KF_FLAG_ASYNC },
3142 { FFSYNC, KF_FLAG_FSYNC },
3143 { FHASLOCK, KF_FLAG_HASLOCK },
3144 { FNONBLOCK, KF_FLAG_NONBLOCK },
3145 { FREAD, KF_FLAG_READ },
3146 { FWRITE, KF_FLAG_WRITE },
3147 { O_CREAT, KF_FLAG_CREAT },
3148 { O_DIRECT, KF_FLAG_DIRECT },
3149 { O_EXCL, KF_FLAG_EXCL },
3150 { O_EXEC, KF_FLAG_EXEC },
3151 { O_EXLOCK, KF_FLAG_EXLOCK },
3152 { O_NOFOLLOW, KF_FLAG_NOFOLLOW },
3153 { O_SHLOCK, KF_FLAG_SHLOCK },
3154 { O_TRUNC, KF_FLAG_TRUNC }
3156 #define NFFLAGS (sizeof(fflags_table) / sizeof(*fflags_table))
3158 int error, vfslocked;
3161 bzero(kif, sizeof(*kif));
3165 vp = (struct vnode *)data;
3166 error = fill_vnode_info(vp, kif);
3167 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3169 VFS_UNLOCK_GIANT(vfslocked);
3171 case KF_TYPE_SOCKET:
3172 error = fill_socket_info((struct socket *)data, kif);
3175 error = fill_pipe_info((struct pipe *)data, kif);
3178 error = fill_pts_info((struct tty *)data, kif);
3180 case KF_TYPE_PROCDESC:
3181 error = fill_procdesc_info((struct procdesc *)data, kif);
3184 error = fill_shm_info((struct file *)data, kif);
3190 kif->kf_status |= KF_ATTR_VALID;
3193 * Translate file access flags.
3195 for (i = 0; i < NFFLAGS; i++)
3196 if (fflags & fflags_table[i].fflag)
3197 kif->kf_flags |= fflags_table[i].kf_fflag;
3199 kif->kf_flags |= KF_FLAG_CAPABILITY;
3201 kif->kf_cap_rights = fd_cap_rights;
3203 kif->kf_type = type;
3204 kif->kf_ref_count = refcnt;
3205 kif->kf_offset = offset;
3206 /* Pack record size down */
3207 kif->kf_structsize = offsetof(struct kinfo_file, kf_path) +
3208 strlen(kif->kf_path) + 1;
3209 kif->kf_structsize = roundup(kif->kf_structsize, sizeof(uint64_t));
3210 error = SYSCTL_OUT(req, kif, kif->kf_structsize);
3215 * Get per-process file descriptors for use by procstat(1), et al.
3218 sysctl_kern_proc_filedesc(SYSCTL_HANDLER_ARGS)
3221 struct filedesc *fdp;
3222 struct kinfo_file *kif;
3224 struct vnode *cttyvp, *textvp, *tracevp;
3228 int error, i, *name;
3229 int fd_is_cap, type, refcnt, fflags;
3230 cap_rights_t fd_cap_rights;
3233 if ((p = pfind((pid_t)name[0])) == NULL)
3235 if ((error = p_candebug(curthread, p))) {
3240 tracevp = p->p_tracevp;
3241 if (tracevp != NULL)
3244 textvp = p->p_textvp;
3247 /* Controlling tty. */
3249 if (p->p_pgrp != NULL && p->p_pgrp->pg_session != NULL) {
3250 cttyvp = p->p_pgrp->pg_session->s_ttyvp;
3256 kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK);
3257 if (tracevp != NULL)
3258 export_fd_for_sysctl(tracevp, KF_TYPE_VNODE, KF_FD_TYPE_TRACE,
3259 FREAD | FWRITE, -1, -1, 0, 0, kif, req);
3261 export_fd_for_sysctl(textvp, KF_TYPE_VNODE, KF_FD_TYPE_TEXT,
3262 FREAD, -1, -1, 0, 0, kif, req);
3264 export_fd_for_sysctl(cttyvp, KF_TYPE_VNODE, KF_FD_TYPE_CTTY,
3265 FREAD | FWRITE, -1, -1, 0, 0, kif, req);
3268 FILEDESC_SLOCK(fdp);
3269 /* working directory */
3270 if (fdp->fd_cdir != NULL) {
3272 data = fdp->fd_cdir;
3273 FILEDESC_SUNLOCK(fdp);
3274 export_fd_for_sysctl(data, KF_TYPE_VNODE, KF_FD_TYPE_CWD,
3275 FREAD, -1, -1, 0, 0, kif, req);
3276 FILEDESC_SLOCK(fdp);
3278 /* root directory */
3279 if (fdp->fd_rdir != NULL) {
3281 data = fdp->fd_rdir;
3282 FILEDESC_SUNLOCK(fdp);
3283 export_fd_for_sysctl(data, KF_TYPE_VNODE, KF_FD_TYPE_ROOT,
3284 FREAD, -1, -1, 0, 0, kif, req);
3285 FILEDESC_SLOCK(fdp);
3287 /* jail directory */
3288 if (fdp->fd_jdir != NULL) {
3290 data = fdp->fd_jdir;
3291 FILEDESC_SUNLOCK(fdp);
3292 export_fd_for_sysctl(data, KF_TYPE_VNODE, KF_FD_TYPE_JAIL,
3293 FREAD, -1, -1, 0, 0, kif, req);
3294 FILEDESC_SLOCK(fdp);
3296 for (i = 0; i < fdp->fd_nfiles; i++) {
3297 if ((fp = fdp->fd_ofiles[i]) == NULL)
3305 * When reporting a capability, most fields will be from the
3306 * underlying object, but do mark as a capability and export
3307 * the capability rights mask.
3309 if (fp->f_type == DTYPE_CAPABILITY) {
3311 fd_cap_rights = cap_rights(fp);
3312 (void)cap_funwrap(fp, 0, &fp);
3314 #else /* !CAPABILITIES */
3315 KASSERT(fp->f_type != DTYPE_CAPABILITY,
3316 ("sysctl_kern_proc_filedesc: saw capability"));
3318 switch (fp->f_type) {
3320 type = KF_TYPE_VNODE;
3326 type = KF_TYPE_SOCKET;
3331 type = KF_TYPE_PIPE;
3336 type = KF_TYPE_FIFO;
3342 type = KF_TYPE_KQUEUE;
3346 type = KF_TYPE_CRYPTO;
3350 type = KF_TYPE_MQUEUE;
3368 case DTYPE_PROCDESC:
3369 type = KF_TYPE_PROCDESC;
3375 type = KF_TYPE_UNKNOWN;
3378 refcnt = fp->f_count;
3379 fflags = fp->f_flag;
3380 offset = fp->f_offset;
3383 * Create sysctl entry.
3384 * It is OK to drop the filedesc lock here as we will
3385 * re-validate and re-evaluate its properties when
3386 * the loop continues.
3388 oldidx = req->oldidx;
3389 if (type == KF_TYPE_VNODE || type == KF_TYPE_FIFO)
3390 FILEDESC_SUNLOCK(fdp);
3391 error = export_fd_for_sysctl(data, type, i, fflags, refcnt,
3392 offset, fd_is_cap, fd_cap_rights, kif, req);
3393 if (type == KF_TYPE_VNODE || type == KF_TYPE_FIFO)
3394 FILEDESC_SLOCK(fdp);
3396 if (error == ENOMEM) {
3398 * The hack to keep the ABI of sysctl
3399 * kern.proc.filedesc intact, but not
3400 * to account a partially copied
3401 * kinfo_file into the oldidx.
3403 req->oldidx = oldidx;
3409 FILEDESC_SUNLOCK(fdp);
3418 vntype_to_kinfo(int vtype)
3423 } vtypes_table[] = {
3424 { VBAD, KF_VTYPE_VBAD },
3425 { VBLK, KF_VTYPE_VBLK },
3426 { VCHR, KF_VTYPE_VCHR },
3427 { VDIR, KF_VTYPE_VDIR },
3428 { VFIFO, KF_VTYPE_VFIFO },
3429 { VLNK, KF_VTYPE_VLNK },
3430 { VNON, KF_VTYPE_VNON },
3431 { VREG, KF_VTYPE_VREG },
3432 { VSOCK, KF_VTYPE_VSOCK }
3434 #define NVTYPES (sizeof(vtypes_table) / sizeof(*vtypes_table))
3438 * Perform vtype translation.
3440 for (i = 0; i < NVTYPES; i++)
3441 if (vtypes_table[i].vtype == vtype)
3444 return (vtypes_table[i].kf_vtype);
3446 return (KF_VTYPE_UNKNOWN);
3450 fill_vnode_info(struct vnode *vp, struct kinfo_file *kif)
3453 char *fullpath, *freepath;
3454 int error, vfslocked;
3458 kif->kf_vnode_type = vntype_to_kinfo(vp->v_type);
3461 error = vn_fullpath(curthread, vp, &fullpath, &freepath);
3463 strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path));
3465 if (freepath != NULL)
3466 free(freepath, M_TEMP);
3469 * Retrieve vnode attributes.
3471 va.va_fsid = VNOVAL;
3473 vfslocked = VFS_LOCK_GIANT(vp->v_mount);
3474 vn_lock(vp, LK_SHARED | LK_RETRY);
3475 error = VOP_GETATTR(vp, &va, curthread->td_ucred);
3477 VFS_UNLOCK_GIANT(vfslocked);
3480 if (va.va_fsid != VNOVAL)
3481 kif->kf_un.kf_file.kf_file_fsid = va.va_fsid;
3483 kif->kf_un.kf_file.kf_file_fsid =
3484 vp->v_mount->mnt_stat.f_fsid.val[0];
3485 kif->kf_un.kf_file.kf_file_fileid = va.va_fileid;
3486 kif->kf_un.kf_file.kf_file_mode = MAKEIMODE(va.va_type, va.va_mode);
3487 kif->kf_un.kf_file.kf_file_size = va.va_size;
3488 kif->kf_un.kf_file.kf_file_rdev = va.va_rdev;
3493 fill_socket_info(struct socket *so, struct kinfo_file *kif)
3495 struct sockaddr *sa;
3496 struct inpcb *inpcb;
3497 struct unpcb *unpcb;
3502 kif->kf_sock_domain = so->so_proto->pr_domain->dom_family;
3503 kif->kf_sock_type = so->so_type;
3504 kif->kf_sock_protocol = so->so_proto->pr_protocol;
3505 kif->kf_un.kf_sock.kf_sock_pcb = (uintptr_t)so->so_pcb;
3506 switch(kif->kf_sock_domain) {
3509 if (kif->kf_sock_protocol == IPPROTO_TCP) {
3510 if (so->so_pcb != NULL) {
3511 inpcb = (struct inpcb *)(so->so_pcb);
3512 kif->kf_un.kf_sock.kf_sock_inpcb =
3513 (uintptr_t)inpcb->inp_ppcb;
3518 if (so->so_pcb != NULL) {
3519 unpcb = (struct unpcb *)(so->so_pcb);
3520 if (unpcb->unp_conn) {
3521 kif->kf_un.kf_sock.kf_sock_unpconn =
3522 (uintptr_t)unpcb->unp_conn;
3523 kif->kf_un.kf_sock.kf_sock_rcv_sb_state =
3524 so->so_rcv.sb_state;
3525 kif->kf_un.kf_sock.kf_sock_snd_sb_state =
3526 so->so_snd.sb_state;
3531 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
3532 if (error == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) {
3533 bcopy(sa, &kif->kf_sa_local, sa->sa_len);
3536 error = so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa);
3537 if (error == 0 && sa->sa_len <= sizeof(kif->kf_sa_peer)) {
3538 bcopy(sa, &kif->kf_sa_peer, sa->sa_len);
3541 strncpy(kif->kf_path, so->so_proto->pr_domain->dom_name,
3542 sizeof(kif->kf_path));
3547 fill_pts_info(struct tty *tp, struct kinfo_file *kif)
3552 kif->kf_un.kf_pts.kf_pts_dev = tty_udev(tp);
3553 strlcpy(kif->kf_path, tty_devname(tp), sizeof(kif->kf_path));
3558 fill_pipe_info(struct pipe *pi, struct kinfo_file *kif)
3563 kif->kf_un.kf_pipe.kf_pipe_addr = (uintptr_t)pi;
3564 kif->kf_un.kf_pipe.kf_pipe_peer = (uintptr_t)pi->pipe_peer;
3565 kif->kf_un.kf_pipe.kf_pipe_buffer_cnt = pi->pipe_buffer.cnt;
3570 fill_procdesc_info(struct procdesc *pdp, struct kinfo_file *kif)
3575 kif->kf_un.kf_proc.kf_pid = pdp->pd_pid;
3580 fill_shm_info(struct file *fp, struct kinfo_file *kif)
3586 if (fp->f_data == NULL)
3588 if (fo_stat(fp, &sb, td->td_ucred, td) != 0)
3590 shm_path(fp->f_data, kif->kf_path, sizeof(kif->kf_path));
3591 kif->kf_un.kf_file.kf_file_mode = sb.st_mode;
3592 kif->kf_un.kf_file.kf_file_size = sb.st_size;
3596 static SYSCTL_NODE(_kern_proc, KERN_PROC_FILEDESC, filedesc, CTLFLAG_RD,
3597 sysctl_kern_proc_filedesc, "Process filedesc entries");
3601 * For the purposes of debugging, generate a human-readable string for the
3605 file_type_to_name(short type)
3635 * For the purposes of debugging, identify a process (if any, perhaps one of
3636 * many) that references the passed file in its file descriptor array. Return
3639 static struct proc *
3640 file_to_first_proc(struct file *fp)
3642 struct filedesc *fdp;
3646 FOREACH_PROC_IN_SYSTEM(p) {
3647 if (p->p_state == PRS_NEW)
3652 for (n = 0; n < fdp->fd_nfiles; n++) {
3653 if (fp == fdp->fd_ofiles[n])
3661 db_print_file(struct file *fp, int header)
3666 db_printf("%8s %4s %8s %8s %4s %5s %6s %8s %5s %12s\n",
3667 "File", "Type", "Data", "Flag", "GCFl", "Count",
3668 "MCount", "Vnode", "FPID", "FCmd");
3669 p = file_to_first_proc(fp);
3670 db_printf("%8p %4s %8p %08x %04x %5d %6d %8p %5d %12s\n", fp,
3671 file_type_to_name(fp->f_type), fp->f_data, fp->f_flag,
3672 0, fp->f_count, 0, fp->f_vnode,
3673 p != NULL ? p->p_pid : -1, p != NULL ? p->p_comm : "-");
3676 DB_SHOW_COMMAND(file, db_show_file)
3681 db_printf("usage: show file <addr>\n");
3684 fp = (struct file *)addr;
3685 db_print_file(fp, 1);
3688 DB_SHOW_COMMAND(files, db_show_files)
3690 struct filedesc *fdp;
3697 FOREACH_PROC_IN_SYSTEM(p) {
3698 if (p->p_state == PRS_NEW)
3700 if ((fdp = p->p_fd) == NULL)
3702 for (n = 0; n < fdp->fd_nfiles; ++n) {
3703 if ((fp = fdp->fd_ofiles[n]) == NULL)
3705 db_print_file(fp, header);
3712 SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
3713 &maxfilesperproc, 0, "Maximum files allowed open per process");
3715 SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
3716 &maxfiles, 0, "Maximum number of files");
3718 SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD,
3719 __DEVOLATILE(int *, &openfiles), 0, "System-wide number of open files");
3723 filelistinit(void *dummy)
3726 file_zone = uma_zcreate("Files", sizeof(struct file), NULL, NULL,
3727 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
3728 mtx_init(&sigio_lock, "sigio lock", NULL, MTX_DEF);
3729 mtx_init(&fdesc_mtx, "fdesc", NULL, MTX_DEF);
3731 SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, filelistinit, NULL);
3733 /*-------------------------------------------------------------------*/
3736 badfo_readwrite(struct file *fp, struct uio *uio, struct ucred *active_cred,
3737 int flags, struct thread *td)
3744 badfo_truncate(struct file *fp, off_t length, struct ucred *active_cred,
3752 badfo_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred,
3760 badfo_poll(struct file *fp, int events, struct ucred *active_cred,
3768 badfo_kqfilter(struct file *fp, struct knote *kn)
3775 badfo_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
3783 badfo_close(struct file *fp, struct thread *td)
3790 badfo_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
3798 badfo_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
3805 struct fileops badfileops = {
3806 .fo_read = badfo_readwrite,
3807 .fo_write = badfo_readwrite,
3808 .fo_truncate = badfo_truncate,
3809 .fo_ioctl = badfo_ioctl,
3810 .fo_poll = badfo_poll,
3811 .fo_kqfilter = badfo_kqfilter,
3812 .fo_stat = badfo_stat,
3813 .fo_close = badfo_close,
3814 .fo_chmod = badfo_chmod,
3815 .fo_chown = badfo_chown,
3819 invfo_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
3827 invfo_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
3834 /*-------------------------------------------------------------------*/
3837 * File Descriptor pseudo-device driver (/dev/fd/).
3839 * Opening minor device N dup()s the file (if any) connected to file
3840 * descriptor N belonging to the calling process. Note that this driver
3841 * consists of only the ``open()'' routine, because all subsequent
3842 * references to this file will be direct to the other driver.
3844 * XXX: we could give this one a cloning event handler if necessary.
3849 fdopen(struct cdev *dev, int mode, int type, struct thread *td)
3853 * XXX Kludge: set curthread->td_dupfd to contain the value of the
3854 * the file descriptor being sought for duplication. The error
3855 * return ensures that the vnode for this device will be released
3856 * by vn_open. Open will detect this special error and take the
3857 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
3858 * will simply report the error.
3860 td->td_dupfd = dev2unit(dev);
3864 static struct cdevsw fildesc_cdevsw = {
3865 .d_version = D_VERSION,
3871 fildesc_drvinit(void *unused)
3875 dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 0, NULL,
3876 UID_ROOT, GID_WHEEL, 0666, "fd/0");
3877 make_dev_alias(dev, "stdin");
3878 dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 1, NULL,
3879 UID_ROOT, GID_WHEEL, 0666, "fd/1");
3880 make_dev_alias(dev, "stdout");
3881 dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 2, NULL,
3882 UID_ROOT, GID_WHEEL, 0666, "fd/2");
3883 make_dev_alias(dev, "stderr");
3886 SYSINIT(fildescdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, fildesc_drvinit, NULL);