2 * Copyright (c) 2000-2016 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1982, 1986, 1989, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)kern_descrip.c 8.8 (Berkeley) 2/14/95
69 * NOTICE: This file was modified by SPARTA, Inc. in 2006 to introduce
70 * support for mandatory and extensible security protections. This notice
71 * is included in support of clause 2.2 (b) of the Apple Public License,
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/filedesc.h>
78 #include <sys/kernel.h>
79 #include <sys/vnode_internal.h>
80 #include <sys/proc_internal.h>
81 #include <sys/kauth.h>
82 #include <sys/file_internal.h>
83 #include <sys/guarded.h>
85 #include <sys/socket.h>
86 #include <sys/socketvar.h>
88 #include <sys/ioctl.h>
89 #include <sys/fcntl.h>
90 #include <sys/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 <sys/cprotect.h>
110 #include <kern/kern_types.h>
111 #include <kern/kalloc.h>
112 #include <kern/waitq.h>
113 #include <libkern/OSAtomic.h>
115 #include <sys/ubc_internal.h>
117 #include <kern/ipc_misc.h>
118 #include <vm/vm_protos.h>
120 #include <mach/mach_port.h>
123 kern_return_t
ipc_object_copyin(ipc_space_t
, mach_port_name_t
,
124 mach_msg_type_name_t
, ipc_port_t
*);
125 void ipc_port_release_send(ipc_port_t
);
130 static int finishdup(proc_t p
,
131 struct filedesc
*fdp
, int old
, int new, int flags
, int32_t *retval
);
133 int falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
, int locked
);
134 void fg_drop(struct fileproc
* fp
);
135 void fg_free(struct fileglob
*fg
);
136 void fg_ref(struct fileproc
* fp
);
137 void fileport_releasefg(struct fileglob
*fg
);
139 /* flags for close_internal_locked */
140 #define FD_DUP2RESV 1
142 /* We don't want these exported */
145 int unlink1(vfs_context_t
, vnode_t
, user_addr_t
, enum uio_seg
, int);
147 static void _fdrelse(struct proc
* p
, int fd
);
150 extern void file_lock_init(void);
152 extern kauth_scope_t kauth_scope_fileop
;
154 /* Conflict wait queue for when selects collide (opaque type) */
155 extern struct waitq select_conflict_queue
;
157 #define f_flag f_fglob->fg_flag
158 #define f_type f_fglob->fg_ops->fo_type
159 #define f_msgcount f_fglob->fg_msgcount
160 #define f_cred f_fglob->fg_cred
161 #define f_ops f_fglob->fg_ops
162 #define f_offset f_fglob->fg_offset
163 #define f_data f_fglob->fg_data
164 #define CHECK_ADD_OVERFLOW_INT64L(x, y) \
165 (((((x) > 0) && ((y) > 0) && ((x) > LLONG_MAX - (y))) || \
166 (((x) < 0) && ((y) < 0) && ((x) < LLONG_MIN - (y)))) \
169 * Descriptor management.
171 struct fmsglist fmsghead
; /* head of list of open files */
172 struct fmsglist fmsg_ithead
; /* head of list of open files */
173 int nfiles
; /* actual number of open files */
176 lck_grp_attr_t
* file_lck_grp_attr
;
177 lck_grp_t
* file_lck_grp
;
178 lck_attr_t
* file_lck_attr
;
180 lck_mtx_t
* uipc_lock
;
184 * check_file_seek_range
186 * Description: Checks if seek offsets are in the range of 0 to LLONG_MAX.
188 * Parameters: fl Flock structure.
189 * cur_file_offset Current offset in the file.
191 * Returns: 0 on Success.
192 * EOVERFLOW on overflow.
193 * EINVAL on offset less than zero.
197 check_file_seek_range(struct flock
*fl
, off_t cur_file_offset
)
199 if (fl
->l_whence
== SEEK_CUR
) {
200 /* Check if the start marker is beyond LLONG_MAX. */
201 if (CHECK_ADD_OVERFLOW_INT64L(fl
->l_start
, cur_file_offset
)) {
202 /* Check if start marker is negative */
203 if (fl
->l_start
< 0) {
208 /* Check if the start marker is negative. */
209 if (fl
->l_start
+ cur_file_offset
< 0) {
212 /* Check if end marker is beyond LLONG_MAX. */
213 if ((fl
->l_len
> 0) && (CHECK_ADD_OVERFLOW_INT64L(fl
->l_start
+
214 cur_file_offset
, fl
->l_len
- 1))) {
217 /* Check if the end marker is negative. */
218 if ((fl
->l_len
<= 0) && (fl
->l_start
+ cur_file_offset
+
222 } else if (fl
->l_whence
== SEEK_SET
) {
223 /* Check if the start marker is negative. */
224 if (fl
->l_start
< 0) {
227 /* Check if the end marker is beyond LLONG_MAX. */
228 if ((fl
->l_len
> 0) &&
229 CHECK_ADD_OVERFLOW_INT64L(fl
->l_start
, fl
->l_len
- 1)) {
232 /* Check if the end marker is negative. */
233 if ((fl
->l_len
< 0) && fl
->l_start
+ fl
->l_len
< 0) {
244 * Description: Initialize the file lock group and the uipc and flist locks
250 * Notes: Called at system startup from bsd_init().
255 /* allocate file lock group attribute and group */
256 file_lck_grp_attr
= lck_grp_attr_alloc_init();
258 file_lck_grp
= lck_grp_alloc_init("file", file_lck_grp_attr
);
260 /* Allocate file lock attribute */
261 file_lck_attr
= lck_attr_alloc_init();
263 uipc_lock
= lck_mtx_alloc_init(file_lck_grp
, file_lck_attr
);
268 * proc_fdlock, proc_fdlock_spin
270 * Description: Lock to control access to the per process struct fileproc
271 * and struct filedesc
273 * Parameters: p Process to take the lock on
277 * Notes: The lock is initialized in forkproc() and destroyed in
278 * reap_child_process().
281 proc_fdlock(proc_t p
)
283 lck_mtx_lock(&p
->p_fdmlock
);
287 proc_fdlock_spin(proc_t p
)
289 lck_mtx_lock_spin(&p
->p_fdmlock
);
293 proc_fdlock_assert(proc_t p
, int assertflags
)
295 lck_mtx_assert(&p
->p_fdmlock
, assertflags
);
302 * Description: Unlock the lock previously locked by a call to proc_fdlock()
304 * Parameters: p Process to drop the lock on
309 proc_fdunlock(proc_t p
)
311 lck_mtx_unlock(&p
->p_fdmlock
);
316 * System calls on descriptors.
323 * Description: Returns the per process maximum size of the descriptor table
325 * Parameters: p Process being queried
326 * retval Pointer to the call return area
331 * *retval (modified) Size of dtable
334 getdtablesize(proc_t p
, __unused
struct getdtablesize_args
*uap
, int32_t *retval
)
337 *retval
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
345 procfdtbl_reservefd(struct proc
* p
, int fd
)
347 p
->p_fd
->fd_ofiles
[fd
] = NULL
;
348 p
->p_fd
->fd_ofileflags
[fd
] |= UF_RESERVED
;
352 procfdtbl_markclosefd(struct proc
* p
, int fd
)
354 p
->p_fd
->fd_ofileflags
[fd
] |= (UF_RESERVED
| UF_CLOSING
);
358 procfdtbl_releasefd(struct proc
* p
, int fd
, struct fileproc
* fp
)
361 p
->p_fd
->fd_ofiles
[fd
] = fp
;
362 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESERVED
;
363 if ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
) == UF_RESVWAIT
) {
364 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESVWAIT
;
370 procfdtbl_waitfd(struct proc
* p
, int fd
)
372 p
->p_fd
->fd_ofileflags
[fd
] |= UF_RESVWAIT
;
373 msleep(&p
->p_fd
, &p
->p_fdmlock
, PRIBIO
, "ftbl_waitfd", NULL
);
378 procfdtbl_clearfd(struct proc
* p
, int fd
)
382 waiting
= (p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
);
383 p
->p_fd
->fd_ofiles
[fd
] = NULL
;
384 p
->p_fd
->fd_ofileflags
[fd
] = 0;
385 if ( waiting
== UF_RESVWAIT
) {
393 * Description: Inline utility function to free an fd in a filedesc
395 * Parameters: fdp Pointer to filedesc fd lies in
397 * reserv fd should be reserved
401 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
405 _fdrelse(struct proc
* p
, int fd
)
407 struct filedesc
*fdp
= p
->p_fd
;
410 if (fd
< fdp
->fd_freefile
)
411 fdp
->fd_freefile
= fd
;
413 if (fd
> fdp
->fd_lastfile
)
414 panic("fdrelse: fd_lastfile inconsistent");
416 procfdtbl_clearfd(p
, fd
);
418 while ((nfd
= fdp
->fd_lastfile
) > 0 &&
419 fdp
->fd_ofiles
[nfd
] == NULL
&&
420 !(fdp
->fd_ofileflags
[nfd
] & UF_RESERVED
))
442 char uio_buf
[ UIO_SIZEOF(1) ];
443 struct vfs_context context
= *(vfs_context_current());
444 bool wrote_some
= false;
448 error
= fp_lookup(p
, fd
, &fp
, 0);
452 if (fp
->f_type
!= DTYPE_VNODE
&& fp
->f_type
!= DTYPE_PIPE
&& fp
->f_type
!= DTYPE_SOCKET
) {
456 if (rw
== UIO_WRITE
&& !(fp
->f_flag
& FWRITE
)) {
461 if (rw
== UIO_READ
&& !(fp
->f_flag
& FREAD
)) {
466 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
468 if (UIO_SEG_IS_USER_SPACE(segflg
))
469 spacetype
= proc_is64bit(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
471 spacetype
= UIO_SYSSPACE
;
473 auio
= uio_createwithbuffer(1, offset
, spacetype
, rw
, &uio_buf
[0], sizeof(uio_buf
));
475 uio_addiov(auio
, base
, len
);
477 if ( !(io_flg
& IO_APPEND
))
480 if (rw
== UIO_WRITE
) {
481 user_ssize_t orig_resid
= uio_resid(auio
);
482 error
= fo_write(fp
, auio
, flags
, &context
);
483 wrote_some
= uio_resid(auio
) < orig_resid
;
485 error
= fo_read(fp
, auio
, flags
, &context
);
488 *aresid
= uio_resid(auio
);
490 if (uio_resid(auio
) && error
== 0)
495 fp_drop_written(p
, fd
, fp
);
497 fp_drop(p
, fd
, fp
, 0);
507 * Description: Duplicate a file descriptor.
509 * Parameters: p Process performing the dup
510 * uap->fd The fd to dup
511 * retval Pointer to the call return area
517 * *retval (modified) The new descriptor
520 dup(proc_t p
, struct dup_args
*uap
, int32_t *retval
)
522 struct filedesc
*fdp
= p
->p_fd
;
528 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
532 if (FP_ISGUARDED(fp
, GUARD_DUP
)) {
533 error
= fp_guard_exception(p
, old
, fp
, kGUARD_EXC_DUP
);
534 (void) fp_drop(p
, old
, fp
, 1);
538 if ( (error
= fdalloc(p
, 0, &new)) ) {
539 fp_drop(p
, old
, fp
, 1);
543 error
= finishdup(p
, fdp
, old
, new, 0, retval
);
544 fp_drop(p
, old
, fp
, 1);
547 if (ENTR_SHOULDTRACE
&& fp
->f_type
== DTYPE_SOCKET
) {
548 KERNEL_ENERGYTRACE(kEnTrActKernSocket
, DBG_FUNC_START
,
549 new, 0, (int64_t)VM_KERNEL_ADDRPERM(fp
->f_data
));
558 * Description: Duplicate a file descriptor to a particular value.
560 * Parameters: p Process performing the dup
561 * uap->from The fd to dup
562 * uap->to The fd to dup it to
563 * retval Pointer to the call return area
569 * *retval (modified) The new descriptor
572 dup2(proc_t p
, struct dup2_args
*uap
, int32_t *retval
)
574 struct filedesc
*fdp
= p
->p_fd
;
575 int old
= uap
->from
, new = uap
->to
;
577 struct fileproc
*fp
, *nfp
;
582 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
586 if (FP_ISGUARDED(fp
, GUARD_DUP
)) {
587 error
= fp_guard_exception(p
, old
, fp
, kGUARD_EXC_DUP
);
588 (void) fp_drop(p
, old
, fp
, 1);
593 (rlim_t
)new >= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
595 fp_drop(p
, old
, fp
, 1);
600 fp_drop(p
, old
, fp
, 1);
605 if (new < 0 || new >= fdp
->fd_nfiles
) {
606 if ( (error
= fdalloc(p
, new, &i
)) ) {
607 fp_drop(p
, old
, fp
, 1);
617 while ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == UF_RESERVED
) {
618 fp_drop(p
, old
, fp
, 1);
619 procfdtbl_waitfd(p
, new);
621 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
626 if ((fdp
->fd_ofiles
[new] != NULL
) &&
627 ((error
= fp_lookup(p
, new, &nfp
, 1)) == 0)) {
628 fp_drop(p
, old
, fp
, 1);
629 if (FP_ISGUARDED(nfp
, GUARD_CLOSE
)) {
630 error
= fp_guard_exception(p
,
631 new, nfp
, kGUARD_EXC_CLOSE
);
632 (void) fp_drop(p
, new, nfp
, 1);
636 (void)close_internal_locked(p
, new, nfp
, FD_DUP2RESV
);
638 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
640 procfdtbl_clearfd(p
, new);
644 if (fdp
->fd_ofiles
[new] != NULL
)
645 panic("dup2: no ref on fileproc %d", new);
647 procfdtbl_reservefd(p
, new);
651 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
656 if (fdp
->fd_ofiles
[new] != 0)
657 panic("dup2: overwriting fd_ofiles with new %d", new);
658 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0)
659 panic("dup2: unreserved fileflags with new %d", new);
661 error
= finishdup(p
, fdp
, old
, new, 0, retval
);
662 fp_drop(p
, old
, fp
, 1);
672 * Description: The file control system call.
674 * Parameters: p Process performing the fcntl
675 * uap->fd The fd to operate against
676 * uap->cmd The command to perform
677 * uap->arg Pointer to the command argument
678 * retval Pointer to the call return area
681 * !0 Errno (see fcntl_nocancel)
684 * *retval (modified) fcntl return value (if any)
686 * Notes: This system call differs from fcntl_nocancel() in that it
687 * tests for cancellation prior to performing a potentially
688 * blocking operation.
691 fcntl(proc_t p
, struct fcntl_args
*uap
, int32_t *retval
)
693 __pthread_testcancel(1);
694 return(fcntl_nocancel(p
, (struct fcntl_nocancel_args
*)uap
, retval
));
701 * Description: A non-cancel-testing file control system call.
703 * Parameters: p Process performing the fcntl
704 * uap->fd The fd to operate against
705 * uap->cmd The command to perform
706 * uap->arg Pointer to the command argument
707 * retval Pointer to the call return area
711 * fp_lookup:EBADF Bad file descriptor
723 * vnode_getwithref:???
731 * vnode_getwithref:???
738 * vnode_getwithref:???
740 * [F_SETSIZE,F_RDADVISE]
743 * vnode_getwithref:???
744 * [F_RDAHEAD,F_NOCACHE]
746 * vnode_getwithref:???
750 * *retval (modified) fcntl return value (if any)
753 fcntl_nocancel(proc_t p
, struct fcntl_nocancel_args
*uap
, int32_t *retval
)
756 struct filedesc
*fdp
= p
->p_fd
;
759 struct vnode
*vp
= NULLVP
; /* for AUDIT_ARG() at end */
760 int i
, tmp
, error
, error2
, flg
= 0;
762 struct flocktimeout fltimeout
;
763 struct timespec
*timeout
= NULL
;
764 struct vfs_context context
;
772 AUDIT_ARG(fd
, uap
->fd
);
773 AUDIT_ARG(cmd
, uap
->cmd
);
776 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
780 context
.vc_thread
= current_thread();
781 context
.vc_ucred
= fp
->f_cred
;
783 is64bit
= proc_is64bit(p
);
789 * Since the arg parameter is defined as a long but may be
790 * either a long or a pointer we must take care to handle
791 * sign extension issues. Our sys call munger will sign
792 * extend a long when we are called from a 32-bit process.
793 * Since we can never have an address greater than 32-bits
794 * from a 32-bit process we lop off the top 32-bits to avoid
795 * getting the wrong address
797 argp
= CAST_USER_ADDR_T((uint32_t)uap
->arg
);
800 pop
= &fdp
->fd_ofileflags
[fd
];
803 error
= mac_file_check_fcntl(proc_ucred(p
), fp
->f_fglob
, uap
->cmd
,
812 case F_DUPFD_CLOEXEC
:
813 if (FP_ISGUARDED(fp
, GUARD_DUP
)) {
814 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_DUP
);
817 newmin
= CAST_DOWN_EXPLICIT(int, uap
->arg
); /* arg is an int, so we won't lose bits */
818 AUDIT_ARG(value32
, newmin
);
819 if ((u_int
)newmin
>= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
820 newmin
>= maxfiles
) {
824 if ( (error
= fdalloc(p
, newmin
, &i
)) )
826 error
= finishdup(p
, fdp
, fd
, i
,
827 uap
->cmd
== F_DUPFD_CLOEXEC
? UF_EXCLOSE
: 0, retval
);
831 *retval
= (*pop
& UF_EXCLOSE
)? FD_CLOEXEC
: 0;
836 AUDIT_ARG(value32
, uap
->arg
);
837 if (uap
->arg
& FD_CLOEXEC
)
840 if (FILEPROC_TYPE(fp
) == FTYPE_GUARDED
) {
841 error
= fp_guard_exception(p
,
842 fd
, fp
, kGUARD_EXC_NOCLOEXEC
);
851 *retval
= OFLAGS(fp
->f_flag
);
856 fp
->f_flag
&= ~FCNTLFLAGS
;
857 tmp
= CAST_DOWN_EXPLICIT(int, uap
->arg
); /* arg is an int, so we won't lose bits */
858 AUDIT_ARG(value32
, tmp
);
859 fp
->f_flag
|= FFLAGS(tmp
) & FCNTLFLAGS
;
860 tmp
= fp
->f_flag
& FNONBLOCK
;
861 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
864 tmp
= fp
->f_flag
& FASYNC
;
865 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, &context
);
868 fp
->f_flag
&= ~FNONBLOCK
;
870 (void)fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
874 if (fp
->f_type
== DTYPE_SOCKET
) {
875 *retval
= ((struct socket
*)fp
->f_data
)->so_pgid
;
879 error
= fo_ioctl(fp
, (int)TIOCGPGRP
, (caddr_t
)retval
, &context
);
884 tmp
= CAST_DOWN_EXPLICIT(pid_t
, uap
->arg
); /* arg is an int, so we won't lose bits */
885 AUDIT_ARG(value32
, tmp
);
886 if (fp
->f_type
== DTYPE_SOCKET
) {
887 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
891 if (fp
->f_type
== DTYPE_PIPE
) {
892 error
= fo_ioctl(fp
, TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
899 proc_t p1
= proc_find(tmp
);
904 tmp
= (int)p1
->p_pgrpid
;
907 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
911 tmp
= CAST_DOWN_EXPLICIT(int, uap
->arg
);
912 if (fp
->f_type
== DTYPE_SOCKET
) {
914 error
= sock_setsockopt((struct socket
*)fp
->f_data
,
915 SOL_SOCKET
, SO_NOSIGPIPE
, &tmp
, sizeof (tmp
));
920 struct fileglob
*fg
= fp
->f_fglob
;
922 lck_mtx_lock_spin(&fg
->fg_lock
);
924 fg
->fg_lflags
|= FG_NOSIGPIPE
;
926 fg
->fg_lflags
&= FG_NOSIGPIPE
;
927 lck_mtx_unlock(&fg
->fg_lock
);
933 if (fp
->f_type
== DTYPE_SOCKET
) {
935 int retsize
= sizeof (*retval
);
936 error
= sock_getsockopt((struct socket
*)fp
->f_data
,
937 SOL_SOCKET
, SO_NOSIGPIPE
, retval
, &retsize
);
942 *retval
= (fp
->f_fglob
->fg_lflags
& FG_NOSIGPIPE
) ?
950 * If this is the only reference to this fglob in the process
951 * and it's already marked as close-on-fork then mark it as
952 * (immutably) "confined" i.e. any fd that points to it will
953 * forever be close-on-fork, and attempts to use an IPC
954 * mechanism to move the descriptor elsewhere will fail.
956 if (CAST_DOWN_EXPLICIT(int, uap
->arg
)) {
957 struct fileglob
*fg
= fp
->f_fglob
;
959 lck_mtx_lock_spin(&fg
->fg_lock
);
960 if (fg
->fg_lflags
& FG_CONFINED
)
962 else if (1 != fg
->fg_count
)
963 error
= EAGAIN
; /* go close the dup .. */
964 else if (UF_FORKCLOSE
== (*pop
& UF_FORKCLOSE
)) {
965 fg
->fg_lflags
|= FG_CONFINED
;
968 error
= EBADF
; /* open without O_CLOFORK? */
969 lck_mtx_unlock(&fg
->fg_lock
);
972 * Other subsystems may have built on the immutability
973 * of FG_CONFINED; clearing it may be tricky.
975 error
= EPERM
; /* immutable */
980 *retval
= (fp
->f_fglob
->fg_lflags
& FG_CONFINED
) ? 1 : 0;
984 case F_SETLKWTIMEOUT
:
986 case F_OFD_SETLKWTIMEOUT
:
989 /* Fall into F_SETLK */
993 if (fp
->f_type
!= DTYPE_VNODE
) {
997 vp
= (struct vnode
*)fp
->f_data
;
1000 offset
= fp
->f_offset
;
1003 /* Copy in the lock structure */
1004 if (F_SETLKWTIMEOUT
== uap
->cmd
||
1005 F_OFD_SETLKWTIMEOUT
== uap
->cmd
) {
1006 error
= copyin(argp
, (caddr_t
) &fltimeout
, sizeof(fltimeout
));
1011 timeout
= &fltimeout
.timeout
;
1013 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
1019 /* Check starting byte and ending byte for EOVERFLOW in SEEK_CUR */
1020 /* and ending byte for EOVERFLOW in SEEK_SET */
1021 error
= check_file_seek_range(&fl
, offset
);
1026 if ( (error
= vnode_getwithref(vp
)) ) {
1029 if (fl
.l_whence
== SEEK_CUR
)
1030 fl
.l_start
+= offset
;
1033 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
1036 (void)vnode_put(vp
);
1043 case F_OFD_SETLKWTIMEOUT
:
1045 switch (fl
.l_type
) {
1047 if ((fflag
& FREAD
) == 0) {
1051 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1052 F_SETLK
, &fl
, flg
, &context
, timeout
);
1055 if ((fflag
& FWRITE
) == 0) {
1059 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1060 F_SETLK
, &fl
, flg
, &context
, timeout
);
1063 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1064 F_UNLCK
, &fl
, F_OFD_LOCK
, &context
,
1072 (F_RDLCK
== fl
.l_type
|| F_WRLCK
== fl
.l_type
)) {
1073 struct fileglob
*fg
= fp
->f_fglob
;
1076 * arrange F_UNLCK on last close (once
1077 * set, FG_HAS_OFDLOCK is immutable)
1079 if ((fg
->fg_lflags
& FG_HAS_OFDLOCK
) == 0) {
1080 lck_mtx_lock_spin(&fg
->fg_lock
);
1081 fg
->fg_lflags
|= FG_HAS_OFDLOCK
;
1082 lck_mtx_unlock(&fg
->fg_lock
);
1088 switch (fl
.l_type
) {
1090 if ((fflag
& FREAD
) == 0) {
1094 // XXX UInt32 unsafe for LP64 kernel
1095 OSBitOrAtomic(P_LADVLOCK
, &p
->p_ladvflag
);
1096 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1097 F_SETLK
, &fl
, flg
, &context
, timeout
);
1100 if ((fflag
& FWRITE
) == 0) {
1104 // XXX UInt32 unsafe for LP64 kernel
1105 OSBitOrAtomic(P_LADVLOCK
, &p
->p_ladvflag
);
1106 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1107 F_SETLK
, &fl
, flg
, &context
, timeout
);
1110 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1111 F_UNLCK
, &fl
, F_POSIX
, &context
, timeout
);
1119 (void) vnode_put(vp
);
1124 if (fp
->f_type
!= DTYPE_VNODE
) {
1128 vp
= (struct vnode
*)fp
->f_data
;
1130 offset
= fp
->f_offset
;
1133 /* Copy in the lock structure */
1134 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
1138 /* Check starting byte and ending byte for EOVERFLOW in SEEK_CUR */
1139 /* and ending byte for EOVERFLOW in SEEK_SET */
1140 error
= check_file_seek_range(&fl
, offset
);
1145 if ((fl
.l_whence
== SEEK_SET
) && (fl
.l_start
< 0)) {
1150 switch (fl
.l_type
) {
1160 switch (fl
.l_whence
) {
1170 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1171 if (fl
.l_whence
== SEEK_CUR
)
1172 fl
.l_start
+= offset
;
1175 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
1181 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1182 F_GETLK
, &fl
, F_OFD_LOCK
, &context
, NULL
);
1184 case F_OFD_GETLKPID
:
1185 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1186 F_GETLKPID
, &fl
, F_OFD_LOCK
, &context
, NULL
);
1189 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1190 uap
->cmd
, &fl
, F_POSIX
, &context
, NULL
);
1194 (void)vnode_put(vp
);
1197 error
= copyout((caddr_t
)&fl
, argp
, sizeof(fl
));
1201 case F_PREALLOCATE
: {
1202 fstore_t alloc_struct
; /* structure for allocate command */
1203 u_int32_t alloc_flags
= 0;
1205 if (fp
->f_type
!= DTYPE_VNODE
) {
1210 vp
= (struct vnode
*)fp
->f_data
;
1213 /* make sure that we have write permission */
1214 if ((fp
->f_flag
& FWRITE
) == 0) {
1219 error
= copyin(argp
, (caddr_t
)&alloc_struct
, sizeof(alloc_struct
));
1223 /* now set the space allocated to 0 */
1224 alloc_struct
.fst_bytesalloc
= 0;
1227 * Do some simple parameter checking
1230 /* set up the flags */
1232 alloc_flags
|= PREALLOCATE
;
1234 if (alloc_struct
.fst_flags
& F_ALLOCATECONTIG
)
1235 alloc_flags
|= ALLOCATECONTIG
;
1237 if (alloc_struct
.fst_flags
& F_ALLOCATEALL
)
1238 alloc_flags
|= ALLOCATEALL
;
1241 * Do any position mode specific stuff. The only
1242 * position mode supported now is PEOFPOSMODE
1245 switch (alloc_struct
.fst_posmode
) {
1248 if (alloc_struct
.fst_offset
!= 0) {
1253 alloc_flags
|= ALLOCATEFROMPEOF
;
1257 if (alloc_struct
.fst_offset
<= 0) {
1262 alloc_flags
|= ALLOCATEFROMVOL
;
1270 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1272 * call allocate to get the space
1274 error
= VNOP_ALLOCATE(vp
,alloc_struct
.fst_length
,alloc_flags
,
1275 &alloc_struct
.fst_bytesalloc
, alloc_struct
.fst_offset
,
1277 (void)vnode_put(vp
);
1279 error2
= copyout((caddr_t
)&alloc_struct
, argp
, sizeof(alloc_struct
));
1288 if (fp
->f_type
!= DTYPE_VNODE
) {
1292 vp
= (struct vnode
*)fp
->f_data
;
1295 error
= copyin(argp
, (caddr_t
)&offset
, sizeof (off_t
));
1298 AUDIT_ARG(value64
, offset
);
1300 error
= vnode_getwithref(vp
);
1305 error
= mac_vnode_check_truncate(&context
,
1306 fp
->f_fglob
->fg_cred
, vp
);
1308 (void)vnode_put(vp
);
1313 * Make sure that we are root. Growing a file
1314 * without zero filling the data is a security hole
1315 * root would have access anyway so we'll allow it
1317 if (!kauth_cred_issuser(kauth_cred_get())) {
1323 error
= vnode_setsize(vp
, offset
, IO_NOZEROFILL
,
1328 mac_vnode_notify_truncate(&context
, fp
->f_fglob
->fg_cred
, vp
);
1332 (void)vnode_put(vp
);
1336 if (fp
->f_type
!= DTYPE_VNODE
) {
1341 fp
->f_fglob
->fg_flag
&= ~FNORDAHEAD
;
1343 fp
->f_fglob
->fg_flag
|= FNORDAHEAD
;
1348 if (fp
->f_type
!= DTYPE_VNODE
) {
1353 fp
->f_fglob
->fg_flag
|= FNOCACHE
;
1355 fp
->f_fglob
->fg_flag
&= ~FNOCACHE
;
1360 if (fp
->f_type
!= DTYPE_VNODE
) {
1365 fp
->f_fglob
->fg_flag
|= FNODIRECT
;
1367 fp
->f_fglob
->fg_flag
&= ~FNODIRECT
;
1371 case F_SINGLE_WRITER
:
1372 if (fp
->f_type
!= DTYPE_VNODE
) {
1377 fp
->f_fglob
->fg_flag
|= FSINGLE_WRITER
;
1379 fp
->f_fglob
->fg_flag
&= ~FSINGLE_WRITER
;
1383 case F_GLOBAL_NOCACHE
:
1384 if (fp
->f_type
!= DTYPE_VNODE
) {
1388 vp
= (struct vnode
*)fp
->f_data
;
1391 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1393 *retval
= vnode_isnocache(vp
);
1396 vnode_setnocache(vp
);
1398 vnode_clearnocache(vp
);
1400 (void)vnode_put(vp
);
1404 case F_CHECK_OPENEVT
:
1405 if (fp
->f_type
!= DTYPE_VNODE
) {
1409 vp
= (struct vnode
*)fp
->f_data
;
1412 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1414 *retval
= vnode_is_openevt(vp
);
1417 vnode_set_openevt(vp
);
1419 vnode_clear_openevt(vp
);
1421 (void)vnode_put(vp
);
1426 struct radvisory ra_struct
;
1428 if (fp
->f_type
!= DTYPE_VNODE
) {
1432 vp
= (struct vnode
*)fp
->f_data
;
1435 if ( (error
= copyin(argp
, (caddr_t
)&ra_struct
, sizeof(ra_struct
))) )
1437 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1438 error
= VNOP_IOCTL(vp
, F_RDADVISE
, (caddr_t
)&ra_struct
, 0, &context
);
1440 (void)vnode_put(vp
);
1447 if (fp
->f_type
!= DTYPE_VNODE
) {
1451 vp
= (struct vnode
*)fp
->f_data
;
1454 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1455 error
= cluster_push(vp
, 0);
1457 (void)vnode_put(vp
);
1462 case F_LOG2PHYS_EXT
: {
1463 struct log2phys l2p_struct
; /* structure for allocate command */
1466 off_t file_offset
= 0;
1470 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1471 error
= copyin(argp
, (caddr_t
)&l2p_struct
, sizeof(l2p_struct
));
1474 file_offset
= l2p_struct
.l2p_devoffset
;
1476 file_offset
= fp
->f_offset
;
1478 if (fp
->f_type
!= DTYPE_VNODE
) {
1482 vp
= (struct vnode
*)fp
->f_data
;
1484 if ( (error
= vnode_getwithref(vp
)) ) {
1487 error
= VNOP_OFFTOBLK(vp
, file_offset
, &lbn
);
1489 (void)vnode_put(vp
);
1492 error
= VNOP_BLKTOOFF(vp
, lbn
, &offset
);
1494 (void)vnode_put(vp
);
1497 devBlockSize
= vfs_devblocksize(vnode_mount(vp
));
1498 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1499 if (l2p_struct
.l2p_contigbytes
< 0) {
1505 a_size
= MIN((uint64_t)l2p_struct
.l2p_contigbytes
, SIZE_MAX
);
1507 a_size
= devBlockSize
;
1510 error
= VNOP_BLOCKMAP(vp
, offset
, a_size
, &bn
, &run
, NULL
, 0, &context
);
1512 (void)vnode_put(vp
);
1515 l2p_struct
.l2p_flags
= 0; /* for now */
1516 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1517 l2p_struct
.l2p_contigbytes
= run
- (file_offset
- offset
);
1519 l2p_struct
.l2p_contigbytes
= 0; /* for now */
1523 * The block number being -1 suggests that the file offset is not backed
1524 * by any real blocks on-disk. As a result, just let it be passed back up wholesale.
1527 /* Don't multiply it by the block size */
1528 l2p_struct
.l2p_devoffset
= bn
;
1531 l2p_struct
.l2p_devoffset
= bn
* devBlockSize
;
1532 l2p_struct
.l2p_devoffset
+= file_offset
- offset
;
1534 error
= copyout((caddr_t
)&l2p_struct
, argp
, sizeof(l2p_struct
));
1542 if (fp
->f_type
!= DTYPE_VNODE
) {
1546 vp
= (struct vnode
*)fp
->f_data
;
1549 pathlen
= MAXPATHLEN
;
1550 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
1551 if (pathbufp
== NULL
) {
1555 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1556 error
= vn_getpath(vp
, pathbufp
, &pathlen
);
1557 (void)vnode_put(vp
);
1560 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
1562 FREE(pathbufp
, M_TEMP
);
1566 case F_PATHPKG_CHECK
: {
1570 if (fp
->f_type
!= DTYPE_VNODE
) {
1574 vp
= (struct vnode
*)fp
->f_data
;
1577 pathlen
= MAXPATHLEN
;
1578 pathbufp
= kalloc(MAXPATHLEN
);
1580 if ( (error
= copyinstr(argp
, pathbufp
, MAXPATHLEN
, &pathlen
)) == 0 ) {
1581 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1582 AUDIT_ARG(text
, pathbufp
);
1583 error
= vn_path_package_check(vp
, pathbufp
, pathlen
, retval
);
1585 (void)vnode_put(vp
);
1588 kfree(pathbufp
, MAXPATHLEN
);
1592 case F_CHKCLEAN
: // used by regression tests to see if all dirty pages got cleaned by fsync()
1593 case F_FULLFSYNC
: // fsync + flush the journal + DKIOCSYNCHRONIZE
1594 case F_BARRIERFSYNC
: // fsync + barrier
1595 case F_FREEZE_FS
: // freeze all other fs operations for the fs of this fd
1596 case F_THAW_FS
: { // thaw all frozen fs operations for the fs of this fd
1597 if (fp
->f_type
!= DTYPE_VNODE
) {
1601 vp
= (struct vnode
*)fp
->f_data
;
1604 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1605 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
1607 (void)vnode_put(vp
);
1613 * SPI (private) for opening a file starting from a dir fd
1616 struct user_fopenfrom fopen
;
1617 struct vnode_attr va
;
1618 struct nameidata nd
;
1621 /* Check if this isn't a valid file descriptor */
1622 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1623 (fp
->f_flag
& FREAD
) == 0) {
1627 vp
= (struct vnode
*)fp
->f_data
;
1630 if (vnode_getwithref(vp
)) {
1635 /* Only valid for directories */
1636 if (vp
->v_type
!= VDIR
) {
1642 /* Get flags, mode and pathname arguments. */
1643 if (IS_64BIT_PROCESS(p
)) {
1644 error
= copyin(argp
, &fopen
, sizeof(fopen
));
1646 struct user32_fopenfrom fopen32
;
1648 error
= copyin(argp
, &fopen32
, sizeof(fopen32
));
1649 fopen
.o_flags
= fopen32
.o_flags
;
1650 fopen
.o_mode
= fopen32
.o_mode
;
1651 fopen
.o_pathname
= CAST_USER_ADDR_T(fopen32
.o_pathname
);
1657 AUDIT_ARG(fflags
, fopen
.o_flags
);
1658 AUDIT_ARG(mode
, fopen
.o_mode
);
1660 /* Mask off all but regular access permissions */
1661 cmode
= ((fopen
.o_mode
&~ fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
1662 VATTR_SET(&va
, va_mode
, cmode
& ACCESSPERMS
);
1664 /* Start the lookup relative to the file descriptor's vnode. */
1665 NDINIT(&nd
, LOOKUP
, OP_OPEN
, USEDVP
| FOLLOW
| AUDITVNPATH1
, UIO_USERSPACE
,
1666 fopen
.o_pathname
, &context
);
1669 error
= open1(&context
, &nd
, fopen
.o_flags
, &va
,
1670 fileproc_alloc_init
, NULL
, retval
);
1676 * SPI (private) for unlinking a file starting from a dir fd
1678 case F_UNLINKFROM
: {
1679 user_addr_t pathname
;
1681 /* Check if this isn't a valid file descriptor */
1682 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1683 (fp
->f_flag
& FREAD
) == 0) {
1687 vp
= (struct vnode
*)fp
->f_data
;
1690 if (vnode_getwithref(vp
)) {
1695 /* Only valid for directories */
1696 if (vp
->v_type
!= VDIR
) {
1702 /* Get flags, mode and pathname arguments. */
1703 if (IS_64BIT_PROCESS(p
)) {
1704 pathname
= (user_addr_t
)argp
;
1706 pathname
= CAST_USER_ADDR_T(argp
);
1709 /* Start the lookup relative to the file descriptor's vnode. */
1710 error
= unlink1(&context
, vp
, pathname
, UIO_USERSPACE
, 0);
1719 case F_ADDFILESIGS_FOR_DYLD_SIM
:
1720 case F_ADDFILESIGS_RETURN
:
1722 struct cs_blob
*blob
= NULL
;
1723 struct user_fsignatures fs
;
1725 vm_offset_t kernel_blob_addr
;
1726 vm_size_t kernel_blob_size
;
1727 int blob_add_flags
= 0;
1729 if (fp
->f_type
!= DTYPE_VNODE
) {
1733 vp
= (struct vnode
*)fp
->f_data
;
1736 if (uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
1737 blob_add_flags
|= MAC_VNODE_CHECK_DYLD_SIM
;
1738 if ((p
->p_csflags
& CS_KILL
) == 0) {
1740 p
->p_csflags
|= CS_KILL
;
1745 error
= vnode_getwithref(vp
);
1749 if (IS_64BIT_PROCESS(p
)) {
1750 error
= copyin(argp
, &fs
, sizeof (fs
));
1752 struct user32_fsignatures fs32
;
1754 error
= copyin(argp
, &fs32
, sizeof (fs32
));
1755 fs
.fs_file_start
= fs32
.fs_file_start
;
1756 fs
.fs_blob_start
= CAST_USER_ADDR_T(fs32
.fs_blob_start
);
1757 fs
.fs_blob_size
= fs32
.fs_blob_size
;
1766 * First check if we have something loaded a this offset
1768 blob
= ubc_cs_blob_get(vp
, CPU_TYPE_ANY
, fs
.fs_file_start
);
1771 /* If this is for dyld_sim revalidate the blob */
1772 if (uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
1773 error
= ubc_cs_blob_revalidate(vp
, blob
, NULL
, blob_add_flags
);
1782 * An arbitrary limit, to prevent someone from mapping in a 20GB blob. This should cover
1783 * our use cases for the immediate future, but note that at the time of this commit, some
1784 * platforms are nearing 2MB blob sizes (with a prior soft limit of 2.5MB).
1786 * We should consider how we can manage this more effectively; the above means that some
1787 * platforms are using megabytes of memory for signing data; it merely hasn't crossed the
1788 * threshold considered ridiculous at the time of this change.
1790 #define CS_MAX_BLOB_SIZE (40ULL * 1024ULL * 1024ULL)
1791 if (fs
.fs_blob_size
> CS_MAX_BLOB_SIZE
) {
1797 kernel_blob_size
= CAST_DOWN(vm_size_t
, fs
.fs_blob_size
);
1798 kr
= ubc_cs_blob_allocate(&kernel_blob_addr
, &kernel_blob_size
);
1799 if (kr
!= KERN_SUCCESS
) {
1805 if(uap
->cmd
== F_ADDSIGS
) {
1806 error
= copyin(fs
.fs_blob_start
,
1807 (void *) kernel_blob_addr
,
1809 } else /* F_ADDFILESIGS || F_ADDFILESIGS_RETURN || F_ADDFILESIGS_FOR_DYLD_SIM */ {
1812 error
= vn_rdwr(UIO_READ
,
1814 (caddr_t
) kernel_blob_addr
,
1816 fs
.fs_file_start
+ fs
.fs_blob_start
,
1822 if ((error
== 0) && resid
) {
1823 /* kernel_blob_size rounded to a page size, but signature may be at end of file */
1824 memset((void *)(kernel_blob_addr
+ (kernel_blob_size
- resid
)), 0x0, resid
);
1829 ubc_cs_blob_deallocate(kernel_blob_addr
,
1836 error
= ubc_cs_blob_add(vp
,
1837 CPU_TYPE_ANY
, /* not for a specific architecture */
1845 /* ubc_blob_add() has consumed "kernel_blob_addr" if it is zeroed */
1847 if (kernel_blob_addr
) {
1848 ubc_cs_blob_deallocate(kernel_blob_addr
,
1854 #if CHECK_CS_VALIDATION_BITMAP
1855 ubc_cs_validation_bitmap_allocate( vp
);
1860 if (uap
->cmd
== F_ADDFILESIGS_RETURN
|| uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
1862 * The first element of the structure is a
1863 * off_t that happen to have the same size for
1864 * all archs. Lets overwrite that.
1866 off_t end_offset
= 0;
1868 end_offset
= blob
->csb_end_offset
;
1869 error
= copyout(&end_offset
, argp
, sizeof (end_offset
));
1872 (void) vnode_put(vp
);
1881 struct fileglob
*fg
;
1884 if (fp
->f_type
!= DTYPE_VNODE
) {
1891 if (IS_64BIT_PROCESS(p
)) {
1892 error
= copyin(argp
, &lv
, sizeof (lv
));
1894 struct user32_fchecklv lv32
;
1896 error
= copyin(argp
, &lv32
, sizeof (lv32
));
1897 lv
.lv_file_start
= lv32
.lv_file_start
;
1898 lv
.lv_error_message
= CAST_USER_ADDR_T(lv32
.lv_error_message
);
1899 lv
.lv_error_message_size
= lv32
.lv_error_message
;
1905 error
= mac_file_check_library_validation(p
, fg
, lv
.lv_file_start
,
1906 lv
.lv_error_message
, lv
.lv_error_message_size
);
1912 case F_GETPROTECTIONCLASS
: {
1913 if (fp
->f_type
!= DTYPE_VNODE
) {
1917 vp
= (struct vnode
*)fp
->f_data
;
1921 if (vnode_getwithref(vp
)) {
1926 struct vnode_attr va
;
1929 VATTR_WANTED(&va
, va_dataprotect_class
);
1930 error
= VNOP_GETATTR(vp
, &va
, &context
);
1932 if (VATTR_IS_SUPPORTED(&va
, va_dataprotect_class
))
1933 *retval
= va
.va_dataprotect_class
;
1942 case F_SETPROTECTIONCLASS
: {
1943 /* tmp must be a valid PROTECTION_CLASS_* */
1944 tmp
= CAST_DOWN_EXPLICIT(uint32_t, uap
->arg
);
1946 if (fp
->f_type
!= DTYPE_VNODE
) {
1950 vp
= (struct vnode
*)fp
->f_data
;
1954 if (vnode_getwithref(vp
)) {
1959 /* Only go forward if you have write access */
1960 vfs_context_t ctx
= vfs_context_current();
1961 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
1967 struct vnode_attr va
;
1970 VATTR_SET(&va
, va_dataprotect_class
, tmp
);
1972 error
= VNOP_SETATTR(vp
, &va
, ctx
);
1978 case F_TRANSCODEKEY
: {
1979 if (fp
->f_type
!= DTYPE_VNODE
) {
1984 vp
= (struct vnode
*)fp
->f_data
;
1987 if (vnode_getwithref(vp
)) {
1993 .len
= CP_MAX_WRAPPEDKEYSIZE
,
1996 MALLOC(k
.key
, char *, k
.len
, M_TEMP
, M_WAITOK
);
1998 error
= VNOP_IOCTL(vp
, F_TRANSCODEKEY
, (caddr_t
)&k
, 1, &context
);
2003 error
= copyout(k
.key
, argp
, k
.len
);
2007 FREE(k
.key
, M_TEMP
);
2012 case F_GETPROTECTIONLEVEL
: {
2013 if (fp
->f_type
!= DTYPE_VNODE
) {
2018 vp
= (struct vnode
*) fp
->f_data
;
2021 if (vnode_getwithref(vp
)) {
2026 error
= VNOP_IOCTL(vp
, F_GETPROTECTIONLEVEL
, (caddr_t
)retval
, 0, &context
);
2032 case F_GETDEFAULTPROTLEVEL
: {
2033 if (fp
->f_type
!= DTYPE_VNODE
) {
2038 vp
= (struct vnode
*) fp
->f_data
;
2041 if (vnode_getwithref(vp
)) {
2047 * if cp_get_major_vers fails, error will be set to proper errno
2048 * and cp_version will still be 0.
2051 error
= VNOP_IOCTL(vp
, F_GETDEFAULTPROTLEVEL
, (caddr_t
)retval
, 0, &context
);
2057 #endif /* CONFIG_PROTECT */
2059 case F_MOVEDATAEXTENTS
: {
2060 struct fileproc
*fp2
= NULL
;
2061 struct vnode
*src_vp
= NULLVP
;
2062 struct vnode
*dst_vp
= NULLVP
;
2063 /* We need to grab the 2nd FD out of the argments before moving on. */
2064 int fd2
= CAST_DOWN_EXPLICIT(int32_t, uap
->arg
);
2066 error
= priv_check_cred(kauth_cred_get(), PRIV_VFS_MOVE_DATA_EXTENTS
, 0);
2070 if (fp
->f_type
!= DTYPE_VNODE
) {
2075 /* For now, special case HFS+ only, since this is SPI. */
2076 src_vp
= (struct vnode
*)fp
->f_data
;
2077 if (src_vp
->v_tag
!= VT_HFS
) {
2083 * Get the references before we start acquiring iocounts on the vnodes,
2084 * while we still hold the proc fd lock
2086 if ( (error
= fp_lookup(p
, fd2
, &fp2
, 1)) ) {
2090 if (fp2
->f_type
!= DTYPE_VNODE
) {
2091 fp_drop(p
, fd2
, fp2
, 1);
2095 dst_vp
= (struct vnode
*)fp2
->f_data
;
2096 if (dst_vp
->v_tag
!= VT_HFS
) {
2097 fp_drop(p
, fd2
, fp2
, 1);
2103 /* Re-do MAC checks against the new FD, pass in a fake argument */
2104 error
= mac_file_check_fcntl(proc_ucred(p
), fp2
->f_fglob
, uap
->cmd
, 0);
2106 fp_drop(p
, fd2
, fp2
, 1);
2110 /* Audit the 2nd FD */
2115 if (vnode_getwithref(src_vp
)) {
2116 fp_drop(p
, fd2
, fp2
, 0);
2120 if (vnode_getwithref(dst_vp
)) {
2122 fp_drop(p
, fd2
, fp2
, 0);
2128 * Basic asserts; validate they are not the same and that
2129 * both live on the same filesystem.
2131 if (dst_vp
== src_vp
) {
2134 fp_drop (p
, fd2
, fp2
, 0);
2139 if (dst_vp
->v_mount
!= src_vp
->v_mount
) {
2142 fp_drop (p
, fd2
, fp2
, 0);
2147 /* Now we have a legit pair of FDs. Go to work */
2149 /* Now check for write access to the target files */
2150 if(vnode_authorize(src_vp
, NULLVP
,
2151 (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), &context
) != 0) {
2154 fp_drop(p
, fd2
, fp2
, 0);
2159 if(vnode_authorize(dst_vp
, NULLVP
,
2160 (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), &context
) != 0) {
2163 fp_drop(p
, fd2
, fp2
, 0);
2168 /* Verify that both vps point to files and not directories */
2169 if ( !vnode_isreg(src_vp
) || !vnode_isreg(dst_vp
)) {
2173 fp_drop (p
, fd2
, fp2
, 0);
2178 * The exchangedata syscall handler passes in 0 for the flags to VNOP_EXCHANGE.
2179 * We'll pass in our special bit indicating that the new behavior is expected
2182 error
= VNOP_EXCHANGE(src_vp
, dst_vp
, FSOPT_EXCHANGE_DATA_ONLY
, &context
);
2186 fp_drop(p
, fd2
, fp2
, 0);
2191 * SPI for making a file compressed.
2193 case F_MAKECOMPRESSED
: {
2194 uint32_t gcounter
= CAST_DOWN_EXPLICIT(uint32_t, uap
->arg
);
2196 if (fp
->f_type
!= DTYPE_VNODE
) {
2201 vp
= (struct vnode
*) fp
->f_data
;
2205 if (vnode_getwithref(vp
)) {
2211 if ((vnode_isreg(vp
) == 0) && (vnode_islnk(vp
) == 0)) {
2217 /* invoke ioctl to pass off to FS */
2218 /* Only go forward if you have write access */
2219 vfs_context_t ctx
= vfs_context_current();
2220 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2226 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)&gcounter
, 0, &context
);
2233 * SPI (private) for indicating to a filesystem that subsequent writes to
2234 * the open FD will written to the Fastflow.
2236 case F_SET_GREEDY_MODE
:
2237 /* intentionally drop through to the same handler as F_SETSTATIC.
2238 * both fcntls should pass the argument and their selector into VNOP_IOCTL.
2242 * SPI (private) for indicating to a filesystem that subsequent writes to
2243 * the open FD will represent static content.
2245 case F_SETSTATICCONTENT
: {
2246 caddr_t ioctl_arg
= NULL
;
2249 ioctl_arg
= (caddr_t
) 1;
2252 if (fp
->f_type
!= DTYPE_VNODE
) {
2256 vp
= (struct vnode
*)fp
->f_data
;
2259 error
= vnode_getwithref(vp
);
2265 /* Only go forward if you have write access */
2266 vfs_context_t ctx
= vfs_context_current();
2267 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2273 error
= VNOP_IOCTL(vp
, uap
->cmd
, ioctl_arg
, 0, &context
);
2274 (void)vnode_put(vp
);
2280 * SPI (private) for indicating to the lower level storage driver that the
2281 * subsequent writes should be of a particular IO type (burst, greedy, static),
2282 * or other flavors that may be necessary.
2289 /* extract 32 bits of flags from userland */
2290 param_ptr
= (caddr_t
) uap
->arg
;
2291 param
= (uint32_t) param_ptr
;
2294 /* If no argument is specified, error out */
2300 * Validate the different types of flags that can be specified:
2301 * all of them are mutually exclusive for now.
2304 case F_IOTYPE_ISOCHRONOUS
:
2313 if (fp
->f_type
!= DTYPE_VNODE
) {
2317 vp
= (struct vnode
*)fp
->f_data
;
2320 error
= vnode_getwithref(vp
);
2326 /* Only go forward if you have write access */
2327 vfs_context_t ctx
= vfs_context_current();
2328 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2334 error
= VNOP_IOCTL(vp
, uap
->cmd
, param_ptr
, 0, &context
);
2335 (void)vnode_put(vp
);
2341 * Set the vnode pointed to by 'fd'
2342 * and tag it as the (potentially future) backing store
2343 * for another filesystem
2345 case F_SETBACKINGSTORE
: {
2346 if (fp
->f_type
!= DTYPE_VNODE
) {
2351 vp
= (struct vnode
*)fp
->f_data
;
2353 if (vp
->v_tag
!= VT_HFS
) {
2359 if (vnode_getwithref(vp
)) {
2364 /* only proceed if you have write access */
2365 vfs_context_t ctx
= vfs_context_current();
2366 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2373 /* If arg != 0, set, otherwise unset */
2375 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
)1, 0, &context
);
2378 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
2386 * like F_GETPATH, but special semantics for
2387 * the mobile time machine handler.
2389 case F_GETPATH_MTMINFO
: {
2393 if (fp
->f_type
!= DTYPE_VNODE
) {
2397 vp
= (struct vnode
*)fp
->f_data
;
2400 pathlen
= MAXPATHLEN
;
2401 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
2402 if (pathbufp
== NULL
) {
2406 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
2407 int backingstore
= 0;
2409 /* Check for error from vn_getpath before moving on */
2410 if ((error
= vn_getpath(vp
, pathbufp
, &pathlen
)) == 0) {
2411 if (vp
->v_tag
== VT_HFS
) {
2412 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
) &backingstore
, 0, &context
);
2414 (void)vnode_put(vp
);
2417 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
2421 * If the copyout was successful, now check to ensure
2422 * that this vnode is not a BACKINGSTORE vnode. mtmd
2423 * wants the path regardless.
2430 (void)vnode_put(vp
);
2432 FREE(pathbufp
, M_TEMP
);
2436 #if DEBUG || DEVELOPMENT
2438 if (fp
->f_type
!= DTYPE_VNODE
) {
2442 vp
= (struct vnode
*)fp
->f_data
;
2451 * This is an fcntl() that we d not recognize at this level;
2452 * if this is a vnode, we send it down into the VNOP_IOCTL
2453 * for this vnode; this can include special devices, and will
2454 * effectively overload fcntl() to send ioctl()'s.
2456 if((uap
->cmd
& IOC_VOID
) && (uap
->cmd
& IOC_INOUT
)){
2461 /* Catch any now-invalid fcntl() selectors */
2463 case F_MARKDEPENDENCY
:
2470 if (fp
->f_type
!= DTYPE_VNODE
) {
2474 vp
= (struct vnode
*)fp
->f_data
;
2477 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
2478 #define STK_PARAMS 128
2479 char stkbuf
[STK_PARAMS
];
2483 * For this to work properly, we have to copy in the
2484 * ioctl() cmd argument if there is one; we must also
2485 * check that a command parameter, if present, does
2486 * not exceed the maximum command length dictated by
2487 * the number of bits we have available in the command
2488 * to represent a structure length. Finally, we have
2489 * to copy the results back out, if it is that type of
2492 size
= IOCPARM_LEN(uap
->cmd
);
2493 if (size
> IOCPARM_MAX
) {
2494 (void)vnode_put(vp
);
2500 if (size
> sizeof (stkbuf
)) {
2501 if ((memp
= (caddr_t
)kalloc(size
)) == 0) {
2502 (void)vnode_put(vp
);
2511 if (uap
->cmd
& IOC_IN
) {
2514 error
= copyin(argp
, data
, size
);
2516 (void)vnode_put(vp
);
2522 /* Bzero the section beyond that which was needed */
2523 if (size
<= sizeof(stkbuf
)) {
2524 bzero ( (((uint8_t*)data
) + size
), (sizeof(stkbuf
) - size
));
2529 *(user_addr_t
*)data
= argp
;
2531 *(uint32_t *)data
= (uint32_t)argp
;
2534 } else if ((uap
->cmd
& IOC_OUT
) && size
) {
2536 * Zero the buffer so the user always
2537 * gets back something deterministic.
2540 } else if (uap
->cmd
& IOC_VOID
) {
2542 *(user_addr_t
*)data
= argp
;
2544 *(uint32_t *)data
= (uint32_t)argp
;
2548 error
= VNOP_IOCTL(vp
, uap
->cmd
, CAST_DOWN(caddr_t
, data
), 0, &context
);
2550 (void)vnode_put(vp
);
2552 /* Copy any output data to user */
2553 if (error
== 0 && (uap
->cmd
& IOC_OUT
) && size
)
2554 error
= copyout(data
, argp
, size
);
2562 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
2563 fp_drop(p
, fd
, fp
, 0);
2566 fp_drop(p
, fd
, fp
, 1);
2575 * Description: Common code for dup, dup2, and fcntl(F_DUPFD).
2577 * Parameters: p Process performing the dup
2579 * new The fd to dup it to
2580 * fd_flags Flags to augment the new fd
2581 * retval Pointer to the call return area
2583 * Returns: 0 Success
2588 * *retval (modified) The new descriptor
2590 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
2593 * Notes: This function may drop and reacquire this lock; it is unsafe
2594 * for a caller to assume that other state protected by the lock
2595 * has not been subsequently changed out from under it.
2599 struct filedesc
*fdp
, int old
, int new, int fd_flags
, int32_t *retval
)
2601 struct fileproc
*nfp
;
2602 struct fileproc
*ofp
;
2608 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2610 if ((ofp
= fdp
->fd_ofiles
[old
]) == NULL
||
2611 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
)) {
2618 error
= mac_file_check_dup(proc_ucred(p
), ofp
->f_fglob
, new);
2628 nfp
= fileproc_alloc_init(NULL
);
2638 nfp
->f_fglob
= ofp
->f_fglob
;
2641 if (fdp
->fd_ofiles
[new] != 0)
2642 panic("finishdup: overwriting fd_ofiles with new %d", new);
2643 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0)
2644 panic("finishdup: unreserved fileflags with new %d", new);
2647 if (new > fdp
->fd_lastfile
)
2648 fdp
->fd_lastfile
= new;
2649 *fdflags(p
, new) |= fd_flags
;
2650 procfdtbl_releasefd(p
, new, nfp
);
2659 * Description: The implementation of the close(2) system call
2661 * Parameters: p Process in whose per process file table
2662 * the close is to occur
2663 * uap->fd fd to be closed
2666 * Returns: 0 Success
2667 * fp_lookup:EBADF Bad file descriptor
2668 * fp_guard_exception:??? Guarded file descriptor
2669 * close_internal:EBADF
2670 * close_internal:??? Anything returnable by a per-fileops
2674 close(proc_t p
, struct close_args
*uap
, int32_t *retval
)
2676 __pthread_testcancel(1);
2677 return(close_nocancel(p
, (struct close_nocancel_args
*)uap
, retval
));
2682 close_nocancel(proc_t p
, struct close_nocancel_args
*uap
, __unused
int32_t *retval
)
2684 struct fileproc
*fp
;
2688 AUDIT_SYSCLOSE(p
, fd
);
2692 if ( (error
= fp_lookup(p
,fd
,&fp
, 1)) ) {
2697 if (FP_ISGUARDED(fp
, GUARD_CLOSE
)) {
2698 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_CLOSE
);
2699 (void) fp_drop(p
, fd
, fp
, 1);
2704 error
= close_internal_locked(p
, fd
, fp
, 0);
2713 * close_internal_locked
2715 * Close a file descriptor.
2717 * Parameters: p Process in whose per process file table
2718 * the close is to occur
2719 * fd fd to be closed
2720 * fp fileproc associated with the fd
2722 * Returns: 0 Success
2723 * EBADF fd already in close wait state
2724 * closef_locked:??? Anything returnable by a per-fileops
2727 * Locks: Assumes proc_fdlock for process is held by the caller and returns
2730 * Notes: This function may drop and reacquire this lock; it is unsafe
2731 * for a caller to assume that other state protected by the lock
2732 * has not been subsequently changed out from under it.
2735 close_internal_locked(proc_t p
, int fd
, struct fileproc
*fp
, int flags
)
2737 struct filedesc
*fdp
= p
->p_fd
;
2739 int resvfd
= flags
& FD_DUP2RESV
;
2743 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2746 /* Keep people from using the filedesc while we are closing it */
2747 procfdtbl_markclosefd(p
, fd
);
2750 if ((fp
->f_flags
& FP_CLOSING
) == FP_CLOSING
) {
2751 panic("close_internal_locked: being called on already closing fd");
2756 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
2757 panic("close_internal: unreserved fileflags with fd %d", fd
);
2760 fp
->f_flags
|= FP_CLOSING
;
2762 if ( (fp
->f_flags
& FP_AIOISSUED
) || kauth_authorize_fileop_has_listeners() ) {
2766 if ( (fp
->f_type
== DTYPE_VNODE
) && kauth_authorize_fileop_has_listeners() ) {
2768 * call out to allow 3rd party notification of close.
2769 * Ignore result of kauth_authorize_fileop call.
2771 if (vnode_getwithref((vnode_t
)fp
->f_data
) == 0) {
2772 u_int fileop_flags
= 0;
2773 if ((fp
->f_flags
& FP_WRITTEN
) != 0)
2774 fileop_flags
|= KAUTH_FILEOP_CLOSE_MODIFIED
;
2775 kauth_authorize_fileop(fp
->f_fglob
->fg_cred
, KAUTH_FILEOP_CLOSE
,
2776 (uintptr_t)fp
->f_data
, (uintptr_t)fileop_flags
);
2777 vnode_put((vnode_t
)fp
->f_data
);
2780 if (fp
->f_flags
& FP_AIOISSUED
)
2782 * cancel all async IO requests that can be cancelled.
2784 _aio_close( p
, fd
);
2789 if (fd
< fdp
->fd_knlistsize
)
2790 knote_fdclose(p
, fd
, FALSE
);
2792 if (fp
->f_flags
& FP_WAITEVENT
)
2793 (void)waitevent_close(p
, fp
);
2795 fileproc_drain(p
, fp
);
2800 procfdtbl_reservefd(p
, fd
);
2803 if (ENTR_SHOULDTRACE
&& fp
->f_type
== DTYPE_SOCKET
)
2804 KERNEL_ENERGYTRACE(kEnTrActKernSocket
, DBG_FUNC_END
,
2805 fd
, 0, (int64_t)VM_KERNEL_ADDRPERM(fp
->f_data
));
2807 error
= closef_locked(fp
, fp
->f_fglob
, p
);
2808 if ((fp
->f_flags
& FP_WAITCLOSE
) == FP_WAITCLOSE
)
2809 wakeup(&fp
->f_flags
);
2810 fp
->f_flags
&= ~(FP_WAITCLOSE
| FP_CLOSING
);
2820 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
2821 panic("close with reserved fd returns with freed fd:%d: proc: %p", fd
, p
);
2832 * Description: Return status information about a file descriptor.
2834 * Parameters: p The process doing the fstat
2836 * ub The user stat buffer
2837 * xsecurity The user extended security
2838 * buffer, or 0 if none
2839 * xsecurity_size The size of xsecurity, or 0
2841 * isstat64 Flag to indicate 64 bit version
2842 * for inode size, etc.
2844 * Returns: 0 Success
2847 * fp_lookup:EBADF Bad file descriptor
2848 * vnode_getwithref:???
2850 * vnode_getwithref:???
2857 * Notes: Internal implementation for all other fstat() related
2860 * XXX switch on node type is bogus; need a stat in struct
2861 * XXX fileops instead.
2864 fstat1(proc_t p
, int fd
, user_addr_t ub
, user_addr_t xsecurity
, user_addr_t xsecurity_size
, int isstat64
)
2866 struct fileproc
*fp
;
2872 struct user64_stat user64_sb
;
2873 struct user32_stat user32_sb
;
2874 struct user64_stat64 user64_sb64
;
2875 struct user32_stat64 user32_sb64
;
2880 kauth_filesec_t fsec
;
2881 user_size_t xsecurity_bufsize
;
2882 vfs_context_t ctx
= vfs_context_current();
2888 if ((error
= fp_lookup(p
, fd
, &fp
, 0)) != 0) {
2893 fsec
= KAUTH_FILESEC_NONE
;
2895 sbptr
= (void *)&source
;
2900 if ((error
= vnode_getwithref((vnode_t
)data
)) == 0) {
2902 * If the caller has the file open, and is not
2903 * requesting extended security information, we are
2904 * going to let them get the basic stat information.
2906 if (xsecurity
== USER_ADDR_NULL
) {
2907 error
= vn_stat_noauth((vnode_t
)data
, sbptr
, NULL
, isstat64
, ctx
,
2908 fp
->f_fglob
->fg_cred
);
2910 error
= vn_stat((vnode_t
)data
, sbptr
, &fsec
, isstat64
, ctx
);
2913 AUDIT_ARG(vnpath
, (struct vnode
*)data
, ARG_VNODE1
);
2914 (void)vnode_put((vnode_t
)data
);
2920 error
= soo_stat((struct socket
*)data
, sbptr
, isstat64
);
2922 #endif /* SOCKETS */
2925 error
= pipe_stat((void *)data
, sbptr
, isstat64
);
2929 error
= pshm_stat((void *)data
, sbptr
, isstat64
);
2933 error
= kqueue_stat((void *)data
, sbptr
, isstat64
, p
);
2943 if (isstat64
!= 0) {
2944 source
.sb64
.st_lspare
= 0;
2945 source
.sb64
.st_qspare
[0] = 0LL;
2946 source
.sb64
.st_qspare
[1] = 0LL;
2948 if (IS_64BIT_PROCESS(current_proc())) {
2949 munge_user64_stat64(&source
.sb64
, &dest
.user64_sb64
);
2950 my_size
= sizeof(dest
.user64_sb64
);
2951 sbp
= (caddr_t
)&dest
.user64_sb64
;
2953 munge_user32_stat64(&source
.sb64
, &dest
.user32_sb64
);
2954 my_size
= sizeof(dest
.user32_sb64
);
2955 sbp
= (caddr_t
)&dest
.user32_sb64
;
2958 source
.sb
.st_lspare
= 0;
2959 source
.sb
.st_qspare
[0] = 0LL;
2960 source
.sb
.st_qspare
[1] = 0LL;
2961 if (IS_64BIT_PROCESS(current_proc())) {
2962 munge_user64_stat(&source
.sb
, &dest
.user64_sb
);
2963 my_size
= sizeof(dest
.user64_sb
);
2964 sbp
= (caddr_t
)&dest
.user64_sb
;
2966 munge_user32_stat(&source
.sb
, &dest
.user32_sb
);
2967 my_size
= sizeof(dest
.user32_sb
);
2968 sbp
= (caddr_t
)&dest
.user32_sb
;
2972 error
= copyout(sbp
, ub
, my_size
);
2975 /* caller wants extended security information? */
2976 if (xsecurity
!= USER_ADDR_NULL
) {
2978 /* did we get any? */
2979 if (fsec
== KAUTH_FILESEC_NONE
) {
2980 if (susize(xsecurity_size
, 0) != 0) {
2985 /* find the user buffer size */
2986 xsecurity_bufsize
= fusize(xsecurity_size
);
2988 /* copy out the actual data size */
2989 if (susize(xsecurity_size
, KAUTH_FILESEC_COPYSIZE(fsec
)) != 0) {
2994 /* if the caller supplied enough room, copy out to it */
2995 if (xsecurity_bufsize
>= KAUTH_FILESEC_COPYSIZE(fsec
))
2996 error
= copyout(fsec
, xsecurity
, KAUTH_FILESEC_COPYSIZE(fsec
));
3000 fp_drop(p
, fd
, fp
, 0);
3002 kauth_filesec_free(fsec
);
3010 * Description: Extended version of fstat supporting returning extended
3011 * security information
3013 * Parameters: p The process doing the fstat
3014 * uap->fd The fd to stat
3015 * uap->ub The user stat buffer
3016 * uap->xsecurity The user extended security
3017 * buffer, or 0 if none
3018 * uap->xsecurity_size The size of xsecurity, or 0
3020 * Returns: 0 Success
3021 * !0 Errno (see fstat1)
3024 fstat_extended(proc_t p
, struct fstat_extended_args
*uap
, __unused
int32_t *retval
)
3026 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 0));
3033 * Description: Get file status for the file associated with fd
3035 * Parameters: p The process doing the fstat
3036 * uap->fd The fd to stat
3037 * uap->ub The user stat buffer
3039 * Returns: 0 Success
3040 * !0 Errno (see fstat1)
3043 fstat(proc_t p
, struct fstat_args
*uap
, __unused
int32_t *retval
)
3045 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 0));
3052 * Description: Extended version of fstat64 supporting returning extended
3053 * security information
3055 * Parameters: p The process doing the fstat
3056 * uap->fd The fd to stat
3057 * uap->ub The user stat buffer
3058 * uap->xsecurity The user extended security
3059 * buffer, or 0 if none
3060 * uap->xsecurity_size The size of xsecurity, or 0
3062 * Returns: 0 Success
3063 * !0 Errno (see fstat1)
3066 fstat64_extended(proc_t p
, struct fstat64_extended_args
*uap
, __unused
int32_t *retval
)
3068 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 1));
3075 * Description: Get 64 bit version of the file status for the file associated
3078 * Parameters: p The process doing the fstat
3079 * uap->fd The fd to stat
3080 * uap->ub The user stat buffer
3082 * Returns: 0 Success
3083 * !0 Errno (see fstat1)
3086 fstat64(proc_t p
, struct fstat64_args
*uap
, __unused
int32_t *retval
)
3088 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 1));
3095 * Description: Return pathconf information about a file descriptor.
3097 * Parameters: p Process making the request
3098 * uap->fd fd to get information about
3099 * uap->name Name of information desired
3100 * retval Pointer to the call return area
3102 * Returns: 0 Success
3104 * fp_lookup:EBADF Bad file descriptor
3105 * vnode_getwithref:???
3109 * *retval (modified) Returned information (numeric)
3112 fpathconf(proc_t p
, struct fpathconf_args
*uap
, int32_t *retval
)
3115 struct fileproc
*fp
;
3122 AUDIT_ARG(fd
, uap
->fd
);
3123 if ( (error
= fp_lookup(p
, fd
, &fp
, 0)) )
3131 if (uap
->name
!= _PC_PIPE_BUF
) {
3140 if (uap
->name
!= _PC_PIPE_BUF
) {
3149 vp
= (struct vnode
*)data
;
3151 if ( (error
= vnode_getwithref(vp
)) == 0) {
3152 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
3154 error
= vn_pathconf(vp
, uap
->name
, retval
, vfs_context_current());
3156 (void)vnode_put(vp
);
3167 fp_drop(p
, fd
, fp
, 0);
3172 * Statistics counter for the number of times a process calling fdalloc()
3173 * has resulted in an expansion of the per process open file table.
3175 * XXX This would likely be of more use if it were per process
3183 * Description: Allocate a file descriptor for the process.
3185 * Parameters: p Process to allocate the fd in
3186 * want The fd we would prefer to get
3187 * result Pointer to fd we got
3189 * Returns: 0 Success
3194 * *result (modified) The fd which was allocated
3197 fdalloc(proc_t p
, int want
, int *result
)
3199 struct filedesc
*fdp
= p
->p_fd
;
3201 int lim
, last
, numfiles
, oldnfiles
;
3202 struct fileproc
**newofiles
, **ofiles
;
3203 char *newofileflags
;
3206 * Search for a free descriptor starting at the higher
3207 * of want or fd_freefile. If that fails, consider
3208 * expanding the ofile array.
3211 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
3214 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
3216 last
= min(fdp
->fd_nfiles
, lim
);
3217 if ((i
= want
) < fdp
->fd_freefile
)
3218 i
= fdp
->fd_freefile
;
3219 for (; i
< last
; i
++) {
3220 if (fdp
->fd_ofiles
[i
] == NULL
&& !(fdp
->fd_ofileflags
[i
] & UF_RESERVED
)) {
3221 procfdtbl_reservefd(p
, i
);
3222 if (i
> fdp
->fd_lastfile
)
3223 fdp
->fd_lastfile
= i
;
3224 if (want
<= fdp
->fd_freefile
)
3225 fdp
->fd_freefile
= i
;
3232 * No space in current array. Expand?
3234 if (fdp
->fd_nfiles
>= lim
)
3236 if (fdp
->fd_nfiles
< NDEXTENT
)
3237 numfiles
= NDEXTENT
;
3239 numfiles
= 2 * fdp
->fd_nfiles
;
3244 MALLOC_ZONE(newofiles
, struct fileproc
**,
3245 numfiles
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
3247 if (newofiles
== NULL
) {
3250 if (fdp
->fd_nfiles
>= numfiles
) {
3251 FREE_ZONE(newofiles
, numfiles
* OFILESIZE
, M_OFILETABL
);
3254 newofileflags
= (char *) &newofiles
[numfiles
];
3256 * Copy the existing ofile and ofileflags arrays
3257 * and zero the new portion of each array.
3259 oldnfiles
= fdp
->fd_nfiles
;
3260 (void) memcpy(newofiles
, fdp
->fd_ofiles
,
3261 oldnfiles
* sizeof(*fdp
->fd_ofiles
));
3262 (void) memset(&newofiles
[oldnfiles
], 0,
3263 (numfiles
- oldnfiles
) * sizeof(*fdp
->fd_ofiles
));
3265 (void) memcpy(newofileflags
, fdp
->fd_ofileflags
,
3266 oldnfiles
* sizeof(*fdp
->fd_ofileflags
));
3267 (void) memset(&newofileflags
[oldnfiles
], 0,
3268 (numfiles
- oldnfiles
) *
3269 sizeof(*fdp
->fd_ofileflags
));
3270 ofiles
= fdp
->fd_ofiles
;
3271 fdp
->fd_ofiles
= newofiles
;
3272 fdp
->fd_ofileflags
= newofileflags
;
3273 fdp
->fd_nfiles
= numfiles
;
3274 FREE_ZONE(ofiles
, oldnfiles
* OFILESIZE
, M_OFILETABL
);
3283 * Description: Check to see whether n user file descriptors are available
3286 * Parameters: p Process to check in
3287 * n The number of fd's desired
3292 * Locks: Assumes proc_fdlock for process is held by the caller
3294 * Notes: The answer only remains valid so long as the proc_fdlock is
3295 * held by the caller.
3298 fdavail(proc_t p
, int n
)
3300 struct filedesc
*fdp
= p
->p_fd
;
3301 struct fileproc
**fpp
;
3305 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
3306 if ((i
= lim
- fdp
->fd_nfiles
) > 0 && (n
-= i
) <= 0)
3308 fpp
= &fdp
->fd_ofiles
[fdp
->fd_freefile
];
3309 flags
= &fdp
->fd_ofileflags
[fdp
->fd_freefile
];
3310 for (i
= fdp
->fd_nfiles
- fdp
->fd_freefile
; --i
>= 0; fpp
++, flags
++)
3311 if (*fpp
== NULL
&& !(*flags
& UF_RESERVED
) && --n
<= 0)
3320 * Description: Legacy KPI wrapper function for _fdrelse
3322 * Parameters: p Process in which fd lives
3327 * Locks: Assumes proc_fdlock for process is held by the caller
3330 fdrelse(proc_t p
, int fd
)
3339 * Description: Get the fileproc pointer for the given fd from the per process
3340 * open file table without taking an explicit reference on it.
3342 * Parameters: p Process containing fd
3343 * fd fd to obtain fileproc for
3344 * resultfp Pointer to pointer return area
3346 * Returns: 0 Success
3350 * *resultfp (modified) Pointer to fileproc pointer
3352 * Locks: Assumes proc_fdlock for process is held by the caller
3354 * Notes: Because there is no reference explicitly taken, the returned
3355 * fileproc pointer is only valid so long as the proc_fdlock
3356 * remains held by the caller.
3359 fdgetf_noref(proc_t p
, int fd
, struct fileproc
**resultfp
)
3361 struct filedesc
*fdp
= p
->p_fd
;
3362 struct fileproc
*fp
;
3364 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3365 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3366 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3378 * Description: Get fileproc and vnode pointer for a given fd from the per
3379 * process open file table of the specified process, and if
3380 * successful, increment the f_iocount
3382 * Parameters: p Process in which fd lives
3383 * fd fd to get information for
3384 * resultfp Pointer to result fileproc
3385 * pointer area, or 0 if none
3386 * resultvp Pointer to result vnode pointer
3387 * area, or 0 if none
3389 * Returns: 0 Success
3390 * EBADF Bad file descriptor
3391 * ENOTSUP fd does not refer to a vnode
3394 * *resultfp (modified) Fileproc pointer
3395 * *resultvp (modified) vnode pointer
3397 * Notes: The resultfp and resultvp fields are optional, and may be
3398 * independently specified as NULL to skip returning information
3400 * Locks: Internally takes and releases proc_fdlock
3403 fp_getfvp(proc_t p
, int fd
, struct fileproc
**resultfp
, struct vnode
**resultvp
)
3405 struct filedesc
*fdp
= p
->p_fd
;
3406 struct fileproc
*fp
;
3408 proc_fdlock_spin(p
);
3409 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3410 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3411 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3415 if (fp
->f_type
!= DTYPE_VNODE
) {
3424 *resultvp
= (struct vnode
*)fp
->f_data
;
3434 * Description: Get fileproc, vnode pointer, and vid for a given fd from the
3435 * per process open file table of the specified process, and if
3436 * successful, increment the f_iocount
3438 * Parameters: p Process in which fd lives
3439 * fd fd to get information for
3440 * resultfp Pointer to result fileproc
3441 * pointer area, or 0 if none
3442 * resultvp Pointer to result vnode pointer
3443 * area, or 0 if none
3444 * vidp Pointer to resuld vid area
3446 * Returns: 0 Success
3447 * EBADF Bad file descriptor
3448 * ENOTSUP fd does not refer to a vnode
3451 * *resultfp (modified) Fileproc pointer
3452 * *resultvp (modified) vnode pointer
3455 * Notes: The resultfp and resultvp fields are optional, and may be
3456 * independently specified as NULL to skip returning information
3458 * Locks: Internally takes and releases proc_fdlock
3461 fp_getfvpandvid(proc_t p
, int fd
, struct fileproc
**resultfp
,
3462 struct vnode
**resultvp
, uint32_t *vidp
)
3464 struct filedesc
*fdp
= p
->p_fd
;
3465 struct fileproc
*fp
;
3467 proc_fdlock_spin(p
);
3468 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3469 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3470 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3474 if (fp
->f_type
!= DTYPE_VNODE
) {
3483 *resultvp
= (struct vnode
*)fp
->f_data
;
3485 *vidp
= (uint32_t)vnode_vid((struct vnode
*)fp
->f_data
);
3495 * Description: Get fileproc and socket pointer for a given fd from the
3496 * per process open file table of the specified process, and if
3497 * successful, increment the f_iocount
3499 * Parameters: p Process in which fd lives
3500 * fd fd to get information for
3501 * resultfp Pointer to result fileproc
3502 * pointer area, or 0 if none
3503 * results Pointer to result socket
3504 * pointer area, or 0 if none
3506 * Returns: EBADF The file descriptor is invalid
3507 * EOPNOTSUPP The file descriptor is not a socket
3511 * *resultfp (modified) Fileproc pointer
3512 * *results (modified) socket pointer
3514 * Notes: EOPNOTSUPP should probably be ENOTSOCK; this function is only
3515 * ever called from accept1().
3518 fp_getfsock(proc_t p
, int fd
, struct fileproc
**resultfp
,
3519 struct socket
**results
)
3521 struct filedesc
*fdp
= p
->p_fd
;
3522 struct fileproc
*fp
;
3524 proc_fdlock_spin(p
);
3525 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3526 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3527 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3531 if (fp
->f_type
!= DTYPE_SOCKET
) {
3540 *results
= (struct socket
*)fp
->f_data
;
3550 * Description: Get fileproc and kqueue pointer for a given fd from the
3551 * per process open file table of the specified process, and if
3552 * successful, increment the f_iocount
3554 * Parameters: p Process in which fd lives
3555 * fd fd to get information for
3556 * resultfp Pointer to result fileproc
3557 * pointer area, or 0 if none
3558 * resultkq Pointer to result kqueue
3559 * pointer area, or 0 if none
3561 * Returns: EBADF The file descriptor is invalid
3562 * EBADF The file descriptor is not a socket
3566 * *resultfp (modified) Fileproc pointer
3567 * *resultkq (modified) kqueue pointer
3569 * Notes: The second EBADF should probably be something else to make
3570 * the error condition distinct.
3573 fp_getfkq(proc_t p
, int fd
, struct fileproc
**resultfp
,
3574 struct kqueue
**resultkq
)
3576 struct filedesc
*fdp
= p
->p_fd
;
3577 struct fileproc
*fp
;
3579 proc_fdlock_spin(p
);
3580 if ( fd
< 0 || fd
>= fdp
->fd_nfiles
||
3581 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3582 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3586 if (fp
->f_type
!= DTYPE_KQUEUE
) {
3595 *resultkq
= (struct kqueue
*)fp
->f_data
;
3605 * Description: Get fileproc and POSIX shared memory pointer for a given fd
3606 * from the per process open file table of the specified process
3607 * and if successful, increment the f_iocount
3609 * Parameters: p Process in which fd lives
3610 * fd fd to get information for
3611 * resultfp Pointer to result fileproc
3612 * pointer area, or 0 if none
3613 * resultpshm Pointer to result POSIX
3614 * shared memory pointer
3615 * pointer area, or 0 if none
3617 * Returns: EBADF The file descriptor is invalid
3618 * EBADF The file descriptor is not a POSIX
3619 * shared memory area
3623 * *resultfp (modified) Fileproc pointer
3624 * *resultpshm (modified) POSIX shared memory pointer
3626 * Notes: The second EBADF should probably be something else to make
3627 * the error condition distinct.
3630 fp_getfpshm(proc_t p
, int fd
, struct fileproc
**resultfp
,
3631 struct pshmnode
**resultpshm
)
3633 struct filedesc
*fdp
= p
->p_fd
;
3634 struct fileproc
*fp
;
3636 proc_fdlock_spin(p
);
3637 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3638 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3639 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3643 if (fp
->f_type
!= DTYPE_PSXSHM
) {
3653 *resultpshm
= (struct pshmnode
*)fp
->f_data
;
3663 * Description: Get fileproc and POSIX semaphore pointer for a given fd from
3664 * the per process open file table of the specified process
3665 * and if successful, increment the f_iocount
3667 * Parameters: p Process in which fd lives
3668 * fd fd to get information for
3669 * resultfp Pointer to result fileproc
3670 * pointer area, or 0 if none
3671 * resultpsem Pointer to result POSIX
3672 * semaphore pointer area, or
3675 * Returns: EBADF The file descriptor is invalid
3676 * EBADF The file descriptor is not a POSIX
3681 * *resultfp (modified) Fileproc pointer
3682 * *resultpsem (modified) POSIX semaphore pointer
3684 * Notes: The second EBADF should probably be something else to make
3685 * the error condition distinct.
3687 * In order to support unnamed POSIX semaphores, the named
3688 * POSIX semaphores will have to move out of the per-process
3689 * open filetable, and into a global table that is shared with
3690 * unnamed POSIX semaphores, since unnamed POSIX semaphores
3691 * are typically used by declaring instances in shared memory,
3692 * and there's no other way to do this without changing the
3693 * underlying type, which would introduce binary compatibility
3697 fp_getfpsem(proc_t p
, int fd
, struct fileproc
**resultfp
,
3698 struct psemnode
**resultpsem
)
3700 struct filedesc
*fdp
= p
->p_fd
;
3701 struct fileproc
*fp
;
3703 proc_fdlock_spin(p
);
3704 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3705 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3706 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3710 if (fp
->f_type
!= DTYPE_PSXSEM
) {
3719 *resultpsem
= (struct psemnode
*)fp
->f_data
;
3729 * Description: Get fileproc and pipe pointer for a given fd from the
3730 * per process open file table of the specified process
3731 * and if successful, increment the f_iocount
3733 * Parameters: p Process in which fd lives
3734 * fd fd to get information for
3735 * resultfp Pointer to result fileproc
3736 * pointer area, or 0 if none
3737 * resultpipe Pointer to result pipe
3738 * pointer area, or 0 if none
3740 * Returns: EBADF The file descriptor is invalid
3741 * EBADF The file descriptor is not a socket
3745 * *resultfp (modified) Fileproc pointer
3746 * *resultpipe (modified) pipe pointer
3748 * Notes: The second EBADF should probably be something else to make
3749 * the error condition distinct.
3752 fp_getfpipe(proc_t p
, int fd
, struct fileproc
**resultfp
,
3753 struct pipe
**resultpipe
)
3755 struct filedesc
*fdp
= p
->p_fd
;
3756 struct fileproc
*fp
;
3758 proc_fdlock_spin(p
);
3759 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3760 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3761 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3765 if (fp
->f_type
!= DTYPE_PIPE
) {
3774 *resultpipe
= (struct pipe
*)fp
->f_data
;
3784 * Description: Get fileproc pointer for a given fd from the per process
3785 * open file table of the specified process and if successful,
3786 * increment the f_iocount
3788 * Parameters: p Process in which fd lives
3789 * fd fd to get information for
3790 * resultfp Pointer to result fileproc
3791 * pointer area, or 0 if none
3792 * locked !0 if the caller holds the
3793 * proc_fdlock, 0 otherwise
3795 * Returns: 0 Success
3796 * EBADF Bad file descriptor
3799 * *resultfp (modified) Fileproc pointer
3801 * Locks: If the argument 'locked' is non-zero, then the caller is
3802 * expected to have taken and held the proc_fdlock; if it is
3803 * zero, than this routine internally takes and drops this lock.
3806 fp_lookup(proc_t p
, int fd
, struct fileproc
**resultfp
, int locked
)
3808 struct filedesc
*fdp
= p
->p_fd
;
3809 struct fileproc
*fp
;
3812 proc_fdlock_spin(p
);
3813 if (fd
< 0 || fdp
== NULL
|| fd
>= fdp
->fd_nfiles
||
3814 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3815 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3834 * Description: Swap the fileproc pointer for a given fd with a new
3835 * fileproc pointer in the per-process open file table of
3836 * the specified process. The fdlock must be held at entry.
3838 * Parameters: p Process containing the fd
3839 * fd The fd of interest
3840 * nfp Pointer to the newfp
3842 * Returns: 0 Success
3843 * EBADF Bad file descriptor
3845 * EKEEPLOOKING f_iocount changed while lock was dropped.
3848 fp_tryswap(proc_t p
, int fd
, struct fileproc
*nfp
)
3850 struct fileproc
*fp
;
3853 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
3855 if (0 != (error
= fp_lookup(p
, fd
, &fp
, 1)))
3858 * At this point, our caller (change_guardedfd_np) has
3859 * one f_iocount reference, and we just took another
3860 * one to begin the replacement.
3862 if (fp
->f_iocount
< 2) {
3863 panic("f_iocount too small %d", fp
->f_iocount
);
3864 } else if (2 == fp
->f_iocount
) {
3866 /* Copy the contents of *fp, preserving the "type" of *nfp */
3868 nfp
->f_flags
= (nfp
->f_flags
& FP_TYPEMASK
) |
3869 (fp
->f_flags
& ~FP_TYPEMASK
);
3870 nfp
->f_iocount
= fp
->f_iocount
;
3871 nfp
->f_fglob
= fp
->f_fglob
;
3872 nfp
->f_wset
= fp
->f_wset
;
3874 p
->p_fd
->fd_ofiles
[fd
] = nfp
;
3875 (void) fp_drop(p
, fd
, nfp
, 1);
3878 * Wait for all other active references to evaporate.
3880 p
->p_fpdrainwait
= 1;
3881 error
= msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
,
3882 PRIBIO
| PCATCH
, "tryswap fpdrain", NULL
);
3885 * Return an "internal" errno to trigger a full
3886 * reevaluation of the change-guard attempt.
3888 error
= EKEEPLOOKING
;
3889 printf("%s: lookup collision fd %d\n", __func__
, fd
);
3891 (void) fp_drop(p
, fd
, fp
, 1);
3900 * Description: Set the FP_WRITTEN flag on the fileproc and drop the I/O
3901 * reference previously taken by calling fp_lookup et. al.
3903 * Parameters: p Process in which the fd lives
3904 * fd fd associated with the fileproc
3905 * fp fileproc on which to set the
3906 * flag and drop the reference
3908 * Returns: 0 Success
3909 * fp_drop:EBADF Bad file descriptor
3911 * Locks: This function internally takes and drops the proc_fdlock for
3912 * the supplied process
3914 * Notes: The fileproc must correspond to the fd in the supplied proc
3917 fp_drop_written(proc_t p
, int fd
, struct fileproc
*fp
)
3921 proc_fdlock_spin(p
);
3923 fp
->f_flags
|= FP_WRITTEN
;
3925 error
= fp_drop(p
, fd
, fp
, 1);
3936 * Description: Set the FP_WAITEVENT flag on the fileproc and drop the I/O
3937 * reference previously taken by calling fp_lookup et. al.
3939 * Parameters: p Process in which the fd lives
3940 * fd fd associated with the fileproc
3941 * fp fileproc on which to set the
3942 * flag and drop the reference
3944 * Returns: 0 Success
3945 * fp_drop:EBADF Bad file descriptor
3947 * Locks: This function internally takes and drops the proc_fdlock for
3948 * the supplied process
3950 * Notes: The fileproc must correspond to the fd in the supplied proc
3953 fp_drop_event(proc_t p
, int fd
, struct fileproc
*fp
)
3957 proc_fdlock_spin(p
);
3959 fp
->f_flags
|= FP_WAITEVENT
;
3961 error
= fp_drop(p
, fd
, fp
, 1);
3972 * Description: Drop the I/O reference previously taken by calling fp_lookup
3975 * Parameters: p Process in which the fd lives
3976 * fd fd associated with the fileproc
3977 * fp fileproc on which to set the
3978 * flag and drop the reference
3979 * locked flag to internally take and
3980 * drop proc_fdlock if it is not
3981 * already held by the caller
3983 * Returns: 0 Success
3984 * EBADF Bad file descriptor
3986 * Locks: This function internally takes and drops the proc_fdlock for
3987 * the supplied process if 'locked' is non-zero, and assumes that
3988 * the caller already holds this lock if 'locked' is non-zero.
3990 * Notes: The fileproc must correspond to the fd in the supplied proc
3993 fp_drop(proc_t p
, int fd
, struct fileproc
*fp
, int locked
)
3995 struct filedesc
*fdp
= p
->p_fd
;
3999 proc_fdlock_spin(p
);
4000 if ((fp
== FILEPROC_NULL
) && (fd
< 0 || fd
>= fdp
->fd_nfiles
||
4001 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
4002 ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
4003 !(fdp
->fd_ofileflags
[fd
] & UF_CLOSING
)))) {
4010 if (fp
->f_iocount
== 0) {
4011 if (fp
->f_flags
& FP_SELCONFLICT
)
4012 fp
->f_flags
&= ~FP_SELCONFLICT
;
4014 if (p
->p_fpdrainwait
) {
4015 p
->p_fpdrainwait
= 0;
4022 wakeup(&p
->p_fpdrainwait
);
4031 * Description: Given an fd, look it up in the current process's per process
4032 * open file table, and return its internal vnode pointer.
4034 * Parameters: fd fd to obtain vnode from
4035 * vpp pointer to vnode return area
4037 * Returns: 0 Success
4038 * EINVAL The fd does not refer to a
4039 * vnode fileproc entry
4040 * fp_lookup:EBADF Bad file descriptor
4043 * *vpp (modified) Returned vnode pointer
4045 * Locks: This function internally takes and drops the proc_fdlock for
4046 * the current process
4048 * Notes: If successful, this function increments the f_iocount on the
4049 * fd's corresponding fileproc.
4051 * The fileproc referenced is not returned; because of this, care
4052 * must be taken to not drop the last reference (e.g. by closing
4053 * the file). This is inherently unsafe, since the reference may
4054 * not be recoverable from the vnode, if there is a subsequent
4055 * close that destroys the associate fileproc. The caller should
4056 * therefore retain their own reference on the fileproc so that
4057 * the f_iocount can be dropped subsequently. Failure to do this
4058 * can result in the returned pointer immediately becoming invalid
4059 * following the call.
4061 * Use of this function is discouraged.
4064 file_vnode(int fd
, struct vnode
**vpp
)
4066 proc_t p
= current_proc();
4067 struct fileproc
*fp
;
4070 proc_fdlock_spin(p
);
4071 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4075 if (fp
->f_type
!= DTYPE_VNODE
) {
4076 fp_drop(p
, fd
, fp
,1);
4081 *vpp
= (struct vnode
*)fp
->f_data
;
4089 * file_vnode_withvid
4091 * Description: Given an fd, look it up in the current process's per process
4092 * open file table, and return its internal vnode pointer.
4094 * Parameters: fd fd to obtain vnode from
4095 * vpp pointer to vnode return area
4096 * vidp pointer to vid of the returned vnode
4098 * Returns: 0 Success
4099 * EINVAL The fd does not refer to a
4100 * vnode fileproc entry
4101 * fp_lookup:EBADF Bad file descriptor
4104 * *vpp (modified) Returned vnode pointer
4106 * Locks: This function internally takes and drops the proc_fdlock for
4107 * the current process
4109 * Notes: If successful, this function increments the f_iocount on the
4110 * fd's corresponding fileproc.
4112 * The fileproc referenced is not returned; because of this, care
4113 * must be taken to not drop the last reference (e.g. by closing
4114 * the file). This is inherently unsafe, since the reference may
4115 * not be recoverable from the vnode, if there is a subsequent
4116 * close that destroys the associate fileproc. The caller should
4117 * therefore retain their own reference on the fileproc so that
4118 * the f_iocount can be dropped subsequently. Failure to do this
4119 * can result in the returned pointer immediately becoming invalid
4120 * following the call.
4122 * Use of this function is discouraged.
4125 file_vnode_withvid(int fd
, struct vnode
**vpp
, uint32_t * vidp
)
4127 proc_t p
= current_proc();
4128 struct fileproc
*fp
;
4132 proc_fdlock_spin(p
);
4133 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4137 if (fp
->f_type
!= DTYPE_VNODE
) {
4138 fp_drop(p
, fd
, fp
,1);
4142 vp
= (struct vnode
*)fp
->f_data
;
4146 if ((vidp
!= NULL
) && (vp
!= NULLVP
))
4147 *vidp
= (uint32_t)vp
->v_id
;
4158 * Description: Given an fd, look it up in the current process's per process
4159 * open file table, and return its internal socket pointer.
4161 * Parameters: fd fd to obtain vnode from
4162 * sp pointer to socket return area
4164 * Returns: 0 Success
4165 * ENOTSOCK Not a socket
4166 * fp_lookup:EBADF Bad file descriptor
4169 * *sp (modified) Returned socket pointer
4171 * Locks: This function internally takes and drops the proc_fdlock for
4172 * the current process
4174 * Notes: If successful, this function increments the f_iocount on the
4175 * fd's corresponding fileproc.
4177 * The fileproc referenced is not returned; because of this, care
4178 * must be taken to not drop the last reference (e.g. by closing
4179 * the file). This is inherently unsafe, since the reference may
4180 * not be recoverable from the socket, if there is a subsequent
4181 * close that destroys the associate fileproc. The caller should
4182 * therefore retain their own reference on the fileproc so that
4183 * the f_iocount can be dropped subsequently. Failure to do this
4184 * can result in the returned pointer immediately becoming invalid
4185 * following the call.
4187 * Use of this function is discouraged.
4190 file_socket(int fd
, struct socket
**sp
)
4192 proc_t p
= current_proc();
4193 struct fileproc
*fp
;
4196 proc_fdlock_spin(p
);
4197 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4201 if (fp
->f_type
!= DTYPE_SOCKET
) {
4202 fp_drop(p
, fd
, fp
,1);
4206 *sp
= (struct socket
*)fp
->f_data
;
4216 * Description: Given an fd, look it up in the current process's per process
4217 * open file table, and return its fileproc's flags field.
4219 * Parameters: fd fd whose flags are to be
4221 * flags pointer to flags data area
4223 * Returns: 0 Success
4224 * ENOTSOCK Not a socket
4225 * fp_lookup:EBADF Bad file descriptor
4228 * *flags (modified) Returned flags field
4230 * Locks: This function internally takes and drops the proc_fdlock for
4231 * the current process
4233 * Notes: This function will internally increment and decrement the
4234 * f_iocount of the fileproc as part of its operation.
4237 file_flags(int fd
, int *flags
)
4240 proc_t p
= current_proc();
4241 struct fileproc
*fp
;
4244 proc_fdlock_spin(p
);
4245 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4249 *flags
= (int)fp
->f_flag
;
4250 fp_drop(p
, fd
, fp
,1);
4260 * Description: Drop an iocount reference on an fd, and wake up any waiters
4261 * for draining (i.e. blocked in fileproc_drain() called during
4262 * the last attempt to close a file).
4264 * Parameters: fd fd on which an ioreference is
4267 * Returns: 0 Success
4268 * EBADF Bad file descriptor
4270 * Description: Given an fd, look it up in the current process's per process
4271 * open file table, and drop it's fileproc's f_iocount by one
4273 * Notes: This is intended as a corresponding operation to the functions
4274 * file_vnode() and file_socket() operations.
4276 * Technically, the close reference is supposed to be protected
4277 * by a fileproc_drain(), however, a drain will only block if
4278 * the fd refers to a character device, and that device has had
4279 * preparefileread() called on it. If it refers to something
4280 * other than a character device, then the drain will occur and
4281 * block each close attempt, rather than merely the last close.
4283 * Since it's possible for an fd that refers to a character
4284 * device to have an intermediate close followed by an open to
4285 * cause a different file to correspond to that descriptor,
4286 * unless there was a cautionary reference taken on the fileproc,
4287 * this is an inherently unsafe function. This happens in the
4288 * case where multiple fd's in a process refer to the same
4289 * character device (e.g. stdin/out/err pointing to a tty, etc.).
4291 * Use of this function is discouraged.
4296 struct fileproc
*fp
;
4297 proc_t p
= current_proc();
4300 proc_fdlock_spin(p
);
4301 if (fd
< 0 || fd
>= p
->p_fd
->fd_nfiles
||
4302 (fp
= p
->p_fd
->fd_ofiles
[fd
]) == NULL
||
4303 ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
4304 !(p
->p_fd
->fd_ofileflags
[fd
] & UF_CLOSING
))) {
4310 if (fp
->f_iocount
== 0) {
4311 if (fp
->f_flags
& FP_SELCONFLICT
)
4312 fp
->f_flags
&= ~FP_SELCONFLICT
;
4314 if (p
->p_fpdrainwait
) {
4315 p
->p_fpdrainwait
= 0;
4322 wakeup(&p
->p_fpdrainwait
);
4327 static int falloc_withalloc_locked(proc_t
, struct fileproc
**, int *,
4328 vfs_context_t
, struct fileproc
* (*)(void *), void *, int);
4333 * Description: Allocate an entry in the per process open file table and
4334 * return the corresponding fileproc and fd.
4336 * Parameters: p The process in whose open file
4337 * table the fd is to be allocated
4338 * resultfp Pointer to fileproc pointer
4340 * resultfd Pointer to fd return area
4343 * Returns: 0 Success
4344 * falloc:ENFILE Too many open files in system
4345 * falloc:EMFILE Too many open files in process
4346 * falloc:ENOMEM M_FILEPROC or M_FILEGLOB zone
4350 * *resultfd (modified) Returned fileproc pointer
4351 * *resultfd (modified) Returned fd
4353 * Locks: This function takes and drops the proc_fdlock; if this lock
4354 * is already held, use falloc_locked() instead.
4356 * Notes: This function takes separate process and context arguments
4357 * solely to support kern_exec.c; otherwise, it would take
4358 * neither, and expect falloc_locked() to use the
4359 * vfs_context_current() routine internally.
4362 falloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
)
4364 return (falloc_withalloc(p
, resultfp
, resultfd
, ctx
,
4365 fileproc_alloc_init
, NULL
));
4369 * Like falloc, but including the fileproc allocator and create-args
4372 falloc_withalloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4373 vfs_context_t ctx
, fp_allocfn_t fp_zalloc
, void *arg
)
4378 error
= falloc_withalloc_locked(p
,
4379 resultfp
, resultfd
, ctx
, fp_zalloc
, arg
, 1);
4386 * "uninitialized" ops -- ensure fg->fg_ops->fo_type always exists
4388 static const struct fileops uninitops
;
4393 * Create a new open file structure and allocate
4394 * a file descriptor for the process that refers to it.
4396 * Returns: 0 Success
4398 * Description: Allocate an entry in the per process open file table and
4399 * return the corresponding fileproc and fd.
4401 * Parameters: p The process in whose open file
4402 * table the fd is to be allocated
4403 * resultfp Pointer to fileproc pointer
4405 * resultfd Pointer to fd return area
4407 * locked Flag to indicate whether the
4408 * caller holds proc_fdlock
4410 * Returns: 0 Success
4411 * ENFILE Too many open files in system
4412 * fdalloc:EMFILE Too many open files in process
4413 * ENOMEM M_FILEPROC or M_FILEGLOB zone
4418 * *resultfd (modified) Returned fileproc pointer
4419 * *resultfd (modified) Returned fd
4421 * Locks: If the parameter 'locked' is zero, this function takes and
4422 * drops the proc_fdlock; if non-zero, the caller must hold the
4425 * Notes: If you intend to use a non-zero 'locked' parameter, use the
4426 * utility function falloc() instead.
4428 * This function takes separate process and context arguments
4429 * solely to support kern_exec.c; otherwise, it would take
4430 * neither, and use the vfs_context_current() routine internally.
4433 falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4434 vfs_context_t ctx
, int locked
)
4436 return (falloc_withalloc_locked(p
, resultfp
, resultfd
, ctx
,
4437 fileproc_alloc_init
, NULL
, locked
));
4441 falloc_withalloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4442 vfs_context_t ctx
, fp_allocfn_t fp_zalloc
, void *crarg
,
4445 struct fileproc
*fp
;
4446 struct fileglob
*fg
;
4451 if ( (error
= fdalloc(p
, 0, &nfd
)) ) {
4456 if (nfiles
>= maxfiles
) {
4463 error
= mac_file_check_create(proc_ucred(p
));
4472 * Allocate a new file descriptor.
4473 * If the process has file descriptor zero open, add to the list
4474 * of open files at that point, otherwise put it at the front of
4475 * the list of open files.
4479 fp
= (*fp_zalloc
)(crarg
);
4485 MALLOC_ZONE(fg
, struct fileglob
*, sizeof(struct fileglob
), M_FILEGLOB
, M_WAITOK
);
4492 bzero(fg
, sizeof(struct fileglob
));
4493 lck_mtx_init(&fg
->fg_lock
, file_lck_grp
, file_lck_attr
);
4497 fg
->fg_ops
= &uninitops
;
4500 mac_file_label_init(fg
);
4503 kauth_cred_ref(ctx
->vc_ucred
);
4507 fp
->f_cred
= ctx
->vc_ucred
;
4510 mac_file_label_associate(fp
->f_cred
, fg
);
4513 OSAddAtomic(1, &nfiles
);
4515 p
->p_fd
->fd_ofiles
[nfd
] = fp
;
4532 * Description: Free a file structure; drop the global open file count, and
4533 * drop the credential reference, if the fileglob has one, and
4534 * destroy the instance mutex before freeing
4536 * Parameters: fg Pointer to fileglob to be
4542 fg_free(struct fileglob
*fg
)
4544 OSAddAtomic(-1, &nfiles
);
4546 if (fg
->fg_vn_data
) {
4547 fg_vn_data_free(fg
->fg_vn_data
);
4548 fg
->fg_vn_data
= NULL
;
4551 if (IS_VALID_CRED(fg
->fg_cred
)) {
4552 kauth_cred_unref(&fg
->fg_cred
);
4554 lck_mtx_destroy(&fg
->fg_lock
, file_lck_grp
);
4557 mac_file_label_destroy(fg
);
4559 FREE_ZONE(fg
, sizeof *fg
, M_FILEGLOB
);
4566 * Description: Perform close-on-exec processing for all files in a process
4567 * that are either marked as close-on-exec, or which were in the
4568 * process of being opened at the time of the execve
4570 * Also handles the case (via posix_spawn()) where -all-
4571 * files except those marked with "inherit" as treated as
4574 * Parameters: p Pointer to process calling
4579 * Locks: This function internally takes and drops proc_fdlock()
4583 fdexec(proc_t p
, short flags
)
4585 struct filedesc
*fdp
= p
->p_fd
;
4587 boolean_t cloexec_default
= (flags
& POSIX_SPAWN_CLOEXEC_DEFAULT
) != 0;
4590 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
4592 struct fileproc
*fp
= fdp
->fd_ofiles
[i
];
4593 char *flagp
= &fdp
->fd_ofileflags
[i
];
4595 if (fp
&& cloexec_default
) {
4597 * Reverse the usual semantics of file descriptor
4598 * inheritance - all of them should be closed
4599 * except files marked explicitly as "inherit" and
4600 * not marked close-on-exec.
4602 if ((*flagp
& (UF_EXCLOSE
|UF_INHERIT
)) != UF_INHERIT
)
4603 *flagp
|= UF_EXCLOSE
;
4604 *flagp
&= ~UF_INHERIT
;
4608 ((*flagp
& (UF_RESERVED
|UF_EXCLOSE
)) == UF_EXCLOSE
)
4610 || (fp
&& mac_file_check_inherit(proc_ucred(p
), fp
->f_fglob
))
4613 if (i
< fdp
->fd_knlistsize
)
4614 knote_fdclose(p
, i
, TRUE
);
4615 procfdtbl_clearfd(p
, i
);
4616 if (i
== fdp
->fd_lastfile
&& i
> 0)
4618 if (i
< fdp
->fd_freefile
)
4619 fdp
->fd_freefile
= i
;
4622 * Wait for any third party viewers (e.g., lsof)
4623 * to release their references to this fileproc.
4625 while (fp
->f_iocount
> 0) {
4626 p
->p_fpdrainwait
= 1;
4627 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
,
4631 closef_locked(fp
, fp
->f_fglob
, p
);
4643 * Description: Copy a filedesc structure. This is normally used as part of
4644 * forkproc() when forking a new process, to copy the per process
4645 * open file table over to the new process.
4647 * Parameters: p Process whose open file table
4648 * is to be copied (parent)
4649 * uth_cdir Per thread current working
4650 * cirectory, or NULL
4652 * Returns: NULL Copy failed
4653 * !NULL Pointer to new struct filedesc
4655 * Locks: This function internally takes and drops proc_fdlock()
4657 * Notes: Files are copied directly, ignoring the new resource limits
4658 * for the process that's being copied into. Since the descriptor
4659 * references are just additional references, this does not count
4660 * against the number of open files on the system.
4662 * The struct filedesc includes the current working directory,
4663 * and the current root directory, if the process is chroot'ed.
4665 * If the exec was called by a thread using a per thread current
4666 * working directory, we inherit the working directory from the
4667 * thread making the call, rather than from the process.
4669 * In the case of a failure to obtain a reference, for most cases,
4670 * the file entry will be silently dropped. There's an exception
4671 * for the case of a chroot dir, since a failure to to obtain a
4672 * reference there would constitute an "escape" from the chroot
4673 * environment, which must not be allowed. In that case, we will
4674 * deny the execve() operation, rather than allowing the escape.
4677 fdcopy(proc_t p
, vnode_t uth_cdir
)
4679 struct filedesc
*newfdp
, *fdp
= p
->p_fd
;
4681 struct fileproc
*ofp
, *fp
;
4684 MALLOC_ZONE(newfdp
, struct filedesc
*,
4685 sizeof(*newfdp
), M_FILEDESC
, M_WAITOK
);
4692 * the FD_CHROOT flag will be inherited via this copy
4694 (void) memcpy(newfdp
, fdp
, sizeof(*newfdp
));
4697 * If we are running with per-thread current working directories,
4698 * inherit the new current working directory from the current thread
4699 * instead, before we take our references.
4701 if (uth_cdir
!= NULLVP
)
4702 newfdp
->fd_cdir
= uth_cdir
;
4705 * For both fd_cdir and fd_rdir make sure we get
4706 * a valid reference... if we can't, than set
4707 * set the pointer(s) to NULL in the child... this
4708 * will keep us from using a non-referenced vp
4709 * and allows us to do the vnode_rele only on
4710 * a properly referenced vp
4712 if ( (v_dir
= newfdp
->fd_cdir
) ) {
4713 if (vnode_getwithref(v_dir
) == 0) {
4714 if ( (vnode_ref(v_dir
)) )
4715 newfdp
->fd_cdir
= NULL
;
4718 newfdp
->fd_cdir
= NULL
;
4720 if (newfdp
->fd_cdir
== NULL
&& fdp
->fd_cdir
) {
4722 * we couldn't get a new reference on
4723 * the current working directory being
4724 * inherited... we might as well drop
4725 * our reference from the parent also
4726 * since the vnode has gone DEAD making
4727 * it useless... by dropping it we'll
4728 * be that much closer to recycling it
4730 vnode_rele(fdp
->fd_cdir
);
4731 fdp
->fd_cdir
= NULL
;
4734 if ( (v_dir
= newfdp
->fd_rdir
) ) {
4735 if (vnode_getwithref(v_dir
) == 0) {
4736 if ( (vnode_ref(v_dir
)) )
4737 newfdp
->fd_rdir
= NULL
;
4740 newfdp
->fd_rdir
= NULL
;
4743 /* Coming from a chroot environment and unable to get a reference... */
4744 if (newfdp
->fd_rdir
== NULL
&& fdp
->fd_rdir
) {
4746 * We couldn't get a new reference on
4747 * the chroot directory being
4748 * inherited... this is fatal, since
4749 * otherwise it would constitute an
4750 * escape from a chroot environment by
4753 if (newfdp
->fd_cdir
)
4754 vnode_rele(newfdp
->fd_cdir
);
4755 FREE_ZONE(newfdp
, sizeof *newfdp
, M_FILEDESC
);
4760 * If the number of open files fits in the internal arrays
4761 * of the open file structure, use them, otherwise allocate
4762 * additional memory for the number of descriptors currently
4765 if (newfdp
->fd_lastfile
< NDFILE
)
4769 * Compute the smallest multiple of NDEXTENT needed
4770 * for the file descriptors currently in use,
4771 * allowing the table to shrink.
4773 i
= newfdp
->fd_nfiles
;
4774 while (i
> 1 + 2 * NDEXTENT
&& i
> 1 + newfdp
->fd_lastfile
* 2)
4779 MALLOC_ZONE(newfdp
->fd_ofiles
, struct fileproc
**,
4780 i
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
4781 if (newfdp
->fd_ofiles
== NULL
) {
4782 if (newfdp
->fd_cdir
)
4783 vnode_rele(newfdp
->fd_cdir
);
4784 if (newfdp
->fd_rdir
)
4785 vnode_rele(newfdp
->fd_rdir
);
4787 FREE_ZONE(newfdp
, sizeof(*newfdp
), M_FILEDESC
);
4790 (void) memset(newfdp
->fd_ofiles
, 0, i
* OFILESIZE
);
4793 newfdp
->fd_ofileflags
= (char *) &newfdp
->fd_ofiles
[i
];
4794 newfdp
->fd_nfiles
= i
;
4796 if (fdp
->fd_nfiles
> 0) {
4797 struct fileproc
**fpp
;
4800 (void) memcpy(newfdp
->fd_ofiles
, fdp
->fd_ofiles
,
4801 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofiles
));
4802 (void) memcpy(newfdp
->fd_ofileflags
, fdp
->fd_ofileflags
,
4803 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofileflags
));
4806 * kq descriptors cannot be copied.
4808 if (newfdp
->fd_knlistsize
!= -1) {
4809 fpp
= &newfdp
->fd_ofiles
[newfdp
->fd_lastfile
];
4810 flags
= &newfdp
->fd_ofileflags
[newfdp
->fd_lastfile
];
4811 for (i
= newfdp
->fd_lastfile
;
4812 i
>= 0; i
--, fpp
--, flags
--) {
4813 if (*flags
& UF_RESERVED
)
4814 continue; /* (removed below) */
4815 if (*fpp
!= NULL
&& (*fpp
)->f_type
== DTYPE_KQUEUE
) {
4818 if (i
< newfdp
->fd_freefile
)
4819 newfdp
->fd_freefile
= i
;
4821 if (*fpp
== NULL
&& i
== newfdp
->fd_lastfile
&& i
> 0)
4822 newfdp
->fd_lastfile
--;
4824 newfdp
->fd_knlist
= NULL
;
4825 newfdp
->fd_knlistsize
= -1;
4826 newfdp
->fd_knhash
= NULL
;
4827 newfdp
->fd_knhashmask
= 0;
4829 fpp
= newfdp
->fd_ofiles
;
4830 flags
= newfdp
->fd_ofileflags
;
4832 for (i
= newfdp
->fd_lastfile
+ 1; --i
>= 0; fpp
++, flags
++)
4833 if ((ofp
= *fpp
) != NULL
&&
4834 0 == (ofp
->f_fglob
->fg_lflags
& FG_CONFINED
) &&
4835 0 == (*flags
& (UF_FORKCLOSE
|UF_RESERVED
))) {
4837 if (FILEPROC_TYPE(ofp
) != FTYPE_SIMPLE
)
4838 panic("complex fileproc");
4840 fp
= fileproc_alloc_init(NULL
);
4843 * XXX no room to copy, unable to
4844 * XXX safely unwind state at present
4849 (ofp
->f_flags
& ~FP_TYPEMASK
);
4850 fp
->f_fglob
= ofp
->f_fglob
;
4855 if (i
< newfdp
->fd_freefile
)
4856 newfdp
->fd_freefile
= i
;
4870 * Description: Release a filedesc (per process open file table) structure;
4871 * this is done on process exit(), or from forkproc_free() if
4872 * the fork fails for some reason subsequent to a successful
4875 * Parameters: p Pointer to process going away
4879 * Locks: This function internally takes and drops proc_fdlock()
4884 struct filedesc
*fdp
;
4885 struct fileproc
*fp
;
4890 if (p
== kernproc
|| NULL
== (fdp
= p
->p_fd
)) {
4895 extern struct filedesc filedesc0
;
4897 if (&filedesc0
== fdp
)
4900 if (fdp
->fd_nfiles
> 0 && fdp
->fd_ofiles
) {
4901 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
4903 /* May still have knotes for fd without open file */
4904 if (i
< fdp
->fd_knlistsize
)
4905 knote_fdclose(p
, i
, TRUE
);
4907 if ((fp
= fdp
->fd_ofiles
[i
]) != NULL
) {
4909 if (fdp
->fd_ofileflags
[i
] & UF_RESERVED
)
4910 panic("fdfree: found fp with UF_RESERVED");
4912 procfdtbl_reservefd(p
, i
);
4914 if (fp
->f_flags
& FP_WAITEVENT
)
4915 (void)waitevent_close(p
, fp
);
4916 (void) closef_locked(fp
, fp
->f_fglob
, p
);
4920 FREE_ZONE(fdp
->fd_ofiles
, fdp
->fd_nfiles
* OFILESIZE
, M_OFILETABL
);
4921 fdp
->fd_ofiles
= NULL
;
4928 vnode_rele(fdp
->fd_cdir
);
4930 vnode_rele(fdp
->fd_rdir
);
4932 proc_fdlock_spin(p
);
4937 FREE(fdp
->fd_knlist
, M_KQUEUE
);
4939 FREE(fdp
->fd_knhash
, M_KQUEUE
);
4941 FREE_ZONE(fdp
, sizeof(*fdp
), M_FILEDESC
);
4947 * Description: Internal form of closef; called with proc_fdlock held
4949 * Parameters: fp Pointer to fileproc for fd
4950 * fg Pointer to fileglob for fd
4951 * p Pointer to proc structure
4953 * Returns: 0 Success
4954 * closef_finish:??? Anything returnable by a per-fileops
4957 * Note: Decrements reference count on file structure; if this was the
4958 * last reference, then closef_finish() is called
4960 * p and fp are allowed to be NULL when closing a file that was
4961 * being passed in a message (but only if we are called when this
4962 * is NOT the last reference).
4965 closef_locked(struct fileproc
*fp
, struct fileglob
*fg
, proc_t p
)
4969 struct vfs_context context
;
4976 /* Set up context with cred stashed in fg */
4977 if (p
== current_proc())
4978 context
.vc_thread
= current_thread();
4980 context
.vc_thread
= NULL
;
4981 context
.vc_ucred
= fg
->fg_cred
;
4984 * POSIX record locking dictates that any close releases ALL
4985 * locks owned by this process. This is handled by setting
4986 * a flag in the unlock to free ONLY locks obeying POSIX
4987 * semantics, and not to free BSD-style file locks.
4988 * If the descriptor was in a message, POSIX-style locks
4989 * aren't passed with the descriptor.
4991 if (p
&& (p
->p_ladvflag
& P_LADVLOCK
) &&
4992 DTYPE_VNODE
== FILEGLOB_DTYPE(fg
)) {
4995 lf
.l_whence
= SEEK_SET
;
4998 lf
.l_type
= F_UNLCK
;
4999 vp
= (struct vnode
*)fg
->fg_data
;
5001 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
5002 (void) VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &lf
, F_POSIX
, &context
, NULL
);
5003 (void)vnode_put(vp
);
5007 lck_mtx_lock_spin(&fg
->fg_lock
);
5010 if (fg
->fg_count
> 0) {
5011 lck_mtx_unlock(&fg
->fg_lock
);
5015 if (fg
->fg_count
!= 0)
5016 panic("fg %p: being freed with bad fg_count (%d)", fg
, fg
->fg_count
);
5019 if (fp
&& (fp
->f_flags
& FP_WRITTEN
))
5020 fg
->fg_flag
|= FWASWRITTEN
;
5022 fg
->fg_lflags
|= FG_TERM
;
5023 lck_mtx_unlock(&fg
->fg_lock
);
5028 /* Since we ensure that fg->fg_ops is always initialized,
5029 * it is safe to invoke fo_close on the fg */
5030 error
= fo_close(fg
, &context
);
5044 * Description: Drain out pending I/O operations
5046 * Parameters: p Process closing this file
5047 * fp fileproc struct for the open
5048 * instance on the file
5052 * Locks: Assumes the caller holds the proc_fdlock
5054 * Notes: For character devices, this occurs on the last close of the
5055 * device; for all other file descriptors, this occurs on each
5056 * close to prevent fd's from being closed out from under
5057 * operations currently in progress and blocked
5059 * See Also: file_vnode(), file_socket(), file_drop(), and the cautions
5060 * regarding their use and interaction with this function.
5063 fileproc_drain(proc_t p
, struct fileproc
* fp
)
5065 struct vfs_context context
;
5067 context
.vc_thread
= proc_thread(p
); /* XXX */
5068 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
5070 fp
->f_iocount
-- ; /* (the one the close holds) */
5072 while (fp
->f_iocount
) {
5074 lck_mtx_convert_spin(&p
->p_fdmlock
);
5076 if (fp
->f_fglob
->fg_ops
->fo_drain
) {
5077 (*fp
->f_fglob
->fg_ops
->fo_drain
)(fp
, &context
);
5079 if ((fp
->f_flags
& FP_INSELECT
) == FP_INSELECT
) {
5080 if (waitq_wakeup64_all((struct waitq
*)fp
->f_wset
, NO_EVENT64
,
5081 THREAD_INTERRUPTED
, WAITQ_ALL_PRIORITIES
) == KERN_INVALID_ARGUMENT
)
5082 panic("bad wait queue for waitq_wakeup64_all %p (fp:%p)", fp
->f_wset
, fp
);
5084 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
) {
5085 if (waitq_wakeup64_all(&select_conflict_queue
, NO_EVENT64
,
5086 THREAD_INTERRUPTED
, WAITQ_ALL_PRIORITIES
) == KERN_INVALID_ARGUMENT
)
5087 panic("bad select_conflict_queue");
5089 p
->p_fpdrainwait
= 1;
5091 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
, "fpdrain", NULL
);
5095 if ((fp
->f_flags
& FP_INSELECT
) != 0)
5096 panic("FP_INSELECT set on drained fp");
5098 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
)
5099 fp
->f_flags
&= ~FP_SELCONFLICT
;
5106 * Description: Release the fd and free the fileproc associated with the fd
5107 * in the per process open file table of the specified process;
5108 * these values must correspond.
5110 * Parameters: p Process containing fd
5111 * fd fd to be released
5112 * fp fileproc to be freed
5114 * Returns: 0 Success
5116 * Notes: XXX function should be void - no one interprets the returns
5120 fp_free(proc_t p
, int fd
, struct fileproc
* fp
)
5122 proc_fdlock_spin(p
);
5126 fg_free(fp
->f_fglob
);
5135 * Description: Apply an advisory lock on a file descriptor.
5137 * Parameters: p Process making request
5138 * uap->fd fd on which the lock is to be
5140 * uap->how (Un)Lock bits, including type
5141 * retval Pointer to the call return area
5143 * Returns: 0 Success
5144 * fp_getfvp:EBADF Bad file descriptor
5145 * fp_getfvp:ENOTSUP fd does not refer to a vnode
5146 * vnode_getwithref:???
5150 * *retval (modified) Size of dtable
5152 * Notes: Just attempt to get a record lock of the requested type on
5153 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
5156 flock(proc_t p
, struct flock_args
*uap
, __unused
int32_t *retval
)
5160 struct fileproc
*fp
;
5163 vfs_context_t ctx
= vfs_context_current();
5166 AUDIT_ARG(fd
, uap
->fd
);
5167 if ( (error
= fp_getfvp(p
, fd
, &fp
, &vp
)) ) {
5170 if ( (error
= vnode_getwithref(vp
)) ) {
5173 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
5175 lf
.l_whence
= SEEK_SET
;
5178 if (how
& LOCK_UN
) {
5179 lf
.l_type
= F_UNLCK
;
5180 fp
->f_flag
&= ~FHASLOCK
;
5181 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_UNLCK
, &lf
, F_FLOCK
, ctx
, NULL
);
5185 lf
.l_type
= F_WRLCK
;
5186 else if (how
& LOCK_SH
)
5187 lf
.l_type
= F_RDLCK
;
5193 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
, F_SETLK
, &lf
);
5197 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
,
5198 (how
& LOCK_NB
? F_FLOCK
: F_FLOCK
| F_WAIT
),
5201 fp
->f_flag
|= FHASLOCK
;
5203 (void)vnode_put(vp
);
5205 fp_drop(p
, fd
, fp
, 0);
5213 * Description: Obtain a Mach send right for a given file descriptor.
5215 * Parameters: p Process calling fileport
5216 * uap->fd The fd to reference
5217 * uap->portnamep User address at which to place port name.
5219 * Returns: 0 Success.
5220 * EBADF Bad file descriptor.
5221 * EINVAL File descriptor had type that cannot be sent, misc. other errors.
5222 * EFAULT Address at which to store port name is not valid.
5223 * EAGAIN Resource shortage.
5226 * On success, name of send right is stored at user-specified address.
5229 fileport_makeport(proc_t p
, struct fileport_makeport_args
*uap
,
5230 __unused
int *retval
)
5234 user_addr_t user_portaddr
= uap
->portnamep
;
5235 struct fileproc
*fp
= FILEPROC_NULL
;
5236 struct fileglob
*fg
= NULL
;
5237 ipc_port_t fileport
;
5238 mach_port_name_t name
= MACH_PORT_NULL
;
5241 err
= fp_lookup(p
, fd
, &fp
, 1);
5246 if (!file_issendable(p
, fp
)) {
5251 if (FP_ISGUARDED(fp
, GUARD_FILEPORT
)) {
5252 err
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_FILEPORT
);
5256 /* Dropped when port is deallocated */
5262 /* Allocate and initialize a port */
5263 fileport
= fileport_alloc(fg
);
5264 if (fileport
== IPC_PORT_NULL
) {
5270 /* Add an entry. Deallocates port on failure. */
5271 name
= ipc_port_copyout_send(fileport
, get_task_ipcspace(p
->task
));
5272 if (!MACH_PORT_VALID(name
)) {
5277 err
= copyout(&name
, user_portaddr
, sizeof(mach_port_name_t
));
5282 /* Tag the fileglob for debugging purposes */
5283 lck_mtx_lock_spin(&fg
->fg_lock
);
5284 fg
->fg_lflags
|= FG_PORTMADE
;
5285 lck_mtx_unlock(&fg
->fg_lock
);
5287 fp_drop(p
, fd
, fp
, 0);
5294 if (MACH_PORT_VALID(name
)) {
5295 /* Don't care if another thread races us to deallocate the entry */
5296 (void) mach_port_deallocate(get_task_ipcspace(p
->task
), name
);
5299 if (fp
!= FILEPROC_NULL
) {
5300 fp_drop(p
, fd
, fp
, 0);
5307 fileport_releasefg(struct fileglob
*fg
)
5309 (void)closef_locked(NULL
, fg
, PROC_NULL
);
5318 * Description: Obtain the file descriptor for a given Mach send right.
5320 * Parameters: p Process calling fileport
5321 * uap->port Name of send right to file port.
5323 * Returns: 0 Success
5324 * EINVAL Invalid Mach port name, or port is not for a file.
5326 * fdalloc:ENOMEM Unable to allocate fileproc or extend file table.
5329 * *retval (modified) The new descriptor
5332 fileport_makefd(proc_t p
, struct fileport_makefd_args
*uap
, int32_t *retval
)
5334 struct fileglob
*fg
;
5335 struct fileproc
*fp
= FILEPROC_NULL
;
5336 ipc_port_t port
= IPC_PORT_NULL
;
5337 mach_port_name_t send
= uap
->port
;
5342 res
= ipc_object_copyin(get_task_ipcspace(p
->task
),
5343 send
, MACH_MSG_TYPE_COPY_SEND
, &port
);
5345 if (res
!= KERN_SUCCESS
) {
5350 fg
= fileport_port_to_fileglob(port
);
5356 fp
= fileproc_alloc_init(NULL
);
5357 if (fp
== FILEPROC_NULL
) {
5366 err
= fdalloc(p
, 0, &fd
);
5371 *fdflags(p
, fd
) |= UF_EXCLOSE
;
5373 procfdtbl_releasefd(p
, fd
, fp
);
5379 if ((fp
!= NULL
) && (0 != err
)) {
5383 if (IPC_PORT_NULL
!= port
) {
5384 ipc_port_release_send(port
);
5394 * Description: Duplicate the specified descriptor to a free descriptor;
5395 * this is the second half of fdopen(), above.
5397 * Parameters: fdp filedesc pointer to fill in
5399 * dfd fd to dup from
5400 * mode mode to set on new fd
5401 * error command code
5403 * Returns: 0 Success
5404 * EBADF Source fd is bad
5405 * EACCES Requested mode not allowed
5406 * !0 'error', if not ENODEV or
5409 * Notes: XXX This is not thread safe; see fdopen() above
5412 dupfdopen(struct filedesc
*fdp
, int indx
, int dfd
, int flags
, int error
)
5414 struct fileproc
*wfp
;
5415 struct fileproc
*fp
;
5419 proc_t p
= current_proc();
5422 * If the to-be-dup'd fd number is greater than the allowed number
5423 * of file descriptors, or the fd to be dup'd has already been
5424 * closed, reject. Note, check for new == old is necessary as
5425 * falloc could allocate an already closed to-be-dup'd descriptor
5426 * as the new descriptor.
5430 fp
= fdp
->fd_ofiles
[indx
];
5431 if (dfd
< 0 || dfd
>= fdp
->fd_nfiles
||
5432 (wfp
= fdp
->fd_ofiles
[dfd
]) == NULL
|| wfp
== fp
||
5433 (fdp
->fd_ofileflags
[dfd
] & UF_RESERVED
)) {
5439 myerror
= mac_file_check_dup(proc_ucred(p
), wfp
->f_fglob
, dfd
);
5446 * There are two cases of interest here.
5448 * For ENODEV simply dup (dfd) to file descriptor
5449 * (indx) and return.
5451 * For ENXIO steal away the file structure from (dfd) and
5452 * store it in (indx). (dfd) is effectively closed by
5455 * Any other error code is just returned.
5459 if (FP_ISGUARDED(wfp
, GUARD_DUP
)) {
5465 * Check that the mode the file is being opened for is a
5466 * subset of the mode of the existing descriptor.
5468 if (((flags
& (FREAD
|FWRITE
)) | wfp
->f_flag
) != wfp
->f_flag
) {
5472 if (indx
> fdp
->fd_lastfile
)
5473 fdp
->fd_lastfile
= indx
;
5477 fg_free(fp
->f_fglob
);
5478 fp
->f_fglob
= wfp
->f_fglob
;
5480 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
] |
5481 (flags
& O_CLOEXEC
) ? UF_EXCLOSE
: 0;
5497 * Description: Add a reference to a fileglob by fileproc
5499 * Parameters: fp fileproc containing fileglob
5504 * Notes: XXX Should use OSAddAtomic?
5507 fg_ref(struct fileproc
* fp
)
5509 struct fileglob
*fg
;
5513 lck_mtx_lock_spin(&fg
->fg_lock
);
5516 if ((fp
->f_flags
& ~((unsigned int)FP_VALID_FLAGS
)) != 0)
5517 panic("fg_ref: invalid bits on fp %p", fp
);
5519 if (fg
->fg_count
== 0)
5520 panic("fg_ref: adding fgcount to zeroed fg: fp %p fg %p",
5524 lck_mtx_unlock(&fg
->fg_lock
);
5531 * Description: Remove a reference to a fileglob by fileproc
5533 * Parameters: fp fileproc containing fileglob
5538 * Notes: XXX Should use OSAddAtomic?
5541 fg_drop(struct fileproc
* fp
)
5543 struct fileglob
*fg
;
5546 lck_mtx_lock_spin(&fg
->fg_lock
);
5548 lck_mtx_unlock(&fg
->fg_lock
);
5553 * fg_insertuipc_mark
5555 * Description: Mark fileglob for insertion onto message queue if needed
5556 * Also takes fileglob reference
5558 * Parameters: fg Fileglob pointer to insert
5560 * Returns: true, if the fileglob needs to be inserted onto msg queue
5562 * Locks: Takes and drops fg_lock, potentially many times
5565 fg_insertuipc_mark(struct fileglob
* fg
)
5567 boolean_t insert
= FALSE
;
5569 lck_mtx_lock_spin(&fg
->fg_lock
);
5570 while (fg
->fg_lflags
& FG_RMMSGQ
) {
5571 lck_mtx_convert_spin(&fg
->fg_lock
);
5573 fg
->fg_lflags
|= FG_WRMMSGQ
;
5574 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_insertuipc", NULL
);
5579 if (fg
->fg_msgcount
== 1) {
5580 fg
->fg_lflags
|= FG_INSMSGQ
;
5583 lck_mtx_unlock(&fg
->fg_lock
);
5590 * Description: Insert marked fileglob onto message queue
5592 * Parameters: fg Fileglob pointer to insert
5596 * Locks: Takes and drops fg_lock & uipc_lock
5597 * DO NOT call this function with proc_fdlock held as unp_gc()
5598 * can potentially try to acquire proc_fdlock, which can result
5599 * in a deadlock if this function is in unp_gc_wait().
5602 fg_insertuipc(struct fileglob
* fg
)
5604 if (fg
->fg_lflags
& FG_INSMSGQ
) {
5605 lck_mtx_lock_spin(uipc_lock
);
5607 LIST_INSERT_HEAD(&fmsghead
, fg
, f_msglist
);
5608 lck_mtx_unlock(uipc_lock
);
5609 lck_mtx_lock(&fg
->fg_lock
);
5610 fg
->fg_lflags
&= ~FG_INSMSGQ
;
5611 if (fg
->fg_lflags
& FG_WINSMSGQ
) {
5612 fg
->fg_lflags
&= ~FG_WINSMSGQ
;
5613 wakeup(&fg
->fg_lflags
);
5615 lck_mtx_unlock(&fg
->fg_lock
);
5620 * fg_removeuipc_mark
5622 * Description: Mark the fileglob for removal from message queue if needed
5623 * Also releases fileglob message queue reference
5625 * Parameters: fg Fileglob pointer to remove
5627 * Returns: true, if the fileglob needs to be removed from msg queue
5629 * Locks: Takes and drops fg_lock, potentially many times
5632 fg_removeuipc_mark(struct fileglob
* fg
)
5634 boolean_t remove
= FALSE
;
5636 lck_mtx_lock_spin(&fg
->fg_lock
);
5637 while (fg
->fg_lflags
& FG_INSMSGQ
) {
5638 lck_mtx_convert_spin(&fg
->fg_lock
);
5640 fg
->fg_lflags
|= FG_WINSMSGQ
;
5641 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_removeuipc", NULL
);
5644 if (fg
->fg_msgcount
== 0) {
5645 fg
->fg_lflags
|= FG_RMMSGQ
;
5648 lck_mtx_unlock(&fg
->fg_lock
);
5655 * Description: Remove marked fileglob from message queue
5657 * Parameters: fg Fileglob pointer to remove
5661 * Locks: Takes and drops fg_lock & uipc_lock
5662 * DO NOT call this function with proc_fdlock held as unp_gc()
5663 * can potentially try to acquire proc_fdlock, which can result
5664 * in a deadlock if this function is in unp_gc_wait().
5667 fg_removeuipc(struct fileglob
* fg
)
5669 if (fg
->fg_lflags
& FG_RMMSGQ
) {
5670 lck_mtx_lock_spin(uipc_lock
);
5672 LIST_REMOVE(fg
, f_msglist
);
5673 lck_mtx_unlock(uipc_lock
);
5674 lck_mtx_lock(&fg
->fg_lock
);
5675 fg
->fg_lflags
&= ~FG_RMMSGQ
;
5676 if (fg
->fg_lflags
& FG_WRMMSGQ
) {
5677 fg
->fg_lflags
&= ~FG_WRMMSGQ
;
5678 wakeup(&fg
->fg_lflags
);
5680 lck_mtx_unlock(&fg
->fg_lock
);
5683 #endif /* SOCKETS */
5688 * Description: Generic fileops read indirected through the fileops pointer
5689 * in the fileproc structure
5691 * Parameters: fp fileproc structure pointer
5692 * uio user I/O structure pointer
5694 * ctx VFS context for operation
5696 * Returns: 0 Success
5697 * !0 Errno from read
5700 fo_read(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
5702 return ((*fp
->f_ops
->fo_read
)(fp
, uio
, flags
, ctx
));
5709 * Description: Generic fileops write indirected through the fileops pointer
5710 * in the fileproc structure
5712 * Parameters: fp fileproc structure pointer
5713 * uio user I/O structure pointer
5715 * ctx VFS context for operation
5717 * Returns: 0 Success
5718 * !0 Errno from write
5721 fo_write(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
5723 return((*fp
->f_ops
->fo_write
)(fp
, uio
, flags
, ctx
));
5730 * Description: Generic fileops ioctl indirected through the fileops pointer
5731 * in the fileproc structure
5733 * Parameters: fp fileproc structure pointer
5735 * data pointer to internalized copy
5736 * of user space ioctl command
5737 * parameter data in kernel space
5738 * ctx VFS context for operation
5740 * Returns: 0 Success
5741 * !0 Errno from ioctl
5743 * Locks: The caller is assumed to have held the proc_fdlock; this
5744 * function releases and reacquires this lock. If the caller
5745 * accesses data protected by this lock prior to calling this
5746 * function, it will need to revalidate/reacquire any cached
5747 * protected data obtained prior to the call.
5750 fo_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
, vfs_context_t ctx
)
5754 proc_fdunlock(vfs_context_proc(ctx
));
5755 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, com
, data
, ctx
);
5756 proc_fdlock(vfs_context_proc(ctx
));
5764 * Description: Generic fileops select indirected through the fileops pointer
5765 * in the fileproc structure
5767 * Parameters: fp fileproc structure pointer
5768 * which select which
5769 * wql pointer to wait queue list
5770 * ctx VFS context for operation
5772 * Returns: 0 Success
5773 * !0 Errno from select
5776 fo_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
5778 return((*fp
->f_ops
->fo_select
)(fp
, which
, wql
, ctx
));
5785 * Description: Generic fileops close indirected through the fileops pointer
5786 * in the fileproc structure
5788 * Parameters: fp fileproc structure pointer for
5790 * ctx VFS context for operation
5792 * Returns: 0 Success
5793 * !0 Errno from close
5796 fo_close(struct fileglob
*fg
, vfs_context_t ctx
)
5798 return((*fg
->fg_ops
->fo_close
)(fg
, ctx
));
5805 * Description: Generic fileops kqueue filter indirected through the fileops
5806 * pointer in the fileproc structure
5808 * Parameters: fp fileproc structure pointer
5809 * kn pointer to knote to filter on
5810 * ctx VFS context for operation
5812 * Returns: (kn->kn_flags & EV_ERROR) error in kn->kn_data
5813 * 0 Filter is not active
5814 * !0 Filter is active
5817 fo_kqfilter(struct fileproc
*fp
, struct knote
*kn
, vfs_context_t ctx
)
5819 return ((*fp
->f_ops
->fo_kqfilter
)(fp
, kn
, ctx
));
5823 * The ability to send a file descriptor to another
5824 * process is opt-in by file type.
5827 file_issendable(proc_t p
, struct fileproc
*fp
)
5829 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
5831 switch (fp
->f_type
) {
5836 return (0 == (fp
->f_fglob
->fg_lflags
& FG_CONFINED
));
5838 /* DTYPE_KQUEUE, DTYPE_FSEVENTS, DTYPE_PSXSEM */
5845 fileproc_alloc_init(__unused
void *arg
)
5847 struct fileproc
*fp
;
5849 MALLOC_ZONE(fp
, struct fileproc
*, sizeof (*fp
), M_FILEPROC
, M_WAITOK
);
5851 bzero(fp
, sizeof (*fp
));
5857 fileproc_free(struct fileproc
*fp
)
5859 switch (FILEPROC_TYPE(fp
)) {
5861 FREE_ZONE(fp
, sizeof (*fp
), M_FILEPROC
);
5864 guarded_fileproc_free(fp
);
5867 panic("%s: corrupt fp %p flags %x", __func__
, fp
, fp
->f_flags
);