xref: /xnu-11417.140.69/bsd/sys/filedesc.h (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1*43a90889SApple OSS Distributions /*
2*43a90889SApple OSS Distributions  * Copyright (c) 2000-2012 Apple Computer, Inc. All rights reserved.
3*43a90889SApple OSS Distributions  *
4*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*43a90889SApple OSS Distributions  *
6*43a90889SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*43a90889SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*43a90889SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*43a90889SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*43a90889SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*43a90889SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*43a90889SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*43a90889SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*43a90889SApple OSS Distributions  *
15*43a90889SApple OSS Distributions  * Please obtain a copy of the License at
16*43a90889SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*43a90889SApple OSS Distributions  *
18*43a90889SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*43a90889SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*43a90889SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*43a90889SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*43a90889SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*43a90889SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*43a90889SApple OSS Distributions  * limitations under the License.
25*43a90889SApple OSS Distributions  *
26*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*43a90889SApple OSS Distributions  */
28*43a90889SApple OSS Distributions /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
29*43a90889SApple OSS Distributions /*
30*43a90889SApple OSS Distributions  * Copyright (c) 1990, 1993
31*43a90889SApple OSS Distributions  *	The Regents of the University of California.  All rights reserved.
32*43a90889SApple OSS Distributions  *
33*43a90889SApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
34*43a90889SApple OSS Distributions  * modification, are permitted provided that the following conditions
35*43a90889SApple OSS Distributions  * are met:
36*43a90889SApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
37*43a90889SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer.
38*43a90889SApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
39*43a90889SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer in the
40*43a90889SApple OSS Distributions  *    documentation and/or other materials provided with the distribution.
41*43a90889SApple OSS Distributions  * 3. All advertising materials mentioning features or use of this software
42*43a90889SApple OSS Distributions  *    must display the following acknowledgement:
43*43a90889SApple OSS Distributions  *	This product includes software developed by the University of
44*43a90889SApple OSS Distributions  *	California, Berkeley and its contributors.
45*43a90889SApple OSS Distributions  * 4. Neither the name of the University nor the names of its contributors
46*43a90889SApple OSS Distributions  *    may be used to endorse or promote products derived from this software
47*43a90889SApple OSS Distributions  *    without specific prior written permission.
48*43a90889SApple OSS Distributions  *
49*43a90889SApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50*43a90889SApple OSS Distributions  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51*43a90889SApple OSS Distributions  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52*43a90889SApple OSS Distributions  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53*43a90889SApple OSS Distributions  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54*43a90889SApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55*43a90889SApple OSS Distributions  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56*43a90889SApple OSS Distributions  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57*43a90889SApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58*43a90889SApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59*43a90889SApple OSS Distributions  * SUCH DAMAGE.
60*43a90889SApple OSS Distributions  *
61*43a90889SApple OSS Distributions  *	@(#)filedesc.h	8.1 (Berkeley) 6/2/93
62*43a90889SApple OSS Distributions  */
63*43a90889SApple OSS Distributions 
64*43a90889SApple OSS Distributions #ifndef _SYS_FILEDESC_H_
65*43a90889SApple OSS Distributions #define _SYS_FILEDESC_H_
66*43a90889SApple OSS Distributions 
67*43a90889SApple OSS Distributions #include <sys/appleapiopts.h>
68*43a90889SApple OSS Distributions 
69*43a90889SApple OSS Distributions /*
70*43a90889SApple OSS Distributions  * This structure is used for the management of descriptors.  It may be
71*43a90889SApple OSS Distributions  * shared by multiple processes.
72*43a90889SApple OSS Distributions  *
73*43a90889SApple OSS Distributions  * A process is initially started out with NDFILE descriptors [XXXstored within
74*43a90889SApple OSS Distributions  * this structureXXX], selected to be enough for typical applications based on
75*43a90889SApple OSS Distributions  * the historical limit of 20 open files (and the usage of descriptors by
76*43a90889SApple OSS Distributions  * shells).  If these descriptors are exhausted, a larger descriptor table
77*43a90889SApple OSS Distributions  * may be allocated, up to a process' resource limit; [XXXthe internal arrays
78*43a90889SApple OSS Distributions  * are then unusedXXX].  The initial expansion is set to NDEXTENT; each time
79*43a90889SApple OSS Distributions  * it runs out, it is doubled until the resource limit is reached. NDEXTENT
80*43a90889SApple OSS Distributions  * should be selected to be the biggest multiple of OFILESIZE (see below)
81*43a90889SApple OSS Distributions  * that will fit in a power-of-two sized piece of memory.
82*43a90889SApple OSS Distributions  */
83*43a90889SApple OSS Distributions #define NDFILE          25              /* 125 bytes */
84*43a90889SApple OSS Distributions #define NDEXTENT        50              /* 250 bytes in 256-byte alloc. */
85*43a90889SApple OSS Distributions 
86*43a90889SApple OSS Distributions #ifdef XNU_KERNEL_PRIVATE
87*43a90889SApple OSS Distributions 
88*43a90889SApple OSS Distributions #include <sys/kernel_types.h>
89*43a90889SApple OSS Distributions #include <kern/locks.h>
90*43a90889SApple OSS Distributions 
91*43a90889SApple OSS Distributions struct klist;
92*43a90889SApple OSS Distributions struct kqwllist;
93*43a90889SApple OSS Distributions struct ucred;
94*43a90889SApple OSS Distributions 
95*43a90889SApple OSS Distributions __options_decl(filedesc_flags_t, uint8_t, {
96*43a90889SApple OSS Distributions 	/*
97*43a90889SApple OSS Distributions 	 * process was chrooted... keep track even
98*43a90889SApple OSS Distributions 	 * if we're force unmounted and unable to
99*43a90889SApple OSS Distributions 	 * take a vnode_ref on fd_rdir during a fork
100*43a90889SApple OSS Distributions 	 */
101*43a90889SApple OSS Distributions 	FD_CHROOT                     = 0x01,
102*43a90889SApple OSS Distributions 
103*43a90889SApple OSS Distributions 	/*
104*43a90889SApple OSS Distributions 	 * process has created a kqworkloop that
105*43a90889SApple OSS Distributions 	 * requires manual cleanup on exit
106*43a90889SApple OSS Distributions 	 */
107*43a90889SApple OSS Distributions 	FD_WORKLOOP                   = 0x02,
108*43a90889SApple OSS Distributions 
109*43a90889SApple OSS Distributions #if CONFIG_PROC_RESOURCE_LIMITS
110*43a90889SApple OSS Distributions 	/* process has exceeded fd_nfiles soft limit */
111*43a90889SApple OSS Distributions 	FD_ABOVE_SOFT_LIMIT           = 0x04,
112*43a90889SApple OSS Distributions 	/* process has exceeded fd_nfiles hard limit */
113*43a90889SApple OSS Distributions 	FD_ABOVE_HARD_LIMIT           = 0x08,
114*43a90889SApple OSS Distributions 	KQWL_ABOVE_SOFT_LIMIT         = 0x10,
115*43a90889SApple OSS Distributions 	KQWL_ABOVE_HARD_LIMIT         = 0x20,
116*43a90889SApple OSS Distributions #endif /* CONFIG_PROC_RESOURCE_LIMITS */
117*43a90889SApple OSS Distributions });
118*43a90889SApple OSS Distributions 
119*43a90889SApple OSS Distributions #define FILEDESC_FORK_INHERITED_MASK (FD_CHROOT)
120*43a90889SApple OSS Distributions 
121*43a90889SApple OSS Distributions struct filedesc {
122*43a90889SApple OSS Distributions 	lck_mtx_t           fd_lock;        /* (L) lock to protect fdesc */
123*43a90889SApple OSS Distributions 	uint8_t             fd_fpdrainwait; /* (L) has drain waiters */
124*43a90889SApple OSS Distributions 	filedesc_flags_t    fd_flags;       /* (L) filedesc flags */
125*43a90889SApple OSS Distributions 	u_short             fd_cmask;       /* (L) mask for file creation */
126*43a90889SApple OSS Distributions 	int                 fd_nfiles;      /* (L) number of open fdesc slots allocated */
127*43a90889SApple OSS Distributions 	int                 fd_afterlast;   /* (L) high-water mark of fd_ofiles */
128*43a90889SApple OSS Distributions 	int                 fd_freefile;    /* (L) approx. next free file */
129*43a90889SApple OSS Distributions #if CONFIG_PROC_RESOURCE_LIMITS
130*43a90889SApple OSS Distributions #define FD_LIMIT_SENTINEL ((int) (-1))
131*43a90889SApple OSS Distributions 	int                 fd_nfiles_open;
132*43a90889SApple OSS Distributions 	int                 fd_nfiles_soft_limit; /* (L) fd_nfiles soft limit to trigger guard. */
133*43a90889SApple OSS Distributions 	int                 fd_nfiles_hard_limit; /* (L) fd_nfiles hard limit to terminate. */
134*43a90889SApple OSS Distributions 
135*43a90889SApple OSS Distributions #define KQWL_LIMIT_SENTINEL ((int) (-1))
136*43a90889SApple OSS Distributions 	int                 num_kqwls;           /* Number of kqwls in the fd_kqhash */
137*43a90889SApple OSS Distributions 	int                 kqwl_dyn_soft_limit; /* (L) soft limit for dynamic kqueue */
138*43a90889SApple OSS Distributions 	int                 kqwl_dyn_hard_limit; /* (L) hard limit for dynamic kqueue */
139*43a90889SApple OSS Distributions #endif /* CONFIG_PROC_RESOURCE_LIMITS */
140*43a90889SApple OSS Distributions 
141*43a90889SApple OSS Distributions 	int                 fd_knlistsize;  /* (L) size of knlist */
142*43a90889SApple OSS Distributions 	int                 unused_padding;/* Due to alignment */
143*43a90889SApple OSS Distributions 	struct fileproc   **XNU_PTRAUTH_SIGNED_PTR("filedesc.fd_ofiles") fd_ofiles; /* (L) file structures for open files */
144*43a90889SApple OSS Distributions 	char               *fd_ofileflags;  /* (L) per-process open file flags */
145*43a90889SApple OSS Distributions 
146*43a90889SApple OSS Distributions 	struct  klist      *fd_knlist;      /* (L) list of attached knotes */
147*43a90889SApple OSS Distributions 
148*43a90889SApple OSS Distributions 	struct  kqworkq    *fd_wqkqueue;    /* (L) the workq kqueue */
149*43a90889SApple OSS Distributions 	struct  vnode      *fd_cdir;        /* (L) current directory */
150*43a90889SApple OSS Distributions 	struct  vnode      *fd_rdir;        /* (L) root directory */
151*43a90889SApple OSS Distributions 	lck_rw_t            fd_dirs_lock;   /* keeps fd_cdir and fd_rdir stable across a lookup */
152*43a90889SApple OSS Distributions 
153*43a90889SApple OSS Distributions 	lck_mtx_t           fd_kqhashlock;  /* (Q) lock for dynamic kqueue hash */
154*43a90889SApple OSS Distributions 	u_long              fd_kqhashmask;  /* (Q) size of dynamic kqueue hash */
155*43a90889SApple OSS Distributions 	struct  kqwllist   *fd_kqhash;      /* (Q) hash table for dynamic kqueues */
156*43a90889SApple OSS Distributions 
157*43a90889SApple OSS Distributions 	lck_mtx_t           fd_knhashlock;  /* (N) lock for hash table for attached knotes */
158*43a90889SApple OSS Distributions 	u_long              fd_knhashmask;  /* (N) size of knhash */
159*43a90889SApple OSS Distributions 	struct  klist      *fd_knhash;      /* (N) hash table for attached knotes */
160*43a90889SApple OSS Distributions };
161*43a90889SApple OSS Distributions 
162*43a90889SApple OSS Distributions #define fdt_flag_test(fdt, flag)        (((fdt)->fd_flags & (flag)) != 0)
163*43a90889SApple OSS Distributions #define fdt_flag_set(fdt, flag)         ((void)((fdt)->fd_flags |= (flag)))
164*43a90889SApple OSS Distributions #define fdt_flag_clear(fdt, flag)       ((void)((fdt)->fd_flags &= ~(flag)))
165*43a90889SApple OSS Distributions 
166*43a90889SApple OSS Distributions #if CONFIG_PROC_RESOURCE_LIMITS
167*43a90889SApple OSS Distributions #define fd_above_soft_limit_notified(fdp)                 fdt_flag_test(fdp, FD_ABOVE_SOFT_LIMIT)
168*43a90889SApple OSS Distributions #define fd_above_hard_limit_notified(fdp)                 fdt_flag_test(fdp, FD_ABOVE_HARD_LIMIT)
169*43a90889SApple OSS Distributions #define fd_above_soft_limit_send_notification(fdp)      fdt_flag_set(fdp, FD_ABOVE_SOFT_LIMIT)
170*43a90889SApple OSS Distributions #define fd_above_hard_limit_send_notification(fdp)      fdt_flag_set(fdp, FD_ABOVE_HARD_LIMIT)
171*43a90889SApple OSS Distributions 
172*43a90889SApple OSS Distributions #define kqwl_above_soft_limit_notified(fdp)              fdt_flag_test(fdp, KQWL_ABOVE_SOFT_LIMIT)
173*43a90889SApple OSS Distributions #define kqwl_above_hard_limit_notified(fdp)              fdt_flag_test(fdp, KQWL_ABOVE_HARD_LIMIT)
174*43a90889SApple OSS Distributions #define kqwl_above_soft_limit_send_notification(fdp)     fdt_flag_set(fdp, KQWL_ABOVE_SOFT_LIMIT)
175*43a90889SApple OSS Distributions #define kqwl_above_hard_limit_send_notification(fdp)     fdt_flag_set(fdp, KQWL_ABOVE_HARD_LIMIT)
176*43a90889SApple OSS Distributions #endif /* CONFIG_PROC_RESOURCE_LIMITS */
177*43a90889SApple OSS Distributions 
178*43a90889SApple OSS Distributions /*
179*43a90889SApple OSS Distributions  * Per-process open flags.
180*43a90889SApple OSS Distributions  */
181*43a90889SApple OSS Distributions #define UF_RESERVED     0x04            /* open pending / in progress */
182*43a90889SApple OSS Distributions #define UF_CLOSING      0x08            /* close in progress */
183*43a90889SApple OSS Distributions #define UF_RESVWAIT     0x10            /* close in progress */
184*43a90889SApple OSS Distributions #define UF_INHERIT      0x20            /* "inherit-on-exec" */
185*43a90889SApple OSS Distributions 
186*43a90889SApple OSS Distributions /*
187*43a90889SApple OSS Distributions  * Storage required per open file descriptor.
188*43a90889SApple OSS Distributions  */
189*43a90889SApple OSS Distributions #define OFILESIZE (sizeof(struct file *) + sizeof(char))
190*43a90889SApple OSS Distributions 
191*43a90889SApple OSS Distributions /*!
192*43a90889SApple OSS Distributions  * @function fdt_available
193*43a90889SApple OSS Distributions  *
194*43a90889SApple OSS Distributions  * @brief
195*43a90889SApple OSS Distributions  * Returns whether the file descritor table can accomodate
196*43a90889SApple OSS Distributions  * for @c n new entries.
197*43a90889SApple OSS Distributions  *
198*43a90889SApple OSS Distributions  * @discussion
199*43a90889SApple OSS Distributions  * The answer is only valid so long as the @c proc_fdlock() is held by the
200*43a90889SApple OSS Distributions  * caller.
201*43a90889SApple OSS Distributions  */
202*43a90889SApple OSS Distributions extern bool
203*43a90889SApple OSS Distributions fdt_available_locked(proc_t p, int n);
204*43a90889SApple OSS Distributions 
205*43a90889SApple OSS Distributions /*!
206*43a90889SApple OSS Distributions  * @struct fdt_iterator
207*43a90889SApple OSS Distributions  *
208*43a90889SApple OSS Distributions  * @brief
209*43a90889SApple OSS Distributions  * Type used to iterate a file descriptor table.
210*43a90889SApple OSS Distributions  */
211*43a90889SApple OSS Distributions struct fdt_iterator {
212*43a90889SApple OSS Distributions 	int              fdti_fd;
213*43a90889SApple OSS Distributions 	struct fileproc *fdti_fp;
214*43a90889SApple OSS Distributions };
215*43a90889SApple OSS Distributions 
216*43a90889SApple OSS Distributions /*!
217*43a90889SApple OSS Distributions  * @function fdt_next
218*43a90889SApple OSS Distributions  *
219*43a90889SApple OSS Distributions  * @brief
220*43a90889SApple OSS Distributions  * Seek the iterator forward.
221*43a90889SApple OSS Distributions  *
222*43a90889SApple OSS Distributions  * @discussion
223*43a90889SApple OSS Distributions  * The @c proc_fdlock() should be held by the caller.
224*43a90889SApple OSS Distributions  *
225*43a90889SApple OSS Distributions  * @param p
226*43a90889SApple OSS Distributions  * The process for which the file descriptor table is being iterated.
227*43a90889SApple OSS Distributions  *
228*43a90889SApple OSS Distributions  * @param fd
229*43a90889SApple OSS Distributions  * The current file file descriptor to scan from (exclusive).
230*43a90889SApple OSS Distributions  *
231*43a90889SApple OSS Distributions  * @param only_settled
232*43a90889SApple OSS Distributions  * When true, only fileprocs with @c UF_RESERVED set are returned.
233*43a90889SApple OSS Distributions  * If false, fileprocs that are in flux (@c UF_RESERVED is set) are returned.
234*43a90889SApple OSS Distributions  *
235*43a90889SApple OSS Distributions  * @returns
236*43a90889SApple OSS Distributions  * The next iterator position.
237*43a90889SApple OSS Distributions  * If @c fdti_fp is NULL, the iteration is done.
238*43a90889SApple OSS Distributions  */
239*43a90889SApple OSS Distributions extern struct fdt_iterator
240*43a90889SApple OSS Distributions fdt_next(proc_t p, int fd, bool only_settled);
241*43a90889SApple OSS Distributions 
242*43a90889SApple OSS Distributions /*!
243*43a90889SApple OSS Distributions  * @function fdt_next
244*43a90889SApple OSS Distributions  *
245*43a90889SApple OSS Distributions  * @brief
246*43a90889SApple OSS Distributions  * Seek the iterator backwards.
247*43a90889SApple OSS Distributions  *
248*43a90889SApple OSS Distributions  * @discussion
249*43a90889SApple OSS Distributions  * The @c proc_fdlock() should be held by the caller.
250*43a90889SApple OSS Distributions  *
251*43a90889SApple OSS Distributions  * @param p
252*43a90889SApple OSS Distributions  * The process for which the file descriptor table is being iterated.
253*43a90889SApple OSS Distributions  *
254*43a90889SApple OSS Distributions  * @param fd
255*43a90889SApple OSS Distributions  * The current file file descriptor to scan from (exclusive).
256*43a90889SApple OSS Distributions  *
257*43a90889SApple OSS Distributions  * @param only_settled
258*43a90889SApple OSS Distributions  * When true, only fileprocs with @c UF_RESERVED set are returned.
259*43a90889SApple OSS Distributions  * If false, fileprocs that are in flux (@c UF_RESERVED is set) are returned.
260*43a90889SApple OSS Distributions  *
261*43a90889SApple OSS Distributions  * @returns
262*43a90889SApple OSS Distributions  * The next iterator position.
263*43a90889SApple OSS Distributions  * If @c fdti_fp is NULL, the iteration is done.
264*43a90889SApple OSS Distributions  */
265*43a90889SApple OSS Distributions extern struct fdt_iterator
266*43a90889SApple OSS Distributions fdt_prev(proc_t p, int fd, bool only_settled);
267*43a90889SApple OSS Distributions 
268*43a90889SApple OSS Distributions /*!
269*43a90889SApple OSS Distributions  * @def fdt_foreach
270*43a90889SApple OSS Distributions  *
271*43a90889SApple OSS Distributions  * @brief
272*43a90889SApple OSS Distributions  * Convenience macro around @c fdt_next() to enumerates fileprocs in a process
273*43a90889SApple OSS Distributions  * file descriptor table.
274*43a90889SApple OSS Distributions  *
275*43a90889SApple OSS Distributions  * @discussion
276*43a90889SApple OSS Distributions  * The @c proc_fdlock() should be held by the caller.
277*43a90889SApple OSS Distributions  *
278*43a90889SApple OSS Distributions  * @param fp
279*43a90889SApple OSS Distributions  * The iteration variable.
280*43a90889SApple OSS Distributions  *
281*43a90889SApple OSS Distributions  * @param p
282*43a90889SApple OSS Distributions  * The process for which the file descriptor table is being iterated.
283*43a90889SApple OSS Distributions  */
284*43a90889SApple OSS Distributions #define fdt_foreach(fp, p) \
285*43a90889SApple OSS Distributions 	for (struct fdt_iterator __fdt_it = fdt_next(p, -1, true); \
286*43a90889SApple OSS Distributions 	    ((fp) = __fdt_it.fdti_fp); \
287*43a90889SApple OSS Distributions 	    __fdt_it = fdt_next(p, __fdt_it.fdti_fd, true))
288*43a90889SApple OSS Distributions 
289*43a90889SApple OSS Distributions /*!
290*43a90889SApple OSS Distributions  * @def fdt_foreach_fd
291*43a90889SApple OSS Distributions  *
292*43a90889SApple OSS Distributions  * @brief
293*43a90889SApple OSS Distributions  * When in an @c fdt_foreach() loop, return the current file descriptor
294*43a90889SApple OSS Distributions  * being inspected.
295*43a90889SApple OSS Distributions  */
296*43a90889SApple OSS Distributions #define fdt_foreach_fd()  __fdt_it.fdti_fd
297*43a90889SApple OSS Distributions 
298*43a90889SApple OSS Distributions /*!
299*43a90889SApple OSS Distributions  * @function fdt_init
300*43a90889SApple OSS Distributions  *
301*43a90889SApple OSS Distributions  * @brief
302*43a90889SApple OSS Distributions  * Initializers a proc file descriptor table.
303*43a90889SApple OSS Distributions  *
304*43a90889SApple OSS Distributions  * @warning
305*43a90889SApple OSS Distributions  * The proc that is passed is supposed to have been zeroed out,
306*43a90889SApple OSS Distributions  * as this function is used to setup @c kernelproc's file descriptor table
307*43a90889SApple OSS Distributions  * and some fields are already initialized when fdt_init() is called.
308*43a90889SApple OSS Distributions  */
309*43a90889SApple OSS Distributions extern void
310*43a90889SApple OSS Distributions fdt_init(proc_t p);
311*43a90889SApple OSS Distributions 
312*43a90889SApple OSS Distributions /*!
313*43a90889SApple OSS Distributions  * @function fdt_destroy
314*43a90889SApple OSS Distributions  *
315*43a90889SApple OSS Distributions  * @brief
316*43a90889SApple OSS Distributions  * Destroys locks from the file descriptor table.
317*43a90889SApple OSS Distributions  *
318*43a90889SApple OSS Distributions  * @description
319*43a90889SApple OSS Distributions  * This function destroys the file descriptor table locks.
320*43a90889SApple OSS Distributions  *
321*43a90889SApple OSS Distributions  * This cannot be done while the process this table belongs
322*43a90889SApple OSS Distributions  * to can be looked up.
323*43a90889SApple OSS Distributions  */
324*43a90889SApple OSS Distributions extern void
325*43a90889SApple OSS Distributions fdt_destroy(proc_t p);
326*43a90889SApple OSS Distributions 
327*43a90889SApple OSS Distributions /*!
328*43a90889SApple OSS Distributions  * @function fdt_fork
329*43a90889SApple OSS Distributions  *
330*43a90889SApple OSS Distributions  * @brief
331*43a90889SApple OSS Distributions  * Clones a file descriptor table for the @c fork() system call.
332*43a90889SApple OSS Distributions  *
333*43a90889SApple OSS Distributions  * @discussion
334*43a90889SApple OSS Distributions  * This function internally takes and drops @c proc_fdlock().
335*43a90889SApple OSS Distributions  *
336*43a90889SApple OSS Distributions  * Files are copied directly, ignoring the new resource limits for the process
337*43a90889SApple OSS Distributions  * that's being copied into.  Since the descriptor references are just
338*43a90889SApple OSS Distributions  * additional references, this does not count against the number of open files
339*43a90889SApple OSS Distributions  * on the system.
340*43a90889SApple OSS Distributions  *
341*43a90889SApple OSS Distributions  * The struct filedesc includes the current working directory, and the current
342*43a90889SApple OSS Distributions  * root directory, if the process is chroot'ed.
343*43a90889SApple OSS Distributions  *
344*43a90889SApple OSS Distributions  * If the exec was called by a thread using a per thread current working
345*43a90889SApple OSS Distributions  * directory, we inherit the working directory from the thread making the call,
346*43a90889SApple OSS Distributions  * rather than from the process.
347*43a90889SApple OSS Distributions  *
348*43a90889SApple OSS Distributions  * In the case of a failure to obtain a reference, for most cases, the file
349*43a90889SApple OSS Distributions  * entry will be silently dropped.  There's an exception for the case of
350*43a90889SApple OSS Distributions  * a chroot dir, since a failure to to obtain a reference there would constitute
351*43a90889SApple OSS Distributions  * an "escape" from the chroot environment, which must not be allowed.
352*43a90889SApple OSS Distributions  *
353*43a90889SApple OSS Distributions  * @param child_fdt
354*43a90889SApple OSS Distributions  * The child process file descriptor table.
355*43a90889SApple OSS Distributions  *
356*43a90889SApple OSS Distributions  * @param parent_p
357*43a90889SApple OSS Distributions  * The parent process to clone the file descriptor table from.
358*43a90889SApple OSS Distributions  *
359*43a90889SApple OSS Distributions  * @param uth_cdir
360*43a90889SApple OSS Distributions  * The vnode for the current thread's current working directory if it is
361*43a90889SApple OSS Distributions  * different from the parent process one.
362*43a90889SApple OSS Distributions  *
363*43a90889SApple OSS Distributions  * @param in_exec
364*43a90889SApple OSS Distributions  * The duplication of fdt is happening for exec
365*43a90889SApple OSS Distributions  *
366*43a90889SApple OSS Distributions  * @returns
367*43a90889SApple OSS Distributions  * 0            Success
368*43a90889SApple OSS Distributions  * EPERM        Unable to acquire a reference to the current chroot directory
369*43a90889SApple OSS Distributions  * ENOMEM       Not enough memory to perform the clone operation
370*43a90889SApple OSS Distributions  */
371*43a90889SApple OSS Distributions extern int
372*43a90889SApple OSS Distributions fdt_fork(struct filedesc *child_fdt, proc_t parent_p, struct vnode *uth_cdir, bool in_exec);
373*43a90889SApple OSS Distributions 
374*43a90889SApple OSS Distributions /*!
375*43a90889SApple OSS Distributions  * @function fdt_exec
376*43a90889SApple OSS Distributions  *
377*43a90889SApple OSS Distributions  * @brief
378*43a90889SApple OSS Distributions  * Perform close-on-exec processing for all files in a process
379*43a90889SApple OSS Distributions  * that are either marked as close-on-exec.
380*43a90889SApple OSS Distributions  *
381*43a90889SApple OSS Distributions  * @description
382*43a90889SApple OSS Distributions  * Also handles the case (via posix_spawn()) where -all- files except those
383*43a90889SApple OSS Distributions  * marked with "inherit" as treated as close-on-exec.
384*43a90889SApple OSS Distributions  *
385*43a90889SApple OSS Distributions  * This function internally takes and drops proc_fdlock()
386*43a90889SApple OSS Distributions  * But assumes tables don't grow/change while unlocked.
387*43a90889SApple OSS Distributions  *
388*43a90889SApple OSS Distributions  * @param p
389*43a90889SApple OSS Distributions  * The process whose file descriptor table is being filrered.
390*43a90889SApple OSS Distributions  *
391*43a90889SApple OSS Distributions  * @param posix_spawn_flags
392*43a90889SApple OSS Distributions  * A set of @c POSIX_SPAWN_* flags.
393*43a90889SApple OSS Distributions  *
394*43a90889SApple OSS Distributions  * @param thread
395*43a90889SApple OSS Distributions  * new thread
396*43a90889SApple OSS Distributions  *
397*43a90889SApple OSS Distributions  * @param in_exec
398*43a90889SApple OSS Distributions  * If the process is in exec
399*43a90889SApple OSS Distributions  */
400*43a90889SApple OSS Distributions extern void
401*43a90889SApple OSS Distributions fdt_exec(proc_t p, struct ucred *p_cred, short posix_spawn_flags, thread_t thread, bool in_exec);
402*43a90889SApple OSS Distributions 
403*43a90889SApple OSS Distributions /*!
404*43a90889SApple OSS Distributions  * @function fdt_invalidate
405*43a90889SApple OSS Distributions  *
406*43a90889SApple OSS Distributions  * @brief
407*43a90889SApple OSS Distributions  * Invalidates a proc file descriptor table.
408*43a90889SApple OSS Distributions  *
409*43a90889SApple OSS Distributions  * @discussion
410*43a90889SApple OSS Distributions  * Closes all open files in the file descriptor table,
411*43a90889SApple OSS Distributions  * empties hash tables, etc...
412*43a90889SApple OSS Distributions  *
413*43a90889SApple OSS Distributions  * However, the fileproc arrays stay allocated to still allow external lookups.
414*43a90889SApple OSS Distributions  * These get cleaned up by @c fdt_destroy().
415*43a90889SApple OSS Distributions  *
416*43a90889SApple OSS Distributions  * This function internally takes and drops proc_fdlock().
417*43a90889SApple OSS Distributions  */
418*43a90889SApple OSS Distributions extern void
419*43a90889SApple OSS Distributions fdt_invalidate(proc_t p);
420*43a90889SApple OSS Distributions 
421*43a90889SApple OSS Distributions /*
422*43a90889SApple OSS Distributions  * Kernel global variables and routines.
423*43a90889SApple OSS Distributions  */
424*43a90889SApple OSS Distributions extern int      dupfdopen(proc_t p, int indx, int dfd, int mode, int error);
425*43a90889SApple OSS Distributions extern int      fdalloc(proc_t p, int want, int *result);
426*43a90889SApple OSS Distributions extern void     fdrelse(struct proc * p, int fd);
427*43a90889SApple OSS Distributions #define         fdfile(p, fd)                                   \
428*43a90889SApple OSS Distributions 	                (&(p)->p_fd.fd_ofiles[(fd)])
429*43a90889SApple OSS Distributions #define         fdflags(p, fd)                                  \
430*43a90889SApple OSS Distributions 	                (&(p)->p_fd.fd_ofileflags[(fd)])
431*43a90889SApple OSS Distributions 
432*43a90889SApple OSS Distributions typedef void (*fp_initfn_t)(struct fileproc *, void *ctx);
433*43a90889SApple OSS Distributions extern int      falloc_withinit(
434*43a90889SApple OSS Distributions 	proc_t                  p,
435*43a90889SApple OSS Distributions 	struct ucred           *p_cred,
436*43a90889SApple OSS Distributions 	struct vfs_context     *ctx,
437*43a90889SApple OSS Distributions 	struct fileproc       **resultfp,
438*43a90889SApple OSS Distributions 	int                    *resultfd,
439*43a90889SApple OSS Distributions 	fp_initfn_t             fp_init,
440*43a90889SApple OSS Distributions 	void                   *initarg);
441*43a90889SApple OSS Distributions 
442*43a90889SApple OSS Distributions #define falloc(p, rfp, rfd)  ({ \
443*43a90889SApple OSS Distributions 	struct proc *__p = (p);                                                 \
444*43a90889SApple OSS Distributions 	falloc_withinit(__p, current_cached_proc_cred(__p),                     \
445*43a90889SApple OSS Distributions 	    vfs_context_current(), rfp, rfd, NULL, NULL);                       \
446*43a90889SApple OSS Distributions })
447*43a90889SApple OSS Distributions 
448*43a90889SApple OSS Distributions #define falloc_exec(p, ctx, rfp, rfd)  ({ \
449*43a90889SApple OSS Distributions 	struct vfs_context *__c = (ctx);                                        \
450*43a90889SApple OSS Distributions 	falloc_withinit(p, vfs_context_ucred(__c), __c, rfp, rfd, NULL, NULL);  \
451*43a90889SApple OSS Distributions })
452*43a90889SApple OSS Distributions 
453*43a90889SApple OSS Distributions #if CONFIG_PROC_RESOURCE_LIMITS
454*43a90889SApple OSS Distributions /* The proc_fdlock has to be held by caller for duration of the call */
455*43a90889SApple OSS Distributions void fd_check_limit_exceeded(struct filedesc *fdp);
456*43a90889SApple OSS Distributions 
457*43a90889SApple OSS Distributions /* The kqhash_lock has to be held by caller for duration of the call */
458*43a90889SApple OSS Distributions void kqworkloop_check_limit_exceeded(struct filedesc *fdp);
459*43a90889SApple OSS Distributions #endif /* CONFIG_PROC_RESOURCE_LIMITS */
460*43a90889SApple OSS Distributions 
461*43a90889SApple OSS Distributions #endif /* XNU_KERNEL_PRIVATE */
462*43a90889SApple OSS Distributions 
463*43a90889SApple OSS Distributions #endif /* !_SYS_FILEDESC_H_ */
464