]>
Commit | Line | Data |
---|---|---|
91447636 | 1 | /* |
39037602 | 2 | * Copyright (c) 2000-2016 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
91447636 A |
27 | */ |
28 | /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */ | |
29 | /* | |
30 | * Copyright (c) 1982, 1986, 1989, 1993 | |
31 | * The Regents of the University of California. All rights reserved. | |
32 | * | |
33 | * Redistribution and use in source and binary forms, with or without | |
34 | * modification, are permitted provided that the following conditions | |
35 | * are met: | |
36 | * 1. Redistributions of source code must retain the above copyright | |
37 | * notice, this list of conditions and the following disclaimer. | |
38 | * 2. Redistributions in binary form must reproduce the above copyright | |
39 | * notice, this list of conditions and the following disclaimer in the | |
40 | * documentation and/or other materials provided with the distribution. | |
41 | * 3. All advertising materials mentioning features or use of this software | |
42 | * must display the following acknowledgement: | |
43 | * This product includes software developed by the University of | |
44 | * California, Berkeley and its contributors. | |
45 | * 4. Neither the name of the University nor the names of its contributors | |
46 | * may be used to endorse or promote products derived from this software | |
47 | * without specific prior written permission. | |
48 | * | |
49 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
50 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
51 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
52 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
53 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
54 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
55 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
56 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
57 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
58 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
59 | * SUCH DAMAGE. | |
60 | * | |
61 | * @(#)file.h 8.3 (Berkeley) 1/9/95 | |
62 | */ | |
63 | ||
64 | #ifndef _SYS_FILE_INTERNAL_H_ | |
0a7de745 | 65 | #define _SYS_FILE_INTERNAL_H_ |
91447636 A |
66 | |
67 | #include <sys/appleapiopts.h> | |
68 | #include <sys/fcntl.h> | |
69 | #include <sys/unistd.h> | |
70 | ||
f427ee49 | 71 | #ifdef XNU_KERNEL_PRIVATE |
91447636 A |
72 | #include <sys/errno.h> |
73 | #include <sys/queue.h> | |
74 | #include <sys/cdefs.h> | |
75 | #include <sys/lock.h> | |
76 | #include <sys/file.h> | |
39236c6e | 77 | #include <sys/filedesc.h> |
39037602 | 78 | #include <sys/guarded.h> |
cb323159 | 79 | #include <os/refcnt.h> |
91447636 | 80 | |
f427ee49 A |
81 | __BEGIN_DECLS |
82 | ||
83 | #pragma GCC visibility push(hidden) | |
84 | ||
91447636 A |
85 | struct proc; |
86 | struct uio; | |
87 | struct knote; | |
cb323159 | 88 | struct kevent_qos_s; |
91447636 | 89 | struct file; |
f427ee49 A |
90 | #ifndef _KAUTH_CRED_T |
91 | #define _KAUTH_CRED_T | |
92 | typedef struct ucred *kauth_cred_t; | |
93 | typedef struct posix_cred *posix_cred_t; | |
94 | #endif /* !_KAUTH_CRED_T */ | |
91447636 | 95 | |
cb323159 A |
96 | __options_decl(fileproc_vflags_t, unsigned int, { |
97 | FPV_NONE = 0, | |
98 | FPV_DRAIN = 0x01, | |
99 | }); | |
91447636 A |
100 | |
101 | /* | |
102 | * Kernel descriptor table. | |
103 | * One entry for each open kernel vnode and socket. | |
104 | */ | |
105 | struct fileproc { | |
f427ee49 A |
106 | unsigned int fp_flags; |
107 | _Atomic fileproc_vflags_t fp_vflags; | |
108 | os_refcnt_t fp_iocount; | |
109 | struct fileglob * fp_glob; | |
110 | void *fp_wset; | |
91447636 A |
111 | }; |
112 | ||
113 | #define FILEPROC_NULL (struct fileproc *)0 | |
114 | ||
0a7de745 | 115 | #define FP_INSELECT 0x0004 |
0a7de745 | 116 | #define FP_AIOISSUED 0x0080 |
0a7de745 | 117 | #define FP_SELCONFLICT 0x0200 /* select conflict on an individual fp */ |
91447636 | 118 | |
39236c6e | 119 | /* squeeze a "type" value into the upper flag bits */ |
2d21ac55 | 120 | |
0a7de745 A |
121 | #define _FP_TYPESHIFT 24 |
122 | #define FP_TYPEMASK (0x7 << _FP_TYPESHIFT) /* 8 "types" of fileproc */ | |
39236c6e | 123 | |
f427ee49 | 124 | #define FILEPROC_TYPE(fp) ((fp)->fp_flags & FP_TYPEMASK) |
39236c6e | 125 | |
39236c6e | 126 | typedef enum { |
0a7de745 A |
127 | FTYPE_SIMPLE = 0, |
128 | FTYPE_GUARDED = (1 << _FP_TYPESHIFT) | |
39236c6e A |
129 | } fileproc_type_t; |
130 | ||
8f6c56a5 A |
131 | /* file types */ |
132 | typedef enum { | |
0a7de745 A |
133 | DTYPE_VNODE = 1, /* file */ |
134 | DTYPE_SOCKET, /* communications endpoint */ | |
135 | DTYPE_PSXSHM, /* POSIX Shared memory */ | |
136 | DTYPE_PSXSEM, /* POSIX Semaphores */ | |
137 | DTYPE_KQUEUE, /* kqueue */ | |
138 | DTYPE_PIPE, /* pipe */ | |
139 | DTYPE_FSEVENTS, /* fsevents */ | |
140 | DTYPE_ATALK, /* (obsolete) */ | |
141 | DTYPE_NETPOLICY, /* networking policy */ | |
8f6c56a5 A |
142 | } file_type_t; |
143 | ||
144 | /* defines for fg_lflags */ | |
f427ee49 | 145 | // was FG_TERM 0x01 |
0a7de745 A |
146 | #define FG_INSMSGQ 0x02 /* insert to msgqueue pending .. */ |
147 | #define FG_WINSMSGQ 0x04 /* wait for the fielglob is in msgque */ | |
148 | #define FG_RMMSGQ 0x08 /* the fileglob is being removed from msgqueue */ | |
149 | #define FG_WRMMSGQ 0x10 /* wait for the fileglob to be removed from msgqueue */ | |
150 | #define FG_PORTMADE 0x20 /* a port was at some point created for this fileglob */ | |
151 | #define FG_NOSIGPIPE 0x40 /* don't deliver SIGPIPE with EPIPE return */ | |
152 | #define FG_OFF_LOCKED 0x80 /* Used as a mutex for offset changes (for vnodes) */ | |
153 | #define FG_OFF_LOCKWANT 0x100 /* Somebody's wating for the lock */ | |
154 | #define FG_CONFINED 0x200 /* fileglob confined to process, immutably */ | |
155 | #define FG_HAS_OFDLOCK 0x400 /* Has or has had an OFD lock */ | |
91447636 | 156 | |
cb323159 A |
157 | struct fileops { |
158 | file_type_t fo_type; /* descriptor type */ | |
159 | int (*fo_read) (struct fileproc *fp, struct uio *uio, | |
160 | int flags, vfs_context_t ctx); | |
161 | int (*fo_write) (struct fileproc *fp, struct uio *uio, | |
162 | int flags, vfs_context_t ctx); | |
163 | #define FOF_OFFSET 0x00000001 /* offset supplied to vn_write */ | |
164 | #define FOF_PCRED 0x00000002 /* cred from proc, not current thread */ | |
165 | int (*fo_ioctl)(struct fileproc *fp, u_long com, | |
166 | caddr_t data, vfs_context_t ctx); | |
167 | int (*fo_select) (struct fileproc *fp, int which, | |
168 | void *wql, vfs_context_t ctx); | |
169 | int (*fo_close) (struct fileglob *fg, vfs_context_t ctx); | |
170 | int (*fo_kqfilter) (struct fileproc *fp, struct knote *, struct kevent_qos_s *); | |
171 | int (*fo_drain) (struct fileproc *fp, vfs_context_t ctx); | |
172 | }; | |
173 | ||
91447636 | 174 | struct fileglob { |
f427ee49 A |
175 | LIST_ENTRY(fileglob) f_msglist; /* list of files in unix messages */ |
176 | uint32_t fg_flag; /* (atomic) see fcntl.h */ | |
177 | os_ref_atomic_t fg_count; /* reference count */ | |
178 | uint32_t fg_msgcount; /* references from message queue */ | |
179 | int32_t fg_lflags; /* file global flags */ | |
180 | kauth_cred_t fg_cred; /* credentials associated with descriptor */ | |
cb323159 | 181 | const struct fileops *fg_ops; |
f427ee49 A |
182 | off_t fg_offset; |
183 | void *fg_data; /* vnode or socket or SHM or semaphore */ | |
184 | void *fg_vn_data; /* Per fd vnode data, used for directories */ | |
185 | lck_mtx_t fg_lock; | |
2d21ac55 | 186 | #if CONFIG_MACF |
f427ee49 | 187 | struct label *fg_label; /* JMM - use the one in the cred? */ |
2d21ac55 | 188 | #endif |
91447636 A |
189 | }; |
190 | ||
0a7de745 A |
191 | extern int maxfiles; /* kernel limit on number of open files */ |
192 | extern int nfiles; /* actual number of open files */ | |
b0d623f7 | 193 | extern int maxfilesperproc; |
f427ee49 | 194 | os_refgrp_decl_extern(f_refgrp); /* os_refgrp_t for file refcounts */ |
39236c6e | 195 | |
0a7de745 | 196 | #define FILEGLOB_DTYPE(fg) ((const file_type_t)((fg)->fg_ops->fo_type)) |
91447636 | 197 | |
f427ee49 | 198 | #pragma mark files (struct fileglob) |
91447636 | 199 | |
f427ee49 A |
200 | /*! |
201 | * @function fg_ref | |
202 | * | |
203 | * @brief | |
204 | * Acquire a file reference on the specified file. | |
205 | * | |
2a1bd2d3 A |
206 | * @description |
207 | * The @c proc must be locked while this operation is being performed | |
208 | * to avoid races with setting the FG_CONFINED flag. | |
209 | * | |
210 | * @param proc | |
211 | * The proc this file reference is taken on behalf of. | |
212 | * | |
f427ee49 A |
213 | * @param fg |
214 | * The specified file | |
215 | */ | |
216 | void | |
2a1bd2d3 A |
217 | fg_ref(proc_t proc, struct fileglob *fg); |
218 | ||
219 | /*! | |
220 | * @function fg_drop_live | |
221 | * | |
222 | * @brief | |
223 | * Drops a file reference on the specified file that isn't the last one. | |
224 | * | |
225 | * @param fg | |
226 | * The file whose reference is being dropped. | |
227 | */ | |
228 | void | |
229 | fg_drop_live(struct fileglob *fg); | |
f427ee49 A |
230 | |
231 | /*! | |
232 | * @function fg_drop | |
233 | * | |
234 | * @brief | |
235 | * Drops a file reference on the specified file. | |
236 | * | |
237 | * @discussion | |
238 | * No locks should be held when calling this function. | |
239 | * | |
240 | * @param p | |
241 | * The process making the request, | |
242 | * or PROC_NULL if the file belongs to a message/fileport. | |
243 | * | |
244 | * @param fg | |
245 | * The file being closed. | |
246 | * | |
247 | * @returns | |
248 | * 0 Success | |
249 | * ??? Any error that @c fileops::fo_close can return | |
250 | */ | |
251 | int | |
252 | fg_drop(proc_t p, struct fileglob *fg); | |
253 | ||
254 | /*! | |
255 | * @function fg_sendable | |
256 | * | |
257 | * @brief | |
258 | * Returns whether a particular file can be sent over IPC. | |
259 | */ | |
260 | bool | |
261 | fg_sendable(struct fileglob *fg); | |
262 | ||
263 | #pragma mark file descriptor entries (struct fileproc) | |
264 | ||
265 | /*! | |
266 | * @function fp_get_ftype | |
267 | * | |
268 | * @brief | |
269 | * Get the fileproc pointer for the given fd from the per process, with the | |
270 | * specified file type, and with an I/O reference. | |
271 | * | |
272 | * @param p | |
273 | * The process in which fd lives. | |
274 | * | |
275 | * @param fd | |
276 | * The file descriptor index to lookup. | |
277 | * | |
278 | * @param ftype | |
279 | * The required file type. | |
280 | * | |
281 | * @param err | |
282 | * The error to return if the file exists but isn't of the specified type. | |
283 | * | |
284 | * @param fpp | |
285 | * The returned fileproc when the call returns 0. | |
286 | * | |
287 | * @returns | |
288 | * 0 Success (@c fpp is set) | |
289 | * EBADF Bad file descriptor | |
290 | * @c err There is an entry, but it isn't of the specified type. | |
291 | */ | |
292 | extern int | |
293 | fp_get_ftype(proc_t p, int fd, file_type_t ftype, int err, struct fileproc **fpp); | |
294 | ||
295 | /*! | |
296 | * @function fp_get_noref_locked | |
297 | * | |
298 | * @brief | |
299 | * Get the fileproc pointer for the given fd from the per process | |
300 | * open file table without taking an explicit reference on it. | |
301 | * | |
302 | * @description | |
303 | * This function assumes that the @c proc_fdlock is held, as the caller | |
304 | * doesn't hold an I/O reference for the returned fileproc. | |
305 | * | |
306 | * Because there is no reference explicitly taken, the returned | |
307 | * fileproc pointer is only valid so long as the @c proc_fdlock | |
308 | * remains held by the caller. | |
309 | * | |
310 | * @param p | |
311 | * The process in which fd lives. | |
312 | * | |
313 | * @param fd | |
314 | * The file descriptor index to lookup. | |
315 | * | |
316 | * @returns | |
317 | * - the fileproc on success | |
318 | * - FILEPROC_NULL on error | |
319 | */ | |
320 | extern struct fileproc * | |
321 | fp_get_noref_locked(proc_t p, int fd); | |
322 | ||
323 | /*! | |
324 | * @function fp_get_noref_locked_with_iocount | |
325 | * | |
326 | * @brief | |
327 | * Similar to fp_get_noref_locked(), but allows returning files that are | |
328 | * closing. | |
329 | * | |
330 | * @discussion | |
331 | * Some parts of the kernel take I/O references on fileprocs but only remember | |
332 | * the file descriptor for which they did that. | |
333 | * | |
334 | * These interfaces later need to drop that reference, but if the file is | |
335 | * already closing, then fp_get_noref_locked() will refuse to resolve | |
336 | * the file descriptor. | |
337 | * | |
338 | * This interface allows the lookup (but will assert that the fileproc has | |
339 | * enouhg I/O references). | |
340 | * | |
341 | * @warning | |
342 | * New code should NOT use this function, it is required for interfaces | |
343 | * that acquire iocounts without remembering the fileproc pointer, | |
344 | * which is bad practice. | |
345 | */ | |
346 | extern struct fileproc * | |
347 | fp_get_noref_locked_with_iocount(proc_t p, int fd); | |
348 | ||
349 | /*! | |
350 | * @function fp_close_and_unlock | |
351 | * | |
352 | * @brief | |
353 | * Close the given file descriptor entry. | |
354 | * | |
355 | * @description | |
356 | * This function assumes that the @c proc_fdlock is held, | |
357 | * and that the caller holds no additional I/O reference | |
358 | * on the specified file descriptor entry. | |
359 | * | |
360 | * The @c proc_fdlock is unlocked upon return. | |
361 | * | |
362 | * @param p | |
363 | * The process in which fd lives. | |
364 | * | |
365 | * @param fd | |
366 | * The file descriptor index being closed. | |
367 | * | |
368 | * @param fp | |
369 | * The fileproc entry associated with @c fd. | |
370 | * | |
371 | * @returns | |
372 | * 0 Success | |
373 | * EBADF Bad file descriptor | |
374 | * ??? Any error that @c fileops::fo_close can return. | |
375 | */ | |
376 | extern int | |
377 | fp_close_and_unlock(proc_t p, int fd, struct fileproc *fp, int flags); | |
cb323159 A |
378 | |
379 | /* wrappers for fp->f_ops->fo_... */ | |
2d21ac55 A |
380 | int fo_read(struct fileproc *fp, struct uio *uio, int flags, vfs_context_t ctx); |
381 | int fo_write(struct fileproc *fp, struct uio *uio, int flags, | |
0a7de745 | 382 | vfs_context_t ctx); |
2d21ac55 A |
383 | int fo_ioctl(struct fileproc *fp, u_long com, caddr_t data, vfs_context_t ctx); |
384 | int fo_select(struct fileproc *fp, int which, void *wql, vfs_context_t ctx); | |
385 | int fo_close(struct fileglob *fg, vfs_context_t ctx); | |
cb323159 A |
386 | int fo_drain(struct fileproc *fp, vfs_context_t ctx); |
387 | int fo_kqfilter(struct fileproc *fp, struct knote *kn, struct kevent_qos_s *kev); | |
388 | ||
389 | /* Functions to use for unsupported fileops */ | |
390 | int fo_no_read(struct fileproc *fp, struct uio *uio, int flags, vfs_context_t ctx); | |
391 | int fo_no_write(struct fileproc *fp, struct uio *uio, int flags, | |
392 | vfs_context_t ctx); | |
393 | int fo_no_ioctl(struct fileproc *fp, u_long com, caddr_t data, vfs_context_t ctx); | |
394 | int fo_no_select(struct fileproc *fp, int which, void *wql, vfs_context_t ctx); | |
395 | int fo_no_drain(struct fileproc *fp, vfs_context_t ctx); | |
396 | int fo_no_kqfilter(struct fileproc *, struct knote *, struct kevent_qos_s *kev); | |
397 | ||
39236c6e | 398 | int fp_tryswap(proc_t, int fd, struct fileproc *nfp); |
91447636 | 399 | int fp_drop(struct proc *p, int fd, struct fileproc *fp, int locked); |
cb323159 | 400 | void fp_free(struct proc * p, int fd, struct fileproc * fp); |
91447636 | 401 | int fp_lookup(struct proc *p, int fd, struct fileproc **resultfp, int locked); |
39236c6e A |
402 | int fp_isguarded(struct fileproc *fp, u_int attribs); |
403 | int fp_guard_exception(proc_t p, int fd, struct fileproc *fp, u_int attribs); | |
39236c6e A |
404 | struct nameidata; |
405 | struct vnode_attr; | |
406 | int open1(vfs_context_t ctx, struct nameidata *ndp, int uflags, | |
407 | struct vnode_attr *vap, fp_allocfn_t fp_zalloc, void *cra, | |
408 | int32_t *retval); | |
cb323159 A |
409 | int chdir_internal(proc_t p, vfs_context_t ctx, struct nameidata *ndp, int per_thread); |
410 | int kqueue_internal(struct proc *p, fp_allocfn_t, void *cra, int32_t *retval); | |
6601e61a | 411 | void procfdtbl_releasefd(struct proc * p, int fd, struct fileproc * fp); |
39236c6e A |
412 | extern struct fileproc *fileproc_alloc_init(void *crargs); |
413 | extern void fileproc_free(struct fileproc *fp); | |
414 | extern void guarded_fileproc_free(struct fileproc *fp); | |
fe8ab488 A |
415 | extern void fg_vn_data_free(void *fgvndata); |
416 | extern int nameiat(struct nameidata *ndp, int dirfd); | |
39037602 A |
417 | extern int falloc_guarded(struct proc *p, struct fileproc **fp, int *fd, |
418 | vfs_context_t ctx, const guardid_t *guard, u_int attrs); | |
cb323159 A |
419 | extern void fileproc_modify_vflags(struct fileproc *fp, fileproc_vflags_t vflags, boolean_t clearflags); |
420 | fileproc_vflags_t fileproc_get_vflags(struct fileproc *fp); | |
91447636 | 421 | |
f427ee49 A |
422 | #pragma mark internal version of syscalls |
423 | ||
424 | int fileport_makefd(proc_t p, ipc_port_t port, int uf_flags, int *fd); | |
425 | int dup2(proc_t p, int from, int to, int *fd); | |
426 | int close_nocancel(proc_t p, int fd); | |
427 | ||
428 | #pragma GCC visibility pop | |
429 | ||
430 | __END_DECLS | |
91447636 | 431 | |
f427ee49 | 432 | #endif /* XNU_KERNEL_PRIVATE */ |
91447636 A |
433 | |
434 | #endif /* !_SYS_FILE_INTERNAL_H_ */ |