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>
59 #include <sys/limits.h>
61 #include <sys/malloc.h>
63 #include <sys/mount.h>
64 #include <sys/mqueue.h>
65 #include <sys/mutex.h>
66 #include <sys/namei.h>
67 #include <sys/selinfo.h>
71 #include <sys/procdesc.h>
72 #include <sys/protosw.h>
73 #include <sys/racct.h>
74 #include <sys/resourcevar.h>
76 #include <sys/signalvar.h>
77 #include <sys/socketvar.h>
80 #include <sys/syscallsubr.h>
81 #include <sys/sysctl.h>
82 #include <sys/sysproto.h>
84 #include <sys/unistd.h>
86 #include <sys/unpcb.h>
88 #include <sys/vnode.h>
90 #include <sys/ktrace.h>
95 #include <netinet/in.h>
96 #include <netinet/in_pcb.h>
98 #include <security/audit/audit.h>
105 static MALLOC_DEFINE(M_FILEDESC, "filedesc", "Open file descriptor table");
106 static MALLOC_DEFINE(M_FILEDESC_TO_LEADER, "filedesc_to_leader",
107 "file desc to leader structures");
108 static MALLOC_DEFINE(M_SIGIO, "sigio", "sigio structures");
109 MALLOC_DEFINE(M_FILECAPS, "filecaps", "descriptor capabilities");
111 MALLOC_DECLARE(M_FADVISE);
113 static uma_zone_t file_zone;
115 void (*ksem_info)(struct ksem *ks, char *path, size_t size, uint32_t *value);
117 static int closefp(struct filedesc *fdp, int fd, struct file *fp,
118 struct thread *td, int holdleaders);
119 static int fd_first_free(struct filedesc *fdp, int low, int size);
120 static int fd_last_used(struct filedesc *fdp, int size);
121 static void fdgrowtable(struct filedesc *fdp, int nfd);
122 static void fdunused(struct filedesc *fdp, int fd);
123 static void fdused(struct filedesc *fdp, int fd);
124 static int fill_pipe_info(struct pipe *pi, struct kinfo_file *kif);
125 static int fill_procdesc_info(struct procdesc *pdp,
126 struct kinfo_file *kif);
127 static int fill_pts_info(struct tty *tp, struct kinfo_file *kif);
128 static int fill_sem_info(struct file *fp, struct kinfo_file *kif);
129 static int fill_shm_info(struct file *fp, struct kinfo_file *kif);
130 static int fill_socket_info(struct socket *so, struct kinfo_file *kif);
131 static int fill_vnode_info(struct vnode *vp, struct kinfo_file *kif);
136 * - An array of open file descriptors (fd_ofiles)
137 * - An array of file flags (fd_ofileflags)
138 * - A bitmap recording which descriptors are in use (fd_map)
140 * A process starts out with NDFILE descriptors. The value of NDFILE has
141 * been selected based the historical limit of 20 open files, and an
142 * assumption that the majority of processes, especially short-lived
143 * processes like shells, will never need more.
145 * If this initial allocation is exhausted, a larger descriptor table and
146 * map are allocated dynamically, and the pointers in the process's struct
147 * filedesc are updated to point to those. This is repeated every time
148 * the process runs out of file descriptors (provided it hasn't hit its
151 * Since threads may hold references to individual descriptor table
152 * entries, the tables are never freed. Instead, they are placed on a
153 * linked list and freed only when the struct filedesc is released.
156 #define NDSLOTSIZE sizeof(NDSLOTTYPE)
157 #define NDENTRIES (NDSLOTSIZE * __CHAR_BIT)
158 #define NDSLOT(x) ((x) / NDENTRIES)
159 #define NDBIT(x) ((NDSLOTTYPE)1 << ((x) % NDENTRIES))
160 #define NDSLOTS(x) (((x) + NDENTRIES - 1) / NDENTRIES)
163 * SLIST entry used to keep track of ofiles which must be reclaimed when
167 struct filedescent *ft_table;
168 SLIST_ENTRY(freetable) ft_next;
172 * Initial allocation: a filedesc structure + the head of SLIST used to
173 * keep track of old ofiles + enough space for NDFILE descriptors.
176 struct filedesc fd_fd;
177 SLIST_HEAD(, freetable) fd_free;
178 struct filedescent fd_dfiles[NDFILE];
179 NDSLOTTYPE fd_dmap[NDSLOTS(NDFILE)];
183 * Descriptor management.
185 volatile int openfiles; /* actual number of open files */
186 struct mtx sigio_lock; /* mtx to protect pointers to sigio */
187 void (*mq_fdclose)(struct thread *td, int fd, struct file *fp);
189 /* A mutex to protect the association between a proc and filedesc. */
190 static struct mtx fdesc_mtx;
193 * If low >= size, just return low. Otherwise find the first zero bit in the
194 * given bitmap, starting at low and not exceeding size - 1. Return size if
198 fd_first_free(struct filedesc *fdp, int low, int size)
200 NDSLOTTYPE *map = fdp->fd_map;
208 if (low % NDENTRIES) {
209 mask = ~(~(NDSLOTTYPE)0 >> (NDENTRIES - (low % NDENTRIES)));
210 if ((mask &= ~map[off]) != 0UL)
211 return (off * NDENTRIES + ffsl(mask) - 1);
214 for (maxoff = NDSLOTS(size); off < maxoff; ++off)
215 if (map[off] != ~0UL)
216 return (off * NDENTRIES + ffsl(~map[off]) - 1);
221 * Find the highest non-zero bit in the given bitmap, starting at 0 and
222 * not exceeding size - 1. Return -1 if not found.
225 fd_last_used(struct filedesc *fdp, int size)
227 NDSLOTTYPE *map = fdp->fd_map;
232 if (size % NDENTRIES) {
233 mask = ~(~(NDSLOTTYPE)0 << (size % NDENTRIES));
234 if ((mask &= map[off]) != 0)
235 return (off * NDENTRIES + flsl(mask) - 1);
238 for (minoff = NDSLOT(0); off >= minoff; --off)
240 return (off * NDENTRIES + flsl(map[off]) - 1);
245 fdisused(struct filedesc *fdp, int fd)
248 FILEDESC_LOCK_ASSERT(fdp);
250 KASSERT(fd >= 0 && fd < fdp->fd_nfiles,
251 ("file descriptor %d out of range (0, %d)", fd, fdp->fd_nfiles));
253 return ((fdp->fd_map[NDSLOT(fd)] & NDBIT(fd)) != 0);
257 * Mark a file descriptor as used.
260 fdused(struct filedesc *fdp, int fd)
263 FILEDESC_XLOCK_ASSERT(fdp);
265 KASSERT(!fdisused(fdp, fd), ("fd=%d is already used", fd));
267 fdp->fd_map[NDSLOT(fd)] |= NDBIT(fd);
268 if (fd > fdp->fd_lastfile)
269 fdp->fd_lastfile = fd;
270 if (fd == fdp->fd_freefile)
271 fdp->fd_freefile = fd_first_free(fdp, fd, fdp->fd_nfiles);
275 * Mark a file descriptor as unused.
278 fdunused(struct filedesc *fdp, int fd)
281 FILEDESC_XLOCK_ASSERT(fdp);
283 KASSERT(fdisused(fdp, fd), ("fd=%d is already unused", fd));
284 KASSERT(fdp->fd_ofiles[fd].fde_file == NULL,
285 ("fd=%d is still in use", fd));
287 fdp->fd_map[NDSLOT(fd)] &= ~NDBIT(fd);
288 if (fd < fdp->fd_freefile)
289 fdp->fd_freefile = fd;
290 if (fd == fdp->fd_lastfile)
291 fdp->fd_lastfile = fd_last_used(fdp, fd);
295 * Free a file descriptor.
298 fdfree(struct filedesc *fdp, int fd)
300 struct filedescent *fde;
302 fde = &fdp->fd_ofiles[fd];
303 filecaps_free(&fde->fde_caps);
304 bzero(fde, sizeof(*fde));
309 * System calls on descriptors.
311 #ifndef _SYS_SYSPROTO_H_
312 struct getdtablesize_args {
318 sys_getdtablesize(struct thread *td, struct getdtablesize_args *uap)
320 struct proc *p = td->td_proc;
325 min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
326 lim = racct_get_limit(td->td_proc, RACCT_NOFILE);
328 if (lim < td->td_retval[0])
329 td->td_retval[0] = lim;
334 * Duplicate a file descriptor to a particular value.
336 * Note: keep in mind that a potential race condition exists when closing
337 * descriptors from a shared descriptor table (via rfork).
339 #ifndef _SYS_SYSPROTO_H_
347 sys_dup2(struct thread *td, struct dup2_args *uap)
350 return (do_dup(td, DUP_FIXED, (int)uap->from, (int)uap->to,
355 * Duplicate a file descriptor.
357 #ifndef _SYS_SYSPROTO_H_
364 sys_dup(struct thread *td, struct dup_args *uap)
367 return (do_dup(td, 0, (int)uap->fd, 0, td->td_retval));
371 * The file control system call.
373 #ifndef _SYS_SYSPROTO_H_
382 sys_fcntl(struct thread *td, struct fcntl_args *uap)
397 * Convert old flock structure to new.
399 error = copyin((void *)(intptr_t)uap->arg, &ofl, sizeof(ofl));
400 fl.l_start = ofl.l_start;
401 fl.l_len = ofl.l_len;
402 fl.l_pid = ofl.l_pid;
403 fl.l_type = ofl.l_type;
404 fl.l_whence = ofl.l_whence;
424 error = copyin((void *)(intptr_t)uap->arg, &fl, sizeof(fl));
433 error = kern_fcntl(td, uap->fd, cmd, arg);
436 if (uap->cmd == F_OGETLK) {
437 ofl.l_start = fl.l_start;
438 ofl.l_len = fl.l_len;
439 ofl.l_pid = fl.l_pid;
440 ofl.l_type = fl.l_type;
441 ofl.l_whence = fl.l_whence;
442 error = copyout(&ofl, (void *)(intptr_t)uap->arg, sizeof(ofl));
443 } else if (uap->cmd == F_GETLK) {
444 error = copyout(&fl, (void *)(intptr_t)uap->arg, sizeof(fl));
450 kern_fcntl(struct thread *td, int fd, int cmd, intptr_t arg)
452 struct filedesc *fdp;
454 struct file *fp, *fp2;
455 struct filedescent *fde;
471 error = do_dup(td, DUP_FCNTL, fd, tmp, td->td_retval);
474 case F_DUPFD_CLOEXEC:
476 error = do_dup(td, DUP_FCNTL | DUP_CLOEXEC, fd, tmp,
482 error = do_dup(td, DUP_FIXED, fd, tmp, td->td_retval);
485 case F_DUP2FD_CLOEXEC:
487 error = do_dup(td, DUP_FIXED | DUP_CLOEXEC, fd, tmp,
493 if ((fp = fget_locked(fdp, fd)) == NULL) {
494 FILEDESC_SUNLOCK(fdp);
498 fde = &fdp->fd_ofiles[fd];
500 (fde->fde_flags & UF_EXCLOSE) ? FD_CLOEXEC : 0;
501 FILEDESC_SUNLOCK(fdp);
506 if ((fp = fget_locked(fdp, fd)) == NULL) {
507 FILEDESC_XUNLOCK(fdp);
511 fde = &fdp->fd_ofiles[fd];
512 fde->fde_flags = (fde->fde_flags & ~UF_EXCLOSE) |
513 (arg & FD_CLOEXEC ? UF_EXCLOSE : 0);
514 FILEDESC_XUNLOCK(fdp);
518 error = fget_unlocked(fdp, fd, CAP_FCNTL, F_GETFL, &fp, NULL);
521 td->td_retval[0] = OFLAGS(fp->f_flag);
526 error = fget_unlocked(fdp, fd, CAP_FCNTL, F_SETFL, &fp, NULL);
530 tmp = flg = fp->f_flag;
532 tmp |= FFLAGS(arg & ~O_ACCMODE) & FCNTLFLAGS;
533 } while(atomic_cmpset_int(&fp->f_flag, flg, tmp) == 0);
534 tmp = fp->f_flag & FNONBLOCK;
535 error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
540 tmp = fp->f_flag & FASYNC;
541 error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td);
546 atomic_clear_int(&fp->f_flag, FNONBLOCK);
548 (void)fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
553 error = fget_unlocked(fdp, fd, CAP_FCNTL, F_GETOWN, &fp, NULL);
556 error = fo_ioctl(fp, FIOGETOWN, &tmp, td->td_ucred, td);
558 td->td_retval[0] = tmp;
563 error = fget_unlocked(fdp, fd, CAP_FCNTL, F_SETOWN, &fp, NULL);
567 error = fo_ioctl(fp, FIOSETOWN, &tmp, td->td_ucred, td);
572 error = priv_check(td, PRIV_NFS_LOCKD);
580 /* FALLTHROUGH F_SETLK */
584 error = fget_unlocked(fdp, fd, CAP_FLOCK, 0, &fp, NULL);
587 if (fp->f_type != DTYPE_VNODE) {
593 flp = (struct flock *)arg;
594 if (flp->l_whence == SEEK_CUR) {
595 foffset = foffset_get(fp);
598 foffset > OFF_MAX - flp->l_start)) {
599 FILEDESC_SUNLOCK(fdp);
604 flp->l_start += foffset;
608 switch (flp->l_type) {
610 if ((fp->f_flag & FREAD) == 0) {
614 PROC_LOCK(p->p_leader);
615 p->p_leader->p_flag |= P_ADVLOCK;
616 PROC_UNLOCK(p->p_leader);
617 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
621 if ((fp->f_flag & FWRITE) == 0) {
625 PROC_LOCK(p->p_leader);
626 p->p_leader->p_flag |= P_ADVLOCK;
627 PROC_UNLOCK(p->p_leader);
628 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
632 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
637 * Temporary api for testing remote lock
640 if (flg != F_REMOTE) {
644 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
645 F_UNLCKSYS, flp, flg);
651 if (error != 0 || flp->l_type == F_UNLCK ||
652 flp->l_type == F_UNLCKSYS) {
658 * Check for a race with close.
660 * The vnode is now advisory locked (or unlocked, but this case
661 * is not really important) as the caller requested.
662 * We had to drop the filedesc lock, so we need to recheck if
663 * the descriptor is still valid, because if it was closed
664 * in the meantime we need to remove advisory lock from the
665 * vnode - close on any descriptor leading to an advisory
666 * locked vnode, removes that lock.
667 * We will return 0 on purpose in that case, as the result of
668 * successful advisory lock might have been externally visible
669 * already. This is fine - effectively we pretend to the caller
670 * that the closing thread was a bit slower and that the
671 * advisory lock succeeded before the close.
673 error = fget_unlocked(fdp, fd, 0, 0, &fp2, NULL);
679 flp->l_whence = SEEK_SET;
682 flp->l_type = F_UNLCK;
683 (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
684 F_UNLCK, flp, F_POSIX);
691 error = fget_unlocked(fdp, fd, CAP_FLOCK, 0, &fp, NULL);
694 if (fp->f_type != DTYPE_VNODE) {
699 flp = (struct flock *)arg;
700 if (flp->l_type != F_RDLCK && flp->l_type != F_WRLCK &&
701 flp->l_type != F_UNLCK) {
706 if (flp->l_whence == SEEK_CUR) {
707 foffset = foffset_get(fp);
708 if ((flp->l_start > 0 &&
709 foffset > OFF_MAX - flp->l_start) ||
711 foffset < OFF_MIN - flp->l_start)) {
712 FILEDESC_SUNLOCK(fdp);
717 flp->l_start += foffset;
720 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK, flp,
726 arg = arg ? 128 * 1024: 0;
729 error = fget_unlocked(fdp, fd, 0, 0, &fp, NULL);
732 if (fp->f_type != DTYPE_VNODE) {
739 error = vn_lock(vp, LK_SHARED);
744 bsize = fp->f_vnode->v_mount->mnt_stat.f_iosize;
746 fp->f_seqcount = (arg + bsize - 1) / bsize;
748 new = old = fp->f_flag;
750 } while (!atomic_cmpset_rel_int(&fp->f_flag, old, new));
753 new = old = fp->f_flag;
755 } while (!atomic_cmpset_rel_int(&fp->f_flag, old, new));
768 * Common code for dup, dup2, fcntl(F_DUPFD) and fcntl(F_DUP2FD).
771 do_dup(struct thread *td, int flags, int old, int new,
774 struct filedesc *fdp;
775 struct filedescent *oldfde, *newfde;
785 * Verify we have a valid descriptor to dup from and possibly to
786 * dup to. Unlike dup() and dup2(), fcntl()'s F_DUPFD should
787 * return EINVAL when the new descriptor is out of bounds.
792 return (flags & DUP_FCNTL ? EINVAL : EBADF);
794 maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
797 return (flags & DUP_FCNTL ? EINVAL : EBADF);
800 if (fget_locked(fdp, old) == NULL) {
801 FILEDESC_XUNLOCK(fdp);
804 oldfde = &fdp->fd_ofiles[old];
805 if (flags & DUP_FIXED && old == new) {
807 if (flags & DUP_CLOEXEC)
808 fdp->fd_ofiles[new].fde_flags |= UF_EXCLOSE;
809 FILEDESC_XUNLOCK(fdp);
812 fp = oldfde->fde_file;
816 * If the caller specified a file descriptor, make sure the file
817 * table is large enough to hold it, and grab it. Otherwise, just
818 * allocate a new descriptor the usual way.
820 if (flags & DUP_FIXED) {
821 if (new >= fdp->fd_nfiles) {
823 * The resource limits are here instead of e.g.
824 * fdalloc(), because the file descriptor table may be
825 * shared between processes, so we can't really use
826 * racct_add()/racct_sub(). Instead of counting the
827 * number of actually allocated descriptors, just put
828 * the limit on the size of the file descriptor table.
832 error = racct_set(p, RACCT_NOFILE, new + 1);
835 FILEDESC_XUNLOCK(fdp);
840 fdgrowtable(fdp, new + 1);
841 oldfde = &fdp->fd_ofiles[old];
843 newfde = &fdp->fd_ofiles[new];
844 if (newfde->fde_file == NULL)
847 if ((error = fdalloc(td, new, &new)) != 0) {
848 FILEDESC_XUNLOCK(fdp);
852 newfde = &fdp->fd_ofiles[new];
855 KASSERT(fp == oldfde->fde_file, ("old fd has been modified"));
856 KASSERT(old != new, ("new fd is same as old"));
858 delfp = newfde->fde_file;
861 * Duplicate the source descriptor.
864 filecaps_copy(&oldfde->fde_caps, &newfde->fde_caps);
865 if ((flags & DUP_CLOEXEC) != 0)
866 newfde->fde_flags = oldfde->fde_flags | UF_EXCLOSE;
868 newfde->fde_flags = oldfde->fde_flags & ~UF_EXCLOSE;
869 if (new > fdp->fd_lastfile)
870 fdp->fd_lastfile = new;
874 (void) closefp(fdp, new, delfp, td, 1);
875 /* closefp() drops the FILEDESC lock for us. */
877 FILEDESC_XUNLOCK(fdp);
884 * If sigio is on the list associated with a process or process group,
885 * disable signalling from the device, remove sigio from the list and
889 funsetown(struct sigio **sigiop)
899 *(sigio->sio_myref) = NULL;
900 if ((sigio)->sio_pgid < 0) {
901 struct pgrp *pg = (sigio)->sio_pgrp;
903 SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio,
907 struct proc *p = (sigio)->sio_proc;
909 SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio,
914 crfree(sigio->sio_ucred);
915 free(sigio, M_SIGIO);
919 * Free a list of sigio structures.
920 * We only need to lock the SIGIO_LOCK because we have made ourselves
921 * inaccessible to callers of fsetown and therefore do not need to lock
922 * the proc or pgrp struct for the list manipulation.
925 funsetownlst(struct sigiolst *sigiolst)
931 sigio = SLIST_FIRST(sigiolst);
938 * Every entry of the list should belong
939 * to a single proc or pgrp.
941 if (sigio->sio_pgid < 0) {
942 pg = sigio->sio_pgrp;
943 PGRP_LOCK_ASSERT(pg, MA_NOTOWNED);
944 } else /* if (sigio->sio_pgid > 0) */ {
946 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
950 while ((sigio = SLIST_FIRST(sigiolst)) != NULL) {
951 *(sigio->sio_myref) = NULL;
953 KASSERT(sigio->sio_pgid < 0,
954 ("Proc sigio in pgrp sigio list"));
955 KASSERT(sigio->sio_pgrp == pg,
956 ("Bogus pgrp in sigio list"));
958 SLIST_REMOVE(&pg->pg_sigiolst, sigio, sigio,
961 } else /* if (p != NULL) */ {
962 KASSERT(sigio->sio_pgid > 0,
963 ("Pgrp sigio in proc sigio list"));
964 KASSERT(sigio->sio_proc == p,
965 ("Bogus proc in sigio list"));
967 SLIST_REMOVE(&p->p_sigiolst, sigio, sigio,
972 crfree(sigio->sio_ucred);
973 free(sigio, M_SIGIO);
980 * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg).
982 * After permission checking, add a sigio structure to the sigio list for
983 * the process or process group.
986 fsetown(pid_t pgid, struct sigio **sigiop)
1000 /* Allocate and fill in the new sigio out of locks. */
1001 sigio = malloc(sizeof(struct sigio), M_SIGIO, M_WAITOK);
1002 sigio->sio_pgid = pgid;
1003 sigio->sio_ucred = crhold(curthread->td_ucred);
1004 sigio->sio_myref = sigiop;
1006 sx_slock(&proctree_lock);
1015 * Policy - Don't allow a process to FSETOWN a process
1016 * in another session.
1018 * Remove this test to allow maximum flexibility or
1019 * restrict FSETOWN to the current process or process
1020 * group for maximum safety.
1023 if (proc->p_session != curthread->td_proc->p_session) {
1029 } else /* if (pgid < 0) */ {
1030 pgrp = pgfind(-pgid);
1038 * Policy - Don't allow a process to FSETOWN a process
1039 * in another session.
1041 * Remove this test to allow maximum flexibility or
1042 * restrict FSETOWN to the current process or process
1043 * group for maximum safety.
1045 if (pgrp->pg_session != curthread->td_proc->p_session) {
1056 * Since funsetownlst() is called without the proctree
1057 * locked, we need to check for P_WEXIT.
1058 * XXX: is ESRCH correct?
1060 if ((proc->p_flag & P_WEXIT) != 0) {
1065 SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio);
1066 sigio->sio_proc = proc;
1070 SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
1071 sigio->sio_pgrp = pgrp;
1074 sx_sunlock(&proctree_lock);
1081 sx_sunlock(&proctree_lock);
1082 crfree(sigio->sio_ucred);
1083 free(sigio, M_SIGIO);
1088 * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
1092 struct sigio **sigiop;
1097 pgid = (*sigiop != NULL) ? (*sigiop)->sio_pgid : 0;
1103 * Function drops the filedesc lock on return.
1106 closefp(struct filedesc *fdp, int fd, struct file *fp, struct thread *td,
1111 FILEDESC_XLOCK_ASSERT(fdp);
1114 if (td->td_proc->p_fdtol != NULL) {
1116 * Ask fdfree() to sleep to ensure that all relevant
1117 * process leaders can be traversed in closef().
1119 fdp->fd_holdleaderscount++;
1126 * We now hold the fp reference that used to be owned by the
1127 * descriptor array. We have to unlock the FILEDESC *AFTER*
1128 * knote_fdclose to prevent a race of the fd getting opened, a knote
1129 * added, and deleteing a knote for the new fd.
1131 knote_fdclose(td, fd);
1134 * We need to notify mqueue if the object is of type mqueue.
1136 if (fp->f_type == DTYPE_MQUEUE)
1137 mq_fdclose(td, fd, fp);
1138 FILEDESC_XUNLOCK(fdp);
1140 error = closef(fp, td);
1142 FILEDESC_XLOCK(fdp);
1143 fdp->fd_holdleaderscount--;
1144 if (fdp->fd_holdleaderscount == 0 &&
1145 fdp->fd_holdleaderswakeup != 0) {
1146 fdp->fd_holdleaderswakeup = 0;
1147 wakeup(&fdp->fd_holdleaderscount);
1149 FILEDESC_XUNLOCK(fdp);
1155 * Close a file descriptor.
1157 #ifndef _SYS_SYSPROTO_H_
1166 struct close_args *uap;
1169 return (kern_close(td, uap->fd));
1177 struct filedesc *fdp;
1180 fdp = td->td_proc->p_fd;
1182 AUDIT_SYSCLOSE(td, fd);
1184 FILEDESC_XLOCK(fdp);
1185 if ((fp = fget_locked(fdp, fd)) == NULL) {
1186 FILEDESC_XUNLOCK(fdp);
1191 /* closefp() drops the FILEDESC lock for us. */
1192 return (closefp(fdp, fd, fp, td, 1));
1196 * Close open file descriptors.
1198 #ifndef _SYS_SYSPROTO_H_
1199 struct closefrom_args {
1205 sys_closefrom(struct thread *td, struct closefrom_args *uap)
1207 struct filedesc *fdp;
1210 fdp = td->td_proc->p_fd;
1211 AUDIT_ARG_FD(uap->lowfd);
1214 * Treat negative starting file descriptor values identical to
1215 * closefrom(0) which closes all files.
1219 FILEDESC_SLOCK(fdp);
1220 for (fd = uap->lowfd; fd < fdp->fd_nfiles; fd++) {
1221 if (fdp->fd_ofiles[fd].fde_file != NULL) {
1222 FILEDESC_SUNLOCK(fdp);
1223 (void)kern_close(td, fd);
1224 FILEDESC_SLOCK(fdp);
1227 FILEDESC_SUNLOCK(fdp);
1231 #if defined(COMPAT_43)
1233 * Return status information about a file descriptor.
1235 #ifndef _SYS_SYSPROTO_H_
1236 struct ofstat_args {
1243 ofstat(struct thread *td, struct ofstat_args *uap)
1249 error = kern_fstat(td, uap->fd, &ub);
1252 error = copyout(&oub, uap->sb, sizeof(oub));
1256 #endif /* COMPAT_43 */
1259 * Return status information about a file descriptor.
1261 #ifndef _SYS_SYSPROTO_H_
1269 sys_fstat(struct thread *td, struct fstat_args *uap)
1274 error = kern_fstat(td, uap->fd, &ub);
1276 error = copyout(&ub, uap->sb, sizeof(ub));
1281 kern_fstat(struct thread *td, int fd, struct stat *sbp)
1288 if ((error = fget(td, fd, CAP_FSTAT, &fp)) != 0)
1291 AUDIT_ARG_FILE(td->td_proc, fp);
1293 error = fo_stat(fp, sbp, td->td_ucred, td);
1296 if (error == 0 && KTRPOINT(td, KTR_STRUCT))
1303 * Return status information about a file descriptor.
1305 #ifndef _SYS_SYSPROTO_H_
1306 struct nfstat_args {
1313 sys_nfstat(struct thread *td, struct nfstat_args *uap)
1319 error = kern_fstat(td, uap->fd, &ub);
1321 cvtnstat(&ub, &nub);
1322 error = copyout(&nub, uap->sb, sizeof(nub));
1328 * Return pathconf information about a file descriptor.
1330 #ifndef _SYS_SYSPROTO_H_
1331 struct fpathconf_args {
1338 sys_fpathconf(struct thread *td, struct fpathconf_args *uap)
1344 if ((error = fget(td, uap->fd, CAP_FPATHCONF, &fp)) != 0)
1347 /* If asynchronous I/O is available, it works for all descriptors. */
1348 if (uap->name == _PC_ASYNC_IO) {
1349 td->td_retval[0] = async_io_version;
1354 vn_lock(vp, LK_SHARED | LK_RETRY);
1355 error = VOP_PATHCONF(vp, uap->name, td->td_retval);
1357 } else if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) {
1358 if (uap->name != _PC_PIPE_BUF) {
1361 td->td_retval[0] = PIPE_BUF;
1373 * Initialize filecaps structure.
1376 filecaps_init(struct filecaps *fcaps)
1379 bzero(fcaps, sizeof(*fcaps));
1380 fcaps->fc_nioctls = -1;
1384 * Copy filecaps structure allocating memory for ioctls array if needed.
1387 filecaps_copy(const struct filecaps *src, struct filecaps *dst)
1392 if (src->fc_ioctls != NULL) {
1393 KASSERT(src->fc_nioctls > 0,
1394 ("fc_ioctls != NULL, but fc_nioctls=%hd", src->fc_nioctls));
1396 size = sizeof(src->fc_ioctls[0]) * src->fc_nioctls;
1397 dst->fc_ioctls = malloc(size, M_FILECAPS, M_WAITOK);
1398 bcopy(src->fc_ioctls, dst->fc_ioctls, size);
1403 * Move filecaps structure to the new place and clear the old place.
1406 filecaps_move(struct filecaps *src, struct filecaps *dst)
1410 bzero(src, sizeof(*src));
1414 * Fill the given filecaps structure with full rights.
1417 filecaps_fill(struct filecaps *fcaps)
1420 fcaps->fc_rights = CAP_ALL;
1421 fcaps->fc_ioctls = NULL;
1422 fcaps->fc_nioctls = -1;
1423 fcaps->fc_fcntls = CAP_FCNTL_ALL;
1427 * Free memory allocated within filecaps structure.
1430 filecaps_free(struct filecaps *fcaps)
1433 free(fcaps->fc_ioctls, M_FILECAPS);
1434 bzero(fcaps, sizeof(*fcaps));
1438 * Validate the given filecaps structure.
1441 filecaps_validate(const struct filecaps *fcaps, const char *func)
1444 KASSERT((fcaps->fc_rights & ~CAP_MASK_VALID) == 0,
1445 ("%s: invalid rights", func));
1446 KASSERT((fcaps->fc_fcntls & ~CAP_FCNTL_ALL) == 0,
1447 ("%s: invalid fcntls", func));
1448 KASSERT(fcaps->fc_fcntls == 0 || (fcaps->fc_rights & CAP_FCNTL) != 0,
1449 ("%s: fcntls without CAP_FCNTL", func));
1450 KASSERT(fcaps->fc_ioctls != NULL ? fcaps->fc_nioctls > 0 :
1451 (fcaps->fc_nioctls == -1 || fcaps->fc_nioctls == 0),
1452 ("%s: invalid ioctls", func));
1453 KASSERT(fcaps->fc_nioctls == 0 || (fcaps->fc_rights & CAP_IOCTL) != 0,
1454 ("%s: ioctls without CAP_IOCTL", func));
1458 * Grow the file table to accomodate (at least) nfd descriptors.
1461 fdgrowtable(struct filedesc *fdp, int nfd)
1463 struct filedesc0 *fdp0;
1464 struct freetable *ft;
1465 struct filedescent *ntable;
1466 struct filedescent *otable;
1467 int nnfiles, onfiles;
1468 NDSLOTTYPE *nmap, *omap;
1470 FILEDESC_XLOCK_ASSERT(fdp);
1472 KASSERT(fdp->fd_nfiles > 0, ("zero-length file table"));
1474 /* save old values */
1475 onfiles = fdp->fd_nfiles;
1476 otable = fdp->fd_ofiles;
1479 /* compute the size of the new table */
1480 nnfiles = NDSLOTS(nfd) * NDENTRIES; /* round up */
1481 if (nnfiles <= onfiles)
1482 /* the table is already large enough */
1486 * Allocate a new table and map. We need enough space for the
1487 * file entries themselves and the struct freetable we will use
1488 * when we decommission the table and place it on the freelist.
1489 * We place the struct freetable in the middle so we don't have
1490 * to worry about padding.
1492 ntable = malloc(nnfiles * sizeof(ntable[0]) + sizeof(struct freetable),
1493 M_FILEDESC, M_ZERO | M_WAITOK);
1494 nmap = malloc(NDSLOTS(nnfiles) * NDSLOTSIZE, M_FILEDESC,
1497 /* copy the old data over and point at the new tables */
1498 memcpy(ntable, otable, onfiles * sizeof(*otable));
1499 memcpy(nmap, omap, NDSLOTS(onfiles) * sizeof(*omap));
1501 /* update the pointers and counters */
1502 fdp->fd_nfiles = nnfiles;
1503 memcpy(ntable, otable, onfiles * sizeof(ntable[0]));
1504 fdp->fd_ofiles = ntable;
1508 * Do not free the old file table, as some threads may still
1509 * reference entries within it. Instead, place it on a freelist
1510 * which will be processed when the struct filedesc is released.
1512 * Do, however, free the old map.
1514 * Note that if onfiles == NDFILE, we're dealing with the original
1515 * static allocation contained within (struct filedesc0 *)fdp,
1516 * which must not be freed.
1518 if (onfiles > NDFILE) {
1519 ft = (struct freetable *)&otable[onfiles];
1520 fdp0 = (struct filedesc0 *)fdp;
1521 ft->ft_table = otable;
1522 SLIST_INSERT_HEAD(&fdp0->fd_free, ft, ft_next);
1523 free(omap, M_FILEDESC);
1528 * Allocate a file descriptor for the process.
1531 fdalloc(struct thread *td, int minfd, int *result)
1533 struct proc *p = td->td_proc;
1534 struct filedesc *fdp = p->p_fd;
1535 int fd = -1, maxfd, allocfd;
1540 FILEDESC_XLOCK_ASSERT(fdp);
1542 if (fdp->fd_freefile > minfd)
1543 minfd = fdp->fd_freefile;
1546 maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
1550 * Search the bitmap for a free descriptor starting at minfd.
1551 * If none is found, grow the file table.
1553 fd = fd_first_free(fdp, minfd, fdp->fd_nfiles);
1556 if (fd >= fdp->fd_nfiles) {
1557 allocfd = min(fd * 2, maxfd);
1560 error = racct_set(p, RACCT_NOFILE, allocfd);
1566 * fd is already equal to first free descriptor >= minfd, so
1567 * we only need to grow the table and we are done.
1569 fdgrowtable(fdp, allocfd);
1573 * Perform some sanity checks, then mark the file descriptor as
1574 * used and return it to the caller.
1576 KASSERT(fd >= 0 && fd < min(maxfd, fdp->fd_nfiles),
1577 ("invalid descriptor %d", fd));
1578 KASSERT(!fdisused(fdp, fd),
1579 ("fd_first_free() returned non-free descriptor"));
1580 KASSERT(fdp->fd_ofiles[fd].fde_file == NULL,
1581 ("file descriptor isn't free"));
1582 KASSERT(fdp->fd_ofiles[fd].fde_flags == 0, ("file flags are set"));
1589 * Allocate n file descriptors for the process.
1592 fdallocn(struct thread *td, int minfd, int *fds, int n)
1594 struct proc *p = td->td_proc;
1595 struct filedesc *fdp = p->p_fd;
1598 FILEDESC_XLOCK_ASSERT(fdp);
1600 if (!fdavail(td, n))
1603 for (i = 0; i < n; i++)
1604 if (fdalloc(td, 0, &fds[i]) != 0)
1608 for (i--; i >= 0; i--)
1609 fdunused(fdp, fds[i]);
1617 * Check to see whether n user file descriptors are available to the process
1621 fdavail(struct thread *td, int n)
1623 struct proc *p = td->td_proc;
1624 struct filedesc *fdp = td->td_proc->p_fd;
1627 FILEDESC_LOCK_ASSERT(fdp);
1630 * XXX: This is only called from uipc_usrreq.c:unp_externalize();
1631 * call racct_add() from there instead of dealing with containers
1635 lim = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
1637 if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
1639 last = min(fdp->fd_nfiles, lim);
1640 for (i = fdp->fd_freefile; i < last; i++) {
1641 if (fdp->fd_ofiles[i].fde_file == NULL && --n <= 0)
1648 * Create a new open file structure and allocate a file decriptor for the
1649 * process that refers to it. We add one reference to the file for the
1650 * descriptor table and one reference for resultfp. This is to prevent us
1651 * being preempted and the entry in the descriptor table closed after we
1652 * release the FILEDESC lock.
1655 falloc(struct thread *td, struct file **resultfp, int *resultfd, int flags)
1660 error = falloc_noinstall(td, &fp);
1662 return (error); /* no reference held on error */
1664 error = finstall(td, fp, &fd, flags, NULL);
1666 fdrop(fp, td); /* one reference (fp only) */
1670 if (resultfp != NULL)
1671 *resultfp = fp; /* copy out result */
1673 fdrop(fp, td); /* release local reference */
1675 if (resultfd != NULL)
1682 * Create a new open file structure without allocating a file descriptor.
1685 falloc_noinstall(struct thread *td, struct file **resultfp)
1688 int maxuserfiles = maxfiles - (maxfiles / 20);
1689 static struct timeval lastfail;
1692 KASSERT(resultfp != NULL, ("%s: resultfp == NULL", __func__));
1694 if ((openfiles >= maxuserfiles &&
1695 priv_check(td, PRIV_MAXFILES) != 0) ||
1696 openfiles >= maxfiles) {
1697 if (ppsratecheck(&lastfail, &curfail, 1)) {
1698 printf("kern.maxfiles limit exceeded by uid %i, "
1699 "please see tuning(7).\n", td->td_ucred->cr_ruid);
1703 atomic_add_int(&openfiles, 1);
1704 fp = uma_zalloc(file_zone, M_WAITOK | M_ZERO);
1705 refcount_init(&fp->f_count, 1);
1706 fp->f_cred = crhold(td->td_ucred);
1707 fp->f_ops = &badfileops;
1715 * Install a file in a file descriptor table.
1718 finstall(struct thread *td, struct file *fp, int *fd, int flags,
1719 struct filecaps *fcaps)
1721 struct filedesc *fdp = td->td_proc->p_fd;
1722 struct filedescent *fde;
1725 KASSERT(fd != NULL, ("%s: fd == NULL", __func__));
1726 KASSERT(fp != NULL, ("%s: fp == NULL", __func__));
1728 filecaps_validate(fcaps, __func__);
1730 FILEDESC_XLOCK(fdp);
1731 if ((error = fdalloc(td, 0, fd))) {
1732 FILEDESC_XUNLOCK(fdp);
1736 fde = &fdp->fd_ofiles[*fd];
1738 if ((flags & O_CLOEXEC) != 0)
1739 fde->fde_flags |= UF_EXCLOSE;
1741 filecaps_move(fcaps, &fde->fde_caps);
1743 filecaps_fill(&fde->fde_caps);
1744 FILEDESC_XUNLOCK(fdp);
1749 * Build a new filedesc structure from another.
1750 * Copy the current, root, and jail root vnode references.
1753 fdinit(struct filedesc *fdp)
1755 struct filedesc0 *newfdp;
1757 newfdp = malloc(sizeof *newfdp, M_FILEDESC, M_WAITOK | M_ZERO);
1758 FILEDESC_LOCK_INIT(&newfdp->fd_fd);
1760 FILEDESC_XLOCK(fdp);
1761 newfdp->fd_fd.fd_cdir = fdp->fd_cdir;
1762 if (newfdp->fd_fd.fd_cdir)
1763 VREF(newfdp->fd_fd.fd_cdir);
1764 newfdp->fd_fd.fd_rdir = fdp->fd_rdir;
1765 if (newfdp->fd_fd.fd_rdir)
1766 VREF(newfdp->fd_fd.fd_rdir);
1767 newfdp->fd_fd.fd_jdir = fdp->fd_jdir;
1768 if (newfdp->fd_fd.fd_jdir)
1769 VREF(newfdp->fd_fd.fd_jdir);
1770 FILEDESC_XUNLOCK(fdp);
1773 /* Create the file descriptor table. */
1774 newfdp->fd_fd.fd_refcnt = 1;
1775 newfdp->fd_fd.fd_holdcnt = 1;
1776 newfdp->fd_fd.fd_cmask = CMASK;
1777 newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
1778 newfdp->fd_fd.fd_nfiles = NDFILE;
1779 newfdp->fd_fd.fd_map = newfdp->fd_dmap;
1780 newfdp->fd_fd.fd_lastfile = -1;
1781 return (&newfdp->fd_fd);
1784 static struct filedesc *
1785 fdhold(struct proc *p)
1787 struct filedesc *fdp;
1789 mtx_lock(&fdesc_mtx);
1793 mtx_unlock(&fdesc_mtx);
1798 fddrop(struct filedesc *fdp)
1800 struct filedesc0 *fdp0;
1801 struct freetable *ft;
1804 mtx_lock(&fdesc_mtx);
1805 i = --fdp->fd_holdcnt;
1806 mtx_unlock(&fdesc_mtx);
1810 FILEDESC_LOCK_DESTROY(fdp);
1811 fdp0 = (struct filedesc0 *)fdp;
1812 while ((ft = SLIST_FIRST(&fdp0->fd_free)) != NULL) {
1813 SLIST_REMOVE_HEAD(&fdp0->fd_free, ft_next);
1814 free(ft->ft_table, M_FILEDESC);
1816 free(fdp, M_FILEDESC);
1820 * Share a filedesc structure.
1823 fdshare(struct filedesc *fdp)
1826 FILEDESC_XLOCK(fdp);
1828 FILEDESC_XUNLOCK(fdp);
1833 * Unshare a filedesc structure, if necessary by making a copy
1836 fdunshare(struct proc *p, struct thread *td)
1839 FILEDESC_XLOCK(p->p_fd);
1840 if (p->p_fd->fd_refcnt > 1) {
1841 struct filedesc *tmp;
1843 FILEDESC_XUNLOCK(p->p_fd);
1844 tmp = fdcopy(p->p_fd);
1848 FILEDESC_XUNLOCK(p->p_fd);
1852 * Copy a filedesc structure. A NULL pointer in returns a NULL reference,
1853 * this is to ease callers, not catch errors.
1856 fdcopy(struct filedesc *fdp)
1858 struct filedesc *newfdp;
1859 struct filedescent *nfde, *ofde;
1862 /* Certain daemons might not have file descriptors. */
1866 newfdp = fdinit(fdp);
1867 FILEDESC_SLOCK(fdp);
1868 while (fdp->fd_lastfile >= newfdp->fd_nfiles) {
1869 FILEDESC_SUNLOCK(fdp);
1870 FILEDESC_XLOCK(newfdp);
1871 fdgrowtable(newfdp, fdp->fd_lastfile + 1);
1872 FILEDESC_XUNLOCK(newfdp);
1873 FILEDESC_SLOCK(fdp);
1875 /* copy all passable descriptors (i.e. not kqueue) */
1876 newfdp->fd_freefile = -1;
1877 for (i = 0; i <= fdp->fd_lastfile; ++i) {
1878 ofde = &fdp->fd_ofiles[i];
1879 if (fdisused(fdp, i) &&
1880 (ofde->fde_file->f_ops->fo_flags & DFLAG_PASSABLE) &&
1881 ofde->fde_file->f_ops != &badfileops) {
1882 nfde = &newfdp->fd_ofiles[i];
1884 filecaps_copy(&ofde->fde_caps, &nfde->fde_caps);
1885 fhold(nfde->fde_file);
1886 newfdp->fd_lastfile = i;
1888 if (newfdp->fd_freefile == -1)
1889 newfdp->fd_freefile = i;
1892 newfdp->fd_cmask = fdp->fd_cmask;
1893 FILEDESC_SUNLOCK(fdp);
1894 FILEDESC_XLOCK(newfdp);
1895 for (i = 0; i <= newfdp->fd_lastfile; ++i) {
1896 if (newfdp->fd_ofiles[i].fde_file != NULL)
1899 if (newfdp->fd_freefile == -1)
1900 newfdp->fd_freefile = i;
1901 FILEDESC_XUNLOCK(newfdp);
1906 * Release a filedesc structure.
1909 fdescfree(struct thread *td)
1911 struct filedesc *fdp;
1913 struct filedesc_to_leader *fdtol;
1915 struct vnode *cdir, *jdir, *rdir, *vp;
1918 /* Certain daemons might not have file descriptors. */
1919 fdp = td->td_proc->p_fd;
1924 PROC_LOCK(td->td_proc);
1925 racct_set(td->td_proc, RACCT_NOFILE, 0);
1926 PROC_UNLOCK(td->td_proc);
1929 /* Check for special need to clear POSIX style locks */
1930 fdtol = td->td_proc->p_fdtol;
1931 if (fdtol != NULL) {
1932 FILEDESC_XLOCK(fdp);
1933 KASSERT(fdtol->fdl_refcount > 0,
1934 ("filedesc_to_refcount botch: fdl_refcount=%d",
1935 fdtol->fdl_refcount));
1936 if (fdtol->fdl_refcount == 1 &&
1937 (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1938 for (i = 0; i <= fdp->fd_lastfile; i++) {
1939 fp = fdp->fd_ofiles[i].fde_file;
1940 if (fp == NULL || fp->f_type != DTYPE_VNODE)
1943 FILEDESC_XUNLOCK(fdp);
1944 lf.l_whence = SEEK_SET;
1947 lf.l_type = F_UNLCK;
1949 (void) VOP_ADVLOCK(vp,
1950 (caddr_t)td->td_proc->p_leader, F_UNLCK,
1952 FILEDESC_XLOCK(fdp);
1957 if (fdtol->fdl_refcount == 1) {
1958 if (fdp->fd_holdleaderscount > 0 &&
1959 (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1961 * close() or do_dup() has cleared a reference
1962 * in a shared file descriptor table.
1964 fdp->fd_holdleaderswakeup = 1;
1965 sx_sleep(&fdp->fd_holdleaderscount,
1966 FILEDESC_LOCK(fdp), PLOCK, "fdlhold", 0);
1969 if (fdtol->fdl_holdcount > 0) {
1971 * Ensure that fdtol->fdl_leader remains
1972 * valid in closef().
1974 fdtol->fdl_wakeup = 1;
1975 sx_sleep(fdtol, FILEDESC_LOCK(fdp), PLOCK,
1980 fdtol->fdl_refcount--;
1981 if (fdtol->fdl_refcount == 0 &&
1982 fdtol->fdl_holdcount == 0) {
1983 fdtol->fdl_next->fdl_prev = fdtol->fdl_prev;
1984 fdtol->fdl_prev->fdl_next = fdtol->fdl_next;
1987 td->td_proc->p_fdtol = NULL;
1988 FILEDESC_XUNLOCK(fdp);
1990 free(fdtol, M_FILEDESC_TO_LEADER);
1992 FILEDESC_XLOCK(fdp);
1993 i = --fdp->fd_refcnt;
1994 FILEDESC_XUNLOCK(fdp);
1998 for (i = 0; i <= fdp->fd_lastfile; i++) {
1999 fp = fdp->fd_ofiles[i].fde_file;
2001 FILEDESC_XLOCK(fdp);
2003 FILEDESC_XUNLOCK(fdp);
2004 (void) closef(fp, td);
2007 FILEDESC_XLOCK(fdp);
2009 /* XXX This should happen earlier. */
2010 mtx_lock(&fdesc_mtx);
2011 td->td_proc->p_fd = NULL;
2012 mtx_unlock(&fdesc_mtx);
2014 if (fdp->fd_nfiles > NDFILE)
2015 free(fdp->fd_ofiles, M_FILEDESC);
2016 if (NDSLOTS(fdp->fd_nfiles) > NDSLOTS(NDFILE))
2017 free(fdp->fd_map, M_FILEDESC);
2021 cdir = fdp->fd_cdir;
2022 fdp->fd_cdir = NULL;
2023 rdir = fdp->fd_rdir;
2024 fdp->fd_rdir = NULL;
2025 jdir = fdp->fd_jdir;
2026 fdp->fd_jdir = NULL;
2027 FILEDESC_XUNLOCK(fdp);
2040 * For setugid programs, we don't want to people to use that setugidness
2041 * to generate error messages which write to a file which otherwise would
2042 * otherwise be off-limits to the process. We check for filesystems where
2043 * the vnode can change out from under us after execve (like [lin]procfs).
2045 * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
2046 * sufficient. We also don't check for setugidness since we know we are.
2049 is_unsafe(struct file *fp)
2051 if (fp->f_type == DTYPE_VNODE) {
2052 struct vnode *vp = fp->f_vnode;
2054 if ((vp->v_vflag & VV_PROCDEP) != 0)
2061 * Make this setguid thing safe, if at all possible.
2064 setugidsafety(struct thread *td)
2066 struct filedesc *fdp;
2070 /* Certain daemons might not have file descriptors. */
2071 fdp = td->td_proc->p_fd;
2076 * Note: fdp->fd_ofiles may be reallocated out from under us while
2077 * we are blocked in a close. Be careful!
2079 FILEDESC_XLOCK(fdp);
2080 for (i = 0; i <= fdp->fd_lastfile; i++) {
2083 fp = fdp->fd_ofiles[i].fde_file;
2084 if (fp != NULL && is_unsafe(fp)) {
2085 knote_fdclose(td, i);
2087 * NULL-out descriptor prior to close to avoid
2088 * a race while close blocks.
2091 FILEDESC_XUNLOCK(fdp);
2092 (void) closef(fp, td);
2093 FILEDESC_XLOCK(fdp);
2096 FILEDESC_XUNLOCK(fdp);
2100 * If a specific file object occupies a specific file descriptor, close the
2101 * file descriptor entry and drop a reference on the file object. This is a
2102 * convenience function to handle a subsequent error in a function that calls
2103 * falloc() that handles the race that another thread might have closed the
2104 * file descriptor out from under the thread creating the file object.
2107 fdclose(struct filedesc *fdp, struct file *fp, int idx, struct thread *td)
2110 FILEDESC_XLOCK(fdp);
2111 if (fdp->fd_ofiles[idx].fde_file == fp) {
2113 FILEDESC_XUNLOCK(fdp);
2116 FILEDESC_XUNLOCK(fdp);
2120 * Close any files on exec?
2123 fdcloseexec(struct thread *td)
2125 struct filedesc *fdp;
2126 struct filedescent *fde;
2130 /* Certain daemons might not have file descriptors. */
2131 fdp = td->td_proc->p_fd;
2136 * We cannot cache fd_ofiles since operations
2137 * may block and rip them out from under us.
2139 FILEDESC_XLOCK(fdp);
2140 for (i = 0; i <= fdp->fd_lastfile; i++) {
2141 fde = &fdp->fd_ofiles[i];
2143 if (fp != NULL && (fp->f_type == DTYPE_MQUEUE ||
2144 (fde->fde_flags & UF_EXCLOSE))) {
2146 (void) closefp(fdp, i, fp, td, 0);
2147 /* closefp() drops the FILEDESC lock. */
2148 FILEDESC_XLOCK(fdp);
2151 FILEDESC_XUNLOCK(fdp);
2155 * It is unsafe for set[ug]id processes to be started with file
2156 * descriptors 0..2 closed, as these descriptors are given implicit
2157 * significance in the Standard C library. fdcheckstd() will create a
2158 * descriptor referencing /dev/null for each of stdin, stdout, and
2159 * stderr that is not already open.
2162 fdcheckstd(struct thread *td)
2164 struct filedesc *fdp;
2165 register_t retval, save;
2166 int i, error, devnull;
2168 fdp = td->td_proc->p_fd;
2171 KASSERT(fdp->fd_refcnt == 1, ("the fdtable should not be shared"));
2174 for (i = 0; i < 3; i++) {
2175 if (fdp->fd_ofiles[i].fde_file != NULL)
2178 save = td->td_retval[0];
2179 error = kern_open(td, "/dev/null", UIO_SYSSPACE,
2181 devnull = td->td_retval[0];
2182 td->td_retval[0] = save;
2185 KASSERT(devnull == i, ("oof, we didn't get our fd"));
2187 error = do_dup(td, DUP_FIXED, devnull, i, &retval);
2196 * Internal form of close. Decrement reference count on file structure.
2197 * Note: td may be NULL when closing a file that was being passed in a
2200 * XXXRW: Giant is not required for the caller, but often will be held; this
2201 * makes it moderately likely the Giant will be recursed in the VFS case.
2204 closef(struct file *fp, struct thread *td)
2208 struct filedesc_to_leader *fdtol;
2209 struct filedesc *fdp;
2212 * POSIX record locking dictates that any close releases ALL
2213 * locks owned by this process. This is handled by setting
2214 * a flag in the unlock to free ONLY locks obeying POSIX
2215 * semantics, and not to free BSD-style file locks.
2216 * If the descriptor was in a message, POSIX-style locks
2217 * aren't passed with the descriptor, and the thread pointer
2218 * will be NULL. Callers should be careful only to pass a
2219 * NULL thread pointer when there really is no owning
2220 * context that might have locks, or the locks will be
2223 if (fp->f_type == DTYPE_VNODE && td != NULL) {
2225 if ((td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
2226 lf.l_whence = SEEK_SET;
2229 lf.l_type = F_UNLCK;
2230 (void) VOP_ADVLOCK(vp, (caddr_t)td->td_proc->p_leader,
2231 F_UNLCK, &lf, F_POSIX);
2233 fdtol = td->td_proc->p_fdtol;
2234 if (fdtol != NULL) {
2236 * Handle special case where file descriptor table is
2237 * shared between multiple process leaders.
2239 fdp = td->td_proc->p_fd;
2240 FILEDESC_XLOCK(fdp);
2241 for (fdtol = fdtol->fdl_next;
2242 fdtol != td->td_proc->p_fdtol;
2243 fdtol = fdtol->fdl_next) {
2244 if ((fdtol->fdl_leader->p_flag &
2247 fdtol->fdl_holdcount++;
2248 FILEDESC_XUNLOCK(fdp);
2249 lf.l_whence = SEEK_SET;
2252 lf.l_type = F_UNLCK;
2254 (void) VOP_ADVLOCK(vp,
2255 (caddr_t)fdtol->fdl_leader, F_UNLCK, &lf,
2257 FILEDESC_XLOCK(fdp);
2258 fdtol->fdl_holdcount--;
2259 if (fdtol->fdl_holdcount == 0 &&
2260 fdtol->fdl_wakeup != 0) {
2261 fdtol->fdl_wakeup = 0;
2265 FILEDESC_XUNLOCK(fdp);
2268 return (fdrop(fp, td));
2272 * Initialize the file pointer with the specified properties.
2274 * The ops are set with release semantics to be certain that the flags, type,
2275 * and data are visible when ops is. This is to prevent ops methods from being
2276 * called with bad data.
2279 finit(struct file *fp, u_int flag, short type, void *data, struct fileops *ops)
2284 atomic_store_rel_ptr((volatile uintptr_t *)&fp->f_ops, (uintptr_t)ops);
2288 fget_unlocked(struct filedesc *fdp, int fd, cap_rights_t needrights,
2289 int needfcntl, struct file **fpp, cap_rights_t *haverightsp)
2294 cap_rights_t haverights;
2298 if (fd < 0 || fd >= fdp->fd_nfiles)
2301 * Fetch the descriptor locklessly. We avoid fdrop() races by
2302 * never raising a refcount above 0. To accomplish this we have
2303 * to use a cmpset loop rather than an atomic_add. The descriptor
2304 * must be re-verified once we acquire a reference to be certain
2305 * that the identity is still correct and we did not lose a race
2306 * due to preemption.
2309 fp = fdp->fd_ofiles[fd].fde_file;
2313 haverights = cap_rights(fdp, fd);
2314 error = cap_check(haverights, needrights);
2317 if ((needrights & CAP_FCNTL) != 0) {
2318 error = cap_fcntl_check(fdp, fd, needfcntl);
2323 count = fp->f_count;
2327 * Use an acquire barrier to prevent caching of fd_ofiles
2328 * so it is refreshed for verification.
2330 if (atomic_cmpset_acq_int(&fp->f_count, count, count + 1) != 1)
2332 if (fp == fdp->fd_ofiles[fd].fde_file)
2334 fdrop(fp, curthread);
2337 if (haverightsp != NULL) {
2339 *haverightsp = haverights;
2341 *haverightsp = CAP_ALL;
2348 * Extract the file pointer associated with the specified descriptor for the
2349 * current user process.
2351 * If the descriptor doesn't exist or doesn't match 'flags', EBADF is
2354 * File's rights will be checked against the capability rights mask.
2356 * If an error occured the non-zero error is returned and *fpp is set to
2357 * NULL. Otherwise *fpp is held and set and zero is returned. Caller is
2358 * responsible for fdrop().
2361 _fget(struct thread *td, int fd, struct file **fpp, int flags,
2362 cap_rights_t needrights, u_char *maxprotp)
2364 struct filedesc *fdp;
2366 cap_rights_t haverights;
2370 if (td == NULL || (fdp = td->td_proc->p_fd) == NULL)
2372 if (maxprotp != NULL)
2373 needrights |= CAP_MMAP;
2374 error = fget_unlocked(fdp, fd, needrights, 0, &fp, &haverights);
2377 if (fp->f_ops == &badfileops) {
2384 * If requested, convert capability rights to access flags.
2386 if (maxprotp != NULL)
2387 *maxprotp = cap_rights_to_vmprot(haverights);
2388 #else /* !CAPABILITIES */
2389 if (maxprotp != NULL)
2390 *maxprotp = VM_PROT_ALL;
2391 #endif /* CAPABILITIES */
2394 * FREAD and FWRITE failure return EBADF as per POSIX.
2400 if ((fp->f_flag & flags) == 0)
2404 if ((fp->f_flag & (FREAD | FEXEC)) == 0 ||
2405 ((fp->f_flag & FWRITE) != 0))
2411 KASSERT(0, ("wrong flags"));
2424 fget(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
2427 return(_fget(td, fd, fpp, 0, rights, NULL));
2431 fget_mmap(struct thread *td, int fd, cap_rights_t rights, u_char *maxprotp,
2435 return (_fget(td, fd, fpp, 0, rights, maxprotp));
2439 fget_read(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
2442 return(_fget(td, fd, fpp, FREAD, rights, NULL));
2446 fget_write(struct thread *td, int fd, cap_rights_t rights, struct file **fpp)
2449 return (_fget(td, fd, fpp, FWRITE, rights, NULL));
2453 * Like fget() but loads the underlying vnode, or returns an error if the
2454 * descriptor does not represent a vnode. Note that pipes use vnodes but
2455 * never have VM objects. The returned vnode will be vref()'d.
2457 * XXX: what about the unused flags ?
2460 _fgetvp(struct thread *td, int fd, int flags, cap_rights_t needrights,
2467 error = _fget(td, fd, &fp, flags, needrights, NULL);
2470 if (fp->f_vnode == NULL) {
2482 fgetvp(struct thread *td, int fd, cap_rights_t rights, struct vnode **vpp)
2485 return (_fgetvp(td, fd, 0, rights, vpp));
2489 fgetvp_rights(struct thread *td, int fd, cap_rights_t need,
2490 struct filecaps *havecaps, struct vnode **vpp)
2492 struct filedesc *fdp;
2498 if (td == NULL || (fdp = td->td_proc->p_fd) == NULL)
2501 fp = fget_locked(fdp, fd);
2502 if (fp == NULL || fp->f_ops == &badfileops)
2506 error = cap_check(cap_rights(fdp, fd), need);
2511 if (fp->f_vnode == NULL)
2516 filecaps_copy(&fdp->fd_ofiles[fd].fde_caps, havecaps);
2522 fgetvp_read(struct thread *td, int fd, cap_rights_t rights, struct vnode **vpp)
2525 return (_fgetvp(td, fd, FREAD, rights, vpp));
2529 fgetvp_exec(struct thread *td, int fd, cap_rights_t rights, struct vnode **vpp)
2532 return (_fgetvp(td, fd, FEXEC, rights, vpp));
2537 fgetvp_write(struct thread *td, int fd, cap_rights_t rights,
2541 return (_fgetvp(td, fd, FWRITE, rights, vpp));
2546 * Like fget() but loads the underlying socket, or returns an error if the
2547 * descriptor does not represent a socket.
2549 * We bump the ref count on the returned socket. XXX Also obtain the SX lock
2552 * Note: fgetsock() and fputsock() are deprecated, as consumers should rely
2553 * on their file descriptor reference to prevent the socket from being free'd
2557 fgetsock(struct thread *td, int fd, cap_rights_t rights, struct socket **spp,
2566 if ((error = _fget(td, fd, &fp, 0, rights, NULL)) != 0)
2568 if (fp->f_type != DTYPE_SOCKET) {
2573 *fflagp = fp->f_flag;
2584 * Drop the reference count on the socket and XXX release the SX lock in the
2585 * future. The last reference closes the socket.
2587 * Note: fputsock() is deprecated, see comment for fgetsock().
2590 fputsock(struct socket *so)
2595 CURVNET_SET(so->so_vnet);
2601 * Handle the last reference to a file being closed.
2604 _fdrop(struct file *fp, struct thread *td)
2609 if (fp->f_count != 0)
2610 panic("fdrop: count %d", fp->f_count);
2611 if (fp->f_ops != &badfileops)
2612 error = fo_close(fp, td);
2613 atomic_subtract_int(&openfiles, 1);
2615 free(fp->f_advice, M_FADVISE);
2616 uma_zfree(file_zone, fp);
2622 * Apply an advisory lock on a file descriptor.
2624 * Just attempt to get a record lock of the requested type on the entire file
2625 * (l_whence = SEEK_SET, l_start = 0, l_len = 0).
2627 #ifndef _SYS_SYSPROTO_H_
2635 sys_flock(struct thread *td, struct flock_args *uap)
2642 if ((error = fget(td, uap->fd, CAP_FLOCK, &fp)) != 0)
2644 if (fp->f_type != DTYPE_VNODE) {
2646 return (EOPNOTSUPP);
2650 lf.l_whence = SEEK_SET;
2653 if (uap->how & LOCK_UN) {
2654 lf.l_type = F_UNLCK;
2655 atomic_clear_int(&fp->f_flag, FHASLOCK);
2656 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
2659 if (uap->how & LOCK_EX)
2660 lf.l_type = F_WRLCK;
2661 else if (uap->how & LOCK_SH)
2662 lf.l_type = F_RDLCK;
2667 atomic_set_int(&fp->f_flag, FHASLOCK);
2668 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
2669 (uap->how & LOCK_NB) ? F_FLOCK : F_FLOCK | F_WAIT);
2675 * Duplicate the specified descriptor to a free descriptor.
2678 dupfdopen(struct thread *td, struct filedesc *fdp, int dfd, int mode,
2679 int openerror, int *indxp)
2684 KASSERT(openerror == ENODEV || openerror == ENXIO,
2685 ("unexpected error %d in %s", openerror, __func__));
2688 * If the to-be-dup'd fd number is greater than the allowed number
2689 * of file descriptors, or the fd to be dup'd has already been
2690 * closed, then reject.
2692 FILEDESC_XLOCK(fdp);
2693 if ((fp = fget_locked(fdp, dfd)) == NULL) {
2694 FILEDESC_XUNLOCK(fdp);
2698 error = fdalloc(td, 0, &indx);
2700 FILEDESC_XUNLOCK(fdp);
2705 * There are two cases of interest here.
2707 * For ENODEV simply dup (dfd) to file descriptor (indx) and return.
2709 * For ENXIO steal away the file structure from (dfd) and store it in
2710 * (indx). (dfd) is effectively closed by this operation.
2712 switch (openerror) {
2715 * Check that the mode the file is being opened for is a
2716 * subset of the mode of the existing descriptor.
2718 if (((mode & (FREAD|FWRITE)) | fp->f_flag) != fp->f_flag) {
2719 fdunused(fdp, indx);
2720 FILEDESC_XUNLOCK(fdp);
2724 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
2725 filecaps_copy(&fdp->fd_ofiles[dfd].fde_caps,
2726 &fdp->fd_ofiles[indx].fde_caps);
2730 * Steal away the file pointer from dfd and stuff it into indx.
2732 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
2733 bzero(&fdp->fd_ofiles[dfd], sizeof(fdp->fd_ofiles[dfd]));
2737 FILEDESC_XUNLOCK(fdp);
2743 * Scan all active processes and prisons to see if any of them have a current
2744 * or root directory of `olddp'. If so, replace them with the new mount point.
2747 mountcheckdirs(struct vnode *olddp, struct vnode *newdp)
2749 struct filedesc *fdp;
2754 if (vrefcnt(olddp) == 1)
2757 sx_slock(&allproc_lock);
2758 FOREACH_PROC_IN_SYSTEM(p) {
2762 FILEDESC_XLOCK(fdp);
2763 if (fdp->fd_cdir == olddp) {
2765 fdp->fd_cdir = newdp;
2768 if (fdp->fd_rdir == olddp) {
2770 fdp->fd_rdir = newdp;
2773 if (fdp->fd_jdir == olddp) {
2775 fdp->fd_jdir = newdp;
2778 FILEDESC_XUNLOCK(fdp);
2781 sx_sunlock(&allproc_lock);
2782 if (rootvnode == olddp) {
2787 mtx_lock(&prison0.pr_mtx);
2788 if (prison0.pr_root == olddp) {
2790 prison0.pr_root = newdp;
2793 mtx_unlock(&prison0.pr_mtx);
2794 sx_slock(&allprison_lock);
2795 TAILQ_FOREACH(pr, &allprison, pr_list) {
2796 mtx_lock(&pr->pr_mtx);
2797 if (pr->pr_root == olddp) {
2799 pr->pr_root = newdp;
2802 mtx_unlock(&pr->pr_mtx);
2804 sx_sunlock(&allprison_lock);
2809 struct filedesc_to_leader *
2810 filedesc_to_leader_alloc(struct filedesc_to_leader *old, struct filedesc *fdp, struct proc *leader)
2812 struct filedesc_to_leader *fdtol;
2814 fdtol = malloc(sizeof(struct filedesc_to_leader),
2815 M_FILEDESC_TO_LEADER,
2817 fdtol->fdl_refcount = 1;
2818 fdtol->fdl_holdcount = 0;
2819 fdtol->fdl_wakeup = 0;
2820 fdtol->fdl_leader = leader;
2822 FILEDESC_XLOCK(fdp);
2823 fdtol->fdl_next = old->fdl_next;
2824 fdtol->fdl_prev = old;
2825 old->fdl_next = fdtol;
2826 fdtol->fdl_next->fdl_prev = fdtol;
2827 FILEDESC_XUNLOCK(fdp);
2829 fdtol->fdl_next = fdtol;
2830 fdtol->fdl_prev = fdtol;
2836 * Get file structures globally.
2839 sysctl_kern_file(SYSCTL_HANDLER_ARGS)
2842 struct filedesc *fdp;
2847 error = sysctl_wire_old_buffer(req, 0);
2850 if (req->oldptr == NULL) {
2852 sx_slock(&allproc_lock);
2853 FOREACH_PROC_IN_SYSTEM(p) {
2854 if (p->p_state == PRS_NEW)
2859 /* overestimates sparse tables. */
2860 if (fdp->fd_lastfile > 0)
2861 n += fdp->fd_lastfile;
2864 sx_sunlock(&allproc_lock);
2865 return (SYSCTL_OUT(req, 0, n * sizeof(xf)));
2868 bzero(&xf, sizeof(xf));
2869 xf.xf_size = sizeof(xf);
2870 sx_slock(&allproc_lock);
2871 FOREACH_PROC_IN_SYSTEM(p) {
2873 if (p->p_state == PRS_NEW) {
2877 if (p_cansee(req->td, p) != 0) {
2881 xf.xf_pid = p->p_pid;
2882 xf.xf_uid = p->p_ucred->cr_uid;
2887 FILEDESC_SLOCK(fdp);
2888 for (n = 0; fdp->fd_refcnt > 0 && n < fdp->fd_nfiles; ++n) {
2889 if ((fp = fdp->fd_ofiles[n].fde_file) == NULL)
2893 xf.xf_data = fp->f_data;
2894 xf.xf_vnode = fp->f_vnode;
2895 xf.xf_type = fp->f_type;
2896 xf.xf_count = fp->f_count;
2898 xf.xf_offset = foffset_get(fp);
2899 xf.xf_flag = fp->f_flag;
2900 error = SYSCTL_OUT(req, &xf, sizeof(xf));
2904 FILEDESC_SUNLOCK(fdp);
2909 sx_sunlock(&allproc_lock);
2913 SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
2914 0, 0, sysctl_kern_file, "S,xfile", "Entire file table");
2916 #ifdef KINFO_OFILE_SIZE
2917 CTASSERT(sizeof(struct kinfo_ofile) == KINFO_OFILE_SIZE);
2920 #ifdef COMPAT_FREEBSD7
2922 export_vnode_for_osysctl(struct vnode *vp, int type,
2923 struct kinfo_ofile *kif, struct filedesc *fdp, struct sysctl_req *req)
2926 char *fullpath, *freepath;
2928 bzero(kif, sizeof(*kif));
2929 kif->kf_structsize = sizeof(*kif);
2933 kif->kf_type = KF_TYPE_VNODE;
2934 /* This function only handles directories. */
2935 if (vp->v_type != VDIR) {
2939 kif->kf_vnode_type = KF_VTYPE_VDIR;
2942 * This is not a true file descriptor, so we set a bogus refcount
2943 * and offset to indicate these fields should be ignored.
2945 kif->kf_ref_count = -1;
2946 kif->kf_offset = -1;
2950 FILEDESC_SUNLOCK(fdp);
2951 vn_fullpath(curthread, vp, &fullpath, &freepath);
2953 strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path));
2954 if (freepath != NULL)
2955 free(freepath, M_TEMP);
2956 error = SYSCTL_OUT(req, kif, sizeof(*kif));
2957 FILEDESC_SLOCK(fdp);
2962 * Get per-process file descriptors for use by procstat(1), et al.
2965 sysctl_kern_proc_ofiledesc(SYSCTL_HANDLER_ARGS)
2967 char *fullpath, *freepath;
2968 struct kinfo_ofile *kif;
2969 struct filedesc *fdp;
2970 int error, i, *name;
2971 struct shmfd *shmfd;
2980 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
2987 kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK);
2988 FILEDESC_SLOCK(fdp);
2989 if (fdp->fd_cdir != NULL)
2990 export_vnode_for_osysctl(fdp->fd_cdir, KF_FD_TYPE_CWD, kif,
2992 if (fdp->fd_rdir != NULL)
2993 export_vnode_for_osysctl(fdp->fd_rdir, KF_FD_TYPE_ROOT, kif,
2995 if (fdp->fd_jdir != NULL)
2996 export_vnode_for_osysctl(fdp->fd_jdir, KF_FD_TYPE_JAIL, kif,
2998 for (i = 0; i < fdp->fd_nfiles; i++) {
2999 if ((fp = fdp->fd_ofiles[i].fde_file) == NULL)
3001 bzero(kif, sizeof(*kif));
3002 kif->kf_structsize = sizeof(*kif);
3010 switch (fp->f_type) {
3012 kif->kf_type = KF_TYPE_VNODE;
3017 kif->kf_type = KF_TYPE_SOCKET;
3022 kif->kf_type = KF_TYPE_PIPE;
3026 kif->kf_type = KF_TYPE_FIFO;
3031 kif->kf_type = KF_TYPE_KQUEUE;
3035 kif->kf_type = KF_TYPE_CRYPTO;
3039 kif->kf_type = KF_TYPE_MQUEUE;
3043 kif->kf_type = KF_TYPE_SHM;
3048 kif->kf_type = KF_TYPE_SEM;
3053 kif->kf_type = KF_TYPE_PTS;
3058 case DTYPE_PROCDESC:
3059 kif->kf_type = KF_TYPE_PROCDESC;
3064 kif->kf_type = KF_TYPE_UNKNOWN;
3067 kif->kf_ref_count = fp->f_count;
3068 if (fp->f_flag & FREAD)
3069 kif->kf_flags |= KF_FLAG_READ;
3070 if (fp->f_flag & FWRITE)
3071 kif->kf_flags |= KF_FLAG_WRITE;
3072 if (fp->f_flag & FAPPEND)
3073 kif->kf_flags |= KF_FLAG_APPEND;
3074 if (fp->f_flag & FASYNC)
3075 kif->kf_flags |= KF_FLAG_ASYNC;
3076 if (fp->f_flag & FFSYNC)
3077 kif->kf_flags |= KF_FLAG_FSYNC;
3078 if (fp->f_flag & FNONBLOCK)
3079 kif->kf_flags |= KF_FLAG_NONBLOCK;
3080 if (fp->f_flag & O_DIRECT)
3081 kif->kf_flags |= KF_FLAG_DIRECT;
3082 if (fp->f_flag & FHASLOCK)
3083 kif->kf_flags |= KF_FLAG_HASLOCK;
3084 kif->kf_offset = foffset_get(fp);
3087 switch (vp->v_type) {
3089 kif->kf_vnode_type = KF_VTYPE_VNON;
3092 kif->kf_vnode_type = KF_VTYPE_VREG;
3095 kif->kf_vnode_type = KF_VTYPE_VDIR;
3098 kif->kf_vnode_type = KF_VTYPE_VBLK;
3101 kif->kf_vnode_type = KF_VTYPE_VCHR;
3104 kif->kf_vnode_type = KF_VTYPE_VLNK;
3107 kif->kf_vnode_type = KF_VTYPE_VSOCK;
3110 kif->kf_vnode_type = KF_VTYPE_VFIFO;
3113 kif->kf_vnode_type = KF_VTYPE_VBAD;
3116 kif->kf_vnode_type = KF_VTYPE_UNKNOWN;
3120 * It is OK to drop the filedesc lock here as we will
3121 * re-validate and re-evaluate its properties when
3122 * the loop continues.
3126 FILEDESC_SUNLOCK(fdp);
3127 vn_fullpath(curthread, vp, &fullpath, &freepath);
3129 strlcpy(kif->kf_path, fullpath,
3130 sizeof(kif->kf_path));
3131 if (freepath != NULL)
3132 free(freepath, M_TEMP);
3133 FILEDESC_SLOCK(fdp);
3136 struct sockaddr *sa;
3138 if (so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa)
3139 == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) {
3140 bcopy(sa, &kif->kf_sa_local, sa->sa_len);
3143 if (so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa)
3144 == 0 && sa->sa_len <= sizeof(kif->kf_sa_peer)) {
3145 bcopy(sa, &kif->kf_sa_peer, sa->sa_len);
3148 kif->kf_sock_domain =
3149 so->so_proto->pr_domain->dom_family;
3150 kif->kf_sock_type = so->so_type;
3151 kif->kf_sock_protocol = so->so_proto->pr_protocol;
3154 strlcpy(kif->kf_path, tty_devname(tp),
3155 sizeof(kif->kf_path));
3158 shm_path(shmfd, kif->kf_path, sizeof(kif->kf_path));
3159 if (ks != NULL && ksem_info != NULL)
3160 ksem_info(ks, kif->kf_path, sizeof(kif->kf_path), NULL);
3161 error = SYSCTL_OUT(req, kif, sizeof(*kif));
3165 FILEDESC_SUNLOCK(fdp);
3171 static SYSCTL_NODE(_kern_proc, KERN_PROC_OFILEDESC, ofiledesc, CTLFLAG_RD,
3172 sysctl_kern_proc_ofiledesc, "Process ofiledesc entries");
3173 #endif /* COMPAT_FREEBSD7 */
3175 #ifdef KINFO_FILE_SIZE
3176 CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE);
3179 struct export_fd_buf {
3180 struct filedesc *fdp;
3183 struct kinfo_file kif;
3187 export_fd_to_sb(void *data, int type, int fd, int fflags, int refcnt,
3188 int64_t offset, cap_rights_t fd_cap_rights, struct export_fd_buf *efbuf)
3193 } fflags_table[] = {
3194 { FAPPEND, KF_FLAG_APPEND },
3195 { FASYNC, KF_FLAG_ASYNC },
3196 { FFSYNC, KF_FLAG_FSYNC },
3197 { FHASLOCK, KF_FLAG_HASLOCK },
3198 { FNONBLOCK, KF_FLAG_NONBLOCK },
3199 { FREAD, KF_FLAG_READ },
3200 { FWRITE, KF_FLAG_WRITE },
3201 { O_CREAT, KF_FLAG_CREAT },
3202 { O_DIRECT, KF_FLAG_DIRECT },
3203 { O_EXCL, KF_FLAG_EXCL },
3204 { O_EXEC, KF_FLAG_EXEC },
3205 { O_EXLOCK, KF_FLAG_EXLOCK },
3206 { O_NOFOLLOW, KF_FLAG_NOFOLLOW },
3207 { O_SHLOCK, KF_FLAG_SHLOCK },
3208 { O_TRUNC, KF_FLAG_TRUNC }
3210 #define NFFLAGS (sizeof(fflags_table) / sizeof(*fflags_table))
3211 struct kinfo_file *kif;
3216 if (efbuf->remainder == 0)
3219 bzero(kif, sizeof(*kif));
3220 locked = efbuf->fdp != NULL;
3225 FILEDESC_SUNLOCK(efbuf->fdp);
3228 vp = (struct vnode *)data;
3229 error = fill_vnode_info(vp, kif);
3232 case KF_TYPE_SOCKET:
3233 error = fill_socket_info((struct socket *)data, kif);
3236 error = fill_pipe_info((struct pipe *)data, kif);
3239 error = fill_pts_info((struct tty *)data, kif);
3241 case KF_TYPE_PROCDESC:
3242 error = fill_procdesc_info((struct procdesc *)data, kif);
3245 error = fill_sem_info((struct file *)data, kif);
3248 error = fill_shm_info((struct file *)data, kif);
3254 kif->kf_status |= KF_ATTR_VALID;
3257 * Translate file access flags.
3259 for (i = 0; i < NFFLAGS; i++)
3260 if (fflags & fflags_table[i].fflag)
3261 kif->kf_flags |= fflags_table[i].kf_fflag;
3262 kif->kf_cap_rights = fd_cap_rights;
3264 kif->kf_type = type;
3265 kif->kf_ref_count = refcnt;
3266 kif->kf_offset = offset;
3267 /* Pack record size down */
3268 kif->kf_structsize = offsetof(struct kinfo_file, kf_path) +
3269 strlen(kif->kf_path) + 1;
3270 kif->kf_structsize = roundup(kif->kf_structsize, sizeof(uint64_t));
3271 if (efbuf->remainder != -1) {
3272 if (efbuf->remainder < kif->kf_structsize) {
3273 /* Terminate export. */
3274 efbuf->remainder = 0;
3275 if (efbuf->fdp != NULL && !locked)
3276 FILEDESC_SLOCK(efbuf->fdp);
3279 efbuf->remainder -= kif->kf_structsize;
3282 FILEDESC_SUNLOCK(efbuf->fdp);
3283 error = sbuf_bcat(efbuf->sb, kif, kif->kf_structsize);
3284 if (efbuf->fdp != NULL)
3285 FILEDESC_SLOCK(efbuf->fdp);
3290 * Store a process file descriptor information to sbuf.
3292 * Takes a locked proc as argument, and returns with the proc unlocked.
3295 kern_proc_filedesc_out(struct proc *p, struct sbuf *sb, ssize_t maxlen)
3298 struct filedesc *fdp;
3299 struct export_fd_buf *efbuf;
3300 struct vnode *cttyvp, *textvp, *tracevp;
3304 int type, refcnt, fflags;
3305 cap_rights_t fd_cap_rights;
3307 PROC_LOCK_ASSERT(p, MA_OWNED);
3310 tracevp = p->p_tracevp;
3311 if (tracevp != NULL)
3314 textvp = p->p_textvp;
3317 /* Controlling tty. */
3319 if (p->p_pgrp != NULL && p->p_pgrp->pg_session != NULL) {
3320 cttyvp = p->p_pgrp->pg_session->s_ttyvp;
3326 efbuf = malloc(sizeof(*efbuf), M_TEMP, M_WAITOK);
3329 efbuf->remainder = maxlen;
3330 if (tracevp != NULL)
3331 export_fd_to_sb(tracevp, KF_TYPE_VNODE, KF_FD_TYPE_TRACE,
3332 FREAD | FWRITE, -1, -1, 0, efbuf);
3334 export_fd_to_sb(textvp, KF_TYPE_VNODE, KF_FD_TYPE_TEXT,
3335 FREAD, -1, -1, 0, efbuf);
3337 export_fd_to_sb(cttyvp, KF_TYPE_VNODE, KF_FD_TYPE_CTTY,
3338 FREAD | FWRITE, -1, -1, 0, efbuf);
3343 FILEDESC_SLOCK(fdp);
3344 /* working directory */
3345 if (fdp->fd_cdir != NULL) {
3347 data = fdp->fd_cdir;
3348 export_fd_to_sb(data, KF_TYPE_VNODE, KF_FD_TYPE_CWD,
3349 FREAD, -1, -1, 0, efbuf);
3351 /* root directory */
3352 if (fdp->fd_rdir != NULL) {
3354 data = fdp->fd_rdir;
3355 export_fd_to_sb(data, KF_TYPE_VNODE, KF_FD_TYPE_ROOT,
3356 FREAD, -1, -1, 0, efbuf);
3358 /* jail directory */
3359 if (fdp->fd_jdir != NULL) {
3361 data = fdp->fd_jdir;
3362 export_fd_to_sb(data, KF_TYPE_VNODE, KF_FD_TYPE_JAIL,
3363 FREAD, -1, -1, 0, efbuf);
3365 for (i = 0; i < fdp->fd_nfiles; i++) {
3366 if ((fp = fdp->fd_ofiles[i].fde_file) == NULL)
3370 fd_cap_rights = cap_rights(fdp, i);
3371 #else /* !CAPABILITIES */
3374 switch (fp->f_type) {
3376 type = KF_TYPE_VNODE;
3382 type = KF_TYPE_SOCKET;
3387 type = KF_TYPE_PIPE;
3392 type = KF_TYPE_FIFO;
3398 type = KF_TYPE_KQUEUE;
3402 type = KF_TYPE_CRYPTO;
3406 type = KF_TYPE_MQUEUE;
3425 case DTYPE_PROCDESC:
3426 type = KF_TYPE_PROCDESC;
3432 type = KF_TYPE_UNKNOWN;
3435 refcnt = fp->f_count;
3436 fflags = fp->f_flag;
3437 offset = foffset_get(fp);
3440 * Create sysctl entry.
3441 * It is OK to drop the filedesc lock here as we will
3442 * re-validate and re-evaluate its properties when
3443 * the loop continues.
3445 error = export_fd_to_sb(data, type, i, fflags, refcnt,
3446 offset, fd_cap_rights, efbuf);
3450 FILEDESC_SUNLOCK(fdp);
3453 free(efbuf, M_TEMP);
3457 #define FILEDESC_SBUF_SIZE (sizeof(struct kinfo_file) * 5)
3460 * Get per-process file descriptors for use by procstat(1), et al.
3463 sysctl_kern_proc_filedesc(SYSCTL_HANDLER_ARGS)
3468 int error, error2, *name;
3472 sbuf_new_for_sysctl(&sb, NULL, FILEDESC_SBUF_SIZE, req);
3473 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
3478 maxlen = req->oldptr != NULL ? req->oldlen : -1;
3479 error = kern_proc_filedesc_out(p, &sb, maxlen);
3480 error2 = sbuf_finish(&sb);
3482 return (error != 0 ? error : error2);
3486 vntype_to_kinfo(int vtype)
3491 } vtypes_table[] = {
3492 { VBAD, KF_VTYPE_VBAD },
3493 { VBLK, KF_VTYPE_VBLK },
3494 { VCHR, KF_VTYPE_VCHR },
3495 { VDIR, KF_VTYPE_VDIR },
3496 { VFIFO, KF_VTYPE_VFIFO },
3497 { VLNK, KF_VTYPE_VLNK },
3498 { VNON, KF_VTYPE_VNON },
3499 { VREG, KF_VTYPE_VREG },
3500 { VSOCK, KF_VTYPE_VSOCK }
3502 #define NVTYPES (sizeof(vtypes_table) / sizeof(*vtypes_table))
3506 * Perform vtype translation.
3508 for (i = 0; i < NVTYPES; i++)
3509 if (vtypes_table[i].vtype == vtype)
3512 return (vtypes_table[i].kf_vtype);
3514 return (KF_VTYPE_UNKNOWN);
3518 fill_vnode_info(struct vnode *vp, struct kinfo_file *kif)
3521 char *fullpath, *freepath;
3526 kif->kf_vnode_type = vntype_to_kinfo(vp->v_type);
3529 error = vn_fullpath(curthread, vp, &fullpath, &freepath);
3531 strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path));
3533 if (freepath != NULL)
3534 free(freepath, M_TEMP);
3537 * Retrieve vnode attributes.
3539 va.va_fsid = VNOVAL;
3541 vn_lock(vp, LK_SHARED | LK_RETRY);
3542 error = VOP_GETATTR(vp, &va, curthread->td_ucred);
3546 if (va.va_fsid != VNOVAL)
3547 kif->kf_un.kf_file.kf_file_fsid = va.va_fsid;
3549 kif->kf_un.kf_file.kf_file_fsid =
3550 vp->v_mount->mnt_stat.f_fsid.val[0];
3551 kif->kf_un.kf_file.kf_file_fileid = va.va_fileid;
3552 kif->kf_un.kf_file.kf_file_mode = MAKEIMODE(va.va_type, va.va_mode);
3553 kif->kf_un.kf_file.kf_file_size = va.va_size;
3554 kif->kf_un.kf_file.kf_file_rdev = va.va_rdev;
3559 fill_socket_info(struct socket *so, struct kinfo_file *kif)
3561 struct sockaddr *sa;
3562 struct inpcb *inpcb;
3563 struct unpcb *unpcb;
3568 kif->kf_sock_domain = so->so_proto->pr_domain->dom_family;
3569 kif->kf_sock_type = so->so_type;
3570 kif->kf_sock_protocol = so->so_proto->pr_protocol;
3571 kif->kf_un.kf_sock.kf_sock_pcb = (uintptr_t)so->so_pcb;
3572 switch(kif->kf_sock_domain) {
3575 if (kif->kf_sock_protocol == IPPROTO_TCP) {
3576 if (so->so_pcb != NULL) {
3577 inpcb = (struct inpcb *)(so->so_pcb);
3578 kif->kf_un.kf_sock.kf_sock_inpcb =
3579 (uintptr_t)inpcb->inp_ppcb;
3584 if (so->so_pcb != NULL) {
3585 unpcb = (struct unpcb *)(so->so_pcb);
3586 if (unpcb->unp_conn) {
3587 kif->kf_un.kf_sock.kf_sock_unpconn =
3588 (uintptr_t)unpcb->unp_conn;
3589 kif->kf_un.kf_sock.kf_sock_rcv_sb_state =
3590 so->so_rcv.sb_state;
3591 kif->kf_un.kf_sock.kf_sock_snd_sb_state =
3592 so->so_snd.sb_state;
3597 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
3598 if (error == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) {
3599 bcopy(sa, &kif->kf_sa_local, sa->sa_len);
3602 error = so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa);
3603 if (error == 0 && sa->sa_len <= sizeof(kif->kf_sa_peer)) {
3604 bcopy(sa, &kif->kf_sa_peer, sa->sa_len);
3607 strncpy(kif->kf_path, so->so_proto->pr_domain->dom_name,
3608 sizeof(kif->kf_path));
3613 fill_pts_info(struct tty *tp, struct kinfo_file *kif)
3618 kif->kf_un.kf_pts.kf_pts_dev = tty_udev(tp);
3619 strlcpy(kif->kf_path, tty_devname(tp), sizeof(kif->kf_path));
3624 fill_pipe_info(struct pipe *pi, struct kinfo_file *kif)
3629 kif->kf_un.kf_pipe.kf_pipe_addr = (uintptr_t)pi;
3630 kif->kf_un.kf_pipe.kf_pipe_peer = (uintptr_t)pi->pipe_peer;
3631 kif->kf_un.kf_pipe.kf_pipe_buffer_cnt = pi->pipe_buffer.cnt;
3636 fill_procdesc_info(struct procdesc *pdp, struct kinfo_file *kif)
3641 kif->kf_un.kf_proc.kf_pid = pdp->pd_pid;
3646 fill_sem_info(struct file *fp, struct kinfo_file *kif)
3652 if (fp->f_data == NULL)
3654 if (fo_stat(fp, &sb, td->td_ucred, td) != 0)
3656 if (ksem_info == NULL)
3658 ksem_info(fp->f_data, kif->kf_path, sizeof(kif->kf_path),
3659 &kif->kf_un.kf_sem.kf_sem_value);
3660 kif->kf_un.kf_sem.kf_sem_mode = sb.st_mode;
3665 fill_shm_info(struct file *fp, struct kinfo_file *kif)
3671 if (fp->f_data == NULL)
3673 if (fo_stat(fp, &sb, td->td_ucred, td) != 0)
3675 shm_path(fp->f_data, kif->kf_path, sizeof(kif->kf_path));
3676 kif->kf_un.kf_file.kf_file_mode = sb.st_mode;
3677 kif->kf_un.kf_file.kf_file_size = sb.st_size;
3681 static SYSCTL_NODE(_kern_proc, KERN_PROC_FILEDESC, filedesc, CTLFLAG_RD,
3682 sysctl_kern_proc_filedesc, "Process filedesc entries");
3686 * For the purposes of debugging, generate a human-readable string for the
3690 file_type_to_name(short type)
3720 * For the purposes of debugging, identify a process (if any, perhaps one of
3721 * many) that references the passed file in its file descriptor array. Return
3724 static struct proc *
3725 file_to_first_proc(struct file *fp)
3727 struct filedesc *fdp;
3731 FOREACH_PROC_IN_SYSTEM(p) {
3732 if (p->p_state == PRS_NEW)
3737 for (n = 0; n < fdp->fd_nfiles; n++) {
3738 if (fp == fdp->fd_ofiles[n].fde_file)
3746 db_print_file(struct file *fp, int header)
3751 db_printf("%8s %4s %8s %8s %4s %5s %6s %8s %5s %12s\n",
3752 "File", "Type", "Data", "Flag", "GCFl", "Count",
3753 "MCount", "Vnode", "FPID", "FCmd");
3754 p = file_to_first_proc(fp);
3755 db_printf("%8p %4s %8p %08x %04x %5d %6d %8p %5d %12s\n", fp,
3756 file_type_to_name(fp->f_type), fp->f_data, fp->f_flag,
3757 0, fp->f_count, 0, fp->f_vnode,
3758 p != NULL ? p->p_pid : -1, p != NULL ? p->p_comm : "-");
3761 DB_SHOW_COMMAND(file, db_show_file)
3766 db_printf("usage: show file <addr>\n");
3769 fp = (struct file *)addr;
3770 db_print_file(fp, 1);
3773 DB_SHOW_COMMAND(files, db_show_files)
3775 struct filedesc *fdp;
3782 FOREACH_PROC_IN_SYSTEM(p) {
3783 if (p->p_state == PRS_NEW)
3785 if ((fdp = p->p_fd) == NULL)
3787 for (n = 0; n < fdp->fd_nfiles; ++n) {
3788 if ((fp = fdp->fd_ofiles[n].fde_file) == NULL)
3790 db_print_file(fp, header);
3797 SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
3798 &maxfilesperproc, 0, "Maximum files allowed open per process");
3800 SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
3801 &maxfiles, 0, "Maximum number of files");
3803 SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD,
3804 __DEVOLATILE(int *, &openfiles), 0, "System-wide number of open files");
3808 filelistinit(void *dummy)
3811 file_zone = uma_zcreate("Files", sizeof(struct file), NULL, NULL,
3812 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
3813 mtx_init(&sigio_lock, "sigio lock", NULL, MTX_DEF);
3814 mtx_init(&fdesc_mtx, "fdesc", NULL, MTX_DEF);
3816 SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, filelistinit, NULL);
3818 /*-------------------------------------------------------------------*/
3821 badfo_readwrite(struct file *fp, struct uio *uio, struct ucred *active_cred,
3822 int flags, struct thread *td)
3829 badfo_truncate(struct file *fp, off_t length, struct ucred *active_cred,
3837 badfo_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred,
3845 badfo_poll(struct file *fp, int events, struct ucred *active_cred,
3853 badfo_kqfilter(struct file *fp, struct knote *kn)
3860 badfo_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
3868 badfo_close(struct file *fp, struct thread *td)
3875 badfo_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
3883 badfo_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
3891 badfo_sendfile(struct file *fp, int sockfd, struct uio *hdr_uio,
3892 struct uio *trl_uio, off_t offset, size_t nbytes, off_t *sent, int flags,
3893 int kflags, struct thread *td)
3899 struct fileops badfileops = {
3900 .fo_read = badfo_readwrite,
3901 .fo_write = badfo_readwrite,
3902 .fo_truncate = badfo_truncate,
3903 .fo_ioctl = badfo_ioctl,
3904 .fo_poll = badfo_poll,
3905 .fo_kqfilter = badfo_kqfilter,
3906 .fo_stat = badfo_stat,
3907 .fo_close = badfo_close,
3908 .fo_chmod = badfo_chmod,
3909 .fo_chown = badfo_chown,
3910 .fo_sendfile = badfo_sendfile,
3914 invfo_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
3922 invfo_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
3930 invfo_sendfile(struct file *fp, int sockfd, struct uio *hdr_uio,
3931 struct uio *trl_uio, off_t offset, size_t nbytes, off_t *sent, int flags,
3932 int kflags, struct thread *td)
3938 /*-------------------------------------------------------------------*/
3941 * File Descriptor pseudo-device driver (/dev/fd/).
3943 * Opening minor device N dup()s the file (if any) connected to file
3944 * descriptor N belonging to the calling process. Note that this driver
3945 * consists of only the ``open()'' routine, because all subsequent
3946 * references to this file will be direct to the other driver.
3948 * XXX: we could give this one a cloning event handler if necessary.
3953 fdopen(struct cdev *dev, int mode, int type, struct thread *td)
3957 * XXX Kludge: set curthread->td_dupfd to contain the value of the
3958 * the file descriptor being sought for duplication. The error
3959 * return ensures that the vnode for this device will be released
3960 * by vn_open. Open will detect this special error and take the
3961 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
3962 * will simply report the error.
3964 td->td_dupfd = dev2unit(dev);
3968 static struct cdevsw fildesc_cdevsw = {
3969 .d_version = D_VERSION,
3975 fildesc_drvinit(void *unused)
3979 dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 0, NULL,
3980 UID_ROOT, GID_WHEEL, 0666, "fd/0");
3981 make_dev_alias(dev, "stdin");
3982 dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 1, NULL,
3983 UID_ROOT, GID_WHEEL, 0666, "fd/1");
3984 make_dev_alias(dev, "stdout");
3985 dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 2, NULL,
3986 UID_ROOT, GID_WHEEL, 0666, "fd/2");
3987 make_dev_alias(dev, "stderr");
3990 SYSINIT(fildescdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, fildesc_drvinit, NULL);