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
;
379 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESERVED
;
380 if ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
) == UF_RESVWAIT
) {
381 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESVWAIT
;
387 procfdtbl_waitfd(struct proc
* p
, int fd
)
389 p
->p_fd
->fd_ofileflags
[fd
] |= UF_RESVWAIT
;
390 msleep(&p
->p_fd
, &p
->p_fdmlock
, PRIBIO
, "ftbl_waitfd", NULL
);
395 procfdtbl_clearfd(struct proc
* p
, int fd
)
399 waiting
= (p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
);
400 p
->p_fd
->fd_ofiles
[fd
] = NULL
;
401 p
->p_fd
->fd_ofileflags
[fd
] = 0;
402 if ( waiting
== UF_RESVWAIT
) {
410 * Description: Inline utility function to free an fd in a filedesc
412 * Parameters: fdp Pointer to filedesc fd lies in
414 * reserv fd should be reserved
418 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
422 _fdrelse(struct proc
* p
, int fd
)
424 struct filedesc
*fdp
= p
->p_fd
;
427 if (fd
< fdp
->fd_freefile
)
428 fdp
->fd_freefile
= fd
;
430 if (fd
> fdp
->fd_lastfile
)
431 panic("fdrelse: fd_lastfile inconsistent");
433 procfdtbl_clearfd(p
, fd
);
435 while ((nfd
= fdp
->fd_lastfile
) > 0 &&
436 fdp
->fd_ofiles
[nfd
] == NULL
&&
437 !(fdp
->fd_ofileflags
[nfd
] & UF_RESERVED
))
438 /* JMM - What about files with lingering EV_VANISHED knotes? */
460 char uio_buf
[ UIO_SIZEOF(1) ];
461 struct vfs_context context
= *(vfs_context_current());
462 bool wrote_some
= false;
466 error
= fp_lookup(p
, fd
, &fp
, 0);
470 if (fp
->f_type
!= DTYPE_VNODE
&& fp
->f_type
!= DTYPE_PIPE
&& fp
->f_type
!= DTYPE_SOCKET
) {
474 if (rw
== UIO_WRITE
&& !(fp
->f_flag
& FWRITE
)) {
479 if (rw
== UIO_READ
&& !(fp
->f_flag
& FREAD
)) {
484 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
486 if (UIO_SEG_IS_USER_SPACE(segflg
))
487 spacetype
= proc_is64bit(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
489 spacetype
= UIO_SYSSPACE
;
491 auio
= uio_createwithbuffer(1, offset
, spacetype
, rw
, &uio_buf
[0], sizeof(uio_buf
));
493 uio_addiov(auio
, base
, len
);
495 if ( !(io_flg
& IO_APPEND
))
498 if (rw
== UIO_WRITE
) {
499 user_ssize_t orig_resid
= uio_resid(auio
);
500 error
= fo_write(fp
, auio
, flags
, &context
);
501 wrote_some
= uio_resid(auio
) < orig_resid
;
503 error
= fo_read(fp
, auio
, flags
, &context
);
506 *aresid
= uio_resid(auio
);
508 if (uio_resid(auio
) && error
== 0)
513 fp_drop_written(p
, fd
, fp
);
515 fp_drop(p
, fd
, fp
, 0);
525 * Description: Duplicate a file descriptor.
527 * Parameters: p Process performing the dup
528 * uap->fd The fd to dup
529 * retval Pointer to the call return area
535 * *retval (modified) The new descriptor
538 dup(proc_t p
, struct dup_args
*uap
, int32_t *retval
)
540 struct filedesc
*fdp
= p
->p_fd
;
546 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
550 if (FP_ISGUARDED(fp
, GUARD_DUP
)) {
551 error
= fp_guard_exception(p
, old
, fp
, kGUARD_EXC_DUP
);
552 (void) fp_drop(p
, old
, fp
, 1);
556 if ( (error
= fdalloc(p
, 0, &new)) ) {
557 fp_drop(p
, old
, fp
, 1);
561 error
= finishdup(p
, fdp
, old
, new, 0, retval
);
562 fp_drop(p
, old
, fp
, 1);
565 if (ENTR_SHOULDTRACE
&& fp
->f_type
== DTYPE_SOCKET
) {
566 KERNEL_ENERGYTRACE(kEnTrActKernSocket
, DBG_FUNC_START
,
567 new, 0, (int64_t)VM_KERNEL_ADDRPERM(fp
->f_data
));
576 * Description: Duplicate a file descriptor to a particular value.
578 * Parameters: p Process performing the dup
579 * uap->from The fd to dup
580 * uap->to The fd to dup it to
581 * retval Pointer to the call return area
587 * *retval (modified) The new descriptor
590 dup2(proc_t p
, struct dup2_args
*uap
, int32_t *retval
)
592 struct filedesc
*fdp
= p
->p_fd
;
593 int old
= uap
->from
, new = uap
->to
;
595 struct fileproc
*fp
, *nfp
;
600 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
604 if (FP_ISGUARDED(fp
, GUARD_DUP
)) {
605 error
= fp_guard_exception(p
, old
, fp
, kGUARD_EXC_DUP
);
606 (void) fp_drop(p
, old
, fp
, 1);
611 (rlim_t
)new >= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
613 fp_drop(p
, old
, fp
, 1);
618 fp_drop(p
, old
, fp
, 1);
623 if (new < 0 || new >= fdp
->fd_nfiles
) {
624 if ( (error
= fdalloc(p
, new, &i
)) ) {
625 fp_drop(p
, old
, fp
, 1);
635 while ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == UF_RESERVED
) {
636 fp_drop(p
, old
, fp
, 1);
637 procfdtbl_waitfd(p
, new);
639 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
644 if ((fdp
->fd_ofiles
[new] != NULL
) &&
645 ((error
= fp_lookup(p
, new, &nfp
, 1)) == 0)) {
646 fp_drop(p
, old
, fp
, 1);
647 if (FP_ISGUARDED(nfp
, GUARD_CLOSE
)) {
648 error
= fp_guard_exception(p
,
649 new, nfp
, kGUARD_EXC_CLOSE
);
650 (void) fp_drop(p
, new, nfp
, 1);
654 (void)close_internal_locked(p
, new, nfp
, FD_DUP2RESV
);
656 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
658 procfdtbl_clearfd(p
, new);
662 if (fdp
->fd_ofiles
[new] != NULL
)
663 panic("dup2: no ref on fileproc %d", new);
665 procfdtbl_reservefd(p
, new);
669 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
674 if (fdp
->fd_ofiles
[new] != 0)
675 panic("dup2: overwriting fd_ofiles with new %d", new);
676 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0)
677 panic("dup2: unreserved fileflags with new %d", new);
679 error
= finishdup(p
, fdp
, old
, new, 0, retval
);
680 fp_drop(p
, old
, fp
, 1);
690 * Description: The file control system call.
692 * Parameters: p Process performing the fcntl
693 * uap->fd The fd to operate against
694 * uap->cmd The command to perform
695 * uap->arg Pointer to the command argument
696 * retval Pointer to the call return area
699 * !0 Errno (see fcntl_nocancel)
702 * *retval (modified) fcntl return value (if any)
704 * Notes: This system call differs from fcntl_nocancel() in that it
705 * tests for cancellation prior to performing a potentially
706 * blocking operation.
709 fcntl(proc_t p
, struct fcntl_args
*uap
, int32_t *retval
)
711 __pthread_testcancel(1);
712 return(fcntl_nocancel(p
, (struct fcntl_nocancel_args
*)uap
, retval
));
719 * Description: A non-cancel-testing file control system call.
721 * Parameters: p Process performing the fcntl
722 * uap->fd The fd to operate against
723 * uap->cmd The command to perform
724 * uap->arg Pointer to the command argument
725 * retval Pointer to the call return area
729 * fp_lookup:EBADF Bad file descriptor
741 * vnode_getwithref:???
749 * vnode_getwithref:???
756 * vnode_getwithref:???
758 * [F_SETSIZE,F_RDADVISE]
761 * vnode_getwithref:???
762 * [F_RDAHEAD,F_NOCACHE]
764 * vnode_getwithref:???
768 * *retval (modified) fcntl return value (if any)
771 fcntl_nocancel(proc_t p
, struct fcntl_nocancel_args
*uap
, int32_t *retval
)
774 struct filedesc
*fdp
= p
->p_fd
;
777 struct vnode
*vp
= NULLVP
; /* for AUDIT_ARG() at end */
778 int i
, tmp
, error
, error2
, flg
= 0;
779 struct flock fl
= {};
780 struct flocktimeout fltimeout
;
781 struct timespec
*timeout
= NULL
;
782 struct vfs_context context
;
790 AUDIT_ARG(fd
, uap
->fd
);
791 AUDIT_ARG(cmd
, uap
->cmd
);
794 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
798 context
.vc_thread
= current_thread();
799 context
.vc_ucred
= fp
->f_cred
;
801 is64bit
= proc_is64bit(p
);
807 * Since the arg parameter is defined as a long but may be
808 * either a long or a pointer we must take care to handle
809 * sign extension issues. Our sys call munger will sign
810 * extend a long when we are called from a 32-bit process.
811 * Since we can never have an address greater than 32-bits
812 * from a 32-bit process we lop off the top 32-bits to avoid
813 * getting the wrong address
815 argp
= CAST_USER_ADDR_T((uint32_t)uap
->arg
);
818 pop
= &fdp
->fd_ofileflags
[fd
];
821 error
= mac_file_check_fcntl(proc_ucred(p
), fp
->f_fglob
, uap
->cmd
,
830 case F_DUPFD_CLOEXEC
:
831 if (FP_ISGUARDED(fp
, GUARD_DUP
)) {
832 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_DUP
);
835 newmin
= CAST_DOWN_EXPLICIT(int, uap
->arg
); /* arg is an int, so we won't lose bits */
836 AUDIT_ARG(value32
, newmin
);
837 if ((u_int
)newmin
>= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
838 newmin
>= maxfiles
) {
842 if ( (error
= fdalloc(p
, newmin
, &i
)) )
844 error
= finishdup(p
, fdp
, fd
, i
,
845 uap
->cmd
== F_DUPFD_CLOEXEC
? UF_EXCLOSE
: 0, retval
);
849 *retval
= (*pop
& UF_EXCLOSE
)? FD_CLOEXEC
: 0;
854 AUDIT_ARG(value32
, uap
->arg
);
855 if (uap
->arg
& FD_CLOEXEC
)
858 if (FILEPROC_TYPE(fp
) == FTYPE_GUARDED
) {
859 error
= fp_guard_exception(p
,
860 fd
, fp
, kGUARD_EXC_NOCLOEXEC
);
869 *retval
= OFLAGS(fp
->f_flag
);
874 fp
->f_flag
&= ~FCNTLFLAGS
;
875 tmp
= CAST_DOWN_EXPLICIT(int, uap
->arg
); /* arg is an int, so we won't lose bits */
876 AUDIT_ARG(value32
, tmp
);
877 fp
->f_flag
|= FFLAGS(tmp
) & FCNTLFLAGS
;
878 tmp
= fp
->f_flag
& FNONBLOCK
;
879 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
882 tmp
= fp
->f_flag
& FASYNC
;
883 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, &context
);
886 fp
->f_flag
&= ~FNONBLOCK
;
888 (void)fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
892 if (fp
->f_type
== DTYPE_SOCKET
) {
893 *retval
= ((struct socket
*)fp
->f_data
)->so_pgid
;
897 error
= fo_ioctl(fp
, (int)TIOCGPGRP
, (caddr_t
)retval
, &context
);
902 tmp
= CAST_DOWN_EXPLICIT(pid_t
, uap
->arg
); /* arg is an int, so we won't lose bits */
903 AUDIT_ARG(value32
, tmp
);
904 if (fp
->f_type
== DTYPE_SOCKET
) {
905 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
909 if (fp
->f_type
== DTYPE_PIPE
) {
910 error
= fo_ioctl(fp
, TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
917 proc_t p1
= proc_find(tmp
);
922 tmp
= (int)p1
->p_pgrpid
;
925 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
929 tmp
= CAST_DOWN_EXPLICIT(int, uap
->arg
);
930 if (fp
->f_type
== DTYPE_SOCKET
) {
932 error
= sock_setsockopt((struct socket
*)fp
->f_data
,
933 SOL_SOCKET
, SO_NOSIGPIPE
, &tmp
, sizeof (tmp
));
938 struct fileglob
*fg
= fp
->f_fglob
;
940 lck_mtx_lock_spin(&fg
->fg_lock
);
942 fg
->fg_lflags
|= FG_NOSIGPIPE
;
944 fg
->fg_lflags
&= FG_NOSIGPIPE
;
945 lck_mtx_unlock(&fg
->fg_lock
);
951 if (fp
->f_type
== DTYPE_SOCKET
) {
953 int retsize
= sizeof (*retval
);
954 error
= sock_getsockopt((struct socket
*)fp
->f_data
,
955 SOL_SOCKET
, SO_NOSIGPIPE
, retval
, &retsize
);
960 *retval
= (fp
->f_fglob
->fg_lflags
& FG_NOSIGPIPE
) ?
968 * If this is the only reference to this fglob in the process
969 * and it's already marked as close-on-fork then mark it as
970 * (immutably) "confined" i.e. any fd that points to it will
971 * forever be close-on-fork, and attempts to use an IPC
972 * mechanism to move the descriptor elsewhere will fail.
974 if (CAST_DOWN_EXPLICIT(int, uap
->arg
)) {
975 struct fileglob
*fg
= fp
->f_fglob
;
977 lck_mtx_lock_spin(&fg
->fg_lock
);
978 if (fg
->fg_lflags
& FG_CONFINED
)
980 else if (1 != fg
->fg_count
)
981 error
= EAGAIN
; /* go close the dup .. */
982 else if (UF_FORKCLOSE
== (*pop
& UF_FORKCLOSE
)) {
983 fg
->fg_lflags
|= FG_CONFINED
;
986 error
= EBADF
; /* open without O_CLOFORK? */
987 lck_mtx_unlock(&fg
->fg_lock
);
990 * Other subsystems may have built on the immutability
991 * of FG_CONFINED; clearing it may be tricky.
993 error
= EPERM
; /* immutable */
998 *retval
= (fp
->f_fglob
->fg_lflags
& FG_CONFINED
) ? 1 : 0;
1002 case F_SETLKWTIMEOUT
:
1004 case F_OFD_SETLKWTIMEOUT
:
1007 /* Fall into F_SETLK */
1011 if (fp
->f_type
!= DTYPE_VNODE
) {
1015 vp
= (struct vnode
*)fp
->f_data
;
1018 offset
= fp
->f_offset
;
1021 /* Copy in the lock structure */
1022 if (F_SETLKWTIMEOUT
== uap
->cmd
||
1023 F_OFD_SETLKWTIMEOUT
== uap
->cmd
) {
1024 error
= copyin(argp
, (caddr_t
) &fltimeout
, sizeof(fltimeout
));
1029 timeout
= &fltimeout
.timeout
;
1031 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
1037 /* Check starting byte and ending byte for EOVERFLOW in SEEK_CUR */
1038 /* and ending byte for EOVERFLOW in SEEK_SET */
1039 error
= check_file_seek_range(&fl
, offset
);
1044 if ( (error
= vnode_getwithref(vp
)) ) {
1047 if (fl
.l_whence
== SEEK_CUR
)
1048 fl
.l_start
+= offset
;
1051 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
1054 (void)vnode_put(vp
);
1061 case F_OFD_SETLKWTIMEOUT
:
1063 switch (fl
.l_type
) {
1065 if ((fflag
& FREAD
) == 0) {
1069 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1070 F_SETLK
, &fl
, flg
, &context
, timeout
);
1073 if ((fflag
& FWRITE
) == 0) {
1077 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1078 F_SETLK
, &fl
, flg
, &context
, timeout
);
1081 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1082 F_UNLCK
, &fl
, F_OFD_LOCK
, &context
,
1090 (F_RDLCK
== fl
.l_type
|| F_WRLCK
== fl
.l_type
)) {
1091 struct fileglob
*fg
= fp
->f_fglob
;
1094 * arrange F_UNLCK on last close (once
1095 * set, FG_HAS_OFDLOCK is immutable)
1097 if ((fg
->fg_lflags
& FG_HAS_OFDLOCK
) == 0) {
1098 lck_mtx_lock_spin(&fg
->fg_lock
);
1099 fg
->fg_lflags
|= FG_HAS_OFDLOCK
;
1100 lck_mtx_unlock(&fg
->fg_lock
);
1106 switch (fl
.l_type
) {
1108 if ((fflag
& FREAD
) == 0) {
1112 // XXX UInt32 unsafe for LP64 kernel
1113 OSBitOrAtomic(P_LADVLOCK
, &p
->p_ladvflag
);
1114 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1115 F_SETLK
, &fl
, flg
, &context
, timeout
);
1118 if ((fflag
& FWRITE
) == 0) {
1122 // XXX UInt32 unsafe for LP64 kernel
1123 OSBitOrAtomic(P_LADVLOCK
, &p
->p_ladvflag
);
1124 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1125 F_SETLK
, &fl
, flg
, &context
, timeout
);
1128 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1129 F_UNLCK
, &fl
, F_POSIX
, &context
, timeout
);
1137 (void) vnode_put(vp
);
1143 case F_OFD_GETLKPID
:
1144 if (fp
->f_type
!= DTYPE_VNODE
) {
1148 vp
= (struct vnode
*)fp
->f_data
;
1150 offset
= fp
->f_offset
;
1153 /* Copy in the lock structure */
1154 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
1158 /* Check starting byte and ending byte for EOVERFLOW in SEEK_CUR */
1159 /* and ending byte for EOVERFLOW in SEEK_SET */
1160 error
= check_file_seek_range(&fl
, offset
);
1165 if ((fl
.l_whence
== SEEK_SET
) && (fl
.l_start
< 0)) {
1170 switch (fl
.l_type
) {
1180 switch (fl
.l_whence
) {
1190 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1191 if (fl
.l_whence
== SEEK_CUR
)
1192 fl
.l_start
+= offset
;
1195 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
1201 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1202 F_GETLK
, &fl
, F_OFD_LOCK
, &context
, NULL
);
1204 case F_OFD_GETLKPID
:
1205 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1206 F_GETLKPID
, &fl
, F_OFD_LOCK
, &context
, NULL
);
1209 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1210 uap
->cmd
, &fl
, F_POSIX
, &context
, NULL
);
1214 (void)vnode_put(vp
);
1217 error
= copyout((caddr_t
)&fl
, argp
, sizeof(fl
));
1221 case F_PREALLOCATE
: {
1222 fstore_t alloc_struct
; /* structure for allocate command */
1223 u_int32_t alloc_flags
= 0;
1225 if (fp
->f_type
!= DTYPE_VNODE
) {
1230 vp
= (struct vnode
*)fp
->f_data
;
1233 /* make sure that we have write permission */
1234 if ((fp
->f_flag
& FWRITE
) == 0) {
1239 error
= copyin(argp
, (caddr_t
)&alloc_struct
, sizeof(alloc_struct
));
1243 /* now set the space allocated to 0 */
1244 alloc_struct
.fst_bytesalloc
= 0;
1247 * Do some simple parameter checking
1250 /* set up the flags */
1252 alloc_flags
|= PREALLOCATE
;
1254 if (alloc_struct
.fst_flags
& F_ALLOCATECONTIG
)
1255 alloc_flags
|= ALLOCATECONTIG
;
1257 if (alloc_struct
.fst_flags
& F_ALLOCATEALL
)
1258 alloc_flags
|= ALLOCATEALL
;
1261 * Do any position mode specific stuff. The only
1262 * position mode supported now is PEOFPOSMODE
1265 switch (alloc_struct
.fst_posmode
) {
1268 if (alloc_struct
.fst_offset
!= 0) {
1273 alloc_flags
|= ALLOCATEFROMPEOF
;
1277 if (alloc_struct
.fst_offset
<= 0) {
1282 alloc_flags
|= ALLOCATEFROMVOL
;
1290 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1292 * call allocate to get the space
1294 error
= VNOP_ALLOCATE(vp
,alloc_struct
.fst_length
,alloc_flags
,
1295 &alloc_struct
.fst_bytesalloc
, alloc_struct
.fst_offset
,
1297 (void)vnode_put(vp
);
1299 error2
= copyout((caddr_t
)&alloc_struct
, argp
, sizeof(alloc_struct
));
1309 if (fp
->f_type
!= DTYPE_VNODE
) {
1314 vp
= (struct vnode
*)fp
->f_data
;
1317 /* need write permissions */
1318 if ((fp
->f_flag
& FWRITE
) == 0) {
1323 if ((error
= copyin(argp
, (caddr_t
)&args
, sizeof(args
)))) {
1327 if ((error
= vnode_getwithref(vp
))) {
1332 if ((error
= mac_vnode_check_write(&context
, fp
->f_fglob
->fg_cred
, vp
))) {
1333 (void)vnode_put(vp
);
1338 error
= VNOP_IOCTL(vp
, F_PUNCHHOLE
, (caddr_t
)&args
, 0, &context
);
1339 (void)vnode_put(vp
);
1343 case F_TRIM_ACTIVE_FILE
: {
1344 ftrimactivefile_t args
;
1346 if (priv_check_cred(kauth_cred_get(), PRIV_TRIM_ACTIVE_FILE
, 0)) {
1351 if (fp
->f_type
!= DTYPE_VNODE
) {
1356 vp
= (struct vnode
*)fp
->f_data
;
1359 /* need write permissions */
1360 if ((fp
->f_flag
& FWRITE
) == 0) {
1365 if ((error
= copyin(argp
, (caddr_t
)&args
, sizeof(args
)))) {
1369 if ((error
= vnode_getwithref(vp
))) {
1373 error
= VNOP_IOCTL(vp
, F_TRIM_ACTIVE_FILE
, (caddr_t
)&args
, 0, &context
);
1374 (void)vnode_put(vp
);
1379 if (fp
->f_type
!= DTYPE_VNODE
) {
1383 vp
= (struct vnode
*)fp
->f_data
;
1386 error
= copyin(argp
, (caddr_t
)&offset
, sizeof (off_t
));
1389 AUDIT_ARG(value64
, offset
);
1391 error
= vnode_getwithref(vp
);
1396 error
= mac_vnode_check_truncate(&context
,
1397 fp
->f_fglob
->fg_cred
, vp
);
1399 (void)vnode_put(vp
);
1404 * Make sure that we are root. Growing a file
1405 * without zero filling the data is a security hole
1406 * root would have access anyway so we'll allow it
1408 if (!kauth_cred_issuser(kauth_cred_get())) {
1414 error
= vnode_setsize(vp
, offset
, IO_NOZEROFILL
,
1419 mac_vnode_notify_truncate(&context
, fp
->f_fglob
->fg_cred
, vp
);
1423 (void)vnode_put(vp
);
1427 if (fp
->f_type
!= DTYPE_VNODE
) {
1432 fp
->f_fglob
->fg_flag
&= ~FNORDAHEAD
;
1434 fp
->f_fglob
->fg_flag
|= FNORDAHEAD
;
1439 if (fp
->f_type
!= DTYPE_VNODE
) {
1444 fp
->f_fglob
->fg_flag
|= FNOCACHE
;
1446 fp
->f_fglob
->fg_flag
&= ~FNOCACHE
;
1451 if (fp
->f_type
!= DTYPE_VNODE
) {
1456 fp
->f_fglob
->fg_flag
|= FNODIRECT
;
1458 fp
->f_fglob
->fg_flag
&= ~FNODIRECT
;
1462 case F_SINGLE_WRITER
:
1463 if (fp
->f_type
!= DTYPE_VNODE
) {
1468 fp
->f_fglob
->fg_flag
|= FSINGLE_WRITER
;
1470 fp
->f_fglob
->fg_flag
&= ~FSINGLE_WRITER
;
1474 case F_GLOBAL_NOCACHE
:
1475 if (fp
->f_type
!= DTYPE_VNODE
) {
1479 vp
= (struct vnode
*)fp
->f_data
;
1482 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1484 *retval
= vnode_isnocache(vp
);
1487 vnode_setnocache(vp
);
1489 vnode_clearnocache(vp
);
1491 (void)vnode_put(vp
);
1495 case F_CHECK_OPENEVT
:
1496 if (fp
->f_type
!= DTYPE_VNODE
) {
1500 vp
= (struct vnode
*)fp
->f_data
;
1503 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1505 *retval
= vnode_is_openevt(vp
);
1508 vnode_set_openevt(vp
);
1510 vnode_clear_openevt(vp
);
1512 (void)vnode_put(vp
);
1517 struct radvisory ra_struct
;
1519 if (fp
->f_type
!= DTYPE_VNODE
) {
1523 vp
= (struct vnode
*)fp
->f_data
;
1526 if ( (error
= copyin(argp
, (caddr_t
)&ra_struct
, sizeof(ra_struct
))) )
1528 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1529 error
= VNOP_IOCTL(vp
, F_RDADVISE
, (caddr_t
)&ra_struct
, 0, &context
);
1531 (void)vnode_put(vp
);
1538 if (fp
->f_type
!= DTYPE_VNODE
) {
1542 vp
= (struct vnode
*)fp
->f_data
;
1545 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1546 error
= VNOP_FSYNC(vp
, MNT_NOWAIT
, &context
);
1548 (void)vnode_put(vp
);
1553 case F_LOG2PHYS_EXT
: {
1554 struct log2phys l2p_struct
= {}; /* structure for allocate command */
1557 off_t file_offset
= 0;
1561 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1562 error
= copyin(argp
, (caddr_t
)&l2p_struct
, sizeof(l2p_struct
));
1565 file_offset
= l2p_struct
.l2p_devoffset
;
1567 file_offset
= fp
->f_offset
;
1569 if (fp
->f_type
!= DTYPE_VNODE
) {
1573 vp
= (struct vnode
*)fp
->f_data
;
1575 if ( (error
= vnode_getwithref(vp
)) ) {
1578 error
= VNOP_OFFTOBLK(vp
, file_offset
, &lbn
);
1580 (void)vnode_put(vp
);
1583 error
= VNOP_BLKTOOFF(vp
, lbn
, &offset
);
1585 (void)vnode_put(vp
);
1588 devBlockSize
= vfs_devblocksize(vnode_mount(vp
));
1589 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1590 if (l2p_struct
.l2p_contigbytes
< 0) {
1596 a_size
= MIN((uint64_t)l2p_struct
.l2p_contigbytes
, SIZE_MAX
);
1598 a_size
= devBlockSize
;
1601 error
= VNOP_BLOCKMAP(vp
, offset
, a_size
, &bn
, &run
, NULL
, 0, &context
);
1603 (void)vnode_put(vp
);
1606 l2p_struct
.l2p_flags
= 0; /* for now */
1607 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1608 l2p_struct
.l2p_contigbytes
= run
- (file_offset
- offset
);
1610 l2p_struct
.l2p_contigbytes
= 0; /* for now */
1614 * The block number being -1 suggests that the file offset is not backed
1615 * by any real blocks on-disk. As a result, just let it be passed back up wholesale.
1618 /* Don't multiply it by the block size */
1619 l2p_struct
.l2p_devoffset
= bn
;
1622 l2p_struct
.l2p_devoffset
= bn
* devBlockSize
;
1623 l2p_struct
.l2p_devoffset
+= file_offset
- offset
;
1625 error
= copyout((caddr_t
)&l2p_struct
, argp
, sizeof(l2p_struct
));
1633 if (fp
->f_type
!= DTYPE_VNODE
) {
1637 vp
= (struct vnode
*)fp
->f_data
;
1640 pathlen
= MAXPATHLEN
;
1641 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
1642 if (pathbufp
== NULL
) {
1646 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1647 error
= vn_getpath(vp
, pathbufp
, &pathlen
);
1648 (void)vnode_put(vp
);
1651 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
1653 FREE(pathbufp
, M_TEMP
);
1657 case F_PATHPKG_CHECK
: {
1661 if (fp
->f_type
!= DTYPE_VNODE
) {
1665 vp
= (struct vnode
*)fp
->f_data
;
1668 pathlen
= MAXPATHLEN
;
1669 pathbufp
= kalloc(MAXPATHLEN
);
1671 if ( (error
= copyinstr(argp
, pathbufp
, MAXPATHLEN
, &pathlen
)) == 0 ) {
1672 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1673 AUDIT_ARG(text
, pathbufp
);
1674 error
= vn_path_package_check(vp
, pathbufp
, pathlen
, retval
);
1676 (void)vnode_put(vp
);
1679 kfree(pathbufp
, MAXPATHLEN
);
1683 case F_CHKCLEAN
: // used by regression tests to see if all dirty pages got cleaned by fsync()
1684 case F_FULLFSYNC
: // fsync + flush the journal + DKIOCSYNCHRONIZE
1685 case F_BARRIERFSYNC
: // fsync + barrier
1686 case F_FREEZE_FS
: // freeze all other fs operations for the fs of this fd
1687 case F_THAW_FS
: { // thaw all frozen fs operations for the fs of this fd
1688 if (fp
->f_type
!= DTYPE_VNODE
) {
1692 vp
= (struct vnode
*)fp
->f_data
;
1695 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1696 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
1698 (void)vnode_put(vp
);
1704 * SPI (private) for opening a file starting from a dir fd
1707 struct user_fopenfrom fopen
;
1708 struct vnode_attr va
;
1709 struct nameidata nd
;
1712 /* Check if this isn't a valid file descriptor */
1713 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1714 (fp
->f_flag
& FREAD
) == 0) {
1718 vp
= (struct vnode
*)fp
->f_data
;
1721 if (vnode_getwithref(vp
)) {
1726 /* Only valid for directories */
1727 if (vp
->v_type
!= VDIR
) {
1733 /* Get flags, mode and pathname arguments. */
1734 if (IS_64BIT_PROCESS(p
)) {
1735 error
= copyin(argp
, &fopen
, sizeof(fopen
));
1737 struct user32_fopenfrom fopen32
;
1739 error
= copyin(argp
, &fopen32
, sizeof(fopen32
));
1740 fopen
.o_flags
= fopen32
.o_flags
;
1741 fopen
.o_mode
= fopen32
.o_mode
;
1742 fopen
.o_pathname
= CAST_USER_ADDR_T(fopen32
.o_pathname
);
1748 AUDIT_ARG(fflags
, fopen
.o_flags
);
1749 AUDIT_ARG(mode
, fopen
.o_mode
);
1751 /* Mask off all but regular access permissions */
1752 cmode
= ((fopen
.o_mode
&~ fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
1753 VATTR_SET(&va
, va_mode
, cmode
& ACCESSPERMS
);
1755 /* Start the lookup relative to the file descriptor's vnode. */
1756 NDINIT(&nd
, LOOKUP
, OP_OPEN
, USEDVP
| FOLLOW
| AUDITVNPATH1
, UIO_USERSPACE
,
1757 fopen
.o_pathname
, &context
);
1760 error
= open1(&context
, &nd
, fopen
.o_flags
, &va
,
1761 fileproc_alloc_init
, NULL
, retval
);
1767 * SPI (private) for unlinking a file starting from a dir fd
1769 case F_UNLINKFROM
: {
1770 user_addr_t pathname
;
1772 /* Check if this isn't a valid file descriptor */
1773 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1774 (fp
->f_flag
& FREAD
) == 0) {
1778 vp
= (struct vnode
*)fp
->f_data
;
1781 if (vnode_getwithref(vp
)) {
1786 /* Only valid for directories */
1787 if (vp
->v_type
!= VDIR
) {
1793 /* Get flags, mode and pathname arguments. */
1794 if (IS_64BIT_PROCESS(p
)) {
1795 pathname
= (user_addr_t
)argp
;
1797 pathname
= CAST_USER_ADDR_T(argp
);
1800 /* Start the lookup relative to the file descriptor's vnode. */
1801 error
= unlink1(&context
, vp
, pathname
, UIO_USERSPACE
, 0);
1810 case F_ADDFILESIGS_FOR_DYLD_SIM
:
1811 case F_ADDFILESIGS_RETURN
:
1813 struct cs_blob
*blob
= NULL
;
1814 struct user_fsignatures fs
;
1816 vm_offset_t kernel_blob_addr
;
1817 vm_size_t kernel_blob_size
;
1818 int blob_add_flags
= 0;
1820 if (fp
->f_type
!= DTYPE_VNODE
) {
1824 vp
= (struct vnode
*)fp
->f_data
;
1827 if (uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
1828 blob_add_flags
|= MAC_VNODE_CHECK_DYLD_SIM
;
1829 if ((p
->p_csflags
& CS_KILL
) == 0) {
1831 p
->p_csflags
|= CS_KILL
;
1836 error
= vnode_getwithref(vp
);
1840 if (IS_64BIT_PROCESS(p
)) {
1841 error
= copyin(argp
, &fs
, sizeof (fs
));
1843 struct user32_fsignatures fs32
;
1845 error
= copyin(argp
, &fs32
, sizeof (fs32
));
1846 fs
.fs_file_start
= fs32
.fs_file_start
;
1847 fs
.fs_blob_start
= CAST_USER_ADDR_T(fs32
.fs_blob_start
);
1848 fs
.fs_blob_size
= fs32
.fs_blob_size
;
1857 * First check if we have something loaded a this offset
1859 blob
= ubc_cs_blob_get(vp
, CPU_TYPE_ANY
, fs
.fs_file_start
);
1862 /* If this is for dyld_sim revalidate the blob */
1863 if (uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
1864 error
= ubc_cs_blob_revalidate(vp
, blob
, NULL
, blob_add_flags
);
1867 if (error
!= EAGAIN
) {
1877 * An arbitrary limit, to prevent someone from mapping in a 20GB blob. This should cover
1878 * our use cases for the immediate future, but note that at the time of this commit, some
1879 * platforms are nearing 2MB blob sizes (with a prior soft limit of 2.5MB).
1881 * We should consider how we can manage this more effectively; the above means that some
1882 * platforms are using megabytes of memory for signing data; it merely hasn't crossed the
1883 * threshold considered ridiculous at the time of this change.
1885 #define CS_MAX_BLOB_SIZE (40ULL * 1024ULL * 1024ULL)
1886 if (fs
.fs_blob_size
> CS_MAX_BLOB_SIZE
) {
1892 kernel_blob_size
= CAST_DOWN(vm_size_t
, fs
.fs_blob_size
);
1893 kr
= ubc_cs_blob_allocate(&kernel_blob_addr
, &kernel_blob_size
);
1894 if (kr
!= KERN_SUCCESS
) {
1900 if(uap
->cmd
== F_ADDSIGS
) {
1901 error
= copyin(fs
.fs_blob_start
,
1902 (void *) kernel_blob_addr
,
1904 } else /* F_ADDFILESIGS || F_ADDFILESIGS_RETURN || F_ADDFILESIGS_FOR_DYLD_SIM */ {
1907 error
= vn_rdwr(UIO_READ
,
1909 (caddr_t
) kernel_blob_addr
,
1911 fs
.fs_file_start
+ fs
.fs_blob_start
,
1917 if ((error
== 0) && resid
) {
1918 /* kernel_blob_size rounded to a page size, but signature may be at end of file */
1919 memset((void *)(kernel_blob_addr
+ (kernel_blob_size
- resid
)), 0x0, resid
);
1924 ubc_cs_blob_deallocate(kernel_blob_addr
,
1931 error
= ubc_cs_blob_add(vp
,
1932 CPU_TYPE_ANY
, /* not for a specific architecture */
1940 /* ubc_blob_add() has consumed "kernel_blob_addr" if it is zeroed */
1942 if (kernel_blob_addr
) {
1943 ubc_cs_blob_deallocate(kernel_blob_addr
,
1949 #if CHECK_CS_VALIDATION_BITMAP
1950 ubc_cs_validation_bitmap_allocate( vp
);
1955 if (uap
->cmd
== F_ADDFILESIGS_RETURN
|| uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
1957 * The first element of the structure is a
1958 * off_t that happen to have the same size for
1959 * all archs. Lets overwrite that.
1961 off_t end_offset
= 0;
1963 end_offset
= blob
->csb_end_offset
;
1964 error
= copyout(&end_offset
, argp
, sizeof (end_offset
));
1967 (void) vnode_put(vp
);
1976 struct fileglob
*fg
;
1979 if (fp
->f_type
!= DTYPE_VNODE
) {
1986 if (IS_64BIT_PROCESS(p
)) {
1987 error
= copyin(argp
, &lv
, sizeof (lv
));
1989 struct user32_fchecklv lv32
;
1991 error
= copyin(argp
, &lv32
, sizeof (lv32
));
1992 lv
.lv_file_start
= lv32
.lv_file_start
;
1993 lv
.lv_error_message
= (void *)(uintptr_t)lv32
.lv_error_message
;
1994 lv
.lv_error_message_size
= lv32
.lv_error_message
;
2000 error
= mac_file_check_library_validation(p
, fg
, lv
.lv_file_start
,
2001 (user_long_t
)lv
.lv_error_message
, lv
.lv_error_message_size
);
2007 case F_GETPROTECTIONCLASS
: {
2008 if (fp
->f_type
!= DTYPE_VNODE
) {
2012 vp
= (struct vnode
*)fp
->f_data
;
2016 if (vnode_getwithref(vp
)) {
2021 struct vnode_attr va
;
2024 VATTR_WANTED(&va
, va_dataprotect_class
);
2025 error
= VNOP_GETATTR(vp
, &va
, &context
);
2027 if (VATTR_IS_SUPPORTED(&va
, va_dataprotect_class
))
2028 *retval
= va
.va_dataprotect_class
;
2037 case F_SETPROTECTIONCLASS
: {
2038 /* tmp must be a valid PROTECTION_CLASS_* */
2039 tmp
= CAST_DOWN_EXPLICIT(uint32_t, uap
->arg
);
2041 if (fp
->f_type
!= DTYPE_VNODE
) {
2045 vp
= (struct vnode
*)fp
->f_data
;
2049 if (vnode_getwithref(vp
)) {
2054 /* Only go forward if you have write access */
2055 vfs_context_t ctx
= vfs_context_current();
2056 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2062 struct vnode_attr va
;
2065 VATTR_SET(&va
, va_dataprotect_class
, tmp
);
2067 error
= VNOP_SETATTR(vp
, &va
, ctx
);
2073 case F_TRANSCODEKEY
: {
2074 if (fp
->f_type
!= DTYPE_VNODE
) {
2079 vp
= (struct vnode
*)fp
->f_data
;
2082 if (vnode_getwithref(vp
)) {
2088 .len
= CP_MAX_WRAPPEDKEYSIZE
,
2091 MALLOC(k
.key
, char *, k
.len
, M_TEMP
, M_WAITOK
| M_ZERO
);
2093 error
= VNOP_IOCTL(vp
, F_TRANSCODEKEY
, (caddr_t
)&k
, 1, &context
);
2098 error
= copyout(k
.key
, argp
, k
.len
);
2102 FREE(k
.key
, M_TEMP
);
2107 case F_GETPROTECTIONLEVEL
: {
2108 if (fp
->f_type
!= DTYPE_VNODE
) {
2113 vp
= (struct vnode
*) fp
->f_data
;
2116 if (vnode_getwithref(vp
)) {
2121 error
= VNOP_IOCTL(vp
, F_GETPROTECTIONLEVEL
, (caddr_t
)retval
, 0, &context
);
2127 case F_GETDEFAULTPROTLEVEL
: {
2128 if (fp
->f_type
!= DTYPE_VNODE
) {
2133 vp
= (struct vnode
*) fp
->f_data
;
2136 if (vnode_getwithref(vp
)) {
2142 * if cp_get_major_vers fails, error will be set to proper errno
2143 * and cp_version will still be 0.
2146 error
= VNOP_IOCTL(vp
, F_GETDEFAULTPROTLEVEL
, (caddr_t
)retval
, 0, &context
);
2152 #endif /* CONFIG_PROTECT */
2154 case F_MOVEDATAEXTENTS
: {
2155 struct fileproc
*fp2
= NULL
;
2156 struct vnode
*src_vp
= NULLVP
;
2157 struct vnode
*dst_vp
= NULLVP
;
2158 /* We need to grab the 2nd FD out of the argments before moving on. */
2159 int fd2
= CAST_DOWN_EXPLICIT(int32_t, uap
->arg
);
2161 error
= priv_check_cred(kauth_cred_get(), PRIV_VFS_MOVE_DATA_EXTENTS
, 0);
2165 if (fp
->f_type
!= DTYPE_VNODE
) {
2170 /* For now, special case HFS+ only, since this is SPI. */
2171 src_vp
= (struct vnode
*)fp
->f_data
;
2172 if (src_vp
->v_tag
!= VT_HFS
) {
2178 * Get the references before we start acquiring iocounts on the vnodes,
2179 * while we still hold the proc fd lock
2181 if ( (error
= fp_lookup(p
, fd2
, &fp2
, 1)) ) {
2185 if (fp2
->f_type
!= DTYPE_VNODE
) {
2186 fp_drop(p
, fd2
, fp2
, 1);
2190 dst_vp
= (struct vnode
*)fp2
->f_data
;
2191 if (dst_vp
->v_tag
!= VT_HFS
) {
2192 fp_drop(p
, fd2
, fp2
, 1);
2198 /* Re-do MAC checks against the new FD, pass in a fake argument */
2199 error
= mac_file_check_fcntl(proc_ucred(p
), fp2
->f_fglob
, uap
->cmd
, 0);
2201 fp_drop(p
, fd2
, fp2
, 1);
2205 /* Audit the 2nd FD */
2210 if (vnode_getwithref(src_vp
)) {
2211 fp_drop(p
, fd2
, fp2
, 0);
2215 if (vnode_getwithref(dst_vp
)) {
2217 fp_drop(p
, fd2
, fp2
, 0);
2223 * Basic asserts; validate they are not the same and that
2224 * both live on the same filesystem.
2226 if (dst_vp
== src_vp
) {
2229 fp_drop (p
, fd2
, fp2
, 0);
2234 if (dst_vp
->v_mount
!= src_vp
->v_mount
) {
2237 fp_drop (p
, fd2
, fp2
, 0);
2242 /* Now we have a legit pair of FDs. Go to work */
2244 /* Now check for write access to the target files */
2245 if(vnode_authorize(src_vp
, NULLVP
,
2246 (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), &context
) != 0) {
2249 fp_drop(p
, fd2
, fp2
, 0);
2254 if(vnode_authorize(dst_vp
, NULLVP
,
2255 (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), &context
) != 0) {
2258 fp_drop(p
, fd2
, fp2
, 0);
2263 /* Verify that both vps point to files and not directories */
2264 if ( !vnode_isreg(src_vp
) || !vnode_isreg(dst_vp
)) {
2268 fp_drop (p
, fd2
, fp2
, 0);
2273 * The exchangedata syscall handler passes in 0 for the flags to VNOP_EXCHANGE.
2274 * We'll pass in our special bit indicating that the new behavior is expected
2277 error
= VNOP_EXCHANGE(src_vp
, dst_vp
, FSOPT_EXCHANGE_DATA_ONLY
, &context
);
2281 fp_drop(p
, fd2
, fp2
, 0);
2286 * SPI for making a file compressed.
2288 case F_MAKECOMPRESSED
: {
2289 uint32_t gcounter
= CAST_DOWN_EXPLICIT(uint32_t, uap
->arg
);
2291 if (fp
->f_type
!= DTYPE_VNODE
) {
2296 vp
= (struct vnode
*) fp
->f_data
;
2300 if (vnode_getwithref(vp
)) {
2306 if ((vnode_isreg(vp
) == 0) && (vnode_islnk(vp
) == 0)) {
2312 /* invoke ioctl to pass off to FS */
2313 /* Only go forward if you have write access */
2314 vfs_context_t ctx
= vfs_context_current();
2315 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2321 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)&gcounter
, 0, &context
);
2328 * SPI (private) for indicating to a filesystem that subsequent writes to
2329 * the open FD will written to the Fastflow.
2331 case F_SET_GREEDY_MODE
:
2332 /* intentionally drop through to the same handler as F_SETSTATIC.
2333 * both fcntls should pass the argument and their selector into VNOP_IOCTL.
2337 * SPI (private) for indicating to a filesystem that subsequent writes to
2338 * the open FD will represent static content.
2340 case F_SETSTATICCONTENT
: {
2341 caddr_t ioctl_arg
= NULL
;
2344 ioctl_arg
= (caddr_t
) 1;
2347 if (fp
->f_type
!= DTYPE_VNODE
) {
2351 vp
= (struct vnode
*)fp
->f_data
;
2354 error
= vnode_getwithref(vp
);
2360 /* Only go forward if you have write access */
2361 vfs_context_t ctx
= vfs_context_current();
2362 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2368 error
= VNOP_IOCTL(vp
, uap
->cmd
, ioctl_arg
, 0, &context
);
2369 (void)vnode_put(vp
);
2375 * SPI (private) for indicating to the lower level storage driver that the
2376 * subsequent writes should be of a particular IO type (burst, greedy, static),
2377 * or other flavors that may be necessary.
2384 /* extract 32 bits of flags from userland */
2385 param_ptr
= (caddr_t
) uap
->arg
;
2386 param
= (uint32_t) param_ptr
;
2389 /* If no argument is specified, error out */
2395 * Validate the different types of flags that can be specified:
2396 * all of them are mutually exclusive for now.
2399 case F_IOTYPE_ISOCHRONOUS
:
2408 if (fp
->f_type
!= DTYPE_VNODE
) {
2412 vp
= (struct vnode
*)fp
->f_data
;
2415 error
= vnode_getwithref(vp
);
2421 /* Only go forward if you have write access */
2422 vfs_context_t ctx
= vfs_context_current();
2423 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2429 error
= VNOP_IOCTL(vp
, uap
->cmd
, param_ptr
, 0, &context
);
2430 (void)vnode_put(vp
);
2436 * Set the vnode pointed to by 'fd'
2437 * and tag it as the (potentially future) backing store
2438 * for another filesystem
2440 case F_SETBACKINGSTORE
: {
2441 if (fp
->f_type
!= DTYPE_VNODE
) {
2446 vp
= (struct vnode
*)fp
->f_data
;
2448 if (vp
->v_tag
!= VT_HFS
) {
2454 if (vnode_getwithref(vp
)) {
2459 /* only proceed if you have write access */
2460 vfs_context_t ctx
= vfs_context_current();
2461 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2468 /* If arg != 0, set, otherwise unset */
2470 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
)1, 0, &context
);
2473 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
2481 * like F_GETPATH, but special semantics for
2482 * the mobile time machine handler.
2484 case F_GETPATH_MTMINFO
: {
2488 if (fp
->f_type
!= DTYPE_VNODE
) {
2492 vp
= (struct vnode
*)fp
->f_data
;
2495 pathlen
= MAXPATHLEN
;
2496 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
2497 if (pathbufp
== NULL
) {
2501 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
2502 int backingstore
= 0;
2504 /* Check for error from vn_getpath before moving on */
2505 if ((error
= vn_getpath(vp
, pathbufp
, &pathlen
)) == 0) {
2506 if (vp
->v_tag
== VT_HFS
) {
2507 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
) &backingstore
, 0, &context
);
2509 (void)vnode_put(vp
);
2512 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
2516 * If the copyout was successful, now check to ensure
2517 * that this vnode is not a BACKINGSTORE vnode. mtmd
2518 * wants the path regardless.
2525 (void)vnode_put(vp
);
2527 FREE(pathbufp
, M_TEMP
);
2531 #if DEBUG || DEVELOPMENT
2533 if (fp
->f_type
!= DTYPE_VNODE
) {
2537 vp
= (struct vnode
*)fp
->f_data
;
2546 * This is an fcntl() that we d not recognize at this level;
2547 * if this is a vnode, we send it down into the VNOP_IOCTL
2548 * for this vnode; this can include special devices, and will
2549 * effectively overload fcntl() to send ioctl()'s.
2551 if((uap
->cmd
& IOC_VOID
) && (uap
->cmd
& IOC_INOUT
)){
2556 /* Catch any now-invalid fcntl() selectors */
2558 case (int)APFSIOC_REVERT_TO_SNAPSHOT
:
2559 case (int)FSIOC_FIOSEEKHOLE
:
2560 case (int)FSIOC_FIOSEEKDATA
:
2561 case HFS_GET_BOOT_INFO
:
2562 case HFS_SET_BOOT_INFO
:
2564 case F_MARKDEPENDENCY
:
2571 if (fp
->f_type
!= DTYPE_VNODE
) {
2575 vp
= (struct vnode
*)fp
->f_data
;
2578 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
2579 #define STK_PARAMS 128
2580 char stkbuf
[STK_PARAMS
] = {0};
2584 * For this to work properly, we have to copy in the
2585 * ioctl() cmd argument if there is one; we must also
2586 * check that a command parameter, if present, does
2587 * not exceed the maximum command length dictated by
2588 * the number of bits we have available in the command
2589 * to represent a structure length. Finally, we have
2590 * to copy the results back out, if it is that type of
2593 size
= IOCPARM_LEN(uap
->cmd
);
2594 if (size
> IOCPARM_MAX
) {
2595 (void)vnode_put(vp
);
2601 if (size
> sizeof (stkbuf
)) {
2602 if ((memp
= (caddr_t
)kalloc(size
)) == 0) {
2603 (void)vnode_put(vp
);
2612 if (uap
->cmd
& IOC_IN
) {
2615 error
= copyin(argp
, data
, size
);
2617 (void)vnode_put(vp
);
2623 /* Bzero the section beyond that which was needed */
2624 if (size
<= sizeof(stkbuf
)) {
2625 bzero ( (((uint8_t*)data
) + size
), (sizeof(stkbuf
) - size
));
2630 *(user_addr_t
*)data
= argp
;
2632 *(uint32_t *)data
= (uint32_t)argp
;
2635 } else if ((uap
->cmd
& IOC_OUT
) && size
) {
2637 * Zero the buffer so the user always
2638 * gets back something deterministic.
2641 } else if (uap
->cmd
& IOC_VOID
) {
2643 *(user_addr_t
*)data
= argp
;
2645 *(uint32_t *)data
= (uint32_t)argp
;
2649 error
= VNOP_IOCTL(vp
, uap
->cmd
, CAST_DOWN(caddr_t
, data
), 0, &context
);
2651 (void)vnode_put(vp
);
2653 /* Copy any output data to user */
2654 if (error
== 0 && (uap
->cmd
& IOC_OUT
) && size
)
2655 error
= copyout(data
, argp
, size
);
2663 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
2664 fp_drop(p
, fd
, fp
, 0);
2667 fp_drop(p
, fd
, fp
, 1);
2676 * Description: Common code for dup, dup2, and fcntl(F_DUPFD).
2678 * Parameters: p Process performing the dup
2680 * new The fd to dup it to
2681 * fd_flags Flags to augment the new fd
2682 * retval Pointer to the call return area
2684 * Returns: 0 Success
2689 * *retval (modified) The new descriptor
2691 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
2694 * Notes: This function may drop and reacquire this lock; it is unsafe
2695 * for a caller to assume that other state protected by the lock
2696 * has not been subsequently changed out from under it.
2700 struct filedesc
*fdp
, int old
, int new, int fd_flags
, int32_t *retval
)
2702 struct fileproc
*nfp
;
2703 struct fileproc
*ofp
;
2709 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2711 if ((ofp
= fdp
->fd_ofiles
[old
]) == NULL
||
2712 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
)) {
2719 error
= mac_file_check_dup(proc_ucred(p
), ofp
->f_fglob
, new);
2729 nfp
= fileproc_alloc_init(NULL
);
2739 nfp
->f_fglob
= ofp
->f_fglob
;
2742 if (fdp
->fd_ofiles
[new] != 0)
2743 panic("finishdup: overwriting fd_ofiles with new %d", new);
2744 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0)
2745 panic("finishdup: unreserved fileflags with new %d", new);
2748 if (new > fdp
->fd_lastfile
)
2749 fdp
->fd_lastfile
= new;
2750 *fdflags(p
, new) |= fd_flags
;
2751 procfdtbl_releasefd(p
, new, nfp
);
2760 * Description: The implementation of the close(2) system call
2762 * Parameters: p Process in whose per process file table
2763 * the close is to occur
2764 * uap->fd fd to be closed
2767 * Returns: 0 Success
2768 * fp_lookup:EBADF Bad file descriptor
2769 * fp_guard_exception:??? Guarded file descriptor
2770 * close_internal:EBADF
2771 * close_internal:??? Anything returnable by a per-fileops
2775 close(proc_t p
, struct close_args
*uap
, int32_t *retval
)
2777 __pthread_testcancel(1);
2778 return(close_nocancel(p
, (struct close_nocancel_args
*)uap
, retval
));
2783 close_nocancel(proc_t p
, struct close_nocancel_args
*uap
, __unused
int32_t *retval
)
2785 struct fileproc
*fp
;
2789 AUDIT_SYSCLOSE(p
, fd
);
2793 if ( (error
= fp_lookup(p
,fd
,&fp
, 1)) ) {
2798 if (FP_ISGUARDED(fp
, GUARD_CLOSE
)) {
2799 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_CLOSE
);
2800 (void) fp_drop(p
, fd
, fp
, 1);
2805 error
= close_internal_locked(p
, fd
, fp
, 0);
2814 * close_internal_locked
2816 * Close a file descriptor.
2818 * Parameters: p Process in whose per process file table
2819 * the close is to occur
2820 * fd fd to be closed
2821 * fp fileproc associated with the fd
2823 * Returns: 0 Success
2824 * EBADF fd already in close wait state
2825 * closef_locked:??? Anything returnable by a per-fileops
2828 * Locks: Assumes proc_fdlock for process is held by the caller and returns
2831 * Notes: This function may drop and reacquire this lock; it is unsafe
2832 * for a caller to assume that other state protected by the lock
2833 * has not been subsequently changed out from under it.
2836 close_internal_locked(proc_t p
, int fd
, struct fileproc
*fp
, int flags
)
2838 struct filedesc
*fdp
= p
->p_fd
;
2840 int resvfd
= flags
& FD_DUP2RESV
;
2844 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2847 /* Keep people from using the filedesc while we are closing it */
2848 procfdtbl_markclosefd(p
, fd
);
2851 if ((fp
->f_flags
& FP_CLOSING
) == FP_CLOSING
) {
2852 panic("close_internal_locked: being called on already closing fd");
2857 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
2858 panic("close_internal: unreserved fileflags with fd %d", fd
);
2861 fp
->f_flags
|= FP_CLOSING
;
2863 if ( (fp
->f_flags
& FP_AIOISSUED
) || kauth_authorize_fileop_has_listeners() ) {
2867 if ( (fp
->f_type
== DTYPE_VNODE
) && kauth_authorize_fileop_has_listeners() ) {
2869 * call out to allow 3rd party notification of close.
2870 * Ignore result of kauth_authorize_fileop call.
2872 if (vnode_getwithref((vnode_t
)fp
->f_data
) == 0) {
2873 u_int fileop_flags
= 0;
2874 if ((fp
->f_flags
& FP_WRITTEN
) != 0)
2875 fileop_flags
|= KAUTH_FILEOP_CLOSE_MODIFIED
;
2876 kauth_authorize_fileop(fp
->f_fglob
->fg_cred
, KAUTH_FILEOP_CLOSE
,
2877 (uintptr_t)fp
->f_data
, (uintptr_t)fileop_flags
);
2878 vnode_put((vnode_t
)fp
->f_data
);
2881 if (fp
->f_flags
& FP_AIOISSUED
)
2883 * cancel all async IO requests that can be cancelled.
2885 _aio_close( p
, fd
);
2890 if (fd
< fdp
->fd_knlistsize
)
2891 knote_fdclose(p
, fd
);
2893 if (fp
->f_flags
& FP_WAITEVENT
)
2894 (void)waitevent_close(p
, fp
);
2896 fileproc_drain(p
, fp
);
2901 procfdtbl_reservefd(p
, fd
);
2904 if (ENTR_SHOULDTRACE
&& fp
->f_type
== DTYPE_SOCKET
)
2905 KERNEL_ENERGYTRACE(kEnTrActKernSocket
, DBG_FUNC_END
,
2906 fd
, 0, (int64_t)VM_KERNEL_ADDRPERM(fp
->f_data
));
2908 error
= closef_locked(fp
, fp
->f_fglob
, p
);
2909 if ((fp
->f_flags
& FP_WAITCLOSE
) == FP_WAITCLOSE
)
2910 wakeup(&fp
->f_flags
);
2911 fp
->f_flags
&= ~(FP_WAITCLOSE
| FP_CLOSING
);
2921 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
2922 panic("close with reserved fd returns with freed fd:%d: proc: %p", fd
, p
);
2933 * Description: Return status information about a file descriptor.
2935 * Parameters: p The process doing the fstat
2937 * ub The user stat buffer
2938 * xsecurity The user extended security
2939 * buffer, or 0 if none
2940 * xsecurity_size The size of xsecurity, or 0
2942 * isstat64 Flag to indicate 64 bit version
2943 * for inode size, etc.
2945 * Returns: 0 Success
2948 * fp_lookup:EBADF Bad file descriptor
2949 * vnode_getwithref:???
2951 * vnode_getwithref:???
2958 * Notes: Internal implementation for all other fstat() related
2961 * XXX switch on node type is bogus; need a stat in struct
2962 * XXX fileops instead.
2965 fstat1(proc_t p
, int fd
, user_addr_t ub
, user_addr_t xsecurity
, user_addr_t xsecurity_size
, int isstat64
)
2967 struct fileproc
*fp
;
2973 struct user64_stat user64_sb
;
2974 struct user32_stat user32_sb
;
2975 struct user64_stat64 user64_sb64
;
2976 struct user32_stat64 user32_sb64
;
2981 kauth_filesec_t fsec
;
2982 user_size_t xsecurity_bufsize
;
2983 vfs_context_t ctx
= vfs_context_current();
2989 if ((error
= fp_lookup(p
, fd
, &fp
, 0)) != 0) {
2994 fsec
= KAUTH_FILESEC_NONE
;
2996 sbptr
= (void *)&source
;
3001 if ((error
= vnode_getwithref((vnode_t
)data
)) == 0) {
3003 * If the caller has the file open, and is not
3004 * requesting extended security information, we are
3005 * going to let them get the basic stat information.
3007 if (xsecurity
== USER_ADDR_NULL
) {
3008 error
= vn_stat_noauth((vnode_t
)data
, sbptr
, NULL
, isstat64
, ctx
,
3009 fp
->f_fglob
->fg_cred
);
3011 error
= vn_stat((vnode_t
)data
, sbptr
, &fsec
, isstat64
, ctx
);
3014 AUDIT_ARG(vnpath
, (struct vnode
*)data
, ARG_VNODE1
);
3015 (void)vnode_put((vnode_t
)data
);
3021 error
= soo_stat((struct socket
*)data
, sbptr
, isstat64
);
3023 #endif /* SOCKETS */
3026 error
= pipe_stat((void *)data
, sbptr
, isstat64
);
3030 error
= pshm_stat((void *)data
, sbptr
, isstat64
);
3034 error
= kqueue_stat((void *)data
, sbptr
, isstat64
, p
);
3044 if (isstat64
!= 0) {
3045 source
.sb64
.st_lspare
= 0;
3046 source
.sb64
.st_qspare
[0] = 0LL;
3047 source
.sb64
.st_qspare
[1] = 0LL;
3049 if (IS_64BIT_PROCESS(current_proc())) {
3050 munge_user64_stat64(&source
.sb64
, &dest
.user64_sb64
);
3051 my_size
= sizeof(dest
.user64_sb64
);
3052 sbp
= (caddr_t
)&dest
.user64_sb64
;
3054 munge_user32_stat64(&source
.sb64
, &dest
.user32_sb64
);
3055 my_size
= sizeof(dest
.user32_sb64
);
3056 sbp
= (caddr_t
)&dest
.user32_sb64
;
3059 source
.sb
.st_lspare
= 0;
3060 source
.sb
.st_qspare
[0] = 0LL;
3061 source
.sb
.st_qspare
[1] = 0LL;
3062 if (IS_64BIT_PROCESS(current_proc())) {
3063 munge_user64_stat(&source
.sb
, &dest
.user64_sb
);
3064 my_size
= sizeof(dest
.user64_sb
);
3065 sbp
= (caddr_t
)&dest
.user64_sb
;
3067 munge_user32_stat(&source
.sb
, &dest
.user32_sb
);
3068 my_size
= sizeof(dest
.user32_sb
);
3069 sbp
= (caddr_t
)&dest
.user32_sb
;
3073 error
= copyout(sbp
, ub
, my_size
);
3076 /* caller wants extended security information? */
3077 if (xsecurity
!= USER_ADDR_NULL
) {
3079 /* did we get any? */
3080 if (fsec
== KAUTH_FILESEC_NONE
) {
3081 if (susize(xsecurity_size
, 0) != 0) {
3086 /* find the user buffer size */
3087 xsecurity_bufsize
= fusize(xsecurity_size
);
3089 /* copy out the actual data size */
3090 if (susize(xsecurity_size
, KAUTH_FILESEC_COPYSIZE(fsec
)) != 0) {
3095 /* if the caller supplied enough room, copy out to it */
3096 if (xsecurity_bufsize
>= KAUTH_FILESEC_COPYSIZE(fsec
))
3097 error
= copyout(fsec
, xsecurity
, KAUTH_FILESEC_COPYSIZE(fsec
));
3101 fp_drop(p
, fd
, fp
, 0);
3103 kauth_filesec_free(fsec
);
3111 * Description: Extended version of fstat supporting returning extended
3112 * security information
3114 * Parameters: p The process doing the fstat
3115 * uap->fd The fd to stat
3116 * uap->ub The user stat buffer
3117 * uap->xsecurity The user extended security
3118 * buffer, or 0 if none
3119 * uap->xsecurity_size The size of xsecurity, or 0
3121 * Returns: 0 Success
3122 * !0 Errno (see fstat1)
3125 fstat_extended(proc_t p
, struct fstat_extended_args
*uap
, __unused
int32_t *retval
)
3127 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 0));
3134 * Description: Get file status for the file associated with fd
3136 * Parameters: p The process doing the fstat
3137 * uap->fd The fd to stat
3138 * uap->ub The user stat buffer
3140 * Returns: 0 Success
3141 * !0 Errno (see fstat1)
3144 fstat(proc_t p
, struct fstat_args
*uap
, __unused
int32_t *retval
)
3146 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 0));
3153 * Description: Extended version of fstat64 supporting returning extended
3154 * security information
3156 * Parameters: p The process doing the fstat
3157 * uap->fd The fd to stat
3158 * uap->ub The user stat buffer
3159 * uap->xsecurity The user extended security
3160 * buffer, or 0 if none
3161 * uap->xsecurity_size The size of xsecurity, or 0
3163 * Returns: 0 Success
3164 * !0 Errno (see fstat1)
3167 fstat64_extended(proc_t p
, struct fstat64_extended_args
*uap
, __unused
int32_t *retval
)
3169 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 1));
3176 * Description: Get 64 bit version of the file status for the file associated
3179 * Parameters: p The process doing the fstat
3180 * uap->fd The fd to stat
3181 * uap->ub The user stat buffer
3183 * Returns: 0 Success
3184 * !0 Errno (see fstat1)
3187 fstat64(proc_t p
, struct fstat64_args
*uap
, __unused
int32_t *retval
)
3189 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 1));
3196 * Description: Return pathconf information about a file descriptor.
3198 * Parameters: p Process making the request
3199 * uap->fd fd to get information about
3200 * uap->name Name of information desired
3201 * retval Pointer to the call return area
3203 * Returns: 0 Success
3205 * fp_lookup:EBADF Bad file descriptor
3206 * vnode_getwithref:???
3210 * *retval (modified) Returned information (numeric)
3213 fpathconf(proc_t p
, struct fpathconf_args
*uap
, int32_t *retval
)
3216 struct fileproc
*fp
;
3223 AUDIT_ARG(fd
, uap
->fd
);
3224 if ( (error
= fp_lookup(p
, fd
, &fp
, 0)) )
3232 if (uap
->name
!= _PC_PIPE_BUF
) {
3241 if (uap
->name
!= _PC_PIPE_BUF
) {
3250 vp
= (struct vnode
*)data
;
3252 if ( (error
= vnode_getwithref(vp
)) == 0) {
3253 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
3255 error
= vn_pathconf(vp
, uap
->name
, retval
, vfs_context_current());
3257 (void)vnode_put(vp
);
3268 fp_drop(p
, fd
, fp
, 0);
3273 * Statistics counter for the number of times a process calling fdalloc()
3274 * has resulted in an expansion of the per process open file table.
3276 * XXX This would likely be of more use if it were per process
3284 * Description: Allocate a file descriptor for the process.
3286 * Parameters: p Process to allocate the fd in
3287 * want The fd we would prefer to get
3288 * result Pointer to fd we got
3290 * Returns: 0 Success
3295 * *result (modified) The fd which was allocated
3298 fdalloc(proc_t p
, int want
, int *result
)
3300 struct filedesc
*fdp
= p
->p_fd
;
3302 int lim
, last
, numfiles
, oldnfiles
;
3303 struct fileproc
**newofiles
, **ofiles
;
3304 char *newofileflags
;
3307 * Search for a free descriptor starting at the higher
3308 * of want or fd_freefile. If that fails, consider
3309 * expanding the ofile array.
3312 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
3315 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
3317 last
= min(fdp
->fd_nfiles
, lim
);
3318 if ((i
= want
) < fdp
->fd_freefile
)
3319 i
= fdp
->fd_freefile
;
3320 for (; i
< last
; i
++) {
3321 if (fdp
->fd_ofiles
[i
] == NULL
&& !(fdp
->fd_ofileflags
[i
] & UF_RESERVED
)) {
3322 procfdtbl_reservefd(p
, i
);
3323 if (i
> fdp
->fd_lastfile
)
3324 fdp
->fd_lastfile
= i
;
3325 if (want
<= fdp
->fd_freefile
)
3326 fdp
->fd_freefile
= i
;
3333 * No space in current array. Expand?
3335 if (fdp
->fd_nfiles
>= lim
)
3337 if (fdp
->fd_nfiles
< NDEXTENT
)
3338 numfiles
= NDEXTENT
;
3340 numfiles
= 2 * fdp
->fd_nfiles
;
3345 MALLOC_ZONE(newofiles
, struct fileproc
**,
3346 numfiles
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
3348 if (newofiles
== NULL
) {
3351 if (fdp
->fd_nfiles
>= numfiles
) {
3352 FREE_ZONE(newofiles
, numfiles
* OFILESIZE
, M_OFILETABL
);
3355 newofileflags
= (char *) &newofiles
[numfiles
];
3357 * Copy the existing ofile and ofileflags arrays
3358 * and zero the new portion of each array.
3360 oldnfiles
= fdp
->fd_nfiles
;
3361 (void) memcpy(newofiles
, fdp
->fd_ofiles
,
3362 oldnfiles
* sizeof(*fdp
->fd_ofiles
));
3363 (void) memset(&newofiles
[oldnfiles
], 0,
3364 (numfiles
- oldnfiles
) * sizeof(*fdp
->fd_ofiles
));
3366 (void) memcpy(newofileflags
, fdp
->fd_ofileflags
,
3367 oldnfiles
* sizeof(*fdp
->fd_ofileflags
));
3368 (void) memset(&newofileflags
[oldnfiles
], 0,
3369 (numfiles
- oldnfiles
) *
3370 sizeof(*fdp
->fd_ofileflags
));
3371 ofiles
= fdp
->fd_ofiles
;
3372 fdp
->fd_ofiles
= newofiles
;
3373 fdp
->fd_ofileflags
= newofileflags
;
3374 fdp
->fd_nfiles
= numfiles
;
3375 FREE_ZONE(ofiles
, oldnfiles
* OFILESIZE
, M_OFILETABL
);
3384 * Description: Check to see whether n user file descriptors are available
3387 * Parameters: p Process to check in
3388 * n The number of fd's desired
3393 * Locks: Assumes proc_fdlock for process is held by the caller
3395 * Notes: The answer only remains valid so long as the proc_fdlock is
3396 * held by the caller.
3399 fdavail(proc_t p
, int n
)
3401 struct filedesc
*fdp
= p
->p_fd
;
3402 struct fileproc
**fpp
;
3406 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
3407 if ((i
= lim
- fdp
->fd_nfiles
) > 0 && (n
-= i
) <= 0)
3409 fpp
= &fdp
->fd_ofiles
[fdp
->fd_freefile
];
3410 flags
= &fdp
->fd_ofileflags
[fdp
->fd_freefile
];
3411 for (i
= fdp
->fd_nfiles
- fdp
->fd_freefile
; --i
>= 0; fpp
++, flags
++)
3412 if (*fpp
== NULL
&& !(*flags
& UF_RESERVED
) && --n
<= 0)
3421 * Description: Legacy KPI wrapper function for _fdrelse
3423 * Parameters: p Process in which fd lives
3428 * Locks: Assumes proc_fdlock for process is held by the caller
3431 fdrelse(proc_t p
, int fd
)
3440 * Description: Get the fileproc pointer for the given fd from the per process
3441 * open file table without taking an explicit reference on it.
3443 * Parameters: p Process containing fd
3444 * fd fd to obtain fileproc for
3445 * resultfp Pointer to pointer return area
3447 * Returns: 0 Success
3451 * *resultfp (modified) Pointer to fileproc pointer
3453 * Locks: Assumes proc_fdlock for process is held by the caller
3455 * Notes: Because there is no reference explicitly taken, the returned
3456 * fileproc pointer is only valid so long as the proc_fdlock
3457 * remains held by the caller.
3460 fdgetf_noref(proc_t p
, int fd
, struct fileproc
**resultfp
)
3462 struct filedesc
*fdp
= p
->p_fd
;
3463 struct fileproc
*fp
;
3465 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3466 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3467 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3479 * Description: Get fileproc and vnode pointer for a given fd from the per
3480 * process open file table of the specified process, and if
3481 * successful, increment the f_iocount
3483 * Parameters: p Process in which fd lives
3484 * fd fd to get information for
3485 * resultfp Pointer to result fileproc
3486 * pointer area, or 0 if none
3487 * resultvp Pointer to result vnode pointer
3488 * area, or 0 if none
3490 * Returns: 0 Success
3491 * EBADF Bad file descriptor
3492 * ENOTSUP fd does not refer to a vnode
3495 * *resultfp (modified) Fileproc pointer
3496 * *resultvp (modified) vnode pointer
3498 * Notes: The resultfp and resultvp fields are optional, and may be
3499 * independently specified as NULL to skip returning information
3501 * Locks: Internally takes and releases proc_fdlock
3504 fp_getfvp(proc_t p
, int fd
, struct fileproc
**resultfp
, struct vnode
**resultvp
)
3506 struct filedesc
*fdp
= p
->p_fd
;
3507 struct fileproc
*fp
;
3509 proc_fdlock_spin(p
);
3510 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3511 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3512 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3516 if (fp
->f_type
!= DTYPE_VNODE
) {
3525 *resultvp
= (struct vnode
*)fp
->f_data
;
3535 * Description: Get fileproc, vnode pointer, and vid for a given fd from the
3536 * per 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
3545 * vidp Pointer to resuld vid area
3547 * Returns: 0 Success
3548 * EBADF Bad file descriptor
3549 * ENOTSUP fd does not refer to a vnode
3552 * *resultfp (modified) Fileproc pointer
3553 * *resultvp (modified) vnode pointer
3556 * Notes: The resultfp and resultvp fields are optional, and may be
3557 * independently specified as NULL to skip returning information
3559 * Locks: Internally takes and releases proc_fdlock
3562 fp_getfvpandvid(proc_t p
, int fd
, struct fileproc
**resultfp
,
3563 struct vnode
**resultvp
, uint32_t *vidp
)
3565 struct filedesc
*fdp
= p
->p_fd
;
3566 struct fileproc
*fp
;
3568 proc_fdlock_spin(p
);
3569 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3570 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3571 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3575 if (fp
->f_type
!= DTYPE_VNODE
) {
3584 *resultvp
= (struct vnode
*)fp
->f_data
;
3586 *vidp
= (uint32_t)vnode_vid((struct vnode
*)fp
->f_data
);
3596 * Description: Get fileproc and socket pointer for a given fd from the
3597 * per process open file table of the specified process, and if
3598 * successful, increment the f_iocount
3600 * Parameters: p Process in which fd lives
3601 * fd fd to get information for
3602 * resultfp Pointer to result fileproc
3603 * pointer area, or 0 if none
3604 * results Pointer to result socket
3605 * pointer area, or 0 if none
3607 * Returns: EBADF The file descriptor is invalid
3608 * EOPNOTSUPP The file descriptor is not a socket
3612 * *resultfp (modified) Fileproc pointer
3613 * *results (modified) socket pointer
3615 * Notes: EOPNOTSUPP should probably be ENOTSOCK; this function is only
3616 * ever called from accept1().
3619 fp_getfsock(proc_t p
, int fd
, struct fileproc
**resultfp
,
3620 struct socket
**results
)
3622 struct filedesc
*fdp
= p
->p_fd
;
3623 struct fileproc
*fp
;
3625 proc_fdlock_spin(p
);
3626 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3627 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3628 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3632 if (fp
->f_type
!= DTYPE_SOCKET
) {
3641 *results
= (struct socket
*)fp
->f_data
;
3651 * Description: Get fileproc and kqueue pointer for a given fd from the
3652 * per process open file table of the specified process, and if
3653 * successful, increment the f_iocount
3655 * Parameters: p Process in which fd lives
3656 * fd fd to get information for
3657 * resultfp Pointer to result fileproc
3658 * pointer area, or 0 if none
3659 * resultkq Pointer to result kqueue
3660 * pointer area, or 0 if none
3662 * Returns: EBADF The file descriptor is invalid
3663 * EBADF The file descriptor is not a socket
3667 * *resultfp (modified) Fileproc pointer
3668 * *resultkq (modified) kqueue pointer
3670 * Notes: The second EBADF should probably be something else to make
3671 * the error condition distinct.
3674 fp_getfkq(proc_t p
, int fd
, struct fileproc
**resultfp
,
3675 struct kqueue
**resultkq
)
3677 struct filedesc
*fdp
= p
->p_fd
;
3678 struct fileproc
*fp
;
3680 proc_fdlock_spin(p
);
3681 if ( fd
< 0 || fd
>= fdp
->fd_nfiles
||
3682 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3683 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3687 if (fp
->f_type
!= DTYPE_KQUEUE
) {
3696 *resultkq
= (struct kqueue
*)fp
->f_data
;
3706 * Description: Get fileproc and POSIX shared memory pointer for a given fd
3707 * from the per process open file table of the specified process
3708 * and if successful, increment the f_iocount
3710 * Parameters: p Process in which fd lives
3711 * fd fd to get information for
3712 * resultfp Pointer to result fileproc
3713 * pointer area, or 0 if none
3714 * resultpshm Pointer to result POSIX
3715 * shared memory pointer
3716 * pointer area, or 0 if none
3718 * Returns: EBADF The file descriptor is invalid
3719 * EBADF The file descriptor is not a POSIX
3720 * shared memory area
3724 * *resultfp (modified) Fileproc pointer
3725 * *resultpshm (modified) POSIX shared memory pointer
3727 * Notes: The second EBADF should probably be something else to make
3728 * the error condition distinct.
3731 fp_getfpshm(proc_t p
, int fd
, struct fileproc
**resultfp
,
3732 struct pshmnode
**resultpshm
)
3734 struct filedesc
*fdp
= p
->p_fd
;
3735 struct fileproc
*fp
;
3737 proc_fdlock_spin(p
);
3738 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3739 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3740 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3744 if (fp
->f_type
!= DTYPE_PSXSHM
) {
3754 *resultpshm
= (struct pshmnode
*)fp
->f_data
;
3764 * Description: Get fileproc and POSIX semaphore pointer for a given fd from
3765 * the per process open file table of the specified process
3766 * and if successful, increment the f_iocount
3768 * Parameters: p Process in which fd lives
3769 * fd fd to get information for
3770 * resultfp Pointer to result fileproc
3771 * pointer area, or 0 if none
3772 * resultpsem Pointer to result POSIX
3773 * semaphore pointer area, or
3776 * Returns: EBADF The file descriptor is invalid
3777 * EBADF The file descriptor is not a POSIX
3782 * *resultfp (modified) Fileproc pointer
3783 * *resultpsem (modified) POSIX semaphore pointer
3785 * Notes: The second EBADF should probably be something else to make
3786 * the error condition distinct.
3788 * In order to support unnamed POSIX semaphores, the named
3789 * POSIX semaphores will have to move out of the per-process
3790 * open filetable, and into a global table that is shared with
3791 * unnamed POSIX semaphores, since unnamed POSIX semaphores
3792 * are typically used by declaring instances in shared memory,
3793 * and there's no other way to do this without changing the
3794 * underlying type, which would introduce binary compatibility
3798 fp_getfpsem(proc_t p
, int fd
, struct fileproc
**resultfp
,
3799 struct psemnode
**resultpsem
)
3801 struct filedesc
*fdp
= p
->p_fd
;
3802 struct fileproc
*fp
;
3804 proc_fdlock_spin(p
);
3805 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3806 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3807 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3811 if (fp
->f_type
!= DTYPE_PSXSEM
) {
3820 *resultpsem
= (struct psemnode
*)fp
->f_data
;
3830 * Description: Get fileproc and pipe pointer for a given fd from the
3831 * 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 * resultpipe Pointer to result pipe
3839 * pointer area, or 0 if none
3841 * Returns: EBADF The file descriptor is invalid
3842 * EBADF The file descriptor is not a socket
3846 * *resultfp (modified) Fileproc pointer
3847 * *resultpipe (modified) pipe pointer
3849 * Notes: The second EBADF should probably be something else to make
3850 * the error condition distinct.
3853 fp_getfpipe(proc_t p
, int fd
, struct fileproc
**resultfp
,
3854 struct pipe
**resultpipe
)
3856 struct filedesc
*fdp
= p
->p_fd
;
3857 struct fileproc
*fp
;
3859 proc_fdlock_spin(p
);
3860 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3861 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3862 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3866 if (fp
->f_type
!= DTYPE_PIPE
) {
3875 *resultpipe
= (struct pipe
*)fp
->f_data
;
3885 * Description: Get fileproc pointer for a given fd from the per process
3886 * open file table of the specified process and if successful,
3887 * increment the f_iocount
3889 * Parameters: p Process in which fd lives
3890 * fd fd to get information for
3891 * resultfp Pointer to result fileproc
3892 * pointer area, or 0 if none
3893 * locked !0 if the caller holds the
3894 * proc_fdlock, 0 otherwise
3896 * Returns: 0 Success
3897 * EBADF Bad file descriptor
3900 * *resultfp (modified) Fileproc pointer
3902 * Locks: If the argument 'locked' is non-zero, then the caller is
3903 * expected to have taken and held the proc_fdlock; if it is
3904 * zero, than this routine internally takes and drops this lock.
3907 fp_lookup(proc_t p
, int fd
, struct fileproc
**resultfp
, int locked
)
3909 struct filedesc
*fdp
= p
->p_fd
;
3910 struct fileproc
*fp
;
3913 proc_fdlock_spin(p
);
3914 if (fd
< 0 || fdp
== NULL
|| fd
>= fdp
->fd_nfiles
||
3915 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3916 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3935 * Description: Swap the fileproc pointer for a given fd with a new
3936 * fileproc pointer in the per-process open file table of
3937 * the specified process. The fdlock must be held at entry.
3939 * Parameters: p Process containing the fd
3940 * fd The fd of interest
3941 * nfp Pointer to the newfp
3943 * Returns: 0 Success
3944 * EBADF Bad file descriptor
3946 * EKEEPLOOKING f_iocount changed while lock was dropped.
3949 fp_tryswap(proc_t p
, int fd
, struct fileproc
*nfp
)
3951 struct fileproc
*fp
;
3954 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
3956 if (0 != (error
= fp_lookup(p
, fd
, &fp
, 1)))
3959 * At this point, our caller (change_guardedfd_np) has
3960 * one f_iocount reference, and we just took another
3961 * one to begin the replacement.
3963 if (fp
->f_iocount
< 2) {
3964 panic("f_iocount too small %d", fp
->f_iocount
);
3965 } else if (2 == fp
->f_iocount
) {
3967 /* Copy the contents of *fp, preserving the "type" of *nfp */
3969 nfp
->f_flags
= (nfp
->f_flags
& FP_TYPEMASK
) |
3970 (fp
->f_flags
& ~FP_TYPEMASK
);
3971 nfp
->f_iocount
= fp
->f_iocount
;
3972 nfp
->f_fglob
= fp
->f_fglob
;
3973 nfp
->f_wset
= fp
->f_wset
;
3975 p
->p_fd
->fd_ofiles
[fd
] = nfp
;
3976 (void) fp_drop(p
, fd
, nfp
, 1);
3979 * Wait for all other active references to evaporate.
3981 p
->p_fpdrainwait
= 1;
3982 error
= msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
,
3983 PRIBIO
| PCATCH
, "tryswap fpdrain", NULL
);
3986 * Return an "internal" errno to trigger a full
3987 * reevaluation of the change-guard attempt.
3989 error
= EKEEPLOOKING
;
3990 printf("%s: lookup collision fd %d\n", __func__
, fd
);
3992 (void) fp_drop(p
, fd
, fp
, 1);
4001 * Description: Set the FP_WRITTEN flag on the fileproc and drop the I/O
4002 * reference previously taken by calling fp_lookup et. al.
4004 * Parameters: p Process in which the fd lives
4005 * fd fd associated with the fileproc
4006 * fp fileproc on which to set the
4007 * flag and drop the reference
4009 * Returns: 0 Success
4010 * fp_drop:EBADF Bad file descriptor
4012 * Locks: This function internally takes and drops the proc_fdlock for
4013 * the supplied process
4015 * Notes: The fileproc must correspond to the fd in the supplied proc
4018 fp_drop_written(proc_t p
, int fd
, struct fileproc
*fp
)
4022 proc_fdlock_spin(p
);
4024 fp
->f_flags
|= FP_WRITTEN
;
4026 error
= fp_drop(p
, fd
, fp
, 1);
4037 * Description: Set the FP_WAITEVENT flag on the fileproc and drop the I/O
4038 * reference previously taken by calling fp_lookup et. al.
4040 * Parameters: p Process in which the fd lives
4041 * fd fd associated with the fileproc
4042 * fp fileproc on which to set the
4043 * flag and drop the reference
4045 * Returns: 0 Success
4046 * fp_drop:EBADF Bad file descriptor
4048 * Locks: This function internally takes and drops the proc_fdlock for
4049 * the supplied process
4051 * Notes: The fileproc must correspond to the fd in the supplied proc
4054 fp_drop_event(proc_t p
, int fd
, struct fileproc
*fp
)
4058 proc_fdlock_spin(p
);
4060 fp
->f_flags
|= FP_WAITEVENT
;
4062 error
= fp_drop(p
, fd
, fp
, 1);
4073 * Description: Drop the I/O reference previously taken by calling fp_lookup
4076 * Parameters: p Process in which the fd lives
4077 * fd fd associated with the fileproc
4078 * fp fileproc on which to set the
4079 * flag and drop the reference
4080 * locked flag to internally take and
4081 * drop proc_fdlock if it is not
4082 * already held by the caller
4084 * Returns: 0 Success
4085 * EBADF Bad file descriptor
4087 * Locks: This function internally takes and drops the proc_fdlock for
4088 * the supplied process if 'locked' is non-zero, and assumes that
4089 * the caller already holds this lock if 'locked' is non-zero.
4091 * Notes: The fileproc must correspond to the fd in the supplied proc
4094 fp_drop(proc_t p
, int fd
, struct fileproc
*fp
, int locked
)
4096 struct filedesc
*fdp
= p
->p_fd
;
4100 proc_fdlock_spin(p
);
4101 if ((fp
== FILEPROC_NULL
) && (fd
< 0 || fd
>= fdp
->fd_nfiles
||
4102 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
4103 ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
4104 !(fdp
->fd_ofileflags
[fd
] & UF_CLOSING
)))) {
4111 if (fp
->f_iocount
== 0) {
4112 if (fp
->f_flags
& FP_SELCONFLICT
)
4113 fp
->f_flags
&= ~FP_SELCONFLICT
;
4115 if (p
->p_fpdrainwait
) {
4116 p
->p_fpdrainwait
= 0;
4123 wakeup(&p
->p_fpdrainwait
);
4132 * Description: Given an fd, look it up in the current process's per process
4133 * open file table, and return its internal vnode pointer.
4135 * Parameters: fd fd to obtain vnode from
4136 * vpp pointer to vnode return area
4138 * Returns: 0 Success
4139 * EINVAL The fd does not refer to a
4140 * vnode fileproc entry
4141 * fp_lookup:EBADF Bad file descriptor
4144 * *vpp (modified) Returned vnode pointer
4146 * Locks: This function internally takes and drops the proc_fdlock for
4147 * the current process
4149 * Notes: If successful, this function increments the f_iocount on the
4150 * fd's corresponding fileproc.
4152 * The fileproc referenced is not returned; because of this, care
4153 * must be taken to not drop the last reference (e.g. by closing
4154 * the file). This is inherently unsafe, since the reference may
4155 * not be recoverable from the vnode, if there is a subsequent
4156 * close that destroys the associate fileproc. The caller should
4157 * therefore retain their own reference on the fileproc so that
4158 * the f_iocount can be dropped subsequently. Failure to do this
4159 * can result in the returned pointer immediately becoming invalid
4160 * following the call.
4162 * Use of this function is discouraged.
4165 file_vnode(int fd
, struct vnode
**vpp
)
4167 proc_t p
= current_proc();
4168 struct fileproc
*fp
;
4171 proc_fdlock_spin(p
);
4172 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4176 if (fp
->f_type
!= DTYPE_VNODE
) {
4177 fp_drop(p
, fd
, fp
,1);
4182 *vpp
= (struct vnode
*)fp
->f_data
;
4190 * file_vnode_withvid
4192 * Description: Given an fd, look it up in the current process's per process
4193 * open file table, and return its internal vnode pointer.
4195 * Parameters: fd fd to obtain vnode from
4196 * vpp pointer to vnode return area
4197 * vidp pointer to vid of the returned vnode
4199 * Returns: 0 Success
4200 * EINVAL The fd does not refer to a
4201 * vnode fileproc entry
4202 * fp_lookup:EBADF Bad file descriptor
4205 * *vpp (modified) Returned vnode pointer
4207 * Locks: This function internally takes and drops the proc_fdlock for
4208 * the current process
4210 * Notes: If successful, this function increments the f_iocount on the
4211 * fd's corresponding fileproc.
4213 * The fileproc referenced is not returned; because of this, care
4214 * must be taken to not drop the last reference (e.g. by closing
4215 * the file). This is inherently unsafe, since the reference may
4216 * not be recoverable from the vnode, if there is a subsequent
4217 * close that destroys the associate fileproc. The caller should
4218 * therefore retain their own reference on the fileproc so that
4219 * the f_iocount can be dropped subsequently. Failure to do this
4220 * can result in the returned pointer immediately becoming invalid
4221 * following the call.
4223 * Use of this function is discouraged.
4226 file_vnode_withvid(int fd
, struct vnode
**vpp
, uint32_t * vidp
)
4228 proc_t p
= current_proc();
4229 struct fileproc
*fp
;
4233 proc_fdlock_spin(p
);
4234 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4238 if (fp
->f_type
!= DTYPE_VNODE
) {
4239 fp_drop(p
, fd
, fp
,1);
4243 vp
= (struct vnode
*)fp
->f_data
;
4247 if ((vidp
!= NULL
) && (vp
!= NULLVP
))
4248 *vidp
= (uint32_t)vp
->v_id
;
4259 * Description: Given an fd, look it up in the current process's per process
4260 * open file table, and return its internal socket pointer.
4262 * Parameters: fd fd to obtain vnode from
4263 * sp pointer to socket return area
4265 * Returns: 0 Success
4266 * ENOTSOCK Not a socket
4267 * fp_lookup:EBADF Bad file descriptor
4270 * *sp (modified) Returned socket pointer
4272 * Locks: This function internally takes and drops the proc_fdlock for
4273 * the current process
4275 * Notes: If successful, this function increments the f_iocount on the
4276 * fd's corresponding fileproc.
4278 * The fileproc referenced is not returned; because of this, care
4279 * must be taken to not drop the last reference (e.g. by closing
4280 * the file). This is inherently unsafe, since the reference may
4281 * not be recoverable from the socket, if there is a subsequent
4282 * close that destroys the associate fileproc. The caller should
4283 * therefore retain their own reference on the fileproc so that
4284 * the f_iocount can be dropped subsequently. Failure to do this
4285 * can result in the returned pointer immediately becoming invalid
4286 * following the call.
4288 * Use of this function is discouraged.
4291 file_socket(int fd
, struct socket
**sp
)
4293 proc_t p
= current_proc();
4294 struct fileproc
*fp
;
4297 proc_fdlock_spin(p
);
4298 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4302 if (fp
->f_type
!= DTYPE_SOCKET
) {
4303 fp_drop(p
, fd
, fp
,1);
4307 *sp
= (struct socket
*)fp
->f_data
;
4317 * Description: Given an fd, look it up in the current process's per process
4318 * open file table, and return its fileproc's flags field.
4320 * Parameters: fd fd whose flags are to be
4322 * flags pointer to flags data area
4324 * Returns: 0 Success
4325 * ENOTSOCK Not a socket
4326 * fp_lookup:EBADF Bad file descriptor
4329 * *flags (modified) Returned flags field
4331 * Locks: This function internally takes and drops the proc_fdlock for
4332 * the current process
4334 * Notes: This function will internally increment and decrement the
4335 * f_iocount of the fileproc as part of its operation.
4338 file_flags(int fd
, int *flags
)
4341 proc_t p
= current_proc();
4342 struct fileproc
*fp
;
4345 proc_fdlock_spin(p
);
4346 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4350 *flags
= (int)fp
->f_flag
;
4351 fp_drop(p
, fd
, fp
,1);
4361 * Description: Drop an iocount reference on an fd, and wake up any waiters
4362 * for draining (i.e. blocked in fileproc_drain() called during
4363 * the last attempt to close a file).
4365 * Parameters: fd fd on which an ioreference is
4368 * Returns: 0 Success
4369 * EBADF Bad file descriptor
4371 * Description: Given an fd, look it up in the current process's per process
4372 * open file table, and drop it's fileproc's f_iocount by one
4374 * Notes: This is intended as a corresponding operation to the functions
4375 * file_vnode() and file_socket() operations.
4377 * Technically, the close reference is supposed to be protected
4378 * by a fileproc_drain(), however, a drain will only block if
4379 * the fd refers to a character device, and that device has had
4380 * preparefileread() called on it. If it refers to something
4381 * other than a character device, then the drain will occur and
4382 * block each close attempt, rather than merely the last close.
4384 * Since it's possible for an fd that refers to a character
4385 * device to have an intermediate close followed by an open to
4386 * cause a different file to correspond to that descriptor,
4387 * unless there was a cautionary reference taken on the fileproc,
4388 * this is an inherently unsafe function. This happens in the
4389 * case where multiple fd's in a process refer to the same
4390 * character device (e.g. stdin/out/err pointing to a tty, etc.).
4392 * Use of this function is discouraged.
4397 struct fileproc
*fp
;
4398 proc_t p
= current_proc();
4401 proc_fdlock_spin(p
);
4402 if (fd
< 0 || fd
>= p
->p_fd
->fd_nfiles
||
4403 (fp
= p
->p_fd
->fd_ofiles
[fd
]) == NULL
||
4404 ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
4405 !(p
->p_fd
->fd_ofileflags
[fd
] & UF_CLOSING
))) {
4411 if (fp
->f_iocount
== 0) {
4412 if (fp
->f_flags
& FP_SELCONFLICT
)
4413 fp
->f_flags
&= ~FP_SELCONFLICT
;
4415 if (p
->p_fpdrainwait
) {
4416 p
->p_fpdrainwait
= 0;
4423 wakeup(&p
->p_fpdrainwait
);
4428 static int falloc_withalloc_locked(proc_t
, struct fileproc
**, int *,
4429 vfs_context_t
, struct fileproc
* (*)(void *), void *, int);
4434 * Description: Allocate an entry in the per process open file table and
4435 * return the corresponding fileproc and fd.
4437 * Parameters: p The process in whose open file
4438 * table the fd is to be allocated
4439 * resultfp Pointer to fileproc pointer
4441 * resultfd Pointer to fd return area
4444 * Returns: 0 Success
4445 * falloc:ENFILE Too many open files in system
4446 * falloc:EMFILE Too many open files in process
4447 * falloc:ENOMEM M_FILEPROC or M_FILEGLOB zone
4451 * *resultfd (modified) Returned fileproc pointer
4452 * *resultfd (modified) Returned fd
4454 * Locks: This function takes and drops the proc_fdlock; if this lock
4455 * is already held, use falloc_locked() instead.
4457 * Notes: This function takes separate process and context arguments
4458 * solely to support kern_exec.c; otherwise, it would take
4459 * neither, and expect falloc_locked() to use the
4460 * vfs_context_current() routine internally.
4463 falloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
)
4465 return (falloc_withalloc(p
, resultfp
, resultfd
, ctx
,
4466 fileproc_alloc_init
, NULL
));
4470 * Like falloc, but including the fileproc allocator and create-args
4473 falloc_withalloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4474 vfs_context_t ctx
, fp_allocfn_t fp_zalloc
, void *arg
)
4479 error
= falloc_withalloc_locked(p
,
4480 resultfp
, resultfd
, ctx
, fp_zalloc
, arg
, 1);
4487 * "uninitialized" ops -- ensure fg->fg_ops->fo_type always exists
4489 static const struct fileops uninitops
;
4494 * Create a new open file structure and allocate
4495 * a file descriptor for the process that refers to it.
4497 * Returns: 0 Success
4499 * Description: Allocate an entry in the per process open file table and
4500 * return the corresponding fileproc and fd.
4502 * Parameters: p The process in whose open file
4503 * table the fd is to be allocated
4504 * resultfp Pointer to fileproc pointer
4506 * resultfd Pointer to fd return area
4508 * locked Flag to indicate whether the
4509 * caller holds proc_fdlock
4511 * Returns: 0 Success
4512 * ENFILE Too many open files in system
4513 * fdalloc:EMFILE Too many open files in process
4514 * ENOMEM M_FILEPROC or M_FILEGLOB zone
4519 * *resultfd (modified) Returned fileproc pointer
4520 * *resultfd (modified) Returned fd
4522 * Locks: If the parameter 'locked' is zero, this function takes and
4523 * drops the proc_fdlock; if non-zero, the caller must hold the
4526 * Notes: If you intend to use a non-zero 'locked' parameter, use the
4527 * utility function falloc() instead.
4529 * This function takes separate process and context arguments
4530 * solely to support kern_exec.c; otherwise, it would take
4531 * neither, and use the vfs_context_current() routine internally.
4534 falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4535 vfs_context_t ctx
, int locked
)
4537 return (falloc_withalloc_locked(p
, resultfp
, resultfd
, ctx
,
4538 fileproc_alloc_init
, NULL
, locked
));
4542 falloc_withalloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4543 vfs_context_t ctx
, fp_allocfn_t fp_zalloc
, void *crarg
,
4546 struct fileproc
*fp
;
4547 struct fileglob
*fg
;
4552 if ( (error
= fdalloc(p
, 0, &nfd
)) ) {
4557 if (nfiles
>= maxfiles
) {
4564 error
= mac_file_check_create(proc_ucred(p
));
4573 * Allocate a new file descriptor.
4574 * If the process has file descriptor zero open, add to the list
4575 * of open files at that point, otherwise put it at the front of
4576 * the list of open files.
4580 fp
= (*fp_zalloc
)(crarg
);
4586 MALLOC_ZONE(fg
, struct fileglob
*, sizeof(struct fileglob
), M_FILEGLOB
, M_WAITOK
);
4593 bzero(fg
, sizeof(struct fileglob
));
4594 lck_mtx_init(&fg
->fg_lock
, file_lck_grp
, file_lck_attr
);
4598 fg
->fg_ops
= &uninitops
;
4601 mac_file_label_init(fg
);
4604 kauth_cred_ref(ctx
->vc_ucred
);
4608 fp
->f_cred
= ctx
->vc_ucred
;
4611 mac_file_label_associate(fp
->f_cred
, fg
);
4614 OSAddAtomic(1, &nfiles
);
4616 p
->p_fd
->fd_ofiles
[nfd
] = fp
;
4633 * Description: Free a file structure; drop the global open file count, and
4634 * drop the credential reference, if the fileglob has one, and
4635 * destroy the instance mutex before freeing
4637 * Parameters: fg Pointer to fileglob to be
4643 fg_free(struct fileglob
*fg
)
4645 OSAddAtomic(-1, &nfiles
);
4647 if (fg
->fg_vn_data
) {
4648 fg_vn_data_free(fg
->fg_vn_data
);
4649 fg
->fg_vn_data
= NULL
;
4652 if (IS_VALID_CRED(fg
->fg_cred
)) {
4653 kauth_cred_unref(&fg
->fg_cred
);
4655 lck_mtx_destroy(&fg
->fg_lock
, file_lck_grp
);
4658 mac_file_label_destroy(fg
);
4660 FREE_ZONE(fg
, sizeof *fg
, M_FILEGLOB
);
4668 * Description: Perform close-on-exec processing for all files in a process
4669 * that are either marked as close-on-exec, or which were in the
4670 * process of being opened at the time of the execve
4672 * Also handles the case (via posix_spawn()) where -all-
4673 * files except those marked with "inherit" as treated as
4676 * Parameters: p Pointer to process calling
4681 * Locks: This function internally takes and drops proc_fdlock()
4682 * But assumes tables don't grow/change while unlocked.
4686 fdexec(proc_t p
, short flags
, int self_exec
)
4688 struct filedesc
*fdp
= p
->p_fd
;
4690 boolean_t cloexec_default
= (flags
& POSIX_SPAWN_CLOEXEC_DEFAULT
) != 0;
4691 thread_t self
= current_thread();
4692 struct uthread
*ut
= get_bsdthread_info(self
);
4693 struct kqueue
*dealloc_kq
= NULL
;
4696 * If the current thread is bound as a workq/workloop
4697 * servicing thread, we need to unbind it first.
4699 if (ut
->uu_kqr_bound
&& self_exec
) {
4700 kqueue_threadreq_unbind(p
, ut
->uu_kqr_bound
);
4706 * Deallocate the knotes for this process
4707 * and mark the tables non-existent so
4708 * subsequent kqueue closes go faster.
4711 assert(fdp
->fd_knlistsize
== -1);
4712 assert(fdp
->fd_knhashmask
== 0);
4714 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
4716 struct fileproc
*fp
= fdp
->fd_ofiles
[i
];
4717 char *flagp
= &fdp
->fd_ofileflags
[i
];
4719 if (fp
&& cloexec_default
) {
4721 * Reverse the usual semantics of file descriptor
4722 * inheritance - all of them should be closed
4723 * except files marked explicitly as "inherit" and
4724 * not marked close-on-exec.
4726 if ((*flagp
& (UF_EXCLOSE
|UF_INHERIT
)) != UF_INHERIT
)
4727 *flagp
|= UF_EXCLOSE
;
4728 *flagp
&= ~UF_INHERIT
;
4732 ((*flagp
& (UF_RESERVED
|UF_EXCLOSE
)) == UF_EXCLOSE
)
4734 || (fp
&& mac_file_check_inherit(proc_ucred(p
), fp
->f_fglob
))
4737 procfdtbl_clearfd(p
, i
);
4738 if (i
== fdp
->fd_lastfile
&& i
> 0)
4740 if (i
< fdp
->fd_freefile
)
4741 fdp
->fd_freefile
= i
;
4744 * Wait for any third party viewers (e.g., lsof)
4745 * to release their references to this fileproc.
4747 while (fp
->f_iocount
> 0) {
4748 p
->p_fpdrainwait
= 1;
4749 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
,
4753 closef_locked(fp
, fp
->f_fglob
, p
);
4759 /* release the per-process workq kq */
4760 if (fdp
->fd_wqkqueue
) {
4761 dealloc_kq
= fdp
->fd_wqkqueue
;
4762 fdp
->fd_wqkqueue
= NULL
;
4767 /* Anything to free? */
4769 kqueue_dealloc(dealloc_kq
);
4776 * Description: Copy a filedesc structure. This is normally used as part of
4777 * forkproc() when forking a new process, to copy the per process
4778 * open file table over to the new process.
4780 * Parameters: p Process whose open file table
4781 * is to be copied (parent)
4782 * uth_cdir Per thread current working
4783 * cirectory, or NULL
4785 * Returns: NULL Copy failed
4786 * !NULL Pointer to new struct filedesc
4788 * Locks: This function internally takes and drops proc_fdlock()
4790 * Notes: Files are copied directly, ignoring the new resource limits
4791 * for the process that's being copied into. Since the descriptor
4792 * references are just additional references, this does not count
4793 * against the number of open files on the system.
4795 * The struct filedesc includes the current working directory,
4796 * and the current root directory, if the process is chroot'ed.
4798 * If the exec was called by a thread using a per thread current
4799 * working directory, we inherit the working directory from the
4800 * thread making the call, rather than from the process.
4802 * In the case of a failure to obtain a reference, for most cases,
4803 * the file entry will be silently dropped. There's an exception
4804 * for the case of a chroot dir, since a failure to to obtain a
4805 * reference there would constitute an "escape" from the chroot
4806 * environment, which must not be allowed. In that case, we will
4807 * deny the execve() operation, rather than allowing the escape.
4810 fdcopy(proc_t p
, vnode_t uth_cdir
)
4812 struct filedesc
*newfdp
, *fdp
= p
->p_fd
;
4814 struct fileproc
*ofp
, *fp
;
4817 MALLOC_ZONE(newfdp
, struct filedesc
*,
4818 sizeof(*newfdp
), M_FILEDESC
, M_WAITOK
);
4825 * the FD_CHROOT flag will be inherited via this copy
4827 (void) memcpy(newfdp
, fdp
, sizeof(*newfdp
));
4830 * If we are running with per-thread current working directories,
4831 * inherit the new current working directory from the current thread
4832 * instead, before we take our references.
4834 if (uth_cdir
!= NULLVP
)
4835 newfdp
->fd_cdir
= uth_cdir
;
4838 * For both fd_cdir and fd_rdir make sure we get
4839 * a valid reference... if we can't, than set
4840 * set the pointer(s) to NULL in the child... this
4841 * will keep us from using a non-referenced vp
4842 * and allows us to do the vnode_rele only on
4843 * a properly referenced vp
4845 if ( (v_dir
= newfdp
->fd_cdir
) ) {
4846 if (vnode_getwithref(v_dir
) == 0) {
4847 if ( (vnode_ref(v_dir
)) )
4848 newfdp
->fd_cdir
= NULL
;
4851 newfdp
->fd_cdir
= NULL
;
4853 if (newfdp
->fd_cdir
== NULL
&& fdp
->fd_cdir
) {
4855 * we couldn't get a new reference on
4856 * the current working directory being
4857 * inherited... we might as well drop
4858 * our reference from the parent also
4859 * since the vnode has gone DEAD making
4860 * it useless... by dropping it we'll
4861 * be that much closer to recycling it
4863 vnode_rele(fdp
->fd_cdir
);
4864 fdp
->fd_cdir
= NULL
;
4867 if ( (v_dir
= newfdp
->fd_rdir
) ) {
4868 if (vnode_getwithref(v_dir
) == 0) {
4869 if ( (vnode_ref(v_dir
)) )
4870 newfdp
->fd_rdir
= NULL
;
4873 newfdp
->fd_rdir
= NULL
;
4876 /* Coming from a chroot environment and unable to get a reference... */
4877 if (newfdp
->fd_rdir
== NULL
&& fdp
->fd_rdir
) {
4879 * We couldn't get a new reference on
4880 * the chroot directory being
4881 * inherited... this is fatal, since
4882 * otherwise it would constitute an
4883 * escape from a chroot environment by
4886 if (newfdp
->fd_cdir
)
4887 vnode_rele(newfdp
->fd_cdir
);
4888 FREE_ZONE(newfdp
, sizeof *newfdp
, M_FILEDESC
);
4893 * If the number of open files fits in the internal arrays
4894 * of the open file structure, use them, otherwise allocate
4895 * additional memory for the number of descriptors currently
4898 if (newfdp
->fd_lastfile
< NDFILE
)
4902 * Compute the smallest multiple of NDEXTENT needed
4903 * for the file descriptors currently in use,
4904 * allowing the table to shrink.
4906 i
= newfdp
->fd_nfiles
;
4907 while (i
> 1 + 2 * NDEXTENT
&& i
> 1 + newfdp
->fd_lastfile
* 2)
4912 MALLOC_ZONE(newfdp
->fd_ofiles
, struct fileproc
**,
4913 i
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
4914 if (newfdp
->fd_ofiles
== NULL
) {
4915 if (newfdp
->fd_cdir
)
4916 vnode_rele(newfdp
->fd_cdir
);
4917 if (newfdp
->fd_rdir
)
4918 vnode_rele(newfdp
->fd_rdir
);
4920 FREE_ZONE(newfdp
, sizeof(*newfdp
), M_FILEDESC
);
4923 (void) memset(newfdp
->fd_ofiles
, 0, i
* OFILESIZE
);
4926 newfdp
->fd_ofileflags
= (char *) &newfdp
->fd_ofiles
[i
];
4927 newfdp
->fd_nfiles
= i
;
4929 if (fdp
->fd_nfiles
> 0) {
4930 struct fileproc
**fpp
;
4933 (void) memcpy(newfdp
->fd_ofiles
, fdp
->fd_ofiles
,
4934 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofiles
));
4935 (void) memcpy(newfdp
->fd_ofileflags
, fdp
->fd_ofileflags
,
4936 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofileflags
));
4939 * kq descriptors cannot be copied.
4941 if (newfdp
->fd_knlistsize
!= -1) {
4942 fpp
= &newfdp
->fd_ofiles
[newfdp
->fd_lastfile
];
4943 flags
= &newfdp
->fd_ofileflags
[newfdp
->fd_lastfile
];
4944 for (i
= newfdp
->fd_lastfile
;
4945 i
>= 0; i
--, fpp
--, flags
--) {
4946 if (*flags
& UF_RESERVED
)
4947 continue; /* (removed below) */
4948 if (*fpp
!= NULL
&& (*fpp
)->f_type
== DTYPE_KQUEUE
) {
4951 if (i
< newfdp
->fd_freefile
)
4952 newfdp
->fd_freefile
= i
;
4954 if (*fpp
== NULL
&& i
== newfdp
->fd_lastfile
&& i
> 0)
4955 newfdp
->fd_lastfile
--;
4958 fpp
= newfdp
->fd_ofiles
;
4959 flags
= newfdp
->fd_ofileflags
;
4961 for (i
= newfdp
->fd_lastfile
+ 1; --i
>= 0; fpp
++, flags
++)
4962 if ((ofp
= *fpp
) != NULL
&&
4963 0 == (ofp
->f_fglob
->fg_lflags
& FG_CONFINED
) &&
4964 0 == (*flags
& (UF_FORKCLOSE
|UF_RESERVED
))) {
4966 if (FILEPROC_TYPE(ofp
) != FTYPE_SIMPLE
)
4967 panic("complex fileproc");
4969 fp
= fileproc_alloc_init(NULL
);
4972 * XXX no room to copy, unable to
4973 * XXX safely unwind state at present
4978 (ofp
->f_flags
& ~FP_TYPEMASK
);
4979 fp
->f_fglob
= ofp
->f_fglob
;
4984 if (i
< newfdp
->fd_freefile
)
4985 newfdp
->fd_freefile
= i
;
4994 * Initialize knote and kqueue tracking structs
4996 newfdp
->fd_knlist
= NULL
;
4997 newfdp
->fd_knlistsize
= -1;
4998 newfdp
->fd_knhash
= NULL
;
4999 newfdp
->fd_knhashmask
= 0;
5000 newfdp
->fd_kqhash
= NULL
;
5001 newfdp
->fd_kqhashmask
= 0;
5002 newfdp
->fd_wqkqueue
= NULL
;
5003 lck_mtx_init(&newfdp
->fd_kqhashlock
, proc_kqhashlock_grp
, proc_lck_attr
);
5004 lck_mtx_init(&newfdp
->fd_knhashlock
, proc_knhashlock_grp
, proc_lck_attr
);
5013 * Description: Release a filedesc (per process open file table) structure;
5014 * this is done on process exit(), or from forkproc_free() if
5015 * the fork fails for some reason subsequent to a successful
5018 * Parameters: p Pointer to process going away
5022 * Locks: This function internally takes and drops proc_fdlock()
5027 struct filedesc
*fdp
;
5028 struct fileproc
*fp
;
5029 struct kqueue
*dealloc_kq
= NULL
;
5034 if (p
== kernproc
|| NULL
== (fdp
= p
->p_fd
)) {
5039 extern struct filedesc filedesc0
;
5041 if (&filedesc0
== fdp
)
5045 * deallocate all the knotes up front and claim empty
5046 * tables to make any subsequent kqueue closes faster.
5049 assert(fdp
->fd_knlistsize
== -1);
5050 assert(fdp
->fd_knhashmask
== 0);
5053 * dealloc all workloops that have outstanding retains
5054 * when created with scheduling parameters.
5056 kqworkloops_dealloc(p
);
5058 /* close file descriptors */
5059 if (fdp
->fd_nfiles
> 0 && fdp
->fd_ofiles
) {
5060 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
5061 if ((fp
= fdp
->fd_ofiles
[i
]) != NULL
) {
5063 if (fdp
->fd_ofileflags
[i
] & UF_RESERVED
)
5064 panic("fdfree: found fp with UF_RESERVED");
5066 procfdtbl_reservefd(p
, i
);
5068 if (fp
->f_flags
& FP_WAITEVENT
)
5069 (void)waitevent_close(p
, fp
);
5070 (void) closef_locked(fp
, fp
->f_fglob
, p
);
5074 FREE_ZONE(fdp
->fd_ofiles
, fdp
->fd_nfiles
* OFILESIZE
, M_OFILETABL
);
5075 fdp
->fd_ofiles
= NULL
;
5079 if (fdp
->fd_wqkqueue
) {
5080 dealloc_kq
= fdp
->fd_wqkqueue
;
5081 fdp
->fd_wqkqueue
= NULL
;
5087 kqueue_dealloc(dealloc_kq
);
5090 vnode_rele(fdp
->fd_cdir
);
5092 vnode_rele(fdp
->fd_rdir
);
5094 proc_fdlock_spin(p
);
5098 if (fdp
->fd_kqhash
) {
5099 for (uint32_t j
= 0; j
<= fdp
->fd_kqhashmask
; j
++)
5100 assert(SLIST_EMPTY(&fdp
->fd_kqhash
[j
]));
5101 FREE(fdp
->fd_kqhash
, M_KQUEUE
);
5104 lck_mtx_destroy(&fdp
->fd_kqhashlock
, proc_kqhashlock_grp
);
5105 lck_mtx_destroy(&fdp
->fd_knhashlock
, proc_knhashlock_grp
);
5107 FREE_ZONE(fdp
, sizeof(*fdp
), M_FILEDESC
);
5113 * Description: Internal form of closef; called with proc_fdlock held
5115 * Parameters: fp Pointer to fileproc for fd
5116 * fg Pointer to fileglob for fd
5117 * p Pointer to proc structure
5119 * Returns: 0 Success
5120 * closef_finish:??? Anything returnable by a per-fileops
5123 * Note: Decrements reference count on file structure; if this was the
5124 * last reference, then closef_finish() is called
5126 * p and fp are allowed to be NULL when closing a file that was
5127 * being passed in a message (but only if we are called when this
5128 * is NOT the last reference).
5131 closef_locked(struct fileproc
*fp
, struct fileglob
*fg
, proc_t p
)
5135 struct vfs_context context
;
5142 /* Set up context with cred stashed in fg */
5143 if (p
== current_proc())
5144 context
.vc_thread
= current_thread();
5146 context
.vc_thread
= NULL
;
5147 context
.vc_ucred
= fg
->fg_cred
;
5150 * POSIX record locking dictates that any close releases ALL
5151 * locks owned by this process. This is handled by setting
5152 * a flag in the unlock to free ONLY locks obeying POSIX
5153 * semantics, and not to free BSD-style file locks.
5154 * If the descriptor was in a message, POSIX-style locks
5155 * aren't passed with the descriptor.
5157 if (p
&& (p
->p_ladvflag
& P_LADVLOCK
) &&
5158 DTYPE_VNODE
== FILEGLOB_DTYPE(fg
)) {
5161 lf
.l_whence
= SEEK_SET
;
5164 lf
.l_type
= F_UNLCK
;
5165 vp
= (struct vnode
*)fg
->fg_data
;
5167 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
5168 (void) VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &lf
, F_POSIX
, &context
, NULL
);
5169 (void)vnode_put(vp
);
5173 lck_mtx_lock_spin(&fg
->fg_lock
);
5176 if (fg
->fg_count
> 0) {
5177 lck_mtx_unlock(&fg
->fg_lock
);
5181 if (fg
->fg_count
!= 0)
5182 panic("fg %p: being freed with bad fg_count (%d)", fg
, fg
->fg_count
);
5185 if (fp
&& (fp
->f_flags
& FP_WRITTEN
))
5186 fg
->fg_flag
|= FWASWRITTEN
;
5188 fg
->fg_lflags
|= FG_TERM
;
5189 lck_mtx_unlock(&fg
->fg_lock
);
5194 /* Since we ensure that fg->fg_ops is always initialized,
5195 * it is safe to invoke fo_close on the fg */
5196 error
= fo_close(fg
, &context
);
5210 * Description: Drain out pending I/O operations
5212 * Parameters: p Process closing this file
5213 * fp fileproc struct for the open
5214 * instance on the file
5218 * Locks: Assumes the caller holds the proc_fdlock
5220 * Notes: For character devices, this occurs on the last close of the
5221 * device; for all other file descriptors, this occurs on each
5222 * close to prevent fd's from being closed out from under
5223 * operations currently in progress and blocked
5225 * See Also: file_vnode(), file_socket(), file_drop(), and the cautions
5226 * regarding their use and interaction with this function.
5229 fileproc_drain(proc_t p
, struct fileproc
* fp
)
5231 struct vfs_context context
;
5233 context
.vc_thread
= proc_thread(p
); /* XXX */
5234 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
5236 fp
->f_iocount
-- ; /* (the one the close holds) */
5238 while (fp
->f_iocount
) {
5240 lck_mtx_convert_spin(&p
->p_fdmlock
);
5242 if (fp
->f_fglob
->fg_ops
->fo_drain
) {
5243 (*fp
->f_fglob
->fg_ops
->fo_drain
)(fp
, &context
);
5245 if ((fp
->f_flags
& FP_INSELECT
) == FP_INSELECT
) {
5246 if (waitq_wakeup64_all((struct waitq
*)fp
->f_wset
, NO_EVENT64
,
5247 THREAD_INTERRUPTED
, WAITQ_ALL_PRIORITIES
) == KERN_INVALID_ARGUMENT
)
5248 panic("bad wait queue for waitq_wakeup64_all %p (fp:%p)", fp
->f_wset
, fp
);
5250 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
) {
5251 if (waitq_wakeup64_all(&select_conflict_queue
, NO_EVENT64
,
5252 THREAD_INTERRUPTED
, WAITQ_ALL_PRIORITIES
) == KERN_INVALID_ARGUMENT
)
5253 panic("bad select_conflict_queue");
5255 p
->p_fpdrainwait
= 1;
5257 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
, "fpdrain", NULL
);
5261 if ((fp
->f_flags
& FP_INSELECT
) != 0)
5262 panic("FP_INSELECT set on drained fp");
5264 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
)
5265 fp
->f_flags
&= ~FP_SELCONFLICT
;
5272 * Description: Release the fd and free the fileproc associated with the fd
5273 * in the per process open file table of the specified process;
5274 * these values must correspond.
5276 * Parameters: p Process containing fd
5277 * fd fd to be released
5278 * fp fileproc to be freed
5280 * Returns: 0 Success
5282 * Notes: XXX function should be void - no one interprets the returns
5286 fp_free(proc_t p
, int fd
, struct fileproc
* fp
)
5288 proc_fdlock_spin(p
);
5292 fg_free(fp
->f_fglob
);
5301 * Description: Apply an advisory lock on a file descriptor.
5303 * Parameters: p Process making request
5304 * uap->fd fd on which the lock is to be
5306 * uap->how (Un)Lock bits, including type
5307 * retval Pointer to the call return area
5309 * Returns: 0 Success
5310 * fp_getfvp:EBADF Bad file descriptor
5311 * fp_getfvp:ENOTSUP fd does not refer to a vnode
5312 * vnode_getwithref:???
5316 * *retval (modified) Size of dtable
5318 * Notes: Just attempt to get a record lock of the requested type on
5319 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
5322 flock(proc_t p
, struct flock_args
*uap
, __unused
int32_t *retval
)
5326 struct fileproc
*fp
;
5329 vfs_context_t ctx
= vfs_context_current();
5332 AUDIT_ARG(fd
, uap
->fd
);
5333 if ( (error
= fp_getfvp(p
, fd
, &fp
, &vp
)) ) {
5336 if ( (error
= vnode_getwithref(vp
)) ) {
5339 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
5341 lf
.l_whence
= SEEK_SET
;
5344 if (how
& LOCK_UN
) {
5345 lf
.l_type
= F_UNLCK
;
5346 fp
->f_flag
&= ~FHASLOCK
;
5347 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_UNLCK
, &lf
, F_FLOCK
, ctx
, NULL
);
5351 lf
.l_type
= F_WRLCK
;
5352 else if (how
& LOCK_SH
)
5353 lf
.l_type
= F_RDLCK
;
5359 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
, F_SETLK
, &lf
);
5363 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
,
5364 (how
& LOCK_NB
? F_FLOCK
: F_FLOCK
| F_WAIT
),
5367 fp
->f_flag
|= FHASLOCK
;
5369 (void)vnode_put(vp
);
5371 fp_drop(p
, fd
, fp
, 0);
5379 * Description: Obtain a Mach send right for a given file descriptor.
5381 * Parameters: p Process calling fileport
5382 * uap->fd The fd to reference
5383 * uap->portnamep User address at which to place port name.
5385 * Returns: 0 Success.
5386 * EBADF Bad file descriptor.
5387 * EINVAL File descriptor had type that cannot be sent, misc. other errors.
5388 * EFAULT Address at which to store port name is not valid.
5389 * EAGAIN Resource shortage.
5392 * On success, name of send right is stored at user-specified address.
5395 fileport_makeport(proc_t p
, struct fileport_makeport_args
*uap
,
5396 __unused
int *retval
)
5400 user_addr_t user_portaddr
= uap
->portnamep
;
5401 struct fileproc
*fp
= FILEPROC_NULL
;
5402 struct fileglob
*fg
= NULL
;
5403 ipc_port_t fileport
;
5404 mach_port_name_t name
= MACH_PORT_NULL
;
5407 err
= fp_lookup(p
, fd
, &fp
, 1);
5412 if (!file_issendable(p
, fp
)) {
5417 if (FP_ISGUARDED(fp
, GUARD_FILEPORT
)) {
5418 err
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_FILEPORT
);
5422 /* Dropped when port is deallocated */
5428 /* Allocate and initialize a port */
5429 fileport
= fileport_alloc(fg
);
5430 if (fileport
== IPC_PORT_NULL
) {
5436 /* Add an entry. Deallocates port on failure. */
5437 name
= ipc_port_copyout_send(fileport
, get_task_ipcspace(p
->task
));
5438 if (!MACH_PORT_VALID(name
)) {
5443 err
= copyout(&name
, user_portaddr
, sizeof(mach_port_name_t
));
5448 /* Tag the fileglob for debugging purposes */
5449 lck_mtx_lock_spin(&fg
->fg_lock
);
5450 fg
->fg_lflags
|= FG_PORTMADE
;
5451 lck_mtx_unlock(&fg
->fg_lock
);
5453 fp_drop(p
, fd
, fp
, 0);
5460 if (MACH_PORT_VALID(name
)) {
5461 /* Don't care if another thread races us to deallocate the entry */
5462 (void) mach_port_deallocate(get_task_ipcspace(p
->task
), name
);
5465 if (fp
!= FILEPROC_NULL
) {
5466 fp_drop(p
, fd
, fp
, 0);
5473 fileport_releasefg(struct fileglob
*fg
)
5475 (void)closef_locked(NULL
, fg
, PROC_NULL
);
5484 * Description: Obtain the file descriptor for a given Mach send right.
5486 * Parameters: p Process calling fileport
5487 * uap->port Name of send right to file port.
5489 * Returns: 0 Success
5490 * EINVAL Invalid Mach port name, or port is not for a file.
5492 * fdalloc:ENOMEM Unable to allocate fileproc or extend file table.
5495 * *retval (modified) The new descriptor
5498 fileport_makefd(proc_t p
, struct fileport_makefd_args
*uap
, int32_t *retval
)
5500 struct fileglob
*fg
;
5501 struct fileproc
*fp
= FILEPROC_NULL
;
5502 ipc_port_t port
= IPC_PORT_NULL
;
5503 mach_port_name_t send
= uap
->port
;
5508 res
= ipc_object_copyin(get_task_ipcspace(p
->task
),
5509 send
, MACH_MSG_TYPE_COPY_SEND
, &port
);
5511 if (res
!= KERN_SUCCESS
) {
5516 fg
= fileport_port_to_fileglob(port
);
5522 fp
= fileproc_alloc_init(NULL
);
5523 if (fp
== FILEPROC_NULL
) {
5532 err
= fdalloc(p
, 0, &fd
);
5538 *fdflags(p
, fd
) |= UF_EXCLOSE
;
5540 procfdtbl_releasefd(p
, fd
, fp
);
5546 if ((fp
!= NULL
) && (0 != err
)) {
5550 if (IPC_PORT_NULL
!= port
) {
5551 ipc_port_release_send(port
);
5561 * Description: Duplicate the specified descriptor to a free descriptor;
5562 * this is the second half of fdopen(), above.
5564 * Parameters: fdp filedesc pointer to fill in
5566 * dfd fd to dup from
5567 * mode mode to set on new fd
5568 * error command code
5570 * Returns: 0 Success
5571 * EBADF Source fd is bad
5572 * EACCES Requested mode not allowed
5573 * !0 'error', if not ENODEV or
5576 * Notes: XXX This is not thread safe; see fdopen() above
5579 dupfdopen(struct filedesc
*fdp
, int indx
, int dfd
, int flags
, int error
)
5581 struct fileproc
*wfp
;
5582 struct fileproc
*fp
;
5586 proc_t p
= current_proc();
5589 * If the to-be-dup'd fd number is greater than the allowed number
5590 * of file descriptors, or the fd to be dup'd has already been
5591 * closed, reject. Note, check for new == old is necessary as
5592 * falloc could allocate an already closed to-be-dup'd descriptor
5593 * as the new descriptor.
5597 fp
= fdp
->fd_ofiles
[indx
];
5598 if (dfd
< 0 || dfd
>= fdp
->fd_nfiles
||
5599 (wfp
= fdp
->fd_ofiles
[dfd
]) == NULL
|| wfp
== fp
||
5600 (fdp
->fd_ofileflags
[dfd
] & UF_RESERVED
)) {
5606 myerror
= mac_file_check_dup(proc_ucred(p
), wfp
->f_fglob
, dfd
);
5613 * There are two cases of interest here.
5615 * For ENODEV simply dup (dfd) to file descriptor
5616 * (indx) and return.
5618 * For ENXIO steal away the file structure from (dfd) and
5619 * store it in (indx). (dfd) is effectively closed by
5622 * Any other error code is just returned.
5626 if (FP_ISGUARDED(wfp
, GUARD_DUP
)) {
5632 * Check that the mode the file is being opened for is a
5633 * subset of the mode of the existing descriptor.
5635 if (((flags
& (FREAD
|FWRITE
)) | wfp
->f_flag
) != wfp
->f_flag
) {
5639 if (indx
> fdp
->fd_lastfile
)
5640 fdp
->fd_lastfile
= indx
;
5644 fg_free(fp
->f_fglob
);
5645 fp
->f_fglob
= wfp
->f_fglob
;
5647 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
] |
5648 (flags
& O_CLOEXEC
) ? UF_EXCLOSE
: 0;
5664 * Description: Add a reference to a fileglob by fileproc
5666 * Parameters: fp fileproc containing fileglob
5671 * Notes: XXX Should use OSAddAtomic?
5674 fg_ref(struct fileproc
* fp
)
5676 struct fileglob
*fg
;
5680 lck_mtx_lock_spin(&fg
->fg_lock
);
5683 if ((fp
->f_flags
& ~((unsigned int)FP_VALID_FLAGS
)) != 0)
5684 panic("fg_ref: invalid bits on fp %p", fp
);
5686 if (fg
->fg_count
== 0)
5687 panic("fg_ref: adding fgcount to zeroed fg: fp %p fg %p",
5691 lck_mtx_unlock(&fg
->fg_lock
);
5698 * Description: Remove a reference to a fileglob by fileproc
5700 * Parameters: fp fileproc containing fileglob
5705 * Notes: XXX Should use OSAddAtomic?
5708 fg_drop(struct fileproc
* fp
)
5710 struct fileglob
*fg
;
5713 lck_mtx_lock_spin(&fg
->fg_lock
);
5715 lck_mtx_unlock(&fg
->fg_lock
);
5720 * fg_insertuipc_mark
5722 * Description: Mark fileglob for insertion onto message queue if needed
5723 * Also takes fileglob reference
5725 * Parameters: fg Fileglob pointer to insert
5727 * Returns: true, if the fileglob needs to be inserted onto msg queue
5729 * Locks: Takes and drops fg_lock, potentially many times
5732 fg_insertuipc_mark(struct fileglob
* fg
)
5734 boolean_t insert
= FALSE
;
5736 lck_mtx_lock_spin(&fg
->fg_lock
);
5737 while (fg
->fg_lflags
& FG_RMMSGQ
) {
5738 lck_mtx_convert_spin(&fg
->fg_lock
);
5740 fg
->fg_lflags
|= FG_WRMMSGQ
;
5741 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_insertuipc", NULL
);
5746 if (fg
->fg_msgcount
== 1) {
5747 fg
->fg_lflags
|= FG_INSMSGQ
;
5750 lck_mtx_unlock(&fg
->fg_lock
);
5757 * Description: Insert marked fileglob onto message queue
5759 * Parameters: fg Fileglob pointer to insert
5763 * Locks: Takes and drops fg_lock & uipc_lock
5764 * DO NOT call this function with proc_fdlock held as unp_gc()
5765 * can potentially try to acquire proc_fdlock, which can result
5766 * in a deadlock if this function is in unp_gc_wait().
5769 fg_insertuipc(struct fileglob
* fg
)
5771 if (fg
->fg_lflags
& FG_INSMSGQ
) {
5772 lck_mtx_lock_spin(uipc_lock
);
5774 LIST_INSERT_HEAD(&fmsghead
, fg
, f_msglist
);
5775 lck_mtx_unlock(uipc_lock
);
5776 lck_mtx_lock(&fg
->fg_lock
);
5777 fg
->fg_lflags
&= ~FG_INSMSGQ
;
5778 if (fg
->fg_lflags
& FG_WINSMSGQ
) {
5779 fg
->fg_lflags
&= ~FG_WINSMSGQ
;
5780 wakeup(&fg
->fg_lflags
);
5782 lck_mtx_unlock(&fg
->fg_lock
);
5787 * fg_removeuipc_mark
5789 * Description: Mark the fileglob for removal from message queue if needed
5790 * Also releases fileglob message queue reference
5792 * Parameters: fg Fileglob pointer to remove
5794 * Returns: true, if the fileglob needs to be removed from msg queue
5796 * Locks: Takes and drops fg_lock, potentially many times
5799 fg_removeuipc_mark(struct fileglob
* fg
)
5801 boolean_t remove
= FALSE
;
5803 lck_mtx_lock_spin(&fg
->fg_lock
);
5804 while (fg
->fg_lflags
& FG_INSMSGQ
) {
5805 lck_mtx_convert_spin(&fg
->fg_lock
);
5807 fg
->fg_lflags
|= FG_WINSMSGQ
;
5808 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_removeuipc", NULL
);
5811 if (fg
->fg_msgcount
== 0) {
5812 fg
->fg_lflags
|= FG_RMMSGQ
;
5815 lck_mtx_unlock(&fg
->fg_lock
);
5822 * Description: Remove marked fileglob from message queue
5824 * Parameters: fg Fileglob pointer to remove
5828 * Locks: Takes and drops fg_lock & uipc_lock
5829 * DO NOT call this function with proc_fdlock held as unp_gc()
5830 * can potentially try to acquire proc_fdlock, which can result
5831 * in a deadlock if this function is in unp_gc_wait().
5834 fg_removeuipc(struct fileglob
* fg
)
5836 if (fg
->fg_lflags
& FG_RMMSGQ
) {
5837 lck_mtx_lock_spin(uipc_lock
);
5839 LIST_REMOVE(fg
, f_msglist
);
5840 lck_mtx_unlock(uipc_lock
);
5841 lck_mtx_lock(&fg
->fg_lock
);
5842 fg
->fg_lflags
&= ~FG_RMMSGQ
;
5843 if (fg
->fg_lflags
& FG_WRMMSGQ
) {
5844 fg
->fg_lflags
&= ~FG_WRMMSGQ
;
5845 wakeup(&fg
->fg_lflags
);
5847 lck_mtx_unlock(&fg
->fg_lock
);
5850 #endif /* SOCKETS */
5855 * Description: Generic fileops read indirected through the fileops pointer
5856 * in the fileproc structure
5858 * Parameters: fp fileproc structure pointer
5859 * uio user I/O structure pointer
5861 * ctx VFS context for operation
5863 * Returns: 0 Success
5864 * !0 Errno from read
5867 fo_read(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
5869 return ((*fp
->f_ops
->fo_read
)(fp
, uio
, flags
, ctx
));
5876 * Description: Generic fileops write indirected through the fileops pointer
5877 * in the fileproc structure
5879 * Parameters: fp fileproc structure pointer
5880 * uio user I/O structure pointer
5882 * ctx VFS context for operation
5884 * Returns: 0 Success
5885 * !0 Errno from write
5888 fo_write(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
5890 return((*fp
->f_ops
->fo_write
)(fp
, uio
, flags
, ctx
));
5897 * Description: Generic fileops ioctl indirected through the fileops pointer
5898 * in the fileproc structure
5900 * Parameters: fp fileproc structure pointer
5902 * data pointer to internalized copy
5903 * of user space ioctl command
5904 * parameter data in kernel space
5905 * ctx VFS context for operation
5907 * Returns: 0 Success
5908 * !0 Errno from ioctl
5910 * Locks: The caller is assumed to have held the proc_fdlock; this
5911 * function releases and reacquires this lock. If the caller
5912 * accesses data protected by this lock prior to calling this
5913 * function, it will need to revalidate/reacquire any cached
5914 * protected data obtained prior to the call.
5917 fo_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
, vfs_context_t ctx
)
5921 proc_fdunlock(vfs_context_proc(ctx
));
5922 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, com
, data
, ctx
);
5923 proc_fdlock(vfs_context_proc(ctx
));
5931 * Description: Generic fileops select indirected through the fileops pointer
5932 * in the fileproc structure
5934 * Parameters: fp fileproc structure pointer
5935 * which select which
5936 * wql pointer to wait queue list
5937 * ctx VFS context for operation
5939 * Returns: 0 Success
5940 * !0 Errno from select
5943 fo_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
5945 return((*fp
->f_ops
->fo_select
)(fp
, which
, wql
, ctx
));
5952 * Description: Generic fileops close indirected through the fileops pointer
5953 * in the fileproc structure
5955 * Parameters: fp fileproc structure pointer for
5957 * ctx VFS context for operation
5959 * Returns: 0 Success
5960 * !0 Errno from close
5963 fo_close(struct fileglob
*fg
, vfs_context_t ctx
)
5965 return((*fg
->fg_ops
->fo_close
)(fg
, ctx
));
5972 * Description: Generic fileops kqueue filter indirected through the fileops
5973 * pointer in the fileproc structure
5975 * Parameters: fp fileproc structure pointer
5976 * kn pointer to knote to filter on
5977 * ctx VFS context for operation
5979 * Returns: (kn->kn_flags & EV_ERROR) error in kn->kn_data
5980 * 0 Filter is not active
5981 * !0 Filter is active
5984 fo_kqfilter(struct fileproc
*fp
, struct knote
*kn
,
5985 struct kevent_internal_s
*kev
, vfs_context_t ctx
)
5987 return ((*fp
->f_ops
->fo_kqfilter
)(fp
, kn
, kev
, ctx
));
5991 * The ability to send a file descriptor to another
5992 * process is opt-in by file type.
5995 file_issendable(proc_t p
, struct fileproc
*fp
)
5997 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
5999 switch (fp
->f_type
) {
6004 case DTYPE_NETPOLICY
:
6005 return (0 == (fp
->f_fglob
->fg_lflags
& FG_CONFINED
));
6007 /* DTYPE_KQUEUE, DTYPE_FSEVENTS, DTYPE_PSXSEM */
6014 fileproc_alloc_init(__unused
void *arg
)
6016 struct fileproc
*fp
;
6018 MALLOC_ZONE(fp
, struct fileproc
*, sizeof (*fp
), M_FILEPROC
, M_WAITOK
);
6020 bzero(fp
, sizeof (*fp
));
6026 fileproc_free(struct fileproc
*fp
)
6028 switch (FILEPROC_TYPE(fp
)) {
6030 FREE_ZONE(fp
, sizeof (*fp
), M_FILEPROC
);
6033 guarded_fileproc_free(fp
);
6036 panic("%s: corrupt fp %p flags %x", __func__
, fp
, fp
->f_flags
);