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 kern_return_t
ipc_object_copyin(ipc_space_t
, mach_port_name_t
,
129 mach_msg_type_name_t
, ipc_port_t
*);
130 void ipc_port_release_send(ipc_port_t
);
135 static int finishdup(proc_t p
,
136 struct filedesc
*fdp
, int old
, int new, int flags
, int32_t *retval
);
138 int falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
, int locked
);
139 void fg_drop(struct fileproc
* fp
);
140 void fg_free(struct fileglob
*fg
);
141 void fg_ref(struct fileproc
* fp
);
142 void fileport_releasefg(struct fileglob
*fg
);
144 /* flags for close_internal_locked */
145 #define FD_DUP2RESV 1
147 /* We don't want these exported */
150 int unlink1(vfs_context_t
, vnode_t
, user_addr_t
, enum uio_seg
, int);
152 static void _fdrelse(struct proc
* p
, int fd
);
155 extern void file_lock_init(void);
157 extern kauth_scope_t kauth_scope_fileop
;
159 /* Conflict wait queue for when selects collide (opaque type) */
160 extern struct waitq select_conflict_queue
;
162 #ifndef HFS_GET_BOOT_INFO
163 #define HFS_GET_BOOT_INFO (FCNTL_FS_SPECIFIC_BASE + 0x00004)
166 #ifndef HFS_SET_BOOT_INFO
167 #define HFS_SET_BOOT_INFO (FCNTL_FS_SPECIFIC_BASE + 0x00005)
170 #ifndef APFSIOC_REVERT_TO_SNAPSHOT
171 #define APFSIOC_REVERT_TO_SNAPSHOT _IOW('J', 1, u_int64_t)
174 #define f_flag f_fglob->fg_flag
175 #define f_type f_fglob->fg_ops->fo_type
176 #define f_msgcount f_fglob->fg_msgcount
177 #define f_cred f_fglob->fg_cred
178 #define f_ops f_fglob->fg_ops
179 #define f_offset f_fglob->fg_offset
180 #define f_data f_fglob->fg_data
181 #define CHECK_ADD_OVERFLOW_INT64L(x, y) \
182 (((((x) > 0) && ((y) > 0) && ((x) > LLONG_MAX - (y))) || \
183 (((x) < 0) && ((y) < 0) && ((x) < LLONG_MIN - (y)))) \
186 * Descriptor management.
188 struct fmsglist fmsghead
; /* head of list of open files */
189 struct fmsglist fmsg_ithead
; /* head of list of open files */
190 int nfiles
; /* actual number of open files */
193 lck_grp_attr_t
* file_lck_grp_attr
;
194 lck_grp_t
* file_lck_grp
;
195 lck_attr_t
* file_lck_attr
;
197 lck_mtx_t
* uipc_lock
;
201 * check_file_seek_range
203 * Description: Checks if seek offsets are in the range of 0 to LLONG_MAX.
205 * Parameters: fl Flock structure.
206 * cur_file_offset Current offset in the file.
208 * Returns: 0 on Success.
209 * EOVERFLOW on overflow.
210 * EINVAL on offset less than zero.
214 check_file_seek_range(struct flock
*fl
, off_t cur_file_offset
)
216 if (fl
->l_whence
== SEEK_CUR
) {
217 /* Check if the start marker is beyond LLONG_MAX. */
218 if (CHECK_ADD_OVERFLOW_INT64L(fl
->l_start
, cur_file_offset
)) {
219 /* Check if start marker is negative */
220 if (fl
->l_start
< 0) {
225 /* Check if the start marker is negative. */
226 if (fl
->l_start
+ cur_file_offset
< 0) {
229 /* Check if end marker is beyond LLONG_MAX. */
230 if ((fl
->l_len
> 0) && (CHECK_ADD_OVERFLOW_INT64L(fl
->l_start
+
231 cur_file_offset
, fl
->l_len
- 1))) {
234 /* Check if the end marker is negative. */
235 if ((fl
->l_len
<= 0) && (fl
->l_start
+ cur_file_offset
+
239 } else if (fl
->l_whence
== SEEK_SET
) {
240 /* Check if the start marker is negative. */
241 if (fl
->l_start
< 0) {
244 /* Check if the end marker is beyond LLONG_MAX. */
245 if ((fl
->l_len
> 0) &&
246 CHECK_ADD_OVERFLOW_INT64L(fl
->l_start
, fl
->l_len
- 1)) {
249 /* Check if the end marker is negative. */
250 if ((fl
->l_len
< 0) && fl
->l_start
+ fl
->l_len
< 0) {
261 * Description: Initialize the file lock group and the uipc and flist locks
267 * Notes: Called at system startup from bsd_init().
272 /* allocate file lock group attribute and group */
273 file_lck_grp_attr
= lck_grp_attr_alloc_init();
275 file_lck_grp
= lck_grp_alloc_init("file", file_lck_grp_attr
);
277 /* Allocate file lock attribute */
278 file_lck_attr
= lck_attr_alloc_init();
280 uipc_lock
= lck_mtx_alloc_init(file_lck_grp
, file_lck_attr
);
285 * proc_fdlock, proc_fdlock_spin
287 * Description: Lock to control access to the per process struct fileproc
288 * and struct filedesc
290 * Parameters: p Process to take the lock on
294 * Notes: The lock is initialized in forkproc() and destroyed in
295 * reap_child_process().
298 proc_fdlock(proc_t p
)
300 lck_mtx_lock(&p
->p_fdmlock
);
304 proc_fdlock_spin(proc_t p
)
306 lck_mtx_lock_spin(&p
->p_fdmlock
);
310 proc_fdlock_assert(proc_t p
, int assertflags
)
312 lck_mtx_assert(&p
->p_fdmlock
, assertflags
);
319 * Description: Unlock the lock previously locked by a call to proc_fdlock()
321 * Parameters: p Process to drop the lock on
326 proc_fdunlock(proc_t p
)
328 lck_mtx_unlock(&p
->p_fdmlock
);
333 * System calls on descriptors.
340 * Description: Returns the per process maximum size of the descriptor table
342 * Parameters: p Process being queried
343 * retval Pointer to the call return area
348 * *retval (modified) Size of dtable
351 getdtablesize(proc_t p
, __unused
struct getdtablesize_args
*uap
, int32_t *retval
)
354 *retval
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
362 procfdtbl_reservefd(struct proc
* p
, int fd
)
364 p
->p_fd
->fd_ofiles
[fd
] = NULL
;
365 p
->p_fd
->fd_ofileflags
[fd
] |= UF_RESERVED
;
369 procfdtbl_markclosefd(struct proc
* p
, int fd
)
371 p
->p_fd
->fd_ofileflags
[fd
] |= (UF_RESERVED
| UF_CLOSING
);
375 procfdtbl_releasefd(struct proc
* p
, int fd
, struct fileproc
* fp
)
378 p
->p_fd
->fd_ofiles
[fd
] = fp
;
380 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESERVED
;
381 if ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
) == UF_RESVWAIT
) {
382 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESVWAIT
;
388 procfdtbl_waitfd(struct proc
* p
, int fd
)
390 p
->p_fd
->fd_ofileflags
[fd
] |= UF_RESVWAIT
;
391 msleep(&p
->p_fd
, &p
->p_fdmlock
, PRIBIO
, "ftbl_waitfd", NULL
);
396 procfdtbl_clearfd(struct proc
* p
, int fd
)
400 waiting
= (p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
);
401 p
->p_fd
->fd_ofiles
[fd
] = NULL
;
402 p
->p_fd
->fd_ofileflags
[fd
] = 0;
403 if (waiting
== UF_RESVWAIT
) {
411 * Description: Inline utility function to free an fd in a filedesc
413 * Parameters: fdp Pointer to filedesc fd lies in
415 * reserv fd should be reserved
419 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
423 _fdrelse(struct proc
* p
, int fd
)
425 struct filedesc
*fdp
= p
->p_fd
;
428 if (fd
< fdp
->fd_freefile
) {
429 fdp
->fd_freefile
= fd
;
432 if (fd
> fdp
->fd_lastfile
) {
433 panic("fdrelse: fd_lastfile inconsistent");
436 procfdtbl_clearfd(p
, fd
);
438 while ((nfd
= fdp
->fd_lastfile
) > 0 &&
439 fdp
->fd_ofiles
[nfd
] == NULL
&&
440 !(fdp
->fd_ofileflags
[nfd
] & UF_RESERVED
)) {
441 /* JMM - What about files with lingering EV_VANISHED knotes? */
464 char uio_buf
[UIO_SIZEOF(1)];
465 struct vfs_context context
= *(vfs_context_current());
466 bool wrote_some
= false;
470 error
= fp_lookup(p
, fd
, &fp
, 0);
475 if (fp
->f_type
!= DTYPE_VNODE
&& fp
->f_type
!= DTYPE_PIPE
&& fp
->f_type
!= DTYPE_SOCKET
) {
479 if (rw
== UIO_WRITE
&& !(fp
->f_flag
& FWRITE
)) {
484 if (rw
== UIO_READ
&& !(fp
->f_flag
& FREAD
)) {
489 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
491 if (UIO_SEG_IS_USER_SPACE(segflg
)) {
492 spacetype
= proc_is64bit(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
494 spacetype
= UIO_SYSSPACE
;
497 auio
= uio_createwithbuffer(1, offset
, spacetype
, rw
, &uio_buf
[0], sizeof(uio_buf
));
499 uio_addiov(auio
, base
, len
);
501 if (!(io_flg
& IO_APPEND
)) {
505 if (rw
== UIO_WRITE
) {
506 user_ssize_t orig_resid
= uio_resid(auio
);
507 error
= fo_write(fp
, auio
, flags
, &context
);
508 wrote_some
= uio_resid(auio
) < orig_resid
;
510 error
= fo_read(fp
, auio
, flags
, &context
);
514 *aresid
= uio_resid(auio
);
516 if (uio_resid(auio
) && error
== 0) {
522 fp_drop_written(p
, fd
, fp
);
524 fp_drop(p
, fd
, fp
, 0);
535 * Description: Duplicate a file descriptor.
537 * Parameters: p Process performing the dup
538 * uap->fd The fd to dup
539 * retval Pointer to the call return area
545 * *retval (modified) The new descriptor
548 dup(proc_t p
, struct dup_args
*uap
, int32_t *retval
)
550 struct filedesc
*fdp
= p
->p_fd
;
556 if ((error
= fp_lookup(p
, old
, &fp
, 1))) {
560 if (FP_ISGUARDED(fp
, GUARD_DUP
)) {
561 error
= fp_guard_exception(p
, old
, fp
, kGUARD_EXC_DUP
);
562 (void) fp_drop(p
, old
, fp
, 1);
566 if ((error
= fdalloc(p
, 0, &new))) {
567 fp_drop(p
, old
, fp
, 1);
571 error
= finishdup(p
, fdp
, old
, new, 0, retval
);
572 fp_drop(p
, old
, fp
, 1);
575 if (ENTR_SHOULDTRACE
&& fp
->f_type
== DTYPE_SOCKET
) {
576 KERNEL_ENERGYTRACE(kEnTrActKernSocket
, DBG_FUNC_START
,
577 new, 0, (int64_t)VM_KERNEL_ADDRPERM(fp
->f_data
));
586 * Description: Duplicate a file descriptor to a particular value.
588 * Parameters: p Process performing the dup
589 * uap->from The fd to dup
590 * uap->to The fd to dup it to
591 * retval Pointer to the call return area
597 * *retval (modified) The new descriptor
600 dup2(proc_t p
, struct dup2_args
*uap
, int32_t *retval
)
602 struct filedesc
*fdp
= p
->p_fd
;
603 int old
= uap
->from
, new = uap
->to
;
605 struct fileproc
*fp
, *nfp
;
610 if ((error
= fp_lookup(p
, old
, &fp
, 1))) {
614 if (FP_ISGUARDED(fp
, GUARD_DUP
)) {
615 error
= fp_guard_exception(p
, old
, fp
, kGUARD_EXC_DUP
);
616 (void) fp_drop(p
, old
, fp
, 1);
621 (rlim_t
)new >= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
623 fp_drop(p
, old
, fp
, 1);
628 fp_drop(p
, old
, fp
, 1);
633 if (new < 0 || new >= fdp
->fd_nfiles
) {
634 if ((error
= fdalloc(p
, new, &i
))) {
635 fp_drop(p
, old
, fp
, 1);
645 while ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == UF_RESERVED
) {
646 fp_drop(p
, old
, fp
, 1);
647 procfdtbl_waitfd(p
, new);
649 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
654 if ((fdp
->fd_ofiles
[new] != NULL
) &&
655 ((error
= fp_lookup(p
, new, &nfp
, 1)) == 0)) {
656 fp_drop(p
, old
, fp
, 1);
657 if (FP_ISGUARDED(nfp
, GUARD_CLOSE
)) {
658 error
= fp_guard_exception(p
,
659 new, nfp
, kGUARD_EXC_CLOSE
);
660 (void) fp_drop(p
, new, nfp
, 1);
664 (void)close_internal_locked(p
, new, nfp
, FD_DUP2RESV
);
666 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
668 procfdtbl_clearfd(p
, new);
672 if (fdp
->fd_ofiles
[new] != NULL
) {
673 panic("dup2: no ref on fileproc %d", new);
676 procfdtbl_reservefd(p
, new);
680 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
684 if (fdp
->fd_ofiles
[new] != 0) {
685 panic("dup2: overwriting fd_ofiles with new %d", new);
687 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0) {
688 panic("dup2: unreserved fileflags with new %d", new);
691 error
= finishdup(p
, fdp
, old
, new, 0, retval
);
692 fp_drop(p
, old
, fp
, 1);
702 * Description: The file control system call.
704 * Parameters: p Process performing the fcntl
705 * uap->fd The fd to operate against
706 * uap->cmd The command to perform
707 * uap->arg Pointer to the command argument
708 * retval Pointer to the call return area
711 * !0 Errno (see fcntl_nocancel)
714 * *retval (modified) fcntl return value (if any)
716 * Notes: This system call differs from fcntl_nocancel() in that it
717 * tests for cancellation prior to performing a potentially
718 * blocking operation.
721 fcntl(proc_t p
, struct fcntl_args
*uap
, int32_t *retval
)
723 __pthread_testcancel(1);
724 return fcntl_nocancel(p
, (struct fcntl_nocancel_args
*)uap
, retval
);
731 * Description: A non-cancel-testing file control system call.
733 * Parameters: p Process performing the fcntl
734 * uap->fd The fd to operate against
735 * uap->cmd The command to perform
736 * uap->arg Pointer to the command argument
737 * retval Pointer to the call return area
741 * fp_lookup:EBADF Bad file descriptor
753 * vnode_getwithref:???
761 * vnode_getwithref:???
768 * vnode_getwithref:???
770 * [F_SETSIZE,F_RDADVISE]
773 * vnode_getwithref:???
774 * [F_RDAHEAD,F_NOCACHE]
776 * vnode_getwithref:???
780 * *retval (modified) fcntl return value (if any)
783 fcntl_nocancel(proc_t p
, struct fcntl_nocancel_args
*uap
, int32_t *retval
)
786 struct filedesc
*fdp
= p
->p_fd
;
789 struct vnode
*vp
= NULLVP
; /* for AUDIT_ARG() at end */
790 int i
, tmp
, error
, error2
, flg
= 0;
791 struct flock fl
= {};
792 struct flocktimeout fltimeout
;
793 struct timespec
*timeout
= NULL
;
794 struct vfs_context context
;
802 AUDIT_ARG(fd
, uap
->fd
);
803 AUDIT_ARG(cmd
, uap
->cmd
);
806 if ((error
= fp_lookup(p
, fd
, &fp
, 1))) {
810 context
.vc_thread
= current_thread();
811 context
.vc_ucred
= fp
->f_cred
;
813 is64bit
= proc_is64bit(p
);
818 * Since the arg parameter is defined as a long but may be
819 * either a long or a pointer we must take care to handle
820 * sign extension issues. Our sys call munger will sign
821 * extend a long when we are called from a 32-bit process.
822 * Since we can never have an address greater than 32-bits
823 * from a 32-bit process we lop off the top 32-bits to avoid
824 * getting the wrong address
826 argp
= CAST_USER_ADDR_T((uint32_t)uap
->arg
);
829 pop
= &fdp
->fd_ofileflags
[fd
];
832 error
= mac_file_check_fcntl(proc_ucred(p
), fp
->f_fglob
, uap
->cmd
,
841 case F_DUPFD_CLOEXEC
:
842 if (FP_ISGUARDED(fp
, GUARD_DUP
)) {
843 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_DUP
);
846 newmin
= CAST_DOWN_EXPLICIT(int, uap
->arg
); /* arg is an int, so we won't lose bits */
847 AUDIT_ARG(value32
, newmin
);
848 if ((u_int
)newmin
>= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
849 newmin
>= maxfiles
) {
853 if ((error
= fdalloc(p
, newmin
, &i
))) {
856 error
= finishdup(p
, fdp
, fd
, i
,
857 uap
->cmd
== F_DUPFD_CLOEXEC
? UF_EXCLOSE
: 0, retval
);
861 *retval
= (*pop
& UF_EXCLOSE
)? FD_CLOEXEC
: 0;
866 AUDIT_ARG(value32
, uap
->arg
);
867 if (uap
->arg
& FD_CLOEXEC
) {
870 if (FILEPROC_TYPE(fp
) == FTYPE_GUARDED
) {
871 error
= fp_guard_exception(p
,
872 fd
, fp
, kGUARD_EXC_NOCLOEXEC
);
881 *retval
= OFLAGS(fp
->f_flag
);
886 fp
->f_flag
&= ~FCNTLFLAGS
;
887 tmp
= CAST_DOWN_EXPLICIT(int, uap
->arg
); /* arg is an int, so we won't lose bits */
888 AUDIT_ARG(value32
, tmp
);
889 fp
->f_flag
|= FFLAGS(tmp
) & FCNTLFLAGS
;
890 tmp
= fp
->f_flag
& FNONBLOCK
;
891 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
895 tmp
= fp
->f_flag
& FASYNC
;
896 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, &context
);
900 fp
->f_flag
&= ~FNONBLOCK
;
902 (void)fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
906 if (fp
->f_type
== DTYPE_SOCKET
) {
907 *retval
= ((struct socket
*)fp
->f_data
)->so_pgid
;
911 error
= fo_ioctl(fp
, (int)TIOCGPGRP
, (caddr_t
)retval
, &context
);
916 tmp
= CAST_DOWN_EXPLICIT(pid_t
, uap
->arg
); /* arg is an int, so we won't lose bits */
917 AUDIT_ARG(value32
, tmp
);
918 if (fp
->f_type
== DTYPE_SOCKET
) {
919 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
923 if (fp
->f_type
== DTYPE_PIPE
) {
924 error
= fo_ioctl(fp
, TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
931 proc_t p1
= proc_find(tmp
);
936 tmp
= (int)p1
->p_pgrpid
;
939 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
943 tmp
= CAST_DOWN_EXPLICIT(int, uap
->arg
);
944 if (fp
->f_type
== DTYPE_SOCKET
) {
946 error
= sock_setsockopt((struct socket
*)fp
->f_data
,
947 SOL_SOCKET
, SO_NOSIGPIPE
, &tmp
, sizeof(tmp
));
952 struct fileglob
*fg
= fp
->f_fglob
;
954 lck_mtx_lock_spin(&fg
->fg_lock
);
956 fg
->fg_lflags
|= FG_NOSIGPIPE
;
958 fg
->fg_lflags
&= ~FG_NOSIGPIPE
;
960 lck_mtx_unlock(&fg
->fg_lock
);
966 if (fp
->f_type
== DTYPE_SOCKET
) {
968 int retsize
= sizeof(*retval
);
969 error
= sock_getsockopt((struct socket
*)fp
->f_data
,
970 SOL_SOCKET
, SO_NOSIGPIPE
, retval
, &retsize
);
975 *retval
= (fp
->f_fglob
->fg_lflags
& FG_NOSIGPIPE
) ?
983 * If this is the only reference to this fglob in the process
984 * and it's already marked as close-on-fork then mark it as
985 * (immutably) "confined" i.e. any fd that points to it will
986 * forever be close-on-fork, and attempts to use an IPC
987 * mechanism to move the descriptor elsewhere will fail.
989 if (CAST_DOWN_EXPLICIT(int, uap
->arg
)) {
990 struct fileglob
*fg
= fp
->f_fglob
;
992 lck_mtx_lock_spin(&fg
->fg_lock
);
993 if (fg
->fg_lflags
& FG_CONFINED
) {
995 } else if (1 != fg
->fg_count
) {
996 error
= EAGAIN
; /* go close the dup .. */
997 } else if (UF_FORKCLOSE
== (*pop
& UF_FORKCLOSE
)) {
998 fg
->fg_lflags
|= FG_CONFINED
;
1001 error
= EBADF
; /* open without O_CLOFORK? */
1003 lck_mtx_unlock(&fg
->fg_lock
);
1006 * Other subsystems may have built on the immutability
1007 * of FG_CONFINED; clearing it may be tricky.
1009 error
= EPERM
; /* immutable */
1014 *retval
= (fp
->f_fglob
->fg_lflags
& FG_CONFINED
) ? 1 : 0;
1018 case F_SETLKWTIMEOUT
:
1020 case F_OFD_SETLKWTIMEOUT
:
1023 /* Fall into F_SETLK */
1027 if (fp
->f_type
!= DTYPE_VNODE
) {
1031 vp
= (struct vnode
*)fp
->f_data
;
1034 offset
= fp
->f_offset
;
1037 /* Copy in the lock structure */
1038 if (F_SETLKWTIMEOUT
== uap
->cmd
||
1039 F_OFD_SETLKWTIMEOUT
== uap
->cmd
) {
1040 error
= copyin(argp
, (caddr_t
) &fltimeout
, sizeof(fltimeout
));
1045 timeout
= &fltimeout
.timeout
;
1047 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
1053 /* Check starting byte and ending byte for EOVERFLOW in SEEK_CUR */
1054 /* and ending byte for EOVERFLOW in SEEK_SET */
1055 error
= check_file_seek_range(&fl
, offset
);
1060 if ((error
= vnode_getwithref(vp
))) {
1063 if (fl
.l_whence
== SEEK_CUR
) {
1064 fl
.l_start
+= offset
;
1068 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
1071 (void)vnode_put(vp
);
1078 case F_OFD_SETLKWTIMEOUT
:
1080 switch (fl
.l_type
) {
1082 if ((fflag
& FREAD
) == 0) {
1086 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1087 F_SETLK
, &fl
, flg
, &context
, timeout
);
1090 if ((fflag
& FWRITE
) == 0) {
1094 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1095 F_SETLK
, &fl
, flg
, &context
, timeout
);
1098 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1099 F_UNLCK
, &fl
, F_OFD_LOCK
, &context
,
1107 (F_RDLCK
== fl
.l_type
|| F_WRLCK
== fl
.l_type
)) {
1108 struct fileglob
*fg
= fp
->f_fglob
;
1111 * arrange F_UNLCK on last close (once
1112 * set, FG_HAS_OFDLOCK is immutable)
1114 if ((fg
->fg_lflags
& FG_HAS_OFDLOCK
) == 0) {
1115 lck_mtx_lock_spin(&fg
->fg_lock
);
1116 fg
->fg_lflags
|= FG_HAS_OFDLOCK
;
1117 lck_mtx_unlock(&fg
->fg_lock
);
1123 switch (fl
.l_type
) {
1125 if ((fflag
& FREAD
) == 0) {
1129 // XXX UInt32 unsafe for LP64 kernel
1130 OSBitOrAtomic(P_LADVLOCK
, &p
->p_ladvflag
);
1131 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1132 F_SETLK
, &fl
, flg
, &context
, timeout
);
1135 if ((fflag
& FWRITE
) == 0) {
1139 // XXX UInt32 unsafe for LP64 kernel
1140 OSBitOrAtomic(P_LADVLOCK
, &p
->p_ladvflag
);
1141 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1142 F_SETLK
, &fl
, flg
, &context
, timeout
);
1145 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1146 F_UNLCK
, &fl
, F_POSIX
, &context
, timeout
);
1154 (void) vnode_put(vp
);
1160 case F_OFD_GETLKPID
:
1161 if (fp
->f_type
!= DTYPE_VNODE
) {
1165 vp
= (struct vnode
*)fp
->f_data
;
1167 offset
= fp
->f_offset
;
1170 /* Copy in the lock structure */
1171 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
1176 /* Check starting byte and ending byte for EOVERFLOW in SEEK_CUR */
1177 /* and ending byte for EOVERFLOW in SEEK_SET */
1178 error
= check_file_seek_range(&fl
, offset
);
1183 if ((fl
.l_whence
== SEEK_SET
) && (fl
.l_start
< 0)) {
1188 switch (fl
.l_type
) {
1198 switch (fl
.l_whence
) {
1208 if ((error
= vnode_getwithref(vp
)) == 0) {
1209 if (fl
.l_whence
== SEEK_CUR
) {
1210 fl
.l_start
+= offset
;
1214 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
1220 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1221 F_GETLK
, &fl
, F_OFD_LOCK
, &context
, NULL
);
1223 case F_OFD_GETLKPID
:
1224 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1225 F_GETLKPID
, &fl
, F_OFD_LOCK
, &context
, NULL
);
1228 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1229 uap
->cmd
, &fl
, F_POSIX
, &context
, NULL
);
1233 (void)vnode_put(vp
);
1236 error
= copyout((caddr_t
)&fl
, argp
, sizeof(fl
));
1241 case F_PREALLOCATE
: {
1242 fstore_t alloc_struct
; /* structure for allocate command */
1243 u_int32_t alloc_flags
= 0;
1245 if (fp
->f_type
!= DTYPE_VNODE
) {
1250 vp
= (struct vnode
*)fp
->f_data
;
1253 /* make sure that we have write permission */
1254 if ((fp
->f_flag
& FWRITE
) == 0) {
1259 error
= copyin(argp
, (caddr_t
)&alloc_struct
, sizeof(alloc_struct
));
1264 /* now set the space allocated to 0 */
1265 alloc_struct
.fst_bytesalloc
= 0;
1268 * Do some simple parameter checking
1271 /* set up the flags */
1273 alloc_flags
|= PREALLOCATE
;
1275 if (alloc_struct
.fst_flags
& F_ALLOCATECONTIG
) {
1276 alloc_flags
|= ALLOCATECONTIG
;
1279 if (alloc_struct
.fst_flags
& F_ALLOCATEALL
) {
1280 alloc_flags
|= ALLOCATEALL
;
1284 * Do any position mode specific stuff. The only
1285 * position mode supported now is PEOFPOSMODE
1288 switch (alloc_struct
.fst_posmode
) {
1290 if (alloc_struct
.fst_offset
!= 0) {
1295 alloc_flags
|= ALLOCATEFROMPEOF
;
1299 if (alloc_struct
.fst_offset
<= 0) {
1304 alloc_flags
|= ALLOCATEFROMVOL
;
1312 if ((error
= vnode_getwithref(vp
)) == 0) {
1314 * call allocate to get the space
1316 error
= VNOP_ALLOCATE(vp
, alloc_struct
.fst_length
, alloc_flags
,
1317 &alloc_struct
.fst_bytesalloc
, alloc_struct
.fst_offset
,
1319 (void)vnode_put(vp
);
1321 error2
= copyout((caddr_t
)&alloc_struct
, argp
, sizeof(alloc_struct
));
1332 if (fp
->f_type
!= DTYPE_VNODE
) {
1337 vp
= (struct vnode
*)fp
->f_data
;
1340 /* need write permissions */
1341 if ((fp
->f_flag
& FWRITE
) == 0) {
1346 if ((error
= copyin(argp
, (caddr_t
)&args
, sizeof(args
)))) {
1350 if ((error
= vnode_getwithref(vp
))) {
1355 if ((error
= mac_vnode_check_write(&context
, fp
->f_fglob
->fg_cred
, vp
))) {
1356 (void)vnode_put(vp
);
1361 error
= VNOP_IOCTL(vp
, F_PUNCHHOLE
, (caddr_t
)&args
, 0, &context
);
1362 (void)vnode_put(vp
);
1366 case F_TRIM_ACTIVE_FILE
: {
1367 ftrimactivefile_t args
;
1369 if (priv_check_cred(kauth_cred_get(), PRIV_TRIM_ACTIVE_FILE
, 0)) {
1374 if (fp
->f_type
!= DTYPE_VNODE
) {
1379 vp
= (struct vnode
*)fp
->f_data
;
1382 /* need write permissions */
1383 if ((fp
->f_flag
& FWRITE
) == 0) {
1388 if ((error
= copyin(argp
, (caddr_t
)&args
, sizeof(args
)))) {
1392 if ((error
= vnode_getwithref(vp
))) {
1396 error
= VNOP_IOCTL(vp
, F_TRIM_ACTIVE_FILE
, (caddr_t
)&args
, 0, &context
);
1397 (void)vnode_put(vp
);
1402 if (fp
->f_type
!= DTYPE_VNODE
) {
1406 vp
= (struct vnode
*)fp
->f_data
;
1409 error
= copyin(argp
, (caddr_t
)&offset
, sizeof(off_t
));
1413 AUDIT_ARG(value64
, offset
);
1415 error
= vnode_getwithref(vp
);
1421 error
= mac_vnode_check_truncate(&context
,
1422 fp
->f_fglob
->fg_cred
, vp
);
1424 (void)vnode_put(vp
);
1429 * Make sure that we are root. Growing a file
1430 * without zero filling the data is a security hole
1431 * root would have access anyway so we'll allow it
1433 if (!kauth_cred_issuser(kauth_cred_get())) {
1439 error
= vnode_setsize(vp
, offset
, IO_NOZEROFILL
,
1444 mac_vnode_notify_truncate(&context
, fp
->f_fglob
->fg_cred
, vp
);
1449 (void)vnode_put(vp
);
1453 if (fp
->f_type
!= DTYPE_VNODE
) {
1458 fp
->f_fglob
->fg_flag
&= ~FNORDAHEAD
;
1460 fp
->f_fglob
->fg_flag
|= FNORDAHEAD
;
1466 if (fp
->f_type
!= DTYPE_VNODE
) {
1471 fp
->f_fglob
->fg_flag
|= FNOCACHE
;
1473 fp
->f_fglob
->fg_flag
&= ~FNOCACHE
;
1479 if (fp
->f_type
!= DTYPE_VNODE
) {
1484 fp
->f_fglob
->fg_flag
|= FNODIRECT
;
1486 fp
->f_fglob
->fg_flag
&= ~FNODIRECT
;
1491 case F_SINGLE_WRITER
:
1492 if (fp
->f_type
!= DTYPE_VNODE
) {
1497 fp
->f_fglob
->fg_flag
|= FSINGLE_WRITER
;
1499 fp
->f_fglob
->fg_flag
&= ~FSINGLE_WRITER
;
1504 case F_GLOBAL_NOCACHE
:
1505 if (fp
->f_type
!= DTYPE_VNODE
) {
1509 vp
= (struct vnode
*)fp
->f_data
;
1512 if ((error
= vnode_getwithref(vp
)) == 0) {
1513 *retval
= vnode_isnocache(vp
);
1516 vnode_setnocache(vp
);
1518 vnode_clearnocache(vp
);
1521 (void)vnode_put(vp
);
1525 case F_CHECK_OPENEVT
:
1526 if (fp
->f_type
!= DTYPE_VNODE
) {
1530 vp
= (struct vnode
*)fp
->f_data
;
1533 if ((error
= vnode_getwithref(vp
)) == 0) {
1534 *retval
= vnode_is_openevt(vp
);
1537 vnode_set_openevt(vp
);
1539 vnode_clear_openevt(vp
);
1542 (void)vnode_put(vp
);
1547 struct radvisory ra_struct
;
1549 if (fp
->f_type
!= DTYPE_VNODE
) {
1553 vp
= (struct vnode
*)fp
->f_data
;
1556 if ((error
= copyin(argp
, (caddr_t
)&ra_struct
, sizeof(ra_struct
)))) {
1559 if ((error
= vnode_getwithref(vp
)) == 0) {
1560 error
= VNOP_IOCTL(vp
, F_RDADVISE
, (caddr_t
)&ra_struct
, 0, &context
);
1562 (void)vnode_put(vp
);
1569 if (fp
->f_type
!= DTYPE_VNODE
) {
1573 vp
= (struct vnode
*)fp
->f_data
;
1576 if ((error
= vnode_getwithref(vp
)) == 0) {
1577 error
= VNOP_FSYNC(vp
, MNT_NOWAIT
, &context
);
1579 (void)vnode_put(vp
);
1584 case F_LOG2PHYS_EXT
: {
1585 struct log2phys l2p_struct
= {}; /* structure for allocate command */
1588 off_t file_offset
= 0;
1592 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1593 error
= copyin(argp
, (caddr_t
)&l2p_struct
, sizeof(l2p_struct
));
1597 file_offset
= l2p_struct
.l2p_devoffset
;
1599 file_offset
= fp
->f_offset
;
1601 if (fp
->f_type
!= DTYPE_VNODE
) {
1605 vp
= (struct vnode
*)fp
->f_data
;
1607 if ((error
= vnode_getwithref(vp
))) {
1610 error
= VNOP_OFFTOBLK(vp
, file_offset
, &lbn
);
1612 (void)vnode_put(vp
);
1615 error
= VNOP_BLKTOOFF(vp
, lbn
, &offset
);
1617 (void)vnode_put(vp
);
1620 devBlockSize
= vfs_devblocksize(vnode_mount(vp
));
1621 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1622 if (l2p_struct
.l2p_contigbytes
< 0) {
1628 a_size
= MIN((uint64_t)l2p_struct
.l2p_contigbytes
, SIZE_MAX
);
1630 a_size
= devBlockSize
;
1633 error
= VNOP_BLOCKMAP(vp
, offset
, a_size
, &bn
, &run
, NULL
, 0, &context
);
1635 (void)vnode_put(vp
);
1638 l2p_struct
.l2p_flags
= 0; /* for now */
1639 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1640 l2p_struct
.l2p_contigbytes
= run
- (file_offset
- offset
);
1642 l2p_struct
.l2p_contigbytes
= 0; /* for now */
1646 * The block number being -1 suggests that the file offset is not backed
1647 * by any real blocks on-disk. As a result, just let it be passed back up wholesale.
1650 /* Don't multiply it by the block size */
1651 l2p_struct
.l2p_devoffset
= bn
;
1653 l2p_struct
.l2p_devoffset
= bn
* devBlockSize
;
1654 l2p_struct
.l2p_devoffset
+= file_offset
- offset
;
1656 error
= copyout((caddr_t
)&l2p_struct
, argp
, sizeof(l2p_struct
));
1664 if (fp
->f_type
!= DTYPE_VNODE
) {
1668 vp
= (struct vnode
*)fp
->f_data
;
1671 pathlen
= MAXPATHLEN
;
1672 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
1673 if (pathbufp
== NULL
) {
1677 if ((error
= vnode_getwithref(vp
)) == 0) {
1678 error
= vn_getpath(vp
, pathbufp
, &pathlen
);
1679 (void)vnode_put(vp
);
1682 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
1685 FREE(pathbufp
, M_TEMP
);
1689 case F_PATHPKG_CHECK
: {
1693 if (fp
->f_type
!= DTYPE_VNODE
) {
1697 vp
= (struct vnode
*)fp
->f_data
;
1700 pathlen
= MAXPATHLEN
;
1701 pathbufp
= kalloc(MAXPATHLEN
);
1703 if ((error
= copyinstr(argp
, pathbufp
, MAXPATHLEN
, &pathlen
)) == 0) {
1704 if ((error
= vnode_getwithref(vp
)) == 0) {
1705 AUDIT_ARG(text
, pathbufp
);
1706 error
= vn_path_package_check(vp
, pathbufp
, pathlen
, retval
);
1708 (void)vnode_put(vp
);
1711 kfree(pathbufp
, MAXPATHLEN
);
1715 case F_CHKCLEAN
: // used by regression tests to see if all dirty pages got cleaned by fsync()
1716 case F_FULLFSYNC
: // fsync + flush the journal + DKIOCSYNCHRONIZE
1717 case F_BARRIERFSYNC
: // fsync + barrier
1718 case F_FREEZE_FS
: // freeze all other fs operations for the fs of this fd
1719 case F_THAW_FS
: { // thaw all frozen fs operations for the fs of this fd
1720 if (fp
->f_type
!= DTYPE_VNODE
) {
1724 vp
= (struct vnode
*)fp
->f_data
;
1727 if ((error
= vnode_getwithref(vp
)) == 0) {
1728 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
1730 (void)vnode_put(vp
);
1736 * SPI (private) for opening a file starting from a dir fd
1739 struct user_fopenfrom fopen
;
1740 struct vnode_attr va
;
1741 struct nameidata nd
;
1744 /* Check if this isn't a valid file descriptor */
1745 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1746 (fp
->f_flag
& FREAD
) == 0) {
1750 vp
= (struct vnode
*)fp
->f_data
;
1753 if (vnode_getwithref(vp
)) {
1758 /* Only valid for directories */
1759 if (vp
->v_type
!= VDIR
) {
1765 /* Get flags, mode and pathname arguments. */
1766 if (IS_64BIT_PROCESS(p
)) {
1767 error
= copyin(argp
, &fopen
, sizeof(fopen
));
1769 struct user32_fopenfrom fopen32
;
1771 error
= copyin(argp
, &fopen32
, sizeof(fopen32
));
1772 fopen
.o_flags
= fopen32
.o_flags
;
1773 fopen
.o_mode
= fopen32
.o_mode
;
1774 fopen
.o_pathname
= CAST_USER_ADDR_T(fopen32
.o_pathname
);
1780 AUDIT_ARG(fflags
, fopen
.o_flags
);
1781 AUDIT_ARG(mode
, fopen
.o_mode
);
1783 /* Mask off all but regular access permissions */
1784 cmode
= ((fopen
.o_mode
& ~fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
1785 VATTR_SET(&va
, va_mode
, cmode
& ACCESSPERMS
);
1787 /* Start the lookup relative to the file descriptor's vnode. */
1788 NDINIT(&nd
, LOOKUP
, OP_OPEN
, USEDVP
| FOLLOW
| AUDITVNPATH1
, UIO_USERSPACE
,
1789 fopen
.o_pathname
, &context
);
1792 error
= open1(&context
, &nd
, fopen
.o_flags
, &va
,
1793 fileproc_alloc_init
, NULL
, retval
);
1799 * SPI (private) for unlinking a file starting from a dir fd
1801 case F_UNLINKFROM
: {
1802 user_addr_t pathname
;
1804 /* Check if this isn't a valid file descriptor */
1805 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1806 (fp
->f_flag
& FREAD
) == 0) {
1810 vp
= (struct vnode
*)fp
->f_data
;
1813 if (vnode_getwithref(vp
)) {
1818 /* Only valid for directories */
1819 if (vp
->v_type
!= VDIR
) {
1825 /* Get flags, mode and pathname arguments. */
1826 if (IS_64BIT_PROCESS(p
)) {
1827 pathname
= (user_addr_t
)argp
;
1829 pathname
= CAST_USER_ADDR_T(argp
);
1832 /* Start the lookup relative to the file descriptor's vnode. */
1833 error
= unlink1(&context
, vp
, pathname
, UIO_USERSPACE
, 0);
1841 case F_ADDFILESIGS_FOR_DYLD_SIM
:
1842 case F_ADDFILESIGS_RETURN
:
1844 struct cs_blob
*blob
= NULL
;
1845 struct user_fsignatures fs
;
1847 vm_offset_t kernel_blob_addr
;
1848 vm_size_t kernel_blob_size
;
1849 int blob_add_flags
= 0;
1851 if (fp
->f_type
!= DTYPE_VNODE
) {
1855 vp
= (struct vnode
*)fp
->f_data
;
1858 if (uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
1859 blob_add_flags
|= MAC_VNODE_CHECK_DYLD_SIM
;
1860 if ((p
->p_csflags
& CS_KILL
) == 0) {
1862 p
->p_csflags
|= CS_KILL
;
1867 error
= vnode_getwithref(vp
);
1872 if (IS_64BIT_PROCESS(p
)) {
1873 error
= copyin(argp
, &fs
, sizeof(fs
));
1875 struct user32_fsignatures fs32
;
1877 error
= copyin(argp
, &fs32
, sizeof(fs32
));
1878 fs
.fs_file_start
= fs32
.fs_file_start
;
1879 fs
.fs_blob_start
= CAST_USER_ADDR_T(fs32
.fs_blob_start
);
1880 fs
.fs_blob_size
= fs32
.fs_blob_size
;
1889 * First check if we have something loaded a this offset
1891 blob
= ubc_cs_blob_get(vp
, CPU_TYPE_ANY
, fs
.fs_file_start
);
1893 /* If this is for dyld_sim revalidate the blob */
1894 if (uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
1895 error
= ubc_cs_blob_revalidate(vp
, blob
, NULL
, blob_add_flags
);
1898 if (error
!= EAGAIN
) {
1908 * An arbitrary limit, to prevent someone from mapping in a 20GB blob. This should cover
1909 * our use cases for the immediate future, but note that at the time of this commit, some
1910 * platforms are nearing 2MB blob sizes (with a prior soft limit of 2.5MB).
1912 * We should consider how we can manage this more effectively; the above means that some
1913 * platforms are using megabytes of memory for signing data; it merely hasn't crossed the
1914 * threshold considered ridiculous at the time of this change.
1916 #define CS_MAX_BLOB_SIZE (40ULL * 1024ULL * 1024ULL)
1917 if (fs
.fs_blob_size
> CS_MAX_BLOB_SIZE
) {
1923 kernel_blob_size
= CAST_DOWN(vm_size_t
, fs
.fs_blob_size
);
1924 kr
= ubc_cs_blob_allocate(&kernel_blob_addr
, &kernel_blob_size
);
1925 if (kr
!= KERN_SUCCESS
) {
1931 if (uap
->cmd
== F_ADDSIGS
) {
1932 error
= copyin(fs
.fs_blob_start
,
1933 (void *) kernel_blob_addr
,
1935 } else { /* F_ADDFILESIGS || F_ADDFILESIGS_RETURN || F_ADDFILESIGS_FOR_DYLD_SIM */
1938 error
= vn_rdwr(UIO_READ
,
1940 (caddr_t
) kernel_blob_addr
,
1942 fs
.fs_file_start
+ fs
.fs_blob_start
,
1948 if ((error
== 0) && resid
) {
1949 /* kernel_blob_size rounded to a page size, but signature may be at end of file */
1950 memset((void *)(kernel_blob_addr
+ (kernel_blob_size
- resid
)), 0x0, resid
);
1955 ubc_cs_blob_deallocate(kernel_blob_addr
,
1962 error
= ubc_cs_blob_add(vp
,
1963 CPU_TYPE_ANY
, /* not for a specific architecture */
1971 /* ubc_blob_add() has consumed "kernel_blob_addr" if it is zeroed */
1973 if (kernel_blob_addr
) {
1974 ubc_cs_blob_deallocate(kernel_blob_addr
,
1980 #if CHECK_CS_VALIDATION_BITMAP
1981 ubc_cs_validation_bitmap_allocate( vp
);
1986 if (uap
->cmd
== F_ADDFILESIGS_RETURN
|| uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
1988 * The first element of the structure is a
1989 * off_t that happen to have the same size for
1990 * all archs. Lets overwrite that.
1992 off_t end_offset
= 0;
1994 end_offset
= blob
->csb_end_offset
;
1996 error
= copyout(&end_offset
, argp
, sizeof(end_offset
));
1999 (void) vnode_put(vp
);
2008 struct fileglob
*fg
;
2011 if (fp
->f_type
!= DTYPE_VNODE
) {
2018 if (IS_64BIT_PROCESS(p
)) {
2019 error
= copyin(argp
, &lv
, sizeof(lv
));
2021 struct user32_fchecklv lv32
= {};
2023 error
= copyin(argp
, &lv32
, sizeof(lv32
));
2024 lv
.lv_file_start
= lv32
.lv_file_start
;
2025 lv
.lv_error_message
= (void *)(uintptr_t)lv32
.lv_error_message
;
2026 lv
.lv_error_message_size
= lv32
.lv_error_message_size
;
2033 error
= mac_file_check_library_validation(p
, fg
, lv
.lv_file_start
,
2034 (user_long_t
)lv
.lv_error_message
, lv
.lv_error_message_size
);
2040 case F_GETPROTECTIONCLASS
: {
2041 if (fp
->f_type
!= DTYPE_VNODE
) {
2045 vp
= (struct vnode
*)fp
->f_data
;
2049 if (vnode_getwithref(vp
)) {
2054 struct vnode_attr va
;
2057 VATTR_WANTED(&va
, va_dataprotect_class
);
2058 error
= VNOP_GETATTR(vp
, &va
, &context
);
2060 if (VATTR_IS_SUPPORTED(&va
, va_dataprotect_class
)) {
2061 *retval
= va
.va_dataprotect_class
;
2071 case F_SETPROTECTIONCLASS
: {
2072 /* tmp must be a valid PROTECTION_CLASS_* */
2073 tmp
= CAST_DOWN_EXPLICIT(uint32_t, uap
->arg
);
2075 if (fp
->f_type
!= DTYPE_VNODE
) {
2079 vp
= (struct vnode
*)fp
->f_data
;
2083 if (vnode_getwithref(vp
)) {
2088 /* Only go forward if you have write access */
2089 vfs_context_t ctx
= vfs_context_current();
2090 if (vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2096 struct vnode_attr va
;
2099 VATTR_SET(&va
, va_dataprotect_class
, tmp
);
2101 error
= VNOP_SETATTR(vp
, &va
, ctx
);
2107 case F_TRANSCODEKEY
: {
2108 if (fp
->f_type
!= DTYPE_VNODE
) {
2113 vp
= (struct vnode
*)fp
->f_data
;
2116 if (vnode_getwithref(vp
)) {
2122 .len
= CP_MAX_WRAPPEDKEYSIZE
,
2125 MALLOC(k
.key
, char *, k
.len
, M_TEMP
, M_WAITOK
| M_ZERO
);
2127 error
= VNOP_IOCTL(vp
, F_TRANSCODEKEY
, (caddr_t
)&k
, 1, &context
);
2132 error
= copyout(k
.key
, argp
, k
.len
);
2136 FREE(k
.key
, M_TEMP
);
2141 case F_GETPROTECTIONLEVEL
: {
2142 if (fp
->f_type
!= DTYPE_VNODE
) {
2147 vp
= (struct vnode
*) fp
->f_data
;
2150 if (vnode_getwithref(vp
)) {
2155 error
= VNOP_IOCTL(vp
, F_GETPROTECTIONLEVEL
, (caddr_t
)retval
, 0, &context
);
2161 case F_GETDEFAULTPROTLEVEL
: {
2162 if (fp
->f_type
!= DTYPE_VNODE
) {
2167 vp
= (struct vnode
*) fp
->f_data
;
2170 if (vnode_getwithref(vp
)) {
2176 * if cp_get_major_vers fails, error will be set to proper errno
2177 * and cp_version will still be 0.
2180 error
= VNOP_IOCTL(vp
, F_GETDEFAULTPROTLEVEL
, (caddr_t
)retval
, 0, &context
);
2186 #endif /* CONFIG_PROTECT */
2188 case F_MOVEDATAEXTENTS
: {
2189 struct fileproc
*fp2
= NULL
;
2190 struct vnode
*src_vp
= NULLVP
;
2191 struct vnode
*dst_vp
= NULLVP
;
2192 /* We need to grab the 2nd FD out of the argments before moving on. */
2193 int fd2
= CAST_DOWN_EXPLICIT(int32_t, uap
->arg
);
2195 error
= priv_check_cred(kauth_cred_get(), PRIV_VFS_MOVE_DATA_EXTENTS
, 0);
2200 if (fp
->f_type
!= DTYPE_VNODE
) {
2205 /* For now, special case HFS+ only, since this is SPI. */
2206 src_vp
= (struct vnode
*)fp
->f_data
;
2207 if (src_vp
->v_tag
!= VT_HFS
) {
2213 * Get the references before we start acquiring iocounts on the vnodes,
2214 * while we still hold the proc fd lock
2216 if ((error
= fp_lookup(p
, fd2
, &fp2
, 1))) {
2220 if (fp2
->f_type
!= DTYPE_VNODE
) {
2221 fp_drop(p
, fd2
, fp2
, 1);
2225 dst_vp
= (struct vnode
*)fp2
->f_data
;
2226 if (dst_vp
->v_tag
!= VT_HFS
) {
2227 fp_drop(p
, fd2
, fp2
, 1);
2233 /* Re-do MAC checks against the new FD, pass in a fake argument */
2234 error
= mac_file_check_fcntl(proc_ucred(p
), fp2
->f_fglob
, uap
->cmd
, 0);
2236 fp_drop(p
, fd2
, fp2
, 1);
2240 /* Audit the 2nd FD */
2245 if (vnode_getwithref(src_vp
)) {
2246 fp_drop(p
, fd2
, fp2
, 0);
2250 if (vnode_getwithref(dst_vp
)) {
2252 fp_drop(p
, fd2
, fp2
, 0);
2258 * Basic asserts; validate they are not the same and that
2259 * both live on the same filesystem.
2261 if (dst_vp
== src_vp
) {
2264 fp_drop(p
, fd2
, fp2
, 0);
2269 if (dst_vp
->v_mount
!= src_vp
->v_mount
) {
2272 fp_drop(p
, fd2
, fp2
, 0);
2277 /* Now we have a legit pair of FDs. Go to work */
2279 /* Now check for write access to the target files */
2280 if (vnode_authorize(src_vp
, NULLVP
,
2281 (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), &context
) != 0) {
2284 fp_drop(p
, fd2
, fp2
, 0);
2289 if (vnode_authorize(dst_vp
, NULLVP
,
2290 (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), &context
) != 0) {
2293 fp_drop(p
, fd2
, fp2
, 0);
2298 /* Verify that both vps point to files and not directories */
2299 if (!vnode_isreg(src_vp
) || !vnode_isreg(dst_vp
)) {
2303 fp_drop(p
, fd2
, fp2
, 0);
2308 * The exchangedata syscall handler passes in 0 for the flags to VNOP_EXCHANGE.
2309 * We'll pass in our special bit indicating that the new behavior is expected
2312 error
= VNOP_EXCHANGE(src_vp
, dst_vp
, FSOPT_EXCHANGE_DATA_ONLY
, &context
);
2316 fp_drop(p
, fd2
, fp2
, 0);
2321 * SPI for making a file compressed.
2323 case F_MAKECOMPRESSED
: {
2324 uint32_t gcounter
= CAST_DOWN_EXPLICIT(uint32_t, uap
->arg
);
2326 if (fp
->f_type
!= DTYPE_VNODE
) {
2331 vp
= (struct vnode
*) fp
->f_data
;
2335 if (vnode_getwithref(vp
)) {
2341 if ((vnode_isreg(vp
) == 0) && (vnode_islnk(vp
) == 0)) {
2347 /* invoke ioctl to pass off to FS */
2348 /* Only go forward if you have write access */
2349 vfs_context_t ctx
= vfs_context_current();
2350 if (vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2356 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)&gcounter
, 0, &context
);
2363 * SPI (private) for indicating to a filesystem that subsequent writes to
2364 * the open FD will written to the Fastflow.
2366 case F_SET_GREEDY_MODE
:
2367 /* intentionally drop through to the same handler as F_SETSTATIC.
2368 * both fcntls should pass the argument and their selector into VNOP_IOCTL.
2372 * SPI (private) for indicating to a filesystem that subsequent writes to
2373 * the open FD will represent static content.
2375 case F_SETSTATICCONTENT
: {
2376 caddr_t ioctl_arg
= NULL
;
2379 ioctl_arg
= (caddr_t
) 1;
2382 if (fp
->f_type
!= DTYPE_VNODE
) {
2386 vp
= (struct vnode
*)fp
->f_data
;
2389 error
= vnode_getwithref(vp
);
2395 /* Only go forward if you have write access */
2396 vfs_context_t ctx
= vfs_context_current();
2397 if (vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2403 error
= VNOP_IOCTL(vp
, uap
->cmd
, ioctl_arg
, 0, &context
);
2404 (void)vnode_put(vp
);
2410 * SPI (private) for indicating to the lower level storage driver that the
2411 * subsequent writes should be of a particular IO type (burst, greedy, static),
2412 * or other flavors that may be necessary.
2419 /* extract 32 bits of flags from userland */
2420 param_ptr
= (caddr_t
) uap
->arg
;
2421 param
= (uint32_t) param_ptr
;
2423 /* If no argument is specified, error out */
2429 * Validate the different types of flags that can be specified:
2430 * all of them are mutually exclusive for now.
2433 case F_IOTYPE_ISOCHRONOUS
:
2442 if (fp
->f_type
!= DTYPE_VNODE
) {
2446 vp
= (struct vnode
*)fp
->f_data
;
2449 error
= vnode_getwithref(vp
);
2455 /* Only go forward if you have write access */
2456 vfs_context_t ctx
= vfs_context_current();
2457 if (vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2463 error
= VNOP_IOCTL(vp
, uap
->cmd
, param_ptr
, 0, &context
);
2464 (void)vnode_put(vp
);
2470 * Set the vnode pointed to by 'fd'
2471 * and tag it as the (potentially future) backing store
2472 * for another filesystem
2474 case F_SETBACKINGSTORE
: {
2475 if (fp
->f_type
!= DTYPE_VNODE
) {
2480 vp
= (struct vnode
*)fp
->f_data
;
2482 if (vp
->v_tag
!= VT_HFS
) {
2488 if (vnode_getwithref(vp
)) {
2493 /* only proceed if you have write access */
2494 vfs_context_t ctx
= vfs_context_current();
2495 if (vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2502 /* If arg != 0, set, otherwise unset */
2504 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)1, 0, &context
);
2506 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
2514 * like F_GETPATH, but special semantics for
2515 * the mobile time machine handler.
2517 case F_GETPATH_MTMINFO
: {
2521 if (fp
->f_type
!= DTYPE_VNODE
) {
2525 vp
= (struct vnode
*)fp
->f_data
;
2528 pathlen
= MAXPATHLEN
;
2529 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
2530 if (pathbufp
== NULL
) {
2534 if ((error
= vnode_getwithref(vp
)) == 0) {
2535 int backingstore
= 0;
2537 /* Check for error from vn_getpath before moving on */
2538 if ((error
= vn_getpath(vp
, pathbufp
, &pathlen
)) == 0) {
2539 if (vp
->v_tag
== VT_HFS
) {
2540 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
) &backingstore
, 0, &context
);
2542 (void)vnode_put(vp
);
2545 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
2549 * If the copyout was successful, now check to ensure
2550 * that this vnode is not a BACKINGSTORE vnode. mtmd
2551 * wants the path regardless.
2558 (void)vnode_put(vp
);
2561 FREE(pathbufp
, M_TEMP
);
2565 #if DEBUG || DEVELOPMENT
2567 if (fp
->f_type
!= DTYPE_VNODE
) {
2571 vp
= (struct vnode
*)fp
->f_data
;
2580 * This is an fcntl() that we d not recognize at this level;
2581 * if this is a vnode, we send it down into the VNOP_IOCTL
2582 * for this vnode; this can include special devices, and will
2583 * effectively overload fcntl() to send ioctl()'s.
2585 if ((uap
->cmd
& IOC_VOID
) && (uap
->cmd
& IOC_INOUT
)) {
2590 /* Catch any now-invalid fcntl() selectors */
2592 case (int)APFSIOC_REVERT_TO_SNAPSHOT
:
2593 case (int)FSIOC_FIOSEEKHOLE
:
2594 case (int)FSIOC_FIOSEEKDATA
:
2595 case HFS_GET_BOOT_INFO
:
2596 case HFS_SET_BOOT_INFO
:
2598 case F_MARKDEPENDENCY
:
2605 if (fp
->f_type
!= DTYPE_VNODE
) {
2609 vp
= (struct vnode
*)fp
->f_data
;
2612 if ((error
= vnode_getwithref(vp
)) == 0) {
2613 #define STK_PARAMS 128
2614 char stkbuf
[STK_PARAMS
] = {0};
2618 * For this to work properly, we have to copy in the
2619 * ioctl() cmd argument if there is one; we must also
2620 * check that a command parameter, if present, does
2621 * not exceed the maximum command length dictated by
2622 * the number of bits we have available in the command
2623 * to represent a structure length. Finally, we have
2624 * to copy the results back out, if it is that type of
2627 size
= IOCPARM_LEN(uap
->cmd
);
2628 if (size
> IOCPARM_MAX
) {
2629 (void)vnode_put(vp
);
2635 if (size
> sizeof(stkbuf
)) {
2636 if ((memp
= (caddr_t
)kalloc(size
)) == 0) {
2637 (void)vnode_put(vp
);
2646 if (uap
->cmd
& IOC_IN
) {
2649 error
= copyin(argp
, data
, size
);
2651 (void)vnode_put(vp
);
2658 /* Bzero the section beyond that which was needed */
2659 if (size
<= sizeof(stkbuf
)) {
2660 bzero((((uint8_t*)data
) + size
), (sizeof(stkbuf
) - size
));
2665 *(user_addr_t
*)data
= argp
;
2667 *(uint32_t *)data
= (uint32_t)argp
;
2670 } else if ((uap
->cmd
& IOC_OUT
) && size
) {
2672 * Zero the buffer so the user always
2673 * gets back something deterministic.
2676 } else if (uap
->cmd
& IOC_VOID
) {
2678 *(user_addr_t
*)data
= argp
;
2680 *(uint32_t *)data
= (uint32_t)argp
;
2684 error
= VNOP_IOCTL(vp
, uap
->cmd
, CAST_DOWN(caddr_t
, data
), 0, &context
);
2686 (void)vnode_put(vp
);
2688 /* Copy any output data to user */
2689 if (error
== 0 && (uap
->cmd
& IOC_OUT
) && size
) {
2690 error
= copyout(data
, argp
, size
);
2700 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
2701 fp_drop(p
, fd
, fp
, 0);
2704 fp_drop(p
, fd
, fp
, 1);
2713 * Description: Common code for dup, dup2, and fcntl(F_DUPFD).
2715 * Parameters: p Process performing the dup
2717 * new The fd to dup it to
2718 * fd_flags Flags to augment the new fd
2719 * retval Pointer to the call return area
2721 * Returns: 0 Success
2726 * *retval (modified) The new descriptor
2728 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
2731 * Notes: This function may drop and reacquire this lock; it is unsafe
2732 * for a caller to assume that other state protected by the lock
2733 * has not been subsequently changed out from under it.
2737 struct filedesc
*fdp
, int old
, int new, int fd_flags
, int32_t *retval
)
2739 struct fileproc
*nfp
;
2740 struct fileproc
*ofp
;
2746 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2748 if ((ofp
= fdp
->fd_ofiles
[old
]) == NULL
||
2749 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
)) {
2756 error
= mac_file_check_dup(proc_ucred(p
), ofp
->f_fglob
, new);
2766 nfp
= fileproc_alloc_init(NULL
);
2776 nfp
->f_fglob
= ofp
->f_fglob
;
2779 if (fdp
->fd_ofiles
[new] != 0) {
2780 panic("finishdup: overwriting fd_ofiles with new %d", new);
2782 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0) {
2783 panic("finishdup: unreserved fileflags with new %d", new);
2787 if (new > fdp
->fd_lastfile
) {
2788 fdp
->fd_lastfile
= new;
2790 *fdflags(p
, new) |= fd_flags
;
2791 procfdtbl_releasefd(p
, new, nfp
);
2800 * Description: The implementation of the close(2) system call
2802 * Parameters: p Process in whose per process file table
2803 * the close is to occur
2804 * uap->fd fd to be closed
2807 * Returns: 0 Success
2808 * fp_lookup:EBADF Bad file descriptor
2809 * fp_guard_exception:??? Guarded file descriptor
2810 * close_internal:EBADF
2811 * close_internal:??? Anything returnable by a per-fileops
2815 close(proc_t p
, struct close_args
*uap
, int32_t *retval
)
2817 __pthread_testcancel(1);
2818 return close_nocancel(p
, (struct close_nocancel_args
*)uap
, retval
);
2823 close_nocancel(proc_t p
, struct close_nocancel_args
*uap
, __unused
int32_t *retval
)
2825 struct fileproc
*fp
;
2829 AUDIT_SYSCLOSE(p
, fd
);
2833 if ((error
= fp_lookup(p
, fd
, &fp
, 1))) {
2838 if (FP_ISGUARDED(fp
, GUARD_CLOSE
)) {
2839 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_CLOSE
);
2840 (void) fp_drop(p
, fd
, fp
, 1);
2845 error
= close_internal_locked(p
, fd
, fp
, 0);
2854 * close_internal_locked
2856 * Close a file descriptor.
2858 * Parameters: p Process in whose per process file table
2859 * the close is to occur
2860 * fd fd to be closed
2861 * fp fileproc associated with the fd
2863 * Returns: 0 Success
2864 * EBADF fd already in close wait state
2865 * closef_locked:??? Anything returnable by a per-fileops
2868 * Locks: Assumes proc_fdlock for process is held by the caller and returns
2871 * Notes: This function may drop and reacquire this lock; it is unsafe
2872 * for a caller to assume that other state protected by the lock
2873 * has not been subsequently changed out from under it.
2876 close_internal_locked(proc_t p
, int fd
, struct fileproc
*fp
, int flags
)
2878 struct filedesc
*fdp
= p
->p_fd
;
2880 int resvfd
= flags
& FD_DUP2RESV
;
2884 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2887 /* Keep people from using the filedesc while we are closing it */
2888 procfdtbl_markclosefd(p
, fd
);
2891 if ((fp
->f_flags
& FP_CLOSING
) == FP_CLOSING
) {
2892 panic("close_internal_locked: being called on already closing fd");
2897 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0) {
2898 panic("close_internal: unreserved fileflags with fd %d", fd
);
2902 fp
->f_flags
|= FP_CLOSING
;
2904 if ((fp
->f_flags
& FP_AIOISSUED
) || kauth_authorize_fileop_has_listeners()) {
2907 if ((fp
->f_type
== DTYPE_VNODE
) && kauth_authorize_fileop_has_listeners()) {
2909 * call out to allow 3rd party notification of close.
2910 * Ignore result of kauth_authorize_fileop call.
2912 if (vnode_getwithref((vnode_t
)fp
->f_data
) == 0) {
2913 u_int fileop_flags
= 0;
2914 if ((fp
->f_flags
& FP_WRITTEN
) != 0) {
2915 fileop_flags
|= KAUTH_FILEOP_CLOSE_MODIFIED
;
2917 kauth_authorize_fileop(fp
->f_fglob
->fg_cred
, KAUTH_FILEOP_CLOSE
,
2918 (uintptr_t)fp
->f_data
, (uintptr_t)fileop_flags
);
2919 vnode_put((vnode_t
)fp
->f_data
);
2922 if (fp
->f_flags
& FP_AIOISSUED
) {
2924 * cancel all async IO requests that can be cancelled.
2926 _aio_close( p
, fd
);
2932 if (fd
< fdp
->fd_knlistsize
) {
2933 knote_fdclose(p
, fd
);
2936 fileproc_drain(p
, fp
);
2938 if (fp
->f_flags
& FP_WAITEVENT
) {
2939 (void)waitevent_close(p
, fp
);
2945 procfdtbl_reservefd(p
, fd
);
2948 if (ENTR_SHOULDTRACE
&& fp
->f_type
== DTYPE_SOCKET
) {
2949 KERNEL_ENERGYTRACE(kEnTrActKernSocket
, DBG_FUNC_END
,
2950 fd
, 0, (int64_t)VM_KERNEL_ADDRPERM(fp
->f_data
));
2953 error
= closef_locked(fp
, fp
->f_fglob
, p
);
2954 if ((fp
->f_flags
& FP_WAITCLOSE
) == FP_WAITCLOSE
) {
2955 wakeup(&fp
->f_flags
);
2957 fp
->f_flags
&= ~(FP_WAITCLOSE
| FP_CLOSING
);
2967 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0) {
2968 panic("close with reserved fd returns with freed fd:%d: proc: %p", fd
, p
);
2980 * Description: Return status information about a file descriptor.
2982 * Parameters: p The process doing the fstat
2984 * ub The user stat buffer
2985 * xsecurity The user extended security
2986 * buffer, or 0 if none
2987 * xsecurity_size The size of xsecurity, or 0
2989 * isstat64 Flag to indicate 64 bit version
2990 * for inode size, etc.
2992 * Returns: 0 Success
2995 * fp_lookup:EBADF Bad file descriptor
2996 * vnode_getwithref:???
2998 * vnode_getwithref:???
3005 * Notes: Internal implementation for all other fstat() related
3008 * XXX switch on node type is bogus; need a stat in struct
3009 * XXX fileops instead.
3012 fstat1(proc_t p
, int fd
, user_addr_t ub
, user_addr_t xsecurity
, user_addr_t xsecurity_size
, int isstat64
)
3014 struct fileproc
*fp
;
3020 struct user64_stat user64_sb
;
3021 struct user32_stat user32_sb
;
3022 struct user64_stat64 user64_sb64
;
3023 struct user32_stat64 user32_sb64
;
3028 kauth_filesec_t fsec
;
3029 user_size_t xsecurity_bufsize
;
3030 vfs_context_t ctx
= vfs_context_current();
3036 if ((error
= fp_lookup(p
, fd
, &fp
, 0)) != 0) {
3041 fsec
= KAUTH_FILESEC_NONE
;
3043 sbptr
= (void *)&source
;
3047 if ((error
= vnode_getwithref((vnode_t
)data
)) == 0) {
3049 * If the caller has the file open, and is not
3050 * requesting extended security information, we are
3051 * going to let them get the basic stat information.
3053 if (xsecurity
== USER_ADDR_NULL
) {
3054 error
= vn_stat_noauth((vnode_t
)data
, sbptr
, NULL
, isstat64
, ctx
,
3055 fp
->f_fglob
->fg_cred
);
3057 error
= vn_stat((vnode_t
)data
, sbptr
, &fsec
, isstat64
, ctx
);
3060 AUDIT_ARG(vnpath
, (struct vnode
*)data
, ARG_VNODE1
);
3061 (void)vnode_put((vnode_t
)data
);
3067 error
= soo_stat((struct socket
*)data
, sbptr
, isstat64
);
3069 #endif /* SOCKETS */
3072 error
= pipe_stat((void *)data
, sbptr
, isstat64
);
3076 error
= pshm_stat((void *)data
, sbptr
, isstat64
);
3080 error
= kqueue_stat((void *)data
, sbptr
, isstat64
, p
);
3090 if (isstat64
!= 0) {
3091 source
.sb64
.st_lspare
= 0;
3092 source
.sb64
.st_qspare
[0] = 0LL;
3093 source
.sb64
.st_qspare
[1] = 0LL;
3095 if (IS_64BIT_PROCESS(current_proc())) {
3096 munge_user64_stat64(&source
.sb64
, &dest
.user64_sb64
);
3097 my_size
= sizeof(dest
.user64_sb64
);
3098 sbp
= (caddr_t
)&dest
.user64_sb64
;
3100 munge_user32_stat64(&source
.sb64
, &dest
.user32_sb64
);
3101 my_size
= sizeof(dest
.user32_sb64
);
3102 sbp
= (caddr_t
)&dest
.user32_sb64
;
3105 source
.sb
.st_lspare
= 0;
3106 source
.sb
.st_qspare
[0] = 0LL;
3107 source
.sb
.st_qspare
[1] = 0LL;
3108 if (IS_64BIT_PROCESS(current_proc())) {
3109 munge_user64_stat(&source
.sb
, &dest
.user64_sb
);
3110 my_size
= sizeof(dest
.user64_sb
);
3111 sbp
= (caddr_t
)&dest
.user64_sb
;
3113 munge_user32_stat(&source
.sb
, &dest
.user32_sb
);
3114 my_size
= sizeof(dest
.user32_sb
);
3115 sbp
= (caddr_t
)&dest
.user32_sb
;
3119 error
= copyout(sbp
, ub
, my_size
);
3122 /* caller wants extended security information? */
3123 if (xsecurity
!= USER_ADDR_NULL
) {
3124 /* did we get any? */
3125 if (fsec
== KAUTH_FILESEC_NONE
) {
3126 if (susize(xsecurity_size
, 0) != 0) {
3131 /* find the user buffer size */
3132 xsecurity_bufsize
= fusize(xsecurity_size
);
3134 /* copy out the actual data size */
3135 if (susize(xsecurity_size
, KAUTH_FILESEC_COPYSIZE(fsec
)) != 0) {
3140 /* if the caller supplied enough room, copy out to it */
3141 if (xsecurity_bufsize
>= KAUTH_FILESEC_COPYSIZE(fsec
)) {
3142 error
= copyout(fsec
, xsecurity
, KAUTH_FILESEC_COPYSIZE(fsec
));
3147 fp_drop(p
, fd
, fp
, 0);
3149 kauth_filesec_free(fsec
);
3158 * Description: Extended version of fstat supporting returning extended
3159 * security information
3161 * Parameters: p The process doing the fstat
3162 * uap->fd The fd to stat
3163 * uap->ub The user stat buffer
3164 * uap->xsecurity The user extended security
3165 * buffer, or 0 if none
3166 * uap->xsecurity_size The size of xsecurity, or 0
3168 * Returns: 0 Success
3169 * !0 Errno (see fstat1)
3172 fstat_extended(proc_t p
, struct fstat_extended_args
*uap
, __unused
int32_t *retval
)
3174 return fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 0);
3181 * Description: Get file status for the file associated with fd
3183 * Parameters: p The process doing the fstat
3184 * uap->fd The fd to stat
3185 * uap->ub The user stat buffer
3187 * Returns: 0 Success
3188 * !0 Errno (see fstat1)
3191 fstat(proc_t p
, struct fstat_args
*uap
, __unused
int32_t *retval
)
3193 return fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 0);
3200 * Description: Extended version of fstat64 supporting returning extended
3201 * security information
3203 * Parameters: p The process doing the fstat
3204 * uap->fd The fd to stat
3205 * uap->ub The user stat buffer
3206 * uap->xsecurity The user extended security
3207 * buffer, or 0 if none
3208 * uap->xsecurity_size The size of xsecurity, or 0
3210 * Returns: 0 Success
3211 * !0 Errno (see fstat1)
3214 fstat64_extended(proc_t p
, struct fstat64_extended_args
*uap
, __unused
int32_t *retval
)
3216 return fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 1);
3223 * Description: Get 64 bit version of the file status for the file associated
3226 * Parameters: p The process doing the fstat
3227 * uap->fd The fd to stat
3228 * uap->ub The user stat buffer
3230 * Returns: 0 Success
3231 * !0 Errno (see fstat1)
3234 fstat64(proc_t p
, struct fstat64_args
*uap
, __unused
int32_t *retval
)
3236 return fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 1);
3243 * Description: Return pathconf information about a file descriptor.
3245 * Parameters: p Process making the request
3246 * uap->fd fd to get information about
3247 * uap->name Name of information desired
3248 * retval Pointer to the call return area
3250 * Returns: 0 Success
3252 * fp_lookup:EBADF Bad file descriptor
3253 * vnode_getwithref:???
3257 * *retval (modified) Returned information (numeric)
3260 fpathconf(proc_t p
, struct fpathconf_args
*uap
, int32_t *retval
)
3263 struct fileproc
*fp
;
3270 AUDIT_ARG(fd
, uap
->fd
);
3271 if ((error
= fp_lookup(p
, fd
, &fp
, 0))) {
3279 if (uap
->name
!= _PC_PIPE_BUF
) {
3288 if (uap
->name
!= _PC_PIPE_BUF
) {
3297 vp
= (struct vnode
*)data
;
3299 if ((error
= vnode_getwithref(vp
)) == 0) {
3300 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
3302 error
= vn_pathconf(vp
, uap
->name
, retval
, vfs_context_current());
3304 (void)vnode_put(vp
);
3314 fp_drop(p
, fd
, fp
, 0);
3319 * Statistics counter for the number of times a process calling fdalloc()
3320 * has resulted in an expansion of the per process open file table.
3322 * XXX This would likely be of more use if it were per process
3330 * Description: Allocate a file descriptor for the process.
3332 * Parameters: p Process to allocate the fd in
3333 * want The fd we would prefer to get
3334 * result Pointer to fd we got
3336 * Returns: 0 Success
3341 * *result (modified) The fd which was allocated
3344 fdalloc(proc_t p
, int want
, int *result
)
3346 struct filedesc
*fdp
= p
->p_fd
;
3348 int lim
, last
, numfiles
, oldnfiles
;
3349 struct fileproc
**newofiles
, **ofiles
;
3350 char *newofileflags
;
3353 * Search for a free descriptor starting at the higher
3354 * of want or fd_freefile. If that fails, consider
3355 * expanding the ofile array.
3358 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
3361 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
3363 last
= min(fdp
->fd_nfiles
, lim
);
3364 if ((i
= want
) < fdp
->fd_freefile
) {
3365 i
= fdp
->fd_freefile
;
3367 for (; i
< last
; i
++) {
3368 if (fdp
->fd_ofiles
[i
] == NULL
&& !(fdp
->fd_ofileflags
[i
] & UF_RESERVED
)) {
3369 procfdtbl_reservefd(p
, i
);
3370 if (i
> fdp
->fd_lastfile
) {
3371 fdp
->fd_lastfile
= i
;
3373 if (want
<= fdp
->fd_freefile
) {
3374 fdp
->fd_freefile
= i
;
3382 * No space in current array. Expand?
3384 if (fdp
->fd_nfiles
>= lim
) {
3387 if (fdp
->fd_nfiles
< NDEXTENT
) {
3388 numfiles
= NDEXTENT
;
3390 numfiles
= 2 * fdp
->fd_nfiles
;
3393 if (numfiles
> lim
) {
3397 MALLOC_ZONE(newofiles
, struct fileproc
**,
3398 numfiles
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
3400 if (newofiles
== NULL
) {
3403 if (fdp
->fd_nfiles
>= numfiles
) {
3404 FREE_ZONE(newofiles
, numfiles
* OFILESIZE
, M_OFILETABL
);
3407 newofileflags
= (char *) &newofiles
[numfiles
];
3409 * Copy the existing ofile and ofileflags arrays
3410 * and zero the new portion of each array.
3412 oldnfiles
= fdp
->fd_nfiles
;
3413 (void) memcpy(newofiles
, fdp
->fd_ofiles
,
3414 oldnfiles
* sizeof(*fdp
->fd_ofiles
));
3415 (void) memset(&newofiles
[oldnfiles
], 0,
3416 (numfiles
- oldnfiles
) * sizeof(*fdp
->fd_ofiles
));
3418 (void) memcpy(newofileflags
, fdp
->fd_ofileflags
,
3419 oldnfiles
* sizeof(*fdp
->fd_ofileflags
));
3420 (void) memset(&newofileflags
[oldnfiles
], 0,
3421 (numfiles
- oldnfiles
) *
3422 sizeof(*fdp
->fd_ofileflags
));
3423 ofiles
= fdp
->fd_ofiles
;
3424 fdp
->fd_ofiles
= newofiles
;
3425 fdp
->fd_ofileflags
= newofileflags
;
3426 fdp
->fd_nfiles
= numfiles
;
3427 FREE_ZONE(ofiles
, oldnfiles
* OFILESIZE
, M_OFILETABL
);
3436 * Description: Check to see whether n user file descriptors are available
3439 * Parameters: p Process to check in
3440 * n The number of fd's desired
3445 * Locks: Assumes proc_fdlock for process is held by the caller
3447 * Notes: The answer only remains valid so long as the proc_fdlock is
3448 * held by the caller.
3451 fdavail(proc_t p
, int n
)
3453 struct filedesc
*fdp
= p
->p_fd
;
3454 struct fileproc
**fpp
;
3458 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
3459 if ((i
= lim
- fdp
->fd_nfiles
) > 0 && (n
-= i
) <= 0) {
3462 fpp
= &fdp
->fd_ofiles
[fdp
->fd_freefile
];
3463 flags
= &fdp
->fd_ofileflags
[fdp
->fd_freefile
];
3464 for (i
= fdp
->fd_nfiles
- fdp
->fd_freefile
; --i
>= 0; fpp
++, flags
++) {
3465 if (*fpp
== NULL
&& !(*flags
& UF_RESERVED
) && --n
<= 0) {
3476 * Description: Legacy KPI wrapper function for _fdrelse
3478 * Parameters: p Process in which fd lives
3483 * Locks: Assumes proc_fdlock for process is held by the caller
3486 fdrelse(proc_t p
, int fd
)
3495 * Description: Get the fileproc pointer for the given fd from the per process
3496 * open file table without taking an explicit reference on it.
3498 * Parameters: p Process containing fd
3499 * fd fd to obtain fileproc for
3500 * resultfp Pointer to pointer return area
3502 * Returns: 0 Success
3506 * *resultfp (modified) Pointer to fileproc pointer
3508 * Locks: Assumes proc_fdlock for process is held by the caller
3510 * Notes: Because there is no reference explicitly taken, the returned
3511 * fileproc pointer is only valid so long as the proc_fdlock
3512 * remains held by the caller.
3515 fdgetf_noref(proc_t p
, int fd
, struct fileproc
**resultfp
)
3517 struct filedesc
*fdp
= p
->p_fd
;
3518 struct fileproc
*fp
;
3520 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3521 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3522 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3535 * Description: Get fileproc and vnode pointer for a given fd from the per
3536 * process open file table of the specified process, and if
3537 * successful, increment the f_iocount
3539 * Parameters: p Process in which fd lives
3540 * fd fd to get information for
3541 * resultfp Pointer to result fileproc
3542 * pointer area, or 0 if none
3543 * resultvp Pointer to result vnode pointer
3544 * area, or 0 if none
3546 * Returns: 0 Success
3547 * EBADF Bad file descriptor
3548 * ENOTSUP fd does not refer to a vnode
3551 * *resultfp (modified) Fileproc pointer
3552 * *resultvp (modified) vnode pointer
3554 * Notes: The resultfp and resultvp fields are optional, and may be
3555 * independently specified as NULL to skip returning information
3557 * Locks: Internally takes and releases proc_fdlock
3560 fp_getfvp(proc_t p
, int fd
, struct fileproc
**resultfp
, struct vnode
**resultvp
)
3562 struct filedesc
*fdp
= p
->p_fd
;
3563 struct fileproc
*fp
;
3565 proc_fdlock_spin(p
);
3566 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3567 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3568 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3572 if (fp
->f_type
!= DTYPE_VNODE
) {
3582 *resultvp
= (struct vnode
*)fp
->f_data
;
3593 * Description: Get fileproc, vnode pointer, and vid for a given fd from the
3594 * per process open file table of the specified process, and if
3595 * successful, increment the f_iocount
3597 * Parameters: p Process in which fd lives
3598 * fd fd to get information for
3599 * resultfp Pointer to result fileproc
3600 * pointer area, or 0 if none
3601 * resultvp Pointer to result vnode pointer
3602 * area, or 0 if none
3603 * vidp Pointer to resuld vid area
3605 * Returns: 0 Success
3606 * EBADF Bad file descriptor
3607 * ENOTSUP fd does not refer to a vnode
3610 * *resultfp (modified) Fileproc pointer
3611 * *resultvp (modified) vnode pointer
3614 * Notes: The resultfp and resultvp fields are optional, and may be
3615 * independently specified as NULL to skip returning information
3617 * Locks: Internally takes and releases proc_fdlock
3620 fp_getfvpandvid(proc_t p
, int fd
, struct fileproc
**resultfp
,
3621 struct vnode
**resultvp
, uint32_t *vidp
)
3623 struct filedesc
*fdp
= p
->p_fd
;
3624 struct fileproc
*fp
;
3626 proc_fdlock_spin(p
);
3627 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3628 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3629 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3633 if (fp
->f_type
!= DTYPE_VNODE
) {
3643 *resultvp
= (struct vnode
*)fp
->f_data
;
3646 *vidp
= (uint32_t)vnode_vid((struct vnode
*)fp
->f_data
);
3657 * Description: Get fileproc and socket pointer for a given fd from the
3658 * per process open file table of the specified process, and if
3659 * successful, increment the f_iocount
3661 * Parameters: p Process in which fd lives
3662 * fd fd to get information for
3663 * resultfp Pointer to result fileproc
3664 * pointer area, or 0 if none
3665 * results Pointer to result socket
3666 * pointer area, or 0 if none
3668 * Returns: EBADF The file descriptor is invalid
3669 * EOPNOTSUPP The file descriptor is not a socket
3673 * *resultfp (modified) Fileproc pointer
3674 * *results (modified) socket pointer
3676 * Notes: EOPNOTSUPP should probably be ENOTSOCK; this function is only
3677 * ever called from accept1().
3680 fp_getfsock(proc_t p
, int fd
, struct fileproc
**resultfp
,
3681 struct socket
**results
)
3683 struct filedesc
*fdp
= p
->p_fd
;
3684 struct fileproc
*fp
;
3686 proc_fdlock_spin(p
);
3687 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3688 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3689 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3693 if (fp
->f_type
!= DTYPE_SOCKET
) {
3703 *results
= (struct socket
*)fp
->f_data
;
3714 * Description: Get fileproc and kqueue pointer for a given fd from the
3715 * per process open file table of the specified process, and if
3716 * successful, increment the f_iocount
3718 * Parameters: p Process in which fd lives
3719 * fd fd to get information for
3720 * resultfp Pointer to result fileproc
3721 * pointer area, or 0 if none
3722 * resultkq Pointer to result kqueue
3723 * pointer area, or 0 if none
3725 * Returns: EBADF The file descriptor is invalid
3726 * EBADF The file descriptor is not a socket
3730 * *resultfp (modified) Fileproc pointer
3731 * *resultkq (modified) kqueue pointer
3733 * Notes: The second EBADF should probably be something else to make
3734 * the error condition distinct.
3737 fp_getfkq(proc_t p
, int fd
, struct fileproc
**resultfp
,
3738 struct kqueue
**resultkq
)
3740 struct filedesc
*fdp
= p
->p_fd
;
3741 struct fileproc
*fp
;
3743 proc_fdlock_spin(p
);
3744 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3745 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3746 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3750 if (fp
->f_type
!= DTYPE_KQUEUE
) {
3760 *resultkq
= (struct kqueue
*)fp
->f_data
;
3771 * Description: Get fileproc and POSIX shared memory pointer for a given fd
3772 * from the per process open file table of the specified process
3773 * and if successful, increment the f_iocount
3775 * Parameters: p Process in which fd lives
3776 * fd fd to get information for
3777 * resultfp Pointer to result fileproc
3778 * pointer area, or 0 if none
3779 * resultpshm Pointer to result POSIX
3780 * shared memory pointer
3781 * pointer area, or 0 if none
3783 * Returns: EBADF The file descriptor is invalid
3784 * EBADF The file descriptor is not a POSIX
3785 * shared memory area
3789 * *resultfp (modified) Fileproc pointer
3790 * *resultpshm (modified) POSIX shared memory pointer
3792 * Notes: The second EBADF should probably be something else to make
3793 * the error condition distinct.
3796 fp_getfpshm(proc_t p
, int fd
, struct fileproc
**resultfp
,
3797 struct pshmnode
**resultpshm
)
3799 struct filedesc
*fdp
= p
->p_fd
;
3800 struct fileproc
*fp
;
3802 proc_fdlock_spin(p
);
3803 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3804 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3805 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3809 if (fp
->f_type
!= DTYPE_PSXSHM
) {
3819 *resultpshm
= (struct pshmnode
*)fp
->f_data
;
3830 * Description: Get fileproc and POSIX semaphore pointer for a given fd from
3831 * the per process open file table of the specified process
3832 * and if successful, increment the f_iocount
3834 * Parameters: p Process in which fd lives
3835 * fd fd to get information for
3836 * resultfp Pointer to result fileproc
3837 * pointer area, or 0 if none
3838 * resultpsem Pointer to result POSIX
3839 * semaphore pointer area, or
3842 * Returns: EBADF The file descriptor is invalid
3843 * EBADF The file descriptor is not a POSIX
3848 * *resultfp (modified) Fileproc pointer
3849 * *resultpsem (modified) POSIX semaphore pointer
3851 * Notes: The second EBADF should probably be something else to make
3852 * the error condition distinct.
3854 * In order to support unnamed POSIX semaphores, the named
3855 * POSIX semaphores will have to move out of the per-process
3856 * open filetable, and into a global table that is shared with
3857 * unnamed POSIX semaphores, since unnamed POSIX semaphores
3858 * are typically used by declaring instances in shared memory,
3859 * and there's no other way to do this without changing the
3860 * underlying type, which would introduce binary compatibility
3864 fp_getfpsem(proc_t p
, int fd
, struct fileproc
**resultfp
,
3865 struct psemnode
**resultpsem
)
3867 struct filedesc
*fdp
= p
->p_fd
;
3868 struct fileproc
*fp
;
3870 proc_fdlock_spin(p
);
3871 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3872 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3873 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3877 if (fp
->f_type
!= DTYPE_PSXSEM
) {
3887 *resultpsem
= (struct psemnode
*)fp
->f_data
;
3898 * Description: Get fileproc and pipe pointer for a given fd from the
3899 * per process open file table of the specified process
3900 * and if successful, increment the f_iocount
3902 * Parameters: p Process in which fd lives
3903 * fd fd to get information for
3904 * resultfp Pointer to result fileproc
3905 * pointer area, or 0 if none
3906 * resultpipe Pointer to result pipe
3907 * pointer area, or 0 if none
3909 * Returns: EBADF The file descriptor is invalid
3910 * EBADF The file descriptor is not a socket
3914 * *resultfp (modified) Fileproc pointer
3915 * *resultpipe (modified) pipe pointer
3917 * Notes: The second EBADF should probably be something else to make
3918 * the error condition distinct.
3921 fp_getfpipe(proc_t p
, int fd
, struct fileproc
**resultfp
,
3922 struct pipe
**resultpipe
)
3924 struct filedesc
*fdp
= p
->p_fd
;
3925 struct fileproc
*fp
;
3927 proc_fdlock_spin(p
);
3928 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3929 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3930 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3934 if (fp
->f_type
!= DTYPE_PIPE
) {
3944 *resultpipe
= (struct pipe
*)fp
->f_data
;
3955 * Description: Get fileproc pointer for a given fd from the per process
3956 * open file table of the specified process and if successful,
3957 * increment the f_iocount
3959 * Parameters: p Process in which fd lives
3960 * fd fd to get information for
3961 * resultfp Pointer to result fileproc
3962 * pointer area, or 0 if none
3963 * locked !0 if the caller holds the
3964 * proc_fdlock, 0 otherwise
3966 * Returns: 0 Success
3967 * EBADF Bad file descriptor
3970 * *resultfp (modified) Fileproc pointer
3972 * Locks: If the argument 'locked' is non-zero, then the caller is
3973 * expected to have taken and held the proc_fdlock; if it is
3974 * zero, than this routine internally takes and drops this lock.
3977 fp_lookup(proc_t p
, int fd
, struct fileproc
**resultfp
, int locked
)
3979 struct filedesc
*fdp
= p
->p_fd
;
3980 struct fileproc
*fp
;
3983 proc_fdlock_spin(p
);
3985 if (fd
< 0 || fdp
== NULL
|| fd
>= fdp
->fd_nfiles
||
3986 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3987 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
4009 * Description: Swap the fileproc pointer for a given fd with a new
4010 * fileproc pointer in the per-process open file table of
4011 * the specified process. The fdlock must be held at entry.
4013 * Parameters: p Process containing the fd
4014 * fd The fd of interest
4015 * nfp Pointer to the newfp
4017 * Returns: 0 Success
4018 * EBADF Bad file descriptor
4020 * EKEEPLOOKING f_iocount changed while lock was dropped.
4023 fp_tryswap(proc_t p
, int fd
, struct fileproc
*nfp
)
4025 struct fileproc
*fp
;
4028 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
4030 if (0 != (error
= fp_lookup(p
, fd
, &fp
, 1))) {
4034 * At this point, our caller (change_guardedfd_np) has
4035 * one f_iocount reference, and we just took another
4036 * one to begin the replacement.
4038 if (fp
->f_iocount
< 2) {
4039 panic("f_iocount too small %d", fp
->f_iocount
);
4040 } else if (2 == fp
->f_iocount
) {
4041 /* Copy the contents of *fp, preserving the "type" of *nfp */
4043 nfp
->f_flags
= (nfp
->f_flags
& FP_TYPEMASK
) |
4044 (fp
->f_flags
& ~FP_TYPEMASK
);
4045 nfp
->f_iocount
= fp
->f_iocount
;
4046 nfp
->f_fglob
= fp
->f_fglob
;
4047 nfp
->f_wset
= fp
->f_wset
;
4049 p
->p_fd
->fd_ofiles
[fd
] = nfp
;
4050 (void) fp_drop(p
, fd
, nfp
, 1);
4053 * Wait for all other active references to evaporate.
4055 p
->p_fpdrainwait
= 1;
4056 error
= msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
,
4057 PRIBIO
| PCATCH
, "tryswap fpdrain", NULL
);
4060 * Return an "internal" errno to trigger a full
4061 * reevaluation of the change-guard attempt.
4063 error
= EKEEPLOOKING
;
4064 printf("%s: lookup collision fd %d\n", __func__
, fd
);
4066 (void) fp_drop(p
, fd
, fp
, 1);
4075 * Description: Set the FP_WRITTEN flag on the fileproc and drop the I/O
4076 * reference previously taken by calling fp_lookup et. al.
4078 * Parameters: p Process in which the fd lives
4079 * fd fd associated with the fileproc
4080 * fp fileproc on which to set the
4081 * flag and drop the reference
4083 * Returns: 0 Success
4084 * fp_drop:EBADF Bad file descriptor
4086 * Locks: This function internally takes and drops the proc_fdlock for
4087 * the supplied process
4089 * Notes: The fileproc must correspond to the fd in the supplied proc
4092 fp_drop_written(proc_t p
, int fd
, struct fileproc
*fp
)
4096 proc_fdlock_spin(p
);
4098 fp
->f_flags
|= FP_WRITTEN
;
4100 error
= fp_drop(p
, fd
, fp
, 1);
4111 * Description: Set the FP_WAITEVENT flag on the fileproc and drop the I/O
4112 * reference previously taken by calling fp_lookup et. al.
4114 * Parameters: p Process in which the fd lives
4115 * fd fd associated with the fileproc
4116 * fp fileproc on which to set the
4117 * flag and drop the reference
4119 * Returns: 0 Success
4120 * fp_drop:EBADF Bad file descriptor
4122 * Locks: This function internally takes and drops the proc_fdlock for
4123 * the supplied process
4125 * Notes: The fileproc must correspond to the fd in the supplied proc
4128 fp_drop_event(proc_t p
, int fd
, struct fileproc
*fp
)
4132 proc_fdlock_spin(p
);
4134 fp
->f_flags
|= FP_WAITEVENT
;
4136 error
= fp_drop(p
, fd
, fp
, 1);
4147 * Description: Drop the I/O reference previously taken by calling fp_lookup
4150 * Parameters: p Process in which the fd lives
4151 * fd fd associated with the fileproc
4152 * fp fileproc on which to set the
4153 * flag and drop the reference
4154 * locked flag to internally take and
4155 * drop proc_fdlock if it is not
4156 * already held by the caller
4158 * Returns: 0 Success
4159 * EBADF Bad file descriptor
4161 * Locks: This function internally takes and drops the proc_fdlock for
4162 * the supplied process if 'locked' is non-zero, and assumes that
4163 * the caller already holds this lock if 'locked' is non-zero.
4165 * Notes: The fileproc must correspond to the fd in the supplied proc
4168 fp_drop(proc_t p
, int fd
, struct fileproc
*fp
, int locked
)
4170 struct filedesc
*fdp
= p
->p_fd
;
4174 proc_fdlock_spin(p
);
4176 if ((fp
== FILEPROC_NULL
) && (fd
< 0 || fd
>= fdp
->fd_nfiles
||
4177 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
4178 ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
4179 !(fdp
->fd_ofileflags
[fd
] & UF_CLOSING
)))) {
4187 if (fp
->f_iocount
== 0) {
4188 if (fp
->f_flags
& FP_SELCONFLICT
) {
4189 fp
->f_flags
&= ~FP_SELCONFLICT
;
4192 if (p
->p_fpdrainwait
) {
4193 p
->p_fpdrainwait
= 0;
4201 wakeup(&p
->p_fpdrainwait
);
4211 * Description: Given an fd, look it up in the current process's per process
4212 * open file table, and return its internal vnode pointer.
4214 * Parameters: fd fd to obtain vnode from
4215 * vpp pointer to vnode return area
4217 * Returns: 0 Success
4218 * EINVAL The fd does not refer to a
4219 * vnode fileproc entry
4220 * fp_lookup:EBADF Bad file descriptor
4223 * *vpp (modified) Returned vnode pointer
4225 * Locks: This function internally takes and drops the proc_fdlock for
4226 * the current process
4228 * Notes: If successful, this function increments the f_iocount on the
4229 * fd's corresponding fileproc.
4231 * The fileproc referenced is not returned; because of this, care
4232 * must be taken to not drop the last reference (e.g. by closing
4233 * the file). This is inherently unsafe, since the reference may
4234 * not be recoverable from the vnode, if there is a subsequent
4235 * close that destroys the associate fileproc. The caller should
4236 * therefore retain their own reference on the fileproc so that
4237 * the f_iocount can be dropped subsequently. Failure to do this
4238 * can result in the returned pointer immediately becoming invalid
4239 * following the call.
4241 * Use of this function is discouraged.
4244 file_vnode(int fd
, struct vnode
**vpp
)
4246 proc_t p
= current_proc();
4247 struct fileproc
*fp
;
4250 proc_fdlock_spin(p
);
4251 if ((error
= fp_lookup(p
, fd
, &fp
, 1))) {
4255 if (fp
->f_type
!= DTYPE_VNODE
) {
4256 fp_drop(p
, fd
, fp
, 1);
4261 *vpp
= (struct vnode
*)fp
->f_data
;
4270 * file_vnode_withvid
4272 * Description: Given an fd, look it up in the current process's per process
4273 * open file table, and return its internal vnode pointer.
4275 * Parameters: fd fd to obtain vnode from
4276 * vpp pointer to vnode return area
4277 * vidp pointer to vid of the returned vnode
4279 * Returns: 0 Success
4280 * EINVAL The fd does not refer to a
4281 * vnode fileproc entry
4282 * fp_lookup:EBADF Bad file descriptor
4285 * *vpp (modified) Returned vnode pointer
4287 * Locks: This function internally takes and drops the proc_fdlock for
4288 * the current process
4290 * Notes: If successful, this function increments the f_iocount on the
4291 * fd's corresponding fileproc.
4293 * The fileproc referenced is not returned; because of this, care
4294 * must be taken to not drop the last reference (e.g. by closing
4295 * the file). This is inherently unsafe, since the reference may
4296 * not be recoverable from the vnode, if there is a subsequent
4297 * close that destroys the associate fileproc. The caller should
4298 * therefore retain their own reference on the fileproc so that
4299 * the f_iocount can be dropped subsequently. Failure to do this
4300 * can result in the returned pointer immediately becoming invalid
4301 * following the call.
4303 * Use of this function is discouraged.
4306 file_vnode_withvid(int fd
, struct vnode
**vpp
, uint32_t * vidp
)
4308 proc_t p
= current_proc();
4309 struct fileproc
*fp
;
4313 proc_fdlock_spin(p
);
4314 if ((error
= fp_lookup(p
, fd
, &fp
, 1))) {
4318 if (fp
->f_type
!= DTYPE_VNODE
) {
4319 fp_drop(p
, fd
, fp
, 1);
4323 vp
= (struct vnode
*)fp
->f_data
;
4328 if ((vidp
!= NULL
) && (vp
!= NULLVP
)) {
4329 *vidp
= (uint32_t)vp
->v_id
;
4341 * Description: Given an fd, look it up in the current process's per process
4342 * open file table, and return its internal socket pointer.
4344 * Parameters: fd fd to obtain vnode from
4345 * sp pointer to socket return area
4347 * Returns: 0 Success
4348 * ENOTSOCK Not a socket
4349 * fp_lookup:EBADF Bad file descriptor
4352 * *sp (modified) Returned socket pointer
4354 * Locks: This function internally takes and drops the proc_fdlock for
4355 * the current process
4357 * Notes: If successful, this function increments the f_iocount on the
4358 * fd's corresponding fileproc.
4360 * The fileproc referenced is not returned; because of this, care
4361 * must be taken to not drop the last reference (e.g. by closing
4362 * the file). This is inherently unsafe, since the reference may
4363 * not be recoverable from the socket, if there is a subsequent
4364 * close that destroys the associate fileproc. The caller should
4365 * therefore retain their own reference on the fileproc so that
4366 * the f_iocount can be dropped subsequently. Failure to do this
4367 * can result in the returned pointer immediately becoming invalid
4368 * following the call.
4370 * Use of this function is discouraged.
4373 file_socket(int fd
, struct socket
**sp
)
4375 proc_t p
= current_proc();
4376 struct fileproc
*fp
;
4379 proc_fdlock_spin(p
);
4380 if ((error
= fp_lookup(p
, fd
, &fp
, 1))) {
4384 if (fp
->f_type
!= DTYPE_SOCKET
) {
4385 fp_drop(p
, fd
, fp
, 1);
4389 *sp
= (struct socket
*)fp
->f_data
;
4399 * Description: Given an fd, look it up in the current process's per process
4400 * open file table, and return its fileproc's flags field.
4402 * Parameters: fd fd whose flags are to be
4404 * flags pointer to flags data area
4406 * Returns: 0 Success
4407 * ENOTSOCK Not a socket
4408 * fp_lookup:EBADF Bad file descriptor
4411 * *flags (modified) Returned flags field
4413 * Locks: This function internally takes and drops the proc_fdlock for
4414 * the current process
4416 * Notes: This function will internally increment and decrement the
4417 * f_iocount of the fileproc as part of its operation.
4420 file_flags(int fd
, int *flags
)
4422 proc_t p
= current_proc();
4423 struct fileproc
*fp
;
4426 proc_fdlock_spin(p
);
4427 if ((error
= fp_lookup(p
, fd
, &fp
, 1))) {
4431 *flags
= (int)fp
->f_flag
;
4432 fp_drop(p
, fd
, fp
, 1);
4442 * Description: Drop an iocount reference on an fd, and wake up any waiters
4443 * for draining (i.e. blocked in fileproc_drain() called during
4444 * the last attempt to close a file).
4446 * Parameters: fd fd on which an ioreference is
4449 * Returns: 0 Success
4450 * EBADF Bad file descriptor
4452 * Description: Given an fd, look it up in the current process's per process
4453 * open file table, and drop it's fileproc's f_iocount by one
4455 * Notes: This is intended as a corresponding operation to the functions
4456 * file_vnode() and file_socket() operations.
4458 * Technically, the close reference is supposed to be protected
4459 * by a fileproc_drain(), however, a drain will only block if
4460 * the fd refers to a character device, and that device has had
4461 * preparefileread() called on it. If it refers to something
4462 * other than a character device, then the drain will occur and
4463 * block each close attempt, rather than merely the last close.
4465 * Since it's possible for an fd that refers to a character
4466 * device to have an intermediate close followed by an open to
4467 * cause a different file to correspond to that descriptor,
4468 * unless there was a cautionary reference taken on the fileproc,
4469 * this is an inherently unsafe function. This happens in the
4470 * case where multiple fd's in a process refer to the same
4471 * character device (e.g. stdin/out/err pointing to a tty, etc.).
4473 * Use of this function is discouraged.
4478 struct fileproc
*fp
;
4479 proc_t p
= current_proc();
4482 proc_fdlock_spin(p
);
4483 if (fd
< 0 || fd
>= p
->p_fd
->fd_nfiles
||
4484 (fp
= p
->p_fd
->fd_ofiles
[fd
]) == NULL
||
4485 ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
4486 !(p
->p_fd
->fd_ofileflags
[fd
] & UF_CLOSING
))) {
4492 if (fp
->f_iocount
== 0) {
4493 if (fp
->f_flags
& FP_SELCONFLICT
) {
4494 fp
->f_flags
&= ~FP_SELCONFLICT
;
4497 if (p
->p_fpdrainwait
) {
4498 p
->p_fpdrainwait
= 0;
4505 wakeup(&p
->p_fpdrainwait
);
4511 static int falloc_withalloc_locked(proc_t
, struct fileproc
**, int *,
4512 vfs_context_t
, struct fileproc
* (*)(void *), void *, int);
4517 * Description: Allocate an entry in the per process open file table and
4518 * return the corresponding fileproc and fd.
4520 * Parameters: p The process in whose open file
4521 * table the fd is to be allocated
4522 * resultfp Pointer to fileproc pointer
4524 * resultfd Pointer to fd return area
4527 * Returns: 0 Success
4528 * falloc:ENFILE Too many open files in system
4529 * falloc:EMFILE Too many open files in process
4530 * falloc:ENOMEM M_FILEPROC or M_FILEGLOB zone
4534 * *resultfd (modified) Returned fileproc pointer
4535 * *resultfd (modified) Returned fd
4537 * Locks: This function takes and drops the proc_fdlock; if this lock
4538 * is already held, use falloc_locked() instead.
4540 * Notes: This function takes separate process and context arguments
4541 * solely to support kern_exec.c; otherwise, it would take
4542 * neither, and expect falloc_locked() to use the
4543 * vfs_context_current() routine internally.
4546 falloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
)
4548 return falloc_withalloc(p
, resultfp
, resultfd
, ctx
,
4549 fileproc_alloc_init
, NULL
);
4553 * Like falloc, but including the fileproc allocator and create-args
4556 falloc_withalloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4557 vfs_context_t ctx
, fp_allocfn_t fp_zalloc
, void *arg
)
4562 error
= falloc_withalloc_locked(p
,
4563 resultfp
, resultfd
, ctx
, fp_zalloc
, arg
, 1);
4570 * "uninitialized" ops -- ensure fg->fg_ops->fo_type always exists
4572 static const struct fileops uninitops
;
4577 * Create a new open file structure and allocate
4578 * a file descriptor for the process that refers to it.
4580 * Returns: 0 Success
4582 * Description: Allocate an entry in the per process open file table and
4583 * return the corresponding fileproc and fd.
4585 * Parameters: p The process in whose open file
4586 * table the fd is to be allocated
4587 * resultfp Pointer to fileproc pointer
4589 * resultfd Pointer to fd return area
4591 * locked Flag to indicate whether the
4592 * caller holds proc_fdlock
4594 * Returns: 0 Success
4595 * ENFILE Too many open files in system
4596 * fdalloc:EMFILE Too many open files in process
4597 * ENOMEM M_FILEPROC or M_FILEGLOB zone
4602 * *resultfd (modified) Returned fileproc pointer
4603 * *resultfd (modified) Returned fd
4605 * Locks: If the parameter 'locked' is zero, this function takes and
4606 * drops the proc_fdlock; if non-zero, the caller must hold the
4609 * Notes: If you intend to use a non-zero 'locked' parameter, use the
4610 * utility function falloc() instead.
4612 * This function takes separate process and context arguments
4613 * solely to support kern_exec.c; otherwise, it would take
4614 * neither, and use the vfs_context_current() routine internally.
4617 falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4618 vfs_context_t ctx
, int locked
)
4620 return falloc_withalloc_locked(p
, resultfp
, resultfd
, ctx
,
4621 fileproc_alloc_init
, NULL
, locked
);
4625 falloc_withalloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4626 vfs_context_t ctx
, fp_allocfn_t fp_zalloc
, void *crarg
,
4629 struct fileproc
*fp
;
4630 struct fileglob
*fg
;
4636 if ((error
= fdalloc(p
, 0, &nfd
))) {
4642 if (nfiles
>= maxfiles
) {
4650 error
= mac_file_check_create(proc_ucred(p
));
4660 * Allocate a new file descriptor.
4661 * If the process has file descriptor zero open, add to the list
4662 * of open files at that point, otherwise put it at the front of
4663 * the list of open files.
4667 fp
= (*fp_zalloc
)(crarg
);
4674 MALLOC_ZONE(fg
, struct fileglob
*, sizeof(struct fileglob
), M_FILEGLOB
, M_WAITOK
);
4682 bzero(fg
, sizeof(struct fileglob
));
4683 lck_mtx_init(&fg
->fg_lock
, file_lck_grp
, file_lck_attr
);
4687 fg
->fg_ops
= &uninitops
;
4690 mac_file_label_init(fg
);
4693 kauth_cred_ref(ctx
->vc_ucred
);
4697 fp
->f_cred
= ctx
->vc_ucred
;
4700 mac_file_label_associate(fp
->f_cred
, fg
);
4703 OSAddAtomic(1, &nfiles
);
4705 p
->p_fd
->fd_ofiles
[nfd
] = fp
;
4725 * Description: Free a file structure; drop the global open file count, and
4726 * drop the credential reference, if the fileglob has one, and
4727 * destroy the instance mutex before freeing
4729 * Parameters: fg Pointer to fileglob to be
4735 fg_free(struct fileglob
*fg
)
4737 OSAddAtomic(-1, &nfiles
);
4739 if (fg
->fg_vn_data
) {
4740 fg_vn_data_free(fg
->fg_vn_data
);
4741 fg
->fg_vn_data
= NULL
;
4744 if (IS_VALID_CRED(fg
->fg_cred
)) {
4745 kauth_cred_unref(&fg
->fg_cred
);
4747 lck_mtx_destroy(&fg
->fg_lock
, file_lck_grp
);
4750 mac_file_label_destroy(fg
);
4752 FREE_ZONE(fg
, sizeof *fg
, M_FILEGLOB
);
4760 * Description: Perform close-on-exec processing for all files in a process
4761 * that are either marked as close-on-exec, or which were in the
4762 * process of being opened at the time of the execve
4764 * Also handles the case (via posix_spawn()) where -all-
4765 * files except those marked with "inherit" as treated as
4768 * Parameters: p Pointer to process calling
4773 * Locks: This function internally takes and drops proc_fdlock()
4774 * But assumes tables don't grow/change while unlocked.
4778 fdexec(proc_t p
, short flags
, int self_exec
)
4780 struct filedesc
*fdp
= p
->p_fd
;
4782 boolean_t cloexec_default
= (flags
& POSIX_SPAWN_CLOEXEC_DEFAULT
) != 0;
4783 thread_t self
= current_thread();
4784 struct uthread
*ut
= get_bsdthread_info(self
);
4785 struct kqueue
*dealloc_kq
= NULL
;
4788 * If the current thread is bound as a workq/workloop
4789 * servicing thread, we need to unbind it first.
4791 if (ut
->uu_kqr_bound
&& self_exec
) {
4792 kqueue_threadreq_unbind(p
, ut
->uu_kqr_bound
);
4798 * Deallocate the knotes for this process
4799 * and mark the tables non-existent so
4800 * subsequent kqueue closes go faster.
4803 assert(fdp
->fd_knlistsize
== -1);
4804 assert(fdp
->fd_knhashmask
== 0);
4806 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
4807 struct fileproc
*fp
= fdp
->fd_ofiles
[i
];
4808 char *flagp
= &fdp
->fd_ofileflags
[i
];
4810 if (fp
&& cloexec_default
) {
4812 * Reverse the usual semantics of file descriptor
4813 * inheritance - all of them should be closed
4814 * except files marked explicitly as "inherit" and
4815 * not marked close-on-exec.
4817 if ((*flagp
& (UF_EXCLOSE
| UF_INHERIT
)) != UF_INHERIT
) {
4818 *flagp
|= UF_EXCLOSE
;
4820 *flagp
&= ~UF_INHERIT
;
4824 ((*flagp
& (UF_RESERVED
| UF_EXCLOSE
)) == UF_EXCLOSE
)
4826 || (fp
&& mac_file_check_inherit(proc_ucred(p
), fp
->f_fglob
))
4829 procfdtbl_clearfd(p
, i
);
4830 if (i
== fdp
->fd_lastfile
&& i
> 0) {
4833 if (i
< fdp
->fd_freefile
) {
4834 fdp
->fd_freefile
= i
;
4838 * Wait for any third party viewers (e.g., lsof)
4839 * to release their references to this fileproc.
4841 while (fp
->f_iocount
> 0) {
4842 p
->p_fpdrainwait
= 1;
4843 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
,
4846 if (fp
->f_flags
& FP_WAITEVENT
) {
4847 (void)waitevent_close(p
, fp
);
4849 closef_locked(fp
, fp
->f_fglob
, p
);
4855 /* release the per-process workq kq */
4856 if (fdp
->fd_wqkqueue
) {
4857 dealloc_kq
= fdp
->fd_wqkqueue
;
4858 fdp
->fd_wqkqueue
= NULL
;
4863 /* Anything to free? */
4865 kqueue_dealloc(dealloc_kq
);
4873 * Description: Copy a filedesc structure. This is normally used as part of
4874 * forkproc() when forking a new process, to copy the per process
4875 * open file table over to the new process.
4877 * Parameters: p Process whose open file table
4878 * is to be copied (parent)
4879 * uth_cdir Per thread current working
4880 * cirectory, or NULL
4882 * Returns: NULL Copy failed
4883 * !NULL Pointer to new struct filedesc
4885 * Locks: This function internally takes and drops proc_fdlock()
4887 * Notes: Files are copied directly, ignoring the new resource limits
4888 * for the process that's being copied into. Since the descriptor
4889 * references are just additional references, this does not count
4890 * against the number of open files on the system.
4892 * The struct filedesc includes the current working directory,
4893 * and the current root directory, if the process is chroot'ed.
4895 * If the exec was called by a thread using a per thread current
4896 * working directory, we inherit the working directory from the
4897 * thread making the call, rather than from the process.
4899 * In the case of a failure to obtain a reference, for most cases,
4900 * the file entry will be silently dropped. There's an exception
4901 * for the case of a chroot dir, since a failure to to obtain a
4902 * reference there would constitute an "escape" from the chroot
4903 * environment, which must not be allowed. In that case, we will
4904 * deny the execve() operation, rather than allowing the escape.
4907 fdcopy(proc_t p
, vnode_t uth_cdir
)
4909 struct filedesc
*newfdp
, *fdp
= p
->p_fd
;
4911 struct fileproc
*ofp
, *fp
;
4914 MALLOC_ZONE(newfdp
, struct filedesc
*,
4915 sizeof(*newfdp
), M_FILEDESC
, M_WAITOK
);
4916 if (newfdp
== NULL
) {
4923 * the FD_CHROOT flag will be inherited via this copy
4925 (void) memcpy(newfdp
, fdp
, sizeof(*newfdp
));
4928 * If we are running with per-thread current working directories,
4929 * inherit the new current working directory from the current thread
4930 * instead, before we take our references.
4932 if (uth_cdir
!= NULLVP
) {
4933 newfdp
->fd_cdir
= uth_cdir
;
4937 * For both fd_cdir and fd_rdir make sure we get
4938 * a valid reference... if we can't, than set
4939 * set the pointer(s) to NULL in the child... this
4940 * will keep us from using a non-referenced vp
4941 * and allows us to do the vnode_rele only on
4942 * a properly referenced vp
4944 if ((v_dir
= newfdp
->fd_cdir
)) {
4945 if (vnode_getwithref(v_dir
) == 0) {
4946 if ((vnode_ref(v_dir
))) {
4947 newfdp
->fd_cdir
= NULL
;
4951 newfdp
->fd_cdir
= NULL
;
4954 if (newfdp
->fd_cdir
== NULL
&& fdp
->fd_cdir
) {
4956 * we couldn't get a new reference on
4957 * the current working directory being
4958 * inherited... we might as well drop
4959 * our reference from the parent also
4960 * since the vnode has gone DEAD making
4961 * it useless... by dropping it we'll
4962 * be that much closer to recycling it
4964 vnode_rele(fdp
->fd_cdir
);
4965 fdp
->fd_cdir
= NULL
;
4968 if ((v_dir
= newfdp
->fd_rdir
)) {
4969 if (vnode_getwithref(v_dir
) == 0) {
4970 if ((vnode_ref(v_dir
))) {
4971 newfdp
->fd_rdir
= NULL
;
4975 newfdp
->fd_rdir
= NULL
;
4978 /* Coming from a chroot environment and unable to get a reference... */
4979 if (newfdp
->fd_rdir
== NULL
&& fdp
->fd_rdir
) {
4981 * We couldn't get a new reference on
4982 * the chroot directory being
4983 * inherited... this is fatal, since
4984 * otherwise it would constitute an
4985 * escape from a chroot environment by
4988 if (newfdp
->fd_cdir
) {
4989 vnode_rele(newfdp
->fd_cdir
);
4991 FREE_ZONE(newfdp
, sizeof *newfdp
, M_FILEDESC
);
4996 * If the number of open files fits in the internal arrays
4997 * of the open file structure, use them, otherwise allocate
4998 * additional memory for the number of descriptors currently
5001 if (newfdp
->fd_lastfile
< NDFILE
) {
5005 * Compute the smallest multiple of NDEXTENT needed
5006 * for the file descriptors currently in use,
5007 * allowing the table to shrink.
5009 i
= newfdp
->fd_nfiles
;
5010 while (i
> 1 + 2 * NDEXTENT
&& i
> 1 + newfdp
->fd_lastfile
* 2) {
5016 MALLOC_ZONE(newfdp
->fd_ofiles
, struct fileproc
**,
5017 i
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
5018 if (newfdp
->fd_ofiles
== NULL
) {
5019 if (newfdp
->fd_cdir
) {
5020 vnode_rele(newfdp
->fd_cdir
);
5022 if (newfdp
->fd_rdir
) {
5023 vnode_rele(newfdp
->fd_rdir
);
5026 FREE_ZONE(newfdp
, sizeof(*newfdp
), M_FILEDESC
);
5029 (void) memset(newfdp
->fd_ofiles
, 0, i
* OFILESIZE
);
5032 newfdp
->fd_ofileflags
= (char *) &newfdp
->fd_ofiles
[i
];
5033 newfdp
->fd_nfiles
= i
;
5035 if (fdp
->fd_nfiles
> 0) {
5036 struct fileproc
**fpp
;
5039 (void) memcpy(newfdp
->fd_ofiles
, fdp
->fd_ofiles
,
5040 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofiles
));
5041 (void) memcpy(newfdp
->fd_ofileflags
, fdp
->fd_ofileflags
,
5042 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofileflags
));
5044 fpp
= &newfdp
->fd_ofiles
[newfdp
->fd_lastfile
];
5045 flags
= &newfdp
->fd_ofileflags
[newfdp
->fd_lastfile
];
5046 for (i
= newfdp
->fd_lastfile
; i
>= 0; i
--, fpp
--, flags
--) {
5047 if ((ofp
= *fpp
) != NULL
&&
5048 0 == (ofp
->f_fglob
->fg_lflags
& FG_CONFINED
) &&
5049 0 == (*flags
& (UF_FORKCLOSE
| UF_RESERVED
))) {
5051 if (FILEPROC_TYPE(ofp
) != FTYPE_SIMPLE
) {
5052 panic("complex fileproc");
5055 fp
= fileproc_alloc_init(NULL
);
5058 * XXX no room to copy, unable to
5059 * XXX safely unwind state at present
5064 (ofp
->f_flags
& ~FP_TYPEMASK
);
5065 fp
->f_fglob
= ofp
->f_fglob
;
5074 if (i
== newfdp
->fd_lastfile
&& i
> 0) {
5075 newfdp
->fd_lastfile
--;
5077 if (i
< newfdp
->fd_freefile
) {
5078 newfdp
->fd_freefile
= i
;
5087 * Initialize knote and kqueue tracking structs
5089 newfdp
->fd_knlist
= NULL
;
5090 newfdp
->fd_knlistsize
= -1;
5091 newfdp
->fd_knhash
= NULL
;
5092 newfdp
->fd_knhashmask
= 0;
5093 newfdp
->fd_kqhash
= NULL
;
5094 newfdp
->fd_kqhashmask
= 0;
5095 newfdp
->fd_wqkqueue
= NULL
;
5096 lck_mtx_init(&newfdp
->fd_kqhashlock
, proc_kqhashlock_grp
, proc_lck_attr
);
5097 lck_mtx_init(&newfdp
->fd_knhashlock
, proc_knhashlock_grp
, proc_lck_attr
);
5106 * Description: Release a filedesc (per process open file table) structure;
5107 * this is done on process exit(), or from forkproc_free() if
5108 * the fork fails for some reason subsequent to a successful
5111 * Parameters: p Pointer to process going away
5115 * Locks: This function internally takes and drops proc_fdlock()
5120 struct filedesc
*fdp
;
5121 struct fileproc
*fp
;
5122 struct kqueue
*dealloc_kq
= NULL
;
5127 if (p
== kernproc
|| NULL
== (fdp
= p
->p_fd
)) {
5132 extern struct filedesc filedesc0
;
5134 if (&filedesc0
== fdp
) {
5139 * deallocate all the knotes up front and claim empty
5140 * tables to make any subsequent kqueue closes faster.
5143 assert(fdp
->fd_knlistsize
== -1);
5144 assert(fdp
->fd_knhashmask
== 0);
5147 * dealloc all workloops that have outstanding retains
5148 * when created with scheduling parameters.
5150 kqworkloops_dealloc(p
);
5152 /* close file descriptors */
5153 if (fdp
->fd_nfiles
> 0 && fdp
->fd_ofiles
) {
5154 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
5155 if ((fp
= fdp
->fd_ofiles
[i
]) != NULL
) {
5156 if (fdp
->fd_ofileflags
[i
] & UF_RESERVED
) {
5157 panic("fdfree: found fp with UF_RESERVED");
5160 procfdtbl_reservefd(p
, i
);
5162 if (fp
->f_flags
& FP_WAITEVENT
) {
5163 (void)waitevent_close(p
, fp
);
5165 (void) closef_locked(fp
, fp
->f_fglob
, p
);
5169 FREE_ZONE(fdp
->fd_ofiles
, fdp
->fd_nfiles
* OFILESIZE
, M_OFILETABL
);
5170 fdp
->fd_ofiles
= NULL
;
5174 if (fdp
->fd_wqkqueue
) {
5175 dealloc_kq
= fdp
->fd_wqkqueue
;
5176 fdp
->fd_wqkqueue
= NULL
;
5182 kqueue_dealloc(dealloc_kq
);
5186 vnode_rele(fdp
->fd_cdir
);
5189 vnode_rele(fdp
->fd_rdir
);
5192 proc_fdlock_spin(p
);
5196 if (fdp
->fd_kqhash
) {
5197 for (uint32_t j
= 0; j
<= fdp
->fd_kqhashmask
; j
++) {
5198 assert(SLIST_EMPTY(&fdp
->fd_kqhash
[j
]));
5200 FREE(fdp
->fd_kqhash
, M_KQUEUE
);
5203 lck_mtx_destroy(&fdp
->fd_kqhashlock
, proc_kqhashlock_grp
);
5204 lck_mtx_destroy(&fdp
->fd_knhashlock
, proc_knhashlock_grp
);
5206 FREE_ZONE(fdp
, sizeof(*fdp
), M_FILEDESC
);
5212 * Description: Internal form of closef; called with proc_fdlock held
5214 * Parameters: fp Pointer to fileproc for fd
5215 * fg Pointer to fileglob for fd
5216 * p Pointer to proc structure
5218 * Returns: 0 Success
5219 * closef_finish:??? Anything returnable by a per-fileops
5222 * Note: Decrements reference count on file structure; if this was the
5223 * last reference, then closef_finish() is called
5225 * p and fp are allowed to be NULL when closing a file that was
5226 * being passed in a message (but only if we are called when this
5227 * is NOT the last reference).
5230 closef_locked(struct fileproc
*fp
, struct fileglob
*fg
, proc_t p
)
5234 struct vfs_context context
;
5241 /* Set up context with cred stashed in fg */
5242 if (p
== current_proc()) {
5243 context
.vc_thread
= current_thread();
5245 context
.vc_thread
= NULL
;
5247 context
.vc_ucred
= fg
->fg_cred
;
5250 * POSIX record locking dictates that any close releases ALL
5251 * locks owned by this process. This is handled by setting
5252 * a flag in the unlock to free ONLY locks obeying POSIX
5253 * semantics, and not to free BSD-style file locks.
5254 * If the descriptor was in a message, POSIX-style locks
5255 * aren't passed with the descriptor.
5257 if (p
&& (p
->p_ladvflag
& P_LADVLOCK
) &&
5258 DTYPE_VNODE
== FILEGLOB_DTYPE(fg
)) {
5261 lf
.l_whence
= SEEK_SET
;
5264 lf
.l_type
= F_UNLCK
;
5265 vp
= (struct vnode
*)fg
->fg_data
;
5267 if ((error
= vnode_getwithref(vp
)) == 0) {
5268 (void) VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &lf
, F_POSIX
, &context
, NULL
);
5269 (void)vnode_put(vp
);
5273 lck_mtx_lock_spin(&fg
->fg_lock
);
5276 if (fg
->fg_count
> 0) {
5277 lck_mtx_unlock(&fg
->fg_lock
);
5281 if (fg
->fg_count
!= 0) {
5282 panic("fg %p: being freed with bad fg_count (%d)", fg
, fg
->fg_count
);
5286 if (fp
&& (fp
->f_flags
& FP_WRITTEN
)) {
5287 fg
->fg_flag
|= FWASWRITTEN
;
5290 fg
->fg_lflags
|= FG_TERM
;
5291 lck_mtx_unlock(&fg
->fg_lock
);
5297 /* Since we ensure that fg->fg_ops is always initialized,
5298 * it is safe to invoke fo_close on the fg */
5299 error
= fo_close(fg
, &context
);
5314 * Description: Drain out pending I/O operations
5316 * Parameters: p Process closing this file
5317 * fp fileproc struct for the open
5318 * instance on the file
5322 * Locks: Assumes the caller holds the proc_fdlock
5324 * Notes: For character devices, this occurs on the last close of the
5325 * device; for all other file descriptors, this occurs on each
5326 * close to prevent fd's from being closed out from under
5327 * operations currently in progress and blocked
5329 * See Also: file_vnode(), file_socket(), file_drop(), and the cautions
5330 * regarding their use and interaction with this function.
5333 fileproc_drain(proc_t p
, struct fileproc
* fp
)
5335 struct vfs_context context
;
5337 context
.vc_thread
= proc_thread(p
); /* XXX */
5338 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
5340 fp
->f_iocount
--; /* (the one the close holds) */
5342 while (fp
->f_iocount
) {
5343 lck_mtx_convert_spin(&p
->p_fdmlock
);
5345 if (fp
->f_fglob
->fg_ops
->fo_drain
) {
5346 (*fp
->f_fglob
->fg_ops
->fo_drain
)(fp
, &context
);
5348 if ((fp
->f_flags
& FP_INSELECT
) == FP_INSELECT
) {
5349 if (waitq_wakeup64_all((struct waitq
*)fp
->f_wset
, NO_EVENT64
,
5350 THREAD_INTERRUPTED
, WAITQ_ALL_PRIORITIES
) == KERN_INVALID_ARGUMENT
) {
5351 panic("bad wait queue for waitq_wakeup64_all %p (fp:%p)", fp
->f_wset
, fp
);
5354 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
) {
5355 if (waitq_wakeup64_all(&select_conflict_queue
, NO_EVENT64
,
5356 THREAD_INTERRUPTED
, WAITQ_ALL_PRIORITIES
) == KERN_INVALID_ARGUMENT
) {
5357 panic("bad select_conflict_queue");
5360 p
->p_fpdrainwait
= 1;
5362 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
, "fpdrain", NULL
);
5365 if ((fp
->f_flags
& FP_INSELECT
) != 0) {
5366 panic("FP_INSELECT set on drained fp");
5369 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
) {
5370 fp
->f_flags
&= ~FP_SELCONFLICT
;
5378 * Description: Release the fd and free the fileproc associated with the fd
5379 * in the per process open file table of the specified process;
5380 * these values must correspond.
5382 * Parameters: p Process containing fd
5383 * fd fd to be released
5384 * fp fileproc to be freed
5386 * Returns: 0 Success
5388 * Notes: XXX function should be void - no one interprets the returns
5392 fp_free(proc_t p
, int fd
, struct fileproc
* fp
)
5394 proc_fdlock_spin(p
);
5398 fg_free(fp
->f_fglob
);
5407 * Description: Apply an advisory lock on a file descriptor.
5409 * Parameters: p Process making request
5410 * uap->fd fd on which the lock is to be
5412 * uap->how (Un)Lock bits, including type
5413 * retval Pointer to the call return area
5415 * Returns: 0 Success
5416 * fp_getfvp:EBADF Bad file descriptor
5417 * fp_getfvp:ENOTSUP fd does not refer to a vnode
5418 * vnode_getwithref:???
5422 * *retval (modified) Size of dtable
5424 * Notes: Just attempt to get a record lock of the requested type on
5425 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
5428 flock(proc_t p
, struct flock_args
*uap
, __unused
int32_t *retval
)
5432 struct fileproc
*fp
;
5435 vfs_context_t ctx
= vfs_context_current();
5438 AUDIT_ARG(fd
, uap
->fd
);
5439 if ((error
= fp_getfvp(p
, fd
, &fp
, &vp
))) {
5442 if ((error
= vnode_getwithref(vp
))) {
5445 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
5447 lf
.l_whence
= SEEK_SET
;
5450 if (how
& LOCK_UN
) {
5451 lf
.l_type
= F_UNLCK
;
5452 fp
->f_flag
&= ~FHASLOCK
;
5453 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_UNLCK
, &lf
, F_FLOCK
, ctx
, NULL
);
5456 if (how
& LOCK_EX
) {
5457 lf
.l_type
= F_WRLCK
;
5458 } else if (how
& LOCK_SH
) {
5459 lf
.l_type
= F_RDLCK
;
5465 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
, F_SETLK
, &lf
);
5470 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
,
5471 (how
& LOCK_NB
? F_FLOCK
: F_FLOCK
| F_WAIT
),
5474 fp
->f_flag
|= FHASLOCK
;
5477 (void)vnode_put(vp
);
5479 fp_drop(p
, fd
, fp
, 0);
5486 * Description: Obtain a Mach send right for a given file descriptor.
5488 * Parameters: p Process calling fileport
5489 * uap->fd The fd to reference
5490 * uap->portnamep User address at which to place port name.
5492 * Returns: 0 Success.
5493 * EBADF Bad file descriptor.
5494 * EINVAL File descriptor had type that cannot be sent, misc. other errors.
5495 * EFAULT Address at which to store port name is not valid.
5496 * EAGAIN Resource shortage.
5499 * On success, name of send right is stored at user-specified address.
5502 fileport_makeport(proc_t p
, struct fileport_makeport_args
*uap
,
5503 __unused
int *retval
)
5507 user_addr_t user_portaddr
= uap
->portnamep
;
5508 struct fileproc
*fp
= FILEPROC_NULL
;
5509 struct fileglob
*fg
= NULL
;
5510 ipc_port_t fileport
;
5511 mach_port_name_t name
= MACH_PORT_NULL
;
5514 err
= fp_lookup(p
, fd
, &fp
, 1);
5519 if (!file_issendable(p
, fp
)) {
5524 if (FP_ISGUARDED(fp
, GUARD_FILEPORT
)) {
5525 err
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_FILEPORT
);
5529 /* Dropped when port is deallocated */
5535 /* Allocate and initialize a port */
5536 fileport
= fileport_alloc(fg
);
5537 if (fileport
== IPC_PORT_NULL
) {
5543 /* Add an entry. Deallocates port on failure. */
5544 name
= ipc_port_copyout_send(fileport
, get_task_ipcspace(p
->task
));
5545 if (!MACH_PORT_VALID(name
)) {
5550 err
= copyout(&name
, user_portaddr
, sizeof(mach_port_name_t
));
5555 /* Tag the fileglob for debugging purposes */
5556 lck_mtx_lock_spin(&fg
->fg_lock
);
5557 fg
->fg_lflags
|= FG_PORTMADE
;
5558 lck_mtx_unlock(&fg
->fg_lock
);
5560 fp_drop(p
, fd
, fp
, 0);
5567 if (MACH_PORT_VALID(name
)) {
5568 /* Don't care if another thread races us to deallocate the entry */
5569 (void) mach_port_deallocate(get_task_ipcspace(p
->task
), name
);
5572 if (fp
!= FILEPROC_NULL
) {
5573 fp_drop(p
, fd
, fp
, 0);
5580 fileport_releasefg(struct fileglob
*fg
)
5582 (void)closef_locked(NULL
, fg
, PROC_NULL
);
5591 * Description: Obtain the file descriptor for a given Mach send right.
5593 * Parameters: p Process calling fileport
5594 * uap->port Name of send right to file port.
5596 * Returns: 0 Success
5597 * EINVAL Invalid Mach port name, or port is not for a file.
5599 * fdalloc:ENOMEM Unable to allocate fileproc or extend file table.
5602 * *retval (modified) The new descriptor
5605 fileport_makefd(proc_t p
, struct fileport_makefd_args
*uap
, int32_t *retval
)
5607 struct fileglob
*fg
;
5608 struct fileproc
*fp
= FILEPROC_NULL
;
5609 ipc_port_t port
= IPC_PORT_NULL
;
5610 mach_port_name_t send
= uap
->port
;
5615 res
= ipc_object_copyin(get_task_ipcspace(p
->task
),
5616 send
, MACH_MSG_TYPE_COPY_SEND
, &port
);
5618 if (res
!= KERN_SUCCESS
) {
5623 fg
= fileport_port_to_fileglob(port
);
5629 fp
= fileproc_alloc_init(NULL
);
5630 if (fp
== FILEPROC_NULL
) {
5639 err
= fdalloc(p
, 0, &fd
);
5645 *fdflags(p
, fd
) |= UF_EXCLOSE
;
5647 procfdtbl_releasefd(p
, fd
, fp
);
5653 if ((fp
!= NULL
) && (0 != err
)) {
5657 if (IPC_PORT_NULL
!= port
) {
5658 ipc_port_release_send(port
);
5668 * Description: Duplicate the specified descriptor to a free descriptor;
5669 * this is the second half of fdopen(), above.
5671 * Parameters: fdp filedesc pointer to fill in
5673 * dfd fd to dup from
5674 * mode mode to set on new fd
5675 * error command code
5677 * Returns: 0 Success
5678 * EBADF Source fd is bad
5679 * EACCES Requested mode not allowed
5680 * !0 'error', if not ENODEV or
5683 * Notes: XXX This is not thread safe; see fdopen() above
5686 dupfdopen(struct filedesc
*fdp
, int indx
, int dfd
, int flags
, int error
)
5688 struct fileproc
*wfp
;
5689 struct fileproc
*fp
;
5693 proc_t p
= current_proc();
5696 * If the to-be-dup'd fd number is greater than the allowed number
5697 * of file descriptors, or the fd to be dup'd has already been
5698 * closed, reject. Note, check for new == old is necessary as
5699 * falloc could allocate an already closed to-be-dup'd descriptor
5700 * as the new descriptor.
5704 fp
= fdp
->fd_ofiles
[indx
];
5705 if (dfd
< 0 || dfd
>= fdp
->fd_nfiles
||
5706 (wfp
= fdp
->fd_ofiles
[dfd
]) == NULL
|| wfp
== fp
||
5707 (fdp
->fd_ofileflags
[dfd
] & UF_RESERVED
)) {
5712 myerror
= mac_file_check_dup(proc_ucred(p
), wfp
->f_fglob
, dfd
);
5719 * There are two cases of interest here.
5721 * For ENODEV simply dup (dfd) to file descriptor
5722 * (indx) and return.
5724 * For ENXIO steal away the file structure from (dfd) and
5725 * store it in (indx). (dfd) is effectively closed by
5728 * Any other error code is just returned.
5732 if (FP_ISGUARDED(wfp
, GUARD_DUP
)) {
5738 * Check that the mode the file is being opened for is a
5739 * subset of the mode of the existing descriptor.
5741 if (((flags
& (FREAD
| FWRITE
)) | wfp
->f_flag
) != wfp
->f_flag
) {
5745 if (indx
> fdp
->fd_lastfile
) {
5746 fdp
->fd_lastfile
= indx
;
5751 fg_free(fp
->f_fglob
);
5753 fp
->f_fglob
= wfp
->f_fglob
;
5755 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
] |
5756 (flags
& O_CLOEXEC
) ? UF_EXCLOSE
: 0;
5772 * Description: Add a reference to a fileglob by fileproc
5774 * Parameters: fp fileproc containing fileglob
5779 * Notes: XXX Should use OSAddAtomic?
5782 fg_ref(struct fileproc
* fp
)
5784 struct fileglob
*fg
;
5788 lck_mtx_lock_spin(&fg
->fg_lock
);
5791 if ((fp
->f_flags
& ~((unsigned int)FP_VALID_FLAGS
)) != 0) {
5792 panic("fg_ref: invalid bits on fp %p", fp
);
5795 if (fg
->fg_count
== 0) {
5796 panic("fg_ref: adding fgcount to zeroed fg: fp %p fg %p",
5801 lck_mtx_unlock(&fg
->fg_lock
);
5808 * Description: Remove a reference to a fileglob by fileproc
5810 * Parameters: fp fileproc containing fileglob
5815 * Notes: XXX Should use OSAddAtomic?
5818 fg_drop(struct fileproc
* fp
)
5820 struct fileglob
*fg
;
5823 lck_mtx_lock_spin(&fg
->fg_lock
);
5825 lck_mtx_unlock(&fg
->fg_lock
);
5830 * fg_insertuipc_mark
5832 * Description: Mark fileglob for insertion onto message queue if needed
5833 * Also takes fileglob reference
5835 * Parameters: fg Fileglob pointer to insert
5837 * Returns: true, if the fileglob needs to be inserted onto msg queue
5839 * Locks: Takes and drops fg_lock, potentially many times
5842 fg_insertuipc_mark(struct fileglob
* fg
)
5844 boolean_t insert
= FALSE
;
5846 lck_mtx_lock_spin(&fg
->fg_lock
);
5847 while (fg
->fg_lflags
& FG_RMMSGQ
) {
5848 lck_mtx_convert_spin(&fg
->fg_lock
);
5850 fg
->fg_lflags
|= FG_WRMMSGQ
;
5851 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_insertuipc", NULL
);
5856 if (fg
->fg_msgcount
== 1) {
5857 fg
->fg_lflags
|= FG_INSMSGQ
;
5860 lck_mtx_unlock(&fg
->fg_lock
);
5867 * Description: Insert marked fileglob onto message queue
5869 * Parameters: fg Fileglob pointer to insert
5873 * Locks: Takes and drops fg_lock & uipc_lock
5874 * DO NOT call this function with proc_fdlock held as unp_gc()
5875 * can potentially try to acquire proc_fdlock, which can result
5876 * in a deadlock if this function is in unp_gc_wait().
5879 fg_insertuipc(struct fileglob
* fg
)
5881 if (fg
->fg_lflags
& FG_INSMSGQ
) {
5882 lck_mtx_lock_spin(uipc_lock
);
5884 LIST_INSERT_HEAD(&fmsghead
, fg
, f_msglist
);
5885 lck_mtx_unlock(uipc_lock
);
5886 lck_mtx_lock(&fg
->fg_lock
);
5887 fg
->fg_lflags
&= ~FG_INSMSGQ
;
5888 if (fg
->fg_lflags
& FG_WINSMSGQ
) {
5889 fg
->fg_lflags
&= ~FG_WINSMSGQ
;
5890 wakeup(&fg
->fg_lflags
);
5892 lck_mtx_unlock(&fg
->fg_lock
);
5897 * fg_removeuipc_mark
5899 * Description: Mark the fileglob for removal from message queue if needed
5900 * Also releases fileglob message queue reference
5902 * Parameters: fg Fileglob pointer to remove
5904 * Returns: true, if the fileglob needs to be removed from msg queue
5906 * Locks: Takes and drops fg_lock, potentially many times
5909 fg_removeuipc_mark(struct fileglob
* fg
)
5911 boolean_t remove
= FALSE
;
5913 lck_mtx_lock_spin(&fg
->fg_lock
);
5914 while (fg
->fg_lflags
& FG_INSMSGQ
) {
5915 lck_mtx_convert_spin(&fg
->fg_lock
);
5917 fg
->fg_lflags
|= FG_WINSMSGQ
;
5918 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_removeuipc", NULL
);
5921 if (fg
->fg_msgcount
== 0) {
5922 fg
->fg_lflags
|= FG_RMMSGQ
;
5925 lck_mtx_unlock(&fg
->fg_lock
);
5932 * Description: Remove marked fileglob from message queue
5934 * Parameters: fg Fileglob pointer to remove
5938 * Locks: Takes and drops fg_lock & uipc_lock
5939 * DO NOT call this function with proc_fdlock held as unp_gc()
5940 * can potentially try to acquire proc_fdlock, which can result
5941 * in a deadlock if this function is in unp_gc_wait().
5944 fg_removeuipc(struct fileglob
* fg
)
5946 if (fg
->fg_lflags
& FG_RMMSGQ
) {
5947 lck_mtx_lock_spin(uipc_lock
);
5949 LIST_REMOVE(fg
, f_msglist
);
5950 lck_mtx_unlock(uipc_lock
);
5951 lck_mtx_lock(&fg
->fg_lock
);
5952 fg
->fg_lflags
&= ~FG_RMMSGQ
;
5953 if (fg
->fg_lflags
& FG_WRMMSGQ
) {
5954 fg
->fg_lflags
&= ~FG_WRMMSGQ
;
5955 wakeup(&fg
->fg_lflags
);
5957 lck_mtx_unlock(&fg
->fg_lock
);
5960 #endif /* SOCKETS */
5965 * Description: Generic fileops read indirected through the fileops pointer
5966 * in the fileproc structure
5968 * Parameters: fp fileproc structure pointer
5969 * uio user I/O structure pointer
5971 * ctx VFS context for operation
5973 * Returns: 0 Success
5974 * !0 Errno from read
5977 fo_read(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
5979 return (*fp
->f_ops
->fo_read
)(fp
, uio
, flags
, ctx
);
5986 * Description: Generic fileops write indirected through the fileops pointer
5987 * in the fileproc structure
5989 * Parameters: fp fileproc structure pointer
5990 * uio user I/O structure pointer
5992 * ctx VFS context for operation
5994 * Returns: 0 Success
5995 * !0 Errno from write
5998 fo_write(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
6000 return (*fp
->f_ops
->fo_write
)(fp
, uio
, flags
, ctx
);
6007 * Description: Generic fileops ioctl indirected through the fileops pointer
6008 * in the fileproc structure
6010 * Parameters: fp fileproc structure pointer
6012 * data pointer to internalized copy
6013 * of user space ioctl command
6014 * parameter data in kernel space
6015 * ctx VFS context for operation
6017 * Returns: 0 Success
6018 * !0 Errno from ioctl
6020 * Locks: The caller is assumed to have held the proc_fdlock; this
6021 * function releases and reacquires this lock. If the caller
6022 * accesses data protected by this lock prior to calling this
6023 * function, it will need to revalidate/reacquire any cached
6024 * protected data obtained prior to the call.
6027 fo_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
, vfs_context_t ctx
)
6031 proc_fdunlock(vfs_context_proc(ctx
));
6032 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, com
, data
, ctx
);
6033 proc_fdlock(vfs_context_proc(ctx
));
6041 * Description: Generic fileops select indirected through the fileops pointer
6042 * in the fileproc structure
6044 * Parameters: fp fileproc structure pointer
6045 * which select which
6046 * wql pointer to wait queue list
6047 * ctx VFS context for operation
6049 * Returns: 0 Success
6050 * !0 Errno from select
6053 fo_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
6055 return (*fp
->f_ops
->fo_select
)(fp
, which
, wql
, ctx
);
6062 * Description: Generic fileops close indirected through the fileops pointer
6063 * in the fileproc structure
6065 * Parameters: fp fileproc structure pointer for
6067 * ctx VFS context for operation
6069 * Returns: 0 Success
6070 * !0 Errno from close
6073 fo_close(struct fileglob
*fg
, vfs_context_t ctx
)
6075 return (*fg
->fg_ops
->fo_close
)(fg
, ctx
);
6082 * Description: Generic fileops kqueue filter indirected through the fileops
6083 * pointer in the fileproc structure
6085 * Parameters: fp fileproc structure pointer
6086 * kn pointer to knote to filter on
6087 * ctx VFS context for operation
6089 * Returns: (kn->kn_flags & EV_ERROR) error in kn->kn_data
6090 * 0 Filter is not active
6091 * !0 Filter is active
6094 fo_kqfilter(struct fileproc
*fp
, struct knote
*kn
,
6095 struct kevent_internal_s
*kev
, vfs_context_t ctx
)
6097 return (*fp
->f_ops
->fo_kqfilter
)(fp
, kn
, kev
, ctx
);
6101 * The ability to send a file descriptor to another
6102 * process is opt-in by file type.
6105 file_issendable(proc_t p
, struct fileproc
*fp
)
6107 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
6109 switch (fp
->f_type
) {
6114 case DTYPE_NETPOLICY
:
6115 return 0 == (fp
->f_fglob
->fg_lflags
& FG_CONFINED
);
6117 /* DTYPE_KQUEUE, DTYPE_FSEVENTS, DTYPE_PSXSEM */
6124 fileproc_alloc_init(__unused
void *arg
)
6126 struct fileproc
*fp
;
6128 MALLOC_ZONE(fp
, struct fileproc
*, sizeof(*fp
), M_FILEPROC
, M_WAITOK
);
6130 bzero(fp
, sizeof(*fp
));
6137 fileproc_free(struct fileproc
*fp
)
6139 switch (FILEPROC_TYPE(fp
)) {
6141 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
6144 guarded_fileproc_free(fp
);
6147 panic("%s: corrupt fp %p flags %x", __func__
, fp
, fp
->f_flags
);