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