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>
103 #include <security/audit/audit.h>
105 #include <sys/mount_internal.h>
106 #include <sys/kdebug.h>
107 #include <sys/sysproto.h>
108 #include <sys/pipe.h>
109 #include <sys/spawn.h>
110 #include <sys/cprotect.h>
111 #include <kern/kern_types.h>
112 #include <kern/kalloc.h>
113 #include <kern/waitq.h>
114 #include <libkern/OSAtomic.h>
116 #include <sys/ubc_internal.h>
118 #include <kern/ipc_misc.h>
119 #include <vm/vm_protos.h>
121 #include <mach/mach_port.h>
125 #include <security/mac_framework.h>
128 #define IPC_KMSG_FLAGS_ALLOW_IMMOVABLE_SEND 0x1
129 kern_return_t
ipc_object_copyin(ipc_space_t
, mach_port_name_t
,
130 mach_msg_type_name_t
, ipc_port_t
*, mach_port_context_t
, mach_msg_guard_flags_t
*, uint32_t);
131 void ipc_port_release_send(ipc_port_t
);
136 static int finishdup(proc_t p
,
137 struct filedesc
*fdp
, int old
, int new, int flags
, int32_t *retval
);
139 int falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
, int locked
);
140 void fg_drop(struct fileproc
* fp
);
141 void fg_free(struct fileglob
*fg
);
142 void fg_ref(struct fileproc
* fp
);
143 void fileport_releasefg(struct fileglob
*fg
);
145 /* flags for close_internal_locked */
146 #define FD_DUP2RESV 1
148 /* We don't want these exported */
151 int unlink1(vfs_context_t
, vnode_t
, user_addr_t
, enum uio_seg
, int);
153 static void _fdrelse(struct proc
* p
, int fd
);
156 extern void file_lock_init(void);
158 extern kauth_scope_t kauth_scope_fileop
;
160 /* Conflict wait queue for when selects collide (opaque type) */
161 extern struct waitq select_conflict_queue
;
163 #ifndef HFS_GET_BOOT_INFO
164 #define HFS_GET_BOOT_INFO (FCNTL_FS_SPECIFIC_BASE + 0x00004)
167 #ifndef HFS_SET_BOOT_INFO
168 #define HFS_SET_BOOT_INFO (FCNTL_FS_SPECIFIC_BASE + 0x00005)
171 #ifndef APFSIOC_REVERT_TO_SNAPSHOT
172 #define APFSIOC_REVERT_TO_SNAPSHOT _IOW('J', 1, u_int64_t)
175 #define f_flag f_fglob->fg_flag
176 #define f_type f_fglob->fg_ops->fo_type
177 #define f_msgcount f_fglob->fg_msgcount
178 #define f_cred f_fglob->fg_cred
179 #define f_ops f_fglob->fg_ops
180 #define f_offset f_fglob->fg_offset
181 #define f_data f_fglob->fg_data
182 #define CHECK_ADD_OVERFLOW_INT64L(x, y) \
183 (((((x) > 0) && ((y) > 0) && ((x) > LLONG_MAX - (y))) || \
184 (((x) < 0) && ((y) < 0) && ((x) < LLONG_MIN - (y)))) \
187 * Descriptor management.
189 struct fmsglist fmsghead
; /* head of list of open files */
190 struct fmsglist fmsg_ithead
; /* head of list of open files */
191 int nfiles
; /* actual number of open files */
194 lck_grp_attr_t
* file_lck_grp_attr
;
195 lck_grp_t
* file_lck_grp
;
196 lck_attr_t
* file_lck_attr
;
198 lck_mtx_t
* uipc_lock
;
202 * check_file_seek_range
204 * Description: Checks if seek offsets are in the range of 0 to LLONG_MAX.
206 * Parameters: fl Flock structure.
207 * cur_file_offset Current offset in the file.
209 * Returns: 0 on Success.
210 * EOVERFLOW on overflow.
211 * EINVAL on offset less than zero.
215 check_file_seek_range(struct flock
*fl
, off_t cur_file_offset
)
217 if (fl
->l_whence
== SEEK_CUR
) {
218 /* Check if the start marker is beyond LLONG_MAX. */
219 if (CHECK_ADD_OVERFLOW_INT64L(fl
->l_start
, cur_file_offset
)) {
220 /* Check if start marker is negative */
221 if (fl
->l_start
< 0) {
226 /* Check if the start marker is negative. */
227 if (fl
->l_start
+ cur_file_offset
< 0) {
230 /* Check if end marker is beyond LLONG_MAX. */
231 if ((fl
->l_len
> 0) && (CHECK_ADD_OVERFLOW_INT64L(fl
->l_start
+
232 cur_file_offset
, fl
->l_len
- 1))) {
235 /* Check if the end marker is negative. */
236 if ((fl
->l_len
<= 0) && (fl
->l_start
+ cur_file_offset
+
240 } else if (fl
->l_whence
== SEEK_SET
) {
241 /* Check if the start marker is negative. */
242 if (fl
->l_start
< 0) {
245 /* Check if the end marker is beyond LLONG_MAX. */
246 if ((fl
->l_len
> 0) &&
247 CHECK_ADD_OVERFLOW_INT64L(fl
->l_start
, fl
->l_len
- 1)) {
250 /* Check if the end marker is negative. */
251 if ((fl
->l_len
< 0) && fl
->l_start
+ fl
->l_len
< 0) {
262 * Description: Initialize the file lock group and the uipc and flist locks
268 * Notes: Called at system startup from bsd_init().
273 /* allocate file lock group attribute and group */
274 file_lck_grp_attr
= lck_grp_attr_alloc_init();
276 file_lck_grp
= lck_grp_alloc_init("file", file_lck_grp_attr
);
278 /* Allocate file lock attribute */
279 file_lck_attr
= lck_attr_alloc_init();
281 uipc_lock
= lck_mtx_alloc_init(file_lck_grp
, file_lck_attr
);
286 proc_dirs_lock_shared(proc_t p
)
288 lck_rw_lock_shared(&p
->p_dirs_lock
);
292 proc_dirs_unlock_shared(proc_t p
)
294 lck_rw_unlock_shared(&p
->p_dirs_lock
);
298 proc_dirs_lock_exclusive(proc_t p
)
300 lck_rw_lock_exclusive(&p
->p_dirs_lock
);
304 proc_dirs_unlock_exclusive(proc_t p
)
306 lck_rw_unlock_exclusive(&p
->p_dirs_lock
);
310 * proc_fdlock, proc_fdlock_spin
312 * Description: Lock to control access to the per process struct fileproc
313 * and struct filedesc
315 * Parameters: p Process to take the lock on
319 * Notes: The lock is initialized in forkproc() and destroyed in
320 * reap_child_process().
323 proc_fdlock(proc_t p
)
325 lck_mtx_lock(&p
->p_fdmlock
);
329 proc_fdlock_spin(proc_t p
)
331 lck_mtx_lock_spin(&p
->p_fdmlock
);
335 proc_fdlock_assert(proc_t p
, int assertflags
)
337 lck_mtx_assert(&p
->p_fdmlock
, assertflags
);
344 * Description: Unlock the lock previously locked by a call to proc_fdlock()
346 * Parameters: p Process to drop the lock on
351 proc_fdunlock(proc_t p
)
353 lck_mtx_unlock(&p
->p_fdmlock
);
358 * System calls on descriptors.
365 * Description: Returns the per process maximum size of the descriptor table
367 * Parameters: p Process being queried
368 * retval Pointer to the call return area
373 * *retval (modified) Size of dtable
376 getdtablesize(proc_t p
, __unused
struct getdtablesize_args
*uap
, int32_t *retval
)
379 *retval
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
387 procfdtbl_reservefd(struct proc
* p
, int fd
)
389 p
->p_fd
->fd_ofiles
[fd
] = NULL
;
390 p
->p_fd
->fd_ofileflags
[fd
] |= UF_RESERVED
;
394 procfdtbl_markclosefd(struct proc
* p
, int fd
)
396 p
->p_fd
->fd_ofileflags
[fd
] |= (UF_RESERVED
| UF_CLOSING
);
400 procfdtbl_releasefd(struct proc
* p
, int fd
, struct fileproc
* fp
)
403 p
->p_fd
->fd_ofiles
[fd
] = fp
;
405 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESERVED
;
406 if ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
) == UF_RESVWAIT
) {
407 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESVWAIT
;
413 procfdtbl_waitfd(struct proc
* p
, int fd
)
415 p
->p_fd
->fd_ofileflags
[fd
] |= UF_RESVWAIT
;
416 msleep(&p
->p_fd
, &p
->p_fdmlock
, PRIBIO
, "ftbl_waitfd", NULL
);
421 procfdtbl_clearfd(struct proc
* p
, int fd
)
425 waiting
= (p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
);
426 p
->p_fd
->fd_ofiles
[fd
] = NULL
;
427 p
->p_fd
->fd_ofileflags
[fd
] = 0;
428 if (waiting
== UF_RESVWAIT
) {
436 * Description: Inline utility function to free an fd in a filedesc
438 * Parameters: fdp Pointer to filedesc fd lies in
440 * reserv fd should be reserved
444 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
448 _fdrelse(struct proc
* p
, int fd
)
450 struct filedesc
*fdp
= p
->p_fd
;
453 if (fd
< fdp
->fd_freefile
) {
454 fdp
->fd_freefile
= fd
;
457 if (fd
> fdp
->fd_lastfile
) {
458 panic("fdrelse: fd_lastfile inconsistent");
461 procfdtbl_clearfd(p
, fd
);
463 while ((nfd
= fdp
->fd_lastfile
) > 0 &&
464 fdp
->fd_ofiles
[nfd
] == NULL
&&
465 !(fdp
->fd_ofileflags
[nfd
] & UF_RESERVED
)) {
466 /* JMM - What about files with lingering EV_VANISHED knotes? */
489 char uio_buf
[UIO_SIZEOF(1)];
490 struct vfs_context context
= *(vfs_context_current());
491 bool wrote_some
= false;
495 error
= fp_lookup(p
, fd
, &fp
, 0);
500 if (fp
->f_type
!= DTYPE_VNODE
&& fp
->f_type
!= DTYPE_PIPE
&& fp
->f_type
!= DTYPE_SOCKET
) {
504 if (rw
== UIO_WRITE
&& !(fp
->f_flag
& FWRITE
)) {
509 if (rw
== UIO_READ
&& !(fp
->f_flag
& FREAD
)) {
514 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
516 if (UIO_SEG_IS_USER_SPACE(segflg
)) {
517 spacetype
= proc_is64bit(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
519 spacetype
= UIO_SYSSPACE
;
522 auio
= uio_createwithbuffer(1, offset
, spacetype
, rw
, &uio_buf
[0], sizeof(uio_buf
));
524 uio_addiov(auio
, base
, len
);
526 if (!(io_flg
& IO_APPEND
)) {
530 if (rw
== UIO_WRITE
) {
531 user_ssize_t orig_resid
= uio_resid(auio
);
532 error
= fo_write(fp
, auio
, flags
, &context
);
533 wrote_some
= uio_resid(auio
) < orig_resid
;
535 error
= fo_read(fp
, auio
, flags
, &context
);
539 *aresid
= uio_resid(auio
);
541 if (uio_resid(auio
) && error
== 0) {
547 fp_drop_written(p
, fd
, fp
);
549 fp_drop(p
, fd
, fp
, 0);
560 * Description: Duplicate a file descriptor.
562 * Parameters: p Process performing the dup
563 * uap->fd The fd to dup
564 * retval Pointer to the call return area
570 * *retval (modified) The new descriptor
573 dup(proc_t p
, struct dup_args
*uap
, int32_t *retval
)
575 struct filedesc
*fdp
= p
->p_fd
;
581 if ((error
= fp_lookup(p
, old
, &fp
, 1))) {
585 if (FP_ISGUARDED(fp
, GUARD_DUP
)) {
586 error
= fp_guard_exception(p
, old
, fp
, kGUARD_EXC_DUP
);
587 (void) fp_drop(p
, old
, fp
, 1);
591 if ((error
= fdalloc(p
, 0, &new))) {
592 fp_drop(p
, old
, fp
, 1);
596 error
= finishdup(p
, fdp
, old
, new, 0, retval
);
597 fp_drop(p
, old
, fp
, 1);
600 if (ENTR_SHOULDTRACE
&& fp
->f_type
== DTYPE_SOCKET
) {
601 KERNEL_ENERGYTRACE(kEnTrActKernSocket
, DBG_FUNC_START
,
602 new, 0, (int64_t)VM_KERNEL_ADDRPERM(fp
->f_data
));
611 * Description: Duplicate a file descriptor to a particular value.
613 * Parameters: p Process performing the dup
614 * uap->from The fd to dup
615 * uap->to The fd to dup it to
616 * retval Pointer to the call return area
622 * *retval (modified) The new descriptor
625 dup2(proc_t p
, struct dup2_args
*uap
, int32_t *retval
)
627 struct filedesc
*fdp
= p
->p_fd
;
628 int old
= uap
->from
, new = uap
->to
;
630 struct fileproc
*fp
, *nfp
;
635 if ((error
= fp_lookup(p
, old
, &fp
, 1))) {
639 if (FP_ISGUARDED(fp
, GUARD_DUP
)) {
640 error
= fp_guard_exception(p
, old
, fp
, kGUARD_EXC_DUP
);
641 (void) fp_drop(p
, old
, fp
, 1);
646 (rlim_t
)new >= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
648 fp_drop(p
, old
, fp
, 1);
653 fp_drop(p
, old
, fp
, 1);
658 if (new < 0 || new >= fdp
->fd_nfiles
) {
659 if ((error
= fdalloc(p
, new, &i
))) {
660 fp_drop(p
, old
, fp
, 1);
670 while ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == UF_RESERVED
) {
671 fp_drop(p
, old
, fp
, 1);
672 procfdtbl_waitfd(p
, new);
674 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
679 if ((fdp
->fd_ofiles
[new] != NULL
) &&
680 ((error
= fp_lookup(p
, new, &nfp
, 1)) == 0)) {
681 fp_drop(p
, old
, fp
, 1);
682 if (FP_ISGUARDED(nfp
, GUARD_CLOSE
)) {
683 error
= fp_guard_exception(p
,
684 new, nfp
, kGUARD_EXC_CLOSE
);
685 (void) fp_drop(p
, new, nfp
, 1);
689 (void)close_internal_locked(p
, new, nfp
, FD_DUP2RESV
);
691 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
693 procfdtbl_clearfd(p
, new);
697 if (fdp
->fd_ofiles
[new] != NULL
) {
698 panic("dup2: no ref on fileproc %d", new);
701 procfdtbl_reservefd(p
, new);
705 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
709 if (fdp
->fd_ofiles
[new] != 0) {
710 panic("dup2: overwriting fd_ofiles with new %d", new);
712 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0) {
713 panic("dup2: unreserved fileflags with new %d", new);
716 error
= finishdup(p
, fdp
, old
, new, 0, retval
);
717 fp_drop(p
, old
, fp
, 1);
727 * Description: The file control system call.
729 * Parameters: p Process performing the fcntl
730 * uap->fd The fd to operate against
731 * uap->cmd The command to perform
732 * uap->arg Pointer to the command argument
733 * retval Pointer to the call return area
736 * !0 Errno (see fcntl_nocancel)
739 * *retval (modified) fcntl return value (if any)
741 * Notes: This system call differs from fcntl_nocancel() in that it
742 * tests for cancellation prior to performing a potentially
743 * blocking operation.
746 fcntl(proc_t p
, struct fcntl_args
*uap
, int32_t *retval
)
748 __pthread_testcancel(1);
749 return fcntl_nocancel(p
, (struct fcntl_nocancel_args
*)uap
, retval
);
756 * Description: A non-cancel-testing file control system call.
758 * Parameters: p Process performing the fcntl
759 * uap->fd The fd to operate against
760 * uap->cmd The command to perform
761 * uap->arg Pointer to the command argument
762 * retval Pointer to the call return area
766 * fp_lookup:EBADF Bad file descriptor
778 * vnode_getwithref:???
786 * vnode_getwithref:???
793 * vnode_getwithref:???
795 * [F_SETSIZE,F_RDADVISE]
798 * vnode_getwithref:???
799 * [F_RDAHEAD,F_NOCACHE]
801 * vnode_getwithref:???
805 * *retval (modified) fcntl return value (if any)
808 fcntl_nocancel(proc_t p
, struct fcntl_nocancel_args
*uap
, int32_t *retval
)
811 struct filedesc
*fdp
= p
->p_fd
;
814 struct vnode
*vp
= NULLVP
; /* for AUDIT_ARG() at end */
815 int i
, tmp
, error
, error2
, flg
= 0;
816 struct flock fl
= {};
817 struct flocktimeout fltimeout
;
818 struct timespec
*timeout
= NULL
;
819 struct vfs_context context
;
827 AUDIT_ARG(fd
, uap
->fd
);
828 AUDIT_ARG(cmd
, uap
->cmd
);
831 if ((error
= fp_lookup(p
, fd
, &fp
, 1))) {
835 context
.vc_thread
= current_thread();
836 context
.vc_ucred
= fp
->f_cred
;
838 is64bit
= proc_is64bit(p
);
843 * Since the arg parameter is defined as a long but may be
844 * either a long or a pointer we must take care to handle
845 * sign extension issues. Our sys call munger will sign
846 * extend a long when we are called from a 32-bit process.
847 * Since we can never have an address greater than 32-bits
848 * from a 32-bit process we lop off the top 32-bits to avoid
849 * getting the wrong address
851 argp
= CAST_USER_ADDR_T((uint32_t)uap
->arg
);
854 pop
= &fdp
->fd_ofileflags
[fd
];
857 error
= mac_file_check_fcntl(proc_ucred(p
), fp
->f_fglob
, uap
->cmd
,
866 case F_DUPFD_CLOEXEC
:
867 if (FP_ISGUARDED(fp
, GUARD_DUP
)) {
868 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_DUP
);
871 newmin
= CAST_DOWN_EXPLICIT(int, uap
->arg
); /* arg is an int, so we won't lose bits */
872 AUDIT_ARG(value32
, newmin
);
873 if ((u_int
)newmin
>= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
874 newmin
>= maxfiles
) {
878 if ((error
= fdalloc(p
, newmin
, &i
))) {
881 error
= finishdup(p
, fdp
, fd
, i
,
882 uap
->cmd
== F_DUPFD_CLOEXEC
? UF_EXCLOSE
: 0, retval
);
886 *retval
= (*pop
& UF_EXCLOSE
)? FD_CLOEXEC
: 0;
891 AUDIT_ARG(value32
, uap
->arg
);
892 if (uap
->arg
& FD_CLOEXEC
) {
895 if (FILEPROC_TYPE(fp
) == FTYPE_GUARDED
) {
896 error
= fp_guard_exception(p
,
897 fd
, fp
, kGUARD_EXC_NOCLOEXEC
);
906 *retval
= OFLAGS(fp
->f_flag
);
911 fp
->f_flag
&= ~FCNTLFLAGS
;
912 tmp
= CAST_DOWN_EXPLICIT(int, uap
->arg
); /* arg is an int, so we won't lose bits */
913 AUDIT_ARG(value32
, tmp
);
914 fp
->f_flag
|= FFLAGS(tmp
) & FCNTLFLAGS
;
915 tmp
= fp
->f_flag
& FNONBLOCK
;
916 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
920 tmp
= fp
->f_flag
& FASYNC
;
921 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, &context
);
925 fp
->f_flag
&= ~FNONBLOCK
;
927 (void)fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
931 if (fp
->f_type
== DTYPE_SOCKET
) {
932 *retval
= ((struct socket
*)fp
->f_data
)->so_pgid
;
936 error
= fo_ioctl(fp
, TIOCGPGRP
, (caddr_t
)retval
, &context
);
941 tmp
= CAST_DOWN_EXPLICIT(pid_t
, uap
->arg
); /* arg is an int, so we won't lose bits */
942 AUDIT_ARG(value32
, tmp
);
943 if (fp
->f_type
== DTYPE_SOCKET
) {
944 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
948 if (fp
->f_type
== DTYPE_PIPE
) {
949 error
= fo_ioctl(fp
, TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
956 proc_t p1
= proc_find(tmp
);
961 tmp
= (int)p1
->p_pgrpid
;
964 error
= fo_ioctl(fp
, TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
968 tmp
= CAST_DOWN_EXPLICIT(int, uap
->arg
);
969 if (fp
->f_type
== DTYPE_SOCKET
) {
971 error
= sock_setsockopt((struct socket
*)fp
->f_data
,
972 SOL_SOCKET
, SO_NOSIGPIPE
, &tmp
, sizeof(tmp
));
977 struct fileglob
*fg
= fp
->f_fglob
;
979 lck_mtx_lock_spin(&fg
->fg_lock
);
981 fg
->fg_lflags
|= FG_NOSIGPIPE
;
983 fg
->fg_lflags
&= ~FG_NOSIGPIPE
;
985 lck_mtx_unlock(&fg
->fg_lock
);
991 if (fp
->f_type
== DTYPE_SOCKET
) {
993 int retsize
= sizeof(*retval
);
994 error
= sock_getsockopt((struct socket
*)fp
->f_data
,
995 SOL_SOCKET
, SO_NOSIGPIPE
, retval
, &retsize
);
1000 *retval
= (fp
->f_fglob
->fg_lflags
& FG_NOSIGPIPE
) ?
1008 * If this is the only reference to this fglob in the process
1009 * and it's already marked as close-on-fork then mark it as
1010 * (immutably) "confined" i.e. any fd that points to it will
1011 * forever be close-on-fork, and attempts to use an IPC
1012 * mechanism to move the descriptor elsewhere will fail.
1014 if (CAST_DOWN_EXPLICIT(int, uap
->arg
)) {
1015 struct fileglob
*fg
= fp
->f_fglob
;
1017 lck_mtx_lock_spin(&fg
->fg_lock
);
1018 if (fg
->fg_lflags
& FG_CONFINED
) {
1020 } else if (1 != fg
->fg_count
) {
1021 error
= EAGAIN
; /* go close the dup .. */
1022 } else if (UF_FORKCLOSE
== (*pop
& UF_FORKCLOSE
)) {
1023 fg
->fg_lflags
|= FG_CONFINED
;
1026 error
= EBADF
; /* open without O_CLOFORK? */
1028 lck_mtx_unlock(&fg
->fg_lock
);
1031 * Other subsystems may have built on the immutability
1032 * of FG_CONFINED; clearing it may be tricky.
1034 error
= EPERM
; /* immutable */
1039 *retval
= (fp
->f_fglob
->fg_lflags
& FG_CONFINED
) ? 1 : 0;
1043 case F_SETLKWTIMEOUT
:
1045 case F_OFD_SETLKWTIMEOUT
:
1048 /* Fall into F_SETLK */
1052 if (fp
->f_type
!= DTYPE_VNODE
) {
1056 vp
= (struct vnode
*)fp
->f_data
;
1059 offset
= fp
->f_offset
;
1062 /* Copy in the lock structure */
1063 if (F_SETLKWTIMEOUT
== uap
->cmd
||
1064 F_OFD_SETLKWTIMEOUT
== uap
->cmd
) {
1065 error
= copyin(argp
, (caddr_t
) &fltimeout
, sizeof(fltimeout
));
1070 timeout
= &fltimeout
.timeout
;
1072 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
1078 /* Check starting byte and ending byte for EOVERFLOW in SEEK_CUR */
1079 /* and ending byte for EOVERFLOW in SEEK_SET */
1080 error
= check_file_seek_range(&fl
, offset
);
1085 if ((error
= vnode_getwithref(vp
))) {
1088 if (fl
.l_whence
== SEEK_CUR
) {
1089 fl
.l_start
+= offset
;
1093 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
1096 (void)vnode_put(vp
);
1103 case F_OFD_SETLKWTIMEOUT
:
1105 switch (fl
.l_type
) {
1107 if ((fflag
& FREAD
) == 0) {
1111 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1112 F_SETLK
, &fl
, flg
, &context
, timeout
);
1115 if ((fflag
& FWRITE
) == 0) {
1119 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1120 F_SETLK
, &fl
, flg
, &context
, timeout
);
1123 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1124 F_UNLCK
, &fl
, F_OFD_LOCK
, &context
,
1132 (F_RDLCK
== fl
.l_type
|| F_WRLCK
== fl
.l_type
)) {
1133 struct fileglob
*fg
= fp
->f_fglob
;
1136 * arrange F_UNLCK on last close (once
1137 * set, FG_HAS_OFDLOCK is immutable)
1139 if ((fg
->fg_lflags
& FG_HAS_OFDLOCK
) == 0) {
1140 lck_mtx_lock_spin(&fg
->fg_lock
);
1141 fg
->fg_lflags
|= FG_HAS_OFDLOCK
;
1142 lck_mtx_unlock(&fg
->fg_lock
);
1148 switch (fl
.l_type
) {
1150 if ((fflag
& FREAD
) == 0) {
1154 // XXX UInt32 unsafe for LP64 kernel
1155 OSBitOrAtomic(P_LADVLOCK
, &p
->p_ladvflag
);
1156 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1157 F_SETLK
, &fl
, flg
, &context
, timeout
);
1160 if ((fflag
& FWRITE
) == 0) {
1164 // XXX UInt32 unsafe for LP64 kernel
1165 OSBitOrAtomic(P_LADVLOCK
, &p
->p_ladvflag
);
1166 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1167 F_SETLK
, &fl
, flg
, &context
, timeout
);
1170 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1171 F_UNLCK
, &fl
, F_POSIX
, &context
, timeout
);
1179 (void) vnode_put(vp
);
1185 case F_OFD_GETLKPID
:
1186 if (fp
->f_type
!= DTYPE_VNODE
) {
1190 vp
= (struct vnode
*)fp
->f_data
;
1192 offset
= fp
->f_offset
;
1195 /* Copy in the lock structure */
1196 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
1201 /* Check starting byte and ending byte for EOVERFLOW in SEEK_CUR */
1202 /* and ending byte for EOVERFLOW in SEEK_SET */
1203 error
= check_file_seek_range(&fl
, offset
);
1208 if ((fl
.l_whence
== SEEK_SET
) && (fl
.l_start
< 0)) {
1213 switch (fl
.l_type
) {
1223 switch (fl
.l_whence
) {
1233 if ((error
= vnode_getwithref(vp
)) == 0) {
1234 if (fl
.l_whence
== SEEK_CUR
) {
1235 fl
.l_start
+= offset
;
1239 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
1245 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1246 F_GETLK
, &fl
, F_OFD_LOCK
, &context
, NULL
);
1248 case F_OFD_GETLKPID
:
1249 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1250 F_GETLKPID
, &fl
, F_OFD_LOCK
, &context
, NULL
);
1253 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1254 uap
->cmd
, &fl
, F_POSIX
, &context
, NULL
);
1258 (void)vnode_put(vp
);
1261 error
= copyout((caddr_t
)&fl
, argp
, sizeof(fl
));
1266 case F_PREALLOCATE
: {
1267 fstore_t alloc_struct
; /* structure for allocate command */
1268 u_int32_t alloc_flags
= 0;
1270 if (fp
->f_type
!= DTYPE_VNODE
) {
1275 vp
= (struct vnode
*)fp
->f_data
;
1278 /* make sure that we have write permission */
1279 if ((fp
->f_flag
& FWRITE
) == 0) {
1284 error
= copyin(argp
, (caddr_t
)&alloc_struct
, sizeof(alloc_struct
));
1289 /* now set the space allocated to 0 */
1290 alloc_struct
.fst_bytesalloc
= 0;
1293 * Do some simple parameter checking
1296 /* set up the flags */
1298 alloc_flags
|= PREALLOCATE
;
1300 if (alloc_struct
.fst_flags
& F_ALLOCATECONTIG
) {
1301 alloc_flags
|= ALLOCATECONTIG
;
1304 if (alloc_struct
.fst_flags
& F_ALLOCATEALL
) {
1305 alloc_flags
|= ALLOCATEALL
;
1309 * Do any position mode specific stuff. The only
1310 * position mode supported now is PEOFPOSMODE
1313 switch (alloc_struct
.fst_posmode
) {
1315 if (alloc_struct
.fst_offset
!= 0) {
1320 alloc_flags
|= ALLOCATEFROMPEOF
;
1324 if (alloc_struct
.fst_offset
<= 0) {
1329 alloc_flags
|= ALLOCATEFROMVOL
;
1337 if ((error
= vnode_getwithref(vp
)) == 0) {
1339 * call allocate to get the space
1341 error
= VNOP_ALLOCATE(vp
, alloc_struct
.fst_length
, alloc_flags
,
1342 &alloc_struct
.fst_bytesalloc
, alloc_struct
.fst_offset
,
1344 (void)vnode_put(vp
);
1346 error2
= copyout((caddr_t
)&alloc_struct
, argp
, sizeof(alloc_struct
));
1357 if (fp
->f_type
!= DTYPE_VNODE
) {
1362 vp
= (struct vnode
*)fp
->f_data
;
1365 /* need write permissions */
1366 if ((fp
->f_flag
& FWRITE
) == 0) {
1371 if ((error
= copyin(argp
, (caddr_t
)&args
, sizeof(args
)))) {
1375 if ((error
= vnode_getwithref(vp
))) {
1380 if ((error
= mac_vnode_check_write(&context
, fp
->f_fglob
->fg_cred
, vp
))) {
1381 (void)vnode_put(vp
);
1386 error
= VNOP_IOCTL(vp
, F_PUNCHHOLE
, (caddr_t
)&args
, 0, &context
);
1387 (void)vnode_put(vp
);
1391 case F_TRIM_ACTIVE_FILE
: {
1392 ftrimactivefile_t args
;
1394 if (priv_check_cred(kauth_cred_get(), PRIV_TRIM_ACTIVE_FILE
, 0)) {
1399 if (fp
->f_type
!= DTYPE_VNODE
) {
1404 vp
= (struct vnode
*)fp
->f_data
;
1407 /* need write permissions */
1408 if ((fp
->f_flag
& FWRITE
) == 0) {
1413 if ((error
= copyin(argp
, (caddr_t
)&args
, sizeof(args
)))) {
1417 if ((error
= vnode_getwithref(vp
))) {
1421 error
= VNOP_IOCTL(vp
, F_TRIM_ACTIVE_FILE
, (caddr_t
)&args
, 0, &context
);
1422 (void)vnode_put(vp
);
1426 case F_SPECULATIVE_READ
: {
1429 if (fp
->f_type
!= DTYPE_VNODE
) {
1434 vp
= (struct vnode
*)fp
->f_data
;
1437 if ((error
= copyin(argp
, (caddr_t
)&args
, sizeof(args
)))) {
1441 /* Discard invalid offsets or lengths */
1442 if ((args
.fsr_offset
< 0) || (args
.fsr_length
< 0)) {
1448 * Round the file offset down to a page-size boundary (or to 0).
1449 * The filesystem will need to round the length up to the end of the page boundary
1450 * or to the EOF of the file.
1452 uint64_t foff
= (((uint64_t)args
.fsr_offset
) & ~((uint64_t)PAGE_MASK
));
1453 uint64_t foff_delta
= args
.fsr_offset
- foff
;
1454 args
.fsr_offset
= (off_t
) foff
;
1457 * Now add in the delta to the supplied length. Since we may have adjusted the
1458 * offset, increase it by the amount that we adjusted.
1460 args
.fsr_length
+= foff_delta
;
1462 if ((error
= vnode_getwithref(vp
))) {
1465 error
= VNOP_IOCTL(vp
, F_SPECULATIVE_READ
, (caddr_t
)&args
, 0, &context
);
1466 (void)vnode_put(vp
);
1471 if (fp
->f_type
!= DTYPE_VNODE
) {
1475 vp
= (struct vnode
*)fp
->f_data
;
1478 error
= copyin(argp
, (caddr_t
)&offset
, sizeof(off_t
));
1482 AUDIT_ARG(value64
, offset
);
1484 error
= vnode_getwithref(vp
);
1490 error
= mac_vnode_check_truncate(&context
,
1491 fp
->f_fglob
->fg_cred
, vp
);
1493 (void)vnode_put(vp
);
1498 * Make sure that we are root. Growing a file
1499 * without zero filling the data is a security hole
1500 * root would have access anyway so we'll allow it
1502 if (!kauth_cred_issuser(kauth_cred_get())) {
1508 error
= vnode_setsize(vp
, offset
, IO_NOZEROFILL
,
1513 mac_vnode_notify_truncate(&context
, fp
->f_fglob
->fg_cred
, vp
);
1518 (void)vnode_put(vp
);
1522 if (fp
->f_type
!= DTYPE_VNODE
) {
1527 fp
->f_fglob
->fg_flag
&= ~FNORDAHEAD
;
1529 fp
->f_fglob
->fg_flag
|= FNORDAHEAD
;
1535 if (fp
->f_type
!= DTYPE_VNODE
) {
1540 fp
->f_fglob
->fg_flag
|= FNOCACHE
;
1542 fp
->f_fglob
->fg_flag
&= ~FNOCACHE
;
1548 if (fp
->f_type
!= DTYPE_VNODE
) {
1553 fp
->f_fglob
->fg_flag
|= FNODIRECT
;
1555 fp
->f_fglob
->fg_flag
&= ~FNODIRECT
;
1560 case F_SINGLE_WRITER
:
1561 if (fp
->f_type
!= DTYPE_VNODE
) {
1566 fp
->f_fglob
->fg_flag
|= FSINGLE_WRITER
;
1568 fp
->f_fglob
->fg_flag
&= ~FSINGLE_WRITER
;
1573 case F_GLOBAL_NOCACHE
:
1574 if (fp
->f_type
!= DTYPE_VNODE
) {
1578 vp
= (struct vnode
*)fp
->f_data
;
1581 if ((error
= vnode_getwithref(vp
)) == 0) {
1582 *retval
= vnode_isnocache(vp
);
1585 vnode_setnocache(vp
);
1587 vnode_clearnocache(vp
);
1590 (void)vnode_put(vp
);
1594 case F_CHECK_OPENEVT
:
1595 if (fp
->f_type
!= DTYPE_VNODE
) {
1599 vp
= (struct vnode
*)fp
->f_data
;
1602 if ((error
= vnode_getwithref(vp
)) == 0) {
1603 *retval
= vnode_is_openevt(vp
);
1606 vnode_set_openevt(vp
);
1608 vnode_clear_openevt(vp
);
1611 (void)vnode_put(vp
);
1616 struct radvisory ra_struct
;
1618 if (fp
->f_type
!= DTYPE_VNODE
) {
1622 vp
= (struct vnode
*)fp
->f_data
;
1625 if ((error
= copyin(argp
, (caddr_t
)&ra_struct
, sizeof(ra_struct
)))) {
1628 if ((error
= vnode_getwithref(vp
)) == 0) {
1629 error
= VNOP_IOCTL(vp
, F_RDADVISE
, (caddr_t
)&ra_struct
, 0, &context
);
1631 (void)vnode_put(vp
);
1638 if (fp
->f_type
!= DTYPE_VNODE
) {
1642 vp
= (struct vnode
*)fp
->f_data
;
1645 if ((error
= vnode_getwithref(vp
)) == 0) {
1646 error
= VNOP_FSYNC(vp
, MNT_NOWAIT
, &context
);
1648 (void)vnode_put(vp
);
1653 case F_LOG2PHYS_EXT
: {
1654 struct log2phys l2p_struct
= {}; /* structure for allocate command */
1657 off_t file_offset
= 0;
1661 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1662 error
= copyin(argp
, (caddr_t
)&l2p_struct
, sizeof(l2p_struct
));
1666 file_offset
= l2p_struct
.l2p_devoffset
;
1668 file_offset
= fp
->f_offset
;
1670 if (fp
->f_type
!= DTYPE_VNODE
) {
1674 vp
= (struct vnode
*)fp
->f_data
;
1676 if ((error
= vnode_getwithref(vp
))) {
1679 error
= VNOP_OFFTOBLK(vp
, file_offset
, &lbn
);
1681 (void)vnode_put(vp
);
1684 error
= VNOP_BLKTOOFF(vp
, lbn
, &offset
);
1686 (void)vnode_put(vp
);
1689 devBlockSize
= vfs_devblocksize(vnode_mount(vp
));
1690 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1691 if (l2p_struct
.l2p_contigbytes
< 0) {
1697 a_size
= MIN((uint64_t)l2p_struct
.l2p_contigbytes
, SIZE_MAX
);
1699 a_size
= devBlockSize
;
1702 error
= VNOP_BLOCKMAP(vp
, offset
, a_size
, &bn
, &run
, NULL
, 0, &context
);
1704 (void)vnode_put(vp
);
1707 l2p_struct
.l2p_flags
= 0; /* for now */
1708 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1709 l2p_struct
.l2p_contigbytes
= run
- (file_offset
- offset
);
1711 l2p_struct
.l2p_contigbytes
= 0; /* for now */
1715 * The block number being -1 suggests that the file offset is not backed
1716 * by any real blocks on-disk. As a result, just let it be passed back up wholesale.
1719 /* Don't multiply it by the block size */
1720 l2p_struct
.l2p_devoffset
= bn
;
1722 l2p_struct
.l2p_devoffset
= bn
* devBlockSize
;
1723 l2p_struct
.l2p_devoffset
+= file_offset
- offset
;
1725 error
= copyout((caddr_t
)&l2p_struct
, argp
, sizeof(l2p_struct
));
1730 case F_GETPATH_NOFIRMLINK
: {
1734 if (fp
->f_type
!= DTYPE_VNODE
) {
1738 vp
= (struct vnode
*)fp
->f_data
;
1741 pathlen
= MAXPATHLEN
;
1742 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
1743 if (pathbufp
== NULL
) {
1747 if ((error
= vnode_getwithref(vp
)) == 0) {
1748 if (uap
->cmd
== F_GETPATH_NOFIRMLINK
) {
1749 error
= vn_getpath_ext(vp
, NULL
, pathbufp
, &pathlen
, VN_GETPATH_NO_FIRMLINK
);
1751 error
= vn_getpath(vp
, pathbufp
, &pathlen
);
1753 (void)vnode_put(vp
);
1756 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
1759 FREE(pathbufp
, M_TEMP
);
1763 case F_PATHPKG_CHECK
: {
1767 if (fp
->f_type
!= DTYPE_VNODE
) {
1771 vp
= (struct vnode
*)fp
->f_data
;
1774 pathlen
= MAXPATHLEN
;
1775 pathbufp
= kalloc(MAXPATHLEN
);
1777 if ((error
= copyinstr(argp
, pathbufp
, MAXPATHLEN
, &pathlen
)) == 0) {
1778 if ((error
= vnode_getwithref(vp
)) == 0) {
1779 AUDIT_ARG(text
, pathbufp
);
1780 error
= vn_path_package_check(vp
, pathbufp
, pathlen
, retval
);
1782 (void)vnode_put(vp
);
1785 kfree(pathbufp
, MAXPATHLEN
);
1789 case F_CHKCLEAN
: // used by regression tests to see if all dirty pages got cleaned by fsync()
1790 case F_FULLFSYNC
: // fsync + flush the journal + DKIOCSYNCHRONIZE
1791 case F_BARRIERFSYNC
: // fsync + barrier
1792 case F_FREEZE_FS
: // freeze all other fs operations for the fs of this fd
1793 case F_THAW_FS
: { // thaw all frozen fs operations for the fs of this fd
1794 if (fp
->f_type
!= DTYPE_VNODE
) {
1798 vp
= (struct vnode
*)fp
->f_data
;
1801 if ((error
= vnode_getwithref(vp
)) == 0) {
1802 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
1804 (void)vnode_put(vp
);
1810 * SPI (private) for opening a file starting from a dir fd
1813 struct user_fopenfrom fopen
;
1814 struct vnode_attr va
;
1815 struct nameidata nd
;
1818 /* Check if this isn't a valid file descriptor */
1819 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1820 (fp
->f_flag
& FREAD
) == 0) {
1824 vp
= (struct vnode
*)fp
->f_data
;
1827 if (vnode_getwithref(vp
)) {
1832 /* Only valid for directories */
1833 if (vp
->v_type
!= VDIR
) {
1839 /* Get flags, mode and pathname arguments. */
1840 if (IS_64BIT_PROCESS(p
)) {
1841 error
= copyin(argp
, &fopen
, sizeof(fopen
));
1843 struct user32_fopenfrom fopen32
;
1845 error
= copyin(argp
, &fopen32
, sizeof(fopen32
));
1846 fopen
.o_flags
= fopen32
.o_flags
;
1847 fopen
.o_mode
= fopen32
.o_mode
;
1848 fopen
.o_pathname
= CAST_USER_ADDR_T(fopen32
.o_pathname
);
1854 AUDIT_ARG(fflags
, fopen
.o_flags
);
1855 AUDIT_ARG(mode
, fopen
.o_mode
);
1857 /* Mask off all but regular access permissions */
1858 cmode
= ((fopen
.o_mode
& ~fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
1859 VATTR_SET(&va
, va_mode
, cmode
& ACCESSPERMS
);
1861 /* Start the lookup relative to the file descriptor's vnode. */
1862 NDINIT(&nd
, LOOKUP
, OP_OPEN
, USEDVP
| FOLLOW
| AUDITVNPATH1
, UIO_USERSPACE
,
1863 fopen
.o_pathname
, &context
);
1866 error
= open1(&context
, &nd
, fopen
.o_flags
, &va
,
1867 fileproc_alloc_init
, NULL
, retval
);
1873 * SPI (private) for unlinking a file starting from a dir fd
1875 case F_UNLINKFROM
: {
1876 user_addr_t pathname
;
1878 /* Check if this isn't a valid file descriptor */
1879 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1880 (fp
->f_flag
& FREAD
) == 0) {
1884 vp
= (struct vnode
*)fp
->f_data
;
1887 if (vnode_getwithref(vp
)) {
1892 /* Only valid for directories */
1893 if (vp
->v_type
!= VDIR
) {
1899 /* Get flags, mode and pathname arguments. */
1900 if (IS_64BIT_PROCESS(p
)) {
1901 pathname
= (user_addr_t
)argp
;
1903 pathname
= CAST_USER_ADDR_T(argp
);
1906 /* Start the lookup relative to the file descriptor's vnode. */
1907 error
= unlink1(&context
, vp
, pathname
, UIO_USERSPACE
, 0);
1915 case F_ADDFILESIGS_FOR_DYLD_SIM
:
1916 case F_ADDFILESIGS_RETURN
:
1918 struct cs_blob
*blob
= NULL
;
1919 struct user_fsignatures fs
;
1921 vm_offset_t kernel_blob_addr
;
1922 vm_size_t kernel_blob_size
;
1923 int blob_add_flags
= 0;
1925 if (fp
->f_type
!= DTYPE_VNODE
) {
1929 vp
= (struct vnode
*)fp
->f_data
;
1932 if (uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
1933 blob_add_flags
|= MAC_VNODE_CHECK_DYLD_SIM
;
1934 if ((p
->p_csflags
& CS_KILL
) == 0) {
1936 p
->p_csflags
|= CS_KILL
;
1941 error
= vnode_getwithref(vp
);
1946 if (IS_64BIT_PROCESS(p
)) {
1947 error
= copyin(argp
, &fs
, sizeof(fs
));
1949 struct user32_fsignatures fs32
;
1951 error
= copyin(argp
, &fs32
, sizeof(fs32
));
1952 fs
.fs_file_start
= fs32
.fs_file_start
;
1953 fs
.fs_blob_start
= CAST_USER_ADDR_T(fs32
.fs_blob_start
);
1954 fs
.fs_blob_size
= fs32
.fs_blob_size
;
1963 * First check if we have something loaded a this offset
1965 blob
= ubc_cs_blob_get(vp
, CPU_TYPE_ANY
, fs
.fs_file_start
);
1967 /* If this is for dyld_sim revalidate the blob */
1968 if (uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
1969 error
= ubc_cs_blob_revalidate(vp
, blob
, NULL
, blob_add_flags
);
1972 if (error
!= EAGAIN
) {
1982 * An arbitrary limit, to prevent someone from mapping in a 20GB blob. This should cover
1983 * our use cases for the immediate future, but note that at the time of this commit, some
1984 * platforms are nearing 2MB blob sizes (with a prior soft limit of 2.5MB).
1986 * We should consider how we can manage this more effectively; the above means that some
1987 * platforms are using megabytes of memory for signing data; it merely hasn't crossed the
1988 * threshold considered ridiculous at the time of this change.
1990 #define CS_MAX_BLOB_SIZE (40ULL * 1024ULL * 1024ULL)
1991 if (fs
.fs_blob_size
> CS_MAX_BLOB_SIZE
) {
1997 kernel_blob_size
= CAST_DOWN(vm_size_t
, fs
.fs_blob_size
);
1998 kr
= ubc_cs_blob_allocate(&kernel_blob_addr
, &kernel_blob_size
);
1999 if (kr
!= KERN_SUCCESS
|| kernel_blob_size
< fs
.fs_blob_size
) {
2005 if (uap
->cmd
== F_ADDSIGS
) {
2006 error
= copyin(fs
.fs_blob_start
,
2007 (void *) kernel_blob_addr
,
2009 } else { /* F_ADDFILESIGS || F_ADDFILESIGS_RETURN || F_ADDFILESIGS_FOR_DYLD_SIM */
2012 error
= vn_rdwr(UIO_READ
,
2014 (caddr_t
) kernel_blob_addr
,
2016 fs
.fs_file_start
+ fs
.fs_blob_start
,
2022 if ((error
== 0) && resid
) {
2023 /* kernel_blob_size rounded to a page size, but signature may be at end of file */
2024 memset((void *)(kernel_blob_addr
+ (kernel_blob_size
- resid
)), 0x0, resid
);
2029 ubc_cs_blob_deallocate(kernel_blob_addr
,
2036 error
= ubc_cs_blob_add(vp
,
2037 CPU_TYPE_ANY
, /* not for a specific architecture */
2045 /* ubc_blob_add() has consumed "kernel_blob_addr" if it is zeroed */
2047 if (kernel_blob_addr
) {
2048 ubc_cs_blob_deallocate(kernel_blob_addr
,
2054 #if CHECK_CS_VALIDATION_BITMAP
2055 ubc_cs_validation_bitmap_allocate( vp
);
2060 if (uap
->cmd
== F_ADDFILESIGS_RETURN
|| uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
2062 * The first element of the structure is a
2063 * off_t that happen to have the same size for
2064 * all archs. Lets overwrite that.
2066 off_t end_offset
= 0;
2068 end_offset
= blob
->csb_end_offset
;
2070 error
= copyout(&end_offset
, argp
, sizeof(end_offset
));
2073 (void) vnode_put(vp
);
2082 struct fileglob
*fg
;
2085 if (fp
->f_type
!= DTYPE_VNODE
) {
2092 if (IS_64BIT_PROCESS(p
)) {
2093 error
= copyin(argp
, &lv
, sizeof(lv
));
2095 struct user32_fchecklv lv32
= {};
2097 error
= copyin(argp
, &lv32
, sizeof(lv32
));
2098 lv
.lv_file_start
= lv32
.lv_file_start
;
2099 lv
.lv_error_message
= (void *)(uintptr_t)lv32
.lv_error_message
;
2100 lv
.lv_error_message_size
= lv32
.lv_error_message_size
;
2107 error
= mac_file_check_library_validation(p
, fg
, lv
.lv_file_start
,
2108 (user_long_t
)lv
.lv_error_message
, lv
.lv_error_message_size
);
2114 case F_GETPROTECTIONCLASS
: {
2115 if (fp
->f_type
!= DTYPE_VNODE
) {
2119 vp
= (struct vnode
*)fp
->f_data
;
2123 if (vnode_getwithref(vp
)) {
2128 struct vnode_attr va
;
2131 VATTR_WANTED(&va
, va_dataprotect_class
);
2132 error
= VNOP_GETATTR(vp
, &va
, &context
);
2134 if (VATTR_IS_SUPPORTED(&va
, va_dataprotect_class
)) {
2135 *retval
= va
.va_dataprotect_class
;
2145 case F_SETPROTECTIONCLASS
: {
2146 /* tmp must be a valid PROTECTION_CLASS_* */
2147 tmp
= CAST_DOWN_EXPLICIT(uint32_t, uap
->arg
);
2149 if (fp
->f_type
!= DTYPE_VNODE
) {
2153 vp
= (struct vnode
*)fp
->f_data
;
2157 if (vnode_getwithref(vp
)) {
2162 /* Only go forward if you have write access */
2163 vfs_context_t ctx
= vfs_context_current();
2164 if (vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2170 struct vnode_attr va
;
2173 VATTR_SET(&va
, va_dataprotect_class
, tmp
);
2175 error
= VNOP_SETATTR(vp
, &va
, ctx
);
2181 case F_TRANSCODEKEY
: {
2182 if (fp
->f_type
!= DTYPE_VNODE
) {
2187 vp
= (struct vnode
*)fp
->f_data
;
2190 if (vnode_getwithref(vp
)) {
2196 .len
= CP_MAX_WRAPPEDKEYSIZE
,
2199 MALLOC(k
.key
, char *, k
.len
, M_TEMP
, M_WAITOK
| M_ZERO
);
2201 error
= VNOP_IOCTL(vp
, F_TRANSCODEKEY
, (caddr_t
)&k
, 1, &context
);
2206 error
= copyout(k
.key
, argp
, k
.len
);
2210 FREE(k
.key
, M_TEMP
);
2215 case F_GETPROTECTIONLEVEL
: {
2216 if (fp
->f_type
!= DTYPE_VNODE
) {
2221 vp
= (struct vnode
*) fp
->f_data
;
2224 if (vnode_getwithref(vp
)) {
2229 error
= VNOP_IOCTL(vp
, F_GETPROTECTIONLEVEL
, (caddr_t
)retval
, 0, &context
);
2235 case F_GETDEFAULTPROTLEVEL
: {
2236 if (fp
->f_type
!= DTYPE_VNODE
) {
2241 vp
= (struct vnode
*) fp
->f_data
;
2244 if (vnode_getwithref(vp
)) {
2250 * if cp_get_major_vers fails, error will be set to proper errno
2251 * and cp_version will still be 0.
2254 error
= VNOP_IOCTL(vp
, F_GETDEFAULTPROTLEVEL
, (caddr_t
)retval
, 0, &context
);
2260 #endif /* CONFIG_PROTECT */
2262 case F_MOVEDATAEXTENTS
: {
2263 struct fileproc
*fp2
= NULL
;
2264 struct vnode
*src_vp
= NULLVP
;
2265 struct vnode
*dst_vp
= NULLVP
;
2266 /* We need to grab the 2nd FD out of the argments before moving on. */
2267 int fd2
= CAST_DOWN_EXPLICIT(int32_t, uap
->arg
);
2269 error
= priv_check_cred(kauth_cred_get(), PRIV_VFS_MOVE_DATA_EXTENTS
, 0);
2274 if (fp
->f_type
!= DTYPE_VNODE
) {
2280 * For now, special case HFS+ and APFS only, since this
2283 src_vp
= (struct vnode
*)fp
->f_data
;
2284 if (src_vp
->v_tag
!= VT_HFS
&& src_vp
->v_tag
!= VT_APFS
) {
2290 * Get the references before we start acquiring iocounts on the vnodes,
2291 * while we still hold the proc fd lock
2293 if ((error
= fp_lookup(p
, fd2
, &fp2
, 1))) {
2297 if (fp2
->f_type
!= DTYPE_VNODE
) {
2298 fp_drop(p
, fd2
, fp2
, 1);
2302 dst_vp
= (struct vnode
*)fp2
->f_data
;
2303 if (dst_vp
->v_tag
!= VT_HFS
&& dst_vp
->v_tag
!= VT_APFS
) {
2304 fp_drop(p
, fd2
, fp2
, 1);
2310 /* Re-do MAC checks against the new FD, pass in a fake argument */
2311 error
= mac_file_check_fcntl(proc_ucred(p
), fp2
->f_fglob
, uap
->cmd
, 0);
2313 fp_drop(p
, fd2
, fp2
, 1);
2317 /* Audit the 2nd FD */
2322 if (vnode_getwithref(src_vp
)) {
2323 fp_drop(p
, fd2
, fp2
, 0);
2327 if (vnode_getwithref(dst_vp
)) {
2329 fp_drop(p
, fd2
, fp2
, 0);
2335 * Basic asserts; validate they are not the same and that
2336 * both live on the same filesystem.
2338 if (dst_vp
== src_vp
) {
2341 fp_drop(p
, fd2
, fp2
, 0);
2346 if (dst_vp
->v_mount
!= src_vp
->v_mount
) {
2349 fp_drop(p
, fd2
, fp2
, 0);
2354 /* Now we have a legit pair of FDs. Go to work */
2356 /* Now check for write access to the target files */
2357 if (vnode_authorize(src_vp
, NULLVP
,
2358 (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), &context
) != 0) {
2361 fp_drop(p
, fd2
, fp2
, 0);
2366 if (vnode_authorize(dst_vp
, NULLVP
,
2367 (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), &context
) != 0) {
2370 fp_drop(p
, fd2
, fp2
, 0);
2375 /* Verify that both vps point to files and not directories */
2376 if (!vnode_isreg(src_vp
) || !vnode_isreg(dst_vp
)) {
2380 fp_drop(p
, fd2
, fp2
, 0);
2385 * The exchangedata syscall handler passes in 0 for the flags to VNOP_EXCHANGE.
2386 * We'll pass in our special bit indicating that the new behavior is expected
2389 error
= VNOP_EXCHANGE(src_vp
, dst_vp
, FSOPT_EXCHANGE_DATA_ONLY
, &context
);
2393 fp_drop(p
, fd2
, fp2
, 0);
2398 * SPI for making a file compressed.
2400 case F_MAKECOMPRESSED
: {
2401 uint32_t gcounter
= CAST_DOWN_EXPLICIT(uint32_t, uap
->arg
);
2403 if (fp
->f_type
!= DTYPE_VNODE
) {
2408 vp
= (struct vnode
*) fp
->f_data
;
2412 if (vnode_getwithref(vp
)) {
2418 if ((vnode_isreg(vp
) == 0) && (vnode_islnk(vp
) == 0)) {
2424 /* invoke ioctl to pass off to FS */
2425 /* Only go forward if you have write access */
2426 vfs_context_t ctx
= vfs_context_current();
2427 if (vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2433 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)&gcounter
, 0, &context
);
2440 * SPI (private) for indicating to a filesystem that subsequent writes to
2441 * the open FD will written to the Fastflow.
2443 case F_SET_GREEDY_MODE
:
2444 /* intentionally drop through to the same handler as F_SETSTATIC.
2445 * both fcntls should pass the argument and their selector into VNOP_IOCTL.
2449 * SPI (private) for indicating to a filesystem that subsequent writes to
2450 * the open FD will represent static content.
2452 case F_SETSTATICCONTENT
: {
2453 caddr_t ioctl_arg
= NULL
;
2456 ioctl_arg
= (caddr_t
) 1;
2459 if (fp
->f_type
!= DTYPE_VNODE
) {
2463 vp
= (struct vnode
*)fp
->f_data
;
2466 error
= vnode_getwithref(vp
);
2472 /* Only go forward if you have write access */
2473 vfs_context_t ctx
= vfs_context_current();
2474 if (vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2480 error
= VNOP_IOCTL(vp
, uap
->cmd
, ioctl_arg
, 0, &context
);
2481 (void)vnode_put(vp
);
2487 * SPI (private) for indicating to the lower level storage driver that the
2488 * subsequent writes should be of a particular IO type (burst, greedy, static),
2489 * or other flavors that may be necessary.
2496 /* extract 32 bits of flags from userland */
2497 param_ptr
= (caddr_t
) uap
->arg
;
2498 param
= (uint32_t) param_ptr
;
2500 /* If no argument is specified, error out */
2506 * Validate the different types of flags that can be specified:
2507 * all of them are mutually exclusive for now.
2510 case F_IOTYPE_ISOCHRONOUS
:
2519 if (fp
->f_type
!= DTYPE_VNODE
) {
2523 vp
= (struct vnode
*)fp
->f_data
;
2526 error
= vnode_getwithref(vp
);
2532 /* Only go forward if you have write access */
2533 vfs_context_t ctx
= vfs_context_current();
2534 if (vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2540 error
= VNOP_IOCTL(vp
, uap
->cmd
, param_ptr
, 0, &context
);
2541 (void)vnode_put(vp
);
2547 * Set the vnode pointed to by 'fd'
2548 * and tag it as the (potentially future) backing store
2549 * for another filesystem
2551 case F_SETBACKINGSTORE
: {
2552 if (fp
->f_type
!= DTYPE_VNODE
) {
2557 vp
= (struct vnode
*)fp
->f_data
;
2559 if (vp
->v_tag
!= VT_HFS
) {
2565 if (vnode_getwithref(vp
)) {
2570 /* only proceed if you have write access */
2571 vfs_context_t ctx
= vfs_context_current();
2572 if (vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2579 /* If arg != 0, set, otherwise unset */
2581 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)1, 0, &context
);
2583 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
2591 * like F_GETPATH, but special semantics for
2592 * the mobile time machine handler.
2594 case F_GETPATH_MTMINFO
: {
2598 if (fp
->f_type
!= DTYPE_VNODE
) {
2602 vp
= (struct vnode
*)fp
->f_data
;
2605 pathlen
= MAXPATHLEN
;
2606 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
2607 if (pathbufp
== NULL
) {
2611 if ((error
= vnode_getwithref(vp
)) == 0) {
2612 int backingstore
= 0;
2614 /* Check for error from vn_getpath before moving on */
2615 if ((error
= vn_getpath(vp
, pathbufp
, &pathlen
)) == 0) {
2616 if (vp
->v_tag
== VT_HFS
) {
2617 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
) &backingstore
, 0, &context
);
2619 (void)vnode_put(vp
);
2622 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
2626 * If the copyout was successful, now check to ensure
2627 * that this vnode is not a BACKINGSTORE vnode. mtmd
2628 * wants the path regardless.
2635 (void)vnode_put(vp
);
2638 FREE(pathbufp
, M_TEMP
);
2642 #if DEBUG || DEVELOPMENT
2644 if (fp
->f_type
!= DTYPE_VNODE
) {
2648 vp
= (struct vnode
*)fp
->f_data
;
2657 * This is an fcntl() that we d not recognize at this level;
2658 * if this is a vnode, we send it down into the VNOP_IOCTL
2659 * for this vnode; this can include special devices, and will
2660 * effectively overload fcntl() to send ioctl()'s.
2662 if ((uap
->cmd
& IOC_VOID
) && (uap
->cmd
& IOC_INOUT
)) {
2667 /* Catch any now-invalid fcntl() selectors */
2669 case (int)APFSIOC_REVERT_TO_SNAPSHOT
:
2670 case (int)FSIOC_FIOSEEKHOLE
:
2671 case (int)FSIOC_FIOSEEKDATA
:
2672 case (int)FSIOC_CAS_BSDFLAGS
:
2673 case HFS_GET_BOOT_INFO
:
2674 case HFS_SET_BOOT_INFO
:
2676 case F_MARKDEPENDENCY
:
2684 if (fp
->f_type
!= DTYPE_VNODE
) {
2688 vp
= (struct vnode
*)fp
->f_data
;
2691 if ((error
= vnode_getwithref(vp
)) == 0) {
2692 #define STK_PARAMS 128
2693 char stkbuf
[STK_PARAMS
] = {0};
2697 * For this to work properly, we have to copy in the
2698 * ioctl() cmd argument if there is one; we must also
2699 * check that a command parameter, if present, does
2700 * not exceed the maximum command length dictated by
2701 * the number of bits we have available in the command
2702 * to represent a structure length. Finally, we have
2703 * to copy the results back out, if it is that type of
2706 size
= IOCPARM_LEN(uap
->cmd
);
2707 if (size
> IOCPARM_MAX
) {
2708 (void)vnode_put(vp
);
2714 if (size
> sizeof(stkbuf
)) {
2715 if ((memp
= (caddr_t
)kalloc(size
)) == 0) {
2716 (void)vnode_put(vp
);
2725 if (uap
->cmd
& IOC_IN
) {
2728 error
= copyin(argp
, data
, size
);
2730 (void)vnode_put(vp
);
2737 /* Bzero the section beyond that which was needed */
2738 if (size
<= sizeof(stkbuf
)) {
2739 bzero((((uint8_t*)data
) + size
), (sizeof(stkbuf
) - size
));
2744 *(user_addr_t
*)data
= argp
;
2746 *(uint32_t *)data
= (uint32_t)argp
;
2749 } else if ((uap
->cmd
& IOC_OUT
) && size
) {
2751 * Zero the buffer so the user always
2752 * gets back something deterministic.
2755 } else if (uap
->cmd
& IOC_VOID
) {
2757 *(user_addr_t
*)data
= argp
;
2759 *(uint32_t *)data
= (uint32_t)argp
;
2763 error
= VNOP_IOCTL(vp
, uap
->cmd
, CAST_DOWN(caddr_t
, data
), 0, &context
);
2765 (void)vnode_put(vp
);
2767 /* Copy any output data to user */
2768 if (error
== 0 && (uap
->cmd
& IOC_OUT
) && size
) {
2769 error
= copyout(data
, argp
, size
);
2779 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
2780 fp_drop(p
, fd
, fp
, 0);
2783 fp_drop(p
, fd
, fp
, 1);
2792 * Description: Common code for dup, dup2, and fcntl(F_DUPFD).
2794 * Parameters: p Process performing the dup
2796 * new The fd to dup it to
2797 * fd_flags Flags to augment the new fd
2798 * retval Pointer to the call return area
2800 * Returns: 0 Success
2805 * *retval (modified) The new descriptor
2807 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
2810 * Notes: This function may drop and reacquire this lock; it is unsafe
2811 * for a caller to assume that other state protected by the lock
2812 * has not been subsequently changed out from under it.
2816 struct filedesc
*fdp
, int old
, int new, int fd_flags
, int32_t *retval
)
2818 struct fileproc
*nfp
;
2819 struct fileproc
*ofp
;
2825 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2827 if ((ofp
= fdp
->fd_ofiles
[old
]) == NULL
||
2828 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
)) {
2835 error
= mac_file_check_dup(proc_ucred(p
), ofp
->f_fglob
, new);
2845 nfp
= fileproc_alloc_init(NULL
);
2855 nfp
->f_fglob
= ofp
->f_fglob
;
2858 if (fdp
->fd_ofiles
[new] != 0) {
2859 panic("finishdup: overwriting fd_ofiles with new %d", new);
2861 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0) {
2862 panic("finishdup: unreserved fileflags with new %d", new);
2866 if (new > fdp
->fd_lastfile
) {
2867 fdp
->fd_lastfile
= new;
2869 *fdflags(p
, new) |= fd_flags
;
2870 procfdtbl_releasefd(p
, new, nfp
);
2879 * Description: The implementation of the close(2) system call
2881 * Parameters: p Process in whose per process file table
2882 * the close is to occur
2883 * uap->fd fd to be closed
2886 * Returns: 0 Success
2887 * fp_lookup:EBADF Bad file descriptor
2888 * fp_guard_exception:??? Guarded file descriptor
2889 * close_internal:EBADF
2890 * close_internal:??? Anything returnable by a per-fileops
2894 close(proc_t p
, struct close_args
*uap
, int32_t *retval
)
2896 __pthread_testcancel(1);
2897 return close_nocancel(p
, (struct close_nocancel_args
*)uap
, retval
);
2902 close_nocancel(proc_t p
, struct close_nocancel_args
*uap
, __unused
int32_t *retval
)
2904 struct fileproc
*fp
;
2908 AUDIT_SYSCLOSE(p
, fd
);
2912 if ((error
= fp_lookup(p
, fd
, &fp
, 1))) {
2917 if (FP_ISGUARDED(fp
, GUARD_CLOSE
)) {
2918 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_CLOSE
);
2919 (void) fp_drop(p
, fd
, fp
, 1);
2924 error
= close_internal_locked(p
, fd
, fp
, 0);
2933 * close_internal_locked
2935 * Close a file descriptor.
2937 * Parameters: p Process in whose per process file table
2938 * the close is to occur
2939 * fd fd to be closed
2940 * fp fileproc associated with the fd
2942 * Returns: 0 Success
2943 * EBADF fd already in close wait state
2944 * closef_locked:??? Anything returnable by a per-fileops
2947 * Locks: Assumes proc_fdlock for process is held by the caller and returns
2950 * Notes: This function may drop and reacquire this lock; it is unsafe
2951 * for a caller to assume that other state protected by the lock
2952 * has not been subsequently changed out from under it.
2955 close_internal_locked(proc_t p
, int fd
, struct fileproc
*fp
, int flags
)
2957 struct filedesc
*fdp
= p
->p_fd
;
2959 int resvfd
= flags
& FD_DUP2RESV
;
2963 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2966 /* Keep people from using the filedesc while we are closing it */
2967 procfdtbl_markclosefd(p
, fd
);
2970 if ((fp
->f_flags
& FP_CLOSING
) == FP_CLOSING
) {
2971 panic("close_internal_locked: being called on already closing fd");
2976 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0) {
2977 panic("close_internal: unreserved fileflags with fd %d", fd
);
2981 fp
->f_flags
|= FP_CLOSING
;
2983 if ((fp
->f_flags
& FP_AIOISSUED
) || kauth_authorize_fileop_has_listeners()) {
2986 if ((fp
->f_type
== DTYPE_VNODE
) && kauth_authorize_fileop_has_listeners()) {
2988 * call out to allow 3rd party notification of close.
2989 * Ignore result of kauth_authorize_fileop call.
2991 if (vnode_getwithref((vnode_t
)fp
->f_data
) == 0) {
2992 u_int fileop_flags
= 0;
2993 if ((fp
->f_flags
& FP_WRITTEN
) != 0) {
2994 fileop_flags
|= KAUTH_FILEOP_CLOSE_MODIFIED
;
2996 kauth_authorize_fileop(fp
->f_fglob
->fg_cred
, KAUTH_FILEOP_CLOSE
,
2997 (uintptr_t)fp
->f_data
, (uintptr_t)fileop_flags
);
2998 vnode_put((vnode_t
)fp
->f_data
);
3001 if (fp
->f_flags
& FP_AIOISSUED
) {
3003 * cancel all async IO requests that can be cancelled.
3005 _aio_close( p
, fd
);
3011 if (fd
< fdp
->fd_knlistsize
) {
3012 knote_fdclose(p
, fd
);
3015 /* release the ref returned from fp_lookup before calling drain */
3016 (void) os_ref_release_locked(&fp
->f_iocount
);
3017 fileproc_drain(p
, fp
);
3019 if (fp
->f_flags
& FP_WAITEVENT
) {
3020 (void)waitevent_close(p
, fp
);
3026 procfdtbl_reservefd(p
, fd
);
3029 if (ENTR_SHOULDTRACE
&& fp
->f_type
== DTYPE_SOCKET
) {
3030 KERNEL_ENERGYTRACE(kEnTrActKernSocket
, DBG_FUNC_END
,
3031 fd
, 0, (int64_t)VM_KERNEL_ADDRPERM(fp
->f_data
));
3034 error
= closef_locked(fp
, fp
->f_fglob
, p
);
3035 if ((fp
->f_flags
& FP_WAITCLOSE
) == FP_WAITCLOSE
) {
3036 wakeup(&fp
->f_flags
);
3038 fp
->f_flags
&= ~(FP_WAITCLOSE
| FP_CLOSING
);
3048 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0) {
3049 panic("close with reserved fd returns with freed fd:%d: proc: %p", fd
, p
);
3061 * Description: Return status information about a file descriptor.
3063 * Parameters: p The process doing the fstat
3065 * ub The user stat buffer
3066 * xsecurity The user extended security
3067 * buffer, or 0 if none
3068 * xsecurity_size The size of xsecurity, or 0
3070 * isstat64 Flag to indicate 64 bit version
3071 * for inode size, etc.
3073 * Returns: 0 Success
3076 * fp_lookup:EBADF Bad file descriptor
3077 * vnode_getwithref:???
3079 * vnode_getwithref:???
3086 * Notes: Internal implementation for all other fstat() related
3089 * XXX switch on node type is bogus; need a stat in struct
3090 * XXX fileops instead.
3093 fstat1(proc_t p
, int fd
, user_addr_t ub
, user_addr_t xsecurity
, user_addr_t xsecurity_size
, int isstat64
)
3095 struct fileproc
*fp
;
3101 struct user64_stat user64_sb
;
3102 struct user32_stat user32_sb
;
3103 struct user64_stat64 user64_sb64
;
3104 struct user32_stat64 user32_sb64
;
3109 kauth_filesec_t fsec
;
3110 user_size_t xsecurity_bufsize
;
3111 vfs_context_t ctx
= vfs_context_current();
3117 if ((error
= fp_lookup(p
, fd
, &fp
, 0)) != 0) {
3122 fsec
= KAUTH_FILESEC_NONE
;
3124 sbptr
= (void *)&source
;
3128 if ((error
= vnode_getwithref((vnode_t
)data
)) == 0) {
3130 * If the caller has the file open, and is not
3131 * requesting extended security information, we are
3132 * going to let them get the basic stat information.
3134 if (xsecurity
== USER_ADDR_NULL
) {
3135 error
= vn_stat_noauth((vnode_t
)data
, sbptr
, NULL
, isstat64
, 0, ctx
,
3136 fp
->f_fglob
->fg_cred
);
3138 error
= vn_stat((vnode_t
)data
, sbptr
, &fsec
, isstat64
, 0, ctx
);
3141 AUDIT_ARG(vnpath
, (struct vnode
*)data
, ARG_VNODE1
);
3142 (void)vnode_put((vnode_t
)data
);
3148 error
= soo_stat((struct socket
*)data
, sbptr
, isstat64
);
3150 #endif /* SOCKETS */
3153 error
= pipe_stat((void *)data
, sbptr
, isstat64
);
3157 error
= pshm_stat((void *)data
, sbptr
, isstat64
);
3161 error
= kqueue_stat((void *)data
, sbptr
, isstat64
, p
);
3171 if (isstat64
!= 0) {
3172 source
.sb64
.st_lspare
= 0;
3173 source
.sb64
.st_qspare
[0] = 0LL;
3174 source
.sb64
.st_qspare
[1] = 0LL;
3176 if (IS_64BIT_PROCESS(current_proc())) {
3177 munge_user64_stat64(&source
.sb64
, &dest
.user64_sb64
);
3178 my_size
= sizeof(dest
.user64_sb64
);
3179 sbp
= (caddr_t
)&dest
.user64_sb64
;
3181 munge_user32_stat64(&source
.sb64
, &dest
.user32_sb64
);
3182 my_size
= sizeof(dest
.user32_sb64
);
3183 sbp
= (caddr_t
)&dest
.user32_sb64
;
3186 source
.sb
.st_lspare
= 0;
3187 source
.sb
.st_qspare
[0] = 0LL;
3188 source
.sb
.st_qspare
[1] = 0LL;
3189 if (IS_64BIT_PROCESS(current_proc())) {
3190 munge_user64_stat(&source
.sb
, &dest
.user64_sb
);
3191 my_size
= sizeof(dest
.user64_sb
);
3192 sbp
= (caddr_t
)&dest
.user64_sb
;
3194 munge_user32_stat(&source
.sb
, &dest
.user32_sb
);
3195 my_size
= sizeof(dest
.user32_sb
);
3196 sbp
= (caddr_t
)&dest
.user32_sb
;
3200 error
= copyout(sbp
, ub
, my_size
);
3203 /* caller wants extended security information? */
3204 if (xsecurity
!= USER_ADDR_NULL
) {
3205 /* did we get any? */
3206 if (fsec
== KAUTH_FILESEC_NONE
) {
3207 if (susize(xsecurity_size
, 0) != 0) {
3212 /* find the user buffer size */
3213 xsecurity_bufsize
= fusize(xsecurity_size
);
3215 /* copy out the actual data size */
3216 if (susize(xsecurity_size
, KAUTH_FILESEC_COPYSIZE(fsec
)) != 0) {
3221 /* if the caller supplied enough room, copy out to it */
3222 if (xsecurity_bufsize
>= KAUTH_FILESEC_COPYSIZE(fsec
)) {
3223 error
= copyout(fsec
, xsecurity
, KAUTH_FILESEC_COPYSIZE(fsec
));
3228 fp_drop(p
, fd
, fp
, 0);
3230 kauth_filesec_free(fsec
);
3239 * Description: Extended version of fstat supporting returning extended
3240 * security information
3242 * Parameters: p The process doing the fstat
3243 * uap->fd The fd to stat
3244 * uap->ub The user stat buffer
3245 * uap->xsecurity The user extended security
3246 * buffer, or 0 if none
3247 * uap->xsecurity_size The size of xsecurity, or 0
3249 * Returns: 0 Success
3250 * !0 Errno (see fstat1)
3253 fstat_extended(proc_t p
, struct fstat_extended_args
*uap
, __unused
int32_t *retval
)
3255 return fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 0);
3262 * Description: Get file status for the file associated with fd
3264 * Parameters: p The process doing the fstat
3265 * uap->fd The fd to stat
3266 * uap->ub The user stat buffer
3268 * Returns: 0 Success
3269 * !0 Errno (see fstat1)
3272 fstat(proc_t p
, struct fstat_args
*uap
, __unused
int32_t *retval
)
3274 return fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 0);
3281 * Description: Extended version of fstat64 supporting returning extended
3282 * security information
3284 * Parameters: p The process doing the fstat
3285 * uap->fd The fd to stat
3286 * uap->ub The user stat buffer
3287 * uap->xsecurity The user extended security
3288 * buffer, or 0 if none
3289 * uap->xsecurity_size The size of xsecurity, or 0
3291 * Returns: 0 Success
3292 * !0 Errno (see fstat1)
3295 fstat64_extended(proc_t p
, struct fstat64_extended_args
*uap
, __unused
int32_t *retval
)
3297 return fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 1);
3304 * Description: Get 64 bit version of the file status for the file associated
3307 * Parameters: p The process doing the fstat
3308 * uap->fd The fd to stat
3309 * uap->ub The user stat buffer
3311 * Returns: 0 Success
3312 * !0 Errno (see fstat1)
3315 fstat64(proc_t p
, struct fstat64_args
*uap
, __unused
int32_t *retval
)
3317 return fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 1);
3324 * Description: Return pathconf information about a file descriptor.
3326 * Parameters: p Process making the request
3327 * uap->fd fd to get information about
3328 * uap->name Name of information desired
3329 * retval Pointer to the call return area
3331 * Returns: 0 Success
3333 * fp_lookup:EBADF Bad file descriptor
3334 * vnode_getwithref:???
3338 * *retval (modified) Returned information (numeric)
3341 fpathconf(proc_t p
, struct fpathconf_args
*uap
, int32_t *retval
)
3344 struct fileproc
*fp
;
3351 AUDIT_ARG(fd
, uap
->fd
);
3352 if ((error
= fp_lookup(p
, fd
, &fp
, 0))) {
3360 if (uap
->name
!= _PC_PIPE_BUF
) {
3369 if (uap
->name
!= _PC_PIPE_BUF
) {
3378 vp
= (struct vnode
*)data
;
3380 if ((error
= vnode_getwithref(vp
)) == 0) {
3381 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
3383 error
= vn_pathconf(vp
, uap
->name
, retval
, vfs_context_current());
3385 (void)vnode_put(vp
);
3395 fp_drop(p
, fd
, fp
, 0);
3400 * Statistics counter for the number of times a process calling fdalloc()
3401 * has resulted in an expansion of the per process open file table.
3403 * XXX This would likely be of more use if it were per process
3411 * Description: Allocate a file descriptor for the process.
3413 * Parameters: p Process to allocate the fd in
3414 * want The fd we would prefer to get
3415 * result Pointer to fd we got
3417 * Returns: 0 Success
3422 * *result (modified) The fd which was allocated
3425 fdalloc(proc_t p
, int want
, int *result
)
3427 struct filedesc
*fdp
= p
->p_fd
;
3429 int lim
, last
, numfiles
, oldnfiles
;
3430 struct fileproc
**newofiles
, **ofiles
;
3431 char *newofileflags
;
3434 * Search for a free descriptor starting at the higher
3435 * of want or fd_freefile. If that fails, consider
3436 * expanding the ofile array.
3439 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
3442 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
3444 last
= min(fdp
->fd_nfiles
, lim
);
3445 if ((i
= want
) < fdp
->fd_freefile
) {
3446 i
= fdp
->fd_freefile
;
3448 for (; i
< last
; i
++) {
3449 if (fdp
->fd_ofiles
[i
] == NULL
&& !(fdp
->fd_ofileflags
[i
] & UF_RESERVED
)) {
3450 procfdtbl_reservefd(p
, i
);
3451 if (i
> fdp
->fd_lastfile
) {
3452 fdp
->fd_lastfile
= i
;
3454 if (want
<= fdp
->fd_freefile
) {
3455 fdp
->fd_freefile
= i
;
3463 * No space in current array. Expand?
3465 if (fdp
->fd_nfiles
>= lim
) {
3468 if (fdp
->fd_nfiles
< NDEXTENT
) {
3469 numfiles
= NDEXTENT
;
3471 numfiles
= 2 * fdp
->fd_nfiles
;
3474 if (numfiles
> lim
) {
3478 MALLOC_ZONE(newofiles
, struct fileproc
**,
3479 numfiles
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
3481 if (newofiles
== NULL
) {
3484 if (fdp
->fd_nfiles
>= numfiles
) {
3485 FREE_ZONE(newofiles
, numfiles
* OFILESIZE
, M_OFILETABL
);
3488 newofileflags
= (char *) &newofiles
[numfiles
];
3490 * Copy the existing ofile and ofileflags arrays
3491 * and zero the new portion of each array.
3493 oldnfiles
= fdp
->fd_nfiles
;
3494 (void) memcpy(newofiles
, fdp
->fd_ofiles
,
3495 oldnfiles
* sizeof(*fdp
->fd_ofiles
));
3496 (void) memset(&newofiles
[oldnfiles
], 0,
3497 (numfiles
- oldnfiles
) * sizeof(*fdp
->fd_ofiles
));
3499 (void) memcpy(newofileflags
, fdp
->fd_ofileflags
,
3500 oldnfiles
* sizeof(*fdp
->fd_ofileflags
));
3501 (void) memset(&newofileflags
[oldnfiles
], 0,
3502 (numfiles
- oldnfiles
) *
3503 sizeof(*fdp
->fd_ofileflags
));
3504 ofiles
= fdp
->fd_ofiles
;
3505 fdp
->fd_ofiles
= newofiles
;
3506 fdp
->fd_ofileflags
= newofileflags
;
3507 fdp
->fd_nfiles
= numfiles
;
3508 FREE_ZONE(ofiles
, oldnfiles
* OFILESIZE
, M_OFILETABL
);
3517 * Description: Check to see whether n user file descriptors are available
3520 * Parameters: p Process to check in
3521 * n The number of fd's desired
3526 * Locks: Assumes proc_fdlock for process is held by the caller
3528 * Notes: The answer only remains valid so long as the proc_fdlock is
3529 * held by the caller.
3532 fdavail(proc_t p
, int n
)
3534 struct filedesc
*fdp
= p
->p_fd
;
3535 struct fileproc
**fpp
;
3539 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
3540 if ((i
= lim
- fdp
->fd_nfiles
) > 0 && (n
-= i
) <= 0) {
3543 fpp
= &fdp
->fd_ofiles
[fdp
->fd_freefile
];
3544 flags
= &fdp
->fd_ofileflags
[fdp
->fd_freefile
];
3545 for (i
= fdp
->fd_nfiles
- fdp
->fd_freefile
; --i
>= 0; fpp
++, flags
++) {
3546 if (*fpp
== NULL
&& !(*flags
& UF_RESERVED
) && --n
<= 0) {
3557 * Description: Legacy KPI wrapper function for _fdrelse
3559 * Parameters: p Process in which fd lives
3564 * Locks: Assumes proc_fdlock for process is held by the caller
3567 fdrelse(proc_t p
, int fd
)
3576 * Description: Get the fileproc pointer for the given fd from the per process
3577 * open file table without taking an explicit reference on it.
3579 * Parameters: p Process containing fd
3580 * fd fd to obtain fileproc for
3581 * resultfp Pointer to pointer return area
3583 * Returns: 0 Success
3587 * *resultfp (modified) Pointer to fileproc pointer
3589 * Locks: Assumes proc_fdlock for process is held by the caller
3591 * Notes: Because there is no reference explicitly taken, the returned
3592 * fileproc pointer is only valid so long as the proc_fdlock
3593 * remains held by the caller.
3596 fdgetf_noref(proc_t p
, int fd
, struct fileproc
**resultfp
)
3598 struct filedesc
*fdp
= p
->p_fd
;
3599 struct fileproc
*fp
;
3601 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3602 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3603 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3616 * Description: Get fileproc and vnode pointer for a given fd from the per
3617 * process open file table of the specified process, and if
3618 * successful, increment the f_iocount
3620 * Parameters: p Process in which fd lives
3621 * fd fd to get information for
3622 * resultfp Pointer to result fileproc
3623 * pointer area, or 0 if none
3624 * resultvp Pointer to result vnode pointer
3625 * area, or 0 if none
3627 * Returns: 0 Success
3628 * EBADF Bad file descriptor
3629 * ENOTSUP fd does not refer to a vnode
3632 * *resultfp (modified) Fileproc pointer
3633 * *resultvp (modified) vnode pointer
3635 * Notes: The resultfp and resultvp fields are optional, and may be
3636 * independently specified as NULL to skip returning information
3638 * Locks: Internally takes and releases proc_fdlock
3641 fp_getfvp(proc_t p
, int fd
, struct fileproc
**resultfp
, struct vnode
**resultvp
)
3643 struct filedesc
*fdp
= p
->p_fd
;
3644 struct fileproc
*fp
;
3646 proc_fdlock_spin(p
);
3647 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3648 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3649 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3653 if (fp
->f_type
!= DTYPE_VNODE
) {
3657 os_ref_retain_locked(&fp
->f_iocount
);
3663 *resultvp
= (struct vnode
*)fp
->f_data
;
3674 * Description: Get fileproc, vnode pointer, and vid for a given fd from the
3675 * per process open file table of the specified process, and if
3676 * successful, increment the f_iocount
3678 * Parameters: p Process in which fd lives
3679 * fd fd to get information for
3680 * resultfp Pointer to result fileproc
3681 * pointer area, or 0 if none
3682 * resultvp Pointer to result vnode pointer
3683 * area, or 0 if none
3684 * vidp Pointer to resuld vid area
3686 * Returns: 0 Success
3687 * EBADF Bad file descriptor
3688 * ENOTSUP fd does not refer to a vnode
3691 * *resultfp (modified) Fileproc pointer
3692 * *resultvp (modified) vnode pointer
3695 * Notes: The resultfp and resultvp fields are optional, and may be
3696 * independently specified as NULL to skip returning information
3698 * Locks: Internally takes and releases proc_fdlock
3701 fp_getfvpandvid(proc_t p
, int fd
, struct fileproc
**resultfp
,
3702 struct vnode
**resultvp
, uint32_t *vidp
)
3704 struct filedesc
*fdp
= p
->p_fd
;
3705 struct fileproc
*fp
;
3707 proc_fdlock_spin(p
);
3708 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3709 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3710 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3714 if (fp
->f_type
!= DTYPE_VNODE
) {
3718 os_ref_retain_locked(&fp
->f_iocount
);
3724 *resultvp
= (struct vnode
*)fp
->f_data
;
3727 *vidp
= (uint32_t)vnode_vid((struct vnode
*)fp
->f_data
);
3738 * Description: Get fileproc and socket pointer for a given fd from the
3739 * per process open file table of the specified process, and if
3740 * successful, increment the f_iocount
3742 * Parameters: p Process in which fd lives
3743 * fd fd to get information for
3744 * resultfp Pointer to result fileproc
3745 * pointer area, or 0 if none
3746 * results Pointer to result socket
3747 * pointer area, or 0 if none
3749 * Returns: EBADF The file descriptor is invalid
3750 * EOPNOTSUPP The file descriptor is not a socket
3754 * *resultfp (modified) Fileproc pointer
3755 * *results (modified) socket pointer
3757 * Notes: EOPNOTSUPP should probably be ENOTSOCK; this function is only
3758 * ever called from accept1().
3761 fp_getfsock(proc_t p
, int fd
, struct fileproc
**resultfp
,
3762 struct socket
**results
)
3764 struct filedesc
*fdp
= p
->p_fd
;
3765 struct fileproc
*fp
;
3767 proc_fdlock_spin(p
);
3768 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3769 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3770 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3774 if (fp
->f_type
!= DTYPE_SOCKET
) {
3778 os_ref_retain_locked(&fp
->f_iocount
);
3784 *results
= (struct socket
*)fp
->f_data
;
3795 * Description: Get fileproc and kqueue pointer for a given fd from the
3796 * per process open file table of the specified process, and if
3797 * successful, increment the f_iocount
3799 * Parameters: p Process in which fd lives
3800 * fd fd to get information for
3801 * resultfp Pointer to result fileproc
3802 * pointer area, or 0 if none
3803 * resultkq Pointer to result kqueue
3804 * pointer area, or 0 if none
3806 * Returns: EBADF The file descriptor is invalid
3807 * EBADF The file descriptor is not a socket
3811 * *resultfp (modified) Fileproc pointer
3812 * *resultkq (modified) kqueue pointer
3814 * Notes: The second EBADF should probably be something else to make
3815 * the error condition distinct.
3818 fp_getfkq(proc_t p
, int fd
, struct fileproc
**resultfp
,
3819 struct kqueue
**resultkq
)
3821 struct filedesc
*fdp
= p
->p_fd
;
3822 struct fileproc
*fp
;
3824 proc_fdlock_spin(p
);
3825 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3826 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3827 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3831 if (fp
->f_type
!= DTYPE_KQUEUE
) {
3835 os_ref_retain_locked(&fp
->f_iocount
);
3841 *resultkq
= (struct kqueue
*)fp
->f_data
;
3852 * Description: Get fileproc and POSIX shared memory pointer for a given fd
3853 * from the per process open file table of the specified process
3854 * and if successful, increment the f_iocount
3856 * Parameters: p Process in which fd lives
3857 * fd fd to get information for
3858 * resultfp Pointer to result fileproc
3859 * pointer area, or 0 if none
3860 * resultpshm Pointer to result POSIX
3861 * shared memory pointer
3862 * pointer area, or 0 if none
3864 * Returns: EBADF The file descriptor is invalid
3865 * EBADF The file descriptor is not a POSIX
3866 * shared memory area
3870 * *resultfp (modified) Fileproc pointer
3871 * *resultpshm (modified) POSIX shared memory pointer
3873 * Notes: The second EBADF should probably be something else to make
3874 * the error condition distinct.
3877 fp_getfpshm(proc_t p
, int fd
, struct fileproc
**resultfp
,
3878 struct pshmnode
**resultpshm
)
3880 struct filedesc
*fdp
= p
->p_fd
;
3881 struct fileproc
*fp
;
3883 proc_fdlock_spin(p
);
3884 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3885 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3886 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3890 if (fp
->f_type
!= DTYPE_PSXSHM
) {
3894 os_ref_retain_locked(&fp
->f_iocount
);
3900 *resultpshm
= (struct pshmnode
*)fp
->f_data
;
3911 * Description: Get fileproc and POSIX semaphore pointer for a given fd from
3912 * the per process open file table of the specified process
3913 * and if successful, increment the f_iocount
3915 * Parameters: p Process in which fd lives
3916 * fd fd to get information for
3917 * resultfp Pointer to result fileproc
3918 * pointer area, or 0 if none
3919 * resultpsem Pointer to result POSIX
3920 * semaphore pointer area, or
3923 * Returns: EBADF The file descriptor is invalid
3924 * EBADF The file descriptor is not a POSIX
3929 * *resultfp (modified) Fileproc pointer
3930 * *resultpsem (modified) POSIX semaphore pointer
3932 * Notes: The second EBADF should probably be something else to make
3933 * the error condition distinct.
3935 * In order to support unnamed POSIX semaphores, the named
3936 * POSIX semaphores will have to move out of the per-process
3937 * open filetable, and into a global table that is shared with
3938 * unnamed POSIX semaphores, since unnamed POSIX semaphores
3939 * are typically used by declaring instances in shared memory,
3940 * and there's no other way to do this without changing the
3941 * underlying type, which would introduce binary compatibility
3945 fp_getfpsem(proc_t p
, int fd
, struct fileproc
**resultfp
,
3946 struct psemnode
**resultpsem
)
3948 struct filedesc
*fdp
= p
->p_fd
;
3949 struct fileproc
*fp
;
3951 proc_fdlock_spin(p
);
3952 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3953 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3954 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3958 if (fp
->f_type
!= DTYPE_PSXSEM
) {
3962 os_ref_retain_locked(&fp
->f_iocount
);
3968 *resultpsem
= (struct psemnode
*)fp
->f_data
;
3979 * Description: Get fileproc and pipe pointer for a given fd from the
3980 * per process open file table of the specified process
3981 * and if successful, increment the f_iocount
3983 * Parameters: p Process in which fd lives
3984 * fd fd to get information for
3985 * resultfp Pointer to result fileproc
3986 * pointer area, or 0 if none
3987 * resultpipe Pointer to result pipe
3988 * pointer area, or 0 if none
3990 * Returns: EBADF The file descriptor is invalid
3991 * EBADF The file descriptor is not a socket
3995 * *resultfp (modified) Fileproc pointer
3996 * *resultpipe (modified) pipe pointer
3998 * Notes: The second EBADF should probably be something else to make
3999 * the error condition distinct.
4002 fp_getfpipe(proc_t p
, int fd
, struct fileproc
**resultfp
,
4003 struct pipe
**resultpipe
)
4005 struct filedesc
*fdp
= p
->p_fd
;
4006 struct fileproc
*fp
;
4008 proc_fdlock_spin(p
);
4009 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
4010 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
4011 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
4015 if (fp
->f_type
!= DTYPE_PIPE
) {
4019 os_ref_retain_locked(&fp
->f_iocount
);
4025 *resultpipe
= (struct pipe
*)fp
->f_data
;
4036 * Description: Get fileproc pointer for a given fd from the per process
4037 * open file table of the specified process and if successful,
4038 * increment the f_iocount
4040 * Parameters: p Process in which fd lives
4041 * fd fd to get information for
4042 * resultfp Pointer to result fileproc
4043 * pointer area, or 0 if none
4044 * locked !0 if the caller holds the
4045 * proc_fdlock, 0 otherwise
4047 * Returns: 0 Success
4048 * EBADF Bad file descriptor
4051 * *resultfp (modified) Fileproc pointer
4053 * Locks: If the argument 'locked' is non-zero, then the caller is
4054 * expected to have taken and held the proc_fdlock; if it is
4055 * zero, than this routine internally takes and drops this lock.
4058 fp_lookup(proc_t p
, int fd
, struct fileproc
**resultfp
, int locked
)
4060 struct filedesc
*fdp
= p
->p_fd
;
4061 struct fileproc
*fp
;
4064 proc_fdlock_spin(p
);
4066 if (fd
< 0 || fdp
== NULL
|| fd
>= fdp
->fd_nfiles
||
4067 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
4068 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
4074 os_ref_retain_locked(&fp
->f_iocount
);
4090 * Description: Swap the fileproc pointer for a given fd with a new
4091 * fileproc pointer in the per-process open file table of
4092 * the specified process. The fdlock must be held at entry.
4093 * Iff the swap is successful, the old fileproc pointer is freed.
4095 * Parameters: p Process containing the fd
4096 * fd The fd of interest
4097 * nfp Pointer to the newfp
4099 * Returns: 0 Success
4100 * EBADF Bad file descriptor
4102 * EKEEPLOOKING Other references were active, try again.
4105 fp_tryswap(proc_t p
, int fd
, struct fileproc
*nfp
)
4107 struct fileproc
*fp
;
4110 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
4112 if (0 != (error
= fp_lookup(p
, fd
, &fp
, 1))) {
4116 * At this point, our caller (change_guardedfd_np) has
4117 * one f_iocount reference, and we just took another
4118 * one to begin the replacement.
4119 * fp and nfp have a +1 reference from allocation.
4120 * Thus if no-one else is looking, f_iocount should be 3.
4122 if (os_ref_get_count(&fp
->f_iocount
) < 3 ||
4123 1 != os_ref_get_count(&nfp
->f_iocount
)) {
4124 panic("%s: f_iocount", __func__
);
4125 } else if (3 == os_ref_get_count(&fp
->f_iocount
)) {
4126 /* Copy the contents of *fp, preserving the "type" of *nfp */
4128 nfp
->f_flags
= (nfp
->f_flags
& FP_TYPEMASK
) |
4129 (fp
->f_flags
& ~FP_TYPEMASK
);
4130 os_ref_retain_locked(&nfp
->f_iocount
);
4131 os_ref_retain_locked(&nfp
->f_iocount
);
4132 nfp
->f_fglob
= fp
->f_fglob
;
4133 nfp
->f_wset
= fp
->f_wset
;
4135 p
->p_fd
->fd_ofiles
[fd
] = nfp
;
4136 fp_drop(p
, fd
, nfp
, 1);
4138 os_ref_release_live(&fp
->f_iocount
);
4139 os_ref_release_live(&fp
->f_iocount
);
4143 * Wait for all other active references to evaporate.
4145 p
->p_fpdrainwait
= 1;
4146 error
= msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
,
4147 PRIBIO
| PCATCH
, "tryswap fpdrain", NULL
);
4150 * Return an "internal" errno to trigger a full
4151 * reevaluation of the change-guard attempt.
4153 error
= EKEEPLOOKING
;
4155 (void) fp_drop(p
, fd
, fp
, 1);
4164 * Description: Set the FP_WRITTEN flag on the fileproc and drop the I/O
4165 * reference previously taken by calling fp_lookup et. al.
4167 * Parameters: p Process in which the fd lives
4168 * fd fd associated with the fileproc
4169 * fp fileproc on which to set the
4170 * flag and drop the reference
4172 * Returns: 0 Success
4173 * fp_drop:EBADF Bad file descriptor
4175 * Locks: This function internally takes and drops the proc_fdlock for
4176 * the supplied process
4178 * Notes: The fileproc must correspond to the fd in the supplied proc
4181 fp_drop_written(proc_t p
, int fd
, struct fileproc
*fp
)
4185 proc_fdlock_spin(p
);
4187 fp
->f_flags
|= FP_WRITTEN
;
4189 error
= fp_drop(p
, fd
, fp
, 1);
4200 * Description: Set the FP_WAITEVENT flag on the fileproc and drop the I/O
4201 * reference previously taken by calling fp_lookup et. al.
4203 * Parameters: p Process in which the fd lives
4204 * fd fd associated with the fileproc
4205 * fp fileproc on which to set the
4206 * flag and drop the reference
4208 * Returns: 0 Success
4209 * fp_drop:EBADF Bad file descriptor
4211 * Locks: This function internally takes and drops the proc_fdlock for
4212 * the supplied process
4214 * Notes: The fileproc must correspond to the fd in the supplied proc
4217 fp_drop_event(proc_t p
, int fd
, struct fileproc
*fp
)
4221 proc_fdlock_spin(p
);
4223 fp
->f_flags
|= FP_WAITEVENT
;
4225 error
= fp_drop(p
, fd
, fp
, 1);
4236 * Description: Drop the I/O reference previously taken by calling fp_lookup
4239 * Parameters: p Process in which the fd lives
4240 * fd fd associated with the fileproc
4241 * fp fileproc on which to set the
4242 * flag and drop the reference
4243 * locked flag to internally take and
4244 * drop proc_fdlock if it is not
4245 * already held by the caller
4247 * Returns: 0 Success
4248 * EBADF Bad file descriptor
4250 * Locks: This function internally takes and drops the proc_fdlock for
4251 * the supplied process if 'locked' is non-zero, and assumes that
4252 * the caller already holds this lock if 'locked' is non-zero.
4254 * Notes: The fileproc must correspond to the fd in the supplied proc
4257 fp_drop(proc_t p
, int fd
, struct fileproc
*fp
, int locked
)
4259 struct filedesc
*fdp
= p
->p_fd
;
4263 proc_fdlock_spin(p
);
4265 if ((fp
== FILEPROC_NULL
) && (fd
< 0 || fd
>= fdp
->fd_nfiles
||
4266 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
4267 ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
4268 !(fdp
->fd_ofileflags
[fd
] & UF_CLOSING
)))) {
4275 if (1 == os_ref_release_locked(&fp
->f_iocount
)) {
4276 if (fp
->f_flags
& FP_SELCONFLICT
) {
4277 fp
->f_flags
&= ~FP_SELCONFLICT
;
4280 if (p
->p_fpdrainwait
) {
4281 p
->p_fpdrainwait
= 0;
4289 wakeup(&p
->p_fpdrainwait
);
4299 * Description: Given an fd, look it up in the current process's per process
4300 * open file table, and return its internal vnode pointer.
4302 * Parameters: fd fd to obtain vnode from
4303 * vpp pointer to vnode return area
4305 * Returns: 0 Success
4306 * EINVAL The fd does not refer to a
4307 * vnode fileproc entry
4308 * fp_lookup:EBADF Bad file descriptor
4311 * *vpp (modified) Returned vnode pointer
4313 * Locks: This function internally takes and drops the proc_fdlock for
4314 * the current process
4316 * Notes: If successful, this function increments the f_iocount on the
4317 * fd's corresponding fileproc.
4319 * The fileproc referenced is not returned; because of this, care
4320 * must be taken to not drop the last reference (e.g. by closing
4321 * the file). This is inherently unsafe, since the reference may
4322 * not be recoverable from the vnode, if there is a subsequent
4323 * close that destroys the associate fileproc. The caller should
4324 * therefore retain their own reference on the fileproc so that
4325 * the f_iocount can be dropped subsequently. Failure to do this
4326 * can result in the returned pointer immediately becoming invalid
4327 * following the call.
4329 * Use of this function is discouraged.
4332 file_vnode(int fd
, struct vnode
**vpp
)
4334 proc_t p
= current_proc();
4335 struct fileproc
*fp
;
4338 proc_fdlock_spin(p
);
4339 if ((error
= fp_lookup(p
, fd
, &fp
, 1))) {
4343 if (fp
->f_type
!= DTYPE_VNODE
) {
4344 fp_drop(p
, fd
, fp
, 1);
4349 *vpp
= (struct vnode
*)fp
->f_data
;
4358 * file_vnode_withvid
4360 * Description: Given an fd, look it up in the current process's per process
4361 * open file table, and return its internal vnode pointer.
4363 * Parameters: fd fd to obtain vnode from
4364 * vpp pointer to vnode return area
4365 * vidp pointer to vid of the returned vnode
4367 * Returns: 0 Success
4368 * EINVAL The fd does not refer to a
4369 * vnode fileproc entry
4370 * fp_lookup:EBADF Bad file descriptor
4373 * *vpp (modified) Returned vnode pointer
4375 * Locks: This function internally takes and drops the proc_fdlock for
4376 * the current process
4378 * Notes: If successful, this function increments the f_iocount on the
4379 * fd's corresponding fileproc.
4381 * The fileproc referenced is not returned; because of this, care
4382 * must be taken to not drop the last reference (e.g. by closing
4383 * the file). This is inherently unsafe, since the reference may
4384 * not be recoverable from the vnode, if there is a subsequent
4385 * close that destroys the associate fileproc. The caller should
4386 * therefore retain their own reference on the fileproc so that
4387 * the f_iocount can be dropped subsequently. Failure to do this
4388 * can result in the returned pointer immediately becoming invalid
4389 * following the call.
4391 * Use of this function is discouraged.
4394 file_vnode_withvid(int fd
, struct vnode
**vpp
, uint32_t * vidp
)
4396 proc_t p
= current_proc();
4397 struct fileproc
*fp
;
4401 proc_fdlock_spin(p
);
4402 if ((error
= fp_lookup(p
, fd
, &fp
, 1))) {
4406 if (fp
->f_type
!= DTYPE_VNODE
) {
4407 fp_drop(p
, fd
, fp
, 1);
4411 vp
= (struct vnode
*)fp
->f_data
;
4416 if ((vidp
!= NULL
) && (vp
!= NULLVP
)) {
4417 *vidp
= (uint32_t)vp
->v_id
;
4429 * Description: Given an fd, look it up in the current process's per process
4430 * open file table, and return its internal socket pointer.
4432 * Parameters: fd fd to obtain vnode from
4433 * sp pointer to socket return area
4435 * Returns: 0 Success
4436 * ENOTSOCK Not a socket
4437 * fp_lookup:EBADF Bad file descriptor
4440 * *sp (modified) Returned socket pointer
4442 * Locks: This function internally takes and drops the proc_fdlock for
4443 * the current process
4445 * Notes: If successful, this function increments the f_iocount on the
4446 * fd's corresponding fileproc.
4448 * The fileproc referenced is not returned; because of this, care
4449 * must be taken to not drop the last reference (e.g. by closing
4450 * the file). This is inherently unsafe, since the reference may
4451 * not be recoverable from the socket, if there is a subsequent
4452 * close that destroys the associate fileproc. The caller should
4453 * therefore retain their own reference on the fileproc so that
4454 * the f_iocount can be dropped subsequently. Failure to do this
4455 * can result in the returned pointer immediately becoming invalid
4456 * following the call.
4458 * Use of this function is discouraged.
4461 file_socket(int fd
, struct socket
**sp
)
4463 proc_t p
= current_proc();
4464 struct fileproc
*fp
;
4467 proc_fdlock_spin(p
);
4468 if ((error
= fp_lookup(p
, fd
, &fp
, 1))) {
4472 if (fp
->f_type
!= DTYPE_SOCKET
) {
4473 fp_drop(p
, fd
, fp
, 1);
4477 *sp
= (struct socket
*)fp
->f_data
;
4487 * Description: Given an fd, look it up in the current process's per process
4488 * open file table, and return its fileproc's flags field.
4490 * Parameters: fd fd whose flags are to be
4492 * flags pointer to flags data area
4494 * Returns: 0 Success
4495 * ENOTSOCK Not a socket
4496 * fp_lookup:EBADF Bad file descriptor
4499 * *flags (modified) Returned flags field
4501 * Locks: This function internally takes and drops the proc_fdlock for
4502 * the current process
4504 * Notes: This function will internally increment and decrement the
4505 * f_iocount of the fileproc as part of its operation.
4508 file_flags(int fd
, int *flags
)
4510 proc_t p
= current_proc();
4511 struct fileproc
*fp
;
4514 proc_fdlock_spin(p
);
4515 if ((error
= fp_lookup(p
, fd
, &fp
, 1))) {
4519 *flags
= (int)fp
->f_flag
;
4520 fp_drop(p
, fd
, fp
, 1);
4530 * Description: Drop an iocount reference on an fd, and wake up any waiters
4531 * for draining (i.e. blocked in fileproc_drain() called during
4532 * the last attempt to close a file).
4534 * Parameters: fd fd on which an ioreference is
4537 * Returns: 0 Success
4538 * EBADF Bad file descriptor
4540 * Description: Given an fd, look it up in the current process's per process
4541 * open file table, and drop it's fileproc's f_iocount by one
4543 * Notes: This is intended as a corresponding operation to the functions
4544 * file_vnode() and file_socket() operations.
4546 * Technically, the close reference is supposed to be protected
4547 * by a fileproc_drain(), however, a drain will only block if
4548 * the fd refers to a character device, and that device has had
4549 * preparefileread() called on it. If it refers to something
4550 * other than a character device, then the drain will occur and
4551 * block each close attempt, rather than merely the last close.
4553 * Since it's possible for an fd that refers to a character
4554 * device to have an intermediate close followed by an open to
4555 * cause a different file to correspond to that descriptor,
4556 * unless there was a cautionary reference taken on the fileproc,
4557 * this is an inherently unsafe function. This happens in the
4558 * case where multiple fd's in a process refer to the same
4559 * character device (e.g. stdin/out/err pointing to a tty, etc.).
4561 * Use of this function is discouraged.
4566 struct fileproc
*fp
;
4567 proc_t p
= current_proc();
4570 proc_fdlock_spin(p
);
4571 if (fd
< 0 || fd
>= p
->p_fd
->fd_nfiles
||
4572 (fp
= p
->p_fd
->fd_ofiles
[fd
]) == NULL
||
4573 ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
4574 !(p
->p_fd
->fd_ofileflags
[fd
] & UF_CLOSING
))) {
4579 if (1 == os_ref_release_locked(&fp
->f_iocount
)) {
4580 if (fp
->f_flags
& FP_SELCONFLICT
) {
4581 fp
->f_flags
&= ~FP_SELCONFLICT
;
4584 if (p
->p_fpdrainwait
) {
4585 p
->p_fpdrainwait
= 0;
4592 wakeup(&p
->p_fpdrainwait
);
4598 static int falloc_withalloc_locked(proc_t
, struct fileproc
**, int *,
4599 vfs_context_t
, struct fileproc
* (*)(void *), void *, int);
4604 * Description: Allocate an entry in the per process open file table and
4605 * return the corresponding fileproc and fd.
4607 * Parameters: p The process in whose open file
4608 * table the fd is to be allocated
4609 * resultfp Pointer to fileproc pointer
4611 * resultfd Pointer to fd return area
4614 * Returns: 0 Success
4615 * falloc:ENFILE Too many open files in system
4616 * falloc:EMFILE Too many open files in process
4617 * falloc:ENOMEM M_FILEPROC or M_FILEGLOB zone
4621 * *resultfd (modified) Returned fileproc pointer
4622 * *resultfd (modified) Returned fd
4624 * Locks: This function takes and drops the proc_fdlock; if this lock
4625 * is already held, use falloc_locked() instead.
4627 * Notes: This function takes separate process and context arguments
4628 * solely to support kern_exec.c; otherwise, it would take
4629 * neither, and expect falloc_locked() to use the
4630 * vfs_context_current() routine internally.
4633 falloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
)
4635 return falloc_withalloc(p
, resultfp
, resultfd
, ctx
,
4636 fileproc_alloc_init
, NULL
);
4640 * Like falloc, but including the fileproc allocator and create-args
4643 falloc_withalloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4644 vfs_context_t ctx
, fp_allocfn_t fp_zalloc
, void *arg
)
4649 error
= falloc_withalloc_locked(p
,
4650 resultfp
, resultfd
, ctx
, fp_zalloc
, arg
, 1);
4657 * "uninitialized" ops -- ensure fg->fg_ops->fo_type always exists
4659 static const struct fileops uninitops
;
4664 * Create a new open file structure and allocate
4665 * a file descriptor for the process that refers to it.
4667 * Returns: 0 Success
4669 * Description: Allocate an entry in the per process open file table and
4670 * return the corresponding fileproc and fd.
4672 * Parameters: p The process in whose open file
4673 * table the fd is to be allocated
4674 * resultfp Pointer to fileproc pointer
4676 * resultfd Pointer to fd return area
4678 * locked Flag to indicate whether the
4679 * caller holds proc_fdlock
4681 * Returns: 0 Success
4682 * ENFILE Too many open files in system
4683 * fdalloc:EMFILE Too many open files in process
4684 * ENOMEM M_FILEPROC or M_FILEGLOB zone
4689 * *resultfd (modified) Returned fileproc pointer
4690 * *resultfd (modified) Returned fd
4692 * Locks: If the parameter 'locked' is zero, this function takes and
4693 * drops the proc_fdlock; if non-zero, the caller must hold the
4696 * Notes: If you intend to use a non-zero 'locked' parameter, use the
4697 * utility function falloc() instead.
4699 * This function takes separate process and context arguments
4700 * solely to support kern_exec.c; otherwise, it would take
4701 * neither, and use the vfs_context_current() routine internally.
4704 falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4705 vfs_context_t ctx
, int locked
)
4707 return falloc_withalloc_locked(p
, resultfp
, resultfd
, ctx
,
4708 fileproc_alloc_init
, NULL
, locked
);
4712 falloc_withalloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4713 vfs_context_t ctx
, fp_allocfn_t fp_zalloc
, void *crarg
,
4716 struct fileproc
*fp
;
4717 struct fileglob
*fg
;
4720 if (nfiles
>= maxfiles
) {
4729 if ((error
= fdalloc(p
, 0, &nfd
))) {
4737 error
= mac_file_check_create(proc_ucred(p
));
4747 * Allocate a new file descriptor.
4748 * If the process has file descriptor zero open, add to the list
4749 * of open files at that point, otherwise put it at the front of
4750 * the list of open files.
4754 fp
= (*fp_zalloc
)(crarg
);
4761 MALLOC_ZONE(fg
, struct fileglob
*, sizeof(struct fileglob
), M_FILEGLOB
, M_WAITOK
);
4769 bzero(fg
, sizeof(struct fileglob
));
4770 lck_mtx_init(&fg
->fg_lock
, file_lck_grp
, file_lck_attr
);
4772 os_ref_retain_locked(&fp
->f_iocount
);
4774 fg
->fg_ops
= &uninitops
;
4777 mac_file_label_init(fg
);
4780 kauth_cred_ref(ctx
->vc_ucred
);
4784 fp
->f_cred
= ctx
->vc_ucred
;
4787 mac_file_label_associate(fp
->f_cred
, fg
);
4790 OSAddAtomic(1, &nfiles
);
4792 p
->p_fd
->fd_ofiles
[nfd
] = fp
;
4812 * Description: Free a file structure; drop the global open file count, and
4813 * drop the credential reference, if the fileglob has one, and
4814 * destroy the instance mutex before freeing
4816 * Parameters: fg Pointer to fileglob to be
4822 fg_free(struct fileglob
*fg
)
4824 OSAddAtomic(-1, &nfiles
);
4826 if (fg
->fg_vn_data
) {
4827 fg_vn_data_free(fg
->fg_vn_data
);
4828 fg
->fg_vn_data
= NULL
;
4831 if (IS_VALID_CRED(fg
->fg_cred
)) {
4832 kauth_cred_unref(&fg
->fg_cred
);
4834 lck_mtx_destroy(&fg
->fg_lock
, file_lck_grp
);
4837 mac_file_label_destroy(fg
);
4839 FREE_ZONE(fg
, sizeof *fg
, M_FILEGLOB
);
4846 * Description: Return vnode associated with the file structure, if
4847 * any. The lifetime of the returned vnode is bound to
4848 * the lifetime of the file structure.
4850 * Parameters: fg Pointer to fileglob to
4856 fg_get_vnode(struct fileglob
*fg
)
4858 if (FILEGLOB_DTYPE(fg
) == DTYPE_VNODE
) {
4859 return (vnode_t
)fg
->fg_data
;
4868 * Description: Perform close-on-exec processing for all files in a process
4869 * that are either marked as close-on-exec, or which were in the
4870 * process of being opened at the time of the execve
4872 * Also handles the case (via posix_spawn()) where -all-
4873 * files except those marked with "inherit" as treated as
4876 * Parameters: p Pointer to process calling
4881 * Locks: This function internally takes and drops proc_fdlock()
4882 * But assumes tables don't grow/change while unlocked.
4886 fdexec(proc_t p
, short flags
, int self_exec
)
4888 struct filedesc
*fdp
= p
->p_fd
;
4890 boolean_t cloexec_default
= (flags
& POSIX_SPAWN_CLOEXEC_DEFAULT
) != 0;
4891 thread_t self
= current_thread();
4892 struct uthread
*ut
= get_bsdthread_info(self
);
4893 struct kqworkq
*dealloc_kqwq
= NULL
;
4896 * If the current thread is bound as a workq/workloop
4897 * servicing thread, we need to unbind it first.
4899 if (ut
->uu_kqr_bound
&& self_exec
) {
4900 kqueue_threadreq_unbind(p
, ut
->uu_kqr_bound
);
4906 * Deallocate the knotes for this process
4907 * and mark the tables non-existent so
4908 * subsequent kqueue closes go faster.
4911 assert(fdp
->fd_knlistsize
== 0);
4912 assert(fdp
->fd_knhashmask
== 0);
4914 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
4915 struct fileproc
*fp
= fdp
->fd_ofiles
[i
];
4916 char *flagp
= &fdp
->fd_ofileflags
[i
];
4918 if (fp
&& cloexec_default
) {
4920 * Reverse the usual semantics of file descriptor
4921 * inheritance - all of them should be closed
4922 * except files marked explicitly as "inherit" and
4923 * not marked close-on-exec.
4925 if ((*flagp
& (UF_EXCLOSE
| UF_INHERIT
)) != UF_INHERIT
) {
4926 *flagp
|= UF_EXCLOSE
;
4928 *flagp
&= ~UF_INHERIT
;
4932 ((*flagp
& (UF_RESERVED
| UF_EXCLOSE
)) == UF_EXCLOSE
)
4934 || (fp
&& mac_file_check_inherit(proc_ucred(p
), fp
->f_fglob
))
4937 procfdtbl_clearfd(p
, i
);
4938 if (i
== fdp
->fd_lastfile
&& i
> 0) {
4941 if (i
< fdp
->fd_freefile
) {
4942 fdp
->fd_freefile
= i
;
4946 * Wait for any third party viewers (e.g., lsof)
4947 * to release their references to this fileproc.
4949 while (os_ref_get_count(&fp
->f_iocount
) > 1) {
4950 p
->p_fpdrainwait
= 1;
4951 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
,
4954 if (fp
->f_flags
& FP_WAITEVENT
) {
4955 (void)waitevent_close(p
, fp
);
4957 closef_locked(fp
, fp
->f_fglob
, p
);
4963 /* release the per-process workq kq */
4964 if (fdp
->fd_wqkqueue
) {
4965 dealloc_kqwq
= fdp
->fd_wqkqueue
;
4966 fdp
->fd_wqkqueue
= NULL
;
4971 /* Anything to free? */
4973 kqworkq_dealloc(dealloc_kqwq
);
4981 * Description: Copy a filedesc structure. This is normally used as part of
4982 * forkproc() when forking a new process, to copy the per process
4983 * open file table over to the new process.
4985 * Parameters: p Process whose open file table
4986 * is to be copied (parent)
4987 * uth_cdir Per thread current working
4988 * cirectory, or NULL
4990 * Returns: NULL Copy failed
4991 * !NULL Pointer to new struct filedesc
4993 * Locks: This function internally takes and drops proc_fdlock()
4995 * Notes: Files are copied directly, ignoring the new resource limits
4996 * for the process that's being copied into. Since the descriptor
4997 * references are just additional references, this does not count
4998 * against the number of open files on the system.
5000 * The struct filedesc includes the current working directory,
5001 * and the current root directory, if the process is chroot'ed.
5003 * If the exec was called by a thread using a per thread current
5004 * working directory, we inherit the working directory from the
5005 * thread making the call, rather than from the process.
5007 * In the case of a failure to obtain a reference, for most cases,
5008 * the file entry will be silently dropped. There's an exception
5009 * for the case of a chroot dir, since a failure to to obtain a
5010 * reference there would constitute an "escape" from the chroot
5011 * environment, which must not be allowed. In that case, we will
5012 * deny the execve() operation, rather than allowing the escape.
5015 fdcopy(proc_t p
, vnode_t uth_cdir
)
5017 struct filedesc
*newfdp
, *fdp
= p
->p_fd
;
5019 struct fileproc
*ofp
, *fp
;
5022 MALLOC_ZONE(newfdp
, struct filedesc
*,
5023 sizeof(*newfdp
), M_FILEDESC
, M_WAITOK
);
5024 if (newfdp
== NULL
) {
5031 * the FD_CHROOT flag will be inherited via this copy
5033 (void) memcpy(newfdp
, fdp
, sizeof(*newfdp
));
5036 * If we are running with per-thread current working directories,
5037 * inherit the new current working directory from the current thread
5038 * instead, before we take our references.
5040 if (uth_cdir
!= NULLVP
) {
5041 newfdp
->fd_cdir
= uth_cdir
;
5045 * For both fd_cdir and fd_rdir make sure we get
5046 * a valid reference... if we can't, than set
5047 * set the pointer(s) to NULL in the child... this
5048 * will keep us from using a non-referenced vp
5049 * and allows us to do the vnode_rele only on
5050 * a properly referenced vp
5052 if ((v_dir
= newfdp
->fd_cdir
)) {
5053 if (vnode_getwithref(v_dir
) == 0) {
5054 if ((vnode_ref(v_dir
))) {
5055 newfdp
->fd_cdir
= NULL
;
5059 newfdp
->fd_cdir
= NULL
;
5062 if (newfdp
->fd_cdir
== NULL
&& fdp
->fd_cdir
) {
5064 * we couldn't get a new reference on
5065 * the current working directory being
5066 * inherited... we might as well drop
5067 * our reference from the parent also
5068 * since the vnode has gone DEAD making
5069 * it useless... by dropping it we'll
5070 * be that much closer to recycling it
5072 vnode_rele(fdp
->fd_cdir
);
5073 fdp
->fd_cdir
= NULL
;
5076 if ((v_dir
= newfdp
->fd_rdir
)) {
5077 if (vnode_getwithref(v_dir
) == 0) {
5078 if ((vnode_ref(v_dir
))) {
5079 newfdp
->fd_rdir
= NULL
;
5083 newfdp
->fd_rdir
= NULL
;
5086 /* Coming from a chroot environment and unable to get a reference... */
5087 if (newfdp
->fd_rdir
== NULL
&& fdp
->fd_rdir
) {
5090 * We couldn't get a new reference on
5091 * the chroot directory being
5092 * inherited... this is fatal, since
5093 * otherwise it would constitute an
5094 * escape from a chroot environment by
5097 if (newfdp
->fd_cdir
) {
5098 vnode_rele(newfdp
->fd_cdir
);
5100 FREE_ZONE(newfdp
, sizeof *newfdp
, M_FILEDESC
);
5105 * If the number of open files fits in the internal arrays
5106 * of the open file structure, use them, otherwise allocate
5107 * additional memory for the number of descriptors currently
5110 if (newfdp
->fd_lastfile
< NDFILE
) {
5114 * Compute the smallest multiple of NDEXTENT needed
5115 * for the file descriptors currently in use,
5116 * allowing the table to shrink.
5118 i
= newfdp
->fd_nfiles
;
5119 while (i
> 1 + 2 * NDEXTENT
&& i
> 1 + newfdp
->fd_lastfile
* 2) {
5125 MALLOC_ZONE(newfdp
->fd_ofiles
, struct fileproc
**,
5126 i
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
5127 if (newfdp
->fd_ofiles
== NULL
) {
5128 if (newfdp
->fd_cdir
) {
5129 vnode_rele(newfdp
->fd_cdir
);
5131 if (newfdp
->fd_rdir
) {
5132 vnode_rele(newfdp
->fd_rdir
);
5135 FREE_ZONE(newfdp
, sizeof(*newfdp
), M_FILEDESC
);
5138 (void) memset(newfdp
->fd_ofiles
, 0, i
* OFILESIZE
);
5141 newfdp
->fd_ofileflags
= (char *) &newfdp
->fd_ofiles
[i
];
5142 newfdp
->fd_nfiles
= i
;
5144 if (fdp
->fd_nfiles
> 0) {
5145 struct fileproc
**fpp
;
5148 (void) memcpy(newfdp
->fd_ofiles
, fdp
->fd_ofiles
,
5149 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofiles
));
5150 (void) memcpy(newfdp
->fd_ofileflags
, fdp
->fd_ofileflags
,
5151 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofileflags
));
5153 fpp
= &newfdp
->fd_ofiles
[newfdp
->fd_lastfile
];
5154 flags
= &newfdp
->fd_ofileflags
[newfdp
->fd_lastfile
];
5155 for (i
= newfdp
->fd_lastfile
; i
>= 0; i
--, fpp
--, flags
--) {
5156 if ((ofp
= *fpp
) != NULL
&&
5157 0 == (ofp
->f_fglob
->fg_lflags
& FG_CONFINED
) &&
5158 0 == (*flags
& (UF_FORKCLOSE
| UF_RESERVED
))) {
5160 if (FILEPROC_TYPE(ofp
) != FTYPE_SIMPLE
) {
5161 panic("complex fileproc");
5164 fp
= fileproc_alloc_init(NULL
);
5167 * XXX no room to copy, unable to
5168 * XXX safely unwind state at present
5173 (ofp
->f_flags
& ~FP_TYPEMASK
);
5174 fp
->f_fglob
= ofp
->f_fglob
;
5183 if (i
== newfdp
->fd_lastfile
&& i
> 0) {
5184 newfdp
->fd_lastfile
--;
5186 if (i
< newfdp
->fd_freefile
) {
5187 newfdp
->fd_freefile
= i
;
5196 * Initialize knote and kqueue tracking structs
5198 newfdp
->fd_knlist
= NULL
;
5199 newfdp
->fd_knlistsize
= 0;
5200 newfdp
->fd_knhash
= NULL
;
5201 newfdp
->fd_knhashmask
= 0;
5202 newfdp
->fd_kqhash
= NULL
;
5203 newfdp
->fd_kqhashmask
= 0;
5204 newfdp
->fd_wqkqueue
= NULL
;
5205 lck_mtx_init(&newfdp
->fd_kqhashlock
, proc_kqhashlock_grp
, proc_lck_attr
);
5206 lck_mtx_init(&newfdp
->fd_knhashlock
, proc_knhashlock_grp
, proc_lck_attr
);
5215 * Description: Release a filedesc (per process open file table) structure;
5216 * this is done on process exit(), or from forkproc_free() if
5217 * the fork fails for some reason subsequent to a successful
5220 * Parameters: p Pointer to process going away
5224 * Locks: This function internally takes and drops proc_fdlock()
5229 struct filedesc
*fdp
;
5230 struct fileproc
*fp
;
5231 struct kqworkq
*dealloc_kqwq
= NULL
;
5236 if (p
== kernproc
|| NULL
== (fdp
= p
->p_fd
)) {
5241 extern struct filedesc filedesc0
;
5243 if (&filedesc0
== fdp
) {
5248 * deallocate all the knotes up front and claim empty
5249 * tables to make any subsequent kqueue closes faster.
5252 assert(fdp
->fd_knlistsize
== 0);
5253 assert(fdp
->fd_knhashmask
== 0);
5256 * dealloc all workloops that have outstanding retains
5257 * when created with scheduling parameters.
5259 kqworkloops_dealloc(p
);
5261 /* close file descriptors */
5262 if (fdp
->fd_nfiles
> 0 && fdp
->fd_ofiles
) {
5263 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
5264 if ((fp
= fdp
->fd_ofiles
[i
]) != NULL
) {
5265 if (fdp
->fd_ofileflags
[i
] & UF_RESERVED
) {
5266 panic("fdfree: found fp with UF_RESERVED");
5269 fileproc_drain(p
, fp
);
5270 procfdtbl_reservefd(p
, i
);
5272 if (fp
->f_flags
& FP_WAITEVENT
) {
5273 (void)waitevent_close(p
, fp
);
5275 (void) closef_locked(fp
, fp
->f_fglob
, p
);
5279 FREE_ZONE(fdp
->fd_ofiles
, fdp
->fd_nfiles
* OFILESIZE
, M_OFILETABL
);
5280 fdp
->fd_ofiles
= NULL
;
5284 if (fdp
->fd_wqkqueue
) {
5285 dealloc_kqwq
= fdp
->fd_wqkqueue
;
5286 fdp
->fd_wqkqueue
= NULL
;
5292 kqworkq_dealloc(dealloc_kqwq
);
5295 vnode_rele(fdp
->fd_cdir
);
5298 vnode_rele(fdp
->fd_rdir
);
5301 proc_fdlock_spin(p
);
5305 if (fdp
->fd_kqhash
) {
5306 for (uint32_t j
= 0; j
<= fdp
->fd_kqhashmask
; j
++) {
5307 assert(LIST_EMPTY(&fdp
->fd_kqhash
[j
]));
5309 FREE(fdp
->fd_kqhash
, M_KQUEUE
);
5312 lck_mtx_destroy(&fdp
->fd_kqhashlock
, proc_kqhashlock_grp
);
5313 lck_mtx_destroy(&fdp
->fd_knhashlock
, proc_knhashlock_grp
);
5315 FREE_ZONE(fdp
, sizeof(*fdp
), M_FILEDESC
);
5321 * Description: Internal form of closef; called with proc_fdlock held
5323 * Parameters: fp Pointer to fileproc for fd
5324 * fg Pointer to fileglob for fd
5325 * p Pointer to proc structure
5327 * Returns: 0 Success
5328 * closef_finish:??? Anything returnable by a per-fileops
5331 * Note: Decrements reference count on file structure; if this was the
5332 * last reference, then closef_finish() is called
5334 * p and fp are allowed to be NULL when closing a file that was
5335 * being passed in a message (but only if we are called when this
5336 * is NOT the last reference).
5339 closef_locked(struct fileproc
*fp
, struct fileglob
*fg
, proc_t p
)
5343 struct vfs_context context
;
5350 /* Set up context with cred stashed in fg */
5351 if (p
== current_proc()) {
5352 context
.vc_thread
= current_thread();
5354 context
.vc_thread
= NULL
;
5356 context
.vc_ucred
= fg
->fg_cred
;
5359 * POSIX record locking dictates that any close releases ALL
5360 * locks owned by this process. This is handled by setting
5361 * a flag in the unlock to free ONLY locks obeying POSIX
5362 * semantics, and not to free BSD-style file locks.
5363 * If the descriptor was in a message, POSIX-style locks
5364 * aren't passed with the descriptor.
5366 if (p
&& (p
->p_ladvflag
& P_LADVLOCK
) &&
5367 DTYPE_VNODE
== FILEGLOB_DTYPE(fg
)) {
5370 lf
.l_whence
= SEEK_SET
;
5373 lf
.l_type
= F_UNLCK
;
5374 vp
= (struct vnode
*)fg
->fg_data
;
5376 if ((error
= vnode_getwithref(vp
)) == 0) {
5377 (void) VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &lf
, F_POSIX
, &context
, NULL
);
5378 (void)vnode_put(vp
);
5382 lck_mtx_lock_spin(&fg
->fg_lock
);
5385 if (fg
->fg_count
> 0) {
5386 lck_mtx_unlock(&fg
->fg_lock
);
5390 if (fg
->fg_count
!= 0) {
5391 panic("fg %p: being freed with bad fg_count (%d)", fg
, fg
->fg_count
);
5395 if (fp
&& (fp
->f_flags
& FP_WRITTEN
)) {
5396 fg
->fg_flag
|= FWASWRITTEN
;
5399 fg
->fg_lflags
|= FG_TERM
;
5400 lck_mtx_unlock(&fg
->fg_lock
);
5406 /* Since we ensure that fg->fg_ops is always initialized,
5407 * it is safe to invoke fo_close on the fg */
5408 error
= fo_close(fg
, &context
);
5423 * Description: Drain out pending I/O operations
5425 * Parameters: p Process closing this file
5426 * fp fileproc struct for the open
5427 * instance on the file
5431 * Locks: Assumes the caller holds the proc_fdlock
5433 * Notes: For character devices, this occurs on the last close of the
5434 * device; for all other file descriptors, this occurs on each
5435 * close to prevent fd's from being closed out from under
5436 * operations currently in progress and blocked
5438 * See Also: file_vnode(), file_socket(), file_drop(), and the cautions
5439 * regarding their use and interaction with this function.
5442 fileproc_drain(proc_t p
, struct fileproc
* fp
)
5444 struct vfs_context context
;
5446 context
.vc_thread
= proc_thread(p
); /* XXX */
5447 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
5449 /* Set the vflag for drain */
5450 fileproc_modify_vflags(fp
, FPV_DRAIN
, FALSE
);
5452 while (os_ref_get_count(&fp
->f_iocount
) > 1) {
5453 lck_mtx_convert_spin(&p
->p_fdmlock
);
5455 fo_drain(fp
, &context
);
5456 if ((fp
->f_flags
& FP_INSELECT
) == FP_INSELECT
) {
5457 if (waitq_wakeup64_all((struct waitq
*)fp
->f_wset
, NO_EVENT64
,
5458 THREAD_INTERRUPTED
, WAITQ_ALL_PRIORITIES
) == KERN_INVALID_ARGUMENT
) {
5459 panic("bad wait queue for waitq_wakeup64_all %p (fp:%p)", fp
->f_wset
, fp
);
5462 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
) {
5463 if (waitq_wakeup64_all(&select_conflict_queue
, NO_EVENT64
,
5464 THREAD_INTERRUPTED
, WAITQ_ALL_PRIORITIES
) == KERN_INVALID_ARGUMENT
) {
5465 panic("bad select_conflict_queue");
5468 p
->p_fpdrainwait
= 1;
5470 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
, "fpdrain", NULL
);
5473 if ((fp
->f_flags
& FP_INSELECT
) != 0) {
5474 panic("FP_INSELECT set on drained fp");
5477 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
) {
5478 fp
->f_flags
&= ~FP_SELCONFLICT
;
5486 * Description: Release the fd and free the fileproc associated with the fd
5487 * in the per process open file table of the specified process;
5488 * these values must correspond.
5490 * Parameters: p Process containing fd
5491 * fd fd to be released
5492 * fp fileproc to be freed
5495 fp_free(proc_t p
, int fd
, struct fileproc
* fp
)
5497 proc_fdlock_spin(p
);
5501 fg_free(fp
->f_fglob
);
5502 os_ref_release_live(&fp
->f_iocount
);
5510 * Description: Apply an advisory lock on a file descriptor.
5512 * Parameters: p Process making request
5513 * uap->fd fd on which the lock is to be
5515 * uap->how (Un)Lock bits, including type
5516 * retval Pointer to the call return area
5518 * Returns: 0 Success
5519 * fp_getfvp:EBADF Bad file descriptor
5520 * fp_getfvp:ENOTSUP fd does not refer to a vnode
5521 * vnode_getwithref:???
5525 * *retval (modified) Size of dtable
5527 * Notes: Just attempt to get a record lock of the requested type on
5528 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
5531 flock(proc_t p
, struct flock_args
*uap
, __unused
int32_t *retval
)
5535 struct fileproc
*fp
;
5538 vfs_context_t ctx
= vfs_context_current();
5541 AUDIT_ARG(fd
, uap
->fd
);
5542 if ((error
= fp_getfvp(p
, fd
, &fp
, &vp
))) {
5545 if ((error
= vnode_getwithref(vp
))) {
5548 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
5550 lf
.l_whence
= SEEK_SET
;
5553 if (how
& LOCK_UN
) {
5554 lf
.l_type
= F_UNLCK
;
5555 fp
->f_flag
&= ~FHASLOCK
;
5556 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_UNLCK
, &lf
, F_FLOCK
, ctx
, NULL
);
5559 if (how
& LOCK_EX
) {
5560 lf
.l_type
= F_WRLCK
;
5561 } else if (how
& LOCK_SH
) {
5562 lf
.l_type
= F_RDLCK
;
5568 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
, F_SETLK
, &lf
);
5573 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
,
5574 (how
& LOCK_NB
? F_FLOCK
: F_FLOCK
| F_WAIT
),
5577 fp
->f_flag
|= FHASLOCK
;
5580 (void)vnode_put(vp
);
5582 fp_drop(p
, fd
, fp
, 0);
5589 * Description: Obtain a Mach send right for a given file descriptor.
5591 * Parameters: p Process calling fileport
5592 * uap->fd The fd to reference
5593 * uap->portnamep User address at which to place port name.
5595 * Returns: 0 Success.
5596 * EBADF Bad file descriptor.
5597 * EINVAL File descriptor had type that cannot be sent, misc. other errors.
5598 * EFAULT Address at which to store port name is not valid.
5599 * EAGAIN Resource shortage.
5602 * On success, name of send right is stored at user-specified address.
5605 fileport_makeport(proc_t p
, struct fileport_makeport_args
*uap
,
5606 __unused
int *retval
)
5610 user_addr_t user_portaddr
= uap
->portnamep
;
5611 struct fileproc
*fp
= FILEPROC_NULL
;
5612 struct fileglob
*fg
= NULL
;
5613 ipc_port_t fileport
;
5614 mach_port_name_t name
= MACH_PORT_NULL
;
5617 err
= fp_lookup(p
, fd
, &fp
, 1);
5622 if (!file_issendable(p
, fp
)) {
5627 if (FP_ISGUARDED(fp
, GUARD_FILEPORT
)) {
5628 err
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_FILEPORT
);
5632 /* Dropped when port is deallocated */
5638 /* Allocate and initialize a port */
5639 fileport
= fileport_alloc(fg
);
5640 if (fileport
== IPC_PORT_NULL
) {
5646 /* Add an entry. Deallocates port on failure. */
5647 name
= ipc_port_copyout_send(fileport
, get_task_ipcspace(p
->task
));
5648 if (!MACH_PORT_VALID(name
)) {
5653 err
= copyout(&name
, user_portaddr
, sizeof(mach_port_name_t
));
5658 /* Tag the fileglob for debugging purposes */
5659 lck_mtx_lock_spin(&fg
->fg_lock
);
5660 fg
->fg_lflags
|= FG_PORTMADE
;
5661 lck_mtx_unlock(&fg
->fg_lock
);
5663 fp_drop(p
, fd
, fp
, 0);
5670 if (MACH_PORT_VALID(name
)) {
5671 /* Don't care if another thread races us to deallocate the entry */
5672 (void) mach_port_deallocate(get_task_ipcspace(p
->task
), name
);
5675 if (fp
!= FILEPROC_NULL
) {
5676 fp_drop(p
, fd
, fp
, 0);
5683 fileport_releasefg(struct fileglob
*fg
)
5685 (void)closef_locked(NULL
, fg
, PROC_NULL
);
5691 * fileport_makefd_internal
5693 * Description: Obtain the file descriptor for a given Mach send right.
5695 * Returns: 0 Success
5696 * EINVAL Invalid Mach port name, or port is not for a file.
5698 * fdalloc:ENOMEM Unable to allocate fileproc or extend file table.
5701 * *retval (modified) The new descriptor
5704 fileport_makefd_internal(proc_t p
, ipc_port_t port
, int uf_flags
, int *retval
)
5706 struct fileglob
*fg
;
5707 struct fileproc
*fp
= FILEPROC_NULL
;
5711 fg
= fileport_port_to_fileglob(port
);
5717 fp
= fileproc_alloc_init(NULL
);
5718 if (fp
== FILEPROC_NULL
) {
5727 err
= fdalloc(p
, 0, &fd
);
5734 *fdflags(p
, fd
) |= uf_flags
;
5737 procfdtbl_releasefd(p
, fd
, fp
);
5743 if ((fp
!= NULL
) && (0 != err
)) {
5753 * Description: Obtain the file descriptor for a given Mach send right.
5755 * Parameters: p Process calling fileport
5756 * uap->port Name of send right to file port.
5758 * Returns: 0 Success
5759 * EINVAL Invalid Mach port name, or port is not for a file.
5761 * fdalloc:ENOMEM Unable to allocate fileproc or extend file table.
5764 * *retval (modified) The new descriptor
5767 fileport_makefd(proc_t p
, struct fileport_makefd_args
*uap
, int32_t *retval
)
5769 ipc_port_t port
= IPC_PORT_NULL
;
5770 mach_port_name_t send
= uap
->port
;
5774 res
= ipc_object_copyin(get_task_ipcspace(p
->task
),
5775 send
, MACH_MSG_TYPE_COPY_SEND
, &port
, 0, NULL
, IPC_KMSG_FLAGS_ALLOW_IMMOVABLE_SEND
);
5777 if (res
== KERN_SUCCESS
) {
5778 err
= fileport_makefd_internal(p
, port
, UF_EXCLOSE
, retval
);
5783 if (IPC_PORT_NULL
!= port
) {
5784 ipc_port_release_send(port
);
5794 * Description: Duplicate the specified descriptor to a free descriptor;
5795 * this is the second half of fdopen(), above.
5797 * Parameters: fdp filedesc pointer to fill in
5799 * dfd fd to dup from
5800 * mode mode to set on new fd
5801 * error command code
5803 * Returns: 0 Success
5804 * EBADF Source fd is bad
5805 * EACCES Requested mode not allowed
5806 * !0 'error', if not ENODEV or
5809 * Notes: XXX This is not thread safe; see fdopen() above
5812 dupfdopen(struct filedesc
*fdp
, int indx
, int dfd
, int flags
, int error
)
5814 struct fileproc
*wfp
;
5815 struct fileproc
*fp
;
5819 proc_t p
= current_proc();
5822 * If the to-be-dup'd fd number is greater than the allowed number
5823 * of file descriptors, or the fd to be dup'd has already been
5824 * closed, reject. Note, check for new == old is necessary as
5825 * falloc could allocate an already closed to-be-dup'd descriptor
5826 * as the new descriptor.
5830 fp
= fdp
->fd_ofiles
[indx
];
5831 if (dfd
< 0 || dfd
>= fdp
->fd_nfiles
||
5832 (wfp
= fdp
->fd_ofiles
[dfd
]) == NULL
|| wfp
== fp
||
5833 (fdp
->fd_ofileflags
[dfd
] & UF_RESERVED
)) {
5838 myerror
= mac_file_check_dup(proc_ucred(p
), wfp
->f_fglob
, dfd
);
5845 * There are two cases of interest here.
5847 * For ENODEV simply dup (dfd) to file descriptor
5848 * (indx) and return.
5850 * For ENXIO steal away the file structure from (dfd) and
5851 * store it in (indx). (dfd) is effectively closed by
5854 * Any other error code is just returned.
5858 if (FP_ISGUARDED(wfp
, GUARD_DUP
)) {
5864 * Check that the mode the file is being opened for is a
5865 * subset of the mode of the existing descriptor.
5867 if (((flags
& (FREAD
| FWRITE
)) | wfp
->f_flag
) != wfp
->f_flag
) {
5871 if (indx
> fdp
->fd_lastfile
) {
5872 fdp
->fd_lastfile
= indx
;
5877 fg_free(fp
->f_fglob
);
5879 fp
->f_fglob
= wfp
->f_fglob
;
5881 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
] |
5882 (flags
& O_CLOEXEC
) ? UF_EXCLOSE
: 0;
5898 * Description: Add a reference to a fileglob by fileproc
5900 * Parameters: fp fileproc containing fileglob
5905 * Notes: XXX Should use OSAddAtomic?
5908 fg_ref(struct fileproc
* fp
)
5910 struct fileglob
*fg
;
5914 lck_mtx_lock_spin(&fg
->fg_lock
);
5917 if ((fp
->f_flags
& ~((unsigned int)FP_VALID_FLAGS
)) != 0) {
5918 panic("fg_ref: invalid bits on fp %p", fp
);
5921 if (fg
->fg_count
== 0) {
5922 panic("fg_ref: adding fgcount to zeroed fg: fp %p fg %p",
5927 lck_mtx_unlock(&fg
->fg_lock
);
5934 * Description: Remove a reference to a fileglob by fileproc
5936 * Parameters: fp fileproc containing fileglob
5941 * Notes: XXX Should use OSAddAtomic?
5944 fg_drop(struct fileproc
* fp
)
5946 struct fileglob
*fg
;
5949 lck_mtx_lock_spin(&fg
->fg_lock
);
5951 lck_mtx_unlock(&fg
->fg_lock
);
5956 * fg_insertuipc_mark
5958 * Description: Mark fileglob for insertion onto message queue if needed
5959 * Also takes fileglob reference
5961 * Parameters: fg Fileglob pointer to insert
5963 * Returns: true, if the fileglob needs to be inserted onto msg queue
5965 * Locks: Takes and drops fg_lock, potentially many times
5968 fg_insertuipc_mark(struct fileglob
* fg
)
5970 boolean_t insert
= FALSE
;
5972 lck_mtx_lock_spin(&fg
->fg_lock
);
5973 while (fg
->fg_lflags
& FG_RMMSGQ
) {
5974 lck_mtx_convert_spin(&fg
->fg_lock
);
5976 fg
->fg_lflags
|= FG_WRMMSGQ
;
5977 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_insertuipc", NULL
);
5982 if (fg
->fg_msgcount
== 1) {
5983 fg
->fg_lflags
|= FG_INSMSGQ
;
5986 lck_mtx_unlock(&fg
->fg_lock
);
5993 * Description: Insert marked fileglob onto message queue
5995 * Parameters: fg Fileglob pointer to insert
5999 * Locks: Takes and drops fg_lock & uipc_lock
6000 * DO NOT call this function with proc_fdlock held as unp_gc()
6001 * can potentially try to acquire proc_fdlock, which can result
6002 * in a deadlock if this function is in unp_gc_wait().
6005 fg_insertuipc(struct fileglob
* fg
)
6007 if (fg
->fg_lflags
& FG_INSMSGQ
) {
6008 lck_mtx_lock_spin(uipc_lock
);
6010 LIST_INSERT_HEAD(&fmsghead
, fg
, f_msglist
);
6011 lck_mtx_unlock(uipc_lock
);
6012 lck_mtx_lock(&fg
->fg_lock
);
6013 fg
->fg_lflags
&= ~FG_INSMSGQ
;
6014 if (fg
->fg_lflags
& FG_WINSMSGQ
) {
6015 fg
->fg_lflags
&= ~FG_WINSMSGQ
;
6016 wakeup(&fg
->fg_lflags
);
6018 lck_mtx_unlock(&fg
->fg_lock
);
6023 * fg_removeuipc_mark
6025 * Description: Mark the fileglob for removal from message queue if needed
6026 * Also releases fileglob message queue reference
6028 * Parameters: fg Fileglob pointer to remove
6030 * Returns: true, if the fileglob needs to be removed from msg queue
6032 * Locks: Takes and drops fg_lock, potentially many times
6035 fg_removeuipc_mark(struct fileglob
* fg
)
6037 boolean_t remove
= FALSE
;
6039 lck_mtx_lock_spin(&fg
->fg_lock
);
6040 while (fg
->fg_lflags
& FG_INSMSGQ
) {
6041 lck_mtx_convert_spin(&fg
->fg_lock
);
6043 fg
->fg_lflags
|= FG_WINSMSGQ
;
6044 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_removeuipc", NULL
);
6047 if (fg
->fg_msgcount
== 0) {
6048 fg
->fg_lflags
|= FG_RMMSGQ
;
6051 lck_mtx_unlock(&fg
->fg_lock
);
6058 * Description: Remove marked fileglob from message queue
6060 * Parameters: fg Fileglob pointer to remove
6064 * Locks: Takes and drops fg_lock & uipc_lock
6065 * DO NOT call this function with proc_fdlock held as unp_gc()
6066 * can potentially try to acquire proc_fdlock, which can result
6067 * in a deadlock if this function is in unp_gc_wait().
6070 fg_removeuipc(struct fileglob
* fg
)
6072 if (fg
->fg_lflags
& FG_RMMSGQ
) {
6073 lck_mtx_lock_spin(uipc_lock
);
6075 LIST_REMOVE(fg
, f_msglist
);
6076 lck_mtx_unlock(uipc_lock
);
6077 lck_mtx_lock(&fg
->fg_lock
);
6078 fg
->fg_lflags
&= ~FG_RMMSGQ
;
6079 if (fg
->fg_lflags
& FG_WRMMSGQ
) {
6080 fg
->fg_lflags
&= ~FG_WRMMSGQ
;
6081 wakeup(&fg
->fg_lflags
);
6083 lck_mtx_unlock(&fg
->fg_lock
);
6086 #endif /* SOCKETS */
6091 * Description: Generic fileops read indirected through the fileops pointer
6092 * in the fileproc structure
6094 * Parameters: fp fileproc structure pointer
6095 * uio user I/O structure pointer
6097 * ctx VFS context for operation
6099 * Returns: 0 Success
6100 * !0 Errno from read
6103 fo_read(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
6105 return (*fp
->f_ops
->fo_read
)(fp
, uio
, flags
, ctx
);
6109 fo_no_read(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
6111 #pragma unused(fp, uio, flags, ctx)
6119 * Description: Generic fileops write indirected through the fileops pointer
6120 * in the fileproc structure
6122 * Parameters: fp fileproc structure pointer
6123 * uio user I/O structure pointer
6125 * ctx VFS context for operation
6127 * Returns: 0 Success
6128 * !0 Errno from write
6131 fo_write(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
6133 return (*fp
->f_ops
->fo_write
)(fp
, uio
, flags
, ctx
);
6137 fo_no_write(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
6139 #pragma unused(fp, uio, flags, ctx)
6147 * Description: Generic fileops ioctl indirected through the fileops pointer
6148 * in the fileproc structure
6150 * Parameters: fp fileproc structure pointer
6152 * data pointer to internalized copy
6153 * of user space ioctl command
6154 * parameter data in kernel space
6155 * ctx VFS context for operation
6157 * Returns: 0 Success
6158 * !0 Errno from ioctl
6160 * Locks: The caller is assumed to have held the proc_fdlock; this
6161 * function releases and reacquires this lock. If the caller
6162 * accesses data protected by this lock prior to calling this
6163 * function, it will need to revalidate/reacquire any cached
6164 * protected data obtained prior to the call.
6167 fo_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
, vfs_context_t ctx
)
6171 proc_fdunlock(vfs_context_proc(ctx
));
6172 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, com
, data
, ctx
);
6173 proc_fdlock(vfs_context_proc(ctx
));
6178 fo_no_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
, vfs_context_t ctx
)
6180 #pragma unused(fp, com, data, ctx)
6188 * Description: Generic fileops select indirected through the fileops pointer
6189 * in the fileproc structure
6191 * Parameters: fp fileproc structure pointer
6192 * which select which
6193 * wql pointer to wait queue list
6194 * ctx VFS context for operation
6196 * Returns: 0 Success
6197 * !0 Errno from select
6200 fo_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
6202 return (*fp
->f_ops
->fo_select
)(fp
, which
, wql
, ctx
);
6206 fo_no_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
6208 #pragma unused(fp, which, wql, ctx)
6216 * Description: Generic fileops close indirected through the fileops pointer
6217 * in the fileproc structure
6219 * Parameters: fp fileproc structure pointer for
6221 * ctx VFS context for operation
6223 * Returns: 0 Success
6224 * !0 Errno from close
6227 fo_close(struct fileglob
*fg
, vfs_context_t ctx
)
6229 return (*fg
->fg_ops
->fo_close
)(fg
, ctx
);
6236 * Description: Generic fileops kqueue filter indirected through the fileops
6237 * pointer in the fileproc structure
6239 * Parameters: fp fileproc structure pointer
6240 * ctx VFS context for operation
6242 * Returns: 0 Success
6243 * !0 errno from drain
6246 fo_drain(struct fileproc
*fp
, vfs_context_t ctx
)
6248 return (*fp
->f_ops
->fo_drain
)(fp
, ctx
);
6252 fo_no_drain(struct fileproc
*fp
, vfs_context_t ctx
)
6254 #pragma unused(fp, ctx)
6262 * Description: Generic fileops kqueue filter indirected through the fileops
6263 * pointer in the fileproc structure
6265 * Parameters: fp fileproc structure pointer
6266 * kn pointer to knote to filter on
6268 * Returns: (kn->kn_flags & EV_ERROR) error in kn->kn_data
6269 * 0 Filter is not active
6270 * !0 Filter is active
6273 fo_kqfilter(struct fileproc
*fp
, struct knote
*kn
, struct kevent_qos_s
*kev
)
6275 return (*fp
->f_ops
->fo_kqfilter
)(fp
, kn
, kev
);
6279 fo_no_kqfilter(struct fileproc
*fp
, struct knote
*kn
, struct kevent_qos_s
*kev
)
6281 #pragma unused(fp, kev)
6282 knote_set_error(kn
, ENOTSUP
);
6288 * The ability to send a file descriptor to another
6289 * process is opt-in by file type.
6292 file_issendable(proc_t p
, struct fileproc
*fp
)
6294 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
6296 switch (fp
->f_type
) {
6301 case DTYPE_NETPOLICY
:
6302 return 0 == (fp
->f_fglob
->fg_lflags
& FG_CONFINED
);
6304 /* DTYPE_KQUEUE, DTYPE_FSEVENTS, DTYPE_PSXSEM */
6309 os_refgrp_decl(, f_iocount_refgrp
, "f_iocount", NULL
);
6312 fileproc_alloc_init(__unused
void *arg
)
6314 struct fileproc
*fp
;
6316 MALLOC_ZONE(fp
, struct fileproc
*, sizeof(*fp
), M_FILEPROC
, M_WAITOK
);
6318 bzero(fp
, sizeof(*fp
));
6319 os_ref_init(&fp
->f_iocount
, &f_iocount_refgrp
);
6327 fileproc_free(struct fileproc
*fp
)
6329 os_ref_count_t __unused refc
= os_ref_release(&fp
->f_iocount
);
6330 #if DEVELOPMENT || DEBUG
6332 panic("%s: pid %d refc: %u != 0",
6333 __func__
, proc_pid(current_proc()), refc
);
6336 switch (FILEPROC_TYPE(fp
)) {
6338 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
6341 guarded_fileproc_free(fp
);
6344 panic("%s: corrupt fp %p flags %x", __func__
, fp
, fp
->f_flags
);
6349 fileproc_modify_vflags(struct fileproc
*fp
, fileproc_vflags_t vflags
, boolean_t clearflags
)
6352 os_atomic_andnot(&fp
->f_vflags
, vflags
, relaxed
);
6354 os_atomic_or(&fp
->f_vflags
, vflags
, relaxed
);
6359 fileproc_get_vflags(struct fileproc
*fp
)
6361 return os_atomic_load(&fp
->f_vflags
, relaxed
);