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>
85 #include <sys/socket.h>
86 #include <sys/socketvar.h>
88 #include <sys/ioctl.h>
89 #include <sys/fcntl.h>
90 #include <sys/malloc.h>
92 #include <sys/syslog.h>
93 #include <sys/unistd.h>
94 #include <sys/resourcevar.h>
95 #include <sys/aio_kern.h>
97 #include <kern/locks.h>
98 #include <sys/uio_internal.h>
99 #include <sys/codesign.h>
100 #include <sys/codedir_internal.h>
102 #include <security/audit/audit.h>
104 #include <sys/mount_internal.h>
105 #include <sys/kdebug.h>
106 #include <sys/sysproto.h>
107 #include <sys/pipe.h>
108 #include <sys/spawn.h>
109 #include <kern/kern_types.h>
110 #include <kern/kalloc.h>
111 #include <kern/waitq.h>
112 #include <libkern/OSAtomic.h>
114 #include <sys/ubc_internal.h>
116 #include <kern/ipc_misc.h>
117 #include <vm/vm_protos.h>
119 #include <mach/mach_port.h>
124 kern_return_t
ipc_object_copyin(ipc_space_t
, mach_port_name_t
,
125 mach_msg_type_name_t
, ipc_port_t
*);
126 void ipc_port_release_send(ipc_port_t
);
131 static int finishdup(proc_t p
,
132 struct filedesc
*fdp
, int old
, int new, int flags
, int32_t *retval
);
134 int falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
, int locked
);
135 void fg_drop(struct fileproc
* fp
);
136 void fg_free(struct fileglob
*fg
);
137 void fg_ref(struct fileproc
* fp
);
138 void fileport_releasefg(struct fileglob
*fg
);
140 /* flags for close_internal_locked */
141 #define FD_DUP2RESV 1
143 /* We don't want these exported */
146 int unlink1(vfs_context_t
, vnode_t
, user_addr_t
, enum uio_seg
, int);
148 static void _fdrelse(struct proc
* p
, int fd
);
151 extern void file_lock_init(void);
153 extern kauth_scope_t kauth_scope_fileop
;
155 /* Conflict wait queue for when selects collide (opaque type) */
156 extern struct waitq select_conflict_queue
;
158 #define f_flag f_fglob->fg_flag
159 #define f_type f_fglob->fg_ops->fo_type
160 #define f_msgcount f_fglob->fg_msgcount
161 #define f_cred f_fglob->fg_cred
162 #define f_ops f_fglob->fg_ops
163 #define f_offset f_fglob->fg_offset
164 #define f_data f_fglob->fg_data
165 #define CHECK_ADD_OVERFLOW_INT64L(x, y) \
166 (((((x) > 0) && ((y) > 0) && ((x) > LLONG_MAX - (y))) || \
167 (((x) < 0) && ((y) < 0) && ((x) < LLONG_MIN - (y)))) \
170 * Descriptor management.
172 struct fmsglist fmsghead
; /* head of list of open files */
173 struct fmsglist fmsg_ithead
; /* head of list of open files */
174 int nfiles
; /* actual number of open files */
177 lck_grp_attr_t
* file_lck_grp_attr
;
178 lck_grp_t
* file_lck_grp
;
179 lck_attr_t
* file_lck_attr
;
181 lck_mtx_t
* uipc_lock
;
185 * check_file_seek_range
187 * Description: Checks if seek offsets are in the range of 0 to LLONG_MAX.
189 * Parameters: fl Flock structure.
190 * cur_file_offset Current offset in the file.
192 * Returns: 0 on Success.
193 * EOVERFLOW on overflow.
194 * EINVAL on offset less than zero.
198 check_file_seek_range(struct flock
*fl
, off_t cur_file_offset
)
200 if (fl
->l_whence
== SEEK_CUR
) {
201 /* Check if the start marker is beyond LLONG_MAX. */
202 if (CHECK_ADD_OVERFLOW_INT64L(fl
->l_start
, cur_file_offset
)) {
203 /* Check if start marker is negative */
204 if (fl
->l_start
< 0) {
209 /* Check if the start marker is negative. */
210 if (fl
->l_start
+ cur_file_offset
< 0) {
213 /* Check if end marker is beyond LLONG_MAX. */
214 if ((fl
->l_len
> 0) && (CHECK_ADD_OVERFLOW_INT64L(fl
->l_start
+
215 cur_file_offset
, fl
->l_len
- 1))) {
218 /* Check if the end marker is negative. */
219 if ((fl
->l_len
<= 0) && (fl
->l_start
+ cur_file_offset
+
223 } else if (fl
->l_whence
== SEEK_SET
) {
224 /* Check if the start marker is negative. */
225 if (fl
->l_start
< 0) {
228 /* Check if the end marker is beyond LLONG_MAX. */
229 if ((fl
->l_len
> 0) &&
230 CHECK_ADD_OVERFLOW_INT64L(fl
->l_start
, fl
->l_len
- 1)) {
233 /* Check if the end marker is negative. */
234 if ((fl
->l_len
< 0) && fl
->l_start
+ fl
->l_len
< 0) {
245 * Description: Initialize the file lock group and the uipc and flist locks
251 * Notes: Called at system startup from bsd_init().
256 /* allocate file lock group attribute and group */
257 file_lck_grp_attr
= lck_grp_attr_alloc_init();
259 file_lck_grp
= lck_grp_alloc_init("file", file_lck_grp_attr
);
261 /* Allocate file lock attribute */
262 file_lck_attr
= lck_attr_alloc_init();
264 uipc_lock
= lck_mtx_alloc_init(file_lck_grp
, file_lck_attr
);
269 * proc_fdlock, proc_fdlock_spin
271 * Description: Lock to control access to the per process struct fileproc
272 * and struct filedesc
274 * Parameters: p Process to take the lock on
278 * Notes: The lock is initialized in forkproc() and destroyed in
279 * reap_child_process().
282 proc_fdlock(proc_t p
)
284 lck_mtx_lock(&p
->p_fdmlock
);
288 proc_fdlock_spin(proc_t p
)
290 lck_mtx_lock_spin(&p
->p_fdmlock
);
294 proc_fdlock_assert(proc_t p
, int assertflags
)
296 lck_mtx_assert(&p
->p_fdmlock
, assertflags
);
303 * Description: Unlock the lock previously locked by a call to proc_fdlock()
305 * Parameters: p Process to drop the lock on
310 proc_fdunlock(proc_t p
)
312 lck_mtx_unlock(&p
->p_fdmlock
);
317 * System calls on descriptors.
324 * Description: Returns the per process maximum size of the descriptor table
326 * Parameters: p Process being queried
327 * retval Pointer to the call return area
332 * *retval (modified) Size of dtable
335 getdtablesize(proc_t p
, __unused
struct getdtablesize_args
*uap
, int32_t *retval
)
338 *retval
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
346 procfdtbl_reservefd(struct proc
* p
, int fd
)
348 p
->p_fd
->fd_ofiles
[fd
] = NULL
;
349 p
->p_fd
->fd_ofileflags
[fd
] |= UF_RESERVED
;
353 procfdtbl_markclosefd(struct proc
* p
, int fd
)
355 p
->p_fd
->fd_ofileflags
[fd
] |= (UF_RESERVED
| UF_CLOSING
);
359 procfdtbl_releasefd(struct proc
* p
, int fd
, struct fileproc
* fp
)
362 p
->p_fd
->fd_ofiles
[fd
] = fp
;
363 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESERVED
;
364 if ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
) == UF_RESVWAIT
) {
365 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESVWAIT
;
371 procfdtbl_waitfd(struct proc
* p
, int fd
)
373 p
->p_fd
->fd_ofileflags
[fd
] |= UF_RESVWAIT
;
374 msleep(&p
->p_fd
, &p
->p_fdmlock
, PRIBIO
, "ftbl_waitfd", NULL
);
379 procfdtbl_clearfd(struct proc
* p
, int fd
)
383 waiting
= (p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
);
384 p
->p_fd
->fd_ofiles
[fd
] = NULL
;
385 p
->p_fd
->fd_ofileflags
[fd
] = 0;
386 if ( waiting
== UF_RESVWAIT
) {
394 * Description: Inline utility function to free an fd in a filedesc
396 * Parameters: fdp Pointer to filedesc fd lies in
398 * reserv fd should be reserved
402 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
406 _fdrelse(struct proc
* p
, int fd
)
408 struct filedesc
*fdp
= p
->p_fd
;
411 if (fd
< fdp
->fd_freefile
)
412 fdp
->fd_freefile
= fd
;
414 if (fd
> fdp
->fd_lastfile
)
415 panic("fdrelse: fd_lastfile inconsistent");
417 procfdtbl_clearfd(p
, fd
);
419 while ((nfd
= fdp
->fd_lastfile
) > 0 &&
420 fdp
->fd_ofiles
[nfd
] == NULL
&&
421 !(fdp
->fd_ofileflags
[nfd
] & UF_RESERVED
))
443 char uio_buf
[ UIO_SIZEOF(1) ];
444 struct vfs_context context
= *(vfs_context_current());
445 bool wrote_some
= false;
449 error
= fp_lookup(p
, fd
, &fp
, 0);
453 if (fp
->f_type
!= DTYPE_VNODE
&& fp
->f_type
!= DTYPE_PIPE
&& fp
->f_type
!= DTYPE_SOCKET
) {
457 if (rw
== UIO_WRITE
&& !(fp
->f_flag
& FWRITE
)) {
462 if (rw
== UIO_READ
&& !(fp
->f_flag
& FREAD
)) {
467 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
469 if (UIO_SEG_IS_USER_SPACE(segflg
))
470 spacetype
= proc_is64bit(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
472 spacetype
= UIO_SYSSPACE
;
474 auio
= uio_createwithbuffer(1, offset
, spacetype
, rw
, &uio_buf
[0], sizeof(uio_buf
));
476 uio_addiov(auio
, base
, len
);
478 if ( !(io_flg
& IO_APPEND
))
481 if (rw
== UIO_WRITE
) {
482 user_ssize_t orig_resid
= uio_resid(auio
);
483 error
= fo_write(fp
, auio
, flags
, &context
);
484 wrote_some
= uio_resid(auio
) < orig_resid
;
486 error
= fo_read(fp
, auio
, flags
, &context
);
489 *aresid
= uio_resid(auio
);
491 if (uio_resid(auio
) && error
== 0)
496 fp_drop_written(p
, fd
, fp
);
498 fp_drop(p
, fd
, fp
, 0);
508 * Description: Duplicate a file descriptor.
510 * Parameters: p Process performing the dup
511 * uap->fd The fd to dup
512 * retval Pointer to the call return area
518 * *retval (modified) The new descriptor
521 dup(proc_t p
, struct dup_args
*uap
, int32_t *retval
)
523 struct filedesc
*fdp
= p
->p_fd
;
529 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
533 if (FP_ISGUARDED(fp
, GUARD_DUP
)) {
534 error
= fp_guard_exception(p
, old
, fp
, kGUARD_EXC_DUP
);
535 (void) fp_drop(p
, old
, fp
, 1);
539 if ( (error
= fdalloc(p
, 0, &new)) ) {
540 fp_drop(p
, old
, fp
, 1);
544 error
= finishdup(p
, fdp
, old
, new, 0, retval
);
545 fp_drop(p
, old
, fp
, 1);
548 if (ENTR_SHOULDTRACE
&& fp
->f_type
== DTYPE_SOCKET
) {
549 KERNEL_ENERGYTRACE(kEnTrActKernSocket
, DBG_FUNC_START
,
550 new, 0, (int64_t)VM_KERNEL_ADDRPERM(fp
->f_data
));
559 * Description: Duplicate a file descriptor to a particular value.
561 * Parameters: p Process performing the dup
562 * uap->from The fd to dup
563 * uap->to The fd to dup it to
564 * retval Pointer to the call return area
570 * *retval (modified) The new descriptor
573 dup2(proc_t p
, struct dup2_args
*uap
, int32_t *retval
)
575 struct filedesc
*fdp
= p
->p_fd
;
576 int old
= uap
->from
, new = uap
->to
;
578 struct fileproc
*fp
, *nfp
;
583 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
587 if (FP_ISGUARDED(fp
, GUARD_DUP
)) {
588 error
= fp_guard_exception(p
, old
, fp
, kGUARD_EXC_DUP
);
589 (void) fp_drop(p
, old
, fp
, 1);
594 (rlim_t
)new >= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
596 fp_drop(p
, old
, fp
, 1);
601 fp_drop(p
, old
, fp
, 1);
606 if (new < 0 || new >= fdp
->fd_nfiles
) {
607 if ( (error
= fdalloc(p
, new, &i
)) ) {
608 fp_drop(p
, old
, fp
, 1);
618 while ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == UF_RESERVED
) {
619 fp_drop(p
, old
, fp
, 1);
620 procfdtbl_waitfd(p
, new);
622 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
627 if ((fdp
->fd_ofiles
[new] != NULL
) &&
628 ((error
= fp_lookup(p
, new, &nfp
, 1)) == 0)) {
629 fp_drop(p
, old
, fp
, 1);
630 if (FP_ISGUARDED(nfp
, GUARD_CLOSE
)) {
631 error
= fp_guard_exception(p
,
632 new, nfp
, kGUARD_EXC_CLOSE
);
633 (void) fp_drop(p
, new, nfp
, 1);
637 (void)close_internal_locked(p
, new, nfp
, FD_DUP2RESV
);
639 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
641 procfdtbl_clearfd(p
, new);
645 if (fdp
->fd_ofiles
[new] != NULL
)
646 panic("dup2: no ref on fileproc %d", new);
648 procfdtbl_reservefd(p
, new);
652 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
657 if (fdp
->fd_ofiles
[new] != 0)
658 panic("dup2: overwriting fd_ofiles with new %d", new);
659 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0)
660 panic("dup2: unreserved fileflags with new %d", new);
662 error
= finishdup(p
, fdp
, old
, new, 0, retval
);
663 fp_drop(p
, old
, fp
, 1);
673 * Description: The file control system call.
675 * Parameters: p Process performing the fcntl
676 * uap->fd The fd to operate against
677 * uap->cmd The command to perform
678 * uap->arg Pointer to the command argument
679 * retval Pointer to the call return area
682 * !0 Errno (see fcntl_nocancel)
685 * *retval (modified) fcntl return value (if any)
687 * Notes: This system call differs from fcntl_nocancel() in that it
688 * tests for cancellation prior to performing a potentially
689 * blocking operation.
692 fcntl(proc_t p
, struct fcntl_args
*uap
, int32_t *retval
)
694 __pthread_testcancel(1);
695 return(fcntl_nocancel(p
, (struct fcntl_nocancel_args
*)uap
, retval
));
702 * Description: A non-cancel-testing file control system call.
704 * Parameters: p Process performing the fcntl
705 * uap->fd The fd to operate against
706 * uap->cmd The command to perform
707 * uap->arg Pointer to the command argument
708 * retval Pointer to the call return area
712 * fp_lookup:EBADF Bad file descriptor
724 * vnode_getwithref:???
732 * vnode_getwithref:???
739 * vnode_getwithref:???
741 * [F_SETSIZE,F_RDADVISE]
744 * vnode_getwithref:???
745 * [F_RDAHEAD,F_NOCACHE]
747 * vnode_getwithref:???
751 * *retval (modified) fcntl return value (if any)
754 fcntl_nocancel(proc_t p
, struct fcntl_nocancel_args
*uap
, int32_t *retval
)
757 struct filedesc
*fdp
= p
->p_fd
;
760 struct vnode
*vp
= NULLVP
; /* for AUDIT_ARG() at end */
761 int i
, tmp
, error
, error2
, flg
= 0;
763 struct flocktimeout fltimeout
;
764 struct timespec
*timeout
= NULL
;
765 struct vfs_context context
;
773 AUDIT_ARG(fd
, uap
->fd
);
774 AUDIT_ARG(cmd
, uap
->cmd
);
777 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
781 context
.vc_thread
= current_thread();
782 context
.vc_ucred
= fp
->f_cred
;
784 is64bit
= proc_is64bit(p
);
790 * Since the arg parameter is defined as a long but may be
791 * either a long or a pointer we must take care to handle
792 * sign extension issues. Our sys call munger will sign
793 * extend a long when we are called from a 32-bit process.
794 * Since we can never have an address greater than 32-bits
795 * from a 32-bit process we lop off the top 32-bits to avoid
796 * getting the wrong address
798 argp
= CAST_USER_ADDR_T((uint32_t)uap
->arg
);
801 pop
= &fdp
->fd_ofileflags
[fd
];
804 error
= mac_file_check_fcntl(proc_ucred(p
), fp
->f_fglob
, uap
->cmd
,
813 case F_DUPFD_CLOEXEC
:
814 if (FP_ISGUARDED(fp
, GUARD_DUP
)) {
815 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_DUP
);
818 newmin
= CAST_DOWN_EXPLICIT(int, uap
->arg
); /* arg is an int, so we won't lose bits */
819 AUDIT_ARG(value32
, newmin
);
820 if ((u_int
)newmin
>= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
821 newmin
>= maxfiles
) {
825 if ( (error
= fdalloc(p
, newmin
, &i
)) )
827 error
= finishdup(p
, fdp
, fd
, i
,
828 uap
->cmd
== F_DUPFD_CLOEXEC
? UF_EXCLOSE
: 0, retval
);
832 *retval
= (*pop
& UF_EXCLOSE
)? FD_CLOEXEC
: 0;
837 AUDIT_ARG(value32
, uap
->arg
);
838 if (uap
->arg
& FD_CLOEXEC
)
841 if (FILEPROC_TYPE(fp
) == FTYPE_GUARDED
) {
842 error
= fp_guard_exception(p
,
843 fd
, fp
, kGUARD_EXC_NOCLOEXEC
);
852 *retval
= OFLAGS(fp
->f_flag
);
857 fp
->f_flag
&= ~FCNTLFLAGS
;
858 tmp
= CAST_DOWN_EXPLICIT(int, uap
->arg
); /* arg is an int, so we won't lose bits */
859 AUDIT_ARG(value32
, tmp
);
860 fp
->f_flag
|= FFLAGS(tmp
) & FCNTLFLAGS
;
861 tmp
= fp
->f_flag
& FNONBLOCK
;
862 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
865 tmp
= fp
->f_flag
& FASYNC
;
866 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, &context
);
869 fp
->f_flag
&= ~FNONBLOCK
;
871 (void)fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
875 if (fp
->f_type
== DTYPE_SOCKET
) {
876 *retval
= ((struct socket
*)fp
->f_data
)->so_pgid
;
880 error
= fo_ioctl(fp
, (int)TIOCGPGRP
, (caddr_t
)retval
, &context
);
885 tmp
= CAST_DOWN_EXPLICIT(pid_t
, uap
->arg
); /* arg is an int, so we won't lose bits */
886 AUDIT_ARG(value32
, tmp
);
887 if (fp
->f_type
== DTYPE_SOCKET
) {
888 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
892 if (fp
->f_type
== DTYPE_PIPE
) {
893 error
= fo_ioctl(fp
, TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
900 proc_t p1
= proc_find(tmp
);
905 tmp
= (int)p1
->p_pgrpid
;
908 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
912 tmp
= CAST_DOWN_EXPLICIT(int, uap
->arg
);
913 if (fp
->f_type
== DTYPE_SOCKET
) {
915 error
= sock_setsockopt((struct socket
*)fp
->f_data
,
916 SOL_SOCKET
, SO_NOSIGPIPE
, &tmp
, sizeof (tmp
));
921 struct fileglob
*fg
= fp
->f_fglob
;
923 lck_mtx_lock_spin(&fg
->fg_lock
);
925 fg
->fg_lflags
|= FG_NOSIGPIPE
;
927 fg
->fg_lflags
&= FG_NOSIGPIPE
;
928 lck_mtx_unlock(&fg
->fg_lock
);
934 if (fp
->f_type
== DTYPE_SOCKET
) {
936 int retsize
= sizeof (*retval
);
937 error
= sock_getsockopt((struct socket
*)fp
->f_data
,
938 SOL_SOCKET
, SO_NOSIGPIPE
, retval
, &retsize
);
943 *retval
= (fp
->f_fglob
->fg_lflags
& FG_NOSIGPIPE
) ?
951 * If this is the only reference to this fglob in the process
952 * and it's already marked as close-on-fork then mark it as
953 * (immutably) "confined" i.e. any fd that points to it will
954 * forever be close-on-fork, and attempts to use an IPC
955 * mechanism to move the descriptor elsewhere will fail.
957 if (CAST_DOWN_EXPLICIT(int, uap
->arg
)) {
958 struct fileglob
*fg
= fp
->f_fglob
;
960 lck_mtx_lock_spin(&fg
->fg_lock
);
961 if (fg
->fg_lflags
& FG_CONFINED
)
963 else if (1 != fg
->fg_count
)
964 error
= EAGAIN
; /* go close the dup .. */
965 else if (UF_FORKCLOSE
== (*pop
& UF_FORKCLOSE
)) {
966 fg
->fg_lflags
|= FG_CONFINED
;
969 error
= EBADF
; /* open without O_CLOFORK? */
970 lck_mtx_unlock(&fg
->fg_lock
);
973 * Other subsystems may have built on the immutability
974 * of FG_CONFINED; clearing it may be tricky.
976 error
= EPERM
; /* immutable */
981 *retval
= (fp
->f_fglob
->fg_lflags
& FG_CONFINED
) ? 1 : 0;
985 case F_SETLKWTIMEOUT
:
987 case F_OFD_SETLKWTIMEOUT
:
990 /* Fall into F_SETLK */
994 if (fp
->f_type
!= DTYPE_VNODE
) {
998 vp
= (struct vnode
*)fp
->f_data
;
1001 offset
= fp
->f_offset
;
1004 /* Copy in the lock structure */
1005 if (F_SETLKWTIMEOUT
== uap
->cmd
||
1006 F_OFD_SETLKWTIMEOUT
== uap
->cmd
) {
1007 error
= copyin(argp
, (caddr_t
) &fltimeout
, sizeof(fltimeout
));
1012 timeout
= &fltimeout
.timeout
;
1014 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
1020 /* Check starting byte and ending byte for EOVERFLOW in SEEK_CUR */
1021 /* and ending byte for EOVERFLOW in SEEK_SET */
1022 error
= check_file_seek_range(&fl
, offset
);
1027 if ( (error
= vnode_getwithref(vp
)) ) {
1030 if (fl
.l_whence
== SEEK_CUR
)
1031 fl
.l_start
+= offset
;
1034 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
1037 (void)vnode_put(vp
);
1044 case F_OFD_SETLKWTIMEOUT
:
1046 switch (fl
.l_type
) {
1048 if ((fflag
& FREAD
) == 0) {
1052 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1053 F_SETLK
, &fl
, flg
, &context
, timeout
);
1056 if ((fflag
& FWRITE
) == 0) {
1060 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1061 F_SETLK
, &fl
, flg
, &context
, timeout
);
1064 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1065 F_UNLCK
, &fl
, F_OFD_LOCK
, &context
,
1073 (F_RDLCK
== fl
.l_type
|| F_WRLCK
== fl
.l_type
)) {
1074 struct fileglob
*fg
= fp
->f_fglob
;
1077 * arrange F_UNLCK on last close (once
1078 * set, FG_HAS_OFDLOCK is immutable)
1080 if ((fg
->fg_lflags
& FG_HAS_OFDLOCK
) == 0) {
1081 lck_mtx_lock_spin(&fg
->fg_lock
);
1082 fg
->fg_lflags
|= FG_HAS_OFDLOCK
;
1083 lck_mtx_unlock(&fg
->fg_lock
);
1089 switch (fl
.l_type
) {
1091 if ((fflag
& FREAD
) == 0) {
1095 // XXX UInt32 unsafe for LP64 kernel
1096 OSBitOrAtomic(P_LADVLOCK
, &p
->p_ladvflag
);
1097 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1098 F_SETLK
, &fl
, flg
, &context
, timeout
);
1101 if ((fflag
& FWRITE
) == 0) {
1105 // XXX UInt32 unsafe for LP64 kernel
1106 OSBitOrAtomic(P_LADVLOCK
, &p
->p_ladvflag
);
1107 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1108 F_SETLK
, &fl
, flg
, &context
, timeout
);
1111 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1112 F_UNLCK
, &fl
, F_POSIX
, &context
, timeout
);
1120 (void) vnode_put(vp
);
1125 if (fp
->f_type
!= DTYPE_VNODE
) {
1129 vp
= (struct vnode
*)fp
->f_data
;
1131 offset
= fp
->f_offset
;
1134 /* Copy in the lock structure */
1135 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
1139 /* Check starting byte and ending byte for EOVERFLOW in SEEK_CUR */
1140 /* and ending byte for EOVERFLOW in SEEK_SET */
1141 error
= check_file_seek_range(&fl
, offset
);
1146 if ((fl
.l_whence
== SEEK_SET
) && (fl
.l_start
< 0)) {
1151 switch (fl
.l_type
) {
1161 switch (fl
.l_whence
) {
1171 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1172 if (fl
.l_whence
== SEEK_CUR
)
1173 fl
.l_start
+= offset
;
1176 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
1182 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1183 F_GETLK
, &fl
, F_OFD_LOCK
, &context
, NULL
);
1185 case F_OFD_GETLKPID
:
1186 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1187 F_GETLKPID
, &fl
, F_OFD_LOCK
, &context
, NULL
);
1190 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1191 uap
->cmd
, &fl
, F_POSIX
, &context
, NULL
);
1195 (void)vnode_put(vp
);
1198 error
= copyout((caddr_t
)&fl
, argp
, sizeof(fl
));
1202 case F_PREALLOCATE
: {
1203 fstore_t alloc_struct
; /* structure for allocate command */
1204 u_int32_t alloc_flags
= 0;
1206 if (fp
->f_type
!= DTYPE_VNODE
) {
1211 vp
= (struct vnode
*)fp
->f_data
;
1214 /* make sure that we have write permission */
1215 if ((fp
->f_flag
& FWRITE
) == 0) {
1220 error
= copyin(argp
, (caddr_t
)&alloc_struct
, sizeof(alloc_struct
));
1224 /* now set the space allocated to 0 */
1225 alloc_struct
.fst_bytesalloc
= 0;
1228 * Do some simple parameter checking
1231 /* set up the flags */
1233 alloc_flags
|= PREALLOCATE
;
1235 if (alloc_struct
.fst_flags
& F_ALLOCATECONTIG
)
1236 alloc_flags
|= ALLOCATECONTIG
;
1238 if (alloc_struct
.fst_flags
& F_ALLOCATEALL
)
1239 alloc_flags
|= ALLOCATEALL
;
1242 * Do any position mode specific stuff. The only
1243 * position mode supported now is PEOFPOSMODE
1246 switch (alloc_struct
.fst_posmode
) {
1249 if (alloc_struct
.fst_offset
!= 0) {
1254 alloc_flags
|= ALLOCATEFROMPEOF
;
1258 if (alloc_struct
.fst_offset
<= 0) {
1263 alloc_flags
|= ALLOCATEFROMVOL
;
1271 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1273 * call allocate to get the space
1275 error
= VNOP_ALLOCATE(vp
,alloc_struct
.fst_length
,alloc_flags
,
1276 &alloc_struct
.fst_bytesalloc
, alloc_struct
.fst_offset
,
1278 (void)vnode_put(vp
);
1280 error2
= copyout((caddr_t
)&alloc_struct
, argp
, sizeof(alloc_struct
));
1289 if (fp
->f_type
!= DTYPE_VNODE
) {
1293 vp
= (struct vnode
*)fp
->f_data
;
1296 error
= copyin(argp
, (caddr_t
)&offset
, sizeof (off_t
));
1299 AUDIT_ARG(value64
, offset
);
1301 error
= vnode_getwithref(vp
);
1306 error
= mac_vnode_check_truncate(&context
,
1307 fp
->f_fglob
->fg_cred
, vp
);
1309 (void)vnode_put(vp
);
1314 * Make sure that we are root. Growing a file
1315 * without zero filling the data is a security hole
1316 * root would have access anyway so we'll allow it
1318 if (!kauth_cred_issuser(kauth_cred_get())) {
1324 error
= vnode_setsize(vp
, offset
, IO_NOZEROFILL
,
1328 (void)vnode_put(vp
);
1332 if (fp
->f_type
!= DTYPE_VNODE
) {
1337 fp
->f_fglob
->fg_flag
&= ~FNORDAHEAD
;
1339 fp
->f_fglob
->fg_flag
|= FNORDAHEAD
;
1344 if (fp
->f_type
!= DTYPE_VNODE
) {
1349 fp
->f_fglob
->fg_flag
|= FNOCACHE
;
1351 fp
->f_fglob
->fg_flag
&= ~FNOCACHE
;
1356 if (fp
->f_type
!= DTYPE_VNODE
) {
1361 fp
->f_fglob
->fg_flag
|= FNODIRECT
;
1363 fp
->f_fglob
->fg_flag
&= ~FNODIRECT
;
1367 case F_SINGLE_WRITER
:
1368 if (fp
->f_type
!= DTYPE_VNODE
) {
1373 fp
->f_fglob
->fg_flag
|= FSINGLE_WRITER
;
1375 fp
->f_fglob
->fg_flag
&= ~FSINGLE_WRITER
;
1379 case F_GLOBAL_NOCACHE
:
1380 if (fp
->f_type
!= DTYPE_VNODE
) {
1384 vp
= (struct vnode
*)fp
->f_data
;
1387 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1389 *retval
= vnode_isnocache(vp
);
1392 vnode_setnocache(vp
);
1394 vnode_clearnocache(vp
);
1396 (void)vnode_put(vp
);
1400 case F_CHECK_OPENEVT
:
1401 if (fp
->f_type
!= DTYPE_VNODE
) {
1405 vp
= (struct vnode
*)fp
->f_data
;
1408 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1410 *retval
= vnode_is_openevt(vp
);
1413 vnode_set_openevt(vp
);
1415 vnode_clear_openevt(vp
);
1417 (void)vnode_put(vp
);
1422 struct radvisory ra_struct
;
1424 if (fp
->f_type
!= DTYPE_VNODE
) {
1428 vp
= (struct vnode
*)fp
->f_data
;
1431 if ( (error
= copyin(argp
, (caddr_t
)&ra_struct
, sizeof(ra_struct
))) )
1433 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1434 error
= VNOP_IOCTL(vp
, F_RDADVISE
, (caddr_t
)&ra_struct
, 0, &context
);
1436 (void)vnode_put(vp
);
1443 if (fp
->f_type
!= DTYPE_VNODE
) {
1447 vp
= (struct vnode
*)fp
->f_data
;
1450 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1451 error
= cluster_push(vp
, 0);
1453 (void)vnode_put(vp
);
1458 case F_LOG2PHYS_EXT
: {
1459 struct log2phys l2p_struct
; /* structure for allocate command */
1462 off_t file_offset
= 0;
1466 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1467 error
= copyin(argp
, (caddr_t
)&l2p_struct
, sizeof(l2p_struct
));
1470 file_offset
= l2p_struct
.l2p_devoffset
;
1472 file_offset
= fp
->f_offset
;
1474 if (fp
->f_type
!= DTYPE_VNODE
) {
1478 vp
= (struct vnode
*)fp
->f_data
;
1480 if ( (error
= vnode_getwithref(vp
)) ) {
1483 error
= VNOP_OFFTOBLK(vp
, file_offset
, &lbn
);
1485 (void)vnode_put(vp
);
1488 error
= VNOP_BLKTOOFF(vp
, lbn
, &offset
);
1490 (void)vnode_put(vp
);
1493 devBlockSize
= vfs_devblocksize(vnode_mount(vp
));
1494 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1495 if (l2p_struct
.l2p_contigbytes
< 0) {
1501 a_size
= MIN((uint64_t)l2p_struct
.l2p_contigbytes
, SIZE_MAX
);
1503 a_size
= devBlockSize
;
1506 error
= VNOP_BLOCKMAP(vp
, offset
, a_size
, &bn
, &run
, NULL
, 0, &context
);
1508 (void)vnode_put(vp
);
1511 l2p_struct
.l2p_flags
= 0; /* for now */
1512 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1513 l2p_struct
.l2p_contigbytes
= run
- (file_offset
- offset
);
1515 l2p_struct
.l2p_contigbytes
= 0; /* for now */
1519 * The block number being -1 suggests that the file offset is not backed
1520 * by any real blocks on-disk. As a result, just let it be passed back up wholesale.
1523 /* Don't multiply it by the block size */
1524 l2p_struct
.l2p_devoffset
= bn
;
1527 l2p_struct
.l2p_devoffset
= bn
* devBlockSize
;
1528 l2p_struct
.l2p_devoffset
+= file_offset
- offset
;
1530 error
= copyout((caddr_t
)&l2p_struct
, argp
, sizeof(l2p_struct
));
1538 if (fp
->f_type
!= DTYPE_VNODE
) {
1542 vp
= (struct vnode
*)fp
->f_data
;
1545 pathlen
= MAXPATHLEN
;
1546 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
1547 if (pathbufp
== NULL
) {
1551 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1552 error
= vn_getpath(vp
, pathbufp
, &pathlen
);
1553 (void)vnode_put(vp
);
1556 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
1558 FREE(pathbufp
, M_TEMP
);
1562 case F_PATHPKG_CHECK
: {
1566 if (fp
->f_type
!= DTYPE_VNODE
) {
1570 vp
= (struct vnode
*)fp
->f_data
;
1573 pathlen
= MAXPATHLEN
;
1574 pathbufp
= kalloc(MAXPATHLEN
);
1576 if ( (error
= copyinstr(argp
, pathbufp
, MAXPATHLEN
, &pathlen
)) == 0 ) {
1577 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1578 AUDIT_ARG(text
, pathbufp
);
1579 error
= vn_path_package_check(vp
, pathbufp
, pathlen
, retval
);
1581 (void)vnode_put(vp
);
1584 kfree(pathbufp
, MAXPATHLEN
);
1588 case F_CHKCLEAN
: // used by regression tests to see if all dirty pages got cleaned by fsync()
1589 case F_FULLFSYNC
: // fsync + flush the journal + DKIOCSYNCHRONIZE
1590 case F_BARRIERFSYNC
: // fsync + barrier
1591 case F_FREEZE_FS
: // freeze all other fs operations for the fs of this fd
1592 case F_THAW_FS
: { // thaw all frozen fs operations for the fs of this fd
1593 if (fp
->f_type
!= DTYPE_VNODE
) {
1597 vp
= (struct vnode
*)fp
->f_data
;
1600 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1601 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
1603 (void)vnode_put(vp
);
1609 * SPI (private) for opening a file starting from a dir fd
1612 struct user_fopenfrom fopen
;
1613 struct vnode_attr va
;
1614 struct nameidata nd
;
1617 /* Check if this isn't a valid file descriptor */
1618 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1619 (fp
->f_flag
& FREAD
) == 0) {
1623 vp
= (struct vnode
*)fp
->f_data
;
1626 if (vnode_getwithref(vp
)) {
1631 /* Only valid for directories */
1632 if (vp
->v_type
!= VDIR
) {
1638 /* Get flags, mode and pathname arguments. */
1639 if (IS_64BIT_PROCESS(p
)) {
1640 error
= copyin(argp
, &fopen
, sizeof(fopen
));
1642 struct user32_fopenfrom fopen32
;
1644 error
= copyin(argp
, &fopen32
, sizeof(fopen32
));
1645 fopen
.o_flags
= fopen32
.o_flags
;
1646 fopen
.o_mode
= fopen32
.o_mode
;
1647 fopen
.o_pathname
= CAST_USER_ADDR_T(fopen32
.o_pathname
);
1653 AUDIT_ARG(fflags
, fopen
.o_flags
);
1654 AUDIT_ARG(mode
, fopen
.o_mode
);
1656 /* Mask off all but regular access permissions */
1657 cmode
= ((fopen
.o_mode
&~ fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
1658 VATTR_SET(&va
, va_mode
, cmode
& ACCESSPERMS
);
1660 /* Start the lookup relative to the file descriptor's vnode. */
1661 NDINIT(&nd
, LOOKUP
, OP_OPEN
, USEDVP
| FOLLOW
| AUDITVNPATH1
, UIO_USERSPACE
,
1662 fopen
.o_pathname
, &context
);
1665 error
= open1(&context
, &nd
, fopen
.o_flags
, &va
,
1666 fileproc_alloc_init
, NULL
, retval
);
1672 * SPI (private) for unlinking a file starting from a dir fd
1674 case F_UNLINKFROM
: {
1675 user_addr_t pathname
;
1677 /* Check if this isn't a valid file descriptor */
1678 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1679 (fp
->f_flag
& FREAD
) == 0) {
1683 vp
= (struct vnode
*)fp
->f_data
;
1686 if (vnode_getwithref(vp
)) {
1691 /* Only valid for directories */
1692 if (vp
->v_type
!= VDIR
) {
1698 /* Get flags, mode and pathname arguments. */
1699 if (IS_64BIT_PROCESS(p
)) {
1700 pathname
= (user_addr_t
)argp
;
1702 pathname
= CAST_USER_ADDR_T(argp
);
1705 /* Start the lookup relative to the file descriptor's vnode. */
1706 error
= unlink1(&context
, vp
, pathname
, UIO_USERSPACE
, 0);
1715 case F_ADDFILESIGS_FOR_DYLD_SIM
:
1716 case F_ADDFILESIGS_RETURN
:
1718 struct cs_blob
*blob
= NULL
;
1719 struct user_fsignatures fs
;
1721 vm_offset_t kernel_blob_addr
;
1722 vm_size_t kernel_blob_size
;
1723 int blob_add_flags
= 0;
1725 if (fp
->f_type
!= DTYPE_VNODE
) {
1729 vp
= (struct vnode
*)fp
->f_data
;
1732 if (uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
1733 blob_add_flags
|= MAC_VNODE_CHECK_DYLD_SIM
;
1734 if ((p
->p_csflags
& CS_KILL
) == 0) {
1736 p
->p_csflags
|= CS_KILL
;
1741 error
= vnode_getwithref(vp
);
1745 if (IS_64BIT_PROCESS(p
)) {
1746 error
= copyin(argp
, &fs
, sizeof (fs
));
1748 struct user32_fsignatures fs32
;
1750 error
= copyin(argp
, &fs32
, sizeof (fs32
));
1751 fs
.fs_file_start
= fs32
.fs_file_start
;
1752 fs
.fs_blob_start
= CAST_USER_ADDR_T(fs32
.fs_blob_start
);
1753 fs
.fs_blob_size
= fs32
.fs_blob_size
;
1762 * First check if we have something loaded a this offset
1764 blob
= ubc_cs_blob_get(vp
, CPU_TYPE_ANY
, fs
.fs_file_start
);
1767 /* If this is for dyld_sim revalidate the blob */
1768 if (uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
1769 error
= ubc_cs_blob_revalidate(vp
, blob
, blob_add_flags
);
1774 * An arbitrary limit, to prevent someone from mapping in a 20GB blob. This should cover
1775 * our use cases for the immediate future, but note that at the time of this commit, some
1776 * platforms are nearing 2MB blob sizes (with a prior soft limit of 2.5MB).
1778 * We should consider how we can manage this more effectively; the above means that some
1779 * platforms are using megabytes of memory for signing data; it merely hasn't crossed the
1780 * threshold considered ridiculous at the time of this change.
1782 #define CS_MAX_BLOB_SIZE (40ULL * 1024ULL * 1024ULL)
1783 if (fs
.fs_blob_size
> CS_MAX_BLOB_SIZE
) {
1789 kernel_blob_size
= CAST_DOWN(vm_size_t
, fs
.fs_blob_size
);
1790 kr
= ubc_cs_blob_allocate(&kernel_blob_addr
, &kernel_blob_size
);
1791 if (kr
!= KERN_SUCCESS
) {
1797 if(uap
->cmd
== F_ADDSIGS
) {
1798 error
= copyin(fs
.fs_blob_start
,
1799 (void *) kernel_blob_addr
,
1801 } else /* F_ADDFILESIGS || F_ADDFILESIGS_RETURN || F_ADDFILESIGS_FOR_DYLD_SIM */ {
1804 error
= vn_rdwr(UIO_READ
,
1806 (caddr_t
) kernel_blob_addr
,
1808 fs
.fs_file_start
+ fs
.fs_blob_start
,
1814 if ((error
== 0) && resid
) {
1815 /* kernel_blob_size rounded to a page size, but signature may be at end of file */
1816 memset((void *)(kernel_blob_addr
+ (kernel_blob_size
- resid
)), 0x0, resid
);
1821 ubc_cs_blob_deallocate(kernel_blob_addr
,
1828 error
= ubc_cs_blob_add(vp
,
1829 CPU_TYPE_ANY
, /* not for a specific architecture */
1836 ubc_cs_blob_deallocate(kernel_blob_addr
,
1839 /* ubc_blob_add() has consumed "kernel_blob_addr" */
1840 #if CHECK_CS_VALIDATION_BITMAP
1841 ubc_cs_validation_bitmap_allocate( vp
);
1846 if (uap
->cmd
== F_ADDFILESIGS_RETURN
|| uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
1848 * The first element of the structure is a
1849 * off_t that happen to have the same size for
1850 * all archs. Lets overwrite that.
1852 off_t end_offset
= 0;
1854 end_offset
= blob
->csb_end_offset
;
1855 error
= copyout(&end_offset
, argp
, sizeof (end_offset
));
1858 (void) vnode_put(vp
);
1866 case F_GETPROTECTIONCLASS
: {
1869 if (fp
->f_type
!= DTYPE_VNODE
) {
1873 vp
= (struct vnode
*)fp
->f_data
;
1877 if (vnode_getwithref(vp
)) {
1882 error
= cp_vnode_getclass (vp
, &class);
1891 case F_SETPROTECTIONCLASS
: {
1892 /* tmp must be a valid PROTECTION_CLASS_* */
1893 tmp
= CAST_DOWN_EXPLICIT(uint32_t, uap
->arg
);
1895 if (fp
->f_type
!= DTYPE_VNODE
) {
1899 vp
= (struct vnode
*)fp
->f_data
;
1903 if (vnode_getwithref(vp
)) {
1908 /* Only go forward if you have write access */
1909 vfs_context_t ctx
= vfs_context_current();
1910 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
1915 error
= cp_vnode_setclass (vp
, tmp
);
1920 case F_TRANSCODEKEY
: {
1922 char *backup_keyp
= NULL
;
1923 unsigned backup_key_len
= CP_MAX_WRAPPEDKEYSIZE
;
1925 if (fp
->f_type
!= DTYPE_VNODE
) {
1930 vp
= (struct vnode
*)fp
->f_data
;
1933 if (vnode_getwithref(vp
)) {
1938 MALLOC(backup_keyp
, char *, backup_key_len
, M_TEMP
, M_WAITOK
);
1939 if (backup_keyp
== NULL
) {
1944 error
= cp_vnode_transcode (vp
, backup_keyp
, &backup_key_len
);
1948 error
= copyout((caddr_t
)backup_keyp
, argp
, backup_key_len
);
1949 *retval
= backup_key_len
;
1952 FREE(backup_keyp
, M_TEMP
);
1957 case F_GETPROTECTIONLEVEL
: {
1958 uint32_t cp_version
= 0;
1960 if (fp
->f_type
!= DTYPE_VNODE
) {
1965 vp
= (struct vnode
*) fp
->f_data
;
1968 if (vnode_getwithref(vp
)) {
1974 * if cp_get_major_vers fails, error will be set to proper errno
1975 * and cp_version will still be 0.
1978 error
= cp_get_root_major_vers (vp
, &cp_version
);
1979 *retval
= cp_version
;
1985 case F_GETDEFAULTPROTLEVEL
: {
1986 uint32_t cp_default
= 0;
1988 if (fp
->f_type
!= DTYPE_VNODE
) {
1993 vp
= (struct vnode
*) fp
->f_data
;
1996 if (vnode_getwithref(vp
)) {
2002 * if cp_get_major_vers fails, error will be set to proper errno
2003 * and cp_version will still be 0.
2006 error
= cp_get_default_level(vp
, &cp_default
);
2007 *retval
= cp_default
;
2014 #endif /* CONFIG_PROTECT */
2016 case F_MOVEDATAEXTENTS
: {
2017 struct fileproc
*fp2
= NULL
;
2018 struct vnode
*src_vp
= NULLVP
;
2019 struct vnode
*dst_vp
= NULLVP
;
2020 /* We need to grab the 2nd FD out of the argments before moving on. */
2021 int fd2
= CAST_DOWN_EXPLICIT(int32_t, uap
->arg
);
2023 error
= priv_check_cred(kauth_cred_get(), PRIV_VFS_MOVE_DATA_EXTENTS
, 0);
2027 if (fp
->f_type
!= DTYPE_VNODE
) {
2032 /* For now, special case HFS+ only, since this is SPI. */
2033 src_vp
= (struct vnode
*)fp
->f_data
;
2034 if (src_vp
->v_tag
!= VT_HFS
) {
2040 * Get the references before we start acquiring iocounts on the vnodes,
2041 * while we still hold the proc fd lock
2043 if ( (error
= fp_lookup(p
, fd2
, &fp2
, 1)) ) {
2047 if (fp2
->f_type
!= DTYPE_VNODE
) {
2048 fp_drop(p
, fd2
, fp2
, 1);
2052 dst_vp
= (struct vnode
*)fp2
->f_data
;
2053 if (dst_vp
->v_tag
!= VT_HFS
) {
2054 fp_drop(p
, fd2
, fp2
, 1);
2060 /* Re-do MAC checks against the new FD, pass in a fake argument */
2061 error
= mac_file_check_fcntl(proc_ucred(p
), fp2
->f_fglob
, uap
->cmd
, 0);
2063 fp_drop(p
, fd2
, fp2
, 1);
2067 /* Audit the 2nd FD */
2072 if (vnode_getwithref(src_vp
)) {
2073 fp_drop(p
, fd2
, fp2
, 0);
2077 if (vnode_getwithref(dst_vp
)) {
2079 fp_drop(p
, fd2
, fp2
, 0);
2085 * Basic asserts; validate they are not the same and that
2086 * both live on the same filesystem.
2088 if (dst_vp
== src_vp
) {
2091 fp_drop (p
, fd2
, fp2
, 0);
2096 if (dst_vp
->v_mount
!= src_vp
->v_mount
) {
2099 fp_drop (p
, fd2
, fp2
, 0);
2104 /* Now we have a legit pair of FDs. Go to work */
2106 /* Now check for write access to the target files */
2107 if(vnode_authorize(src_vp
, NULLVP
,
2108 (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), &context
) != 0) {
2111 fp_drop(p
, fd2
, fp2
, 0);
2116 if(vnode_authorize(dst_vp
, NULLVP
,
2117 (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), &context
) != 0) {
2120 fp_drop(p
, fd2
, fp2
, 0);
2125 /* Verify that both vps point to files and not directories */
2126 if ( !vnode_isreg(src_vp
) || !vnode_isreg(dst_vp
)) {
2130 fp_drop (p
, fd2
, fp2
, 0);
2135 * The exchangedata syscall handler passes in 0 for the flags to VNOP_EXCHANGE.
2136 * We'll pass in our special bit indicating that the new behavior is expected
2139 error
= VNOP_EXCHANGE(src_vp
, dst_vp
, FSOPT_EXCHANGE_DATA_ONLY
, &context
);
2143 fp_drop(p
, fd2
, fp2
, 0);
2148 * SPI for making a file compressed.
2150 case F_MAKECOMPRESSED
: {
2151 uint32_t gcounter
= CAST_DOWN_EXPLICIT(uint32_t, uap
->arg
);
2153 if (fp
->f_type
!= DTYPE_VNODE
) {
2158 vp
= (struct vnode
*) fp
->f_data
;
2162 if (vnode_getwithref(vp
)) {
2168 if ((vnode_isreg(vp
) == 0) && (vnode_islnk(vp
) == 0)) {
2174 /* invoke ioctl to pass off to FS */
2175 /* Only go forward if you have write access */
2176 vfs_context_t ctx
= vfs_context_current();
2177 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2183 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)&gcounter
, 0, &context
);
2190 * SPI (private) for indicating to a filesystem that subsequent writes to
2191 * the open FD will written to the Fastflow.
2193 case F_SET_GREEDY_MODE
:
2194 /* intentionally drop through to the same handler as F_SETSTATIC.
2195 * both fcntls should pass the argument and their selector into VNOP_IOCTL.
2199 * SPI (private) for indicating to a filesystem that subsequent writes to
2200 * the open FD will represent static content.
2202 case F_SETSTATICCONTENT
: {
2203 caddr_t ioctl_arg
= NULL
;
2206 ioctl_arg
= (caddr_t
) 1;
2209 if (fp
->f_type
!= DTYPE_VNODE
) {
2213 vp
= (struct vnode
*)fp
->f_data
;
2216 error
= vnode_getwithref(vp
);
2222 /* Only go forward if you have write access */
2223 vfs_context_t ctx
= vfs_context_current();
2224 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2230 error
= VNOP_IOCTL(vp
, uap
->cmd
, ioctl_arg
, 0, &context
);
2231 (void)vnode_put(vp
);
2237 * SPI (private) for indicating to the lower level storage driver that the
2238 * subsequent writes should be of a particular IO type (burst, greedy, static),
2239 * or other flavors that may be necessary.
2246 /* extract 32 bits of flags from userland */
2247 param_ptr
= (caddr_t
) uap
->arg
;
2248 param
= (uint32_t) param_ptr
;
2251 /* If no argument is specified, error out */
2257 * Validate the different types of flags that can be specified:
2258 * all of them are mutually exclusive for now.
2261 case F_IOTYPE_ISOCHRONOUS
:
2270 if (fp
->f_type
!= DTYPE_VNODE
) {
2274 vp
= (struct vnode
*)fp
->f_data
;
2277 error
= vnode_getwithref(vp
);
2283 /* Only go forward if you have write access */
2284 vfs_context_t ctx
= vfs_context_current();
2285 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2291 error
= VNOP_IOCTL(vp
, uap
->cmd
, param_ptr
, 0, &context
);
2292 (void)vnode_put(vp
);
2299 * Extract the CodeDirectory of the vnode associated with
2300 * the file descriptor and copy it back to user space
2302 case F_GETCODEDIR
: {
2303 struct user_fcodeblobs args
;
2305 if (fp
->f_type
!= DTYPE_VNODE
) {
2310 vp
= (struct vnode
*)fp
->f_data
;
2313 if ((fp
->f_flag
& FREAD
) == 0) {
2318 if (IS_64BIT_PROCESS(p
)) {
2319 struct user64_fcodeblobs args64
;
2321 error
= copyin(argp
, &args64
, sizeof(args64
));
2325 args
.f_cd_hash
= args64
.f_cd_hash
;
2326 args
.f_hash_size
= args64
.f_hash_size
;
2327 args
.f_cd_buffer
= args64
.f_cd_buffer
;
2328 args
.f_cd_size
= args64
.f_cd_size
;
2329 args
.f_out_size
= args64
.f_out_size
;
2330 args
.f_arch
= args64
.f_arch
;
2332 struct user32_fcodeblobs args32
;
2334 error
= copyin(argp
, &args32
, sizeof(args32
));
2338 args
.f_cd_hash
= CAST_USER_ADDR_T(args32
.f_cd_hash
);
2339 args
.f_hash_size
= args32
.f_hash_size
;
2340 args
.f_cd_buffer
= CAST_USER_ADDR_T(args32
.f_cd_buffer
);
2341 args
.f_cd_size
= args32
.f_cd_size
;
2342 args
.f_out_size
= CAST_USER_ADDR_T(args32
.f_out_size
);
2343 args
.f_arch
= args32
.f_arch
;
2346 if (vp
->v_ubcinfo
== NULL
) {
2351 struct cs_blob
*t_blob
= vp
->v_ubcinfo
->cs_blobs
;
2354 * This call fails if there is no cs_blob corresponding to the
2355 * vnode, or if there are multiple cs_blobs present, and the caller
2356 * did not specify which cpu_type they want the cs_blob for
2358 if (t_blob
== NULL
) {
2359 error
= ENOENT
; /* there is no codesigning blob for this process */
2361 } else if (args
.f_arch
== 0 && t_blob
->csb_next
!= NULL
) {
2362 error
= ENOENT
; /* too many architectures and none specified */
2366 /* If the user specified an architecture, find the right blob */
2367 if (args
.f_arch
!= 0) {
2369 if (t_blob
->csb_cpu_type
== args
.f_arch
)
2371 t_blob
= t_blob
->csb_next
;
2373 /* The cpu_type the user requested could not be found */
2374 if (t_blob
== NULL
) {
2380 const CS_CodeDirectory
*cd
= t_blob
->csb_cd
;
2386 uint64_t buffer_size
= ntohl(cd
->length
);
2388 if (buffer_size
> UINT_MAX
) {
2393 error
= copyout(&buffer_size
, args
.f_out_size
, sizeof(unsigned int));
2397 if (sizeof(t_blob
->csb_cdhash
) > args
.f_hash_size
||
2398 buffer_size
> args
.f_cd_size
) {
2403 error
= copyout(t_blob
->csb_cdhash
, args
.f_cd_hash
, sizeof(t_blob
->csb_cdhash
));
2406 error
= copyout(cd
, args
.f_cd_buffer
, buffer_size
);
2414 * Set the vnode pointed to by 'fd'
2415 * and tag it as the (potentially future) backing store
2416 * for another filesystem
2418 case F_SETBACKINGSTORE
: {
2419 if (fp
->f_type
!= DTYPE_VNODE
) {
2424 vp
= (struct vnode
*)fp
->f_data
;
2426 if (vp
->v_tag
!= VT_HFS
) {
2432 if (vnode_getwithref(vp
)) {
2437 /* only proceed if you have write access */
2438 vfs_context_t ctx
= vfs_context_current();
2439 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2446 /* If arg != 0, set, otherwise unset */
2448 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
)1, 0, &context
);
2451 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
2459 * like F_GETPATH, but special semantics for
2460 * the mobile time machine handler.
2462 case F_GETPATH_MTMINFO
: {
2466 if (fp
->f_type
!= DTYPE_VNODE
) {
2470 vp
= (struct vnode
*)fp
->f_data
;
2473 pathlen
= MAXPATHLEN
;
2474 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
2475 if (pathbufp
== NULL
) {
2479 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
2480 int backingstore
= 0;
2482 /* Check for error from vn_getpath before moving on */
2483 if ((error
= vn_getpath(vp
, pathbufp
, &pathlen
)) == 0) {
2484 if (vp
->v_tag
== VT_HFS
) {
2485 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
) &backingstore
, 0, &context
);
2487 (void)vnode_put(vp
);
2490 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
2494 * If the copyout was successful, now check to ensure
2495 * that this vnode is not a BACKINGSTORE vnode. mtmd
2496 * wants the path regardless.
2503 (void)vnode_put(vp
);
2505 FREE(pathbufp
, M_TEMP
);
2509 #if DEBUG || DEVELOPMENT
2511 if (fp
->f_type
!= DTYPE_VNODE
) {
2515 vp
= (struct vnode
*)fp
->f_data
;
2524 * This is an fcntl() that we d not recognize at this level;
2525 * if this is a vnode, we send it down into the VNOP_IOCTL
2526 * for this vnode; this can include special devices, and will
2527 * effectively overload fcntl() to send ioctl()'s.
2529 if((uap
->cmd
& IOC_VOID
) && (uap
->cmd
& IOC_INOUT
)){
2534 /* Catch any now-invalid fcntl() selectors */
2536 case F_MARKDEPENDENCY
:
2543 if (fp
->f_type
!= DTYPE_VNODE
) {
2547 vp
= (struct vnode
*)fp
->f_data
;
2550 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
2551 #define STK_PARAMS 128
2552 char stkbuf
[STK_PARAMS
];
2556 * For this to work properly, we have to copy in the
2557 * ioctl() cmd argument if there is one; we must also
2558 * check that a command parameter, if present, does
2559 * not exceed the maximum command length dictated by
2560 * the number of bits we have available in the command
2561 * to represent a structure length. Finally, we have
2562 * to copy the results back out, if it is that type of
2565 size
= IOCPARM_LEN(uap
->cmd
);
2566 if (size
> IOCPARM_MAX
) {
2567 (void)vnode_put(vp
);
2573 if (size
> sizeof (stkbuf
)) {
2574 if ((memp
= (caddr_t
)kalloc(size
)) == 0) {
2575 (void)vnode_put(vp
);
2584 if (uap
->cmd
& IOC_IN
) {
2587 error
= copyin(argp
, data
, size
);
2589 (void)vnode_put(vp
);
2595 /* Bzero the section beyond that which was needed */
2596 if (size
<= sizeof(stkbuf
)) {
2597 bzero ( (((uint8_t*)data
) + size
), (sizeof(stkbuf
) - size
));
2602 *(user_addr_t
*)data
= argp
;
2604 *(uint32_t *)data
= (uint32_t)argp
;
2607 } else if ((uap
->cmd
& IOC_OUT
) && size
) {
2609 * Zero the buffer so the user always
2610 * gets back something deterministic.
2613 } else if (uap
->cmd
& IOC_VOID
) {
2615 *(user_addr_t
*)data
= argp
;
2617 *(uint32_t *)data
= (uint32_t)argp
;
2621 error
= VNOP_IOCTL(vp
, uap
->cmd
, CAST_DOWN(caddr_t
, data
), 0, &context
);
2623 (void)vnode_put(vp
);
2625 /* Copy any output data to user */
2626 if (error
== 0 && (uap
->cmd
& IOC_OUT
) && size
)
2627 error
= copyout(data
, argp
, size
);
2635 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
2636 fp_drop(p
, fd
, fp
, 0);
2639 fp_drop(p
, fd
, fp
, 1);
2648 * Description: Common code for dup, dup2, and fcntl(F_DUPFD).
2650 * Parameters: p Process performing the dup
2652 * new The fd to dup it to
2653 * fd_flags Flags to augment the new fd
2654 * retval Pointer to the call return area
2656 * Returns: 0 Success
2661 * *retval (modified) The new descriptor
2663 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
2666 * Notes: This function may drop and reacquire this lock; it is unsafe
2667 * for a caller to assume that other state protected by the lock
2668 * has not been subsequently changed out from under it.
2672 struct filedesc
*fdp
, int old
, int new, int fd_flags
, int32_t *retval
)
2674 struct fileproc
*nfp
;
2675 struct fileproc
*ofp
;
2681 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2683 if ((ofp
= fdp
->fd_ofiles
[old
]) == NULL
||
2684 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
)) {
2691 error
= mac_file_check_dup(proc_ucred(p
), ofp
->f_fglob
, new);
2701 nfp
= fileproc_alloc_init(NULL
);
2711 nfp
->f_fglob
= ofp
->f_fglob
;
2714 if (fdp
->fd_ofiles
[new] != 0)
2715 panic("finishdup: overwriting fd_ofiles with new %d", new);
2716 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0)
2717 panic("finishdup: unreserved fileflags with new %d", new);
2720 if (new > fdp
->fd_lastfile
)
2721 fdp
->fd_lastfile
= new;
2722 *fdflags(p
, new) |= fd_flags
;
2723 procfdtbl_releasefd(p
, new, nfp
);
2732 * Description: The implementation of the close(2) system call
2734 * Parameters: p Process in whose per process file table
2735 * the close is to occur
2736 * uap->fd fd to be closed
2739 * Returns: 0 Success
2740 * fp_lookup:EBADF Bad file descriptor
2741 * fp_guard_exception:??? Guarded file descriptor
2742 * close_internal:EBADF
2743 * close_internal:??? Anything returnable by a per-fileops
2747 close(proc_t p
, struct close_args
*uap
, int32_t *retval
)
2749 __pthread_testcancel(1);
2750 return(close_nocancel(p
, (struct close_nocancel_args
*)uap
, retval
));
2755 close_nocancel(proc_t p
, struct close_nocancel_args
*uap
, __unused
int32_t *retval
)
2757 struct fileproc
*fp
;
2761 AUDIT_SYSCLOSE(p
, fd
);
2765 if ( (error
= fp_lookup(p
,fd
,&fp
, 1)) ) {
2770 if (FP_ISGUARDED(fp
, GUARD_CLOSE
)) {
2771 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_CLOSE
);
2772 (void) fp_drop(p
, fd
, fp
, 1);
2777 error
= close_internal_locked(p
, fd
, fp
, 0);
2786 * close_internal_locked
2788 * Close a file descriptor.
2790 * Parameters: p Process in whose per process file table
2791 * the close is to occur
2792 * fd fd to be closed
2793 * fp fileproc associated with the fd
2795 * Returns: 0 Success
2796 * EBADF fd already in close wait state
2797 * closef_locked:??? Anything returnable by a per-fileops
2800 * Locks: Assumes proc_fdlock for process is held by the caller and returns
2803 * Notes: This function may drop and reacquire this lock; it is unsafe
2804 * for a caller to assume that other state protected by the lock
2805 * has not been subsequently changed out from under it.
2808 close_internal_locked(proc_t p
, int fd
, struct fileproc
*fp
, int flags
)
2810 struct filedesc
*fdp
= p
->p_fd
;
2812 int resvfd
= flags
& FD_DUP2RESV
;
2816 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2819 /* Keep people from using the filedesc while we are closing it */
2820 procfdtbl_markclosefd(p
, fd
);
2823 if ((fp
->f_flags
& FP_CLOSING
) == FP_CLOSING
) {
2824 panic("close_internal_locked: being called on already closing fd");
2829 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
2830 panic("close_internal: unreserved fileflags with fd %d", fd
);
2833 fp
->f_flags
|= FP_CLOSING
;
2835 if ( (fp
->f_flags
& FP_AIOISSUED
) || kauth_authorize_fileop_has_listeners() ) {
2839 if ( (fp
->f_type
== DTYPE_VNODE
) && kauth_authorize_fileop_has_listeners() ) {
2841 * call out to allow 3rd party notification of close.
2842 * Ignore result of kauth_authorize_fileop call.
2844 if (vnode_getwithref((vnode_t
)fp
->f_data
) == 0) {
2845 u_int fileop_flags
= 0;
2846 if ((fp
->f_flags
& FP_WRITTEN
) != 0)
2847 fileop_flags
|= KAUTH_FILEOP_CLOSE_MODIFIED
;
2848 kauth_authorize_fileop(fp
->f_fglob
->fg_cred
, KAUTH_FILEOP_CLOSE
,
2849 (uintptr_t)fp
->f_data
, (uintptr_t)fileop_flags
);
2850 vnode_put((vnode_t
)fp
->f_data
);
2853 if (fp
->f_flags
& FP_AIOISSUED
)
2855 * cancel all async IO requests that can be cancelled.
2857 _aio_close( p
, fd
);
2862 if (fd
< fdp
->fd_knlistsize
)
2863 knote_fdclose(p
, fd
);
2865 if (fp
->f_flags
& FP_WAITEVENT
)
2866 (void)waitevent_close(p
, fp
);
2868 fileproc_drain(p
, fp
);
2873 procfdtbl_reservefd(p
, fd
);
2876 if (ENTR_SHOULDTRACE
&& fp
->f_type
== DTYPE_SOCKET
)
2877 KERNEL_ENERGYTRACE(kEnTrActKernSocket
, DBG_FUNC_END
,
2878 fd
, 0, (int64_t)VM_KERNEL_ADDRPERM(fp
->f_data
));
2880 error
= closef_locked(fp
, fp
->f_fglob
, p
);
2881 if ((fp
->f_flags
& FP_WAITCLOSE
) == FP_WAITCLOSE
)
2882 wakeup(&fp
->f_flags
);
2883 fp
->f_flags
&= ~(FP_WAITCLOSE
| FP_CLOSING
);
2893 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
2894 panic("close with reserved fd returns with freed fd:%d: proc: %p", fd
, p
);
2905 * Description: Return status information about a file descriptor.
2907 * Parameters: p The process doing the fstat
2909 * ub The user stat buffer
2910 * xsecurity The user extended security
2911 * buffer, or 0 if none
2912 * xsecurity_size The size of xsecurity, or 0
2914 * isstat64 Flag to indicate 64 bit version
2915 * for inode size, etc.
2917 * Returns: 0 Success
2920 * fp_lookup:EBADF Bad file descriptor
2921 * vnode_getwithref:???
2923 * vnode_getwithref:???
2930 * Notes: Internal implementation for all other fstat() related
2933 * XXX switch on node type is bogus; need a stat in struct
2934 * XXX fileops instead.
2937 fstat1(proc_t p
, int fd
, user_addr_t ub
, user_addr_t xsecurity
, user_addr_t xsecurity_size
, int isstat64
)
2939 struct fileproc
*fp
;
2945 struct user64_stat user64_sb
;
2946 struct user32_stat user32_sb
;
2947 struct user64_stat64 user64_sb64
;
2948 struct user32_stat64 user32_sb64
;
2953 kauth_filesec_t fsec
;
2954 user_size_t xsecurity_bufsize
;
2955 vfs_context_t ctx
= vfs_context_current();
2961 if ((error
= fp_lookup(p
, fd
, &fp
, 0)) != 0) {
2966 fsec
= KAUTH_FILESEC_NONE
;
2968 sbptr
= (void *)&source
;
2973 if ((error
= vnode_getwithref((vnode_t
)data
)) == 0) {
2975 * If the caller has the file open, and is not
2976 * requesting extended security information, we are
2977 * going to let them get the basic stat information.
2979 if (xsecurity
== USER_ADDR_NULL
) {
2980 error
= vn_stat_noauth((vnode_t
)data
, sbptr
, NULL
, isstat64
, ctx
);
2982 error
= vn_stat((vnode_t
)data
, sbptr
, &fsec
, isstat64
, ctx
);
2985 AUDIT_ARG(vnpath
, (struct vnode
*)data
, ARG_VNODE1
);
2986 (void)vnode_put((vnode_t
)data
);
2992 error
= soo_stat((struct socket
*)data
, sbptr
, isstat64
);
2994 #endif /* SOCKETS */
2997 error
= pipe_stat((void *)data
, sbptr
, isstat64
);
3001 error
= pshm_stat((void *)data
, sbptr
, isstat64
);
3005 error
= kqueue_stat((void *)data
, sbptr
, isstat64
, p
);
3015 if (isstat64
!= 0) {
3016 source
.sb64
.st_lspare
= 0;
3017 source
.sb64
.st_qspare
[0] = 0LL;
3018 source
.sb64
.st_qspare
[1] = 0LL;
3020 if (IS_64BIT_PROCESS(current_proc())) {
3021 munge_user64_stat64(&source
.sb64
, &dest
.user64_sb64
);
3022 my_size
= sizeof(dest
.user64_sb64
);
3023 sbp
= (caddr_t
)&dest
.user64_sb64
;
3025 munge_user32_stat64(&source
.sb64
, &dest
.user32_sb64
);
3026 my_size
= sizeof(dest
.user32_sb64
);
3027 sbp
= (caddr_t
)&dest
.user32_sb64
;
3030 source
.sb
.st_lspare
= 0;
3031 source
.sb
.st_qspare
[0] = 0LL;
3032 source
.sb
.st_qspare
[1] = 0LL;
3033 if (IS_64BIT_PROCESS(current_proc())) {
3034 munge_user64_stat(&source
.sb
, &dest
.user64_sb
);
3035 my_size
= sizeof(dest
.user64_sb
);
3036 sbp
= (caddr_t
)&dest
.user64_sb
;
3038 munge_user32_stat(&source
.sb
, &dest
.user32_sb
);
3039 my_size
= sizeof(dest
.user32_sb
);
3040 sbp
= (caddr_t
)&dest
.user32_sb
;
3044 error
= copyout(sbp
, ub
, my_size
);
3047 /* caller wants extended security information? */
3048 if (xsecurity
!= USER_ADDR_NULL
) {
3050 /* did we get any? */
3051 if (fsec
== KAUTH_FILESEC_NONE
) {
3052 if (susize(xsecurity_size
, 0) != 0) {
3057 /* find the user buffer size */
3058 xsecurity_bufsize
= fusize(xsecurity_size
);
3060 /* copy out the actual data size */
3061 if (susize(xsecurity_size
, KAUTH_FILESEC_COPYSIZE(fsec
)) != 0) {
3066 /* if the caller supplied enough room, copy out to it */
3067 if (xsecurity_bufsize
>= KAUTH_FILESEC_COPYSIZE(fsec
))
3068 error
= copyout(fsec
, xsecurity
, KAUTH_FILESEC_COPYSIZE(fsec
));
3072 fp_drop(p
, fd
, fp
, 0);
3074 kauth_filesec_free(fsec
);
3082 * Description: Extended version of fstat supporting returning extended
3083 * security information
3085 * Parameters: p The process doing the fstat
3086 * uap->fd The fd to stat
3087 * uap->ub The user stat buffer
3088 * uap->xsecurity The user extended security
3089 * buffer, or 0 if none
3090 * uap->xsecurity_size The size of xsecurity, or 0
3092 * Returns: 0 Success
3093 * !0 Errno (see fstat1)
3096 fstat_extended(proc_t p
, struct fstat_extended_args
*uap
, __unused
int32_t *retval
)
3098 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 0));
3105 * Description: Get file status for the file associated with fd
3107 * Parameters: p The process doing the fstat
3108 * uap->fd The fd to stat
3109 * uap->ub The user stat buffer
3111 * Returns: 0 Success
3112 * !0 Errno (see fstat1)
3115 fstat(proc_t p
, register struct fstat_args
*uap
, __unused
int32_t *retval
)
3117 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 0));
3124 * Description: Extended version of fstat64 supporting returning extended
3125 * security information
3127 * Parameters: p The process doing the fstat
3128 * uap->fd The fd to stat
3129 * uap->ub The user stat buffer
3130 * uap->xsecurity The user extended security
3131 * buffer, or 0 if none
3132 * uap->xsecurity_size The size of xsecurity, or 0
3134 * Returns: 0 Success
3135 * !0 Errno (see fstat1)
3138 fstat64_extended(proc_t p
, struct fstat64_extended_args
*uap
, __unused
int32_t *retval
)
3140 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 1));
3147 * Description: Get 64 bit version of the file status for the file associated
3150 * Parameters: p The process doing the fstat
3151 * uap->fd The fd to stat
3152 * uap->ub The user stat buffer
3154 * Returns: 0 Success
3155 * !0 Errno (see fstat1)
3158 fstat64(proc_t p
, register struct fstat64_args
*uap
, __unused
int32_t *retval
)
3160 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 1));
3167 * Description: Return pathconf information about a file descriptor.
3169 * Parameters: p Process making the request
3170 * uap->fd fd to get information about
3171 * uap->name Name of information desired
3172 * retval Pointer to the call return area
3174 * Returns: 0 Success
3176 * fp_lookup:EBADF Bad file descriptor
3177 * vnode_getwithref:???
3181 * *retval (modified) Returned information (numeric)
3184 fpathconf(proc_t p
, struct fpathconf_args
*uap
, int32_t *retval
)
3187 struct fileproc
*fp
;
3194 AUDIT_ARG(fd
, uap
->fd
);
3195 if ( (error
= fp_lookup(p
, fd
, &fp
, 0)) )
3203 if (uap
->name
!= _PC_PIPE_BUF
) {
3212 if (uap
->name
!= _PC_PIPE_BUF
) {
3221 vp
= (struct vnode
*)data
;
3223 if ( (error
= vnode_getwithref(vp
)) == 0) {
3224 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
3226 error
= vn_pathconf(vp
, uap
->name
, retval
, vfs_context_current());
3228 (void)vnode_put(vp
);
3239 fp_drop(p
, fd
, fp
, 0);
3244 * Statistics counter for the number of times a process calling fdalloc()
3245 * has resulted in an expansion of the per process open file table.
3247 * XXX This would likely be of more use if it were per process
3255 * Description: Allocate a file descriptor for the process.
3257 * Parameters: p Process to allocate the fd in
3258 * want The fd we would prefer to get
3259 * result Pointer to fd we got
3261 * Returns: 0 Success
3266 * *result (modified) The fd which was allocated
3269 fdalloc(proc_t p
, int want
, int *result
)
3271 struct filedesc
*fdp
= p
->p_fd
;
3273 int lim
, last
, numfiles
, oldnfiles
;
3274 struct fileproc
**newofiles
, **ofiles
;
3275 char *newofileflags
;
3278 * Search for a free descriptor starting at the higher
3279 * of want or fd_freefile. If that fails, consider
3280 * expanding the ofile array.
3283 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
3286 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
3288 last
= min(fdp
->fd_nfiles
, lim
);
3289 if ((i
= want
) < fdp
->fd_freefile
)
3290 i
= fdp
->fd_freefile
;
3291 for (; i
< last
; i
++) {
3292 if (fdp
->fd_ofiles
[i
] == NULL
&& !(fdp
->fd_ofileflags
[i
] & UF_RESERVED
)) {
3293 procfdtbl_reservefd(p
, i
);
3294 if (i
> fdp
->fd_lastfile
)
3295 fdp
->fd_lastfile
= i
;
3296 if (want
<= fdp
->fd_freefile
)
3297 fdp
->fd_freefile
= i
;
3304 * No space in current array. Expand?
3306 if (fdp
->fd_nfiles
>= lim
)
3308 if (fdp
->fd_nfiles
< NDEXTENT
)
3309 numfiles
= NDEXTENT
;
3311 numfiles
= 2 * fdp
->fd_nfiles
;
3316 MALLOC_ZONE(newofiles
, struct fileproc
**,
3317 numfiles
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
3319 if (newofiles
== NULL
) {
3322 if (fdp
->fd_nfiles
>= numfiles
) {
3323 FREE_ZONE(newofiles
, numfiles
* OFILESIZE
, M_OFILETABL
);
3326 newofileflags
= (char *) &newofiles
[numfiles
];
3328 * Copy the existing ofile and ofileflags arrays
3329 * and zero the new portion of each array.
3331 oldnfiles
= fdp
->fd_nfiles
;
3332 (void) memcpy(newofiles
, fdp
->fd_ofiles
,
3333 oldnfiles
* sizeof(*fdp
->fd_ofiles
));
3334 (void) memset(&newofiles
[oldnfiles
], 0,
3335 (numfiles
- oldnfiles
) * sizeof(*fdp
->fd_ofiles
));
3337 (void) memcpy(newofileflags
, fdp
->fd_ofileflags
,
3338 oldnfiles
* sizeof(*fdp
->fd_ofileflags
));
3339 (void) memset(&newofileflags
[oldnfiles
], 0,
3340 (numfiles
- oldnfiles
) *
3341 sizeof(*fdp
->fd_ofileflags
));
3342 ofiles
= fdp
->fd_ofiles
;
3343 fdp
->fd_ofiles
= newofiles
;
3344 fdp
->fd_ofileflags
= newofileflags
;
3345 fdp
->fd_nfiles
= numfiles
;
3346 FREE_ZONE(ofiles
, oldnfiles
* OFILESIZE
, M_OFILETABL
);
3355 * Description: Check to see whether n user file descriptors are available
3358 * Parameters: p Process to check in
3359 * n The number of fd's desired
3364 * Locks: Assumes proc_fdlock for process is held by the caller
3366 * Notes: The answer only remains valid so long as the proc_fdlock is
3367 * held by the caller.
3370 fdavail(proc_t p
, int n
)
3372 struct filedesc
*fdp
= p
->p_fd
;
3373 struct fileproc
**fpp
;
3377 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
3378 if ((i
= lim
- fdp
->fd_nfiles
) > 0 && (n
-= i
) <= 0)
3380 fpp
= &fdp
->fd_ofiles
[fdp
->fd_freefile
];
3381 flags
= &fdp
->fd_ofileflags
[fdp
->fd_freefile
];
3382 for (i
= fdp
->fd_nfiles
- fdp
->fd_freefile
; --i
>= 0; fpp
++, flags
++)
3383 if (*fpp
== NULL
&& !(*flags
& UF_RESERVED
) && --n
<= 0)
3392 * Description: Legacy KPI wrapper function for _fdrelse
3394 * Parameters: p Process in which fd lives
3399 * Locks: Assumes proc_fdlock for process is held by the caller
3402 fdrelse(proc_t p
, int fd
)
3411 * Description: Get the fileproc pointer for the given fd from the per process
3412 * open file table without taking an explicit reference on it.
3414 * Parameters: p Process containing fd
3415 * fd fd to obtain fileproc for
3416 * resultfp Pointer to pointer return area
3418 * Returns: 0 Success
3422 * *resultfp (modified) Pointer to fileproc pointer
3424 * Locks: Assumes proc_fdlock for process is held by the caller
3426 * Notes: Because there is no reference explicitly taken, the returned
3427 * fileproc pointer is only valid so long as the proc_fdlock
3428 * remains held by the caller.
3431 fdgetf_noref(proc_t p
, int fd
, struct fileproc
**resultfp
)
3433 struct filedesc
*fdp
= p
->p_fd
;
3434 struct fileproc
*fp
;
3436 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3437 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3438 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3450 * Description: Get fileproc and vnode pointer for a given fd from the per
3451 * process open file table of the specified process, and if
3452 * successful, increment the f_iocount
3454 * Parameters: p Process in which fd lives
3455 * fd fd to get information for
3456 * resultfp Pointer to result fileproc
3457 * pointer area, or 0 if none
3458 * resultvp Pointer to result vnode pointer
3459 * area, or 0 if none
3461 * Returns: 0 Success
3462 * EBADF Bad file descriptor
3463 * ENOTSUP fd does not refer to a vnode
3466 * *resultfp (modified) Fileproc pointer
3467 * *resultvp (modified) vnode pointer
3469 * Notes: The resultfp and resultvp fields are optional, and may be
3470 * independently specified as NULL to skip returning information
3472 * Locks: Internally takes and releases proc_fdlock
3475 fp_getfvp(proc_t p
, int fd
, struct fileproc
**resultfp
, struct vnode
**resultvp
)
3477 struct filedesc
*fdp
= p
->p_fd
;
3478 struct fileproc
*fp
;
3480 proc_fdlock_spin(p
);
3481 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3482 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3483 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3487 if (fp
->f_type
!= DTYPE_VNODE
) {
3496 *resultvp
= (struct vnode
*)fp
->f_data
;
3506 * Description: Get fileproc, vnode pointer, and vid for a given fd from the
3507 * per process open file table of the specified process, and if
3508 * successful, increment the f_iocount
3510 * Parameters: p Process in which fd lives
3511 * fd fd to get information for
3512 * resultfp Pointer to result fileproc
3513 * pointer area, or 0 if none
3514 * resultvp Pointer to result vnode pointer
3515 * area, or 0 if none
3516 * vidp Pointer to resuld vid area
3518 * Returns: 0 Success
3519 * EBADF Bad file descriptor
3520 * ENOTSUP fd does not refer to a vnode
3523 * *resultfp (modified) Fileproc pointer
3524 * *resultvp (modified) vnode pointer
3527 * Notes: The resultfp and resultvp fields are optional, and may be
3528 * independently specified as NULL to skip returning information
3530 * Locks: Internally takes and releases proc_fdlock
3533 fp_getfvpandvid(proc_t p
, int fd
, struct fileproc
**resultfp
,
3534 struct vnode
**resultvp
, uint32_t *vidp
)
3536 struct filedesc
*fdp
= p
->p_fd
;
3537 struct fileproc
*fp
;
3539 proc_fdlock_spin(p
);
3540 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3541 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3542 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3546 if (fp
->f_type
!= DTYPE_VNODE
) {
3555 *resultvp
= (struct vnode
*)fp
->f_data
;
3557 *vidp
= (uint32_t)vnode_vid((struct vnode
*)fp
->f_data
);
3567 * Description: Get fileproc and socket pointer for a given fd from the
3568 * per process open file table of the specified process, and if
3569 * successful, increment the f_iocount
3571 * Parameters: p Process in which fd lives
3572 * fd fd to get information for
3573 * resultfp Pointer to result fileproc
3574 * pointer area, or 0 if none
3575 * results Pointer to result socket
3576 * pointer area, or 0 if none
3578 * Returns: EBADF The file descriptor is invalid
3579 * EOPNOTSUPP The file descriptor is not a socket
3583 * *resultfp (modified) Fileproc pointer
3584 * *results (modified) socket pointer
3586 * Notes: EOPNOTSUPP should probably be ENOTSOCK; this function is only
3587 * ever called from accept1().
3590 fp_getfsock(proc_t p
, int fd
, struct fileproc
**resultfp
,
3591 struct socket
**results
)
3593 struct filedesc
*fdp
= p
->p_fd
;
3594 struct fileproc
*fp
;
3596 proc_fdlock_spin(p
);
3597 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3598 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3599 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3603 if (fp
->f_type
!= DTYPE_SOCKET
) {
3612 *results
= (struct socket
*)fp
->f_data
;
3622 * Description: Get fileproc and kqueue pointer for a given fd from the
3623 * per process open file table of the specified process, and if
3624 * successful, increment the f_iocount
3626 * Parameters: p Process in which fd lives
3627 * fd fd to get information for
3628 * resultfp Pointer to result fileproc
3629 * pointer area, or 0 if none
3630 * resultkq Pointer to result kqueue
3631 * pointer area, or 0 if none
3633 * Returns: EBADF The file descriptor is invalid
3634 * EBADF The file descriptor is not a socket
3638 * *resultfp (modified) Fileproc pointer
3639 * *resultkq (modified) kqueue pointer
3641 * Notes: The second EBADF should probably be something else to make
3642 * the error condition distinct.
3645 fp_getfkq(proc_t p
, int fd
, struct fileproc
**resultfp
,
3646 struct kqueue
**resultkq
)
3648 struct filedesc
*fdp
= p
->p_fd
;
3649 struct fileproc
*fp
;
3651 proc_fdlock_spin(p
);
3652 if ( fd
< 0 || fd
>= fdp
->fd_nfiles
||
3653 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3654 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3658 if (fp
->f_type
!= DTYPE_KQUEUE
) {
3667 *resultkq
= (struct kqueue
*)fp
->f_data
;
3677 * Description: Get fileproc and POSIX shared memory pointer for a given fd
3678 * from the per process open file table of the specified process
3679 * and if successful, increment the f_iocount
3681 * Parameters: p Process in which fd lives
3682 * fd fd to get information for
3683 * resultfp Pointer to result fileproc
3684 * pointer area, or 0 if none
3685 * resultpshm Pointer to result POSIX
3686 * shared memory pointer
3687 * pointer area, or 0 if none
3689 * Returns: EBADF The file descriptor is invalid
3690 * EBADF The file descriptor is not a POSIX
3691 * shared memory area
3695 * *resultfp (modified) Fileproc pointer
3696 * *resultpshm (modified) POSIX shared memory pointer
3698 * Notes: The second EBADF should probably be something else to make
3699 * the error condition distinct.
3702 fp_getfpshm(proc_t p
, int fd
, struct fileproc
**resultfp
,
3703 struct pshmnode
**resultpshm
)
3705 struct filedesc
*fdp
= p
->p_fd
;
3706 struct fileproc
*fp
;
3708 proc_fdlock_spin(p
);
3709 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3710 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3711 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3715 if (fp
->f_type
!= DTYPE_PSXSHM
) {
3725 *resultpshm
= (struct pshmnode
*)fp
->f_data
;
3735 * Description: Get fileproc and POSIX semaphore pointer for a given fd from
3736 * the per process open file table of the specified process
3737 * and if successful, increment the f_iocount
3739 * Parameters: p Process in which fd lives
3740 * fd fd to get information for
3741 * resultfp Pointer to result fileproc
3742 * pointer area, or 0 if none
3743 * resultpsem Pointer to result POSIX
3744 * semaphore pointer area, or
3747 * Returns: EBADF The file descriptor is invalid
3748 * EBADF The file descriptor is not a POSIX
3753 * *resultfp (modified) Fileproc pointer
3754 * *resultpsem (modified) POSIX semaphore pointer
3756 * Notes: The second EBADF should probably be something else to make
3757 * the error condition distinct.
3759 * In order to support unnamed POSIX semaphores, the named
3760 * POSIX semaphores will have to move out of the per-process
3761 * open filetable, and into a global table that is shared with
3762 * unnamed POSIX semaphores, since unnamed POSIX semaphores
3763 * are typically used by declaring instances in shared memory,
3764 * and there's no other way to do this without changing the
3765 * underlying type, which would introduce binary compatibility
3769 fp_getfpsem(proc_t p
, int fd
, struct fileproc
**resultfp
,
3770 struct psemnode
**resultpsem
)
3772 struct filedesc
*fdp
= p
->p_fd
;
3773 struct fileproc
*fp
;
3775 proc_fdlock_spin(p
);
3776 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3777 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3778 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3782 if (fp
->f_type
!= DTYPE_PSXSEM
) {
3791 *resultpsem
= (struct psemnode
*)fp
->f_data
;
3801 * Description: Get fileproc and pipe pointer for a given fd from the
3802 * per process open file table of the specified process
3803 * and if successful, increment the f_iocount
3805 * Parameters: p Process in which fd lives
3806 * fd fd to get information for
3807 * resultfp Pointer to result fileproc
3808 * pointer area, or 0 if none
3809 * resultpipe Pointer to result pipe
3810 * pointer area, or 0 if none
3812 * Returns: EBADF The file descriptor is invalid
3813 * EBADF The file descriptor is not a socket
3817 * *resultfp (modified) Fileproc pointer
3818 * *resultpipe (modified) pipe pointer
3820 * Notes: The second EBADF should probably be something else to make
3821 * the error condition distinct.
3824 fp_getfpipe(proc_t p
, int fd
, struct fileproc
**resultfp
,
3825 struct pipe
**resultpipe
)
3827 struct filedesc
*fdp
= p
->p_fd
;
3828 struct fileproc
*fp
;
3830 proc_fdlock_spin(p
);
3831 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3832 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3833 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3837 if (fp
->f_type
!= DTYPE_PIPE
) {
3846 *resultpipe
= (struct pipe
*)fp
->f_data
;
3855 * Description: Get fileproc pointer for a given fd from the per process
3856 * open file table of the specified process and if successful,
3857 * increment the f_iocount
3859 * Parameters: p Process in which fd lives
3860 * fd fd to get information for
3861 * resultfp Pointer to result fileproc
3862 * pointer area, or 0 if none
3863 * locked !0 if the caller holds the
3864 * proc_fdlock, 0 otherwise
3866 * Returns: 0 Success
3867 * EBADF Bad file descriptor
3870 * *resultfp (modified) Fileproc pointer
3872 * Locks: If the argument 'locked' is non-zero, then the caller is
3873 * expected to have taken and held the proc_fdlock; if it is
3874 * zero, than this routine internally takes and drops this lock.
3877 fp_lookup(proc_t p
, int fd
, struct fileproc
**resultfp
, int locked
)
3879 struct filedesc
*fdp
= p
->p_fd
;
3880 struct fileproc
*fp
;
3883 proc_fdlock_spin(p
);
3884 if (fd
< 0 || fdp
== NULL
|| fd
>= fdp
->fd_nfiles
||
3885 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3886 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3905 * Description: Swap the fileproc pointer for a given fd with a new
3906 * fileproc pointer in the per-process open file table of
3907 * the specified process. The fdlock must be held at entry.
3909 * Parameters: p Process containing the fd
3910 * fd The fd of interest
3911 * nfp Pointer to the newfp
3913 * Returns: 0 Success
3914 * EBADF Bad file descriptor
3916 * EKEEPLOOKING f_iocount changed while lock was dropped.
3919 fp_tryswap(proc_t p
, int fd
, struct fileproc
*nfp
)
3921 struct fileproc
*fp
;
3924 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
3926 if (0 != (error
= fp_lookup(p
, fd
, &fp
, 1)))
3929 * At this point, our caller (change_guardedfd_np) has
3930 * one f_iocount reference, and we just took another
3931 * one to begin the replacement.
3933 if (fp
->f_iocount
< 2) {
3934 panic("f_iocount too small %d", fp
->f_iocount
);
3935 } else if (2 == fp
->f_iocount
) {
3937 /* Copy the contents of *fp, preserving the "type" of *nfp */
3939 nfp
->f_flags
= (nfp
->f_flags
& FP_TYPEMASK
) |
3940 (fp
->f_flags
& ~FP_TYPEMASK
);
3941 nfp
->f_iocount
= fp
->f_iocount
;
3942 nfp
->f_fglob
= fp
->f_fglob
;
3943 nfp
->f_wset
= fp
->f_wset
;
3945 p
->p_fd
->fd_ofiles
[fd
] = nfp
;
3946 (void) fp_drop(p
, fd
, nfp
, 1);
3949 * Wait for all other active references to evaporate.
3951 p
->p_fpdrainwait
= 1;
3952 error
= msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
,
3953 PRIBIO
| PCATCH
, "tryswap fpdrain", NULL
);
3956 * Return an "internal" errno to trigger a full
3957 * reevaluation of the change-guard attempt.
3959 error
= EKEEPLOOKING
;
3960 printf("%s: lookup collision fd %d\n", __func__
, fd
);
3962 (void) fp_drop(p
, fd
, fp
, 1);
3971 * Description: Set the FP_WRITTEN flag on the fileproc and drop the I/O
3972 * reference previously taken by calling fp_lookup et. al.
3974 * Parameters: p Process in which the fd lives
3975 * fd fd associated with the fileproc
3976 * fp fileproc on which to set the
3977 * flag and drop the reference
3979 * Returns: 0 Success
3980 * fp_drop:EBADF Bad file descriptor
3982 * Locks: This function internally takes and drops the proc_fdlock for
3983 * the supplied process
3985 * Notes: The fileproc must correspond to the fd in the supplied proc
3988 fp_drop_written(proc_t p
, int fd
, struct fileproc
*fp
)
3992 proc_fdlock_spin(p
);
3994 fp
->f_flags
|= FP_WRITTEN
;
3996 error
= fp_drop(p
, fd
, fp
, 1);
4007 * Description: Set the FP_WAITEVENT flag on the fileproc and drop the I/O
4008 * reference previously taken by calling fp_lookup et. al.
4010 * Parameters: p Process in which the fd lives
4011 * fd fd associated with the fileproc
4012 * fp fileproc on which to set the
4013 * flag and drop the reference
4015 * Returns: 0 Success
4016 * fp_drop:EBADF Bad file descriptor
4018 * Locks: This function internally takes and drops the proc_fdlock for
4019 * the supplied process
4021 * Notes: The fileproc must correspond to the fd in the supplied proc
4024 fp_drop_event(proc_t p
, int fd
, struct fileproc
*fp
)
4028 proc_fdlock_spin(p
);
4030 fp
->f_flags
|= FP_WAITEVENT
;
4032 error
= fp_drop(p
, fd
, fp
, 1);
4043 * Description: Drop the I/O reference previously taken by calling fp_lookup
4046 * Parameters: p Process in which the fd lives
4047 * fd fd associated with the fileproc
4048 * fp fileproc on which to set the
4049 * flag and drop the reference
4050 * locked flag to internally take and
4051 * drop proc_fdlock if it is not
4052 * already held by the caller
4054 * Returns: 0 Success
4055 * EBADF Bad file descriptor
4057 * Locks: This function internally takes and drops the proc_fdlock for
4058 * the supplied process if 'locked' is non-zero, and assumes that
4059 * the caller already holds this lock if 'locked' is non-zero.
4061 * Notes: The fileproc must correspond to the fd in the supplied proc
4064 fp_drop(proc_t p
, int fd
, struct fileproc
*fp
, int locked
)
4066 struct filedesc
*fdp
= p
->p_fd
;
4070 proc_fdlock_spin(p
);
4071 if ((fp
== FILEPROC_NULL
) && (fd
< 0 || fd
>= fdp
->fd_nfiles
||
4072 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
4073 ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
4074 !(fdp
->fd_ofileflags
[fd
] & UF_CLOSING
)))) {
4081 if (fp
->f_iocount
== 0) {
4082 if (fp
->f_flags
& FP_SELCONFLICT
)
4083 fp
->f_flags
&= ~FP_SELCONFLICT
;
4085 if (p
->p_fpdrainwait
) {
4086 p
->p_fpdrainwait
= 0;
4093 wakeup(&p
->p_fpdrainwait
);
4102 * Description: Given an fd, look it up in the current process's per process
4103 * open file table, and return its internal vnode pointer.
4105 * Parameters: fd fd to obtain vnode from
4106 * vpp pointer to vnode return area
4108 * Returns: 0 Success
4109 * EINVAL The fd does not refer to a
4110 * vnode fileproc entry
4111 * fp_lookup:EBADF Bad file descriptor
4114 * *vpp (modified) Returned vnode pointer
4116 * Locks: This function internally takes and drops the proc_fdlock for
4117 * the current process
4119 * Notes: If successful, this function increments the f_iocount on the
4120 * fd's corresponding fileproc.
4122 * The fileproc referenced is not returned; because of this, care
4123 * must be taken to not drop the last reference (e.g. by closing
4124 * the file). This is inherently unsafe, since the reference may
4125 * not be recoverable from the vnode, if there is a subsequent
4126 * close that destroys the associate fileproc. The caller should
4127 * therefore retain their own reference on the fileproc so that
4128 * the f_iocount can be dropped subsequently. Failure to do this
4129 * can result in the returned pointer immediately becoming invalid
4130 * following the call.
4132 * Use of this function is discouraged.
4135 file_vnode(int fd
, struct vnode
**vpp
)
4137 proc_t p
= current_proc();
4138 struct fileproc
*fp
;
4141 proc_fdlock_spin(p
);
4142 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4146 if (fp
->f_type
!= DTYPE_VNODE
) {
4147 fp_drop(p
, fd
, fp
,1);
4152 *vpp
= (struct vnode
*)fp
->f_data
;
4160 * file_vnode_withvid
4162 * Description: Given an fd, look it up in the current process's per process
4163 * open file table, and return its internal vnode pointer.
4165 * Parameters: fd fd to obtain vnode from
4166 * vpp pointer to vnode return area
4167 * vidp pointer to vid of the returned vnode
4169 * Returns: 0 Success
4170 * EINVAL The fd does not refer to a
4171 * vnode fileproc entry
4172 * fp_lookup:EBADF Bad file descriptor
4175 * *vpp (modified) Returned vnode pointer
4177 * Locks: This function internally takes and drops the proc_fdlock for
4178 * the current process
4180 * Notes: If successful, this function increments the f_iocount on the
4181 * fd's corresponding fileproc.
4183 * The fileproc referenced is not returned; because of this, care
4184 * must be taken to not drop the last reference (e.g. by closing
4185 * the file). This is inherently unsafe, since the reference may
4186 * not be recoverable from the vnode, if there is a subsequent
4187 * close that destroys the associate fileproc. The caller should
4188 * therefore retain their own reference on the fileproc so that
4189 * the f_iocount can be dropped subsequently. Failure to do this
4190 * can result in the returned pointer immediately becoming invalid
4191 * following the call.
4193 * Use of this function is discouraged.
4196 file_vnode_withvid(int fd
, struct vnode
**vpp
, uint32_t * vidp
)
4198 proc_t p
= current_proc();
4199 struct fileproc
*fp
;
4203 proc_fdlock_spin(p
);
4204 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4208 if (fp
->f_type
!= DTYPE_VNODE
) {
4209 fp_drop(p
, fd
, fp
,1);
4213 vp
= (struct vnode
*)fp
->f_data
;
4217 if ((vidp
!= NULL
) && (vp
!= NULLVP
))
4218 *vidp
= (uint32_t)vp
->v_id
;
4229 * Description: Given an fd, look it up in the current process's per process
4230 * open file table, and return its internal socket pointer.
4232 * Parameters: fd fd to obtain vnode from
4233 * sp pointer to socket return area
4235 * Returns: 0 Success
4236 * ENOTSOCK Not a socket
4237 * fp_lookup:EBADF Bad file descriptor
4240 * *sp (modified) Returned socket pointer
4242 * Locks: This function internally takes and drops the proc_fdlock for
4243 * the current process
4245 * Notes: If successful, this function increments the f_iocount on the
4246 * fd's corresponding fileproc.
4248 * The fileproc referenced is not returned; because of this, care
4249 * must be taken to not drop the last reference (e.g. by closing
4250 * the file). This is inherently unsafe, since the reference may
4251 * not be recoverable from the socket, if there is a subsequent
4252 * close that destroys the associate fileproc. The caller should
4253 * therefore retain their own reference on the fileproc so that
4254 * the f_iocount can be dropped subsequently. Failure to do this
4255 * can result in the returned pointer immediately becoming invalid
4256 * following the call.
4258 * Use of this function is discouraged.
4261 file_socket(int fd
, struct socket
**sp
)
4263 proc_t p
= current_proc();
4264 struct fileproc
*fp
;
4267 proc_fdlock_spin(p
);
4268 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4272 if (fp
->f_type
!= DTYPE_SOCKET
) {
4273 fp_drop(p
, fd
, fp
,1);
4277 *sp
= (struct socket
*)fp
->f_data
;
4287 * Description: Given an fd, look it up in the current process's per process
4288 * open file table, and return its fileproc's flags field.
4290 * Parameters: fd fd whose flags are to be
4292 * flags pointer to flags data area
4294 * Returns: 0 Success
4295 * ENOTSOCK Not a socket
4296 * fp_lookup:EBADF Bad file descriptor
4299 * *flags (modified) Returned flags field
4301 * Locks: This function internally takes and drops the proc_fdlock for
4302 * the current process
4304 * Notes: This function will internally increment and decrement the
4305 * f_iocount of the fileproc as part of its operation.
4308 file_flags(int fd
, int *flags
)
4311 proc_t p
= current_proc();
4312 struct fileproc
*fp
;
4315 proc_fdlock_spin(p
);
4316 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4320 *flags
= (int)fp
->f_flag
;
4321 fp_drop(p
, fd
, fp
,1);
4331 * Description: Drop an iocount reference on an fd, and wake up any waiters
4332 * for draining (i.e. blocked in fileproc_drain() called during
4333 * the last attempt to close a file).
4335 * Parameters: fd fd on which an ioreference is
4338 * Returns: 0 Success
4339 * EBADF Bad file descriptor
4341 * Description: Given an fd, look it up in the current process's per process
4342 * open file table, and drop it's fileproc's f_iocount by one
4344 * Notes: This is intended as a corresponding operation to the functions
4345 * file_vnode() and file_socket() operations.
4347 * Technically, the close reference is supposed to be protected
4348 * by a fileproc_drain(), however, a drain will only block if
4349 * the fd refers to a character device, and that device has had
4350 * preparefileread() called on it. If it refers to something
4351 * other than a character device, then the drain will occur and
4352 * block each close attempt, rather than merely the last close.
4354 * Since it's possible for an fd that refers to a character
4355 * device to have an intermediate close followed by an open to
4356 * cause a different file to correspond to that descriptor,
4357 * unless there was a cautionary reference taken on the fileproc,
4358 * this is an inherently unsafe function. This happens in the
4359 * case where multiple fd's in a process refer to the same
4360 * character device (e.g. stdin/out/err pointing to a tty, etc.).
4362 * Use of this function is discouraged.
4367 struct fileproc
*fp
;
4368 proc_t p
= current_proc();
4371 proc_fdlock_spin(p
);
4372 if (fd
< 0 || fd
>= p
->p_fd
->fd_nfiles
||
4373 (fp
= p
->p_fd
->fd_ofiles
[fd
]) == NULL
||
4374 ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
4375 !(p
->p_fd
->fd_ofileflags
[fd
] & UF_CLOSING
))) {
4381 if (fp
->f_iocount
== 0) {
4382 if (fp
->f_flags
& FP_SELCONFLICT
)
4383 fp
->f_flags
&= ~FP_SELCONFLICT
;
4385 if (p
->p_fpdrainwait
) {
4386 p
->p_fpdrainwait
= 0;
4393 wakeup(&p
->p_fpdrainwait
);
4398 static int falloc_withalloc_locked(proc_t
, struct fileproc
**, int *,
4399 vfs_context_t
, struct fileproc
* (*)(void *), void *, int);
4404 * Description: Allocate an entry in the per process open file table and
4405 * return the corresponding fileproc and fd.
4407 * Parameters: p The process in whose open file
4408 * table the fd is to be allocated
4409 * resultfp Pointer to fileproc pointer
4411 * resultfd Pointer to fd return area
4414 * Returns: 0 Success
4415 * falloc:ENFILE Too many open files in system
4416 * falloc:EMFILE Too many open files in process
4417 * falloc:ENOMEM M_FILEPROC or M_FILEGLOB zone
4421 * *resultfd (modified) Returned fileproc pointer
4422 * *resultfd (modified) Returned fd
4424 * Locks: This function takes and drops the proc_fdlock; if this lock
4425 * is already held, use falloc_locked() instead.
4427 * Notes: This function takes separate process and context arguments
4428 * solely to support kern_exec.c; otherwise, it would take
4429 * neither, and expect falloc_locked() to use the
4430 * vfs_context_current() routine internally.
4433 falloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
)
4435 return (falloc_withalloc(p
, resultfp
, resultfd
, ctx
,
4436 fileproc_alloc_init
, NULL
));
4440 * Like falloc, but including the fileproc allocator and create-args
4443 falloc_withalloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4444 vfs_context_t ctx
, fp_allocfn_t fp_zalloc
, void *arg
)
4449 error
= falloc_withalloc_locked(p
,
4450 resultfp
, resultfd
, ctx
, fp_zalloc
, arg
, 1);
4457 * "uninitialized" ops -- ensure fg->fg_ops->fo_type always exists
4459 static const struct fileops uninitops
;
4464 * Create a new open file structure and allocate
4465 * a file descriptor for the process that refers to it.
4467 * Returns: 0 Success
4469 * Description: Allocate an entry in the per process open file table and
4470 * return the corresponding fileproc and fd.
4472 * Parameters: p The process in whose open file
4473 * table the fd is to be allocated
4474 * resultfp Pointer to fileproc pointer
4476 * resultfd Pointer to fd return area
4478 * locked Flag to indicate whether the
4479 * caller holds proc_fdlock
4481 * Returns: 0 Success
4482 * ENFILE Too many open files in system
4483 * fdalloc:EMFILE Too many open files in process
4484 * ENOMEM M_FILEPROC or M_FILEGLOB zone
4489 * *resultfd (modified) Returned fileproc pointer
4490 * *resultfd (modified) Returned fd
4492 * Locks: If the parameter 'locked' is zero, this function takes and
4493 * drops the proc_fdlock; if non-zero, the caller must hold the
4496 * Notes: If you intend to use a non-zero 'locked' parameter, use the
4497 * utility function falloc() instead.
4499 * This function takes separate process and context arguments
4500 * solely to support kern_exec.c; otherwise, it would take
4501 * neither, and use the vfs_context_current() routine internally.
4504 falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4505 vfs_context_t ctx
, int locked
)
4507 return (falloc_withalloc_locked(p
, resultfp
, resultfd
, ctx
,
4508 fileproc_alloc_init
, NULL
, locked
));
4512 falloc_withalloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4513 vfs_context_t ctx
, fp_allocfn_t fp_zalloc
, void *crarg
,
4516 struct fileproc
*fp
;
4517 struct fileglob
*fg
;
4522 if ( (error
= fdalloc(p
, 0, &nfd
)) ) {
4527 if (nfiles
>= maxfiles
) {
4534 error
= mac_file_check_create(proc_ucred(p
));
4543 * Allocate a new file descriptor.
4544 * If the process has file descriptor zero open, add to the list
4545 * of open files at that point, otherwise put it at the front of
4546 * the list of open files.
4550 fp
= (*fp_zalloc
)(crarg
);
4556 MALLOC_ZONE(fg
, struct fileglob
*, sizeof(struct fileglob
), M_FILEGLOB
, M_WAITOK
);
4563 bzero(fg
, sizeof(struct fileglob
));
4564 lck_mtx_init(&fg
->fg_lock
, file_lck_grp
, file_lck_attr
);
4568 fg
->fg_ops
= &uninitops
;
4571 mac_file_label_init(fg
);
4574 kauth_cred_ref(ctx
->vc_ucred
);
4578 fp
->f_cred
= ctx
->vc_ucred
;
4581 mac_file_label_associate(fp
->f_cred
, fg
);
4584 OSAddAtomic(1, &nfiles
);
4586 p
->p_fd
->fd_ofiles
[nfd
] = fp
;
4603 * Description: Free a file structure; drop the global open file count, and
4604 * drop the credential reference, if the fileglob has one, and
4605 * destroy the instance mutex before freeing
4607 * Parameters: fg Pointer to fileglob to be
4613 fg_free(struct fileglob
*fg
)
4615 OSAddAtomic(-1, &nfiles
);
4617 if (fg
->fg_vn_data
) {
4618 fg_vn_data_free(fg
->fg_vn_data
);
4619 fg
->fg_vn_data
= NULL
;
4622 if (IS_VALID_CRED(fg
->fg_cred
)) {
4623 kauth_cred_unref(&fg
->fg_cred
);
4625 lck_mtx_destroy(&fg
->fg_lock
, file_lck_grp
);
4628 mac_file_label_destroy(fg
);
4630 FREE_ZONE(fg
, sizeof *fg
, M_FILEGLOB
);
4637 * Description: Perform close-on-exec processing for all files in a process
4638 * that are either marked as close-on-exec, or which were in the
4639 * process of being opened at the time of the execve
4641 * Also handles the case (via posix_spawn()) where -all-
4642 * files except those marked with "inherit" as treated as
4645 * Parameters: p Pointer to process calling
4650 * Locks: This function internally takes and drops proc_fdlock()
4654 fdexec(proc_t p
, short flags
)
4656 struct filedesc
*fdp
= p
->p_fd
;
4658 boolean_t cloexec_default
= (flags
& POSIX_SPAWN_CLOEXEC_DEFAULT
) != 0;
4661 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
4663 struct fileproc
*fp
= fdp
->fd_ofiles
[i
];
4664 char *flagp
= &fdp
->fd_ofileflags
[i
];
4666 if (fp
&& cloexec_default
) {
4668 * Reverse the usual semantics of file descriptor
4669 * inheritance - all of them should be closed
4670 * except files marked explicitly as "inherit" and
4671 * not marked close-on-exec.
4673 if ((*flagp
& (UF_EXCLOSE
|UF_INHERIT
)) != UF_INHERIT
)
4674 *flagp
|= UF_EXCLOSE
;
4675 *flagp
&= ~UF_INHERIT
;
4679 ((*flagp
& (UF_RESERVED
|UF_EXCLOSE
)) == UF_EXCLOSE
)
4681 || (fp
&& mac_file_check_inherit(proc_ucred(p
), fp
->f_fglob
))
4684 if (i
< fdp
->fd_knlistsize
)
4685 knote_fdclose(p
, i
);
4686 procfdtbl_clearfd(p
, i
);
4687 if (i
== fdp
->fd_lastfile
&& i
> 0)
4689 if (i
< fdp
->fd_freefile
)
4690 fdp
->fd_freefile
= i
;
4693 * Wait for any third party viewers (e.g., lsof)
4694 * to release their references to this fileproc.
4696 while (fp
->f_iocount
> 0) {
4697 p
->p_fpdrainwait
= 1;
4698 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
,
4702 closef_locked(fp
, fp
->f_fglob
, p
);
4714 * Description: Copy a filedesc structure. This is normally used as part of
4715 * forkproc() when forking a new process, to copy the per process
4716 * open file table over to the new process.
4718 * Parameters: p Process whose open file table
4719 * is to be copied (parent)
4720 * uth_cdir Per thread current working
4721 * cirectory, or NULL
4723 * Returns: NULL Copy failed
4724 * !NULL Pointer to new struct filedesc
4726 * Locks: This function internally takes and drops proc_fdlock()
4728 * Notes: Files are copied directly, ignoring the new resource limits
4729 * for the process that's being copied into. Since the descriptor
4730 * references are just additional references, this does not count
4731 * against the number of open files on the system.
4733 * The struct filedesc includes the current working directory,
4734 * and the current root directory, if the process is chroot'ed.
4736 * If the exec was called by a thread using a per thread current
4737 * working directory, we inherit the working directory from the
4738 * thread making the call, rather than from the process.
4740 * In the case of a failure to obtain a reference, for most cases,
4741 * the file entry will be silently dropped. There's an exception
4742 * for the case of a chroot dir, since a failure to to obtain a
4743 * reference there would constitute an "escape" from the chroot
4744 * environment, which must not be allowed. In that case, we will
4745 * deny the execve() operation, rather than allowing the escape.
4748 fdcopy(proc_t p
, vnode_t uth_cdir
)
4750 struct filedesc
*newfdp
, *fdp
= p
->p_fd
;
4752 struct fileproc
*ofp
, *fp
;
4755 MALLOC_ZONE(newfdp
, struct filedesc
*,
4756 sizeof(*newfdp
), M_FILEDESC
, M_WAITOK
);
4763 * the FD_CHROOT flag will be inherited via this copy
4765 (void) memcpy(newfdp
, fdp
, sizeof(*newfdp
));
4768 * If we are running with per-thread current working directories,
4769 * inherit the new current working directory from the current thread
4770 * instead, before we take our references.
4772 if (uth_cdir
!= NULLVP
)
4773 newfdp
->fd_cdir
= uth_cdir
;
4776 * For both fd_cdir and fd_rdir make sure we get
4777 * a valid reference... if we can't, than set
4778 * set the pointer(s) to NULL in the child... this
4779 * will keep us from using a non-referenced vp
4780 * and allows us to do the vnode_rele only on
4781 * a properly referenced vp
4783 if ( (v_dir
= newfdp
->fd_cdir
) ) {
4784 if (vnode_getwithref(v_dir
) == 0) {
4785 if ( (vnode_ref(v_dir
)) )
4786 newfdp
->fd_cdir
= NULL
;
4789 newfdp
->fd_cdir
= NULL
;
4791 if (newfdp
->fd_cdir
== NULL
&& fdp
->fd_cdir
) {
4793 * we couldn't get a new reference on
4794 * the current working directory being
4795 * inherited... we might as well drop
4796 * our reference from the parent also
4797 * since the vnode has gone DEAD making
4798 * it useless... by dropping it we'll
4799 * be that much closer to recycling it
4801 vnode_rele(fdp
->fd_cdir
);
4802 fdp
->fd_cdir
= NULL
;
4805 if ( (v_dir
= newfdp
->fd_rdir
) ) {
4806 if (vnode_getwithref(v_dir
) == 0) {
4807 if ( (vnode_ref(v_dir
)) )
4808 newfdp
->fd_rdir
= NULL
;
4811 newfdp
->fd_rdir
= NULL
;
4814 /* Coming from a chroot environment and unable to get a reference... */
4815 if (newfdp
->fd_rdir
== NULL
&& fdp
->fd_rdir
) {
4817 * We couldn't get a new reference on
4818 * the chroot directory being
4819 * inherited... this is fatal, since
4820 * otherwise it would constitute an
4821 * escape from a chroot environment by
4824 if (newfdp
->fd_cdir
)
4825 vnode_rele(newfdp
->fd_cdir
);
4826 FREE_ZONE(newfdp
, sizeof *newfdp
, M_FILEDESC
);
4831 * If the number of open files fits in the internal arrays
4832 * of the open file structure, use them, otherwise allocate
4833 * additional memory for the number of descriptors currently
4836 if (newfdp
->fd_lastfile
< NDFILE
)
4840 * Compute the smallest multiple of NDEXTENT needed
4841 * for the file descriptors currently in use,
4842 * allowing the table to shrink.
4844 i
= newfdp
->fd_nfiles
;
4845 while (i
> 1 + 2 * NDEXTENT
&& i
> 1 + newfdp
->fd_lastfile
* 2)
4850 MALLOC_ZONE(newfdp
->fd_ofiles
, struct fileproc
**,
4851 i
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
4852 if (newfdp
->fd_ofiles
== NULL
) {
4853 if (newfdp
->fd_cdir
)
4854 vnode_rele(newfdp
->fd_cdir
);
4855 if (newfdp
->fd_rdir
)
4856 vnode_rele(newfdp
->fd_rdir
);
4858 FREE_ZONE(newfdp
, sizeof(*newfdp
), M_FILEDESC
);
4861 (void) memset(newfdp
->fd_ofiles
, 0, i
* OFILESIZE
);
4864 newfdp
->fd_ofileflags
= (char *) &newfdp
->fd_ofiles
[i
];
4865 newfdp
->fd_nfiles
= i
;
4867 if (fdp
->fd_nfiles
> 0) {
4868 struct fileproc
**fpp
;
4871 (void) memcpy(newfdp
->fd_ofiles
, fdp
->fd_ofiles
,
4872 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofiles
));
4873 (void) memcpy(newfdp
->fd_ofileflags
, fdp
->fd_ofileflags
,
4874 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofileflags
));
4877 * kq descriptors cannot be copied.
4879 if (newfdp
->fd_knlistsize
!= -1) {
4880 fpp
= &newfdp
->fd_ofiles
[newfdp
->fd_lastfile
];
4881 flags
= &newfdp
->fd_ofileflags
[newfdp
->fd_lastfile
];
4882 for (i
= newfdp
->fd_lastfile
;
4883 i
>= 0; i
--, fpp
--, flags
--) {
4884 if (*flags
& UF_RESERVED
)
4885 continue; /* (removed below) */
4886 if (*fpp
!= NULL
&& (*fpp
)->f_type
== DTYPE_KQUEUE
) {
4889 if (i
< newfdp
->fd_freefile
)
4890 newfdp
->fd_freefile
= i
;
4892 if (*fpp
== NULL
&& i
== newfdp
->fd_lastfile
&& i
> 0)
4893 newfdp
->fd_lastfile
--;
4895 newfdp
->fd_knlist
= NULL
;
4896 newfdp
->fd_knlistsize
= -1;
4897 newfdp
->fd_knhash
= NULL
;
4898 newfdp
->fd_knhashmask
= 0;
4900 fpp
= newfdp
->fd_ofiles
;
4901 flags
= newfdp
->fd_ofileflags
;
4903 for (i
= newfdp
->fd_lastfile
+ 1; --i
>= 0; fpp
++, flags
++)
4904 if ((ofp
= *fpp
) != NULL
&&
4905 0 == (ofp
->f_fglob
->fg_lflags
& FG_CONFINED
) &&
4906 0 == (*flags
& (UF_FORKCLOSE
|UF_RESERVED
))) {
4908 if (FILEPROC_TYPE(ofp
) != FTYPE_SIMPLE
)
4909 panic("complex fileproc");
4911 fp
= fileproc_alloc_init(NULL
);
4914 * XXX no room to copy, unable to
4915 * XXX safely unwind state at present
4920 (ofp
->f_flags
& ~FP_TYPEMASK
);
4921 fp
->f_fglob
= ofp
->f_fglob
;
4926 if (i
< newfdp
->fd_freefile
)
4927 newfdp
->fd_freefile
= i
;
4941 * Description: Release a filedesc (per process open file table) structure;
4942 * this is done on process exit(), or from forkproc_free() if
4943 * the fork fails for some reason subsequent to a successful
4946 * Parameters: p Pointer to process going away
4950 * Locks: This function internally takes and drops proc_fdlock()
4955 struct filedesc
*fdp
;
4956 struct fileproc
*fp
;
4961 if (p
== kernproc
|| NULL
== (fdp
= p
->p_fd
)) {
4966 extern struct filedesc filedesc0
;
4968 if (&filedesc0
== fdp
)
4971 if (fdp
->fd_nfiles
> 0 && fdp
->fd_ofiles
) {
4972 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
4973 if ((fp
= fdp
->fd_ofiles
[i
]) != NULL
) {
4975 if (fdp
->fd_ofileflags
[i
] & UF_RESERVED
)
4976 panic("fdfree: found fp with UF_RESERVED");
4978 procfdtbl_reservefd(p
, i
);
4980 if (i
< fdp
->fd_knlistsize
)
4981 knote_fdclose(p
, i
);
4982 if (fp
->f_flags
& FP_WAITEVENT
)
4983 (void)waitevent_close(p
, fp
);
4984 (void) closef_locked(fp
, fp
->f_fglob
, p
);
4988 FREE_ZONE(fdp
->fd_ofiles
, fdp
->fd_nfiles
* OFILESIZE
, M_OFILETABL
);
4989 fdp
->fd_ofiles
= NULL
;
4996 vnode_rele(fdp
->fd_cdir
);
4998 vnode_rele(fdp
->fd_rdir
);
5000 proc_fdlock_spin(p
);
5005 FREE(fdp
->fd_knlist
, M_KQUEUE
);
5007 FREE(fdp
->fd_knhash
, M_KQUEUE
);
5009 FREE_ZONE(fdp
, sizeof(*fdp
), M_FILEDESC
);
5015 * Description: Internal form of closef; called with proc_fdlock held
5017 * Parameters: fp Pointer to fileproc for fd
5018 * fg Pointer to fileglob for fd
5019 * p Pointer to proc structure
5021 * Returns: 0 Success
5022 * closef_finish:??? Anything returnable by a per-fileops
5025 * Note: Decrements reference count on file structure; if this was the
5026 * last reference, then closef_finish() is called
5028 * p and fp are allowed to be NULL when closing a file that was
5029 * being passed in a message (but only if we are called when this
5030 * is NOT the last reference).
5033 closef_locked(struct fileproc
*fp
, struct fileglob
*fg
, proc_t p
)
5037 struct vfs_context context
;
5044 /* Set up context with cred stashed in fg */
5045 if (p
== current_proc())
5046 context
.vc_thread
= current_thread();
5048 context
.vc_thread
= NULL
;
5049 context
.vc_ucred
= fg
->fg_cred
;
5052 * POSIX record locking dictates that any close releases ALL
5053 * locks owned by this process. This is handled by setting
5054 * a flag in the unlock to free ONLY locks obeying POSIX
5055 * semantics, and not to free BSD-style file locks.
5056 * If the descriptor was in a message, POSIX-style locks
5057 * aren't passed with the descriptor.
5059 if (p
&& (p
->p_ladvflag
& P_LADVLOCK
) &&
5060 DTYPE_VNODE
== FILEGLOB_DTYPE(fg
)) {
5063 lf
.l_whence
= SEEK_SET
;
5066 lf
.l_type
= F_UNLCK
;
5067 vp
= (struct vnode
*)fg
->fg_data
;
5069 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
5070 (void) VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &lf
, F_POSIX
, &context
, NULL
);
5071 (void)vnode_put(vp
);
5075 lck_mtx_lock_spin(&fg
->fg_lock
);
5078 if (fg
->fg_count
> 0) {
5079 lck_mtx_unlock(&fg
->fg_lock
);
5083 if (fg
->fg_count
!= 0)
5084 panic("fg %p: being freed with bad fg_count (%d)", fg
, fg
->fg_count
);
5087 if (fp
&& (fp
->f_flags
& FP_WRITTEN
))
5088 fg
->fg_flag
|= FWASWRITTEN
;
5090 fg
->fg_lflags
|= FG_TERM
;
5091 lck_mtx_unlock(&fg
->fg_lock
);
5096 /* Since we ensure that fg->fg_ops is always initialized,
5097 * it is safe to invoke fo_close on the fg */
5098 error
= fo_close(fg
, &context
);
5112 * Description: Drain out pending I/O operations
5114 * Parameters: p Process closing this file
5115 * fp fileproc struct for the open
5116 * instance on the file
5120 * Locks: Assumes the caller holds the proc_fdlock
5122 * Notes: For character devices, this occurs on the last close of the
5123 * device; for all other file descriptors, this occurs on each
5124 * close to prevent fd's from being closed out from under
5125 * operations currently in progress and blocked
5127 * See Also: file_vnode(), file_socket(), file_drop(), and the cautions
5128 * regarding their use and interaction with this function.
5131 fileproc_drain(proc_t p
, struct fileproc
* fp
)
5133 struct vfs_context context
;
5135 context
.vc_thread
= proc_thread(p
); /* XXX */
5136 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
5138 fp
->f_iocount
-- ; /* (the one the close holds) */
5140 while (fp
->f_iocount
) {
5142 lck_mtx_convert_spin(&p
->p_fdmlock
);
5144 if (fp
->f_fglob
->fg_ops
->fo_drain
) {
5145 (*fp
->f_fglob
->fg_ops
->fo_drain
)(fp
, &context
);
5147 if ((fp
->f_flags
& FP_INSELECT
) == FP_INSELECT
) {
5148 if (waitq_wakeup64_all((struct waitq
*)fp
->f_wset
, NO_EVENT64
,
5149 THREAD_INTERRUPTED
, WAITQ_ALL_PRIORITIES
) == KERN_INVALID_ARGUMENT
)
5150 panic("bad wait queue for waitq_wakeup64_all %p (fp:%p)", fp
->f_wset
, fp
);
5152 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
) {
5153 if (waitq_wakeup64_all(&select_conflict_queue
, NO_EVENT64
,
5154 THREAD_INTERRUPTED
, WAITQ_ALL_PRIORITIES
) == KERN_INVALID_ARGUMENT
)
5155 panic("bad select_conflict_queue");
5157 p
->p_fpdrainwait
= 1;
5159 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
, "fpdrain", NULL
);
5163 if ((fp
->f_flags
& FP_INSELECT
) != 0)
5164 panic("FP_INSELECT set on drained fp");
5166 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
)
5167 fp
->f_flags
&= ~FP_SELCONFLICT
;
5174 * Description: Release the fd and free the fileproc associated with the fd
5175 * in the per process open file table of the specified process;
5176 * these values must correspond.
5178 * Parameters: p Process containing fd
5179 * fd fd to be released
5180 * fp fileproc to be freed
5182 * Returns: 0 Success
5184 * Notes: XXX function should be void - no one interprets the returns
5188 fp_free(proc_t p
, int fd
, struct fileproc
* fp
)
5190 proc_fdlock_spin(p
);
5194 fg_free(fp
->f_fglob
);
5203 * Description: Apply an advisory lock on a file descriptor.
5205 * Parameters: p Process making request
5206 * uap->fd fd on which the lock is to be
5208 * uap->how (Un)Lock bits, including type
5209 * retval Pointer to the call return area
5211 * Returns: 0 Success
5212 * fp_getfvp:EBADF Bad file descriptor
5213 * fp_getfvp:ENOTSUP fd does not refer to a vnode
5214 * vnode_getwithref:???
5218 * *retval (modified) Size of dtable
5220 * Notes: Just attempt to get a record lock of the requested type on
5221 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
5224 flock(proc_t p
, struct flock_args
*uap
, __unused
int32_t *retval
)
5228 struct fileproc
*fp
;
5231 vfs_context_t ctx
= vfs_context_current();
5234 AUDIT_ARG(fd
, uap
->fd
);
5235 if ( (error
= fp_getfvp(p
, fd
, &fp
, &vp
)) ) {
5238 if ( (error
= vnode_getwithref(vp
)) ) {
5241 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
5243 lf
.l_whence
= SEEK_SET
;
5246 if (how
& LOCK_UN
) {
5247 lf
.l_type
= F_UNLCK
;
5248 fp
->f_flag
&= ~FHASLOCK
;
5249 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_UNLCK
, &lf
, F_FLOCK
, ctx
, NULL
);
5253 lf
.l_type
= F_WRLCK
;
5254 else if (how
& LOCK_SH
)
5255 lf
.l_type
= F_RDLCK
;
5261 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
, F_SETLK
, &lf
);
5265 fp
->f_flag
|= FHASLOCK
;
5266 if (how
& LOCK_NB
) {
5267 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, F_FLOCK
, ctx
, NULL
);
5270 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, F_FLOCK
|F_WAIT
, ctx
, NULL
);
5272 (void)vnode_put(vp
);
5274 fp_drop(p
, fd
, fp
, 0);
5282 * Description: Obtain a Mach send right for a given file descriptor.
5284 * Parameters: p Process calling fileport
5285 * uap->fd The fd to reference
5286 * uap->portnamep User address at which to place port name.
5288 * Returns: 0 Success.
5289 * EBADF Bad file descriptor.
5290 * EINVAL File descriptor had type that cannot be sent, misc. other errors.
5291 * EFAULT Address at which to store port name is not valid.
5292 * EAGAIN Resource shortage.
5295 * On success, name of send right is stored at user-specified address.
5298 fileport_makeport(proc_t p
, struct fileport_makeport_args
*uap
,
5299 __unused
int *retval
)
5303 user_addr_t user_portaddr
= uap
->portnamep
;
5304 struct fileproc
*fp
= FILEPROC_NULL
;
5305 struct fileglob
*fg
= NULL
;
5306 ipc_port_t fileport
;
5307 mach_port_name_t name
= MACH_PORT_NULL
;
5310 err
= fp_lookup(p
, fd
, &fp
, 1);
5315 if (!file_issendable(p
, fp
)) {
5320 if (FP_ISGUARDED(fp
, GUARD_FILEPORT
)) {
5321 err
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_FILEPORT
);
5325 /* Dropped when port is deallocated */
5331 /* Allocate and initialize a port */
5332 fileport
= fileport_alloc(fg
);
5333 if (fileport
== IPC_PORT_NULL
) {
5339 /* Add an entry. Deallocates port on failure. */
5340 name
= ipc_port_copyout_send(fileport
, get_task_ipcspace(p
->task
));
5341 if (!MACH_PORT_VALID(name
)) {
5346 err
= copyout(&name
, user_portaddr
, sizeof(mach_port_name_t
));
5351 /* Tag the fileglob for debugging purposes */
5352 lck_mtx_lock_spin(&fg
->fg_lock
);
5353 fg
->fg_lflags
|= FG_PORTMADE
;
5354 lck_mtx_unlock(&fg
->fg_lock
);
5356 fp_drop(p
, fd
, fp
, 0);
5363 if (MACH_PORT_VALID(name
)) {
5364 /* Don't care if another thread races us to deallocate the entry */
5365 (void) mach_port_deallocate(get_task_ipcspace(p
->task
), name
);
5368 if (fp
!= FILEPROC_NULL
) {
5369 fp_drop(p
, fd
, fp
, 0);
5376 fileport_releasefg(struct fileglob
*fg
)
5378 (void)closef_locked(NULL
, fg
, PROC_NULL
);
5387 * Description: Obtain the file descriptor for a given Mach send right.
5389 * Parameters: p Process calling fileport
5390 * uap->port Name of send right to file port.
5392 * Returns: 0 Success
5393 * EINVAL Invalid Mach port name, or port is not for a file.
5395 * fdalloc:ENOMEM Unable to allocate fileproc or extend file table.
5398 * *retval (modified) The new descriptor
5401 fileport_makefd(proc_t p
, struct fileport_makefd_args
*uap
, int32_t *retval
)
5403 struct fileglob
*fg
;
5404 struct fileproc
*fp
= FILEPROC_NULL
;
5405 ipc_port_t port
= IPC_PORT_NULL
;
5406 mach_port_name_t send
= uap
->port
;
5411 res
= ipc_object_copyin(get_task_ipcspace(p
->task
),
5412 send
, MACH_MSG_TYPE_COPY_SEND
, &port
);
5414 if (res
!= KERN_SUCCESS
) {
5419 fg
= fileport_port_to_fileglob(port
);
5425 fp
= fileproc_alloc_init(NULL
);
5426 if (fp
== FILEPROC_NULL
) {
5435 err
= fdalloc(p
, 0, &fd
);
5440 *fdflags(p
, fd
) |= UF_EXCLOSE
;
5442 procfdtbl_releasefd(p
, fd
, fp
);
5448 if ((fp
!= NULL
) && (0 != err
)) {
5452 if (IPC_PORT_NULL
!= port
) {
5453 ipc_port_release_send(port
);
5463 * Description: Duplicate the specified descriptor to a free descriptor;
5464 * this is the second half of fdopen(), above.
5466 * Parameters: fdp filedesc pointer to fill in
5468 * dfd fd to dup from
5469 * mode mode to set on new fd
5470 * error command code
5472 * Returns: 0 Success
5473 * EBADF Source fd is bad
5474 * EACCES Requested mode not allowed
5475 * !0 'error', if not ENODEV or
5478 * Notes: XXX This is not thread safe; see fdopen() above
5481 dupfdopen(struct filedesc
*fdp
, int indx
, int dfd
, int flags
, int error
)
5483 struct fileproc
*wfp
;
5484 struct fileproc
*fp
;
5488 proc_t p
= current_proc();
5491 * If the to-be-dup'd fd number is greater than the allowed number
5492 * of file descriptors, or the fd to be dup'd has already been
5493 * closed, reject. Note, check for new == old is necessary as
5494 * falloc could allocate an already closed to-be-dup'd descriptor
5495 * as the new descriptor.
5499 fp
= fdp
->fd_ofiles
[indx
];
5500 if (dfd
< 0 || dfd
>= fdp
->fd_nfiles
||
5501 (wfp
= fdp
->fd_ofiles
[dfd
]) == NULL
|| wfp
== fp
||
5502 (fdp
->fd_ofileflags
[dfd
] & UF_RESERVED
)) {
5508 myerror
= mac_file_check_dup(proc_ucred(p
), wfp
->f_fglob
, dfd
);
5515 * There are two cases of interest here.
5517 * For ENODEV simply dup (dfd) to file descriptor
5518 * (indx) and return.
5520 * For ENXIO steal away the file structure from (dfd) and
5521 * store it in (indx). (dfd) is effectively closed by
5524 * Any other error code is just returned.
5528 if (FP_ISGUARDED(wfp
, GUARD_DUP
)) {
5534 * Check that the mode the file is being opened for is a
5535 * subset of the mode of the existing descriptor.
5537 if (((flags
& (FREAD
|FWRITE
)) | wfp
->f_flag
) != wfp
->f_flag
) {
5541 if (indx
> fdp
->fd_lastfile
)
5542 fdp
->fd_lastfile
= indx
;
5546 fg_free(fp
->f_fglob
);
5547 fp
->f_fglob
= wfp
->f_fglob
;
5549 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
] |
5550 (flags
& O_CLOEXEC
) ? UF_EXCLOSE
: 0;
5566 * Description: Add a reference to a fileglob by fileproc
5568 * Parameters: fp fileproc containing fileglob
5573 * Notes: XXX Should use OSAddAtomic?
5576 fg_ref(struct fileproc
* fp
)
5578 struct fileglob
*fg
;
5582 lck_mtx_lock_spin(&fg
->fg_lock
);
5585 if ((fp
->f_flags
& ~((unsigned int)FP_VALID_FLAGS
)) != 0)
5586 panic("fg_ref: invalid bits on fp %p", fp
);
5588 if (fg
->fg_count
== 0)
5589 panic("fg_ref: adding fgcount to zeroed fg: fp %p fg %p",
5593 lck_mtx_unlock(&fg
->fg_lock
);
5600 * Description: Remove a reference to a fileglob by fileproc
5602 * Parameters: fp fileproc containing fileglob
5607 * Notes: XXX Should use OSAddAtomic?
5610 fg_drop(struct fileproc
* fp
)
5612 struct fileglob
*fg
;
5615 lck_mtx_lock_spin(&fg
->fg_lock
);
5617 lck_mtx_unlock(&fg
->fg_lock
);
5622 * fg_insertuipc_mark
5624 * Description: Mark fileglob for insertion onto message queue if needed
5625 * Also takes fileglob reference
5627 * Parameters: fg Fileglob pointer to insert
5629 * Returns: true, if the fileglob needs to be inserted onto msg queue
5631 * Locks: Takes and drops fg_lock, potentially many times
5634 fg_insertuipc_mark(struct fileglob
* fg
)
5636 boolean_t insert
= FALSE
;
5638 lck_mtx_lock_spin(&fg
->fg_lock
);
5639 while (fg
->fg_lflags
& FG_RMMSGQ
) {
5640 lck_mtx_convert_spin(&fg
->fg_lock
);
5642 fg
->fg_lflags
|= FG_WRMMSGQ
;
5643 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_insertuipc", NULL
);
5648 if (fg
->fg_msgcount
== 1) {
5649 fg
->fg_lflags
|= FG_INSMSGQ
;
5652 lck_mtx_unlock(&fg
->fg_lock
);
5659 * Description: Insert marked fileglob onto message queue
5661 * Parameters: fg Fileglob pointer to insert
5665 * Locks: Takes and drops fg_lock & uipc_lock
5666 * DO NOT call this function with proc_fdlock held as unp_gc()
5667 * can potentially try to acquire proc_fdlock, which can result
5668 * in a deadlock if this function is in unp_gc_wait().
5671 fg_insertuipc(struct fileglob
* fg
)
5673 if (fg
->fg_lflags
& FG_INSMSGQ
) {
5674 lck_mtx_lock_spin(uipc_lock
);
5676 LIST_INSERT_HEAD(&fmsghead
, fg
, f_msglist
);
5677 lck_mtx_unlock(uipc_lock
);
5678 lck_mtx_lock(&fg
->fg_lock
);
5679 fg
->fg_lflags
&= ~FG_INSMSGQ
;
5680 if (fg
->fg_lflags
& FG_WINSMSGQ
) {
5681 fg
->fg_lflags
&= ~FG_WINSMSGQ
;
5682 wakeup(&fg
->fg_lflags
);
5684 lck_mtx_unlock(&fg
->fg_lock
);
5689 * fg_removeuipc_mark
5691 * Description: Mark the fileglob for removal from message queue if needed
5692 * Also releases fileglob message queue reference
5694 * Parameters: fg Fileglob pointer to remove
5696 * Returns: true, if the fileglob needs to be removed from msg queue
5698 * Locks: Takes and drops fg_lock, potentially many times
5701 fg_removeuipc_mark(struct fileglob
* fg
)
5703 boolean_t remove
= FALSE
;
5705 lck_mtx_lock_spin(&fg
->fg_lock
);
5706 while (fg
->fg_lflags
& FG_INSMSGQ
) {
5707 lck_mtx_convert_spin(&fg
->fg_lock
);
5709 fg
->fg_lflags
|= FG_WINSMSGQ
;
5710 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_removeuipc", NULL
);
5713 if (fg
->fg_msgcount
== 0) {
5714 fg
->fg_lflags
|= FG_RMMSGQ
;
5717 lck_mtx_unlock(&fg
->fg_lock
);
5724 * Description: Remove marked fileglob from message queue
5726 * Parameters: fg Fileglob pointer to remove
5730 * Locks: Takes and drops fg_lock & uipc_lock
5731 * DO NOT call this function with proc_fdlock held as unp_gc()
5732 * can potentially try to acquire proc_fdlock, which can result
5733 * in a deadlock if this function is in unp_gc_wait().
5736 fg_removeuipc(struct fileglob
* fg
)
5738 if (fg
->fg_lflags
& FG_RMMSGQ
) {
5739 lck_mtx_lock_spin(uipc_lock
);
5741 LIST_REMOVE(fg
, f_msglist
);
5742 lck_mtx_unlock(uipc_lock
);
5743 lck_mtx_lock(&fg
->fg_lock
);
5744 fg
->fg_lflags
&= ~FG_RMMSGQ
;
5745 if (fg
->fg_lflags
& FG_WRMMSGQ
) {
5746 fg
->fg_lflags
&= ~FG_WRMMSGQ
;
5747 wakeup(&fg
->fg_lflags
);
5749 lck_mtx_unlock(&fg
->fg_lock
);
5752 #endif /* SOCKETS */
5757 * Description: Generic fileops read indirected through the fileops pointer
5758 * in the fileproc structure
5760 * Parameters: fp fileproc structure pointer
5761 * uio user I/O structure pointer
5763 * ctx VFS context for operation
5765 * Returns: 0 Success
5766 * !0 Errno from read
5769 fo_read(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
5771 return ((*fp
->f_ops
->fo_read
)(fp
, uio
, flags
, ctx
));
5778 * Description: Generic fileops write indirected through the fileops pointer
5779 * in the fileproc structure
5781 * Parameters: fp fileproc structure pointer
5782 * uio user I/O structure pointer
5784 * ctx VFS context for operation
5786 * Returns: 0 Success
5787 * !0 Errno from write
5790 fo_write(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
5792 return((*fp
->f_ops
->fo_write
)(fp
, uio
, flags
, ctx
));
5799 * Description: Generic fileops ioctl indirected through the fileops pointer
5800 * in the fileproc structure
5802 * Parameters: fp fileproc structure pointer
5804 * data pointer to internalized copy
5805 * of user space ioctl command
5806 * parameter data in kernel space
5807 * ctx VFS context for operation
5809 * Returns: 0 Success
5810 * !0 Errno from ioctl
5812 * Locks: The caller is assumed to have held the proc_fdlock; this
5813 * function releases and reacquires this lock. If the caller
5814 * accesses data protected by this lock prior to calling this
5815 * function, it will need to revalidate/reacquire any cached
5816 * protected data obtained prior to the call.
5819 fo_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
, vfs_context_t ctx
)
5823 proc_fdunlock(vfs_context_proc(ctx
));
5824 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, com
, data
, ctx
);
5825 proc_fdlock(vfs_context_proc(ctx
));
5833 * Description: Generic fileops select indirected through the fileops pointer
5834 * in the fileproc structure
5836 * Parameters: fp fileproc structure pointer
5837 * which select which
5838 * wql pointer to wait queue list
5839 * ctx VFS context for operation
5841 * Returns: 0 Success
5842 * !0 Errno from select
5845 fo_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
5847 return((*fp
->f_ops
->fo_select
)(fp
, which
, wql
, ctx
));
5854 * Description: Generic fileops close indirected through the fileops pointer
5855 * in the fileproc structure
5857 * Parameters: fp fileproc structure pointer for
5859 * ctx VFS context for operation
5861 * Returns: 0 Success
5862 * !0 Errno from close
5865 fo_close(struct fileglob
*fg
, vfs_context_t ctx
)
5867 return((*fg
->fg_ops
->fo_close
)(fg
, ctx
));
5874 * Description: Generic fileops kqueue filter indirected through the fileops
5875 * pointer in the fileproc structure
5877 * Parameters: fp fileproc structure pointer
5878 * kn pointer to knote to filter on
5879 * ctx VFS context for operation
5881 * Returns: 0 Success
5882 * !0 Errno from kqueue filter
5885 fo_kqfilter(struct fileproc
*fp
, struct knote
*kn
, vfs_context_t ctx
)
5887 return ((*fp
->f_ops
->fo_kqfilter
)(fp
, kn
, ctx
));
5891 * The ability to send a file descriptor to another
5892 * process is opt-in by file type.
5895 file_issendable(proc_t p
, struct fileproc
*fp
)
5897 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
5899 switch (fp
->f_type
) {
5904 return (0 == (fp
->f_fglob
->fg_lflags
& FG_CONFINED
));
5906 /* DTYPE_KQUEUE, DTYPE_FSEVENTS, DTYPE_PSXSEM */
5913 fileproc_alloc_init(__unused
void *arg
)
5915 struct fileproc
*fp
;
5917 MALLOC_ZONE(fp
, struct fileproc
*, sizeof (*fp
), M_FILEPROC
, M_WAITOK
);
5919 bzero(fp
, sizeof (*fp
));
5925 fileproc_free(struct fileproc
*fp
)
5927 switch (FILEPROC_TYPE(fp
)) {
5929 FREE_ZONE(fp
, sizeof (*fp
), M_FILEPROC
);
5932 guarded_fileproc_free(fp
);
5935 panic("%s: corrupt fp %p flags %x", __func__
, fp
, fp
->f_flags
);