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_SuperBlob
*super_blob
= (void *)t_blob
->csb_mem_kaddr
;
2381 const CS_CodeDirectory
*cd
= findCodeDirectory(super_blob
,
2382 (const char *) super_blob
,
2383 (const char *) super_blob
+ t_blob
->csb_mem_size
);
2389 uint64_t buffer_size
= ntohl(cd
->length
);
2391 if (buffer_size
> UINT_MAX
) {
2396 error
= copyout(&buffer_size
, args
.f_out_size
, sizeof(unsigned int));
2400 if (sizeof(t_blob
->csb_cdhash
) > args
.f_hash_size
||
2401 buffer_size
> args
.f_cd_size
) {
2406 error
= copyout(t_blob
->csb_cdhash
, args
.f_cd_hash
, sizeof(t_blob
->csb_cdhash
));
2409 error
= copyout(cd
, args
.f_cd_buffer
, buffer_size
);
2417 * Set the vnode pointed to by 'fd'
2418 * and tag it as the (potentially future) backing store
2419 * for another filesystem
2421 case F_SETBACKINGSTORE
: {
2422 if (fp
->f_type
!= DTYPE_VNODE
) {
2427 vp
= (struct vnode
*)fp
->f_data
;
2429 if (vp
->v_tag
!= VT_HFS
) {
2435 if (vnode_getwithref(vp
)) {
2440 /* only proceed if you have write access */
2441 vfs_context_t ctx
= vfs_context_current();
2442 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2449 /* If arg != 0, set, otherwise unset */
2451 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
)1, 0, &context
);
2454 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
2462 * like F_GETPATH, but special semantics for
2463 * the mobile time machine handler.
2465 case F_GETPATH_MTMINFO
: {
2469 if (fp
->f_type
!= DTYPE_VNODE
) {
2473 vp
= (struct vnode
*)fp
->f_data
;
2476 pathlen
= MAXPATHLEN
;
2477 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
2478 if (pathbufp
== NULL
) {
2482 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
2483 int backingstore
= 0;
2485 /* Check for error from vn_getpath before moving on */
2486 if ((error
= vn_getpath(vp
, pathbufp
, &pathlen
)) == 0) {
2487 if (vp
->v_tag
== VT_HFS
) {
2488 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
) &backingstore
, 0, &context
);
2490 (void)vnode_put(vp
);
2493 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
2497 * If the copyout was successful, now check to ensure
2498 * that this vnode is not a BACKINGSTORE vnode. mtmd
2499 * wants the path regardless.
2506 (void)vnode_put(vp
);
2508 FREE(pathbufp
, M_TEMP
);
2512 #if DEBUG || DEVELOPMENT
2514 if (fp
->f_type
!= DTYPE_VNODE
) {
2518 vp
= (struct vnode
*)fp
->f_data
;
2527 * This is an fcntl() that we d not recognize at this level;
2528 * if this is a vnode, we send it down into the VNOP_IOCTL
2529 * for this vnode; this can include special devices, and will
2530 * effectively overload fcntl() to send ioctl()'s.
2532 if((uap
->cmd
& IOC_VOID
) && (uap
->cmd
& IOC_INOUT
)){
2537 /* Catch any now-invalid fcntl() selectors */
2539 case F_MARKDEPENDENCY
:
2546 if (fp
->f_type
!= DTYPE_VNODE
) {
2550 vp
= (struct vnode
*)fp
->f_data
;
2553 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
2554 #define STK_PARAMS 128
2555 char stkbuf
[STK_PARAMS
];
2559 * For this to work properly, we have to copy in the
2560 * ioctl() cmd argument if there is one; we must also
2561 * check that a command parameter, if present, does
2562 * not exceed the maximum command length dictated by
2563 * the number of bits we have available in the command
2564 * to represent a structure length. Finally, we have
2565 * to copy the results back out, if it is that type of
2568 size
= IOCPARM_LEN(uap
->cmd
);
2569 if (size
> IOCPARM_MAX
) {
2570 (void)vnode_put(vp
);
2576 if (size
> sizeof (stkbuf
)) {
2577 if ((memp
= (caddr_t
)kalloc(size
)) == 0) {
2578 (void)vnode_put(vp
);
2587 if (uap
->cmd
& IOC_IN
) {
2590 error
= copyin(argp
, data
, size
);
2592 (void)vnode_put(vp
);
2598 /* Bzero the section beyond that which was needed */
2599 if (size
<= sizeof(stkbuf
)) {
2600 bzero ( (((uint8_t*)data
) + size
), (sizeof(stkbuf
) - size
));
2605 *(user_addr_t
*)data
= argp
;
2607 *(uint32_t *)data
= (uint32_t)argp
;
2610 } else if ((uap
->cmd
& IOC_OUT
) && size
) {
2612 * Zero the buffer so the user always
2613 * gets back something deterministic.
2616 } else if (uap
->cmd
& IOC_VOID
) {
2618 *(user_addr_t
*)data
= argp
;
2620 *(uint32_t *)data
= (uint32_t)argp
;
2624 error
= VNOP_IOCTL(vp
, uap
->cmd
, CAST_DOWN(caddr_t
, data
), 0, &context
);
2626 (void)vnode_put(vp
);
2628 /* Copy any output data to user */
2629 if (error
== 0 && (uap
->cmd
& IOC_OUT
) && size
)
2630 error
= copyout(data
, argp
, size
);
2638 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
2639 fp_drop(p
, fd
, fp
, 0);
2642 fp_drop(p
, fd
, fp
, 1);
2651 * Description: Common code for dup, dup2, and fcntl(F_DUPFD).
2653 * Parameters: p Process performing the dup
2655 * new The fd to dup it to
2656 * fd_flags Flags to augment the new fd
2657 * retval Pointer to the call return area
2659 * Returns: 0 Success
2664 * *retval (modified) The new descriptor
2666 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
2669 * Notes: This function may drop and reacquire this lock; it is unsafe
2670 * for a caller to assume that other state protected by the lock
2671 * has not been subsequently changed out from under it.
2675 struct filedesc
*fdp
, int old
, int new, int fd_flags
, int32_t *retval
)
2677 struct fileproc
*nfp
;
2678 struct fileproc
*ofp
;
2684 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2686 if ((ofp
= fdp
->fd_ofiles
[old
]) == NULL
||
2687 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
)) {
2694 error
= mac_file_check_dup(proc_ucred(p
), ofp
->f_fglob
, new);
2704 nfp
= fileproc_alloc_init(NULL
);
2714 nfp
->f_fglob
= ofp
->f_fglob
;
2717 if (fdp
->fd_ofiles
[new] != 0)
2718 panic("finishdup: overwriting fd_ofiles with new %d", new);
2719 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0)
2720 panic("finishdup: unreserved fileflags with new %d", new);
2723 if (new > fdp
->fd_lastfile
)
2724 fdp
->fd_lastfile
= new;
2725 *fdflags(p
, new) |= fd_flags
;
2726 procfdtbl_releasefd(p
, new, nfp
);
2735 * Description: The implementation of the close(2) system call
2737 * Parameters: p Process in whose per process file table
2738 * the close is to occur
2739 * uap->fd fd to be closed
2742 * Returns: 0 Success
2743 * fp_lookup:EBADF Bad file descriptor
2744 * fp_guard_exception:??? Guarded file descriptor
2745 * close_internal:EBADF
2746 * close_internal:??? Anything returnable by a per-fileops
2750 close(proc_t p
, struct close_args
*uap
, int32_t *retval
)
2752 __pthread_testcancel(1);
2753 return(close_nocancel(p
, (struct close_nocancel_args
*)uap
, retval
));
2758 close_nocancel(proc_t p
, struct close_nocancel_args
*uap
, __unused
int32_t *retval
)
2760 struct fileproc
*fp
;
2764 AUDIT_SYSCLOSE(p
, fd
);
2768 if ( (error
= fp_lookup(p
,fd
,&fp
, 1)) ) {
2773 if (FP_ISGUARDED(fp
, GUARD_CLOSE
)) {
2774 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_CLOSE
);
2775 (void) fp_drop(p
, fd
, fp
, 1);
2780 error
= close_internal_locked(p
, fd
, fp
, 0);
2789 * close_internal_locked
2791 * Close a file descriptor.
2793 * Parameters: p Process in whose per process file table
2794 * the close is to occur
2795 * fd fd to be closed
2796 * fp fileproc associated with the fd
2798 * Returns: 0 Success
2799 * EBADF fd already in close wait state
2800 * closef_locked:??? Anything returnable by a per-fileops
2803 * Locks: Assumes proc_fdlock for process is held by the caller and returns
2806 * Notes: This function may drop and reacquire this lock; it is unsafe
2807 * for a caller to assume that other state protected by the lock
2808 * has not been subsequently changed out from under it.
2811 close_internal_locked(proc_t p
, int fd
, struct fileproc
*fp
, int flags
)
2813 struct filedesc
*fdp
= p
->p_fd
;
2815 int resvfd
= flags
& FD_DUP2RESV
;
2819 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2822 /* Keep people from using the filedesc while we are closing it */
2823 procfdtbl_markclosefd(p
, fd
);
2826 if ((fp
->f_flags
& FP_CLOSING
) == FP_CLOSING
) {
2827 panic("close_internal_locked: being called on already closing fd");
2832 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
2833 panic("close_internal: unreserved fileflags with fd %d", fd
);
2836 fp
->f_flags
|= FP_CLOSING
;
2838 if ( (fp
->f_flags
& FP_AIOISSUED
) || kauth_authorize_fileop_has_listeners() ) {
2842 if ( (fp
->f_type
== DTYPE_VNODE
) && kauth_authorize_fileop_has_listeners() ) {
2844 * call out to allow 3rd party notification of close.
2845 * Ignore result of kauth_authorize_fileop call.
2847 if (vnode_getwithref((vnode_t
)fp
->f_data
) == 0) {
2848 u_int fileop_flags
= 0;
2849 if ((fp
->f_flags
& FP_WRITTEN
) != 0)
2850 fileop_flags
|= KAUTH_FILEOP_CLOSE_MODIFIED
;
2851 kauth_authorize_fileop(fp
->f_fglob
->fg_cred
, KAUTH_FILEOP_CLOSE
,
2852 (uintptr_t)fp
->f_data
, (uintptr_t)fileop_flags
);
2853 vnode_put((vnode_t
)fp
->f_data
);
2856 if (fp
->f_flags
& FP_AIOISSUED
)
2858 * cancel all async IO requests that can be cancelled.
2860 _aio_close( p
, fd
);
2865 if (fd
< fdp
->fd_knlistsize
)
2866 knote_fdclose(p
, fd
);
2868 if (fp
->f_flags
& FP_WAITEVENT
)
2869 (void)waitevent_close(p
, fp
);
2871 fileproc_drain(p
, fp
);
2876 procfdtbl_reservefd(p
, fd
);
2879 if (ENTR_SHOULDTRACE
&& fp
->f_type
== DTYPE_SOCKET
)
2880 KERNEL_ENERGYTRACE(kEnTrActKernSocket
, DBG_FUNC_END
,
2881 fd
, 0, (int64_t)VM_KERNEL_ADDRPERM(fp
->f_data
));
2883 error
= closef_locked(fp
, fp
->f_fglob
, p
);
2884 if ((fp
->f_flags
& FP_WAITCLOSE
) == FP_WAITCLOSE
)
2885 wakeup(&fp
->f_flags
);
2886 fp
->f_flags
&= ~(FP_WAITCLOSE
| FP_CLOSING
);
2896 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
2897 panic("close with reserved fd returns with freed fd:%d: proc: %p", fd
, p
);
2908 * Description: Return status information about a file descriptor.
2910 * Parameters: p The process doing the fstat
2912 * ub The user stat buffer
2913 * xsecurity The user extended security
2914 * buffer, or 0 if none
2915 * xsecurity_size The size of xsecurity, or 0
2917 * isstat64 Flag to indicate 64 bit version
2918 * for inode size, etc.
2920 * Returns: 0 Success
2923 * fp_lookup:EBADF Bad file descriptor
2924 * vnode_getwithref:???
2926 * vnode_getwithref:???
2933 * Notes: Internal implementation for all other fstat() related
2936 * XXX switch on node type is bogus; need a stat in struct
2937 * XXX fileops instead.
2940 fstat1(proc_t p
, int fd
, user_addr_t ub
, user_addr_t xsecurity
, user_addr_t xsecurity_size
, int isstat64
)
2942 struct fileproc
*fp
;
2948 struct user64_stat user64_sb
;
2949 struct user32_stat user32_sb
;
2950 struct user64_stat64 user64_sb64
;
2951 struct user32_stat64 user32_sb64
;
2956 kauth_filesec_t fsec
;
2957 user_size_t xsecurity_bufsize
;
2958 vfs_context_t ctx
= vfs_context_current();
2964 if ((error
= fp_lookup(p
, fd
, &fp
, 0)) != 0) {
2969 fsec
= KAUTH_FILESEC_NONE
;
2971 sbptr
= (void *)&source
;
2976 if ((error
= vnode_getwithref((vnode_t
)data
)) == 0) {
2978 * If the caller has the file open, and is not
2979 * requesting extended security information, we are
2980 * going to let them get the basic stat information.
2982 if (xsecurity
== USER_ADDR_NULL
) {
2983 error
= vn_stat_noauth((vnode_t
)data
, sbptr
, NULL
, isstat64
, ctx
);
2985 error
= vn_stat((vnode_t
)data
, sbptr
, &fsec
, isstat64
, ctx
);
2988 AUDIT_ARG(vnpath
, (struct vnode
*)data
, ARG_VNODE1
);
2989 (void)vnode_put((vnode_t
)data
);
2995 error
= soo_stat((struct socket
*)data
, sbptr
, isstat64
);
2997 #endif /* SOCKETS */
3000 error
= pipe_stat((void *)data
, sbptr
, isstat64
);
3004 error
= pshm_stat((void *)data
, sbptr
, isstat64
);
3008 error
= kqueue_stat((void *)data
, sbptr
, isstat64
, p
);
3018 if (isstat64
!= 0) {
3019 source
.sb64
.st_lspare
= 0;
3020 source
.sb64
.st_qspare
[0] = 0LL;
3021 source
.sb64
.st_qspare
[1] = 0LL;
3023 if (IS_64BIT_PROCESS(current_proc())) {
3024 munge_user64_stat64(&source
.sb64
, &dest
.user64_sb64
);
3025 my_size
= sizeof(dest
.user64_sb64
);
3026 sbp
= (caddr_t
)&dest
.user64_sb64
;
3028 munge_user32_stat64(&source
.sb64
, &dest
.user32_sb64
);
3029 my_size
= sizeof(dest
.user32_sb64
);
3030 sbp
= (caddr_t
)&dest
.user32_sb64
;
3033 source
.sb
.st_lspare
= 0;
3034 source
.sb
.st_qspare
[0] = 0LL;
3035 source
.sb
.st_qspare
[1] = 0LL;
3036 if (IS_64BIT_PROCESS(current_proc())) {
3037 munge_user64_stat(&source
.sb
, &dest
.user64_sb
);
3038 my_size
= sizeof(dest
.user64_sb
);
3039 sbp
= (caddr_t
)&dest
.user64_sb
;
3041 munge_user32_stat(&source
.sb
, &dest
.user32_sb
);
3042 my_size
= sizeof(dest
.user32_sb
);
3043 sbp
= (caddr_t
)&dest
.user32_sb
;
3047 error
= copyout(sbp
, ub
, my_size
);
3050 /* caller wants extended security information? */
3051 if (xsecurity
!= USER_ADDR_NULL
) {
3053 /* did we get any? */
3054 if (fsec
== KAUTH_FILESEC_NONE
) {
3055 if (susize(xsecurity_size
, 0) != 0) {
3060 /* find the user buffer size */
3061 xsecurity_bufsize
= fusize(xsecurity_size
);
3063 /* copy out the actual data size */
3064 if (susize(xsecurity_size
, KAUTH_FILESEC_COPYSIZE(fsec
)) != 0) {
3069 /* if the caller supplied enough room, copy out to it */
3070 if (xsecurity_bufsize
>= KAUTH_FILESEC_COPYSIZE(fsec
))
3071 error
= copyout(fsec
, xsecurity
, KAUTH_FILESEC_COPYSIZE(fsec
));
3075 fp_drop(p
, fd
, fp
, 0);
3077 kauth_filesec_free(fsec
);
3085 * Description: Extended version of fstat supporting returning extended
3086 * security information
3088 * Parameters: p The process doing the fstat
3089 * uap->fd The fd to stat
3090 * uap->ub The user stat buffer
3091 * uap->xsecurity The user extended security
3092 * buffer, or 0 if none
3093 * uap->xsecurity_size The size of xsecurity, or 0
3095 * Returns: 0 Success
3096 * !0 Errno (see fstat1)
3099 fstat_extended(proc_t p
, struct fstat_extended_args
*uap
, __unused
int32_t *retval
)
3101 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 0));
3108 * Description: Get file status for the file associated with fd
3110 * Parameters: p The process doing the fstat
3111 * uap->fd The fd to stat
3112 * uap->ub The user stat buffer
3114 * Returns: 0 Success
3115 * !0 Errno (see fstat1)
3118 fstat(proc_t p
, register struct fstat_args
*uap
, __unused
int32_t *retval
)
3120 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 0));
3127 * Description: Extended version of fstat64 supporting returning extended
3128 * security information
3130 * Parameters: p The process doing the fstat
3131 * uap->fd The fd to stat
3132 * uap->ub The user stat buffer
3133 * uap->xsecurity The user extended security
3134 * buffer, or 0 if none
3135 * uap->xsecurity_size The size of xsecurity, or 0
3137 * Returns: 0 Success
3138 * !0 Errno (see fstat1)
3141 fstat64_extended(proc_t p
, struct fstat64_extended_args
*uap
, __unused
int32_t *retval
)
3143 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 1));
3150 * Description: Get 64 bit version of the file status for the file associated
3153 * Parameters: p The process doing the fstat
3154 * uap->fd The fd to stat
3155 * uap->ub The user stat buffer
3157 * Returns: 0 Success
3158 * !0 Errno (see fstat1)
3161 fstat64(proc_t p
, register struct fstat64_args
*uap
, __unused
int32_t *retval
)
3163 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 1));
3170 * Description: Return pathconf information about a file descriptor.
3172 * Parameters: p Process making the request
3173 * uap->fd fd to get information about
3174 * uap->name Name of information desired
3175 * retval Pointer to the call return area
3177 * Returns: 0 Success
3179 * fp_lookup:EBADF Bad file descriptor
3180 * vnode_getwithref:???
3184 * *retval (modified) Returned information (numeric)
3187 fpathconf(proc_t p
, struct fpathconf_args
*uap
, int32_t *retval
)
3190 struct fileproc
*fp
;
3197 AUDIT_ARG(fd
, uap
->fd
);
3198 if ( (error
= fp_lookup(p
, fd
, &fp
, 0)) )
3206 if (uap
->name
!= _PC_PIPE_BUF
) {
3215 if (uap
->name
!= _PC_PIPE_BUF
) {
3224 vp
= (struct vnode
*)data
;
3226 if ( (error
= vnode_getwithref(vp
)) == 0) {
3227 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
3229 error
= vn_pathconf(vp
, uap
->name
, retval
, vfs_context_current());
3231 (void)vnode_put(vp
);
3242 fp_drop(p
, fd
, fp
, 0);
3247 * Statistics counter for the number of times a process calling fdalloc()
3248 * has resulted in an expansion of the per process open file table.
3250 * XXX This would likely be of more use if it were per process
3258 * Description: Allocate a file descriptor for the process.
3260 * Parameters: p Process to allocate the fd in
3261 * want The fd we would prefer to get
3262 * result Pointer to fd we got
3264 * Returns: 0 Success
3269 * *result (modified) The fd which was allocated
3272 fdalloc(proc_t p
, int want
, int *result
)
3274 struct filedesc
*fdp
= p
->p_fd
;
3276 int lim
, last
, numfiles
, oldnfiles
;
3277 struct fileproc
**newofiles
, **ofiles
;
3278 char *newofileflags
;
3281 * Search for a free descriptor starting at the higher
3282 * of want or fd_freefile. If that fails, consider
3283 * expanding the ofile array.
3286 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
3289 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
3291 last
= min(fdp
->fd_nfiles
, lim
);
3292 if ((i
= want
) < fdp
->fd_freefile
)
3293 i
= fdp
->fd_freefile
;
3294 for (; i
< last
; i
++) {
3295 if (fdp
->fd_ofiles
[i
] == NULL
&& !(fdp
->fd_ofileflags
[i
] & UF_RESERVED
)) {
3296 procfdtbl_reservefd(p
, i
);
3297 if (i
> fdp
->fd_lastfile
)
3298 fdp
->fd_lastfile
= i
;
3299 if (want
<= fdp
->fd_freefile
)
3300 fdp
->fd_freefile
= i
;
3307 * No space in current array. Expand?
3309 if (fdp
->fd_nfiles
>= lim
)
3311 if (fdp
->fd_nfiles
< NDEXTENT
)
3312 numfiles
= NDEXTENT
;
3314 numfiles
= 2 * fdp
->fd_nfiles
;
3319 MALLOC_ZONE(newofiles
, struct fileproc
**,
3320 numfiles
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
3322 if (newofiles
== NULL
) {
3325 if (fdp
->fd_nfiles
>= numfiles
) {
3326 FREE_ZONE(newofiles
, numfiles
* OFILESIZE
, M_OFILETABL
);
3329 newofileflags
= (char *) &newofiles
[numfiles
];
3331 * Copy the existing ofile and ofileflags arrays
3332 * and zero the new portion of each array.
3334 oldnfiles
= fdp
->fd_nfiles
;
3335 (void) memcpy(newofiles
, fdp
->fd_ofiles
,
3336 oldnfiles
* sizeof(*fdp
->fd_ofiles
));
3337 (void) memset(&newofiles
[oldnfiles
], 0,
3338 (numfiles
- oldnfiles
) * sizeof(*fdp
->fd_ofiles
));
3340 (void) memcpy(newofileflags
, fdp
->fd_ofileflags
,
3341 oldnfiles
* sizeof(*fdp
->fd_ofileflags
));
3342 (void) memset(&newofileflags
[oldnfiles
], 0,
3343 (numfiles
- oldnfiles
) *
3344 sizeof(*fdp
->fd_ofileflags
));
3345 ofiles
= fdp
->fd_ofiles
;
3346 fdp
->fd_ofiles
= newofiles
;
3347 fdp
->fd_ofileflags
= newofileflags
;
3348 fdp
->fd_nfiles
= numfiles
;
3349 FREE_ZONE(ofiles
, oldnfiles
* OFILESIZE
, M_OFILETABL
);
3358 * Description: Check to see whether n user file descriptors are available
3361 * Parameters: p Process to check in
3362 * n The number of fd's desired
3367 * Locks: Assumes proc_fdlock for process is held by the caller
3369 * Notes: The answer only remains valid so long as the proc_fdlock is
3370 * held by the caller.
3373 fdavail(proc_t p
, int n
)
3375 struct filedesc
*fdp
= p
->p_fd
;
3376 struct fileproc
**fpp
;
3380 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
3381 if ((i
= lim
- fdp
->fd_nfiles
) > 0 && (n
-= i
) <= 0)
3383 fpp
= &fdp
->fd_ofiles
[fdp
->fd_freefile
];
3384 flags
= &fdp
->fd_ofileflags
[fdp
->fd_freefile
];
3385 for (i
= fdp
->fd_nfiles
- fdp
->fd_freefile
; --i
>= 0; fpp
++, flags
++)
3386 if (*fpp
== NULL
&& !(*flags
& UF_RESERVED
) && --n
<= 0)
3395 * Description: Legacy KPI wrapper function for _fdrelse
3397 * Parameters: p Process in which fd lives
3402 * Locks: Assumes proc_fdlock for process is held by the caller
3405 fdrelse(proc_t p
, int fd
)
3414 * Description: Get the fileproc pointer for the given fd from the per process
3415 * open file table without taking an explicit reference on it.
3417 * Parameters: p Process containing fd
3418 * fd fd to obtain fileproc for
3419 * resultfp Pointer to pointer return area
3421 * Returns: 0 Success
3425 * *resultfp (modified) Pointer to fileproc pointer
3427 * Locks: Assumes proc_fdlock for process is held by the caller
3429 * Notes: Because there is no reference explicitly taken, the returned
3430 * fileproc pointer is only valid so long as the proc_fdlock
3431 * remains held by the caller.
3434 fdgetf_noref(proc_t p
, int fd
, struct fileproc
**resultfp
)
3436 struct filedesc
*fdp
= p
->p_fd
;
3437 struct fileproc
*fp
;
3439 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3440 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3441 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3453 * Description: Get fileproc and vnode pointer for a given fd from the per
3454 * process open file table of the specified process, and if
3455 * successful, increment the f_iocount
3457 * Parameters: p Process in which fd lives
3458 * fd fd to get information for
3459 * resultfp Pointer to result fileproc
3460 * pointer area, or 0 if none
3461 * resultvp Pointer to result vnode pointer
3462 * area, or 0 if none
3464 * Returns: 0 Success
3465 * EBADF Bad file descriptor
3466 * ENOTSUP fd does not refer to a vnode
3469 * *resultfp (modified) Fileproc pointer
3470 * *resultvp (modified) vnode pointer
3472 * Notes: The resultfp and resultvp fields are optional, and may be
3473 * independently specified as NULL to skip returning information
3475 * Locks: Internally takes and releases proc_fdlock
3478 fp_getfvp(proc_t p
, int fd
, struct fileproc
**resultfp
, struct vnode
**resultvp
)
3480 struct filedesc
*fdp
= p
->p_fd
;
3481 struct fileproc
*fp
;
3483 proc_fdlock_spin(p
);
3484 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3485 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3486 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3490 if (fp
->f_type
!= DTYPE_VNODE
) {
3499 *resultvp
= (struct vnode
*)fp
->f_data
;
3509 * Description: Get fileproc, vnode pointer, and vid for a given fd from the
3510 * per process open file table of the specified process, and if
3511 * successful, increment the f_iocount
3513 * Parameters: p Process in which fd lives
3514 * fd fd to get information for
3515 * resultfp Pointer to result fileproc
3516 * pointer area, or 0 if none
3517 * resultvp Pointer to result vnode pointer
3518 * area, or 0 if none
3519 * vidp Pointer to resuld vid area
3521 * Returns: 0 Success
3522 * EBADF Bad file descriptor
3523 * ENOTSUP fd does not refer to a vnode
3526 * *resultfp (modified) Fileproc pointer
3527 * *resultvp (modified) vnode pointer
3530 * Notes: The resultfp and resultvp fields are optional, and may be
3531 * independently specified as NULL to skip returning information
3533 * Locks: Internally takes and releases proc_fdlock
3536 fp_getfvpandvid(proc_t p
, int fd
, struct fileproc
**resultfp
,
3537 struct vnode
**resultvp
, uint32_t *vidp
)
3539 struct filedesc
*fdp
= p
->p_fd
;
3540 struct fileproc
*fp
;
3542 proc_fdlock_spin(p
);
3543 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3544 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3545 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3549 if (fp
->f_type
!= DTYPE_VNODE
) {
3558 *resultvp
= (struct vnode
*)fp
->f_data
;
3560 *vidp
= (uint32_t)vnode_vid((struct vnode
*)fp
->f_data
);
3570 * Description: Get fileproc and socket pointer for a given fd from the
3571 * per process open file table of the specified process, and if
3572 * successful, increment the f_iocount
3574 * Parameters: p Process in which fd lives
3575 * fd fd to get information for
3576 * resultfp Pointer to result fileproc
3577 * pointer area, or 0 if none
3578 * results Pointer to result socket
3579 * pointer area, or 0 if none
3581 * Returns: EBADF The file descriptor is invalid
3582 * EOPNOTSUPP The file descriptor is not a socket
3586 * *resultfp (modified) Fileproc pointer
3587 * *results (modified) socket pointer
3589 * Notes: EOPNOTSUPP should probably be ENOTSOCK; this function is only
3590 * ever called from accept1().
3593 fp_getfsock(proc_t p
, int fd
, struct fileproc
**resultfp
,
3594 struct socket
**results
)
3596 struct filedesc
*fdp
= p
->p_fd
;
3597 struct fileproc
*fp
;
3599 proc_fdlock_spin(p
);
3600 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3601 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3602 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3606 if (fp
->f_type
!= DTYPE_SOCKET
) {
3615 *results
= (struct socket
*)fp
->f_data
;
3625 * Description: Get fileproc and kqueue pointer for a given fd from the
3626 * per process open file table of the specified process, and if
3627 * successful, increment the f_iocount
3629 * Parameters: p Process in which fd lives
3630 * fd fd to get information for
3631 * resultfp Pointer to result fileproc
3632 * pointer area, or 0 if none
3633 * resultkq Pointer to result kqueue
3634 * pointer area, or 0 if none
3636 * Returns: EBADF The file descriptor is invalid
3637 * EBADF The file descriptor is not a socket
3641 * *resultfp (modified) Fileproc pointer
3642 * *resultkq (modified) kqueue pointer
3644 * Notes: The second EBADF should probably be something else to make
3645 * the error condition distinct.
3648 fp_getfkq(proc_t p
, int fd
, struct fileproc
**resultfp
,
3649 struct kqueue
**resultkq
)
3651 struct filedesc
*fdp
= p
->p_fd
;
3652 struct fileproc
*fp
;
3654 proc_fdlock_spin(p
);
3655 if ( fd
< 0 || fd
>= fdp
->fd_nfiles
||
3656 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3657 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3661 if (fp
->f_type
!= DTYPE_KQUEUE
) {
3670 *resultkq
= (struct kqueue
*)fp
->f_data
;
3680 * Description: Get fileproc and POSIX shared memory pointer for a given fd
3681 * from the per process open file table of the specified process
3682 * and if successful, increment the f_iocount
3684 * Parameters: p Process in which fd lives
3685 * fd fd to get information for
3686 * resultfp Pointer to result fileproc
3687 * pointer area, or 0 if none
3688 * resultpshm Pointer to result POSIX
3689 * shared memory pointer
3690 * pointer area, or 0 if none
3692 * Returns: EBADF The file descriptor is invalid
3693 * EBADF The file descriptor is not a POSIX
3694 * shared memory area
3698 * *resultfp (modified) Fileproc pointer
3699 * *resultpshm (modified) POSIX shared memory pointer
3701 * Notes: The second EBADF should probably be something else to make
3702 * the error condition distinct.
3705 fp_getfpshm(proc_t p
, int fd
, struct fileproc
**resultfp
,
3706 struct pshmnode
**resultpshm
)
3708 struct filedesc
*fdp
= p
->p_fd
;
3709 struct fileproc
*fp
;
3711 proc_fdlock_spin(p
);
3712 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3713 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3714 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3718 if (fp
->f_type
!= DTYPE_PSXSHM
) {
3728 *resultpshm
= (struct pshmnode
*)fp
->f_data
;
3738 * Description: Get fileproc and POSIX semaphore pointer for a given fd from
3739 * the per process open file table of the specified process
3740 * and if successful, increment the f_iocount
3742 * Parameters: p Process in which fd lives
3743 * fd fd to get information for
3744 * resultfp Pointer to result fileproc
3745 * pointer area, or 0 if none
3746 * resultpsem Pointer to result POSIX
3747 * semaphore pointer area, or
3750 * Returns: EBADF The file descriptor is invalid
3751 * EBADF The file descriptor is not a POSIX
3756 * *resultfp (modified) Fileproc pointer
3757 * *resultpsem (modified) POSIX semaphore pointer
3759 * Notes: The second EBADF should probably be something else to make
3760 * the error condition distinct.
3762 * In order to support unnamed POSIX semaphores, the named
3763 * POSIX semaphores will have to move out of the per-process
3764 * open filetable, and into a global table that is shared with
3765 * unnamed POSIX semaphores, since unnamed POSIX semaphores
3766 * are typically used by declaring instances in shared memory,
3767 * and there's no other way to do this without changing the
3768 * underlying type, which would introduce binary compatibility
3772 fp_getfpsem(proc_t p
, int fd
, struct fileproc
**resultfp
,
3773 struct psemnode
**resultpsem
)
3775 struct filedesc
*fdp
= p
->p_fd
;
3776 struct fileproc
*fp
;
3778 proc_fdlock_spin(p
);
3779 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3780 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3781 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3785 if (fp
->f_type
!= DTYPE_PSXSEM
) {
3794 *resultpsem
= (struct psemnode
*)fp
->f_data
;
3804 * Description: Get fileproc and pipe pointer for a given fd from the
3805 * per process open file table of the specified process
3806 * and if successful, increment the f_iocount
3808 * Parameters: p Process in which fd lives
3809 * fd fd to get information for
3810 * resultfp Pointer to result fileproc
3811 * pointer area, or 0 if none
3812 * resultpipe Pointer to result pipe
3813 * pointer area, or 0 if none
3815 * Returns: EBADF The file descriptor is invalid
3816 * EBADF The file descriptor is not a socket
3820 * *resultfp (modified) Fileproc pointer
3821 * *resultpipe (modified) pipe pointer
3823 * Notes: The second EBADF should probably be something else to make
3824 * the error condition distinct.
3827 fp_getfpipe(proc_t p
, int fd
, struct fileproc
**resultfp
,
3828 struct pipe
**resultpipe
)
3830 struct filedesc
*fdp
= p
->p_fd
;
3831 struct fileproc
*fp
;
3833 proc_fdlock_spin(p
);
3834 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3835 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3836 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3840 if (fp
->f_type
!= DTYPE_PIPE
) {
3849 *resultpipe
= (struct pipe
*)fp
->f_data
;
3858 * Description: Get fileproc pointer for a given fd from the per process
3859 * open file table of the specified process and if successful,
3860 * increment the f_iocount
3862 * Parameters: p Process in which fd lives
3863 * fd fd to get information for
3864 * resultfp Pointer to result fileproc
3865 * pointer area, or 0 if none
3866 * locked !0 if the caller holds the
3867 * proc_fdlock, 0 otherwise
3869 * Returns: 0 Success
3870 * EBADF Bad file descriptor
3873 * *resultfp (modified) Fileproc pointer
3875 * Locks: If the argument 'locked' is non-zero, then the caller is
3876 * expected to have taken and held the proc_fdlock; if it is
3877 * zero, than this routine internally takes and drops this lock.
3880 fp_lookup(proc_t p
, int fd
, struct fileproc
**resultfp
, int locked
)
3882 struct filedesc
*fdp
= p
->p_fd
;
3883 struct fileproc
*fp
;
3886 proc_fdlock_spin(p
);
3887 if (fd
< 0 || fdp
== NULL
|| fd
>= fdp
->fd_nfiles
||
3888 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3889 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3908 * Description: Swap the fileproc pointer for a given fd with a new
3909 * fileproc pointer in the per-process open file table of
3910 * the specified process. The fdlock must be held at entry.
3912 * Parameters: p Process containing the fd
3913 * fd The fd of interest
3914 * nfp Pointer to the newfp
3916 * Returns: 0 Success
3917 * EBADF Bad file descriptor
3919 * EKEEPLOOKING f_iocount changed while lock was dropped.
3922 fp_tryswap(proc_t p
, int fd
, struct fileproc
*nfp
)
3924 struct fileproc
*fp
;
3927 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
3929 if (0 != (error
= fp_lookup(p
, fd
, &fp
, 1)))
3932 * At this point, our caller (change_guardedfd_np) has
3933 * one f_iocount reference, and we just took another
3934 * one to begin the replacement.
3936 if (fp
->f_iocount
< 2) {
3937 panic("f_iocount too small %d", fp
->f_iocount
);
3938 } else if (2 == fp
->f_iocount
) {
3940 /* Copy the contents of *fp, preserving the "type" of *nfp */
3942 nfp
->f_flags
= (nfp
->f_flags
& FP_TYPEMASK
) |
3943 (fp
->f_flags
& ~FP_TYPEMASK
);
3944 nfp
->f_iocount
= fp
->f_iocount
;
3945 nfp
->f_fglob
= fp
->f_fglob
;
3946 nfp
->f_wset
= fp
->f_wset
;
3948 p
->p_fd
->fd_ofiles
[fd
] = nfp
;
3949 (void) fp_drop(p
, fd
, nfp
, 1);
3952 * Wait for all other active references to evaporate.
3954 p
->p_fpdrainwait
= 1;
3955 error
= msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
,
3956 PRIBIO
| PCATCH
, "tryswap fpdrain", NULL
);
3959 * Return an "internal" errno to trigger a full
3960 * reevaluation of the change-guard attempt.
3962 error
= EKEEPLOOKING
;
3963 printf("%s: lookup collision fd %d\n", __func__
, fd
);
3965 (void) fp_drop(p
, fd
, fp
, 1);
3974 * Description: Set the FP_WRITTEN flag on the fileproc and drop the I/O
3975 * reference previously taken by calling fp_lookup et. al.
3977 * Parameters: p Process in which the fd lives
3978 * fd fd associated with the fileproc
3979 * fp fileproc on which to set the
3980 * flag and drop the reference
3982 * Returns: 0 Success
3983 * fp_drop:EBADF Bad file descriptor
3985 * Locks: This function internally takes and drops the proc_fdlock for
3986 * the supplied process
3988 * Notes: The fileproc must correspond to the fd in the supplied proc
3991 fp_drop_written(proc_t p
, int fd
, struct fileproc
*fp
)
3995 proc_fdlock_spin(p
);
3997 fp
->f_flags
|= FP_WRITTEN
;
3999 error
= fp_drop(p
, fd
, fp
, 1);
4010 * Description: Set the FP_WAITEVENT flag on the fileproc and drop the I/O
4011 * reference previously taken by calling fp_lookup et. al.
4013 * Parameters: p Process in which the fd lives
4014 * fd fd associated with the fileproc
4015 * fp fileproc on which to set the
4016 * flag and drop the reference
4018 * Returns: 0 Success
4019 * fp_drop:EBADF Bad file descriptor
4021 * Locks: This function internally takes and drops the proc_fdlock for
4022 * the supplied process
4024 * Notes: The fileproc must correspond to the fd in the supplied proc
4027 fp_drop_event(proc_t p
, int fd
, struct fileproc
*fp
)
4031 proc_fdlock_spin(p
);
4033 fp
->f_flags
|= FP_WAITEVENT
;
4035 error
= fp_drop(p
, fd
, fp
, 1);
4046 * Description: Drop the I/O reference previously taken by calling fp_lookup
4049 * Parameters: p Process in which the fd lives
4050 * fd fd associated with the fileproc
4051 * fp fileproc on which to set the
4052 * flag and drop the reference
4053 * locked flag to internally take and
4054 * drop proc_fdlock if it is not
4055 * already held by the caller
4057 * Returns: 0 Success
4058 * EBADF Bad file descriptor
4060 * Locks: This function internally takes and drops the proc_fdlock for
4061 * the supplied process if 'locked' is non-zero, and assumes that
4062 * the caller already holds this lock if 'locked' is non-zero.
4064 * Notes: The fileproc must correspond to the fd in the supplied proc
4067 fp_drop(proc_t p
, int fd
, struct fileproc
*fp
, int locked
)
4069 struct filedesc
*fdp
= p
->p_fd
;
4073 proc_fdlock_spin(p
);
4074 if ((fp
== FILEPROC_NULL
) && (fd
< 0 || fd
>= fdp
->fd_nfiles
||
4075 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
4076 ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
4077 !(fdp
->fd_ofileflags
[fd
] & UF_CLOSING
)))) {
4084 if (fp
->f_iocount
== 0) {
4085 if (fp
->f_flags
& FP_SELCONFLICT
)
4086 fp
->f_flags
&= ~FP_SELCONFLICT
;
4088 if (p
->p_fpdrainwait
) {
4089 p
->p_fpdrainwait
= 0;
4096 wakeup(&p
->p_fpdrainwait
);
4105 * Description: Given an fd, look it up in the current process's per process
4106 * open file table, and return its internal vnode pointer.
4108 * Parameters: fd fd to obtain vnode from
4109 * vpp pointer to vnode return area
4111 * Returns: 0 Success
4112 * EINVAL The fd does not refer to a
4113 * vnode fileproc entry
4114 * fp_lookup:EBADF Bad file descriptor
4117 * *vpp (modified) Returned vnode pointer
4119 * Locks: This function internally takes and drops the proc_fdlock for
4120 * the current process
4122 * Notes: If successful, this function increments the f_iocount on the
4123 * fd's corresponding fileproc.
4125 * The fileproc referenced is not returned; because of this, care
4126 * must be taken to not drop the last reference (e.g. by closing
4127 * the file). This is inherently unsafe, since the reference may
4128 * not be recoverable from the vnode, if there is a subsequent
4129 * close that destroys the associate fileproc. The caller should
4130 * therefore retain their own reference on the fileproc so that
4131 * the f_iocount can be dropped subsequently. Failure to do this
4132 * can result in the returned pointer immediately becoming invalid
4133 * following the call.
4135 * Use of this function is discouraged.
4138 file_vnode(int fd
, struct vnode
**vpp
)
4140 proc_t p
= current_proc();
4141 struct fileproc
*fp
;
4144 proc_fdlock_spin(p
);
4145 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4149 if (fp
->f_type
!= DTYPE_VNODE
) {
4150 fp_drop(p
, fd
, fp
,1);
4155 *vpp
= (struct vnode
*)fp
->f_data
;
4163 * file_vnode_withvid
4165 * Description: Given an fd, look it up in the current process's per process
4166 * open file table, and return its internal vnode pointer.
4168 * Parameters: fd fd to obtain vnode from
4169 * vpp pointer to vnode return area
4170 * vidp pointer to vid of the returned vnode
4172 * Returns: 0 Success
4173 * EINVAL The fd does not refer to a
4174 * vnode fileproc entry
4175 * fp_lookup:EBADF Bad file descriptor
4178 * *vpp (modified) Returned vnode pointer
4180 * Locks: This function internally takes and drops the proc_fdlock for
4181 * the current process
4183 * Notes: If successful, this function increments the f_iocount on the
4184 * fd's corresponding fileproc.
4186 * The fileproc referenced is not returned; because of this, care
4187 * must be taken to not drop the last reference (e.g. by closing
4188 * the file). This is inherently unsafe, since the reference may
4189 * not be recoverable from the vnode, if there is a subsequent
4190 * close that destroys the associate fileproc. The caller should
4191 * therefore retain their own reference on the fileproc so that
4192 * the f_iocount can be dropped subsequently. Failure to do this
4193 * can result in the returned pointer immediately becoming invalid
4194 * following the call.
4196 * Use of this function is discouraged.
4199 file_vnode_withvid(int fd
, struct vnode
**vpp
, uint32_t * vidp
)
4201 proc_t p
= current_proc();
4202 struct fileproc
*fp
;
4206 proc_fdlock_spin(p
);
4207 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4211 if (fp
->f_type
!= DTYPE_VNODE
) {
4212 fp_drop(p
, fd
, fp
,1);
4216 vp
= (struct vnode
*)fp
->f_data
;
4220 if ((vidp
!= NULL
) && (vp
!= NULLVP
))
4221 *vidp
= (uint32_t)vp
->v_id
;
4232 * Description: Given an fd, look it up in the current process's per process
4233 * open file table, and return its internal socket pointer.
4235 * Parameters: fd fd to obtain vnode from
4236 * sp pointer to socket return area
4238 * Returns: 0 Success
4239 * ENOTSOCK Not a socket
4240 * fp_lookup:EBADF Bad file descriptor
4243 * *sp (modified) Returned socket pointer
4245 * Locks: This function internally takes and drops the proc_fdlock for
4246 * the current process
4248 * Notes: If successful, this function increments the f_iocount on the
4249 * fd's corresponding fileproc.
4251 * The fileproc referenced is not returned; because of this, care
4252 * must be taken to not drop the last reference (e.g. by closing
4253 * the file). This is inherently unsafe, since the reference may
4254 * not be recoverable from the socket, if there is a subsequent
4255 * close that destroys the associate fileproc. The caller should
4256 * therefore retain their own reference on the fileproc so that
4257 * the f_iocount can be dropped subsequently. Failure to do this
4258 * can result in the returned pointer immediately becoming invalid
4259 * following the call.
4261 * Use of this function is discouraged.
4264 file_socket(int fd
, struct socket
**sp
)
4266 proc_t p
= current_proc();
4267 struct fileproc
*fp
;
4270 proc_fdlock_spin(p
);
4271 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4275 if (fp
->f_type
!= DTYPE_SOCKET
) {
4276 fp_drop(p
, fd
, fp
,1);
4280 *sp
= (struct socket
*)fp
->f_data
;
4290 * Description: Given an fd, look it up in the current process's per process
4291 * open file table, and return its fileproc's flags field.
4293 * Parameters: fd fd whose flags are to be
4295 * flags pointer to flags data area
4297 * Returns: 0 Success
4298 * ENOTSOCK Not a socket
4299 * fp_lookup:EBADF Bad file descriptor
4302 * *flags (modified) Returned flags field
4304 * Locks: This function internally takes and drops the proc_fdlock for
4305 * the current process
4307 * Notes: This function will internally increment and decrement the
4308 * f_iocount of the fileproc as part of its operation.
4311 file_flags(int fd
, int *flags
)
4314 proc_t p
= current_proc();
4315 struct fileproc
*fp
;
4318 proc_fdlock_spin(p
);
4319 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4323 *flags
= (int)fp
->f_flag
;
4324 fp_drop(p
, fd
, fp
,1);
4334 * Description: Drop an iocount reference on an fd, and wake up any waiters
4335 * for draining (i.e. blocked in fileproc_drain() called during
4336 * the last attempt to close a file).
4338 * Parameters: fd fd on which an ioreference is
4341 * Returns: 0 Success
4342 * EBADF Bad file descriptor
4344 * Description: Given an fd, look it up in the current process's per process
4345 * open file table, and drop it's fileproc's f_iocount by one
4347 * Notes: This is intended as a corresponding operation to the functions
4348 * file_vnode() and file_socket() operations.
4350 * Technically, the close reference is supposed to be protected
4351 * by a fileproc_drain(), however, a drain will only block if
4352 * the fd refers to a character device, and that device has had
4353 * preparefileread() called on it. If it refers to something
4354 * other than a character device, then the drain will occur and
4355 * block each close attempt, rather than merely the last close.
4357 * Since it's possible for an fd that refers to a character
4358 * device to have an intermediate close followed by an open to
4359 * cause a different file to correspond to that descriptor,
4360 * unless there was a cautionary reference taken on the fileproc,
4361 * this is an inherently unsafe function. This happens in the
4362 * case where multiple fd's in a process refer to the same
4363 * character device (e.g. stdin/out/err pointing to a tty, etc.).
4365 * Use of this function is discouraged.
4370 struct fileproc
*fp
;
4371 proc_t p
= current_proc();
4374 proc_fdlock_spin(p
);
4375 if (fd
< 0 || fd
>= p
->p_fd
->fd_nfiles
||
4376 (fp
= p
->p_fd
->fd_ofiles
[fd
]) == NULL
||
4377 ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
4378 !(p
->p_fd
->fd_ofileflags
[fd
] & UF_CLOSING
))) {
4384 if (fp
->f_iocount
== 0) {
4385 if (fp
->f_flags
& FP_SELCONFLICT
)
4386 fp
->f_flags
&= ~FP_SELCONFLICT
;
4388 if (p
->p_fpdrainwait
) {
4389 p
->p_fpdrainwait
= 0;
4396 wakeup(&p
->p_fpdrainwait
);
4401 static int falloc_withalloc_locked(proc_t
, struct fileproc
**, int *,
4402 vfs_context_t
, struct fileproc
* (*)(void *), void *, int);
4407 * Description: Allocate an entry in the per process open file table and
4408 * return the corresponding fileproc and fd.
4410 * Parameters: p The process in whose open file
4411 * table the fd is to be allocated
4412 * resultfp Pointer to fileproc pointer
4414 * resultfd Pointer to fd return area
4417 * Returns: 0 Success
4418 * falloc:ENFILE Too many open files in system
4419 * falloc:EMFILE Too many open files in process
4420 * falloc:ENOMEM M_FILEPROC or M_FILEGLOB zone
4424 * *resultfd (modified) Returned fileproc pointer
4425 * *resultfd (modified) Returned fd
4427 * Locks: This function takes and drops the proc_fdlock; if this lock
4428 * is already held, use falloc_locked() instead.
4430 * Notes: This function takes separate process and context arguments
4431 * solely to support kern_exec.c; otherwise, it would take
4432 * neither, and expect falloc_locked() to use the
4433 * vfs_context_current() routine internally.
4436 falloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
)
4438 return (falloc_withalloc(p
, resultfp
, resultfd
, ctx
,
4439 fileproc_alloc_init
, NULL
));
4443 * Like falloc, but including the fileproc allocator and create-args
4446 falloc_withalloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4447 vfs_context_t ctx
, fp_allocfn_t fp_zalloc
, void *arg
)
4452 error
= falloc_withalloc_locked(p
,
4453 resultfp
, resultfd
, ctx
, fp_zalloc
, arg
, 1);
4460 * "uninitialized" ops -- ensure fg->fg_ops->fo_type always exists
4462 static const struct fileops uninitops
;
4467 * Create a new open file structure and allocate
4468 * a file descriptor for the process that refers to it.
4470 * Returns: 0 Success
4472 * Description: Allocate an entry in the per process open file table and
4473 * return the corresponding fileproc and fd.
4475 * Parameters: p The process in whose open file
4476 * table the fd is to be allocated
4477 * resultfp Pointer to fileproc pointer
4479 * resultfd Pointer to fd return area
4481 * locked Flag to indicate whether the
4482 * caller holds proc_fdlock
4484 * Returns: 0 Success
4485 * ENFILE Too many open files in system
4486 * fdalloc:EMFILE Too many open files in process
4487 * ENOMEM M_FILEPROC or M_FILEGLOB zone
4492 * *resultfd (modified) Returned fileproc pointer
4493 * *resultfd (modified) Returned fd
4495 * Locks: If the parameter 'locked' is zero, this function takes and
4496 * drops the proc_fdlock; if non-zero, the caller must hold the
4499 * Notes: If you intend to use a non-zero 'locked' parameter, use the
4500 * utility function falloc() instead.
4502 * This function takes separate process and context arguments
4503 * solely to support kern_exec.c; otherwise, it would take
4504 * neither, and use the vfs_context_current() routine internally.
4507 falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4508 vfs_context_t ctx
, int locked
)
4510 return (falloc_withalloc_locked(p
, resultfp
, resultfd
, ctx
,
4511 fileproc_alloc_init
, NULL
, locked
));
4515 falloc_withalloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4516 vfs_context_t ctx
, fp_allocfn_t fp_zalloc
, void *crarg
,
4519 struct fileproc
*fp
;
4520 struct fileglob
*fg
;
4525 if ( (error
= fdalloc(p
, 0, &nfd
)) ) {
4530 if (nfiles
>= maxfiles
) {
4537 error
= mac_file_check_create(proc_ucred(p
));
4546 * Allocate a new file descriptor.
4547 * If the process has file descriptor zero open, add to the list
4548 * of open files at that point, otherwise put it at the front of
4549 * the list of open files.
4553 fp
= (*fp_zalloc
)(crarg
);
4559 MALLOC_ZONE(fg
, struct fileglob
*, sizeof(struct fileglob
), M_FILEGLOB
, M_WAITOK
);
4566 bzero(fg
, sizeof(struct fileglob
));
4567 lck_mtx_init(&fg
->fg_lock
, file_lck_grp
, file_lck_attr
);
4571 fg
->fg_ops
= &uninitops
;
4574 mac_file_label_init(fg
);
4577 kauth_cred_ref(ctx
->vc_ucred
);
4581 fp
->f_cred
= ctx
->vc_ucred
;
4584 mac_file_label_associate(fp
->f_cred
, fg
);
4587 OSAddAtomic(1, &nfiles
);
4589 p
->p_fd
->fd_ofiles
[nfd
] = fp
;
4606 * Description: Free a file structure; drop the global open file count, and
4607 * drop the credential reference, if the fileglob has one, and
4608 * destroy the instance mutex before freeing
4610 * Parameters: fg Pointer to fileglob to be
4616 fg_free(struct fileglob
*fg
)
4618 OSAddAtomic(-1, &nfiles
);
4620 if (fg
->fg_vn_data
) {
4621 fg_vn_data_free(fg
->fg_vn_data
);
4622 fg
->fg_vn_data
= NULL
;
4625 if (IS_VALID_CRED(fg
->fg_cred
)) {
4626 kauth_cred_unref(&fg
->fg_cred
);
4628 lck_mtx_destroy(&fg
->fg_lock
, file_lck_grp
);
4631 mac_file_label_destroy(fg
);
4633 FREE_ZONE(fg
, sizeof *fg
, M_FILEGLOB
);
4640 * Description: Perform close-on-exec processing for all files in a process
4641 * that are either marked as close-on-exec, or which were in the
4642 * process of being opened at the time of the execve
4644 * Also handles the case (via posix_spawn()) where -all-
4645 * files except those marked with "inherit" as treated as
4648 * Parameters: p Pointer to process calling
4653 * Locks: This function internally takes and drops proc_fdlock()
4657 fdexec(proc_t p
, short flags
)
4659 struct filedesc
*fdp
= p
->p_fd
;
4661 boolean_t cloexec_default
= (flags
& POSIX_SPAWN_CLOEXEC_DEFAULT
) != 0;
4664 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
4666 struct fileproc
*fp
= fdp
->fd_ofiles
[i
];
4667 char *flagp
= &fdp
->fd_ofileflags
[i
];
4669 if (fp
&& cloexec_default
) {
4671 * Reverse the usual semantics of file descriptor
4672 * inheritance - all of them should be closed
4673 * except files marked explicitly as "inherit" and
4674 * not marked close-on-exec.
4676 if ((*flagp
& (UF_EXCLOSE
|UF_INHERIT
)) != UF_INHERIT
)
4677 *flagp
|= UF_EXCLOSE
;
4678 *flagp
&= ~UF_INHERIT
;
4682 ((*flagp
& (UF_RESERVED
|UF_EXCLOSE
)) == UF_EXCLOSE
)
4684 || (fp
&& mac_file_check_inherit(proc_ucred(p
), fp
->f_fglob
))
4687 if (i
< fdp
->fd_knlistsize
)
4688 knote_fdclose(p
, i
);
4689 procfdtbl_clearfd(p
, i
);
4690 if (i
== fdp
->fd_lastfile
&& i
> 0)
4692 if (i
< fdp
->fd_freefile
)
4693 fdp
->fd_freefile
= i
;
4696 * Wait for any third party viewers (e.g., lsof)
4697 * to release their references to this fileproc.
4699 while (fp
->f_iocount
> 0) {
4700 p
->p_fpdrainwait
= 1;
4701 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
,
4705 closef_locked(fp
, fp
->f_fglob
, p
);
4717 * Description: Copy a filedesc structure. This is normally used as part of
4718 * forkproc() when forking a new process, to copy the per process
4719 * open file table over to the new process.
4721 * Parameters: p Process whose open file table
4722 * is to be copied (parent)
4723 * uth_cdir Per thread current working
4724 * cirectory, or NULL
4726 * Returns: NULL Copy failed
4727 * !NULL Pointer to new struct filedesc
4729 * Locks: This function internally takes and drops proc_fdlock()
4731 * Notes: Files are copied directly, ignoring the new resource limits
4732 * for the process that's being copied into. Since the descriptor
4733 * references are just additional references, this does not count
4734 * against the number of open files on the system.
4736 * The struct filedesc includes the current working directory,
4737 * and the current root directory, if the process is chroot'ed.
4739 * If the exec was called by a thread using a per thread current
4740 * working directory, we inherit the working directory from the
4741 * thread making the call, rather than from the process.
4743 * In the case of a failure to obtain a reference, for most cases,
4744 * the file entry will be silently dropped. There's an exception
4745 * for the case of a chroot dir, since a failure to to obtain a
4746 * reference there would constitute an "escape" from the chroot
4747 * environment, which must not be allowed. In that case, we will
4748 * deny the execve() operation, rather than allowing the escape.
4751 fdcopy(proc_t p
, vnode_t uth_cdir
)
4753 struct filedesc
*newfdp
, *fdp
= p
->p_fd
;
4755 struct fileproc
*ofp
, *fp
;
4758 MALLOC_ZONE(newfdp
, struct filedesc
*,
4759 sizeof(*newfdp
), M_FILEDESC
, M_WAITOK
);
4766 * the FD_CHROOT flag will be inherited via this copy
4768 (void) memcpy(newfdp
, fdp
, sizeof(*newfdp
));
4771 * If we are running with per-thread current working directories,
4772 * inherit the new current working directory from the current thread
4773 * instead, before we take our references.
4775 if (uth_cdir
!= NULLVP
)
4776 newfdp
->fd_cdir
= uth_cdir
;
4779 * For both fd_cdir and fd_rdir make sure we get
4780 * a valid reference... if we can't, than set
4781 * set the pointer(s) to NULL in the child... this
4782 * will keep us from using a non-referenced vp
4783 * and allows us to do the vnode_rele only on
4784 * a properly referenced vp
4786 if ( (v_dir
= newfdp
->fd_cdir
) ) {
4787 if (vnode_getwithref(v_dir
) == 0) {
4788 if ( (vnode_ref(v_dir
)) )
4789 newfdp
->fd_cdir
= NULL
;
4792 newfdp
->fd_cdir
= NULL
;
4794 if (newfdp
->fd_cdir
== NULL
&& fdp
->fd_cdir
) {
4796 * we couldn't get a new reference on
4797 * the current working directory being
4798 * inherited... we might as well drop
4799 * our reference from the parent also
4800 * since the vnode has gone DEAD making
4801 * it useless... by dropping it we'll
4802 * be that much closer to recycling it
4804 vnode_rele(fdp
->fd_cdir
);
4805 fdp
->fd_cdir
= NULL
;
4808 if ( (v_dir
= newfdp
->fd_rdir
) ) {
4809 if (vnode_getwithref(v_dir
) == 0) {
4810 if ( (vnode_ref(v_dir
)) )
4811 newfdp
->fd_rdir
= NULL
;
4814 newfdp
->fd_rdir
= NULL
;
4817 /* Coming from a chroot environment and unable to get a reference... */
4818 if (newfdp
->fd_rdir
== NULL
&& fdp
->fd_rdir
) {
4820 * We couldn't get a new reference on
4821 * the chroot directory being
4822 * inherited... this is fatal, since
4823 * otherwise it would constitute an
4824 * escape from a chroot environment by
4827 if (newfdp
->fd_cdir
)
4828 vnode_rele(newfdp
->fd_cdir
);
4829 FREE_ZONE(newfdp
, sizeof *newfdp
, M_FILEDESC
);
4834 * If the number of open files fits in the internal arrays
4835 * of the open file structure, use them, otherwise allocate
4836 * additional memory for the number of descriptors currently
4839 if (newfdp
->fd_lastfile
< NDFILE
)
4843 * Compute the smallest multiple of NDEXTENT needed
4844 * for the file descriptors currently in use,
4845 * allowing the table to shrink.
4847 i
= newfdp
->fd_nfiles
;
4848 while (i
> 1 + 2 * NDEXTENT
&& i
> 1 + newfdp
->fd_lastfile
* 2)
4853 MALLOC_ZONE(newfdp
->fd_ofiles
, struct fileproc
**,
4854 i
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
4855 if (newfdp
->fd_ofiles
== NULL
) {
4856 if (newfdp
->fd_cdir
)
4857 vnode_rele(newfdp
->fd_cdir
);
4858 if (newfdp
->fd_rdir
)
4859 vnode_rele(newfdp
->fd_rdir
);
4861 FREE_ZONE(newfdp
, sizeof(*newfdp
), M_FILEDESC
);
4864 (void) memset(newfdp
->fd_ofiles
, 0, i
* OFILESIZE
);
4867 newfdp
->fd_ofileflags
= (char *) &newfdp
->fd_ofiles
[i
];
4868 newfdp
->fd_nfiles
= i
;
4870 if (fdp
->fd_nfiles
> 0) {
4871 struct fileproc
**fpp
;
4874 (void) memcpy(newfdp
->fd_ofiles
, fdp
->fd_ofiles
,
4875 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofiles
));
4876 (void) memcpy(newfdp
->fd_ofileflags
, fdp
->fd_ofileflags
,
4877 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofileflags
));
4880 * kq descriptors cannot be copied.
4882 if (newfdp
->fd_knlistsize
!= -1) {
4883 fpp
= &newfdp
->fd_ofiles
[newfdp
->fd_lastfile
];
4884 flags
= &newfdp
->fd_ofileflags
[newfdp
->fd_lastfile
];
4885 for (i
= newfdp
->fd_lastfile
;
4886 i
>= 0; i
--, fpp
--, flags
--) {
4887 if (*flags
& UF_RESERVED
)
4888 continue; /* (removed below) */
4889 if (*fpp
!= NULL
&& (*fpp
)->f_type
== DTYPE_KQUEUE
) {
4892 if (i
< newfdp
->fd_freefile
)
4893 newfdp
->fd_freefile
= i
;
4895 if (*fpp
== NULL
&& i
== newfdp
->fd_lastfile
&& i
> 0)
4896 newfdp
->fd_lastfile
--;
4898 newfdp
->fd_knlist
= NULL
;
4899 newfdp
->fd_knlistsize
= -1;
4900 newfdp
->fd_knhash
= NULL
;
4901 newfdp
->fd_knhashmask
= 0;
4903 fpp
= newfdp
->fd_ofiles
;
4904 flags
= newfdp
->fd_ofileflags
;
4906 for (i
= newfdp
->fd_lastfile
+ 1; --i
>= 0; fpp
++, flags
++)
4907 if ((ofp
= *fpp
) != NULL
&&
4908 0 == (ofp
->f_fglob
->fg_lflags
& FG_CONFINED
) &&
4909 0 == (*flags
& (UF_FORKCLOSE
|UF_RESERVED
))) {
4911 if (FILEPROC_TYPE(ofp
) != FTYPE_SIMPLE
)
4912 panic("complex fileproc");
4914 fp
= fileproc_alloc_init(NULL
);
4917 * XXX no room to copy, unable to
4918 * XXX safely unwind state at present
4923 (ofp
->f_flags
& ~FP_TYPEMASK
);
4924 fp
->f_fglob
= ofp
->f_fglob
;
4929 if (i
< newfdp
->fd_freefile
)
4930 newfdp
->fd_freefile
= i
;
4944 * Description: Release a filedesc (per process open file table) structure;
4945 * this is done on process exit(), or from forkproc_free() if
4946 * the fork fails for some reason subsequent to a successful
4949 * Parameters: p Pointer to process going away
4953 * Locks: This function internally takes and drops proc_fdlock()
4958 struct filedesc
*fdp
;
4959 struct fileproc
*fp
;
4964 if (p
== kernproc
|| NULL
== (fdp
= p
->p_fd
)) {
4969 extern struct filedesc filedesc0
;
4971 if (&filedesc0
== fdp
)
4974 if (fdp
->fd_nfiles
> 0 && fdp
->fd_ofiles
) {
4975 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
4976 if ((fp
= fdp
->fd_ofiles
[i
]) != NULL
) {
4978 if (fdp
->fd_ofileflags
[i
] & UF_RESERVED
)
4979 panic("fdfree: found fp with UF_RESERVED");
4981 procfdtbl_reservefd(p
, i
);
4983 if (i
< fdp
->fd_knlistsize
)
4984 knote_fdclose(p
, i
);
4985 if (fp
->f_flags
& FP_WAITEVENT
)
4986 (void)waitevent_close(p
, fp
);
4987 (void) closef_locked(fp
, fp
->f_fglob
, p
);
4991 FREE_ZONE(fdp
->fd_ofiles
, fdp
->fd_nfiles
* OFILESIZE
, M_OFILETABL
);
4992 fdp
->fd_ofiles
= NULL
;
4999 vnode_rele(fdp
->fd_cdir
);
5001 vnode_rele(fdp
->fd_rdir
);
5003 proc_fdlock_spin(p
);
5008 FREE(fdp
->fd_knlist
, M_KQUEUE
);
5010 FREE(fdp
->fd_knhash
, M_KQUEUE
);
5012 FREE_ZONE(fdp
, sizeof(*fdp
), M_FILEDESC
);
5018 * Description: Internal form of closef; called with proc_fdlock held
5020 * Parameters: fp Pointer to fileproc for fd
5021 * fg Pointer to fileglob for fd
5022 * p Pointer to proc structure
5024 * Returns: 0 Success
5025 * closef_finish:??? Anything returnable by a per-fileops
5028 * Note: Decrements reference count on file structure; if this was the
5029 * last reference, then closef_finish() is called
5031 * p and fp are allowed to be NULL when closing a file that was
5032 * being passed in a message (but only if we are called when this
5033 * is NOT the last reference).
5036 closef_locked(struct fileproc
*fp
, struct fileglob
*fg
, proc_t p
)
5040 struct vfs_context context
;
5047 /* Set up context with cred stashed in fg */
5048 if (p
== current_proc())
5049 context
.vc_thread
= current_thread();
5051 context
.vc_thread
= NULL
;
5052 context
.vc_ucred
= fg
->fg_cred
;
5055 * POSIX record locking dictates that any close releases ALL
5056 * locks owned by this process. This is handled by setting
5057 * a flag in the unlock to free ONLY locks obeying POSIX
5058 * semantics, and not to free BSD-style file locks.
5059 * If the descriptor was in a message, POSIX-style locks
5060 * aren't passed with the descriptor.
5062 if (p
&& (p
->p_ladvflag
& P_LADVLOCK
) &&
5063 DTYPE_VNODE
== FILEGLOB_DTYPE(fg
)) {
5066 lf
.l_whence
= SEEK_SET
;
5069 lf
.l_type
= F_UNLCK
;
5070 vp
= (struct vnode
*)fg
->fg_data
;
5072 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
5073 (void) VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &lf
, F_POSIX
, &context
, NULL
);
5074 (void)vnode_put(vp
);
5078 lck_mtx_lock_spin(&fg
->fg_lock
);
5081 if (fg
->fg_count
> 0) {
5082 lck_mtx_unlock(&fg
->fg_lock
);
5086 if (fg
->fg_count
!= 0)
5087 panic("fg %p: being freed with bad fg_count (%d)", fg
, fg
->fg_count
);
5090 if (fp
&& (fp
->f_flags
& FP_WRITTEN
))
5091 fg
->fg_flag
|= FWASWRITTEN
;
5093 fg
->fg_lflags
|= FG_TERM
;
5094 lck_mtx_unlock(&fg
->fg_lock
);
5099 /* Since we ensure that fg->fg_ops is always initialized,
5100 * it is safe to invoke fo_close on the fg */
5101 error
= fo_close(fg
, &context
);
5115 * Description: Drain out pending I/O operations
5117 * Parameters: p Process closing this file
5118 * fp fileproc struct for the open
5119 * instance on the file
5123 * Locks: Assumes the caller holds the proc_fdlock
5125 * Notes: For character devices, this occurs on the last close of the
5126 * device; for all other file descriptors, this occurs on each
5127 * close to prevent fd's from being closed out from under
5128 * operations currently in progress and blocked
5130 * See Also: file_vnode(), file_socket(), file_drop(), and the cautions
5131 * regarding their use and interaction with this function.
5134 fileproc_drain(proc_t p
, struct fileproc
* fp
)
5136 struct vfs_context context
;
5138 context
.vc_thread
= proc_thread(p
); /* XXX */
5139 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
5141 fp
->f_iocount
-- ; /* (the one the close holds) */
5143 while (fp
->f_iocount
) {
5145 lck_mtx_convert_spin(&p
->p_fdmlock
);
5147 if (fp
->f_fglob
->fg_ops
->fo_drain
) {
5148 (*fp
->f_fglob
->fg_ops
->fo_drain
)(fp
, &context
);
5150 if ((fp
->f_flags
& FP_INSELECT
) == FP_INSELECT
) {
5151 if (waitq_wakeup64_all((struct waitq
*)fp
->f_wset
, NO_EVENT64
,
5152 THREAD_INTERRUPTED
, WAITQ_ALL_PRIORITIES
) == KERN_INVALID_ARGUMENT
)
5153 panic("bad wait queue for waitq_wakeup64_all %p (fp:%p)", fp
->f_wset
, fp
);
5155 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
) {
5156 if (waitq_wakeup64_all(&select_conflict_queue
, NO_EVENT64
,
5157 THREAD_INTERRUPTED
, WAITQ_ALL_PRIORITIES
) == KERN_INVALID_ARGUMENT
)
5158 panic("bad select_conflict_queue");
5160 p
->p_fpdrainwait
= 1;
5162 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
, "fpdrain", NULL
);
5166 if ((fp
->f_flags
& FP_INSELECT
) != 0)
5167 panic("FP_INSELECT set on drained fp");
5169 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
)
5170 fp
->f_flags
&= ~FP_SELCONFLICT
;
5177 * Description: Release the fd and free the fileproc associated with the fd
5178 * in the per process open file table of the specified process;
5179 * these values must correspond.
5181 * Parameters: p Process containing fd
5182 * fd fd to be released
5183 * fp fileproc to be freed
5185 * Returns: 0 Success
5187 * Notes: XXX function should be void - no one interprets the returns
5191 fp_free(proc_t p
, int fd
, struct fileproc
* fp
)
5193 proc_fdlock_spin(p
);
5197 fg_free(fp
->f_fglob
);
5206 * Description: Apply an advisory lock on a file descriptor.
5208 * Parameters: p Process making request
5209 * uap->fd fd on which the lock is to be
5211 * uap->how (Un)Lock bits, including type
5212 * retval Pointer to the call return area
5214 * Returns: 0 Success
5215 * fp_getfvp:EBADF Bad file descriptor
5216 * fp_getfvp:ENOTSUP fd does not refer to a vnode
5217 * vnode_getwithref:???
5221 * *retval (modified) Size of dtable
5223 * Notes: Just attempt to get a record lock of the requested type on
5224 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
5227 flock(proc_t p
, struct flock_args
*uap
, __unused
int32_t *retval
)
5231 struct fileproc
*fp
;
5234 vfs_context_t ctx
= vfs_context_current();
5237 AUDIT_ARG(fd
, uap
->fd
);
5238 if ( (error
= fp_getfvp(p
, fd
, &fp
, &vp
)) ) {
5241 if ( (error
= vnode_getwithref(vp
)) ) {
5244 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
5246 lf
.l_whence
= SEEK_SET
;
5249 if (how
& LOCK_UN
) {
5250 lf
.l_type
= F_UNLCK
;
5251 fp
->f_flag
&= ~FHASLOCK
;
5252 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_UNLCK
, &lf
, F_FLOCK
, ctx
, NULL
);
5256 lf
.l_type
= F_WRLCK
;
5257 else if (how
& LOCK_SH
)
5258 lf
.l_type
= F_RDLCK
;
5264 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
, F_SETLK
, &lf
);
5268 fp
->f_flag
|= FHASLOCK
;
5269 if (how
& LOCK_NB
) {
5270 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, F_FLOCK
, ctx
, NULL
);
5273 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, F_FLOCK
|F_WAIT
, ctx
, NULL
);
5275 (void)vnode_put(vp
);
5277 fp_drop(p
, fd
, fp
, 0);
5285 * Description: Obtain a Mach send right for a given file descriptor.
5287 * Parameters: p Process calling fileport
5288 * uap->fd The fd to reference
5289 * uap->portnamep User address at which to place port name.
5291 * Returns: 0 Success.
5292 * EBADF Bad file descriptor.
5293 * EINVAL File descriptor had type that cannot be sent, misc. other errors.
5294 * EFAULT Address at which to store port name is not valid.
5295 * EAGAIN Resource shortage.
5298 * On success, name of send right is stored at user-specified address.
5301 fileport_makeport(proc_t p
, struct fileport_makeport_args
*uap
,
5302 __unused
int *retval
)
5306 user_addr_t user_portaddr
= uap
->portnamep
;
5307 struct fileproc
*fp
= FILEPROC_NULL
;
5308 struct fileglob
*fg
= NULL
;
5309 ipc_port_t fileport
;
5310 mach_port_name_t name
= MACH_PORT_NULL
;
5313 err
= fp_lookup(p
, fd
, &fp
, 1);
5318 if (!file_issendable(p
, fp
)) {
5323 if (FP_ISGUARDED(fp
, GUARD_FILEPORT
)) {
5324 err
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_FILEPORT
);
5328 /* Dropped when port is deallocated */
5334 /* Allocate and initialize a port */
5335 fileport
= fileport_alloc(fg
);
5336 if (fileport
== IPC_PORT_NULL
) {
5342 /* Add an entry. Deallocates port on failure. */
5343 name
= ipc_port_copyout_send(fileport
, get_task_ipcspace(p
->task
));
5344 if (!MACH_PORT_VALID(name
)) {
5349 err
= copyout(&name
, user_portaddr
, sizeof(mach_port_name_t
));
5354 /* Tag the fileglob for debugging purposes */
5355 lck_mtx_lock_spin(&fg
->fg_lock
);
5356 fg
->fg_lflags
|= FG_PORTMADE
;
5357 lck_mtx_unlock(&fg
->fg_lock
);
5359 fp_drop(p
, fd
, fp
, 0);
5366 if (MACH_PORT_VALID(name
)) {
5367 /* Don't care if another thread races us to deallocate the entry */
5368 (void) mach_port_deallocate(get_task_ipcspace(p
->task
), name
);
5371 if (fp
!= FILEPROC_NULL
) {
5372 fp_drop(p
, fd
, fp
, 0);
5379 fileport_releasefg(struct fileglob
*fg
)
5381 (void)closef_locked(NULL
, fg
, PROC_NULL
);
5390 * Description: Obtain the file descriptor for a given Mach send right.
5392 * Parameters: p Process calling fileport
5393 * uap->port Name of send right to file port.
5395 * Returns: 0 Success
5396 * EINVAL Invalid Mach port name, or port is not for a file.
5398 * fdalloc:ENOMEM Unable to allocate fileproc or extend file table.
5401 * *retval (modified) The new descriptor
5404 fileport_makefd(proc_t p
, struct fileport_makefd_args
*uap
, int32_t *retval
)
5406 struct fileglob
*fg
;
5407 struct fileproc
*fp
= FILEPROC_NULL
;
5408 ipc_port_t port
= IPC_PORT_NULL
;
5409 mach_port_name_t send
= uap
->port
;
5414 res
= ipc_object_copyin(get_task_ipcspace(p
->task
),
5415 send
, MACH_MSG_TYPE_COPY_SEND
, &port
);
5417 if (res
!= KERN_SUCCESS
) {
5422 fg
= fileport_port_to_fileglob(port
);
5428 fp
= fileproc_alloc_init(NULL
);
5429 if (fp
== FILEPROC_NULL
) {
5438 err
= fdalloc(p
, 0, &fd
);
5443 *fdflags(p
, fd
) |= UF_EXCLOSE
;
5445 procfdtbl_releasefd(p
, fd
, fp
);
5451 if ((fp
!= NULL
) && (0 != err
)) {
5455 if (IPC_PORT_NULL
!= port
) {
5456 ipc_port_release_send(port
);
5466 * Description: Duplicate the specified descriptor to a free descriptor;
5467 * this is the second half of fdopen(), above.
5469 * Parameters: fdp filedesc pointer to fill in
5471 * dfd fd to dup from
5472 * mode mode to set on new fd
5473 * error command code
5475 * Returns: 0 Success
5476 * EBADF Source fd is bad
5477 * EACCES Requested mode not allowed
5478 * !0 'error', if not ENODEV or
5481 * Notes: XXX This is not thread safe; see fdopen() above
5484 dupfdopen(struct filedesc
*fdp
, int indx
, int dfd
, int flags
, int error
)
5486 struct fileproc
*wfp
;
5487 struct fileproc
*fp
;
5491 proc_t p
= current_proc();
5494 * If the to-be-dup'd fd number is greater than the allowed number
5495 * of file descriptors, or the fd to be dup'd has already been
5496 * closed, reject. Note, check for new == old is necessary as
5497 * falloc could allocate an already closed to-be-dup'd descriptor
5498 * as the new descriptor.
5502 fp
= fdp
->fd_ofiles
[indx
];
5503 if (dfd
< 0 || dfd
>= fdp
->fd_nfiles
||
5504 (wfp
= fdp
->fd_ofiles
[dfd
]) == NULL
|| wfp
== fp
||
5505 (fdp
->fd_ofileflags
[dfd
] & UF_RESERVED
)) {
5511 myerror
= mac_file_check_dup(proc_ucred(p
), wfp
->f_fglob
, dfd
);
5518 * There are two cases of interest here.
5520 * For ENODEV simply dup (dfd) to file descriptor
5521 * (indx) and return.
5523 * For ENXIO steal away the file structure from (dfd) and
5524 * store it in (indx). (dfd) is effectively closed by
5527 * Any other error code is just returned.
5531 if (FP_ISGUARDED(wfp
, GUARD_DUP
)) {
5537 * Check that the mode the file is being opened for is a
5538 * subset of the mode of the existing descriptor.
5540 if (((flags
& (FREAD
|FWRITE
)) | wfp
->f_flag
) != wfp
->f_flag
) {
5544 if (indx
> fdp
->fd_lastfile
)
5545 fdp
->fd_lastfile
= indx
;
5549 fg_free(fp
->f_fglob
);
5550 fp
->f_fglob
= wfp
->f_fglob
;
5552 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
] |
5553 (flags
& O_CLOEXEC
) ? UF_EXCLOSE
: 0;
5569 * Description: Add a reference to a fileglob by fileproc
5571 * Parameters: fp fileproc containing fileglob
5576 * Notes: XXX Should use OSAddAtomic?
5579 fg_ref(struct fileproc
* fp
)
5581 struct fileglob
*fg
;
5585 lck_mtx_lock_spin(&fg
->fg_lock
);
5588 if ((fp
->f_flags
& ~((unsigned int)FP_VALID_FLAGS
)) != 0)
5589 panic("fg_ref: invalid bits on fp %p", fp
);
5591 if (fg
->fg_count
== 0)
5592 panic("fg_ref: adding fgcount to zeroed fg: fp %p fg %p",
5596 lck_mtx_unlock(&fg
->fg_lock
);
5603 * Description: Remove a reference to a fileglob by fileproc
5605 * Parameters: fp fileproc containing fileglob
5610 * Notes: XXX Should use OSAddAtomic?
5613 fg_drop(struct fileproc
* fp
)
5615 struct fileglob
*fg
;
5618 lck_mtx_lock_spin(&fg
->fg_lock
);
5620 lck_mtx_unlock(&fg
->fg_lock
);
5625 * fg_insertuipc_mark
5627 * Description: Mark fileglob for insertion onto message queue if needed
5628 * Also takes fileglob reference
5630 * Parameters: fg Fileglob pointer to insert
5632 * Returns: true, if the fileglob needs to be inserted onto msg queue
5634 * Locks: Takes and drops fg_lock, potentially many times
5637 fg_insertuipc_mark(struct fileglob
* fg
)
5639 boolean_t insert
= FALSE
;
5641 lck_mtx_lock_spin(&fg
->fg_lock
);
5642 while (fg
->fg_lflags
& FG_RMMSGQ
) {
5643 lck_mtx_convert_spin(&fg
->fg_lock
);
5645 fg
->fg_lflags
|= FG_WRMMSGQ
;
5646 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_insertuipc", NULL
);
5651 if (fg
->fg_msgcount
== 1) {
5652 fg
->fg_lflags
|= FG_INSMSGQ
;
5655 lck_mtx_unlock(&fg
->fg_lock
);
5662 * Description: Insert marked fileglob onto message queue
5664 * Parameters: fg Fileglob pointer to insert
5668 * Locks: Takes and drops fg_lock & uipc_lock
5669 * DO NOT call this function with proc_fdlock held as unp_gc()
5670 * can potentially try to acquire proc_fdlock, which can result
5671 * in a deadlock if this function is in unp_gc_wait().
5674 fg_insertuipc(struct fileglob
* fg
)
5676 if (fg
->fg_lflags
& FG_INSMSGQ
) {
5677 lck_mtx_lock_spin(uipc_lock
);
5679 LIST_INSERT_HEAD(&fmsghead
, fg
, f_msglist
);
5680 lck_mtx_unlock(uipc_lock
);
5681 lck_mtx_lock(&fg
->fg_lock
);
5682 fg
->fg_lflags
&= ~FG_INSMSGQ
;
5683 if (fg
->fg_lflags
& FG_WINSMSGQ
) {
5684 fg
->fg_lflags
&= ~FG_WINSMSGQ
;
5685 wakeup(&fg
->fg_lflags
);
5687 lck_mtx_unlock(&fg
->fg_lock
);
5692 * fg_removeuipc_mark
5694 * Description: Mark the fileglob for removal from message queue if needed
5695 * Also releases fileglob message queue reference
5697 * Parameters: fg Fileglob pointer to remove
5699 * Returns: true, if the fileglob needs to be removed from msg queue
5701 * Locks: Takes and drops fg_lock, potentially many times
5704 fg_removeuipc_mark(struct fileglob
* fg
)
5706 boolean_t remove
= FALSE
;
5708 lck_mtx_lock_spin(&fg
->fg_lock
);
5709 while (fg
->fg_lflags
& FG_INSMSGQ
) {
5710 lck_mtx_convert_spin(&fg
->fg_lock
);
5712 fg
->fg_lflags
|= FG_WINSMSGQ
;
5713 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_removeuipc", NULL
);
5716 if (fg
->fg_msgcount
== 0) {
5717 fg
->fg_lflags
|= FG_RMMSGQ
;
5720 lck_mtx_unlock(&fg
->fg_lock
);
5727 * Description: Remove marked fileglob from message queue
5729 * Parameters: fg Fileglob pointer to remove
5733 * Locks: Takes and drops fg_lock & uipc_lock
5734 * DO NOT call this function with proc_fdlock held as unp_gc()
5735 * can potentially try to acquire proc_fdlock, which can result
5736 * in a deadlock if this function is in unp_gc_wait().
5739 fg_removeuipc(struct fileglob
* fg
)
5741 if (fg
->fg_lflags
& FG_RMMSGQ
) {
5742 lck_mtx_lock_spin(uipc_lock
);
5744 LIST_REMOVE(fg
, f_msglist
);
5745 lck_mtx_unlock(uipc_lock
);
5746 lck_mtx_lock(&fg
->fg_lock
);
5747 fg
->fg_lflags
&= ~FG_RMMSGQ
;
5748 if (fg
->fg_lflags
& FG_WRMMSGQ
) {
5749 fg
->fg_lflags
&= ~FG_WRMMSGQ
;
5750 wakeup(&fg
->fg_lflags
);
5752 lck_mtx_unlock(&fg
->fg_lock
);
5755 #endif /* SOCKETS */
5760 * Description: Generic fileops read indirected through the fileops pointer
5761 * in the fileproc structure
5763 * Parameters: fp fileproc structure pointer
5764 * uio user I/O structure pointer
5766 * ctx VFS context for operation
5768 * Returns: 0 Success
5769 * !0 Errno from read
5772 fo_read(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
5774 return ((*fp
->f_ops
->fo_read
)(fp
, uio
, flags
, ctx
));
5781 * Description: Generic fileops write indirected through the fileops pointer
5782 * in the fileproc structure
5784 * Parameters: fp fileproc structure pointer
5785 * uio user I/O structure pointer
5787 * ctx VFS context for operation
5789 * Returns: 0 Success
5790 * !0 Errno from write
5793 fo_write(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
5795 return((*fp
->f_ops
->fo_write
)(fp
, uio
, flags
, ctx
));
5802 * Description: Generic fileops ioctl indirected through the fileops pointer
5803 * in the fileproc structure
5805 * Parameters: fp fileproc structure pointer
5807 * data pointer to internalized copy
5808 * of user space ioctl command
5809 * parameter data in kernel space
5810 * ctx VFS context for operation
5812 * Returns: 0 Success
5813 * !0 Errno from ioctl
5815 * Locks: The caller is assumed to have held the proc_fdlock; this
5816 * function releases and reacquires this lock. If the caller
5817 * accesses data protected by this lock prior to calling this
5818 * function, it will need to revalidate/reacquire any cached
5819 * protected data obtained prior to the call.
5822 fo_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
, vfs_context_t ctx
)
5826 proc_fdunlock(vfs_context_proc(ctx
));
5827 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, com
, data
, ctx
);
5828 proc_fdlock(vfs_context_proc(ctx
));
5836 * Description: Generic fileops select indirected through the fileops pointer
5837 * in the fileproc structure
5839 * Parameters: fp fileproc structure pointer
5840 * which select which
5841 * wql pointer to wait queue list
5842 * ctx VFS context for operation
5844 * Returns: 0 Success
5845 * !0 Errno from select
5848 fo_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
5850 return((*fp
->f_ops
->fo_select
)(fp
, which
, wql
, ctx
));
5857 * Description: Generic fileops close indirected through the fileops pointer
5858 * in the fileproc structure
5860 * Parameters: fp fileproc structure pointer for
5862 * ctx VFS context for operation
5864 * Returns: 0 Success
5865 * !0 Errno from close
5868 fo_close(struct fileglob
*fg
, vfs_context_t ctx
)
5870 return((*fg
->fg_ops
->fo_close
)(fg
, ctx
));
5877 * Description: Generic fileops kqueue filter indirected through the fileops
5878 * pointer in the fileproc structure
5880 * Parameters: fp fileproc structure pointer
5881 * kn pointer to knote to filter on
5882 * ctx VFS context for operation
5884 * Returns: 0 Success
5885 * !0 Errno from kqueue filter
5888 fo_kqfilter(struct fileproc
*fp
, struct knote
*kn
, vfs_context_t ctx
)
5890 return ((*fp
->f_ops
->fo_kqfilter
)(fp
, kn
, ctx
));
5894 * The ability to send a file descriptor to another
5895 * process is opt-in by file type.
5898 file_issendable(proc_t p
, struct fileproc
*fp
)
5900 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
5902 switch (fp
->f_type
) {
5907 return (0 == (fp
->f_fglob
->fg_lflags
& FG_CONFINED
));
5909 /* DTYPE_KQUEUE, DTYPE_FSEVENTS, DTYPE_PSXSEM */
5916 fileproc_alloc_init(__unused
void *arg
)
5918 struct fileproc
*fp
;
5920 MALLOC_ZONE(fp
, struct fileproc
*, sizeof (*fp
), M_FILEPROC
, M_WAITOK
);
5922 bzero(fp
, sizeof (*fp
));
5928 fileproc_free(struct fileproc
*fp
)
5930 switch (FILEPROC_TYPE(fp
)) {
5932 FREE_ZONE(fp
, sizeof (*fp
), M_FILEPROC
);
5935 guarded_fileproc_free(fp
);
5938 panic("%s: corrupt fp %p flags %x", __func__
, fp
, fp
->f_flags
);