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$ |
---|
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 <rtems/bsd/sys/lock.h> |
---|
39 | #include <sys/priority.h> |
---|
40 | #include <sys/sx.h> |
---|
41 | |
---|
42 | #include <machine/_limits.h> |
---|
43 | |
---|
44 | /* |
---|
45 | * This structure is used for the management of descriptors. It may be |
---|
46 | * shared by multiple processes. |
---|
47 | */ |
---|
48 | #define NDSLOTTYPE u_long |
---|
49 | |
---|
50 | #ifndef __rtems__ |
---|
51 | struct filedesc { |
---|
52 | struct file **fd_ofiles; /* file structures for open files */ |
---|
53 | char *fd_ofileflags; /* per-process open file flags */ |
---|
54 | struct vnode *fd_cdir; /* current directory */ |
---|
55 | struct vnode *fd_rdir; /* root directory */ |
---|
56 | struct vnode *fd_jdir; /* jail root directory */ |
---|
57 | int fd_nfiles; /* number of open files allocated */ |
---|
58 | NDSLOTTYPE *fd_map; /* bitmap of free fds */ |
---|
59 | int fd_lastfile; /* high-water mark of fd_ofiles */ |
---|
60 | int fd_freefile; /* approx. next free file */ |
---|
61 | u_short fd_cmask; /* mask for file creation */ |
---|
62 | u_short fd_refcnt; /* thread reference count */ |
---|
63 | u_short fd_holdcnt; /* hold count on structure + mutex */ |
---|
64 | struct sx fd_sx; /* protects members of this struct */ |
---|
65 | struct kqlist fd_kqlist; /* list of kqueues on this filedesc */ |
---|
66 | int fd_holdleaderscount; /* block fdfree() for shared close() */ |
---|
67 | int fd_holdleaderswakeup; /* fdfree() needs wakeup */ |
---|
68 | }; |
---|
69 | #else /* __rtems__ */ |
---|
70 | struct filedesc; |
---|
71 | #endif /* __rtems__ */ |
---|
72 | |
---|
73 | /* |
---|
74 | * Structure to keep track of (process leader, struct fildedesc) tuples. |
---|
75 | * Each process has a pointer to such a structure when detailed tracking |
---|
76 | * is needed, e.g., when rfork(RFPROC | RFMEM) causes a file descriptor |
---|
77 | * table to be shared by processes having different "p_leader" pointers |
---|
78 | * and thus distinct POSIX style locks. |
---|
79 | * |
---|
80 | * fdl_refcount and fdl_holdcount are protected by struct filedesc mtx. |
---|
81 | */ |
---|
82 | #ifndef __rtems__ |
---|
83 | struct filedesc_to_leader { |
---|
84 | int fdl_refcount; /* references from struct proc */ |
---|
85 | int fdl_holdcount; /* temporary hold during closef */ |
---|
86 | int fdl_wakeup; /* fdfree() waits on closef() */ |
---|
87 | struct proc *fdl_leader; /* owner of POSIX locks */ |
---|
88 | /* Circular list: */ |
---|
89 | struct filedesc_to_leader *fdl_prev; |
---|
90 | struct filedesc_to_leader *fdl_next; |
---|
91 | }; |
---|
92 | #else /* __rtems__ */ |
---|
93 | struct filedesc_to_leader; |
---|
94 | #endif /* __rtems__ */ |
---|
95 | |
---|
96 | /* |
---|
97 | * Per-process open flags. |
---|
98 | */ |
---|
99 | #define UF_EXCLOSE 0x01 /* auto-close on exec */ |
---|
100 | |
---|
101 | #ifdef _KERNEL |
---|
102 | #ifdef __rtems__ |
---|
103 | #include <sys/file.h> |
---|
104 | #include <rtems/libio_.h> |
---|
105 | #endif /* __rtems__ */ |
---|
106 | |
---|
107 | /* Lock a file descriptor table. */ |
---|
108 | #define FILEDESC_LOCK_INIT(fdp) sx_init(&(fdp)->fd_sx, "filedesc structure") |
---|
109 | #define FILEDESC_LOCK_DESTROY(fdp) sx_destroy(&(fdp)->fd_sx) |
---|
110 | #define FILEDESC_LOCK(fdp) (&(fdp)->fd_sx) |
---|
111 | #ifndef __rtems__ |
---|
112 | #define FILEDESC_XLOCK(fdp) sx_xlock(&(fdp)->fd_sx) |
---|
113 | #define FILEDESC_XUNLOCK(fdp) sx_xunlock(&(fdp)->fd_sx) |
---|
114 | #define FILEDESC_SLOCK(fdp) sx_slock(&(fdp)->fd_sx) |
---|
115 | #define FILEDESC_SUNLOCK(fdp) sx_sunlock(&(fdp)->fd_sx) |
---|
116 | #else /* __rtems__ */ |
---|
117 | #define FILEDESC_XLOCK(fdp) do { } while (0) |
---|
118 | #define FILEDESC_XUNLOCK(fdp) do { } while (0) |
---|
119 | #define FILEDESC_SLOCK(fdp) do { } while (0) |
---|
120 | #define FILEDESC_SUNLOCK(fdp) do { } while (0) |
---|
121 | #endif /* __rtems__ */ |
---|
122 | |
---|
123 | #define FILEDESC_LOCK_ASSERT(fdp) sx_assert(&(fdp)->fd_sx, SX_LOCKED | \ |
---|
124 | SX_NOTRECURSED) |
---|
125 | #define FILEDESC_XLOCK_ASSERT(fdp) sx_assert(&(fdp)->fd_sx, SX_XLOCKED | \ |
---|
126 | SX_NOTRECURSED) |
---|
127 | |
---|
128 | struct thread; |
---|
129 | |
---|
130 | int closef(struct file *fp, struct thread *td); |
---|
131 | int dupfdopen(struct thread *td, struct filedesc *fdp, int indx, int dfd, |
---|
132 | int mode, int error); |
---|
133 | #ifndef __rtems__ |
---|
134 | int falloc(struct thread *td, struct file **resultfp, int *resultfd, |
---|
135 | int flags); |
---|
136 | #else /* __rtems__ */ |
---|
137 | static inline int |
---|
138 | falloc(struct thread *td, struct file **resultfp, int *resultfd, |
---|
139 | int flags) |
---|
140 | { |
---|
141 | rtems_libio_t *iop = rtems_libio_allocate(); |
---|
142 | |
---|
143 | (void) td; |
---|
144 | (void) flags; |
---|
145 | |
---|
146 | *resultfp = rtems_bsd_iop_to_fp(iop); |
---|
147 | |
---|
148 | if (iop != NULL) { |
---|
149 | iop->pathinfo.mt_entry = &rtems_filesystem_null_mt_entry; |
---|
150 | rtems_filesystem_location_add_to_mt_entry(&iop->pathinfo); |
---|
151 | *resultfd = rtems_libio_iop_to_descriptor(iop); |
---|
152 | |
---|
153 | return (0); |
---|
154 | } else { |
---|
155 | return (ENFILE); |
---|
156 | } |
---|
157 | } |
---|
158 | #endif /* __rtems__ */ |
---|
159 | int falloc_noinstall(struct thread *td, struct file **resultfp); |
---|
160 | int finstall(struct thread *td, struct file *fp, int *resultfp, int flags); |
---|
161 | int fdalloc(struct thread *td, int minfd, int *result); |
---|
162 | int fdallocn(struct thread *td, int minfd, int *fds, int n); |
---|
163 | int fdavail(struct thread *td, int n); |
---|
164 | int fdcheckstd(struct thread *td); |
---|
165 | #ifndef __rtems__ |
---|
166 | void fdclose(struct filedesc *fdp, struct file *fp, int idx, struct thread *td); |
---|
167 | #else /* __rtems__ */ |
---|
168 | static inline void |
---|
169 | rtems_bsd_fdclose(struct file *fp, int idx, struct thread *td) |
---|
170 | { |
---|
171 | (void) idx; |
---|
172 | (void) td; |
---|
173 | |
---|
174 | rtems_libio_free(&fp->f_io); |
---|
175 | } |
---|
176 | |
---|
177 | #define fdclose(fdp, fp, idx, td) rtems_bsd_fdclose(fp, idx, td) |
---|
178 | #endif /* __rtems__ */ |
---|
179 | void fdcloseexec(struct thread *td); |
---|
180 | struct filedesc *fdcopy(struct filedesc *fdp); |
---|
181 | void fdunshare(struct proc *p, struct thread *td); |
---|
182 | void fdfree(struct thread *td); |
---|
183 | struct filedesc *fdinit(struct filedesc *fdp); |
---|
184 | struct filedesc *fdshare(struct filedesc *fdp); |
---|
185 | struct filedesc_to_leader * |
---|
186 | filedesc_to_leader_alloc(struct filedesc_to_leader *old, |
---|
187 | struct filedesc *fdp, struct proc *leader); |
---|
188 | int getvnode(struct filedesc *fdp, int fd, cap_rights_t rights, |
---|
189 | struct file **fpp); |
---|
190 | void mountcheckdirs(struct vnode *olddp, struct vnode *newdp); |
---|
191 | void setugidsafety(struct thread *td); |
---|
192 | |
---|
193 | /* Return a referenced file from an unlocked descriptor. */ |
---|
194 | #ifndef __rtems__ |
---|
195 | struct file *fget_unlocked(struct filedesc *fdp, int fd); |
---|
196 | #else /* __rtems__ */ |
---|
197 | static inline struct file * |
---|
198 | fget_unlocked(struct filedesc *fdp, int fd) |
---|
199 | { |
---|
200 | (void) fdp; |
---|
201 | |
---|
202 | return rtems_bsd_get_file(fd); |
---|
203 | } |
---|
204 | #endif /* __rtems__ */ |
---|
205 | |
---|
206 | #ifndef __rtems__ |
---|
207 | /* Requires a FILEDESC_{S,X}LOCK held and returns without a ref. */ |
---|
208 | static __inline struct file * |
---|
209 | fget_locked(struct filedesc *fdp, int fd) |
---|
210 | { |
---|
211 | |
---|
212 | return (fd < 0 || fd >= fdp->fd_nfiles ? NULL : fdp->fd_ofiles[fd]); |
---|
213 | } |
---|
214 | #endif /* __rtems__ */ |
---|
215 | |
---|
216 | #endif /* _KERNEL */ |
---|
217 | |
---|
218 | #endif /* !_SYS_FILEDESC_H_ */ |
---|