2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)filedesc.h 8.1 (Berkeley) 6/2/93
35 #ifndef _SYS_FILEDESC_H_
36 #define _SYS_FILEDESC_H_
38 #include <sys/caprights.h>
39 #include <sys/queue.h>
40 #include <sys/event.h>
42 #include <sys/mutex.h>
43 #include <sys/priority.h>
47 #include <sys/smr_types.h>
49 #include <machine/_limits.h>
52 cap_rights_t fc_rights; /* per-descriptor capability rights */
53 u_long *fc_ioctls; /* per-descriptor allowed ioctls */
54 int16_t fc_nioctls; /* fc_ioctls array size */
55 uint32_t fc_fcntls; /* per-descriptor allowed fcntls */
59 struct file *fde_file; /* file structure for open file */
60 struct filecaps fde_caps; /* per-descriptor rights */
61 uint8_t fde_flags; /* per-process open file flags */
62 seqc_t fde_seqc; /* keep file and caps in sync */
64 #define fde_rights fde_caps.fc_rights
65 #define fde_fcntls fde_caps.fc_fcntls
66 #define fde_ioctls fde_caps.fc_ioctls
67 #define fde_nioctls fde_caps.fc_nioctls
68 #define fde_change_size (offsetof(struct filedescent, fde_seqc))
71 int fdt_nfiles; /* number of open files allocated */
72 struct filedescent fdt_ofiles[0]; /* open files */
74 #define fd_seqc(fdt, fd) (&(fdt)->fdt_ofiles[(fd)].fde_seqc)
77 * This structure is used for the management of descriptors. It may be
78 * shared by multiple processes.
80 #define NDSLOTTYPE u_long
83 * This struct is copy-on-write and allocated from an SMR zone.
84 * All fields are constant after initialization apart from the reference count.
86 * Check pwd_* routines for usage.
89 volatile u_int pwd_refcount;
90 struct vnode *pwd_cdir; /* current directory */
91 struct vnode *pwd_rdir; /* root directory */
92 struct vnode *pwd_jdir; /* jail root directory */
94 typedef SMR_POINTER(struct pwd *) smrpwd_t;
97 struct mtx pd_lock; /* protects members of this struct */
98 smrpwd_t pd_pwd; /* directories */
99 volatile u_int pd_refcount;
100 u_short pd_cmask; /* mask for file creation */
104 struct fdescenttbl *fd_files; /* open files table */
105 NDSLOTTYPE *fd_map; /* bitmap of free fds */
106 int fd_freefile; /* approx. next free file */
107 int fd_refcnt; /* thread reference count */
108 int fd_holdcnt; /* hold count on structure + mutex */
109 struct sx fd_sx; /* protects members of this struct */
110 struct kqlist fd_kqlist; /* list of kqueues on this filedesc */
111 int fd_holdleaderscount; /* block fdfree() for shared close() */
112 int fd_holdleaderswakeup; /* fdfree() needs wakeup */
116 * Structure to keep track of (process leader, struct fildedesc) tuples.
117 * Each process has a pointer to such a structure when detailed tracking
118 * is needed, e.g., when rfork(RFPROC | RFMEM) causes a file descriptor
119 * table to be shared by processes having different "p_leader" pointers
120 * and thus distinct POSIX style locks.
122 * fdl_refcount and fdl_holdcount are protected by struct filedesc mtx.
124 struct filedesc_to_leader {
125 int fdl_refcount; /* references from struct proc */
126 int fdl_holdcount; /* temporary hold during closef */
127 int fdl_wakeup; /* fdfree() waits on closef() */
128 struct proc *fdl_leader; /* owner of POSIX locks */
130 struct filedesc_to_leader *fdl_prev;
131 struct filedesc_to_leader *fdl_next;
133 #define fd_nfiles fd_files->fdt_nfiles
134 #define fd_ofiles fd_files->fdt_ofiles
137 * Per-process open flags.
139 #define UF_EXCLOSE 0x01 /* auto-close on exec */
143 /* Lock a paths descriptor table. */
144 #define PWDDESC_LOCK(pdp) (&(pdp)->pd_lock)
145 #define PWDDESC_LOCK_INIT(pdp) \
146 mtx_init(PWDDESC_LOCK(pdp), "pwddesc", NULL, MTX_DEF)
147 #define PWDDESC_LOCK_DESTROY(pdp) mtx_destroy(PWDDESC_LOCK(pdp))
148 #define PWDDESC_XLOCK(pdp) mtx_lock(PWDDESC_LOCK(pdp))
149 #define PWDDESC_XUNLOCK(pdp) mtx_unlock(PWDDESC_LOCK(pdp))
150 #define PWDDESC_LOCK_ASSERT(pdp, what) \
151 mtx_assert(PWDDESC_LOCK(pdp), (what))
152 #define PWDDESC_ASSERT_XLOCKED(pdp) \
153 PWDDESC_LOCK_ASSERT((pdp), MA_OWNED)
154 #define PWDDESC_ASSERT_UNLOCKED(pdp) \
155 PWDDESC_LOCK_ASSERT((pdp), MA_NOTOWNED)
157 #define PWDDESC_XLOCKED_LOAD_PWD(pdp) ({ \
158 struct pwddesc *_pdp = (pdp); \
160 _pwd = smr_serialized_load(&(_pdp)->pd_pwd, \
161 (PWDDESC_ASSERT_XLOCKED(_pdp), true)); \
165 /* Lock a file descriptor table. */
166 #define FILEDESC_LOCK_INIT(fdp) sx_init(&(fdp)->fd_sx, "filedesc structure")
167 #define FILEDESC_LOCK_DESTROY(fdp) sx_destroy(&(fdp)->fd_sx)
168 #define FILEDESC_LOCK(fdp) (&(fdp)->fd_sx)
169 #define FILEDESC_XLOCK(fdp) sx_xlock(&(fdp)->fd_sx)
170 #define FILEDESC_XUNLOCK(fdp) sx_xunlock(&(fdp)->fd_sx)
171 #define FILEDESC_SLOCK(fdp) sx_slock(&(fdp)->fd_sx)
172 #define FILEDESC_SUNLOCK(fdp) sx_sunlock(&(fdp)->fd_sx)
174 #define FILEDESC_LOCK_ASSERT(fdp) sx_assert(&(fdp)->fd_sx, SX_LOCKED | \
176 #define FILEDESC_XLOCK_ASSERT(fdp) sx_assert(&(fdp)->fd_sx, SX_XLOCKED | \
178 #define FILEDESC_UNLOCK_ASSERT(fdp) sx_assert(&(fdp)->fd_sx, SX_UNLOCKED)
183 * Accessor for libkvm et al.
185 #define PWDDESC_KVM_LOAD_PWD(pdp) ({ \
186 struct pwddesc *_pdp = (pdp); \
188 _pwd = smr_kvm_load(&(_pdp)->pd_pwd); \
196 /* Operation types for kern_dup(). */
198 FDDUP_NORMAL, /* dup() behavior. */
199 FDDUP_FCNTL, /* fcntl()-style errors. */
200 FDDUP_FIXED, /* Force fixed allocation. */
201 FDDUP_MUSTREPLACE, /* Target must exist. */
205 /* Flags for kern_dup(). */
206 #define FDDUP_FLAG_CLOEXEC 0x1 /* Atomically set UF_EXCLOSE. */
208 /* For backward compatibility. */
209 #define falloc(td, resultfp, resultfd, flags) \
210 falloc_caps(td, resultfp, resultfd, flags, NULL)
215 filecaps_init(struct filecaps *fcaps)
218 bzero(fcaps, sizeof(*fcaps));
219 fcaps->fc_nioctls = -1;
221 bool filecaps_copy(const struct filecaps *src, struct filecaps *dst,
223 void filecaps_move(struct filecaps *src, struct filecaps *dst);
224 void filecaps_free(struct filecaps *fcaps);
226 int closef(struct file *fp, struct thread *td);
227 int dupfdopen(struct thread *td, struct filedesc *fdp, int dfd, int mode,
228 int openerror, int *indxp);
229 int falloc_caps(struct thread *td, struct file **resultfp, int *resultfd,
230 int flags, struct filecaps *fcaps);
231 int falloc_noinstall(struct thread *td, struct file **resultfp);
232 void _finstall(struct filedesc *fdp, struct file *fp, int fd, int flags,
233 struct filecaps *fcaps);
234 int finstall(struct thread *td, struct file *fp, int *resultfd, int flags,
235 struct filecaps *fcaps);
236 int fdalloc(struct thread *td, int minfd, int *result);
237 int fdallocn(struct thread *td, int minfd, int *fds, int n);
238 int fdcheckstd(struct thread *td);
239 void fdclose(struct thread *td, struct file *fp, int idx);
240 void fdcloseexec(struct thread *td);
241 void fdsetugidsafety(struct thread *td);
242 struct filedesc *fdcopy(struct filedesc *fdp);
243 int fdcopy_remapped(struct filedesc *fdp, const int *fds, size_t nfds,
244 struct filedesc **newfdp);
245 void fdinstall_remapped(struct thread *td, struct filedesc *fdp);
246 void fdunshare(struct thread *td);
247 void fdescfree(struct thread *td);
248 void fdescfree_remapped(struct filedesc *fdp);
249 int fdlastfile(struct filedesc *fdp);
250 int fdlastfile_single(struct filedesc *fdp);
251 struct filedesc *fdinit(struct filedesc *fdp, bool prepfiles, int *lastfile);
252 struct filedesc *fdshare(struct filedesc *fdp);
253 struct filedesc_to_leader *
254 filedesc_to_leader_alloc(struct filedesc_to_leader *old,
255 struct filedesc *fdp, struct proc *leader);
256 int getvnode(struct thread *td, int fd, cap_rights_t *rightsp,
258 void mountcheckdirs(struct vnode *olddp, struct vnode *newdp);
260 int fget_cap_locked(struct filedesc *fdp, int fd, cap_rights_t *needrightsp,
261 struct file **fpp, struct filecaps *havecapsp);
262 int fget_cap(struct thread *td, int fd, cap_rights_t *needrightsp,
263 struct file **fpp, struct filecaps *havecapsp);
265 /* Return a referenced file from an unlocked descriptor. */
266 int fget_unlocked_seq(struct filedesc *fdp, int fd, cap_rights_t *needrightsp,
267 struct file **fpp, seqc_t *seqp);
268 int fget_unlocked(struct filedesc *fdp, int fd, cap_rights_t *needrightsp,
271 /* Requires a FILEDESC_{S,X}LOCK held and returns without a ref. */
272 static __inline struct file *
273 fget_locked(struct filedesc *fdp, int fd)
276 FILEDESC_LOCK_ASSERT(fdp);
278 if (__predict_false((u_int)fd >= fdp->fd_nfiles))
281 return (fdp->fd_ofiles[fd].fde_file);
284 static __inline struct filedescent *
285 fdeget_locked(struct filedesc *fdp, int fd)
287 struct filedescent *fde;
289 FILEDESC_LOCK_ASSERT(fdp);
291 if (__predict_false((u_int)fd >= fdp->fd_nfiles))
294 fde = &fdp->fd_ofiles[fd];
295 if (__predict_false(fde->fde_file == NULL))
303 fd_modified(struct filedesc *fdp, int fd, seqc_t seqc)
306 return (!seqc_consistent(fd_seqc(fdp->fd_files, fd), seqc));
310 /* cdir/rdir/jdir manipulation functions. */
311 struct pwddesc *pdcopy(struct pwddesc *pdp);
312 void pdescfree(struct thread *td);
313 struct pwddesc *pdinit(struct pwddesc *pdp, bool keeplock);
314 struct pwddesc *pdshare(struct pwddesc *pdp);
315 void pdunshare(struct thread *td);
317 void pwd_chdir(struct thread *td, struct vnode *vp);
318 int pwd_chroot(struct thread *td, struct vnode *vp);
319 void pwd_ensure_dirs(void);
320 void pwd_set_rootvnode(void);
322 struct pwd *pwd_hold_pwddesc(struct pwddesc *pdp);
323 bool pwd_hold_smr(struct pwd *pwd);
324 struct pwd *pwd_hold(struct thread *td);
325 void pwd_drop(struct pwd *pwd);
327 pwd_set(struct pwddesc *pdp, struct pwd *newpwd)
329 smr_serialized_store(&pdp->pd_pwd, newpwd,
330 (PWDDESC_ASSERT_XLOCKED(pdp), true));
332 struct pwd *pwd_get_smr(void);
336 #endif /* !_SYS_FILEDESC_H_ */