2 * Copyright (c) 2000-2015 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>
84 #include <sys/socket.h>
85 #include <sys/socketvar.h>
87 #include <sys/ioctl.h>
88 #include <sys/fcntl.h>
89 #include <sys/malloc.h>
91 #include <sys/syslog.h>
92 #include <sys/unistd.h>
93 #include <sys/resourcevar.h>
94 #include <sys/aio_kern.h>
96 #include <kern/locks.h>
97 #include <sys/uio_internal.h>
98 #include <sys/codesign.h>
99 #include <sys/codedir_internal.h>
101 #include <security/audit/audit.h>
103 #include <sys/mount_internal.h>
104 #include <sys/kdebug.h>
105 #include <sys/sysproto.h>
106 #include <sys/pipe.h>
107 #include <sys/spawn.h>
108 #include <kern/kern_types.h>
109 #include <kern/kalloc.h>
110 #include <kern/waitq.h>
111 #include <libkern/OSAtomic.h>
113 #include <sys/ubc_internal.h>
115 #include <kern/ipc_misc.h>
116 #include <vm/vm_protos.h>
118 #include <mach/mach_port.h>
123 kern_return_t
ipc_object_copyin(ipc_space_t
, mach_port_name_t
,
124 mach_msg_type_name_t
, ipc_port_t
*);
125 void ipc_port_release_send(ipc_port_t
);
130 static int finishdup(proc_t p
,
131 struct filedesc
*fdp
, int old
, int new, int flags
, int32_t *retval
);
133 int falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
, int locked
);
134 void fg_drop(struct fileproc
* fp
);
135 void fg_free(struct fileglob
*fg
);
136 void fg_ref(struct fileproc
* fp
);
137 void fileport_releasefg(struct fileglob
*fg
);
139 /* flags for close_internal_locked */
140 #define FD_DUP2RESV 1
142 /* We don't want these exported */
145 int unlink1(vfs_context_t
, vnode_t
, user_addr_t
, enum uio_seg
, int);
147 static void _fdrelse(struct proc
* p
, int fd
);
150 extern void file_lock_init(void);
152 extern kauth_scope_t kauth_scope_fileop
;
154 /* Conflict wait queue for when selects collide (opaque type) */
155 extern struct waitq select_conflict_queue
;
157 #define f_flag f_fglob->fg_flag
158 #define f_type f_fglob->fg_ops->fo_type
159 #define f_msgcount f_fglob->fg_msgcount
160 #define f_cred f_fglob->fg_cred
161 #define f_ops f_fglob->fg_ops
162 #define f_offset f_fglob->fg_offset
163 #define f_data f_fglob->fg_data
164 #define CHECK_ADD_OVERFLOW_INT64L(x, y) \
165 (((((x) > 0) && ((y) > 0) && ((x) > LLONG_MAX - (y))) || \
166 (((x) < 0) && ((y) < 0) && ((x) < LLONG_MIN - (y)))) \
169 * Descriptor management.
171 struct fmsglist fmsghead
; /* head of list of open files */
172 struct fmsglist fmsg_ithead
; /* head of list of open files */
173 int nfiles
; /* actual number of open files */
176 lck_grp_attr_t
* file_lck_grp_attr
;
177 lck_grp_t
* file_lck_grp
;
178 lck_attr_t
* file_lck_attr
;
180 lck_mtx_t
* uipc_lock
;
184 * check_file_seek_range
186 * Description: Checks if seek offsets are in the range of 0 to LLONG_MAX.
188 * Parameters: fl Flock structure.
189 * cur_file_offset Current offset in the file.
191 * Returns: 0 on Success.
192 * EOVERFLOW on overflow.
193 * EINVAL on offset less than zero.
197 check_file_seek_range(struct flock
*fl
, off_t cur_file_offset
)
199 if (fl
->l_whence
== SEEK_CUR
) {
200 /* Check if the start marker is beyond LLONG_MAX. */
201 if (CHECK_ADD_OVERFLOW_INT64L(fl
->l_start
, cur_file_offset
)) {
202 /* Check if start marker is negative */
203 if (fl
->l_start
< 0) {
208 /* Check if the start marker is negative. */
209 if (fl
->l_start
+ cur_file_offset
< 0) {
212 /* Check if end marker is beyond LLONG_MAX. */
213 if ((fl
->l_len
> 0) && (CHECK_ADD_OVERFLOW_INT64L(fl
->l_start
+
214 cur_file_offset
, fl
->l_len
- 1))) {
217 /* Check if the end marker is negative. */
218 if ((fl
->l_len
<= 0) && (fl
->l_start
+ cur_file_offset
+
222 } else if (fl
->l_whence
== SEEK_SET
) {
223 /* Check if the start marker is negative. */
224 if (fl
->l_start
< 0) {
227 /* Check if the end marker is beyond LLONG_MAX. */
228 if ((fl
->l_len
> 0) &&
229 CHECK_ADD_OVERFLOW_INT64L(fl
->l_start
, fl
->l_len
- 1)) {
232 /* Check if the end marker is negative. */
233 if ((fl
->l_len
< 0) && fl
->l_start
+ fl
->l_len
< 0) {
244 * Description: Initialize the file lock group and the uipc and flist locks
250 * Notes: Called at system startup from bsd_init().
255 /* allocate file lock group attribute and group */
256 file_lck_grp_attr
= lck_grp_attr_alloc_init();
258 file_lck_grp
= lck_grp_alloc_init("file", file_lck_grp_attr
);
260 /* Allocate file lock attribute */
261 file_lck_attr
= lck_attr_alloc_init();
263 uipc_lock
= lck_mtx_alloc_init(file_lck_grp
, file_lck_attr
);
268 * proc_fdlock, proc_fdlock_spin
270 * Description: Lock to control access to the per process struct fileproc
271 * and struct filedesc
273 * Parameters: p Process to take the lock on
277 * Notes: The lock is initialized in forkproc() and destroyed in
278 * reap_child_process().
281 proc_fdlock(proc_t p
)
283 lck_mtx_lock(&p
->p_fdmlock
);
287 proc_fdlock_spin(proc_t p
)
289 lck_mtx_lock_spin(&p
->p_fdmlock
);
293 proc_fdlock_assert(proc_t p
, int assertflags
)
295 lck_mtx_assert(&p
->p_fdmlock
, assertflags
);
302 * Description: Unlock the lock previously locked by a call to proc_fdlock()
304 * Parameters: p Process to drop the lock on
309 proc_fdunlock(proc_t p
)
311 lck_mtx_unlock(&p
->p_fdmlock
);
316 * System calls on descriptors.
323 * Description: Returns the per process maximum size of the descriptor table
325 * Parameters: p Process being queried
326 * retval Pointer to the call return area
331 * *retval (modified) Size of dtable
334 getdtablesize(proc_t p
, __unused
struct getdtablesize_args
*uap
, int32_t *retval
)
337 *retval
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
345 procfdtbl_reservefd(struct proc
* p
, int fd
)
347 p
->p_fd
->fd_ofiles
[fd
] = NULL
;
348 p
->p_fd
->fd_ofileflags
[fd
] |= UF_RESERVED
;
352 procfdtbl_markclosefd(struct proc
* p
, int fd
)
354 p
->p_fd
->fd_ofileflags
[fd
] |= (UF_RESERVED
| UF_CLOSING
);
358 procfdtbl_releasefd(struct proc
* p
, int fd
, struct fileproc
* fp
)
361 p
->p_fd
->fd_ofiles
[fd
] = fp
;
362 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESERVED
;
363 if ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
) == UF_RESVWAIT
) {
364 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESVWAIT
;
370 procfdtbl_waitfd(struct proc
* p
, int fd
)
372 p
->p_fd
->fd_ofileflags
[fd
] |= UF_RESVWAIT
;
373 msleep(&p
->p_fd
, &p
->p_fdmlock
, PRIBIO
, "ftbl_waitfd", NULL
);
378 procfdtbl_clearfd(struct proc
* p
, int fd
)
382 waiting
= (p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
);
383 p
->p_fd
->fd_ofiles
[fd
] = NULL
;
384 p
->p_fd
->fd_ofileflags
[fd
] = 0;
385 if ( waiting
== UF_RESVWAIT
) {
393 * Description: Inline utility function to free an fd in a filedesc
395 * Parameters: fdp Pointer to filedesc fd lies in
397 * reserv fd should be reserved
401 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
405 _fdrelse(struct proc
* p
, int fd
)
407 struct filedesc
*fdp
= p
->p_fd
;
410 if (fd
< fdp
->fd_freefile
)
411 fdp
->fd_freefile
= fd
;
413 if (fd
> fdp
->fd_lastfile
)
414 panic("fdrelse: fd_lastfile inconsistent");
416 procfdtbl_clearfd(p
, fd
);
418 while ((nfd
= fdp
->fd_lastfile
) > 0 &&
419 fdp
->fd_ofiles
[nfd
] == NULL
&&
420 !(fdp
->fd_ofileflags
[nfd
] & UF_RESERVED
))
442 char uio_buf
[ UIO_SIZEOF(1) ];
443 struct vfs_context context
= *(vfs_context_current());
444 bool wrote_some
= false;
448 error
= fp_lookup(p
, fd
, &fp
, 0);
452 if (fp
->f_type
!= DTYPE_VNODE
&& fp
->f_type
!= DTYPE_PIPE
&& fp
->f_type
!= DTYPE_SOCKET
) {
456 if (rw
== UIO_WRITE
&& !(fp
->f_flag
& FWRITE
)) {
461 if (rw
== UIO_READ
&& !(fp
->f_flag
& FREAD
)) {
466 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
468 if (UIO_SEG_IS_USER_SPACE(segflg
))
469 spacetype
= proc_is64bit(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
471 spacetype
= UIO_SYSSPACE
;
473 auio
= uio_createwithbuffer(1, offset
, spacetype
, rw
, &uio_buf
[0], sizeof(uio_buf
));
475 uio_addiov(auio
, base
, len
);
477 if ( !(io_flg
& IO_APPEND
))
480 if (rw
== UIO_WRITE
) {
481 user_ssize_t orig_resid
= uio_resid(auio
);
482 error
= fo_write(fp
, auio
, flags
, &context
);
483 wrote_some
= uio_resid(auio
) < orig_resid
;
485 error
= fo_read(fp
, auio
, flags
, &context
);
488 *aresid
= uio_resid(auio
);
490 if (uio_resid(auio
) && error
== 0)
495 fp_drop_written(p
, fd
, fp
);
497 fp_drop(p
, fd
, fp
, 0);
507 * Description: Duplicate a file descriptor.
509 * Parameters: p Process performing the dup
510 * uap->fd The fd to dup
511 * retval Pointer to the call return area
517 * *retval (modified) The new descriptor
520 dup(proc_t p
, struct dup_args
*uap
, int32_t *retval
)
522 struct filedesc
*fdp
= p
->p_fd
;
528 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
532 if (FP_ISGUARDED(fp
, GUARD_DUP
)) {
533 error
= fp_guard_exception(p
, old
, fp
, kGUARD_EXC_DUP
);
534 (void) fp_drop(p
, old
, fp
, 1);
538 if ( (error
= fdalloc(p
, 0, &new)) ) {
539 fp_drop(p
, old
, fp
, 1);
543 error
= finishdup(p
, fdp
, old
, new, 0, retval
);
544 fp_drop(p
, old
, fp
, 1);
547 if (ENTR_SHOULDTRACE
&& fp
->f_type
== DTYPE_SOCKET
) {
548 KERNEL_ENERGYTRACE(kEnTrActKernSocket
, DBG_FUNC_START
,
549 new, 0, (int64_t)VM_KERNEL_ADDRPERM(fp
->f_data
));
558 * Description: Duplicate a file descriptor to a particular value.
560 * Parameters: p Process performing the dup
561 * uap->from The fd to dup
562 * uap->to The fd to dup it to
563 * retval Pointer to the call return area
569 * *retval (modified) The new descriptor
572 dup2(proc_t p
, struct dup2_args
*uap
, int32_t *retval
)
574 struct filedesc
*fdp
= p
->p_fd
;
575 int old
= uap
->from
, new = uap
->to
;
577 struct fileproc
*fp
, *nfp
;
582 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
586 if (FP_ISGUARDED(fp
, GUARD_DUP
)) {
587 error
= fp_guard_exception(p
, old
, fp
, kGUARD_EXC_DUP
);
588 (void) fp_drop(p
, old
, fp
, 1);
593 (rlim_t
)new >= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
595 fp_drop(p
, old
, fp
, 1);
600 fp_drop(p
, old
, fp
, 1);
605 if (new < 0 || new >= fdp
->fd_nfiles
) {
606 if ( (error
= fdalloc(p
, new, &i
)) ) {
607 fp_drop(p
, old
, fp
, 1);
617 while ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == UF_RESERVED
) {
618 fp_drop(p
, old
, fp
, 1);
619 procfdtbl_waitfd(p
, new);
621 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
626 if ((fdp
->fd_ofiles
[new] != NULL
) &&
627 ((error
= fp_lookup(p
, new, &nfp
, 1)) == 0)) {
628 fp_drop(p
, old
, fp
, 1);
629 if (FP_ISGUARDED(nfp
, GUARD_CLOSE
)) {
630 error
= fp_guard_exception(p
,
631 new, nfp
, kGUARD_EXC_CLOSE
);
632 (void) fp_drop(p
, new, nfp
, 1);
636 (void)close_internal_locked(p
, new, nfp
, FD_DUP2RESV
);
638 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
640 procfdtbl_clearfd(p
, new);
644 if (fdp
->fd_ofiles
[new] != NULL
)
645 panic("dup2: no ref on fileproc %d", new);
647 procfdtbl_reservefd(p
, new);
651 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
656 if (fdp
->fd_ofiles
[new] != 0)
657 panic("dup2: overwriting fd_ofiles with new %d", new);
658 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0)
659 panic("dup2: unreserved fileflags with new %d", new);
661 error
= finishdup(p
, fdp
, old
, new, 0, retval
);
662 fp_drop(p
, old
, fp
, 1);
672 * Description: The file control system call.
674 * Parameters: p Process performing the fcntl
675 * uap->fd The fd to operate against
676 * uap->cmd The command to perform
677 * uap->arg Pointer to the command argument
678 * retval Pointer to the call return area
681 * !0 Errno (see fcntl_nocancel)
684 * *retval (modified) fcntl return value (if any)
686 * Notes: This system call differs from fcntl_nocancel() in that it
687 * tests for cancellation prior to performing a potentially
688 * blocking operation.
691 fcntl(proc_t p
, struct fcntl_args
*uap
, int32_t *retval
)
693 __pthread_testcancel(1);
694 return(fcntl_nocancel(p
, (struct fcntl_nocancel_args
*)uap
, retval
));
701 * Description: A non-cancel-testing file control system call.
703 * Parameters: p Process performing the fcntl
704 * uap->fd The fd to operate against
705 * uap->cmd The command to perform
706 * uap->arg Pointer to the command argument
707 * retval Pointer to the call return area
711 * fp_lookup:EBADF Bad file descriptor
723 * vnode_getwithref:???
731 * vnode_getwithref:???
738 * vnode_getwithref:???
740 * [F_SETSIZE,F_RDADVISE]
743 * vnode_getwithref:???
744 * [F_RDAHEAD,F_NOCACHE]
746 * vnode_getwithref:???
750 * *retval (modified) fcntl return value (if any)
753 fcntl_nocancel(proc_t p
, struct fcntl_nocancel_args
*uap
, int32_t *retval
)
756 struct filedesc
*fdp
= p
->p_fd
;
759 struct vnode
*vp
= NULLVP
; /* for AUDIT_ARG() at end */
760 int i
, tmp
, error
, error2
, flg
= 0;
762 struct flocktimeout fltimeout
;
763 struct timespec
*timeout
= NULL
;
764 struct vfs_context context
;
772 AUDIT_ARG(fd
, uap
->fd
);
773 AUDIT_ARG(cmd
, uap
->cmd
);
776 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
780 context
.vc_thread
= current_thread();
781 context
.vc_ucred
= fp
->f_cred
;
783 is64bit
= proc_is64bit(p
);
789 * Since the arg parameter is defined as a long but may be
790 * either a long or a pointer we must take care to handle
791 * sign extension issues. Our sys call munger will sign
792 * extend a long when we are called from a 32-bit process.
793 * Since we can never have an address greater than 32-bits
794 * from a 32-bit process we lop off the top 32-bits to avoid
795 * getting the wrong address
797 argp
= CAST_USER_ADDR_T((uint32_t)uap
->arg
);
800 pop
= &fdp
->fd_ofileflags
[fd
];
803 error
= mac_file_check_fcntl(proc_ucred(p
), fp
->f_fglob
, uap
->cmd
,
812 case F_DUPFD_CLOEXEC
:
813 if (FP_ISGUARDED(fp
, GUARD_DUP
)) {
814 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_DUP
);
817 newmin
= CAST_DOWN_EXPLICIT(int, uap
->arg
); /* arg is an int, so we won't lose bits */
818 AUDIT_ARG(value32
, newmin
);
819 if ((u_int
)newmin
>= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
820 newmin
>= maxfiles
) {
824 if ( (error
= fdalloc(p
, newmin
, &i
)) )
826 error
= finishdup(p
, fdp
, fd
, i
,
827 uap
->cmd
== F_DUPFD_CLOEXEC
? UF_EXCLOSE
: 0, retval
);
831 *retval
= (*pop
& UF_EXCLOSE
)? FD_CLOEXEC
: 0;
836 AUDIT_ARG(value32
, uap
->arg
);
837 if (uap
->arg
& FD_CLOEXEC
)
840 if (FILEPROC_TYPE(fp
) == FTYPE_GUARDED
) {
841 error
= fp_guard_exception(p
,
842 fd
, fp
, kGUARD_EXC_NOCLOEXEC
);
851 *retval
= OFLAGS(fp
->f_flag
);
856 fp
->f_flag
&= ~FCNTLFLAGS
;
857 tmp
= CAST_DOWN_EXPLICIT(int, uap
->arg
); /* arg is an int, so we won't lose bits */
858 AUDIT_ARG(value32
, tmp
);
859 fp
->f_flag
|= FFLAGS(tmp
) & FCNTLFLAGS
;
860 tmp
= fp
->f_flag
& FNONBLOCK
;
861 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
864 tmp
= fp
->f_flag
& FASYNC
;
865 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, &context
);
868 fp
->f_flag
&= ~FNONBLOCK
;
870 (void)fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
874 if (fp
->f_type
== DTYPE_SOCKET
) {
875 *retval
= ((struct socket
*)fp
->f_data
)->so_pgid
;
879 error
= fo_ioctl(fp
, (int)TIOCGPGRP
, (caddr_t
)retval
, &context
);
884 tmp
= CAST_DOWN_EXPLICIT(pid_t
, uap
->arg
); /* arg is an int, so we won't lose bits */
885 AUDIT_ARG(value32
, tmp
);
886 if (fp
->f_type
== DTYPE_SOCKET
) {
887 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
891 if (fp
->f_type
== DTYPE_PIPE
) {
892 error
= fo_ioctl(fp
, TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
899 proc_t p1
= proc_find(tmp
);
904 tmp
= (int)p1
->p_pgrpid
;
907 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
911 tmp
= CAST_DOWN_EXPLICIT(int, uap
->arg
);
912 if (fp
->f_type
== DTYPE_SOCKET
) {
914 error
= sock_setsockopt((struct socket
*)fp
->f_data
,
915 SOL_SOCKET
, SO_NOSIGPIPE
, &tmp
, sizeof (tmp
));
920 struct fileglob
*fg
= fp
->f_fglob
;
922 lck_mtx_lock_spin(&fg
->fg_lock
);
924 fg
->fg_lflags
|= FG_NOSIGPIPE
;
926 fg
->fg_lflags
&= FG_NOSIGPIPE
;
927 lck_mtx_unlock(&fg
->fg_lock
);
933 if (fp
->f_type
== DTYPE_SOCKET
) {
935 int retsize
= sizeof (*retval
);
936 error
= sock_getsockopt((struct socket
*)fp
->f_data
,
937 SOL_SOCKET
, SO_NOSIGPIPE
, retval
, &retsize
);
942 *retval
= (fp
->f_fglob
->fg_lflags
& FG_NOSIGPIPE
) ?
950 * If this is the only reference to this fglob in the process
951 * and it's already marked as close-on-fork then mark it as
952 * (immutably) "confined" i.e. any fd that points to it will
953 * forever be close-on-fork, and attempts to use an IPC
954 * mechanism to move the descriptor elsewhere will fail.
956 if (CAST_DOWN_EXPLICIT(int, uap
->arg
)) {
957 struct fileglob
*fg
= fp
->f_fglob
;
959 lck_mtx_lock_spin(&fg
->fg_lock
);
960 if (fg
->fg_lflags
& FG_CONFINED
)
962 else if (1 != fg
->fg_count
)
963 error
= EAGAIN
; /* go close the dup .. */
964 else if (UF_FORKCLOSE
== (*pop
& UF_FORKCLOSE
)) {
965 fg
->fg_lflags
|= FG_CONFINED
;
968 error
= EBADF
; /* open without O_CLOFORK? */
969 lck_mtx_unlock(&fg
->fg_lock
);
972 * Other subsystems may have built on the immutability
973 * of FG_CONFINED; clearing it may be tricky.
975 error
= EPERM
; /* immutable */
980 *retval
= (fp
->f_fglob
->fg_lflags
& FG_CONFINED
) ? 1 : 0;
984 case F_SETLKWTIMEOUT
:
986 case F_OFD_SETLKWTIMEOUT
:
989 /* Fall into F_SETLK */
993 if (fp
->f_type
!= DTYPE_VNODE
) {
997 vp
= (struct vnode
*)fp
->f_data
;
1000 offset
= fp
->f_offset
;
1003 /* Copy in the lock structure */
1004 if (F_SETLKWTIMEOUT
== uap
->cmd
||
1005 F_OFD_SETLKWTIMEOUT
== uap
->cmd
) {
1006 error
= copyin(argp
, (caddr_t
) &fltimeout
, sizeof(fltimeout
));
1011 timeout
= &fltimeout
.timeout
;
1013 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
1019 /* Check starting byte and ending byte for EOVERFLOW in SEEK_CUR */
1020 /* and ending byte for EOVERFLOW in SEEK_SET */
1021 error
= check_file_seek_range(&fl
, offset
);
1026 if ( (error
= vnode_getwithref(vp
)) ) {
1029 if (fl
.l_whence
== SEEK_CUR
)
1030 fl
.l_start
+= offset
;
1033 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
1036 (void)vnode_put(vp
);
1043 case F_OFD_SETLKWTIMEOUT
:
1045 switch (fl
.l_type
) {
1047 if ((fflag
& FREAD
) == 0) {
1051 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1052 F_SETLK
, &fl
, flg
, &context
, timeout
);
1055 if ((fflag
& FWRITE
) == 0) {
1059 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1060 F_SETLK
, &fl
, flg
, &context
, timeout
);
1063 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1064 F_UNLCK
, &fl
, F_OFD_LOCK
, &context
,
1072 (F_RDLCK
== fl
.l_type
|| F_WRLCK
== fl
.l_type
)) {
1073 struct fileglob
*fg
= fp
->f_fglob
;
1076 * arrange F_UNLCK on last close (once
1077 * set, FG_HAS_OFDLOCK is immutable)
1079 if ((fg
->fg_lflags
& FG_HAS_OFDLOCK
) == 0) {
1080 lck_mtx_lock_spin(&fg
->fg_lock
);
1081 fg
->fg_lflags
|= FG_HAS_OFDLOCK
;
1082 lck_mtx_unlock(&fg
->fg_lock
);
1088 switch (fl
.l_type
) {
1090 if ((fflag
& FREAD
) == 0) {
1094 // XXX UInt32 unsafe for LP64 kernel
1095 OSBitOrAtomic(P_LADVLOCK
, &p
->p_ladvflag
);
1096 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1097 F_SETLK
, &fl
, flg
, &context
, timeout
);
1100 if ((fflag
& FWRITE
) == 0) {
1104 // XXX UInt32 unsafe for LP64 kernel
1105 OSBitOrAtomic(P_LADVLOCK
, &p
->p_ladvflag
);
1106 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1107 F_SETLK
, &fl
, flg
, &context
, timeout
);
1110 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1111 F_UNLCK
, &fl
, F_POSIX
, &context
, timeout
);
1119 (void) vnode_put(vp
);
1124 if (fp
->f_type
!= DTYPE_VNODE
) {
1128 vp
= (struct vnode
*)fp
->f_data
;
1130 offset
= fp
->f_offset
;
1133 /* Copy in the lock structure */
1134 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
1138 /* Check starting byte and ending byte for EOVERFLOW in SEEK_CUR */
1139 /* and ending byte for EOVERFLOW in SEEK_SET */
1140 error
= check_file_seek_range(&fl
, offset
);
1145 if ((fl
.l_whence
== SEEK_SET
) && (fl
.l_start
< 0)) {
1150 switch (fl
.l_type
) {
1160 switch (fl
.l_whence
) {
1170 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1171 if (fl
.l_whence
== SEEK_CUR
)
1172 fl
.l_start
+= offset
;
1175 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
1181 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1182 F_GETLK
, &fl
, F_OFD_LOCK
, &context
, NULL
);
1184 case F_OFD_GETLKPID
:
1185 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1186 F_GETLKPID
, &fl
, F_OFD_LOCK
, &context
, NULL
);
1189 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1190 uap
->cmd
, &fl
, F_POSIX
, &context
, NULL
);
1194 (void)vnode_put(vp
);
1197 error
= copyout((caddr_t
)&fl
, argp
, sizeof(fl
));
1201 case F_PREALLOCATE
: {
1202 fstore_t alloc_struct
; /* structure for allocate command */
1203 u_int32_t alloc_flags
= 0;
1205 if (fp
->f_type
!= DTYPE_VNODE
) {
1210 vp
= (struct vnode
*)fp
->f_data
;
1213 /* make sure that we have write permission */
1214 if ((fp
->f_flag
& FWRITE
) == 0) {
1219 error
= copyin(argp
, (caddr_t
)&alloc_struct
, sizeof(alloc_struct
));
1223 /* now set the space allocated to 0 */
1224 alloc_struct
.fst_bytesalloc
= 0;
1227 * Do some simple parameter checking
1230 /* set up the flags */
1232 alloc_flags
|= PREALLOCATE
;
1234 if (alloc_struct
.fst_flags
& F_ALLOCATECONTIG
)
1235 alloc_flags
|= ALLOCATECONTIG
;
1237 if (alloc_struct
.fst_flags
& F_ALLOCATEALL
)
1238 alloc_flags
|= ALLOCATEALL
;
1241 * Do any position mode specific stuff. The only
1242 * position mode supported now is PEOFPOSMODE
1245 switch (alloc_struct
.fst_posmode
) {
1248 if (alloc_struct
.fst_offset
!= 0) {
1253 alloc_flags
|= ALLOCATEFROMPEOF
;
1257 if (alloc_struct
.fst_offset
<= 0) {
1262 alloc_flags
|= ALLOCATEFROMVOL
;
1270 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1272 * call allocate to get the space
1274 error
= VNOP_ALLOCATE(vp
,alloc_struct
.fst_length
,alloc_flags
,
1275 &alloc_struct
.fst_bytesalloc
, alloc_struct
.fst_offset
,
1277 (void)vnode_put(vp
);
1279 error2
= copyout((caddr_t
)&alloc_struct
, argp
, sizeof(alloc_struct
));
1288 if (fp
->f_type
!= DTYPE_VNODE
) {
1292 vp
= (struct vnode
*)fp
->f_data
;
1295 error
= copyin(argp
, (caddr_t
)&offset
, sizeof (off_t
));
1298 AUDIT_ARG(value64
, offset
);
1300 error
= vnode_getwithref(vp
);
1305 error
= mac_vnode_check_truncate(&context
,
1306 fp
->f_fglob
->fg_cred
, vp
);
1308 (void)vnode_put(vp
);
1313 * Make sure that we are root. Growing a file
1314 * without zero filling the data is a security hole
1315 * root would have access anyway so we'll allow it
1317 if (!kauth_cred_issuser(kauth_cred_get())) {
1323 error
= vnode_setsize(vp
, offset
, IO_NOZEROFILL
,
1327 (void)vnode_put(vp
);
1331 if (fp
->f_type
!= DTYPE_VNODE
) {
1336 fp
->f_fglob
->fg_flag
&= ~FNORDAHEAD
;
1338 fp
->f_fglob
->fg_flag
|= FNORDAHEAD
;
1343 if (fp
->f_type
!= DTYPE_VNODE
) {
1348 fp
->f_fglob
->fg_flag
|= FNOCACHE
;
1350 fp
->f_fglob
->fg_flag
&= ~FNOCACHE
;
1355 if (fp
->f_type
!= DTYPE_VNODE
) {
1360 fp
->f_fglob
->fg_flag
|= FNODIRECT
;
1362 fp
->f_fglob
->fg_flag
&= ~FNODIRECT
;
1366 case F_SINGLE_WRITER
:
1367 if (fp
->f_type
!= DTYPE_VNODE
) {
1372 fp
->f_fglob
->fg_flag
|= FSINGLE_WRITER
;
1374 fp
->f_fglob
->fg_flag
&= ~FSINGLE_WRITER
;
1378 case F_GLOBAL_NOCACHE
:
1379 if (fp
->f_type
!= DTYPE_VNODE
) {
1383 vp
= (struct vnode
*)fp
->f_data
;
1386 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1388 *retval
= vnode_isnocache(vp
);
1391 vnode_setnocache(vp
);
1393 vnode_clearnocache(vp
);
1395 (void)vnode_put(vp
);
1399 case F_CHECK_OPENEVT
:
1400 if (fp
->f_type
!= DTYPE_VNODE
) {
1404 vp
= (struct vnode
*)fp
->f_data
;
1407 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1409 *retval
= vnode_is_openevt(vp
);
1412 vnode_set_openevt(vp
);
1414 vnode_clear_openevt(vp
);
1416 (void)vnode_put(vp
);
1421 struct radvisory ra_struct
;
1423 if (fp
->f_type
!= DTYPE_VNODE
) {
1427 vp
= (struct vnode
*)fp
->f_data
;
1430 if ( (error
= copyin(argp
, (caddr_t
)&ra_struct
, sizeof(ra_struct
))) )
1432 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1433 error
= VNOP_IOCTL(vp
, F_RDADVISE
, (caddr_t
)&ra_struct
, 0, &context
);
1435 (void)vnode_put(vp
);
1442 if (fp
->f_type
!= DTYPE_VNODE
) {
1446 vp
= (struct vnode
*)fp
->f_data
;
1449 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1450 error
= cluster_push(vp
, 0);
1452 (void)vnode_put(vp
);
1457 case F_LOG2PHYS_EXT
: {
1458 struct log2phys l2p_struct
; /* structure for allocate command */
1461 off_t file_offset
= 0;
1465 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1466 error
= copyin(argp
, (caddr_t
)&l2p_struct
, sizeof(l2p_struct
));
1469 file_offset
= l2p_struct
.l2p_devoffset
;
1471 file_offset
= fp
->f_offset
;
1473 if (fp
->f_type
!= DTYPE_VNODE
) {
1477 vp
= (struct vnode
*)fp
->f_data
;
1479 if ( (error
= vnode_getwithref(vp
)) ) {
1482 error
= VNOP_OFFTOBLK(vp
, file_offset
, &lbn
);
1484 (void)vnode_put(vp
);
1487 error
= VNOP_BLKTOOFF(vp
, lbn
, &offset
);
1489 (void)vnode_put(vp
);
1492 devBlockSize
= vfs_devblocksize(vnode_mount(vp
));
1493 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1494 if (l2p_struct
.l2p_contigbytes
< 0) {
1500 a_size
= MIN((uint64_t)l2p_struct
.l2p_contigbytes
, SIZE_MAX
);
1502 a_size
= devBlockSize
;
1505 error
= VNOP_BLOCKMAP(vp
, offset
, a_size
, &bn
, &run
, NULL
, 0, &context
);
1507 (void)vnode_put(vp
);
1510 l2p_struct
.l2p_flags
= 0; /* for now */
1511 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1512 l2p_struct
.l2p_contigbytes
= run
- (file_offset
- offset
);
1514 l2p_struct
.l2p_contigbytes
= 0; /* for now */
1518 * The block number being -1 suggests that the file offset is not backed
1519 * by any real blocks on-disk. As a result, just let it be passed back up wholesale.
1522 /* Don't multiply it by the block size */
1523 l2p_struct
.l2p_devoffset
= bn
;
1526 l2p_struct
.l2p_devoffset
= bn
* devBlockSize
;
1527 l2p_struct
.l2p_devoffset
+= file_offset
- offset
;
1529 error
= copyout((caddr_t
)&l2p_struct
, argp
, sizeof(l2p_struct
));
1537 if (fp
->f_type
!= DTYPE_VNODE
) {
1541 vp
= (struct vnode
*)fp
->f_data
;
1544 pathlen
= MAXPATHLEN
;
1545 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
1546 if (pathbufp
== NULL
) {
1550 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1551 error
= vn_getpath(vp
, pathbufp
, &pathlen
);
1552 (void)vnode_put(vp
);
1555 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
1557 FREE(pathbufp
, M_TEMP
);
1561 case F_PATHPKG_CHECK
: {
1565 if (fp
->f_type
!= DTYPE_VNODE
) {
1569 vp
= (struct vnode
*)fp
->f_data
;
1572 pathlen
= MAXPATHLEN
;
1573 pathbufp
= kalloc(MAXPATHLEN
);
1575 if ( (error
= copyinstr(argp
, pathbufp
, MAXPATHLEN
, &pathlen
)) == 0 ) {
1576 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1577 AUDIT_ARG(text
, pathbufp
);
1578 error
= vn_path_package_check(vp
, pathbufp
, pathlen
, retval
);
1580 (void)vnode_put(vp
);
1583 kfree(pathbufp
, MAXPATHLEN
);
1587 case F_CHKCLEAN
: // used by regression tests to see if all dirty pages got cleaned by fsync()
1588 case F_FULLFSYNC
: // fsync + flush the journal + DKIOCSYNCHRONIZE
1589 case F_BARRIERFSYNC
: // fsync + barrier
1590 case F_FREEZE_FS
: // freeze all other fs operations for the fs of this fd
1591 case F_THAW_FS
: { // thaw all frozen fs operations for the fs of this fd
1592 if (fp
->f_type
!= DTYPE_VNODE
) {
1596 vp
= (struct vnode
*)fp
->f_data
;
1599 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1600 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
1602 (void)vnode_put(vp
);
1608 * SPI (private) for opening a file starting from a dir fd
1611 struct user_fopenfrom fopen
;
1612 struct vnode_attr va
;
1613 struct nameidata nd
;
1616 /* Check if this isn't a valid file descriptor */
1617 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1618 (fp
->f_flag
& FREAD
) == 0) {
1622 vp
= (struct vnode
*)fp
->f_data
;
1625 if (vnode_getwithref(vp
)) {
1630 /* Only valid for directories */
1631 if (vp
->v_type
!= VDIR
) {
1637 /* Get flags, mode and pathname arguments. */
1638 if (IS_64BIT_PROCESS(p
)) {
1639 error
= copyin(argp
, &fopen
, sizeof(fopen
));
1641 struct user32_fopenfrom fopen32
;
1643 error
= copyin(argp
, &fopen32
, sizeof(fopen32
));
1644 fopen
.o_flags
= fopen32
.o_flags
;
1645 fopen
.o_mode
= fopen32
.o_mode
;
1646 fopen
.o_pathname
= CAST_USER_ADDR_T(fopen32
.o_pathname
);
1652 AUDIT_ARG(fflags
, fopen
.o_flags
);
1653 AUDIT_ARG(mode
, fopen
.o_mode
);
1655 /* Mask off all but regular access permissions */
1656 cmode
= ((fopen
.o_mode
&~ fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
1657 VATTR_SET(&va
, va_mode
, cmode
& ACCESSPERMS
);
1659 /* Start the lookup relative to the file descriptor's vnode. */
1660 NDINIT(&nd
, LOOKUP
, OP_OPEN
, USEDVP
| FOLLOW
| AUDITVNPATH1
, UIO_USERSPACE
,
1661 fopen
.o_pathname
, &context
);
1664 error
= open1(&context
, &nd
, fopen
.o_flags
, &va
,
1665 fileproc_alloc_init
, NULL
, retval
);
1671 * SPI (private) for unlinking a file starting from a dir fd
1673 case F_UNLINKFROM
: {
1674 user_addr_t pathname
;
1676 /* Check if this isn't a valid file descriptor */
1677 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1678 (fp
->f_flag
& FREAD
) == 0) {
1682 vp
= (struct vnode
*)fp
->f_data
;
1685 if (vnode_getwithref(vp
)) {
1690 /* Only valid for directories */
1691 if (vp
->v_type
!= VDIR
) {
1697 /* Get flags, mode and pathname arguments. */
1698 if (IS_64BIT_PROCESS(p
)) {
1699 pathname
= (user_addr_t
)argp
;
1701 pathname
= CAST_USER_ADDR_T(argp
);
1704 /* Start the lookup relative to the file descriptor's vnode. */
1705 error
= unlink1(&context
, vp
, pathname
, UIO_USERSPACE
, 0);
1714 case F_ADDFILESIGS_FOR_DYLD_SIM
:
1715 case F_ADDFILESIGS_RETURN
:
1717 struct cs_blob
*blob
= NULL
;
1718 struct user_fsignatures fs
;
1720 vm_offset_t kernel_blob_addr
;
1721 vm_size_t kernel_blob_size
;
1722 int blob_add_flags
= 0;
1724 if (fp
->f_type
!= DTYPE_VNODE
) {
1728 vp
= (struct vnode
*)fp
->f_data
;
1731 if (uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
1732 blob_add_flags
|= MAC_VNODE_CHECK_DYLD_SIM
;
1733 if ((p
->p_csflags
& CS_KILL
) == 0) {
1735 p
->p_csflags
|= CS_KILL
;
1740 error
= vnode_getwithref(vp
);
1744 if (IS_64BIT_PROCESS(p
)) {
1745 error
= copyin(argp
, &fs
, sizeof (fs
));
1747 struct user32_fsignatures fs32
;
1749 error
= copyin(argp
, &fs32
, sizeof (fs32
));
1750 fs
.fs_file_start
= fs32
.fs_file_start
;
1751 fs
.fs_blob_start
= CAST_USER_ADDR_T(fs32
.fs_blob_start
);
1752 fs
.fs_blob_size
= fs32
.fs_blob_size
;
1761 * First check if we have something loaded a this offset
1763 blob
= ubc_cs_blob_get(vp
, CPU_TYPE_ANY
, fs
.fs_file_start
);
1766 /* If this is for dyld_sim revalidate the blob */
1767 if (uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
1768 error
= ubc_cs_blob_revalidate(vp
, blob
, blob_add_flags
);
1773 * An arbitrary limit, to prevent someone from mapping in a 20GB blob. This should cover
1774 * our use cases for the immediate future, but note that at the time of this commit, some
1775 * platforms are nearing 2MB blob sizes (with a prior soft limit of 2.5MB).
1777 * We should consider how we can manage this more effectively; the above means that some
1778 * platforms are using megabytes of memory for signing data; it merely hasn't crossed the
1779 * threshold considered ridiculous at the time of this change.
1781 #define CS_MAX_BLOB_SIZE (40ULL * 1024ULL * 1024ULL)
1782 if (fs
.fs_blob_size
> CS_MAX_BLOB_SIZE
) {
1788 kernel_blob_size
= CAST_DOWN(vm_size_t
, fs
.fs_blob_size
);
1789 kr
= ubc_cs_blob_allocate(&kernel_blob_addr
, &kernel_blob_size
);
1790 if (kr
!= KERN_SUCCESS
) {
1796 if(uap
->cmd
== F_ADDSIGS
) {
1797 error
= copyin(fs
.fs_blob_start
,
1798 (void *) kernel_blob_addr
,
1800 } else /* F_ADDFILESIGS || F_ADDFILESIGS_RETURN || F_ADDFILESIGS_FOR_DYLD_SIM */ {
1803 error
= vn_rdwr(UIO_READ
,
1805 (caddr_t
) kernel_blob_addr
,
1807 fs
.fs_file_start
+ fs
.fs_blob_start
,
1813 if ((error
== 0) && resid
) {
1814 /* kernel_blob_size rounded to a page size, but signature may be at end of file */
1815 memset((void *)(kernel_blob_addr
+ (kernel_blob_size
- resid
)), 0x0, resid
);
1820 ubc_cs_blob_deallocate(kernel_blob_addr
,
1827 error
= ubc_cs_blob_add(vp
,
1828 CPU_TYPE_ANY
, /* not for a specific architecture */
1835 ubc_cs_blob_deallocate(kernel_blob_addr
,
1838 /* ubc_blob_add() has consumed "kernel_blob_addr" */
1839 #if CHECK_CS_VALIDATION_BITMAP
1840 ubc_cs_validation_bitmap_allocate( vp
);
1845 if (uap
->cmd
== F_ADDFILESIGS_RETURN
|| uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
1847 * The first element of the structure is a
1848 * off_t that happen to have the same size for
1849 * all archs. Lets overwrite that.
1851 off_t end_offset
= 0;
1853 end_offset
= blob
->csb_end_offset
;
1854 error
= copyout(&end_offset
, argp
, sizeof (end_offset
));
1857 (void) vnode_put(vp
);
1865 case F_GETPROTECTIONCLASS
: {
1868 if (fp
->f_type
!= DTYPE_VNODE
) {
1872 vp
= (struct vnode
*)fp
->f_data
;
1876 if (vnode_getwithref(vp
)) {
1881 error
= cp_vnode_getclass (vp
, &class);
1890 case F_SETPROTECTIONCLASS
: {
1891 /* tmp must be a valid PROTECTION_CLASS_* */
1892 tmp
= CAST_DOWN_EXPLICIT(uint32_t, uap
->arg
);
1894 if (fp
->f_type
!= DTYPE_VNODE
) {
1898 vp
= (struct vnode
*)fp
->f_data
;
1902 if (vnode_getwithref(vp
)) {
1907 /* Only go forward if you have write access */
1908 vfs_context_t ctx
= vfs_context_current();
1909 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
1914 error
= cp_vnode_setclass (vp
, tmp
);
1919 case F_TRANSCODEKEY
: {
1921 char *backup_keyp
= NULL
;
1922 unsigned backup_key_len
= CP_MAX_WRAPPEDKEYSIZE
;
1924 if (fp
->f_type
!= DTYPE_VNODE
) {
1929 vp
= (struct vnode
*)fp
->f_data
;
1932 if (vnode_getwithref(vp
)) {
1937 MALLOC(backup_keyp
, char *, backup_key_len
, M_TEMP
, M_WAITOK
);
1938 if (backup_keyp
== NULL
) {
1943 error
= cp_vnode_transcode (vp
, backup_keyp
, &backup_key_len
);
1947 error
= copyout((caddr_t
)backup_keyp
, argp
, backup_key_len
);
1948 *retval
= backup_key_len
;
1951 FREE(backup_keyp
, M_TEMP
);
1956 case F_GETPROTECTIONLEVEL
: {
1957 uint32_t cp_version
= 0;
1959 if (fp
->f_type
!= DTYPE_VNODE
) {
1964 vp
= (struct vnode
*) fp
->f_data
;
1967 if (vnode_getwithref(vp
)) {
1973 * if cp_get_major_vers fails, error will be set to proper errno
1974 * and cp_version will still be 0.
1977 error
= cp_get_root_major_vers (vp
, &cp_version
);
1978 *retval
= cp_version
;
1984 case F_GETDEFAULTPROTLEVEL
: {
1985 uint32_t cp_default
= 0;
1987 if (fp
->f_type
!= DTYPE_VNODE
) {
1992 vp
= (struct vnode
*) fp
->f_data
;
1995 if (vnode_getwithref(vp
)) {
2001 * if cp_get_major_vers fails, error will be set to proper errno
2002 * and cp_version will still be 0.
2005 error
= cp_get_default_level(vp
, &cp_default
);
2006 *retval
= cp_default
;
2013 #endif /* CONFIG_PROTECT */
2015 case F_MOVEDATAEXTENTS
: {
2016 struct fileproc
*fp2
= NULL
;
2017 struct vnode
*src_vp
= NULLVP
;
2018 struct vnode
*dst_vp
= NULLVP
;
2019 /* We need to grab the 2nd FD out of the argments before moving on. */
2020 int fd2
= CAST_DOWN_EXPLICIT(int32_t, uap
->arg
);
2022 if (fp
->f_type
!= DTYPE_VNODE
) {
2027 /* For now, special case HFS+ only, since this is SPI. */
2028 src_vp
= (struct vnode
*)fp
->f_data
;
2029 if (src_vp
->v_tag
!= VT_HFS
) {
2035 * Get the references before we start acquiring iocounts on the vnodes,
2036 * while we still hold the proc fd lock
2038 if ( (error
= fp_lookup(p
, fd2
, &fp2
, 1)) ) {
2042 if (fp2
->f_type
!= DTYPE_VNODE
) {
2043 fp_drop(p
, fd2
, fp2
, 1);
2047 dst_vp
= (struct vnode
*)fp2
->f_data
;
2048 if (dst_vp
->v_tag
!= VT_HFS
) {
2049 fp_drop(p
, fd2
, fp2
, 1);
2055 /* Re-do MAC checks against the new FD, pass in a fake argument */
2056 error
= mac_file_check_fcntl(proc_ucred(p
), fp2
->f_fglob
, uap
->cmd
, 0);
2058 fp_drop(p
, fd2
, fp2
, 1);
2062 /* Audit the 2nd FD */
2067 if (vnode_getwithref(src_vp
)) {
2068 fp_drop(p
, fd2
, fp2
, 0);
2072 if (vnode_getwithref(dst_vp
)) {
2074 fp_drop(p
, fd2
, fp2
, 0);
2080 * Basic asserts; validate they are not the same and that
2081 * both live on the same filesystem.
2083 if (dst_vp
== src_vp
) {
2086 fp_drop (p
, fd2
, fp2
, 0);
2091 if (dst_vp
->v_mount
!= src_vp
->v_mount
) {
2094 fp_drop (p
, fd2
, fp2
, 0);
2099 /* Now we have a legit pair of FDs. Go to work */
2101 /* Now check for write access to the target files */
2102 if(vnode_authorize(src_vp
, NULLVP
,
2103 (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), &context
) != 0) {
2106 fp_drop(p
, fd2
, fp2
, 0);
2111 if(vnode_authorize(dst_vp
, NULLVP
,
2112 (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), &context
) != 0) {
2115 fp_drop(p
, fd2
, fp2
, 0);
2120 /* Verify that both vps point to files and not directories */
2121 if ( !vnode_isreg(src_vp
) || !vnode_isreg(dst_vp
)) {
2125 fp_drop (p
, fd2
, fp2
, 0);
2130 * The exchangedata syscall handler passes in 0 for the flags to VNOP_EXCHANGE.
2131 * We'll pass in our special bit indicating that the new behavior is expected
2134 error
= VNOP_EXCHANGE(src_vp
, dst_vp
, FSOPT_EXCHANGE_DATA_ONLY
, &context
);
2138 fp_drop(p
, fd2
, fp2
, 0);
2143 * SPI for making a file compressed.
2145 case F_MAKECOMPRESSED
: {
2146 uint32_t gcounter
= CAST_DOWN_EXPLICIT(uint32_t, uap
->arg
);
2148 if (fp
->f_type
!= DTYPE_VNODE
) {
2153 vp
= (struct vnode
*) fp
->f_data
;
2157 if (vnode_getwithref(vp
)) {
2163 if ((vnode_isreg(vp
) == 0) && (vnode_islnk(vp
) == 0)) {
2169 /* invoke ioctl to pass off to FS */
2170 /* Only go forward if you have write access */
2171 vfs_context_t ctx
= vfs_context_current();
2172 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2178 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)&gcounter
, 0, &context
);
2185 * SPI (private) for indicating to a filesystem that subsequent writes to
2186 * the open FD will written to the Fastflow.
2188 case F_SET_GREEDY_MODE
:
2189 /* intentionally drop through to the same handler as F_SETSTATIC.
2190 * both fcntls should pass the argument and their selector into VNOP_IOCTL.
2194 * SPI (private) for indicating to a filesystem that subsequent writes to
2195 * the open FD will represent static content.
2197 case F_SETSTATICCONTENT
: {
2198 caddr_t ioctl_arg
= NULL
;
2201 ioctl_arg
= (caddr_t
) 1;
2204 if (fp
->f_type
!= DTYPE_VNODE
) {
2208 vp
= (struct vnode
*)fp
->f_data
;
2211 error
= vnode_getwithref(vp
);
2217 /* Only go forward if you have write access */
2218 vfs_context_t ctx
= vfs_context_current();
2219 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2225 error
= VNOP_IOCTL(vp
, uap
->cmd
, ioctl_arg
, 0, &context
);
2226 (void)vnode_put(vp
);
2232 * SPI (private) for indicating to the lower level storage driver that the
2233 * subsequent writes should be of a particular IO type (burst, greedy, static),
2234 * or other flavors that may be necessary.
2241 /* extract 32 bits of flags from userland */
2242 param_ptr
= (caddr_t
) uap
->arg
;
2243 param
= (uint32_t) param_ptr
;
2246 /* If no argument is specified, error out */
2252 * Validate the different types of flags that can be specified:
2253 * all of them are mutually exclusive for now.
2256 case F_IOTYPE_ISOCHRONOUS
:
2265 if (fp
->f_type
!= DTYPE_VNODE
) {
2269 vp
= (struct vnode
*)fp
->f_data
;
2272 error
= vnode_getwithref(vp
);
2278 /* Only go forward if you have write access */
2279 vfs_context_t ctx
= vfs_context_current();
2280 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2286 error
= VNOP_IOCTL(vp
, uap
->cmd
, param_ptr
, 0, &context
);
2287 (void)vnode_put(vp
);
2294 * Extract the CodeDirectory of the vnode associated with
2295 * the file descriptor and copy it back to user space
2297 case F_GETCODEDIR
: {
2298 struct user_fcodeblobs args
;
2300 if (fp
->f_type
!= DTYPE_VNODE
) {
2305 vp
= (struct vnode
*)fp
->f_data
;
2308 if ((fp
->f_flag
& FREAD
) == 0) {
2313 if (IS_64BIT_PROCESS(p
)) {
2314 struct user64_fcodeblobs args64
;
2316 error
= copyin(argp
, &args64
, sizeof(args64
));
2320 args
.f_cd_hash
= args64
.f_cd_hash
;
2321 args
.f_hash_size
= args64
.f_hash_size
;
2322 args
.f_cd_buffer
= args64
.f_cd_buffer
;
2323 args
.f_cd_size
= args64
.f_cd_size
;
2324 args
.f_out_size
= args64
.f_out_size
;
2325 args
.f_arch
= args64
.f_arch
;
2327 struct user32_fcodeblobs args32
;
2329 error
= copyin(argp
, &args32
, sizeof(args32
));
2333 args
.f_cd_hash
= CAST_USER_ADDR_T(args32
.f_cd_hash
);
2334 args
.f_hash_size
= args32
.f_hash_size
;
2335 args
.f_cd_buffer
= CAST_USER_ADDR_T(args32
.f_cd_buffer
);
2336 args
.f_cd_size
= args32
.f_cd_size
;
2337 args
.f_out_size
= CAST_USER_ADDR_T(args32
.f_out_size
);
2338 args
.f_arch
= args32
.f_arch
;
2341 if (vp
->v_ubcinfo
== NULL
) {
2346 struct cs_blob
*t_blob
= vp
->v_ubcinfo
->cs_blobs
;
2349 * This call fails if there is no cs_blob corresponding to the
2350 * vnode, or if there are multiple cs_blobs present, and the caller
2351 * did not specify which cpu_type they want the cs_blob for
2353 if (t_blob
== NULL
) {
2354 error
= ENOENT
; /* there is no codesigning blob for this process */
2356 } else if (args
.f_arch
== 0 && t_blob
->csb_next
!= NULL
) {
2357 error
= ENOENT
; /* too many architectures and none specified */
2361 /* If the user specified an architecture, find the right blob */
2362 if (args
.f_arch
!= 0) {
2364 if (t_blob
->csb_cpu_type
== args
.f_arch
)
2366 t_blob
= t_blob
->csb_next
;
2368 /* The cpu_type the user requested could not be found */
2369 if (t_blob
== NULL
) {
2375 const CS_SuperBlob
*super_blob
= (void *)t_blob
->csb_mem_kaddr
;
2376 const CS_CodeDirectory
*cd
= findCodeDirectory(super_blob
,
2377 (const char *) super_blob
,
2378 (const char *) super_blob
+ t_blob
->csb_mem_size
);
2384 uint64_t buffer_size
= ntohl(cd
->length
);
2386 if (buffer_size
> UINT_MAX
) {
2391 error
= copyout(&buffer_size
, args
.f_out_size
, sizeof(unsigned int));
2395 if (sizeof(t_blob
->csb_cdhash
) > args
.f_hash_size
||
2396 buffer_size
> args
.f_cd_size
) {
2401 error
= copyout(t_blob
->csb_cdhash
, args
.f_cd_hash
, sizeof(t_blob
->csb_cdhash
));
2404 error
= copyout(cd
, args
.f_cd_buffer
, buffer_size
);
2412 * Set the vnode pointed to by 'fd'
2413 * and tag it as the (potentially future) backing store
2414 * for another filesystem
2416 case F_SETBACKINGSTORE
: {
2417 if (fp
->f_type
!= DTYPE_VNODE
) {
2422 vp
= (struct vnode
*)fp
->f_data
;
2424 if (vp
->v_tag
!= VT_HFS
) {
2430 if (vnode_getwithref(vp
)) {
2435 /* only proceed if you have write access */
2436 vfs_context_t ctx
= vfs_context_current();
2437 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2444 /* If arg != 0, set, otherwise unset */
2446 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
)1, 0, &context
);
2449 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
2457 * like F_GETPATH, but special semantics for
2458 * the mobile time machine handler.
2460 case F_GETPATH_MTMINFO
: {
2464 if (fp
->f_type
!= DTYPE_VNODE
) {
2468 vp
= (struct vnode
*)fp
->f_data
;
2471 pathlen
= MAXPATHLEN
;
2472 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
2473 if (pathbufp
== NULL
) {
2477 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
2478 int backingstore
= 0;
2480 /* Check for error from vn_getpath before moving on */
2481 if ((error
= vn_getpath(vp
, pathbufp
, &pathlen
)) == 0) {
2482 if (vp
->v_tag
== VT_HFS
) {
2483 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
) &backingstore
, 0, &context
);
2485 (void)vnode_put(vp
);
2488 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
2492 * If the copyout was successful, now check to ensure
2493 * that this vnode is not a BACKINGSTORE vnode. mtmd
2494 * wants the path regardless.
2501 (void)vnode_put(vp
);
2503 FREE(pathbufp
, M_TEMP
);
2507 #if DEBUG || DEVELOPMENT
2509 if (fp
->f_type
!= DTYPE_VNODE
) {
2513 vp
= (struct vnode
*)fp
->f_data
;
2522 * This is an fcntl() that we d not recognize at this level;
2523 * if this is a vnode, we send it down into the VNOP_IOCTL
2524 * for this vnode; this can include special devices, and will
2525 * effectively overload fcntl() to send ioctl()'s.
2527 if((uap
->cmd
& IOC_VOID
) && (uap
->cmd
& IOC_INOUT
)){
2532 /* Catch any now-invalid fcntl() selectors */
2534 case F_MARKDEPENDENCY
:
2541 if (fp
->f_type
!= DTYPE_VNODE
) {
2545 vp
= (struct vnode
*)fp
->f_data
;
2548 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
2549 #define STK_PARAMS 128
2550 char stkbuf
[STK_PARAMS
];
2554 * For this to work properly, we have to copy in the
2555 * ioctl() cmd argument if there is one; we must also
2556 * check that a command parameter, if present, does
2557 * not exceed the maximum command length dictated by
2558 * the number of bits we have available in the command
2559 * to represent a structure length. Finally, we have
2560 * to copy the results back out, if it is that type of
2563 size
= IOCPARM_LEN(uap
->cmd
);
2564 if (size
> IOCPARM_MAX
) {
2565 (void)vnode_put(vp
);
2571 if (size
> sizeof (stkbuf
)) {
2572 if ((memp
= (caddr_t
)kalloc(size
)) == 0) {
2573 (void)vnode_put(vp
);
2582 if (uap
->cmd
& IOC_IN
) {
2585 error
= copyin(argp
, data
, size
);
2587 (void)vnode_put(vp
);
2593 /* Bzero the section beyond that which was needed */
2594 if (size
<= sizeof(stkbuf
)) {
2595 bzero ( (((uint8_t*)data
) + size
), (sizeof(stkbuf
) - size
));
2600 *(user_addr_t
*)data
= argp
;
2602 *(uint32_t *)data
= (uint32_t)argp
;
2605 } else if ((uap
->cmd
& IOC_OUT
) && size
) {
2607 * Zero the buffer so the user always
2608 * gets back something deterministic.
2611 } else if (uap
->cmd
& IOC_VOID
) {
2613 *(user_addr_t
*)data
= argp
;
2615 *(uint32_t *)data
= (uint32_t)argp
;
2619 error
= VNOP_IOCTL(vp
, uap
->cmd
, CAST_DOWN(caddr_t
, data
), 0, &context
);
2621 (void)vnode_put(vp
);
2623 /* Copy any output data to user */
2624 if (error
== 0 && (uap
->cmd
& IOC_OUT
) && size
)
2625 error
= copyout(data
, argp
, size
);
2633 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
2634 fp_drop(p
, fd
, fp
, 0);
2637 fp_drop(p
, fd
, fp
, 1);
2646 * Description: Common code for dup, dup2, and fcntl(F_DUPFD).
2648 * Parameters: p Process performing the dup
2650 * new The fd to dup it to
2651 * fd_flags Flags to augment the new fd
2652 * retval Pointer to the call return area
2654 * Returns: 0 Success
2659 * *retval (modified) The new descriptor
2661 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
2664 * Notes: This function may drop and reacquire this lock; it is unsafe
2665 * for a caller to assume that other state protected by the lock
2666 * has not been subsequently changed out from under it.
2670 struct filedesc
*fdp
, int old
, int new, int fd_flags
, int32_t *retval
)
2672 struct fileproc
*nfp
;
2673 struct fileproc
*ofp
;
2679 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2681 if ((ofp
= fdp
->fd_ofiles
[old
]) == NULL
||
2682 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
)) {
2689 error
= mac_file_check_dup(proc_ucred(p
), ofp
->f_fglob
, new);
2699 nfp
= fileproc_alloc_init(NULL
);
2709 nfp
->f_fglob
= ofp
->f_fglob
;
2712 if (fdp
->fd_ofiles
[new] != 0)
2713 panic("finishdup: overwriting fd_ofiles with new %d", new);
2714 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0)
2715 panic("finishdup: unreserved fileflags with new %d", new);
2718 if (new > fdp
->fd_lastfile
)
2719 fdp
->fd_lastfile
= new;
2720 *fdflags(p
, new) |= fd_flags
;
2721 procfdtbl_releasefd(p
, new, nfp
);
2730 * Description: The implementation of the close(2) system call
2732 * Parameters: p Process in whose per process file table
2733 * the close is to occur
2734 * uap->fd fd to be closed
2737 * Returns: 0 Success
2738 * fp_lookup:EBADF Bad file descriptor
2739 * fp_guard_exception:??? Guarded file descriptor
2740 * close_internal:EBADF
2741 * close_internal:??? Anything returnable by a per-fileops
2745 close(proc_t p
, struct close_args
*uap
, int32_t *retval
)
2747 __pthread_testcancel(1);
2748 return(close_nocancel(p
, (struct close_nocancel_args
*)uap
, retval
));
2753 close_nocancel(proc_t p
, struct close_nocancel_args
*uap
, __unused
int32_t *retval
)
2755 struct fileproc
*fp
;
2759 AUDIT_SYSCLOSE(p
, fd
);
2763 if ( (error
= fp_lookup(p
,fd
,&fp
, 1)) ) {
2768 if (FP_ISGUARDED(fp
, GUARD_CLOSE
)) {
2769 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_CLOSE
);
2770 (void) fp_drop(p
, fd
, fp
, 1);
2775 error
= close_internal_locked(p
, fd
, fp
, 0);
2784 * close_internal_locked
2786 * Close a file descriptor.
2788 * Parameters: p Process in whose per process file table
2789 * the close is to occur
2790 * fd fd to be closed
2791 * fp fileproc associated with the fd
2793 * Returns: 0 Success
2794 * EBADF fd already in close wait state
2795 * closef_locked:??? Anything returnable by a per-fileops
2798 * Locks: Assumes proc_fdlock for process is held by the caller and returns
2801 * Notes: This function may drop and reacquire this lock; it is unsafe
2802 * for a caller to assume that other state protected by the lock
2803 * has not been subsequently changed out from under it.
2806 close_internal_locked(proc_t p
, int fd
, struct fileproc
*fp
, int flags
)
2808 struct filedesc
*fdp
= p
->p_fd
;
2810 int resvfd
= flags
& FD_DUP2RESV
;
2814 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2817 /* Keep people from using the filedesc while we are closing it */
2818 procfdtbl_markclosefd(p
, fd
);
2821 if ((fp
->f_flags
& FP_CLOSING
) == FP_CLOSING
) {
2822 panic("close_internal_locked: being called on already closing fd");
2827 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
2828 panic("close_internal: unreserved fileflags with fd %d", fd
);
2831 fp
->f_flags
|= FP_CLOSING
;
2833 if ( (fp
->f_flags
& FP_AIOISSUED
) || kauth_authorize_fileop_has_listeners() ) {
2837 if ( (fp
->f_type
== DTYPE_VNODE
) && kauth_authorize_fileop_has_listeners() ) {
2839 * call out to allow 3rd party notification of close.
2840 * Ignore result of kauth_authorize_fileop call.
2842 if (vnode_getwithref((vnode_t
)fp
->f_data
) == 0) {
2843 u_int fileop_flags
= 0;
2844 if ((fp
->f_flags
& FP_WRITTEN
) != 0)
2845 fileop_flags
|= KAUTH_FILEOP_CLOSE_MODIFIED
;
2846 kauth_authorize_fileop(fp
->f_fglob
->fg_cred
, KAUTH_FILEOP_CLOSE
,
2847 (uintptr_t)fp
->f_data
, (uintptr_t)fileop_flags
);
2848 vnode_put((vnode_t
)fp
->f_data
);
2851 if (fp
->f_flags
& FP_AIOISSUED
)
2853 * cancel all async IO requests that can be cancelled.
2855 _aio_close( p
, fd
);
2860 if (fd
< fdp
->fd_knlistsize
)
2861 knote_fdclose(p
, fd
);
2863 if (fp
->f_flags
& FP_WAITEVENT
)
2864 (void)waitevent_close(p
, fp
);
2866 fileproc_drain(p
, fp
);
2871 procfdtbl_reservefd(p
, fd
);
2874 if (ENTR_SHOULDTRACE
&& fp
->f_type
== DTYPE_SOCKET
)
2875 KERNEL_ENERGYTRACE(kEnTrActKernSocket
, DBG_FUNC_END
,
2876 fd
, 0, (int64_t)VM_KERNEL_ADDRPERM(fp
->f_data
));
2878 error
= closef_locked(fp
, fp
->f_fglob
, p
);
2879 if ((fp
->f_flags
& FP_WAITCLOSE
) == FP_WAITCLOSE
)
2880 wakeup(&fp
->f_flags
);
2881 fp
->f_flags
&= ~(FP_WAITCLOSE
| FP_CLOSING
);
2891 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
2892 panic("close with reserved fd returns with freed fd:%d: proc: %p", fd
, p
);
2903 * Description: Return status information about a file descriptor.
2905 * Parameters: p The process doing the fstat
2907 * ub The user stat buffer
2908 * xsecurity The user extended security
2909 * buffer, or 0 if none
2910 * xsecurity_size The size of xsecurity, or 0
2912 * isstat64 Flag to indicate 64 bit version
2913 * for inode size, etc.
2915 * Returns: 0 Success
2918 * fp_lookup:EBADF Bad file descriptor
2919 * vnode_getwithref:???
2921 * vnode_getwithref:???
2928 * Notes: Internal implementation for all other fstat() related
2931 * XXX switch on node type is bogus; need a stat in struct
2932 * XXX fileops instead.
2935 fstat1(proc_t p
, int fd
, user_addr_t ub
, user_addr_t xsecurity
, user_addr_t xsecurity_size
, int isstat64
)
2937 struct fileproc
*fp
;
2943 struct user64_stat user64_sb
;
2944 struct user32_stat user32_sb
;
2945 struct user64_stat64 user64_sb64
;
2946 struct user32_stat64 user32_sb64
;
2951 kauth_filesec_t fsec
;
2952 user_size_t xsecurity_bufsize
;
2953 vfs_context_t ctx
= vfs_context_current();
2959 if ((error
= fp_lookup(p
, fd
, &fp
, 0)) != 0) {
2964 fsec
= KAUTH_FILESEC_NONE
;
2966 sbptr
= (void *)&source
;
2971 if ((error
= vnode_getwithref((vnode_t
)data
)) == 0) {
2973 * If the caller has the file open, and is not
2974 * requesting extended security information, we are
2975 * going to let them get the basic stat information.
2977 if (xsecurity
== USER_ADDR_NULL
) {
2978 error
= vn_stat_noauth((vnode_t
)data
, sbptr
, NULL
, isstat64
, ctx
);
2980 error
= vn_stat((vnode_t
)data
, sbptr
, &fsec
, isstat64
, ctx
);
2983 AUDIT_ARG(vnpath
, (struct vnode
*)data
, ARG_VNODE1
);
2984 (void)vnode_put((vnode_t
)data
);
2990 error
= soo_stat((struct socket
*)data
, sbptr
, isstat64
);
2992 #endif /* SOCKETS */
2995 error
= pipe_stat((void *)data
, sbptr
, isstat64
);
2999 error
= pshm_stat((void *)data
, sbptr
, isstat64
);
3003 error
= kqueue_stat((void *)data
, sbptr
, isstat64
, p
);
3013 if (isstat64
!= 0) {
3014 source
.sb64
.st_lspare
= 0;
3015 source
.sb64
.st_qspare
[0] = 0LL;
3016 source
.sb64
.st_qspare
[1] = 0LL;
3018 if (IS_64BIT_PROCESS(current_proc())) {
3019 munge_user64_stat64(&source
.sb64
, &dest
.user64_sb64
);
3020 my_size
= sizeof(dest
.user64_sb64
);
3021 sbp
= (caddr_t
)&dest
.user64_sb64
;
3023 munge_user32_stat64(&source
.sb64
, &dest
.user32_sb64
);
3024 my_size
= sizeof(dest
.user32_sb64
);
3025 sbp
= (caddr_t
)&dest
.user32_sb64
;
3028 source
.sb
.st_lspare
= 0;
3029 source
.sb
.st_qspare
[0] = 0LL;
3030 source
.sb
.st_qspare
[1] = 0LL;
3031 if (IS_64BIT_PROCESS(current_proc())) {
3032 munge_user64_stat(&source
.sb
, &dest
.user64_sb
);
3033 my_size
= sizeof(dest
.user64_sb
);
3034 sbp
= (caddr_t
)&dest
.user64_sb
;
3036 munge_user32_stat(&source
.sb
, &dest
.user32_sb
);
3037 my_size
= sizeof(dest
.user32_sb
);
3038 sbp
= (caddr_t
)&dest
.user32_sb
;
3042 error
= copyout(sbp
, ub
, my_size
);
3045 /* caller wants extended security information? */
3046 if (xsecurity
!= USER_ADDR_NULL
) {
3048 /* did we get any? */
3049 if (fsec
== KAUTH_FILESEC_NONE
) {
3050 if (susize(xsecurity_size
, 0) != 0) {
3055 /* find the user buffer size */
3056 xsecurity_bufsize
= fusize(xsecurity_size
);
3058 /* copy out the actual data size */
3059 if (susize(xsecurity_size
, KAUTH_FILESEC_COPYSIZE(fsec
)) != 0) {
3064 /* if the caller supplied enough room, copy out to it */
3065 if (xsecurity_bufsize
>= KAUTH_FILESEC_COPYSIZE(fsec
))
3066 error
= copyout(fsec
, xsecurity
, KAUTH_FILESEC_COPYSIZE(fsec
));
3070 fp_drop(p
, fd
, fp
, 0);
3072 kauth_filesec_free(fsec
);
3080 * Description: Extended version of fstat supporting returning extended
3081 * security information
3083 * Parameters: p The process doing the fstat
3084 * uap->fd The fd to stat
3085 * uap->ub The user stat buffer
3086 * uap->xsecurity The user extended security
3087 * buffer, or 0 if none
3088 * uap->xsecurity_size The size of xsecurity, or 0
3090 * Returns: 0 Success
3091 * !0 Errno (see fstat1)
3094 fstat_extended(proc_t p
, struct fstat_extended_args
*uap
, __unused
int32_t *retval
)
3096 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 0));
3103 * Description: Get file status for the file associated with fd
3105 * Parameters: p The process doing the fstat
3106 * uap->fd The fd to stat
3107 * uap->ub The user stat buffer
3109 * Returns: 0 Success
3110 * !0 Errno (see fstat1)
3113 fstat(proc_t p
, register struct fstat_args
*uap
, __unused
int32_t *retval
)
3115 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 0));
3122 * Description: Extended version of fstat64 supporting returning extended
3123 * security information
3125 * Parameters: p The process doing the fstat
3126 * uap->fd The fd to stat
3127 * uap->ub The user stat buffer
3128 * uap->xsecurity The user extended security
3129 * buffer, or 0 if none
3130 * uap->xsecurity_size The size of xsecurity, or 0
3132 * Returns: 0 Success
3133 * !0 Errno (see fstat1)
3136 fstat64_extended(proc_t p
, struct fstat64_extended_args
*uap
, __unused
int32_t *retval
)
3138 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 1));
3145 * Description: Get 64 bit version of the file status for the file associated
3148 * Parameters: p The process doing the fstat
3149 * uap->fd The fd to stat
3150 * uap->ub The user stat buffer
3152 * Returns: 0 Success
3153 * !0 Errno (see fstat1)
3156 fstat64(proc_t p
, register struct fstat64_args
*uap
, __unused
int32_t *retval
)
3158 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 1));
3165 * Description: Return pathconf information about a file descriptor.
3167 * Parameters: p Process making the request
3168 * uap->fd fd to get information about
3169 * uap->name Name of information desired
3170 * retval Pointer to the call return area
3172 * Returns: 0 Success
3174 * fp_lookup:EBADF Bad file descriptor
3175 * vnode_getwithref:???
3179 * *retval (modified) Returned information (numeric)
3182 fpathconf(proc_t p
, struct fpathconf_args
*uap
, int32_t *retval
)
3185 struct fileproc
*fp
;
3192 AUDIT_ARG(fd
, uap
->fd
);
3193 if ( (error
= fp_lookup(p
, fd
, &fp
, 0)) )
3201 if (uap
->name
!= _PC_PIPE_BUF
) {
3210 if (uap
->name
!= _PC_PIPE_BUF
) {
3219 vp
= (struct vnode
*)data
;
3221 if ( (error
= vnode_getwithref(vp
)) == 0) {
3222 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
3224 error
= vn_pathconf(vp
, uap
->name
, retval
, vfs_context_current());
3226 (void)vnode_put(vp
);
3237 fp_drop(p
, fd
, fp
, 0);
3242 * Statistics counter for the number of times a process calling fdalloc()
3243 * has resulted in an expansion of the per process open file table.
3245 * XXX This would likely be of more use if it were per process
3253 * Description: Allocate a file descriptor for the process.
3255 * Parameters: p Process to allocate the fd in
3256 * want The fd we would prefer to get
3257 * result Pointer to fd we got
3259 * Returns: 0 Success
3264 * *result (modified) The fd which was allocated
3267 fdalloc(proc_t p
, int want
, int *result
)
3269 struct filedesc
*fdp
= p
->p_fd
;
3271 int lim
, last
, numfiles
, oldnfiles
;
3272 struct fileproc
**newofiles
, **ofiles
;
3273 char *newofileflags
;
3276 * Search for a free descriptor starting at the higher
3277 * of want or fd_freefile. If that fails, consider
3278 * expanding the ofile array.
3281 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
3284 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
3286 last
= min(fdp
->fd_nfiles
, lim
);
3287 if ((i
= want
) < fdp
->fd_freefile
)
3288 i
= fdp
->fd_freefile
;
3289 for (; i
< last
; i
++) {
3290 if (fdp
->fd_ofiles
[i
] == NULL
&& !(fdp
->fd_ofileflags
[i
] & UF_RESERVED
)) {
3291 procfdtbl_reservefd(p
, i
);
3292 if (i
> fdp
->fd_lastfile
)
3293 fdp
->fd_lastfile
= i
;
3294 if (want
<= fdp
->fd_freefile
)
3295 fdp
->fd_freefile
= i
;
3302 * No space in current array. Expand?
3304 if (fdp
->fd_nfiles
>= lim
)
3306 if (fdp
->fd_nfiles
< NDEXTENT
)
3307 numfiles
= NDEXTENT
;
3309 numfiles
= 2 * fdp
->fd_nfiles
;
3314 MALLOC_ZONE(newofiles
, struct fileproc
**,
3315 numfiles
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
3317 if (newofiles
== NULL
) {
3320 if (fdp
->fd_nfiles
>= numfiles
) {
3321 FREE_ZONE(newofiles
, numfiles
* OFILESIZE
, M_OFILETABL
);
3324 newofileflags
= (char *) &newofiles
[numfiles
];
3326 * Copy the existing ofile and ofileflags arrays
3327 * and zero the new portion of each array.
3329 oldnfiles
= fdp
->fd_nfiles
;
3330 (void) memcpy(newofiles
, fdp
->fd_ofiles
,
3331 oldnfiles
* sizeof(*fdp
->fd_ofiles
));
3332 (void) memset(&newofiles
[oldnfiles
], 0,
3333 (numfiles
- oldnfiles
) * sizeof(*fdp
->fd_ofiles
));
3335 (void) memcpy(newofileflags
, fdp
->fd_ofileflags
,
3336 oldnfiles
* sizeof(*fdp
->fd_ofileflags
));
3337 (void) memset(&newofileflags
[oldnfiles
], 0,
3338 (numfiles
- oldnfiles
) *
3339 sizeof(*fdp
->fd_ofileflags
));
3340 ofiles
= fdp
->fd_ofiles
;
3341 fdp
->fd_ofiles
= newofiles
;
3342 fdp
->fd_ofileflags
= newofileflags
;
3343 fdp
->fd_nfiles
= numfiles
;
3344 FREE_ZONE(ofiles
, oldnfiles
* OFILESIZE
, M_OFILETABL
);
3353 * Description: Check to see whether n user file descriptors are available
3356 * Parameters: p Process to check in
3357 * n The number of fd's desired
3362 * Locks: Assumes proc_fdlock for process is held by the caller
3364 * Notes: The answer only remains valid so long as the proc_fdlock is
3365 * held by the caller.
3368 fdavail(proc_t p
, int n
)
3370 struct filedesc
*fdp
= p
->p_fd
;
3371 struct fileproc
**fpp
;
3375 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
3376 if ((i
= lim
- fdp
->fd_nfiles
) > 0 && (n
-= i
) <= 0)
3378 fpp
= &fdp
->fd_ofiles
[fdp
->fd_freefile
];
3379 flags
= &fdp
->fd_ofileflags
[fdp
->fd_freefile
];
3380 for (i
= fdp
->fd_nfiles
- fdp
->fd_freefile
; --i
>= 0; fpp
++, flags
++)
3381 if (*fpp
== NULL
&& !(*flags
& UF_RESERVED
) && --n
<= 0)
3390 * Description: Legacy KPI wrapper function for _fdrelse
3392 * Parameters: p Process in which fd lives
3397 * Locks: Assumes proc_fdlock for process is held by the caller
3400 fdrelse(proc_t p
, int fd
)
3409 * Description: Get the fileproc pointer for the given fd from the per process
3410 * open file table without taking an explicit reference on it.
3412 * Parameters: p Process containing fd
3413 * fd fd to obtain fileproc for
3414 * resultfp Pointer to pointer return area
3416 * Returns: 0 Success
3420 * *resultfp (modified) Pointer to fileproc pointer
3422 * Locks: Assumes proc_fdlock for process is held by the caller
3424 * Notes: Because there is no reference explicitly taken, the returned
3425 * fileproc pointer is only valid so long as the proc_fdlock
3426 * remains held by the caller.
3429 fdgetf_noref(proc_t p
, int fd
, struct fileproc
**resultfp
)
3431 struct filedesc
*fdp
= p
->p_fd
;
3432 struct fileproc
*fp
;
3434 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3435 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3436 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3448 * Description: Get fileproc and vnode pointer for a given fd from the per
3449 * process open file table of the specified process, and if
3450 * successful, increment the f_iocount
3452 * Parameters: p Process in which fd lives
3453 * fd fd to get information for
3454 * resultfp Pointer to result fileproc
3455 * pointer area, or 0 if none
3456 * resultvp Pointer to result vnode pointer
3457 * area, or 0 if none
3459 * Returns: 0 Success
3460 * EBADF Bad file descriptor
3461 * ENOTSUP fd does not refer to a vnode
3464 * *resultfp (modified) Fileproc pointer
3465 * *resultvp (modified) vnode pointer
3467 * Notes: The resultfp and resultvp fields are optional, and may be
3468 * independently specified as NULL to skip returning information
3470 * Locks: Internally takes and releases proc_fdlock
3473 fp_getfvp(proc_t p
, int fd
, struct fileproc
**resultfp
, struct vnode
**resultvp
)
3475 struct filedesc
*fdp
= p
->p_fd
;
3476 struct fileproc
*fp
;
3478 proc_fdlock_spin(p
);
3479 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3480 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3481 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3485 if (fp
->f_type
!= DTYPE_VNODE
) {
3494 *resultvp
= (struct vnode
*)fp
->f_data
;
3504 * Description: Get fileproc, vnode pointer, and vid for a given fd from the
3505 * per process open file table of the specified process, and if
3506 * successful, increment the f_iocount
3508 * Parameters: p Process in which fd lives
3509 * fd fd to get information for
3510 * resultfp Pointer to result fileproc
3511 * pointer area, or 0 if none
3512 * resultvp Pointer to result vnode pointer
3513 * area, or 0 if none
3514 * vidp Pointer to resuld vid area
3516 * Returns: 0 Success
3517 * EBADF Bad file descriptor
3518 * ENOTSUP fd does not refer to a vnode
3521 * *resultfp (modified) Fileproc pointer
3522 * *resultvp (modified) vnode pointer
3525 * Notes: The resultfp and resultvp fields are optional, and may be
3526 * independently specified as NULL to skip returning information
3528 * Locks: Internally takes and releases proc_fdlock
3531 fp_getfvpandvid(proc_t p
, int fd
, struct fileproc
**resultfp
,
3532 struct vnode
**resultvp
, uint32_t *vidp
)
3534 struct filedesc
*fdp
= p
->p_fd
;
3535 struct fileproc
*fp
;
3537 proc_fdlock_spin(p
);
3538 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3539 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3540 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3544 if (fp
->f_type
!= DTYPE_VNODE
) {
3553 *resultvp
= (struct vnode
*)fp
->f_data
;
3555 *vidp
= (uint32_t)vnode_vid((struct vnode
*)fp
->f_data
);
3565 * Description: Get fileproc and socket pointer for a given fd from the
3566 * per process open file table of the specified process, and if
3567 * successful, increment the f_iocount
3569 * Parameters: p Process in which fd lives
3570 * fd fd to get information for
3571 * resultfp Pointer to result fileproc
3572 * pointer area, or 0 if none
3573 * results Pointer to result socket
3574 * pointer area, or 0 if none
3576 * Returns: EBADF The file descriptor is invalid
3577 * EOPNOTSUPP The file descriptor is not a socket
3581 * *resultfp (modified) Fileproc pointer
3582 * *results (modified) socket pointer
3584 * Notes: EOPNOTSUPP should probably be ENOTSOCK; this function is only
3585 * ever called from accept1().
3588 fp_getfsock(proc_t p
, int fd
, struct fileproc
**resultfp
,
3589 struct socket
**results
)
3591 struct filedesc
*fdp
= p
->p_fd
;
3592 struct fileproc
*fp
;
3594 proc_fdlock_spin(p
);
3595 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3596 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3597 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3601 if (fp
->f_type
!= DTYPE_SOCKET
) {
3610 *results
= (struct socket
*)fp
->f_data
;
3620 * Description: Get fileproc and kqueue pointer for a given fd from the
3621 * per process open file table of the specified process, and if
3622 * successful, increment the f_iocount
3624 * Parameters: p Process in which fd lives
3625 * fd fd to get information for
3626 * resultfp Pointer to result fileproc
3627 * pointer area, or 0 if none
3628 * resultkq Pointer to result kqueue
3629 * pointer area, or 0 if none
3631 * Returns: EBADF The file descriptor is invalid
3632 * EBADF The file descriptor is not a socket
3636 * *resultfp (modified) Fileproc pointer
3637 * *resultkq (modified) kqueue pointer
3639 * Notes: The second EBADF should probably be something else to make
3640 * the error condition distinct.
3643 fp_getfkq(proc_t p
, int fd
, struct fileproc
**resultfp
,
3644 struct kqueue
**resultkq
)
3646 struct filedesc
*fdp
= p
->p_fd
;
3647 struct fileproc
*fp
;
3649 proc_fdlock_spin(p
);
3650 if ( fd
< 0 || fd
>= fdp
->fd_nfiles
||
3651 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3652 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3656 if (fp
->f_type
!= DTYPE_KQUEUE
) {
3665 *resultkq
= (struct kqueue
*)fp
->f_data
;
3675 * Description: Get fileproc and POSIX shared memory pointer for a given fd
3676 * from the per process open file table of the specified process
3677 * and if successful, increment the f_iocount
3679 * Parameters: p Process in which fd lives
3680 * fd fd to get information for
3681 * resultfp Pointer to result fileproc
3682 * pointer area, or 0 if none
3683 * resultpshm Pointer to result POSIX
3684 * shared memory pointer
3685 * pointer area, or 0 if none
3687 * Returns: EBADF The file descriptor is invalid
3688 * EBADF The file descriptor is not a POSIX
3689 * shared memory area
3693 * *resultfp (modified) Fileproc pointer
3694 * *resultpshm (modified) POSIX shared memory pointer
3696 * Notes: The second EBADF should probably be something else to make
3697 * the error condition distinct.
3700 fp_getfpshm(proc_t p
, int fd
, struct fileproc
**resultfp
,
3701 struct pshmnode
**resultpshm
)
3703 struct filedesc
*fdp
= p
->p_fd
;
3704 struct fileproc
*fp
;
3706 proc_fdlock_spin(p
);
3707 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3708 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3709 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3713 if (fp
->f_type
!= DTYPE_PSXSHM
) {
3723 *resultpshm
= (struct pshmnode
*)fp
->f_data
;
3733 * Description: Get fileproc and POSIX semaphore pointer for a given fd from
3734 * the per process open file table of the specified process
3735 * and if successful, increment the f_iocount
3737 * Parameters: p Process in which fd lives
3738 * fd fd to get information for
3739 * resultfp Pointer to result fileproc
3740 * pointer area, or 0 if none
3741 * resultpsem Pointer to result POSIX
3742 * semaphore pointer area, or
3745 * Returns: EBADF The file descriptor is invalid
3746 * EBADF The file descriptor is not a POSIX
3751 * *resultfp (modified) Fileproc pointer
3752 * *resultpsem (modified) POSIX semaphore pointer
3754 * Notes: The second EBADF should probably be something else to make
3755 * the error condition distinct.
3757 * In order to support unnamed POSIX semaphores, the named
3758 * POSIX semaphores will have to move out of the per-process
3759 * open filetable, and into a global table that is shared with
3760 * unnamed POSIX semaphores, since unnamed POSIX semaphores
3761 * are typically used by declaring instances in shared memory,
3762 * and there's no other way to do this without changing the
3763 * underlying type, which would introduce binary compatibility
3767 fp_getfpsem(proc_t p
, int fd
, struct fileproc
**resultfp
,
3768 struct psemnode
**resultpsem
)
3770 struct filedesc
*fdp
= p
->p_fd
;
3771 struct fileproc
*fp
;
3773 proc_fdlock_spin(p
);
3774 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3775 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3776 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3780 if (fp
->f_type
!= DTYPE_PSXSEM
) {
3789 *resultpsem
= (struct psemnode
*)fp
->f_data
;
3799 * Description: Get fileproc and pipe pointer for a given fd from the
3800 * per process open file table of the specified process
3801 * and if successful, increment the f_iocount
3803 * Parameters: p Process in which fd lives
3804 * fd fd to get information for
3805 * resultfp Pointer to result fileproc
3806 * pointer area, or 0 if none
3807 * resultpipe Pointer to result pipe
3808 * pointer area, or 0 if none
3810 * Returns: EBADF The file descriptor is invalid
3811 * EBADF The file descriptor is not a socket
3815 * *resultfp (modified) Fileproc pointer
3816 * *resultpipe (modified) pipe pointer
3818 * Notes: The second EBADF should probably be something else to make
3819 * the error condition distinct.
3822 fp_getfpipe(proc_t p
, int fd
, struct fileproc
**resultfp
,
3823 struct pipe
**resultpipe
)
3825 struct filedesc
*fdp
= p
->p_fd
;
3826 struct fileproc
*fp
;
3828 proc_fdlock_spin(p
);
3829 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3830 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3831 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3835 if (fp
->f_type
!= DTYPE_PIPE
) {
3844 *resultpipe
= (struct pipe
*)fp
->f_data
;
3853 * Description: Get fileproc pointer for a given fd from the per process
3854 * open file table of the specified process and if successful,
3855 * increment the f_iocount
3857 * Parameters: p Process in which fd lives
3858 * fd fd to get information for
3859 * resultfp Pointer to result fileproc
3860 * pointer area, or 0 if none
3861 * locked !0 if the caller holds the
3862 * proc_fdlock, 0 otherwise
3864 * Returns: 0 Success
3865 * EBADF Bad file descriptor
3868 * *resultfp (modified) Fileproc pointer
3870 * Locks: If the argument 'locked' is non-zero, then the caller is
3871 * expected to have taken and held the proc_fdlock; if it is
3872 * zero, than this routine internally takes and drops this lock.
3875 fp_lookup(proc_t p
, int fd
, struct fileproc
**resultfp
, int locked
)
3877 struct filedesc
*fdp
= p
->p_fd
;
3878 struct fileproc
*fp
;
3881 proc_fdlock_spin(p
);
3882 if (fd
< 0 || fdp
== NULL
|| fd
>= fdp
->fd_nfiles
||
3883 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3884 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3903 * Description: Swap the fileproc pointer for a given fd with a new
3904 * fileproc pointer in the per-process open file table of
3905 * the specified process. The fdlock must be held at entry.
3907 * Parameters: p Process containing the fd
3908 * fd The fd of interest
3909 * nfp Pointer to the newfp
3911 * Returns: 0 Success
3912 * EBADF Bad file descriptor
3914 * EKEEPLOOKING f_iocount changed while lock was dropped.
3917 fp_tryswap(proc_t p
, int fd
, struct fileproc
*nfp
)
3919 struct fileproc
*fp
;
3922 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
3924 if (0 != (error
= fp_lookup(p
, fd
, &fp
, 1)))
3927 * At this point, our caller (change_guardedfd_np) has
3928 * one f_iocount reference, and we just took another
3929 * one to begin the replacement.
3931 if (fp
->f_iocount
< 2) {
3932 panic("f_iocount too small %d", fp
->f_iocount
);
3933 } else if (2 == fp
->f_iocount
) {
3935 /* Copy the contents of *fp, preserving the "type" of *nfp */
3937 nfp
->f_flags
= (nfp
->f_flags
& FP_TYPEMASK
) |
3938 (fp
->f_flags
& ~FP_TYPEMASK
);
3939 nfp
->f_iocount
= fp
->f_iocount
;
3940 nfp
->f_fglob
= fp
->f_fglob
;
3941 nfp
->f_wset
= fp
->f_wset
;
3943 p
->p_fd
->fd_ofiles
[fd
] = nfp
;
3944 (void) fp_drop(p
, fd
, nfp
, 1);
3947 * Wait for all other active references to evaporate.
3949 p
->p_fpdrainwait
= 1;
3950 error
= msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
,
3951 PRIBIO
| PCATCH
, "tryswap fpdrain", NULL
);
3954 * Return an "internal" errno to trigger a full
3955 * reevaluation of the change-guard attempt.
3957 error
= EKEEPLOOKING
;
3958 printf("%s: lookup collision fd %d\n", __func__
, fd
);
3960 (void) fp_drop(p
, fd
, fp
, 1);
3969 * Description: Set the FP_WRITTEN flag on the fileproc and drop the I/O
3970 * reference previously taken by calling fp_lookup et. al.
3972 * Parameters: p Process in which the fd lives
3973 * fd fd associated with the fileproc
3974 * fp fileproc on which to set the
3975 * flag and drop the reference
3977 * Returns: 0 Success
3978 * fp_drop:EBADF Bad file descriptor
3980 * Locks: This function internally takes and drops the proc_fdlock for
3981 * the supplied process
3983 * Notes: The fileproc must correspond to the fd in the supplied proc
3986 fp_drop_written(proc_t p
, int fd
, struct fileproc
*fp
)
3990 proc_fdlock_spin(p
);
3992 fp
->f_flags
|= FP_WRITTEN
;
3994 error
= fp_drop(p
, fd
, fp
, 1);
4005 * Description: Set the FP_WAITEVENT flag on the fileproc and drop the I/O
4006 * reference previously taken by calling fp_lookup et. al.
4008 * Parameters: p Process in which the fd lives
4009 * fd fd associated with the fileproc
4010 * fp fileproc on which to set the
4011 * flag and drop the reference
4013 * Returns: 0 Success
4014 * fp_drop:EBADF Bad file descriptor
4016 * Locks: This function internally takes and drops the proc_fdlock for
4017 * the supplied process
4019 * Notes: The fileproc must correspond to the fd in the supplied proc
4022 fp_drop_event(proc_t p
, int fd
, struct fileproc
*fp
)
4026 proc_fdlock_spin(p
);
4028 fp
->f_flags
|= FP_WAITEVENT
;
4030 error
= fp_drop(p
, fd
, fp
, 1);
4041 * Description: Drop the I/O reference previously taken by calling fp_lookup
4044 * Parameters: p Process in which the fd lives
4045 * fd fd associated with the fileproc
4046 * fp fileproc on which to set the
4047 * flag and drop the reference
4048 * locked flag to internally take and
4049 * drop proc_fdlock if it is not
4050 * already held by the caller
4052 * Returns: 0 Success
4053 * EBADF Bad file descriptor
4055 * Locks: This function internally takes and drops the proc_fdlock for
4056 * the supplied process if 'locked' is non-zero, and assumes that
4057 * the caller already holds this lock if 'locked' is non-zero.
4059 * Notes: The fileproc must correspond to the fd in the supplied proc
4062 fp_drop(proc_t p
, int fd
, struct fileproc
*fp
, int locked
)
4064 struct filedesc
*fdp
= p
->p_fd
;
4068 proc_fdlock_spin(p
);
4069 if ((fp
== FILEPROC_NULL
) && (fd
< 0 || fd
>= fdp
->fd_nfiles
||
4070 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
4071 ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
4072 !(fdp
->fd_ofileflags
[fd
] & UF_CLOSING
)))) {
4079 if (fp
->f_iocount
== 0) {
4080 if (fp
->f_flags
& FP_SELCONFLICT
)
4081 fp
->f_flags
&= ~FP_SELCONFLICT
;
4083 if (p
->p_fpdrainwait
) {
4084 p
->p_fpdrainwait
= 0;
4091 wakeup(&p
->p_fpdrainwait
);
4100 * Description: Given an fd, look it up in the current process's per process
4101 * open file table, and return its internal vnode pointer.
4103 * Parameters: fd fd to obtain vnode from
4104 * vpp pointer to vnode return area
4106 * Returns: 0 Success
4107 * EINVAL The fd does not refer to a
4108 * vnode fileproc entry
4109 * fp_lookup:EBADF Bad file descriptor
4112 * *vpp (modified) Returned vnode pointer
4114 * Locks: This function internally takes and drops the proc_fdlock for
4115 * the current process
4117 * Notes: If successful, this function increments the f_iocount on the
4118 * fd's corresponding fileproc.
4120 * The fileproc referenced is not returned; because of this, care
4121 * must be taken to not drop the last reference (e.g. by closing
4122 * the file). This is inherently unsafe, since the reference may
4123 * not be recoverable from the vnode, if there is a subsequent
4124 * close that destroys the associate fileproc. The caller should
4125 * therefore retain their own reference on the fileproc so that
4126 * the f_iocount can be dropped subsequently. Failure to do this
4127 * can result in the returned pointer immediately becoming invalid
4128 * following the call.
4130 * Use of this function is discouraged.
4133 file_vnode(int fd
, struct vnode
**vpp
)
4135 proc_t p
= current_proc();
4136 struct fileproc
*fp
;
4139 proc_fdlock_spin(p
);
4140 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4144 if (fp
->f_type
!= DTYPE_VNODE
) {
4145 fp_drop(p
, fd
, fp
,1);
4150 *vpp
= (struct vnode
*)fp
->f_data
;
4158 * file_vnode_withvid
4160 * Description: Given an fd, look it up in the current process's per process
4161 * open file table, and return its internal vnode pointer.
4163 * Parameters: fd fd to obtain vnode from
4164 * vpp pointer to vnode return area
4165 * vidp pointer to vid of the returned vnode
4167 * Returns: 0 Success
4168 * EINVAL The fd does not refer to a
4169 * vnode fileproc entry
4170 * fp_lookup:EBADF Bad file descriptor
4173 * *vpp (modified) Returned vnode pointer
4175 * Locks: This function internally takes and drops the proc_fdlock for
4176 * the current process
4178 * Notes: If successful, this function increments the f_iocount on the
4179 * fd's corresponding fileproc.
4181 * The fileproc referenced is not returned; because of this, care
4182 * must be taken to not drop the last reference (e.g. by closing
4183 * the file). This is inherently unsafe, since the reference may
4184 * not be recoverable from the vnode, if there is a subsequent
4185 * close that destroys the associate fileproc. The caller should
4186 * therefore retain their own reference on the fileproc so that
4187 * the f_iocount can be dropped subsequently. Failure to do this
4188 * can result in the returned pointer immediately becoming invalid
4189 * following the call.
4191 * Use of this function is discouraged.
4194 file_vnode_withvid(int fd
, struct vnode
**vpp
, uint32_t * vidp
)
4196 proc_t p
= current_proc();
4197 struct fileproc
*fp
;
4201 proc_fdlock_spin(p
);
4202 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4206 if (fp
->f_type
!= DTYPE_VNODE
) {
4207 fp_drop(p
, fd
, fp
,1);
4211 vp
= (struct vnode
*)fp
->f_data
;
4215 if ((vidp
!= NULL
) && (vp
!= NULLVP
))
4216 *vidp
= (uint32_t)vp
->v_id
;
4227 * Description: Given an fd, look it up in the current process's per process
4228 * open file table, and return its internal socket pointer.
4230 * Parameters: fd fd to obtain vnode from
4231 * sp pointer to socket return area
4233 * Returns: 0 Success
4234 * ENOTSOCK Not a socket
4235 * fp_lookup:EBADF Bad file descriptor
4238 * *sp (modified) Returned socket pointer
4240 * Locks: This function internally takes and drops the proc_fdlock for
4241 * the current process
4243 * Notes: If successful, this function increments the f_iocount on the
4244 * fd's corresponding fileproc.
4246 * The fileproc referenced is not returned; because of this, care
4247 * must be taken to not drop the last reference (e.g. by closing
4248 * the file). This is inherently unsafe, since the reference may
4249 * not be recoverable from the socket, if there is a subsequent
4250 * close that destroys the associate fileproc. The caller should
4251 * therefore retain their own reference on the fileproc so that
4252 * the f_iocount can be dropped subsequently. Failure to do this
4253 * can result in the returned pointer immediately becoming invalid
4254 * following the call.
4256 * Use of this function is discouraged.
4259 file_socket(int fd
, struct socket
**sp
)
4261 proc_t p
= current_proc();
4262 struct fileproc
*fp
;
4265 proc_fdlock_spin(p
);
4266 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4270 if (fp
->f_type
!= DTYPE_SOCKET
) {
4271 fp_drop(p
, fd
, fp
,1);
4275 *sp
= (struct socket
*)fp
->f_data
;
4285 * Description: Given an fd, look it up in the current process's per process
4286 * open file table, and return its fileproc's flags field.
4288 * Parameters: fd fd whose flags are to be
4290 * flags pointer to flags data area
4292 * Returns: 0 Success
4293 * ENOTSOCK Not a socket
4294 * fp_lookup:EBADF Bad file descriptor
4297 * *flags (modified) Returned flags field
4299 * Locks: This function internally takes and drops the proc_fdlock for
4300 * the current process
4302 * Notes: This function will internally increment and decrement the
4303 * f_iocount of the fileproc as part of its operation.
4306 file_flags(int fd
, int *flags
)
4309 proc_t p
= current_proc();
4310 struct fileproc
*fp
;
4313 proc_fdlock_spin(p
);
4314 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4318 *flags
= (int)fp
->f_flag
;
4319 fp_drop(p
, fd
, fp
,1);
4329 * Description: Drop an iocount reference on an fd, and wake up any waiters
4330 * for draining (i.e. blocked in fileproc_drain() called during
4331 * the last attempt to close a file).
4333 * Parameters: fd fd on which an ioreference is
4336 * Returns: 0 Success
4337 * EBADF Bad file descriptor
4339 * Description: Given an fd, look it up in the current process's per process
4340 * open file table, and drop it's fileproc's f_iocount by one
4342 * Notes: This is intended as a corresponding operation to the functions
4343 * file_vnode() and file_socket() operations.
4345 * Technically, the close reference is supposed to be protected
4346 * by a fileproc_drain(), however, a drain will only block if
4347 * the fd refers to a character device, and that device has had
4348 * preparefileread() called on it. If it refers to something
4349 * other than a character device, then the drain will occur and
4350 * block each close attempt, rather than merely the last close.
4352 * Since it's possible for an fd that refers to a character
4353 * device to have an intermediate close followed by an open to
4354 * cause a different file to correspond to that descriptor,
4355 * unless there was a cautionary reference taken on the fileproc,
4356 * this is an inherently unsafe function. This happens in the
4357 * case where multiple fd's in a process refer to the same
4358 * character device (e.g. stdin/out/err pointing to a tty, etc.).
4360 * Use of this function is discouraged.
4365 struct fileproc
*fp
;
4366 proc_t p
= current_proc();
4369 proc_fdlock_spin(p
);
4370 if (fd
< 0 || fd
>= p
->p_fd
->fd_nfiles
||
4371 (fp
= p
->p_fd
->fd_ofiles
[fd
]) == NULL
||
4372 ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
4373 !(p
->p_fd
->fd_ofileflags
[fd
] & UF_CLOSING
))) {
4379 if (fp
->f_iocount
== 0) {
4380 if (fp
->f_flags
& FP_SELCONFLICT
)
4381 fp
->f_flags
&= ~FP_SELCONFLICT
;
4383 if (p
->p_fpdrainwait
) {
4384 p
->p_fpdrainwait
= 0;
4391 wakeup(&p
->p_fpdrainwait
);
4396 static int falloc_withalloc_locked(proc_t
, struct fileproc
**, int *,
4397 vfs_context_t
, struct fileproc
* (*)(void *), void *, int);
4402 * Description: Allocate an entry in the per process open file table and
4403 * return the corresponding fileproc and fd.
4405 * Parameters: p The process in whose open file
4406 * table the fd is to be allocated
4407 * resultfp Pointer to fileproc pointer
4409 * resultfd Pointer to fd return area
4412 * Returns: 0 Success
4413 * falloc:ENFILE Too many open files in system
4414 * falloc:EMFILE Too many open files in process
4415 * falloc:ENOMEM M_FILEPROC or M_FILEGLOB zone
4419 * *resultfd (modified) Returned fileproc pointer
4420 * *resultfd (modified) Returned fd
4422 * Locks: This function takes and drops the proc_fdlock; if this lock
4423 * is already held, use falloc_locked() instead.
4425 * Notes: This function takes separate process and context arguments
4426 * solely to support kern_exec.c; otherwise, it would take
4427 * neither, and expect falloc_locked() to use the
4428 * vfs_context_current() routine internally.
4431 falloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
)
4433 return (falloc_withalloc(p
, resultfp
, resultfd
, ctx
,
4434 fileproc_alloc_init
, NULL
));
4438 * Like falloc, but including the fileproc allocator and create-args
4441 falloc_withalloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4442 vfs_context_t ctx
, fp_allocfn_t fp_zalloc
, void *arg
)
4447 error
= falloc_withalloc_locked(p
,
4448 resultfp
, resultfd
, ctx
, fp_zalloc
, arg
, 1);
4455 * "uninitialized" ops -- ensure fg->fg_ops->fo_type always exists
4457 static const struct fileops uninitops
;
4462 * Create a new open file structure and allocate
4463 * a file descriptor for the process that refers to it.
4465 * Returns: 0 Success
4467 * Description: Allocate an entry in the per process open file table and
4468 * return the corresponding fileproc and fd.
4470 * Parameters: p The process in whose open file
4471 * table the fd is to be allocated
4472 * resultfp Pointer to fileproc pointer
4474 * resultfd Pointer to fd return area
4476 * locked Flag to indicate whether the
4477 * caller holds proc_fdlock
4479 * Returns: 0 Success
4480 * ENFILE Too many open files in system
4481 * fdalloc:EMFILE Too many open files in process
4482 * ENOMEM M_FILEPROC or M_FILEGLOB zone
4487 * *resultfd (modified) Returned fileproc pointer
4488 * *resultfd (modified) Returned fd
4490 * Locks: If the parameter 'locked' is zero, this function takes and
4491 * drops the proc_fdlock; if non-zero, the caller must hold the
4494 * Notes: If you intend to use a non-zero 'locked' parameter, use the
4495 * utility function falloc() instead.
4497 * This function takes separate process and context arguments
4498 * solely to support kern_exec.c; otherwise, it would take
4499 * neither, and use the vfs_context_current() routine internally.
4502 falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4503 vfs_context_t ctx
, int locked
)
4505 return (falloc_withalloc_locked(p
, resultfp
, resultfd
, ctx
,
4506 fileproc_alloc_init
, NULL
, locked
));
4510 falloc_withalloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4511 vfs_context_t ctx
, fp_allocfn_t fp_zalloc
, void *crarg
,
4514 struct fileproc
*fp
;
4515 struct fileglob
*fg
;
4520 if ( (error
= fdalloc(p
, 0, &nfd
)) ) {
4525 if (nfiles
>= maxfiles
) {
4532 error
= mac_file_check_create(proc_ucred(p
));
4541 * Allocate a new file descriptor.
4542 * If the process has file descriptor zero open, add to the list
4543 * of open files at that point, otherwise put it at the front of
4544 * the list of open files.
4548 fp
= (*fp_zalloc
)(crarg
);
4554 MALLOC_ZONE(fg
, struct fileglob
*, sizeof(struct fileglob
), M_FILEGLOB
, M_WAITOK
);
4561 bzero(fg
, sizeof(struct fileglob
));
4562 lck_mtx_init(&fg
->fg_lock
, file_lck_grp
, file_lck_attr
);
4566 fg
->fg_ops
= &uninitops
;
4569 mac_file_label_init(fg
);
4572 kauth_cred_ref(ctx
->vc_ucred
);
4576 fp
->f_cred
= ctx
->vc_ucred
;
4579 mac_file_label_associate(fp
->f_cred
, fg
);
4582 OSAddAtomic(1, &nfiles
);
4584 p
->p_fd
->fd_ofiles
[nfd
] = fp
;
4601 * Description: Free a file structure; drop the global open file count, and
4602 * drop the credential reference, if the fileglob has one, and
4603 * destroy the instance mutex before freeing
4605 * Parameters: fg Pointer to fileglob to be
4611 fg_free(struct fileglob
*fg
)
4613 OSAddAtomic(-1, &nfiles
);
4615 if (fg
->fg_vn_data
) {
4616 fg_vn_data_free(fg
->fg_vn_data
);
4617 fg
->fg_vn_data
= NULL
;
4620 if (IS_VALID_CRED(fg
->fg_cred
)) {
4621 kauth_cred_unref(&fg
->fg_cred
);
4623 lck_mtx_destroy(&fg
->fg_lock
, file_lck_grp
);
4626 mac_file_label_destroy(fg
);
4628 FREE_ZONE(fg
, sizeof *fg
, M_FILEGLOB
);
4635 * Description: Perform close-on-exec processing for all files in a process
4636 * that are either marked as close-on-exec, or which were in the
4637 * process of being opened at the time of the execve
4639 * Also handles the case (via posix_spawn()) where -all-
4640 * files except those marked with "inherit" as treated as
4643 * Parameters: p Pointer to process calling
4648 * Locks: This function internally takes and drops proc_fdlock()
4652 fdexec(proc_t p
, short flags
)
4654 struct filedesc
*fdp
= p
->p_fd
;
4656 boolean_t cloexec_default
= (flags
& POSIX_SPAWN_CLOEXEC_DEFAULT
) != 0;
4659 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
4661 struct fileproc
*fp
= fdp
->fd_ofiles
[i
];
4662 char *flagp
= &fdp
->fd_ofileflags
[i
];
4664 if (fp
&& cloexec_default
) {
4666 * Reverse the usual semantics of file descriptor
4667 * inheritance - all of them should be closed
4668 * except files marked explicitly as "inherit" and
4669 * not marked close-on-exec.
4671 if ((*flagp
& (UF_EXCLOSE
|UF_INHERIT
)) != UF_INHERIT
)
4672 *flagp
|= UF_EXCLOSE
;
4673 *flagp
&= ~UF_INHERIT
;
4677 ((*flagp
& (UF_RESERVED
|UF_EXCLOSE
)) == UF_EXCLOSE
)
4679 || (fp
&& mac_file_check_inherit(proc_ucred(p
), fp
->f_fglob
))
4682 if (i
< fdp
->fd_knlistsize
)
4683 knote_fdclose(p
, i
);
4684 procfdtbl_clearfd(p
, i
);
4685 if (i
== fdp
->fd_lastfile
&& i
> 0)
4687 if (i
< fdp
->fd_freefile
)
4688 fdp
->fd_freefile
= i
;
4691 * Wait for any third party viewers (e.g., lsof)
4692 * to release their references to this fileproc.
4694 while (fp
->f_iocount
> 0) {
4695 p
->p_fpdrainwait
= 1;
4696 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
,
4700 closef_locked(fp
, fp
->f_fglob
, p
);
4712 * Description: Copy a filedesc structure. This is normally used as part of
4713 * forkproc() when forking a new process, to copy the per process
4714 * open file table over to the new process.
4716 * Parameters: p Process whose open file table
4717 * is to be copied (parent)
4718 * uth_cdir Per thread current working
4719 * cirectory, or NULL
4721 * Returns: NULL Copy failed
4722 * !NULL Pointer to new struct filedesc
4724 * Locks: This function internally takes and drops proc_fdlock()
4726 * Notes: Files are copied directly, ignoring the new resource limits
4727 * for the process that's being copied into. Since the descriptor
4728 * references are just additional references, this does not count
4729 * against the number of open files on the system.
4731 * The struct filedesc includes the current working directory,
4732 * and the current root directory, if the process is chroot'ed.
4734 * If the exec was called by a thread using a per thread current
4735 * working directory, we inherit the working directory from the
4736 * thread making the call, rather than from the process.
4738 * In the case of a failure to obtain a reference, for most cases,
4739 * the file entry will be silently dropped. There's an exception
4740 * for the case of a chroot dir, since a failure to to obtain a
4741 * reference there would constitute an "escape" from the chroot
4742 * environment, which must not be allowed. In that case, we will
4743 * deny the execve() operation, rather than allowing the escape.
4746 fdcopy(proc_t p
, vnode_t uth_cdir
)
4748 struct filedesc
*newfdp
, *fdp
= p
->p_fd
;
4750 struct fileproc
*ofp
, *fp
;
4753 MALLOC_ZONE(newfdp
, struct filedesc
*,
4754 sizeof(*newfdp
), M_FILEDESC
, M_WAITOK
);
4761 * the FD_CHROOT flag will be inherited via this copy
4763 (void) memcpy(newfdp
, fdp
, sizeof(*newfdp
));
4766 * If we are running with per-thread current working directories,
4767 * inherit the new current working directory from the current thread
4768 * instead, before we take our references.
4770 if (uth_cdir
!= NULLVP
)
4771 newfdp
->fd_cdir
= uth_cdir
;
4774 * For both fd_cdir and fd_rdir make sure we get
4775 * a valid reference... if we can't, than set
4776 * set the pointer(s) to NULL in the child... this
4777 * will keep us from using a non-referenced vp
4778 * and allows us to do the vnode_rele only on
4779 * a properly referenced vp
4781 if ( (v_dir
= newfdp
->fd_cdir
) ) {
4782 if (vnode_getwithref(v_dir
) == 0) {
4783 if ( (vnode_ref(v_dir
)) )
4784 newfdp
->fd_cdir
= NULL
;
4787 newfdp
->fd_cdir
= NULL
;
4789 if (newfdp
->fd_cdir
== NULL
&& fdp
->fd_cdir
) {
4791 * we couldn't get a new reference on
4792 * the current working directory being
4793 * inherited... we might as well drop
4794 * our reference from the parent also
4795 * since the vnode has gone DEAD making
4796 * it useless... by dropping it we'll
4797 * be that much closer to recycling it
4799 vnode_rele(fdp
->fd_cdir
);
4800 fdp
->fd_cdir
= NULL
;
4803 if ( (v_dir
= newfdp
->fd_rdir
) ) {
4804 if (vnode_getwithref(v_dir
) == 0) {
4805 if ( (vnode_ref(v_dir
)) )
4806 newfdp
->fd_rdir
= NULL
;
4809 newfdp
->fd_rdir
= NULL
;
4812 /* Coming from a chroot environment and unable to get a reference... */
4813 if (newfdp
->fd_rdir
== NULL
&& fdp
->fd_rdir
) {
4815 * We couldn't get a new reference on
4816 * the chroot directory being
4817 * inherited... this is fatal, since
4818 * otherwise it would constitute an
4819 * escape from a chroot environment by
4822 if (newfdp
->fd_cdir
)
4823 vnode_rele(newfdp
->fd_cdir
);
4824 FREE_ZONE(newfdp
, sizeof *newfdp
, M_FILEDESC
);
4829 * If the number of open files fits in the internal arrays
4830 * of the open file structure, use them, otherwise allocate
4831 * additional memory for the number of descriptors currently
4834 if (newfdp
->fd_lastfile
< NDFILE
)
4838 * Compute the smallest multiple of NDEXTENT needed
4839 * for the file descriptors currently in use,
4840 * allowing the table to shrink.
4842 i
= newfdp
->fd_nfiles
;
4843 while (i
> 1 + 2 * NDEXTENT
&& i
> 1 + newfdp
->fd_lastfile
* 2)
4848 MALLOC_ZONE(newfdp
->fd_ofiles
, struct fileproc
**,
4849 i
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
4850 if (newfdp
->fd_ofiles
== NULL
) {
4851 if (newfdp
->fd_cdir
)
4852 vnode_rele(newfdp
->fd_cdir
);
4853 if (newfdp
->fd_rdir
)
4854 vnode_rele(newfdp
->fd_rdir
);
4856 FREE_ZONE(newfdp
, sizeof(*newfdp
), M_FILEDESC
);
4859 (void) memset(newfdp
->fd_ofiles
, 0, i
* OFILESIZE
);
4862 newfdp
->fd_ofileflags
= (char *) &newfdp
->fd_ofiles
[i
];
4863 newfdp
->fd_nfiles
= i
;
4865 if (fdp
->fd_nfiles
> 0) {
4866 struct fileproc
**fpp
;
4869 (void) memcpy(newfdp
->fd_ofiles
, fdp
->fd_ofiles
,
4870 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofiles
));
4871 (void) memcpy(newfdp
->fd_ofileflags
, fdp
->fd_ofileflags
,
4872 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofileflags
));
4875 * kq descriptors cannot be copied.
4877 if (newfdp
->fd_knlistsize
!= -1) {
4878 fpp
= &newfdp
->fd_ofiles
[newfdp
->fd_lastfile
];
4879 flags
= &newfdp
->fd_ofileflags
[newfdp
->fd_lastfile
];
4880 for (i
= newfdp
->fd_lastfile
;
4881 i
>= 0; i
--, fpp
--, flags
--) {
4882 if (*flags
& UF_RESERVED
)
4883 continue; /* (removed below) */
4884 if (*fpp
!= NULL
&& (*fpp
)->f_type
== DTYPE_KQUEUE
) {
4887 if (i
< newfdp
->fd_freefile
)
4888 newfdp
->fd_freefile
= i
;
4890 if (*fpp
== NULL
&& i
== newfdp
->fd_lastfile
&& i
> 0)
4891 newfdp
->fd_lastfile
--;
4893 newfdp
->fd_knlist
= NULL
;
4894 newfdp
->fd_knlistsize
= -1;
4895 newfdp
->fd_knhash
= NULL
;
4896 newfdp
->fd_knhashmask
= 0;
4898 fpp
= newfdp
->fd_ofiles
;
4899 flags
= newfdp
->fd_ofileflags
;
4901 for (i
= newfdp
->fd_lastfile
+ 1; --i
>= 0; fpp
++, flags
++)
4902 if ((ofp
= *fpp
) != NULL
&&
4903 0 == (ofp
->f_fglob
->fg_lflags
& FG_CONFINED
) &&
4904 0 == (*flags
& (UF_FORKCLOSE
|UF_RESERVED
))) {
4906 if (FILEPROC_TYPE(ofp
) != FTYPE_SIMPLE
)
4907 panic("complex fileproc");
4909 fp
= fileproc_alloc_init(NULL
);
4912 * XXX no room to copy, unable to
4913 * XXX safely unwind state at present
4918 (ofp
->f_flags
& ~FP_TYPEMASK
);
4919 fp
->f_fglob
= ofp
->f_fglob
;
4924 if (i
< newfdp
->fd_freefile
)
4925 newfdp
->fd_freefile
= i
;
4939 * Description: Release a filedesc (per process open file table) structure;
4940 * this is done on process exit(), or from forkproc_free() if
4941 * the fork fails for some reason subsequent to a successful
4944 * Parameters: p Pointer to process going away
4948 * Locks: This function internally takes and drops proc_fdlock()
4953 struct filedesc
*fdp
;
4954 struct fileproc
*fp
;
4959 if (p
== kernproc
|| NULL
== (fdp
= p
->p_fd
)) {
4964 extern struct filedesc filedesc0
;
4966 if (&filedesc0
== fdp
)
4969 if (fdp
->fd_nfiles
> 0 && fdp
->fd_ofiles
) {
4970 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
4971 if ((fp
= fdp
->fd_ofiles
[i
]) != NULL
) {
4973 if (fdp
->fd_ofileflags
[i
] & UF_RESERVED
)
4974 panic("fdfree: found fp with UF_RESERVED");
4976 procfdtbl_reservefd(p
, i
);
4978 if (i
< fdp
->fd_knlistsize
)
4979 knote_fdclose(p
, i
);
4980 if (fp
->f_flags
& FP_WAITEVENT
)
4981 (void)waitevent_close(p
, fp
);
4982 (void) closef_locked(fp
, fp
->f_fglob
, p
);
4986 FREE_ZONE(fdp
->fd_ofiles
, fdp
->fd_nfiles
* OFILESIZE
, M_OFILETABL
);
4987 fdp
->fd_ofiles
= NULL
;
4994 vnode_rele(fdp
->fd_cdir
);
4996 vnode_rele(fdp
->fd_rdir
);
4998 proc_fdlock_spin(p
);
5003 FREE(fdp
->fd_knlist
, M_KQUEUE
);
5005 FREE(fdp
->fd_knhash
, M_KQUEUE
);
5007 FREE_ZONE(fdp
, sizeof(*fdp
), M_FILEDESC
);
5013 * Description: Internal form of closef; called with proc_fdlock held
5015 * Parameters: fp Pointer to fileproc for fd
5016 * fg Pointer to fileglob for fd
5017 * p Pointer to proc structure
5019 * Returns: 0 Success
5020 * closef_finish:??? Anything returnable by a per-fileops
5023 * Note: Decrements reference count on file structure; if this was the
5024 * last reference, then closef_finish() is called
5026 * p and fp are allowed to be NULL when closing a file that was
5027 * being passed in a message (but only if we are called when this
5028 * is NOT the last reference).
5031 closef_locked(struct fileproc
*fp
, struct fileglob
*fg
, proc_t p
)
5035 struct vfs_context context
;
5042 /* Set up context with cred stashed in fg */
5043 if (p
== current_proc())
5044 context
.vc_thread
= current_thread();
5046 context
.vc_thread
= NULL
;
5047 context
.vc_ucred
= fg
->fg_cred
;
5050 * POSIX record locking dictates that any close releases ALL
5051 * locks owned by this process. This is handled by setting
5052 * a flag in the unlock to free ONLY locks obeying POSIX
5053 * semantics, and not to free BSD-style file locks.
5054 * If the descriptor was in a message, POSIX-style locks
5055 * aren't passed with the descriptor.
5057 if (p
&& (p
->p_ladvflag
& P_LADVLOCK
) &&
5058 DTYPE_VNODE
== FILEGLOB_DTYPE(fg
)) {
5061 lf
.l_whence
= SEEK_SET
;
5064 lf
.l_type
= F_UNLCK
;
5065 vp
= (struct vnode
*)fg
->fg_data
;
5067 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
5068 (void) VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &lf
, F_POSIX
, &context
, NULL
);
5069 (void)vnode_put(vp
);
5073 lck_mtx_lock_spin(&fg
->fg_lock
);
5076 if (fg
->fg_count
> 0) {
5077 lck_mtx_unlock(&fg
->fg_lock
);
5081 if (fg
->fg_count
!= 0)
5082 panic("fg %p: being freed with bad fg_count (%d)", fg
, fg
->fg_count
);
5085 if (fp
&& (fp
->f_flags
& FP_WRITTEN
))
5086 fg
->fg_flag
|= FWASWRITTEN
;
5088 fg
->fg_lflags
|= FG_TERM
;
5089 lck_mtx_unlock(&fg
->fg_lock
);
5094 /* Since we ensure that fg->fg_ops is always initialized,
5095 * it is safe to invoke fo_close on the fg */
5096 error
= fo_close(fg
, &context
);
5110 * Description: Drain out pending I/O operations
5112 * Parameters: p Process closing this file
5113 * fp fileproc struct for the open
5114 * instance on the file
5118 * Locks: Assumes the caller holds the proc_fdlock
5120 * Notes: For character devices, this occurs on the last close of the
5121 * device; for all other file descriptors, this occurs on each
5122 * close to prevent fd's from being closed out from under
5123 * operations currently in progress and blocked
5125 * See Also: file_vnode(), file_socket(), file_drop(), and the cautions
5126 * regarding their use and interaction with this function.
5129 fileproc_drain(proc_t p
, struct fileproc
* fp
)
5131 struct vfs_context context
;
5133 context
.vc_thread
= proc_thread(p
); /* XXX */
5134 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
5136 fp
->f_iocount
-- ; /* (the one the close holds) */
5138 while (fp
->f_iocount
) {
5140 lck_mtx_convert_spin(&p
->p_fdmlock
);
5142 if (fp
->f_fglob
->fg_ops
->fo_drain
) {
5143 (*fp
->f_fglob
->fg_ops
->fo_drain
)(fp
, &context
);
5145 if ((fp
->f_flags
& FP_INSELECT
) == FP_INSELECT
) {
5146 if (waitq_wakeup64_all((struct waitq
*)fp
->f_wset
, NO_EVENT64
,
5147 THREAD_INTERRUPTED
, WAITQ_ALL_PRIORITIES
) == KERN_INVALID_ARGUMENT
)
5148 panic("bad wait queue for waitq_wakeup64_all %p (fp:%p)", fp
->f_wset
, fp
);
5150 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
) {
5151 if (waitq_wakeup64_all(&select_conflict_queue
, NO_EVENT64
,
5152 THREAD_INTERRUPTED
, WAITQ_ALL_PRIORITIES
) == KERN_INVALID_ARGUMENT
)
5153 panic("bad select_conflict_queue");
5155 p
->p_fpdrainwait
= 1;
5157 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
, "fpdrain", NULL
);
5161 if ((fp
->f_flags
& FP_INSELECT
) != 0)
5162 panic("FP_INSELECT set on drained fp");
5164 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
)
5165 fp
->f_flags
&= ~FP_SELCONFLICT
;
5172 * Description: Release the fd and free the fileproc associated with the fd
5173 * in the per process open file table of the specified process;
5174 * these values must correspond.
5176 * Parameters: p Process containing fd
5177 * fd fd to be released
5178 * fp fileproc to be freed
5180 * Returns: 0 Success
5182 * Notes: XXX function should be void - no one interprets the returns
5186 fp_free(proc_t p
, int fd
, struct fileproc
* fp
)
5188 proc_fdlock_spin(p
);
5192 fg_free(fp
->f_fglob
);
5201 * Description: Apply an advisory lock on a file descriptor.
5203 * Parameters: p Process making request
5204 * uap->fd fd on which the lock is to be
5206 * uap->how (Un)Lock bits, including type
5207 * retval Pointer to the call return area
5209 * Returns: 0 Success
5210 * fp_getfvp:EBADF Bad file descriptor
5211 * fp_getfvp:ENOTSUP fd does not refer to a vnode
5212 * vnode_getwithref:???
5216 * *retval (modified) Size of dtable
5218 * Notes: Just attempt to get a record lock of the requested type on
5219 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
5222 flock(proc_t p
, struct flock_args
*uap
, __unused
int32_t *retval
)
5226 struct fileproc
*fp
;
5229 vfs_context_t ctx
= vfs_context_current();
5232 AUDIT_ARG(fd
, uap
->fd
);
5233 if ( (error
= fp_getfvp(p
, fd
, &fp
, &vp
)) ) {
5236 if ( (error
= vnode_getwithref(vp
)) ) {
5239 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
5241 lf
.l_whence
= SEEK_SET
;
5244 if (how
& LOCK_UN
) {
5245 lf
.l_type
= F_UNLCK
;
5246 fp
->f_flag
&= ~FHASLOCK
;
5247 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_UNLCK
, &lf
, F_FLOCK
, ctx
, NULL
);
5251 lf
.l_type
= F_WRLCK
;
5252 else if (how
& LOCK_SH
)
5253 lf
.l_type
= F_RDLCK
;
5259 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
, F_SETLK
, &lf
);
5263 fp
->f_flag
|= FHASLOCK
;
5264 if (how
& LOCK_NB
) {
5265 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, F_FLOCK
, ctx
, NULL
);
5268 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, F_FLOCK
|F_WAIT
, ctx
, NULL
);
5270 (void)vnode_put(vp
);
5272 fp_drop(p
, fd
, fp
, 0);
5280 * Description: Obtain a Mach send right for a given file descriptor.
5282 * Parameters: p Process calling fileport
5283 * uap->fd The fd to reference
5284 * uap->portnamep User address at which to place port name.
5286 * Returns: 0 Success.
5287 * EBADF Bad file descriptor.
5288 * EINVAL File descriptor had type that cannot be sent, misc. other errors.
5289 * EFAULT Address at which to store port name is not valid.
5290 * EAGAIN Resource shortage.
5293 * On success, name of send right is stored at user-specified address.
5296 fileport_makeport(proc_t p
, struct fileport_makeport_args
*uap
,
5297 __unused
int *retval
)
5301 user_addr_t user_portaddr
= uap
->portnamep
;
5302 struct fileproc
*fp
= FILEPROC_NULL
;
5303 struct fileglob
*fg
= NULL
;
5304 ipc_port_t fileport
;
5305 mach_port_name_t name
= MACH_PORT_NULL
;
5308 err
= fp_lookup(p
, fd
, &fp
, 1);
5313 if (!file_issendable(p
, fp
)) {
5318 if (FP_ISGUARDED(fp
, GUARD_FILEPORT
)) {
5319 err
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_FILEPORT
);
5323 /* Dropped when port is deallocated */
5329 /* Allocate and initialize a port */
5330 fileport
= fileport_alloc(fg
);
5331 if (fileport
== IPC_PORT_NULL
) {
5337 /* Add an entry. Deallocates port on failure. */
5338 name
= ipc_port_copyout_send(fileport
, get_task_ipcspace(p
->task
));
5339 if (!MACH_PORT_VALID(name
)) {
5344 err
= copyout(&name
, user_portaddr
, sizeof(mach_port_name_t
));
5349 /* Tag the fileglob for debugging purposes */
5350 lck_mtx_lock_spin(&fg
->fg_lock
);
5351 fg
->fg_lflags
|= FG_PORTMADE
;
5352 lck_mtx_unlock(&fg
->fg_lock
);
5354 fp_drop(p
, fd
, fp
, 0);
5361 if (MACH_PORT_VALID(name
)) {
5362 /* Don't care if another thread races us to deallocate the entry */
5363 (void) mach_port_deallocate(get_task_ipcspace(p
->task
), name
);
5366 if (fp
!= FILEPROC_NULL
) {
5367 fp_drop(p
, fd
, fp
, 0);
5374 fileport_releasefg(struct fileglob
*fg
)
5376 (void)closef_locked(NULL
, fg
, PROC_NULL
);
5385 * Description: Obtain the file descriptor for a given Mach send right.
5387 * Parameters: p Process calling fileport
5388 * uap->port Name of send right to file port.
5390 * Returns: 0 Success
5391 * EINVAL Invalid Mach port name, or port is not for a file.
5393 * fdalloc:ENOMEM Unable to allocate fileproc or extend file table.
5396 * *retval (modified) The new descriptor
5399 fileport_makefd(proc_t p
, struct fileport_makefd_args
*uap
, int32_t *retval
)
5401 struct fileglob
*fg
;
5402 struct fileproc
*fp
= FILEPROC_NULL
;
5403 ipc_port_t port
= IPC_PORT_NULL
;
5404 mach_port_name_t send
= uap
->port
;
5409 res
= ipc_object_copyin(get_task_ipcspace(p
->task
),
5410 send
, MACH_MSG_TYPE_COPY_SEND
, &port
);
5412 if (res
!= KERN_SUCCESS
) {
5417 fg
= fileport_port_to_fileglob(port
);
5423 fp
= fileproc_alloc_init(NULL
);
5424 if (fp
== FILEPROC_NULL
) {
5433 err
= fdalloc(p
, 0, &fd
);
5438 *fdflags(p
, fd
) |= UF_EXCLOSE
;
5440 procfdtbl_releasefd(p
, fd
, fp
);
5446 if ((fp
!= NULL
) && (0 != err
)) {
5450 if (IPC_PORT_NULL
!= port
) {
5451 ipc_port_release_send(port
);
5461 * Description: Duplicate the specified descriptor to a free descriptor;
5462 * this is the second half of fdopen(), above.
5464 * Parameters: fdp filedesc pointer to fill in
5466 * dfd fd to dup from
5467 * mode mode to set on new fd
5468 * error command code
5470 * Returns: 0 Success
5471 * EBADF Source fd is bad
5472 * EACCES Requested mode not allowed
5473 * !0 'error', if not ENODEV or
5476 * Notes: XXX This is not thread safe; see fdopen() above
5479 dupfdopen(struct filedesc
*fdp
, int indx
, int dfd
, int flags
, int error
)
5481 struct fileproc
*wfp
;
5482 struct fileproc
*fp
;
5486 proc_t p
= current_proc();
5489 * If the to-be-dup'd fd number is greater than the allowed number
5490 * of file descriptors, or the fd to be dup'd has already been
5491 * closed, reject. Note, check for new == old is necessary as
5492 * falloc could allocate an already closed to-be-dup'd descriptor
5493 * as the new descriptor.
5497 fp
= fdp
->fd_ofiles
[indx
];
5498 if (dfd
< 0 || dfd
>= fdp
->fd_nfiles
||
5499 (wfp
= fdp
->fd_ofiles
[dfd
]) == NULL
|| wfp
== fp
||
5500 (fdp
->fd_ofileflags
[dfd
] & UF_RESERVED
)) {
5506 myerror
= mac_file_check_dup(proc_ucred(p
), wfp
->f_fglob
, dfd
);
5513 * There are two cases of interest here.
5515 * For ENODEV simply dup (dfd) to file descriptor
5516 * (indx) and return.
5518 * For ENXIO steal away the file structure from (dfd) and
5519 * store it in (indx). (dfd) is effectively closed by
5522 * Any other error code is just returned.
5526 if (FP_ISGUARDED(wfp
, GUARD_DUP
)) {
5532 * Check that the mode the file is being opened for is a
5533 * subset of the mode of the existing descriptor.
5535 if (((flags
& (FREAD
|FWRITE
)) | wfp
->f_flag
) != wfp
->f_flag
) {
5539 if (indx
> fdp
->fd_lastfile
)
5540 fdp
->fd_lastfile
= indx
;
5544 fg_free(fp
->f_fglob
);
5545 fp
->f_fglob
= wfp
->f_fglob
;
5547 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
] |
5548 (flags
& O_CLOEXEC
) ? UF_EXCLOSE
: 0;
5564 * Description: Add a reference to a fileglob by fileproc
5566 * Parameters: fp fileproc containing fileglob
5571 * Notes: XXX Should use OSAddAtomic?
5574 fg_ref(struct fileproc
* fp
)
5576 struct fileglob
*fg
;
5580 lck_mtx_lock_spin(&fg
->fg_lock
);
5583 if ((fp
->f_flags
& ~((unsigned int)FP_VALID_FLAGS
)) != 0)
5584 panic("fg_ref: invalid bits on fp %p", fp
);
5586 if (fg
->fg_count
== 0)
5587 panic("fg_ref: adding fgcount to zeroed fg: fp %p fg %p",
5591 lck_mtx_unlock(&fg
->fg_lock
);
5598 * Description: Remove a reference to a fileglob by fileproc
5600 * Parameters: fp fileproc containing fileglob
5605 * Notes: XXX Should use OSAddAtomic?
5608 fg_drop(struct fileproc
* fp
)
5610 struct fileglob
*fg
;
5613 lck_mtx_lock_spin(&fg
->fg_lock
);
5615 lck_mtx_unlock(&fg
->fg_lock
);
5620 * fg_insertuipc_mark
5622 * Description: Mark fileglob for insertion onto message queue if needed
5623 * Also takes fileglob reference
5625 * Parameters: fg Fileglob pointer to insert
5627 * Returns: true, if the fileglob needs to be inserted onto msg queue
5629 * Locks: Takes and drops fg_lock, potentially many times
5632 fg_insertuipc_mark(struct fileglob
* fg
)
5634 boolean_t insert
= FALSE
;
5636 lck_mtx_lock_spin(&fg
->fg_lock
);
5637 while (fg
->fg_lflags
& FG_RMMSGQ
) {
5638 lck_mtx_convert_spin(&fg
->fg_lock
);
5640 fg
->fg_lflags
|= FG_WRMMSGQ
;
5641 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_insertuipc", NULL
);
5646 if (fg
->fg_msgcount
== 1) {
5647 fg
->fg_lflags
|= FG_INSMSGQ
;
5650 lck_mtx_unlock(&fg
->fg_lock
);
5657 * Description: Insert marked fileglob onto message queue
5659 * Parameters: fg Fileglob pointer to insert
5663 * Locks: Takes and drops fg_lock & uipc_lock
5664 * DO NOT call this function with proc_fdlock held as unp_gc()
5665 * can potentially try to acquire proc_fdlock, which can result
5666 * in a deadlock if this function is in unp_gc_wait().
5669 fg_insertuipc(struct fileglob
* fg
)
5671 if (fg
->fg_lflags
& FG_INSMSGQ
) {
5672 lck_mtx_lock_spin(uipc_lock
);
5674 LIST_INSERT_HEAD(&fmsghead
, fg
, f_msglist
);
5675 lck_mtx_unlock(uipc_lock
);
5676 lck_mtx_lock(&fg
->fg_lock
);
5677 fg
->fg_lflags
&= ~FG_INSMSGQ
;
5678 if (fg
->fg_lflags
& FG_WINSMSGQ
) {
5679 fg
->fg_lflags
&= ~FG_WINSMSGQ
;
5680 wakeup(&fg
->fg_lflags
);
5682 lck_mtx_unlock(&fg
->fg_lock
);
5687 * fg_removeuipc_mark
5689 * Description: Mark the fileglob for removal from message queue if needed
5690 * Also releases fileglob message queue reference
5692 * Parameters: fg Fileglob pointer to remove
5694 * Returns: true, if the fileglob needs to be removed from msg queue
5696 * Locks: Takes and drops fg_lock, potentially many times
5699 fg_removeuipc_mark(struct fileglob
* fg
)
5701 boolean_t remove
= FALSE
;
5703 lck_mtx_lock_spin(&fg
->fg_lock
);
5704 while (fg
->fg_lflags
& FG_INSMSGQ
) {
5705 lck_mtx_convert_spin(&fg
->fg_lock
);
5707 fg
->fg_lflags
|= FG_WINSMSGQ
;
5708 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_removeuipc", NULL
);
5711 if (fg
->fg_msgcount
== 0) {
5712 fg
->fg_lflags
|= FG_RMMSGQ
;
5715 lck_mtx_unlock(&fg
->fg_lock
);
5722 * Description: Remove marked fileglob from message queue
5724 * Parameters: fg Fileglob pointer to remove
5728 * Locks: Takes and drops fg_lock & uipc_lock
5729 * DO NOT call this function with proc_fdlock held as unp_gc()
5730 * can potentially try to acquire proc_fdlock, which can result
5731 * in a deadlock if this function is in unp_gc_wait().
5734 fg_removeuipc(struct fileglob
* fg
)
5736 if (fg
->fg_lflags
& FG_RMMSGQ
) {
5737 lck_mtx_lock_spin(uipc_lock
);
5739 LIST_REMOVE(fg
, f_msglist
);
5740 lck_mtx_unlock(uipc_lock
);
5741 lck_mtx_lock(&fg
->fg_lock
);
5742 fg
->fg_lflags
&= ~FG_RMMSGQ
;
5743 if (fg
->fg_lflags
& FG_WRMMSGQ
) {
5744 fg
->fg_lflags
&= ~FG_WRMMSGQ
;
5745 wakeup(&fg
->fg_lflags
);
5747 lck_mtx_unlock(&fg
->fg_lock
);
5750 #endif /* SOCKETS */
5755 * Description: Generic fileops read indirected through the fileops pointer
5756 * in the fileproc structure
5758 * Parameters: fp fileproc structure pointer
5759 * uio user I/O structure pointer
5761 * ctx VFS context for operation
5763 * Returns: 0 Success
5764 * !0 Errno from read
5767 fo_read(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
5769 return ((*fp
->f_ops
->fo_read
)(fp
, uio
, flags
, ctx
));
5776 * Description: Generic fileops write indirected through the fileops pointer
5777 * in the fileproc structure
5779 * Parameters: fp fileproc structure pointer
5780 * uio user I/O structure pointer
5782 * ctx VFS context for operation
5784 * Returns: 0 Success
5785 * !0 Errno from write
5788 fo_write(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
5790 return((*fp
->f_ops
->fo_write
)(fp
, uio
, flags
, ctx
));
5797 * Description: Generic fileops ioctl indirected through the fileops pointer
5798 * in the fileproc structure
5800 * Parameters: fp fileproc structure pointer
5802 * data pointer to internalized copy
5803 * of user space ioctl command
5804 * parameter data in kernel space
5805 * ctx VFS context for operation
5807 * Returns: 0 Success
5808 * !0 Errno from ioctl
5810 * Locks: The caller is assumed to have held the proc_fdlock; this
5811 * function releases and reacquires this lock. If the caller
5812 * accesses data protected by this lock prior to calling this
5813 * function, it will need to revalidate/reacquire any cached
5814 * protected data obtained prior to the call.
5817 fo_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
, vfs_context_t ctx
)
5821 proc_fdunlock(vfs_context_proc(ctx
));
5822 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, com
, data
, ctx
);
5823 proc_fdlock(vfs_context_proc(ctx
));
5831 * Description: Generic fileops select indirected through the fileops pointer
5832 * in the fileproc structure
5834 * Parameters: fp fileproc structure pointer
5835 * which select which
5836 * wql pointer to wait queue list
5837 * ctx VFS context for operation
5839 * Returns: 0 Success
5840 * !0 Errno from select
5843 fo_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
5845 return((*fp
->f_ops
->fo_select
)(fp
, which
, wql
, ctx
));
5852 * Description: Generic fileops close indirected through the fileops pointer
5853 * in the fileproc structure
5855 * Parameters: fp fileproc structure pointer for
5857 * ctx VFS context for operation
5859 * Returns: 0 Success
5860 * !0 Errno from close
5863 fo_close(struct fileglob
*fg
, vfs_context_t ctx
)
5865 return((*fg
->fg_ops
->fo_close
)(fg
, ctx
));
5872 * Description: Generic fileops kqueue filter indirected through the fileops
5873 * pointer in the fileproc structure
5875 * Parameters: fp fileproc structure pointer
5876 * kn pointer to knote to filter on
5877 * ctx VFS context for operation
5879 * Returns: 0 Success
5880 * !0 Errno from kqueue filter
5883 fo_kqfilter(struct fileproc
*fp
, struct knote
*kn
, vfs_context_t ctx
)
5885 return ((*fp
->f_ops
->fo_kqfilter
)(fp
, kn
, ctx
));
5889 * The ability to send a file descriptor to another
5890 * process is opt-in by file type.
5893 file_issendable(proc_t p
, struct fileproc
*fp
)
5895 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
5897 switch (fp
->f_type
) {
5902 return (0 == (fp
->f_fglob
->fg_lflags
& FG_CONFINED
));
5904 /* DTYPE_KQUEUE, DTYPE_FSEVENTS, DTYPE_PSXSEM */
5911 fileproc_alloc_init(__unused
void *arg
)
5913 struct fileproc
*fp
;
5915 MALLOC_ZONE(fp
, struct fileproc
*, sizeof (*fp
), M_FILEPROC
, M_WAITOK
);
5917 bzero(fp
, sizeof (*fp
));
5923 fileproc_free(struct fileproc
*fp
)
5925 switch (FILEPROC_TYPE(fp
)) {
5927 FREE_ZONE(fp
, sizeof (*fp
), M_FILEPROC
);
5930 guarded_fileproc_free(fp
);
5933 panic("%s: corrupt fp %p flags %x", __func__
, fp
, fp
->f_flags
);