FreeBSD/Linux Kernel Cross Reference
sys/sys/filedesc.h
1 /*
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)filedesc.h 8.1 (Berkeley) 6/2/93
30 * $FreeBSD: releng/5.3/sys/sys/filedesc.h 136588 2004-10-16 08:43:07Z cvs2svn $
31 */
32
33 #ifndef _SYS_FILEDESC_H_
34 #define _SYS_FILEDESC_H_
35
36 #include <sys/queue.h>
37 #include <sys/event.h>
38 #include <sys/_lock.h>
39 #include <sys/_mutex.h>
40
41 #include <machine/_limits.h>
42
43 /*
44 * This structure is used for the management of descriptors. It may be
45 * shared by multiple processes.
46 *
47 * A process is initially started out with NDFILE descriptors stored within
48 * this structure, selected to be enough for typical applications based on
49 * the historical limit of 20 open files (and the usage of descriptors by
50 * shells). If these descriptors are exhausted, a larger descriptor table
51 * may be allocated, up to a process' resource limit; the internal arrays
52 * are then unused.
53 */
54 #define NDFILE 20
55 #define NDSLOTTYPE u_long
56 #define NDSLOTSIZE sizeof(NDSLOTTYPE)
57 #define NDENTRIES (NDSLOTSIZE * __CHAR_BIT)
58 #define NDSLOT(x) ((x) / NDENTRIES)
59 #define NDBIT(x) ((NDSLOTTYPE)1 << ((x) % NDENTRIES))
60 #define NDSLOTS(x) (((x) + NDENTRIES - 1) / NDENTRIES)
61
62 struct filedesc {
63 struct file **fd_ofiles; /* file structures for open files */
64 char *fd_ofileflags; /* per-process open file flags */
65 struct vnode *fd_cdir; /* current directory */
66 struct vnode *fd_rdir; /* root directory */
67 struct vnode *fd_jdir; /* jail root directory */
68 int fd_nfiles; /* number of open files allocated */
69 NDSLOTTYPE *fd_map; /* bitmap of free fds */
70 int fd_lastfile; /* high-water mark of fd_ofiles */
71 int fd_freefile; /* approx. next free file */
72 u_short fd_cmask; /* mask for file creation */
73 u_short fd_refcnt; /* reference count */
74
75 struct mtx fd_mtx; /* protects members of this struct */
76 struct kqlist fd_kqlist; /* list of kqueues on this filedesc */
77 int fd_holdleaderscount; /* block fdfree() for shared close() */
78 int fd_holdleaderswakeup; /* fdfree() needs wakeup */
79 };
80
81 /*
82 * Basic allocation of descriptors:
83 * one of the above, plus arrays for NDFILE descriptors.
84 */
85 struct filedesc0 {
86 struct filedesc fd_fd;
87 /*
88 * These arrays are used when the number of open files is
89 * <= NDFILE, and are then pointed to by the pointers above.
90 */
91 struct file *fd_dfiles[NDFILE];
92 char fd_dfileflags[NDFILE];
93 NDSLOTTYPE fd_dmap[NDSLOTS(NDFILE)];
94 };
95
96 /*
97 * Structure to keep track of (process leader, struct fildedesc) tuples.
98 * Each process has a pointer to such a structure when detailed tracking
99 * is needed, e.g., when rfork(RFPROC | RFMEM) causes a file descriptor
100 * table to be shared by processes having different "p_leader" pointers
101 * and thus distinct POSIX style locks.
102 *
103 * fdl_refcount and fdl_holdcount are protected by struct filedesc mtx.
104 */
105 struct filedesc_to_leader {
106 int fdl_refcount; /* references from struct proc */
107 int fdl_holdcount; /* temporary hold during closef */
108 int fdl_wakeup; /* fdfree() waits on closef() */
109 struct proc *fdl_leader; /* owner of POSIX locks */
110 /* Circular list: */
111 struct filedesc_to_leader *fdl_prev;
112 struct filedesc_to_leader *fdl_next;
113 };
114
115 /*
116 * Per-process open flags.
117 */
118 #define UF_EXCLOSE 0x01 /* auto-close on exec */
119
120 /*
121 * Storage required per open file descriptor.
122 */
123 #define OFILESIZE (sizeof(struct file *) + sizeof(char))
124
125 #ifdef _KERNEL
126
127 /* Lock a file descriptor table. */
128 #define FILEDESC_LOCK(fd) mtx_lock(&(fd)->fd_mtx)
129 #define FILEDESC_UNLOCK(fd) mtx_unlock(&(fd)->fd_mtx)
130 #define FILEDESC_LOCKED(fd) mtx_owned(&(fd)->fd_mtx)
131 #define FILEDESC_LOCK_ASSERT(fd, type) mtx_assert(&(fd)->fd_mtx, (type))
132 #define FILEDESC_LOCK_DESC "filedesc structure"
133
134 struct thread;
135
136 int closef(struct file *fp, struct thread *td);
137 int dupfdopen(struct thread *td, struct filedesc *fdp, int indx, int dfd,
138 int mode, int error);
139 int falloc(struct thread *td, struct file **resultfp, int *resultfd);
140 int fdalloc(struct thread *td, int minfd, int *result);
141 int fdavail(struct thread *td, int n);
142 int fdcheckstd(struct thread *td);
143 void fdcloseexec(struct thread *td);
144 struct filedesc *fdcopy(struct filedesc *fdp);
145 void fdfree(struct thread *td);
146 struct filedesc *fdinit(struct filedesc *fdp);
147 struct filedesc *fdshare(struct filedesc *fdp);
148 void fdunused(struct filedesc *fdp, int fd);
149 void fdused(struct filedesc *fdp, int fd);
150 void ffree(struct file *fp);
151 struct filedesc_to_leader *
152 filedesc_to_leader_alloc(struct filedesc_to_leader *old,
153 struct filedesc *fdp, struct proc *leader);
154 int getvnode(struct filedesc *fdp, int fd, struct file **fpp);
155 void setugidsafety(struct thread *td);
156
157 static __inline struct file *
158 fget_locked(struct filedesc *fdp, int fd)
159 {
160
161 return (fd < 0 || fd >= fdp->fd_nfiles ? NULL : fdp->fd_ofiles[fd]);
162 }
163
164 extern struct mtx fdesc_mtx;
165
166 #endif /* _KERNEL */
167
168 #endif /* !_SYS_FILEDESC_H_ */
Cache object: 62bbc560b138eaf47c9a5adff78e0994
|