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;
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
);
1144 case F_OFD_GETLKPID
:
1146 if (fp
->f_type
!= DTYPE_VNODE
) {
1150 vp
= (struct vnode
*)fp
->f_data
;
1152 offset
= fp
->f_offset
;
1155 /* Copy in the lock structure */
1156 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
1160 /* Check starting byte and ending byte for EOVERFLOW in SEEK_CUR */
1161 /* and ending byte for EOVERFLOW in SEEK_SET */
1162 error
= check_file_seek_range(&fl
, offset
);
1167 if ((fl
.l_whence
== SEEK_SET
) && (fl
.l_start
< 0)) {
1172 switch (fl
.l_type
) {
1182 switch (fl
.l_whence
) {
1192 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1193 if (fl
.l_whence
== SEEK_CUR
)
1194 fl
.l_start
+= offset
;
1197 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
1203 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1204 F_GETLK
, &fl
, F_OFD_LOCK
, &context
, NULL
);
1206 case F_OFD_GETLKPID
:
1207 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1208 F_GETLKPID
, &fl
, F_OFD_LOCK
, &context
, NULL
);
1211 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1212 uap
->cmd
, &fl
, F_POSIX
, &context
, NULL
);
1216 (void)vnode_put(vp
);
1219 error
= copyout((caddr_t
)&fl
, argp
, sizeof(fl
));
1223 case F_PREALLOCATE
: {
1224 fstore_t alloc_struct
; /* structure for allocate command */
1225 u_int32_t alloc_flags
= 0;
1227 if (fp
->f_type
!= DTYPE_VNODE
) {
1232 vp
= (struct vnode
*)fp
->f_data
;
1235 /* make sure that we have write permission */
1236 if ((fp
->f_flag
& FWRITE
) == 0) {
1241 error
= copyin(argp
, (caddr_t
)&alloc_struct
, sizeof(alloc_struct
));
1245 /* now set the space allocated to 0 */
1246 alloc_struct
.fst_bytesalloc
= 0;
1249 * Do some simple parameter checking
1252 /* set up the flags */
1254 alloc_flags
|= PREALLOCATE
;
1256 if (alloc_struct
.fst_flags
& F_ALLOCATECONTIG
)
1257 alloc_flags
|= ALLOCATECONTIG
;
1259 if (alloc_struct
.fst_flags
& F_ALLOCATEALL
)
1260 alloc_flags
|= ALLOCATEALL
;
1263 * Do any position mode specific stuff. The only
1264 * position mode supported now is PEOFPOSMODE
1267 switch (alloc_struct
.fst_posmode
) {
1270 if (alloc_struct
.fst_offset
!= 0) {
1275 alloc_flags
|= ALLOCATEFROMPEOF
;
1279 if (alloc_struct
.fst_offset
<= 0) {
1284 alloc_flags
|= ALLOCATEFROMVOL
;
1292 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1294 * call allocate to get the space
1296 error
= VNOP_ALLOCATE(vp
,alloc_struct
.fst_length
,alloc_flags
,
1297 &alloc_struct
.fst_bytesalloc
, alloc_struct
.fst_offset
,
1299 (void)vnode_put(vp
);
1301 error2
= copyout((caddr_t
)&alloc_struct
, argp
, sizeof(alloc_struct
));
1311 if (fp
->f_type
!= DTYPE_VNODE
) {
1316 vp
= (struct vnode
*)fp
->f_data
;
1319 /* need write permissions */
1320 if ((fp
->f_flag
& FWRITE
) == 0) {
1325 if ((error
= copyin(argp
, (caddr_t
)&args
, sizeof(args
)))) {
1329 if ((error
= vnode_getwithref(vp
))) {
1334 if ((error
= mac_vnode_check_write(&context
, fp
->f_fglob
->fg_cred
, vp
))) {
1335 (void)vnode_put(vp
);
1340 error
= VNOP_IOCTL(vp
, F_PUNCHHOLE
, (caddr_t
)&args
, 0, &context
);
1341 (void)vnode_put(vp
);
1345 case F_TRIM_ACTIVE_FILE
: {
1346 ftrimactivefile_t args
;
1348 if (priv_check_cred(kauth_cred_get(), PRIV_TRIM_ACTIVE_FILE
, 0)) {
1353 if (fp
->f_type
!= DTYPE_VNODE
) {
1358 vp
= (struct vnode
*)fp
->f_data
;
1361 /* need write permissions */
1362 if ((fp
->f_flag
& FWRITE
) == 0) {
1367 if ((error
= copyin(argp
, (caddr_t
)&args
, sizeof(args
)))) {
1371 if ((error
= vnode_getwithref(vp
))) {
1375 error
= VNOP_IOCTL(vp
, F_TRIM_ACTIVE_FILE
, (caddr_t
)&args
, 0, &context
);
1376 (void)vnode_put(vp
);
1381 if (fp
->f_type
!= DTYPE_VNODE
) {
1385 vp
= (struct vnode
*)fp
->f_data
;
1388 error
= copyin(argp
, (caddr_t
)&offset
, sizeof (off_t
));
1391 AUDIT_ARG(value64
, offset
);
1393 error
= vnode_getwithref(vp
);
1398 error
= mac_vnode_check_truncate(&context
,
1399 fp
->f_fglob
->fg_cred
, vp
);
1401 (void)vnode_put(vp
);
1406 * Make sure that we are root. Growing a file
1407 * without zero filling the data is a security hole
1408 * root would have access anyway so we'll allow it
1410 if (!kauth_cred_issuser(kauth_cred_get())) {
1416 error
= vnode_setsize(vp
, offset
, IO_NOZEROFILL
,
1421 mac_vnode_notify_truncate(&context
, fp
->f_fglob
->fg_cred
, vp
);
1425 (void)vnode_put(vp
);
1429 if (fp
->f_type
!= DTYPE_VNODE
) {
1434 fp
->f_fglob
->fg_flag
&= ~FNORDAHEAD
;
1436 fp
->f_fglob
->fg_flag
|= FNORDAHEAD
;
1441 if (fp
->f_type
!= DTYPE_VNODE
) {
1446 fp
->f_fglob
->fg_flag
|= FNOCACHE
;
1448 fp
->f_fglob
->fg_flag
&= ~FNOCACHE
;
1453 if (fp
->f_type
!= DTYPE_VNODE
) {
1458 fp
->f_fglob
->fg_flag
|= FNODIRECT
;
1460 fp
->f_fglob
->fg_flag
&= ~FNODIRECT
;
1464 case F_SINGLE_WRITER
:
1465 if (fp
->f_type
!= DTYPE_VNODE
) {
1470 fp
->f_fglob
->fg_flag
|= FSINGLE_WRITER
;
1472 fp
->f_fglob
->fg_flag
&= ~FSINGLE_WRITER
;
1476 case F_GLOBAL_NOCACHE
:
1477 if (fp
->f_type
!= DTYPE_VNODE
) {
1481 vp
= (struct vnode
*)fp
->f_data
;
1484 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1486 *retval
= vnode_isnocache(vp
);
1489 vnode_setnocache(vp
);
1491 vnode_clearnocache(vp
);
1493 (void)vnode_put(vp
);
1497 case F_CHECK_OPENEVT
:
1498 if (fp
->f_type
!= DTYPE_VNODE
) {
1502 vp
= (struct vnode
*)fp
->f_data
;
1505 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1507 *retval
= vnode_is_openevt(vp
);
1510 vnode_set_openevt(vp
);
1512 vnode_clear_openevt(vp
);
1514 (void)vnode_put(vp
);
1519 struct radvisory ra_struct
;
1521 if (fp
->f_type
!= DTYPE_VNODE
) {
1525 vp
= (struct vnode
*)fp
->f_data
;
1528 if ( (error
= copyin(argp
, (caddr_t
)&ra_struct
, sizeof(ra_struct
))) )
1530 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1531 error
= VNOP_IOCTL(vp
, F_RDADVISE
, (caddr_t
)&ra_struct
, 0, &context
);
1533 (void)vnode_put(vp
);
1540 if (fp
->f_type
!= DTYPE_VNODE
) {
1544 vp
= (struct vnode
*)fp
->f_data
;
1547 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1548 error
= VNOP_FSYNC(vp
, MNT_NOWAIT
, &context
);
1550 (void)vnode_put(vp
);
1555 case F_LOG2PHYS_EXT
: {
1556 struct log2phys l2p_struct
; /* structure for allocate command */
1559 off_t file_offset
= 0;
1563 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1564 error
= copyin(argp
, (caddr_t
)&l2p_struct
, sizeof(l2p_struct
));
1567 file_offset
= l2p_struct
.l2p_devoffset
;
1569 file_offset
= fp
->f_offset
;
1571 if (fp
->f_type
!= DTYPE_VNODE
) {
1575 vp
= (struct vnode
*)fp
->f_data
;
1577 if ( (error
= vnode_getwithref(vp
)) ) {
1580 error
= VNOP_OFFTOBLK(vp
, file_offset
, &lbn
);
1582 (void)vnode_put(vp
);
1585 error
= VNOP_BLKTOOFF(vp
, lbn
, &offset
);
1587 (void)vnode_put(vp
);
1590 devBlockSize
= vfs_devblocksize(vnode_mount(vp
));
1591 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1592 if (l2p_struct
.l2p_contigbytes
< 0) {
1598 a_size
= MIN((uint64_t)l2p_struct
.l2p_contigbytes
, SIZE_MAX
);
1600 a_size
= devBlockSize
;
1603 error
= VNOP_BLOCKMAP(vp
, offset
, a_size
, &bn
, &run
, NULL
, 0, &context
);
1605 (void)vnode_put(vp
);
1608 l2p_struct
.l2p_flags
= 0; /* for now */
1609 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1610 l2p_struct
.l2p_contigbytes
= run
- (file_offset
- offset
);
1612 l2p_struct
.l2p_contigbytes
= 0; /* for now */
1616 * The block number being -1 suggests that the file offset is not backed
1617 * by any real blocks on-disk. As a result, just let it be passed back up wholesale.
1620 /* Don't multiply it by the block size */
1621 l2p_struct
.l2p_devoffset
= bn
;
1624 l2p_struct
.l2p_devoffset
= bn
* devBlockSize
;
1625 l2p_struct
.l2p_devoffset
+= file_offset
- offset
;
1627 error
= copyout((caddr_t
)&l2p_struct
, argp
, sizeof(l2p_struct
));
1635 if (fp
->f_type
!= DTYPE_VNODE
) {
1639 vp
= (struct vnode
*)fp
->f_data
;
1642 pathlen
= MAXPATHLEN
;
1643 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
1644 if (pathbufp
== NULL
) {
1648 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1649 error
= vn_getpath(vp
, pathbufp
, &pathlen
);
1650 (void)vnode_put(vp
);
1653 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
1655 FREE(pathbufp
, M_TEMP
);
1659 case F_PATHPKG_CHECK
: {
1663 if (fp
->f_type
!= DTYPE_VNODE
) {
1667 vp
= (struct vnode
*)fp
->f_data
;
1670 pathlen
= MAXPATHLEN
;
1671 pathbufp
= kalloc(MAXPATHLEN
);
1673 if ( (error
= copyinstr(argp
, pathbufp
, MAXPATHLEN
, &pathlen
)) == 0 ) {
1674 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1675 AUDIT_ARG(text
, pathbufp
);
1676 error
= vn_path_package_check(vp
, pathbufp
, pathlen
, retval
);
1678 (void)vnode_put(vp
);
1681 kfree(pathbufp
, MAXPATHLEN
);
1685 case F_CHKCLEAN
: // used by regression tests to see if all dirty pages got cleaned by fsync()
1686 case F_FULLFSYNC
: // fsync + flush the journal + DKIOCSYNCHRONIZE
1687 case F_BARRIERFSYNC
: // fsync + barrier
1688 case F_FREEZE_FS
: // freeze all other fs operations for the fs of this fd
1689 case F_THAW_FS
: { // thaw all frozen fs operations for the fs of this fd
1690 if (fp
->f_type
!= DTYPE_VNODE
) {
1694 vp
= (struct vnode
*)fp
->f_data
;
1697 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1698 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
1700 (void)vnode_put(vp
);
1706 * SPI (private) for opening a file starting from a dir fd
1709 struct user_fopenfrom fopen
;
1710 struct vnode_attr va
;
1711 struct nameidata nd
;
1714 /* Check if this isn't a valid file descriptor */
1715 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1716 (fp
->f_flag
& FREAD
) == 0) {
1720 vp
= (struct vnode
*)fp
->f_data
;
1723 if (vnode_getwithref(vp
)) {
1728 /* Only valid for directories */
1729 if (vp
->v_type
!= VDIR
) {
1735 /* Get flags, mode and pathname arguments. */
1736 if (IS_64BIT_PROCESS(p
)) {
1737 error
= copyin(argp
, &fopen
, sizeof(fopen
));
1739 struct user32_fopenfrom fopen32
;
1741 error
= copyin(argp
, &fopen32
, sizeof(fopen32
));
1742 fopen
.o_flags
= fopen32
.o_flags
;
1743 fopen
.o_mode
= fopen32
.o_mode
;
1744 fopen
.o_pathname
= CAST_USER_ADDR_T(fopen32
.o_pathname
);
1750 AUDIT_ARG(fflags
, fopen
.o_flags
);
1751 AUDIT_ARG(mode
, fopen
.o_mode
);
1753 /* Mask off all but regular access permissions */
1754 cmode
= ((fopen
.o_mode
&~ fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
1755 VATTR_SET(&va
, va_mode
, cmode
& ACCESSPERMS
);
1757 /* Start the lookup relative to the file descriptor's vnode. */
1758 NDINIT(&nd
, LOOKUP
, OP_OPEN
, USEDVP
| FOLLOW
| AUDITVNPATH1
, UIO_USERSPACE
,
1759 fopen
.o_pathname
, &context
);
1762 error
= open1(&context
, &nd
, fopen
.o_flags
, &va
,
1763 fileproc_alloc_init
, NULL
, retval
);
1769 * SPI (private) for unlinking a file starting from a dir fd
1771 case F_UNLINKFROM
: {
1772 user_addr_t pathname
;
1774 /* Check if this isn't a valid file descriptor */
1775 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1776 (fp
->f_flag
& FREAD
) == 0) {
1780 vp
= (struct vnode
*)fp
->f_data
;
1783 if (vnode_getwithref(vp
)) {
1788 /* Only valid for directories */
1789 if (vp
->v_type
!= VDIR
) {
1795 /* Get flags, mode and pathname arguments. */
1796 if (IS_64BIT_PROCESS(p
)) {
1797 pathname
= (user_addr_t
)argp
;
1799 pathname
= CAST_USER_ADDR_T(argp
);
1802 /* Start the lookup relative to the file descriptor's vnode. */
1803 error
= unlink1(&context
, vp
, pathname
, UIO_USERSPACE
, 0);
1812 case F_ADDFILESIGS_FOR_DYLD_SIM
:
1813 case F_ADDFILESIGS_RETURN
:
1815 struct cs_blob
*blob
= NULL
;
1816 struct user_fsignatures fs
;
1818 vm_offset_t kernel_blob_addr
;
1819 vm_size_t kernel_blob_size
;
1820 int blob_add_flags
= 0;
1822 if (fp
->f_type
!= DTYPE_VNODE
) {
1826 vp
= (struct vnode
*)fp
->f_data
;
1829 if (uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
1830 blob_add_flags
|= MAC_VNODE_CHECK_DYLD_SIM
;
1831 if ((p
->p_csflags
& CS_KILL
) == 0) {
1833 p
->p_csflags
|= CS_KILL
;
1838 error
= vnode_getwithref(vp
);
1842 if (IS_64BIT_PROCESS(p
)) {
1843 error
= copyin(argp
, &fs
, sizeof (fs
));
1845 struct user32_fsignatures fs32
;
1847 error
= copyin(argp
, &fs32
, sizeof (fs32
));
1848 fs
.fs_file_start
= fs32
.fs_file_start
;
1849 fs
.fs_blob_start
= CAST_USER_ADDR_T(fs32
.fs_blob_start
);
1850 fs
.fs_blob_size
= fs32
.fs_blob_size
;
1859 * First check if we have something loaded a this offset
1861 blob
= ubc_cs_blob_get(vp
, CPU_TYPE_ANY
, fs
.fs_file_start
);
1864 /* If this is for dyld_sim revalidate the blob */
1865 if (uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
1866 error
= ubc_cs_blob_revalidate(vp
, blob
, NULL
, blob_add_flags
);
1875 * An arbitrary limit, to prevent someone from mapping in a 20GB blob. This should cover
1876 * our use cases for the immediate future, but note that at the time of this commit, some
1877 * platforms are nearing 2MB blob sizes (with a prior soft limit of 2.5MB).
1879 * We should consider how we can manage this more effectively; the above means that some
1880 * platforms are using megabytes of memory for signing data; it merely hasn't crossed the
1881 * threshold considered ridiculous at the time of this change.
1883 #define CS_MAX_BLOB_SIZE (40ULL * 1024ULL * 1024ULL)
1884 if (fs
.fs_blob_size
> CS_MAX_BLOB_SIZE
) {
1890 kernel_blob_size
= CAST_DOWN(vm_size_t
, fs
.fs_blob_size
);
1891 kr
= ubc_cs_blob_allocate(&kernel_blob_addr
, &kernel_blob_size
);
1892 if (kr
!= KERN_SUCCESS
) {
1898 if(uap
->cmd
== F_ADDSIGS
) {
1899 error
= copyin(fs
.fs_blob_start
,
1900 (void *) kernel_blob_addr
,
1902 } else /* F_ADDFILESIGS || F_ADDFILESIGS_RETURN || F_ADDFILESIGS_FOR_DYLD_SIM */ {
1905 error
= vn_rdwr(UIO_READ
,
1907 (caddr_t
) kernel_blob_addr
,
1909 fs
.fs_file_start
+ fs
.fs_blob_start
,
1915 if ((error
== 0) && resid
) {
1916 /* kernel_blob_size rounded to a page size, but signature may be at end of file */
1917 memset((void *)(kernel_blob_addr
+ (kernel_blob_size
- resid
)), 0x0, resid
);
1922 ubc_cs_blob_deallocate(kernel_blob_addr
,
1929 error
= ubc_cs_blob_add(vp
,
1930 CPU_TYPE_ANY
, /* not for a specific architecture */
1938 /* ubc_blob_add() has consumed "kernel_blob_addr" if it is zeroed */
1940 if (kernel_blob_addr
) {
1941 ubc_cs_blob_deallocate(kernel_blob_addr
,
1947 #if CHECK_CS_VALIDATION_BITMAP
1948 ubc_cs_validation_bitmap_allocate( vp
);
1953 if (uap
->cmd
== F_ADDFILESIGS_RETURN
|| uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
1955 * The first element of the structure is a
1956 * off_t that happen to have the same size for
1957 * all archs. Lets overwrite that.
1959 off_t end_offset
= 0;
1961 end_offset
= blob
->csb_end_offset
;
1962 error
= copyout(&end_offset
, argp
, sizeof (end_offset
));
1965 (void) vnode_put(vp
);
1974 struct fileglob
*fg
;
1977 if (fp
->f_type
!= DTYPE_VNODE
) {
1984 if (IS_64BIT_PROCESS(p
)) {
1985 error
= copyin(argp
, &lv
, sizeof (lv
));
1987 struct user32_fchecklv lv32
;
1989 error
= copyin(argp
, &lv32
, sizeof (lv32
));
1990 lv
.lv_file_start
= lv32
.lv_file_start
;
1991 lv
.lv_error_message
= (void *)(uintptr_t)lv32
.lv_error_message
;
1992 lv
.lv_error_message_size
= lv32
.lv_error_message
;
1998 error
= mac_file_check_library_validation(p
, fg
, lv
.lv_file_start
,
1999 (user_long_t
)lv
.lv_error_message
, lv
.lv_error_message_size
);
2005 case F_GETPROTECTIONCLASS
: {
2006 if (fp
->f_type
!= DTYPE_VNODE
) {
2010 vp
= (struct vnode
*)fp
->f_data
;
2014 if (vnode_getwithref(vp
)) {
2019 struct vnode_attr va
;
2022 VATTR_WANTED(&va
, va_dataprotect_class
);
2023 error
= VNOP_GETATTR(vp
, &va
, &context
);
2025 if (VATTR_IS_SUPPORTED(&va
, va_dataprotect_class
))
2026 *retval
= va
.va_dataprotect_class
;
2035 case F_SETPROTECTIONCLASS
: {
2036 /* tmp must be a valid PROTECTION_CLASS_* */
2037 tmp
= CAST_DOWN_EXPLICIT(uint32_t, uap
->arg
);
2039 if (fp
->f_type
!= DTYPE_VNODE
) {
2043 vp
= (struct vnode
*)fp
->f_data
;
2047 if (vnode_getwithref(vp
)) {
2052 /* Only go forward if you have write access */
2053 vfs_context_t ctx
= vfs_context_current();
2054 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2060 struct vnode_attr va
;
2063 VATTR_SET(&va
, va_dataprotect_class
, tmp
);
2065 error
= VNOP_SETATTR(vp
, &va
, ctx
);
2071 case F_TRANSCODEKEY
: {
2072 if (fp
->f_type
!= DTYPE_VNODE
) {
2077 vp
= (struct vnode
*)fp
->f_data
;
2080 if (vnode_getwithref(vp
)) {
2086 .len
= CP_MAX_WRAPPEDKEYSIZE
,
2089 MALLOC(k
.key
, char *, k
.len
, M_TEMP
, M_WAITOK
);
2091 error
= VNOP_IOCTL(vp
, F_TRANSCODEKEY
, (caddr_t
)&k
, 1, &context
);
2096 error
= copyout(k
.key
, argp
, k
.len
);
2100 FREE(k
.key
, M_TEMP
);
2105 case F_GETPROTECTIONLEVEL
: {
2106 if (fp
->f_type
!= DTYPE_VNODE
) {
2111 vp
= (struct vnode
*) fp
->f_data
;
2114 if (vnode_getwithref(vp
)) {
2119 error
= VNOP_IOCTL(vp
, F_GETPROTECTIONLEVEL
, (caddr_t
)retval
, 0, &context
);
2125 case F_GETDEFAULTPROTLEVEL
: {
2126 if (fp
->f_type
!= DTYPE_VNODE
) {
2131 vp
= (struct vnode
*) fp
->f_data
;
2134 if (vnode_getwithref(vp
)) {
2140 * if cp_get_major_vers fails, error will be set to proper errno
2141 * and cp_version will still be 0.
2144 error
= VNOP_IOCTL(vp
, F_GETDEFAULTPROTLEVEL
, (caddr_t
)retval
, 0, &context
);
2150 #endif /* CONFIG_PROTECT */
2152 case F_MOVEDATAEXTENTS
: {
2153 struct fileproc
*fp2
= NULL
;
2154 struct vnode
*src_vp
= NULLVP
;
2155 struct vnode
*dst_vp
= NULLVP
;
2156 /* We need to grab the 2nd FD out of the argments before moving on. */
2157 int fd2
= CAST_DOWN_EXPLICIT(int32_t, uap
->arg
);
2159 error
= priv_check_cred(kauth_cred_get(), PRIV_VFS_MOVE_DATA_EXTENTS
, 0);
2163 if (fp
->f_type
!= DTYPE_VNODE
) {
2168 /* For now, special case HFS+ only, since this is SPI. */
2169 src_vp
= (struct vnode
*)fp
->f_data
;
2170 if (src_vp
->v_tag
!= VT_HFS
) {
2176 * Get the references before we start acquiring iocounts on the vnodes,
2177 * while we still hold the proc fd lock
2179 if ( (error
= fp_lookup(p
, fd2
, &fp2
, 1)) ) {
2183 if (fp2
->f_type
!= DTYPE_VNODE
) {
2184 fp_drop(p
, fd2
, fp2
, 1);
2188 dst_vp
= (struct vnode
*)fp2
->f_data
;
2189 if (dst_vp
->v_tag
!= VT_HFS
) {
2190 fp_drop(p
, fd2
, fp2
, 1);
2196 /* Re-do MAC checks against the new FD, pass in a fake argument */
2197 error
= mac_file_check_fcntl(proc_ucred(p
), fp2
->f_fglob
, uap
->cmd
, 0);
2199 fp_drop(p
, fd2
, fp2
, 1);
2203 /* Audit the 2nd FD */
2208 if (vnode_getwithref(src_vp
)) {
2209 fp_drop(p
, fd2
, fp2
, 0);
2213 if (vnode_getwithref(dst_vp
)) {
2215 fp_drop(p
, fd2
, fp2
, 0);
2221 * Basic asserts; validate they are not the same and that
2222 * both live on the same filesystem.
2224 if (dst_vp
== src_vp
) {
2227 fp_drop (p
, fd2
, fp2
, 0);
2232 if (dst_vp
->v_mount
!= src_vp
->v_mount
) {
2235 fp_drop (p
, fd2
, fp2
, 0);
2240 /* Now we have a legit pair of FDs. Go to work */
2242 /* Now check for write access to the target files */
2243 if(vnode_authorize(src_vp
, NULLVP
,
2244 (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), &context
) != 0) {
2247 fp_drop(p
, fd2
, fp2
, 0);
2252 if(vnode_authorize(dst_vp
, NULLVP
,
2253 (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), &context
) != 0) {
2256 fp_drop(p
, fd2
, fp2
, 0);
2261 /* Verify that both vps point to files and not directories */
2262 if ( !vnode_isreg(src_vp
) || !vnode_isreg(dst_vp
)) {
2266 fp_drop (p
, fd2
, fp2
, 0);
2271 * The exchangedata syscall handler passes in 0 for the flags to VNOP_EXCHANGE.
2272 * We'll pass in our special bit indicating that the new behavior is expected
2275 error
= VNOP_EXCHANGE(src_vp
, dst_vp
, FSOPT_EXCHANGE_DATA_ONLY
, &context
);
2279 fp_drop(p
, fd2
, fp2
, 0);
2284 * SPI for making a file compressed.
2286 case F_MAKECOMPRESSED
: {
2287 uint32_t gcounter
= CAST_DOWN_EXPLICIT(uint32_t, uap
->arg
);
2289 if (fp
->f_type
!= DTYPE_VNODE
) {
2294 vp
= (struct vnode
*) fp
->f_data
;
2298 if (vnode_getwithref(vp
)) {
2304 if ((vnode_isreg(vp
) == 0) && (vnode_islnk(vp
) == 0)) {
2310 /* invoke ioctl to pass off to FS */
2311 /* Only go forward if you have write access */
2312 vfs_context_t ctx
= vfs_context_current();
2313 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2319 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)&gcounter
, 0, &context
);
2326 * SPI (private) for indicating to a filesystem that subsequent writes to
2327 * the open FD will written to the Fastflow.
2329 case F_SET_GREEDY_MODE
:
2330 /* intentionally drop through to the same handler as F_SETSTATIC.
2331 * both fcntls should pass the argument and their selector into VNOP_IOCTL.
2335 * SPI (private) for indicating to a filesystem that subsequent writes to
2336 * the open FD will represent static content.
2338 case F_SETSTATICCONTENT
: {
2339 caddr_t ioctl_arg
= NULL
;
2342 ioctl_arg
= (caddr_t
) 1;
2345 if (fp
->f_type
!= DTYPE_VNODE
) {
2349 vp
= (struct vnode
*)fp
->f_data
;
2352 error
= vnode_getwithref(vp
);
2358 /* Only go forward if you have write access */
2359 vfs_context_t ctx
= vfs_context_current();
2360 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2366 error
= VNOP_IOCTL(vp
, uap
->cmd
, ioctl_arg
, 0, &context
);
2367 (void)vnode_put(vp
);
2373 * SPI (private) for indicating to the lower level storage driver that the
2374 * subsequent writes should be of a particular IO type (burst, greedy, static),
2375 * or other flavors that may be necessary.
2382 /* extract 32 bits of flags from userland */
2383 param_ptr
= (caddr_t
) uap
->arg
;
2384 param
= (uint32_t) param_ptr
;
2387 /* If no argument is specified, error out */
2393 * Validate the different types of flags that can be specified:
2394 * all of them are mutually exclusive for now.
2397 case F_IOTYPE_ISOCHRONOUS
:
2406 if (fp
->f_type
!= DTYPE_VNODE
) {
2410 vp
= (struct vnode
*)fp
->f_data
;
2413 error
= vnode_getwithref(vp
);
2419 /* Only go forward if you have write access */
2420 vfs_context_t ctx
= vfs_context_current();
2421 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2427 error
= VNOP_IOCTL(vp
, uap
->cmd
, param_ptr
, 0, &context
);
2428 (void)vnode_put(vp
);
2434 * Set the vnode pointed to by 'fd'
2435 * and tag it as the (potentially future) backing store
2436 * for another filesystem
2438 case F_SETBACKINGSTORE
: {
2439 if (fp
->f_type
!= DTYPE_VNODE
) {
2444 vp
= (struct vnode
*)fp
->f_data
;
2446 if (vp
->v_tag
!= VT_HFS
) {
2452 if (vnode_getwithref(vp
)) {
2457 /* only proceed if you have write access */
2458 vfs_context_t ctx
= vfs_context_current();
2459 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2466 /* If arg != 0, set, otherwise unset */
2468 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
)1, 0, &context
);
2471 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
2479 * like F_GETPATH, but special semantics for
2480 * the mobile time machine handler.
2482 case F_GETPATH_MTMINFO
: {
2486 if (fp
->f_type
!= DTYPE_VNODE
) {
2490 vp
= (struct vnode
*)fp
->f_data
;
2493 pathlen
= MAXPATHLEN
;
2494 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
2495 if (pathbufp
== NULL
) {
2499 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
2500 int backingstore
= 0;
2502 /* Check for error from vn_getpath before moving on */
2503 if ((error
= vn_getpath(vp
, pathbufp
, &pathlen
)) == 0) {
2504 if (vp
->v_tag
== VT_HFS
) {
2505 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
) &backingstore
, 0, &context
);
2507 (void)vnode_put(vp
);
2510 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
2514 * If the copyout was successful, now check to ensure
2515 * that this vnode is not a BACKINGSTORE vnode. mtmd
2516 * wants the path regardless.
2523 (void)vnode_put(vp
);
2525 FREE(pathbufp
, M_TEMP
);
2529 #if DEBUG || DEVELOPMENT
2531 if (fp
->f_type
!= DTYPE_VNODE
) {
2535 vp
= (struct vnode
*)fp
->f_data
;
2544 * This is an fcntl() that we d not recognize at this level;
2545 * if this is a vnode, we send it down into the VNOP_IOCTL
2546 * for this vnode; this can include special devices, and will
2547 * effectively overload fcntl() to send ioctl()'s.
2549 if((uap
->cmd
& IOC_VOID
) && (uap
->cmd
& IOC_INOUT
)){
2554 /* Catch any now-invalid fcntl() selectors */
2556 case (int)APFSIOC_REVERT_TO_SNAPSHOT
:
2557 case (int)FSIOC_FIOSEEKHOLE
:
2558 case (int)FSIOC_FIOSEEKDATA
:
2559 case HFS_GET_BOOT_INFO
:
2560 case HFS_SET_BOOT_INFO
:
2562 case F_MARKDEPENDENCY
:
2569 if (fp
->f_type
!= DTYPE_VNODE
) {
2573 vp
= (struct vnode
*)fp
->f_data
;
2576 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
2577 #define STK_PARAMS 128
2578 char stkbuf
[STK_PARAMS
] = {0};
2582 * For this to work properly, we have to copy in the
2583 * ioctl() cmd argument if there is one; we must also
2584 * check that a command parameter, if present, does
2585 * not exceed the maximum command length dictated by
2586 * the number of bits we have available in the command
2587 * to represent a structure length. Finally, we have
2588 * to copy the results back out, if it is that type of
2591 size
= IOCPARM_LEN(uap
->cmd
);
2592 if (size
> IOCPARM_MAX
) {
2593 (void)vnode_put(vp
);
2599 if (size
> sizeof (stkbuf
)) {
2600 if ((memp
= (caddr_t
)kalloc(size
)) == 0) {
2601 (void)vnode_put(vp
);
2610 if (uap
->cmd
& IOC_IN
) {
2613 error
= copyin(argp
, data
, size
);
2615 (void)vnode_put(vp
);
2621 /* Bzero the section beyond that which was needed */
2622 if (size
<= sizeof(stkbuf
)) {
2623 bzero ( (((uint8_t*)data
) + size
), (sizeof(stkbuf
) - size
));
2628 *(user_addr_t
*)data
= argp
;
2630 *(uint32_t *)data
= (uint32_t)argp
;
2633 } else if ((uap
->cmd
& IOC_OUT
) && size
) {
2635 * Zero the buffer so the user always
2636 * gets back something deterministic.
2639 } else if (uap
->cmd
& IOC_VOID
) {
2641 *(user_addr_t
*)data
= argp
;
2643 *(uint32_t *)data
= (uint32_t)argp
;
2647 error
= VNOP_IOCTL(vp
, uap
->cmd
, CAST_DOWN(caddr_t
, data
), 0, &context
);
2649 (void)vnode_put(vp
);
2651 /* Copy any output data to user */
2652 if (error
== 0 && (uap
->cmd
& IOC_OUT
) && size
)
2653 error
= copyout(data
, argp
, size
);
2661 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
2662 fp_drop(p
, fd
, fp
, 0);
2665 fp_drop(p
, fd
, fp
, 1);
2674 * Description: Common code for dup, dup2, and fcntl(F_DUPFD).
2676 * Parameters: p Process performing the dup
2678 * new The fd to dup it to
2679 * fd_flags Flags to augment the new fd
2680 * retval Pointer to the call return area
2682 * Returns: 0 Success
2687 * *retval (modified) The new descriptor
2689 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
2692 * Notes: This function may drop and reacquire this lock; it is unsafe
2693 * for a caller to assume that other state protected by the lock
2694 * has not been subsequently changed out from under it.
2698 struct filedesc
*fdp
, int old
, int new, int fd_flags
, int32_t *retval
)
2700 struct fileproc
*nfp
;
2701 struct fileproc
*ofp
;
2707 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2709 if ((ofp
= fdp
->fd_ofiles
[old
]) == NULL
||
2710 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
)) {
2717 error
= mac_file_check_dup(proc_ucred(p
), ofp
->f_fglob
, new);
2727 nfp
= fileproc_alloc_init(NULL
);
2737 nfp
->f_fglob
= ofp
->f_fglob
;
2740 if (fdp
->fd_ofiles
[new] != 0)
2741 panic("finishdup: overwriting fd_ofiles with new %d", new);
2742 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0)
2743 panic("finishdup: unreserved fileflags with new %d", new);
2746 if (new > fdp
->fd_lastfile
)
2747 fdp
->fd_lastfile
= new;
2748 *fdflags(p
, new) |= fd_flags
;
2749 procfdtbl_releasefd(p
, new, nfp
);
2758 * Description: The implementation of the close(2) system call
2760 * Parameters: p Process in whose per process file table
2761 * the close is to occur
2762 * uap->fd fd to be closed
2765 * Returns: 0 Success
2766 * fp_lookup:EBADF Bad file descriptor
2767 * fp_guard_exception:??? Guarded file descriptor
2768 * close_internal:EBADF
2769 * close_internal:??? Anything returnable by a per-fileops
2773 close(proc_t p
, struct close_args
*uap
, int32_t *retval
)
2775 __pthread_testcancel(1);
2776 return(close_nocancel(p
, (struct close_nocancel_args
*)uap
, retval
));
2781 close_nocancel(proc_t p
, struct close_nocancel_args
*uap
, __unused
int32_t *retval
)
2783 struct fileproc
*fp
;
2787 AUDIT_SYSCLOSE(p
, fd
);
2791 if ( (error
= fp_lookup(p
,fd
,&fp
, 1)) ) {
2796 if (FP_ISGUARDED(fp
, GUARD_CLOSE
)) {
2797 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_CLOSE
);
2798 (void) fp_drop(p
, fd
, fp
, 1);
2803 error
= close_internal_locked(p
, fd
, fp
, 0);
2812 * close_internal_locked
2814 * Close a file descriptor.
2816 * Parameters: p Process in whose per process file table
2817 * the close is to occur
2818 * fd fd to be closed
2819 * fp fileproc associated with the fd
2821 * Returns: 0 Success
2822 * EBADF fd already in close wait state
2823 * closef_locked:??? Anything returnable by a per-fileops
2826 * Locks: Assumes proc_fdlock for process is held by the caller and returns
2829 * Notes: This function may drop and reacquire this lock; it is unsafe
2830 * for a caller to assume that other state protected by the lock
2831 * has not been subsequently changed out from under it.
2834 close_internal_locked(proc_t p
, int fd
, struct fileproc
*fp
, int flags
)
2836 struct filedesc
*fdp
= p
->p_fd
;
2838 int resvfd
= flags
& FD_DUP2RESV
;
2842 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2845 /* Keep people from using the filedesc while we are closing it */
2846 procfdtbl_markclosefd(p
, fd
);
2849 if ((fp
->f_flags
& FP_CLOSING
) == FP_CLOSING
) {
2850 panic("close_internal_locked: being called on already closing fd");
2855 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
2856 panic("close_internal: unreserved fileflags with fd %d", fd
);
2859 fp
->f_flags
|= FP_CLOSING
;
2861 if ( (fp
->f_flags
& FP_AIOISSUED
) || kauth_authorize_fileop_has_listeners() ) {
2865 if ( (fp
->f_type
== DTYPE_VNODE
) && kauth_authorize_fileop_has_listeners() ) {
2867 * call out to allow 3rd party notification of close.
2868 * Ignore result of kauth_authorize_fileop call.
2870 if (vnode_getwithref((vnode_t
)fp
->f_data
) == 0) {
2871 u_int fileop_flags
= 0;
2872 if ((fp
->f_flags
& FP_WRITTEN
) != 0)
2873 fileop_flags
|= KAUTH_FILEOP_CLOSE_MODIFIED
;
2874 kauth_authorize_fileop(fp
->f_fglob
->fg_cred
, KAUTH_FILEOP_CLOSE
,
2875 (uintptr_t)fp
->f_data
, (uintptr_t)fileop_flags
);
2876 vnode_put((vnode_t
)fp
->f_data
);
2879 if (fp
->f_flags
& FP_AIOISSUED
)
2881 * cancel all async IO requests that can be cancelled.
2883 _aio_close( p
, fd
);
2888 if (fd
< fdp
->fd_knlistsize
)
2889 knote_fdclose(p
, fd
, FALSE
);
2891 if (fp
->f_flags
& FP_WAITEVENT
)
2892 (void)waitevent_close(p
, fp
);
2894 fileproc_drain(p
, fp
);
2899 procfdtbl_reservefd(p
, fd
);
2902 if (ENTR_SHOULDTRACE
&& fp
->f_type
== DTYPE_SOCKET
)
2903 KERNEL_ENERGYTRACE(kEnTrActKernSocket
, DBG_FUNC_END
,
2904 fd
, 0, (int64_t)VM_KERNEL_ADDRPERM(fp
->f_data
));
2906 error
= closef_locked(fp
, fp
->f_fglob
, p
);
2907 if ((fp
->f_flags
& FP_WAITCLOSE
) == FP_WAITCLOSE
)
2908 wakeup(&fp
->f_flags
);
2909 fp
->f_flags
&= ~(FP_WAITCLOSE
| FP_CLOSING
);
2919 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
2920 panic("close with reserved fd returns with freed fd:%d: proc: %p", fd
, p
);
2931 * Description: Return status information about a file descriptor.
2933 * Parameters: p The process doing the fstat
2935 * ub The user stat buffer
2936 * xsecurity The user extended security
2937 * buffer, or 0 if none
2938 * xsecurity_size The size of xsecurity, or 0
2940 * isstat64 Flag to indicate 64 bit version
2941 * for inode size, etc.
2943 * Returns: 0 Success
2946 * fp_lookup:EBADF Bad file descriptor
2947 * vnode_getwithref:???
2949 * vnode_getwithref:???
2956 * Notes: Internal implementation for all other fstat() related
2959 * XXX switch on node type is bogus; need a stat in struct
2960 * XXX fileops instead.
2963 fstat1(proc_t p
, int fd
, user_addr_t ub
, user_addr_t xsecurity
, user_addr_t xsecurity_size
, int isstat64
)
2965 struct fileproc
*fp
;
2971 struct user64_stat user64_sb
;
2972 struct user32_stat user32_sb
;
2973 struct user64_stat64 user64_sb64
;
2974 struct user32_stat64 user32_sb64
;
2979 kauth_filesec_t fsec
;
2980 user_size_t xsecurity_bufsize
;
2981 vfs_context_t ctx
= vfs_context_current();
2987 if ((error
= fp_lookup(p
, fd
, &fp
, 0)) != 0) {
2992 fsec
= KAUTH_FILESEC_NONE
;
2994 sbptr
= (void *)&source
;
2999 if ((error
= vnode_getwithref((vnode_t
)data
)) == 0) {
3001 * If the caller has the file open, and is not
3002 * requesting extended security information, we are
3003 * going to let them get the basic stat information.
3005 if (xsecurity
== USER_ADDR_NULL
) {
3006 error
= vn_stat_noauth((vnode_t
)data
, sbptr
, NULL
, isstat64
, ctx
,
3007 fp
->f_fglob
->fg_cred
);
3009 error
= vn_stat((vnode_t
)data
, sbptr
, &fsec
, isstat64
, ctx
);
3012 AUDIT_ARG(vnpath
, (struct vnode
*)data
, ARG_VNODE1
);
3013 (void)vnode_put((vnode_t
)data
);
3019 error
= soo_stat((struct socket
*)data
, sbptr
, isstat64
);
3021 #endif /* SOCKETS */
3024 error
= pipe_stat((void *)data
, sbptr
, isstat64
);
3028 error
= pshm_stat((void *)data
, sbptr
, isstat64
);
3032 error
= kqueue_stat((void *)data
, sbptr
, isstat64
, p
);
3042 if (isstat64
!= 0) {
3043 source
.sb64
.st_lspare
= 0;
3044 source
.sb64
.st_qspare
[0] = 0LL;
3045 source
.sb64
.st_qspare
[1] = 0LL;
3047 if (IS_64BIT_PROCESS(current_proc())) {
3048 munge_user64_stat64(&source
.sb64
, &dest
.user64_sb64
);
3049 my_size
= sizeof(dest
.user64_sb64
);
3050 sbp
= (caddr_t
)&dest
.user64_sb64
;
3052 munge_user32_stat64(&source
.sb64
, &dest
.user32_sb64
);
3053 my_size
= sizeof(dest
.user32_sb64
);
3054 sbp
= (caddr_t
)&dest
.user32_sb64
;
3057 source
.sb
.st_lspare
= 0;
3058 source
.sb
.st_qspare
[0] = 0LL;
3059 source
.sb
.st_qspare
[1] = 0LL;
3060 if (IS_64BIT_PROCESS(current_proc())) {
3061 munge_user64_stat(&source
.sb
, &dest
.user64_sb
);
3062 my_size
= sizeof(dest
.user64_sb
);
3063 sbp
= (caddr_t
)&dest
.user64_sb
;
3065 munge_user32_stat(&source
.sb
, &dest
.user32_sb
);
3066 my_size
= sizeof(dest
.user32_sb
);
3067 sbp
= (caddr_t
)&dest
.user32_sb
;
3071 error
= copyout(sbp
, ub
, my_size
);
3074 /* caller wants extended security information? */
3075 if (xsecurity
!= USER_ADDR_NULL
) {
3077 /* did we get any? */
3078 if (fsec
== KAUTH_FILESEC_NONE
) {
3079 if (susize(xsecurity_size
, 0) != 0) {
3084 /* find the user buffer size */
3085 xsecurity_bufsize
= fusize(xsecurity_size
);
3087 /* copy out the actual data size */
3088 if (susize(xsecurity_size
, KAUTH_FILESEC_COPYSIZE(fsec
)) != 0) {
3093 /* if the caller supplied enough room, copy out to it */
3094 if (xsecurity_bufsize
>= KAUTH_FILESEC_COPYSIZE(fsec
))
3095 error
= copyout(fsec
, xsecurity
, KAUTH_FILESEC_COPYSIZE(fsec
));
3099 fp_drop(p
, fd
, fp
, 0);
3101 kauth_filesec_free(fsec
);
3109 * Description: Extended version of fstat supporting returning extended
3110 * security information
3112 * Parameters: p The process doing the fstat
3113 * uap->fd The fd to stat
3114 * uap->ub The user stat buffer
3115 * uap->xsecurity The user extended security
3116 * buffer, or 0 if none
3117 * uap->xsecurity_size The size of xsecurity, or 0
3119 * Returns: 0 Success
3120 * !0 Errno (see fstat1)
3123 fstat_extended(proc_t p
, struct fstat_extended_args
*uap
, __unused
int32_t *retval
)
3125 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 0));
3132 * Description: Get file status for the file associated with fd
3134 * Parameters: p The process doing the fstat
3135 * uap->fd The fd to stat
3136 * uap->ub The user stat buffer
3138 * Returns: 0 Success
3139 * !0 Errno (see fstat1)
3142 fstat(proc_t p
, struct fstat_args
*uap
, __unused
int32_t *retval
)
3144 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 0));
3151 * Description: Extended version of fstat64 supporting returning extended
3152 * security information
3154 * Parameters: p The process doing the fstat
3155 * uap->fd The fd to stat
3156 * uap->ub The user stat buffer
3157 * uap->xsecurity The user extended security
3158 * buffer, or 0 if none
3159 * uap->xsecurity_size The size of xsecurity, or 0
3161 * Returns: 0 Success
3162 * !0 Errno (see fstat1)
3165 fstat64_extended(proc_t p
, struct fstat64_extended_args
*uap
, __unused
int32_t *retval
)
3167 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 1));
3174 * Description: Get 64 bit version of the file status for the file associated
3177 * Parameters: p The process doing the fstat
3178 * uap->fd The fd to stat
3179 * uap->ub The user stat buffer
3181 * Returns: 0 Success
3182 * !0 Errno (see fstat1)
3185 fstat64(proc_t p
, struct fstat64_args
*uap
, __unused
int32_t *retval
)
3187 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 1));
3194 * Description: Return pathconf information about a file descriptor.
3196 * Parameters: p Process making the request
3197 * uap->fd fd to get information about
3198 * uap->name Name of information desired
3199 * retval Pointer to the call return area
3201 * Returns: 0 Success
3203 * fp_lookup:EBADF Bad file descriptor
3204 * vnode_getwithref:???
3208 * *retval (modified) Returned information (numeric)
3211 fpathconf(proc_t p
, struct fpathconf_args
*uap
, int32_t *retval
)
3214 struct fileproc
*fp
;
3221 AUDIT_ARG(fd
, uap
->fd
);
3222 if ( (error
= fp_lookup(p
, fd
, &fp
, 0)) )
3230 if (uap
->name
!= _PC_PIPE_BUF
) {
3239 if (uap
->name
!= _PC_PIPE_BUF
) {
3248 vp
= (struct vnode
*)data
;
3250 if ( (error
= vnode_getwithref(vp
)) == 0) {
3251 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
3253 error
= vn_pathconf(vp
, uap
->name
, retval
, vfs_context_current());
3255 (void)vnode_put(vp
);
3266 fp_drop(p
, fd
, fp
, 0);
3271 * Statistics counter for the number of times a process calling fdalloc()
3272 * has resulted in an expansion of the per process open file table.
3274 * XXX This would likely be of more use if it were per process
3282 * Description: Allocate a file descriptor for the process.
3284 * Parameters: p Process to allocate the fd in
3285 * want The fd we would prefer to get
3286 * result Pointer to fd we got
3288 * Returns: 0 Success
3293 * *result (modified) The fd which was allocated
3296 fdalloc(proc_t p
, int want
, int *result
)
3298 struct filedesc
*fdp
= p
->p_fd
;
3300 int lim
, last
, numfiles
, oldnfiles
;
3301 struct fileproc
**newofiles
, **ofiles
;
3302 char *newofileflags
;
3305 * Search for a free descriptor starting at the higher
3306 * of want or fd_freefile. If that fails, consider
3307 * expanding the ofile array.
3310 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
3313 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
3315 last
= min(fdp
->fd_nfiles
, lim
);
3316 if ((i
= want
) < fdp
->fd_freefile
)
3317 i
= fdp
->fd_freefile
;
3318 for (; i
< last
; i
++) {
3319 if (fdp
->fd_ofiles
[i
] == NULL
&& !(fdp
->fd_ofileflags
[i
] & UF_RESERVED
)) {
3320 procfdtbl_reservefd(p
, i
);
3321 if (i
> fdp
->fd_lastfile
)
3322 fdp
->fd_lastfile
= i
;
3323 if (want
<= fdp
->fd_freefile
)
3324 fdp
->fd_freefile
= i
;
3331 * No space in current array. Expand?
3333 if (fdp
->fd_nfiles
>= lim
)
3335 if (fdp
->fd_nfiles
< NDEXTENT
)
3336 numfiles
= NDEXTENT
;
3338 numfiles
= 2 * fdp
->fd_nfiles
;
3343 MALLOC_ZONE(newofiles
, struct fileproc
**,
3344 numfiles
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
3346 if (newofiles
== NULL
) {
3349 if (fdp
->fd_nfiles
>= numfiles
) {
3350 FREE_ZONE(newofiles
, numfiles
* OFILESIZE
, M_OFILETABL
);
3353 newofileflags
= (char *) &newofiles
[numfiles
];
3355 * Copy the existing ofile and ofileflags arrays
3356 * and zero the new portion of each array.
3358 oldnfiles
= fdp
->fd_nfiles
;
3359 (void) memcpy(newofiles
, fdp
->fd_ofiles
,
3360 oldnfiles
* sizeof(*fdp
->fd_ofiles
));
3361 (void) memset(&newofiles
[oldnfiles
], 0,
3362 (numfiles
- oldnfiles
) * sizeof(*fdp
->fd_ofiles
));
3364 (void) memcpy(newofileflags
, fdp
->fd_ofileflags
,
3365 oldnfiles
* sizeof(*fdp
->fd_ofileflags
));
3366 (void) memset(&newofileflags
[oldnfiles
], 0,
3367 (numfiles
- oldnfiles
) *
3368 sizeof(*fdp
->fd_ofileflags
));
3369 ofiles
= fdp
->fd_ofiles
;
3370 fdp
->fd_ofiles
= newofiles
;
3371 fdp
->fd_ofileflags
= newofileflags
;
3372 fdp
->fd_nfiles
= numfiles
;
3373 FREE_ZONE(ofiles
, oldnfiles
* OFILESIZE
, M_OFILETABL
);
3382 * Description: Check to see whether n user file descriptors are available
3385 * Parameters: p Process to check in
3386 * n The number of fd's desired
3391 * Locks: Assumes proc_fdlock for process is held by the caller
3393 * Notes: The answer only remains valid so long as the proc_fdlock is
3394 * held by the caller.
3397 fdavail(proc_t p
, int n
)
3399 struct filedesc
*fdp
= p
->p_fd
;
3400 struct fileproc
**fpp
;
3404 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
3405 if ((i
= lim
- fdp
->fd_nfiles
) > 0 && (n
-= i
) <= 0)
3407 fpp
= &fdp
->fd_ofiles
[fdp
->fd_freefile
];
3408 flags
= &fdp
->fd_ofileflags
[fdp
->fd_freefile
];
3409 for (i
= fdp
->fd_nfiles
- fdp
->fd_freefile
; --i
>= 0; fpp
++, flags
++)
3410 if (*fpp
== NULL
&& !(*flags
& UF_RESERVED
) && --n
<= 0)
3419 * Description: Legacy KPI wrapper function for _fdrelse
3421 * Parameters: p Process in which fd lives
3426 * Locks: Assumes proc_fdlock for process is held by the caller
3429 fdrelse(proc_t p
, int fd
)
3438 * Description: Get the fileproc pointer for the given fd from the per process
3439 * open file table without taking an explicit reference on it.
3441 * Parameters: p Process containing fd
3442 * fd fd to obtain fileproc for
3443 * resultfp Pointer to pointer return area
3445 * Returns: 0 Success
3449 * *resultfp (modified) Pointer to fileproc pointer
3451 * Locks: Assumes proc_fdlock for process is held by the caller
3453 * Notes: Because there is no reference explicitly taken, the returned
3454 * fileproc pointer is only valid so long as the proc_fdlock
3455 * remains held by the caller.
3458 fdgetf_noref(proc_t p
, int fd
, struct fileproc
**resultfp
)
3460 struct filedesc
*fdp
= p
->p_fd
;
3461 struct fileproc
*fp
;
3463 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3464 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3465 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3477 * Description: Get fileproc and vnode pointer for a given fd from the per
3478 * process open file table of the specified process, and if
3479 * successful, increment the f_iocount
3481 * Parameters: p Process in which fd lives
3482 * fd fd to get information for
3483 * resultfp Pointer to result fileproc
3484 * pointer area, or 0 if none
3485 * resultvp Pointer to result vnode pointer
3486 * area, or 0 if none
3488 * Returns: 0 Success
3489 * EBADF Bad file descriptor
3490 * ENOTSUP fd does not refer to a vnode
3493 * *resultfp (modified) Fileproc pointer
3494 * *resultvp (modified) vnode pointer
3496 * Notes: The resultfp and resultvp fields are optional, and may be
3497 * independently specified as NULL to skip returning information
3499 * Locks: Internally takes and releases proc_fdlock
3502 fp_getfvp(proc_t p
, int fd
, struct fileproc
**resultfp
, struct vnode
**resultvp
)
3504 struct filedesc
*fdp
= p
->p_fd
;
3505 struct fileproc
*fp
;
3507 proc_fdlock_spin(p
);
3508 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3509 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3510 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3514 if (fp
->f_type
!= DTYPE_VNODE
) {
3523 *resultvp
= (struct vnode
*)fp
->f_data
;
3533 * Description: Get fileproc, vnode pointer, and vid for a given fd from the
3534 * per process open file table of the specified process, and if
3535 * successful, increment the f_iocount
3537 * Parameters: p Process in which fd lives
3538 * fd fd to get information for
3539 * resultfp Pointer to result fileproc
3540 * pointer area, or 0 if none
3541 * resultvp Pointer to result vnode pointer
3542 * area, or 0 if none
3543 * vidp Pointer to resuld vid area
3545 * Returns: 0 Success
3546 * EBADF Bad file descriptor
3547 * ENOTSUP fd does not refer to a vnode
3550 * *resultfp (modified) Fileproc pointer
3551 * *resultvp (modified) vnode pointer
3554 * Notes: The resultfp and resultvp fields are optional, and may be
3555 * independently specified as NULL to skip returning information
3557 * Locks: Internally takes and releases proc_fdlock
3560 fp_getfvpandvid(proc_t p
, int fd
, struct fileproc
**resultfp
,
3561 struct vnode
**resultvp
, uint32_t *vidp
)
3563 struct filedesc
*fdp
= p
->p_fd
;
3564 struct fileproc
*fp
;
3566 proc_fdlock_spin(p
);
3567 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3568 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3569 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3573 if (fp
->f_type
!= DTYPE_VNODE
) {
3582 *resultvp
= (struct vnode
*)fp
->f_data
;
3584 *vidp
= (uint32_t)vnode_vid((struct vnode
*)fp
->f_data
);
3594 * Description: Get fileproc and socket pointer for a given fd from the
3595 * per process open file table of the specified process, and if
3596 * successful, increment the f_iocount
3598 * Parameters: p Process in which fd lives
3599 * fd fd to get information for
3600 * resultfp Pointer to result fileproc
3601 * pointer area, or 0 if none
3602 * results Pointer to result socket
3603 * pointer area, or 0 if none
3605 * Returns: EBADF The file descriptor is invalid
3606 * EOPNOTSUPP The file descriptor is not a socket
3610 * *resultfp (modified) Fileproc pointer
3611 * *results (modified) socket pointer
3613 * Notes: EOPNOTSUPP should probably be ENOTSOCK; this function is only
3614 * ever called from accept1().
3617 fp_getfsock(proc_t p
, int fd
, struct fileproc
**resultfp
,
3618 struct socket
**results
)
3620 struct filedesc
*fdp
= p
->p_fd
;
3621 struct fileproc
*fp
;
3623 proc_fdlock_spin(p
);
3624 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3625 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3626 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3630 if (fp
->f_type
!= DTYPE_SOCKET
) {
3639 *results
= (struct socket
*)fp
->f_data
;
3649 * Description: Get fileproc and kqueue pointer for a given fd from the
3650 * per process open file table of the specified process, and if
3651 * successful, increment the f_iocount
3653 * Parameters: p Process in which fd lives
3654 * fd fd to get information for
3655 * resultfp Pointer to result fileproc
3656 * pointer area, or 0 if none
3657 * resultkq Pointer to result kqueue
3658 * pointer area, or 0 if none
3660 * Returns: EBADF The file descriptor is invalid
3661 * EBADF The file descriptor is not a socket
3665 * *resultfp (modified) Fileproc pointer
3666 * *resultkq (modified) kqueue pointer
3668 * Notes: The second EBADF should probably be something else to make
3669 * the error condition distinct.
3672 fp_getfkq(proc_t p
, int fd
, struct fileproc
**resultfp
,
3673 struct kqueue
**resultkq
)
3675 struct filedesc
*fdp
= p
->p_fd
;
3676 struct fileproc
*fp
;
3678 proc_fdlock_spin(p
);
3679 if ( fd
< 0 || fd
>= fdp
->fd_nfiles
||
3680 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3681 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3685 if (fp
->f_type
!= DTYPE_KQUEUE
) {
3694 *resultkq
= (struct kqueue
*)fp
->f_data
;
3704 * Description: Get fileproc and POSIX shared memory pointer for a given fd
3705 * from the per process open file table of the specified process
3706 * and if successful, increment the f_iocount
3708 * Parameters: p Process in which fd lives
3709 * fd fd to get information for
3710 * resultfp Pointer to result fileproc
3711 * pointer area, or 0 if none
3712 * resultpshm Pointer to result POSIX
3713 * shared memory pointer
3714 * pointer area, or 0 if none
3716 * Returns: EBADF The file descriptor is invalid
3717 * EBADF The file descriptor is not a POSIX
3718 * shared memory area
3722 * *resultfp (modified) Fileproc pointer
3723 * *resultpshm (modified) POSIX shared memory pointer
3725 * Notes: The second EBADF should probably be something else to make
3726 * the error condition distinct.
3729 fp_getfpshm(proc_t p
, int fd
, struct fileproc
**resultfp
,
3730 struct pshmnode
**resultpshm
)
3732 struct filedesc
*fdp
= p
->p_fd
;
3733 struct fileproc
*fp
;
3735 proc_fdlock_spin(p
);
3736 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3737 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3738 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3742 if (fp
->f_type
!= DTYPE_PSXSHM
) {
3752 *resultpshm
= (struct pshmnode
*)fp
->f_data
;
3762 * Description: Get fileproc and POSIX semaphore pointer for a given fd from
3763 * the per process open file table of the specified process
3764 * and if successful, increment the f_iocount
3766 * Parameters: p Process in which fd lives
3767 * fd fd to get information for
3768 * resultfp Pointer to result fileproc
3769 * pointer area, or 0 if none
3770 * resultpsem Pointer to result POSIX
3771 * semaphore pointer area, or
3774 * Returns: EBADF The file descriptor is invalid
3775 * EBADF The file descriptor is not a POSIX
3780 * *resultfp (modified) Fileproc pointer
3781 * *resultpsem (modified) POSIX semaphore pointer
3783 * Notes: The second EBADF should probably be something else to make
3784 * the error condition distinct.
3786 * In order to support unnamed POSIX semaphores, the named
3787 * POSIX semaphores will have to move out of the per-process
3788 * open filetable, and into a global table that is shared with
3789 * unnamed POSIX semaphores, since unnamed POSIX semaphores
3790 * are typically used by declaring instances in shared memory,
3791 * and there's no other way to do this without changing the
3792 * underlying type, which would introduce binary compatibility
3796 fp_getfpsem(proc_t p
, int fd
, struct fileproc
**resultfp
,
3797 struct psemnode
**resultpsem
)
3799 struct filedesc
*fdp
= p
->p_fd
;
3800 struct fileproc
*fp
;
3802 proc_fdlock_spin(p
);
3803 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3804 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3805 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3809 if (fp
->f_type
!= DTYPE_PSXSEM
) {
3818 *resultpsem
= (struct psemnode
*)fp
->f_data
;
3828 * Description: Get fileproc and pipe pointer for a given fd from the
3829 * per process open file table of the specified process
3830 * and if successful, increment the f_iocount
3832 * Parameters: p Process in which fd lives
3833 * fd fd to get information for
3834 * resultfp Pointer to result fileproc
3835 * pointer area, or 0 if none
3836 * resultpipe Pointer to result pipe
3837 * pointer area, or 0 if none
3839 * Returns: EBADF The file descriptor is invalid
3840 * EBADF The file descriptor is not a socket
3844 * *resultfp (modified) Fileproc pointer
3845 * *resultpipe (modified) pipe pointer
3847 * Notes: The second EBADF should probably be something else to make
3848 * the error condition distinct.
3851 fp_getfpipe(proc_t p
, int fd
, struct fileproc
**resultfp
,
3852 struct pipe
**resultpipe
)
3854 struct filedesc
*fdp
= p
->p_fd
;
3855 struct fileproc
*fp
;
3857 proc_fdlock_spin(p
);
3858 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3859 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3860 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3864 if (fp
->f_type
!= DTYPE_PIPE
) {
3873 *resultpipe
= (struct pipe
*)fp
->f_data
;
3883 * Description: Get fileproc pointer for a given fd from the per process
3884 * open file table of the specified process and if successful,
3885 * increment the f_iocount
3887 * Parameters: p Process in which fd lives
3888 * fd fd to get information for
3889 * resultfp Pointer to result fileproc
3890 * pointer area, or 0 if none
3891 * locked !0 if the caller holds the
3892 * proc_fdlock, 0 otherwise
3894 * Returns: 0 Success
3895 * EBADF Bad file descriptor
3898 * *resultfp (modified) Fileproc pointer
3900 * Locks: If the argument 'locked' is non-zero, then the caller is
3901 * expected to have taken and held the proc_fdlock; if it is
3902 * zero, than this routine internally takes and drops this lock.
3905 fp_lookup(proc_t p
, int fd
, struct fileproc
**resultfp
, int locked
)
3907 struct filedesc
*fdp
= p
->p_fd
;
3908 struct fileproc
*fp
;
3911 proc_fdlock_spin(p
);
3912 if (fd
< 0 || fdp
== NULL
|| fd
>= fdp
->fd_nfiles
||
3913 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3914 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3933 * Description: Swap the fileproc pointer for a given fd with a new
3934 * fileproc pointer in the per-process open file table of
3935 * the specified process. The fdlock must be held at entry.
3937 * Parameters: p Process containing the fd
3938 * fd The fd of interest
3939 * nfp Pointer to the newfp
3941 * Returns: 0 Success
3942 * EBADF Bad file descriptor
3944 * EKEEPLOOKING f_iocount changed while lock was dropped.
3947 fp_tryswap(proc_t p
, int fd
, struct fileproc
*nfp
)
3949 struct fileproc
*fp
;
3952 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
3954 if (0 != (error
= fp_lookup(p
, fd
, &fp
, 1)))
3957 * At this point, our caller (change_guardedfd_np) has
3958 * one f_iocount reference, and we just took another
3959 * one to begin the replacement.
3961 if (fp
->f_iocount
< 2) {
3962 panic("f_iocount too small %d", fp
->f_iocount
);
3963 } else if (2 == fp
->f_iocount
) {
3965 /* Copy the contents of *fp, preserving the "type" of *nfp */
3967 nfp
->f_flags
= (nfp
->f_flags
& FP_TYPEMASK
) |
3968 (fp
->f_flags
& ~FP_TYPEMASK
);
3969 nfp
->f_iocount
= fp
->f_iocount
;
3970 nfp
->f_fglob
= fp
->f_fglob
;
3971 nfp
->f_wset
= fp
->f_wset
;
3973 p
->p_fd
->fd_ofiles
[fd
] = nfp
;
3974 (void) fp_drop(p
, fd
, nfp
, 1);
3977 * Wait for all other active references to evaporate.
3979 p
->p_fpdrainwait
= 1;
3980 error
= msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
,
3981 PRIBIO
| PCATCH
, "tryswap fpdrain", NULL
);
3984 * Return an "internal" errno to trigger a full
3985 * reevaluation of the change-guard attempt.
3987 error
= EKEEPLOOKING
;
3988 printf("%s: lookup collision fd %d\n", __func__
, fd
);
3990 (void) fp_drop(p
, fd
, fp
, 1);
3999 * Description: Set the FP_WRITTEN flag on the fileproc and drop the I/O
4000 * reference previously taken by calling fp_lookup et. al.
4002 * Parameters: p Process in which the fd lives
4003 * fd fd associated with the fileproc
4004 * fp fileproc on which to set the
4005 * flag and drop the reference
4007 * Returns: 0 Success
4008 * fp_drop:EBADF Bad file descriptor
4010 * Locks: This function internally takes and drops the proc_fdlock for
4011 * the supplied process
4013 * Notes: The fileproc must correspond to the fd in the supplied proc
4016 fp_drop_written(proc_t p
, int fd
, struct fileproc
*fp
)
4020 proc_fdlock_spin(p
);
4022 fp
->f_flags
|= FP_WRITTEN
;
4024 error
= fp_drop(p
, fd
, fp
, 1);
4035 * Description: Set the FP_WAITEVENT flag on the fileproc and drop the I/O
4036 * reference previously taken by calling fp_lookup et. al.
4038 * Parameters: p Process in which the fd lives
4039 * fd fd associated with the fileproc
4040 * fp fileproc on which to set the
4041 * flag and drop the reference
4043 * Returns: 0 Success
4044 * fp_drop:EBADF Bad file descriptor
4046 * Locks: This function internally takes and drops the proc_fdlock for
4047 * the supplied process
4049 * Notes: The fileproc must correspond to the fd in the supplied proc
4052 fp_drop_event(proc_t p
, int fd
, struct fileproc
*fp
)
4056 proc_fdlock_spin(p
);
4058 fp
->f_flags
|= FP_WAITEVENT
;
4060 error
= fp_drop(p
, fd
, fp
, 1);
4071 * Description: Drop the I/O reference previously taken by calling fp_lookup
4074 * Parameters: p Process in which the fd lives
4075 * fd fd associated with the fileproc
4076 * fp fileproc on which to set the
4077 * flag and drop the reference
4078 * locked flag to internally take and
4079 * drop proc_fdlock if it is not
4080 * already held by the caller
4082 * Returns: 0 Success
4083 * EBADF Bad file descriptor
4085 * Locks: This function internally takes and drops the proc_fdlock for
4086 * the supplied process if 'locked' is non-zero, and assumes that
4087 * the caller already holds this lock if 'locked' is non-zero.
4089 * Notes: The fileproc must correspond to the fd in the supplied proc
4092 fp_drop(proc_t p
, int fd
, struct fileproc
*fp
, int locked
)
4094 struct filedesc
*fdp
= p
->p_fd
;
4098 proc_fdlock_spin(p
);
4099 if ((fp
== FILEPROC_NULL
) && (fd
< 0 || fd
>= fdp
->fd_nfiles
||
4100 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
4101 ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
4102 !(fdp
->fd_ofileflags
[fd
] & UF_CLOSING
)))) {
4109 if (fp
->f_iocount
== 0) {
4110 if (fp
->f_flags
& FP_SELCONFLICT
)
4111 fp
->f_flags
&= ~FP_SELCONFLICT
;
4113 if (p
->p_fpdrainwait
) {
4114 p
->p_fpdrainwait
= 0;
4121 wakeup(&p
->p_fpdrainwait
);
4130 * Description: Given an fd, look it up in the current process's per process
4131 * open file table, and return its internal vnode pointer.
4133 * Parameters: fd fd to obtain vnode from
4134 * vpp pointer to vnode return area
4136 * Returns: 0 Success
4137 * EINVAL The fd does not refer to a
4138 * vnode fileproc entry
4139 * fp_lookup:EBADF Bad file descriptor
4142 * *vpp (modified) Returned vnode pointer
4144 * Locks: This function internally takes and drops the proc_fdlock for
4145 * the current process
4147 * Notes: If successful, this function increments the f_iocount on the
4148 * fd's corresponding fileproc.
4150 * The fileproc referenced is not returned; because of this, care
4151 * must be taken to not drop the last reference (e.g. by closing
4152 * the file). This is inherently unsafe, since the reference may
4153 * not be recoverable from the vnode, if there is a subsequent
4154 * close that destroys the associate fileproc. The caller should
4155 * therefore retain their own reference on the fileproc so that
4156 * the f_iocount can be dropped subsequently. Failure to do this
4157 * can result in the returned pointer immediately becoming invalid
4158 * following the call.
4160 * Use of this function is discouraged.
4163 file_vnode(int fd
, struct vnode
**vpp
)
4165 proc_t p
= current_proc();
4166 struct fileproc
*fp
;
4169 proc_fdlock_spin(p
);
4170 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4174 if (fp
->f_type
!= DTYPE_VNODE
) {
4175 fp_drop(p
, fd
, fp
,1);
4180 *vpp
= (struct vnode
*)fp
->f_data
;
4188 * file_vnode_withvid
4190 * Description: Given an fd, look it up in the current process's per process
4191 * open file table, and return its internal vnode pointer.
4193 * Parameters: fd fd to obtain vnode from
4194 * vpp pointer to vnode return area
4195 * vidp pointer to vid of the returned vnode
4197 * Returns: 0 Success
4198 * EINVAL The fd does not refer to a
4199 * vnode fileproc entry
4200 * fp_lookup:EBADF Bad file descriptor
4203 * *vpp (modified) Returned vnode pointer
4205 * Locks: This function internally takes and drops the proc_fdlock for
4206 * the current process
4208 * Notes: If successful, this function increments the f_iocount on the
4209 * fd's corresponding fileproc.
4211 * The fileproc referenced is not returned; because of this, care
4212 * must be taken to not drop the last reference (e.g. by closing
4213 * the file). This is inherently unsafe, since the reference may
4214 * not be recoverable from the vnode, if there is a subsequent
4215 * close that destroys the associate fileproc. The caller should
4216 * therefore retain their own reference on the fileproc so that
4217 * the f_iocount can be dropped subsequently. Failure to do this
4218 * can result in the returned pointer immediately becoming invalid
4219 * following the call.
4221 * Use of this function is discouraged.
4224 file_vnode_withvid(int fd
, struct vnode
**vpp
, uint32_t * vidp
)
4226 proc_t p
= current_proc();
4227 struct fileproc
*fp
;
4231 proc_fdlock_spin(p
);
4232 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4236 if (fp
->f_type
!= DTYPE_VNODE
) {
4237 fp_drop(p
, fd
, fp
,1);
4241 vp
= (struct vnode
*)fp
->f_data
;
4245 if ((vidp
!= NULL
) && (vp
!= NULLVP
))
4246 *vidp
= (uint32_t)vp
->v_id
;
4257 * Description: Given an fd, look it up in the current process's per process
4258 * open file table, and return its internal socket pointer.
4260 * Parameters: fd fd to obtain vnode from
4261 * sp pointer to socket return area
4263 * Returns: 0 Success
4264 * ENOTSOCK Not a socket
4265 * fp_lookup:EBADF Bad file descriptor
4268 * *sp (modified) Returned socket pointer
4270 * Locks: This function internally takes and drops the proc_fdlock for
4271 * the current process
4273 * Notes: If successful, this function increments the f_iocount on the
4274 * fd's corresponding fileproc.
4276 * The fileproc referenced is not returned; because of this, care
4277 * must be taken to not drop the last reference (e.g. by closing
4278 * the file). This is inherently unsafe, since the reference may
4279 * not be recoverable from the socket, if there is a subsequent
4280 * close that destroys the associate fileproc. The caller should
4281 * therefore retain their own reference on the fileproc so that
4282 * the f_iocount can be dropped subsequently. Failure to do this
4283 * can result in the returned pointer immediately becoming invalid
4284 * following the call.
4286 * Use of this function is discouraged.
4289 file_socket(int fd
, struct socket
**sp
)
4291 proc_t p
= current_proc();
4292 struct fileproc
*fp
;
4295 proc_fdlock_spin(p
);
4296 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4300 if (fp
->f_type
!= DTYPE_SOCKET
) {
4301 fp_drop(p
, fd
, fp
,1);
4305 *sp
= (struct socket
*)fp
->f_data
;
4315 * Description: Given an fd, look it up in the current process's per process
4316 * open file table, and return its fileproc's flags field.
4318 * Parameters: fd fd whose flags are to be
4320 * flags pointer to flags data area
4322 * Returns: 0 Success
4323 * ENOTSOCK Not a socket
4324 * fp_lookup:EBADF Bad file descriptor
4327 * *flags (modified) Returned flags field
4329 * Locks: This function internally takes and drops the proc_fdlock for
4330 * the current process
4332 * Notes: This function will internally increment and decrement the
4333 * f_iocount of the fileproc as part of its operation.
4336 file_flags(int fd
, int *flags
)
4339 proc_t p
= current_proc();
4340 struct fileproc
*fp
;
4343 proc_fdlock_spin(p
);
4344 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4348 *flags
= (int)fp
->f_flag
;
4349 fp_drop(p
, fd
, fp
,1);
4359 * Description: Drop an iocount reference on an fd, and wake up any waiters
4360 * for draining (i.e. blocked in fileproc_drain() called during
4361 * the last attempt to close a file).
4363 * Parameters: fd fd on which an ioreference is
4366 * Returns: 0 Success
4367 * EBADF Bad file descriptor
4369 * Description: Given an fd, look it up in the current process's per process
4370 * open file table, and drop it's fileproc's f_iocount by one
4372 * Notes: This is intended as a corresponding operation to the functions
4373 * file_vnode() and file_socket() operations.
4375 * Technically, the close reference is supposed to be protected
4376 * by a fileproc_drain(), however, a drain will only block if
4377 * the fd refers to a character device, and that device has had
4378 * preparefileread() called on it. If it refers to something
4379 * other than a character device, then the drain will occur and
4380 * block each close attempt, rather than merely the last close.
4382 * Since it's possible for an fd that refers to a character
4383 * device to have an intermediate close followed by an open to
4384 * cause a different file to correspond to that descriptor,
4385 * unless there was a cautionary reference taken on the fileproc,
4386 * this is an inherently unsafe function. This happens in the
4387 * case where multiple fd's in a process refer to the same
4388 * character device (e.g. stdin/out/err pointing to a tty, etc.).
4390 * Use of this function is discouraged.
4395 struct fileproc
*fp
;
4396 proc_t p
= current_proc();
4399 proc_fdlock_spin(p
);
4400 if (fd
< 0 || fd
>= p
->p_fd
->fd_nfiles
||
4401 (fp
= p
->p_fd
->fd_ofiles
[fd
]) == NULL
||
4402 ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
4403 !(p
->p_fd
->fd_ofileflags
[fd
] & UF_CLOSING
))) {
4409 if (fp
->f_iocount
== 0) {
4410 if (fp
->f_flags
& FP_SELCONFLICT
)
4411 fp
->f_flags
&= ~FP_SELCONFLICT
;
4413 if (p
->p_fpdrainwait
) {
4414 p
->p_fpdrainwait
= 0;
4421 wakeup(&p
->p_fpdrainwait
);
4426 static int falloc_withalloc_locked(proc_t
, struct fileproc
**, int *,
4427 vfs_context_t
, struct fileproc
* (*)(void *), void *, int);
4432 * Description: Allocate an entry in the per process open file table and
4433 * return the corresponding fileproc and fd.
4435 * Parameters: p The process in whose open file
4436 * table the fd is to be allocated
4437 * resultfp Pointer to fileproc pointer
4439 * resultfd Pointer to fd return area
4442 * Returns: 0 Success
4443 * falloc:ENFILE Too many open files in system
4444 * falloc:EMFILE Too many open files in process
4445 * falloc:ENOMEM M_FILEPROC or M_FILEGLOB zone
4449 * *resultfd (modified) Returned fileproc pointer
4450 * *resultfd (modified) Returned fd
4452 * Locks: This function takes and drops the proc_fdlock; if this lock
4453 * is already held, use falloc_locked() instead.
4455 * Notes: This function takes separate process and context arguments
4456 * solely to support kern_exec.c; otherwise, it would take
4457 * neither, and expect falloc_locked() to use the
4458 * vfs_context_current() routine internally.
4461 falloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
)
4463 return (falloc_withalloc(p
, resultfp
, resultfd
, ctx
,
4464 fileproc_alloc_init
, NULL
));
4468 * Like falloc, but including the fileproc allocator and create-args
4471 falloc_withalloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4472 vfs_context_t ctx
, fp_allocfn_t fp_zalloc
, void *arg
)
4477 error
= falloc_withalloc_locked(p
,
4478 resultfp
, resultfd
, ctx
, fp_zalloc
, arg
, 1);
4485 * "uninitialized" ops -- ensure fg->fg_ops->fo_type always exists
4487 static const struct fileops uninitops
;
4492 * Create a new open file structure and allocate
4493 * a file descriptor for the process that refers to it.
4495 * Returns: 0 Success
4497 * Description: Allocate an entry in the per process open file table and
4498 * return the corresponding fileproc and fd.
4500 * Parameters: p The process in whose open file
4501 * table the fd is to be allocated
4502 * resultfp Pointer to fileproc pointer
4504 * resultfd Pointer to fd return area
4506 * locked Flag to indicate whether the
4507 * caller holds proc_fdlock
4509 * Returns: 0 Success
4510 * ENFILE Too many open files in system
4511 * fdalloc:EMFILE Too many open files in process
4512 * ENOMEM M_FILEPROC or M_FILEGLOB zone
4517 * *resultfd (modified) Returned fileproc pointer
4518 * *resultfd (modified) Returned fd
4520 * Locks: If the parameter 'locked' is zero, this function takes and
4521 * drops the proc_fdlock; if non-zero, the caller must hold the
4524 * Notes: If you intend to use a non-zero 'locked' parameter, use the
4525 * utility function falloc() instead.
4527 * This function takes separate process and context arguments
4528 * solely to support kern_exec.c; otherwise, it would take
4529 * neither, and use the vfs_context_current() routine internally.
4532 falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4533 vfs_context_t ctx
, int locked
)
4535 return (falloc_withalloc_locked(p
, resultfp
, resultfd
, ctx
,
4536 fileproc_alloc_init
, NULL
, locked
));
4540 falloc_withalloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4541 vfs_context_t ctx
, fp_allocfn_t fp_zalloc
, void *crarg
,
4544 struct fileproc
*fp
;
4545 struct fileglob
*fg
;
4550 if ( (error
= fdalloc(p
, 0, &nfd
)) ) {
4555 if (nfiles
>= maxfiles
) {
4562 error
= mac_file_check_create(proc_ucred(p
));
4571 * Allocate a new file descriptor.
4572 * If the process has file descriptor zero open, add to the list
4573 * of open files at that point, otherwise put it at the front of
4574 * the list of open files.
4578 fp
= (*fp_zalloc
)(crarg
);
4584 MALLOC_ZONE(fg
, struct fileglob
*, sizeof(struct fileglob
), M_FILEGLOB
, M_WAITOK
);
4591 bzero(fg
, sizeof(struct fileglob
));
4592 lck_mtx_init(&fg
->fg_lock
, file_lck_grp
, file_lck_attr
);
4596 fg
->fg_ops
= &uninitops
;
4599 mac_file_label_init(fg
);
4602 kauth_cred_ref(ctx
->vc_ucred
);
4606 fp
->f_cred
= ctx
->vc_ucred
;
4609 mac_file_label_associate(fp
->f_cred
, fg
);
4612 OSAddAtomic(1, &nfiles
);
4614 p
->p_fd
->fd_ofiles
[nfd
] = fp
;
4631 * Description: Free a file structure; drop the global open file count, and
4632 * drop the credential reference, if the fileglob has one, and
4633 * destroy the instance mutex before freeing
4635 * Parameters: fg Pointer to fileglob to be
4641 fg_free(struct fileglob
*fg
)
4643 OSAddAtomic(-1, &nfiles
);
4645 if (fg
->fg_vn_data
) {
4646 fg_vn_data_free(fg
->fg_vn_data
);
4647 fg
->fg_vn_data
= NULL
;
4650 if (IS_VALID_CRED(fg
->fg_cred
)) {
4651 kauth_cred_unref(&fg
->fg_cred
);
4653 lck_mtx_destroy(&fg
->fg_lock
, file_lck_grp
);
4656 mac_file_label_destroy(fg
);
4658 FREE_ZONE(fg
, sizeof *fg
, M_FILEGLOB
);
4666 * Description: Perform close-on-exec processing for all files in a process
4667 * that are either marked as close-on-exec, or which were in the
4668 * process of being opened at the time of the execve
4670 * Also handles the case (via posix_spawn()) where -all-
4671 * files except those marked with "inherit" as treated as
4674 * Parameters: p Pointer to process calling
4679 * Locks: This function internally takes and drops proc_fdlock()
4680 * But assumes tables don't grow/change while unlocked.
4684 fdexec(proc_t p
, short flags
, int self_exec
)
4686 struct filedesc
*fdp
= p
->p_fd
;
4688 boolean_t cloexec_default
= (flags
& POSIX_SPAWN_CLOEXEC_DEFAULT
) != 0;
4689 thread_t self
= current_thread();
4690 struct uthread
*ut
= get_bsdthread_info(self
);
4691 struct kqueue
*dealloc_kq
= NULL
;
4694 * If the current thread is bound as a workq/workloop
4695 * servicing thread, we need to unbind it first.
4697 if (ut
->uu_kqueue_bound
&& self_exec
) {
4698 kevent_qos_internal_unbind(p
, 0, self
,
4699 ut
->uu_kqueue_flags
);
4705 * Deallocate the knotes for this process
4706 * and mark the tables non-existent so
4707 * subsequent kqueue closes go faster.
4710 assert(fdp
->fd_knlistsize
== -1);
4711 assert(fdp
->fd_knhashmask
== 0);
4713 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
4715 struct fileproc
*fp
= fdp
->fd_ofiles
[i
];
4716 char *flagp
= &fdp
->fd_ofileflags
[i
];
4718 if (fp
&& cloexec_default
) {
4720 * Reverse the usual semantics of file descriptor
4721 * inheritance - all of them should be closed
4722 * except files marked explicitly as "inherit" and
4723 * not marked close-on-exec.
4725 if ((*flagp
& (UF_EXCLOSE
|UF_INHERIT
)) != UF_INHERIT
)
4726 *flagp
|= UF_EXCLOSE
;
4727 *flagp
&= ~UF_INHERIT
;
4731 ((*flagp
& (UF_RESERVED
|UF_EXCLOSE
)) == UF_EXCLOSE
)
4733 || (fp
&& mac_file_check_inherit(proc_ucred(p
), fp
->f_fglob
))
4736 procfdtbl_clearfd(p
, i
);
4737 if (i
== fdp
->fd_lastfile
&& i
> 0)
4739 if (i
< fdp
->fd_freefile
)
4740 fdp
->fd_freefile
= i
;
4743 * Wait for any third party viewers (e.g., lsof)
4744 * to release their references to this fileproc.
4746 while (fp
->f_iocount
> 0) {
4747 p
->p_fpdrainwait
= 1;
4748 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
,
4752 closef_locked(fp
, fp
->f_fglob
, p
);
4758 /* release the per-process workq kq */
4759 if (fdp
->fd_wqkqueue
) {
4760 dealloc_kq
= fdp
->fd_wqkqueue
;
4761 fdp
->fd_wqkqueue
= NULL
;
4766 /* Anything to free? */
4768 kqueue_dealloc(dealloc_kq
);
4775 * Description: Copy a filedesc structure. This is normally used as part of
4776 * forkproc() when forking a new process, to copy the per process
4777 * open file table over to the new process.
4779 * Parameters: p Process whose open file table
4780 * is to be copied (parent)
4781 * uth_cdir Per thread current working
4782 * cirectory, or NULL
4784 * Returns: NULL Copy failed
4785 * !NULL Pointer to new struct filedesc
4787 * Locks: This function internally takes and drops proc_fdlock()
4789 * Notes: Files are copied directly, ignoring the new resource limits
4790 * for the process that's being copied into. Since the descriptor
4791 * references are just additional references, this does not count
4792 * against the number of open files on the system.
4794 * The struct filedesc includes the current working directory,
4795 * and the current root directory, if the process is chroot'ed.
4797 * If the exec was called by a thread using a per thread current
4798 * working directory, we inherit the working directory from the
4799 * thread making the call, rather than from the process.
4801 * In the case of a failure to obtain a reference, for most cases,
4802 * the file entry will be silently dropped. There's an exception
4803 * for the case of a chroot dir, since a failure to to obtain a
4804 * reference there would constitute an "escape" from the chroot
4805 * environment, which must not be allowed. In that case, we will
4806 * deny the execve() operation, rather than allowing the escape.
4809 fdcopy(proc_t p
, vnode_t uth_cdir
)
4811 struct filedesc
*newfdp
, *fdp
= p
->p_fd
;
4813 struct fileproc
*ofp
, *fp
;
4816 MALLOC_ZONE(newfdp
, struct filedesc
*,
4817 sizeof(*newfdp
), M_FILEDESC
, M_WAITOK
);
4824 * the FD_CHROOT flag will be inherited via this copy
4826 (void) memcpy(newfdp
, fdp
, sizeof(*newfdp
));
4829 * If we are running with per-thread current working directories,
4830 * inherit the new current working directory from the current thread
4831 * instead, before we take our references.
4833 if (uth_cdir
!= NULLVP
)
4834 newfdp
->fd_cdir
= uth_cdir
;
4837 * For both fd_cdir and fd_rdir make sure we get
4838 * a valid reference... if we can't, than set
4839 * set the pointer(s) to NULL in the child... this
4840 * will keep us from using a non-referenced vp
4841 * and allows us to do the vnode_rele only on
4842 * a properly referenced vp
4844 if ( (v_dir
= newfdp
->fd_cdir
) ) {
4845 if (vnode_getwithref(v_dir
) == 0) {
4846 if ( (vnode_ref(v_dir
)) )
4847 newfdp
->fd_cdir
= NULL
;
4850 newfdp
->fd_cdir
= NULL
;
4852 if (newfdp
->fd_cdir
== NULL
&& fdp
->fd_cdir
) {
4854 * we couldn't get a new reference on
4855 * the current working directory being
4856 * inherited... we might as well drop
4857 * our reference from the parent also
4858 * since the vnode has gone DEAD making
4859 * it useless... by dropping it we'll
4860 * be that much closer to recycling it
4862 vnode_rele(fdp
->fd_cdir
);
4863 fdp
->fd_cdir
= NULL
;
4866 if ( (v_dir
= newfdp
->fd_rdir
) ) {
4867 if (vnode_getwithref(v_dir
) == 0) {
4868 if ( (vnode_ref(v_dir
)) )
4869 newfdp
->fd_rdir
= NULL
;
4872 newfdp
->fd_rdir
= NULL
;
4875 /* Coming from a chroot environment and unable to get a reference... */
4876 if (newfdp
->fd_rdir
== NULL
&& fdp
->fd_rdir
) {
4878 * We couldn't get a new reference on
4879 * the chroot directory being
4880 * inherited... this is fatal, since
4881 * otherwise it would constitute an
4882 * escape from a chroot environment by
4885 if (newfdp
->fd_cdir
)
4886 vnode_rele(newfdp
->fd_cdir
);
4887 FREE_ZONE(newfdp
, sizeof *newfdp
, M_FILEDESC
);
4892 * If the number of open files fits in the internal arrays
4893 * of the open file structure, use them, otherwise allocate
4894 * additional memory for the number of descriptors currently
4897 if (newfdp
->fd_lastfile
< NDFILE
)
4901 * Compute the smallest multiple of NDEXTENT needed
4902 * for the file descriptors currently in use,
4903 * allowing the table to shrink.
4905 i
= newfdp
->fd_nfiles
;
4906 while (i
> 1 + 2 * NDEXTENT
&& i
> 1 + newfdp
->fd_lastfile
* 2)
4911 MALLOC_ZONE(newfdp
->fd_ofiles
, struct fileproc
**,
4912 i
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
4913 if (newfdp
->fd_ofiles
== NULL
) {
4914 if (newfdp
->fd_cdir
)
4915 vnode_rele(newfdp
->fd_cdir
);
4916 if (newfdp
->fd_rdir
)
4917 vnode_rele(newfdp
->fd_rdir
);
4919 FREE_ZONE(newfdp
, sizeof(*newfdp
), M_FILEDESC
);
4922 (void) memset(newfdp
->fd_ofiles
, 0, i
* OFILESIZE
);
4925 newfdp
->fd_ofileflags
= (char *) &newfdp
->fd_ofiles
[i
];
4926 newfdp
->fd_nfiles
= i
;
4928 if (fdp
->fd_nfiles
> 0) {
4929 struct fileproc
**fpp
;
4932 (void) memcpy(newfdp
->fd_ofiles
, fdp
->fd_ofiles
,
4933 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofiles
));
4934 (void) memcpy(newfdp
->fd_ofileflags
, fdp
->fd_ofileflags
,
4935 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofileflags
));
4938 * kq descriptors cannot be copied.
4940 if (newfdp
->fd_knlistsize
!= -1) {
4941 fpp
= &newfdp
->fd_ofiles
[newfdp
->fd_lastfile
];
4942 flags
= &newfdp
->fd_ofileflags
[newfdp
->fd_lastfile
];
4943 for (i
= newfdp
->fd_lastfile
;
4944 i
>= 0; i
--, fpp
--, flags
--) {
4945 if (*flags
& UF_RESERVED
)
4946 continue; /* (removed below) */
4947 if (*fpp
!= NULL
&& (*fpp
)->f_type
== DTYPE_KQUEUE
) {
4950 if (i
< newfdp
->fd_freefile
)
4951 newfdp
->fd_freefile
= i
;
4953 if (*fpp
== NULL
&& i
== newfdp
->fd_lastfile
&& i
> 0)
4954 newfdp
->fd_lastfile
--;
4957 fpp
= newfdp
->fd_ofiles
;
4958 flags
= newfdp
->fd_ofileflags
;
4960 for (i
= newfdp
->fd_lastfile
+ 1; --i
>= 0; fpp
++, flags
++)
4961 if ((ofp
= *fpp
) != NULL
&&
4962 0 == (ofp
->f_fglob
->fg_lflags
& FG_CONFINED
) &&
4963 0 == (*flags
& (UF_FORKCLOSE
|UF_RESERVED
))) {
4965 if (FILEPROC_TYPE(ofp
) != FTYPE_SIMPLE
)
4966 panic("complex fileproc");
4968 fp
= fileproc_alloc_init(NULL
);
4971 * XXX no room to copy, unable to
4972 * XXX safely unwind state at present
4977 (ofp
->f_flags
& ~FP_TYPEMASK
);
4978 fp
->f_fglob
= ofp
->f_fglob
;
4983 if (i
< newfdp
->fd_freefile
)
4984 newfdp
->fd_freefile
= i
;
4993 * Initialize knote and kqueue tracking structs
4995 newfdp
->fd_knlist
= NULL
;
4996 newfdp
->fd_knlistsize
= -1;
4997 newfdp
->fd_knhash
= NULL
;
4998 newfdp
->fd_knhashmask
= 0;
4999 newfdp
->fd_kqhash
= NULL
;
5000 newfdp
->fd_kqhashmask
= 0;
5001 newfdp
->fd_wqkqueue
= NULL
;
5002 lck_mtx_init(&newfdp
->fd_kqhashlock
, proc_kqhashlock_grp
, proc_lck_attr
);
5003 lck_mtx_init(&newfdp
->fd_knhashlock
, proc_knhashlock_grp
, proc_lck_attr
);
5012 * Description: Release a filedesc (per process open file table) structure;
5013 * this is done on process exit(), or from forkproc_free() if
5014 * the fork fails for some reason subsequent to a successful
5017 * Parameters: p Pointer to process going away
5021 * Locks: This function internally takes and drops proc_fdlock()
5026 struct filedesc
*fdp
;
5027 struct fileproc
*fp
;
5028 struct kqueue
*dealloc_kq
= NULL
;
5033 if (p
== kernproc
|| NULL
== (fdp
= p
->p_fd
)) {
5038 extern struct filedesc filedesc0
;
5040 if (&filedesc0
== fdp
)
5044 * deallocate all the knotes up front and claim empty
5045 * tables to make any subsequent kqueue closes faster.
5048 assert(fdp
->fd_knlistsize
== -1);
5049 assert(fdp
->fd_knhashmask
== 0);
5051 /* close file descriptors */
5052 if (fdp
->fd_nfiles
> 0 && fdp
->fd_ofiles
) {
5053 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
5054 if ((fp
= fdp
->fd_ofiles
[i
]) != NULL
) {
5056 if (fdp
->fd_ofileflags
[i
] & UF_RESERVED
)
5057 panic("fdfree: found fp with UF_RESERVED");
5059 procfdtbl_reservefd(p
, i
);
5061 if (fp
->f_flags
& FP_WAITEVENT
)
5062 (void)waitevent_close(p
, fp
);
5063 (void) closef_locked(fp
, fp
->f_fglob
, p
);
5067 FREE_ZONE(fdp
->fd_ofiles
, fdp
->fd_nfiles
* OFILESIZE
, M_OFILETABL
);
5068 fdp
->fd_ofiles
= NULL
;
5072 if (fdp
->fd_wqkqueue
) {
5073 dealloc_kq
= fdp
->fd_wqkqueue
;
5074 fdp
->fd_wqkqueue
= NULL
;
5080 kqueue_dealloc(dealloc_kq
);
5083 vnode_rele(fdp
->fd_cdir
);
5085 vnode_rele(fdp
->fd_rdir
);
5087 proc_fdlock_spin(p
);
5091 if (fdp
->fd_kqhash
) {
5092 for (uint32_t j
= 0; j
<= fdp
->fd_kqhashmask
; j
++)
5093 assert(SLIST_EMPTY(&fdp
->fd_kqhash
[j
]));
5094 FREE(fdp
->fd_kqhash
, M_KQUEUE
);
5097 lck_mtx_destroy(&fdp
->fd_kqhashlock
, proc_kqhashlock_grp
);
5098 lck_mtx_destroy(&fdp
->fd_knhashlock
, proc_knhashlock_grp
);
5100 FREE_ZONE(fdp
, sizeof(*fdp
), M_FILEDESC
);
5106 * Description: Internal form of closef; called with proc_fdlock held
5108 * Parameters: fp Pointer to fileproc for fd
5109 * fg Pointer to fileglob for fd
5110 * p Pointer to proc structure
5112 * Returns: 0 Success
5113 * closef_finish:??? Anything returnable by a per-fileops
5116 * Note: Decrements reference count on file structure; if this was the
5117 * last reference, then closef_finish() is called
5119 * p and fp are allowed to be NULL when closing a file that was
5120 * being passed in a message (but only if we are called when this
5121 * is NOT the last reference).
5124 closef_locked(struct fileproc
*fp
, struct fileglob
*fg
, proc_t p
)
5128 struct vfs_context context
;
5135 /* Set up context with cred stashed in fg */
5136 if (p
== current_proc())
5137 context
.vc_thread
= current_thread();
5139 context
.vc_thread
= NULL
;
5140 context
.vc_ucred
= fg
->fg_cred
;
5143 * POSIX record locking dictates that any close releases ALL
5144 * locks owned by this process. This is handled by setting
5145 * a flag in the unlock to free ONLY locks obeying POSIX
5146 * semantics, and not to free BSD-style file locks.
5147 * If the descriptor was in a message, POSIX-style locks
5148 * aren't passed with the descriptor.
5150 if (p
&& (p
->p_ladvflag
& P_LADVLOCK
) &&
5151 DTYPE_VNODE
== FILEGLOB_DTYPE(fg
)) {
5154 lf
.l_whence
= SEEK_SET
;
5157 lf
.l_type
= F_UNLCK
;
5158 vp
= (struct vnode
*)fg
->fg_data
;
5160 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
5161 (void) VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &lf
, F_POSIX
, &context
, NULL
);
5162 (void)vnode_put(vp
);
5166 lck_mtx_lock_spin(&fg
->fg_lock
);
5169 if (fg
->fg_count
> 0) {
5170 lck_mtx_unlock(&fg
->fg_lock
);
5174 if (fg
->fg_count
!= 0)
5175 panic("fg %p: being freed with bad fg_count (%d)", fg
, fg
->fg_count
);
5178 if (fp
&& (fp
->f_flags
& FP_WRITTEN
))
5179 fg
->fg_flag
|= FWASWRITTEN
;
5181 fg
->fg_lflags
|= FG_TERM
;
5182 lck_mtx_unlock(&fg
->fg_lock
);
5187 /* Since we ensure that fg->fg_ops is always initialized,
5188 * it is safe to invoke fo_close on the fg */
5189 error
= fo_close(fg
, &context
);
5203 * Description: Drain out pending I/O operations
5205 * Parameters: p Process closing this file
5206 * fp fileproc struct for the open
5207 * instance on the file
5211 * Locks: Assumes the caller holds the proc_fdlock
5213 * Notes: For character devices, this occurs on the last close of the
5214 * device; for all other file descriptors, this occurs on each
5215 * close to prevent fd's from being closed out from under
5216 * operations currently in progress and blocked
5218 * See Also: file_vnode(), file_socket(), file_drop(), and the cautions
5219 * regarding their use and interaction with this function.
5222 fileproc_drain(proc_t p
, struct fileproc
* fp
)
5224 struct vfs_context context
;
5226 context
.vc_thread
= proc_thread(p
); /* XXX */
5227 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
5229 fp
->f_iocount
-- ; /* (the one the close holds) */
5231 while (fp
->f_iocount
) {
5233 lck_mtx_convert_spin(&p
->p_fdmlock
);
5235 if (fp
->f_fglob
->fg_ops
->fo_drain
) {
5236 (*fp
->f_fglob
->fg_ops
->fo_drain
)(fp
, &context
);
5238 if ((fp
->f_flags
& FP_INSELECT
) == FP_INSELECT
) {
5239 if (waitq_wakeup64_all((struct waitq
*)fp
->f_wset
, NO_EVENT64
,
5240 THREAD_INTERRUPTED
, WAITQ_ALL_PRIORITIES
) == KERN_INVALID_ARGUMENT
)
5241 panic("bad wait queue for waitq_wakeup64_all %p (fp:%p)", fp
->f_wset
, fp
);
5243 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
) {
5244 if (waitq_wakeup64_all(&select_conflict_queue
, NO_EVENT64
,
5245 THREAD_INTERRUPTED
, WAITQ_ALL_PRIORITIES
) == KERN_INVALID_ARGUMENT
)
5246 panic("bad select_conflict_queue");
5248 p
->p_fpdrainwait
= 1;
5250 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
, "fpdrain", NULL
);
5254 if ((fp
->f_flags
& FP_INSELECT
) != 0)
5255 panic("FP_INSELECT set on drained fp");
5257 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
)
5258 fp
->f_flags
&= ~FP_SELCONFLICT
;
5265 * Description: Release the fd and free the fileproc associated with the fd
5266 * in the per process open file table of the specified process;
5267 * these values must correspond.
5269 * Parameters: p Process containing fd
5270 * fd fd to be released
5271 * fp fileproc to be freed
5273 * Returns: 0 Success
5275 * Notes: XXX function should be void - no one interprets the returns
5279 fp_free(proc_t p
, int fd
, struct fileproc
* fp
)
5281 proc_fdlock_spin(p
);
5285 fg_free(fp
->f_fglob
);
5294 * Description: Apply an advisory lock on a file descriptor.
5296 * Parameters: p Process making request
5297 * uap->fd fd on which the lock is to be
5299 * uap->how (Un)Lock bits, including type
5300 * retval Pointer to the call return area
5302 * Returns: 0 Success
5303 * fp_getfvp:EBADF Bad file descriptor
5304 * fp_getfvp:ENOTSUP fd does not refer to a vnode
5305 * vnode_getwithref:???
5309 * *retval (modified) Size of dtable
5311 * Notes: Just attempt to get a record lock of the requested type on
5312 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
5315 flock(proc_t p
, struct flock_args
*uap
, __unused
int32_t *retval
)
5319 struct fileproc
*fp
;
5322 vfs_context_t ctx
= vfs_context_current();
5325 AUDIT_ARG(fd
, uap
->fd
);
5326 if ( (error
= fp_getfvp(p
, fd
, &fp
, &vp
)) ) {
5329 if ( (error
= vnode_getwithref(vp
)) ) {
5332 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
5334 lf
.l_whence
= SEEK_SET
;
5337 if (how
& LOCK_UN
) {
5338 lf
.l_type
= F_UNLCK
;
5339 fp
->f_flag
&= ~FHASLOCK
;
5340 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_UNLCK
, &lf
, F_FLOCK
, ctx
, NULL
);
5344 lf
.l_type
= F_WRLCK
;
5345 else if (how
& LOCK_SH
)
5346 lf
.l_type
= F_RDLCK
;
5352 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
, F_SETLK
, &lf
);
5356 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
,
5357 (how
& LOCK_NB
? F_FLOCK
: F_FLOCK
| F_WAIT
),
5360 fp
->f_flag
|= FHASLOCK
;
5362 (void)vnode_put(vp
);
5364 fp_drop(p
, fd
, fp
, 0);
5372 * Description: Obtain a Mach send right for a given file descriptor.
5374 * Parameters: p Process calling fileport
5375 * uap->fd The fd to reference
5376 * uap->portnamep User address at which to place port name.
5378 * Returns: 0 Success.
5379 * EBADF Bad file descriptor.
5380 * EINVAL File descriptor had type that cannot be sent, misc. other errors.
5381 * EFAULT Address at which to store port name is not valid.
5382 * EAGAIN Resource shortage.
5385 * On success, name of send right is stored at user-specified address.
5388 fileport_makeport(proc_t p
, struct fileport_makeport_args
*uap
,
5389 __unused
int *retval
)
5393 user_addr_t user_portaddr
= uap
->portnamep
;
5394 struct fileproc
*fp
= FILEPROC_NULL
;
5395 struct fileglob
*fg
= NULL
;
5396 ipc_port_t fileport
;
5397 mach_port_name_t name
= MACH_PORT_NULL
;
5400 err
= fp_lookup(p
, fd
, &fp
, 1);
5405 if (!file_issendable(p
, fp
)) {
5410 if (FP_ISGUARDED(fp
, GUARD_FILEPORT
)) {
5411 err
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_FILEPORT
);
5415 /* Dropped when port is deallocated */
5421 /* Allocate and initialize a port */
5422 fileport
= fileport_alloc(fg
);
5423 if (fileport
== IPC_PORT_NULL
) {
5429 /* Add an entry. Deallocates port on failure. */
5430 name
= ipc_port_copyout_send(fileport
, get_task_ipcspace(p
->task
));
5431 if (!MACH_PORT_VALID(name
)) {
5436 err
= copyout(&name
, user_portaddr
, sizeof(mach_port_name_t
));
5441 /* Tag the fileglob for debugging purposes */
5442 lck_mtx_lock_spin(&fg
->fg_lock
);
5443 fg
->fg_lflags
|= FG_PORTMADE
;
5444 lck_mtx_unlock(&fg
->fg_lock
);
5446 fp_drop(p
, fd
, fp
, 0);
5453 if (MACH_PORT_VALID(name
)) {
5454 /* Don't care if another thread races us to deallocate the entry */
5455 (void) mach_port_deallocate(get_task_ipcspace(p
->task
), name
);
5458 if (fp
!= FILEPROC_NULL
) {
5459 fp_drop(p
, fd
, fp
, 0);
5466 fileport_releasefg(struct fileglob
*fg
)
5468 (void)closef_locked(NULL
, fg
, PROC_NULL
);
5477 * Description: Obtain the file descriptor for a given Mach send right.
5479 * Parameters: p Process calling fileport
5480 * uap->port Name of send right to file port.
5482 * Returns: 0 Success
5483 * EINVAL Invalid Mach port name, or port is not for a file.
5485 * fdalloc:ENOMEM Unable to allocate fileproc or extend file table.
5488 * *retval (modified) The new descriptor
5491 fileport_makefd(proc_t p
, struct fileport_makefd_args
*uap
, int32_t *retval
)
5493 struct fileglob
*fg
;
5494 struct fileproc
*fp
= FILEPROC_NULL
;
5495 ipc_port_t port
= IPC_PORT_NULL
;
5496 mach_port_name_t send
= uap
->port
;
5501 res
= ipc_object_copyin(get_task_ipcspace(p
->task
),
5502 send
, MACH_MSG_TYPE_COPY_SEND
, &port
);
5504 if (res
!= KERN_SUCCESS
) {
5509 fg
= fileport_port_to_fileglob(port
);
5515 fp
= fileproc_alloc_init(NULL
);
5516 if (fp
== FILEPROC_NULL
) {
5525 err
= fdalloc(p
, 0, &fd
);
5531 *fdflags(p
, fd
) |= UF_EXCLOSE
;
5533 procfdtbl_releasefd(p
, fd
, fp
);
5539 if ((fp
!= NULL
) && (0 != err
)) {
5543 if (IPC_PORT_NULL
!= port
) {
5544 ipc_port_release_send(port
);
5554 * Description: Duplicate the specified descriptor to a free descriptor;
5555 * this is the second half of fdopen(), above.
5557 * Parameters: fdp filedesc pointer to fill in
5559 * dfd fd to dup from
5560 * mode mode to set on new fd
5561 * error command code
5563 * Returns: 0 Success
5564 * EBADF Source fd is bad
5565 * EACCES Requested mode not allowed
5566 * !0 'error', if not ENODEV or
5569 * Notes: XXX This is not thread safe; see fdopen() above
5572 dupfdopen(struct filedesc
*fdp
, int indx
, int dfd
, int flags
, int error
)
5574 struct fileproc
*wfp
;
5575 struct fileproc
*fp
;
5579 proc_t p
= current_proc();
5582 * If the to-be-dup'd fd number is greater than the allowed number
5583 * of file descriptors, or the fd to be dup'd has already been
5584 * closed, reject. Note, check for new == old is necessary as
5585 * falloc could allocate an already closed to-be-dup'd descriptor
5586 * as the new descriptor.
5590 fp
= fdp
->fd_ofiles
[indx
];
5591 if (dfd
< 0 || dfd
>= fdp
->fd_nfiles
||
5592 (wfp
= fdp
->fd_ofiles
[dfd
]) == NULL
|| wfp
== fp
||
5593 (fdp
->fd_ofileflags
[dfd
] & UF_RESERVED
)) {
5599 myerror
= mac_file_check_dup(proc_ucred(p
), wfp
->f_fglob
, dfd
);
5606 * There are two cases of interest here.
5608 * For ENODEV simply dup (dfd) to file descriptor
5609 * (indx) and return.
5611 * For ENXIO steal away the file structure from (dfd) and
5612 * store it in (indx). (dfd) is effectively closed by
5615 * Any other error code is just returned.
5619 if (FP_ISGUARDED(wfp
, GUARD_DUP
)) {
5625 * Check that the mode the file is being opened for is a
5626 * subset of the mode of the existing descriptor.
5628 if (((flags
& (FREAD
|FWRITE
)) | wfp
->f_flag
) != wfp
->f_flag
) {
5632 if (indx
> fdp
->fd_lastfile
)
5633 fdp
->fd_lastfile
= indx
;
5637 fg_free(fp
->f_fglob
);
5638 fp
->f_fglob
= wfp
->f_fglob
;
5640 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
] |
5641 (flags
& O_CLOEXEC
) ? UF_EXCLOSE
: 0;
5657 * Description: Add a reference to a fileglob by fileproc
5659 * Parameters: fp fileproc containing fileglob
5664 * Notes: XXX Should use OSAddAtomic?
5667 fg_ref(struct fileproc
* fp
)
5669 struct fileglob
*fg
;
5673 lck_mtx_lock_spin(&fg
->fg_lock
);
5676 if ((fp
->f_flags
& ~((unsigned int)FP_VALID_FLAGS
)) != 0)
5677 panic("fg_ref: invalid bits on fp %p", fp
);
5679 if (fg
->fg_count
== 0)
5680 panic("fg_ref: adding fgcount to zeroed fg: fp %p fg %p",
5684 lck_mtx_unlock(&fg
->fg_lock
);
5691 * Description: Remove a reference to a fileglob by fileproc
5693 * Parameters: fp fileproc containing fileglob
5698 * Notes: XXX Should use OSAddAtomic?
5701 fg_drop(struct fileproc
* fp
)
5703 struct fileglob
*fg
;
5706 lck_mtx_lock_spin(&fg
->fg_lock
);
5708 lck_mtx_unlock(&fg
->fg_lock
);
5713 * fg_insertuipc_mark
5715 * Description: Mark fileglob for insertion onto message queue if needed
5716 * Also takes fileglob reference
5718 * Parameters: fg Fileglob pointer to insert
5720 * Returns: true, if the fileglob needs to be inserted onto msg queue
5722 * Locks: Takes and drops fg_lock, potentially many times
5725 fg_insertuipc_mark(struct fileglob
* fg
)
5727 boolean_t insert
= FALSE
;
5729 lck_mtx_lock_spin(&fg
->fg_lock
);
5730 while (fg
->fg_lflags
& FG_RMMSGQ
) {
5731 lck_mtx_convert_spin(&fg
->fg_lock
);
5733 fg
->fg_lflags
|= FG_WRMMSGQ
;
5734 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_insertuipc", NULL
);
5739 if (fg
->fg_msgcount
== 1) {
5740 fg
->fg_lflags
|= FG_INSMSGQ
;
5743 lck_mtx_unlock(&fg
->fg_lock
);
5750 * Description: Insert marked fileglob onto message queue
5752 * Parameters: fg Fileglob pointer to insert
5756 * Locks: Takes and drops fg_lock & uipc_lock
5757 * DO NOT call this function with proc_fdlock held as unp_gc()
5758 * can potentially try to acquire proc_fdlock, which can result
5759 * in a deadlock if this function is in unp_gc_wait().
5762 fg_insertuipc(struct fileglob
* fg
)
5764 if (fg
->fg_lflags
& FG_INSMSGQ
) {
5765 lck_mtx_lock_spin(uipc_lock
);
5767 LIST_INSERT_HEAD(&fmsghead
, fg
, f_msglist
);
5768 lck_mtx_unlock(uipc_lock
);
5769 lck_mtx_lock(&fg
->fg_lock
);
5770 fg
->fg_lflags
&= ~FG_INSMSGQ
;
5771 if (fg
->fg_lflags
& FG_WINSMSGQ
) {
5772 fg
->fg_lflags
&= ~FG_WINSMSGQ
;
5773 wakeup(&fg
->fg_lflags
);
5775 lck_mtx_unlock(&fg
->fg_lock
);
5780 * fg_removeuipc_mark
5782 * Description: Mark the fileglob for removal from message queue if needed
5783 * Also releases fileglob message queue reference
5785 * Parameters: fg Fileglob pointer to remove
5787 * Returns: true, if the fileglob needs to be removed from msg queue
5789 * Locks: Takes and drops fg_lock, potentially many times
5792 fg_removeuipc_mark(struct fileglob
* fg
)
5794 boolean_t remove
= FALSE
;
5796 lck_mtx_lock_spin(&fg
->fg_lock
);
5797 while (fg
->fg_lflags
& FG_INSMSGQ
) {
5798 lck_mtx_convert_spin(&fg
->fg_lock
);
5800 fg
->fg_lflags
|= FG_WINSMSGQ
;
5801 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_removeuipc", NULL
);
5804 if (fg
->fg_msgcount
== 0) {
5805 fg
->fg_lflags
|= FG_RMMSGQ
;
5808 lck_mtx_unlock(&fg
->fg_lock
);
5815 * Description: Remove marked fileglob from message queue
5817 * Parameters: fg Fileglob pointer to remove
5821 * Locks: Takes and drops fg_lock & uipc_lock
5822 * DO NOT call this function with proc_fdlock held as unp_gc()
5823 * can potentially try to acquire proc_fdlock, which can result
5824 * in a deadlock if this function is in unp_gc_wait().
5827 fg_removeuipc(struct fileglob
* fg
)
5829 if (fg
->fg_lflags
& FG_RMMSGQ
) {
5830 lck_mtx_lock_spin(uipc_lock
);
5832 LIST_REMOVE(fg
, f_msglist
);
5833 lck_mtx_unlock(uipc_lock
);
5834 lck_mtx_lock(&fg
->fg_lock
);
5835 fg
->fg_lflags
&= ~FG_RMMSGQ
;
5836 if (fg
->fg_lflags
& FG_WRMMSGQ
) {
5837 fg
->fg_lflags
&= ~FG_WRMMSGQ
;
5838 wakeup(&fg
->fg_lflags
);
5840 lck_mtx_unlock(&fg
->fg_lock
);
5843 #endif /* SOCKETS */
5848 * Description: Generic fileops read indirected through the fileops pointer
5849 * in the fileproc structure
5851 * Parameters: fp fileproc structure pointer
5852 * uio user I/O structure pointer
5854 * ctx VFS context for operation
5856 * Returns: 0 Success
5857 * !0 Errno from read
5860 fo_read(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
5862 return ((*fp
->f_ops
->fo_read
)(fp
, uio
, flags
, ctx
));
5869 * Description: Generic fileops write indirected through the fileops pointer
5870 * in the fileproc structure
5872 * Parameters: fp fileproc structure pointer
5873 * uio user I/O structure pointer
5875 * ctx VFS context for operation
5877 * Returns: 0 Success
5878 * !0 Errno from write
5881 fo_write(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
5883 return((*fp
->f_ops
->fo_write
)(fp
, uio
, flags
, ctx
));
5890 * Description: Generic fileops ioctl indirected through the fileops pointer
5891 * in the fileproc structure
5893 * Parameters: fp fileproc structure pointer
5895 * data pointer to internalized copy
5896 * of user space ioctl command
5897 * parameter data in kernel space
5898 * ctx VFS context for operation
5900 * Returns: 0 Success
5901 * !0 Errno from ioctl
5903 * Locks: The caller is assumed to have held the proc_fdlock; this
5904 * function releases and reacquires this lock. If the caller
5905 * accesses data protected by this lock prior to calling this
5906 * function, it will need to revalidate/reacquire any cached
5907 * protected data obtained prior to the call.
5910 fo_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
, vfs_context_t ctx
)
5914 proc_fdunlock(vfs_context_proc(ctx
));
5915 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, com
, data
, ctx
);
5916 proc_fdlock(vfs_context_proc(ctx
));
5924 * Description: Generic fileops select indirected through the fileops pointer
5925 * in the fileproc structure
5927 * Parameters: fp fileproc structure pointer
5928 * which select which
5929 * wql pointer to wait queue list
5930 * ctx VFS context for operation
5932 * Returns: 0 Success
5933 * !0 Errno from select
5936 fo_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
5938 return((*fp
->f_ops
->fo_select
)(fp
, which
, wql
, ctx
));
5945 * Description: Generic fileops close indirected through the fileops pointer
5946 * in the fileproc structure
5948 * Parameters: fp fileproc structure pointer for
5950 * ctx VFS context for operation
5952 * Returns: 0 Success
5953 * !0 Errno from close
5956 fo_close(struct fileglob
*fg
, vfs_context_t ctx
)
5958 return((*fg
->fg_ops
->fo_close
)(fg
, ctx
));
5965 * Description: Generic fileops kqueue filter indirected through the fileops
5966 * pointer in the fileproc structure
5968 * Parameters: fp fileproc structure pointer
5969 * kn pointer to knote to filter on
5970 * ctx VFS context for operation
5972 * Returns: (kn->kn_flags & EV_ERROR) error in kn->kn_data
5973 * 0 Filter is not active
5974 * !0 Filter is active
5977 fo_kqfilter(struct fileproc
*fp
, struct knote
*kn
,
5978 struct kevent_internal_s
*kev
, vfs_context_t ctx
)
5980 return ((*fp
->f_ops
->fo_kqfilter
)(fp
, kn
, kev
, ctx
));
5984 * The ability to send a file descriptor to another
5985 * process is opt-in by file type.
5988 file_issendable(proc_t p
, struct fileproc
*fp
)
5990 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
5992 switch (fp
->f_type
) {
5997 case DTYPE_NETPOLICY
:
5998 return (0 == (fp
->f_fglob
->fg_lflags
& FG_CONFINED
));
6000 /* DTYPE_KQUEUE, DTYPE_FSEVENTS, DTYPE_PSXSEM */
6007 fileproc_alloc_init(__unused
void *arg
)
6009 struct fileproc
*fp
;
6011 MALLOC_ZONE(fp
, struct fileproc
*, sizeof (*fp
), M_FILEPROC
, M_WAITOK
);
6013 bzero(fp
, sizeof (*fp
));
6019 fileproc_free(struct fileproc
*fp
)
6021 switch (FILEPROC_TYPE(fp
)) {
6023 FREE_ZONE(fp
, sizeof (*fp
), M_FILEPROC
);
6026 guarded_fileproc_free(fp
);
6029 panic("%s: corrupt fp %p flags %x", __func__
, fp
, fp
->f_flags
);