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 */
116 #define DUP_CLOEXEC 0x4 /* Atomically set FD_CLOEXEC. */
118 static int closefp(struct filedesc *fdp, int fd, struct file *fp,
119 struct thread *td, int holdleaders);
120 static int do_dup(struct thread *td, int flags, int old, int new,
122 static int fd_first_free(struct filedesc *fdp, int low, int size);
123 static int fd_last_used(struct filedesc *fdp, int size);
124 static void fdgrowtable(struct filedesc *fdp, int nfd);
125 static void fdunused(struct filedesc *fdp, int fd);
126 static void fdused(struct filedesc *fdp, int fd);
127 static int fill_pipe_info(struct pipe *pi, struct kinfo_file *kif);
128 static int fill_procdesc_info(struct procdesc *pdp,
129 struct kinfo_file *kif);
130 static int fill_pts_info(struct tty *tp, struct kinfo_file *kif);
131 static int fill_shm_info(struct file *fp, struct kinfo_file *kif);
132 static int fill_socket_info(struct socket *so, struct kinfo_file *kif);
133 static int fill_vnode_info(struct vnode *vp, struct kinfo_file *kif);
138 * - An array of open file descriptors (fd_ofiles)
139 * - An array of file flags (fd_ofileflags)
140 * - A bitmap recording which descriptors are in use (fd_map)
142 * A process starts out with NDFILE descriptors. The value of NDFILE has
143 * been selected based the historical limit of 20 open files, and an
144 * assumption that the majority of processes, especially short-lived
145 * processes like shells, will never need more.
147 * If this initial allocation is exhausted, a larger descriptor table and
148 * map are allocated dynamically, and the pointers in the process's struct
149 * filedesc are updated to point to those. This is repeated every time
150 * the process runs out of file descriptors (provided it hasn't hit its
153 * Since threads may hold references to individual descriptor table
154 * entries, the tables are never freed. Instead, they are placed on a
155 * linked list and freed only when the struct filedesc is released.
158 #define NDSLOTSIZE sizeof(NDSLOTTYPE)
159 #define NDENTRIES (NDSLOTSIZE * __CHAR_BIT)
160 #define NDSLOT(x) ((x) / NDENTRIES)
161 #define NDBIT(x) ((NDSLOTTYPE)1 << ((x) % NDENTRIES))
162 #define NDSLOTS(x) (((x) + NDENTRIES - 1) / NDENTRIES)
165 * SLIST entry used to keep track of ofiles which must be reclaimed when
169 struct file **ft_table;
170 SLIST_ENTRY(freetable) ft_next;
174 * Initial allocation: a filedesc structure + the head of SLIST used to
175 * keep track of old ofiles + enough space for NDFILE descriptors.
178 struct filedesc fd_fd;
179 SLIST_HEAD(, freetable) fd_free;
180 struct file *fd_dfiles[NDFILE];
181 char fd_dfileflags[NDFILE];
182 NDSLOTTYPE fd_dmap[NDSLOTS(NDFILE)];
186 * Descriptor management.
188 volatile int openfiles; /* actual number of open files */
189 struct mtx sigio_lock; /* mtx to protect pointers to sigio */
190 void (*mq_fdclose)(struct thread *td, int fd, struct file *fp);
192 /* A mutex to protect the association between a proc and filedesc. */
193 static struct mtx fdesc_mtx;
196 * If low >= size, just return low. Otherwise find the first zero bit in the
197 * given bitmap, starting at low and not exceeding size - 1. Return size if
201 fd_first_free(struct filedesc *fdp, int low, int size)
203 NDSLOTTYPE *map = fdp->fd_map;
211 if (low % NDENTRIES) {
212 mask = ~(~(NDSLOTTYPE)0 >> (NDENTRIES - (low % NDENTRIES)));
213 if ((mask &= ~map[off]) != 0UL)
214 return (off * NDENTRIES + ffsl(mask) - 1);
217 for (maxoff = NDSLOTS(size); off < maxoff; ++off)
218 if (map[off] != ~0UL)
219 return (off * NDENTRIES + ffsl(~map[off]) - 1);
224 * Find the highest non-zero bit in the given bitmap, starting at 0 and
225 * not exceeding size - 1. Return -1 if not found.
228 fd_last_used(struct filedesc *fdp, int size)
230 NDSLOTTYPE *map = fdp->fd_map;
235 if (size % NDENTRIES) {
236 mask = ~(~(NDSLOTTYPE)0 << (size % NDENTRIES));
237 if ((mask &= map[off]) != 0)
238 return (off * NDENTRIES + flsl(mask) - 1);
241 for (minoff = NDSLOT(0); off >= minoff; --off)
243 return (off * NDENTRIES + flsl(map[off]) - 1);
248 fdisused(struct filedesc *fdp, int fd)
251 FILEDESC_LOCK_ASSERT(fdp);
253 KASSERT(fd >= 0 && fd < fdp->fd_nfiles,
254 ("file descriptor %d out of range (0, %d)", fd, fdp->fd_nfiles));
256 return ((fdp->fd_map[NDSLOT(fd)] & NDBIT(fd)) != 0);
260 * Mark a file descriptor as used.
263 fdused(struct filedesc *fdp, int fd)
266 FILEDESC_XLOCK_ASSERT(fdp);
268 KASSERT(!fdisused(fdp, fd), ("fd=%d is already used", fd));
270 fdp->fd_map[NDSLOT(fd)] |= NDBIT(fd);
271 if (fd > fdp->fd_lastfile)
272 fdp->fd_lastfile = fd;
273 if (fd == fdp->fd_freefile)
274 fdp->fd_freefile = fd_first_free(fdp, fd, fdp->fd_nfiles);
278 * Mark a file descriptor as unused.
281 fdunused(struct filedesc *fdp, int fd)
284 FILEDESC_XLOCK_ASSERT(fdp);
286 KASSERT(fdisused(fdp, fd), ("fd=%d is already unused", fd));
287 KASSERT(fdp->fd_ofiles[fd] == NULL, ("fd=%d is still in use", fd));
289 fdp->fd_map[NDSLOT(fd)] &= ~NDBIT(fd);
290 if (fd < fdp->fd_freefile)
291 fdp->fd_freefile = fd;
292 if (fd == fdp->fd_lastfile)
293 fdp->fd_lastfile = fd_last_used(fdp, fd);
297 * System calls on descriptors.
299 #ifndef _SYS_SYSPROTO_H_
300 struct getdtablesize_args {
306 sys_getdtablesize(struct thread *td, struct getdtablesize_args *uap)
308 struct proc *p = td->td_proc;
313 min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
314 lim = racct_get_limit(td->td_proc, RACCT_NOFILE);
316 if (lim < td->td_retval[0])
317 td->td_retval[0] = lim;
322 * Duplicate a file descriptor to a particular value.
324 * Note: keep in mind that a potential race condition exists when closing
325 * descriptors from a shared descriptor table (via rfork).
327 #ifndef _SYS_SYSPROTO_H_
335 sys_dup2(struct thread *td, struct dup2_args *uap)
338 return (do_dup(td, DUP_FIXED, (int)uap->from, (int)uap->to,
343 * Duplicate a file descriptor.
345 #ifndef _SYS_SYSPROTO_H_
352 sys_dup(struct thread *td, struct dup_args *uap)
355 return (do_dup(td, 0, (int)uap->fd, 0, td->td_retval));
359 * The file control system call.
361 #ifndef _SYS_SYSPROTO_H_
370 sys_fcntl(struct thread *td, struct fcntl_args *uap)
385 * Convert old flock structure to new.
387 error = copyin((void *)(intptr_t)uap->arg, &ofl, sizeof(ofl));
388 fl.l_start = ofl.l_start;
389 fl.l_len = ofl.l_len;
390 fl.l_pid = ofl.l_pid;
391 fl.l_type = ofl.l_type;
392 fl.l_whence = ofl.l_whence;
412 error = copyin((void *)(intptr_t)uap->arg, &fl, sizeof(fl));
421 error = kern_fcntl(td, uap->fd, cmd, arg);
424 if (uap->cmd == F_OGETLK) {
425 ofl.l_start = fl.l_start;
426 ofl.l_len = fl.l_len;
427 ofl.l_pid = fl.l_pid;
428 ofl.l_type = fl.l_type;
429 ofl.l_whence = fl.l_whence;
430 error = copyout(&ofl, (void *)(intptr_t)uap->arg, sizeof(ofl));
431 } else if (uap->cmd == F_GETLK) {
432 error = copyout(&fl, (void *)(intptr_t)uap->arg, sizeof(fl));
438 fdunwrap(int fd, cap_rights_t rights, struct filedesc *fdp, struct file **fpp)
441 FILEDESC_LOCK_ASSERT(fdp);
443 *fpp = fget_locked(fdp, fd);
448 if ((*fpp)->f_type == DTYPE_CAPABILITY) {
449 int err = cap_funwrap(*fpp, rights, fpp);
455 #endif /* CAPABILITIES */
460 kern_fcntl(struct thread *td, int fd, int cmd, intptr_t arg)
462 struct filedesc *fdp;
481 error = do_dup(td, DUP_FCNTL, fd, tmp, td->td_retval);
484 case F_DUPFD_CLOEXEC:
486 error = do_dup(td, DUP_FCNTL | DUP_CLOEXEC, fd, tmp,
492 error = do_dup(td, DUP_FIXED, fd, tmp, td->td_retval);
495 case F_DUP2FD_CLOEXEC:
497 error = do_dup(td, DUP_FIXED | DUP_CLOEXEC, fd, tmp,
503 if ((fp = fget_locked(fdp, fd)) == NULL) {
504 FILEDESC_SUNLOCK(fdp);
508 pop = &fdp->fd_ofileflags[fd];
509 td->td_retval[0] = (*pop & UF_EXCLOSE) ? FD_CLOEXEC : 0;
510 FILEDESC_SUNLOCK(fdp);
515 if ((fp = fget_locked(fdp, fd)) == NULL) {
516 FILEDESC_XUNLOCK(fdp);
520 pop = &fdp->fd_ofileflags[fd];
521 *pop = (*pop &~ UF_EXCLOSE) |
522 (arg & FD_CLOEXEC ? UF_EXCLOSE : 0);
523 FILEDESC_XUNLOCK(fdp);
528 error = fdunwrap(fd, CAP_FCNTL, fdp, &fp);
530 FILEDESC_SUNLOCK(fdp);
533 td->td_retval[0] = OFLAGS(fp->f_flag);
534 FILEDESC_SUNLOCK(fdp);
539 error = fdunwrap(fd, CAP_FCNTL, fdp, &fp);
541 FILEDESC_SUNLOCK(fdp);
545 FILEDESC_SUNLOCK(fdp);
547 tmp = flg = fp->f_flag;
549 tmp |= FFLAGS(arg & ~O_ACCMODE) & FCNTLFLAGS;
550 } while(atomic_cmpset_int(&fp->f_flag, flg, tmp) == 0);
551 tmp = fp->f_flag & FNONBLOCK;
552 error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
557 tmp = fp->f_flag & FASYNC;
558 error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td);
563 atomic_clear_int(&fp->f_flag, FNONBLOCK);
565 (void)fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
571 error = fdunwrap(fd, CAP_FCNTL, fdp, &fp);
573 FILEDESC_SUNLOCK(fdp);
577 FILEDESC_SUNLOCK(fdp);
578 error = fo_ioctl(fp, FIOGETOWN, &tmp, td->td_ucred, td);
580 td->td_retval[0] = tmp;
586 error = fdunwrap(fd, CAP_FCNTL, fdp, &fp);
588 FILEDESC_SUNLOCK(fdp);
592 FILEDESC_SUNLOCK(fdp);
594 error = fo_ioctl(fp, FIOSETOWN, &tmp, td->td_ucred, td);
599 error = priv_check(td, PRIV_NFS_LOCKD);
607 /* FALLTHROUGH F_SETLK */
612 error = fdunwrap(fd, CAP_FLOCK, fdp, &fp);
614 FILEDESC_SUNLOCK(fdp);
617 if (fp->f_type != DTYPE_VNODE) {
618 FILEDESC_SUNLOCK(fdp);
622 flp = (struct flock *)arg;
623 if (flp->l_whence == SEEK_CUR) {
624 foffset = foffset_get(fp);
627 foffset > OFF_MAX - flp->l_start)) {
628 FILEDESC_SUNLOCK(fdp);
632 flp->l_start += foffset;
636 * VOP_ADVLOCK() may block.
639 FILEDESC_SUNLOCK(fdp);
641 switch (flp->l_type) {
643 if ((fp->f_flag & FREAD) == 0) {
647 PROC_LOCK(p->p_leader);
648 p->p_leader->p_flag |= P_ADVLOCK;
649 PROC_UNLOCK(p->p_leader);
650 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
654 if ((fp->f_flag & FWRITE) == 0) {
658 PROC_LOCK(p->p_leader);
659 p->p_leader->p_flag |= P_ADVLOCK;
660 PROC_UNLOCK(p->p_leader);
661 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
665 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
670 * Temporary api for testing remote lock
673 if (flg != F_REMOTE) {
677 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
678 F_UNLCKSYS, flp, flg);
684 if (error != 0 || flp->l_type == F_UNLCK ||
685 flp->l_type == F_UNLCKSYS) {
691 * Check for a race with close.
693 * The vnode is now advisory locked (or unlocked, but this case
694 * is not really important) as the caller requested.
695 * We had to drop the filedesc lock, so we need to recheck if
696 * the descriptor is still valid, because if it was closed
697 * in the meantime we need to remove advisory lock from the
698 * vnode - close on any descriptor leading to an advisory
699 * locked vnode, removes that lock.
700 * We will return 0 on purpose in that case, as the result of
701 * successful advisory lock might have been externally visible
702 * already. This is fine - effectively we pretend to the caller
703 * that the closing thread was a bit slower and that the
704 * advisory lock succeeded before the close.
707 if (fget_locked(fdp, fd) != fp) {
708 FILEDESC_SUNLOCK(fdp);
709 flp->l_whence = SEEK_SET;
712 flp->l_type = F_UNLCK;
713 (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
714 F_UNLCK, flp, F_POSIX);
716 FILEDESC_SUNLOCK(fdp);
722 error = fdunwrap(fd, CAP_FLOCK, fdp, &fp);
724 FILEDESC_SUNLOCK(fdp);
727 if (fp->f_type != DTYPE_VNODE) {
728 FILEDESC_SUNLOCK(fdp);
732 flp = (struct flock *)arg;
733 if (flp->l_type != F_RDLCK && flp->l_type != F_WRLCK &&
734 flp->l_type != F_UNLCK) {
735 FILEDESC_SUNLOCK(fdp);
739 if (flp->l_whence == SEEK_CUR) {
740 foffset = foffset_get(fp);
741 if ((flp->l_start > 0 &&
742 foffset > OFF_MAX - flp->l_start) ||
744 foffset < OFF_MIN - flp->l_start)) {
745 FILEDESC_SUNLOCK(fdp);
749 flp->l_start += foffset;
752 * VOP_ADVLOCK() may block.
755 FILEDESC_SUNLOCK(fdp);
757 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK, flp,
763 arg = arg ? 128 * 1024: 0;
767 if ((fp = fget_locked(fdp, fd)) == NULL) {
768 FILEDESC_SUNLOCK(fdp);
772 if (fp->f_type != DTYPE_VNODE) {
773 FILEDESC_SUNLOCK(fdp);
778 FILEDESC_SUNLOCK(fdp);
781 error = vn_lock(vp, LK_SHARED);
786 bsize = fp->f_vnode->v_mount->mnt_stat.f_iosize;
788 fp->f_seqcount = (arg + bsize - 1) / bsize;
790 new = old = fp->f_flag;
792 } while (!atomic_cmpset_rel_int(&fp->f_flag, old, new));
795 new = old = fp->f_flag;
797 } while (!atomic_cmpset_rel_int(&fp->f_flag, old, new));
810 * Common code for dup, dup2, fcntl(F_DUPFD) and fcntl(F_DUP2FD).
813 do_dup(struct thread *td, int flags, int old, int new,
816 struct filedesc *fdp;
826 * Verify we have a valid descriptor to dup from and possibly to
827 * dup to. Unlike dup() and dup2(), fcntl()'s F_DUPFD should
828 * return EINVAL when the new descriptor is out of bounds.
833 return (flags & DUP_FCNTL ? EINVAL : EBADF);
835 maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
838 return (flags & DUP_FCNTL ? EINVAL : EBADF);
841 if (fget_locked(fdp, old) == NULL) {
842 FILEDESC_XUNLOCK(fdp);
845 if (flags & DUP_FIXED && old == new) {
847 if (flags & DUP_CLOEXEC)
848 fdp->fd_ofileflags[new] |= UF_EXCLOSE;
849 FILEDESC_XUNLOCK(fdp);
852 fp = fdp->fd_ofiles[old];
856 * If the caller specified a file descriptor, make sure the file
857 * table is large enough to hold it, and grab it. Otherwise, just
858 * allocate a new descriptor the usual way.
860 if (flags & DUP_FIXED) {
861 if (new >= fdp->fd_nfiles) {
863 * The resource limits are here instead of e.g.
864 * fdalloc(), because the file descriptor table may be
865 * shared between processes, so we can't really use
866 * racct_add()/racct_sub(). Instead of counting the
867 * number of actually allocated descriptors, just put
868 * the limit on the size of the file descriptor table.
872 error = racct_set(p, RACCT_NOFILE, new + 1);
875 FILEDESC_XUNLOCK(fdp);
880 fdgrowtable(fdp, new + 1);
882 if (fdp->fd_ofiles[new] == NULL)
885 if ((error = fdalloc(td, new, &new)) != 0) {
886 FILEDESC_XUNLOCK(fdp);
892 KASSERT(fp == fdp->fd_ofiles[old], ("old fd has been modified"));
893 KASSERT(old != new, ("new fd is same as old"));
895 delfp = fdp->fd_ofiles[new];
897 * Duplicate the source descriptor.
899 fdp->fd_ofiles[new] = fp;
900 if ((flags & DUP_CLOEXEC) != 0)
901 fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] | UF_EXCLOSE;
903 fdp->fd_ofileflags[new] = fdp->fd_ofileflags[old] & ~UF_EXCLOSE;
904 if (new > fdp->fd_lastfile)
905 fdp->fd_lastfile = new;
909 (void) closefp(fdp, new, delfp, td, 1);
910 /* closefp() drops the FILEDESC lock for us. */
912 FILEDESC_XUNLOCK(fdp);
919 * If sigio is on the list associated with a process or process group,
920 * disable signalling from the device, remove sigio from the list and
924 funsetown(struct sigio **sigiop)
934 *(sigio->sio_myref) = NULL;
935 if ((sigio)->sio_pgid < 0) {
936 struct pgrp *pg = (sigio)->sio_pgrp;
938 SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio,
942 struct proc *p = (sigio)->sio_proc;
944 SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio,
949 crfree(sigio->sio_ucred);
950 free(sigio, M_SIGIO);
954 * Free a list of sigio structures.
955 * We only need to lock the SIGIO_LOCK because we have made ourselves
956 * inaccessible to callers of fsetown and therefore do not need to lock
957 * the proc or pgrp struct for the list manipulation.
960 funsetownlst(struct sigiolst *sigiolst)
966 sigio = SLIST_FIRST(sigiolst);
973 * Every entry of the list should belong
974 * to a single proc or pgrp.
976 if (sigio->sio_pgid < 0) {
977 pg = sigio->sio_pgrp;
978 PGRP_LOCK_ASSERT(pg, MA_NOTOWNED);
979 } else /* if (sigio->sio_pgid > 0) */ {
981 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
985 while ((sigio = SLIST_FIRST(sigiolst)) != NULL) {
986 *(sigio->sio_myref) = NULL;
988 KASSERT(sigio->sio_pgid < 0,
989 ("Proc sigio in pgrp sigio list"));
990 KASSERT(sigio->sio_pgrp == pg,
991 ("Bogus pgrp in sigio list"));
993 SLIST_REMOVE(&pg->pg_sigiolst, sigio, sigio,
996 } else /* if (p != NULL) */ {
997 KASSERT(sigio->sio_pgid > 0,
998 ("Pgrp sigio in proc sigio list"));
999 KASSERT(sigio->sio_proc == p,
1000 ("Bogus proc in sigio list"));
1002 SLIST_REMOVE(&p->p_sigiolst, sigio, sigio,
1007 crfree(sigio->sio_ucred);
1008 free(sigio, M_SIGIO);
1015 * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg).
1017 * After permission checking, add a sigio structure to the sigio list for
1018 * the process or process group.
1021 fsetown(pid_t pgid, struct sigio **sigiop)
1025 struct sigio *sigio;
1035 /* Allocate and fill in the new sigio out of locks. */
1036 sigio = malloc(sizeof(struct sigio), M_SIGIO, M_WAITOK);
1037 sigio->sio_pgid = pgid;
1038 sigio->sio_ucred = crhold(curthread->td_ucred);
1039 sigio->sio_myref = sigiop;
1041 sx_slock(&proctree_lock);
1050 * Policy - Don't allow a process to FSETOWN a process
1051 * in another session.
1053 * Remove this test to allow maximum flexibility or
1054 * restrict FSETOWN to the current process or process
1055 * group for maximum safety.
1058 if (proc->p_session != curthread->td_proc->p_session) {
1064 } else /* if (pgid < 0) */ {
1065 pgrp = pgfind(-pgid);
1073 * Policy - Don't allow a process to FSETOWN a process
1074 * in another session.
1076 * Remove this test to allow maximum flexibility or
1077 * restrict FSETOWN to the current process or process
1078 * group for maximum safety.
1080 if (pgrp->pg_session != curthread->td_proc->p_session) {
1091 * Since funsetownlst() is called without the proctree
1092 * locked, we need to check for P_WEXIT.
1093 * XXX: is ESRCH correct?
1095 if ((proc->p_flag & P_WEXIT) != 0) {
1100 SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio);
1101 sigio->sio_proc = proc;
1105 SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
1106 sigio->sio_pgrp = pgrp;
1109 sx_sunlock(&proctree_lock);
1116 sx_sunlock(&proctree_lock);
1117 crfree(sigio->sio_ucred);
1118 free(sigio, M_SIGIO);
1123 * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
1127 struct sigio **sigiop;
1132 pgid = (*sigiop != NULL) ? (*sigiop)->sio_pgid : 0;
1138 * Function drops the filedesc lock on return.
1141 closefp(struct filedesc *fdp, int fd, struct file *fp, struct thread *td,
1144 struct file *fp_object;
1147 FILEDESC_XLOCK_ASSERT(fdp);
1150 if (td->td_proc->p_fdtol != NULL) {
1152 * Ask fdfree() to sleep to ensure that all relevant
1153 * process leaders can be traversed in closef().
1155 fdp->fd_holdleaderscount++;
1162 * We now hold the fp reference that used to be owned by the
1163 * descriptor array. We have to unlock the FILEDESC *AFTER*
1164 * knote_fdclose to prevent a race of the fd getting opened, a knote
1165 * added, and deleteing a knote for the new fd.
1167 knote_fdclose(td, fd);
1170 * When we're closing an fd with a capability, we need to notify
1171 * mqueue if the underlying object is of type mqueue.
1173 (void)cap_funwrap(fp, 0, &fp_object);
1174 if (fp_object->f_type == DTYPE_MQUEUE)
1175 mq_fdclose(td, fd, fp_object);
1176 FILEDESC_XUNLOCK(fdp);
1178 error = closef(fp, td);
1180 FILEDESC_XLOCK(fdp);
1181 fdp->fd_holdleaderscount--;
1182 if (fdp->fd_holdleaderscount == 0 &&
1183 fdp->fd_holdleaderswakeup != 0) {
1184 fdp->fd_holdleaderswakeup = 0;
1185 wakeup(&fdp->fd_holdleaderscount);
1187 FILEDESC_XUNLOCK(fdp);
1193 * Close a file descriptor.
1195 #ifndef _SYS_SYSPROTO_H_
1204 struct close_args *uap;
1207 return (kern_close(td, uap->fd));
1215 struct filedesc *fdp;
1218 fdp = td->td_proc->p_fd;
1220 AUDIT_SYSCLOSE(td, fd);
1222 FILEDESC_XLOCK(fdp);
1223 if ((fp = fget_locked(fdp, fd)) == NULL) {
1224 FILEDESC_XUNLOCK(fdp);
1227 fdp->fd_ofiles[fd] = NULL;
1228 fdp->fd_ofileflags[fd] = 0;
1231 /* closefp() drops the FILEDESC lock for us. */
1232 return (closefp(fdp, fd, fp, td, 1));
1236 * Close open file descriptors.
1238 #ifndef _SYS_SYSPROTO_H_
1239 struct closefrom_args {
1245 sys_closefrom(struct thread *td, struct closefrom_args *uap)
1247 struct filedesc *fdp;
1250 fdp = td->td_proc->p_fd;
1251 AUDIT_ARG_FD(uap->lowfd);
1254 * Treat negative starting file descriptor values identical to
1255 * closefrom(0) which closes all files.
1259 FILEDESC_SLOCK(fdp);
1260 for (fd = uap->lowfd; fd < fdp->fd_nfiles; fd++) {
1261 if (fdp->fd_ofiles[fd] != NULL) {
1262 FILEDESC_SUNLOCK(fdp);
1263 (void)kern_close(td, fd);
1264 FILEDESC_SLOCK(fdp);
1267 FILEDESC_SUNLOCK(fdp);
1271 #if defined(COMPAT_43)
1273 * Return status information about a file descriptor.
1275 #ifndef _SYS_SYSPROTO_H_
1276 struct ofstat_args {
1283 ofstat(struct thread *td, struct ofstat_args *uap)
1289 error = kern_fstat(td, uap->fd, &ub);
1292 error = copyout(&oub, uap->sb, sizeof(oub));
1296 #endif /* COMPAT_43 */
1299 * Return status information about a file descriptor.
1301 #ifndef _SYS_SYSPROTO_H_
1309 sys_fstat(struct thread *td, struct fstat_args *uap)
1314 error = kern_fstat(td, uap->fd, &ub);
1316 error = copyout(&ub, uap->sb, sizeof(ub));
1321 kern_fstat(struct thread *td, int fd, struct stat *sbp)
1328 if ((error = fget(td, fd, CAP_FSTAT, &fp)) != 0)
1331 AUDIT_ARG_FILE(td->td_proc, fp);
1333 error = fo_stat(fp, sbp, td->td_ucred, td);
1336 if (error == 0 && KTRPOINT(td, KTR_STRUCT))
1343 * Return status information about a file descriptor.
1345 #ifndef _SYS_SYSPROTO_H_
1346 struct nfstat_args {
1353 sys_nfstat(struct thread *td, struct nfstat_args *uap)
1359 error = kern_fstat(td, uap->fd, &ub);
1361 cvtnstat(&ub, &nub);
1362 error = copyout(&nub, uap->sb, sizeof(nub));
1368 * Return pathconf information about a file descriptor.
1370 #ifndef _SYS_SYSPROTO_H_
1371 struct fpathconf_args {
1378 sys_fpathconf(struct thread *td, struct fpathconf_args *uap)
1384 if ((error = fget(td, uap->fd, CAP_FPATHCONF, &fp)) != 0)
1387 /* If asynchronous I/O is available, it works for all descriptors. */
1388 if (uap->name == _PC_ASYNC_IO) {
1389 td->td_retval[0] = async_io_version;
1394 vn_lock(vp, LK_SHARED | LK_RETRY);
1395 error = VOP_PATHCONF(vp, uap->name, td->td_retval);
1397 } else if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) {
1398 if (uap->name != _PC_PIPE_BUF) {
1401 td->td_retval[0] = PIPE_BUF;
1413 * Grow the file table to accomodate (at least) nfd descriptors.
1416 fdgrowtable(struct filedesc *fdp, int nfd)
1418 struct filedesc0 *fdp0;
1419 struct freetable *ft;
1420 struct file **ntable;
1421 struct file **otable;
1422 char *nfileflags, *ofileflags;
1423 int nnfiles, onfiles;
1424 NDSLOTTYPE *nmap, *omap;
1426 FILEDESC_XLOCK_ASSERT(fdp);
1428 KASSERT(fdp->fd_nfiles > 0,
1429 ("zero-length file table"));
1431 /* save old values */
1432 onfiles = fdp->fd_nfiles;
1433 otable = fdp->fd_ofiles;
1434 ofileflags = fdp->fd_ofileflags;
1437 /* compute the size of the new table */
1438 nnfiles = NDSLOTS(nfd) * NDENTRIES; /* round up */
1439 if (nnfiles <= onfiles)
1440 /* the table is already large enough */
1444 * Allocate a new table and map. We need enough space for a) the
1445 * file entries themselves, b) the file flags, and c) the struct
1446 * freetable we will use when we decommission the table and place
1447 * it on the freelist. We place the struct freetable in the
1448 * middle so we don't have to worry about padding.
1450 ntable = malloc(nnfiles * sizeof(*ntable) +
1451 sizeof(struct freetable) +
1452 nnfiles * sizeof(*nfileflags),
1453 M_FILEDESC, M_ZERO | M_WAITOK);
1454 nfileflags = (char *)&ntable[nnfiles] + sizeof(struct freetable);
1455 nmap = malloc(NDSLOTS(nnfiles) * NDSLOTSIZE,
1456 M_FILEDESC, M_ZERO | M_WAITOK);
1458 /* copy the old data over and point at the new tables */
1459 memcpy(ntable, otable, onfiles * sizeof(*otable));
1460 memcpy(nfileflags, ofileflags, onfiles * sizeof(*ofileflags));
1461 memcpy(nmap, omap, NDSLOTS(onfiles) * sizeof(*omap));
1463 /* update the pointers and counters */
1464 fdp->fd_nfiles = nnfiles;
1465 fdp->fd_ofiles = ntable;
1466 fdp->fd_ofileflags = nfileflags;
1470 * Do not free the old file table, as some threads may still
1471 * reference entries within it. Instead, place it on a freelist
1472 * which will be processed when the struct filedesc is released.
1474 * Do, however, free the old map.
1476 * Note that if onfiles == NDFILE, we're dealing with the original
1477 * static allocation contained within (struct filedesc0 *)fdp,
1478 * which must not be freed.
1480 if (onfiles > NDFILE) {
1481 ft = (struct freetable *)&otable[onfiles];
1482 fdp0 = (struct filedesc0 *)fdp;
1483 ft->ft_table = otable;
1484 SLIST_INSERT_HEAD(&fdp0->fd_free, ft, ft_next);
1485 free(omap, M_FILEDESC);
1490 * Allocate a file descriptor for the process.
1493 fdalloc(struct thread *td, int minfd, int *result)
1495 struct proc *p = td->td_proc;
1496 struct filedesc *fdp = p->p_fd;
1497 int fd = -1, maxfd, allocfd;
1502 FILEDESC_XLOCK_ASSERT(fdp);
1504 if (fdp->fd_freefile > minfd)
1505 minfd = fdp->fd_freefile;
1508 maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
1512 * Search the bitmap for a free descriptor starting at minfd.
1513 * If none is found, grow the file table.
1515 fd = fd_first_free(fdp, minfd, fdp->fd_nfiles);
1518 if (fd >= fdp->fd_nfiles) {
1519 allocfd = min(fd * 2, maxfd);
1522 error = racct_set(p, RACCT_NOFILE, allocfd);
1528 * fd is already equal to first free descriptor >= minfd, so
1529 * we only need to grow the table and we are done.
1531 fdgrowtable(fdp, allocfd);
1535 * Perform some sanity checks, then mark the file descriptor as
1536 * used and return it to the caller.
1538 KASSERT(fd >= 0 && fd < min(maxfd, fdp->fd_nfiles),
1539 ("invalid descriptor %d", fd));
1540 KASSERT(!fdisused(fdp, fd),
1541 ("fd_first_free() returned non-free descriptor"));
1542 KASSERT(fdp->fd_ofiles[fd] == NULL, ("file descriptor isn't free"));
1543 KASSERT(fdp->fd_ofileflags[fd] == 0, ("file flags are set"));
1550 * Check to see whether n user file descriptors are available to the process
1554 fdavail(struct thread *td, int n)
1556 struct proc *p = td->td_proc;
1557 struct filedesc *fdp = td->td_proc->p_fd;
1560 FILEDESC_LOCK_ASSERT(fdp);
1563 * XXX: This is only called from uipc_usrreq.c:unp_externalize();
1564 * call racct_add() from there instead of dealing with containers
1568 lim = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
1570 if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
1572 last = min(fdp->fd_nfiles, lim);
1573 for (i = fdp->fd_freefile; i < last; i++) {
1574 if (fdp->fd_ofiles[i] == NULL && --n <= 0)
1581 * Create a new open file structure and allocate a file decriptor for the
1582 * process that refers to it. We add one reference to the file for the
1583 * descriptor table and one reference for resultfp. This is to prevent us
1584 * being preempted and the entry in the descriptor table closed after we
1585 * release the FILEDESC lock.
1588 falloc(struct thread *td, struct file **resultfp, int *resultfd, int flags)
1593 error = falloc_noinstall(td, &fp);
1595 return (error); /* no reference held on error */
1597 error = finstall(td, fp, &fd, flags);
1599 fdrop(fp, td); /* one reference (fp only) */
1603 if (resultfp != NULL)
1604 *resultfp = fp; /* copy out result */
1606 fdrop(fp, td); /* release local reference */
1608 if (resultfd != NULL)
1615 * Create a new open file structure without allocating a file descriptor.
1618 falloc_noinstall(struct thread *td, struct file **resultfp)
1621 int maxuserfiles = maxfiles - (maxfiles / 20);
1622 static struct timeval lastfail;
1625 KASSERT(resultfp != NULL, ("%s: resultfp == NULL", __func__));
1627 if ((openfiles >= maxuserfiles &&
1628 priv_check(td, PRIV_MAXFILES) != 0) ||
1629 openfiles >= maxfiles) {
1630 if (ppsratecheck(&lastfail, &curfail, 1)) {
1631 printf("kern.maxfiles limit exceeded by uid %i, "
1632 "please see tuning(7).\n", td->td_ucred->cr_ruid);
1636 atomic_add_int(&openfiles, 1);
1637 fp = uma_zalloc(file_zone, M_WAITOK | M_ZERO);
1638 refcount_init(&fp->f_count, 1);
1639 fp->f_cred = crhold(td->td_ucred);
1640 fp->f_ops = &badfileops;
1648 * Install a file in a file descriptor table.
1651 finstall(struct thread *td, struct file *fp, int *fd, int flags)
1653 struct filedesc *fdp = td->td_proc->p_fd;
1656 KASSERT(fd != NULL, ("%s: fd == NULL", __func__));
1657 KASSERT(fp != NULL, ("%s: fp == NULL", __func__));
1659 FILEDESC_XLOCK(fdp);
1660 if ((error = fdalloc(td, 0, fd))) {
1661 FILEDESC_XUNLOCK(fdp);
1665 fdp->fd_ofiles[*fd] = fp;
1666 if ((flags & O_CLOEXEC) != 0)
1667 fdp->fd_ofileflags[*fd] |= UF_EXCLOSE;
1668 FILEDESC_XUNLOCK(fdp);
1673 * Build a new filedesc structure from another.
1674 * Copy the current, root, and jail root vnode references.
1677 fdinit(struct filedesc *fdp)
1679 struct filedesc0 *newfdp;
1681 newfdp = malloc(sizeof *newfdp, M_FILEDESC, M_WAITOK | M_ZERO);
1682 FILEDESC_LOCK_INIT(&newfdp->fd_fd);
1684 FILEDESC_XLOCK(fdp);
1685 newfdp->fd_fd.fd_cdir = fdp->fd_cdir;
1686 if (newfdp->fd_fd.fd_cdir)
1687 VREF(newfdp->fd_fd.fd_cdir);
1688 newfdp->fd_fd.fd_rdir = fdp->fd_rdir;
1689 if (newfdp->fd_fd.fd_rdir)
1690 VREF(newfdp->fd_fd.fd_rdir);
1691 newfdp->fd_fd.fd_jdir = fdp->fd_jdir;
1692 if (newfdp->fd_fd.fd_jdir)
1693 VREF(newfdp->fd_fd.fd_jdir);
1694 FILEDESC_XUNLOCK(fdp);
1697 /* Create the file descriptor table. */
1698 newfdp->fd_fd.fd_refcnt = 1;
1699 newfdp->fd_fd.fd_holdcnt = 1;
1700 newfdp->fd_fd.fd_cmask = CMASK;
1701 newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
1702 newfdp->fd_fd.fd_ofileflags = newfdp->fd_dfileflags;
1703 newfdp->fd_fd.fd_nfiles = NDFILE;
1704 newfdp->fd_fd.fd_map = newfdp->fd_dmap;
1705 newfdp->fd_fd.fd_lastfile = -1;
1706 return (&newfdp->fd_fd);
1709 static struct filedesc *
1710 fdhold(struct proc *p)
1712 struct filedesc *fdp;
1714 mtx_lock(&fdesc_mtx);
1718 mtx_unlock(&fdesc_mtx);
1723 fddrop(struct filedesc *fdp)
1725 struct filedesc0 *fdp0;
1726 struct freetable *ft;
1729 mtx_lock(&fdesc_mtx);
1730 i = --fdp->fd_holdcnt;
1731 mtx_unlock(&fdesc_mtx);
1735 FILEDESC_LOCK_DESTROY(fdp);
1736 fdp0 = (struct filedesc0 *)fdp;
1737 while ((ft = SLIST_FIRST(&fdp0->fd_free)) != NULL) {
1738 SLIST_REMOVE_HEAD(&fdp0->fd_free, ft_next);
1739 free(ft->ft_table, M_FILEDESC);
1741 free(fdp, M_FILEDESC);
1745 * Share a filedesc structure.
1748 fdshare(struct filedesc *fdp)
1751 FILEDESC_XLOCK(fdp);
1753 FILEDESC_XUNLOCK(fdp);
1758 * Unshare a filedesc structure, if necessary by making a copy
1761 fdunshare(struct proc *p, struct thread *td)
1764 FILEDESC_XLOCK(p->p_fd);
1765 if (p->p_fd->fd_refcnt > 1) {
1766 struct filedesc *tmp;
1768 FILEDESC_XUNLOCK(p->p_fd);
1769 tmp = fdcopy(p->p_fd);
1773 FILEDESC_XUNLOCK(p->p_fd);
1777 * Copy a filedesc structure. A NULL pointer in returns a NULL reference,
1778 * this is to ease callers, not catch errors.
1781 fdcopy(struct filedesc *fdp)
1783 struct filedesc *newfdp;
1786 /* Certain daemons might not have file descriptors. */
1790 newfdp = fdinit(fdp);
1791 FILEDESC_SLOCK(fdp);
1792 while (fdp->fd_lastfile >= newfdp->fd_nfiles) {
1793 FILEDESC_SUNLOCK(fdp);
1794 FILEDESC_XLOCK(newfdp);
1795 fdgrowtable(newfdp, fdp->fd_lastfile + 1);
1796 FILEDESC_XUNLOCK(newfdp);
1797 FILEDESC_SLOCK(fdp);
1799 /* copy all passable descriptors (i.e. not kqueue) */
1800 newfdp->fd_freefile = -1;
1801 for (i = 0; i <= fdp->fd_lastfile; ++i) {
1802 if (fdisused(fdp, i) &&
1803 (fdp->fd_ofiles[i]->f_ops->fo_flags & DFLAG_PASSABLE) &&
1804 fdp->fd_ofiles[i]->f_ops != &badfileops) {
1805 newfdp->fd_ofiles[i] = fdp->fd_ofiles[i];
1806 newfdp->fd_ofileflags[i] = fdp->fd_ofileflags[i];
1807 fhold(newfdp->fd_ofiles[i]);
1808 newfdp->fd_lastfile = i;
1810 if (newfdp->fd_freefile == -1)
1811 newfdp->fd_freefile = i;
1814 newfdp->fd_cmask = fdp->fd_cmask;
1815 FILEDESC_SUNLOCK(fdp);
1816 FILEDESC_XLOCK(newfdp);
1817 for (i = 0; i <= newfdp->fd_lastfile; ++i)
1818 if (newfdp->fd_ofiles[i] != NULL)
1820 if (newfdp->fd_freefile == -1)
1821 newfdp->fd_freefile = i;
1822 FILEDESC_XUNLOCK(newfdp);
1827 * Release a filedesc structure.
1830 fdfree(struct thread *td)
1832 struct filedesc *fdp;
1834 struct filedesc_to_leader *fdtol;
1836 struct vnode *cdir, *jdir, *rdir, *vp;
1839 /* Certain daemons might not have file descriptors. */
1840 fdp = td->td_proc->p_fd;
1845 PROC_LOCK(td->td_proc);
1846 racct_set(td->td_proc, RACCT_NOFILE, 0);
1847 PROC_UNLOCK(td->td_proc);
1850 /* Check for special need to clear POSIX style locks */
1851 fdtol = td->td_proc->p_fdtol;
1852 if (fdtol != NULL) {
1853 FILEDESC_XLOCK(fdp);
1854 KASSERT(fdtol->fdl_refcount > 0,
1855 ("filedesc_to_refcount botch: fdl_refcount=%d",
1856 fdtol->fdl_refcount));
1857 if (fdtol->fdl_refcount == 1 &&
1858 (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1859 for (i = 0; i <= fdp->fd_lastfile; i++) {
1860 fp = fdp->fd_ofiles[i];
1861 if (fp == NULL || fp->f_type != DTYPE_VNODE)
1864 FILEDESC_XUNLOCK(fdp);
1865 lf.l_whence = SEEK_SET;
1868 lf.l_type = F_UNLCK;
1870 (void) VOP_ADVLOCK(vp,
1871 (caddr_t)td->td_proc->p_leader, F_UNLCK,
1873 FILEDESC_XLOCK(fdp);
1878 if (fdtol->fdl_refcount == 1) {
1879 if (fdp->fd_holdleaderscount > 0 &&
1880 (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1882 * close() or do_dup() has cleared a reference
1883 * in a shared file descriptor table.
1885 fdp->fd_holdleaderswakeup = 1;
1886 sx_sleep(&fdp->fd_holdleaderscount,
1887 FILEDESC_LOCK(fdp), PLOCK, "fdlhold", 0);
1890 if (fdtol->fdl_holdcount > 0) {
1892 * Ensure that fdtol->fdl_leader remains
1893 * valid in closef().
1895 fdtol->fdl_wakeup = 1;
1896 sx_sleep(fdtol, FILEDESC_LOCK(fdp), PLOCK,
1901 fdtol->fdl_refcount--;
1902 if (fdtol->fdl_refcount == 0 &&
1903 fdtol->fdl_holdcount == 0) {
1904 fdtol->fdl_next->fdl_prev = fdtol->fdl_prev;
1905 fdtol->fdl_prev->fdl_next = fdtol->fdl_next;
1908 td->td_proc->p_fdtol = NULL;
1909 FILEDESC_XUNLOCK(fdp);
1911 free(fdtol, M_FILEDESC_TO_LEADER);
1913 FILEDESC_XLOCK(fdp);
1914 i = --fdp->fd_refcnt;
1915 FILEDESC_XUNLOCK(fdp);
1919 for (i = 0; i <= fdp->fd_lastfile; i++) {
1920 fp = fdp->fd_ofiles[i];
1922 FILEDESC_XLOCK(fdp);
1923 fdp->fd_ofiles[i] = NULL;
1924 FILEDESC_XUNLOCK(fdp);
1925 (void) closef(fp, td);
1928 FILEDESC_XLOCK(fdp);
1930 /* XXX This should happen earlier. */
1931 mtx_lock(&fdesc_mtx);
1932 td->td_proc->p_fd = NULL;
1933 mtx_unlock(&fdesc_mtx);
1935 if (fdp->fd_nfiles > NDFILE)
1936 free(fdp->fd_ofiles, M_FILEDESC);
1937 if (NDSLOTS(fdp->fd_nfiles) > NDSLOTS(NDFILE))
1938 free(fdp->fd_map, M_FILEDESC);
1942 cdir = fdp->fd_cdir;
1943 fdp->fd_cdir = NULL;
1944 rdir = fdp->fd_rdir;
1945 fdp->fd_rdir = NULL;
1946 jdir = fdp->fd_jdir;
1947 fdp->fd_jdir = NULL;
1948 FILEDESC_XUNLOCK(fdp);
1961 * For setugid programs, we don't want to people to use that setugidness
1962 * to generate error messages which write to a file which otherwise would
1963 * otherwise be off-limits to the process. We check for filesystems where
1964 * the vnode can change out from under us after execve (like [lin]procfs).
1966 * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
1967 * sufficient. We also don't check for setugidness since we know we are.
1970 is_unsafe(struct file *fp)
1972 if (fp->f_type == DTYPE_VNODE) {
1973 struct vnode *vp = fp->f_vnode;
1975 if ((vp->v_vflag & VV_PROCDEP) != 0)
1982 * Make this setguid thing safe, if at all possible.
1985 setugidsafety(struct thread *td)
1987 struct filedesc *fdp;
1990 /* Certain daemons might not have file descriptors. */
1991 fdp = td->td_proc->p_fd;
1996 * Note: fdp->fd_ofiles may be reallocated out from under us while
1997 * we are blocked in a close. Be careful!
1999 FILEDESC_XLOCK(fdp);
2000 for (i = 0; i <= fdp->fd_lastfile; i++) {
2003 if (fdp->fd_ofiles[i] && is_unsafe(fdp->fd_ofiles[i])) {
2006 knote_fdclose(td, i);
2008 * NULL-out descriptor prior to close to avoid
2009 * a race while close blocks.
2011 fp = fdp->fd_ofiles[i];
2012 fdp->fd_ofiles[i] = NULL;
2013 fdp->fd_ofileflags[i] = 0;
2015 FILEDESC_XUNLOCK(fdp);
2016 (void) closef(fp, td);
2017 FILEDESC_XLOCK(fdp);
2020 FILEDESC_XUNLOCK(fdp);
2024 * If a specific file object occupies a specific file descriptor, close the
2025 * file descriptor entry and drop a reference on the file object. This is a
2026 * convenience function to handle a subsequent error in a function that calls
2027 * falloc() that handles the race that another thread might have closed the
2028 * file descriptor out from under the thread creating the file object.
2031 fdclose(struct filedesc *fdp, struct file *fp, int idx, struct thread *td)
2034 FILEDESC_XLOCK(fdp);
2035 if (fdp->fd_ofiles[idx] == fp) {
2036 fdp->fd_ofiles[idx] = NULL;
2038 FILEDESC_XUNLOCK(fdp);
2041 FILEDESC_XUNLOCK(fdp);
2045 * Close any files on exec?
2048 fdcloseexec(struct thread *td)
2050 struct filedesc *fdp;
2054 /* Certain daemons might not have file descriptors. */
2055 fdp = td->td_proc->p_fd;
2060 * We cannot cache fd_ofiles or fd_ofileflags since operations
2061 * may block and rip them out from under us.
2063 FILEDESC_XLOCK(fdp);
2064 for (i = 0; i <= fdp->fd_lastfile; i++) {
2065 fp = fdp->fd_ofiles[i];
2066 if (fp != NULL && (fp->f_type == DTYPE_MQUEUE ||
2067 (fdp->fd_ofileflags[i] & UF_EXCLOSE))) {
2068 fdp->fd_ofiles[i] = NULL;
2069 fdp->fd_ofileflags[i] = 0;
2071 (void) closefp(fdp, i, fp, td, 0);
2072 /* closefp() drops the FILEDESC lock. */
2073 FILEDESC_XLOCK(fdp);
2076 FILEDESC_XUNLOCK(fdp);
2080 * It is unsafe for set[ug]id processes to be started with file
2081 * descriptors 0..2 closed, as these descriptors are given implicit
2082 * significance in the Standard C library. fdcheckstd() will create a
2083 * descriptor referencing /dev/null for each of stdin, stdout, and
2084 * stderr that is not already open.
2087 fdcheckstd(struct thread *td)
2089 struct filedesc *fdp;
2090 register_t retval, save;
2091 int i, error, devnull;
2093 fdp = td->td_proc->p_fd;
2096 KASSERT(fdp->fd_refcnt == 1, ("the fdtable should not be shared"));
2099 for (i = 0; i < 3; i++) {
2100 if (fdp->fd_ofiles[i] != NULL)
2103 save = td->td_retval[0];
2104 error = kern_open(td, "/dev/null", UIO_SYSSPACE,
2106 devnull = td->td_retval[0];
2107 td->td_retval[0] = save;
2110 KASSERT(devnull == i, ("oof, we didn't get our fd"));
2112 error = do_dup(td, DUP_FIXED, devnull, i, &retval);
2121 * Internal form of close. Decrement reference count on file structure.
2122 * Note: td may be NULL when closing a file that was being passed in a
2125 * XXXRW: Giant is not required for the caller, but often will be held; this
2126 * makes it moderately likely the Giant will be recursed in the VFS case.
2129 closef(struct file *fp, struct thread *td)
2133 struct filedesc_to_leader *fdtol;
2134 struct filedesc *fdp;
2135 struct file *fp_object;
2138 * POSIX record locking dictates that any close releases ALL
2139 * locks owned by this process. This is handled by setting
2140 * a flag in the unlock to free ONLY locks obeying POSIX
2141 * semantics, and not to free BSD-style file locks.
2142 * If the descriptor was in a message, POSIX-style locks
2143 * aren't passed with the descriptor, and the thread pointer
2144 * will be NULL. Callers should be careful only to pass a
2145 * NULL thread pointer when there really is no owning
2146 * context that might have locks, or the locks will be
2149 * If this is a capability, we do lock processing under the underlying
2150 * node, not the capability itself.
2152 (void)cap_funwrap(fp, 0, &fp_object);
2153 if (fp_object->f_type == DTYPE_VNODE && td != NULL) {
2154 vp = fp_object->f_vnode;
2155 if ((td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
2156 lf.l_whence = SEEK_SET;
2159 lf.l_type = F_UNLCK;
2160 (void) VOP_ADVLOCK(vp, (caddr_t)td->td_proc->p_leader,
2161 F_UNLCK, &lf, F_POSIX);
2163 fdtol = td->td_proc->p_fdtol;
2164 if (fdtol != NULL) {
2166 * Handle special case where file descriptor table is
2167 * shared between multiple process leaders.
2169 fdp = td->td_proc->p_fd;
2170 FILEDESC_XLOCK(fdp);
2171 for (fdtol = fdtol->fdl_next;
2172 fdtol != td->td_proc->p_fdtol;
2173 fdtol = fdtol->fdl_next) {
2174 if ((fdtol->fdl_leader->p_flag &
2177 fdtol->fdl_holdcount++;
2178 FILEDESC_XUNLOCK(fdp);
2179 lf.l_whence = SEEK_SET;
2182 lf.l_type = F_UNLCK;
2183 vp = fp_object->f_vnode;
2184 (void) VOP_ADVLOCK(vp,
2185 (caddr_t)fdtol->fdl_leader, F_UNLCK, &lf,
2187 FILEDESC_XLOCK(fdp);
2188 fdtol->fdl_holdcount--;
2189 if (fdtol->fdl_holdcount == 0 &&
2190 fdtol->fdl_wakeup != 0) {
2191 fdtol->fdl_wakeup = 0;
2195 FILEDESC_XUNLOCK(fdp);
2198 return (fdrop(fp, td));
2202 * Initialize the file pointer with the specified properties.
2204 * The ops are set with release semantics to be certain that the flags, type,
2205 * and data are visible when ops is. This is to prevent ops methods from being
2206 * called with bad data.
2209 finit(struct file *fp, u_int flag, short type, void *data, struct fileops *ops)
2214 atomic_store_rel_ptr((volatile uintptr_t *)&fp->f_ops, (uintptr_t)ops);
2218 fget_unlocked(struct filedesc *fdp, int fd)
2223 if (fd < 0 || fd >= fdp->fd_nfiles)
2226 * Fetch the descriptor locklessly. We avoid fdrop() races by
2227 * never raising a refcount above 0. To accomplish this we have
2228 * to use a cmpset loop rather than an atomic_add. The descriptor
2229 * must be re-verified once we acquire a reference to be certain
2230 * that the identity is still correct and we did not lose a race
2231 * due to preemption.
2234 fp = fdp->fd_ofiles[fd];
2237 count = fp->f_count;
2241 * Use an acquire barrier to prevent caching of fd_ofiles
2242 * so it is refreshed for verification.
2244 if (atomic_cmpset_acq_int(&fp->f_count, count, count + 1) != 1)
2246 if (fp == fdp->fd_ofiles[fd])
2248 fdrop(fp, curthread);
2255 * Extract the file pointer associated with the specified descriptor for the
2256 * current user process.
2258 * If the descriptor doesn't exist or doesn't match 'flags', EBADF is
2261 * If the FGET_GETCAP flag is set, the capability itself will be returned.
2262 * Calling _fget() with FGET_GETCAP on a non-capability will return EINVAL.
2263 * Otherwise, if the file is a capability, its rights will be checked against
2264 * the capability rights mask, and if successful, the object will be unwrapped.
2266 * If an error occured the non-zero error is returned and *fpp is set to
2267 * NULL. Otherwise *fpp is held and set and zero is returned. Caller is
2268 * responsible for fdrop().
2270 #define FGET_GETCAP 0x00000001
2272 _fget(struct thread *td, int fd, struct file **fpp, int flags,
2273 cap_rights_t needrights, cap_rights_t *haverightsp, u_char *maxprotp,
2276 struct filedesc *fdp;
2279 struct file *fp_fromcap;
2284 if (td == NULL || (fdp = td->td_proc->p_fd) == NULL)
2286 if ((fp = fget_unlocked(fdp, fd)) == NULL)
2288 if (fp->f_ops == &badfileops) {
2295 * If this is a capability, what rights does it have?
2297 if (haverightsp != NULL) {
2298 if (fp->f_type == DTYPE_CAPABILITY)
2299 *haverightsp = cap_rights(fp);
2301 *haverightsp = CAP_MASK_VALID;
2305 * If a capability has been requested, return the capability directly.
2306 * Otherwise, check capability rights, extract the underlying object,
2307 * and check its access flags.
2309 if (fget_flags & FGET_GETCAP) {
2310 if (fp->f_type != DTYPE_CAPABILITY) {
2315 if (maxprotp == NULL)
2316 error = cap_funwrap(fp, needrights, &fp_fromcap);
2318 error = cap_funwrap_mmap(fp, needrights, maxprotp,
2326 * If we've unwrapped a file, drop the original capability
2327 * and hold the new descriptor. fp after this point refers to
2328 * the actual (unwrapped) object, not the capability.
2330 if (fp != fp_fromcap) {
2336 #else /* !CAPABILITIES */
2337 KASSERT(fp->f_type != DTYPE_CAPABILITY,
2338 ("%s: saw capability", __func__));
2339 if (maxprotp != NULL)
2340 *maxprotp = VM_PROT_ALL;
2341 #endif /* CAPABILITIES */
2344 * FREAD and FWRITE failure return EBADF as per POSIX.
2350 if ((fp->f_flag & flags) == 0)
2354 if ((fp->f_flag & (FREAD | FEXEC)) == 0 ||
2355 ((fp->f_flag & FWRITE) != 0))
2361 KASSERT(0, ("wrong flags"));
2374 fget(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
2377 return(_fget(td, fd, fpp, 0, rights, NULL, NULL, 0));
2381 fget_mmap(struct thread *td, int fd, cap_rights_t rights, u_char *maxprotp,
2385 return (_fget(td, fd, fpp, 0, rights, NULL, maxprotp, 0));
2389 fget_read(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
2392 return(_fget(td, fd, fpp, FREAD, rights, NULL, NULL, 0));
2396 fget_write(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
2399 return (_fget(td, fd, fpp, FWRITE, rights, NULL, NULL, 0));
2403 * Unlike the other fget() calls, which accept and check capability rights
2404 * but never return capabilities, fgetcap() returns the capability but doesn't
2405 * check capability rights.
2408 fgetcap(struct thread *td, int fd, struct file **fpp)
2411 return (_fget(td, fd, fpp, 0, 0, NULL, NULL, FGET_GETCAP));
2416 * Like fget() but loads the underlying vnode, or returns an error if the
2417 * descriptor does not represent a vnode. Note that pipes use vnodes but
2418 * never have VM objects. The returned vnode will be vref()'d.
2420 * XXX: what about the unused flags ?
2423 _fgetvp(struct thread *td, int fd, int flags, cap_rights_t needrights,
2424 cap_rights_t *haverightsp, struct vnode **vpp)
2430 if ((error = _fget(td, fd, &fp, flags, needrights, haverightsp,
2433 if (fp->f_vnode == NULL) {
2445 fgetvp(struct thread *td, int fd, cap_rights_t rights, struct vnode **vpp)
2448 return (_fgetvp(td, fd, 0, rights, NULL, vpp));
2452 fgetvp_rights(struct thread *td, int fd, cap_rights_t need, cap_rights_t *have,
2455 return (_fgetvp(td, fd, 0, need, have, vpp));
2459 fgetvp_read(struct thread *td, int fd, cap_rights_t rights, struct vnode **vpp)
2462 return (_fgetvp(td, fd, FREAD, rights, NULL, vpp));
2466 fgetvp_exec(struct thread *td, int fd, cap_rights_t rights, struct vnode **vpp)
2469 return (_fgetvp(td, fd, FEXEC, rights, NULL, vpp));
2474 fgetvp_write(struct thread *td, int fd, cap_rights_t rights,
2478 return (_fgetvp(td, fd, FWRITE, rights, NULL, vpp));
2483 * Like fget() but loads the underlying socket, or returns an error if the
2484 * descriptor does not represent a socket.
2486 * We bump the ref count on the returned socket. XXX Also obtain the SX lock
2489 * Note: fgetsock() and fputsock() are deprecated, as consumers should rely
2490 * on their file descriptor reference to prevent the socket from being free'd
2494 fgetsock(struct thread *td, int fd, cap_rights_t rights, struct socket **spp,
2503 if ((error = _fget(td, fd, &fp, 0, rights, NULL, NULL, 0)) != 0)
2505 if (fp->f_type != DTYPE_SOCKET) {
2510 *fflagp = fp->f_flag;
2521 * Drop the reference count on the socket and XXX release the SX lock in the
2522 * future. The last reference closes the socket.
2524 * Note: fputsock() is deprecated, see comment for fgetsock().
2527 fputsock(struct socket *so)
2532 CURVNET_SET(so->so_vnet);
2538 * Handle the last reference to a file being closed.
2540 * No special capability handling here, as the capability's fo_close will run
2541 * instead of the object here, and perform any necessary drop on the object.
2544 _fdrop(struct file *fp, struct thread *td)
2549 if (fp->f_count != 0)
2550 panic("fdrop: count %d", fp->f_count);
2551 if (fp->f_ops != &badfileops)
2552 error = fo_close(fp, td);
2553 atomic_subtract_int(&openfiles, 1);
2555 free(fp->f_advice, M_FADVISE);
2556 uma_zfree(file_zone, fp);
2562 * Apply an advisory lock on a file descriptor.
2564 * Just attempt to get a record lock of the requested type on the entire file
2565 * (l_whence = SEEK_SET, l_start = 0, l_len = 0).
2567 #ifndef _SYS_SYSPROTO_H_
2575 sys_flock(struct thread *td, struct flock_args *uap)
2582 if ((error = fget(td, uap->fd, CAP_FLOCK, &fp)) != 0)
2584 if (fp->f_type != DTYPE_VNODE) {
2586 return (EOPNOTSUPP);
2590 lf.l_whence = SEEK_SET;
2593 if (uap->how & LOCK_UN) {
2594 lf.l_type = F_UNLCK;
2595 atomic_clear_int(&fp->f_flag, FHASLOCK);
2596 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
2599 if (uap->how & LOCK_EX)
2600 lf.l_type = F_WRLCK;
2601 else if (uap->how & LOCK_SH)
2602 lf.l_type = F_RDLCK;
2607 atomic_set_int(&fp->f_flag, FHASLOCK);
2608 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
2609 (uap->how & LOCK_NB) ? F_FLOCK : F_FLOCK | F_WAIT);
2615 * Duplicate the specified descriptor to a free descriptor.
2618 dupfdopen(struct thread *td, struct filedesc *fdp, int dfd, int mode, int openerror, int *indxp)
2623 KASSERT(openerror == ENODEV || openerror == ENXIO,
2624 ("unexpected error %d in %s", openerror, __func__));
2627 * If the to-be-dup'd fd number is greater than the allowed number
2628 * of file descriptors, or the fd to be dup'd has already been
2629 * closed, then reject.
2631 FILEDESC_XLOCK(fdp);
2632 if ((fp = fget_locked(fdp, dfd)) == NULL) {
2633 FILEDESC_XUNLOCK(fdp);
2637 error = fdalloc(td, 0, &indx);
2639 FILEDESC_XUNLOCK(fdp);
2644 * There are two cases of interest here.
2646 * For ENODEV simply dup (dfd) to file descriptor (indx) and return.
2648 * For ENXIO steal away the file structure from (dfd) and store it in
2649 * (indx). (dfd) is effectively closed by this operation.
2651 switch (openerror) {
2654 * Check that the mode the file is being opened for is a
2655 * subset of the mode of the existing descriptor.
2657 if (((mode & (FREAD|FWRITE)) | fp->f_flag) != fp->f_flag) {
2658 fdunused(fdp, indx);
2659 FILEDESC_XUNLOCK(fdp);
2662 fdp->fd_ofiles[indx] = fp;
2663 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
2668 * Steal away the file pointer from dfd and stuff it into indx.
2670 fdp->fd_ofiles[indx] = fp;
2671 fdp->fd_ofiles[dfd] = NULL;
2672 fdp->fd_ofileflags[indx] = fdp->fd_ofileflags[dfd];
2673 fdp->fd_ofileflags[dfd] = 0;
2677 FILEDESC_XUNLOCK(fdp);
2683 * Scan all active processes and prisons to see if any of them have a current
2684 * or root directory of `olddp'. If so, replace them with the new mount point.
2687 mountcheckdirs(struct vnode *olddp, struct vnode *newdp)
2689 struct filedesc *fdp;
2694 if (vrefcnt(olddp) == 1)
2697 sx_slock(&allproc_lock);
2698 FOREACH_PROC_IN_SYSTEM(p) {
2702 FILEDESC_XLOCK(fdp);
2703 if (fdp->fd_cdir == olddp) {
2705 fdp->fd_cdir = newdp;
2708 if (fdp->fd_rdir == olddp) {
2710 fdp->fd_rdir = newdp;
2713 if (fdp->fd_jdir == olddp) {
2715 fdp->fd_jdir = newdp;
2718 FILEDESC_XUNLOCK(fdp);
2721 sx_sunlock(&allproc_lock);
2722 if (rootvnode == olddp) {
2727 mtx_lock(&prison0.pr_mtx);
2728 if (prison0.pr_root == olddp) {
2730 prison0.pr_root = newdp;
2733 mtx_unlock(&prison0.pr_mtx);
2734 sx_slock(&allprison_lock);
2735 TAILQ_FOREACH(pr, &allprison, pr_list) {
2736 mtx_lock(&pr->pr_mtx);
2737 if (pr->pr_root == olddp) {
2739 pr->pr_root = newdp;
2742 mtx_unlock(&pr->pr_mtx);
2744 sx_sunlock(&allprison_lock);
2749 struct filedesc_to_leader *
2750 filedesc_to_leader_alloc(struct filedesc_to_leader *old, struct filedesc *fdp, struct proc *leader)
2752 struct filedesc_to_leader *fdtol;
2754 fdtol = malloc(sizeof(struct filedesc_to_leader),
2755 M_FILEDESC_TO_LEADER,
2757 fdtol->fdl_refcount = 1;
2758 fdtol->fdl_holdcount = 0;
2759 fdtol->fdl_wakeup = 0;
2760 fdtol->fdl_leader = leader;
2762 FILEDESC_XLOCK(fdp);
2763 fdtol->fdl_next = old->fdl_next;
2764 fdtol->fdl_prev = old;
2765 old->fdl_next = fdtol;
2766 fdtol->fdl_next->fdl_prev = fdtol;
2767 FILEDESC_XUNLOCK(fdp);
2769 fdtol->fdl_next = fdtol;
2770 fdtol->fdl_prev = fdtol;
2776 * Get file structures globally.
2779 sysctl_kern_file(SYSCTL_HANDLER_ARGS)
2782 struct filedesc *fdp;
2787 error = sysctl_wire_old_buffer(req, 0);
2790 if (req->oldptr == NULL) {
2792 sx_slock(&allproc_lock);
2793 FOREACH_PROC_IN_SYSTEM(p) {
2794 if (p->p_state == PRS_NEW)
2799 /* overestimates sparse tables. */
2800 if (fdp->fd_lastfile > 0)
2801 n += fdp->fd_lastfile;
2804 sx_sunlock(&allproc_lock);
2805 return (SYSCTL_OUT(req, 0, n * sizeof(xf)));
2808 bzero(&xf, sizeof(xf));
2809 xf.xf_size = sizeof(xf);
2810 sx_slock(&allproc_lock);
2811 FOREACH_PROC_IN_SYSTEM(p) {
2813 if (p->p_state == PRS_NEW) {
2817 if (p_cansee(req->td, p) != 0) {
2821 xf.xf_pid = p->p_pid;
2822 xf.xf_uid = p->p_ucred->cr_uid;
2827 FILEDESC_SLOCK(fdp);
2828 for (n = 0; fdp->fd_refcnt > 0 && n < fdp->fd_nfiles; ++n) {
2829 if ((fp = fdp->fd_ofiles[n]) == NULL)
2833 xf.xf_data = fp->f_data;
2834 xf.xf_vnode = fp->f_vnode;
2835 xf.xf_type = fp->f_type;
2836 xf.xf_count = fp->f_count;
2838 xf.xf_offset = foffset_get(fp);
2839 xf.xf_flag = fp->f_flag;
2840 error = SYSCTL_OUT(req, &xf, sizeof(xf));
2844 FILEDESC_SUNLOCK(fdp);
2849 sx_sunlock(&allproc_lock);
2853 SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
2854 0, 0, sysctl_kern_file, "S,xfile", "Entire file table");
2856 #ifdef KINFO_OFILE_SIZE
2857 CTASSERT(sizeof(struct kinfo_ofile) == KINFO_OFILE_SIZE);
2860 #ifdef COMPAT_FREEBSD7
2862 export_vnode_for_osysctl(struct vnode *vp, int type,
2863 struct kinfo_ofile *kif, struct filedesc *fdp, struct sysctl_req *req)
2866 char *fullpath, *freepath;
2868 bzero(kif, sizeof(*kif));
2869 kif->kf_structsize = sizeof(*kif);
2873 kif->kf_type = KF_TYPE_VNODE;
2874 /* This function only handles directories. */
2875 if (vp->v_type != VDIR) {
2879 kif->kf_vnode_type = KF_VTYPE_VDIR;
2882 * This is not a true file descriptor, so we set a bogus refcount
2883 * and offset to indicate these fields should be ignored.
2885 kif->kf_ref_count = -1;
2886 kif->kf_offset = -1;
2890 FILEDESC_SUNLOCK(fdp);
2891 vn_fullpath(curthread, vp, &fullpath, &freepath);
2893 strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path));
2894 if (freepath != NULL)
2895 free(freepath, M_TEMP);
2896 error = SYSCTL_OUT(req, kif, sizeof(*kif));
2897 FILEDESC_SLOCK(fdp);
2902 * Get per-process file descriptors for use by procstat(1), et al.
2905 sysctl_kern_proc_ofiledesc(SYSCTL_HANDLER_ARGS)
2907 char *fullpath, *freepath;
2908 struct kinfo_ofile *kif;
2909 struct filedesc *fdp;
2910 int error, i, *name;
2911 struct shmfd *shmfd;
2919 if ((p = pfind((pid_t)name[0])) == NULL)
2921 if ((error = p_candebug(curthread, p))) {
2929 kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK);
2930 FILEDESC_SLOCK(fdp);
2931 if (fdp->fd_cdir != NULL)
2932 export_vnode_for_osysctl(fdp->fd_cdir, KF_FD_TYPE_CWD, kif,
2934 if (fdp->fd_rdir != NULL)
2935 export_vnode_for_osysctl(fdp->fd_rdir, KF_FD_TYPE_ROOT, kif,
2937 if (fdp->fd_jdir != NULL)
2938 export_vnode_for_osysctl(fdp->fd_jdir, KF_FD_TYPE_JAIL, kif,
2940 for (i = 0; i < fdp->fd_nfiles; i++) {
2941 if ((fp = fdp->fd_ofiles[i]) == NULL)
2943 bzero(kif, sizeof(*kif));
2944 kif->kf_structsize = sizeof(*kif);
2953 * When reporting a capability, most fields will be from the
2954 * underlying object, but do mark as a capability. With
2955 * ofiledesc, we don't have a field to export the cap_rights_t,
2956 * but we do with the new filedesc.
2958 if (fp->f_type == DTYPE_CAPABILITY) {
2959 kif->kf_flags |= KF_FLAG_CAPABILITY;
2960 (void)cap_funwrap(fp, 0, &fp);
2963 KASSERT(fp->f_type != DTYPE_CAPABILITY,
2964 ("sysctl_kern_proc_ofiledesc: saw capability"));
2966 switch (fp->f_type) {
2968 kif->kf_type = KF_TYPE_VNODE;
2973 kif->kf_type = KF_TYPE_SOCKET;
2978 kif->kf_type = KF_TYPE_PIPE;
2982 kif->kf_type = KF_TYPE_FIFO;
2987 kif->kf_type = KF_TYPE_KQUEUE;
2991 kif->kf_type = KF_TYPE_CRYPTO;
2995 kif->kf_type = KF_TYPE_MQUEUE;
2999 kif->kf_type = KF_TYPE_SHM;
3004 kif->kf_type = KF_TYPE_SEM;
3008 kif->kf_type = KF_TYPE_PTS;
3013 case DTYPE_PROCDESC:
3014 kif->kf_type = KF_TYPE_PROCDESC;
3019 kif->kf_type = KF_TYPE_UNKNOWN;
3022 kif->kf_ref_count = fp->f_count;
3023 if (fp->f_flag & FREAD)
3024 kif->kf_flags |= KF_FLAG_READ;
3025 if (fp->f_flag & FWRITE)
3026 kif->kf_flags |= KF_FLAG_WRITE;
3027 if (fp->f_flag & FAPPEND)
3028 kif->kf_flags |= KF_FLAG_APPEND;
3029 if (fp->f_flag & FASYNC)
3030 kif->kf_flags |= KF_FLAG_ASYNC;
3031 if (fp->f_flag & FFSYNC)
3032 kif->kf_flags |= KF_FLAG_FSYNC;
3033 if (fp->f_flag & FNONBLOCK)
3034 kif->kf_flags |= KF_FLAG_NONBLOCK;
3035 if (fp->f_flag & O_DIRECT)
3036 kif->kf_flags |= KF_FLAG_DIRECT;
3037 if (fp->f_flag & FHASLOCK)
3038 kif->kf_flags |= KF_FLAG_HASLOCK;
3039 kif->kf_offset = foffset_get(fp);
3042 switch (vp->v_type) {
3044 kif->kf_vnode_type = KF_VTYPE_VNON;
3047 kif->kf_vnode_type = KF_VTYPE_VREG;
3050 kif->kf_vnode_type = KF_VTYPE_VDIR;
3053 kif->kf_vnode_type = KF_VTYPE_VBLK;
3056 kif->kf_vnode_type = KF_VTYPE_VCHR;
3059 kif->kf_vnode_type = KF_VTYPE_VLNK;
3062 kif->kf_vnode_type = KF_VTYPE_VSOCK;
3065 kif->kf_vnode_type = KF_VTYPE_VFIFO;
3068 kif->kf_vnode_type = KF_VTYPE_VBAD;
3071 kif->kf_vnode_type = KF_VTYPE_UNKNOWN;
3075 * It is OK to drop the filedesc lock here as we will
3076 * re-validate and re-evaluate its properties when
3077 * the loop continues.
3081 FILEDESC_SUNLOCK(fdp);
3082 vn_fullpath(curthread, vp, &fullpath, &freepath);
3084 strlcpy(kif->kf_path, fullpath,
3085 sizeof(kif->kf_path));
3086 if (freepath != NULL)
3087 free(freepath, M_TEMP);
3088 FILEDESC_SLOCK(fdp);
3091 struct sockaddr *sa;
3093 if (so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa)
3094 == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) {
3095 bcopy(sa, &kif->kf_sa_local, sa->sa_len);
3098 if (so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa)
3099 == 0 && sa->sa_len <= sizeof(kif->kf_sa_peer)) {
3100 bcopy(sa, &kif->kf_sa_peer, sa->sa_len);
3103 kif->kf_sock_domain =
3104 so->so_proto->pr_domain->dom_family;
3105 kif->kf_sock_type = so->so_type;
3106 kif->kf_sock_protocol = so->so_proto->pr_protocol;
3109 strlcpy(kif->kf_path, tty_devname(tp),
3110 sizeof(kif->kf_path));
3113 shm_path(shmfd, kif->kf_path, sizeof(kif->kf_path));
3114 error = SYSCTL_OUT(req, kif, sizeof(*kif));
3118 FILEDESC_SUNLOCK(fdp);
3124 static SYSCTL_NODE(_kern_proc, KERN_PROC_OFILEDESC, ofiledesc, CTLFLAG_RD,
3125 sysctl_kern_proc_ofiledesc, "Process ofiledesc entries");
3126 #endif /* COMPAT_FREEBSD7 */
3128 #ifdef KINFO_FILE_SIZE
3129 CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE);
3133 export_fd_for_sysctl(void *data, int type, int fd, int fflags, int refcnt,
3134 int64_t offset, int fd_is_cap, cap_rights_t fd_cap_rights,
3135 struct kinfo_file *kif, struct sysctl_req *req)
3140 } fflags_table[] = {
3141 { FAPPEND, KF_FLAG_APPEND },
3142 { FASYNC, KF_FLAG_ASYNC },
3143 { FFSYNC, KF_FLAG_FSYNC },
3144 { FHASLOCK, KF_FLAG_HASLOCK },
3145 { FNONBLOCK, KF_FLAG_NONBLOCK },
3146 { FREAD, KF_FLAG_READ },
3147 { FWRITE, KF_FLAG_WRITE },
3148 { O_CREAT, KF_FLAG_CREAT },
3149 { O_DIRECT, KF_FLAG_DIRECT },
3150 { O_EXCL, KF_FLAG_EXCL },
3151 { O_EXEC, KF_FLAG_EXEC },
3152 { O_EXLOCK, KF_FLAG_EXLOCK },
3153 { O_NOFOLLOW, KF_FLAG_NOFOLLOW },
3154 { O_SHLOCK, KF_FLAG_SHLOCK },
3155 { O_TRUNC, KF_FLAG_TRUNC }
3157 #define NFFLAGS (sizeof(fflags_table) / sizeof(*fflags_table))
3162 bzero(kif, sizeof(*kif));
3166 vp = (struct vnode *)data;
3167 error = fill_vnode_info(vp, kif);
3170 case KF_TYPE_SOCKET:
3171 error = fill_socket_info((struct socket *)data, kif);
3174 error = fill_pipe_info((struct pipe *)data, kif);
3177 error = fill_pts_info((struct tty *)data, kif);
3179 case KF_TYPE_PROCDESC:
3180 error = fill_procdesc_info((struct procdesc *)data, kif);
3183 error = fill_shm_info((struct file *)data, kif);
3189 kif->kf_status |= KF_ATTR_VALID;
3192 * Translate file access flags.
3194 for (i = 0; i < NFFLAGS; i++)
3195 if (fflags & fflags_table[i].fflag)
3196 kif->kf_flags |= fflags_table[i].kf_fflag;
3198 kif->kf_flags |= KF_FLAG_CAPABILITY;
3200 kif->kf_cap_rights = fd_cap_rights;
3202 kif->kf_type = type;
3203 kif->kf_ref_count = refcnt;
3204 kif->kf_offset = offset;
3205 /* Pack record size down */
3206 kif->kf_structsize = offsetof(struct kinfo_file, kf_path) +
3207 strlen(kif->kf_path) + 1;
3208 kif->kf_structsize = roundup(kif->kf_structsize, sizeof(uint64_t));
3209 error = SYSCTL_OUT(req, kif, kif->kf_structsize);
3214 * Get per-process file descriptors for use by procstat(1), et al.
3217 sysctl_kern_proc_filedesc(SYSCTL_HANDLER_ARGS)
3220 struct filedesc *fdp;
3221 struct kinfo_file *kif;
3223 struct vnode *cttyvp, *textvp, *tracevp;
3227 int error, i, *name;
3228 int fd_is_cap, type, refcnt, fflags;
3229 cap_rights_t fd_cap_rights;
3232 if ((p = pfind((pid_t)name[0])) == NULL)
3234 if ((error = p_candebug(curthread, p))) {
3239 tracevp = p->p_tracevp;
3240 if (tracevp != NULL)
3243 textvp = p->p_textvp;
3246 /* Controlling tty. */
3248 if (p->p_pgrp != NULL && p->p_pgrp->pg_session != NULL) {
3249 cttyvp = p->p_pgrp->pg_session->s_ttyvp;
3255 kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK);
3256 if (tracevp != NULL)
3257 export_fd_for_sysctl(tracevp, KF_TYPE_VNODE, KF_FD_TYPE_TRACE,
3258 FREAD | FWRITE, -1, -1, 0, 0, kif, req);
3260 export_fd_for_sysctl(textvp, KF_TYPE_VNODE, KF_FD_TYPE_TEXT,
3261 FREAD, -1, -1, 0, 0, kif, req);
3263 export_fd_for_sysctl(cttyvp, KF_TYPE_VNODE, KF_FD_TYPE_CTTY,
3264 FREAD | FWRITE, -1, -1, 0, 0, kif, req);
3267 FILEDESC_SLOCK(fdp);
3268 /* working directory */
3269 if (fdp->fd_cdir != NULL) {
3271 data = fdp->fd_cdir;
3272 FILEDESC_SUNLOCK(fdp);
3273 export_fd_for_sysctl(data, KF_TYPE_VNODE, KF_FD_TYPE_CWD,
3274 FREAD, -1, -1, 0, 0, kif, req);
3275 FILEDESC_SLOCK(fdp);
3277 /* root directory */
3278 if (fdp->fd_rdir != NULL) {
3280 data = fdp->fd_rdir;
3281 FILEDESC_SUNLOCK(fdp);
3282 export_fd_for_sysctl(data, KF_TYPE_VNODE, KF_FD_TYPE_ROOT,
3283 FREAD, -1, -1, 0, 0, kif, req);
3284 FILEDESC_SLOCK(fdp);
3286 /* jail directory */
3287 if (fdp->fd_jdir != NULL) {
3289 data = fdp->fd_jdir;
3290 FILEDESC_SUNLOCK(fdp);
3291 export_fd_for_sysctl(data, KF_TYPE_VNODE, KF_FD_TYPE_JAIL,
3292 FREAD, -1, -1, 0, 0, kif, req);
3293 FILEDESC_SLOCK(fdp);
3295 for (i = 0; i < fdp->fd_nfiles; i++) {
3296 if ((fp = fdp->fd_ofiles[i]) == NULL)
3304 * When reporting a capability, most fields will be from the
3305 * underlying object, but do mark as a capability and export
3306 * the capability rights mask.
3308 if (fp->f_type == DTYPE_CAPABILITY) {
3310 fd_cap_rights = cap_rights(fp);
3311 (void)cap_funwrap(fp, 0, &fp);
3313 #else /* !CAPABILITIES */
3314 KASSERT(fp->f_type != DTYPE_CAPABILITY,
3315 ("sysctl_kern_proc_filedesc: saw capability"));
3317 switch (fp->f_type) {
3319 type = KF_TYPE_VNODE;
3325 type = KF_TYPE_SOCKET;
3330 type = KF_TYPE_PIPE;
3335 type = KF_TYPE_FIFO;
3341 type = KF_TYPE_KQUEUE;
3345 type = KF_TYPE_CRYPTO;
3349 type = KF_TYPE_MQUEUE;
3367 case DTYPE_PROCDESC:
3368 type = KF_TYPE_PROCDESC;
3374 type = KF_TYPE_UNKNOWN;
3377 refcnt = fp->f_count;
3378 fflags = fp->f_flag;
3379 offset = foffset_get(fp);
3382 * Create sysctl entry.
3383 * It is OK to drop the filedesc lock here as we will
3384 * re-validate and re-evaluate its properties when
3385 * the loop continues.
3387 oldidx = req->oldidx;
3388 if (type == KF_TYPE_VNODE || type == KF_TYPE_FIFO)
3389 FILEDESC_SUNLOCK(fdp);
3390 error = export_fd_for_sysctl(data, type, i, fflags, refcnt,
3391 offset, fd_is_cap, fd_cap_rights, kif, req);
3392 if (type == KF_TYPE_VNODE || type == KF_TYPE_FIFO)
3393 FILEDESC_SLOCK(fdp);
3395 if (error == ENOMEM) {
3397 * The hack to keep the ABI of sysctl
3398 * kern.proc.filedesc intact, but not
3399 * to account a partially copied
3400 * kinfo_file into the oldidx.
3402 req->oldidx = oldidx;
3408 FILEDESC_SUNLOCK(fdp);
3417 vntype_to_kinfo(int vtype)
3422 } vtypes_table[] = {
3423 { VBAD, KF_VTYPE_VBAD },
3424 { VBLK, KF_VTYPE_VBLK },
3425 { VCHR, KF_VTYPE_VCHR },
3426 { VDIR, KF_VTYPE_VDIR },
3427 { VFIFO, KF_VTYPE_VFIFO },
3428 { VLNK, KF_VTYPE_VLNK },
3429 { VNON, KF_VTYPE_VNON },
3430 { VREG, KF_VTYPE_VREG },
3431 { VSOCK, KF_VTYPE_VSOCK }
3433 #define NVTYPES (sizeof(vtypes_table) / sizeof(*vtypes_table))
3437 * Perform vtype translation.
3439 for (i = 0; i < NVTYPES; i++)
3440 if (vtypes_table[i].vtype == vtype)
3443 return (vtypes_table[i].kf_vtype);
3445 return (KF_VTYPE_UNKNOWN);
3449 fill_vnode_info(struct vnode *vp, struct kinfo_file *kif)
3452 char *fullpath, *freepath;
3457 kif->kf_vnode_type = vntype_to_kinfo(vp->v_type);
3460 error = vn_fullpath(curthread, vp, &fullpath, &freepath);
3462 strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path));
3464 if (freepath != NULL)
3465 free(freepath, M_TEMP);
3468 * Retrieve vnode attributes.
3470 va.va_fsid = VNOVAL;
3472 vn_lock(vp, LK_SHARED | LK_RETRY);
3473 error = VOP_GETATTR(vp, &va, curthread->td_ucred);
3477 if (va.va_fsid != VNOVAL)
3478 kif->kf_un.kf_file.kf_file_fsid = va.va_fsid;
3480 kif->kf_un.kf_file.kf_file_fsid =
3481 vp->v_mount->mnt_stat.f_fsid.val[0];
3482 kif->kf_un.kf_file.kf_file_fileid = va.va_fileid;
3483 kif->kf_un.kf_file.kf_file_mode = MAKEIMODE(va.va_type, va.va_mode);
3484 kif->kf_un.kf_file.kf_file_size = va.va_size;
3485 kif->kf_un.kf_file.kf_file_rdev = va.va_rdev;
3490 fill_socket_info(struct socket *so, struct kinfo_file *kif)
3492 struct sockaddr *sa;
3493 struct inpcb *inpcb;
3494 struct unpcb *unpcb;
3499 kif->kf_sock_domain = so->so_proto->pr_domain->dom_family;
3500 kif->kf_sock_type = so->so_type;
3501 kif->kf_sock_protocol = so->so_proto->pr_protocol;
3502 kif->kf_un.kf_sock.kf_sock_pcb = (uintptr_t)so->so_pcb;
3503 switch(kif->kf_sock_domain) {
3506 if (kif->kf_sock_protocol == IPPROTO_TCP) {
3507 if (so->so_pcb != NULL) {
3508 inpcb = (struct inpcb *)(so->so_pcb);
3509 kif->kf_un.kf_sock.kf_sock_inpcb =
3510 (uintptr_t)inpcb->inp_ppcb;
3515 if (so->so_pcb != NULL) {
3516 unpcb = (struct unpcb *)(so->so_pcb);
3517 if (unpcb->unp_conn) {
3518 kif->kf_un.kf_sock.kf_sock_unpconn =
3519 (uintptr_t)unpcb->unp_conn;
3520 kif->kf_un.kf_sock.kf_sock_rcv_sb_state =
3521 so->so_rcv.sb_state;
3522 kif->kf_un.kf_sock.kf_sock_snd_sb_state =
3523 so->so_snd.sb_state;
3528 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
3529 if (error == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) {
3530 bcopy(sa, &kif->kf_sa_local, sa->sa_len);
3533 error = so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa);
3534 if (error == 0 && sa->sa_len <= sizeof(kif->kf_sa_peer)) {
3535 bcopy(sa, &kif->kf_sa_peer, sa->sa_len);
3538 strncpy(kif->kf_path, so->so_proto->pr_domain->dom_name,
3539 sizeof(kif->kf_path));
3544 fill_pts_info(struct tty *tp, struct kinfo_file *kif)
3549 kif->kf_un.kf_pts.kf_pts_dev = tty_udev(tp);
3550 strlcpy(kif->kf_path, tty_devname(tp), sizeof(kif->kf_path));
3555 fill_pipe_info(struct pipe *pi, struct kinfo_file *kif)
3560 kif->kf_un.kf_pipe.kf_pipe_addr = (uintptr_t)pi;
3561 kif->kf_un.kf_pipe.kf_pipe_peer = (uintptr_t)pi->pipe_peer;
3562 kif->kf_un.kf_pipe.kf_pipe_buffer_cnt = pi->pipe_buffer.cnt;
3567 fill_procdesc_info(struct procdesc *pdp, struct kinfo_file *kif)
3572 kif->kf_un.kf_proc.kf_pid = pdp->pd_pid;
3577 fill_shm_info(struct file *fp, struct kinfo_file *kif)
3583 if (fp->f_data == NULL)
3585 if (fo_stat(fp, &sb, td->td_ucred, td) != 0)
3587 shm_path(fp->f_data, kif->kf_path, sizeof(kif->kf_path));
3588 kif->kf_un.kf_file.kf_file_mode = sb.st_mode;
3589 kif->kf_un.kf_file.kf_file_size = sb.st_size;
3593 static SYSCTL_NODE(_kern_proc, KERN_PROC_FILEDESC, filedesc, CTLFLAG_RD,
3594 sysctl_kern_proc_filedesc, "Process filedesc entries");
3598 * For the purposes of debugging, generate a human-readable string for the
3602 file_type_to_name(short type)
3632 * For the purposes of debugging, identify a process (if any, perhaps one of
3633 * many) that references the passed file in its file descriptor array. Return
3636 static struct proc *
3637 file_to_first_proc(struct file *fp)
3639 struct filedesc *fdp;
3643 FOREACH_PROC_IN_SYSTEM(p) {
3644 if (p->p_state == PRS_NEW)
3649 for (n = 0; n < fdp->fd_nfiles; n++) {
3650 if (fp == fdp->fd_ofiles[n])
3658 db_print_file(struct file *fp, int header)
3663 db_printf("%8s %4s %8s %8s %4s %5s %6s %8s %5s %12s\n",
3664 "File", "Type", "Data", "Flag", "GCFl", "Count",
3665 "MCount", "Vnode", "FPID", "FCmd");
3666 p = file_to_first_proc(fp);
3667 db_printf("%8p %4s %8p %08x %04x %5d %6d %8p %5d %12s\n", fp,
3668 file_type_to_name(fp->f_type), fp->f_data, fp->f_flag,
3669 0, fp->f_count, 0, fp->f_vnode,
3670 p != NULL ? p->p_pid : -1, p != NULL ? p->p_comm : "-");
3673 DB_SHOW_COMMAND(file, db_show_file)
3678 db_printf("usage: show file <addr>\n");
3681 fp = (struct file *)addr;
3682 db_print_file(fp, 1);
3685 DB_SHOW_COMMAND(files, db_show_files)
3687 struct filedesc *fdp;
3694 FOREACH_PROC_IN_SYSTEM(p) {
3695 if (p->p_state == PRS_NEW)
3697 if ((fdp = p->p_fd) == NULL)
3699 for (n = 0; n < fdp->fd_nfiles; ++n) {
3700 if ((fp = fdp->fd_ofiles[n]) == NULL)
3702 db_print_file(fp, header);
3709 SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
3710 &maxfilesperproc, 0, "Maximum files allowed open per process");
3712 SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
3713 &maxfiles, 0, "Maximum number of files");
3715 SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD,
3716 __DEVOLATILE(int *, &openfiles), 0, "System-wide number of open files");
3720 filelistinit(void *dummy)
3723 file_zone = uma_zcreate("Files", sizeof(struct file), NULL, NULL,
3724 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
3725 mtx_init(&sigio_lock, "sigio lock", NULL, MTX_DEF);
3726 mtx_init(&fdesc_mtx, "fdesc", NULL, MTX_DEF);
3728 SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, filelistinit, NULL);
3730 /*-------------------------------------------------------------------*/
3733 badfo_readwrite(struct file *fp, struct uio *uio, struct ucred *active_cred,
3734 int flags, struct thread *td)
3741 badfo_truncate(struct file *fp, off_t length, struct ucred *active_cred,
3749 badfo_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred,
3757 badfo_poll(struct file *fp, int events, struct ucred *active_cred,
3765 badfo_kqfilter(struct file *fp, struct knote *kn)
3772 badfo_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
3780 badfo_close(struct file *fp, struct thread *td)
3787 badfo_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
3795 badfo_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
3802 struct fileops badfileops = {
3803 .fo_read = badfo_readwrite,
3804 .fo_write = badfo_readwrite,
3805 .fo_truncate = badfo_truncate,
3806 .fo_ioctl = badfo_ioctl,
3807 .fo_poll = badfo_poll,
3808 .fo_kqfilter = badfo_kqfilter,
3809 .fo_stat = badfo_stat,
3810 .fo_close = badfo_close,
3811 .fo_chmod = badfo_chmod,
3812 .fo_chown = badfo_chown,
3816 invfo_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
3824 invfo_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
3831 /*-------------------------------------------------------------------*/
3834 * File Descriptor pseudo-device driver (/dev/fd/).
3836 * Opening minor device N dup()s the file (if any) connected to file
3837 * descriptor N belonging to the calling process. Note that this driver
3838 * consists of only the ``open()'' routine, because all subsequent
3839 * references to this file will be direct to the other driver.
3841 * XXX: we could give this one a cloning event handler if necessary.
3846 fdopen(struct cdev *dev, int mode, int type, struct thread *td)
3850 * XXX Kludge: set curthread->td_dupfd to contain the value of the
3851 * the file descriptor being sought for duplication. The error
3852 * return ensures that the vnode for this device will be released
3853 * by vn_open. Open will detect this special error and take the
3854 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
3855 * will simply report the error.
3857 td->td_dupfd = dev2unit(dev);
3861 static struct cdevsw fildesc_cdevsw = {
3862 .d_version = D_VERSION,
3868 fildesc_drvinit(void *unused)
3872 dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 0, NULL,
3873 UID_ROOT, GID_WHEEL, 0666, "fd/0");
3874 make_dev_alias(dev, "stdin");
3875 dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 1, NULL,
3876 UID_ROOT, GID_WHEEL, 0666, "fd/1");
3877 make_dev_alias(dev, "stdout");
3878 dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 2, NULL,
3879 UID_ROOT, GID_WHEEL, 0666, "fd/2");
3880 make_dev_alias(dev, "stderr");
3883 SYSINIT(fildescdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, fildesc_drvinit, NULL);