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