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