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