]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/filedesc.h
Merge ^/head r358466 through r358677.
[FreeBSD/FreeBSD.git] / sys / sys / filedesc.h
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1990, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  *
31  *      @(#)filedesc.h  8.1 (Berkeley) 6/2/93
32  * $FreeBSD$
33  */
34
35 #ifndef _SYS_FILEDESC_H_
36 #define _SYS_FILEDESC_H_
37
38 #include <sys/caprights.h>
39 #include <sys/queue.h>
40 #include <sys/event.h>
41 #include <sys/lock.h>
42 #include <sys/priority.h>
43 #include <sys/seqc.h>
44 #include <sys/sx.h>
45
46 #include <machine/_limits.h>
47
48 struct filecaps {
49         cap_rights_t     fc_rights;     /* per-descriptor capability rights */
50         u_long          *fc_ioctls;     /* per-descriptor allowed ioctls */
51         int16_t          fc_nioctls;    /* fc_ioctls array size */
52         uint32_t         fc_fcntls;     /* per-descriptor allowed fcntls */
53 };
54
55 struct filedescent {
56         struct file     *fde_file;      /* file structure for open file */
57         struct filecaps  fde_caps;      /* per-descriptor rights */
58         uint8_t          fde_flags;     /* per-process open file flags */
59         seqc_t           fde_seqc;      /* keep file and caps in sync */
60 };
61 #define fde_rights      fde_caps.fc_rights
62 #define fde_fcntls      fde_caps.fc_fcntls
63 #define fde_ioctls      fde_caps.fc_ioctls
64 #define fde_nioctls     fde_caps.fc_nioctls
65 #define fde_change_size (offsetof(struct filedescent, fde_seqc))
66
67 struct fdescenttbl {
68         int     fdt_nfiles;             /* number of open files allocated */
69         struct  filedescent fdt_ofiles[0];      /* open files */
70 };
71 #define fd_seqc(fdt, fd)        (&(fdt)->fdt_ofiles[(fd)].fde_seqc)
72
73 /*
74  * This structure is used for the management of descriptors.  It may be
75  * shared by multiple processes.
76  */
77 #define NDSLOTTYPE      u_long
78
79 struct pwd {
80         volatile u_int pwd_refcount;
81         struct  vnode *pwd_cdir;                /* current directory */
82         struct  vnode *pwd_rdir;                /* root directory */
83         struct  vnode *pwd_jdir;                /* jail root directory */
84 };
85
86 struct filedesc {
87         struct  fdescenttbl *fd_files;  /* open files table */
88         struct  pwd *fd_pwd;            /* directories */
89         NDSLOTTYPE *fd_map;             /* bitmap of free fds */
90         int     fd_lastfile;            /* high-water mark of fd_ofiles */
91         int     fd_freefile;            /* approx. next free file */
92         u_short fd_cmask;               /* mask for file creation */
93         int     fd_refcnt;              /* thread reference count */
94         int     fd_holdcnt;             /* hold count on structure + mutex */
95         struct  sx fd_sx;               /* protects members of this struct */
96         struct  kqlist fd_kqlist;       /* list of kqueues on this filedesc */
97         int     fd_holdleaderscount;    /* block fdfree() for shared close() */
98         int     fd_holdleaderswakeup;   /* fdfree() needs wakeup */
99 };
100
101 /*
102  * Structure to keep track of (process leader, struct fildedesc) tuples.
103  * Each process has a pointer to such a structure when detailed tracking
104  * is needed, e.g., when rfork(RFPROC | RFMEM) causes a file descriptor
105  * table to be shared by processes having different "p_leader" pointers
106  * and thus distinct POSIX style locks.
107  *
108  * fdl_refcount and fdl_holdcount are protected by struct filedesc mtx.
109  */
110 struct filedesc_to_leader {
111         int             fdl_refcount;   /* references from struct proc */
112         int             fdl_holdcount;  /* temporary hold during closef */
113         int             fdl_wakeup;     /* fdfree() waits on closef() */
114         struct proc     *fdl_leader;    /* owner of POSIX locks */
115         /* Circular list: */
116         struct filedesc_to_leader *fdl_prev;
117         struct filedesc_to_leader *fdl_next;
118 };
119 #define fd_nfiles       fd_files->fdt_nfiles
120 #define fd_ofiles       fd_files->fdt_ofiles
121
122 /*
123  * Per-process open flags.
124  */
125 #define UF_EXCLOSE      0x01            /* auto-close on exec */
126
127 #ifdef _KERNEL
128
129 /* Lock a file descriptor table. */
130 #define FILEDESC_LOCK_INIT(fdp) sx_init(&(fdp)->fd_sx, "filedesc structure")
131 #define FILEDESC_LOCK_DESTROY(fdp)      sx_destroy(&(fdp)->fd_sx)
132 #define FILEDESC_LOCK(fdp)      (&(fdp)->fd_sx)
133 #define FILEDESC_XLOCK(fdp)     sx_xlock(&(fdp)->fd_sx)
134 #define FILEDESC_XUNLOCK(fdp)   sx_xunlock(&(fdp)->fd_sx)
135 #define FILEDESC_SLOCK(fdp)     sx_slock(&(fdp)->fd_sx)
136 #define FILEDESC_SUNLOCK(fdp)   sx_sunlock(&(fdp)->fd_sx)
137
138 #define FILEDESC_LOCK_ASSERT(fdp)       sx_assert(&(fdp)->fd_sx, SX_LOCKED | \
139                                             SX_NOTRECURSED)
140 #define FILEDESC_XLOCK_ASSERT(fdp)      sx_assert(&(fdp)->fd_sx, SX_XLOCKED | \
141                                             SX_NOTRECURSED)
142 #define FILEDESC_UNLOCK_ASSERT(fdp)     sx_assert(&(fdp)->fd_sx, SX_UNLOCKED)
143
144 /* Operation types for kern_dup(). */
145 enum {
146         FDDUP_NORMAL,           /* dup() behavior. */
147         FDDUP_FCNTL,            /* fcntl()-style errors. */
148         FDDUP_FIXED,            /* Force fixed allocation. */
149         FDDUP_MUSTREPLACE,      /* Target must exist. */
150         FDDUP_LASTMODE,
151 };
152
153 /* Flags for kern_dup(). */
154 #define FDDUP_FLAG_CLOEXEC      0x1     /* Atomically set UF_EXCLOSE. */
155
156 /* For backward compatibility. */
157 #define falloc(td, resultfp, resultfd, flags) \
158         falloc_caps(td, resultfp, resultfd, flags, NULL)
159
160 struct thread;
161
162 static __inline void
163 filecaps_init(struct filecaps *fcaps)
164 {
165
166         bzero(fcaps, sizeof(*fcaps));
167         fcaps->fc_nioctls = -1;
168 }
169 bool    filecaps_copy(const struct filecaps *src, struct filecaps *dst,
170             bool locked);
171 void    filecaps_move(struct filecaps *src, struct filecaps *dst);
172 void    filecaps_free(struct filecaps *fcaps);
173
174 int     closef(struct file *fp, struct thread *td);
175 int     dupfdopen(struct thread *td, struct filedesc *fdp, int dfd, int mode,
176             int openerror, int *indxp);
177 int     falloc_caps(struct thread *td, struct file **resultfp, int *resultfd,
178             int flags, struct filecaps *fcaps);
179 int     falloc_noinstall(struct thread *td, struct file **resultfp);
180 void    _finstall(struct filedesc *fdp, struct file *fp, int fd, int flags,
181             struct filecaps *fcaps);
182 int     finstall(struct thread *td, struct file *fp, int *resultfd, int flags,
183             struct filecaps *fcaps);
184 int     fdalloc(struct thread *td, int minfd, int *result);
185 int     fdallocn(struct thread *td, int minfd, int *fds, int n);
186 int     fdcheckstd(struct thread *td);
187 void    fdclose(struct thread *td, struct file *fp, int idx);
188 void    fdcloseexec(struct thread *td);
189 void    fdsetugidsafety(struct thread *td);
190 struct  filedesc *fdcopy(struct filedesc *fdp);
191 int     fdcopy_remapped(struct filedesc *fdp, const int *fds, size_t nfds,
192             struct filedesc **newfdp);
193 void    fdinstall_remapped(struct thread *td, struct filedesc *fdp);
194 void    fdunshare(struct thread *td);
195 void    fdescfree(struct thread *td);
196 void    fdescfree_remapped(struct filedesc *fdp);
197 struct  filedesc *fdinit(struct filedesc *fdp, bool prepfiles);
198 struct  filedesc *fdshare(struct filedesc *fdp);
199 struct filedesc_to_leader *
200         filedesc_to_leader_alloc(struct filedesc_to_leader *old,
201             struct filedesc *fdp, struct proc *leader);
202 int     getvnode(struct thread *td, int fd, cap_rights_t *rightsp,
203             struct file **fpp);
204 void    mountcheckdirs(struct vnode *olddp, struct vnode *newdp);
205
206 int     fget_cap_locked(struct filedesc *fdp, int fd, cap_rights_t *needrightsp,
207             struct file **fpp, struct filecaps *havecapsp);
208 int     fget_cap(struct thread *td, int fd, cap_rights_t *needrightsp,
209             struct file **fpp, struct filecaps *havecapsp);
210
211 /* Return a referenced file from an unlocked descriptor. */
212 int     fget_unlocked_seq(struct filedesc *fdp, int fd, cap_rights_t *needrightsp,
213             struct file **fpp, seqc_t *seqp);
214 int     fget_unlocked(struct filedesc *fdp, int fd, cap_rights_t *needrightsp,
215             struct file **fpp);
216
217 /* Requires a FILEDESC_{S,X}LOCK held and returns without a ref. */
218 static __inline struct file *
219 fget_locked(struct filedesc *fdp, int fd)
220 {
221
222         FILEDESC_LOCK_ASSERT(fdp);
223
224         if (__predict_false((u_int)fd >= fdp->fd_nfiles))
225                 return (NULL);
226
227         return (fdp->fd_ofiles[fd].fde_file);
228 }
229
230 static __inline struct filedescent *
231 fdeget_locked(struct filedesc *fdp, int fd)
232 {
233         struct filedescent *fde;
234
235         FILEDESC_LOCK_ASSERT(fdp);
236
237         if (__predict_false((u_int)fd >= fdp->fd_nfiles))
238                 return (NULL);
239
240         fde = &fdp->fd_ofiles[fd];
241         if (__predict_false(fde->fde_file == NULL))
242                 return (NULL);
243
244         return (fde);
245 }
246
247 #ifdef CAPABILITIES
248 static __inline bool
249 fd_modified(struct filedesc *fdp, int fd, seqc_t seqc)
250 {
251
252         return (!seqc_consistent(fd_seqc(fdp->fd_files, fd), seqc));
253 }
254 #endif
255
256 /* cdir/rdir/jdir manipulation functions. */
257 void    pwd_chdir(struct thread *td, struct vnode *vp);
258 int     pwd_chroot(struct thread *td, struct vnode *vp);
259 void    pwd_ensure_dirs(void);
260
261 struct pwd *pwd_hold_filedesc(struct filedesc *fdp);
262 struct pwd *pwd_hold(struct thread *td);
263 void    pwd_drop(struct pwd *pwd);
264 static inline void
265 pwd_set(struct filedesc *fdp, struct pwd *newpwd)
266 {
267
268         FILEDESC_XLOCK_ASSERT(fdp);
269         fdp->fd_pwd = newpwd;
270 }
271
272 #endif /* _KERNEL */
273
274 #endif /* !_SYS_FILEDESC_H_ */