2 * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1982, 1986, 1989, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)kern_descrip.c 8.8 (Berkeley) 2/14/95
69 * NOTICE: This file was modified by SPARTA, Inc. in 2006 to introduce
70 * support for mandatory and extensible security protections. This notice
71 * is included in support of clause 2.2 (b) of the Apple Public License,
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/filedesc.h>
78 #include <sys/kernel.h>
79 #include <sys/vnode_internal.h>
80 #include <sys/proc_internal.h>
81 #include <sys/kauth.h>
82 #include <sys/file_internal.h>
83 #include <sys/guarded.h>
85 #include <sys/socket.h>
86 #include <sys/socketvar.h>
88 #include <sys/ioctl.h>
89 #include <sys/fcntl.h>
90 #include <sys/fsctl.h>
91 #include <sys/malloc.h>
93 #include <sys/syslog.h>
94 #include <sys/unistd.h>
95 #include <sys/resourcevar.h>
96 #include <sys/aio_kern.h>
98 #include <kern/locks.h>
99 #include <sys/uio_internal.h>
100 #include <sys/codesign.h>
101 #include <sys/codedir_internal.h>
102 #include <sys/mount_internal.h>
103 #include <sys/kdebug.h>
104 #include <sys/sysproto.h>
105 #include <sys/pipe.h>
106 #include <sys/spawn.h>
107 #include <sys/cprotect.h>
108 #include <sys/ubc_internal.h>
110 #include <kern/kern_types.h>
111 #include <kern/kalloc.h>
112 #include <kern/waitq.h>
113 #include <kern/ipc_misc.h>
115 #include <vm/vm_protos.h>
116 #include <mach/mach_port.h>
118 #include <security/audit/audit.h>
120 #include <security/mac_framework.h>
124 #include <os/atomic_private.h>
125 #include <IOKit/IOBSD.h>
127 #define IPC_KMSG_FLAGS_ALLOW_IMMOVABLE_SEND 0x1
128 kern_return_t
ipc_object_copyin(ipc_space_t
, mach_port_name_t
,
129 mach_msg_type_name_t
, ipc_port_t
*, mach_port_context_t
, mach_msg_guard_flags_t
*, uint32_t);
130 void ipc_port_release_send(ipc_port_t
);
132 static void fileproc_drain(proc_t
, struct fileproc
*);
133 static int finishdup(proc_t p
,
134 struct filedesc
*fdp
, int old
, int new, int flags
, int32_t *retval
);
136 void fileport_releasefg(struct fileglob
*fg
);
138 /* flags for fp_close_and_unlock */
139 #define FD_DUP2RESV 1
141 /* We don't want these exported */
144 int unlink1(vfs_context_t
, vnode_t
, user_addr_t
, enum uio_seg
, int);
146 static void fdrelse(struct proc
* p
, int fd
);
148 extern void file_lock_init(void);
150 extern kauth_scope_t kauth_scope_fileop
;
152 /* Conflict wait queue for when selects collide (opaque type) */
153 extern struct waitq select_conflict_queue
;
155 #ifndef HFS_GET_BOOT_INFO
156 #define HFS_GET_BOOT_INFO (FCNTL_FS_SPECIFIC_BASE + 0x00004)
159 #ifndef HFS_SET_BOOT_INFO
160 #define HFS_SET_BOOT_INFO (FCNTL_FS_SPECIFIC_BASE + 0x00005)
163 #ifndef APFSIOC_REVERT_TO_SNAPSHOT
164 #define APFSIOC_REVERT_TO_SNAPSHOT _IOW('J', 1, u_int64_t)
167 #define f_flag fp_glob->fg_flag
168 #define f_type fp_glob->fg_ops->fo_type
169 #define f_cred fp_glob->fg_cred
170 #define f_ops fp_glob->fg_ops
171 #define f_offset fp_glob->fg_offset
172 #define f_data fp_glob->fg_data
173 #define CHECK_ADD_OVERFLOW_INT64L(x, y) \
174 (((((x) > 0) && ((y) > 0) && ((x) > LLONG_MAX - (y))) || \
175 (((x) < 0) && ((y) < 0) && ((x) < LLONG_MIN - (y)))) \
178 ZONE_DECLARE(fg_zone
, "fileglob",
179 sizeof(struct fileglob
), ZC_NOENCRYPT
| ZC_ZFREE_CLEARMEM
);
180 ZONE_DECLARE(fp_zone
, "fileproc",
181 sizeof(struct fileproc
), ZC_NOENCRYPT
| ZC_ZFREE_CLEARMEM
);
182 ZONE_DECLARE(fdp_zone
, "filedesc",
183 sizeof(struct filedesc
), ZC_NOENCRYPT
| ZC_ZFREE_CLEARMEM
);
186 * Descriptor management.
188 int nfiles
; /* actual number of open files */
190 * "uninitialized" ops -- ensure FILEGLOB_DTYPE(fg) always exists
192 static const struct fileops uninitops
;
194 os_refgrp_decl(, f_refgrp
, "files refcounts", NULL
);
195 lck_grp_attr_t
* file_lck_grp_attr
;
196 lck_grp_t
* file_lck_grp
;
197 lck_attr_t
* file_lck_attr
;
199 #pragma mark fileglobs
205 * Free a file structure.
208 fg_free(struct fileglob
*fg
)
210 os_atomic_dec(&nfiles
, relaxed
);
212 if (fg
->fg_vn_data
) {
213 fg_vn_data_free(fg
->fg_vn_data
);
214 fg
->fg_vn_data
= NULL
;
217 if (IS_VALID_CRED(fg
->fg_cred
)) {
218 kauth_cred_unref(&fg
->fg_cred
);
220 lck_mtx_destroy(&fg
->fg_lock
, file_lck_grp
);
223 mac_file_label_destroy(fg
);
230 fg_ref(struct fileglob
*fg
)
232 os_ref_retain_raw(&fg
->fg_count
, &f_refgrp
);
236 fg_drop(proc_t p
, struct fileglob
*fg
)
239 struct vfs_context context
;
246 /* Set up context with cred stashed in fg */
247 if (p
== current_proc()) {
248 context
.vc_thread
= current_thread();
250 context
.vc_thread
= NULL
;
252 context
.vc_ucred
= fg
->fg_cred
;
255 * POSIX record locking dictates that any close releases ALL
256 * locks owned by this process. This is handled by setting
257 * a flag in the unlock to free ONLY locks obeying POSIX
258 * semantics, and not to free BSD-style file locks.
259 * If the descriptor was in a message, POSIX-style locks
260 * aren't passed with the descriptor.
262 if (p
&& DTYPE_VNODE
== FILEGLOB_DTYPE(fg
) &&
263 (p
->p_ladvflag
& P_LADVLOCK
)) {
265 .l_whence
= SEEK_SET
,
269 vp
= (struct vnode
*)fg
->fg_data
;
270 if ((error
= vnode_getwithref(vp
)) == 0) {
271 (void)VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &lf
, F_POSIX
, &context
, NULL
);
276 if (os_ref_release_raw(&fg
->fg_count
, &f_refgrp
) == 0) {
278 * Since we ensure that fg->fg_ops is always initialized,
279 * it is safe to invoke fo_close on the fg
281 error
= fo_close(fg
, &context
);
292 * Description: Return vnode associated with the file structure, if
293 * any. The lifetime of the returned vnode is bound to
294 * the lifetime of the file structure.
296 * Parameters: fg Pointer to fileglob to
302 fg_get_vnode(struct fileglob
*fg
)
304 if (FILEGLOB_DTYPE(fg
) == DTYPE_VNODE
) {
305 return (vnode_t
)fg
->fg_data
;
312 fg_sendable(struct fileglob
*fg
)
314 switch (FILEGLOB_DTYPE(fg
)) {
319 case DTYPE_NETPOLICY
:
320 return (fg
->fg_lflags
& FG_CONFINED
) == 0;
328 #pragma mark fileprocs
331 * check_file_seek_range
333 * Description: Checks if seek offsets are in the range of 0 to LLONG_MAX.
335 * Parameters: fl Flock structure.
336 * cur_file_offset Current offset in the file.
338 * Returns: 0 on Success.
339 * EOVERFLOW on overflow.
340 * EINVAL on offset less than zero.
344 check_file_seek_range(struct flock
*fl
, off_t cur_file_offset
)
346 if (fl
->l_whence
== SEEK_CUR
) {
347 /* Check if the start marker is beyond LLONG_MAX. */
348 if (CHECK_ADD_OVERFLOW_INT64L(fl
->l_start
, cur_file_offset
)) {
349 /* Check if start marker is negative */
350 if (fl
->l_start
< 0) {
355 /* Check if the start marker is negative. */
356 if (fl
->l_start
+ cur_file_offset
< 0) {
359 /* Check if end marker is beyond LLONG_MAX. */
360 if ((fl
->l_len
> 0) && (CHECK_ADD_OVERFLOW_INT64L(fl
->l_start
+
361 cur_file_offset
, fl
->l_len
- 1))) {
364 /* Check if the end marker is negative. */
365 if ((fl
->l_len
<= 0) && (fl
->l_start
+ cur_file_offset
+
369 } else if (fl
->l_whence
== SEEK_SET
) {
370 /* Check if the start marker is negative. */
371 if (fl
->l_start
< 0) {
374 /* Check if the end marker is beyond LLONG_MAX. */
375 if ((fl
->l_len
> 0) &&
376 CHECK_ADD_OVERFLOW_INT64L(fl
->l_start
, fl
->l_len
- 1)) {
379 /* Check if the end marker is negative. */
380 if ((fl
->l_len
< 0) && fl
->l_start
+ fl
->l_len
< 0) {
391 * Description: Initialize the file lock group and the uipc and flist locks
397 * Notes: Called at system startup from bsd_init().
402 /* allocate file lock group attribute and group */
403 file_lck_grp_attr
= lck_grp_attr_alloc_init();
405 file_lck_grp
= lck_grp_alloc_init("file", file_lck_grp_attr
);
407 /* Allocate file lock attribute */
408 file_lck_attr
= lck_attr_alloc_init();
413 proc_dirs_lock_shared(proc_t p
)
415 lck_rw_lock_shared(&p
->p_dirs_lock
);
419 proc_dirs_unlock_shared(proc_t p
)
421 lck_rw_unlock_shared(&p
->p_dirs_lock
);
425 proc_dirs_lock_exclusive(proc_t p
)
427 lck_rw_lock_exclusive(&p
->p_dirs_lock
);
431 proc_dirs_unlock_exclusive(proc_t p
)
433 lck_rw_unlock_exclusive(&p
->p_dirs_lock
);
437 * proc_fdlock, proc_fdlock_spin
439 * Description: Lock to control access to the per process struct fileproc
440 * and struct filedesc
442 * Parameters: p Process to take the lock on
446 * Notes: The lock is initialized in forkproc() and destroyed in
447 * reap_child_process().
450 proc_fdlock(proc_t p
)
452 lck_mtx_lock(&p
->p_fdmlock
);
456 proc_fdlock_spin(proc_t p
)
458 lck_mtx_lock_spin(&p
->p_fdmlock
);
462 proc_fdlock_assert(proc_t p
, int assertflags
)
464 lck_mtx_assert(&p
->p_fdmlock
, assertflags
);
471 * Description: Unlock the lock previously locked by a call to proc_fdlock()
473 * Parameters: p Process to drop the lock on
478 proc_fdunlock(proc_t p
)
480 lck_mtx_unlock(&p
->p_fdmlock
);
484 fdt_next(proc_t p
, int fd
, bool only_settled
)
486 struct fdt_iterator it
;
487 struct filedesc
*fdp
= p
->p_fd
;
489 int nfds
= min(fdp
->fd_lastfile
+ 1, fdp
->fd_nfiles
);
491 while (++fd
< nfds
) {
492 fp
= fdp
->fd_ofiles
[fd
];
493 if (fp
== NULL
|| fp
->fp_glob
== NULL
) {
496 if (only_settled
&& (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
510 fdt_prev(proc_t p
, int fd
, bool only_settled
)
512 struct fdt_iterator it
;
513 struct filedesc
*fdp
= p
->p_fd
;
517 fp
= fdp
->fd_ofiles
[fd
];
518 if (fp
== NULL
|| fp
->fp_glob
== NULL
) {
521 if (only_settled
&& (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
535 * System calls on descriptors.
542 * Description: Returns the per process maximum size of the descriptor table
544 * Parameters: p Process being queried
545 * retval Pointer to the call return area
550 * *retval (modified) Size of dtable
553 sys_getdtablesize(proc_t p
, __unused
struct getdtablesize_args
*uap
, int32_t *retval
)
555 *retval
= (int32_t)MIN(proc_limitgetcur(p
, RLIMIT_NOFILE
, TRUE
), maxfilesperproc
);
562 procfdtbl_reservefd(struct proc
* p
, int fd
)
564 p
->p_fd
->fd_ofiles
[fd
] = NULL
;
565 p
->p_fd
->fd_ofileflags
[fd
] |= UF_RESERVED
;
569 procfdtbl_releasefd(struct proc
* p
, int fd
, struct fileproc
* fp
)
572 p
->p_fd
->fd_ofiles
[fd
] = fp
;
574 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESERVED
;
575 if ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
) == UF_RESVWAIT
) {
576 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESVWAIT
;
582 procfdtbl_waitfd(struct proc
* p
, int fd
)
584 p
->p_fd
->fd_ofileflags
[fd
] |= UF_RESVWAIT
;
585 msleep(&p
->p_fd
, &p
->p_fdmlock
, PRIBIO
, "ftbl_waitfd", NULL
);
589 procfdtbl_clearfd(struct proc
* p
, int fd
)
593 waiting
= (p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
);
594 p
->p_fd
->fd_ofiles
[fd
] = NULL
;
595 p
->p_fd
->fd_ofileflags
[fd
] = 0;
596 if (waiting
== UF_RESVWAIT
) {
604 * Description: Inline utility function to free an fd in a filedesc
606 * Parameters: fdp Pointer to filedesc fd lies in
608 * reserv fd should be reserved
612 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
616 fdrelse(struct proc
* p
, int fd
)
618 struct filedesc
*fdp
= p
->p_fd
;
621 if (fd
< fdp
->fd_freefile
) {
622 fdp
->fd_freefile
= fd
;
625 if (fd
> fdp
->fd_lastfile
) {
626 panic("fdrelse: fd_lastfile inconsistent");
629 procfdtbl_clearfd(p
, fd
);
631 while ((nfd
= fdp
->fd_lastfile
) > 0 &&
632 fdp
->fd_ofiles
[nfd
] == NULL
&&
633 !(fdp
->fd_ofileflags
[nfd
] & UF_RESERVED
)) {
634 /* JMM - What about files with lingering EV_VANISHED knotes? */
657 char uio_buf
[UIO_SIZEOF(1)];
658 struct vfs_context context
= *(vfs_context_current());
662 error
= fp_lookup(p
, fd
, &fp
, 0);
667 switch (FILEGLOB_DTYPE(fp
->fp_glob
)) {
676 if (rw
== UIO_WRITE
&& !(fp
->f_flag
& FWRITE
)) {
681 if (rw
== UIO_READ
&& !(fp
->f_flag
& FREAD
)) {
686 context
.vc_ucred
= fp
->fp_glob
->fg_cred
;
688 if (UIO_SEG_IS_USER_SPACE(segflg
)) {
689 spacetype
= proc_is64bit(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
691 spacetype
= UIO_SYSSPACE
;
694 auio
= uio_createwithbuffer(1, offset
, spacetype
, rw
, &uio_buf
[0], sizeof(uio_buf
));
696 uio_addiov(auio
, (user_addr_t
)base
, (user_size_t
)len
);
698 if (!(io_flg
& IO_APPEND
)) {
702 if (rw
== UIO_WRITE
) {
703 user_ssize_t orig_resid
= uio_resid(auio
);
704 error
= fo_write(fp
, auio
, flags
, &context
);
705 if (uio_resid(auio
) < orig_resid
) {
706 os_atomic_or(&fp
->fp_glob
->fg_flag
, FWASWRITTEN
, relaxed
);
709 error
= fo_read(fp
, auio
, flags
, &context
);
713 *aresid
= uio_resid(auio
);
714 } else if (uio_resid(auio
) && error
== 0) {
718 fp_drop(p
, fd
, fp
, 0);
727 * Description: Duplicate a file descriptor.
729 * Parameters: p Process performing the dup
730 * uap->fd The fd to dup
731 * retval Pointer to the call return area
737 * *retval (modified) The new descriptor
740 sys_dup(proc_t p
, struct dup_args
*uap
, int32_t *retval
)
742 struct filedesc
*fdp
= p
->p_fd
;
748 if ((error
= fp_lookup(p
, old
, &fp
, 1))) {
752 if (FP_ISGUARDED(fp
, GUARD_DUP
)) {
753 error
= fp_guard_exception(p
, old
, fp
, kGUARD_EXC_DUP
);
754 (void) fp_drop(p
, old
, fp
, 1);
758 if ((error
= fdalloc(p
, 0, &new))) {
759 fp_drop(p
, old
, fp
, 1);
763 error
= finishdup(p
, fdp
, old
, new, 0, retval
);
764 fp_drop(p
, old
, fp
, 1);
767 if (ENTR_SHOULDTRACE
&& FILEGLOB_DTYPE(fp
->fp_glob
) == DTYPE_SOCKET
) {
768 KERNEL_ENERGYTRACE(kEnTrActKernSocket
, DBG_FUNC_START
,
769 new, 0, (int64_t)VM_KERNEL_ADDRPERM(fp
->f_data
));
778 * Description: Duplicate a file descriptor to a particular value.
780 * Parameters: p Process performing the dup
781 * uap->from The fd to dup
782 * uap->to The fd to dup it to
783 * retval Pointer to the call return area
789 * *retval (modified) The new descriptor
792 sys_dup2(proc_t p
, struct dup2_args
*uap
, int32_t *retval
)
794 return dup2(p
, uap
->from
, uap
->to
, retval
);
798 dup2(proc_t p
, int old
, int new, int *retval
)
800 struct filedesc
*fdp
= p
->p_fd
;
801 struct fileproc
*fp
, *nfp
;
803 rlim_t nofile
= proc_limitgetcur(p
, RLIMIT_NOFILE
, TRUE
);
808 if ((error
= fp_lookup(p
, old
, &fp
, 1))) {
812 if (FP_ISGUARDED(fp
, GUARD_DUP
)) {
813 error
= fp_guard_exception(p
, old
, fp
, kGUARD_EXC_DUP
);
814 (void) fp_drop(p
, old
, fp
, 1);
819 (rlim_t
)new >= nofile
||
820 new >= maxfilesperproc
) {
821 fp_drop(p
, old
, fp
, 1);
826 fp_drop(p
, old
, fp
, 1);
831 if (new < 0 || new >= fdp
->fd_nfiles
) {
832 if ((error
= fdalloc(p
, new, &i
))) {
833 fp_drop(p
, old
, fp
, 1);
843 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == UF_RESERVED
) {
844 fp_drop(p
, old
, fp
, 1);
845 procfdtbl_waitfd(p
, new);
847 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
852 if ((nfp
= fdp
->fd_ofiles
[new]) != NULL
) {
853 if (FP_ISGUARDED(nfp
, GUARD_CLOSE
)) {
854 fp_drop(p
, old
, fp
, 1);
855 error
= fp_guard_exception(p
,
856 new, nfp
, kGUARD_EXC_CLOSE
);
860 (void)fp_close_and_unlock(p
, new, nfp
, FD_DUP2RESV
);
862 assert(fdp
->fd_ofileflags
[new] & UF_RESERVED
);
865 if (fdp
->fd_ofiles
[new] != NULL
) {
866 panic("dup2: no ref on fileproc %d", new);
869 procfdtbl_reservefd(p
, new);
873 if (fdp
->fd_ofiles
[new] != 0) {
874 panic("dup2: overwriting fd_ofiles with new %d", new);
876 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0) {
877 panic("dup2: unreserved fileflags with new %d", new);
880 error
= finishdup(p
, fdp
, old
, new, 0, retval
);
881 fp_drop(p
, old
, fp
, 1);
891 * Description: The file control system call.
893 * Parameters: p Process performing the fcntl
894 * uap->fd The fd to operate against
895 * uap->cmd The command to perform
896 * uap->arg Pointer to the command argument
897 * retval Pointer to the call return area
900 * !0 Errno (see fcntl_nocancel)
903 * *retval (modified) fcntl return value (if any)
905 * Notes: This system call differs from fcntl_nocancel() in that it
906 * tests for cancellation prior to performing a potentially
907 * blocking operation.
910 sys_fcntl(proc_t p
, struct fcntl_args
*uap
, int32_t *retval
)
912 __pthread_testcancel(1);
913 return sys_fcntl_nocancel(p
, (struct fcntl_nocancel_args
*)uap
, retval
);
916 #define ACCOUNT_OPENFROM_ENTITLEMENT \
917 "com.apple.private.vfs.role-account-openfrom"
922 * Description: A non-cancel-testing file control system call.
924 * Parameters: p Process performing the fcntl
925 * uap->fd The fd to operate against
926 * uap->cmd The command to perform
927 * uap->arg Pointer to the command argument
928 * retval Pointer to the call return area
932 * fp_lookup:EBADF Bad file descriptor
944 * vnode_getwithref:???
952 * vnode_getwithref:???
959 * vnode_getwithref:???
961 * [F_SETSIZE,F_RDADVISE]
965 * vnode_getwithref:???
966 * [F_RDAHEAD,F_NOCACHE]
968 * vnode_getwithref:???
972 * *retval (modified) fcntl return value (if any)
975 sys_fcntl_nocancel(proc_t p
, struct fcntl_nocancel_args
*uap
, int32_t *retval
)
978 struct filedesc
*fdp
= p
->p_fd
;
981 struct vnode
*vp
= NULLVP
; /* for AUDIT_ARG() at end */
982 unsigned int oflags
, nflags
;
983 int i
, tmp
, error
, error2
, flg
= 0;
984 struct flock fl
= {};
985 struct flocktimeout fltimeout
;
986 struct timespec
*timeout
= NULL
;
987 struct vfs_context context
;
995 int has_entitlement
= 0;
997 AUDIT_ARG(fd
, uap
->fd
);
998 AUDIT_ARG(cmd
, uap
->cmd
);
1000 nofile
= proc_limitgetcur(p
, RLIMIT_NOFILE
, TRUE
);
1003 if ((error
= fp_lookup(p
, fd
, &fp
, 1))) {
1007 context
.vc_thread
= current_thread();
1008 context
.vc_ucred
= fp
->f_cred
;
1010 is64bit
= proc_is64bit(p
);
1015 * Since the arg parameter is defined as a long but may be
1016 * either a long or a pointer we must take care to handle
1017 * sign extension issues. Our sys call munger will sign
1018 * extend a long when we are called from a 32-bit process.
1019 * Since we can never have an address greater than 32-bits
1020 * from a 32-bit process we lop off the top 32-bits to avoid
1021 * getting the wrong address
1023 argp
= CAST_USER_ADDR_T((uint32_t)uap
->arg
);
1027 error
= mac_file_check_fcntl(proc_ucred(p
), fp
->fp_glob
, uap
->cmd
,
1034 pop
= &fdp
->fd_ofileflags
[fd
];
1038 case F_DUPFD_CLOEXEC
:
1039 if (FP_ISGUARDED(fp
, GUARD_DUP
)) {
1040 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_DUP
);
1043 newmin
= CAST_DOWN_EXPLICIT(int, uap
->arg
); /* arg is an int, so we won't lose bits */
1044 AUDIT_ARG(value32
, newmin
);
1045 if ((rlim_t
)newmin
>= nofile
||
1046 newmin
>= maxfilesperproc
) {
1050 if ((error
= fdalloc(p
, newmin
, &i
))) {
1053 error
= finishdup(p
, fdp
, fd
, i
,
1054 uap
->cmd
== F_DUPFD_CLOEXEC
? UF_EXCLOSE
: 0, retval
);
1058 *retval
= (*pop
& UF_EXCLOSE
)? FD_CLOEXEC
: 0;
1063 AUDIT_ARG(value32
, (uint32_t)uap
->arg
);
1064 if (uap
->arg
& FD_CLOEXEC
) {
1067 if (FILEPROC_TYPE(fp
) == FTYPE_GUARDED
) {
1068 error
= fp_guard_exception(p
,
1069 fd
, fp
, kGUARD_EXC_NOCLOEXEC
);
1072 *pop
&= ~UF_EXCLOSE
;
1078 *retval
= OFLAGS(fp
->f_flag
);
1083 // FIXME (rdar://54898652)
1085 // this code is broken if fnctl(F_SETFL), ioctl() are
1086 // called concurrently for the same fileglob.
1088 tmp
= CAST_DOWN_EXPLICIT(int, uap
->arg
); /* arg is an int, so we won't lose bits */
1089 AUDIT_ARG(value32
, tmp
);
1091 os_atomic_rmw_loop(&fp
->f_flag
, oflags
, nflags
, relaxed
, {
1092 nflags
= oflags
& ~FCNTLFLAGS
;
1093 nflags
|= FFLAGS(tmp
) & FCNTLFLAGS
;
1095 tmp
= nflags
& FNONBLOCK
;
1096 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
1100 tmp
= nflags
& FASYNC
;
1101 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, &context
);
1105 os_atomic_andnot(&fp
->f_flag
, FNONBLOCK
, relaxed
);
1107 (void)fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
1111 if (fp
->f_type
== DTYPE_SOCKET
) {
1112 *retval
= ((struct socket
*)fp
->f_data
)->so_pgid
;
1116 error
= fo_ioctl(fp
, TIOCGPGRP
, (caddr_t
)retval
, &context
);
1121 tmp
= CAST_DOWN_EXPLICIT(pid_t
, uap
->arg
); /* arg is an int, so we won't lose bits */
1122 AUDIT_ARG(value32
, tmp
);
1123 if (fp
->f_type
== DTYPE_SOCKET
) {
1124 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
1128 if (fp
->f_type
== DTYPE_PIPE
) {
1129 error
= fo_ioctl(fp
, TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
1136 proc_t p1
= proc_find(tmp
);
1141 tmp
= (int)p1
->p_pgrpid
;
1144 error
= fo_ioctl(fp
, TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
1147 case F_SETNOSIGPIPE
:
1148 tmp
= CAST_DOWN_EXPLICIT(int, uap
->arg
);
1149 if (fp
->f_type
== DTYPE_SOCKET
) {
1151 error
= sock_setsockopt((struct socket
*)fp
->f_data
,
1152 SOL_SOCKET
, SO_NOSIGPIPE
, &tmp
, sizeof(tmp
));
1157 struct fileglob
*fg
= fp
->fp_glob
;
1159 lck_mtx_lock_spin(&fg
->fg_lock
);
1161 fg
->fg_lflags
|= FG_NOSIGPIPE
;
1163 fg
->fg_lflags
&= ~FG_NOSIGPIPE
;
1165 lck_mtx_unlock(&fg
->fg_lock
);
1170 case F_GETNOSIGPIPE
:
1171 if (fp
->f_type
== DTYPE_SOCKET
) {
1173 int retsize
= sizeof(*retval
);
1174 error
= sock_getsockopt((struct socket
*)fp
->f_data
,
1175 SOL_SOCKET
, SO_NOSIGPIPE
, retval
, &retsize
);
1180 *retval
= (fp
->fp_glob
->fg_lflags
& FG_NOSIGPIPE
) ?
1188 * If this is the only reference to this fglob in the process
1189 * and it's already marked as close-on-fork then mark it as
1190 * (immutably) "confined" i.e. any fd that points to it will
1191 * forever be close-on-fork, and attempts to use an IPC
1192 * mechanism to move the descriptor elsewhere will fail.
1194 if (CAST_DOWN_EXPLICIT(int, uap
->arg
)) {
1195 struct fileglob
*fg
= fp
->fp_glob
;
1197 lck_mtx_lock_spin(&fg
->fg_lock
);
1198 if (fg
->fg_lflags
& FG_CONFINED
) {
1200 } else if (1 != os_ref_get_count_raw(&fg
->fg_count
)) {
1201 error
= EAGAIN
; /* go close the dup .. */
1202 } else if (UF_FORKCLOSE
== (*pop
& UF_FORKCLOSE
)) {
1203 fg
->fg_lflags
|= FG_CONFINED
;
1206 error
= EBADF
; /* open without O_CLOFORK? */
1208 lck_mtx_unlock(&fg
->fg_lock
);
1211 * Other subsystems may have built on the immutability
1212 * of FG_CONFINED; clearing it may be tricky.
1214 error
= EPERM
; /* immutable */
1219 *retval
= (fp
->fp_glob
->fg_lflags
& FG_CONFINED
) ? 1 : 0;
1223 case F_SETLKWTIMEOUT
:
1225 case F_OFD_SETLKWTIMEOUT
:
1232 if (fp
->f_type
!= DTYPE_VNODE
) {
1236 vp
= (struct vnode
*)fp
->f_data
;
1239 offset
= fp
->f_offset
;
1242 /* Copy in the lock structure */
1243 if (F_SETLKWTIMEOUT
== uap
->cmd
||
1244 F_OFD_SETLKWTIMEOUT
== uap
->cmd
) {
1245 error
= copyin(argp
, (caddr_t
) &fltimeout
, sizeof(fltimeout
));
1250 timeout
= &fltimeout
.timeout
;
1252 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
1258 /* Check starting byte and ending byte for EOVERFLOW in SEEK_CUR */
1259 /* and ending byte for EOVERFLOW in SEEK_SET */
1260 error
= check_file_seek_range(&fl
, offset
);
1265 if ((error
= vnode_getwithref(vp
))) {
1268 if (fl
.l_whence
== SEEK_CUR
) {
1269 fl
.l_start
+= offset
;
1273 error
= mac_file_check_lock(proc_ucred(p
), fp
->fp_glob
,
1276 (void)vnode_put(vp
);
1283 case F_OFD_SETLKWTIMEOUT
:
1285 switch (fl
.l_type
) {
1287 if ((fflag
& FREAD
) == 0) {
1291 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->fp_glob
,
1292 F_SETLK
, &fl
, flg
, &context
, timeout
);
1295 if ((fflag
& FWRITE
) == 0) {
1299 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->fp_glob
,
1300 F_SETLK
, &fl
, flg
, &context
, timeout
);
1303 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->fp_glob
,
1304 F_UNLCK
, &fl
, F_OFD_LOCK
, &context
,
1312 (F_RDLCK
== fl
.l_type
|| F_WRLCK
== fl
.l_type
)) {
1313 struct fileglob
*fg
= fp
->fp_glob
;
1316 * arrange F_UNLCK on last close (once
1317 * set, FG_HAS_OFDLOCK is immutable)
1319 if ((fg
->fg_lflags
& FG_HAS_OFDLOCK
) == 0) {
1320 lck_mtx_lock_spin(&fg
->fg_lock
);
1321 fg
->fg_lflags
|= FG_HAS_OFDLOCK
;
1322 lck_mtx_unlock(&fg
->fg_lock
);
1328 switch (fl
.l_type
) {
1330 if ((fflag
& FREAD
) == 0) {
1334 // XXX UInt32 unsafe for LP64 kernel
1335 os_atomic_or(&p
->p_ladvflag
, P_LADVLOCK
, relaxed
);
1336 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1337 F_SETLK
, &fl
, flg
, &context
, timeout
);
1340 if ((fflag
& FWRITE
) == 0) {
1344 // XXX UInt32 unsafe for LP64 kernel
1345 os_atomic_or(&p
->p_ladvflag
, P_LADVLOCK
, relaxed
);
1346 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1347 F_SETLK
, &fl
, flg
, &context
, timeout
);
1350 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1351 F_UNLCK
, &fl
, F_POSIX
, &context
, timeout
);
1359 (void) vnode_put(vp
);
1365 case F_OFD_GETLKPID
:
1366 if (fp
->f_type
!= DTYPE_VNODE
) {
1370 vp
= (struct vnode
*)fp
->f_data
;
1372 offset
= fp
->f_offset
;
1375 /* Copy in the lock structure */
1376 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
1381 /* Check starting byte and ending byte for EOVERFLOW in SEEK_CUR */
1382 /* and ending byte for EOVERFLOW in SEEK_SET */
1383 error
= check_file_seek_range(&fl
, offset
);
1388 if ((fl
.l_whence
== SEEK_SET
) && (fl
.l_start
< 0)) {
1393 switch (fl
.l_type
) {
1403 switch (fl
.l_whence
) {
1413 if ((error
= vnode_getwithref(vp
)) == 0) {
1414 if (fl
.l_whence
== SEEK_CUR
) {
1415 fl
.l_start
+= offset
;
1419 error
= mac_file_check_lock(proc_ucred(p
), fp
->fp_glob
,
1425 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->fp_glob
,
1426 F_GETLK
, &fl
, F_OFD_LOCK
, &context
, NULL
);
1428 case F_OFD_GETLKPID
:
1429 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->fp_glob
,
1430 F_GETLKPID
, &fl
, F_OFD_LOCK
, &context
, NULL
);
1433 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1434 uap
->cmd
, &fl
, F_POSIX
, &context
, NULL
);
1438 (void)vnode_put(vp
);
1441 error
= copyout((caddr_t
)&fl
, argp
, sizeof(fl
));
1446 case F_PREALLOCATE
: {
1447 fstore_t alloc_struct
; /* structure for allocate command */
1448 u_int32_t alloc_flags
= 0;
1450 if (fp
->f_type
!= DTYPE_VNODE
) {
1455 vp
= (struct vnode
*)fp
->f_data
;
1458 /* make sure that we have write permission */
1459 if ((fp
->f_flag
& FWRITE
) == 0) {
1464 error
= copyin(argp
, (caddr_t
)&alloc_struct
, sizeof(alloc_struct
));
1469 /* now set the space allocated to 0 */
1470 alloc_struct
.fst_bytesalloc
= 0;
1473 * Do some simple parameter checking
1476 /* set up the flags */
1478 alloc_flags
|= PREALLOCATE
;
1480 if (alloc_struct
.fst_flags
& F_ALLOCATECONTIG
) {
1481 alloc_flags
|= ALLOCATECONTIG
;
1484 if (alloc_struct
.fst_flags
& F_ALLOCATEALL
) {
1485 alloc_flags
|= ALLOCATEALL
;
1489 * Do any position mode specific stuff. The only
1490 * position mode supported now is PEOFPOSMODE
1493 switch (alloc_struct
.fst_posmode
) {
1495 if (alloc_struct
.fst_offset
!= 0) {
1500 alloc_flags
|= ALLOCATEFROMPEOF
;
1504 if (alloc_struct
.fst_offset
<= 0) {
1509 alloc_flags
|= ALLOCATEFROMVOL
;
1517 if ((error
= vnode_getwithref(vp
)) == 0) {
1519 * call allocate to get the space
1521 error
= VNOP_ALLOCATE(vp
, alloc_struct
.fst_length
, alloc_flags
,
1522 &alloc_struct
.fst_bytesalloc
, alloc_struct
.fst_offset
,
1524 (void)vnode_put(vp
);
1526 error2
= copyout((caddr_t
)&alloc_struct
, argp
, sizeof(alloc_struct
));
1537 if (fp
->f_type
!= DTYPE_VNODE
) {
1542 vp
= (struct vnode
*)fp
->f_data
;
1545 /* need write permissions */
1546 if ((fp
->f_flag
& FWRITE
) == 0) {
1551 if ((error
= copyin(argp
, (caddr_t
)&args
, sizeof(args
)))) {
1555 if ((error
= vnode_getwithref(vp
))) {
1560 if ((error
= mac_vnode_check_write(&context
, fp
->fp_glob
->fg_cred
, vp
))) {
1561 (void)vnode_put(vp
);
1566 error
= VNOP_IOCTL(vp
, F_PUNCHHOLE
, (caddr_t
)&args
, 0, &context
);
1567 (void)vnode_put(vp
);
1571 case F_TRIM_ACTIVE_FILE
: {
1572 ftrimactivefile_t args
;
1574 if (priv_check_cred(kauth_cred_get(), PRIV_TRIM_ACTIVE_FILE
, 0)) {
1579 if (fp
->f_type
!= DTYPE_VNODE
) {
1584 vp
= (struct vnode
*)fp
->f_data
;
1587 /* need write permissions */
1588 if ((fp
->f_flag
& FWRITE
) == 0) {
1593 if ((error
= copyin(argp
, (caddr_t
)&args
, sizeof(args
)))) {
1597 if ((error
= vnode_getwithref(vp
))) {
1601 error
= VNOP_IOCTL(vp
, F_TRIM_ACTIVE_FILE
, (caddr_t
)&args
, 0, &context
);
1602 (void)vnode_put(vp
);
1606 case F_SPECULATIVE_READ
: {
1609 if (fp
->f_type
!= DTYPE_VNODE
) {
1614 vp
= (struct vnode
*)fp
->f_data
;
1617 if ((error
= copyin(argp
, (caddr_t
)&args
, sizeof(args
)))) {
1621 /* Discard invalid offsets or lengths */
1622 if ((args
.fsr_offset
< 0) || (args
.fsr_length
< 0)) {
1628 * Round the file offset down to a page-size boundary (or to 0).
1629 * The filesystem will need to round the length up to the end of the page boundary
1630 * or to the EOF of the file.
1632 uint64_t foff
= (((uint64_t)args
.fsr_offset
) & ~((uint64_t)PAGE_MASK
));
1633 uint64_t foff_delta
= args
.fsr_offset
- foff
;
1634 args
.fsr_offset
= (off_t
) foff
;
1637 * Now add in the delta to the supplied length. Since we may have adjusted the
1638 * offset, increase it by the amount that we adjusted.
1640 args
.fsr_length
+= foff_delta
;
1642 if ((error
= vnode_getwithref(vp
))) {
1645 error
= VNOP_IOCTL(vp
, F_SPECULATIVE_READ
, (caddr_t
)&args
, 0, &context
);
1646 (void)vnode_put(vp
);
1651 if (fp
->f_type
!= DTYPE_VNODE
) {
1655 vp
= (struct vnode
*)fp
->f_data
;
1658 error
= copyin(argp
, (caddr_t
)&offset
, sizeof(off_t
));
1662 AUDIT_ARG(value64
, offset
);
1664 error
= vnode_getwithref(vp
);
1670 error
= mac_vnode_check_truncate(&context
,
1671 fp
->fp_glob
->fg_cred
, vp
);
1673 (void)vnode_put(vp
);
1678 * Make sure that we are root. Growing a file
1679 * without zero filling the data is a security hole.
1681 if (!kauth_cred_issuser(kauth_cred_get())) {
1685 * Require privilege to change file size without zerofill,
1686 * else will change the file size and zerofill it.
1688 error
= priv_check_cred(kauth_cred_get(), PRIV_VFS_SETSIZE
, 0);
1690 error
= vnode_setsize(vp
, offset
, IO_NOZEROFILL
, &context
);
1692 error
= vnode_setsize(vp
, offset
, 0, &context
);
1697 mac_vnode_notify_truncate(&context
, fp
->fp_glob
->fg_cred
, vp
);
1702 (void)vnode_put(vp
);
1706 if (fp
->f_type
!= DTYPE_VNODE
) {
1711 os_atomic_andnot(&fp
->fp_glob
->fg_flag
, FNORDAHEAD
, relaxed
);
1713 os_atomic_or(&fp
->fp_glob
->fg_flag
, FNORDAHEAD
, relaxed
);
1718 if (fp
->f_type
!= DTYPE_VNODE
) {
1723 os_atomic_or(&fp
->fp_glob
->fg_flag
, FNOCACHE
, relaxed
);
1725 os_atomic_andnot(&fp
->fp_glob
->fg_flag
, FNOCACHE
, relaxed
);
1730 if (fp
->f_type
!= DTYPE_VNODE
) {
1735 os_atomic_or(&fp
->fp_glob
->fg_flag
, FNODIRECT
, relaxed
);
1737 os_atomic_andnot(&fp
->fp_glob
->fg_flag
, FNODIRECT
, relaxed
);
1741 case F_SINGLE_WRITER
:
1742 if (fp
->f_type
!= DTYPE_VNODE
) {
1747 os_atomic_or(&fp
->fp_glob
->fg_flag
, FSINGLE_WRITER
, relaxed
);
1749 os_atomic_andnot(&fp
->fp_glob
->fg_flag
, FSINGLE_WRITER
, relaxed
);
1753 case F_GLOBAL_NOCACHE
:
1754 if (fp
->f_type
!= DTYPE_VNODE
) {
1758 vp
= (struct vnode
*)fp
->f_data
;
1761 if ((error
= vnode_getwithref(vp
)) == 0) {
1762 *retval
= vnode_isnocache(vp
);
1765 vnode_setnocache(vp
);
1767 vnode_clearnocache(vp
);
1770 (void)vnode_put(vp
);
1774 case F_CHECK_OPENEVT
:
1775 if (fp
->f_type
!= DTYPE_VNODE
) {
1779 vp
= (struct vnode
*)fp
->f_data
;
1782 if ((error
= vnode_getwithref(vp
)) == 0) {
1783 *retval
= vnode_is_openevt(vp
);
1786 vnode_set_openevt(vp
);
1788 vnode_clear_openevt(vp
);
1791 (void)vnode_put(vp
);
1796 struct radvisory ra_struct
;
1798 if (fp
->f_type
!= DTYPE_VNODE
) {
1802 vp
= (struct vnode
*)fp
->f_data
;
1805 if ((error
= copyin(argp
, (caddr_t
)&ra_struct
, sizeof(ra_struct
)))) {
1808 if (ra_struct
.ra_offset
< 0 || ra_struct
.ra_count
< 0) {
1812 if ((error
= vnode_getwithref(vp
)) == 0) {
1813 error
= VNOP_IOCTL(vp
, F_RDADVISE
, (caddr_t
)&ra_struct
, 0, &context
);
1815 (void)vnode_put(vp
);
1822 if (fp
->f_type
!= DTYPE_VNODE
) {
1826 vp
= (struct vnode
*)fp
->f_data
;
1829 if ((error
= vnode_getwithref(vp
)) == 0) {
1830 error
= VNOP_FSYNC(vp
, MNT_NOWAIT
, &context
);
1832 (void)vnode_put(vp
);
1837 case F_LOG2PHYS_EXT
: {
1838 struct log2phys l2p_struct
= {}; /* structure for allocate command */
1841 off_t file_offset
= 0;
1845 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1846 error
= copyin(argp
, (caddr_t
)&l2p_struct
, sizeof(l2p_struct
));
1850 file_offset
= l2p_struct
.l2p_devoffset
;
1852 file_offset
= fp
->f_offset
;
1854 if (fp
->f_type
!= DTYPE_VNODE
) {
1858 vp
= (struct vnode
*)fp
->f_data
;
1860 if ((error
= vnode_getwithref(vp
))) {
1863 error
= VNOP_OFFTOBLK(vp
, file_offset
, &lbn
);
1865 (void)vnode_put(vp
);
1868 error
= VNOP_BLKTOOFF(vp
, lbn
, &offset
);
1870 (void)vnode_put(vp
);
1873 devBlockSize
= vfs_devblocksize(vnode_mount(vp
));
1874 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1875 if (l2p_struct
.l2p_contigbytes
< 0) {
1881 a_size
= (size_t)MIN((uint64_t)l2p_struct
.l2p_contigbytes
, SIZE_MAX
);
1883 a_size
= devBlockSize
;
1886 error
= VNOP_BLOCKMAP(vp
, offset
, a_size
, &bn
, &run
, NULL
, 0, &context
);
1888 (void)vnode_put(vp
);
1891 l2p_struct
.l2p_flags
= 0; /* for now */
1892 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1893 l2p_struct
.l2p_contigbytes
= run
- (file_offset
- offset
);
1895 l2p_struct
.l2p_contigbytes
= 0; /* for now */
1899 * The block number being -1 suggests that the file offset is not backed
1900 * by any real blocks on-disk. As a result, just let it be passed back up wholesale.
1903 /* Don't multiply it by the block size */
1904 l2p_struct
.l2p_devoffset
= bn
;
1906 l2p_struct
.l2p_devoffset
= bn
* devBlockSize
;
1907 l2p_struct
.l2p_devoffset
+= file_offset
- offset
;
1909 error
= copyout((caddr_t
)&l2p_struct
, argp
, sizeof(l2p_struct
));
1914 case F_GETPATH_NOFIRMLINK
: {
1918 if (fp
->f_type
!= DTYPE_VNODE
) {
1922 vp
= (struct vnode
*)fp
->f_data
;
1925 pathlen
= MAXPATHLEN
;
1926 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
1927 if (pathbufp
== NULL
) {
1931 if ((error
= vnode_getwithref(vp
)) == 0) {
1932 if (uap
->cmd
== F_GETPATH_NOFIRMLINK
) {
1933 error
= vn_getpath_ext(vp
, NULL
, pathbufp
, &pathlen
, VN_GETPATH_NO_FIRMLINK
);
1935 error
= vn_getpath(vp
, pathbufp
, &pathlen
);
1937 (void)vnode_put(vp
);
1940 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
1943 FREE(pathbufp
, M_TEMP
);
1947 case F_PATHPKG_CHECK
: {
1951 if (fp
->f_type
!= DTYPE_VNODE
) {
1955 vp
= (struct vnode
*)fp
->f_data
;
1958 pathlen
= MAXPATHLEN
;
1959 pathbufp
= zalloc(ZV_NAMEI
);
1961 if ((error
= copyinstr(argp
, pathbufp
, MAXPATHLEN
, &pathlen
)) == 0) {
1962 if ((error
= vnode_getwithref(vp
)) == 0) {
1963 AUDIT_ARG(text
, pathbufp
);
1964 error
= vn_path_package_check(vp
, pathbufp
, (int)pathlen
, retval
);
1966 (void)vnode_put(vp
);
1969 zfree(ZV_NAMEI
, pathbufp
);
1973 case F_CHKCLEAN
: // used by regression tests to see if all dirty pages got cleaned by fsync()
1974 case F_FULLFSYNC
: // fsync + flush the journal + DKIOCSYNCHRONIZE
1975 case F_BARRIERFSYNC
: // fsync + barrier
1976 case F_FREEZE_FS
: // freeze all other fs operations for the fs of this fd
1977 case F_THAW_FS
: { // thaw all frozen fs operations for the fs of this fd
1978 if (fp
->f_type
!= DTYPE_VNODE
) {
1982 vp
= (struct vnode
*)fp
->f_data
;
1985 if ((error
= vnode_getwithref(vp
)) == 0) {
1986 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
1988 (void)vnode_put(vp
);
1994 * SPI (private) for opening a file starting from a dir fd
1997 struct user_fopenfrom fopen
;
1998 struct vnode_attr va
;
1999 struct nameidata nd
;
2002 /* Check if this isn't a valid file descriptor */
2003 if ((fp
->f_type
!= DTYPE_VNODE
) ||
2004 (fp
->f_flag
& FREAD
) == 0) {
2008 vp
= (struct vnode
*)fp
->f_data
;
2011 if (vnode_getwithref(vp
)) {
2016 /* Only valid for directories */
2017 if (vp
->v_type
!= VDIR
) {
2024 * Only entitled apps may use the credentials of the thread
2025 * that opened the file descriptor.
2026 * Non-entitled threads will use their own context.
2028 if (IOTaskHasEntitlement(current_task(), ACCOUNT_OPENFROM_ENTITLEMENT
)) {
2029 has_entitlement
= 1;
2032 /* Get flags, mode and pathname arguments. */
2033 if (IS_64BIT_PROCESS(p
)) {
2034 error
= copyin(argp
, &fopen
, sizeof(fopen
));
2036 struct user32_fopenfrom fopen32
;
2038 error
= copyin(argp
, &fopen32
, sizeof(fopen32
));
2039 fopen
.o_flags
= fopen32
.o_flags
;
2040 fopen
.o_mode
= fopen32
.o_mode
;
2041 fopen
.o_pathname
= CAST_USER_ADDR_T(fopen32
.o_pathname
);
2047 AUDIT_ARG(fflags
, fopen
.o_flags
);
2048 AUDIT_ARG(mode
, fopen
.o_mode
);
2050 /* Mask off all but regular access permissions */
2051 cmode
= ((fopen
.o_mode
& ~fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
2052 VATTR_SET(&va
, va_mode
, cmode
& ACCESSPERMS
);
2054 /* Start the lookup relative to the file descriptor's vnode. */
2055 NDINIT(&nd
, LOOKUP
, OP_OPEN
, USEDVP
| FOLLOW
| AUDITVNPATH1
, UIO_USERSPACE
,
2056 fopen
.o_pathname
, has_entitlement
? &context
: vfs_context_current());
2059 error
= open1(has_entitlement
? &context
: vfs_context_current(),
2060 &nd
, fopen
.o_flags
, &va
, fileproc_alloc_init
, NULL
, retval
);
2066 * SPI (private) for unlinking a file starting from a dir fd
2068 case F_UNLINKFROM
: {
2069 user_addr_t pathname
;
2071 /* Check if this isn't a valid file descriptor */
2072 if ((fp
->f_type
!= DTYPE_VNODE
) ||
2073 (fp
->f_flag
& FREAD
) == 0) {
2077 vp
= (struct vnode
*)fp
->f_data
;
2080 if (vnode_getwithref(vp
)) {
2085 /* Only valid for directories */
2086 if (vp
->v_type
!= VDIR
) {
2093 * Only entitled apps may use the credentials of the thread
2094 * that opened the file descriptor.
2095 * Non-entitled threads will use their own context.
2097 if (IOTaskHasEntitlement(current_task(), ACCOUNT_OPENFROM_ENTITLEMENT
)) {
2098 has_entitlement
= 1;
2101 /* Get flags, mode and pathname arguments. */
2102 if (IS_64BIT_PROCESS(p
)) {
2103 pathname
= (user_addr_t
)argp
;
2105 pathname
= CAST_USER_ADDR_T(argp
);
2108 /* Start the lookup relative to the file descriptor's vnode. */
2109 error
= unlink1(has_entitlement
? &context
: vfs_context_current(),
2110 vp
, pathname
, UIO_USERSPACE
, 0);
2118 case F_ADDFILESIGS_FOR_DYLD_SIM
:
2119 case F_ADDFILESIGS_RETURN
:
2120 case F_ADDFILESIGS_INFO
:
2122 struct cs_blob
*blob
= NULL
;
2123 struct user_fsignatures fs
;
2125 vm_offset_t kernel_blob_addr
;
2126 vm_size_t kernel_blob_size
;
2127 int blob_add_flags
= 0;
2128 const size_t sizeof_fs
= (uap
->cmd
== F_ADDFILESIGS_INFO
?
2129 offsetof(struct user_fsignatures
, fs_cdhash
/* first output element */) :
2130 offsetof(struct user_fsignatures
, fs_fsignatures_size
/* compat */));
2132 if (fp
->f_type
!= DTYPE_VNODE
) {
2136 vp
= (struct vnode
*)fp
->f_data
;
2139 if (uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
2140 blob_add_flags
|= MAC_VNODE_CHECK_DYLD_SIM
;
2141 if ((p
->p_csflags
& CS_KILL
) == 0) {
2143 p
->p_csflags
|= CS_KILL
;
2148 error
= vnode_getwithref(vp
);
2153 if (IS_64BIT_PROCESS(p
)) {
2154 error
= copyin(argp
, &fs
, sizeof_fs
);
2156 if (uap
->cmd
== F_ADDFILESIGS_INFO
) {
2162 struct user32_fsignatures fs32
;
2164 error
= copyin(argp
, &fs32
, sizeof(fs32
));
2165 fs
.fs_file_start
= fs32
.fs_file_start
;
2166 fs
.fs_blob_start
= CAST_USER_ADDR_T(fs32
.fs_blob_start
);
2167 fs
.fs_blob_size
= fs32
.fs_blob_size
;
2176 * First check if we have something loaded a this offset
2178 blob
= ubc_cs_blob_get(vp
, CPU_TYPE_ANY
, CPU_SUBTYPE_ANY
, fs
.fs_file_start
);
2180 /* If this is for dyld_sim revalidate the blob */
2181 if (uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
2182 error
= ubc_cs_blob_revalidate(vp
, blob
, NULL
, blob_add_flags
, proc_platform(p
));
2185 if (error
!= EAGAIN
) {
2195 * An arbitrary limit, to prevent someone from mapping in a 20GB blob. This should cover
2196 * our use cases for the immediate future, but note that at the time of this commit, some
2197 * platforms are nearing 2MB blob sizes (with a prior soft limit of 2.5MB).
2199 * We should consider how we can manage this more effectively; the above means that some
2200 * platforms are using megabytes of memory for signing data; it merely hasn't crossed the
2201 * threshold considered ridiculous at the time of this change.
2203 #define CS_MAX_BLOB_SIZE (40ULL * 1024ULL * 1024ULL)
2204 if (fs
.fs_blob_size
> CS_MAX_BLOB_SIZE
) {
2210 kernel_blob_size
= CAST_DOWN(vm_size_t
, fs
.fs_blob_size
);
2211 kr
= ubc_cs_blob_allocate(&kernel_blob_addr
, &kernel_blob_size
);
2212 if (kr
!= KERN_SUCCESS
|| kernel_blob_size
< fs
.fs_blob_size
) {
2218 if (uap
->cmd
== F_ADDSIGS
) {
2219 error
= copyin(fs
.fs_blob_start
,
2220 (void *) kernel_blob_addr
,
2222 } else { /* F_ADDFILESIGS || F_ADDFILESIGS_RETURN || F_ADDFILESIGS_FOR_DYLD_SIM || F_ADDFILESIGS_INFO */
2225 error
= vn_rdwr(UIO_READ
,
2227 (caddr_t
) kernel_blob_addr
,
2228 (int)kernel_blob_size
,
2229 fs
.fs_file_start
+ fs
.fs_blob_start
,
2235 if ((error
== 0) && resid
) {
2236 /* kernel_blob_size rounded to a page size, but signature may be at end of file */
2237 memset((void *)(kernel_blob_addr
+ (kernel_blob_size
- resid
)), 0x0, resid
);
2242 ubc_cs_blob_deallocate(kernel_blob_addr
,
2249 error
= ubc_cs_blob_add(vp
,
2251 CPU_TYPE_ANY
, /* not for a specific architecture */
2260 /* ubc_blob_add() has consumed "kernel_blob_addr" if it is zeroed */
2262 if (kernel_blob_addr
) {
2263 ubc_cs_blob_deallocate(kernel_blob_addr
,
2269 #if CHECK_CS_VALIDATION_BITMAP
2270 ubc_cs_validation_bitmap_allocate( vp
);
2275 if (uap
->cmd
== F_ADDFILESIGS_RETURN
|| uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
||
2276 uap
->cmd
== F_ADDFILESIGS_INFO
) {
2278 * The first element of the structure is a
2279 * off_t that happen to have the same size for
2280 * all archs. Lets overwrite that.
2282 off_t end_offset
= 0;
2284 end_offset
= blob
->csb_end_offset
;
2286 error
= copyout(&end_offset
, argp
, sizeof(end_offset
));
2294 if (uap
->cmd
== F_ADDFILESIGS_INFO
) {
2295 /* Return information. What we copy out depends on the size of the
2296 * passed in structure, to keep binary compatibility. */
2298 if (fs
.fs_fsignatures_size
>= sizeof(struct user_fsignatures
)) {
2299 // enough room for fs_cdhash[20]+fs_hash_type
2302 error
= copyout(blob
->csb_cdhash
,
2303 (vm_address_t
)argp
+ offsetof(struct user_fsignatures
, fs_cdhash
),
2304 USER_FSIGNATURES_CDHASH_LEN
);
2309 int hashtype
= cs_hash_type(blob
->csb_hashtype
);
2310 error
= copyout(&hashtype
,
2311 (vm_address_t
)argp
+ offsetof(struct user_fsignatures
, fs_hash_type
),
2321 (void) vnode_put(vp
);
2324 #if CONFIG_SUPPLEMENTAL_SIGNATURES
2325 case F_ADDFILESUPPL
:
2328 struct cs_blob
*blob
= NULL
;
2329 struct user_fsupplement fs
;
2331 struct fileproc
* orig_fp
= NULL
;
2333 vm_offset_t kernel_blob_addr
;
2334 vm_size_t kernel_blob_size
;
2336 if (!IS_64BIT_PROCESS(p
)) {
2338 goto out
; // drop fp and unlock fds
2341 if (fp
->f_type
!= DTYPE_VNODE
) {
2346 error
= copyin(argp
, &fs
, sizeof(fs
));
2351 orig_fd
= fs
.fs_orig_fd
;
2352 if ((error
= fp_lookup(p
, orig_fd
, &orig_fp
, 1))) {
2353 printf("CODE SIGNING: Failed to find original file for supplemental signature attachment\n");
2357 if (orig_fp
->f_type
!= DTYPE_VNODE
) {
2359 fp_drop(p
, orig_fd
, orig_fp
, 1);
2363 ivp
= (struct vnode
*)orig_fp
->f_data
;
2365 vp
= (struct vnode
*)fp
->f_data
;
2369 error
= vnode_getwithref(ivp
);
2371 fp_drop(p
, orig_fd
, orig_fp
, 0);
2372 goto outdrop
; //drop fp
2375 error
= vnode_getwithref(vp
);
2378 fp_drop(p
, orig_fd
, orig_fp
, 0);
2382 if (fs
.fs_blob_size
> CS_MAX_BLOB_SIZE
) {
2384 goto dropboth
; // drop iocounts on vp and ivp, drop orig_fp then drop fp via outdrop
2387 kernel_blob_size
= CAST_DOWN(vm_size_t
, fs
.fs_blob_size
);
2388 kr
= ubc_cs_blob_allocate(&kernel_blob_addr
, &kernel_blob_size
);
2389 if (kr
!= KERN_SUCCESS
) {
2395 error
= vn_rdwr(UIO_READ
, vp
,
2396 (caddr_t
)kernel_blob_addr
, (int)kernel_blob_size
,
2397 fs
.fs_file_start
+ fs
.fs_blob_start
,
2399 kauth_cred_get(), &resid
, p
);
2400 if ((error
== 0) && resid
) {
2401 /* kernel_blob_size rounded to a page size, but signature may be at end of file */
2402 memset((void *)(kernel_blob_addr
+ (kernel_blob_size
- resid
)), 0x0, resid
);
2406 ubc_cs_blob_deallocate(kernel_blob_addr
,
2411 error
= ubc_cs_blob_add_supplement(vp
, ivp
, fs
.fs_file_start
,
2412 &kernel_blob_addr
, kernel_blob_size
, &blob
);
2414 /* ubc_blob_add_supplement() has consumed kernel_blob_addr if it is zeroed */
2416 if (kernel_blob_addr
) {
2417 ubc_cs_blob_deallocate(kernel_blob_addr
,
2424 fp_drop(p
, orig_fd
, orig_fp
, 0);
2430 fp_drop(p
, orig_fd
, orig_fp
, 0);
2440 struct fileglob
*fg
;
2443 if (fp
->f_type
!= DTYPE_VNODE
) {
2450 if (IS_64BIT_PROCESS(p
)) {
2451 error
= copyin(argp
, &lv
, sizeof(lv
));
2453 struct user32_fchecklv lv32
= {};
2455 error
= copyin(argp
, &lv32
, sizeof(lv32
));
2456 lv
.lv_file_start
= lv32
.lv_file_start
;
2457 lv
.lv_error_message
= (void *)(uintptr_t)lv32
.lv_error_message
;
2458 lv
.lv_error_message_size
= lv32
.lv_error_message_size
;
2465 error
= mac_file_check_library_validation(p
, fg
, lv
.lv_file_start
,
2466 (user_long_t
)lv
.lv_error_message
, lv
.lv_error_message_size
);
2471 case F_GETSIGSINFO
: {
2472 struct cs_blob
*blob
= NULL
;
2473 fgetsigsinfo_t sigsinfo
= {};
2475 if (fp
->f_type
!= DTYPE_VNODE
) {
2479 vp
= (struct vnode
*)fp
->f_data
;
2482 error
= vnode_getwithref(vp
);
2487 error
= copyin(argp
, &sigsinfo
, sizeof(sigsinfo
));
2493 blob
= ubc_cs_blob_get(vp
, CPU_TYPE_ANY
, CPU_SUBTYPE_ANY
, sigsinfo
.fg_file_start
);
2499 switch (sigsinfo
.fg_info_request
) {
2500 case GETSIGSINFO_PLATFORM_BINARY
:
2501 sigsinfo
.fg_sig_is_platform
= blob
->csb_platform_binary
;
2502 error
= copyout(&sigsinfo
.fg_sig_is_platform
,
2503 (vm_address_t
)argp
+ offsetof(struct fgetsigsinfo
, fg_sig_is_platform
),
2504 sizeof(sigsinfo
.fg_sig_is_platform
));
2519 case F_GETPROTECTIONCLASS
: {
2520 if (fp
->f_type
!= DTYPE_VNODE
) {
2524 vp
= (struct vnode
*)fp
->f_data
;
2528 if (vnode_getwithref(vp
)) {
2533 struct vnode_attr va
;
2536 VATTR_WANTED(&va
, va_dataprotect_class
);
2537 error
= VNOP_GETATTR(vp
, &va
, &context
);
2539 if (VATTR_IS_SUPPORTED(&va
, va_dataprotect_class
)) {
2540 *retval
= va
.va_dataprotect_class
;
2550 case F_SETPROTECTIONCLASS
: {
2551 /* tmp must be a valid PROTECTION_CLASS_* */
2552 tmp
= CAST_DOWN_EXPLICIT(uint32_t, uap
->arg
);
2554 if (fp
->f_type
!= DTYPE_VNODE
) {
2558 vp
= (struct vnode
*)fp
->f_data
;
2562 if (vnode_getwithref(vp
)) {
2567 /* Only go forward if you have write access */
2568 vfs_context_t ctx
= vfs_context_current();
2569 if (vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2575 struct vnode_attr va
;
2578 VATTR_SET(&va
, va_dataprotect_class
, tmp
);
2580 error
= VNOP_SETATTR(vp
, &va
, ctx
);
2586 case F_TRANSCODEKEY
: {
2587 if (fp
->f_type
!= DTYPE_VNODE
) {
2592 vp
= (struct vnode
*)fp
->f_data
;
2595 if (vnode_getwithref(vp
)) {
2601 .len
= CP_MAX_WRAPPEDKEYSIZE
,
2604 MALLOC(k
.key
, char *, k
.len
, M_TEMP
, M_WAITOK
| M_ZERO
);
2606 error
= VNOP_IOCTL(vp
, F_TRANSCODEKEY
, (caddr_t
)&k
, 1, &context
);
2611 error
= copyout(k
.key
, argp
, k
.len
);
2615 FREE(k
.key
, M_TEMP
);
2620 case F_GETPROTECTIONLEVEL
: {
2621 if (fp
->f_type
!= DTYPE_VNODE
) {
2626 vp
= (struct vnode
*) fp
->f_data
;
2629 if (vnode_getwithref(vp
)) {
2634 error
= VNOP_IOCTL(vp
, F_GETPROTECTIONLEVEL
, (caddr_t
)retval
, 0, &context
);
2640 case F_GETDEFAULTPROTLEVEL
: {
2641 if (fp
->f_type
!= DTYPE_VNODE
) {
2646 vp
= (struct vnode
*) fp
->f_data
;
2649 if (vnode_getwithref(vp
)) {
2655 * if cp_get_major_vers fails, error will be set to proper errno
2656 * and cp_version will still be 0.
2659 error
= VNOP_IOCTL(vp
, F_GETDEFAULTPROTLEVEL
, (caddr_t
)retval
, 0, &context
);
2665 #endif /* CONFIG_PROTECT */
2667 case F_MOVEDATAEXTENTS
: {
2668 struct fileproc
*fp2
= NULL
;
2669 struct vnode
*src_vp
= NULLVP
;
2670 struct vnode
*dst_vp
= NULLVP
;
2671 /* We need to grab the 2nd FD out of the argments before moving on. */
2672 int fd2
= CAST_DOWN_EXPLICIT(int32_t, uap
->arg
);
2674 error
= priv_check_cred(kauth_cred_get(), PRIV_VFS_MOVE_DATA_EXTENTS
, 0);
2679 if (fp
->f_type
!= DTYPE_VNODE
) {
2685 * For now, special case HFS+ and APFS only, since this
2688 src_vp
= (struct vnode
*)fp
->f_data
;
2689 if (src_vp
->v_tag
!= VT_HFS
&& src_vp
->v_tag
!= VT_APFS
) {
2695 * Get the references before we start acquiring iocounts on the vnodes,
2696 * while we still hold the proc fd lock
2698 if ((error
= fp_lookup(p
, fd2
, &fp2
, 1))) {
2702 if (fp2
->f_type
!= DTYPE_VNODE
) {
2703 fp_drop(p
, fd2
, fp2
, 1);
2707 dst_vp
= (struct vnode
*)fp2
->f_data
;
2708 if (dst_vp
->v_tag
!= VT_HFS
&& dst_vp
->v_tag
!= VT_APFS
) {
2709 fp_drop(p
, fd2
, fp2
, 1);
2715 /* Re-do MAC checks against the new FD, pass in a fake argument */
2716 error
= mac_file_check_fcntl(proc_ucred(p
), fp2
->fp_glob
, uap
->cmd
, 0);
2718 fp_drop(p
, fd2
, fp2
, 1);
2722 /* Audit the 2nd FD */
2727 if (vnode_getwithref(src_vp
)) {
2728 fp_drop(p
, fd2
, fp2
, 0);
2732 if (vnode_getwithref(dst_vp
)) {
2734 fp_drop(p
, fd2
, fp2
, 0);
2740 * Basic asserts; validate they are not the same and that
2741 * both live on the same filesystem.
2743 if (dst_vp
== src_vp
) {
2746 fp_drop(p
, fd2
, fp2
, 0);
2751 if (dst_vp
->v_mount
!= src_vp
->v_mount
) {
2754 fp_drop(p
, fd2
, fp2
, 0);
2759 /* Now we have a legit pair of FDs. Go to work */
2761 /* Now check for write access to the target files */
2762 if (vnode_authorize(src_vp
, NULLVP
,
2763 (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), &context
) != 0) {
2766 fp_drop(p
, fd2
, fp2
, 0);
2771 if (vnode_authorize(dst_vp
, NULLVP
,
2772 (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), &context
) != 0) {
2775 fp_drop(p
, fd2
, fp2
, 0);
2780 /* Verify that both vps point to files and not directories */
2781 if (!vnode_isreg(src_vp
) || !vnode_isreg(dst_vp
)) {
2785 fp_drop(p
, fd2
, fp2
, 0);
2790 * The exchangedata syscall handler passes in 0 for the flags to VNOP_EXCHANGE.
2791 * We'll pass in our special bit indicating that the new behavior is expected
2794 error
= VNOP_EXCHANGE(src_vp
, dst_vp
, FSOPT_EXCHANGE_DATA_ONLY
, &context
);
2798 fp_drop(p
, fd2
, fp2
, 0);
2803 * SPI for making a file compressed.
2805 case F_MAKECOMPRESSED
: {
2806 uint32_t gcounter
= CAST_DOWN_EXPLICIT(uint32_t, uap
->arg
);
2808 if (fp
->f_type
!= DTYPE_VNODE
) {
2813 vp
= (struct vnode
*) fp
->f_data
;
2817 if (vnode_getwithref(vp
)) {
2823 if ((vnode_isreg(vp
) == 0) && (vnode_islnk(vp
) == 0)) {
2829 /* invoke ioctl to pass off to FS */
2830 /* Only go forward if you have write access */
2831 vfs_context_t ctx
= vfs_context_current();
2832 if (vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2838 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)&gcounter
, 0, &context
);
2845 * SPI (private) for indicating to a filesystem that subsequent writes to
2846 * the open FD will written to the Fastflow.
2848 case F_SET_GREEDY_MODE
:
2849 /* intentionally drop through to the same handler as F_SETSTATIC.
2850 * both fcntls should pass the argument and their selector into VNOP_IOCTL.
2854 * SPI (private) for indicating to a filesystem that subsequent writes to
2855 * the open FD will represent static content.
2857 case F_SETSTATICCONTENT
: {
2858 caddr_t ioctl_arg
= NULL
;
2861 ioctl_arg
= (caddr_t
) 1;
2864 if (fp
->f_type
!= DTYPE_VNODE
) {
2868 vp
= (struct vnode
*)fp
->f_data
;
2871 error
= vnode_getwithref(vp
);
2877 /* Only go forward if you have write access */
2878 vfs_context_t ctx
= vfs_context_current();
2879 if (vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2885 error
= VNOP_IOCTL(vp
, uap
->cmd
, ioctl_arg
, 0, &context
);
2886 (void)vnode_put(vp
);
2892 * SPI (private) for indicating to the lower level storage driver that the
2893 * subsequent writes should be of a particular IO type (burst, greedy, static),
2894 * or other flavors that may be necessary.
2901 /* extract 32 bits of flags from userland */
2902 param_ptr
= (caddr_t
) uap
->arg
;
2903 param
= (uint32_t) param_ptr
;
2905 /* If no argument is specified, error out */
2911 * Validate the different types of flags that can be specified:
2912 * all of them are mutually exclusive for now.
2915 case F_IOTYPE_ISOCHRONOUS
:
2924 if (fp
->f_type
!= DTYPE_VNODE
) {
2928 vp
= (struct vnode
*)fp
->f_data
;
2931 error
= vnode_getwithref(vp
);
2937 /* Only go forward if you have write access */
2938 vfs_context_t ctx
= vfs_context_current();
2939 if (vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2945 error
= VNOP_IOCTL(vp
, uap
->cmd
, param_ptr
, 0, &context
);
2946 (void)vnode_put(vp
);
2952 * Set the vnode pointed to by 'fd'
2953 * and tag it as the (potentially future) backing store
2954 * for another filesystem
2956 case F_SETBACKINGSTORE
: {
2957 if (fp
->f_type
!= DTYPE_VNODE
) {
2962 vp
= (struct vnode
*)fp
->f_data
;
2964 if (vp
->v_tag
!= VT_HFS
) {
2970 if (vnode_getwithref(vp
)) {
2975 /* only proceed if you have write access */
2976 vfs_context_t ctx
= vfs_context_current();
2977 if (vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2984 /* If arg != 0, set, otherwise unset */
2986 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)1, 0, &context
);
2988 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
2996 * like F_GETPATH, but special semantics for
2997 * the mobile time machine handler.
2999 case F_GETPATH_MTMINFO
: {
3003 if (fp
->f_type
!= DTYPE_VNODE
) {
3007 vp
= (struct vnode
*)fp
->f_data
;
3010 pathlen
= MAXPATHLEN
;
3011 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
3012 if (pathbufp
== NULL
) {
3016 if ((error
= vnode_getwithref(vp
)) == 0) {
3017 int backingstore
= 0;
3019 /* Check for error from vn_getpath before moving on */
3020 if ((error
= vn_getpath(vp
, pathbufp
, &pathlen
)) == 0) {
3021 if (vp
->v_tag
== VT_HFS
) {
3022 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
) &backingstore
, 0, &context
);
3024 (void)vnode_put(vp
);
3027 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
3031 * If the copyout was successful, now check to ensure
3032 * that this vnode is not a BACKINGSTORE vnode. mtmd
3033 * wants the path regardless.
3040 (void)vnode_put(vp
);
3043 FREE(pathbufp
, M_TEMP
);
3047 #if DEBUG || DEVELOPMENT
3049 if (fp
->f_type
!= DTYPE_VNODE
) {
3053 vp
= (struct vnode
*)fp
->f_data
;
3062 * This is an fcntl() that we d not recognize at this level;
3063 * if this is a vnode, we send it down into the VNOP_IOCTL
3064 * for this vnode; this can include special devices, and will
3065 * effectively overload fcntl() to send ioctl()'s.
3067 if ((uap
->cmd
& IOC_VOID
) && (uap
->cmd
& IOC_INOUT
)) {
3072 /* Catch any now-invalid fcntl() selectors */
3074 case (int)APFSIOC_REVERT_TO_SNAPSHOT
:
3075 case (int)FSIOC_FIOSEEKHOLE
:
3076 case (int)FSIOC_FIOSEEKDATA
:
3077 case (int)FSIOC_CAS_BSDFLAGS
:
3078 case HFS_GET_BOOT_INFO
:
3079 case HFS_SET_BOOT_INFO
:
3081 case F_MARKDEPENDENCY
:
3083 case TIOCREVOKECLEAR
:
3090 if (fp
->f_type
!= DTYPE_VNODE
) {
3094 vp
= (struct vnode
*)fp
->f_data
;
3097 if ((error
= vnode_getwithref(vp
)) == 0) {
3098 #define STK_PARAMS 128
3099 char stkbuf
[STK_PARAMS
] = {0};
3103 * For this to work properly, we have to copy in the
3104 * ioctl() cmd argument if there is one; we must also
3105 * check that a command parameter, if present, does
3106 * not exceed the maximum command length dictated by
3107 * the number of bits we have available in the command
3108 * to represent a structure length. Finally, we have
3109 * to copy the results back out, if it is that type of
3112 size
= IOCPARM_LEN(uap
->cmd
);
3113 if (size
> IOCPARM_MAX
) {
3114 (void)vnode_put(vp
);
3120 if (size
> sizeof(stkbuf
)) {
3121 memp
= (caddr_t
)kheap_alloc(KHEAP_TEMP
, size
, Z_WAITOK
);
3123 (void)vnode_put(vp
);
3132 if (uap
->cmd
& IOC_IN
) {
3135 error
= copyin(argp
, data
, size
);
3137 (void)vnode_put(vp
);
3139 kheap_free(KHEAP_TEMP
, memp
, size
);
3144 /* Bzero the section beyond that which was needed */
3145 if (size
<= sizeof(stkbuf
)) {
3146 bzero((((uint8_t*)data
) + size
), (sizeof(stkbuf
) - size
));
3151 *(user_addr_t
*)data
= argp
;
3153 *(uint32_t *)data
= (uint32_t)argp
;
3156 } else if ((uap
->cmd
& IOC_OUT
) && size
) {
3158 * Zero the buffer so the user always
3159 * gets back something deterministic.
3162 } else if (uap
->cmd
& IOC_VOID
) {
3164 *(user_addr_t
*)data
= argp
;
3166 *(uint32_t *)data
= (uint32_t)argp
;
3170 error
= VNOP_IOCTL(vp
, uap
->cmd
, CAST_DOWN(caddr_t
, data
), 0, &context
);
3172 (void)vnode_put(vp
);
3174 /* Copy any output data to user */
3175 if (error
== 0 && (uap
->cmd
& IOC_OUT
) && size
) {
3176 error
= copyout(data
, argp
, size
);
3179 kheap_free(KHEAP_TEMP
, memp
, size
);
3186 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
3187 fp_drop(p
, fd
, fp
, 0);
3190 fp_drop(p
, fd
, fp
, 1);
3199 * Description: Common code for dup, dup2, and fcntl(F_DUPFD).
3201 * Parameters: p Process performing the dup
3203 * new The fd to dup it to
3204 * fd_flags Flags to augment the new fd
3205 * retval Pointer to the call return area
3207 * Returns: 0 Success
3212 * *retval (modified) The new descriptor
3214 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
3217 * Notes: This function may drop and reacquire this lock; it is unsafe
3218 * for a caller to assume that other state protected by the lock
3219 * has not been subsequently changed out from under it.
3223 struct filedesc
*fdp
, int old
, int new, int fd_flags
, int32_t *retval
)
3225 struct fileproc
*nfp
;
3226 struct fileproc
*ofp
;
3232 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
3234 if ((ofp
= fdp
->fd_ofiles
[old
]) == NULL
||
3235 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
)) {
3241 error
= mac_file_check_dup(proc_ucred(p
), ofp
->fp_glob
, new);
3250 nfp
= fileproc_alloc_init(NULL
);
3259 fg_ref(ofp
->fp_glob
);
3260 nfp
->fp_glob
= ofp
->fp_glob
;
3263 if (fdp
->fd_ofiles
[new] != 0) {
3264 panic("finishdup: overwriting fd_ofiles with new %d", new);
3266 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0) {
3267 panic("finishdup: unreserved fileflags with new %d", new);
3271 if (new > fdp
->fd_lastfile
) {
3272 fdp
->fd_lastfile
= new;
3274 *fdflags(p
, new) |= fd_flags
;
3275 procfdtbl_releasefd(p
, new, nfp
);
3284 * Description: The implementation of the close(2) system call
3286 * Parameters: p Process in whose per process file table
3287 * the close is to occur
3288 * uap->fd fd to be closed
3291 * Returns: 0 Success
3292 * fp_lookup:EBADF Bad file descriptor
3293 * fp_guard_exception:??? Guarded file descriptor
3294 * close_internal:EBADF
3295 * close_internal:??? Anything returnable by a per-fileops
3299 sys_close(proc_t p
, struct close_args
*uap
, __unused
int32_t *retval
)
3301 __pthread_testcancel(1);
3302 return close_nocancel(p
, uap
->fd
);
3306 sys_close_nocancel(proc_t p
, struct close_nocancel_args
*uap
, __unused
int32_t *retval
)
3308 return close_nocancel(p
, uap
->fd
);
3312 close_nocancel(proc_t p
, int fd
)
3314 struct fileproc
*fp
;
3316 AUDIT_SYSCLOSE(p
, fd
);
3319 if ((fp
= fp_get_noref_locked(p
, fd
)) == NULL
) {
3324 if (FP_ISGUARDED(fp
, GUARD_CLOSE
)) {
3325 int error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_CLOSE
);
3330 return fp_close_and_unlock(p
, fd
, fp
, 0);
3335 fp_close_and_unlock(proc_t p
, int fd
, struct fileproc
*fp
, int flags
)
3337 struct filedesc
*fdp
= p
->p_fd
;
3338 struct fileglob
*fg
= fp
->fp_glob
;
3341 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
3345 * Keep most people from finding the filedesc while we are closing it.
3349 * - dup2() which always waits for UF_RESERVED to clear
3351 * - close/guarded_close/... who will fail the fileproc lookup if
3352 * UF_RESERVED is set,
3354 * - fdexec()/fdfree() who only run once all threads in the proc
3355 * are properly canceled, hence no fileproc in this proc should
3358 * Which means that neither UF_RESERVED nor UF_CLOSING should be set.
3360 * Callers of fp_get_noref_locked_with_iocount() can still find
3361 * this entry so that they can drop their I/O reference despite
3362 * not having remembered the fileproc pointer (namely select() and
3365 if (p
->p_fd
->fd_ofileflags
[fd
] & (UF_RESERVED
| UF_CLOSING
)) {
3366 panic("%s: called with fileproc in flux (%d/:%p)",
3369 p
->p_fd
->fd_ofileflags
[fd
] |= (UF_RESERVED
| UF_CLOSING
);
3371 if ((fp
->fp_flags
& FP_AIOISSUED
) || kauth_authorize_fileop_has_listeners()) {
3374 if ((FILEGLOB_DTYPE(fg
) == DTYPE_VNODE
) && kauth_authorize_fileop_has_listeners()) {
3376 * call out to allow 3rd party notification of close.
3377 * Ignore result of kauth_authorize_fileop call.
3379 if (vnode_getwithref((vnode_t
)fg
->fg_data
) == 0) {
3380 u_int fileop_flags
= 0;
3381 if (fg
->fg_flag
& FWASWRITTEN
) {
3382 fileop_flags
|= KAUTH_FILEOP_CLOSE_MODIFIED
;
3384 kauth_authorize_fileop(fg
->fg_cred
, KAUTH_FILEOP_CLOSE
,
3385 (uintptr_t)fg
->fg_data
, (uintptr_t)fileop_flags
);
3387 mac_file_notify_close(proc_ucred(p
), fp
->fp_glob
);
3389 vnode_put((vnode_t
)fg
->fg_data
);
3392 if (fp
->fp_flags
& FP_AIOISSUED
) {
3394 * cancel all async IO requests that can be cancelled.
3396 _aio_close( p
, fd
);
3402 if (fd
< fdp
->fd_knlistsize
) {
3403 knote_fdclose(p
, fd
);
3406 fileproc_drain(p
, fp
);
3408 if (flags
& FD_DUP2RESV
) {
3409 fdp
->fd_ofiles
[fd
] = NULL
;
3410 fdp
->fd_ofileflags
[fd
] &= ~(UF_CLOSING
| UF_EXCLOSE
| UF_FORKCLOSE
);
3417 if (ENTR_SHOULDTRACE
&& FILEGLOB_DTYPE(fg
) == DTYPE_SOCKET
) {
3418 KERNEL_ENERGYTRACE(kEnTrActKernSocket
, DBG_FUNC_END
,
3419 fd
, 0, (int64_t)VM_KERNEL_ADDRPERM(fg
->fg_data
));
3424 return fg_drop(p
, fg
);
3431 * Description: Return status information about a file descriptor.
3433 * Parameters: p The process doing the fstat
3435 * ub The user stat buffer
3436 * xsecurity The user extended security
3437 * buffer, or 0 if none
3438 * xsecurity_size The size of xsecurity, or 0
3440 * isstat64 Flag to indicate 64 bit version
3441 * for inode size, etc.
3443 * Returns: 0 Success
3446 * fp_lookup:EBADF Bad file descriptor
3447 * vnode_getwithref:???
3449 * vnode_getwithref:???
3456 * Notes: Internal implementation for all other fstat() related
3459 * XXX switch on node type is bogus; need a stat in struct
3460 * XXX fileops instead.
3463 fstat(proc_t p
, int fd
, user_addr_t ub
, user_addr_t xsecurity
, user_addr_t xsecurity_size
, int isstat64
)
3465 struct fileproc
*fp
;
3471 struct user64_stat user64_sb
;
3472 struct user32_stat user32_sb
;
3473 struct user64_stat64 user64_sb64
;
3474 struct user32_stat64 user32_sb64
;
3479 kauth_filesec_t fsec
;
3480 user_size_t xsecurity_bufsize
;
3481 vfs_context_t ctx
= vfs_context_current();
3487 if ((error
= fp_lookup(p
, fd
, &fp
, 0)) != 0) {
3492 fsec
= KAUTH_FILESEC_NONE
;
3494 sbptr
= (void *)&source
;
3498 if ((error
= vnode_getwithref((vnode_t
)data
)) == 0) {
3500 * If the caller has the file open, and is not
3501 * requesting extended security information, we are
3502 * going to let them get the basic stat information.
3504 if (xsecurity
== USER_ADDR_NULL
) {
3505 error
= vn_stat_noauth((vnode_t
)data
, sbptr
, NULL
, isstat64
, 0, ctx
,
3506 fp
->fp_glob
->fg_cred
);
3508 error
= vn_stat((vnode_t
)data
, sbptr
, &fsec
, isstat64
, 0, ctx
);
3511 AUDIT_ARG(vnpath
, (struct vnode
*)data
, ARG_VNODE1
);
3512 (void)vnode_put((vnode_t
)data
);
3518 error
= soo_stat((struct socket
*)data
, sbptr
, isstat64
);
3520 #endif /* SOCKETS */
3523 error
= pipe_stat((void *)data
, sbptr
, isstat64
);
3527 error
= pshm_stat((void *)data
, sbptr
, isstat64
);
3531 error
= kqueue_stat((void *)data
, sbptr
, isstat64
, p
);
3541 if (isstat64
!= 0) {
3542 source
.sb64
.st_lspare
= 0;
3543 source
.sb64
.st_qspare
[0] = 0LL;
3544 source
.sb64
.st_qspare
[1] = 0LL;
3546 if (IS_64BIT_PROCESS(current_proc())) {
3547 munge_user64_stat64(&source
.sb64
, &dest
.user64_sb64
);
3548 my_size
= sizeof(dest
.user64_sb64
);
3549 sbp
= (caddr_t
)&dest
.user64_sb64
;
3551 munge_user32_stat64(&source
.sb64
, &dest
.user32_sb64
);
3552 my_size
= sizeof(dest
.user32_sb64
);
3553 sbp
= (caddr_t
)&dest
.user32_sb64
;
3556 source
.sb
.st_lspare
= 0;
3557 source
.sb
.st_qspare
[0] = 0LL;
3558 source
.sb
.st_qspare
[1] = 0LL;
3559 if (IS_64BIT_PROCESS(current_proc())) {
3560 munge_user64_stat(&source
.sb
, &dest
.user64_sb
);
3561 my_size
= sizeof(dest
.user64_sb
);
3562 sbp
= (caddr_t
)&dest
.user64_sb
;
3564 munge_user32_stat(&source
.sb
, &dest
.user32_sb
);
3565 my_size
= sizeof(dest
.user32_sb
);
3566 sbp
= (caddr_t
)&dest
.user32_sb
;
3570 error
= copyout(sbp
, ub
, my_size
);
3573 /* caller wants extended security information? */
3574 if (xsecurity
!= USER_ADDR_NULL
) {
3575 /* did we get any? */
3576 if (fsec
== KAUTH_FILESEC_NONE
) {
3577 if (susize(xsecurity_size
, 0) != 0) {
3582 /* find the user buffer size */
3583 xsecurity_bufsize
= fusize(xsecurity_size
);
3585 /* copy out the actual data size */
3586 if (susize(xsecurity_size
, KAUTH_FILESEC_COPYSIZE(fsec
)) != 0) {
3591 /* if the caller supplied enough room, copy out to it */
3592 if (xsecurity_bufsize
>= KAUTH_FILESEC_COPYSIZE(fsec
)) {
3593 error
= copyout(fsec
, xsecurity
, KAUTH_FILESEC_COPYSIZE(fsec
));
3598 fp_drop(p
, fd
, fp
, 0);
3600 kauth_filesec_free(fsec
);
3607 * sys_fstat_extended
3609 * Description: Extended version of fstat supporting returning extended
3610 * security information
3612 * Parameters: p The process doing the fstat
3613 * uap->fd The fd to stat
3614 * uap->ub The user stat buffer
3615 * uap->xsecurity The user extended security
3616 * buffer, or 0 if none
3617 * uap->xsecurity_size The size of xsecurity, or 0
3619 * Returns: 0 Success
3620 * !0 Errno (see fstat)
3623 sys_fstat_extended(proc_t p
, struct fstat_extended_args
*uap
, __unused
int32_t *retval
)
3625 return fstat(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 0);
3632 * Description: Get file status for the file associated with fd
3634 * Parameters: p The process doing the fstat
3635 * uap->fd The fd to stat
3636 * uap->ub The user stat buffer
3638 * Returns: 0 Success
3639 * !0 Errno (see fstat)
3642 sys_fstat(proc_t p
, struct fstat_args
*uap
, __unused
int32_t *retval
)
3644 return fstat(p
, uap
->fd
, uap
->ub
, 0, 0, 0);
3649 * sys_fstat64_extended
3651 * Description: Extended version of fstat64 supporting returning extended
3652 * security information
3654 * Parameters: p The process doing the fstat
3655 * uap->fd The fd to stat
3656 * uap->ub The user stat buffer
3657 * uap->xsecurity The user extended security
3658 * buffer, or 0 if none
3659 * uap->xsecurity_size The size of xsecurity, or 0
3661 * Returns: 0 Success
3662 * !0 Errno (see fstat)
3665 sys_fstat64_extended(proc_t p
, struct fstat64_extended_args
*uap
, __unused
int32_t *retval
)
3667 return fstat(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 1);
3674 * Description: Get 64 bit version of the file status for the file associated
3677 * Parameters: p The process doing the fstat
3678 * uap->fd The fd to stat
3679 * uap->ub The user stat buffer
3681 * Returns: 0 Success
3682 * !0 Errno (see fstat)
3685 sys_fstat64(proc_t p
, struct fstat64_args
*uap
, __unused
int32_t *retval
)
3687 return fstat(p
, uap
->fd
, uap
->ub
, 0, 0, 1);
3694 * Description: Return pathconf information about a file descriptor.
3696 * Parameters: p Process making the request
3697 * uap->fd fd to get information about
3698 * uap->name Name of information desired
3699 * retval Pointer to the call return area
3701 * Returns: 0 Success
3703 * fp_lookup:EBADF Bad file descriptor
3704 * vnode_getwithref:???
3708 * *retval (modified) Returned information (numeric)
3711 sys_fpathconf(proc_t p
, struct fpathconf_args
*uap
, int32_t *retval
)
3714 struct fileproc
*fp
;
3721 AUDIT_ARG(fd
, uap
->fd
);
3722 if ((error
= fp_lookup(p
, fd
, &fp
, 0))) {
3730 if (uap
->name
!= _PC_PIPE_BUF
) {
3739 if (uap
->name
!= _PC_PIPE_BUF
) {
3748 vp
= (struct vnode
*)data
;
3750 if ((error
= vnode_getwithref(vp
)) == 0) {
3751 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
3753 error
= vn_pathconf(vp
, uap
->name
, retval
, vfs_context_current());
3755 (void)vnode_put(vp
);
3765 fp_drop(p
, fd
, fp
, 0);
3770 * Statistics counter for the number of times a process calling fdalloc()
3771 * has resulted in an expansion of the per process open file table.
3773 * XXX This would likely be of more use if it were per process
3781 * Description: Allocate a file descriptor for the process.
3783 * Parameters: p Process to allocate the fd in
3784 * want The fd we would prefer to get
3785 * result Pointer to fd we got
3787 * Returns: 0 Success
3792 * *result (modified) The fd which was allocated
3795 fdalloc(proc_t p
, int want
, int *result
)
3797 struct filedesc
*fdp
= p
->p_fd
;
3799 int last
, numfiles
, oldnfiles
;
3800 struct fileproc
**newofiles
, **ofiles
;
3801 char *newofileflags
;
3803 rlim_t nofile
= proc_limitgetcur(p
, RLIMIT_NOFILE
, TRUE
);
3805 nofile
= MIN(nofile
, INT_MAX
);
3808 * Search for a free descriptor starting at the higher
3809 * of want or fd_freefile. If that fails, consider
3810 * expanding the ofile array.
3813 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
3816 lim
= MIN(nofile
, maxfilesperproc
);
3818 last
= (int)MIN((unsigned int)fdp
->fd_nfiles
, (unsigned int)lim
);
3819 if ((i
= want
) < fdp
->fd_freefile
) {
3820 i
= fdp
->fd_freefile
;
3822 for (; i
< last
; i
++) {
3823 if (fdp
->fd_ofiles
[i
] == NULL
&& !(fdp
->fd_ofileflags
[i
] & UF_RESERVED
)) {
3824 procfdtbl_reservefd(p
, i
);
3825 if (i
> fdp
->fd_lastfile
) {
3826 fdp
->fd_lastfile
= i
;
3828 if (want
<= fdp
->fd_freefile
) {
3829 fdp
->fd_freefile
= i
;
3837 * No space in current array. Expand?
3839 if ((rlim_t
)fdp
->fd_nfiles
>= lim
) {
3842 if (fdp
->fd_nfiles
< NDEXTENT
) {
3843 numfiles
= NDEXTENT
;
3845 numfiles
= 2 * fdp
->fd_nfiles
;
3848 if ((rlim_t
)numfiles
> lim
) {
3849 numfiles
= (int)lim
;
3852 MALLOC(newofiles
, struct fileproc
**,
3853 numfiles
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
3855 if (newofiles
== NULL
) {
3858 if (fdp
->fd_nfiles
>= numfiles
) {
3859 FREE(newofiles
, M_OFILETABL
);
3862 newofileflags
= (char *) &newofiles
[numfiles
];
3864 * Copy the existing ofile and ofileflags arrays
3865 * and zero the new portion of each array.
3867 oldnfiles
= fdp
->fd_nfiles
;
3868 (void) memcpy(newofiles
, fdp
->fd_ofiles
,
3869 oldnfiles
* sizeof(*fdp
->fd_ofiles
));
3870 (void) memset(&newofiles
[oldnfiles
], 0,
3871 (numfiles
- oldnfiles
) * sizeof(*fdp
->fd_ofiles
));
3873 (void) memcpy(newofileflags
, fdp
->fd_ofileflags
,
3874 oldnfiles
* sizeof(*fdp
->fd_ofileflags
));
3875 (void) memset(&newofileflags
[oldnfiles
], 0,
3876 (numfiles
- oldnfiles
) *
3877 sizeof(*fdp
->fd_ofileflags
));
3878 ofiles
= fdp
->fd_ofiles
;
3879 fdp
->fd_ofiles
= newofiles
;
3880 fdp
->fd_ofileflags
= newofileflags
;
3881 fdp
->fd_nfiles
= numfiles
;
3882 FREE(ofiles
, M_OFILETABL
);
3891 * Description: Check to see whether n user file descriptors are available
3894 * Parameters: p Process to check in
3895 * n The number of fd's desired
3900 * Locks: Assumes proc_fdlock for process is held by the caller
3902 * Notes: The answer only remains valid so long as the proc_fdlock is
3903 * held by the caller.
3906 fdavail(proc_t p
, int n
)
3908 struct filedesc
*fdp
= p
->p_fd
;
3909 struct fileproc
**fpp
;
3913 rlim_t nofile
= proc_limitgetcur(p
, RLIMIT_NOFILE
, TRUE
);
3915 lim
= (int)MIN(nofile
, maxfilesperproc
);
3916 if ((i
= lim
- fdp
->fd_nfiles
) > 0 && (n
-= i
) <= 0) {
3919 fpp
= &fdp
->fd_ofiles
[fdp
->fd_freefile
];
3920 flags
= &fdp
->fd_ofileflags
[fdp
->fd_freefile
];
3921 for (i
= fdp
->fd_nfiles
- fdp
->fd_freefile
; --i
>= 0; fpp
++, flags
++) {
3922 if (*fpp
== NULL
&& !(*flags
& UF_RESERVED
) && --n
<= 0) {
3931 fp_get_noref_locked(proc_t p
, int fd
)
3933 struct filedesc
*fdp
= p
->p_fd
;
3934 struct fileproc
*fp
;
3936 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3937 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3938 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3945 fp_get_noref_locked_with_iocount(proc_t p
, int fd
)
3947 struct filedesc
*fdp
= p
->p_fd
;
3948 struct fileproc
*fp
= NULL
;
3950 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3951 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3952 os_ref_get_count(&fp
->fp_iocount
) <= 1 ||
3953 ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
3954 !(fdp
->fd_ofileflags
[fd
] & UF_CLOSING
))) {
3955 panic("%s: caller without an ioccount on fileproc (%d/:%p)",
3963 fp_get_ftype(proc_t p
, int fd
, file_type_t ftype
, int err
, struct fileproc
**fpp
)
3965 struct filedesc
*fdp
= p
->p_fd
;
3966 struct fileproc
*fp
;
3968 proc_fdlock_spin(p
);
3969 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3970 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3971 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3976 if (fp
->f_type
!= ftype
) {
3981 os_ref_retain_locked(&fp
->fp_iocount
);
3992 * Description: Get fileproc and vnode pointer for a given fd from the per
3993 * process open file table of the specified process, and if
3994 * successful, increment the fp_iocount
3996 * Parameters: p Process in which fd lives
3997 * fd fd to get information for
3998 * resultfp Pointer to result fileproc
3999 * pointer area, or 0 if none
4000 * resultvp Pointer to result vnode pointer
4001 * area, or 0 if none
4003 * Returns: 0 Success
4004 * EBADF Bad file descriptor
4005 * ENOTSUP fd does not refer to a vnode
4008 * *resultfp (modified) Fileproc pointer
4009 * *resultvp (modified) vnode pointer
4011 * Notes: The resultfp and resultvp fields are optional, and may be
4012 * independently specified as NULL to skip returning information
4014 * Locks: Internally takes and releases proc_fdlock
4017 fp_getfvp(proc_t p
, int fd
, struct fileproc
**resultfp
, struct vnode
**resultvp
)
4019 struct fileproc
*fp
;
4022 error
= fp_get_ftype(p
, fd
, DTYPE_VNODE
, ENOTSUP
, &fp
);
4028 *resultvp
= (struct vnode
*)fp
->f_data
;
4039 * Description: Get pipe id for a given fd from the per process open file table
4040 * of the specified process.
4042 * Parameters: p Process in which fd lives
4043 * fd fd to get information for
4044 * result_pipe_id Pointer to result pipe id
4046 * Returns: 0 Success
4047 * EIVAL NULL pointer arguments passed
4048 * fp_lookup:EBADF Bad file descriptor
4049 * ENOTSUP fd does not refer to a pipe
4052 * *result_pipe_id (modified) pipe id
4054 * Locks: Internally takes and releases proc_fdlock
4057 fp_get_pipe_id(proc_t p
, int fd
, uint64_t *result_pipe_id
)
4059 struct fileproc
*fp
= FILEPROC_NULL
;
4060 struct fileglob
*fg
= NULL
;
4063 if (p
== NULL
|| result_pipe_id
== NULL
) {
4068 if ((error
= fp_lookup(p
, fd
, &fp
, 1))) {
4074 if (FILEGLOB_DTYPE(fg
) == DTYPE_PIPE
) {
4075 *result_pipe_id
= pipe_id((struct pipe
*)fg
->fg_data
);
4080 fp_drop(p
, fd
, fp
, 1);
4089 * Description: Get fileproc pointer for a given fd from the per process
4090 * open file table of the specified process and if successful,
4091 * increment the fp_iocount
4093 * Parameters: p Process in which fd lives
4094 * fd fd to get information for
4095 * resultfp Pointer to result fileproc
4096 * pointer area, or 0 if none
4097 * locked !0 if the caller holds the
4098 * proc_fdlock, 0 otherwise
4100 * Returns: 0 Success
4101 * EBADF Bad file descriptor
4104 * *resultfp (modified) Fileproc pointer
4106 * Locks: If the argument 'locked' is non-zero, then the caller is
4107 * expected to have taken and held the proc_fdlock; if it is
4108 * zero, than this routine internally takes and drops this lock.
4111 fp_lookup(proc_t p
, int fd
, struct fileproc
**resultfp
, int locked
)
4113 struct filedesc
*fdp
= p
->p_fd
;
4114 struct fileproc
*fp
;
4117 proc_fdlock_spin(p
);
4119 if (fd
< 0 || fdp
== NULL
|| fd
>= fdp
->fd_nfiles
||
4120 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
4121 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
4127 os_ref_retain_locked(&fp
->fp_iocount
);
4143 * Description: Swap the fileproc pointer for a given fd with a new
4144 * fileproc pointer in the per-process open file table of
4145 * the specified process. The fdlock must be held at entry.
4146 * Iff the swap is successful, the old fileproc pointer is freed.
4148 * Parameters: p Process containing the fd
4149 * fd The fd of interest
4150 * nfp Pointer to the newfp
4152 * Returns: 0 Success
4153 * EBADF Bad file descriptor
4155 * EKEEPLOOKING Other references were active, try again.
4158 fp_tryswap(proc_t p
, int fd
, struct fileproc
*nfp
)
4160 struct fileproc
*fp
;
4163 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
4165 if (0 != (error
= fp_lookup(p
, fd
, &fp
, 1))) {
4169 * At this point, our caller (change_guardedfd_np) has
4170 * one fp_iocount reference, and we just took another
4171 * one to begin the replacement.
4172 * fp and nfp have a +1 reference from allocation.
4173 * Thus if no-one else is looking, fp_iocount should be 3.
4175 if (os_ref_get_count(&fp
->fp_iocount
) < 3 ||
4176 1 != os_ref_get_count(&nfp
->fp_iocount
)) {
4177 panic("%s: fp_iocount", __func__
);
4178 } else if (3 == os_ref_get_count(&fp
->fp_iocount
)) {
4179 /* Copy the contents of *fp, preserving the "type" of *nfp */
4181 nfp
->fp_flags
= (nfp
->fp_flags
& FP_TYPEMASK
) |
4182 (fp
->fp_flags
& ~FP_TYPEMASK
);
4183 os_ref_retain_locked(&nfp
->fp_iocount
);
4184 os_ref_retain_locked(&nfp
->fp_iocount
);
4185 nfp
->fp_glob
= fp
->fp_glob
;
4186 nfp
->fp_wset
= fp
->fp_wset
;
4188 p
->p_fd
->fd_ofiles
[fd
] = nfp
;
4189 fp_drop(p
, fd
, nfp
, 1);
4191 os_ref_release_live(&fp
->fp_iocount
);
4192 os_ref_release_live(&fp
->fp_iocount
);
4196 * Wait for all other active references to evaporate.
4198 p
->p_fpdrainwait
= 1;
4199 error
= msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
,
4200 PRIBIO
| PCATCH
, "tryswap fpdrain", NULL
);
4203 * Return an "internal" errno to trigger a full
4204 * reevaluation of the change-guard attempt.
4206 error
= EKEEPLOOKING
;
4208 (void) fp_drop(p
, fd
, fp
, 1);
4217 * Description: Drop the I/O reference previously taken by calling fp_lookup
4220 * Parameters: p Process in which the fd lives
4221 * fd fd associated with the fileproc
4222 * fp fileproc on which to set the
4223 * flag and drop the reference
4224 * locked flag to internally take and
4225 * drop proc_fdlock if it is not
4226 * already held by the caller
4228 * Returns: 0 Success
4229 * EBADF Bad file descriptor
4231 * Locks: This function internally takes and drops the proc_fdlock for
4232 * the supplied process if 'locked' is non-zero, and assumes that
4233 * the caller already holds this lock if 'locked' is non-zero.
4235 * Notes: The fileproc must correspond to the fd in the supplied proc
4238 fp_drop(proc_t p
, int fd
, struct fileproc
*fp
, int locked
)
4240 struct filedesc
*fdp
= p
->p_fd
;
4244 proc_fdlock_spin(p
);
4246 if ((fp
== FILEPROC_NULL
) && (fd
< 0 || fd
>= fdp
->fd_nfiles
||
4247 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
4248 ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
4249 !(fdp
->fd_ofileflags
[fd
] & UF_CLOSING
)))) {
4256 if (1 == os_ref_release_locked(&fp
->fp_iocount
)) {
4257 if (fp
->fp_flags
& FP_SELCONFLICT
) {
4258 fp
->fp_flags
&= ~FP_SELCONFLICT
;
4261 if (p
->p_fpdrainwait
) {
4262 p
->p_fpdrainwait
= 0;
4270 wakeup(&p
->p_fpdrainwait
);
4280 * Description: Given an fd, look it up in the current process's per process
4281 * open file table, and return its internal vnode pointer.
4283 * Parameters: fd fd to obtain vnode from
4284 * vpp pointer to vnode return area
4286 * Returns: 0 Success
4287 * EINVAL The fd does not refer to a
4288 * vnode fileproc entry
4289 * fp_lookup:EBADF Bad file descriptor
4292 * *vpp (modified) Returned vnode pointer
4294 * Locks: This function internally takes and drops the proc_fdlock for
4295 * the current process
4297 * Notes: If successful, this function increments the fp_iocount on the
4298 * fd's corresponding fileproc.
4300 * The fileproc referenced is not returned; because of this, care
4301 * must be taken to not drop the last reference (e.g. by closing
4302 * the file). This is inherently unsafe, since the reference may
4303 * not be recoverable from the vnode, if there is a subsequent
4304 * close that destroys the associate fileproc. The caller should
4305 * therefore retain their own reference on the fileproc so that
4306 * the fp_iocount can be dropped subsequently. Failure to do this
4307 * can result in the returned pointer immediately becoming invalid
4308 * following the call.
4310 * Use of this function is discouraged.
4313 file_vnode(int fd
, struct vnode
**vpp
)
4315 return file_vnode_withvid(fd
, vpp
, NULL
);
4319 * file_vnode_withvid
4321 * Description: Given an fd, look it up in the current process's per process
4322 * open file table, and return its internal vnode pointer.
4324 * Parameters: fd fd to obtain vnode from
4325 * vpp pointer to vnode return area
4326 * vidp pointer to vid of the returned vnode
4328 * Returns: 0 Success
4329 * EINVAL The fd does not refer to a
4330 * vnode fileproc entry
4331 * fp_lookup:EBADF Bad file descriptor
4334 * *vpp (modified) Returned vnode pointer
4336 * Locks: This function internally takes and drops the proc_fdlock for
4337 * the current process
4339 * Notes: If successful, this function increments the fp_iocount on the
4340 * fd's corresponding fileproc.
4342 * The fileproc referenced is not returned; because of this, care
4343 * must be taken to not drop the last reference (e.g. by closing
4344 * the file). This is inherently unsafe, since the reference may
4345 * not be recoverable from the vnode, if there is a subsequent
4346 * close that destroys the associate fileproc. The caller should
4347 * therefore retain their own reference on the fileproc so that
4348 * the fp_iocount can be dropped subsequently. Failure to do this
4349 * can result in the returned pointer immediately becoming invalid
4350 * following the call.
4352 * Use of this function is discouraged.
4355 file_vnode_withvid(int fd
, struct vnode
**vpp
, uint32_t *vidp
)
4357 struct fileproc
*fp
;
4360 error
= fp_get_ftype(current_proc(), fd
, DTYPE_VNODE
, EINVAL
, &fp
);
4366 *vidp
= vnode_vid(fp
->f_data
);
4375 * Description: Given an fd, look it up in the current process's per process
4376 * open file table, and return its internal socket pointer.
4378 * Parameters: fd fd to obtain vnode from
4379 * sp pointer to socket return area
4381 * Returns: 0 Success
4382 * ENOTSOCK Not a socket
4383 * fp_lookup:EBADF Bad file descriptor
4386 * *sp (modified) Returned socket pointer
4388 * Locks: This function internally takes and drops the proc_fdlock for
4389 * the current process
4391 * Notes: If successful, this function increments the fp_iocount on the
4392 * fd's corresponding fileproc.
4394 * The fileproc referenced is not returned; because of this, care
4395 * must be taken to not drop the last reference (e.g. by closing
4396 * the file). This is inherently unsafe, since the reference may
4397 * not be recoverable from the socket, if there is a subsequent
4398 * close that destroys the associate fileproc. The caller should
4399 * therefore retain their own reference on the fileproc so that
4400 * the fp_iocount can be dropped subsequently. Failure to do this
4401 * can result in the returned pointer immediately becoming invalid
4402 * following the call.
4404 * Use of this function is discouraged.
4407 file_socket(int fd
, struct socket
**sp
)
4409 struct fileproc
*fp
;
4412 error
= fp_get_ftype(current_proc(), fd
, DTYPE_SOCKET
, ENOTSOCK
, &fp
);
4415 *sp
= (struct socket
*)fp
->f_data
;
4425 * Description: Given an fd, look it up in the current process's per process
4426 * open file table, and return its fileproc's flags field.
4428 * Parameters: fd fd whose flags are to be
4430 * flags pointer to flags data area
4432 * Returns: 0 Success
4433 * ENOTSOCK Not a socket
4434 * fp_lookup:EBADF Bad file descriptor
4437 * *flags (modified) Returned flags field
4439 * Locks: This function internally takes and drops the proc_fdlock for
4440 * the current process
4443 file_flags(int fd
, int *flags
)
4445 proc_t p
= current_proc();
4446 struct fileproc
*fp
;
4449 proc_fdlock_spin(p
);
4450 fp
= fp_get_noref_locked(p
, fd
);
4452 *flags
= (int)fp
->f_flag
;
4464 * Description: Drop an iocount reference on an fd, and wake up any waiters
4465 * for draining (i.e. blocked in fileproc_drain() called during
4466 * the last attempt to close a file).
4468 * Parameters: fd fd on which an ioreference is
4471 * Returns: 0 Success
4473 * Description: Given an fd, look it up in the current process's per process
4474 * open file table, and drop it's fileproc's fp_iocount by one
4476 * Notes: This is intended as a corresponding operation to the functions
4477 * file_vnode() and file_socket() operations.
4479 * If the caller can't possibly hold an I/O reference,
4480 * this function will panic the kernel rather than allowing
4481 * for memory corruption. Callers should always call this
4482 * because they acquired an I/O reference on this file before.
4484 * Use of this function is discouraged.
4489 struct fileproc
*fp
;
4490 proc_t p
= current_proc();
4493 proc_fdlock_spin(p
);
4494 fp
= fp_get_noref_locked_with_iocount(p
, fd
);
4496 if (1 == os_ref_release_locked(&fp
->fp_iocount
)) {
4497 if (fp
->fp_flags
& FP_SELCONFLICT
) {
4498 fp
->fp_flags
&= ~FP_SELCONFLICT
;
4501 if (p
->p_fpdrainwait
) {
4502 p
->p_fpdrainwait
= 0;
4509 wakeup(&p
->p_fpdrainwait
);
4519 * Create a new open file structure and allocate
4520 * a file descriptor for the process that refers to it.
4522 * Returns: 0 Success
4524 * Description: Allocate an entry in the per process open file table and
4525 * return the corresponding fileproc and fd.
4527 * Parameters: p The process in whose open file
4528 * table the fd is to be allocated
4529 * resultfp Pointer to fileproc pointer
4531 * resultfd Pointer to fd return area
4533 * fp_zalloc fileproc allocator to use
4534 * crarg allocator args
4536 * Returns: 0 Success
4537 * ENFILE Too many open files in system
4538 * fdalloc:EMFILE Too many open files in process
4539 * fdalloc:ENOMEM M_OFILETABL zone exhausted
4540 * ENOMEM fp_zone or fg_zone zone
4544 * *resultfd (modified) Returned fileproc pointer
4545 * *resultfd (modified) Returned fd
4547 * Notes: This function takes separate process and context arguments
4548 * solely to support kern_exec.c; otherwise, it would take
4549 * neither, and use the vfs_context_current() routine internally.
4552 falloc_withalloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4553 vfs_context_t ctx
, fp_allocfn_t fp_zalloc
, void *crarg
)
4555 struct fileproc
*fp
;
4556 struct fileglob
*fg
;
4559 /* Make sure we don't go beyond the system-wide limit */
4560 if (nfiles
>= maxfiles
) {
4567 /* fdalloc will make sure the process stays below per-process limit */
4568 if ((error
= fdalloc(p
, 0, &nfd
))) {
4574 error
= mac_file_check_create(proc_ucred(p
));
4582 * Allocate a new file descriptor.
4583 * If the process has file descriptor zero open, add to the list
4584 * of open files at that point, otherwise put it at the front of
4585 * the list of open files.
4589 fp
= (*fp_zalloc
)(crarg
);
4593 fg
= zalloc_flags(fg_zone
, Z_WAITOK
| Z_ZERO
);
4594 lck_mtx_init(&fg
->fg_lock
, file_lck_grp
, file_lck_attr
);
4596 os_ref_retain_locked(&fp
->fp_iocount
);
4597 os_ref_init_raw(&fg
->fg_count
, &f_refgrp
);
4598 fg
->fg_ops
= &uninitops
;
4601 mac_file_label_init(fg
);
4604 kauth_cred_ref(ctx
->vc_ucred
);
4606 fp
->f_cred
= ctx
->vc_ucred
;
4609 mac_file_label_associate(fp
->f_cred
, fg
);
4612 os_atomic_inc(&nfiles
, relaxed
);
4616 p
->p_fd
->fd_ofiles
[nfd
] = fp
;
4631 falloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
)
4633 return falloc_withalloc(p
, resultfp
, resultfd
, ctx
,
4634 fileproc_alloc_init
, NULL
);
4640 * Description: Perform close-on-exec processing for all files in a process
4641 * that are either marked as close-on-exec, or which were in the
4642 * process of being opened at the time of the execve
4644 * Also handles the case (via posix_spawn()) where -all-
4645 * files except those marked with "inherit" as treated as
4648 * Parameters: p Pointer to process calling
4653 * Locks: This function internally takes and drops proc_fdlock()
4654 * But assumes tables don't grow/change while unlocked.
4658 fdexec(proc_t p
, short flags
, int self_exec
)
4660 struct filedesc
*fdp
= p
->p_fd
;
4662 boolean_t cloexec_default
= (flags
& POSIX_SPAWN_CLOEXEC_DEFAULT
) != 0;
4663 thread_t self
= current_thread();
4664 struct uthread
*ut
= get_bsdthread_info(self
);
4665 struct kqworkq
*dealloc_kqwq
= NULL
;
4668 * If the current thread is bound as a workq/workloop
4669 * servicing thread, we need to unbind it first.
4671 if (ut
->uu_kqr_bound
&& self_exec
) {
4672 kqueue_threadreq_unbind(p
, ut
->uu_kqr_bound
);
4678 * Deallocate the knotes for this process
4679 * and mark the tables non-existent so
4680 * subsequent kqueue closes go faster.
4683 assert(fdp
->fd_knlistsize
== 0);
4684 assert(fdp
->fd_knhashmask
== 0);
4686 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
4687 struct fileproc
*fp
= fdp
->fd_ofiles
[i
];
4688 char *flagp
= &fdp
->fd_ofileflags
[i
];
4690 if (fp
&& cloexec_default
) {
4692 * Reverse the usual semantics of file descriptor
4693 * inheritance - all of them should be closed
4694 * except files marked explicitly as "inherit" and
4695 * not marked close-on-exec.
4697 if ((*flagp
& (UF_EXCLOSE
| UF_INHERIT
)) != UF_INHERIT
) {
4698 *flagp
|= UF_EXCLOSE
;
4700 *flagp
&= ~UF_INHERIT
;
4704 ((*flagp
& (UF_RESERVED
| UF_EXCLOSE
)) == UF_EXCLOSE
)
4706 || (fp
&& mac_file_check_inherit(proc_ucred(p
), fp
->fp_glob
))
4709 fp_close_and_unlock(p
, i
, fp
, 0);
4714 /* release the per-process workq kq */
4715 if (fdp
->fd_wqkqueue
) {
4716 dealloc_kqwq
= fdp
->fd_wqkqueue
;
4717 fdp
->fd_wqkqueue
= NULL
;
4722 /* Anything to free? */
4724 kqworkq_dealloc(dealloc_kqwq
);
4732 * Description: Copy a filedesc structure. This is normally used as part of
4733 * forkproc() when forking a new process, to copy the per process
4734 * open file table over to the new process.
4736 * Parameters: p Process whose open file table
4737 * is to be copied (parent)
4738 * uth_cdir Per thread current working
4739 * cirectory, or NULL
4741 * Returns: NULL Copy failed
4742 * !NULL Pointer to new struct filedesc
4744 * Locks: This function internally takes and drops proc_fdlock()
4746 * Notes: Files are copied directly, ignoring the new resource limits
4747 * for the process that's being copied into. Since the descriptor
4748 * references are just additional references, this does not count
4749 * against the number of open files on the system.
4751 * The struct filedesc includes the current working directory,
4752 * and the current root directory, if the process is chroot'ed.
4754 * If the exec was called by a thread using a per thread current
4755 * working directory, we inherit the working directory from the
4756 * thread making the call, rather than from the process.
4758 * In the case of a failure to obtain a reference, for most cases,
4759 * the file entry will be silently dropped. There's an exception
4760 * for the case of a chroot dir, since a failure to to obtain a
4761 * reference there would constitute an "escape" from the chroot
4762 * environment, which must not be allowed. In that case, we will
4763 * deny the execve() operation, rather than allowing the escape.
4766 fdcopy(proc_t p
, vnode_t uth_cdir
)
4768 struct filedesc
*newfdp
, *fdp
= p
->p_fd
;
4770 struct fileproc
*ofp
, *fp
;
4773 newfdp
= zalloc(fdp_zone
);
4778 * the FD_CHROOT flag will be inherited via this copy
4780 (void) memcpy(newfdp
, fdp
, sizeof(*newfdp
));
4783 * If we are running with per-thread current working directories,
4784 * inherit the new current working directory from the current thread
4785 * instead, before we take our references.
4787 if (uth_cdir
!= NULLVP
) {
4788 newfdp
->fd_cdir
= uth_cdir
;
4792 * For both fd_cdir and fd_rdir make sure we get
4793 * a valid reference... if we can't, than set
4794 * set the pointer(s) to NULL in the child... this
4795 * will keep us from using a non-referenced vp
4796 * and allows us to do the vnode_rele only on
4797 * a properly referenced vp
4799 if ((v_dir
= newfdp
->fd_cdir
)) {
4800 if (vnode_getwithref(v_dir
) == 0) {
4801 if ((vnode_ref(v_dir
))) {
4802 newfdp
->fd_cdir
= NULL
;
4806 newfdp
->fd_cdir
= NULL
;
4809 if (newfdp
->fd_cdir
== NULL
&& fdp
->fd_cdir
) {
4811 * we couldn't get a new reference on
4812 * the current working directory being
4813 * inherited... we might as well drop
4814 * our reference from the parent also
4815 * since the vnode has gone DEAD making
4816 * it useless... by dropping it we'll
4817 * be that much closer to recycling it
4819 vnode_rele(fdp
->fd_cdir
);
4820 fdp
->fd_cdir
= NULL
;
4823 if ((v_dir
= newfdp
->fd_rdir
)) {
4824 if (vnode_getwithref(v_dir
) == 0) {
4825 if ((vnode_ref(v_dir
))) {
4826 newfdp
->fd_rdir
= NULL
;
4830 newfdp
->fd_rdir
= NULL
;
4833 /* Coming from a chroot environment and unable to get a reference... */
4834 if (newfdp
->fd_rdir
== NULL
&& fdp
->fd_rdir
) {
4837 * We couldn't get a new reference on
4838 * the chroot directory being
4839 * inherited... this is fatal, since
4840 * otherwise it would constitute an
4841 * escape from a chroot environment by
4844 if (newfdp
->fd_cdir
) {
4845 vnode_rele(newfdp
->fd_cdir
);
4847 zfree(fdp_zone
, newfdp
);
4852 * If the number of open files fits in the internal arrays
4853 * of the open file structure, use them, otherwise allocate
4854 * additional memory for the number of descriptors currently
4857 if (newfdp
->fd_lastfile
< NDFILE
) {
4861 * Compute the smallest multiple of NDEXTENT needed
4862 * for the file descriptors currently in use,
4863 * allowing the table to shrink.
4865 i
= newfdp
->fd_nfiles
;
4866 while (i
> 1 + 2 * NDEXTENT
&& i
> 1 + newfdp
->fd_lastfile
* 2) {
4872 MALLOC(newfdp
->fd_ofiles
, struct fileproc
**,
4873 i
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
4874 if (newfdp
->fd_ofiles
== NULL
) {
4875 if (newfdp
->fd_cdir
) {
4876 vnode_rele(newfdp
->fd_cdir
);
4878 if (newfdp
->fd_rdir
) {
4879 vnode_rele(newfdp
->fd_rdir
);
4882 zfree(fdp_zone
, newfdp
);
4885 (void) memset(newfdp
->fd_ofiles
, 0, i
* OFILESIZE
);
4888 newfdp
->fd_ofileflags
= (char *) &newfdp
->fd_ofiles
[i
];
4889 newfdp
->fd_nfiles
= i
;
4891 if (fdp
->fd_nfiles
> 0) {
4892 struct fileproc
**fpp
;
4895 (void) memcpy(newfdp
->fd_ofiles
, fdp
->fd_ofiles
,
4896 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofiles
));
4897 (void) memcpy(newfdp
->fd_ofileflags
, fdp
->fd_ofileflags
,
4898 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofileflags
));
4900 fpp
= &newfdp
->fd_ofiles
[newfdp
->fd_lastfile
];
4901 flags
= &newfdp
->fd_ofileflags
[newfdp
->fd_lastfile
];
4902 for (i
= newfdp
->fd_lastfile
; i
>= 0; i
--, fpp
--, flags
--) {
4903 if ((ofp
= *fpp
) != NULL
&&
4904 0 == (ofp
->fp_glob
->fg_lflags
& FG_CONFINED
) &&
4905 0 == (*flags
& (UF_FORKCLOSE
| UF_RESERVED
))) {
4907 if (FILEPROC_TYPE(ofp
) != FTYPE_SIMPLE
) {
4908 panic("complex fileproc");
4911 fp
= fileproc_alloc_init(NULL
);
4914 * XXX no room to copy, unable to
4915 * XXX safely unwind state at present
4920 (ofp
->fp_flags
& ~FP_TYPEMASK
);
4921 fp
->fp_glob
= ofp
->fp_glob
;
4922 fg_ref(fp
->fp_glob
);
4930 if (i
== newfdp
->fd_lastfile
&& i
> 0) {
4931 newfdp
->fd_lastfile
--;
4933 if (i
< newfdp
->fd_freefile
) {
4934 newfdp
->fd_freefile
= i
;
4943 * Initialize knote and kqueue tracking structs
4945 newfdp
->fd_knlist
= NULL
;
4946 newfdp
->fd_knlistsize
= 0;
4947 newfdp
->fd_knhash
= NULL
;
4948 newfdp
->fd_knhashmask
= 0;
4949 newfdp
->fd_kqhash
= NULL
;
4950 newfdp
->fd_kqhashmask
= 0;
4951 newfdp
->fd_wqkqueue
= NULL
;
4952 lck_mtx_init(&newfdp
->fd_kqhashlock
, proc_kqhashlock_grp
, proc_lck_attr
);
4953 lck_mtx_init(&newfdp
->fd_knhashlock
, proc_knhashlock_grp
, proc_lck_attr
);
4962 * Description: Release a filedesc (per process open file table) structure;
4963 * this is done on process exit(), or from forkproc_free() if
4964 * the fork fails for some reason subsequent to a successful
4967 * Parameters: p Pointer to process going away
4971 * Locks: This function internally takes and drops proc_fdlock()
4976 struct filedesc
*fdp
;
4977 struct fileproc
*fp
;
4978 struct kqworkq
*dealloc_kqwq
= NULL
;
4983 if (p
== kernproc
|| NULL
== (fdp
= p
->p_fd
)) {
4988 extern struct filedesc filedesc0
;
4990 if (&filedesc0
== fdp
) {
4995 * deallocate all the knotes up front and claim empty
4996 * tables to make any subsequent kqueue closes faster.
4999 assert(fdp
->fd_knlistsize
== 0);
5000 assert(fdp
->fd_knhashmask
== 0);
5003 * dealloc all workloops that have outstanding retains
5004 * when created with scheduling parameters.
5006 kqworkloops_dealloc(p
);
5008 /* close file descriptors */
5009 if (fdp
->fd_nfiles
> 0 && fdp
->fd_ofiles
) {
5010 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
5011 if ((fp
= fdp
->fd_ofiles
[i
]) != NULL
) {
5012 if (fdp
->fd_ofileflags
[i
] & UF_RESERVED
) {
5013 panic("fdfree: found fp with UF_RESERVED");
5015 fp_close_and_unlock(p
, i
, fp
, 0);
5019 FREE(fdp
->fd_ofiles
, M_OFILETABL
);
5020 fdp
->fd_ofiles
= NULL
;
5024 if (fdp
->fd_wqkqueue
) {
5025 dealloc_kqwq
= fdp
->fd_wqkqueue
;
5026 fdp
->fd_wqkqueue
= NULL
;
5032 kqworkq_dealloc(dealloc_kqwq
);
5035 vnode_rele(fdp
->fd_cdir
);
5038 vnode_rele(fdp
->fd_rdir
);
5041 proc_fdlock_spin(p
);
5045 if (fdp
->fd_kqhash
) {
5046 for (uint32_t j
= 0; j
<= fdp
->fd_kqhashmask
; j
++) {
5047 assert(LIST_EMPTY(&fdp
->fd_kqhash
[j
]));
5049 hashdestroy(fdp
->fd_kqhash
, M_KQUEUE
, fdp
->fd_kqhashmask
);
5052 lck_mtx_destroy(&fdp
->fd_kqhashlock
, proc_kqhashlock_grp
);
5053 lck_mtx_destroy(&fdp
->fd_knhashlock
, proc_knhashlock_grp
);
5055 zfree(fdp_zone
, fdp
);
5061 * Description: Drain out pending I/O operations
5063 * Parameters: p Process closing this file
5064 * fp fileproc struct for the open
5065 * instance on the file
5069 * Locks: Assumes the caller holds the proc_fdlock
5071 * Notes: For character devices, this occurs on the last close of the
5072 * device; for all other file descriptors, this occurs on each
5073 * close to prevent fd's from being closed out from under
5074 * operations currently in progress and blocked
5076 * See Also: file_vnode(), file_socket(), file_drop(), and the cautions
5077 * regarding their use and interaction with this function.
5080 fileproc_drain(proc_t p
, struct fileproc
* fp
)
5082 struct vfs_context context
;
5084 bool is_current_proc
;
5086 is_current_proc
= (p
== current_proc());
5088 if (!is_current_proc
) {
5090 thread
= proc_thread(p
); /* XXX */
5091 thread_reference(thread
);
5094 thread
= current_thread();
5097 context
.vc_thread
= thread
;
5098 context
.vc_ucred
= fp
->fp_glob
->fg_cred
;
5100 /* Set the vflag for drain */
5101 fileproc_modify_vflags(fp
, FPV_DRAIN
, FALSE
);
5103 while (os_ref_get_count(&fp
->fp_iocount
) > 1) {
5104 lck_mtx_convert_spin(&p
->p_fdmlock
);
5106 fo_drain(fp
, &context
);
5107 if ((fp
->fp_flags
& FP_INSELECT
) == FP_INSELECT
) {
5108 if (waitq_wakeup64_all((struct waitq
*)fp
->fp_wset
, NO_EVENT64
,
5109 THREAD_INTERRUPTED
, WAITQ_ALL_PRIORITIES
) == KERN_INVALID_ARGUMENT
) {
5110 panic("bad wait queue for waitq_wakeup64_all %p (fp:%p)", fp
->fp_wset
, fp
);
5113 if ((fp
->fp_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
) {
5114 if (waitq_wakeup64_all(&select_conflict_queue
, NO_EVENT64
,
5115 THREAD_INTERRUPTED
, WAITQ_ALL_PRIORITIES
) == KERN_INVALID_ARGUMENT
) {
5116 panic("bad select_conflict_queue");
5119 p
->p_fpdrainwait
= 1;
5121 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
, "fpdrain", NULL
);
5124 if ((fp
->fp_flags
& FP_INSELECT
) != 0) {
5125 panic("FP_INSELECT set on drained fp");
5128 if ((fp
->fp_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
) {
5129 fp
->fp_flags
&= ~FP_SELCONFLICT
;
5132 if (!is_current_proc
) {
5133 thread_deallocate(thread
);
5141 * Description: Release the fd and free the fileproc associated with the fd
5142 * in the per process open file table of the specified process;
5143 * these values must correspond.
5145 * Parameters: p Process containing fd
5146 * fd fd to be released
5147 * fp fileproc to be freed
5150 fp_free(proc_t p
, int fd
, struct fileproc
* fp
)
5152 proc_fdlock_spin(p
);
5156 fg_free(fp
->fp_glob
);
5157 os_ref_release_live(&fp
->fp_iocount
);
5165 * Description: Apply an advisory lock on a file descriptor.
5167 * Parameters: p Process making request
5168 * uap->fd fd on which the lock is to be
5170 * uap->how (Un)Lock bits, including type
5171 * retval Pointer to the call return area
5173 * Returns: 0 Success
5174 * fp_getfvp:EBADF Bad file descriptor
5175 * fp_getfvp:ENOTSUP fd does not refer to a vnode
5176 * vnode_getwithref:???
5180 * *retval (modified) Size of dtable
5182 * Notes: Just attempt to get a record lock of the requested type on
5183 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
5186 sys_flock(proc_t p
, struct flock_args
*uap
, __unused
int32_t *retval
)
5190 struct fileproc
*fp
;
5193 vfs_context_t ctx
= vfs_context_current();
5196 AUDIT_ARG(fd
, uap
->fd
);
5197 if ((error
= fp_getfvp(p
, fd
, &fp
, &vp
))) {
5200 if ((error
= vnode_getwithref(vp
))) {
5203 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
5205 lf
.l_whence
= SEEK_SET
;
5208 if (how
& LOCK_UN
) {
5209 lf
.l_type
= F_UNLCK
;
5210 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->fp_glob
, F_UNLCK
, &lf
, F_FLOCK
, ctx
, NULL
);
5213 if (how
& LOCK_EX
) {
5214 lf
.l_type
= F_WRLCK
;
5215 } else if (how
& LOCK_SH
) {
5216 lf
.l_type
= F_RDLCK
;
5222 error
= mac_file_check_lock(proc_ucred(p
), fp
->fp_glob
, F_SETLK
, &lf
);
5227 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->fp_glob
, F_SETLK
, &lf
,
5228 (how
& LOCK_NB
? F_FLOCK
: F_FLOCK
| F_WAIT
),
5231 os_atomic_or(&fp
->fp_glob
->fg_flag
, FWASLOCKED
, relaxed
);
5234 (void)vnode_put(vp
);
5236 fp_drop(p
, fd
, fp
, 0);
5241 * sys_fileport_makeport
5243 * Description: Obtain a Mach send right for a given file descriptor.
5245 * Parameters: p Process calling fileport
5246 * uap->fd The fd to reference
5247 * uap->portnamep User address at which to place port name.
5249 * Returns: 0 Success.
5250 * EBADF Bad file descriptor.
5251 * EINVAL File descriptor had type that cannot be sent, misc. other errors.
5252 * EFAULT Address at which to store port name is not valid.
5253 * EAGAIN Resource shortage.
5256 * On success, name of send right is stored at user-specified address.
5259 sys_fileport_makeport(proc_t p
, struct fileport_makeport_args
*uap
,
5260 __unused
int *retval
)
5264 user_addr_t user_portaddr
= uap
->portnamep
;
5265 struct fileproc
*fp
= FILEPROC_NULL
;
5266 struct fileglob
*fg
= NULL
;
5267 ipc_port_t fileport
;
5268 mach_port_name_t name
= MACH_PORT_NULL
;
5271 err
= fp_lookup(p
, fd
, &fp
, 1);
5277 if (!fg_sendable(fg
)) {
5282 if (FP_ISGUARDED(fp
, GUARD_FILEPORT
)) {
5283 err
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_FILEPORT
);
5289 /* Allocate and initialize a port */
5290 fileport
= fileport_alloc(fg
);
5291 if (fileport
== IPC_PORT_NULL
) {
5296 /* Dropped when port is deallocated */
5299 /* Add an entry. Deallocates port on failure. */
5300 name
= ipc_port_copyout_send(fileport
, get_task_ipcspace(p
->task
));
5301 if (!MACH_PORT_VALID(name
)) {
5306 err
= copyout(&name
, user_portaddr
, sizeof(mach_port_name_t
));
5311 /* Tag the fileglob for debugging purposes */
5312 lck_mtx_lock_spin(&fg
->fg_lock
);
5313 fg
->fg_lflags
|= FG_PORTMADE
;
5314 lck_mtx_unlock(&fg
->fg_lock
);
5316 fp_drop(p
, fd
, fp
, 0);
5323 if (MACH_PORT_VALID(name
)) {
5324 /* Don't care if another thread races us to deallocate the entry */
5325 (void) mach_port_deallocate(get_task_ipcspace(p
->task
), name
);
5328 if (fp
!= FILEPROC_NULL
) {
5329 fp_drop(p
, fd
, fp
, 0);
5336 fileport_releasefg(struct fileglob
*fg
)
5338 (void)fg_drop(PROC_NULL
, fg
);
5344 * Description: Obtain the file descriptor for a given Mach send right.
5346 * Returns: 0 Success
5347 * EINVAL Invalid Mach port name, or port is not for a file.
5349 * fdalloc:ENOMEM Unable to allocate fileproc or extend file table.
5352 * *retval (modified) The new descriptor
5355 fileport_makefd(proc_t p
, ipc_port_t port
, int uf_flags
, int *retval
)
5357 struct fileglob
*fg
;
5358 struct fileproc
*fp
= FILEPROC_NULL
;
5362 fg
= fileport_port_to_fileglob(port
);
5368 fp
= fileproc_alloc_init(NULL
);
5369 if (fp
== FILEPROC_NULL
) {
5375 err
= fdalloc(p
, 0, &fd
);
5381 *fdflags(p
, fd
) |= uf_flags
;
5387 procfdtbl_releasefd(p
, fd
, fp
);
5393 if ((fp
!= NULL
) && (0 != err
)) {
5401 * sys_fileport_makefd
5403 * Description: Obtain the file descriptor for a given Mach send right.
5405 * Parameters: p Process calling fileport
5406 * uap->port Name of send right to file port.
5408 * Returns: 0 Success
5409 * EINVAL Invalid Mach port name, or port is not for a file.
5411 * fdalloc:ENOMEM Unable to allocate fileproc or extend file table.
5414 * *retval (modified) The new descriptor
5417 sys_fileport_makefd(proc_t p
, struct fileport_makefd_args
*uap
, int32_t *retval
)
5419 ipc_port_t port
= IPC_PORT_NULL
;
5420 mach_port_name_t send
= uap
->port
;
5424 res
= ipc_object_copyin(get_task_ipcspace(p
->task
),
5425 send
, MACH_MSG_TYPE_COPY_SEND
, &port
, 0, NULL
, IPC_KMSG_FLAGS_ALLOW_IMMOVABLE_SEND
);
5427 if (res
== KERN_SUCCESS
) {
5428 err
= fileport_makefd(p
, port
, UF_EXCLOSE
, retval
);
5433 if (IPC_PORT_NULL
!= port
) {
5434 ipc_port_release_send(port
);
5444 * Description: Duplicate the specified descriptor to a free descriptor;
5445 * this is the second half of fdopen(), above.
5447 * Parameters: fdp filedesc pointer to fill in
5449 * dfd fd to dup from
5450 * mode mode to set on new fd
5451 * error command code
5453 * Returns: 0 Success
5454 * EBADF Source fd is bad
5455 * EACCES Requested mode not allowed
5456 * !0 'error', if not ENODEV or
5459 * Notes: XXX This is not thread safe; see fdopen() above
5462 dupfdopen(struct filedesc
*fdp
, int indx
, int dfd
, int flags
, int error
)
5464 struct fileproc
*wfp
;
5465 struct fileproc
*fp
;
5469 proc_t p
= current_proc();
5472 * If the to-be-dup'd fd number is greater than the allowed number
5473 * of file descriptors, or the fd to be dup'd has already been
5474 * closed, reject. Note, check for new == old is necessary as
5475 * falloc could allocate an already closed to-be-dup'd descriptor
5476 * as the new descriptor.
5480 fp
= fdp
->fd_ofiles
[indx
];
5481 if (dfd
< 0 || dfd
>= fdp
->fd_nfiles
||
5482 (wfp
= fdp
->fd_ofiles
[dfd
]) == NULL
|| wfp
== fp
||
5483 (fdp
->fd_ofileflags
[dfd
] & UF_RESERVED
)) {
5488 myerror
= mac_file_check_dup(proc_ucred(p
), wfp
->fp_glob
, dfd
);
5495 * There are two cases of interest here.
5497 * For ENODEV simply dup (dfd) to file descriptor
5498 * (indx) and return.
5500 * For ENXIO steal away the file structure from (dfd) and
5501 * store it in (indx). (dfd) is effectively closed by
5504 * Any other error code is just returned.
5508 if (FP_ISGUARDED(wfp
, GUARD_DUP
)) {
5514 * Check that the mode the file is being opened for is a
5515 * subset of the mode of the existing descriptor.
5517 if (((flags
& (FREAD
| FWRITE
)) | wfp
->f_flag
) != wfp
->f_flag
) {
5521 if (indx
> fdp
->fd_lastfile
) {
5522 fdp
->fd_lastfile
= indx
;
5526 fg_free(fp
->fp_glob
);
5528 fg_ref(wfp
->fp_glob
);
5529 fp
->fp_glob
= wfp
->fp_glob
;
5531 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
] |
5532 (flags
& O_CLOEXEC
) ? UF_EXCLOSE
: 0;
5548 * Description: Generic fileops read indirected through the fileops pointer
5549 * in the fileproc structure
5551 * Parameters: fp fileproc structure pointer
5552 * uio user I/O structure pointer
5554 * ctx VFS context for operation
5556 * Returns: 0 Success
5557 * !0 Errno from read
5560 fo_read(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
5562 return (*fp
->f_ops
->fo_read
)(fp
, uio
, flags
, ctx
);
5566 fo_no_read(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
5568 #pragma unused(fp, uio, flags, ctx)
5576 * Description: Generic fileops write indirected through the fileops pointer
5577 * in the fileproc structure
5579 * Parameters: fp fileproc structure pointer
5580 * uio user I/O structure pointer
5582 * ctx VFS context for operation
5584 * Returns: 0 Success
5585 * !0 Errno from write
5588 fo_write(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
5590 return (*fp
->f_ops
->fo_write
)(fp
, uio
, flags
, ctx
);
5594 fo_no_write(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
5596 #pragma unused(fp, uio, flags, ctx)
5604 * Description: Generic fileops ioctl indirected through the fileops pointer
5605 * in the fileproc structure
5607 * Parameters: fp fileproc structure pointer
5609 * data pointer to internalized copy
5610 * of user space ioctl command
5611 * parameter data in kernel space
5612 * ctx VFS context for operation
5614 * Returns: 0 Success
5615 * !0 Errno from ioctl
5617 * Locks: The caller is assumed to have held the proc_fdlock; this
5618 * function releases and reacquires this lock. If the caller
5619 * accesses data protected by this lock prior to calling this
5620 * function, it will need to revalidate/reacquire any cached
5621 * protected data obtained prior to the call.
5624 fo_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
, vfs_context_t ctx
)
5628 proc_fdunlock(vfs_context_proc(ctx
));
5629 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, com
, data
, ctx
);
5630 proc_fdlock(vfs_context_proc(ctx
));
5635 fo_no_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
, vfs_context_t ctx
)
5637 #pragma unused(fp, com, data, ctx)
5645 * Description: Generic fileops select indirected through the fileops pointer
5646 * in the fileproc structure
5648 * Parameters: fp fileproc structure pointer
5649 * which select which
5650 * wql pointer to wait queue list
5651 * ctx VFS context for operation
5653 * Returns: 0 Success
5654 * !0 Errno from select
5657 fo_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
5659 return (*fp
->f_ops
->fo_select
)(fp
, which
, wql
, ctx
);
5663 fo_no_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
5665 #pragma unused(fp, which, wql, ctx)
5673 * Description: Generic fileops close indirected through the fileops pointer
5674 * in the fileproc structure
5676 * Parameters: fp fileproc structure pointer for
5678 * ctx VFS context for operation
5680 * Returns: 0 Success
5681 * !0 Errno from close
5684 fo_close(struct fileglob
*fg
, vfs_context_t ctx
)
5686 return (*fg
->fg_ops
->fo_close
)(fg
, ctx
);
5693 * Description: Generic fileops kqueue filter indirected through the fileops
5694 * pointer in the fileproc structure
5696 * Parameters: fp fileproc structure pointer
5697 * ctx VFS context for operation
5699 * Returns: 0 Success
5700 * !0 errno from drain
5703 fo_drain(struct fileproc
*fp
, vfs_context_t ctx
)
5705 return (*fp
->f_ops
->fo_drain
)(fp
, ctx
);
5709 fo_no_drain(struct fileproc
*fp
, vfs_context_t ctx
)
5711 #pragma unused(fp, ctx)
5719 * Description: Generic fileops kqueue filter indirected through the fileops
5720 * pointer in the fileproc structure
5722 * Parameters: fp fileproc structure pointer
5723 * kn pointer to knote to filter on
5725 * Returns: (kn->kn_flags & EV_ERROR) error in kn->kn_data
5726 * 0 Filter is not active
5727 * !0 Filter is active
5730 fo_kqfilter(struct fileproc
*fp
, struct knote
*kn
, struct kevent_qos_s
*kev
)
5732 return (*fp
->f_ops
->fo_kqfilter
)(fp
, kn
, kev
);
5736 fo_no_kqfilter(struct fileproc
*fp
, struct knote
*kn
, struct kevent_qos_s
*kev
)
5738 #pragma unused(fp, kev)
5739 knote_set_error(kn
, ENOTSUP
);
5745 fileproc_alloc_init(__unused
void *arg
)
5747 struct fileproc
*fp
= zalloc_flags(fp_zone
, Z_WAITOK
| Z_ZERO
);
5749 os_ref_init(&fp
->fp_iocount
, &f_refgrp
);
5755 fileproc_free(struct fileproc
*fp
)
5757 os_ref_count_t __unused refc
= os_ref_release(&fp
->fp_iocount
);
5758 #if DEVELOPMENT || DEBUG
5760 panic("%s: pid %d refc: %u != 0",
5761 __func__
, proc_pid(current_proc()), refc
);
5764 switch (FILEPROC_TYPE(fp
)) {
5769 guarded_fileproc_free(fp
);
5772 panic("%s: corrupt fp %p flags %x", __func__
, fp
, fp
->fp_flags
);
5777 fileproc_modify_vflags(struct fileproc
*fp
, fileproc_vflags_t vflags
, boolean_t clearflags
)
5780 os_atomic_andnot(&fp
->fp_vflags
, vflags
, relaxed
);
5782 os_atomic_or(&fp
->fp_vflags
, vflags
, relaxed
);
5787 fileproc_get_vflags(struct fileproc
*fp
)
5789 return os_atomic_load(&fp
->fp_vflags
, relaxed
);