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>
124 #include <security/mac_framework.h>
127 kern_return_t
ipc_object_copyin(ipc_space_t
, mach_port_name_t
,
128 mach_msg_type_name_t
, ipc_port_t
*);
129 void ipc_port_release_send(ipc_port_t
);
134 static int finishdup(proc_t p
,
135 struct filedesc
*fdp
, int old
, int new, int flags
, int32_t *retval
);
137 int falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
, int locked
);
138 void fg_drop(struct fileproc
* fp
);
139 void fg_free(struct fileglob
*fg
);
140 void fg_ref(struct fileproc
* fp
);
141 void fileport_releasefg(struct fileglob
*fg
);
143 /* flags for close_internal_locked */
144 #define FD_DUP2RESV 1
146 /* We don't want these exported */
149 int unlink1(vfs_context_t
, vnode_t
, user_addr_t
, enum uio_seg
, int);
151 static void _fdrelse(struct proc
* p
, int fd
);
154 extern void file_lock_init(void);
156 extern kauth_scope_t kauth_scope_fileop
;
158 /* Conflict wait queue for when selects collide (opaque type) */
159 extern struct waitq select_conflict_queue
;
161 #define f_flag f_fglob->fg_flag
162 #define f_type f_fglob->fg_ops->fo_type
163 #define f_msgcount f_fglob->fg_msgcount
164 #define f_cred f_fglob->fg_cred
165 #define f_ops f_fglob->fg_ops
166 #define f_offset f_fglob->fg_offset
167 #define f_data f_fglob->fg_data
168 #define CHECK_ADD_OVERFLOW_INT64L(x, y) \
169 (((((x) > 0) && ((y) > 0) && ((x) > LLONG_MAX - (y))) || \
170 (((x) < 0) && ((y) < 0) && ((x) < LLONG_MIN - (y)))) \
173 * Descriptor management.
175 struct fmsglist fmsghead
; /* head of list of open files */
176 struct fmsglist fmsg_ithead
; /* head of list of open files */
177 int nfiles
; /* actual number of open files */
180 lck_grp_attr_t
* file_lck_grp_attr
;
181 lck_grp_t
* file_lck_grp
;
182 lck_attr_t
* file_lck_attr
;
184 lck_mtx_t
* uipc_lock
;
188 * check_file_seek_range
190 * Description: Checks if seek offsets are in the range of 0 to LLONG_MAX.
192 * Parameters: fl Flock structure.
193 * cur_file_offset Current offset in the file.
195 * Returns: 0 on Success.
196 * EOVERFLOW on overflow.
197 * EINVAL on offset less than zero.
201 check_file_seek_range(struct flock
*fl
, off_t cur_file_offset
)
203 if (fl
->l_whence
== SEEK_CUR
) {
204 /* Check if the start marker is beyond LLONG_MAX. */
205 if (CHECK_ADD_OVERFLOW_INT64L(fl
->l_start
, cur_file_offset
)) {
206 /* Check if start marker is negative */
207 if (fl
->l_start
< 0) {
212 /* Check if the start marker is negative. */
213 if (fl
->l_start
+ cur_file_offset
< 0) {
216 /* Check if end marker is beyond LLONG_MAX. */
217 if ((fl
->l_len
> 0) && (CHECK_ADD_OVERFLOW_INT64L(fl
->l_start
+
218 cur_file_offset
, fl
->l_len
- 1))) {
221 /* Check if the end marker is negative. */
222 if ((fl
->l_len
<= 0) && (fl
->l_start
+ cur_file_offset
+
226 } else if (fl
->l_whence
== SEEK_SET
) {
227 /* Check if the start marker is negative. */
228 if (fl
->l_start
< 0) {
231 /* Check if the end marker is beyond LLONG_MAX. */
232 if ((fl
->l_len
> 0) &&
233 CHECK_ADD_OVERFLOW_INT64L(fl
->l_start
, fl
->l_len
- 1)) {
236 /* Check if the end marker is negative. */
237 if ((fl
->l_len
< 0) && fl
->l_start
+ fl
->l_len
< 0) {
248 * Description: Initialize the file lock group and the uipc and flist locks
254 * Notes: Called at system startup from bsd_init().
259 /* allocate file lock group attribute and group */
260 file_lck_grp_attr
= lck_grp_attr_alloc_init();
262 file_lck_grp
= lck_grp_alloc_init("file", file_lck_grp_attr
);
264 /* Allocate file lock attribute */
265 file_lck_attr
= lck_attr_alloc_init();
267 uipc_lock
= lck_mtx_alloc_init(file_lck_grp
, file_lck_attr
);
272 * proc_fdlock, proc_fdlock_spin
274 * Description: Lock to control access to the per process struct fileproc
275 * and struct filedesc
277 * Parameters: p Process to take the lock on
281 * Notes: The lock is initialized in forkproc() and destroyed in
282 * reap_child_process().
285 proc_fdlock(proc_t p
)
287 lck_mtx_lock(&p
->p_fdmlock
);
291 proc_fdlock_spin(proc_t p
)
293 lck_mtx_lock_spin(&p
->p_fdmlock
);
297 proc_fdlock_assert(proc_t p
, int assertflags
)
299 lck_mtx_assert(&p
->p_fdmlock
, assertflags
);
306 * Description: Unlock the lock previously locked by a call to proc_fdlock()
308 * Parameters: p Process to drop the lock on
313 proc_fdunlock(proc_t p
)
315 lck_mtx_unlock(&p
->p_fdmlock
);
320 * System calls on descriptors.
327 * Description: Returns the per process maximum size of the descriptor table
329 * Parameters: p Process being queried
330 * retval Pointer to the call return area
335 * *retval (modified) Size of dtable
338 getdtablesize(proc_t p
, __unused
struct getdtablesize_args
*uap
, int32_t *retval
)
341 *retval
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
349 procfdtbl_reservefd(struct proc
* p
, int fd
)
351 p
->p_fd
->fd_ofiles
[fd
] = NULL
;
352 p
->p_fd
->fd_ofileflags
[fd
] |= UF_RESERVED
;
356 procfdtbl_markclosefd(struct proc
* p
, int fd
)
358 p
->p_fd
->fd_ofileflags
[fd
] |= (UF_RESERVED
| UF_CLOSING
);
362 procfdtbl_releasefd(struct proc
* p
, int fd
, struct fileproc
* fp
)
365 p
->p_fd
->fd_ofiles
[fd
] = fp
;
366 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESERVED
;
367 if ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
) == UF_RESVWAIT
) {
368 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESVWAIT
;
374 procfdtbl_waitfd(struct proc
* p
, int fd
)
376 p
->p_fd
->fd_ofileflags
[fd
] |= UF_RESVWAIT
;
377 msleep(&p
->p_fd
, &p
->p_fdmlock
, PRIBIO
, "ftbl_waitfd", NULL
);
382 procfdtbl_clearfd(struct proc
* p
, int fd
)
386 waiting
= (p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
);
387 p
->p_fd
->fd_ofiles
[fd
] = NULL
;
388 p
->p_fd
->fd_ofileflags
[fd
] = 0;
389 if ( waiting
== UF_RESVWAIT
) {
397 * Description: Inline utility function to free an fd in a filedesc
399 * Parameters: fdp Pointer to filedesc fd lies in
401 * reserv fd should be reserved
405 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
409 _fdrelse(struct proc
* p
, int fd
)
411 struct filedesc
*fdp
= p
->p_fd
;
414 if (fd
< fdp
->fd_freefile
)
415 fdp
->fd_freefile
= fd
;
417 if (fd
> fdp
->fd_lastfile
)
418 panic("fdrelse: fd_lastfile inconsistent");
420 procfdtbl_clearfd(p
, fd
);
422 while ((nfd
= fdp
->fd_lastfile
) > 0 &&
423 fdp
->fd_ofiles
[nfd
] == NULL
&&
424 !(fdp
->fd_ofileflags
[nfd
] & UF_RESERVED
))
425 /* JMM - What about files with lingering EV_VANISHED knotes? */
447 char uio_buf
[ UIO_SIZEOF(1) ];
448 struct vfs_context context
= *(vfs_context_current());
449 bool wrote_some
= false;
453 error
= fp_lookup(p
, fd
, &fp
, 0);
457 if (fp
->f_type
!= DTYPE_VNODE
&& fp
->f_type
!= DTYPE_PIPE
&& fp
->f_type
!= DTYPE_SOCKET
) {
461 if (rw
== UIO_WRITE
&& !(fp
->f_flag
& FWRITE
)) {
466 if (rw
== UIO_READ
&& !(fp
->f_flag
& FREAD
)) {
471 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
473 if (UIO_SEG_IS_USER_SPACE(segflg
))
474 spacetype
= proc_is64bit(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
476 spacetype
= UIO_SYSSPACE
;
478 auio
= uio_createwithbuffer(1, offset
, spacetype
, rw
, &uio_buf
[0], sizeof(uio_buf
));
480 uio_addiov(auio
, base
, len
);
482 if ( !(io_flg
& IO_APPEND
))
485 if (rw
== UIO_WRITE
) {
486 user_ssize_t orig_resid
= uio_resid(auio
);
487 error
= fo_write(fp
, auio
, flags
, &context
);
488 wrote_some
= uio_resid(auio
) < orig_resid
;
490 error
= fo_read(fp
, auio
, flags
, &context
);
493 *aresid
= uio_resid(auio
);
495 if (uio_resid(auio
) && error
== 0)
500 fp_drop_written(p
, fd
, fp
);
502 fp_drop(p
, fd
, fp
, 0);
512 * Description: Duplicate a file descriptor.
514 * Parameters: p Process performing the dup
515 * uap->fd The fd to dup
516 * retval Pointer to the call return area
522 * *retval (modified) The new descriptor
525 dup(proc_t p
, struct dup_args
*uap
, int32_t *retval
)
527 struct filedesc
*fdp
= p
->p_fd
;
533 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
537 if (FP_ISGUARDED(fp
, GUARD_DUP
)) {
538 error
= fp_guard_exception(p
, old
, fp
, kGUARD_EXC_DUP
);
539 (void) fp_drop(p
, old
, fp
, 1);
543 if ( (error
= fdalloc(p
, 0, &new)) ) {
544 fp_drop(p
, old
, fp
, 1);
548 error
= finishdup(p
, fdp
, old
, new, 0, retval
);
549 fp_drop(p
, old
, fp
, 1);
552 if (ENTR_SHOULDTRACE
&& fp
->f_type
== DTYPE_SOCKET
) {
553 KERNEL_ENERGYTRACE(kEnTrActKernSocket
, DBG_FUNC_START
,
554 new, 0, (int64_t)VM_KERNEL_ADDRPERM(fp
->f_data
));
563 * Description: Duplicate a file descriptor to a particular value.
565 * Parameters: p Process performing the dup
566 * uap->from The fd to dup
567 * uap->to The fd to dup it to
568 * retval Pointer to the call return area
574 * *retval (modified) The new descriptor
577 dup2(proc_t p
, struct dup2_args
*uap
, int32_t *retval
)
579 struct filedesc
*fdp
= p
->p_fd
;
580 int old
= uap
->from
, new = uap
->to
;
582 struct fileproc
*fp
, *nfp
;
587 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
591 if (FP_ISGUARDED(fp
, GUARD_DUP
)) {
592 error
= fp_guard_exception(p
, old
, fp
, kGUARD_EXC_DUP
);
593 (void) fp_drop(p
, old
, fp
, 1);
598 (rlim_t
)new >= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
600 fp_drop(p
, old
, fp
, 1);
605 fp_drop(p
, old
, fp
, 1);
610 if (new < 0 || new >= fdp
->fd_nfiles
) {
611 if ( (error
= fdalloc(p
, new, &i
)) ) {
612 fp_drop(p
, old
, fp
, 1);
622 while ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == UF_RESERVED
) {
623 fp_drop(p
, old
, fp
, 1);
624 procfdtbl_waitfd(p
, new);
626 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
631 if ((fdp
->fd_ofiles
[new] != NULL
) &&
632 ((error
= fp_lookup(p
, new, &nfp
, 1)) == 0)) {
633 fp_drop(p
, old
, fp
, 1);
634 if (FP_ISGUARDED(nfp
, GUARD_CLOSE
)) {
635 error
= fp_guard_exception(p
,
636 new, nfp
, kGUARD_EXC_CLOSE
);
637 (void) fp_drop(p
, new, nfp
, 1);
641 (void)close_internal_locked(p
, new, nfp
, FD_DUP2RESV
);
643 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
645 procfdtbl_clearfd(p
, new);
649 if (fdp
->fd_ofiles
[new] != NULL
)
650 panic("dup2: no ref on fileproc %d", new);
652 procfdtbl_reservefd(p
, new);
656 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
661 if (fdp
->fd_ofiles
[new] != 0)
662 panic("dup2: overwriting fd_ofiles with new %d", new);
663 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0)
664 panic("dup2: unreserved fileflags with new %d", new);
666 error
= finishdup(p
, fdp
, old
, new, 0, retval
);
667 fp_drop(p
, old
, fp
, 1);
677 * Description: The file control system call.
679 * Parameters: p Process performing the fcntl
680 * uap->fd The fd to operate against
681 * uap->cmd The command to perform
682 * uap->arg Pointer to the command argument
683 * retval Pointer to the call return area
686 * !0 Errno (see fcntl_nocancel)
689 * *retval (modified) fcntl return value (if any)
691 * Notes: This system call differs from fcntl_nocancel() in that it
692 * tests for cancellation prior to performing a potentially
693 * blocking operation.
696 fcntl(proc_t p
, struct fcntl_args
*uap
, int32_t *retval
)
698 __pthread_testcancel(1);
699 return(fcntl_nocancel(p
, (struct fcntl_nocancel_args
*)uap
, retval
));
706 * Description: A non-cancel-testing file control system call.
708 * Parameters: p Process performing the fcntl
709 * uap->fd The fd to operate against
710 * uap->cmd The command to perform
711 * uap->arg Pointer to the command argument
712 * retval Pointer to the call return area
716 * fp_lookup:EBADF Bad file descriptor
728 * vnode_getwithref:???
736 * vnode_getwithref:???
743 * vnode_getwithref:???
745 * [F_SETSIZE,F_RDADVISE]
748 * vnode_getwithref:???
749 * [F_RDAHEAD,F_NOCACHE]
751 * vnode_getwithref:???
755 * *retval (modified) fcntl return value (if any)
758 fcntl_nocancel(proc_t p
, struct fcntl_nocancel_args
*uap
, int32_t *retval
)
761 struct filedesc
*fdp
= p
->p_fd
;
764 struct vnode
*vp
= NULLVP
; /* for AUDIT_ARG() at end */
765 int i
, tmp
, error
, error2
, flg
= 0;
767 struct flocktimeout fltimeout
;
768 struct timespec
*timeout
= NULL
;
769 struct vfs_context context
;
777 AUDIT_ARG(fd
, uap
->fd
);
778 AUDIT_ARG(cmd
, uap
->cmd
);
781 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
785 context
.vc_thread
= current_thread();
786 context
.vc_ucred
= fp
->f_cred
;
788 is64bit
= proc_is64bit(p
);
794 * Since the arg parameter is defined as a long but may be
795 * either a long or a pointer we must take care to handle
796 * sign extension issues. Our sys call munger will sign
797 * extend a long when we are called from a 32-bit process.
798 * Since we can never have an address greater than 32-bits
799 * from a 32-bit process we lop off the top 32-bits to avoid
800 * getting the wrong address
802 argp
= CAST_USER_ADDR_T((uint32_t)uap
->arg
);
805 pop
= &fdp
->fd_ofileflags
[fd
];
808 error
= mac_file_check_fcntl(proc_ucred(p
), fp
->f_fglob
, uap
->cmd
,
817 case F_DUPFD_CLOEXEC
:
818 if (FP_ISGUARDED(fp
, GUARD_DUP
)) {
819 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_DUP
);
822 newmin
= CAST_DOWN_EXPLICIT(int, uap
->arg
); /* arg is an int, so we won't lose bits */
823 AUDIT_ARG(value32
, newmin
);
824 if ((u_int
)newmin
>= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
825 newmin
>= maxfiles
) {
829 if ( (error
= fdalloc(p
, newmin
, &i
)) )
831 error
= finishdup(p
, fdp
, fd
, i
,
832 uap
->cmd
== F_DUPFD_CLOEXEC
? UF_EXCLOSE
: 0, retval
);
836 *retval
= (*pop
& UF_EXCLOSE
)? FD_CLOEXEC
: 0;
841 AUDIT_ARG(value32
, uap
->arg
);
842 if (uap
->arg
& FD_CLOEXEC
)
845 if (FILEPROC_TYPE(fp
) == FTYPE_GUARDED
) {
846 error
= fp_guard_exception(p
,
847 fd
, fp
, kGUARD_EXC_NOCLOEXEC
);
856 *retval
= OFLAGS(fp
->f_flag
);
861 fp
->f_flag
&= ~FCNTLFLAGS
;
862 tmp
= CAST_DOWN_EXPLICIT(int, uap
->arg
); /* arg is an int, so we won't lose bits */
863 AUDIT_ARG(value32
, tmp
);
864 fp
->f_flag
|= FFLAGS(tmp
) & FCNTLFLAGS
;
865 tmp
= fp
->f_flag
& FNONBLOCK
;
866 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
869 tmp
= fp
->f_flag
& FASYNC
;
870 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, &context
);
873 fp
->f_flag
&= ~FNONBLOCK
;
875 (void)fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
879 if (fp
->f_type
== DTYPE_SOCKET
) {
880 *retval
= ((struct socket
*)fp
->f_data
)->so_pgid
;
884 error
= fo_ioctl(fp
, (int)TIOCGPGRP
, (caddr_t
)retval
, &context
);
889 tmp
= CAST_DOWN_EXPLICIT(pid_t
, uap
->arg
); /* arg is an int, so we won't lose bits */
890 AUDIT_ARG(value32
, tmp
);
891 if (fp
->f_type
== DTYPE_SOCKET
) {
892 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
896 if (fp
->f_type
== DTYPE_PIPE
) {
897 error
= fo_ioctl(fp
, TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
904 proc_t p1
= proc_find(tmp
);
909 tmp
= (int)p1
->p_pgrpid
;
912 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
916 tmp
= CAST_DOWN_EXPLICIT(int, uap
->arg
);
917 if (fp
->f_type
== DTYPE_SOCKET
) {
919 error
= sock_setsockopt((struct socket
*)fp
->f_data
,
920 SOL_SOCKET
, SO_NOSIGPIPE
, &tmp
, sizeof (tmp
));
925 struct fileglob
*fg
= fp
->f_fglob
;
927 lck_mtx_lock_spin(&fg
->fg_lock
);
929 fg
->fg_lflags
|= FG_NOSIGPIPE
;
931 fg
->fg_lflags
&= FG_NOSIGPIPE
;
932 lck_mtx_unlock(&fg
->fg_lock
);
938 if (fp
->f_type
== DTYPE_SOCKET
) {
940 int retsize
= sizeof (*retval
);
941 error
= sock_getsockopt((struct socket
*)fp
->f_data
,
942 SOL_SOCKET
, SO_NOSIGPIPE
, retval
, &retsize
);
947 *retval
= (fp
->f_fglob
->fg_lflags
& FG_NOSIGPIPE
) ?
955 * If this is the only reference to this fglob in the process
956 * and it's already marked as close-on-fork then mark it as
957 * (immutably) "confined" i.e. any fd that points to it will
958 * forever be close-on-fork, and attempts to use an IPC
959 * mechanism to move the descriptor elsewhere will fail.
961 if (CAST_DOWN_EXPLICIT(int, uap
->arg
)) {
962 struct fileglob
*fg
= fp
->f_fglob
;
964 lck_mtx_lock_spin(&fg
->fg_lock
);
965 if (fg
->fg_lflags
& FG_CONFINED
)
967 else if (1 != fg
->fg_count
)
968 error
= EAGAIN
; /* go close the dup .. */
969 else if (UF_FORKCLOSE
== (*pop
& UF_FORKCLOSE
)) {
970 fg
->fg_lflags
|= FG_CONFINED
;
973 error
= EBADF
; /* open without O_CLOFORK? */
974 lck_mtx_unlock(&fg
->fg_lock
);
977 * Other subsystems may have built on the immutability
978 * of FG_CONFINED; clearing it may be tricky.
980 error
= EPERM
; /* immutable */
985 *retval
= (fp
->f_fglob
->fg_lflags
& FG_CONFINED
) ? 1 : 0;
989 case F_SETLKWTIMEOUT
:
991 case F_OFD_SETLKWTIMEOUT
:
994 /* Fall into F_SETLK */
998 if (fp
->f_type
!= DTYPE_VNODE
) {
1002 vp
= (struct vnode
*)fp
->f_data
;
1005 offset
= fp
->f_offset
;
1008 /* Copy in the lock structure */
1009 if (F_SETLKWTIMEOUT
== uap
->cmd
||
1010 F_OFD_SETLKWTIMEOUT
== uap
->cmd
) {
1011 error
= copyin(argp
, (caddr_t
) &fltimeout
, sizeof(fltimeout
));
1016 timeout
= &fltimeout
.timeout
;
1018 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
1024 /* Check starting byte and ending byte for EOVERFLOW in SEEK_CUR */
1025 /* and ending byte for EOVERFLOW in SEEK_SET */
1026 error
= check_file_seek_range(&fl
, offset
);
1031 if ( (error
= vnode_getwithref(vp
)) ) {
1034 if (fl
.l_whence
== SEEK_CUR
)
1035 fl
.l_start
+= offset
;
1038 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
1041 (void)vnode_put(vp
);
1048 case F_OFD_SETLKWTIMEOUT
:
1050 switch (fl
.l_type
) {
1052 if ((fflag
& FREAD
) == 0) {
1056 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1057 F_SETLK
, &fl
, flg
, &context
, timeout
);
1060 if ((fflag
& FWRITE
) == 0) {
1064 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1065 F_SETLK
, &fl
, flg
, &context
, timeout
);
1068 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1069 F_UNLCK
, &fl
, F_OFD_LOCK
, &context
,
1077 (F_RDLCK
== fl
.l_type
|| F_WRLCK
== fl
.l_type
)) {
1078 struct fileglob
*fg
= fp
->f_fglob
;
1081 * arrange F_UNLCK on last close (once
1082 * set, FG_HAS_OFDLOCK is immutable)
1084 if ((fg
->fg_lflags
& FG_HAS_OFDLOCK
) == 0) {
1085 lck_mtx_lock_spin(&fg
->fg_lock
);
1086 fg
->fg_lflags
|= FG_HAS_OFDLOCK
;
1087 lck_mtx_unlock(&fg
->fg_lock
);
1093 switch (fl
.l_type
) {
1095 if ((fflag
& FREAD
) == 0) {
1099 // XXX UInt32 unsafe for LP64 kernel
1100 OSBitOrAtomic(P_LADVLOCK
, &p
->p_ladvflag
);
1101 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1102 F_SETLK
, &fl
, flg
, &context
, timeout
);
1105 if ((fflag
& FWRITE
) == 0) {
1109 // XXX UInt32 unsafe for LP64 kernel
1110 OSBitOrAtomic(P_LADVLOCK
, &p
->p_ladvflag
);
1111 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1112 F_SETLK
, &fl
, flg
, &context
, timeout
);
1115 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1116 F_UNLCK
, &fl
, F_POSIX
, &context
, timeout
);
1124 (void) vnode_put(vp
);
1131 case F_OFD_GETLKPID
:
1133 if (fp
->f_type
!= DTYPE_VNODE
) {
1137 vp
= (struct vnode
*)fp
->f_data
;
1139 offset
= fp
->f_offset
;
1142 /* Copy in the lock structure */
1143 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
1147 /* Check starting byte and ending byte for EOVERFLOW in SEEK_CUR */
1148 /* and ending byte for EOVERFLOW in SEEK_SET */
1149 error
= check_file_seek_range(&fl
, offset
);
1154 if ((fl
.l_whence
== SEEK_SET
) && (fl
.l_start
< 0)) {
1159 switch (fl
.l_type
) {
1169 switch (fl
.l_whence
) {
1179 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1180 if (fl
.l_whence
== SEEK_CUR
)
1181 fl
.l_start
+= offset
;
1184 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
1190 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1191 F_GETLK
, &fl
, F_OFD_LOCK
, &context
, NULL
);
1193 case F_OFD_GETLKPID
:
1194 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
,
1195 F_GETLKPID
, &fl
, F_OFD_LOCK
, &context
, NULL
);
1198 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
,
1199 uap
->cmd
, &fl
, F_POSIX
, &context
, NULL
);
1203 (void)vnode_put(vp
);
1206 error
= copyout((caddr_t
)&fl
, argp
, sizeof(fl
));
1210 case F_PREALLOCATE
: {
1211 fstore_t alloc_struct
; /* structure for allocate command */
1212 u_int32_t alloc_flags
= 0;
1214 if (fp
->f_type
!= DTYPE_VNODE
) {
1219 vp
= (struct vnode
*)fp
->f_data
;
1222 /* make sure that we have write permission */
1223 if ((fp
->f_flag
& FWRITE
) == 0) {
1228 error
= copyin(argp
, (caddr_t
)&alloc_struct
, sizeof(alloc_struct
));
1232 /* now set the space allocated to 0 */
1233 alloc_struct
.fst_bytesalloc
= 0;
1236 * Do some simple parameter checking
1239 /* set up the flags */
1241 alloc_flags
|= PREALLOCATE
;
1243 if (alloc_struct
.fst_flags
& F_ALLOCATECONTIG
)
1244 alloc_flags
|= ALLOCATECONTIG
;
1246 if (alloc_struct
.fst_flags
& F_ALLOCATEALL
)
1247 alloc_flags
|= ALLOCATEALL
;
1250 * Do any position mode specific stuff. The only
1251 * position mode supported now is PEOFPOSMODE
1254 switch (alloc_struct
.fst_posmode
) {
1257 if (alloc_struct
.fst_offset
!= 0) {
1262 alloc_flags
|= ALLOCATEFROMPEOF
;
1266 if (alloc_struct
.fst_offset
<= 0) {
1271 alloc_flags
|= ALLOCATEFROMVOL
;
1279 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1281 * call allocate to get the space
1283 error
= VNOP_ALLOCATE(vp
,alloc_struct
.fst_length
,alloc_flags
,
1284 &alloc_struct
.fst_bytesalloc
, alloc_struct
.fst_offset
,
1286 (void)vnode_put(vp
);
1288 error2
= copyout((caddr_t
)&alloc_struct
, argp
, sizeof(alloc_struct
));
1298 if (fp
->f_type
!= DTYPE_VNODE
) {
1303 vp
= (struct vnode
*)fp
->f_data
;
1306 /* need write permissions */
1307 if ((fp
->f_flag
& FWRITE
) == 0) {
1312 if ((error
= copyin(argp
, (caddr_t
)&args
, sizeof(args
)))) {
1316 if ((error
= vnode_getwithref(vp
))) {
1321 if ((error
= mac_vnode_check_write(&context
, fp
->f_fglob
->fg_cred
, vp
))) {
1322 (void)vnode_put(vp
);
1327 error
= VNOP_IOCTL(vp
, F_PUNCHHOLE
, (caddr_t
)&args
, 0, &context
);
1328 (void)vnode_put(vp
);
1332 case F_TRIM_ACTIVE_FILE
: {
1333 ftrimactivefile_t args
;
1335 if (priv_check_cred(kauth_cred_get(), PRIV_TRIM_ACTIVE_FILE
, 0)) {
1340 if (fp
->f_type
!= DTYPE_VNODE
) {
1345 vp
= (struct vnode
*)fp
->f_data
;
1348 /* need write permissions */
1349 if ((fp
->f_flag
& FWRITE
) == 0) {
1354 if ((error
= copyin(argp
, (caddr_t
)&args
, sizeof(args
)))) {
1358 if ((error
= vnode_getwithref(vp
))) {
1362 error
= VNOP_IOCTL(vp
, F_TRIM_ACTIVE_FILE
, (caddr_t
)&args
, 0, &context
);
1363 (void)vnode_put(vp
);
1368 if (fp
->f_type
!= DTYPE_VNODE
) {
1372 vp
= (struct vnode
*)fp
->f_data
;
1375 error
= copyin(argp
, (caddr_t
)&offset
, sizeof (off_t
));
1378 AUDIT_ARG(value64
, offset
);
1380 error
= vnode_getwithref(vp
);
1385 error
= mac_vnode_check_truncate(&context
,
1386 fp
->f_fglob
->fg_cred
, vp
);
1388 (void)vnode_put(vp
);
1393 * Make sure that we are root. Growing a file
1394 * without zero filling the data is a security hole
1395 * root would have access anyway so we'll allow it
1397 if (!kauth_cred_issuser(kauth_cred_get())) {
1403 error
= vnode_setsize(vp
, offset
, IO_NOZEROFILL
,
1408 mac_vnode_notify_truncate(&context
, fp
->f_fglob
->fg_cred
, vp
);
1412 (void)vnode_put(vp
);
1416 if (fp
->f_type
!= DTYPE_VNODE
) {
1421 fp
->f_fglob
->fg_flag
&= ~FNORDAHEAD
;
1423 fp
->f_fglob
->fg_flag
|= FNORDAHEAD
;
1428 if (fp
->f_type
!= DTYPE_VNODE
) {
1433 fp
->f_fglob
->fg_flag
|= FNOCACHE
;
1435 fp
->f_fglob
->fg_flag
&= ~FNOCACHE
;
1440 if (fp
->f_type
!= DTYPE_VNODE
) {
1445 fp
->f_fglob
->fg_flag
|= FNODIRECT
;
1447 fp
->f_fglob
->fg_flag
&= ~FNODIRECT
;
1451 case F_SINGLE_WRITER
:
1452 if (fp
->f_type
!= DTYPE_VNODE
) {
1457 fp
->f_fglob
->fg_flag
|= FSINGLE_WRITER
;
1459 fp
->f_fglob
->fg_flag
&= ~FSINGLE_WRITER
;
1463 case F_GLOBAL_NOCACHE
:
1464 if (fp
->f_type
!= DTYPE_VNODE
) {
1468 vp
= (struct vnode
*)fp
->f_data
;
1471 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1473 *retval
= vnode_isnocache(vp
);
1476 vnode_setnocache(vp
);
1478 vnode_clearnocache(vp
);
1480 (void)vnode_put(vp
);
1484 case F_CHECK_OPENEVT
:
1485 if (fp
->f_type
!= DTYPE_VNODE
) {
1489 vp
= (struct vnode
*)fp
->f_data
;
1492 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1494 *retval
= vnode_is_openevt(vp
);
1497 vnode_set_openevt(vp
);
1499 vnode_clear_openevt(vp
);
1501 (void)vnode_put(vp
);
1506 struct radvisory ra_struct
;
1508 if (fp
->f_type
!= DTYPE_VNODE
) {
1512 vp
= (struct vnode
*)fp
->f_data
;
1515 if ( (error
= copyin(argp
, (caddr_t
)&ra_struct
, sizeof(ra_struct
))) )
1517 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1518 error
= VNOP_IOCTL(vp
, F_RDADVISE
, (caddr_t
)&ra_struct
, 0, &context
);
1520 (void)vnode_put(vp
);
1527 if (fp
->f_type
!= DTYPE_VNODE
) {
1531 vp
= (struct vnode
*)fp
->f_data
;
1534 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1535 error
= VNOP_FSYNC(vp
, MNT_NOWAIT
, &context
);
1537 (void)vnode_put(vp
);
1542 case F_LOG2PHYS_EXT
: {
1543 struct log2phys l2p_struct
; /* structure for allocate command */
1546 off_t file_offset
= 0;
1550 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1551 error
= copyin(argp
, (caddr_t
)&l2p_struct
, sizeof(l2p_struct
));
1554 file_offset
= l2p_struct
.l2p_devoffset
;
1556 file_offset
= fp
->f_offset
;
1558 if (fp
->f_type
!= DTYPE_VNODE
) {
1562 vp
= (struct vnode
*)fp
->f_data
;
1564 if ( (error
= vnode_getwithref(vp
)) ) {
1567 error
= VNOP_OFFTOBLK(vp
, file_offset
, &lbn
);
1569 (void)vnode_put(vp
);
1572 error
= VNOP_BLKTOOFF(vp
, lbn
, &offset
);
1574 (void)vnode_put(vp
);
1577 devBlockSize
= vfs_devblocksize(vnode_mount(vp
));
1578 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1579 if (l2p_struct
.l2p_contigbytes
< 0) {
1585 a_size
= MIN((uint64_t)l2p_struct
.l2p_contigbytes
, SIZE_MAX
);
1587 a_size
= devBlockSize
;
1590 error
= VNOP_BLOCKMAP(vp
, offset
, a_size
, &bn
, &run
, NULL
, 0, &context
);
1592 (void)vnode_put(vp
);
1595 l2p_struct
.l2p_flags
= 0; /* for now */
1596 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1597 l2p_struct
.l2p_contigbytes
= run
- (file_offset
- offset
);
1599 l2p_struct
.l2p_contigbytes
= 0; /* for now */
1603 * The block number being -1 suggests that the file offset is not backed
1604 * by any real blocks on-disk. As a result, just let it be passed back up wholesale.
1607 /* Don't multiply it by the block size */
1608 l2p_struct
.l2p_devoffset
= bn
;
1611 l2p_struct
.l2p_devoffset
= bn
* devBlockSize
;
1612 l2p_struct
.l2p_devoffset
+= file_offset
- offset
;
1614 error
= copyout((caddr_t
)&l2p_struct
, argp
, sizeof(l2p_struct
));
1622 if (fp
->f_type
!= DTYPE_VNODE
) {
1626 vp
= (struct vnode
*)fp
->f_data
;
1629 pathlen
= MAXPATHLEN
;
1630 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
1631 if (pathbufp
== NULL
) {
1635 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1636 error
= vn_getpath(vp
, pathbufp
, &pathlen
);
1637 (void)vnode_put(vp
);
1640 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
1642 FREE(pathbufp
, M_TEMP
);
1646 case F_PATHPKG_CHECK
: {
1650 if (fp
->f_type
!= DTYPE_VNODE
) {
1654 vp
= (struct vnode
*)fp
->f_data
;
1657 pathlen
= MAXPATHLEN
;
1658 pathbufp
= kalloc(MAXPATHLEN
);
1660 if ( (error
= copyinstr(argp
, pathbufp
, MAXPATHLEN
, &pathlen
)) == 0 ) {
1661 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1662 AUDIT_ARG(text
, pathbufp
);
1663 error
= vn_path_package_check(vp
, pathbufp
, pathlen
, retval
);
1665 (void)vnode_put(vp
);
1668 kfree(pathbufp
, MAXPATHLEN
);
1672 case F_CHKCLEAN
: // used by regression tests to see if all dirty pages got cleaned by fsync()
1673 case F_FULLFSYNC
: // fsync + flush the journal + DKIOCSYNCHRONIZE
1674 case F_BARRIERFSYNC
: // fsync + barrier
1675 case F_FREEZE_FS
: // freeze all other fs operations for the fs of this fd
1676 case F_THAW_FS
: { // thaw all frozen fs operations for the fs of this fd
1677 if (fp
->f_type
!= DTYPE_VNODE
) {
1681 vp
= (struct vnode
*)fp
->f_data
;
1684 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1685 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
1687 (void)vnode_put(vp
);
1693 * SPI (private) for opening a file starting from a dir fd
1696 struct user_fopenfrom fopen
;
1697 struct vnode_attr va
;
1698 struct nameidata nd
;
1701 /* Check if this isn't a valid file descriptor */
1702 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1703 (fp
->f_flag
& FREAD
) == 0) {
1707 vp
= (struct vnode
*)fp
->f_data
;
1710 if (vnode_getwithref(vp
)) {
1715 /* Only valid for directories */
1716 if (vp
->v_type
!= VDIR
) {
1722 /* Get flags, mode and pathname arguments. */
1723 if (IS_64BIT_PROCESS(p
)) {
1724 error
= copyin(argp
, &fopen
, sizeof(fopen
));
1726 struct user32_fopenfrom fopen32
;
1728 error
= copyin(argp
, &fopen32
, sizeof(fopen32
));
1729 fopen
.o_flags
= fopen32
.o_flags
;
1730 fopen
.o_mode
= fopen32
.o_mode
;
1731 fopen
.o_pathname
= CAST_USER_ADDR_T(fopen32
.o_pathname
);
1737 AUDIT_ARG(fflags
, fopen
.o_flags
);
1738 AUDIT_ARG(mode
, fopen
.o_mode
);
1740 /* Mask off all but regular access permissions */
1741 cmode
= ((fopen
.o_mode
&~ fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
1742 VATTR_SET(&va
, va_mode
, cmode
& ACCESSPERMS
);
1744 /* Start the lookup relative to the file descriptor's vnode. */
1745 NDINIT(&nd
, LOOKUP
, OP_OPEN
, USEDVP
| FOLLOW
| AUDITVNPATH1
, UIO_USERSPACE
,
1746 fopen
.o_pathname
, &context
);
1749 error
= open1(&context
, &nd
, fopen
.o_flags
, &va
,
1750 fileproc_alloc_init
, NULL
, retval
);
1756 * SPI (private) for unlinking a file starting from a dir fd
1758 case F_UNLINKFROM
: {
1759 user_addr_t pathname
;
1761 /* Check if this isn't a valid file descriptor */
1762 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1763 (fp
->f_flag
& FREAD
) == 0) {
1767 vp
= (struct vnode
*)fp
->f_data
;
1770 if (vnode_getwithref(vp
)) {
1775 /* Only valid for directories */
1776 if (vp
->v_type
!= VDIR
) {
1782 /* Get flags, mode and pathname arguments. */
1783 if (IS_64BIT_PROCESS(p
)) {
1784 pathname
= (user_addr_t
)argp
;
1786 pathname
= CAST_USER_ADDR_T(argp
);
1789 /* Start the lookup relative to the file descriptor's vnode. */
1790 error
= unlink1(&context
, vp
, pathname
, UIO_USERSPACE
, 0);
1799 case F_ADDFILESIGS_FOR_DYLD_SIM
:
1800 case F_ADDFILESIGS_RETURN
:
1802 struct cs_blob
*blob
= NULL
;
1803 struct user_fsignatures fs
;
1805 vm_offset_t kernel_blob_addr
;
1806 vm_size_t kernel_blob_size
;
1807 int blob_add_flags
= 0;
1809 if (fp
->f_type
!= DTYPE_VNODE
) {
1813 vp
= (struct vnode
*)fp
->f_data
;
1816 if (uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
1817 blob_add_flags
|= MAC_VNODE_CHECK_DYLD_SIM
;
1818 if ((p
->p_csflags
& CS_KILL
) == 0) {
1820 p
->p_csflags
|= CS_KILL
;
1825 error
= vnode_getwithref(vp
);
1829 if (IS_64BIT_PROCESS(p
)) {
1830 error
= copyin(argp
, &fs
, sizeof (fs
));
1832 struct user32_fsignatures fs32
;
1834 error
= copyin(argp
, &fs32
, sizeof (fs32
));
1835 fs
.fs_file_start
= fs32
.fs_file_start
;
1836 fs
.fs_blob_start
= CAST_USER_ADDR_T(fs32
.fs_blob_start
);
1837 fs
.fs_blob_size
= fs32
.fs_blob_size
;
1846 * First check if we have something loaded a this offset
1848 blob
= ubc_cs_blob_get(vp
, CPU_TYPE_ANY
, fs
.fs_file_start
);
1851 /* If this is for dyld_sim revalidate the blob */
1852 if (uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
1853 error
= ubc_cs_blob_revalidate(vp
, blob
, NULL
, blob_add_flags
);
1862 * An arbitrary limit, to prevent someone from mapping in a 20GB blob. This should cover
1863 * our use cases for the immediate future, but note that at the time of this commit, some
1864 * platforms are nearing 2MB blob sizes (with a prior soft limit of 2.5MB).
1866 * We should consider how we can manage this more effectively; the above means that some
1867 * platforms are using megabytes of memory for signing data; it merely hasn't crossed the
1868 * threshold considered ridiculous at the time of this change.
1870 #define CS_MAX_BLOB_SIZE (40ULL * 1024ULL * 1024ULL)
1871 if (fs
.fs_blob_size
> CS_MAX_BLOB_SIZE
) {
1877 kernel_blob_size
= CAST_DOWN(vm_size_t
, fs
.fs_blob_size
);
1878 kr
= ubc_cs_blob_allocate(&kernel_blob_addr
, &kernel_blob_size
);
1879 if (kr
!= KERN_SUCCESS
) {
1885 if(uap
->cmd
== F_ADDSIGS
) {
1886 error
= copyin(fs
.fs_blob_start
,
1887 (void *) kernel_blob_addr
,
1889 } else /* F_ADDFILESIGS || F_ADDFILESIGS_RETURN || F_ADDFILESIGS_FOR_DYLD_SIM */ {
1892 error
= vn_rdwr(UIO_READ
,
1894 (caddr_t
) kernel_blob_addr
,
1896 fs
.fs_file_start
+ fs
.fs_blob_start
,
1902 if ((error
== 0) && resid
) {
1903 /* kernel_blob_size rounded to a page size, but signature may be at end of file */
1904 memset((void *)(kernel_blob_addr
+ (kernel_blob_size
- resid
)), 0x0, resid
);
1909 ubc_cs_blob_deallocate(kernel_blob_addr
,
1916 error
= ubc_cs_blob_add(vp
,
1917 CPU_TYPE_ANY
, /* not for a specific architecture */
1925 /* ubc_blob_add() has consumed "kernel_blob_addr" if it is zeroed */
1927 if (kernel_blob_addr
) {
1928 ubc_cs_blob_deallocate(kernel_blob_addr
,
1934 #if CHECK_CS_VALIDATION_BITMAP
1935 ubc_cs_validation_bitmap_allocate( vp
);
1940 if (uap
->cmd
== F_ADDFILESIGS_RETURN
|| uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
1942 * The first element of the structure is a
1943 * off_t that happen to have the same size for
1944 * all archs. Lets overwrite that.
1946 off_t end_offset
= 0;
1948 end_offset
= blob
->csb_end_offset
;
1949 error
= copyout(&end_offset
, argp
, sizeof (end_offset
));
1952 (void) vnode_put(vp
);
1961 struct fileglob
*fg
;
1964 if (fp
->f_type
!= DTYPE_VNODE
) {
1971 if (IS_64BIT_PROCESS(p
)) {
1972 error
= copyin(argp
, &lv
, sizeof (lv
));
1974 struct user32_fchecklv lv32
;
1976 error
= copyin(argp
, &lv32
, sizeof (lv32
));
1977 lv
.lv_file_start
= lv32
.lv_file_start
;
1978 lv
.lv_error_message
= (void *)(uintptr_t)lv32
.lv_error_message
;
1979 lv
.lv_error_message_size
= lv32
.lv_error_message
;
1985 error
= mac_file_check_library_validation(p
, fg
, lv
.lv_file_start
,
1986 (user_long_t
)lv
.lv_error_message
, lv
.lv_error_message_size
);
1992 case F_GETPROTECTIONCLASS
: {
1993 if (fp
->f_type
!= DTYPE_VNODE
) {
1997 vp
= (struct vnode
*)fp
->f_data
;
2001 if (vnode_getwithref(vp
)) {
2006 struct vnode_attr va
;
2009 VATTR_WANTED(&va
, va_dataprotect_class
);
2010 error
= VNOP_GETATTR(vp
, &va
, &context
);
2012 if (VATTR_IS_SUPPORTED(&va
, va_dataprotect_class
))
2013 *retval
= va
.va_dataprotect_class
;
2022 case F_SETPROTECTIONCLASS
: {
2023 /* tmp must be a valid PROTECTION_CLASS_* */
2024 tmp
= CAST_DOWN_EXPLICIT(uint32_t, uap
->arg
);
2026 if (fp
->f_type
!= DTYPE_VNODE
) {
2030 vp
= (struct vnode
*)fp
->f_data
;
2034 if (vnode_getwithref(vp
)) {
2039 /* Only go forward if you have write access */
2040 vfs_context_t ctx
= vfs_context_current();
2041 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2047 struct vnode_attr va
;
2050 VATTR_SET(&va
, va_dataprotect_class
, tmp
);
2052 error
= VNOP_SETATTR(vp
, &va
, ctx
);
2058 case F_TRANSCODEKEY
: {
2059 if (fp
->f_type
!= DTYPE_VNODE
) {
2064 vp
= (struct vnode
*)fp
->f_data
;
2067 if (vnode_getwithref(vp
)) {
2073 .len
= CP_MAX_WRAPPEDKEYSIZE
,
2076 MALLOC(k
.key
, char *, k
.len
, M_TEMP
, M_WAITOK
);
2078 error
= VNOP_IOCTL(vp
, F_TRANSCODEKEY
, (caddr_t
)&k
, 1, &context
);
2083 error
= copyout(k
.key
, argp
, k
.len
);
2087 FREE(k
.key
, M_TEMP
);
2092 case F_GETPROTECTIONLEVEL
: {
2093 if (fp
->f_type
!= DTYPE_VNODE
) {
2098 vp
= (struct vnode
*) fp
->f_data
;
2101 if (vnode_getwithref(vp
)) {
2106 error
= VNOP_IOCTL(vp
, F_GETPROTECTIONLEVEL
, (caddr_t
)retval
, 0, &context
);
2112 case F_GETDEFAULTPROTLEVEL
: {
2113 if (fp
->f_type
!= DTYPE_VNODE
) {
2118 vp
= (struct vnode
*) fp
->f_data
;
2121 if (vnode_getwithref(vp
)) {
2127 * if cp_get_major_vers fails, error will be set to proper errno
2128 * and cp_version will still be 0.
2131 error
= VNOP_IOCTL(vp
, F_GETDEFAULTPROTLEVEL
, (caddr_t
)retval
, 0, &context
);
2137 #endif /* CONFIG_PROTECT */
2139 case F_MOVEDATAEXTENTS
: {
2140 struct fileproc
*fp2
= NULL
;
2141 struct vnode
*src_vp
= NULLVP
;
2142 struct vnode
*dst_vp
= NULLVP
;
2143 /* We need to grab the 2nd FD out of the argments before moving on. */
2144 int fd2
= CAST_DOWN_EXPLICIT(int32_t, uap
->arg
);
2146 error
= priv_check_cred(kauth_cred_get(), PRIV_VFS_MOVE_DATA_EXTENTS
, 0);
2150 if (fp
->f_type
!= DTYPE_VNODE
) {
2155 /* For now, special case HFS+ only, since this is SPI. */
2156 src_vp
= (struct vnode
*)fp
->f_data
;
2157 if (src_vp
->v_tag
!= VT_HFS
) {
2163 * Get the references before we start acquiring iocounts on the vnodes,
2164 * while we still hold the proc fd lock
2166 if ( (error
= fp_lookup(p
, fd2
, &fp2
, 1)) ) {
2170 if (fp2
->f_type
!= DTYPE_VNODE
) {
2171 fp_drop(p
, fd2
, fp2
, 1);
2175 dst_vp
= (struct vnode
*)fp2
->f_data
;
2176 if (dst_vp
->v_tag
!= VT_HFS
) {
2177 fp_drop(p
, fd2
, fp2
, 1);
2183 /* Re-do MAC checks against the new FD, pass in a fake argument */
2184 error
= mac_file_check_fcntl(proc_ucred(p
), fp2
->f_fglob
, uap
->cmd
, 0);
2186 fp_drop(p
, fd2
, fp2
, 1);
2190 /* Audit the 2nd FD */
2195 if (vnode_getwithref(src_vp
)) {
2196 fp_drop(p
, fd2
, fp2
, 0);
2200 if (vnode_getwithref(dst_vp
)) {
2202 fp_drop(p
, fd2
, fp2
, 0);
2208 * Basic asserts; validate they are not the same and that
2209 * both live on the same filesystem.
2211 if (dst_vp
== src_vp
) {
2214 fp_drop (p
, fd2
, fp2
, 0);
2219 if (dst_vp
->v_mount
!= src_vp
->v_mount
) {
2222 fp_drop (p
, fd2
, fp2
, 0);
2227 /* Now we have a legit pair of FDs. Go to work */
2229 /* Now check for write access to the target files */
2230 if(vnode_authorize(src_vp
, NULLVP
,
2231 (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), &context
) != 0) {
2234 fp_drop(p
, fd2
, fp2
, 0);
2239 if(vnode_authorize(dst_vp
, NULLVP
,
2240 (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), &context
) != 0) {
2243 fp_drop(p
, fd2
, fp2
, 0);
2248 /* Verify that both vps point to files and not directories */
2249 if ( !vnode_isreg(src_vp
) || !vnode_isreg(dst_vp
)) {
2253 fp_drop (p
, fd2
, fp2
, 0);
2258 * The exchangedata syscall handler passes in 0 for the flags to VNOP_EXCHANGE.
2259 * We'll pass in our special bit indicating that the new behavior is expected
2262 error
= VNOP_EXCHANGE(src_vp
, dst_vp
, FSOPT_EXCHANGE_DATA_ONLY
, &context
);
2266 fp_drop(p
, fd2
, fp2
, 0);
2271 * SPI for making a file compressed.
2273 case F_MAKECOMPRESSED
: {
2274 uint32_t gcounter
= CAST_DOWN_EXPLICIT(uint32_t, uap
->arg
);
2276 if (fp
->f_type
!= DTYPE_VNODE
) {
2281 vp
= (struct vnode
*) fp
->f_data
;
2285 if (vnode_getwithref(vp
)) {
2291 if ((vnode_isreg(vp
) == 0) && (vnode_islnk(vp
) == 0)) {
2297 /* invoke ioctl to pass off to FS */
2298 /* Only go forward if you have write access */
2299 vfs_context_t ctx
= vfs_context_current();
2300 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2306 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)&gcounter
, 0, &context
);
2313 * SPI (private) for indicating to a filesystem that subsequent writes to
2314 * the open FD will written to the Fastflow.
2316 case F_SET_GREEDY_MODE
:
2317 /* intentionally drop through to the same handler as F_SETSTATIC.
2318 * both fcntls should pass the argument and their selector into VNOP_IOCTL.
2322 * SPI (private) for indicating to a filesystem that subsequent writes to
2323 * the open FD will represent static content.
2325 case F_SETSTATICCONTENT
: {
2326 caddr_t ioctl_arg
= NULL
;
2329 ioctl_arg
= (caddr_t
) 1;
2332 if (fp
->f_type
!= DTYPE_VNODE
) {
2336 vp
= (struct vnode
*)fp
->f_data
;
2339 error
= vnode_getwithref(vp
);
2345 /* Only go forward if you have write access */
2346 vfs_context_t ctx
= vfs_context_current();
2347 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2353 error
= VNOP_IOCTL(vp
, uap
->cmd
, ioctl_arg
, 0, &context
);
2354 (void)vnode_put(vp
);
2360 * SPI (private) for indicating to the lower level storage driver that the
2361 * subsequent writes should be of a particular IO type (burst, greedy, static),
2362 * or other flavors that may be necessary.
2369 /* extract 32 bits of flags from userland */
2370 param_ptr
= (caddr_t
) uap
->arg
;
2371 param
= (uint32_t) param_ptr
;
2374 /* If no argument is specified, error out */
2380 * Validate the different types of flags that can be specified:
2381 * all of them are mutually exclusive for now.
2384 case F_IOTYPE_ISOCHRONOUS
:
2393 if (fp
->f_type
!= DTYPE_VNODE
) {
2397 vp
= (struct vnode
*)fp
->f_data
;
2400 error
= vnode_getwithref(vp
);
2406 /* Only go forward if you have write access */
2407 vfs_context_t ctx
= vfs_context_current();
2408 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2414 error
= VNOP_IOCTL(vp
, uap
->cmd
, param_ptr
, 0, &context
);
2415 (void)vnode_put(vp
);
2421 * Set the vnode pointed to by 'fd'
2422 * and tag it as the (potentially future) backing store
2423 * for another filesystem
2425 case F_SETBACKINGSTORE
: {
2426 if (fp
->f_type
!= DTYPE_VNODE
) {
2431 vp
= (struct vnode
*)fp
->f_data
;
2433 if (vp
->v_tag
!= VT_HFS
) {
2439 if (vnode_getwithref(vp
)) {
2444 /* only proceed if you have write access */
2445 vfs_context_t ctx
= vfs_context_current();
2446 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2453 /* If arg != 0, set, otherwise unset */
2455 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
)1, 0, &context
);
2458 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
2466 * like F_GETPATH, but special semantics for
2467 * the mobile time machine handler.
2469 case F_GETPATH_MTMINFO
: {
2473 if (fp
->f_type
!= DTYPE_VNODE
) {
2477 vp
= (struct vnode
*)fp
->f_data
;
2480 pathlen
= MAXPATHLEN
;
2481 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
2482 if (pathbufp
== NULL
) {
2486 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
2487 int backingstore
= 0;
2489 /* Check for error from vn_getpath before moving on */
2490 if ((error
= vn_getpath(vp
, pathbufp
, &pathlen
)) == 0) {
2491 if (vp
->v_tag
== VT_HFS
) {
2492 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
) &backingstore
, 0, &context
);
2494 (void)vnode_put(vp
);
2497 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
2501 * If the copyout was successful, now check to ensure
2502 * that this vnode is not a BACKINGSTORE vnode. mtmd
2503 * wants the path regardless.
2510 (void)vnode_put(vp
);
2512 FREE(pathbufp
, M_TEMP
);
2516 #if DEBUG || DEVELOPMENT
2518 if (fp
->f_type
!= DTYPE_VNODE
) {
2522 vp
= (struct vnode
*)fp
->f_data
;
2531 * This is an fcntl() that we d not recognize at this level;
2532 * if this is a vnode, we send it down into the VNOP_IOCTL
2533 * for this vnode; this can include special devices, and will
2534 * effectively overload fcntl() to send ioctl()'s.
2536 if((uap
->cmd
& IOC_VOID
) && (uap
->cmd
& IOC_INOUT
)){
2541 /* Catch any now-invalid fcntl() selectors */
2543 case F_MARKDEPENDENCY
:
2550 if (fp
->f_type
!= DTYPE_VNODE
) {
2554 vp
= (struct vnode
*)fp
->f_data
;
2557 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
2558 #define STK_PARAMS 128
2559 char stkbuf
[STK_PARAMS
] = {0};
2563 * For this to work properly, we have to copy in the
2564 * ioctl() cmd argument if there is one; we must also
2565 * check that a command parameter, if present, does
2566 * not exceed the maximum command length dictated by
2567 * the number of bits we have available in the command
2568 * to represent a structure length. Finally, we have
2569 * to copy the results back out, if it is that type of
2572 size
= IOCPARM_LEN(uap
->cmd
);
2573 if (size
> IOCPARM_MAX
) {
2574 (void)vnode_put(vp
);
2580 if (size
> sizeof (stkbuf
)) {
2581 if ((memp
= (caddr_t
)kalloc(size
)) == 0) {
2582 (void)vnode_put(vp
);
2591 if (uap
->cmd
& IOC_IN
) {
2594 error
= copyin(argp
, data
, size
);
2596 (void)vnode_put(vp
);
2602 /* Bzero the section beyond that which was needed */
2603 if (size
<= sizeof(stkbuf
)) {
2604 bzero ( (((uint8_t*)data
) + size
), (sizeof(stkbuf
) - size
));
2609 *(user_addr_t
*)data
= argp
;
2611 *(uint32_t *)data
= (uint32_t)argp
;
2614 } else if ((uap
->cmd
& IOC_OUT
) && size
) {
2616 * Zero the buffer so the user always
2617 * gets back something deterministic.
2620 } else if (uap
->cmd
& IOC_VOID
) {
2622 *(user_addr_t
*)data
= argp
;
2624 *(uint32_t *)data
= (uint32_t)argp
;
2628 error
= VNOP_IOCTL(vp
, uap
->cmd
, CAST_DOWN(caddr_t
, data
), 0, &context
);
2630 (void)vnode_put(vp
);
2632 /* Copy any output data to user */
2633 if (error
== 0 && (uap
->cmd
& IOC_OUT
) && size
)
2634 error
= copyout(data
, argp
, size
);
2642 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
2643 fp_drop(p
, fd
, fp
, 0);
2646 fp_drop(p
, fd
, fp
, 1);
2655 * Description: Common code for dup, dup2, and fcntl(F_DUPFD).
2657 * Parameters: p Process performing the dup
2659 * new The fd to dup it to
2660 * fd_flags Flags to augment the new fd
2661 * retval Pointer to the call return area
2663 * Returns: 0 Success
2668 * *retval (modified) The new descriptor
2670 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
2673 * Notes: This function may drop and reacquire this lock; it is unsafe
2674 * for a caller to assume that other state protected by the lock
2675 * has not been subsequently changed out from under it.
2679 struct filedesc
*fdp
, int old
, int new, int fd_flags
, int32_t *retval
)
2681 struct fileproc
*nfp
;
2682 struct fileproc
*ofp
;
2688 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2690 if ((ofp
= fdp
->fd_ofiles
[old
]) == NULL
||
2691 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
)) {
2698 error
= mac_file_check_dup(proc_ucred(p
), ofp
->f_fglob
, new);
2708 nfp
= fileproc_alloc_init(NULL
);
2718 nfp
->f_fglob
= ofp
->f_fglob
;
2721 if (fdp
->fd_ofiles
[new] != 0)
2722 panic("finishdup: overwriting fd_ofiles with new %d", new);
2723 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0)
2724 panic("finishdup: unreserved fileflags with new %d", new);
2727 if (new > fdp
->fd_lastfile
)
2728 fdp
->fd_lastfile
= new;
2729 *fdflags(p
, new) |= fd_flags
;
2730 procfdtbl_releasefd(p
, new, nfp
);
2739 * Description: The implementation of the close(2) system call
2741 * Parameters: p Process in whose per process file table
2742 * the close is to occur
2743 * uap->fd fd to be closed
2746 * Returns: 0 Success
2747 * fp_lookup:EBADF Bad file descriptor
2748 * fp_guard_exception:??? Guarded file descriptor
2749 * close_internal:EBADF
2750 * close_internal:??? Anything returnable by a per-fileops
2754 close(proc_t p
, struct close_args
*uap
, int32_t *retval
)
2756 __pthread_testcancel(1);
2757 return(close_nocancel(p
, (struct close_nocancel_args
*)uap
, retval
));
2762 close_nocancel(proc_t p
, struct close_nocancel_args
*uap
, __unused
int32_t *retval
)
2764 struct fileproc
*fp
;
2768 AUDIT_SYSCLOSE(p
, fd
);
2772 if ( (error
= fp_lookup(p
,fd
,&fp
, 1)) ) {
2777 if (FP_ISGUARDED(fp
, GUARD_CLOSE
)) {
2778 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_CLOSE
);
2779 (void) fp_drop(p
, fd
, fp
, 1);
2784 error
= close_internal_locked(p
, fd
, fp
, 0);
2793 * close_internal_locked
2795 * Close a file descriptor.
2797 * Parameters: p Process in whose per process file table
2798 * the close is to occur
2799 * fd fd to be closed
2800 * fp fileproc associated with the fd
2802 * Returns: 0 Success
2803 * EBADF fd already in close wait state
2804 * closef_locked:??? Anything returnable by a per-fileops
2807 * Locks: Assumes proc_fdlock for process is held by the caller and returns
2810 * Notes: This function may drop and reacquire this lock; it is unsafe
2811 * for a caller to assume that other state protected by the lock
2812 * has not been subsequently changed out from under it.
2815 close_internal_locked(proc_t p
, int fd
, struct fileproc
*fp
, int flags
)
2817 struct filedesc
*fdp
= p
->p_fd
;
2819 int resvfd
= flags
& FD_DUP2RESV
;
2823 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2826 /* Keep people from using the filedesc while we are closing it */
2827 procfdtbl_markclosefd(p
, fd
);
2830 if ((fp
->f_flags
& FP_CLOSING
) == FP_CLOSING
) {
2831 panic("close_internal_locked: being called on already closing fd");
2836 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
2837 panic("close_internal: unreserved fileflags with fd %d", fd
);
2840 fp
->f_flags
|= FP_CLOSING
;
2842 if ( (fp
->f_flags
& FP_AIOISSUED
) || kauth_authorize_fileop_has_listeners() ) {
2846 if ( (fp
->f_type
== DTYPE_VNODE
) && kauth_authorize_fileop_has_listeners() ) {
2848 * call out to allow 3rd party notification of close.
2849 * Ignore result of kauth_authorize_fileop call.
2851 if (vnode_getwithref((vnode_t
)fp
->f_data
) == 0) {
2852 u_int fileop_flags
= 0;
2853 if ((fp
->f_flags
& FP_WRITTEN
) != 0)
2854 fileop_flags
|= KAUTH_FILEOP_CLOSE_MODIFIED
;
2855 kauth_authorize_fileop(fp
->f_fglob
->fg_cred
, KAUTH_FILEOP_CLOSE
,
2856 (uintptr_t)fp
->f_data
, (uintptr_t)fileop_flags
);
2857 vnode_put((vnode_t
)fp
->f_data
);
2860 if (fp
->f_flags
& FP_AIOISSUED
)
2862 * cancel all async IO requests that can be cancelled.
2864 _aio_close( p
, fd
);
2869 if (fd
< fdp
->fd_knlistsize
)
2870 knote_fdclose(p
, fd
, FALSE
);
2872 if (fp
->f_flags
& FP_WAITEVENT
)
2873 (void)waitevent_close(p
, fp
);
2875 fileproc_drain(p
, fp
);
2880 procfdtbl_reservefd(p
, fd
);
2883 if (ENTR_SHOULDTRACE
&& fp
->f_type
== DTYPE_SOCKET
)
2884 KERNEL_ENERGYTRACE(kEnTrActKernSocket
, DBG_FUNC_END
,
2885 fd
, 0, (int64_t)VM_KERNEL_ADDRPERM(fp
->f_data
));
2887 error
= closef_locked(fp
, fp
->f_fglob
, p
);
2888 if ((fp
->f_flags
& FP_WAITCLOSE
) == FP_WAITCLOSE
)
2889 wakeup(&fp
->f_flags
);
2890 fp
->f_flags
&= ~(FP_WAITCLOSE
| FP_CLOSING
);
2900 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
2901 panic("close with reserved fd returns with freed fd:%d: proc: %p", fd
, p
);
2912 * Description: Return status information about a file descriptor.
2914 * Parameters: p The process doing the fstat
2916 * ub The user stat buffer
2917 * xsecurity The user extended security
2918 * buffer, or 0 if none
2919 * xsecurity_size The size of xsecurity, or 0
2921 * isstat64 Flag to indicate 64 bit version
2922 * for inode size, etc.
2924 * Returns: 0 Success
2927 * fp_lookup:EBADF Bad file descriptor
2928 * vnode_getwithref:???
2930 * vnode_getwithref:???
2937 * Notes: Internal implementation for all other fstat() related
2940 * XXX switch on node type is bogus; need a stat in struct
2941 * XXX fileops instead.
2944 fstat1(proc_t p
, int fd
, user_addr_t ub
, user_addr_t xsecurity
, user_addr_t xsecurity_size
, int isstat64
)
2946 struct fileproc
*fp
;
2952 struct user64_stat user64_sb
;
2953 struct user32_stat user32_sb
;
2954 struct user64_stat64 user64_sb64
;
2955 struct user32_stat64 user32_sb64
;
2960 kauth_filesec_t fsec
;
2961 user_size_t xsecurity_bufsize
;
2962 vfs_context_t ctx
= vfs_context_current();
2968 if ((error
= fp_lookup(p
, fd
, &fp
, 0)) != 0) {
2973 fsec
= KAUTH_FILESEC_NONE
;
2975 sbptr
= (void *)&source
;
2980 if ((error
= vnode_getwithref((vnode_t
)data
)) == 0) {
2982 * If the caller has the file open, and is not
2983 * requesting extended security information, we are
2984 * going to let them get the basic stat information.
2986 if (xsecurity
== USER_ADDR_NULL
) {
2987 error
= vn_stat_noauth((vnode_t
)data
, sbptr
, NULL
, isstat64
, ctx
,
2988 fp
->f_fglob
->fg_cred
);
2990 error
= vn_stat((vnode_t
)data
, sbptr
, &fsec
, isstat64
, ctx
);
2993 AUDIT_ARG(vnpath
, (struct vnode
*)data
, ARG_VNODE1
);
2994 (void)vnode_put((vnode_t
)data
);
3000 error
= soo_stat((struct socket
*)data
, sbptr
, isstat64
);
3002 #endif /* SOCKETS */
3005 error
= pipe_stat((void *)data
, sbptr
, isstat64
);
3009 error
= pshm_stat((void *)data
, sbptr
, isstat64
);
3013 error
= kqueue_stat((void *)data
, sbptr
, isstat64
, p
);
3023 if (isstat64
!= 0) {
3024 source
.sb64
.st_lspare
= 0;
3025 source
.sb64
.st_qspare
[0] = 0LL;
3026 source
.sb64
.st_qspare
[1] = 0LL;
3028 if (IS_64BIT_PROCESS(current_proc())) {
3029 munge_user64_stat64(&source
.sb64
, &dest
.user64_sb64
);
3030 my_size
= sizeof(dest
.user64_sb64
);
3031 sbp
= (caddr_t
)&dest
.user64_sb64
;
3033 munge_user32_stat64(&source
.sb64
, &dest
.user32_sb64
);
3034 my_size
= sizeof(dest
.user32_sb64
);
3035 sbp
= (caddr_t
)&dest
.user32_sb64
;
3038 source
.sb
.st_lspare
= 0;
3039 source
.sb
.st_qspare
[0] = 0LL;
3040 source
.sb
.st_qspare
[1] = 0LL;
3041 if (IS_64BIT_PROCESS(current_proc())) {
3042 munge_user64_stat(&source
.sb
, &dest
.user64_sb
);
3043 my_size
= sizeof(dest
.user64_sb
);
3044 sbp
= (caddr_t
)&dest
.user64_sb
;
3046 munge_user32_stat(&source
.sb
, &dest
.user32_sb
);
3047 my_size
= sizeof(dest
.user32_sb
);
3048 sbp
= (caddr_t
)&dest
.user32_sb
;
3052 error
= copyout(sbp
, ub
, my_size
);
3055 /* caller wants extended security information? */
3056 if (xsecurity
!= USER_ADDR_NULL
) {
3058 /* did we get any? */
3059 if (fsec
== KAUTH_FILESEC_NONE
) {
3060 if (susize(xsecurity_size
, 0) != 0) {
3065 /* find the user buffer size */
3066 xsecurity_bufsize
= fusize(xsecurity_size
);
3068 /* copy out the actual data size */
3069 if (susize(xsecurity_size
, KAUTH_FILESEC_COPYSIZE(fsec
)) != 0) {
3074 /* if the caller supplied enough room, copy out to it */
3075 if (xsecurity_bufsize
>= KAUTH_FILESEC_COPYSIZE(fsec
))
3076 error
= copyout(fsec
, xsecurity
, KAUTH_FILESEC_COPYSIZE(fsec
));
3080 fp_drop(p
, fd
, fp
, 0);
3082 kauth_filesec_free(fsec
);
3090 * Description: Extended version of fstat supporting returning extended
3091 * security information
3093 * Parameters: p The process doing the fstat
3094 * uap->fd The fd to stat
3095 * uap->ub The user stat buffer
3096 * uap->xsecurity The user extended security
3097 * buffer, or 0 if none
3098 * uap->xsecurity_size The size of xsecurity, or 0
3100 * Returns: 0 Success
3101 * !0 Errno (see fstat1)
3104 fstat_extended(proc_t p
, struct fstat_extended_args
*uap
, __unused
int32_t *retval
)
3106 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 0));
3113 * Description: Get file status for the file associated with fd
3115 * Parameters: p The process doing the fstat
3116 * uap->fd The fd to stat
3117 * uap->ub The user stat buffer
3119 * Returns: 0 Success
3120 * !0 Errno (see fstat1)
3123 fstat(proc_t p
, struct fstat_args
*uap
, __unused
int32_t *retval
)
3125 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 0));
3132 * Description: Extended version of fstat64 supporting returning extended
3133 * security information
3135 * Parameters: p The process doing the fstat
3136 * uap->fd The fd to stat
3137 * uap->ub The user stat buffer
3138 * uap->xsecurity The user extended security
3139 * buffer, or 0 if none
3140 * uap->xsecurity_size The size of xsecurity, or 0
3142 * Returns: 0 Success
3143 * !0 Errno (see fstat1)
3146 fstat64_extended(proc_t p
, struct fstat64_extended_args
*uap
, __unused
int32_t *retval
)
3148 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 1));
3155 * Description: Get 64 bit version of the file status for the file associated
3158 * Parameters: p The process doing the fstat
3159 * uap->fd The fd to stat
3160 * uap->ub The user stat buffer
3162 * Returns: 0 Success
3163 * !0 Errno (see fstat1)
3166 fstat64(proc_t p
, struct fstat64_args
*uap
, __unused
int32_t *retval
)
3168 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 1));
3175 * Description: Return pathconf information about a file descriptor.
3177 * Parameters: p Process making the request
3178 * uap->fd fd to get information about
3179 * uap->name Name of information desired
3180 * retval Pointer to the call return area
3182 * Returns: 0 Success
3184 * fp_lookup:EBADF Bad file descriptor
3185 * vnode_getwithref:???
3189 * *retval (modified) Returned information (numeric)
3192 fpathconf(proc_t p
, struct fpathconf_args
*uap
, int32_t *retval
)
3195 struct fileproc
*fp
;
3202 AUDIT_ARG(fd
, uap
->fd
);
3203 if ( (error
= fp_lookup(p
, fd
, &fp
, 0)) )
3211 if (uap
->name
!= _PC_PIPE_BUF
) {
3220 if (uap
->name
!= _PC_PIPE_BUF
) {
3229 vp
= (struct vnode
*)data
;
3231 if ( (error
= vnode_getwithref(vp
)) == 0) {
3232 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
3234 error
= vn_pathconf(vp
, uap
->name
, retval
, vfs_context_current());
3236 (void)vnode_put(vp
);
3247 fp_drop(p
, fd
, fp
, 0);
3252 * Statistics counter for the number of times a process calling fdalloc()
3253 * has resulted in an expansion of the per process open file table.
3255 * XXX This would likely be of more use if it were per process
3263 * Description: Allocate a file descriptor for the process.
3265 * Parameters: p Process to allocate the fd in
3266 * want The fd we would prefer to get
3267 * result Pointer to fd we got
3269 * Returns: 0 Success
3274 * *result (modified) The fd which was allocated
3277 fdalloc(proc_t p
, int want
, int *result
)
3279 struct filedesc
*fdp
= p
->p_fd
;
3281 int lim
, last
, numfiles
, oldnfiles
;
3282 struct fileproc
**newofiles
, **ofiles
;
3283 char *newofileflags
;
3286 * Search for a free descriptor starting at the higher
3287 * of want or fd_freefile. If that fails, consider
3288 * expanding the ofile array.
3291 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
3294 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
3296 last
= min(fdp
->fd_nfiles
, lim
);
3297 if ((i
= want
) < fdp
->fd_freefile
)
3298 i
= fdp
->fd_freefile
;
3299 for (; i
< last
; i
++) {
3300 if (fdp
->fd_ofiles
[i
] == NULL
&& !(fdp
->fd_ofileflags
[i
] & UF_RESERVED
)) {
3301 procfdtbl_reservefd(p
, i
);
3302 if (i
> fdp
->fd_lastfile
)
3303 fdp
->fd_lastfile
= i
;
3304 if (want
<= fdp
->fd_freefile
)
3305 fdp
->fd_freefile
= i
;
3312 * No space in current array. Expand?
3314 if (fdp
->fd_nfiles
>= lim
)
3316 if (fdp
->fd_nfiles
< NDEXTENT
)
3317 numfiles
= NDEXTENT
;
3319 numfiles
= 2 * fdp
->fd_nfiles
;
3324 MALLOC_ZONE(newofiles
, struct fileproc
**,
3325 numfiles
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
3327 if (newofiles
== NULL
) {
3330 if (fdp
->fd_nfiles
>= numfiles
) {
3331 FREE_ZONE(newofiles
, numfiles
* OFILESIZE
, M_OFILETABL
);
3334 newofileflags
= (char *) &newofiles
[numfiles
];
3336 * Copy the existing ofile and ofileflags arrays
3337 * and zero the new portion of each array.
3339 oldnfiles
= fdp
->fd_nfiles
;
3340 (void) memcpy(newofiles
, fdp
->fd_ofiles
,
3341 oldnfiles
* sizeof(*fdp
->fd_ofiles
));
3342 (void) memset(&newofiles
[oldnfiles
], 0,
3343 (numfiles
- oldnfiles
) * sizeof(*fdp
->fd_ofiles
));
3345 (void) memcpy(newofileflags
, fdp
->fd_ofileflags
,
3346 oldnfiles
* sizeof(*fdp
->fd_ofileflags
));
3347 (void) memset(&newofileflags
[oldnfiles
], 0,
3348 (numfiles
- oldnfiles
) *
3349 sizeof(*fdp
->fd_ofileflags
));
3350 ofiles
= fdp
->fd_ofiles
;
3351 fdp
->fd_ofiles
= newofiles
;
3352 fdp
->fd_ofileflags
= newofileflags
;
3353 fdp
->fd_nfiles
= numfiles
;
3354 FREE_ZONE(ofiles
, oldnfiles
* OFILESIZE
, M_OFILETABL
);
3363 * Description: Check to see whether n user file descriptors are available
3366 * Parameters: p Process to check in
3367 * n The number of fd's desired
3372 * Locks: Assumes proc_fdlock for process is held by the caller
3374 * Notes: The answer only remains valid so long as the proc_fdlock is
3375 * held by the caller.
3378 fdavail(proc_t p
, int n
)
3380 struct filedesc
*fdp
= p
->p_fd
;
3381 struct fileproc
**fpp
;
3385 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
3386 if ((i
= lim
- fdp
->fd_nfiles
) > 0 && (n
-= i
) <= 0)
3388 fpp
= &fdp
->fd_ofiles
[fdp
->fd_freefile
];
3389 flags
= &fdp
->fd_ofileflags
[fdp
->fd_freefile
];
3390 for (i
= fdp
->fd_nfiles
- fdp
->fd_freefile
; --i
>= 0; fpp
++, flags
++)
3391 if (*fpp
== NULL
&& !(*flags
& UF_RESERVED
) && --n
<= 0)
3400 * Description: Legacy KPI wrapper function for _fdrelse
3402 * Parameters: p Process in which fd lives
3407 * Locks: Assumes proc_fdlock for process is held by the caller
3410 fdrelse(proc_t p
, int fd
)
3419 * Description: Get the fileproc pointer for the given fd from the per process
3420 * open file table without taking an explicit reference on it.
3422 * Parameters: p Process containing fd
3423 * fd fd to obtain fileproc for
3424 * resultfp Pointer to pointer return area
3426 * Returns: 0 Success
3430 * *resultfp (modified) Pointer to fileproc pointer
3432 * Locks: Assumes proc_fdlock for process is held by the caller
3434 * Notes: Because there is no reference explicitly taken, the returned
3435 * fileproc pointer is only valid so long as the proc_fdlock
3436 * remains held by the caller.
3439 fdgetf_noref(proc_t p
, int fd
, struct fileproc
**resultfp
)
3441 struct filedesc
*fdp
= p
->p_fd
;
3442 struct fileproc
*fp
;
3444 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3445 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3446 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3458 * Description: Get fileproc and vnode pointer for a given fd from the per
3459 * process open file table of the specified process, and if
3460 * successful, increment the f_iocount
3462 * Parameters: p Process in which fd lives
3463 * fd fd to get information for
3464 * resultfp Pointer to result fileproc
3465 * pointer area, or 0 if none
3466 * resultvp Pointer to result vnode pointer
3467 * area, or 0 if none
3469 * Returns: 0 Success
3470 * EBADF Bad file descriptor
3471 * ENOTSUP fd does not refer to a vnode
3474 * *resultfp (modified) Fileproc pointer
3475 * *resultvp (modified) vnode pointer
3477 * Notes: The resultfp and resultvp fields are optional, and may be
3478 * independently specified as NULL to skip returning information
3480 * Locks: Internally takes and releases proc_fdlock
3483 fp_getfvp(proc_t p
, int fd
, struct fileproc
**resultfp
, struct vnode
**resultvp
)
3485 struct filedesc
*fdp
= p
->p_fd
;
3486 struct fileproc
*fp
;
3488 proc_fdlock_spin(p
);
3489 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3490 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3491 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3495 if (fp
->f_type
!= DTYPE_VNODE
) {
3504 *resultvp
= (struct vnode
*)fp
->f_data
;
3514 * Description: Get fileproc, vnode pointer, and vid for a given fd from the
3515 * per process open file table of the specified process, and if
3516 * successful, increment the f_iocount
3518 * Parameters: p Process in which fd lives
3519 * fd fd to get information for
3520 * resultfp Pointer to result fileproc
3521 * pointer area, or 0 if none
3522 * resultvp Pointer to result vnode pointer
3523 * area, or 0 if none
3524 * vidp Pointer to resuld vid area
3526 * Returns: 0 Success
3527 * EBADF Bad file descriptor
3528 * ENOTSUP fd does not refer to a vnode
3531 * *resultfp (modified) Fileproc pointer
3532 * *resultvp (modified) vnode pointer
3535 * Notes: The resultfp and resultvp fields are optional, and may be
3536 * independently specified as NULL to skip returning information
3538 * Locks: Internally takes and releases proc_fdlock
3541 fp_getfvpandvid(proc_t p
, int fd
, struct fileproc
**resultfp
,
3542 struct vnode
**resultvp
, uint32_t *vidp
)
3544 struct filedesc
*fdp
= p
->p_fd
;
3545 struct fileproc
*fp
;
3547 proc_fdlock_spin(p
);
3548 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3549 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3550 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3554 if (fp
->f_type
!= DTYPE_VNODE
) {
3563 *resultvp
= (struct vnode
*)fp
->f_data
;
3565 *vidp
= (uint32_t)vnode_vid((struct vnode
*)fp
->f_data
);
3575 * Description: Get fileproc and socket pointer for a given fd from the
3576 * per process open file table of the specified process, and if
3577 * successful, increment the f_iocount
3579 * Parameters: p Process in which fd lives
3580 * fd fd to get information for
3581 * resultfp Pointer to result fileproc
3582 * pointer area, or 0 if none
3583 * results Pointer to result socket
3584 * pointer area, or 0 if none
3586 * Returns: EBADF The file descriptor is invalid
3587 * EOPNOTSUPP The file descriptor is not a socket
3591 * *resultfp (modified) Fileproc pointer
3592 * *results (modified) socket pointer
3594 * Notes: EOPNOTSUPP should probably be ENOTSOCK; this function is only
3595 * ever called from accept1().
3598 fp_getfsock(proc_t p
, int fd
, struct fileproc
**resultfp
,
3599 struct socket
**results
)
3601 struct filedesc
*fdp
= p
->p_fd
;
3602 struct fileproc
*fp
;
3604 proc_fdlock_spin(p
);
3605 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3606 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3607 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3611 if (fp
->f_type
!= DTYPE_SOCKET
) {
3620 *results
= (struct socket
*)fp
->f_data
;
3630 * Description: Get fileproc and kqueue pointer for a given fd from the
3631 * per process open file table of the specified process, and if
3632 * successful, increment the f_iocount
3634 * Parameters: p Process in which fd lives
3635 * fd fd to get information for
3636 * resultfp Pointer to result fileproc
3637 * pointer area, or 0 if none
3638 * resultkq Pointer to result kqueue
3639 * pointer area, or 0 if none
3641 * Returns: EBADF The file descriptor is invalid
3642 * EBADF The file descriptor is not a socket
3646 * *resultfp (modified) Fileproc pointer
3647 * *resultkq (modified) kqueue pointer
3649 * Notes: The second EBADF should probably be something else to make
3650 * the error condition distinct.
3653 fp_getfkq(proc_t p
, int fd
, struct fileproc
**resultfp
,
3654 struct kqueue
**resultkq
)
3656 struct filedesc
*fdp
= p
->p_fd
;
3657 struct fileproc
*fp
;
3659 proc_fdlock_spin(p
);
3660 if ( fd
< 0 || fd
>= fdp
->fd_nfiles
||
3661 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3662 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3666 if (fp
->f_type
!= DTYPE_KQUEUE
) {
3675 *resultkq
= (struct kqueue
*)fp
->f_data
;
3685 * Description: Get fileproc and POSIX shared memory pointer for a given fd
3686 * from the per process open file table of the specified process
3687 * and if successful, increment the f_iocount
3689 * Parameters: p Process in which fd lives
3690 * fd fd to get information for
3691 * resultfp Pointer to result fileproc
3692 * pointer area, or 0 if none
3693 * resultpshm Pointer to result POSIX
3694 * shared memory pointer
3695 * pointer area, or 0 if none
3697 * Returns: EBADF The file descriptor is invalid
3698 * EBADF The file descriptor is not a POSIX
3699 * shared memory area
3703 * *resultfp (modified) Fileproc pointer
3704 * *resultpshm (modified) POSIX shared memory pointer
3706 * Notes: The second EBADF should probably be something else to make
3707 * the error condition distinct.
3710 fp_getfpshm(proc_t p
, int fd
, struct fileproc
**resultfp
,
3711 struct pshmnode
**resultpshm
)
3713 struct filedesc
*fdp
= p
->p_fd
;
3714 struct fileproc
*fp
;
3716 proc_fdlock_spin(p
);
3717 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3718 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3719 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3723 if (fp
->f_type
!= DTYPE_PSXSHM
) {
3733 *resultpshm
= (struct pshmnode
*)fp
->f_data
;
3743 * Description: Get fileproc and POSIX semaphore pointer for a given fd from
3744 * the per process open file table of the specified process
3745 * and if successful, increment the f_iocount
3747 * Parameters: p Process in which fd lives
3748 * fd fd to get information for
3749 * resultfp Pointer to result fileproc
3750 * pointer area, or 0 if none
3751 * resultpsem Pointer to result POSIX
3752 * semaphore pointer area, or
3755 * Returns: EBADF The file descriptor is invalid
3756 * EBADF The file descriptor is not a POSIX
3761 * *resultfp (modified) Fileproc pointer
3762 * *resultpsem (modified) POSIX semaphore pointer
3764 * Notes: The second EBADF should probably be something else to make
3765 * the error condition distinct.
3767 * In order to support unnamed POSIX semaphores, the named
3768 * POSIX semaphores will have to move out of the per-process
3769 * open filetable, and into a global table that is shared with
3770 * unnamed POSIX semaphores, since unnamed POSIX semaphores
3771 * are typically used by declaring instances in shared memory,
3772 * and there's no other way to do this without changing the
3773 * underlying type, which would introduce binary compatibility
3777 fp_getfpsem(proc_t p
, int fd
, struct fileproc
**resultfp
,
3778 struct psemnode
**resultpsem
)
3780 struct filedesc
*fdp
= p
->p_fd
;
3781 struct fileproc
*fp
;
3783 proc_fdlock_spin(p
);
3784 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3785 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3786 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3790 if (fp
->f_type
!= DTYPE_PSXSEM
) {
3799 *resultpsem
= (struct psemnode
*)fp
->f_data
;
3809 * Description: Get fileproc and pipe pointer for a given fd from the
3810 * per process open file table of the specified process
3811 * and if successful, increment the f_iocount
3813 * Parameters: p Process in which fd lives
3814 * fd fd to get information for
3815 * resultfp Pointer to result fileproc
3816 * pointer area, or 0 if none
3817 * resultpipe Pointer to result pipe
3818 * pointer area, or 0 if none
3820 * Returns: EBADF The file descriptor is invalid
3821 * EBADF The file descriptor is not a socket
3825 * *resultfp (modified) Fileproc pointer
3826 * *resultpipe (modified) pipe pointer
3828 * Notes: The second EBADF should probably be something else to make
3829 * the error condition distinct.
3832 fp_getfpipe(proc_t p
, int fd
, struct fileproc
**resultfp
,
3833 struct pipe
**resultpipe
)
3835 struct filedesc
*fdp
= p
->p_fd
;
3836 struct fileproc
*fp
;
3838 proc_fdlock_spin(p
);
3839 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3840 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3841 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3845 if (fp
->f_type
!= DTYPE_PIPE
) {
3854 *resultpipe
= (struct pipe
*)fp
->f_data
;
3864 * Description: Get fileproc pointer for a given fd from the per process
3865 * open file table of the specified process and if successful,
3866 * increment the f_iocount
3868 * Parameters: p Process in which fd lives
3869 * fd fd to get information for
3870 * resultfp Pointer to result fileproc
3871 * pointer area, or 0 if none
3872 * locked !0 if the caller holds the
3873 * proc_fdlock, 0 otherwise
3875 * Returns: 0 Success
3876 * EBADF Bad file descriptor
3879 * *resultfp (modified) Fileproc pointer
3881 * Locks: If the argument 'locked' is non-zero, then the caller is
3882 * expected to have taken and held the proc_fdlock; if it is
3883 * zero, than this routine internally takes and drops this lock.
3886 fp_lookup(proc_t p
, int fd
, struct fileproc
**resultfp
, int locked
)
3888 struct filedesc
*fdp
= p
->p_fd
;
3889 struct fileproc
*fp
;
3892 proc_fdlock_spin(p
);
3893 if (fd
< 0 || fdp
== NULL
|| fd
>= fdp
->fd_nfiles
||
3894 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3895 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3914 * Description: Swap the fileproc pointer for a given fd with a new
3915 * fileproc pointer in the per-process open file table of
3916 * the specified process. The fdlock must be held at entry.
3918 * Parameters: p Process containing the fd
3919 * fd The fd of interest
3920 * nfp Pointer to the newfp
3922 * Returns: 0 Success
3923 * EBADF Bad file descriptor
3925 * EKEEPLOOKING f_iocount changed while lock was dropped.
3928 fp_tryswap(proc_t p
, int fd
, struct fileproc
*nfp
)
3930 struct fileproc
*fp
;
3933 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
3935 if (0 != (error
= fp_lookup(p
, fd
, &fp
, 1)))
3938 * At this point, our caller (change_guardedfd_np) has
3939 * one f_iocount reference, and we just took another
3940 * one to begin the replacement.
3942 if (fp
->f_iocount
< 2) {
3943 panic("f_iocount too small %d", fp
->f_iocount
);
3944 } else if (2 == fp
->f_iocount
) {
3946 /* Copy the contents of *fp, preserving the "type" of *nfp */
3948 nfp
->f_flags
= (nfp
->f_flags
& FP_TYPEMASK
) |
3949 (fp
->f_flags
& ~FP_TYPEMASK
);
3950 nfp
->f_iocount
= fp
->f_iocount
;
3951 nfp
->f_fglob
= fp
->f_fglob
;
3952 nfp
->f_wset
= fp
->f_wset
;
3954 p
->p_fd
->fd_ofiles
[fd
] = nfp
;
3955 (void) fp_drop(p
, fd
, nfp
, 1);
3958 * Wait for all other active references to evaporate.
3960 p
->p_fpdrainwait
= 1;
3961 error
= msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
,
3962 PRIBIO
| PCATCH
, "tryswap fpdrain", NULL
);
3965 * Return an "internal" errno to trigger a full
3966 * reevaluation of the change-guard attempt.
3968 error
= EKEEPLOOKING
;
3969 printf("%s: lookup collision fd %d\n", __func__
, fd
);
3971 (void) fp_drop(p
, fd
, fp
, 1);
3980 * Description: Set the FP_WRITTEN flag on the fileproc and drop the I/O
3981 * reference previously taken by calling fp_lookup et. al.
3983 * Parameters: p Process in which the fd lives
3984 * fd fd associated with the fileproc
3985 * fp fileproc on which to set the
3986 * flag and drop the reference
3988 * Returns: 0 Success
3989 * fp_drop:EBADF Bad file descriptor
3991 * Locks: This function internally takes and drops the proc_fdlock for
3992 * the supplied process
3994 * Notes: The fileproc must correspond to the fd in the supplied proc
3997 fp_drop_written(proc_t p
, int fd
, struct fileproc
*fp
)
4001 proc_fdlock_spin(p
);
4003 fp
->f_flags
|= FP_WRITTEN
;
4005 error
= fp_drop(p
, fd
, fp
, 1);
4016 * Description: Set the FP_WAITEVENT flag on the fileproc and drop the I/O
4017 * reference previously taken by calling fp_lookup et. al.
4019 * Parameters: p Process in which the fd lives
4020 * fd fd associated with the fileproc
4021 * fp fileproc on which to set the
4022 * flag and drop the reference
4024 * Returns: 0 Success
4025 * fp_drop:EBADF Bad file descriptor
4027 * Locks: This function internally takes and drops the proc_fdlock for
4028 * the supplied process
4030 * Notes: The fileproc must correspond to the fd in the supplied proc
4033 fp_drop_event(proc_t p
, int fd
, struct fileproc
*fp
)
4037 proc_fdlock_spin(p
);
4039 fp
->f_flags
|= FP_WAITEVENT
;
4041 error
= fp_drop(p
, fd
, fp
, 1);
4052 * Description: Drop the I/O reference previously taken by calling fp_lookup
4055 * Parameters: p Process in which the fd lives
4056 * fd fd associated with the fileproc
4057 * fp fileproc on which to set the
4058 * flag and drop the reference
4059 * locked flag to internally take and
4060 * drop proc_fdlock if it is not
4061 * already held by the caller
4063 * Returns: 0 Success
4064 * EBADF Bad file descriptor
4066 * Locks: This function internally takes and drops the proc_fdlock for
4067 * the supplied process if 'locked' is non-zero, and assumes that
4068 * the caller already holds this lock if 'locked' is non-zero.
4070 * Notes: The fileproc must correspond to the fd in the supplied proc
4073 fp_drop(proc_t p
, int fd
, struct fileproc
*fp
, int locked
)
4075 struct filedesc
*fdp
= p
->p_fd
;
4079 proc_fdlock_spin(p
);
4080 if ((fp
== FILEPROC_NULL
) && (fd
< 0 || fd
>= fdp
->fd_nfiles
||
4081 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
4082 ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
4083 !(fdp
->fd_ofileflags
[fd
] & UF_CLOSING
)))) {
4090 if (fp
->f_iocount
== 0) {
4091 if (fp
->f_flags
& FP_SELCONFLICT
)
4092 fp
->f_flags
&= ~FP_SELCONFLICT
;
4094 if (p
->p_fpdrainwait
) {
4095 p
->p_fpdrainwait
= 0;
4102 wakeup(&p
->p_fpdrainwait
);
4111 * Description: Given an fd, look it up in the current process's per process
4112 * open file table, and return its internal vnode pointer.
4114 * Parameters: fd fd to obtain vnode from
4115 * vpp pointer to vnode return area
4117 * Returns: 0 Success
4118 * EINVAL The fd does not refer to a
4119 * vnode fileproc entry
4120 * fp_lookup:EBADF Bad file descriptor
4123 * *vpp (modified) Returned vnode pointer
4125 * Locks: This function internally takes and drops the proc_fdlock for
4126 * the current process
4128 * Notes: If successful, this function increments the f_iocount on the
4129 * fd's corresponding fileproc.
4131 * The fileproc referenced is not returned; because of this, care
4132 * must be taken to not drop the last reference (e.g. by closing
4133 * the file). This is inherently unsafe, since the reference may
4134 * not be recoverable from the vnode, if there is a subsequent
4135 * close that destroys the associate fileproc. The caller should
4136 * therefore retain their own reference on the fileproc so that
4137 * the f_iocount can be dropped subsequently. Failure to do this
4138 * can result in the returned pointer immediately becoming invalid
4139 * following the call.
4141 * Use of this function is discouraged.
4144 file_vnode(int fd
, struct vnode
**vpp
)
4146 proc_t p
= current_proc();
4147 struct fileproc
*fp
;
4150 proc_fdlock_spin(p
);
4151 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4155 if (fp
->f_type
!= DTYPE_VNODE
) {
4156 fp_drop(p
, fd
, fp
,1);
4161 *vpp
= (struct vnode
*)fp
->f_data
;
4169 * file_vnode_withvid
4171 * Description: Given an fd, look it up in the current process's per process
4172 * open file table, and return its internal vnode pointer.
4174 * Parameters: fd fd to obtain vnode from
4175 * vpp pointer to vnode return area
4176 * vidp pointer to vid of the returned vnode
4178 * Returns: 0 Success
4179 * EINVAL The fd does not refer to a
4180 * vnode fileproc entry
4181 * fp_lookup:EBADF Bad file descriptor
4184 * *vpp (modified) Returned vnode pointer
4186 * Locks: This function internally takes and drops the proc_fdlock for
4187 * the current process
4189 * Notes: If successful, this function increments the f_iocount on the
4190 * fd's corresponding fileproc.
4192 * The fileproc referenced is not returned; because of this, care
4193 * must be taken to not drop the last reference (e.g. by closing
4194 * the file). This is inherently unsafe, since the reference may
4195 * not be recoverable from the vnode, if there is a subsequent
4196 * close that destroys the associate fileproc. The caller should
4197 * therefore retain their own reference on the fileproc so that
4198 * the f_iocount can be dropped subsequently. Failure to do this
4199 * can result in the returned pointer immediately becoming invalid
4200 * following the call.
4202 * Use of this function is discouraged.
4205 file_vnode_withvid(int fd
, struct vnode
**vpp
, uint32_t * vidp
)
4207 proc_t p
= current_proc();
4208 struct fileproc
*fp
;
4212 proc_fdlock_spin(p
);
4213 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4217 if (fp
->f_type
!= DTYPE_VNODE
) {
4218 fp_drop(p
, fd
, fp
,1);
4222 vp
= (struct vnode
*)fp
->f_data
;
4226 if ((vidp
!= NULL
) && (vp
!= NULLVP
))
4227 *vidp
= (uint32_t)vp
->v_id
;
4238 * Description: Given an fd, look it up in the current process's per process
4239 * open file table, and return its internal socket pointer.
4241 * Parameters: fd fd to obtain vnode from
4242 * sp pointer to socket return area
4244 * Returns: 0 Success
4245 * ENOTSOCK Not a socket
4246 * fp_lookup:EBADF Bad file descriptor
4249 * *sp (modified) Returned socket pointer
4251 * Locks: This function internally takes and drops the proc_fdlock for
4252 * the current process
4254 * Notes: If successful, this function increments the f_iocount on the
4255 * fd's corresponding fileproc.
4257 * The fileproc referenced is not returned; because of this, care
4258 * must be taken to not drop the last reference (e.g. by closing
4259 * the file). This is inherently unsafe, since the reference may
4260 * not be recoverable from the socket, if there is a subsequent
4261 * close that destroys the associate fileproc. The caller should
4262 * therefore retain their own reference on the fileproc so that
4263 * the f_iocount can be dropped subsequently. Failure to do this
4264 * can result in the returned pointer immediately becoming invalid
4265 * following the call.
4267 * Use of this function is discouraged.
4270 file_socket(int fd
, struct socket
**sp
)
4272 proc_t p
= current_proc();
4273 struct fileproc
*fp
;
4276 proc_fdlock_spin(p
);
4277 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4281 if (fp
->f_type
!= DTYPE_SOCKET
) {
4282 fp_drop(p
, fd
, fp
,1);
4286 *sp
= (struct socket
*)fp
->f_data
;
4296 * Description: Given an fd, look it up in the current process's per process
4297 * open file table, and return its fileproc's flags field.
4299 * Parameters: fd fd whose flags are to be
4301 * flags pointer to flags data area
4303 * Returns: 0 Success
4304 * ENOTSOCK Not a socket
4305 * fp_lookup:EBADF Bad file descriptor
4308 * *flags (modified) Returned flags field
4310 * Locks: This function internally takes and drops the proc_fdlock for
4311 * the current process
4313 * Notes: This function will internally increment and decrement the
4314 * f_iocount of the fileproc as part of its operation.
4317 file_flags(int fd
, int *flags
)
4320 proc_t p
= current_proc();
4321 struct fileproc
*fp
;
4324 proc_fdlock_spin(p
);
4325 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4329 *flags
= (int)fp
->f_flag
;
4330 fp_drop(p
, fd
, fp
,1);
4340 * Description: Drop an iocount reference on an fd, and wake up any waiters
4341 * for draining (i.e. blocked in fileproc_drain() called during
4342 * the last attempt to close a file).
4344 * Parameters: fd fd on which an ioreference is
4347 * Returns: 0 Success
4348 * EBADF Bad file descriptor
4350 * Description: Given an fd, look it up in the current process's per process
4351 * open file table, and drop it's fileproc's f_iocount by one
4353 * Notes: This is intended as a corresponding operation to the functions
4354 * file_vnode() and file_socket() operations.
4356 * Technically, the close reference is supposed to be protected
4357 * by a fileproc_drain(), however, a drain will only block if
4358 * the fd refers to a character device, and that device has had
4359 * preparefileread() called on it. If it refers to something
4360 * other than a character device, then the drain will occur and
4361 * block each close attempt, rather than merely the last close.
4363 * Since it's possible for an fd that refers to a character
4364 * device to have an intermediate close followed by an open to
4365 * cause a different file to correspond to that descriptor,
4366 * unless there was a cautionary reference taken on the fileproc,
4367 * this is an inherently unsafe function. This happens in the
4368 * case where multiple fd's in a process refer to the same
4369 * character device (e.g. stdin/out/err pointing to a tty, etc.).
4371 * Use of this function is discouraged.
4376 struct fileproc
*fp
;
4377 proc_t p
= current_proc();
4380 proc_fdlock_spin(p
);
4381 if (fd
< 0 || fd
>= p
->p_fd
->fd_nfiles
||
4382 (fp
= p
->p_fd
->fd_ofiles
[fd
]) == NULL
||
4383 ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
4384 !(p
->p_fd
->fd_ofileflags
[fd
] & UF_CLOSING
))) {
4390 if (fp
->f_iocount
== 0) {
4391 if (fp
->f_flags
& FP_SELCONFLICT
)
4392 fp
->f_flags
&= ~FP_SELCONFLICT
;
4394 if (p
->p_fpdrainwait
) {
4395 p
->p_fpdrainwait
= 0;
4402 wakeup(&p
->p_fpdrainwait
);
4407 static int falloc_withalloc_locked(proc_t
, struct fileproc
**, int *,
4408 vfs_context_t
, struct fileproc
* (*)(void *), void *, int);
4413 * Description: Allocate an entry in the per process open file table and
4414 * return the corresponding fileproc and fd.
4416 * Parameters: p The process in whose open file
4417 * table the fd is to be allocated
4418 * resultfp Pointer to fileproc pointer
4420 * resultfd Pointer to fd return area
4423 * Returns: 0 Success
4424 * falloc:ENFILE Too many open files in system
4425 * falloc:EMFILE Too many open files in process
4426 * falloc:ENOMEM M_FILEPROC or M_FILEGLOB zone
4430 * *resultfd (modified) Returned fileproc pointer
4431 * *resultfd (modified) Returned fd
4433 * Locks: This function takes and drops the proc_fdlock; if this lock
4434 * is already held, use falloc_locked() instead.
4436 * Notes: This function takes separate process and context arguments
4437 * solely to support kern_exec.c; otherwise, it would take
4438 * neither, and expect falloc_locked() to use the
4439 * vfs_context_current() routine internally.
4442 falloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
)
4444 return (falloc_withalloc(p
, resultfp
, resultfd
, ctx
,
4445 fileproc_alloc_init
, NULL
));
4449 * Like falloc, but including the fileproc allocator and create-args
4452 falloc_withalloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4453 vfs_context_t ctx
, fp_allocfn_t fp_zalloc
, void *arg
)
4458 error
= falloc_withalloc_locked(p
,
4459 resultfp
, resultfd
, ctx
, fp_zalloc
, arg
, 1);
4466 * "uninitialized" ops -- ensure fg->fg_ops->fo_type always exists
4468 static const struct fileops uninitops
;
4473 * Create a new open file structure and allocate
4474 * a file descriptor for the process that refers to it.
4476 * Returns: 0 Success
4478 * Description: Allocate an entry in the per process open file table and
4479 * return the corresponding fileproc and fd.
4481 * Parameters: p The process in whose open file
4482 * table the fd is to be allocated
4483 * resultfp Pointer to fileproc pointer
4485 * resultfd Pointer to fd return area
4487 * locked Flag to indicate whether the
4488 * caller holds proc_fdlock
4490 * Returns: 0 Success
4491 * ENFILE Too many open files in system
4492 * fdalloc:EMFILE Too many open files in process
4493 * ENOMEM M_FILEPROC or M_FILEGLOB zone
4498 * *resultfd (modified) Returned fileproc pointer
4499 * *resultfd (modified) Returned fd
4501 * Locks: If the parameter 'locked' is zero, this function takes and
4502 * drops the proc_fdlock; if non-zero, the caller must hold the
4505 * Notes: If you intend to use a non-zero 'locked' parameter, use the
4506 * utility function falloc() instead.
4508 * This function takes separate process and context arguments
4509 * solely to support kern_exec.c; otherwise, it would take
4510 * neither, and use the vfs_context_current() routine internally.
4513 falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4514 vfs_context_t ctx
, int locked
)
4516 return (falloc_withalloc_locked(p
, resultfp
, resultfd
, ctx
,
4517 fileproc_alloc_init
, NULL
, locked
));
4521 falloc_withalloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4522 vfs_context_t ctx
, fp_allocfn_t fp_zalloc
, void *crarg
,
4525 struct fileproc
*fp
;
4526 struct fileglob
*fg
;
4531 if ( (error
= fdalloc(p
, 0, &nfd
)) ) {
4536 if (nfiles
>= maxfiles
) {
4543 error
= mac_file_check_create(proc_ucred(p
));
4552 * Allocate a new file descriptor.
4553 * If the process has file descriptor zero open, add to the list
4554 * of open files at that point, otherwise put it at the front of
4555 * the list of open files.
4559 fp
= (*fp_zalloc
)(crarg
);
4565 MALLOC_ZONE(fg
, struct fileglob
*, sizeof(struct fileglob
), M_FILEGLOB
, M_WAITOK
);
4572 bzero(fg
, sizeof(struct fileglob
));
4573 lck_mtx_init(&fg
->fg_lock
, file_lck_grp
, file_lck_attr
);
4577 fg
->fg_ops
= &uninitops
;
4580 mac_file_label_init(fg
);
4583 kauth_cred_ref(ctx
->vc_ucred
);
4587 fp
->f_cred
= ctx
->vc_ucred
;
4590 mac_file_label_associate(fp
->f_cred
, fg
);
4593 OSAddAtomic(1, &nfiles
);
4595 p
->p_fd
->fd_ofiles
[nfd
] = fp
;
4612 * Description: Free a file structure; drop the global open file count, and
4613 * drop the credential reference, if the fileglob has one, and
4614 * destroy the instance mutex before freeing
4616 * Parameters: fg Pointer to fileglob to be
4622 fg_free(struct fileglob
*fg
)
4624 OSAddAtomic(-1, &nfiles
);
4626 if (fg
->fg_vn_data
) {
4627 fg_vn_data_free(fg
->fg_vn_data
);
4628 fg
->fg_vn_data
= NULL
;
4631 if (IS_VALID_CRED(fg
->fg_cred
)) {
4632 kauth_cred_unref(&fg
->fg_cred
);
4634 lck_mtx_destroy(&fg
->fg_lock
, file_lck_grp
);
4637 mac_file_label_destroy(fg
);
4639 FREE_ZONE(fg
, sizeof *fg
, M_FILEGLOB
);
4647 * Description: Perform close-on-exec processing for all files in a process
4648 * that are either marked as close-on-exec, or which were in the
4649 * process of being opened at the time of the execve
4651 * Also handles the case (via posix_spawn()) where -all-
4652 * files except those marked with "inherit" as treated as
4655 * Parameters: p Pointer to process calling
4660 * Locks: This function internally takes and drops proc_fdlock()
4661 * But assumes tables don't grow/change while unlocked.
4665 fdexec(proc_t p
, short flags
, int self_exec
)
4667 struct filedesc
*fdp
= p
->p_fd
;
4669 boolean_t cloexec_default
= (flags
& POSIX_SPAWN_CLOEXEC_DEFAULT
) != 0;
4670 thread_t self
= current_thread();
4671 struct uthread
*ut
= get_bsdthread_info(self
);
4672 struct kqueue
*dealloc_kq
= NULL
;
4675 * If the current thread is bound as a workq/workloop
4676 * servicing thread, we need to unbind it first.
4678 if (ut
->uu_kqueue_bound
&& self_exec
) {
4679 kevent_qos_internal_unbind(p
, 0, self
,
4680 ut
->uu_kqueue_flags
);
4686 * Deallocate the knotes for this process
4687 * and mark the tables non-existent so
4688 * subsequent kqueue closes go faster.
4691 assert(fdp
->fd_knlistsize
== -1);
4692 assert(fdp
->fd_knhashmask
== 0);
4694 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
4696 struct fileproc
*fp
= fdp
->fd_ofiles
[i
];
4697 char *flagp
= &fdp
->fd_ofileflags
[i
];
4699 if (fp
&& cloexec_default
) {
4701 * Reverse the usual semantics of file descriptor
4702 * inheritance - all of them should be closed
4703 * except files marked explicitly as "inherit" and
4704 * not marked close-on-exec.
4706 if ((*flagp
& (UF_EXCLOSE
|UF_INHERIT
)) != UF_INHERIT
)
4707 *flagp
|= UF_EXCLOSE
;
4708 *flagp
&= ~UF_INHERIT
;
4712 ((*flagp
& (UF_RESERVED
|UF_EXCLOSE
)) == UF_EXCLOSE
)
4714 || (fp
&& mac_file_check_inherit(proc_ucred(p
), fp
->f_fglob
))
4717 procfdtbl_clearfd(p
, i
);
4718 if (i
== fdp
->fd_lastfile
&& i
> 0)
4720 if (i
< fdp
->fd_freefile
)
4721 fdp
->fd_freefile
= i
;
4724 * Wait for any third party viewers (e.g., lsof)
4725 * to release their references to this fileproc.
4727 while (fp
->f_iocount
> 0) {
4728 p
->p_fpdrainwait
= 1;
4729 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
,
4733 closef_locked(fp
, fp
->f_fglob
, p
);
4739 /* release the per-process workq kq */
4740 if (fdp
->fd_wqkqueue
) {
4741 dealloc_kq
= fdp
->fd_wqkqueue
;
4742 fdp
->fd_wqkqueue
= NULL
;
4747 /* Anything to free? */
4749 kqueue_dealloc(dealloc_kq
);
4756 * Description: Copy a filedesc structure. This is normally used as part of
4757 * forkproc() when forking a new process, to copy the per process
4758 * open file table over to the new process.
4760 * Parameters: p Process whose open file table
4761 * is to be copied (parent)
4762 * uth_cdir Per thread current working
4763 * cirectory, or NULL
4765 * Returns: NULL Copy failed
4766 * !NULL Pointer to new struct filedesc
4768 * Locks: This function internally takes and drops proc_fdlock()
4770 * Notes: Files are copied directly, ignoring the new resource limits
4771 * for the process that's being copied into. Since the descriptor
4772 * references are just additional references, this does not count
4773 * against the number of open files on the system.
4775 * The struct filedesc includes the current working directory,
4776 * and the current root directory, if the process is chroot'ed.
4778 * If the exec was called by a thread using a per thread current
4779 * working directory, we inherit the working directory from the
4780 * thread making the call, rather than from the process.
4782 * In the case of a failure to obtain a reference, for most cases,
4783 * the file entry will be silently dropped. There's an exception
4784 * for the case of a chroot dir, since a failure to to obtain a
4785 * reference there would constitute an "escape" from the chroot
4786 * environment, which must not be allowed. In that case, we will
4787 * deny the execve() operation, rather than allowing the escape.
4790 fdcopy(proc_t p
, vnode_t uth_cdir
)
4792 struct filedesc
*newfdp
, *fdp
= p
->p_fd
;
4794 struct fileproc
*ofp
, *fp
;
4797 MALLOC_ZONE(newfdp
, struct filedesc
*,
4798 sizeof(*newfdp
), M_FILEDESC
, M_WAITOK
);
4805 * the FD_CHROOT flag will be inherited via this copy
4807 (void) memcpy(newfdp
, fdp
, sizeof(*newfdp
));
4810 * If we are running with per-thread current working directories,
4811 * inherit the new current working directory from the current thread
4812 * instead, before we take our references.
4814 if (uth_cdir
!= NULLVP
)
4815 newfdp
->fd_cdir
= uth_cdir
;
4818 * For both fd_cdir and fd_rdir make sure we get
4819 * a valid reference... if we can't, than set
4820 * set the pointer(s) to NULL in the child... this
4821 * will keep us from using a non-referenced vp
4822 * and allows us to do the vnode_rele only on
4823 * a properly referenced vp
4825 if ( (v_dir
= newfdp
->fd_cdir
) ) {
4826 if (vnode_getwithref(v_dir
) == 0) {
4827 if ( (vnode_ref(v_dir
)) )
4828 newfdp
->fd_cdir
= NULL
;
4831 newfdp
->fd_cdir
= NULL
;
4833 if (newfdp
->fd_cdir
== NULL
&& fdp
->fd_cdir
) {
4835 * we couldn't get a new reference on
4836 * the current working directory being
4837 * inherited... we might as well drop
4838 * our reference from the parent also
4839 * since the vnode has gone DEAD making
4840 * it useless... by dropping it we'll
4841 * be that much closer to recycling it
4843 vnode_rele(fdp
->fd_cdir
);
4844 fdp
->fd_cdir
= NULL
;
4847 if ( (v_dir
= newfdp
->fd_rdir
) ) {
4848 if (vnode_getwithref(v_dir
) == 0) {
4849 if ( (vnode_ref(v_dir
)) )
4850 newfdp
->fd_rdir
= NULL
;
4853 newfdp
->fd_rdir
= NULL
;
4856 /* Coming from a chroot environment and unable to get a reference... */
4857 if (newfdp
->fd_rdir
== NULL
&& fdp
->fd_rdir
) {
4859 * We couldn't get a new reference on
4860 * the chroot directory being
4861 * inherited... this is fatal, since
4862 * otherwise it would constitute an
4863 * escape from a chroot environment by
4866 if (newfdp
->fd_cdir
)
4867 vnode_rele(newfdp
->fd_cdir
);
4868 FREE_ZONE(newfdp
, sizeof *newfdp
, M_FILEDESC
);
4873 * If the number of open files fits in the internal arrays
4874 * of the open file structure, use them, otherwise allocate
4875 * additional memory for the number of descriptors currently
4878 if (newfdp
->fd_lastfile
< NDFILE
)
4882 * Compute the smallest multiple of NDEXTENT needed
4883 * for the file descriptors currently in use,
4884 * allowing the table to shrink.
4886 i
= newfdp
->fd_nfiles
;
4887 while (i
> 1 + 2 * NDEXTENT
&& i
> 1 + newfdp
->fd_lastfile
* 2)
4892 MALLOC_ZONE(newfdp
->fd_ofiles
, struct fileproc
**,
4893 i
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
4894 if (newfdp
->fd_ofiles
== NULL
) {
4895 if (newfdp
->fd_cdir
)
4896 vnode_rele(newfdp
->fd_cdir
);
4897 if (newfdp
->fd_rdir
)
4898 vnode_rele(newfdp
->fd_rdir
);
4900 FREE_ZONE(newfdp
, sizeof(*newfdp
), M_FILEDESC
);
4903 (void) memset(newfdp
->fd_ofiles
, 0, i
* OFILESIZE
);
4906 newfdp
->fd_ofileflags
= (char *) &newfdp
->fd_ofiles
[i
];
4907 newfdp
->fd_nfiles
= i
;
4909 if (fdp
->fd_nfiles
> 0) {
4910 struct fileproc
**fpp
;
4913 (void) memcpy(newfdp
->fd_ofiles
, fdp
->fd_ofiles
,
4914 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofiles
));
4915 (void) memcpy(newfdp
->fd_ofileflags
, fdp
->fd_ofileflags
,
4916 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofileflags
));
4919 * kq descriptors cannot be copied.
4921 if (newfdp
->fd_knlistsize
!= -1) {
4922 fpp
= &newfdp
->fd_ofiles
[newfdp
->fd_lastfile
];
4923 flags
= &newfdp
->fd_ofileflags
[newfdp
->fd_lastfile
];
4924 for (i
= newfdp
->fd_lastfile
;
4925 i
>= 0; i
--, fpp
--, flags
--) {
4926 if (*flags
& UF_RESERVED
)
4927 continue; /* (removed below) */
4928 if (*fpp
!= NULL
&& (*fpp
)->f_type
== DTYPE_KQUEUE
) {
4931 if (i
< newfdp
->fd_freefile
)
4932 newfdp
->fd_freefile
= i
;
4934 if (*fpp
== NULL
&& i
== newfdp
->fd_lastfile
&& i
> 0)
4935 newfdp
->fd_lastfile
--;
4938 fpp
= newfdp
->fd_ofiles
;
4939 flags
= newfdp
->fd_ofileflags
;
4941 for (i
= newfdp
->fd_lastfile
+ 1; --i
>= 0; fpp
++, flags
++)
4942 if ((ofp
= *fpp
) != NULL
&&
4943 0 == (ofp
->f_fglob
->fg_lflags
& FG_CONFINED
) &&
4944 0 == (*flags
& (UF_FORKCLOSE
|UF_RESERVED
))) {
4946 if (FILEPROC_TYPE(ofp
) != FTYPE_SIMPLE
)
4947 panic("complex fileproc");
4949 fp
= fileproc_alloc_init(NULL
);
4952 * XXX no room to copy, unable to
4953 * XXX safely unwind state at present
4958 (ofp
->f_flags
& ~FP_TYPEMASK
);
4959 fp
->f_fglob
= ofp
->f_fglob
;
4964 if (i
< newfdp
->fd_freefile
)
4965 newfdp
->fd_freefile
= i
;
4974 * Initialize knote and kqueue tracking structs
4976 newfdp
->fd_knlist
= NULL
;
4977 newfdp
->fd_knlistsize
= -1;
4978 newfdp
->fd_knhash
= NULL
;
4979 newfdp
->fd_knhashmask
= 0;
4980 newfdp
->fd_kqhash
= NULL
;
4981 newfdp
->fd_kqhashmask
= 0;
4982 newfdp
->fd_wqkqueue
= NULL
;
4983 lck_mtx_init(&newfdp
->fd_kqhashlock
, proc_kqhashlock_grp
, proc_lck_attr
);
4984 lck_mtx_init(&newfdp
->fd_knhashlock
, proc_knhashlock_grp
, proc_lck_attr
);
4993 * Description: Release a filedesc (per process open file table) structure;
4994 * this is done on process exit(), or from forkproc_free() if
4995 * the fork fails for some reason subsequent to a successful
4998 * Parameters: p Pointer to process going away
5002 * Locks: This function internally takes and drops proc_fdlock()
5007 struct filedesc
*fdp
;
5008 struct fileproc
*fp
;
5009 struct kqueue
*dealloc_kq
= NULL
;
5014 if (p
== kernproc
|| NULL
== (fdp
= p
->p_fd
)) {
5019 extern struct filedesc filedesc0
;
5021 if (&filedesc0
== fdp
)
5025 * deallocate all the knotes up front and claim empty
5026 * tables to make any subsequent kqueue closes faster.
5029 assert(fdp
->fd_knlistsize
== -1);
5030 assert(fdp
->fd_knhashmask
== 0);
5032 /* close file descriptors */
5033 if (fdp
->fd_nfiles
> 0 && fdp
->fd_ofiles
) {
5034 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
5035 if ((fp
= fdp
->fd_ofiles
[i
]) != NULL
) {
5037 if (fdp
->fd_ofileflags
[i
] & UF_RESERVED
)
5038 panic("fdfree: found fp with UF_RESERVED");
5040 procfdtbl_reservefd(p
, i
);
5042 if (fp
->f_flags
& FP_WAITEVENT
)
5043 (void)waitevent_close(p
, fp
);
5044 (void) closef_locked(fp
, fp
->f_fglob
, p
);
5048 FREE_ZONE(fdp
->fd_ofiles
, fdp
->fd_nfiles
* OFILESIZE
, M_OFILETABL
);
5049 fdp
->fd_ofiles
= NULL
;
5053 if (fdp
->fd_wqkqueue
) {
5054 dealloc_kq
= fdp
->fd_wqkqueue
;
5055 fdp
->fd_wqkqueue
= NULL
;
5061 kqueue_dealloc(dealloc_kq
);
5064 vnode_rele(fdp
->fd_cdir
);
5066 vnode_rele(fdp
->fd_rdir
);
5068 proc_fdlock_spin(p
);
5072 if (fdp
->fd_kqhash
) {
5073 for (uint32_t j
= 0; j
<= fdp
->fd_kqhashmask
; j
++)
5074 assert(SLIST_EMPTY(&fdp
->fd_kqhash
[j
]));
5075 FREE(fdp
->fd_kqhash
, M_KQUEUE
);
5078 lck_mtx_destroy(&fdp
->fd_kqhashlock
, proc_kqhashlock_grp
);
5079 lck_mtx_destroy(&fdp
->fd_knhashlock
, proc_knhashlock_grp
);
5081 FREE_ZONE(fdp
, sizeof(*fdp
), M_FILEDESC
);
5087 * Description: Internal form of closef; called with proc_fdlock held
5089 * Parameters: fp Pointer to fileproc for fd
5090 * fg Pointer to fileglob for fd
5091 * p Pointer to proc structure
5093 * Returns: 0 Success
5094 * closef_finish:??? Anything returnable by a per-fileops
5097 * Note: Decrements reference count on file structure; if this was the
5098 * last reference, then closef_finish() is called
5100 * p and fp are allowed to be NULL when closing a file that was
5101 * being passed in a message (but only if we are called when this
5102 * is NOT the last reference).
5105 closef_locked(struct fileproc
*fp
, struct fileglob
*fg
, proc_t p
)
5109 struct vfs_context context
;
5116 /* Set up context with cred stashed in fg */
5117 if (p
== current_proc())
5118 context
.vc_thread
= current_thread();
5120 context
.vc_thread
= NULL
;
5121 context
.vc_ucred
= fg
->fg_cred
;
5124 * POSIX record locking dictates that any close releases ALL
5125 * locks owned by this process. This is handled by setting
5126 * a flag in the unlock to free ONLY locks obeying POSIX
5127 * semantics, and not to free BSD-style file locks.
5128 * If the descriptor was in a message, POSIX-style locks
5129 * aren't passed with the descriptor.
5131 if (p
&& (p
->p_ladvflag
& P_LADVLOCK
) &&
5132 DTYPE_VNODE
== FILEGLOB_DTYPE(fg
)) {
5135 lf
.l_whence
= SEEK_SET
;
5138 lf
.l_type
= F_UNLCK
;
5139 vp
= (struct vnode
*)fg
->fg_data
;
5141 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
5142 (void) VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &lf
, F_POSIX
, &context
, NULL
);
5143 (void)vnode_put(vp
);
5147 lck_mtx_lock_spin(&fg
->fg_lock
);
5150 if (fg
->fg_count
> 0) {
5151 lck_mtx_unlock(&fg
->fg_lock
);
5155 if (fg
->fg_count
!= 0)
5156 panic("fg %p: being freed with bad fg_count (%d)", fg
, fg
->fg_count
);
5159 if (fp
&& (fp
->f_flags
& FP_WRITTEN
))
5160 fg
->fg_flag
|= FWASWRITTEN
;
5162 fg
->fg_lflags
|= FG_TERM
;
5163 lck_mtx_unlock(&fg
->fg_lock
);
5168 /* Since we ensure that fg->fg_ops is always initialized,
5169 * it is safe to invoke fo_close on the fg */
5170 error
= fo_close(fg
, &context
);
5184 * Description: Drain out pending I/O operations
5186 * Parameters: p Process closing this file
5187 * fp fileproc struct for the open
5188 * instance on the file
5192 * Locks: Assumes the caller holds the proc_fdlock
5194 * Notes: For character devices, this occurs on the last close of the
5195 * device; for all other file descriptors, this occurs on each
5196 * close to prevent fd's from being closed out from under
5197 * operations currently in progress and blocked
5199 * See Also: file_vnode(), file_socket(), file_drop(), and the cautions
5200 * regarding their use and interaction with this function.
5203 fileproc_drain(proc_t p
, struct fileproc
* fp
)
5205 struct vfs_context context
;
5207 context
.vc_thread
= proc_thread(p
); /* XXX */
5208 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
5210 fp
->f_iocount
-- ; /* (the one the close holds) */
5212 while (fp
->f_iocount
) {
5214 lck_mtx_convert_spin(&p
->p_fdmlock
);
5216 if (fp
->f_fglob
->fg_ops
->fo_drain
) {
5217 (*fp
->f_fglob
->fg_ops
->fo_drain
)(fp
, &context
);
5219 if ((fp
->f_flags
& FP_INSELECT
) == FP_INSELECT
) {
5220 if (waitq_wakeup64_all((struct waitq
*)fp
->f_wset
, NO_EVENT64
,
5221 THREAD_INTERRUPTED
, WAITQ_ALL_PRIORITIES
) == KERN_INVALID_ARGUMENT
)
5222 panic("bad wait queue for waitq_wakeup64_all %p (fp:%p)", fp
->f_wset
, fp
);
5224 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
) {
5225 if (waitq_wakeup64_all(&select_conflict_queue
, NO_EVENT64
,
5226 THREAD_INTERRUPTED
, WAITQ_ALL_PRIORITIES
) == KERN_INVALID_ARGUMENT
)
5227 panic("bad select_conflict_queue");
5229 p
->p_fpdrainwait
= 1;
5231 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
, "fpdrain", NULL
);
5235 if ((fp
->f_flags
& FP_INSELECT
) != 0)
5236 panic("FP_INSELECT set on drained fp");
5238 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
)
5239 fp
->f_flags
&= ~FP_SELCONFLICT
;
5246 * Description: Release the fd and free the fileproc associated with the fd
5247 * in the per process open file table of the specified process;
5248 * these values must correspond.
5250 * Parameters: p Process containing fd
5251 * fd fd to be released
5252 * fp fileproc to be freed
5254 * Returns: 0 Success
5256 * Notes: XXX function should be void - no one interprets the returns
5260 fp_free(proc_t p
, int fd
, struct fileproc
* fp
)
5262 proc_fdlock_spin(p
);
5266 fg_free(fp
->f_fglob
);
5275 * Description: Apply an advisory lock on a file descriptor.
5277 * Parameters: p Process making request
5278 * uap->fd fd on which the lock is to be
5280 * uap->how (Un)Lock bits, including type
5281 * retval Pointer to the call return area
5283 * Returns: 0 Success
5284 * fp_getfvp:EBADF Bad file descriptor
5285 * fp_getfvp:ENOTSUP fd does not refer to a vnode
5286 * vnode_getwithref:???
5290 * *retval (modified) Size of dtable
5292 * Notes: Just attempt to get a record lock of the requested type on
5293 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
5296 flock(proc_t p
, struct flock_args
*uap
, __unused
int32_t *retval
)
5300 struct fileproc
*fp
;
5303 vfs_context_t ctx
= vfs_context_current();
5306 AUDIT_ARG(fd
, uap
->fd
);
5307 if ( (error
= fp_getfvp(p
, fd
, &fp
, &vp
)) ) {
5310 if ( (error
= vnode_getwithref(vp
)) ) {
5313 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
5315 lf
.l_whence
= SEEK_SET
;
5318 if (how
& LOCK_UN
) {
5319 lf
.l_type
= F_UNLCK
;
5320 fp
->f_flag
&= ~FHASLOCK
;
5321 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_UNLCK
, &lf
, F_FLOCK
, ctx
, NULL
);
5325 lf
.l_type
= F_WRLCK
;
5326 else if (how
& LOCK_SH
)
5327 lf
.l_type
= F_RDLCK
;
5333 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
, F_SETLK
, &lf
);
5337 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
,
5338 (how
& LOCK_NB
? F_FLOCK
: F_FLOCK
| F_WAIT
),
5341 fp
->f_flag
|= FHASLOCK
;
5343 (void)vnode_put(vp
);
5345 fp_drop(p
, fd
, fp
, 0);
5353 * Description: Obtain a Mach send right for a given file descriptor.
5355 * Parameters: p Process calling fileport
5356 * uap->fd The fd to reference
5357 * uap->portnamep User address at which to place port name.
5359 * Returns: 0 Success.
5360 * EBADF Bad file descriptor.
5361 * EINVAL File descriptor had type that cannot be sent, misc. other errors.
5362 * EFAULT Address at which to store port name is not valid.
5363 * EAGAIN Resource shortage.
5366 * On success, name of send right is stored at user-specified address.
5369 fileport_makeport(proc_t p
, struct fileport_makeport_args
*uap
,
5370 __unused
int *retval
)
5374 user_addr_t user_portaddr
= uap
->portnamep
;
5375 struct fileproc
*fp
= FILEPROC_NULL
;
5376 struct fileglob
*fg
= NULL
;
5377 ipc_port_t fileport
;
5378 mach_port_name_t name
= MACH_PORT_NULL
;
5381 err
= fp_lookup(p
, fd
, &fp
, 1);
5386 if (!file_issendable(p
, fp
)) {
5391 if (FP_ISGUARDED(fp
, GUARD_FILEPORT
)) {
5392 err
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_FILEPORT
);
5396 /* Dropped when port is deallocated */
5402 /* Allocate and initialize a port */
5403 fileport
= fileport_alloc(fg
);
5404 if (fileport
== IPC_PORT_NULL
) {
5410 /* Add an entry. Deallocates port on failure. */
5411 name
= ipc_port_copyout_send(fileport
, get_task_ipcspace(p
->task
));
5412 if (!MACH_PORT_VALID(name
)) {
5417 err
= copyout(&name
, user_portaddr
, sizeof(mach_port_name_t
));
5422 /* Tag the fileglob for debugging purposes */
5423 lck_mtx_lock_spin(&fg
->fg_lock
);
5424 fg
->fg_lflags
|= FG_PORTMADE
;
5425 lck_mtx_unlock(&fg
->fg_lock
);
5427 fp_drop(p
, fd
, fp
, 0);
5434 if (MACH_PORT_VALID(name
)) {
5435 /* Don't care if another thread races us to deallocate the entry */
5436 (void) mach_port_deallocate(get_task_ipcspace(p
->task
), name
);
5439 if (fp
!= FILEPROC_NULL
) {
5440 fp_drop(p
, fd
, fp
, 0);
5447 fileport_releasefg(struct fileglob
*fg
)
5449 (void)closef_locked(NULL
, fg
, PROC_NULL
);
5458 * Description: Obtain the file descriptor for a given Mach send right.
5460 * Parameters: p Process calling fileport
5461 * uap->port Name of send right to file port.
5463 * Returns: 0 Success
5464 * EINVAL Invalid Mach port name, or port is not for a file.
5466 * fdalloc:ENOMEM Unable to allocate fileproc or extend file table.
5469 * *retval (modified) The new descriptor
5472 fileport_makefd(proc_t p
, struct fileport_makefd_args
*uap
, int32_t *retval
)
5474 struct fileglob
*fg
;
5475 struct fileproc
*fp
= FILEPROC_NULL
;
5476 ipc_port_t port
= IPC_PORT_NULL
;
5477 mach_port_name_t send
= uap
->port
;
5482 res
= ipc_object_copyin(get_task_ipcspace(p
->task
),
5483 send
, MACH_MSG_TYPE_COPY_SEND
, &port
);
5485 if (res
!= KERN_SUCCESS
) {
5490 fg
= fileport_port_to_fileglob(port
);
5496 fp
= fileproc_alloc_init(NULL
);
5497 if (fp
== FILEPROC_NULL
) {
5506 err
= fdalloc(p
, 0, &fd
);
5512 *fdflags(p
, fd
) |= UF_EXCLOSE
;
5514 procfdtbl_releasefd(p
, fd
, fp
);
5520 if ((fp
!= NULL
) && (0 != err
)) {
5524 if (IPC_PORT_NULL
!= port
) {
5525 ipc_port_release_send(port
);
5535 * Description: Duplicate the specified descriptor to a free descriptor;
5536 * this is the second half of fdopen(), above.
5538 * Parameters: fdp filedesc pointer to fill in
5540 * dfd fd to dup from
5541 * mode mode to set on new fd
5542 * error command code
5544 * Returns: 0 Success
5545 * EBADF Source fd is bad
5546 * EACCES Requested mode not allowed
5547 * !0 'error', if not ENODEV or
5550 * Notes: XXX This is not thread safe; see fdopen() above
5553 dupfdopen(struct filedesc
*fdp
, int indx
, int dfd
, int flags
, int error
)
5555 struct fileproc
*wfp
;
5556 struct fileproc
*fp
;
5560 proc_t p
= current_proc();
5563 * If the to-be-dup'd fd number is greater than the allowed number
5564 * of file descriptors, or the fd to be dup'd has already been
5565 * closed, reject. Note, check for new == old is necessary as
5566 * falloc could allocate an already closed to-be-dup'd descriptor
5567 * as the new descriptor.
5571 fp
= fdp
->fd_ofiles
[indx
];
5572 if (dfd
< 0 || dfd
>= fdp
->fd_nfiles
||
5573 (wfp
= fdp
->fd_ofiles
[dfd
]) == NULL
|| wfp
== fp
||
5574 (fdp
->fd_ofileflags
[dfd
] & UF_RESERVED
)) {
5580 myerror
= mac_file_check_dup(proc_ucred(p
), wfp
->f_fglob
, dfd
);
5587 * There are two cases of interest here.
5589 * For ENODEV simply dup (dfd) to file descriptor
5590 * (indx) and return.
5592 * For ENXIO steal away the file structure from (dfd) and
5593 * store it in (indx). (dfd) is effectively closed by
5596 * Any other error code is just returned.
5600 if (FP_ISGUARDED(wfp
, GUARD_DUP
)) {
5606 * Check that the mode the file is being opened for is a
5607 * subset of the mode of the existing descriptor.
5609 if (((flags
& (FREAD
|FWRITE
)) | wfp
->f_flag
) != wfp
->f_flag
) {
5613 if (indx
> fdp
->fd_lastfile
)
5614 fdp
->fd_lastfile
= indx
;
5618 fg_free(fp
->f_fglob
);
5619 fp
->f_fglob
= wfp
->f_fglob
;
5621 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
] |
5622 (flags
& O_CLOEXEC
) ? UF_EXCLOSE
: 0;
5638 * Description: Add a reference to a fileglob by fileproc
5640 * Parameters: fp fileproc containing fileglob
5645 * Notes: XXX Should use OSAddAtomic?
5648 fg_ref(struct fileproc
* fp
)
5650 struct fileglob
*fg
;
5654 lck_mtx_lock_spin(&fg
->fg_lock
);
5657 if ((fp
->f_flags
& ~((unsigned int)FP_VALID_FLAGS
)) != 0)
5658 panic("fg_ref: invalid bits on fp %p", fp
);
5660 if (fg
->fg_count
== 0)
5661 panic("fg_ref: adding fgcount to zeroed fg: fp %p fg %p",
5665 lck_mtx_unlock(&fg
->fg_lock
);
5672 * Description: Remove a reference to a fileglob by fileproc
5674 * Parameters: fp fileproc containing fileglob
5679 * Notes: XXX Should use OSAddAtomic?
5682 fg_drop(struct fileproc
* fp
)
5684 struct fileglob
*fg
;
5687 lck_mtx_lock_spin(&fg
->fg_lock
);
5689 lck_mtx_unlock(&fg
->fg_lock
);
5694 * fg_insertuipc_mark
5696 * Description: Mark fileglob for insertion onto message queue if needed
5697 * Also takes fileglob reference
5699 * Parameters: fg Fileglob pointer to insert
5701 * Returns: true, if the fileglob needs to be inserted onto msg queue
5703 * Locks: Takes and drops fg_lock, potentially many times
5706 fg_insertuipc_mark(struct fileglob
* fg
)
5708 boolean_t insert
= FALSE
;
5710 lck_mtx_lock_spin(&fg
->fg_lock
);
5711 while (fg
->fg_lflags
& FG_RMMSGQ
) {
5712 lck_mtx_convert_spin(&fg
->fg_lock
);
5714 fg
->fg_lflags
|= FG_WRMMSGQ
;
5715 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_insertuipc", NULL
);
5720 if (fg
->fg_msgcount
== 1) {
5721 fg
->fg_lflags
|= FG_INSMSGQ
;
5724 lck_mtx_unlock(&fg
->fg_lock
);
5731 * Description: Insert marked fileglob onto message queue
5733 * Parameters: fg Fileglob pointer to insert
5737 * Locks: Takes and drops fg_lock & uipc_lock
5738 * DO NOT call this function with proc_fdlock held as unp_gc()
5739 * can potentially try to acquire proc_fdlock, which can result
5740 * in a deadlock if this function is in unp_gc_wait().
5743 fg_insertuipc(struct fileglob
* fg
)
5745 if (fg
->fg_lflags
& FG_INSMSGQ
) {
5746 lck_mtx_lock_spin(uipc_lock
);
5748 LIST_INSERT_HEAD(&fmsghead
, fg
, f_msglist
);
5749 lck_mtx_unlock(uipc_lock
);
5750 lck_mtx_lock(&fg
->fg_lock
);
5751 fg
->fg_lflags
&= ~FG_INSMSGQ
;
5752 if (fg
->fg_lflags
& FG_WINSMSGQ
) {
5753 fg
->fg_lflags
&= ~FG_WINSMSGQ
;
5754 wakeup(&fg
->fg_lflags
);
5756 lck_mtx_unlock(&fg
->fg_lock
);
5761 * fg_removeuipc_mark
5763 * Description: Mark the fileglob for removal from message queue if needed
5764 * Also releases fileglob message queue reference
5766 * Parameters: fg Fileglob pointer to remove
5768 * Returns: true, if the fileglob needs to be removed from msg queue
5770 * Locks: Takes and drops fg_lock, potentially many times
5773 fg_removeuipc_mark(struct fileglob
* fg
)
5775 boolean_t remove
= FALSE
;
5777 lck_mtx_lock_spin(&fg
->fg_lock
);
5778 while (fg
->fg_lflags
& FG_INSMSGQ
) {
5779 lck_mtx_convert_spin(&fg
->fg_lock
);
5781 fg
->fg_lflags
|= FG_WINSMSGQ
;
5782 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_removeuipc", NULL
);
5785 if (fg
->fg_msgcount
== 0) {
5786 fg
->fg_lflags
|= FG_RMMSGQ
;
5789 lck_mtx_unlock(&fg
->fg_lock
);
5796 * Description: Remove marked fileglob from message queue
5798 * Parameters: fg Fileglob pointer to remove
5802 * Locks: Takes and drops fg_lock & uipc_lock
5803 * DO NOT call this function with proc_fdlock held as unp_gc()
5804 * can potentially try to acquire proc_fdlock, which can result
5805 * in a deadlock if this function is in unp_gc_wait().
5808 fg_removeuipc(struct fileglob
* fg
)
5810 if (fg
->fg_lflags
& FG_RMMSGQ
) {
5811 lck_mtx_lock_spin(uipc_lock
);
5813 LIST_REMOVE(fg
, f_msglist
);
5814 lck_mtx_unlock(uipc_lock
);
5815 lck_mtx_lock(&fg
->fg_lock
);
5816 fg
->fg_lflags
&= ~FG_RMMSGQ
;
5817 if (fg
->fg_lflags
& FG_WRMMSGQ
) {
5818 fg
->fg_lflags
&= ~FG_WRMMSGQ
;
5819 wakeup(&fg
->fg_lflags
);
5821 lck_mtx_unlock(&fg
->fg_lock
);
5824 #endif /* SOCKETS */
5829 * Description: Generic fileops read indirected through the fileops pointer
5830 * in the fileproc structure
5832 * Parameters: fp fileproc structure pointer
5833 * uio user I/O structure pointer
5835 * ctx VFS context for operation
5837 * Returns: 0 Success
5838 * !0 Errno from read
5841 fo_read(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
5843 return ((*fp
->f_ops
->fo_read
)(fp
, uio
, flags
, ctx
));
5850 * Description: Generic fileops write indirected through the fileops pointer
5851 * in the fileproc structure
5853 * Parameters: fp fileproc structure pointer
5854 * uio user I/O structure pointer
5856 * ctx VFS context for operation
5858 * Returns: 0 Success
5859 * !0 Errno from write
5862 fo_write(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
5864 return((*fp
->f_ops
->fo_write
)(fp
, uio
, flags
, ctx
));
5871 * Description: Generic fileops ioctl indirected through the fileops pointer
5872 * in the fileproc structure
5874 * Parameters: fp fileproc structure pointer
5876 * data pointer to internalized copy
5877 * of user space ioctl command
5878 * parameter data in kernel space
5879 * ctx VFS context for operation
5881 * Returns: 0 Success
5882 * !0 Errno from ioctl
5884 * Locks: The caller is assumed to have held the proc_fdlock; this
5885 * function releases and reacquires this lock. If the caller
5886 * accesses data protected by this lock prior to calling this
5887 * function, it will need to revalidate/reacquire any cached
5888 * protected data obtained prior to the call.
5891 fo_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
, vfs_context_t ctx
)
5895 proc_fdunlock(vfs_context_proc(ctx
));
5896 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, com
, data
, ctx
);
5897 proc_fdlock(vfs_context_proc(ctx
));
5905 * Description: Generic fileops select indirected through the fileops pointer
5906 * in the fileproc structure
5908 * Parameters: fp fileproc structure pointer
5909 * which select which
5910 * wql pointer to wait queue list
5911 * ctx VFS context for operation
5913 * Returns: 0 Success
5914 * !0 Errno from select
5917 fo_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
5919 return((*fp
->f_ops
->fo_select
)(fp
, which
, wql
, ctx
));
5926 * Description: Generic fileops close indirected through the fileops pointer
5927 * in the fileproc structure
5929 * Parameters: fp fileproc structure pointer for
5931 * ctx VFS context for operation
5933 * Returns: 0 Success
5934 * !0 Errno from close
5937 fo_close(struct fileglob
*fg
, vfs_context_t ctx
)
5939 return((*fg
->fg_ops
->fo_close
)(fg
, ctx
));
5946 * Description: Generic fileops kqueue filter indirected through the fileops
5947 * pointer in the fileproc structure
5949 * Parameters: fp fileproc structure pointer
5950 * kn pointer to knote to filter on
5951 * ctx VFS context for operation
5953 * Returns: (kn->kn_flags & EV_ERROR) error in kn->kn_data
5954 * 0 Filter is not active
5955 * !0 Filter is active
5958 fo_kqfilter(struct fileproc
*fp
, struct knote
*kn
,
5959 struct kevent_internal_s
*kev
, vfs_context_t ctx
)
5961 return ((*fp
->f_ops
->fo_kqfilter
)(fp
, kn
, kev
, ctx
));
5965 * The ability to send a file descriptor to another
5966 * process is opt-in by file type.
5969 file_issendable(proc_t p
, struct fileproc
*fp
)
5971 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
5973 switch (fp
->f_type
) {
5978 case DTYPE_NETPOLICY
:
5979 return (0 == (fp
->f_fglob
->fg_lflags
& FG_CONFINED
));
5981 /* DTYPE_KQUEUE, DTYPE_FSEVENTS, DTYPE_PSXSEM */
5988 fileproc_alloc_init(__unused
void *arg
)
5990 struct fileproc
*fp
;
5992 MALLOC_ZONE(fp
, struct fileproc
*, sizeof (*fp
), M_FILEPROC
, M_WAITOK
);
5994 bzero(fp
, sizeof (*fp
));
6000 fileproc_free(struct fileproc
*fp
)
6002 switch (FILEPROC_TYPE(fp
)) {
6004 FREE_ZONE(fp
, sizeof (*fp
), M_FILEPROC
);
6007 guarded_fileproc_free(fp
);
6010 panic("%s: corrupt fp %p flags %x", __func__
, fp
, fp
->f_flags
);