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