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;
472 error = do_dup(td, DUP_FCNTL, fd, tmp, td->td_retval);
475 case F_DUPFD_CLOEXEC:
477 error = do_dup(td, DUP_FCNTL | DUP_CLOEXEC, fd, tmp,
483 error = do_dup(td, DUP_FIXED, fd, tmp, td->td_retval);
486 case F_DUP2FD_CLOEXEC:
488 error = do_dup(td, DUP_FIXED | DUP_CLOEXEC, fd, tmp,
494 if ((fp = fget_locked(fdp, fd)) == NULL) {
495 FILEDESC_SUNLOCK(fdp);
499 fde = &fdp->fd_ofiles[fd];
501 (fde->fde_flags & UF_EXCLOSE) ? FD_CLOEXEC : 0;
502 FILEDESC_SUNLOCK(fdp);
507 if ((fp = fget_locked(fdp, fd)) == NULL) {
508 FILEDESC_XUNLOCK(fdp);
512 fde = &fdp->fd_ofiles[fd];
513 fde->fde_flags = (fde->fde_flags & ~UF_EXCLOSE) |
514 (arg & FD_CLOEXEC ? UF_EXCLOSE : 0);
515 FILEDESC_XUNLOCK(fdp);
519 error = fget_unlocked(fdp, fd,
520 cap_rights_init(&rights, CAP_FCNTL), F_GETFL, &fp, NULL);
523 td->td_retval[0] = OFLAGS(fp->f_flag);
528 error = fget_unlocked(fdp, fd,
529 cap_rights_init(&rights, CAP_FCNTL), F_SETFL, &fp, NULL);
533 tmp = flg = fp->f_flag;
535 tmp |= FFLAGS(arg & ~O_ACCMODE) & FCNTLFLAGS;
536 } while(atomic_cmpset_int(&fp->f_flag, flg, tmp) == 0);
537 tmp = fp->f_flag & FNONBLOCK;
538 error = fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
543 tmp = fp->f_flag & FASYNC;
544 error = fo_ioctl(fp, FIOASYNC, &tmp, td->td_ucred, td);
549 atomic_clear_int(&fp->f_flag, FNONBLOCK);
551 (void)fo_ioctl(fp, FIONBIO, &tmp, td->td_ucred, td);
556 error = fget_unlocked(fdp, fd,
557 cap_rights_init(&rights, CAP_FCNTL), F_GETOWN, &fp, NULL);
560 error = fo_ioctl(fp, FIOGETOWN, &tmp, td->td_ucred, td);
562 td->td_retval[0] = tmp;
567 error = fget_unlocked(fdp, fd,
568 cap_rights_init(&rights, CAP_FCNTL), F_SETOWN, &fp, NULL);
572 error = fo_ioctl(fp, FIOSETOWN, &tmp, td->td_ucred, td);
577 error = priv_check(td, PRIV_NFS_LOCKD);
585 /* FALLTHROUGH F_SETLK */
589 cap_rights_init(&rights, CAP_FLOCK);
590 error = fget_unlocked(fdp, fd, &rights, 0, &fp, NULL);
593 if (fp->f_type != DTYPE_VNODE) {
599 flp = (struct flock *)arg;
600 if (flp->l_whence == SEEK_CUR) {
601 foffset = foffset_get(fp);
604 foffset > OFF_MAX - flp->l_start)) {
605 FILEDESC_SUNLOCK(fdp);
610 flp->l_start += foffset;
614 switch (flp->l_type) {
616 if ((fp->f_flag & FREAD) == 0) {
620 PROC_LOCK(p->p_leader);
621 p->p_leader->p_flag |= P_ADVLOCK;
622 PROC_UNLOCK(p->p_leader);
623 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
627 if ((fp->f_flag & FWRITE) == 0) {
631 PROC_LOCK(p->p_leader);
632 p->p_leader->p_flag |= P_ADVLOCK;
633 PROC_UNLOCK(p->p_leader);
634 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_SETLK,
638 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_UNLCK,
643 * Temporary api for testing remote lock
646 if (flg != F_REMOTE) {
650 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
651 F_UNLCKSYS, flp, flg);
657 if (error != 0 || flp->l_type == F_UNLCK ||
658 flp->l_type == F_UNLCKSYS) {
664 * Check for a race with close.
666 * The vnode is now advisory locked (or unlocked, but this case
667 * is not really important) as the caller requested.
668 * We had to drop the filedesc lock, so we need to recheck if
669 * the descriptor is still valid, because if it was closed
670 * in the meantime we need to remove advisory lock from the
671 * vnode - close on any descriptor leading to an advisory
672 * locked vnode, removes that lock.
673 * We will return 0 on purpose in that case, as the result of
674 * successful advisory lock might have been externally visible
675 * already. This is fine - effectively we pretend to the caller
676 * that the closing thread was a bit slower and that the
677 * advisory lock succeeded before the close.
679 error = fget_unlocked(fdp, fd, &rights, 0, &fp2, NULL);
685 flp->l_whence = SEEK_SET;
688 flp->l_type = F_UNLCK;
689 (void) VOP_ADVLOCK(vp, (caddr_t)p->p_leader,
690 F_UNLCK, flp, F_POSIX);
697 error = fget_unlocked(fdp, fd,
698 cap_rights_init(&rights, CAP_FLOCK), 0, &fp, NULL);
701 if (fp->f_type != DTYPE_VNODE) {
706 flp = (struct flock *)arg;
707 if (flp->l_type != F_RDLCK && flp->l_type != F_WRLCK &&
708 flp->l_type != F_UNLCK) {
713 if (flp->l_whence == SEEK_CUR) {
714 foffset = foffset_get(fp);
715 if ((flp->l_start > 0 &&
716 foffset > OFF_MAX - flp->l_start) ||
718 foffset < OFF_MIN - flp->l_start)) {
719 FILEDESC_SUNLOCK(fdp);
724 flp->l_start += foffset;
727 error = VOP_ADVLOCK(vp, (caddr_t)p->p_leader, F_GETLK, flp,
733 arg = arg ? 128 * 1024: 0;
736 error = fget_unlocked(fdp, fd, NULL, 0, &fp, NULL);
739 if (fp->f_type != DTYPE_VNODE) {
746 error = vn_lock(vp, LK_SHARED);
751 bsize = fp->f_vnode->v_mount->mnt_stat.f_iosize;
753 fp->f_seqcount = (arg + bsize - 1) / bsize;
755 new = old = fp->f_flag;
757 } while (!atomic_cmpset_rel_int(&fp->f_flag, old, new));
760 new = old = fp->f_flag;
762 } while (!atomic_cmpset_rel_int(&fp->f_flag, old, new));
775 * Common code for dup, dup2, fcntl(F_DUPFD) and fcntl(F_DUP2FD).
778 do_dup(struct thread *td, int flags, int old, int new,
781 struct filedesc *fdp;
782 struct filedescent *oldfde, *newfde;
792 * Verify we have a valid descriptor to dup from and possibly to
793 * dup to. Unlike dup() and dup2(), fcntl()'s F_DUPFD should
794 * return EINVAL when the new descriptor is out of bounds.
799 return (flags & DUP_FCNTL ? EINVAL : EBADF);
801 maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
804 return (flags & DUP_FCNTL ? EINVAL : EBADF);
807 if (fget_locked(fdp, old) == NULL) {
808 FILEDESC_XUNLOCK(fdp);
811 oldfde = &fdp->fd_ofiles[old];
812 if (flags & DUP_FIXED && old == new) {
814 if (flags & DUP_CLOEXEC)
815 fdp->fd_ofiles[new].fde_flags |= UF_EXCLOSE;
816 FILEDESC_XUNLOCK(fdp);
819 fp = oldfde->fde_file;
823 * If the caller specified a file descriptor, make sure the file
824 * table is large enough to hold it, and grab it. Otherwise, just
825 * allocate a new descriptor the usual way.
827 if (flags & DUP_FIXED) {
828 if (new >= fdp->fd_nfiles) {
830 * The resource limits are here instead of e.g.
831 * fdalloc(), because the file descriptor table may be
832 * shared between processes, so we can't really use
833 * racct_add()/racct_sub(). Instead of counting the
834 * number of actually allocated descriptors, just put
835 * the limit on the size of the file descriptor table.
839 error = racct_set(p, RACCT_NOFILE, new + 1);
842 FILEDESC_XUNLOCK(fdp);
847 fdgrowtable(fdp, new + 1);
848 oldfde = &fdp->fd_ofiles[old];
850 newfde = &fdp->fd_ofiles[new];
851 if (newfde->fde_file == NULL)
854 if ((error = fdalloc(td, new, &new)) != 0) {
855 FILEDESC_XUNLOCK(fdp);
859 newfde = &fdp->fd_ofiles[new];
862 KASSERT(fp == oldfde->fde_file, ("old fd has been modified"));
863 KASSERT(old != new, ("new fd is same as old"));
865 delfp = newfde->fde_file;
868 * Duplicate the source descriptor.
871 filecaps_copy(&oldfde->fde_caps, &newfde->fde_caps);
872 if ((flags & DUP_CLOEXEC) != 0)
873 newfde->fde_flags = oldfde->fde_flags | UF_EXCLOSE;
875 newfde->fde_flags = oldfde->fde_flags & ~UF_EXCLOSE;
876 if (new > fdp->fd_lastfile)
877 fdp->fd_lastfile = new;
881 (void) closefp(fdp, new, delfp, td, 1);
882 /* closefp() drops the FILEDESC lock for us. */
884 FILEDESC_XUNLOCK(fdp);
891 * If sigio is on the list associated with a process or process group,
892 * disable signalling from the device, remove sigio from the list and
896 funsetown(struct sigio **sigiop)
906 *(sigio->sio_myref) = NULL;
907 if ((sigio)->sio_pgid < 0) {
908 struct pgrp *pg = (sigio)->sio_pgrp;
910 SLIST_REMOVE(&sigio->sio_pgrp->pg_sigiolst, sigio,
914 struct proc *p = (sigio)->sio_proc;
916 SLIST_REMOVE(&sigio->sio_proc->p_sigiolst, sigio,
921 crfree(sigio->sio_ucred);
922 free(sigio, M_SIGIO);
926 * Free a list of sigio structures.
927 * We only need to lock the SIGIO_LOCK because we have made ourselves
928 * inaccessible to callers of fsetown and therefore do not need to lock
929 * the proc or pgrp struct for the list manipulation.
932 funsetownlst(struct sigiolst *sigiolst)
938 sigio = SLIST_FIRST(sigiolst);
945 * Every entry of the list should belong
946 * to a single proc or pgrp.
948 if (sigio->sio_pgid < 0) {
949 pg = sigio->sio_pgrp;
950 PGRP_LOCK_ASSERT(pg, MA_NOTOWNED);
951 } else /* if (sigio->sio_pgid > 0) */ {
953 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
957 while ((sigio = SLIST_FIRST(sigiolst)) != NULL) {
958 *(sigio->sio_myref) = NULL;
960 KASSERT(sigio->sio_pgid < 0,
961 ("Proc sigio in pgrp sigio list"));
962 KASSERT(sigio->sio_pgrp == pg,
963 ("Bogus pgrp in sigio list"));
965 SLIST_REMOVE(&pg->pg_sigiolst, sigio, sigio,
968 } else /* if (p != NULL) */ {
969 KASSERT(sigio->sio_pgid > 0,
970 ("Pgrp sigio in proc sigio list"));
971 KASSERT(sigio->sio_proc == p,
972 ("Bogus proc in sigio list"));
974 SLIST_REMOVE(&p->p_sigiolst, sigio, sigio,
979 crfree(sigio->sio_ucred);
980 free(sigio, M_SIGIO);
987 * This is common code for FIOSETOWN ioctl called by fcntl(fd, F_SETOWN, arg).
989 * After permission checking, add a sigio structure to the sigio list for
990 * the process or process group.
993 fsetown(pid_t pgid, struct sigio **sigiop)
1007 /* Allocate and fill in the new sigio out of locks. */
1008 sigio = malloc(sizeof(struct sigio), M_SIGIO, M_WAITOK);
1009 sigio->sio_pgid = pgid;
1010 sigio->sio_ucred = crhold(curthread->td_ucred);
1011 sigio->sio_myref = sigiop;
1013 sx_slock(&proctree_lock);
1022 * Policy - Don't allow a process to FSETOWN a process
1023 * in another session.
1025 * Remove this test to allow maximum flexibility or
1026 * restrict FSETOWN to the current process or process
1027 * group for maximum safety.
1030 if (proc->p_session != curthread->td_proc->p_session) {
1036 } else /* if (pgid < 0) */ {
1037 pgrp = pgfind(-pgid);
1045 * Policy - Don't allow a process to FSETOWN a process
1046 * in another session.
1048 * Remove this test to allow maximum flexibility or
1049 * restrict FSETOWN to the current process or process
1050 * group for maximum safety.
1052 if (pgrp->pg_session != curthread->td_proc->p_session) {
1063 * Since funsetownlst() is called without the proctree
1064 * locked, we need to check for P_WEXIT.
1065 * XXX: is ESRCH correct?
1067 if ((proc->p_flag & P_WEXIT) != 0) {
1072 SLIST_INSERT_HEAD(&proc->p_sigiolst, sigio, sio_pgsigio);
1073 sigio->sio_proc = proc;
1077 SLIST_INSERT_HEAD(&pgrp->pg_sigiolst, sigio, sio_pgsigio);
1078 sigio->sio_pgrp = pgrp;
1081 sx_sunlock(&proctree_lock);
1088 sx_sunlock(&proctree_lock);
1089 crfree(sigio->sio_ucred);
1090 free(sigio, M_SIGIO);
1095 * This is common code for FIOGETOWN ioctl called by fcntl(fd, F_GETOWN, arg).
1099 struct sigio **sigiop;
1104 pgid = (*sigiop != NULL) ? (*sigiop)->sio_pgid : 0;
1110 * Function drops the filedesc lock on return.
1113 closefp(struct filedesc *fdp, int fd, struct file *fp, struct thread *td,
1118 FILEDESC_XLOCK_ASSERT(fdp);
1121 if (td->td_proc->p_fdtol != NULL) {
1123 * Ask fdfree() to sleep to ensure that all relevant
1124 * process leaders can be traversed in closef().
1126 fdp->fd_holdleaderscount++;
1133 * We now hold the fp reference that used to be owned by the
1134 * descriptor array. We have to unlock the FILEDESC *AFTER*
1135 * knote_fdclose to prevent a race of the fd getting opened, a knote
1136 * added, and deleteing a knote for the new fd.
1138 knote_fdclose(td, fd);
1141 * We need to notify mqueue if the object is of type mqueue.
1143 if (fp->f_type == DTYPE_MQUEUE)
1144 mq_fdclose(td, fd, fp);
1145 FILEDESC_XUNLOCK(fdp);
1147 error = closef(fp, td);
1149 FILEDESC_XLOCK(fdp);
1150 fdp->fd_holdleaderscount--;
1151 if (fdp->fd_holdleaderscount == 0 &&
1152 fdp->fd_holdleaderswakeup != 0) {
1153 fdp->fd_holdleaderswakeup = 0;
1154 wakeup(&fdp->fd_holdleaderscount);
1156 FILEDESC_XUNLOCK(fdp);
1162 * Close a file descriptor.
1164 #ifndef _SYS_SYSPROTO_H_
1173 struct close_args *uap;
1176 return (kern_close(td, uap->fd));
1184 struct filedesc *fdp;
1187 fdp = td->td_proc->p_fd;
1189 AUDIT_SYSCLOSE(td, fd);
1191 FILEDESC_XLOCK(fdp);
1192 if ((fp = fget_locked(fdp, fd)) == NULL) {
1193 FILEDESC_XUNLOCK(fdp);
1198 /* closefp() drops the FILEDESC lock for us. */
1199 return (closefp(fdp, fd, fp, td, 1));
1203 * Close open file descriptors.
1205 #ifndef _SYS_SYSPROTO_H_
1206 struct closefrom_args {
1212 sys_closefrom(struct thread *td, struct closefrom_args *uap)
1214 struct filedesc *fdp;
1217 fdp = td->td_proc->p_fd;
1218 AUDIT_ARG_FD(uap->lowfd);
1221 * Treat negative starting file descriptor values identical to
1222 * closefrom(0) which closes all files.
1226 FILEDESC_SLOCK(fdp);
1227 for (fd = uap->lowfd; fd < fdp->fd_nfiles; fd++) {
1228 if (fdp->fd_ofiles[fd].fde_file != NULL) {
1229 FILEDESC_SUNLOCK(fdp);
1230 (void)kern_close(td, fd);
1231 FILEDESC_SLOCK(fdp);
1234 FILEDESC_SUNLOCK(fdp);
1238 #if defined(COMPAT_43)
1240 * Return status information about a file descriptor.
1242 #ifndef _SYS_SYSPROTO_H_
1243 struct ofstat_args {
1250 ofstat(struct thread *td, struct ofstat_args *uap)
1256 error = kern_fstat(td, uap->fd, &ub);
1259 error = copyout(&oub, uap->sb, sizeof(oub));
1263 #endif /* COMPAT_43 */
1266 * Return status information about a file descriptor.
1268 #ifndef _SYS_SYSPROTO_H_
1276 sys_fstat(struct thread *td, struct fstat_args *uap)
1281 error = kern_fstat(td, uap->fd, &ub);
1283 error = copyout(&ub, uap->sb, sizeof(ub));
1288 kern_fstat(struct thread *td, int fd, struct stat *sbp)
1291 cap_rights_t rights;
1296 error = fget(td, fd, cap_rights_init(&rights, CAP_FSTAT), &fp);
1300 AUDIT_ARG_FILE(td->td_proc, fp);
1302 error = fo_stat(fp, sbp, td->td_ucred, td);
1305 if (error == 0 && KTRPOINT(td, KTR_STRUCT))
1312 * Return status information about a file descriptor.
1314 #ifndef _SYS_SYSPROTO_H_
1315 struct nfstat_args {
1322 sys_nfstat(struct thread *td, struct nfstat_args *uap)
1328 error = kern_fstat(td, uap->fd, &ub);
1330 cvtnstat(&ub, &nub);
1331 error = copyout(&nub, uap->sb, sizeof(nub));
1337 * Return pathconf information about a file descriptor.
1339 #ifndef _SYS_SYSPROTO_H_
1340 struct fpathconf_args {
1347 sys_fpathconf(struct thread *td, struct fpathconf_args *uap)
1351 cap_rights_t rights;
1354 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FPATHCONF), &fp);
1358 /* If asynchronous I/O is available, it works for all descriptors. */
1359 if (uap->name == _PC_ASYNC_IO) {
1360 td->td_retval[0] = async_io_version;
1365 vn_lock(vp, LK_SHARED | LK_RETRY);
1366 error = VOP_PATHCONF(vp, uap->name, td->td_retval);
1368 } else if (fp->f_type == DTYPE_PIPE || fp->f_type == DTYPE_SOCKET) {
1369 if (uap->name != _PC_PIPE_BUF) {
1372 td->td_retval[0] = PIPE_BUF;
1384 * Initialize filecaps structure.
1387 filecaps_init(struct filecaps *fcaps)
1390 bzero(fcaps, sizeof(*fcaps));
1391 fcaps->fc_nioctls = -1;
1395 * Copy filecaps structure allocating memory for ioctls array if needed.
1398 filecaps_copy(const struct filecaps *src, struct filecaps *dst)
1403 if (src->fc_ioctls != NULL) {
1404 KASSERT(src->fc_nioctls > 0,
1405 ("fc_ioctls != NULL, but fc_nioctls=%hd", src->fc_nioctls));
1407 size = sizeof(src->fc_ioctls[0]) * src->fc_nioctls;
1408 dst->fc_ioctls = malloc(size, M_FILECAPS, M_WAITOK);
1409 bcopy(src->fc_ioctls, dst->fc_ioctls, size);
1414 * Move filecaps structure to the new place and clear the old place.
1417 filecaps_move(struct filecaps *src, struct filecaps *dst)
1421 bzero(src, sizeof(*src));
1425 * Fill the given filecaps structure with full rights.
1428 filecaps_fill(struct filecaps *fcaps)
1431 CAP_ALL(&fcaps->fc_rights);
1432 fcaps->fc_ioctls = NULL;
1433 fcaps->fc_nioctls = -1;
1434 fcaps->fc_fcntls = CAP_FCNTL_ALL;
1438 * Free memory allocated within filecaps structure.
1441 filecaps_free(struct filecaps *fcaps)
1444 free(fcaps->fc_ioctls, M_FILECAPS);
1445 bzero(fcaps, sizeof(*fcaps));
1449 * Validate the given filecaps structure.
1452 filecaps_validate(const struct filecaps *fcaps, const char *func)
1455 KASSERT(cap_rights_is_valid(&fcaps->fc_rights),
1456 ("%s: invalid rights", func));
1457 KASSERT((fcaps->fc_fcntls & ~CAP_FCNTL_ALL) == 0,
1458 ("%s: invalid fcntls", func));
1459 KASSERT(fcaps->fc_fcntls == 0 ||
1460 cap_rights_is_set(&fcaps->fc_rights, CAP_FCNTL),
1461 ("%s: fcntls without CAP_FCNTL", func));
1462 KASSERT(fcaps->fc_ioctls != NULL ? fcaps->fc_nioctls > 0 :
1463 (fcaps->fc_nioctls == -1 || fcaps->fc_nioctls == 0),
1464 ("%s: invalid ioctls", func));
1465 KASSERT(fcaps->fc_nioctls == 0 ||
1466 cap_rights_is_set(&fcaps->fc_rights, CAP_IOCTL),
1467 ("%s: ioctls without CAP_IOCTL", func));
1471 * Grow the file table to accomodate (at least) nfd descriptors.
1474 fdgrowtable(struct filedesc *fdp, int nfd)
1476 struct filedesc0 *fdp0;
1477 struct freetable *ft;
1478 struct filedescent *ntable;
1479 struct filedescent *otable;
1480 int nnfiles, onfiles;
1481 NDSLOTTYPE *nmap, *omap;
1483 FILEDESC_XLOCK_ASSERT(fdp);
1485 KASSERT(fdp->fd_nfiles > 0, ("zero-length file table"));
1487 /* save old values */
1488 onfiles = fdp->fd_nfiles;
1489 otable = fdp->fd_ofiles;
1492 /* compute the size of the new table */
1493 nnfiles = NDSLOTS(nfd) * NDENTRIES; /* round up */
1494 if (nnfiles <= onfiles)
1495 /* the table is already large enough */
1499 * Allocate a new table and map. We need enough space for the
1500 * file entries themselves and the struct freetable we will use
1501 * when we decommission the table and place it on the freelist.
1502 * We place the struct freetable in the middle so we don't have
1503 * to worry about padding.
1505 ntable = malloc(nnfiles * sizeof(ntable[0]) + sizeof(struct freetable),
1506 M_FILEDESC, M_ZERO | M_WAITOK);
1507 nmap = malloc(NDSLOTS(nnfiles) * NDSLOTSIZE, M_FILEDESC,
1510 /* copy the old data over and point at the new tables */
1511 memcpy(ntable, otable, onfiles * sizeof(*otable));
1512 memcpy(nmap, omap, NDSLOTS(onfiles) * sizeof(*omap));
1514 /* update the pointers and counters */
1515 fdp->fd_nfiles = nnfiles;
1516 memcpy(ntable, otable, onfiles * sizeof(ntable[0]));
1517 fdp->fd_ofiles = ntable;
1521 * Do not free the old file table, as some threads may still
1522 * reference entries within it. Instead, place it on a freelist
1523 * which will be processed when the struct filedesc is released.
1525 * Do, however, free the old map.
1527 * Note that if onfiles == NDFILE, we're dealing with the original
1528 * static allocation contained within (struct filedesc0 *)fdp,
1529 * which must not be freed.
1531 if (onfiles > NDFILE) {
1532 ft = (struct freetable *)&otable[onfiles];
1533 fdp0 = (struct filedesc0 *)fdp;
1534 ft->ft_table = otable;
1535 SLIST_INSERT_HEAD(&fdp0->fd_free, ft, ft_next);
1536 free(omap, M_FILEDESC);
1541 * Allocate a file descriptor for the process.
1544 fdalloc(struct thread *td, int minfd, int *result)
1546 struct proc *p = td->td_proc;
1547 struct filedesc *fdp = p->p_fd;
1548 int fd = -1, maxfd, allocfd;
1553 FILEDESC_XLOCK_ASSERT(fdp);
1555 if (fdp->fd_freefile > minfd)
1556 minfd = fdp->fd_freefile;
1559 maxfd = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
1563 * Search the bitmap for a free descriptor starting at minfd.
1564 * If none is found, grow the file table.
1566 fd = fd_first_free(fdp, minfd, fdp->fd_nfiles);
1569 if (fd >= fdp->fd_nfiles) {
1570 allocfd = min(fd * 2, maxfd);
1573 error = racct_set(p, RACCT_NOFILE, allocfd);
1579 * fd is already equal to first free descriptor >= minfd, so
1580 * we only need to grow the table and we are done.
1582 fdgrowtable(fdp, allocfd);
1586 * Perform some sanity checks, then mark the file descriptor as
1587 * used and return it to the caller.
1589 KASSERT(fd >= 0 && fd < min(maxfd, fdp->fd_nfiles),
1590 ("invalid descriptor %d", fd));
1591 KASSERT(!fdisused(fdp, fd),
1592 ("fd_first_free() returned non-free descriptor"));
1593 KASSERT(fdp->fd_ofiles[fd].fde_file == NULL,
1594 ("file descriptor isn't free"));
1595 KASSERT(fdp->fd_ofiles[fd].fde_flags == 0, ("file flags are set"));
1602 * Allocate n file descriptors for the process.
1605 fdallocn(struct thread *td, int minfd, int *fds, int n)
1607 struct proc *p = td->td_proc;
1608 struct filedesc *fdp = p->p_fd;
1611 FILEDESC_XLOCK_ASSERT(fdp);
1613 if (!fdavail(td, n))
1616 for (i = 0; i < n; i++)
1617 if (fdalloc(td, 0, &fds[i]) != 0)
1621 for (i--; i >= 0; i--)
1622 fdunused(fdp, fds[i]);
1630 * Check to see whether n user file descriptors are available to the process
1634 fdavail(struct thread *td, int n)
1636 struct proc *p = td->td_proc;
1637 struct filedesc *fdp = td->td_proc->p_fd;
1640 FILEDESC_LOCK_ASSERT(fdp);
1643 * XXX: This is only called from uipc_usrreq.c:unp_externalize();
1644 * call racct_add() from there instead of dealing with containers
1648 lim = min((int)lim_cur(p, RLIMIT_NOFILE), maxfilesperproc);
1650 if ((i = lim - fdp->fd_nfiles) > 0 && (n -= i) <= 0)
1652 last = min(fdp->fd_nfiles, lim);
1653 for (i = fdp->fd_freefile; i < last; i++) {
1654 if (fdp->fd_ofiles[i].fde_file == NULL && --n <= 0)
1661 * Create a new open file structure and allocate a file decriptor for the
1662 * process that refers to it. We add one reference to the file for the
1663 * descriptor table and one reference for resultfp. This is to prevent us
1664 * being preempted and the entry in the descriptor table closed after we
1665 * release the FILEDESC lock.
1668 falloc(struct thread *td, struct file **resultfp, int *resultfd, int flags)
1673 error = falloc_noinstall(td, &fp);
1675 return (error); /* no reference held on error */
1677 error = finstall(td, fp, &fd, flags, NULL);
1679 fdrop(fp, td); /* one reference (fp only) */
1683 if (resultfp != NULL)
1684 *resultfp = fp; /* copy out result */
1686 fdrop(fp, td); /* release local reference */
1688 if (resultfd != NULL)
1695 * Create a new open file structure without allocating a file descriptor.
1698 falloc_noinstall(struct thread *td, struct file **resultfp)
1701 int maxuserfiles = maxfiles - (maxfiles / 20);
1702 static struct timeval lastfail;
1705 KASSERT(resultfp != NULL, ("%s: resultfp == NULL", __func__));
1707 if ((openfiles >= maxuserfiles &&
1708 priv_check(td, PRIV_MAXFILES) != 0) ||
1709 openfiles >= maxfiles) {
1710 if (ppsratecheck(&lastfail, &curfail, 1)) {
1711 printf("kern.maxfiles limit exceeded by uid %i, "
1712 "please see tuning(7).\n", td->td_ucred->cr_ruid);
1716 atomic_add_int(&openfiles, 1);
1717 fp = uma_zalloc(file_zone, M_WAITOK | M_ZERO);
1718 refcount_init(&fp->f_count, 1);
1719 fp->f_cred = crhold(td->td_ucred);
1720 fp->f_ops = &badfileops;
1728 * Install a file in a file descriptor table.
1731 finstall(struct thread *td, struct file *fp, int *fd, int flags,
1732 struct filecaps *fcaps)
1734 struct filedesc *fdp = td->td_proc->p_fd;
1735 struct filedescent *fde;
1738 KASSERT(fd != NULL, ("%s: fd == NULL", __func__));
1739 KASSERT(fp != NULL, ("%s: fp == NULL", __func__));
1741 filecaps_validate(fcaps, __func__);
1743 FILEDESC_XLOCK(fdp);
1744 if ((error = fdalloc(td, 0, fd))) {
1745 FILEDESC_XUNLOCK(fdp);
1749 fde = &fdp->fd_ofiles[*fd];
1751 if ((flags & O_CLOEXEC) != 0)
1752 fde->fde_flags |= UF_EXCLOSE;
1754 filecaps_move(fcaps, &fde->fde_caps);
1756 filecaps_fill(&fde->fde_caps);
1757 FILEDESC_XUNLOCK(fdp);
1762 * Build a new filedesc structure from another.
1763 * Copy the current, root, and jail root vnode references.
1766 fdinit(struct filedesc *fdp)
1768 struct filedesc0 *newfdp;
1770 newfdp = malloc(sizeof *newfdp, M_FILEDESC, M_WAITOK | M_ZERO);
1771 FILEDESC_LOCK_INIT(&newfdp->fd_fd);
1773 FILEDESC_XLOCK(fdp);
1774 newfdp->fd_fd.fd_cdir = fdp->fd_cdir;
1775 if (newfdp->fd_fd.fd_cdir)
1776 VREF(newfdp->fd_fd.fd_cdir);
1777 newfdp->fd_fd.fd_rdir = fdp->fd_rdir;
1778 if (newfdp->fd_fd.fd_rdir)
1779 VREF(newfdp->fd_fd.fd_rdir);
1780 newfdp->fd_fd.fd_jdir = fdp->fd_jdir;
1781 if (newfdp->fd_fd.fd_jdir)
1782 VREF(newfdp->fd_fd.fd_jdir);
1783 FILEDESC_XUNLOCK(fdp);
1786 /* Create the file descriptor table. */
1787 newfdp->fd_fd.fd_refcnt = 1;
1788 newfdp->fd_fd.fd_holdcnt = 1;
1789 newfdp->fd_fd.fd_cmask = CMASK;
1790 newfdp->fd_fd.fd_ofiles = newfdp->fd_dfiles;
1791 newfdp->fd_fd.fd_nfiles = NDFILE;
1792 newfdp->fd_fd.fd_map = newfdp->fd_dmap;
1793 newfdp->fd_fd.fd_lastfile = -1;
1794 return (&newfdp->fd_fd);
1797 static struct filedesc *
1798 fdhold(struct proc *p)
1800 struct filedesc *fdp;
1802 mtx_lock(&fdesc_mtx);
1806 mtx_unlock(&fdesc_mtx);
1811 fddrop(struct filedesc *fdp)
1813 struct filedesc0 *fdp0;
1814 struct freetable *ft;
1817 mtx_lock(&fdesc_mtx);
1818 i = --fdp->fd_holdcnt;
1819 mtx_unlock(&fdesc_mtx);
1823 FILEDESC_LOCK_DESTROY(fdp);
1824 fdp0 = (struct filedesc0 *)fdp;
1825 while ((ft = SLIST_FIRST(&fdp0->fd_free)) != NULL) {
1826 SLIST_REMOVE_HEAD(&fdp0->fd_free, ft_next);
1827 free(ft->ft_table, M_FILEDESC);
1829 free(fdp, M_FILEDESC);
1833 * Share a filedesc structure.
1836 fdshare(struct filedesc *fdp)
1839 FILEDESC_XLOCK(fdp);
1841 FILEDESC_XUNLOCK(fdp);
1846 * Unshare a filedesc structure, if necessary by making a copy
1849 fdunshare(struct proc *p, struct thread *td)
1852 FILEDESC_XLOCK(p->p_fd);
1853 if (p->p_fd->fd_refcnt > 1) {
1854 struct filedesc *tmp;
1856 FILEDESC_XUNLOCK(p->p_fd);
1857 tmp = fdcopy(p->p_fd);
1861 FILEDESC_XUNLOCK(p->p_fd);
1865 * Copy a filedesc structure. A NULL pointer in returns a NULL reference,
1866 * this is to ease callers, not catch errors.
1869 fdcopy(struct filedesc *fdp)
1871 struct filedesc *newfdp;
1872 struct filedescent *nfde, *ofde;
1875 /* Certain daemons might not have file descriptors. */
1879 newfdp = fdinit(fdp);
1880 FILEDESC_SLOCK(fdp);
1881 while (fdp->fd_lastfile >= newfdp->fd_nfiles) {
1882 FILEDESC_SUNLOCK(fdp);
1883 FILEDESC_XLOCK(newfdp);
1884 fdgrowtable(newfdp, fdp->fd_lastfile + 1);
1885 FILEDESC_XUNLOCK(newfdp);
1886 FILEDESC_SLOCK(fdp);
1888 /* copy all passable descriptors (i.e. not kqueue) */
1889 newfdp->fd_freefile = -1;
1890 for (i = 0; i <= fdp->fd_lastfile; ++i) {
1891 ofde = &fdp->fd_ofiles[i];
1892 if (fdisused(fdp, i) &&
1893 (ofde->fde_file->f_ops->fo_flags & DFLAG_PASSABLE) &&
1894 ofde->fde_file->f_ops != &badfileops) {
1895 nfde = &newfdp->fd_ofiles[i];
1897 filecaps_copy(&ofde->fde_caps, &nfde->fde_caps);
1898 fhold(nfde->fde_file);
1899 newfdp->fd_lastfile = i;
1901 if (newfdp->fd_freefile == -1)
1902 newfdp->fd_freefile = i;
1905 newfdp->fd_cmask = fdp->fd_cmask;
1906 FILEDESC_SUNLOCK(fdp);
1907 FILEDESC_XLOCK(newfdp);
1908 for (i = 0; i <= newfdp->fd_lastfile; ++i) {
1909 if (newfdp->fd_ofiles[i].fde_file != NULL)
1912 if (newfdp->fd_freefile == -1)
1913 newfdp->fd_freefile = i;
1914 FILEDESC_XUNLOCK(newfdp);
1919 * Release a filedesc structure.
1922 fdescfree(struct thread *td)
1924 struct filedesc *fdp;
1926 struct filedesc_to_leader *fdtol;
1928 struct vnode *cdir, *jdir, *rdir, *vp;
1931 /* Certain daemons might not have file descriptors. */
1932 fdp = td->td_proc->p_fd;
1937 PROC_LOCK(td->td_proc);
1938 racct_set(td->td_proc, RACCT_NOFILE, 0);
1939 PROC_UNLOCK(td->td_proc);
1942 /* Check for special need to clear POSIX style locks */
1943 fdtol = td->td_proc->p_fdtol;
1944 if (fdtol != NULL) {
1945 FILEDESC_XLOCK(fdp);
1946 KASSERT(fdtol->fdl_refcount > 0,
1947 ("filedesc_to_refcount botch: fdl_refcount=%d",
1948 fdtol->fdl_refcount));
1949 if (fdtol->fdl_refcount == 1 &&
1950 (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1951 for (i = 0; i <= fdp->fd_lastfile; i++) {
1952 fp = fdp->fd_ofiles[i].fde_file;
1953 if (fp == NULL || fp->f_type != DTYPE_VNODE)
1956 FILEDESC_XUNLOCK(fdp);
1957 lf.l_whence = SEEK_SET;
1960 lf.l_type = F_UNLCK;
1962 (void) VOP_ADVLOCK(vp,
1963 (caddr_t)td->td_proc->p_leader, F_UNLCK,
1965 FILEDESC_XLOCK(fdp);
1970 if (fdtol->fdl_refcount == 1) {
1971 if (fdp->fd_holdleaderscount > 0 &&
1972 (td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
1974 * close() or do_dup() has cleared a reference
1975 * in a shared file descriptor table.
1977 fdp->fd_holdleaderswakeup = 1;
1978 sx_sleep(&fdp->fd_holdleaderscount,
1979 FILEDESC_LOCK(fdp), PLOCK, "fdlhold", 0);
1982 if (fdtol->fdl_holdcount > 0) {
1984 * Ensure that fdtol->fdl_leader remains
1985 * valid in closef().
1987 fdtol->fdl_wakeup = 1;
1988 sx_sleep(fdtol, FILEDESC_LOCK(fdp), PLOCK,
1993 fdtol->fdl_refcount--;
1994 if (fdtol->fdl_refcount == 0 &&
1995 fdtol->fdl_holdcount == 0) {
1996 fdtol->fdl_next->fdl_prev = fdtol->fdl_prev;
1997 fdtol->fdl_prev->fdl_next = fdtol->fdl_next;
2000 td->td_proc->p_fdtol = NULL;
2001 FILEDESC_XUNLOCK(fdp);
2003 free(fdtol, M_FILEDESC_TO_LEADER);
2005 FILEDESC_XLOCK(fdp);
2006 i = --fdp->fd_refcnt;
2007 FILEDESC_XUNLOCK(fdp);
2011 for (i = 0; i <= fdp->fd_lastfile; i++) {
2012 fp = fdp->fd_ofiles[i].fde_file;
2014 FILEDESC_XLOCK(fdp);
2016 FILEDESC_XUNLOCK(fdp);
2017 (void) closef(fp, td);
2020 FILEDESC_XLOCK(fdp);
2022 /* XXX This should happen earlier. */
2023 mtx_lock(&fdesc_mtx);
2024 td->td_proc->p_fd = NULL;
2025 mtx_unlock(&fdesc_mtx);
2027 if (fdp->fd_nfiles > NDFILE)
2028 free(fdp->fd_ofiles, M_FILEDESC);
2029 if (NDSLOTS(fdp->fd_nfiles) > NDSLOTS(NDFILE))
2030 free(fdp->fd_map, M_FILEDESC);
2034 cdir = fdp->fd_cdir;
2035 fdp->fd_cdir = NULL;
2036 rdir = fdp->fd_rdir;
2037 fdp->fd_rdir = NULL;
2038 jdir = fdp->fd_jdir;
2039 fdp->fd_jdir = NULL;
2040 FILEDESC_XUNLOCK(fdp);
2053 * For setugid programs, we don't want to people to use that setugidness
2054 * to generate error messages which write to a file which otherwise would
2055 * otherwise be off-limits to the process. We check for filesystems where
2056 * the vnode can change out from under us after execve (like [lin]procfs).
2058 * Since setugidsafety calls this only for fd 0, 1 and 2, this check is
2059 * sufficient. We also don't check for setugidness since we know we are.
2062 is_unsafe(struct file *fp)
2064 if (fp->f_type == DTYPE_VNODE) {
2065 struct vnode *vp = fp->f_vnode;
2067 if ((vp->v_vflag & VV_PROCDEP) != 0)
2074 * Make this setguid thing safe, if at all possible.
2077 setugidsafety(struct thread *td)
2079 struct filedesc *fdp;
2083 /* Certain daemons might not have file descriptors. */
2084 fdp = td->td_proc->p_fd;
2089 * Note: fdp->fd_ofiles may be reallocated out from under us while
2090 * we are blocked in a close. Be careful!
2092 FILEDESC_XLOCK(fdp);
2093 for (i = 0; i <= fdp->fd_lastfile; i++) {
2096 fp = fdp->fd_ofiles[i].fde_file;
2097 if (fp != NULL && is_unsafe(fp)) {
2098 knote_fdclose(td, i);
2100 * NULL-out descriptor prior to close to avoid
2101 * a race while close blocks.
2104 FILEDESC_XUNLOCK(fdp);
2105 (void) closef(fp, td);
2106 FILEDESC_XLOCK(fdp);
2109 FILEDESC_XUNLOCK(fdp);
2113 * If a specific file object occupies a specific file descriptor, close the
2114 * file descriptor entry and drop a reference on the file object. This is a
2115 * convenience function to handle a subsequent error in a function that calls
2116 * falloc() that handles the race that another thread might have closed the
2117 * file descriptor out from under the thread creating the file object.
2120 fdclose(struct filedesc *fdp, struct file *fp, int idx, struct thread *td)
2123 FILEDESC_XLOCK(fdp);
2124 if (fdp->fd_ofiles[idx].fde_file == fp) {
2126 FILEDESC_XUNLOCK(fdp);
2129 FILEDESC_XUNLOCK(fdp);
2133 * Close any files on exec?
2136 fdcloseexec(struct thread *td)
2138 struct filedesc *fdp;
2139 struct filedescent *fde;
2143 /* Certain daemons might not have file descriptors. */
2144 fdp = td->td_proc->p_fd;
2149 * We cannot cache fd_ofiles since operations
2150 * may block and rip them out from under us.
2152 FILEDESC_XLOCK(fdp);
2153 for (i = 0; i <= fdp->fd_lastfile; i++) {
2154 fde = &fdp->fd_ofiles[i];
2156 if (fp != NULL && (fp->f_type == DTYPE_MQUEUE ||
2157 (fde->fde_flags & UF_EXCLOSE))) {
2159 (void) closefp(fdp, i, fp, td, 0);
2160 /* closefp() drops the FILEDESC lock. */
2161 FILEDESC_XLOCK(fdp);
2164 FILEDESC_XUNLOCK(fdp);
2168 * It is unsafe for set[ug]id processes to be started with file
2169 * descriptors 0..2 closed, as these descriptors are given implicit
2170 * significance in the Standard C library. fdcheckstd() will create a
2171 * descriptor referencing /dev/null for each of stdin, stdout, and
2172 * stderr that is not already open.
2175 fdcheckstd(struct thread *td)
2177 struct filedesc *fdp;
2178 register_t retval, save;
2179 int i, error, devnull;
2181 fdp = td->td_proc->p_fd;
2184 KASSERT(fdp->fd_refcnt == 1, ("the fdtable should not be shared"));
2187 for (i = 0; i < 3; i++) {
2188 if (fdp->fd_ofiles[i].fde_file != NULL)
2191 save = td->td_retval[0];
2192 error = kern_open(td, "/dev/null", UIO_SYSSPACE,
2194 devnull = td->td_retval[0];
2195 td->td_retval[0] = save;
2198 KASSERT(devnull == i, ("oof, we didn't get our fd"));
2200 error = do_dup(td, DUP_FIXED, devnull, i, &retval);
2209 * Internal form of close. Decrement reference count on file structure.
2210 * Note: td may be NULL when closing a file that was being passed in a
2213 * XXXRW: Giant is not required for the caller, but often will be held; this
2214 * makes it moderately likely the Giant will be recursed in the VFS case.
2217 closef(struct file *fp, struct thread *td)
2221 struct filedesc_to_leader *fdtol;
2222 struct filedesc *fdp;
2225 * POSIX record locking dictates that any close releases ALL
2226 * locks owned by this process. This is handled by setting
2227 * a flag in the unlock to free ONLY locks obeying POSIX
2228 * semantics, and not to free BSD-style file locks.
2229 * If the descriptor was in a message, POSIX-style locks
2230 * aren't passed with the descriptor, and the thread pointer
2231 * will be NULL. Callers should be careful only to pass a
2232 * NULL thread pointer when there really is no owning
2233 * context that might have locks, or the locks will be
2236 if (fp->f_type == DTYPE_VNODE && td != NULL) {
2238 if ((td->td_proc->p_leader->p_flag & P_ADVLOCK) != 0) {
2239 lf.l_whence = SEEK_SET;
2242 lf.l_type = F_UNLCK;
2243 (void) VOP_ADVLOCK(vp, (caddr_t)td->td_proc->p_leader,
2244 F_UNLCK, &lf, F_POSIX);
2246 fdtol = td->td_proc->p_fdtol;
2247 if (fdtol != NULL) {
2249 * Handle special case where file descriptor table is
2250 * shared between multiple process leaders.
2252 fdp = td->td_proc->p_fd;
2253 FILEDESC_XLOCK(fdp);
2254 for (fdtol = fdtol->fdl_next;
2255 fdtol != td->td_proc->p_fdtol;
2256 fdtol = fdtol->fdl_next) {
2257 if ((fdtol->fdl_leader->p_flag &
2260 fdtol->fdl_holdcount++;
2261 FILEDESC_XUNLOCK(fdp);
2262 lf.l_whence = SEEK_SET;
2265 lf.l_type = F_UNLCK;
2267 (void) VOP_ADVLOCK(vp,
2268 (caddr_t)fdtol->fdl_leader, F_UNLCK, &lf,
2270 FILEDESC_XLOCK(fdp);
2271 fdtol->fdl_holdcount--;
2272 if (fdtol->fdl_holdcount == 0 &&
2273 fdtol->fdl_wakeup != 0) {
2274 fdtol->fdl_wakeup = 0;
2278 FILEDESC_XUNLOCK(fdp);
2281 return (fdrop(fp, td));
2285 * Initialize the file pointer with the specified properties.
2287 * The ops are set with release semantics to be certain that the flags, type,
2288 * and data are visible when ops is. This is to prevent ops methods from being
2289 * called with bad data.
2292 finit(struct file *fp, u_int flag, short type, void *data, struct fileops *ops)
2297 atomic_store_rel_ptr((volatile uintptr_t *)&fp->f_ops, (uintptr_t)ops);
2301 fget_unlocked(struct filedesc *fdp, int fd, cap_rights_t *needrightsp,
2302 int needfcntl, struct file **fpp, cap_rights_t *haverightsp)
2307 cap_rights_t haverights;
2311 if (fd < 0 || fd >= fdp->fd_nfiles)
2314 * Fetch the descriptor locklessly. We avoid fdrop() races by
2315 * never raising a refcount above 0. To accomplish this we have
2316 * to use a cmpset loop rather than an atomic_add. The descriptor
2317 * must be re-verified once we acquire a reference to be certain
2318 * that the identity is still correct and we did not lose a race
2319 * due to preemption.
2322 fp = fdp->fd_ofiles[fd].fde_file;
2326 haverights = *cap_rights(fdp, fd);
2327 if (needrightsp != NULL) {
2328 error = cap_check(&haverights, needrightsp);
2331 if (cap_rights_is_set(needrightsp, CAP_FCNTL)) {
2332 error = cap_fcntl_check(fdp, fd, needfcntl);
2338 count = fp->f_count;
2342 * Use an acquire barrier to prevent caching of fd_ofiles
2343 * so it is refreshed for verification.
2345 if (atomic_cmpset_acq_int(&fp->f_count, count, count + 1) != 1)
2347 if (fp == fdp->fd_ofiles[fd].fde_file)
2349 fdrop(fp, curthread);
2352 if (haverightsp != NULL) {
2354 *haverightsp = haverights;
2356 CAP_ALL(haverightsp);
2363 * Extract the file pointer associated with the specified descriptor for the
2364 * current user process.
2366 * If the descriptor doesn't exist or doesn't match 'flags', EBADF is
2369 * File's rights will be checked against the capability rights mask.
2371 * If an error occured the non-zero error is returned and *fpp is set to
2372 * NULL. Otherwise *fpp is held and set and zero is returned. Caller is
2373 * responsible for fdrop().
2376 _fget(struct thread *td, int fd, struct file **fpp, int flags,
2377 cap_rights_t *needrightsp, u_char *maxprotp)
2379 struct filedesc *fdp;
2381 cap_rights_t haverights, needrights;
2385 if (td == NULL || (fdp = td->td_proc->p_fd) == NULL)
2387 if (needrightsp != NULL)
2388 needrights = *needrightsp;
2390 cap_rights_init(&needrights);
2391 if (maxprotp != NULL)
2392 cap_rights_set(&needrights, CAP_MMAP);
2393 error = fget_unlocked(fdp, fd, &needrights, 0, &fp, &haverights);
2396 if (fp->f_ops == &badfileops) {
2403 * If requested, convert capability rights to access flags.
2405 if (maxprotp != NULL)
2406 *maxprotp = cap_rights_to_vmprot(&haverights);
2407 #else /* !CAPABILITIES */
2408 if (maxprotp != NULL)
2409 *maxprotp = VM_PROT_ALL;
2410 #endif /* CAPABILITIES */
2413 * FREAD and FWRITE failure return EBADF as per POSIX.
2419 if ((fp->f_flag & flags) == 0)
2423 if ((fp->f_flag & (FREAD | FEXEC)) == 0 ||
2424 ((fp->f_flag & FWRITE) != 0))
2430 KASSERT(0, ("wrong flags"));
2443 fget(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
2446 return(_fget(td, fd, fpp, 0, rightsp, NULL));
2450 fget_mmap(struct thread *td, int fd, cap_rights_t *rightsp, u_char *maxprotp,
2454 return (_fget(td, fd, fpp, 0, rightsp, maxprotp));
2458 fget_read(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
2461 return(_fget(td, fd, fpp, FREAD, rightsp, NULL));
2465 fget_write(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
2468 return (_fget(td, fd, fpp, FWRITE, rightsp, NULL));
2472 * Like fget() but loads the underlying vnode, or returns an error if the
2473 * descriptor does not represent a vnode. Note that pipes use vnodes but
2474 * never have VM objects. The returned vnode will be vref()'d.
2476 * XXX: what about the unused flags ?
2479 _fgetvp(struct thread *td, int fd, int flags, cap_rights_t *needrightsp,
2486 error = _fget(td, fd, &fp, flags, needrightsp, NULL);
2489 if (fp->f_vnode == NULL) {
2501 fgetvp(struct thread *td, int fd, cap_rights_t *rightsp, struct vnode **vpp)
2504 return (_fgetvp(td, fd, 0, rightsp, vpp));
2508 fgetvp_rights(struct thread *td, int fd, cap_rights_t *needrightsp,
2509 struct filecaps *havecaps, struct vnode **vpp)
2511 struct filedesc *fdp;
2517 if (td == NULL || (fdp = td->td_proc->p_fd) == NULL)
2520 fp = fget_locked(fdp, fd);
2521 if (fp == NULL || fp->f_ops == &badfileops)
2525 if (needrightsp != NULL) {
2526 error = cap_check(cap_rights(fdp, fd), needrightsp);
2532 if (fp->f_vnode == NULL)
2537 filecaps_copy(&fdp->fd_ofiles[fd].fde_caps, havecaps);
2543 fgetvp_read(struct thread *td, int fd, cap_rights_t *rightsp, struct vnode **vpp)
2546 return (_fgetvp(td, fd, FREAD, rightsp, vpp));
2550 fgetvp_exec(struct thread *td, int fd, cap_rights_t *rightsp, struct vnode **vpp)
2553 return (_fgetvp(td, fd, FEXEC, rightsp, vpp));
2558 fgetvp_write(struct thread *td, int fd, cap_rights_t *rightsp,
2562 return (_fgetvp(td, fd, FWRITE, rightsp, vpp));
2567 * Like fget() but loads the underlying socket, or returns an error if the
2568 * descriptor does not represent a socket.
2570 * We bump the ref count on the returned socket. XXX Also obtain the SX lock
2573 * Note: fgetsock() and fputsock() are deprecated, as consumers should rely
2574 * on their file descriptor reference to prevent the socket from being free'd
2578 fgetsock(struct thread *td, int fd, cap_rights_t *rightsp, struct socket **spp,
2587 if ((error = _fget(td, fd, &fp, 0, rightsp, NULL)) != 0)
2589 if (fp->f_type != DTYPE_SOCKET) {
2594 *fflagp = fp->f_flag;
2605 * Drop the reference count on the socket and XXX release the SX lock in the
2606 * future. The last reference closes the socket.
2608 * Note: fputsock() is deprecated, see comment for fgetsock().
2611 fputsock(struct socket *so)
2616 CURVNET_SET(so->so_vnet);
2622 * Handle the last reference to a file being closed.
2625 _fdrop(struct file *fp, struct thread *td)
2630 if (fp->f_count != 0)
2631 panic("fdrop: count %d", fp->f_count);
2632 if (fp->f_ops != &badfileops)
2633 error = fo_close(fp, td);
2634 atomic_subtract_int(&openfiles, 1);
2636 free(fp->f_advice, M_FADVISE);
2637 uma_zfree(file_zone, fp);
2643 * Apply an advisory lock on a file descriptor.
2645 * Just attempt to get a record lock of the requested type on the entire file
2646 * (l_whence = SEEK_SET, l_start = 0, l_len = 0).
2648 #ifndef _SYS_SYSPROTO_H_
2656 sys_flock(struct thread *td, struct flock_args *uap)
2661 cap_rights_t rights;
2664 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_FLOCK), &fp);
2667 if (fp->f_type != DTYPE_VNODE) {
2669 return (EOPNOTSUPP);
2673 lf.l_whence = SEEK_SET;
2676 if (uap->how & LOCK_UN) {
2677 lf.l_type = F_UNLCK;
2678 atomic_clear_int(&fp->f_flag, FHASLOCK);
2679 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_UNLCK, &lf, F_FLOCK);
2682 if (uap->how & LOCK_EX)
2683 lf.l_type = F_WRLCK;
2684 else if (uap->how & LOCK_SH)
2685 lf.l_type = F_RDLCK;
2690 atomic_set_int(&fp->f_flag, FHASLOCK);
2691 error = VOP_ADVLOCK(vp, (caddr_t)fp, F_SETLK, &lf,
2692 (uap->how & LOCK_NB) ? F_FLOCK : F_FLOCK | F_WAIT);
2698 * Duplicate the specified descriptor to a free descriptor.
2701 dupfdopen(struct thread *td, struct filedesc *fdp, int dfd, int mode,
2702 int openerror, int *indxp)
2707 KASSERT(openerror == ENODEV || openerror == ENXIO,
2708 ("unexpected error %d in %s", openerror, __func__));
2711 * If the to-be-dup'd fd number is greater than the allowed number
2712 * of file descriptors, or the fd to be dup'd has already been
2713 * closed, then reject.
2715 FILEDESC_XLOCK(fdp);
2716 if ((fp = fget_locked(fdp, dfd)) == NULL) {
2717 FILEDESC_XUNLOCK(fdp);
2721 error = fdalloc(td, 0, &indx);
2723 FILEDESC_XUNLOCK(fdp);
2728 * There are two cases of interest here.
2730 * For ENODEV simply dup (dfd) to file descriptor (indx) and return.
2732 * For ENXIO steal away the file structure from (dfd) and store it in
2733 * (indx). (dfd) is effectively closed by this operation.
2735 switch (openerror) {
2738 * Check that the mode the file is being opened for is a
2739 * subset of the mode of the existing descriptor.
2741 if (((mode & (FREAD|FWRITE)) | fp->f_flag) != fp->f_flag) {
2742 fdunused(fdp, indx);
2743 FILEDESC_XUNLOCK(fdp);
2747 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
2748 filecaps_copy(&fdp->fd_ofiles[dfd].fde_caps,
2749 &fdp->fd_ofiles[indx].fde_caps);
2753 * Steal away the file pointer from dfd and stuff it into indx.
2755 fdp->fd_ofiles[indx] = fdp->fd_ofiles[dfd];
2756 bzero(&fdp->fd_ofiles[dfd], sizeof(fdp->fd_ofiles[dfd]));
2760 FILEDESC_XUNLOCK(fdp);
2766 * Scan all active processes and prisons to see if any of them have a current
2767 * or root directory of `olddp'. If so, replace them with the new mount point.
2770 mountcheckdirs(struct vnode *olddp, struct vnode *newdp)
2772 struct filedesc *fdp;
2777 if (vrefcnt(olddp) == 1)
2780 sx_slock(&allproc_lock);
2781 FOREACH_PROC_IN_SYSTEM(p) {
2785 FILEDESC_XLOCK(fdp);
2786 if (fdp->fd_cdir == olddp) {
2788 fdp->fd_cdir = newdp;
2791 if (fdp->fd_rdir == olddp) {
2793 fdp->fd_rdir = newdp;
2796 if (fdp->fd_jdir == olddp) {
2798 fdp->fd_jdir = newdp;
2801 FILEDESC_XUNLOCK(fdp);
2804 sx_sunlock(&allproc_lock);
2805 if (rootvnode == olddp) {
2810 mtx_lock(&prison0.pr_mtx);
2811 if (prison0.pr_root == olddp) {
2813 prison0.pr_root = newdp;
2816 mtx_unlock(&prison0.pr_mtx);
2817 sx_slock(&allprison_lock);
2818 TAILQ_FOREACH(pr, &allprison, pr_list) {
2819 mtx_lock(&pr->pr_mtx);
2820 if (pr->pr_root == olddp) {
2822 pr->pr_root = newdp;
2825 mtx_unlock(&pr->pr_mtx);
2827 sx_sunlock(&allprison_lock);
2832 struct filedesc_to_leader *
2833 filedesc_to_leader_alloc(struct filedesc_to_leader *old, struct filedesc *fdp, struct proc *leader)
2835 struct filedesc_to_leader *fdtol;
2837 fdtol = malloc(sizeof(struct filedesc_to_leader),
2838 M_FILEDESC_TO_LEADER,
2840 fdtol->fdl_refcount = 1;
2841 fdtol->fdl_holdcount = 0;
2842 fdtol->fdl_wakeup = 0;
2843 fdtol->fdl_leader = leader;
2845 FILEDESC_XLOCK(fdp);
2846 fdtol->fdl_next = old->fdl_next;
2847 fdtol->fdl_prev = old;
2848 old->fdl_next = fdtol;
2849 fdtol->fdl_next->fdl_prev = fdtol;
2850 FILEDESC_XUNLOCK(fdp);
2852 fdtol->fdl_next = fdtol;
2853 fdtol->fdl_prev = fdtol;
2859 * Get file structures globally.
2862 sysctl_kern_file(SYSCTL_HANDLER_ARGS)
2865 struct filedesc *fdp;
2870 error = sysctl_wire_old_buffer(req, 0);
2873 if (req->oldptr == NULL) {
2875 sx_slock(&allproc_lock);
2876 FOREACH_PROC_IN_SYSTEM(p) {
2877 if (p->p_state == PRS_NEW)
2882 /* overestimates sparse tables. */
2883 if (fdp->fd_lastfile > 0)
2884 n += fdp->fd_lastfile;
2887 sx_sunlock(&allproc_lock);
2888 return (SYSCTL_OUT(req, 0, n * sizeof(xf)));
2891 bzero(&xf, sizeof(xf));
2892 xf.xf_size = sizeof(xf);
2893 sx_slock(&allproc_lock);
2894 FOREACH_PROC_IN_SYSTEM(p) {
2896 if (p->p_state == PRS_NEW) {
2900 if (p_cansee(req->td, p) != 0) {
2904 xf.xf_pid = p->p_pid;
2905 xf.xf_uid = p->p_ucred->cr_uid;
2910 FILEDESC_SLOCK(fdp);
2911 for (n = 0; fdp->fd_refcnt > 0 && n < fdp->fd_nfiles; ++n) {
2912 if ((fp = fdp->fd_ofiles[n].fde_file) == NULL)
2916 xf.xf_data = fp->f_data;
2917 xf.xf_vnode = fp->f_vnode;
2918 xf.xf_type = fp->f_type;
2919 xf.xf_count = fp->f_count;
2921 xf.xf_offset = foffset_get(fp);
2922 xf.xf_flag = fp->f_flag;
2923 error = SYSCTL_OUT(req, &xf, sizeof(xf));
2927 FILEDESC_SUNLOCK(fdp);
2932 sx_sunlock(&allproc_lock);
2936 SYSCTL_PROC(_kern, KERN_FILE, file, CTLTYPE_OPAQUE|CTLFLAG_RD,
2937 0, 0, sysctl_kern_file, "S,xfile", "Entire file table");
2939 #ifdef KINFO_OFILE_SIZE
2940 CTASSERT(sizeof(struct kinfo_ofile) == KINFO_OFILE_SIZE);
2943 #ifdef COMPAT_FREEBSD7
2945 export_vnode_for_osysctl(struct vnode *vp, int type,
2946 struct kinfo_ofile *kif, struct filedesc *fdp, struct sysctl_req *req)
2949 char *fullpath, *freepath;
2951 bzero(kif, sizeof(*kif));
2952 kif->kf_structsize = sizeof(*kif);
2956 kif->kf_type = KF_TYPE_VNODE;
2957 /* This function only handles directories. */
2958 if (vp->v_type != VDIR) {
2962 kif->kf_vnode_type = KF_VTYPE_VDIR;
2965 * This is not a true file descriptor, so we set a bogus refcount
2966 * and offset to indicate these fields should be ignored.
2968 kif->kf_ref_count = -1;
2969 kif->kf_offset = -1;
2973 FILEDESC_SUNLOCK(fdp);
2974 vn_fullpath(curthread, vp, &fullpath, &freepath);
2976 strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path));
2977 if (freepath != NULL)
2978 free(freepath, M_TEMP);
2979 error = SYSCTL_OUT(req, kif, sizeof(*kif));
2980 FILEDESC_SLOCK(fdp);
2985 * Get per-process file descriptors for use by procstat(1), et al.
2988 sysctl_kern_proc_ofiledesc(SYSCTL_HANDLER_ARGS)
2990 char *fullpath, *freepath;
2991 struct kinfo_ofile *kif;
2992 struct filedesc *fdp;
2993 int error, i, *name;
2994 struct shmfd *shmfd;
3003 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
3010 kif = malloc(sizeof(*kif), M_TEMP, M_WAITOK);
3011 FILEDESC_SLOCK(fdp);
3012 if (fdp->fd_cdir != NULL)
3013 export_vnode_for_osysctl(fdp->fd_cdir, KF_FD_TYPE_CWD, kif,
3015 if (fdp->fd_rdir != NULL)
3016 export_vnode_for_osysctl(fdp->fd_rdir, KF_FD_TYPE_ROOT, kif,
3018 if (fdp->fd_jdir != NULL)
3019 export_vnode_for_osysctl(fdp->fd_jdir, KF_FD_TYPE_JAIL, kif,
3021 for (i = 0; i < fdp->fd_nfiles; i++) {
3022 if ((fp = fdp->fd_ofiles[i].fde_file) == NULL)
3024 bzero(kif, sizeof(*kif));
3025 kif->kf_structsize = sizeof(*kif);
3033 switch (fp->f_type) {
3035 kif->kf_type = KF_TYPE_VNODE;
3040 kif->kf_type = KF_TYPE_SOCKET;
3045 kif->kf_type = KF_TYPE_PIPE;
3049 kif->kf_type = KF_TYPE_FIFO;
3054 kif->kf_type = KF_TYPE_KQUEUE;
3058 kif->kf_type = KF_TYPE_CRYPTO;
3062 kif->kf_type = KF_TYPE_MQUEUE;
3066 kif->kf_type = KF_TYPE_SHM;
3071 kif->kf_type = KF_TYPE_SEM;
3076 kif->kf_type = KF_TYPE_PTS;
3081 case DTYPE_PROCDESC:
3082 kif->kf_type = KF_TYPE_PROCDESC;
3087 kif->kf_type = KF_TYPE_UNKNOWN;
3090 kif->kf_ref_count = fp->f_count;
3091 if (fp->f_flag & FREAD)
3092 kif->kf_flags |= KF_FLAG_READ;
3093 if (fp->f_flag & FWRITE)
3094 kif->kf_flags |= KF_FLAG_WRITE;
3095 if (fp->f_flag & FAPPEND)
3096 kif->kf_flags |= KF_FLAG_APPEND;
3097 if (fp->f_flag & FASYNC)
3098 kif->kf_flags |= KF_FLAG_ASYNC;
3099 if (fp->f_flag & FFSYNC)
3100 kif->kf_flags |= KF_FLAG_FSYNC;
3101 if (fp->f_flag & FNONBLOCK)
3102 kif->kf_flags |= KF_FLAG_NONBLOCK;
3103 if (fp->f_flag & O_DIRECT)
3104 kif->kf_flags |= KF_FLAG_DIRECT;
3105 if (fp->f_flag & FHASLOCK)
3106 kif->kf_flags |= KF_FLAG_HASLOCK;
3107 kif->kf_offset = foffset_get(fp);
3110 switch (vp->v_type) {
3112 kif->kf_vnode_type = KF_VTYPE_VNON;
3115 kif->kf_vnode_type = KF_VTYPE_VREG;
3118 kif->kf_vnode_type = KF_VTYPE_VDIR;
3121 kif->kf_vnode_type = KF_VTYPE_VBLK;
3124 kif->kf_vnode_type = KF_VTYPE_VCHR;
3127 kif->kf_vnode_type = KF_VTYPE_VLNK;
3130 kif->kf_vnode_type = KF_VTYPE_VSOCK;
3133 kif->kf_vnode_type = KF_VTYPE_VFIFO;
3136 kif->kf_vnode_type = KF_VTYPE_VBAD;
3139 kif->kf_vnode_type = KF_VTYPE_UNKNOWN;
3143 * It is OK to drop the filedesc lock here as we will
3144 * re-validate and re-evaluate its properties when
3145 * the loop continues.
3149 FILEDESC_SUNLOCK(fdp);
3150 vn_fullpath(curthread, vp, &fullpath, &freepath);
3152 strlcpy(kif->kf_path, fullpath,
3153 sizeof(kif->kf_path));
3154 if (freepath != NULL)
3155 free(freepath, M_TEMP);
3156 FILEDESC_SLOCK(fdp);
3159 struct sockaddr *sa;
3161 if (so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa)
3162 == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) {
3163 bcopy(sa, &kif->kf_sa_local, sa->sa_len);
3166 if (so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa)
3167 == 0 && sa->sa_len <= sizeof(kif->kf_sa_peer)) {
3168 bcopy(sa, &kif->kf_sa_peer, sa->sa_len);
3171 kif->kf_sock_domain =
3172 so->so_proto->pr_domain->dom_family;
3173 kif->kf_sock_type = so->so_type;
3174 kif->kf_sock_protocol = so->so_proto->pr_protocol;
3177 strlcpy(kif->kf_path, tty_devname(tp),
3178 sizeof(kif->kf_path));
3181 shm_path(shmfd, kif->kf_path, sizeof(kif->kf_path));
3182 if (ks != NULL && ksem_info != NULL)
3183 ksem_info(ks, kif->kf_path, sizeof(kif->kf_path), NULL);
3184 error = SYSCTL_OUT(req, kif, sizeof(*kif));
3188 FILEDESC_SUNLOCK(fdp);
3194 static SYSCTL_NODE(_kern_proc, KERN_PROC_OFILEDESC, ofiledesc, CTLFLAG_RD,
3195 sysctl_kern_proc_ofiledesc, "Process ofiledesc entries");
3196 #endif /* COMPAT_FREEBSD7 */
3198 #ifdef KINFO_FILE_SIZE
3199 CTASSERT(sizeof(struct kinfo_file) == KINFO_FILE_SIZE);
3202 struct export_fd_buf {
3203 struct filedesc *fdp;
3206 struct kinfo_file kif;
3210 export_fd_to_sb(void *data, int type, int fd, int fflags, int refcnt,
3211 int64_t offset, cap_rights_t *rightsp, struct export_fd_buf *efbuf)
3216 } fflags_table[] = {
3217 { FAPPEND, KF_FLAG_APPEND },
3218 { FASYNC, KF_FLAG_ASYNC },
3219 { FFSYNC, KF_FLAG_FSYNC },
3220 { FHASLOCK, KF_FLAG_HASLOCK },
3221 { FNONBLOCK, KF_FLAG_NONBLOCK },
3222 { FREAD, KF_FLAG_READ },
3223 { FWRITE, KF_FLAG_WRITE },
3224 { O_CREAT, KF_FLAG_CREAT },
3225 { O_DIRECT, KF_FLAG_DIRECT },
3226 { O_EXCL, KF_FLAG_EXCL },
3227 { O_EXEC, KF_FLAG_EXEC },
3228 { O_EXLOCK, KF_FLAG_EXLOCK },
3229 { O_NOFOLLOW, KF_FLAG_NOFOLLOW },
3230 { O_SHLOCK, KF_FLAG_SHLOCK },
3231 { O_TRUNC, KF_FLAG_TRUNC }
3233 #define NFFLAGS (sizeof(fflags_table) / sizeof(*fflags_table))
3234 struct kinfo_file *kif;
3239 if (efbuf->remainder == 0)
3242 bzero(kif, sizeof(*kif));
3243 locked = efbuf->fdp != NULL;
3248 FILEDESC_SUNLOCK(efbuf->fdp);
3251 vp = (struct vnode *)data;
3252 error = fill_vnode_info(vp, kif);
3255 case KF_TYPE_SOCKET:
3256 error = fill_socket_info((struct socket *)data, kif);
3259 error = fill_pipe_info((struct pipe *)data, kif);
3262 error = fill_pts_info((struct tty *)data, kif);
3264 case KF_TYPE_PROCDESC:
3265 error = fill_procdesc_info((struct procdesc *)data, kif);
3268 error = fill_sem_info((struct file *)data, kif);
3271 error = fill_shm_info((struct file *)data, kif);
3277 kif->kf_status |= KF_ATTR_VALID;
3280 * Translate file access flags.
3282 for (i = 0; i < NFFLAGS; i++)
3283 if (fflags & fflags_table[i].fflag)
3284 kif->kf_flags |= fflags_table[i].kf_fflag;
3285 if (rightsp != NULL)
3286 kif->kf_cap_rights = *rightsp;
3288 cap_rights_init(&kif->kf_cap_rights);
3290 kif->kf_type = type;
3291 kif->kf_ref_count = refcnt;
3292 kif->kf_offset = offset;
3293 /* Pack record size down */
3294 kif->kf_structsize = offsetof(struct kinfo_file, kf_path) +
3295 strlen(kif->kf_path) + 1;
3296 kif->kf_structsize = roundup(kif->kf_structsize, sizeof(uint64_t));
3297 if (efbuf->remainder != -1) {
3298 if (efbuf->remainder < kif->kf_structsize) {
3299 /* Terminate export. */
3300 efbuf->remainder = 0;
3301 if (efbuf->fdp != NULL && !locked)
3302 FILEDESC_SLOCK(efbuf->fdp);
3305 efbuf->remainder -= kif->kf_structsize;
3308 FILEDESC_SUNLOCK(efbuf->fdp);
3309 error = sbuf_bcat(efbuf->sb, kif, kif->kf_structsize);
3310 if (efbuf->fdp != NULL)
3311 FILEDESC_SLOCK(efbuf->fdp);
3316 * Store a process file descriptor information to sbuf.
3318 * Takes a locked proc as argument, and returns with the proc unlocked.
3321 kern_proc_filedesc_out(struct proc *p, struct sbuf *sb, ssize_t maxlen)
3324 struct filedesc *fdp;
3325 struct export_fd_buf *efbuf;
3326 struct vnode *cttyvp, *textvp, *tracevp;
3330 int type, refcnt, fflags;
3331 cap_rights_t rights;
3333 PROC_LOCK_ASSERT(p, MA_OWNED);
3336 tracevp = p->p_tracevp;
3337 if (tracevp != NULL)
3340 textvp = p->p_textvp;
3343 /* Controlling tty. */
3345 if (p->p_pgrp != NULL && p->p_pgrp->pg_session != NULL) {
3346 cttyvp = p->p_pgrp->pg_session->s_ttyvp;
3352 efbuf = malloc(sizeof(*efbuf), M_TEMP, M_WAITOK);
3355 efbuf->remainder = maxlen;
3356 if (tracevp != NULL)
3357 export_fd_to_sb(tracevp, KF_TYPE_VNODE, KF_FD_TYPE_TRACE,
3358 FREAD | FWRITE, -1, -1, NULL, efbuf);
3360 export_fd_to_sb(textvp, KF_TYPE_VNODE, KF_FD_TYPE_TEXT,
3361 FREAD, -1, -1, NULL, efbuf);
3363 export_fd_to_sb(cttyvp, KF_TYPE_VNODE, KF_FD_TYPE_CTTY,
3364 FREAD | FWRITE, -1, -1, NULL, efbuf);
3369 FILEDESC_SLOCK(fdp);
3370 /* working directory */
3371 if (fdp->fd_cdir != NULL) {
3373 data = fdp->fd_cdir;
3374 export_fd_to_sb(data, KF_TYPE_VNODE, KF_FD_TYPE_CWD,
3375 FREAD, -1, -1, NULL, efbuf);
3377 /* root directory */
3378 if (fdp->fd_rdir != NULL) {
3380 data = fdp->fd_rdir;
3381 export_fd_to_sb(data, KF_TYPE_VNODE, KF_FD_TYPE_ROOT,
3382 FREAD, -1, -1, NULL, efbuf);
3384 /* jail directory */
3385 if (fdp->fd_jdir != NULL) {
3387 data = fdp->fd_jdir;
3388 export_fd_to_sb(data, KF_TYPE_VNODE, KF_FD_TYPE_JAIL,
3389 FREAD, -1, -1, NULL, efbuf);
3391 for (i = 0; i < fdp->fd_nfiles; i++) {
3392 if ((fp = fdp->fd_ofiles[i].fde_file) == NULL)
3396 rights = *cap_rights(fdp, i);
3397 #else /* !CAPABILITIES */
3398 cap_rights_init(&rights);
3400 switch (fp->f_type) {
3402 type = KF_TYPE_VNODE;
3408 type = KF_TYPE_SOCKET;
3413 type = KF_TYPE_PIPE;
3418 type = KF_TYPE_FIFO;
3424 type = KF_TYPE_KQUEUE;
3428 type = KF_TYPE_CRYPTO;
3432 type = KF_TYPE_MQUEUE;
3451 case DTYPE_PROCDESC:
3452 type = KF_TYPE_PROCDESC;
3458 type = KF_TYPE_UNKNOWN;
3461 refcnt = fp->f_count;
3462 fflags = fp->f_flag;
3463 offset = foffset_get(fp);
3466 * Create sysctl entry.
3467 * It is OK to drop the filedesc lock here as we will
3468 * re-validate and re-evaluate its properties when
3469 * the loop continues.
3471 error = export_fd_to_sb(data, type, i, fflags, refcnt,
3472 offset, &rights, efbuf);
3476 FILEDESC_SUNLOCK(fdp);
3479 free(efbuf, M_TEMP);
3483 #define FILEDESC_SBUF_SIZE (sizeof(struct kinfo_file) * 5)
3486 * Get per-process file descriptors for use by procstat(1), et al.
3489 sysctl_kern_proc_filedesc(SYSCTL_HANDLER_ARGS)
3494 int error, error2, *name;
3498 sbuf_new_for_sysctl(&sb, NULL, FILEDESC_SBUF_SIZE, req);
3499 error = pget((pid_t)name[0], PGET_CANDEBUG, &p);
3504 maxlen = req->oldptr != NULL ? req->oldlen : -1;
3505 error = kern_proc_filedesc_out(p, &sb, maxlen);
3506 error2 = sbuf_finish(&sb);
3508 return (error != 0 ? error : error2);
3512 vntype_to_kinfo(int vtype)
3517 } vtypes_table[] = {
3518 { VBAD, KF_VTYPE_VBAD },
3519 { VBLK, KF_VTYPE_VBLK },
3520 { VCHR, KF_VTYPE_VCHR },
3521 { VDIR, KF_VTYPE_VDIR },
3522 { VFIFO, KF_VTYPE_VFIFO },
3523 { VLNK, KF_VTYPE_VLNK },
3524 { VNON, KF_VTYPE_VNON },
3525 { VREG, KF_VTYPE_VREG },
3526 { VSOCK, KF_VTYPE_VSOCK }
3528 #define NVTYPES (sizeof(vtypes_table) / sizeof(*vtypes_table))
3532 * Perform vtype translation.
3534 for (i = 0; i < NVTYPES; i++)
3535 if (vtypes_table[i].vtype == vtype)
3538 return (vtypes_table[i].kf_vtype);
3540 return (KF_VTYPE_UNKNOWN);
3544 fill_vnode_info(struct vnode *vp, struct kinfo_file *kif)
3547 char *fullpath, *freepath;
3552 kif->kf_vnode_type = vntype_to_kinfo(vp->v_type);
3555 error = vn_fullpath(curthread, vp, &fullpath, &freepath);
3557 strlcpy(kif->kf_path, fullpath, sizeof(kif->kf_path));
3559 if (freepath != NULL)
3560 free(freepath, M_TEMP);
3563 * Retrieve vnode attributes.
3565 va.va_fsid = VNOVAL;
3567 vn_lock(vp, LK_SHARED | LK_RETRY);
3568 error = VOP_GETATTR(vp, &va, curthread->td_ucred);
3572 if (va.va_fsid != VNOVAL)
3573 kif->kf_un.kf_file.kf_file_fsid = va.va_fsid;
3575 kif->kf_un.kf_file.kf_file_fsid =
3576 vp->v_mount->mnt_stat.f_fsid.val[0];
3577 kif->kf_un.kf_file.kf_file_fileid = va.va_fileid;
3578 kif->kf_un.kf_file.kf_file_mode = MAKEIMODE(va.va_type, va.va_mode);
3579 kif->kf_un.kf_file.kf_file_size = va.va_size;
3580 kif->kf_un.kf_file.kf_file_rdev = va.va_rdev;
3585 fill_socket_info(struct socket *so, struct kinfo_file *kif)
3587 struct sockaddr *sa;
3588 struct inpcb *inpcb;
3589 struct unpcb *unpcb;
3594 kif->kf_sock_domain = so->so_proto->pr_domain->dom_family;
3595 kif->kf_sock_type = so->so_type;
3596 kif->kf_sock_protocol = so->so_proto->pr_protocol;
3597 kif->kf_un.kf_sock.kf_sock_pcb = (uintptr_t)so->so_pcb;
3598 switch(kif->kf_sock_domain) {
3601 if (kif->kf_sock_protocol == IPPROTO_TCP) {
3602 if (so->so_pcb != NULL) {
3603 inpcb = (struct inpcb *)(so->so_pcb);
3604 kif->kf_un.kf_sock.kf_sock_inpcb =
3605 (uintptr_t)inpcb->inp_ppcb;
3610 if (so->so_pcb != NULL) {
3611 unpcb = (struct unpcb *)(so->so_pcb);
3612 if (unpcb->unp_conn) {
3613 kif->kf_un.kf_sock.kf_sock_unpconn =
3614 (uintptr_t)unpcb->unp_conn;
3615 kif->kf_un.kf_sock.kf_sock_rcv_sb_state =
3616 so->so_rcv.sb_state;
3617 kif->kf_un.kf_sock.kf_sock_snd_sb_state =
3618 so->so_snd.sb_state;
3623 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
3624 if (error == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) {
3625 bcopy(sa, &kif->kf_sa_local, sa->sa_len);
3628 error = so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa);
3629 if (error == 0 && sa->sa_len <= sizeof(kif->kf_sa_peer)) {
3630 bcopy(sa, &kif->kf_sa_peer, sa->sa_len);
3633 strncpy(kif->kf_path, so->so_proto->pr_domain->dom_name,
3634 sizeof(kif->kf_path));
3639 fill_pts_info(struct tty *tp, struct kinfo_file *kif)
3644 kif->kf_un.kf_pts.kf_pts_dev = tty_udev(tp);
3645 strlcpy(kif->kf_path, tty_devname(tp), sizeof(kif->kf_path));
3650 fill_pipe_info(struct pipe *pi, struct kinfo_file *kif)
3655 kif->kf_un.kf_pipe.kf_pipe_addr = (uintptr_t)pi;
3656 kif->kf_un.kf_pipe.kf_pipe_peer = (uintptr_t)pi->pipe_peer;
3657 kif->kf_un.kf_pipe.kf_pipe_buffer_cnt = pi->pipe_buffer.cnt;
3662 fill_procdesc_info(struct procdesc *pdp, struct kinfo_file *kif)
3667 kif->kf_un.kf_proc.kf_pid = pdp->pd_pid;
3672 fill_sem_info(struct file *fp, struct kinfo_file *kif)
3678 if (fp->f_data == NULL)
3680 if (fo_stat(fp, &sb, td->td_ucred, td) != 0)
3682 if (ksem_info == NULL)
3684 ksem_info(fp->f_data, kif->kf_path, sizeof(kif->kf_path),
3685 &kif->kf_un.kf_sem.kf_sem_value);
3686 kif->kf_un.kf_sem.kf_sem_mode = sb.st_mode;
3691 fill_shm_info(struct file *fp, struct kinfo_file *kif)
3697 if (fp->f_data == NULL)
3699 if (fo_stat(fp, &sb, td->td_ucred, td) != 0)
3701 shm_path(fp->f_data, kif->kf_path, sizeof(kif->kf_path));
3702 kif->kf_un.kf_file.kf_file_mode = sb.st_mode;
3703 kif->kf_un.kf_file.kf_file_size = sb.st_size;
3707 static SYSCTL_NODE(_kern_proc, KERN_PROC_FILEDESC, filedesc, CTLFLAG_RD,
3708 sysctl_kern_proc_filedesc, "Process filedesc entries");
3712 * For the purposes of debugging, generate a human-readable string for the
3716 file_type_to_name(short type)
3746 * For the purposes of debugging, identify a process (if any, perhaps one of
3747 * many) that references the passed file in its file descriptor array. Return
3750 static struct proc *
3751 file_to_first_proc(struct file *fp)
3753 struct filedesc *fdp;
3757 FOREACH_PROC_IN_SYSTEM(p) {
3758 if (p->p_state == PRS_NEW)
3763 for (n = 0; n < fdp->fd_nfiles; n++) {
3764 if (fp == fdp->fd_ofiles[n].fde_file)
3772 db_print_file(struct file *fp, int header)
3777 db_printf("%8s %4s %8s %8s %4s %5s %6s %8s %5s %12s\n",
3778 "File", "Type", "Data", "Flag", "GCFl", "Count",
3779 "MCount", "Vnode", "FPID", "FCmd");
3780 p = file_to_first_proc(fp);
3781 db_printf("%8p %4s %8p %08x %04x %5d %6d %8p %5d %12s\n", fp,
3782 file_type_to_name(fp->f_type), fp->f_data, fp->f_flag,
3783 0, fp->f_count, 0, fp->f_vnode,
3784 p != NULL ? p->p_pid : -1, p != NULL ? p->p_comm : "-");
3787 DB_SHOW_COMMAND(file, db_show_file)
3792 db_printf("usage: show file <addr>\n");
3795 fp = (struct file *)addr;
3796 db_print_file(fp, 1);
3799 DB_SHOW_COMMAND(files, db_show_files)
3801 struct filedesc *fdp;
3808 FOREACH_PROC_IN_SYSTEM(p) {
3809 if (p->p_state == PRS_NEW)
3811 if ((fdp = p->p_fd) == NULL)
3813 for (n = 0; n < fdp->fd_nfiles; ++n) {
3814 if ((fp = fdp->fd_ofiles[n].fde_file) == NULL)
3816 db_print_file(fp, header);
3823 SYSCTL_INT(_kern, KERN_MAXFILESPERPROC, maxfilesperproc, CTLFLAG_RW,
3824 &maxfilesperproc, 0, "Maximum files allowed open per process");
3826 SYSCTL_INT(_kern, KERN_MAXFILES, maxfiles, CTLFLAG_RW,
3827 &maxfiles, 0, "Maximum number of files");
3829 SYSCTL_INT(_kern, OID_AUTO, openfiles, CTLFLAG_RD,
3830 __DEVOLATILE(int *, &openfiles), 0, "System-wide number of open files");
3834 filelistinit(void *dummy)
3837 file_zone = uma_zcreate("Files", sizeof(struct file), NULL, NULL,
3838 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
3839 mtx_init(&sigio_lock, "sigio lock", NULL, MTX_DEF);
3840 mtx_init(&fdesc_mtx, "fdesc", NULL, MTX_DEF);
3842 SYSINIT(select, SI_SUB_LOCK, SI_ORDER_FIRST, filelistinit, NULL);
3844 /*-------------------------------------------------------------------*/
3847 badfo_readwrite(struct file *fp, struct uio *uio, struct ucred *active_cred,
3848 int flags, struct thread *td)
3855 badfo_truncate(struct file *fp, off_t length, struct ucred *active_cred,
3863 badfo_ioctl(struct file *fp, u_long com, void *data, struct ucred *active_cred,
3871 badfo_poll(struct file *fp, int events, struct ucred *active_cred,
3879 badfo_kqfilter(struct file *fp, struct knote *kn)
3886 badfo_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
3894 badfo_close(struct file *fp, struct thread *td)
3901 badfo_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
3909 badfo_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
3917 badfo_sendfile(struct file *fp, int sockfd, struct uio *hdr_uio,
3918 struct uio *trl_uio, off_t offset, size_t nbytes, off_t *sent, int flags,
3919 int kflags, struct thread *td)
3925 struct fileops badfileops = {
3926 .fo_read = badfo_readwrite,
3927 .fo_write = badfo_readwrite,
3928 .fo_truncate = badfo_truncate,
3929 .fo_ioctl = badfo_ioctl,
3930 .fo_poll = badfo_poll,
3931 .fo_kqfilter = badfo_kqfilter,
3932 .fo_stat = badfo_stat,
3933 .fo_close = badfo_close,
3934 .fo_chmod = badfo_chmod,
3935 .fo_chown = badfo_chown,
3936 .fo_sendfile = badfo_sendfile,
3940 invfo_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
3948 invfo_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
3956 invfo_sendfile(struct file *fp, int sockfd, struct uio *hdr_uio,
3957 struct uio *trl_uio, off_t offset, size_t nbytes, off_t *sent, int flags,
3958 int kflags, struct thread *td)
3964 /*-------------------------------------------------------------------*/
3967 * File Descriptor pseudo-device driver (/dev/fd/).
3969 * Opening minor device N dup()s the file (if any) connected to file
3970 * descriptor N belonging to the calling process. Note that this driver
3971 * consists of only the ``open()'' routine, because all subsequent
3972 * references to this file will be direct to the other driver.
3974 * XXX: we could give this one a cloning event handler if necessary.
3979 fdopen(struct cdev *dev, int mode, int type, struct thread *td)
3983 * XXX Kludge: set curthread->td_dupfd to contain the value of the
3984 * the file descriptor being sought for duplication. The error
3985 * return ensures that the vnode for this device will be released
3986 * by vn_open. Open will detect this special error and take the
3987 * actions in dupfdopen below. Other callers of vn_open or VOP_OPEN
3988 * will simply report the error.
3990 td->td_dupfd = dev2unit(dev);
3994 static struct cdevsw fildesc_cdevsw = {
3995 .d_version = D_VERSION,
4001 fildesc_drvinit(void *unused)
4005 dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 0, NULL,
4006 UID_ROOT, GID_WHEEL, 0666, "fd/0");
4007 make_dev_alias(dev, "stdin");
4008 dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 1, NULL,
4009 UID_ROOT, GID_WHEEL, 0666, "fd/1");
4010 make_dev_alias(dev, "stdout");
4011 dev = make_dev_credf(MAKEDEV_ETERNAL, &fildesc_cdevsw, 2, NULL,
4012 UID_ROOT, GID_WHEEL, 0666, "fd/2");
4013 make_dev_alias(dev, "stderr");
4016 SYSINIT(fildescdev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, fildesc_drvinit, NULL);