2 * Copyright (c) 2000-2014 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1982, 1986, 1989, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)kern_descrip.c 8.8 (Berkeley) 2/14/95
69 * NOTICE: This file was modified by SPARTA, Inc. in 2006 to introduce
70 * support for mandatory and extensible security protections. This notice
71 * is included in support of clause 2.2 (b) of the Apple Public License,
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/filedesc.h>
78 #include <sys/kernel.h>
79 #include <sys/vnode_internal.h>
80 #include <sys/proc_internal.h>
81 #include <sys/kauth.h>
82 #include <sys/file_internal.h>
83 #include <sys/guarded.h>
84 #include <sys/socket.h>
85 #include <sys/socketvar.h>
87 #include <sys/ioctl.h>
88 #include <sys/fcntl.h>
89 #include <sys/malloc.h>
91 #include <sys/syslog.h>
92 #include <sys/unistd.h>
93 #include <sys/resourcevar.h>
94 #include <sys/aio_kern.h>
96 #include <kern/locks.h>
97 #include <sys/uio_internal.h>
98 #include <sys/codesign.h>
99 #include <sys/codedir_internal.h>
101 #include <security/audit/audit.h>
103 #include <sys/mount_internal.h>
104 #include <sys/kdebug.h>
105 #include <sys/sysproto.h>
106 #include <sys/pipe.h>
107 #include <sys/spawn.h>
108 #include <kern/kern_types.h>
109 #include <kern/kalloc.h>
110 #include <libkern/OSAtomic.h>
112 #include <sys/ubc_internal.h>
114 #include <kern/ipc_misc.h>
115 #include <vm/vm_protos.h>
117 #include <mach/mach_port.h>
121 #include <sys/cprotect.h>
125 kern_return_t
ipc_object_copyin(ipc_space_t
, mach_port_name_t
,
126 mach_msg_type_name_t
, ipc_port_t
*);
127 void ipc_port_release_send(ipc_port_t
);
132 static int finishdup(proc_t p
,
133 struct filedesc
*fdp
, int old
, int new, int flags
, int32_t *retval
);
135 int falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
, int locked
);
136 void fg_drop(struct fileproc
* fp
);
137 void fg_free(struct fileglob
*fg
);
138 void fg_ref(struct fileproc
* fp
);
139 void fileport_releasefg(struct fileglob
*fg
);
141 /* flags for close_internal_locked */
142 #define FD_DUP2RESV 1
144 /* We don't want these exported */
147 int unlink1(vfs_context_t
, vnode_t
, user_addr_t
, enum uio_seg
, int);
149 static void _fdrelse(struct proc
* p
, int fd
);
152 extern void file_lock_init(void);
154 extern kauth_scope_t kauth_scope_fileop
;
156 /* Conflict wait queue for when selects collide (opaque type) */
157 extern struct wait_queue select_conflict_queue
;
159 #define f_flag f_fglob->fg_flag
160 #define f_type f_fglob->fg_ops->fo_type
161 #define f_msgcount f_fglob->fg_msgcount
162 #define f_cred f_fglob->fg_cred
163 #define f_ops f_fglob->fg_ops
164 #define f_offset f_fglob->fg_offset
165 #define f_data f_fglob->fg_data
166 #define CHECK_ADD_OVERFLOW_INT64L(x, y) \
167 (((((x) > 0) && ((y) > 0) && ((x) > LLONG_MAX - (y))) || \
168 (((x) < 0) && ((y) < 0) && ((x) < LLONG_MIN - (y)))) \
171 * Descriptor management.
173 struct fmsglist fmsghead
; /* head of list of open files */
174 struct fmsglist fmsg_ithead
; /* head of list of open files */
175 int nfiles
; /* actual number of open files */
178 lck_grp_attr_t
* file_lck_grp_attr
;
179 lck_grp_t
* file_lck_grp
;
180 lck_attr_t
* file_lck_attr
;
182 lck_mtx_t
* uipc_lock
;
186 * check_file_seek_range
188 * Description: Checks if seek offsets are in the range of 0 to LLONG_MAX.
190 * Parameters: fl Flock structure.
191 * cur_file_offset Current offset in the file.
193 * Returns: 0 on Success.
194 * EOVERFLOW on overflow.
195 * EINVAL on offset less than zero.
199 check_file_seek_range(struct flock
*fl
, off_t cur_file_offset
)
201 if (fl
->l_whence
== SEEK_CUR
) {
202 /* Check if the start marker is beyond LLONG_MAX. */
203 if (CHECK_ADD_OVERFLOW_INT64L(fl
->l_start
, cur_file_offset
)) {
204 /* Check if start marker is negative */
205 if (fl
->l_start
< 0) {
210 /* Check if the start marker is negative. */
211 if (fl
->l_start
+ cur_file_offset
< 0) {
214 /* Check if end marker is beyond LLONG_MAX. */
215 if ((fl
->l_len
> 0) && (CHECK_ADD_OVERFLOW_INT64L(fl
->l_start
+
216 cur_file_offset
, fl
->l_len
- 1))) {
219 /* Check if the end marker is negative. */
220 if ((fl
->l_len
<= 0) && (fl
->l_start
+ cur_file_offset
+
224 } else if (fl
->l_whence
== SEEK_SET
) {
225 /* Check if the start marker is negative. */
226 if (fl
->l_start
< 0) {
229 /* Check if the end marker is beyond LLONG_MAX. */
230 if ((fl
->l_len
> 0) &&
231 CHECK_ADD_OVERFLOW_INT64L(fl
->l_start
, fl
->l_len
- 1)) {
234 /* Check if the end marker is negative. */
235 if ((fl
->l_len
< 0) && fl
->l_start
+ fl
->l_len
< 0) {
246 * Description: Initialize the file lock group and the uipc and flist locks
252 * Notes: Called at system startup from bsd_init().
257 /* allocate file lock group attribute and group */
258 file_lck_grp_attr
= lck_grp_attr_alloc_init();
260 file_lck_grp
= lck_grp_alloc_init("file", file_lck_grp_attr
);
262 /* Allocate file lock attribute */
263 file_lck_attr
= lck_attr_alloc_init();
265 uipc_lock
= lck_mtx_alloc_init(file_lck_grp
, file_lck_attr
);
270 * proc_fdlock, proc_fdlock_spin
272 * Description: Lock to control access to the per process struct fileproc
273 * and struct filedesc
275 * Parameters: p Process to take the lock on
279 * Notes: The lock is initialized in forkproc() and destroyed in
280 * reap_child_process().
283 proc_fdlock(proc_t p
)
285 lck_mtx_lock(&p
->p_fdmlock
);
289 proc_fdlock_spin(proc_t p
)
291 lck_mtx_lock_spin(&p
->p_fdmlock
);
295 proc_fdlock_assert(proc_t p
, int assertflags
)
297 lck_mtx_assert(&p
->p_fdmlock
, assertflags
);
304 * Description: Unlock the lock previously locked by a call to proc_fdlock()
306 * Parameters: p Process to drop the lock on
311 proc_fdunlock(proc_t p
)
313 lck_mtx_unlock(&p
->p_fdmlock
);
318 * System calls on descriptors.
325 * Description: Returns the per process maximum size of the descriptor table
327 * Parameters: p Process being queried
328 * retval Pointer to the call return area
333 * *retval (modified) Size of dtable
336 getdtablesize(proc_t p
, __unused
struct getdtablesize_args
*uap
, int32_t *retval
)
339 *retval
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
347 procfdtbl_reservefd(struct proc
* p
, int fd
)
349 p
->p_fd
->fd_ofiles
[fd
] = NULL
;
350 p
->p_fd
->fd_ofileflags
[fd
] |= UF_RESERVED
;
354 procfdtbl_markclosefd(struct proc
* p
, int fd
)
356 p
->p_fd
->fd_ofileflags
[fd
] |= (UF_RESERVED
| UF_CLOSING
);
360 procfdtbl_releasefd(struct proc
* p
, int fd
, struct fileproc
* fp
)
363 p
->p_fd
->fd_ofiles
[fd
] = fp
;
364 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESERVED
;
365 if ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
) == UF_RESVWAIT
) {
366 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESVWAIT
;
372 procfdtbl_waitfd(struct proc
* p
, int fd
)
374 p
->p_fd
->fd_ofileflags
[fd
] |= UF_RESVWAIT
;
375 msleep(&p
->p_fd
, &p
->p_fdmlock
, PRIBIO
, "ftbl_waitfd", NULL
);
380 procfdtbl_clearfd(struct proc
* p
, int fd
)
384 waiting
= (p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
);
385 p
->p_fd
->fd_ofiles
[fd
] = NULL
;
386 p
->p_fd
->fd_ofileflags
[fd
] = 0;
387 if ( waiting
== UF_RESVWAIT
) {
395 * Description: Inline utility function to free an fd in a filedesc
397 * Parameters: fdp Pointer to filedesc fd lies in
399 * reserv fd should be reserved
403 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
407 _fdrelse(struct proc
* p
, int fd
)
409 struct filedesc
*fdp
= p
->p_fd
;
412 if (fd
< fdp
->fd_freefile
)
413 fdp
->fd_freefile
= fd
;
415 if (fd
> fdp
->fd_lastfile
)
416 panic("fdrelse: fd_lastfile inconsistent");
418 procfdtbl_clearfd(p
, fd
);
420 while ((nfd
= fdp
->fd_lastfile
) > 0 &&
421 fdp
->fd_ofiles
[nfd
] == NULL
&&
422 !(fdp
->fd_ofileflags
[nfd
] & UF_RESERVED
))
444 char uio_buf
[ UIO_SIZEOF(1) ];
445 struct vfs_context context
= *(vfs_context_current());
446 bool wrote_some
= false;
450 error
= fp_lookup(p
, fd
, &fp
, 0);
454 if (fp
->f_type
!= DTYPE_VNODE
&& fp
->f_type
!= DTYPE_PIPE
&& fp
->f_type
!= DTYPE_SOCKET
) {
458 if (rw
== UIO_WRITE
&& !(fp
->f_flag
& FWRITE
)) {
463 if (rw
== UIO_READ
&& !(fp
->f_flag
& FREAD
)) {
468 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
470 if (UIO_SEG_IS_USER_SPACE(segflg
))
471 spacetype
= proc_is64bit(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
473 spacetype
= UIO_SYSSPACE
;
475 auio
= uio_createwithbuffer(1, offset
, spacetype
, rw
, &uio_buf
[0], sizeof(uio_buf
));
477 uio_addiov(auio
, base
, len
);
479 if ( !(io_flg
& IO_APPEND
))
482 if (rw
== UIO_WRITE
) {
483 user_ssize_t orig_resid
= uio_resid(auio
);
484 error
= fo_write(fp
, auio
, flags
, &context
);
485 wrote_some
= uio_resid(auio
) < orig_resid
;
487 error
= fo_read(fp
, auio
, flags
, &context
);
490 *aresid
= uio_resid(auio
);
492 if (uio_resid(auio
) && error
== 0)
497 fp_drop_written(p
, fd
, fp
);
499 fp_drop(p
, fd
, fp
, 0);
509 * Description: Duplicate a file descriptor.
511 * Parameters: p Process performing the dup
512 * uap->fd The fd to dup
513 * retval Pointer to the call return area
519 * *retval (modified) The new descriptor
522 dup(proc_t p
, struct dup_args
*uap
, int32_t *retval
)
524 struct filedesc
*fdp
= p
->p_fd
;
530 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
534 if (FP_ISGUARDED(fp
, GUARD_DUP
)) {
535 error
= fp_guard_exception(p
, old
, fp
, kGUARD_EXC_DUP
);
536 (void) fp_drop(p
, old
, fp
, 1);
540 if ( (error
= fdalloc(p
, 0, &new)) ) {
541 fp_drop(p
, old
, fp
, 1);
545 error
= finishdup(p
, fdp
, old
, new, 0, retval
);
546 fp_drop(p
, old
, fp
, 1);
555 * Description: Duplicate a file descriptor to a particular value.
557 * Parameters: p Process performing the dup
558 * uap->from The fd to dup
559 * uap->to The fd to dup it to
560 * retval Pointer to the call return area
566 * *retval (modified) The new descriptor
569 dup2(proc_t p
, struct dup2_args
*uap
, int32_t *retval
)
571 struct filedesc
*fdp
= p
->p_fd
;
572 int old
= uap
->from
, new = uap
->to
;
574 struct fileproc
*fp
, *nfp
;
579 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
583 if (FP_ISGUARDED(fp
, GUARD_DUP
)) {
584 error
= fp_guard_exception(p
, old
, fp
, kGUARD_EXC_DUP
);
585 (void) fp_drop(p
, old
, fp
, 1);
590 (rlim_t
)new >= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
592 fp_drop(p
, old
, fp
, 1);
597 fp_drop(p
, old
, fp
, 1);
602 if (new < 0 || new >= fdp
->fd_nfiles
) {
603 if ( (error
= fdalloc(p
, new, &i
)) ) {
604 fp_drop(p
, old
, fp
, 1);
614 while ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == UF_RESERVED
) {
615 fp_drop(p
, old
, fp
, 1);
616 procfdtbl_waitfd(p
, new);
618 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
623 if ((fdp
->fd_ofiles
[new] != NULL
) &&
624 ((error
= fp_lookup(p
, new, &nfp
, 1)) == 0)) {
625 fp_drop(p
, old
, fp
, 1);
626 if (FP_ISGUARDED(nfp
, GUARD_CLOSE
)) {
627 error
= fp_guard_exception(p
,
628 new, nfp
, kGUARD_EXC_CLOSE
);
629 (void) fp_drop(p
, new, nfp
, 1);
633 (void)close_internal_locked(p
, new, nfp
, FD_DUP2RESV
);
635 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
637 procfdtbl_clearfd(p
, new);
641 if (fdp
->fd_ofiles
[new] != NULL
)
642 panic("dup2: no ref on fileproc %d", new);
644 procfdtbl_reservefd(p
, new);
648 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
653 if (fdp
->fd_ofiles
[new] != 0)
654 panic("dup2: overwriting fd_ofiles with new %d", new);
655 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0)
656 panic("dup2: unreserved fileflags with new %d", new);
658 error
= finishdup(p
, fdp
, old
, new, 0, retval
);
659 fp_drop(p
, old
, fp
, 1);
669 * Description: The file control system call.
671 * Parameters: p Process performing the fcntl
672 * uap->fd The fd to operate against
673 * uap->cmd The command to perform
674 * uap->arg Pointer to the command argument
675 * retval Pointer to the call return area
678 * !0 Errno (see fcntl_nocancel)
681 * *retval (modified) fcntl return value (if any)
683 * Notes: This system call differs from fcntl_nocancel() in that it
684 * tests for cancellation prior to performing a potentially
685 * blocking operation.
688 fcntl(proc_t p
, struct fcntl_args
*uap
, int32_t *retval
)
690 __pthread_testcancel(1);
691 return(fcntl_nocancel(p
, (struct fcntl_nocancel_args
*)uap
, retval
));
698 * Description: A non-cancel-testing file control system call.
700 * Parameters: p Process performing the fcntl
701 * uap->fd The fd to operate against
702 * uap->cmd The command to perform
703 * uap->arg Pointer to the command argument
704 * retval Pointer to the call return area
708 * fp_lookup:EBADF Bad file descriptor
720 * vnode_getwithref:???
728 * vnode_getwithref:???
735 * vnode_getwithref:???
737 * [F_SETSIZE,F_RDADVISE]
740 * vnode_getwithref:???
741 * [F_RDAHEAD,F_NOCACHE]
743 * vnode_getwithref:???
747 * *retval (modified) fcntl return value (if any)
750 fcntl_nocancel(proc_t p
, struct fcntl_nocancel_args
*uap
, int32_t *retval
)
753 struct filedesc
*fdp
= p
->p_fd
;
756 struct vnode
*vp
= NULLVP
; /* for AUDIT_ARG() at end */
757 int i
, tmp
, error
, error2
, flg
= F_POSIX
;
759 struct flocktimeout fltimeout
;
760 struct timespec
*timeout
= NULL
;
761 struct vfs_context context
;
769 AUDIT_ARG(fd
, uap
->fd
);
770 AUDIT_ARG(cmd
, uap
->cmd
);
773 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
777 context
.vc_thread
= current_thread();
778 context
.vc_ucred
= fp
->f_cred
;
780 is64bit
= proc_is64bit(p
);
786 * Since the arg parameter is defined as a long but may be
787 * either a long or a pointer we must take care to handle
788 * sign extension issues. Our sys call munger will sign
789 * extend a long when we are called from a 32-bit process.
790 * Since we can never have an address greater than 32-bits
791 * from a 32-bit process we lop off the top 32-bits to avoid
792 * getting the wrong address
794 argp
= CAST_USER_ADDR_T((uint32_t)uap
->arg
);
797 pop
= &fdp
->fd_ofileflags
[fd
];
800 error
= mac_file_check_fcntl(proc_ucred(p
), fp
->f_fglob
, uap
->cmd
,
809 case F_DUPFD_CLOEXEC
:
810 if (FP_ISGUARDED(fp
, GUARD_DUP
)) {
811 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_DUP
);
814 newmin
= CAST_DOWN_EXPLICIT(int, uap
->arg
); /* arg is an int, so we won't lose bits */
815 AUDIT_ARG(value32
, newmin
);
816 if ((u_int
)newmin
>= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
817 newmin
>= maxfiles
) {
821 if ( (error
= fdalloc(p
, newmin
, &i
)) )
823 error
= finishdup(p
, fdp
, fd
, i
,
824 uap
->cmd
== F_DUPFD_CLOEXEC
? UF_EXCLOSE
: 0, retval
);
828 *retval
= (*pop
& UF_EXCLOSE
)? FD_CLOEXEC
: 0;
833 AUDIT_ARG(value32
, uap
->arg
);
834 if (uap
->arg
& FD_CLOEXEC
)
837 if (FILEPROC_TYPE(fp
) == FTYPE_GUARDED
) {
838 error
= fp_guard_exception(p
,
839 fd
, fp
, kGUARD_EXC_NOCLOEXEC
);
848 *retval
= OFLAGS(fp
->f_flag
);
853 fp
->f_flag
&= ~FCNTLFLAGS
;
854 tmp
= CAST_DOWN_EXPLICIT(int, uap
->arg
); /* arg is an int, so we won't lose bits */
855 AUDIT_ARG(value32
, tmp
);
856 fp
->f_flag
|= FFLAGS(tmp
) & FCNTLFLAGS
;
857 tmp
= fp
->f_flag
& FNONBLOCK
;
858 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
861 tmp
= fp
->f_flag
& FASYNC
;
862 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, &context
);
865 fp
->f_flag
&= ~FNONBLOCK
;
867 (void)fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
871 if (fp
->f_type
== DTYPE_SOCKET
) {
872 *retval
= ((struct socket
*)fp
->f_data
)->so_pgid
;
876 error
= fo_ioctl(fp
, (int)TIOCGPGRP
, (caddr_t
)retval
, &context
);
881 tmp
= CAST_DOWN_EXPLICIT(pid_t
, uap
->arg
); /* arg is an int, so we won't lose bits */
882 AUDIT_ARG(value32
, tmp
);
883 if (fp
->f_type
== DTYPE_SOCKET
) {
884 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
888 if (fp
->f_type
== DTYPE_PIPE
) {
889 error
= fo_ioctl(fp
, TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
896 proc_t p1
= proc_find(tmp
);
901 tmp
= (int)p1
->p_pgrpid
;
904 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
908 tmp
= CAST_DOWN_EXPLICIT(int, uap
->arg
);
909 if (fp
->f_type
== DTYPE_SOCKET
) {
911 error
= sock_setsockopt((struct socket
*)fp
->f_data
,
912 SOL_SOCKET
, SO_NOSIGPIPE
, &tmp
, sizeof (tmp
));
917 struct fileglob
*fg
= fp
->f_fglob
;
919 lck_mtx_lock_spin(&fg
->fg_lock
);
921 fg
->fg_lflags
|= FG_NOSIGPIPE
;
923 fg
->fg_lflags
&= FG_NOSIGPIPE
;
924 lck_mtx_unlock(&fg
->fg_lock
);
930 if (fp
->f_type
== DTYPE_SOCKET
) {
932 int retsize
= sizeof (*retval
);
933 error
= sock_getsockopt((struct socket
*)fp
->f_data
,
934 SOL_SOCKET
, SO_NOSIGPIPE
, retval
, &retsize
);
939 *retval
= (fp
->f_fglob
->fg_lflags
& FG_NOSIGPIPE
) ?
945 case F_SETLKWTIMEOUT
:
948 /* Fall into F_SETLK */
951 if (fp
->f_type
!= DTYPE_VNODE
) {
955 vp
= (struct vnode
*)fp
->f_data
;
958 offset
= fp
->f_offset
;
961 /* Copy in the lock structure */
962 if (uap
->cmd
== F_SETLKWTIMEOUT
) {
963 error
= copyin(argp
, (caddr_t
) &fltimeout
, sizeof(fltimeout
));
968 timeout
= &fltimeout
.timeout
;
970 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
976 /* Check starting byte and ending byte for EOVERFLOW in SEEK_CUR */
977 /* and ending byte for EOVERFLOW in SEEK_SET */
978 error
= check_file_seek_range(&fl
, offset
);
983 if ( (error
= vnode_getwithref(vp
)) ) {
986 if (fl
.l_whence
== SEEK_CUR
)
987 fl
.l_start
+= offset
;
990 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
1000 if ((fflag
& FREAD
) == 0) {
1001 (void)vnode_put(vp
);
1005 // XXX UInt32 unsafe for LP64 kernel
1006 OSBitOrAtomic(P_LADVLOCK
, &p
->p_ladvflag
);
1007 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_SETLK
, &fl
, flg
, &context
, timeout
);
1008 (void)vnode_put(vp
);
1012 if ((fflag
& FWRITE
) == 0) {
1013 (void)vnode_put(vp
);
1017 // XXX UInt32 unsafe for LP64 kernel
1018 OSBitOrAtomic(P_LADVLOCK
, &p
->p_ladvflag
);
1019 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_SETLK
, &fl
, flg
, &context
, timeout
);
1020 (void)vnode_put(vp
);
1024 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &fl
,
1025 F_POSIX
, &context
, timeout
);
1026 (void)vnode_put(vp
);
1030 (void)vnode_put(vp
);
1036 if (fp
->f_type
!= DTYPE_VNODE
) {
1040 vp
= (struct vnode
*)fp
->f_data
;
1042 offset
= fp
->f_offset
;
1045 /* Copy in the lock structure */
1046 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
1050 /* Check starting byte and ending byte for EOVERFLOW in SEEK_CUR */
1051 /* and ending byte for EOVERFLOW in SEEK_SET */
1052 error
= check_file_seek_range(&fl
, offset
);
1057 if ((fl
.l_whence
== SEEK_SET
) && (fl
.l_start
< 0)) {
1062 switch (fl
.l_type
) {
1072 switch (fl
.l_whence
) {
1082 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1083 if (fl
.l_whence
== SEEK_CUR
)
1084 fl
.l_start
+= offset
;
1087 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
1091 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, uap
->cmd
, &fl
, F_POSIX
, &context
, NULL
);
1093 (void)vnode_put(vp
);
1096 error
= copyout((caddr_t
)&fl
, argp
, sizeof(fl
));
1100 case F_PREALLOCATE
: {
1101 fstore_t alloc_struct
; /* structure for allocate command */
1102 u_int32_t alloc_flags
= 0;
1104 if (fp
->f_type
!= DTYPE_VNODE
) {
1109 vp
= (struct vnode
*)fp
->f_data
;
1112 /* make sure that we have write permission */
1113 if ((fp
->f_flag
& FWRITE
) == 0) {
1118 error
= copyin(argp
, (caddr_t
)&alloc_struct
, sizeof(alloc_struct
));
1122 /* now set the space allocated to 0 */
1123 alloc_struct
.fst_bytesalloc
= 0;
1126 * Do some simple parameter checking
1129 /* set up the flags */
1131 alloc_flags
|= PREALLOCATE
;
1133 if (alloc_struct
.fst_flags
& F_ALLOCATECONTIG
)
1134 alloc_flags
|= ALLOCATECONTIG
;
1136 if (alloc_struct
.fst_flags
& F_ALLOCATEALL
)
1137 alloc_flags
|= ALLOCATEALL
;
1140 * Do any position mode specific stuff. The only
1141 * position mode supported now is PEOFPOSMODE
1144 switch (alloc_struct
.fst_posmode
) {
1147 if (alloc_struct
.fst_offset
!= 0) {
1152 alloc_flags
|= ALLOCATEFROMPEOF
;
1156 if (alloc_struct
.fst_offset
<= 0) {
1161 alloc_flags
|= ALLOCATEFROMVOL
;
1169 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1171 * call allocate to get the space
1173 error
= VNOP_ALLOCATE(vp
,alloc_struct
.fst_length
,alloc_flags
,
1174 &alloc_struct
.fst_bytesalloc
, alloc_struct
.fst_offset
,
1176 (void)vnode_put(vp
);
1178 error2
= copyout((caddr_t
)&alloc_struct
, argp
, sizeof(alloc_struct
));
1187 if (fp
->f_type
!= DTYPE_VNODE
) {
1191 vp
= (struct vnode
*)fp
->f_data
;
1194 error
= copyin(argp
, (caddr_t
)&offset
, sizeof (off_t
));
1197 AUDIT_ARG(value64
, offset
);
1199 error
= vnode_getwithref(vp
);
1204 error
= mac_vnode_check_truncate(&context
,
1205 fp
->f_fglob
->fg_cred
, vp
);
1207 (void)vnode_put(vp
);
1212 * Make sure that we are root. Growing a file
1213 * without zero filling the data is a security hole
1214 * root would have access anyway so we'll allow it
1216 if (!kauth_cred_issuser(kauth_cred_get())) {
1222 error
= vnode_setsize(vp
, offset
, IO_NOZEROFILL
,
1226 (void)vnode_put(vp
);
1230 if (fp
->f_type
!= DTYPE_VNODE
) {
1235 fp
->f_fglob
->fg_flag
&= ~FNORDAHEAD
;
1237 fp
->f_fglob
->fg_flag
|= FNORDAHEAD
;
1242 if (fp
->f_type
!= DTYPE_VNODE
) {
1247 fp
->f_fglob
->fg_flag
|= FNOCACHE
;
1249 fp
->f_fglob
->fg_flag
&= ~FNOCACHE
;
1254 if (fp
->f_type
!= DTYPE_VNODE
) {
1259 fp
->f_fglob
->fg_flag
|= FNODIRECT
;
1261 fp
->f_fglob
->fg_flag
&= ~FNODIRECT
;
1265 case F_SINGLE_WRITER
:
1266 if (fp
->f_type
!= DTYPE_VNODE
) {
1271 fp
->f_fglob
->fg_flag
|= FSINGLE_WRITER
;
1273 fp
->f_fglob
->fg_flag
&= ~FSINGLE_WRITER
;
1277 case F_GLOBAL_NOCACHE
:
1278 if (fp
->f_type
!= DTYPE_VNODE
) {
1282 vp
= (struct vnode
*)fp
->f_data
;
1285 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1287 *retval
= vnode_isnocache(vp
);
1290 vnode_setnocache(vp
);
1292 vnode_clearnocache(vp
);
1294 (void)vnode_put(vp
);
1298 case F_CHECK_OPENEVT
:
1299 if (fp
->f_type
!= DTYPE_VNODE
) {
1303 vp
= (struct vnode
*)fp
->f_data
;
1306 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1308 *retval
= vnode_is_openevt(vp
);
1311 vnode_set_openevt(vp
);
1313 vnode_clear_openevt(vp
);
1315 (void)vnode_put(vp
);
1320 struct radvisory ra_struct
;
1322 if (fp
->f_type
!= DTYPE_VNODE
) {
1326 vp
= (struct vnode
*)fp
->f_data
;
1329 if ( (error
= copyin(argp
, (caddr_t
)&ra_struct
, sizeof(ra_struct
))) )
1331 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1332 error
= VNOP_IOCTL(vp
, F_RDADVISE
, (caddr_t
)&ra_struct
, 0, &context
);
1334 (void)vnode_put(vp
);
1341 if (fp
->f_type
!= DTYPE_VNODE
) {
1345 vp
= (struct vnode
*)fp
->f_data
;
1348 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1349 error
= cluster_push(vp
, 0);
1351 (void)vnode_put(vp
);
1356 case F_LOG2PHYS_EXT
: {
1357 struct log2phys l2p_struct
; /* structure for allocate command */
1360 off_t file_offset
= 0;
1364 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1365 error
= copyin(argp
, (caddr_t
)&l2p_struct
, sizeof(l2p_struct
));
1368 file_offset
= l2p_struct
.l2p_devoffset
;
1370 file_offset
= fp
->f_offset
;
1372 if (fp
->f_type
!= DTYPE_VNODE
) {
1376 vp
= (struct vnode
*)fp
->f_data
;
1378 if ( (error
= vnode_getwithref(vp
)) ) {
1381 error
= VNOP_OFFTOBLK(vp
, file_offset
, &lbn
);
1383 (void)vnode_put(vp
);
1386 error
= VNOP_BLKTOOFF(vp
, lbn
, &offset
);
1388 (void)vnode_put(vp
);
1391 devBlockSize
= vfs_devblocksize(vnode_mount(vp
));
1392 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1393 #if defined(__LP64__)
1394 a_size
= l2p_struct
.l2p_contigbytes
;
1396 if ((l2p_struct
.l2p_contigbytes
> SIZE_MAX
) || (l2p_struct
.l2p_contigbytes
< 0)) {
1397 /* size_t is 32-bit on a 32-bit kernel, therefore
1398 * assigning l2p_contigbytes to a_size may have
1399 * caused integer overflow. We, therefore, return
1400 * an error here instead of calculating incorrect
1403 printf ("fcntl: F_LOG2PHYS_EXT: l2p_contigbytes=%lld will overflow, returning error\n", l2p_struct
.l2p_contigbytes
);
1407 a_size
= l2p_struct
.l2p_contigbytes
;
1411 a_size
= devBlockSize
;
1414 error
= VNOP_BLOCKMAP(vp
, offset
, a_size
, &bn
, &run
, NULL
, 0, &context
);
1416 (void)vnode_put(vp
);
1419 l2p_struct
.l2p_flags
= 0; /* for now */
1420 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1421 l2p_struct
.l2p_contigbytes
= run
- (file_offset
- offset
);
1423 l2p_struct
.l2p_contigbytes
= 0; /* for now */
1427 * The block number being -1 suggests that the file offset is not backed
1428 * by any real blocks on-disk. As a result, just let it be passed back up wholesale.
1431 /* Don't multiply it by the block size */
1432 l2p_struct
.l2p_devoffset
= bn
;
1435 l2p_struct
.l2p_devoffset
= bn
* devBlockSize
;
1436 l2p_struct
.l2p_devoffset
+= file_offset
- offset
;
1438 error
= copyout((caddr_t
)&l2p_struct
, argp
, sizeof(l2p_struct
));
1446 if (fp
->f_type
!= DTYPE_VNODE
) {
1450 vp
= (struct vnode
*)fp
->f_data
;
1453 pathlen
= MAXPATHLEN
;
1454 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
1455 if (pathbufp
== NULL
) {
1459 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1460 error
= vn_getpath(vp
, pathbufp
, &pathlen
);
1461 (void)vnode_put(vp
);
1464 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
1466 FREE(pathbufp
, M_TEMP
);
1470 case F_PATHPKG_CHECK
: {
1474 if (fp
->f_type
!= DTYPE_VNODE
) {
1478 vp
= (struct vnode
*)fp
->f_data
;
1481 pathlen
= MAXPATHLEN
;
1482 pathbufp
= kalloc(MAXPATHLEN
);
1484 if ( (error
= copyinstr(argp
, pathbufp
, MAXPATHLEN
, &pathlen
)) == 0 ) {
1485 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1486 AUDIT_ARG(text
, pathbufp
);
1487 error
= vn_path_package_check(vp
, pathbufp
, pathlen
, retval
);
1489 (void)vnode_put(vp
);
1492 kfree(pathbufp
, MAXPATHLEN
);
1496 case F_CHKCLEAN
: // used by regression tests to see if all dirty pages got cleaned by fsync()
1497 case F_FULLFSYNC
: // fsync + flush the journal + DKIOCSYNCHRONIZECACHE
1498 case F_FREEZE_FS
: // freeze all other fs operations for the fs of this fd
1499 case F_THAW_FS
: { // thaw all frozen fs operations for the fs of this fd
1500 if (fp
->f_type
!= DTYPE_VNODE
) {
1504 vp
= (struct vnode
*)fp
->f_data
;
1507 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1508 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
1510 (void)vnode_put(vp
);
1516 * SPI (private) for opening a file starting from a dir fd
1519 struct user_fopenfrom fopen
;
1520 struct vnode_attr va
;
1521 struct nameidata nd
;
1524 /* Check if this isn't a valid file descriptor */
1525 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1526 (fp
->f_flag
& FREAD
) == 0) {
1530 vp
= (struct vnode
*)fp
->f_data
;
1533 if (vnode_getwithref(vp
)) {
1538 /* Only valid for directories */
1539 if (vp
->v_type
!= VDIR
) {
1545 /* Get flags, mode and pathname arguments. */
1546 if (IS_64BIT_PROCESS(p
)) {
1547 error
= copyin(argp
, &fopen
, sizeof(fopen
));
1549 struct user32_fopenfrom fopen32
;
1551 error
= copyin(argp
, &fopen32
, sizeof(fopen32
));
1552 fopen
.o_flags
= fopen32
.o_flags
;
1553 fopen
.o_mode
= fopen32
.o_mode
;
1554 fopen
.o_pathname
= CAST_USER_ADDR_T(fopen32
.o_pathname
);
1560 AUDIT_ARG(fflags
, fopen
.o_flags
);
1561 AUDIT_ARG(mode
, fopen
.o_mode
);
1563 /* Mask off all but regular access permissions */
1564 cmode
= ((fopen
.o_mode
&~ fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
1565 VATTR_SET(&va
, va_mode
, cmode
& ACCESSPERMS
);
1567 /* Start the lookup relative to the file descriptor's vnode. */
1568 NDINIT(&nd
, LOOKUP
, OP_OPEN
, USEDVP
| FOLLOW
| AUDITVNPATH1
, UIO_USERSPACE
,
1569 fopen
.o_pathname
, &context
);
1572 error
= open1(&context
, &nd
, fopen
.o_flags
, &va
,
1573 fileproc_alloc_init
, NULL
, retval
);
1579 * SPI (private) for unlinking a file starting from a dir fd
1581 case F_UNLINKFROM
: {
1582 user_addr_t pathname
;
1584 /* Check if this isn't a valid file descriptor */
1585 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1586 (fp
->f_flag
& FREAD
) == 0) {
1590 vp
= (struct vnode
*)fp
->f_data
;
1593 if (vnode_getwithref(vp
)) {
1598 /* Only valid for directories */
1599 if (vp
->v_type
!= VDIR
) {
1605 /* Get flags, mode and pathname arguments. */
1606 if (IS_64BIT_PROCESS(p
)) {
1607 pathname
= (user_addr_t
)argp
;
1609 pathname
= CAST_USER_ADDR_T(argp
);
1612 /* Start the lookup relative to the file descriptor's vnode. */
1613 error
= unlink1(&context
, vp
, pathname
, UIO_USERSPACE
, 0);
1622 case F_ADDFILESIGS_FOR_DYLD_SIM
:
1624 struct user_fsignatures fs
;
1626 vm_offset_t kernel_blob_addr
;
1627 vm_size_t kernel_blob_size
;
1628 int blob_add_flags
= 0;
1630 if (fp
->f_type
!= DTYPE_VNODE
) {
1634 vp
= (struct vnode
*)fp
->f_data
;
1637 if (uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
1638 blob_add_flags
|= MAC_VNODE_CHECK_DYLD_SIM
;
1639 if ((p
->p_csflags
& CS_KILL
) == 0) {
1641 p
->p_csflags
|= CS_KILL
;
1646 error
= vnode_getwithref(vp
);
1650 if (IS_64BIT_PROCESS(p
)) {
1651 error
= copyin(argp
, &fs
, sizeof (fs
));
1653 struct user32_fsignatures fs32
;
1655 error
= copyin(argp
, &fs32
, sizeof (fs32
));
1656 fs
.fs_file_start
= fs32
.fs_file_start
;
1657 fs
.fs_blob_start
= CAST_USER_ADDR_T(fs32
.fs_blob_start
);
1658 fs
.fs_blob_size
= fs32
.fs_blob_size
;
1666 struct cs_blob
* existing_blob
= ubc_cs_blob_get(vp
, CPU_TYPE_ANY
, fs
.fs_file_start
);
1667 if (existing_blob
!= NULL
)
1669 /* If this is for dyld_sim revalidate the blob */
1670 if (uap
->cmd
== F_ADDFILESIGS_FOR_DYLD_SIM
) {
1671 error
= ubc_cs_blob_revalidate(vp
, existing_blob
, blob_add_flags
);
1677 * An arbitrary limit, to prevent someone from mapping in a 20GB blob. This should cover
1678 * our use cases for the immediate future, but note that at the time of this commit, some
1679 * platforms are nearing 2MB blob sizes (with a prior soft limit of 2.5MB).
1681 * We should consider how we can manage this more effectively; the above means that some
1682 * platforms are using megabytes of memory for signing data; it merely hasn't crossed the
1683 * threshold considered ridiculous at the time of this change.
1685 #define CS_MAX_BLOB_SIZE (10ULL * 1024ULL * 1024ULL)
1686 if (fs
.fs_blob_size
> CS_MAX_BLOB_SIZE
) {
1692 kernel_blob_size
= CAST_DOWN(vm_size_t
, fs
.fs_blob_size
);
1693 kr
= ubc_cs_blob_allocate(&kernel_blob_addr
, &kernel_blob_size
);
1694 if (kr
!= KERN_SUCCESS
) {
1700 if(uap
->cmd
== F_ADDSIGS
) {
1701 error
= copyin(fs
.fs_blob_start
,
1702 (void *) kernel_blob_addr
,
1704 } else /* F_ADDFILESIGS */ {
1707 error
= vn_rdwr(UIO_READ
,
1709 (caddr_t
) kernel_blob_addr
,
1711 fs
.fs_file_start
+ fs
.fs_blob_start
,
1717 if ((error
== 0) && resid
) {
1718 /* kernel_blob_size rounded to a page size, but signature may be at end of file */
1719 memset((void *)(kernel_blob_addr
+ (kernel_blob_size
- resid
)), 0x0, resid
);
1724 ubc_cs_blob_deallocate(kernel_blob_addr
,
1730 error
= ubc_cs_blob_add(
1732 CPU_TYPE_ANY
, /* not for a specific architecture */
1738 ubc_cs_blob_deallocate(kernel_blob_addr
,
1741 /* ubc_blob_add() has consumed "kernel_blob_addr" */
1742 #if CHECK_CS_VALIDATION_BITMAP
1743 ubc_cs_validation_bitmap_allocate( vp
);
1747 (void) vnode_put(vp
);
1751 #ifdef SECURE_KERNEL
1753 #else /* !SECURE_KERNEL */
1756 if (fp
->f_type
!= DTYPE_VNODE
) {
1760 vp
= (struct vnode
*)fp
->f_data
;
1762 error
= vnode_getwithref(vp
);
1766 error
= copyin(argp
, &offsetMacho
, sizeof(offsetMacho
));
1768 (void)vnode_put(vp
);
1773 error
= mac_vnode_find_sigs(p
, vp
, offsetMacho
);
1778 (void)vnode_put(vp
);
1781 #endif /* SECURE_KERNEL */
1785 case F_GETPROTECTIONCLASS
: {
1788 if (fp
->f_type
!= DTYPE_VNODE
) {
1792 vp
= (struct vnode
*)fp
->f_data
;
1796 if (vnode_getwithref(vp
)) {
1801 error
= cp_vnode_getclass (vp
, &class);
1810 case F_SETPROTECTIONCLASS
: {
1811 /* tmp must be a valid PROTECTION_CLASS_* */
1812 tmp
= CAST_DOWN_EXPLICIT(uint32_t, uap
->arg
);
1814 if (fp
->f_type
!= DTYPE_VNODE
) {
1818 vp
= (struct vnode
*)fp
->f_data
;
1822 if (vnode_getwithref(vp
)) {
1827 /* Only go forward if you have write access */
1828 vfs_context_t ctx
= vfs_context_current();
1829 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
1834 error
= cp_vnode_setclass (vp
, tmp
);
1839 case F_TRANSCODEKEY
: {
1841 char *backup_keyp
= NULL
;
1842 unsigned backup_key_len
= CP_MAX_WRAPPEDKEYSIZE
;
1844 if (fp
->f_type
!= DTYPE_VNODE
) {
1849 vp
= (struct vnode
*)fp
->f_data
;
1852 if (vnode_getwithref(vp
)) {
1857 MALLOC(backup_keyp
, char *, backup_key_len
, M_TEMP
, M_WAITOK
);
1858 if (backup_keyp
== NULL
) {
1863 error
= cp_vnode_transcode (vp
, backup_keyp
, &backup_key_len
);
1867 error
= copyout((caddr_t
)backup_keyp
, argp
, backup_key_len
);
1868 *retval
= backup_key_len
;
1871 FREE(backup_keyp
, M_TEMP
);
1876 case F_GETPROTECTIONLEVEL
: {
1877 uint32_t cp_version
= 0;
1879 if (fp
->f_type
!= DTYPE_VNODE
) {
1884 vp
= (struct vnode
*) fp
->f_data
;
1887 if (vnode_getwithref(vp
)) {
1893 * if cp_get_major_vers fails, error will be set to proper errno
1894 * and cp_version will still be 0.
1897 error
= cp_get_root_major_vers (vp
, &cp_version
);
1898 *retval
= cp_version
;
1904 case F_GETDEFAULTPROTLEVEL
: {
1905 uint32_t cp_default
= 0;
1907 if (fp
->f_type
!= DTYPE_VNODE
) {
1912 vp
= (struct vnode
*) fp
->f_data
;
1915 if (vnode_getwithref(vp
)) {
1921 * if cp_get_major_vers fails, error will be set to proper errno
1922 * and cp_version will still be 0.
1925 error
= cp_get_default_level(vp
, &cp_default
);
1926 *retval
= cp_default
;
1933 #endif /* CONFIG_PROTECT */
1935 case F_MOVEDATAEXTENTS
: {
1936 struct fileproc
*fp2
= NULL
;
1937 struct vnode
*src_vp
= NULLVP
;
1938 struct vnode
*dst_vp
= NULLVP
;
1939 /* We need to grab the 2nd FD out of the argments before moving on. */
1940 int fd2
= CAST_DOWN_EXPLICIT(int32_t, uap
->arg
);
1942 if (fp
->f_type
!= DTYPE_VNODE
) {
1947 /* For now, special case HFS+ only, since this is SPI. */
1948 src_vp
= (struct vnode
*)fp
->f_data
;
1949 if (src_vp
->v_tag
!= VT_HFS
) {
1955 * Get the references before we start acquiring iocounts on the vnodes,
1956 * while we still hold the proc fd lock
1958 if ( (error
= fp_lookup(p
, fd2
, &fp2
, 1)) ) {
1962 if (fp2
->f_type
!= DTYPE_VNODE
) {
1963 fp_drop(p
, fd2
, fp2
, 1);
1967 dst_vp
= (struct vnode
*)fp2
->f_data
;
1968 if (dst_vp
->v_tag
!= VT_HFS
) {
1969 fp_drop(p
, fd2
, fp2
, 1);
1975 /* Re-do MAC checks against the new FD, pass in a fake argument */
1976 error
= mac_file_check_fcntl(proc_ucred(p
), fp2
->f_fglob
, uap
->cmd
, 0);
1978 fp_drop(p
, fd2
, fp2
, 1);
1982 /* Audit the 2nd FD */
1987 if (vnode_getwithref(src_vp
)) {
1988 fp_drop(p
, fd2
, fp2
, 0);
1992 if (vnode_getwithref(dst_vp
)) {
1994 fp_drop(p
, fd2
, fp2
, 0);
2000 * Basic asserts; validate they are not the same and that
2001 * both live on the same filesystem.
2003 if (dst_vp
== src_vp
) {
2006 fp_drop (p
, fd2
, fp2
, 0);
2011 if (dst_vp
->v_mount
!= src_vp
->v_mount
) {
2014 fp_drop (p
, fd2
, fp2
, 0);
2019 /* Now we have a legit pair of FDs. Go to work */
2021 /* Now check for write access to the target files */
2022 if(vnode_authorize(src_vp
, NULLVP
,
2023 (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), &context
) != 0) {
2026 fp_drop(p
, fd2
, fp2
, 0);
2031 if(vnode_authorize(dst_vp
, NULLVP
,
2032 (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), &context
) != 0) {
2035 fp_drop(p
, fd2
, fp2
, 0);
2040 /* Verify that both vps point to files and not directories */
2041 if ( !vnode_isreg(src_vp
) || !vnode_isreg(dst_vp
)) {
2045 fp_drop (p
, fd2
, fp2
, 0);
2050 * The exchangedata syscall handler passes in 0 for the flags to VNOP_EXCHANGE.
2051 * We'll pass in our special bit indicating that the new behavior is expected
2054 error
= VNOP_EXCHANGE(src_vp
, dst_vp
, FSOPT_EXCHANGE_DATA_ONLY
, &context
);
2058 fp_drop(p
, fd2
, fp2
, 0);
2063 * SPI for making a file compressed.
2065 case F_MAKECOMPRESSED
: {
2066 uint32_t gcounter
= CAST_DOWN_EXPLICIT(uint32_t, uap
->arg
);
2068 if (fp
->f_type
!= DTYPE_VNODE
) {
2073 vp
= (struct vnode
*) fp
->f_data
;
2077 if (vnode_getwithref(vp
)) {
2083 if ((vnode_isreg(vp
) == 0) && (vnode_islnk(vp
) == 0)) {
2089 /* invoke ioctl to pass off to FS */
2090 /* Only go forward if you have write access */
2091 vfs_context_t ctx
= vfs_context_current();
2092 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2098 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)&gcounter
, 0, &context
);
2105 * SPI (private) for indicating to a filesystem that subsequent writes to
2106 * the open FD will written to the Fastflow.
2108 case F_SET_GREEDY_MODE
:
2109 /* intentionally drop through to the same handler as F_SETSTATIC.
2110 * both fcntls should pass the argument and their selector into VNOP_IOCTL.
2114 * SPI (private) for indicating to a filesystem that subsequent writes to
2115 * the open FD will represent static content.
2117 case F_SETSTATICCONTENT
: {
2118 caddr_t ioctl_arg
= NULL
;
2121 ioctl_arg
= (caddr_t
) 1;
2124 if (fp
->f_type
!= DTYPE_VNODE
) {
2128 vp
= (struct vnode
*)fp
->f_data
;
2131 error
= vnode_getwithref(vp
);
2137 /* Only go forward if you have write access */
2138 vfs_context_t ctx
= vfs_context_current();
2139 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2145 error
= VNOP_IOCTL(vp
, uap
->cmd
, ioctl_arg
, 0, &context
);
2146 (void)vnode_put(vp
);
2152 * SPI (private) for indicating to the lower level storage driver that the
2153 * subsequent writes should be of a particular IO type (burst, greedy, static),
2154 * or other flavors that may be necessary.
2161 /* extract 32 bits of flags from userland */
2162 param_ptr
= (caddr_t
) uap
->arg
;
2163 param
= (uint32_t) param_ptr
;
2166 /* If no argument is specified, error out */
2172 * Validate the different types of flags that can be specified:
2173 * all of them are mutually exclusive for now.
2176 case F_IOTYPE_ISOCHRONOUS
:
2185 if (fp
->f_type
!= DTYPE_VNODE
) {
2189 vp
= (struct vnode
*)fp
->f_data
;
2192 error
= vnode_getwithref(vp
);
2198 /* Only go forward if you have write access */
2199 vfs_context_t ctx
= vfs_context_current();
2200 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2206 error
= VNOP_IOCTL(vp
, uap
->cmd
, param_ptr
, 0, &context
);
2207 (void)vnode_put(vp
);
2214 * Extract the CodeDirectory of the vnode associated with
2215 * the file descriptor and copy it back to user space
2217 case F_GETCODEDIR
: {
2218 struct user_fcodeblobs args
;
2220 if (fp
->f_type
!= DTYPE_VNODE
) {
2225 vp
= (struct vnode
*)fp
->f_data
;
2228 if ((fp
->f_flag
& FREAD
) == 0) {
2233 if (IS_64BIT_PROCESS(p
)) {
2234 struct user64_fcodeblobs args64
;
2236 error
= copyin(argp
, &args64
, sizeof(args64
));
2240 args
.f_cd_hash
= args64
.f_cd_hash
;
2241 args
.f_hash_size
= args64
.f_hash_size
;
2242 args
.f_cd_buffer
= args64
.f_cd_buffer
;
2243 args
.f_cd_size
= args64
.f_cd_size
;
2244 args
.f_out_size
= args64
.f_out_size
;
2245 args
.f_arch
= args64
.f_arch
;
2247 struct user32_fcodeblobs args32
;
2249 error
= copyin(argp
, &args32
, sizeof(args32
));
2253 args
.f_cd_hash
= CAST_USER_ADDR_T(args32
.f_cd_hash
);
2254 args
.f_hash_size
= args32
.f_hash_size
;
2255 args
.f_cd_buffer
= CAST_USER_ADDR_T(args32
.f_cd_buffer
);
2256 args
.f_cd_size
= args32
.f_cd_size
;
2257 args
.f_out_size
= CAST_USER_ADDR_T(args32
.f_out_size
);
2258 args
.f_arch
= args32
.f_arch
;
2261 if (vp
->v_ubcinfo
== NULL
) {
2266 struct cs_blob
*t_blob
= vp
->v_ubcinfo
->cs_blobs
;
2269 * This call fails if there is no cs_blob corresponding to the
2270 * vnode, or if there are multiple cs_blobs present, and the caller
2271 * did not specify which cpu_type they want the cs_blob for
2273 if (t_blob
== NULL
) {
2274 error
= ENOENT
; /* there is no codesigning blob for this process */
2276 } else if (args
.f_arch
== 0 && t_blob
->csb_next
!= NULL
) {
2277 error
= ENOENT
; /* too many architectures and none specified */
2281 /* If the user specified an architecture, find the right blob */
2282 if (args
.f_arch
!= 0) {
2284 if (t_blob
->csb_cpu_type
== args
.f_arch
)
2286 t_blob
= t_blob
->csb_next
;
2288 /* The cpu_type the user requested could not be found */
2289 if (t_blob
== NULL
) {
2295 const CS_SuperBlob
*super_blob
= (void *)t_blob
->csb_mem_kaddr
;
2296 const CS_CodeDirectory
*cd
= findCodeDirectory(super_blob
,
2297 (char *) super_blob
,
2298 (char *) super_blob
+ t_blob
->csb_mem_size
);
2304 uint64_t buffer_size
= ntohl(cd
->length
);
2306 if (buffer_size
> UINT_MAX
) {
2311 error
= copyout(&buffer_size
, args
.f_out_size
, sizeof(unsigned int));
2315 if (sizeof(t_blob
->csb_sha1
) > args
.f_hash_size
||
2316 buffer_size
> args
.f_cd_size
) {
2321 error
= copyout(t_blob
->csb_sha1
, args
.f_cd_hash
, sizeof(t_blob
->csb_sha1
));
2324 error
= copyout(cd
, args
.f_cd_buffer
, buffer_size
);
2332 * Set the vnode pointed to by 'fd'
2333 * and tag it as the (potentially future) backing store
2334 * for another filesystem
2336 case F_SETBACKINGSTORE
: {
2337 if (fp
->f_type
!= DTYPE_VNODE
) {
2342 vp
= (struct vnode
*)fp
->f_data
;
2344 if (vp
->v_tag
!= VT_HFS
) {
2350 if (vnode_getwithref(vp
)) {
2355 /* only proceed if you have write access */
2356 vfs_context_t ctx
= vfs_context_current();
2357 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2364 /* If arg != 0, set, otherwise unset */
2366 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
)1, 0, &context
);
2369 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
2377 * like F_GETPATH, but special semantics for
2378 * the mobile time machine handler.
2380 case F_GETPATH_MTMINFO
: {
2384 if (fp
->f_type
!= DTYPE_VNODE
) {
2388 vp
= (struct vnode
*)fp
->f_data
;
2391 pathlen
= MAXPATHLEN
;
2392 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
2393 if (pathbufp
== NULL
) {
2397 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
2398 int backingstore
= 0;
2400 /* Check for error from vn_getpath before moving on */
2401 if ((error
= vn_getpath(vp
, pathbufp
, &pathlen
)) == 0) {
2402 if (vp
->v_tag
== VT_HFS
) {
2403 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
) &backingstore
, 0, &context
);
2405 (void)vnode_put(vp
);
2408 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
2412 * If the copyout was successful, now check to ensure
2413 * that this vnode is not a BACKINGSTORE vnode. mtmd
2414 * wants the path regardless.
2421 (void)vnode_put(vp
);
2423 FREE(pathbufp
, M_TEMP
);
2429 * This is an fcntl() that we d not recognize at this level;
2430 * if this is a vnode, we send it down into the VNOP_IOCTL
2431 * for this vnode; this can include special devices, and will
2432 * effectively overload fcntl() to send ioctl()'s.
2434 if((uap
->cmd
& IOC_VOID
) && (uap
->cmd
& IOC_INOUT
)){
2439 /* Catch any now-invalid fcntl() selectors */
2441 case F_MARKDEPENDENCY
:
2448 if (fp
->f_type
!= DTYPE_VNODE
) {
2452 vp
= (struct vnode
*)fp
->f_data
;
2455 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
2456 #define STK_PARAMS 128
2457 char stkbuf
[STK_PARAMS
];
2461 * For this to work properly, we have to copy in the
2462 * ioctl() cmd argument if there is one; we must also
2463 * check that a command parameter, if present, does
2464 * not exceed the maximum command length dictated by
2465 * the number of bits we have available in the command
2466 * to represent a structure length. Finally, we have
2467 * to copy the results back out, if it is that type of
2470 size
= IOCPARM_LEN(uap
->cmd
);
2471 if (size
> IOCPARM_MAX
) {
2472 (void)vnode_put(vp
);
2478 if (size
> sizeof (stkbuf
)) {
2479 if ((memp
= (caddr_t
)kalloc(size
)) == 0) {
2480 (void)vnode_put(vp
);
2489 if (uap
->cmd
& IOC_IN
) {
2492 error
= copyin(argp
, data
, size
);
2494 (void)vnode_put(vp
);
2500 /* Bzero the section beyond that which was needed */
2501 if (size
<= sizeof(stkbuf
)) {
2502 bzero ( (((uint8_t*)data
) + size
), (sizeof(stkbuf
) - size
));
2507 *(user_addr_t
*)data
= argp
;
2509 *(uint32_t *)data
= (uint32_t)argp
;
2512 } else if ((uap
->cmd
& IOC_OUT
) && size
) {
2514 * Zero the buffer so the user always
2515 * gets back something deterministic.
2518 } else if (uap
->cmd
& IOC_VOID
) {
2520 *(user_addr_t
*)data
= argp
;
2522 *(uint32_t *)data
= (uint32_t)argp
;
2526 error
= VNOP_IOCTL(vp
, uap
->cmd
, CAST_DOWN(caddr_t
, data
), 0, &context
);
2528 (void)vnode_put(vp
);
2530 /* Copy any output data to user */
2531 if (error
== 0 && (uap
->cmd
& IOC_OUT
) && size
)
2532 error
= copyout(data
, argp
, size
);
2540 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
2541 fp_drop(p
, fd
, fp
, 0);
2544 fp_drop(p
, fd
, fp
, 1);
2553 * Description: Common code for dup, dup2, and fcntl(F_DUPFD).
2555 * Parameters: p Process performing the dup
2557 * new The fd to dup it to
2558 * fd_flags Flags to augment the new fd
2559 * retval Pointer to the call return area
2561 * Returns: 0 Success
2566 * *retval (modified) The new descriptor
2568 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
2571 * Notes: This function may drop and reacquire this lock; it is unsafe
2572 * for a caller to assume that other state protected by the lock
2573 * has not been subsequently changed out from under it.
2577 struct filedesc
*fdp
, int old
, int new, int fd_flags
, int32_t *retval
)
2579 struct fileproc
*nfp
;
2580 struct fileproc
*ofp
;
2586 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2588 if ((ofp
= fdp
->fd_ofiles
[old
]) == NULL
||
2589 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
)) {
2596 error
= mac_file_check_dup(proc_ucred(p
), ofp
->f_fglob
, new);
2606 nfp
= fileproc_alloc_init(NULL
);
2616 nfp
->f_fglob
= ofp
->f_fglob
;
2619 if (fdp
->fd_ofiles
[new] != 0)
2620 panic("finishdup: overwriting fd_ofiles with new %d", new);
2621 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0)
2622 panic("finishdup: unreserved fileflags with new %d", new);
2625 if (new > fdp
->fd_lastfile
)
2626 fdp
->fd_lastfile
= new;
2627 *fdflags(p
, new) |= fd_flags
;
2628 procfdtbl_releasefd(p
, new, nfp
);
2637 * Description: The implementation of the close(2) system call
2639 * Parameters: p Process in whose per process file table
2640 * the close is to occur
2641 * uap->fd fd to be closed
2644 * Returns: 0 Success
2645 * fp_lookup:EBADF Bad file descriptor
2646 * fp_guard_exception:??? Guarded file descriptor
2647 * close_internal:EBADF
2648 * close_internal:??? Anything returnable by a per-fileops
2652 close(proc_t p
, struct close_args
*uap
, int32_t *retval
)
2654 __pthread_testcancel(1);
2655 return(close_nocancel(p
, (struct close_nocancel_args
*)uap
, retval
));
2660 close_nocancel(proc_t p
, struct close_nocancel_args
*uap
, __unused
int32_t *retval
)
2662 struct fileproc
*fp
;
2666 AUDIT_SYSCLOSE(p
, fd
);
2670 if ( (error
= fp_lookup(p
,fd
,&fp
, 1)) ) {
2675 if (FP_ISGUARDED(fp
, GUARD_CLOSE
)) {
2676 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_CLOSE
);
2677 (void) fp_drop(p
, fd
, fp
, 1);
2682 error
= close_internal_locked(p
, fd
, fp
, 0);
2691 * close_internal_locked
2693 * Close a file descriptor.
2695 * Parameters: p Process in whose per process file table
2696 * the close is to occur
2697 * fd fd to be closed
2698 * fp fileproc associated with the fd
2700 * Returns: 0 Success
2701 * EBADF fd already in close wait state
2702 * closef_locked:??? Anything returnable by a per-fileops
2705 * Locks: Assumes proc_fdlock for process is held by the caller and returns
2708 * Notes: This function may drop and reacquire this lock; it is unsafe
2709 * for a caller to assume that other state protected by the lock
2710 * has not been subsequently changed out from under it.
2713 close_internal_locked(proc_t p
, int fd
, struct fileproc
*fp
, int flags
)
2715 struct filedesc
*fdp
= p
->p_fd
;
2717 int resvfd
= flags
& FD_DUP2RESV
;
2721 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2724 /* Keep people from using the filedesc while we are closing it */
2725 procfdtbl_markclosefd(p
, fd
);
2728 if ((fp
->f_flags
& FP_CLOSING
) == FP_CLOSING
) {
2729 panic("close_internal_locked: being called on already closing fd");
2734 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
2735 panic("close_internal: unreserved fileflags with fd %d", fd
);
2738 fp
->f_flags
|= FP_CLOSING
;
2740 if ( (fp
->f_flags
& FP_AIOISSUED
) || kauth_authorize_fileop_has_listeners() ) {
2744 if ( (fp
->f_type
== DTYPE_VNODE
) && kauth_authorize_fileop_has_listeners() ) {
2746 * call out to allow 3rd party notification of close.
2747 * Ignore result of kauth_authorize_fileop call.
2749 if (vnode_getwithref((vnode_t
)fp
->f_data
) == 0) {
2750 u_int fileop_flags
= 0;
2751 if ((fp
->f_flags
& FP_WRITTEN
) != 0)
2752 fileop_flags
|= KAUTH_FILEOP_CLOSE_MODIFIED
;
2753 kauth_authorize_fileop(fp
->f_fglob
->fg_cred
, KAUTH_FILEOP_CLOSE
,
2754 (uintptr_t)fp
->f_data
, (uintptr_t)fileop_flags
);
2755 vnode_put((vnode_t
)fp
->f_data
);
2758 if (fp
->f_flags
& FP_AIOISSUED
)
2760 * cancel all async IO requests that can be cancelled.
2762 _aio_close( p
, fd
);
2767 if (fd
< fdp
->fd_knlistsize
)
2768 knote_fdclose(p
, fd
);
2770 if (fp
->f_flags
& FP_WAITEVENT
)
2771 (void)waitevent_close(p
, fp
);
2773 fileproc_drain(p
, fp
);
2778 procfdtbl_reservefd(p
, fd
);
2781 error
= closef_locked(fp
, fp
->f_fglob
, p
);
2782 if ((fp
->f_flags
& FP_WAITCLOSE
) == FP_WAITCLOSE
)
2783 wakeup(&fp
->f_flags
);
2784 fp
->f_flags
&= ~(FP_WAITCLOSE
| FP_CLOSING
);
2794 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
2795 panic("close with reserved fd returns with freed fd:%d: proc: %p", fd
, p
);
2806 * Description: Return status information about a file descriptor.
2808 * Parameters: p The process doing the fstat
2810 * ub The user stat buffer
2811 * xsecurity The user extended security
2812 * buffer, or 0 if none
2813 * xsecurity_size The size of xsecurity, or 0
2815 * isstat64 Flag to indicate 64 bit version
2816 * for inode size, etc.
2818 * Returns: 0 Success
2821 * fp_lookup:EBADF Bad file descriptor
2822 * vnode_getwithref:???
2824 * vnode_getwithref:???
2831 * Notes: Internal implementation for all other fstat() related
2834 * XXX switch on node type is bogus; need a stat in struct
2835 * XXX fileops instead.
2838 fstat1(proc_t p
, int fd
, user_addr_t ub
, user_addr_t xsecurity
, user_addr_t xsecurity_size
, int isstat64
)
2840 struct fileproc
*fp
;
2846 struct user64_stat user64_sb
;
2847 struct user32_stat user32_sb
;
2848 struct user64_stat64 user64_sb64
;
2849 struct user32_stat64 user32_sb64
;
2854 kauth_filesec_t fsec
;
2855 user_size_t xsecurity_bufsize
;
2856 vfs_context_t ctx
= vfs_context_current();
2862 if ((error
= fp_lookup(p
, fd
, &fp
, 0)) != 0) {
2867 fsec
= KAUTH_FILESEC_NONE
;
2869 sbptr
= (void *)&source
;
2874 if ((error
= vnode_getwithref((vnode_t
)data
)) == 0) {
2876 * If the caller has the file open, and is not
2877 * requesting extended security information, we are
2878 * going to let them get the basic stat information.
2880 if (xsecurity
== USER_ADDR_NULL
) {
2881 error
= vn_stat_noauth((vnode_t
)data
, sbptr
, NULL
, isstat64
, ctx
);
2883 error
= vn_stat((vnode_t
)data
, sbptr
, &fsec
, isstat64
, ctx
);
2886 AUDIT_ARG(vnpath
, (struct vnode
*)data
, ARG_VNODE1
);
2887 (void)vnode_put((vnode_t
)data
);
2893 error
= soo_stat((struct socket
*)data
, sbptr
, isstat64
);
2895 #endif /* SOCKETS */
2898 error
= pipe_stat((void *)data
, sbptr
, isstat64
);
2902 error
= pshm_stat((void *)data
, sbptr
, isstat64
);
2906 error
= kqueue_stat((void *)data
, sbptr
, isstat64
, p
);
2916 if (isstat64
!= 0) {
2917 source
.sb64
.st_lspare
= 0;
2918 source
.sb64
.st_qspare
[0] = 0LL;
2919 source
.sb64
.st_qspare
[1] = 0LL;
2921 if (IS_64BIT_PROCESS(current_proc())) {
2922 munge_user64_stat64(&source
.sb64
, &dest
.user64_sb64
);
2923 my_size
= sizeof(dest
.user64_sb64
);
2924 sbp
= (caddr_t
)&dest
.user64_sb64
;
2926 munge_user32_stat64(&source
.sb64
, &dest
.user32_sb64
);
2927 my_size
= sizeof(dest
.user32_sb64
);
2928 sbp
= (caddr_t
)&dest
.user32_sb64
;
2931 source
.sb
.st_lspare
= 0;
2932 source
.sb
.st_qspare
[0] = 0LL;
2933 source
.sb
.st_qspare
[1] = 0LL;
2934 if (IS_64BIT_PROCESS(current_proc())) {
2935 munge_user64_stat(&source
.sb
, &dest
.user64_sb
);
2936 my_size
= sizeof(dest
.user64_sb
);
2937 sbp
= (caddr_t
)&dest
.user64_sb
;
2939 munge_user32_stat(&source
.sb
, &dest
.user32_sb
);
2940 my_size
= sizeof(dest
.user32_sb
);
2941 sbp
= (caddr_t
)&dest
.user32_sb
;
2945 error
= copyout(sbp
, ub
, my_size
);
2948 /* caller wants extended security information? */
2949 if (xsecurity
!= USER_ADDR_NULL
) {
2951 /* did we get any? */
2952 if (fsec
== KAUTH_FILESEC_NONE
) {
2953 if (susize(xsecurity_size
, 0) != 0) {
2958 /* find the user buffer size */
2959 xsecurity_bufsize
= fusize(xsecurity_size
);
2961 /* copy out the actual data size */
2962 if (susize(xsecurity_size
, KAUTH_FILESEC_COPYSIZE(fsec
)) != 0) {
2967 /* if the caller supplied enough room, copy out to it */
2968 if (xsecurity_bufsize
>= KAUTH_FILESEC_COPYSIZE(fsec
))
2969 error
= copyout(fsec
, xsecurity
, KAUTH_FILESEC_COPYSIZE(fsec
));
2973 fp_drop(p
, fd
, fp
, 0);
2975 kauth_filesec_free(fsec
);
2983 * Description: Extended version of fstat supporting returning extended
2984 * security information
2986 * Parameters: p The process doing the fstat
2987 * uap->fd The fd to stat
2988 * uap->ub The user stat buffer
2989 * uap->xsecurity The user extended security
2990 * buffer, or 0 if none
2991 * uap->xsecurity_size The size of xsecurity, or 0
2993 * Returns: 0 Success
2994 * !0 Errno (see fstat1)
2997 fstat_extended(proc_t p
, struct fstat_extended_args
*uap
, __unused
int32_t *retval
)
2999 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 0));
3006 * Description: Get file status for the file associated with fd
3008 * Parameters: p The process doing the fstat
3009 * uap->fd The fd to stat
3010 * uap->ub The user stat buffer
3012 * Returns: 0 Success
3013 * !0 Errno (see fstat1)
3016 fstat(proc_t p
, register struct fstat_args
*uap
, __unused
int32_t *retval
)
3018 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 0));
3025 * Description: Extended version of fstat64 supporting returning extended
3026 * security information
3028 * Parameters: p The process doing the fstat
3029 * uap->fd The fd to stat
3030 * uap->ub The user stat buffer
3031 * uap->xsecurity The user extended security
3032 * buffer, or 0 if none
3033 * uap->xsecurity_size The size of xsecurity, or 0
3035 * Returns: 0 Success
3036 * !0 Errno (see fstat1)
3039 fstat64_extended(proc_t p
, struct fstat64_extended_args
*uap
, __unused
int32_t *retval
)
3041 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 1));
3048 * Description: Get 64 bit version of the file status for the file associated
3051 * Parameters: p The process doing the fstat
3052 * uap->fd The fd to stat
3053 * uap->ub The user stat buffer
3055 * Returns: 0 Success
3056 * !0 Errno (see fstat1)
3059 fstat64(proc_t p
, register struct fstat64_args
*uap
, __unused
int32_t *retval
)
3061 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 1));
3068 * Description: Return pathconf information about a file descriptor.
3070 * Parameters: p Process making the request
3071 * uap->fd fd to get information about
3072 * uap->name Name of information desired
3073 * retval Pointer to the call return area
3075 * Returns: 0 Success
3077 * fp_lookup:EBADF Bad file descriptor
3078 * vnode_getwithref:???
3082 * *retval (modified) Returned information (numeric)
3085 fpathconf(proc_t p
, struct fpathconf_args
*uap
, int32_t *retval
)
3088 struct fileproc
*fp
;
3095 AUDIT_ARG(fd
, uap
->fd
);
3096 if ( (error
= fp_lookup(p
, fd
, &fp
, 0)) )
3104 if (uap
->name
!= _PC_PIPE_BUF
) {
3113 if (uap
->name
!= _PC_PIPE_BUF
) {
3122 vp
= (struct vnode
*)data
;
3124 if ( (error
= vnode_getwithref(vp
)) == 0) {
3125 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
3127 error
= vn_pathconf(vp
, uap
->name
, retval
, vfs_context_current());
3129 (void)vnode_put(vp
);
3140 fp_drop(p
, fd
, fp
, 0);
3145 * Statistics counter for the number of times a process calling fdalloc()
3146 * has resulted in an expansion of the per process open file table.
3148 * XXX This would likely be of more use if it were per process
3156 * Description: Allocate a file descriptor for the process.
3158 * Parameters: p Process to allocate the fd in
3159 * want The fd we would prefer to get
3160 * result Pointer to fd we got
3162 * Returns: 0 Success
3167 * *result (modified) The fd which was allocated
3170 fdalloc(proc_t p
, int want
, int *result
)
3172 struct filedesc
*fdp
= p
->p_fd
;
3174 int lim
, last
, numfiles
, oldnfiles
;
3175 struct fileproc
**newofiles
, **ofiles
;
3176 char *newofileflags
;
3179 * Search for a free descriptor starting at the higher
3180 * of want or fd_freefile. If that fails, consider
3181 * expanding the ofile array.
3184 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
3187 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
3189 last
= min(fdp
->fd_nfiles
, lim
);
3190 if ((i
= want
) < fdp
->fd_freefile
)
3191 i
= fdp
->fd_freefile
;
3192 for (; i
< last
; i
++) {
3193 if (fdp
->fd_ofiles
[i
] == NULL
&& !(fdp
->fd_ofileflags
[i
] & UF_RESERVED
)) {
3194 procfdtbl_reservefd(p
, i
);
3195 if (i
> fdp
->fd_lastfile
)
3196 fdp
->fd_lastfile
= i
;
3197 if (want
<= fdp
->fd_freefile
)
3198 fdp
->fd_freefile
= i
;
3205 * No space in current array. Expand?
3207 if (fdp
->fd_nfiles
>= lim
)
3209 if (fdp
->fd_nfiles
< NDEXTENT
)
3210 numfiles
= NDEXTENT
;
3212 numfiles
= 2 * fdp
->fd_nfiles
;
3217 MALLOC_ZONE(newofiles
, struct fileproc
**,
3218 numfiles
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
3220 if (newofiles
== NULL
) {
3223 if (fdp
->fd_nfiles
>= numfiles
) {
3224 FREE_ZONE(newofiles
, numfiles
* OFILESIZE
, M_OFILETABL
);
3227 newofileflags
= (char *) &newofiles
[numfiles
];
3229 * Copy the existing ofile and ofileflags arrays
3230 * and zero the new portion of each array.
3232 oldnfiles
= fdp
->fd_nfiles
;
3233 (void) memcpy(newofiles
, fdp
->fd_ofiles
,
3234 oldnfiles
* sizeof(*fdp
->fd_ofiles
));
3235 (void) memset(&newofiles
[oldnfiles
], 0,
3236 (numfiles
- oldnfiles
) * sizeof(*fdp
->fd_ofiles
));
3238 (void) memcpy(newofileflags
, fdp
->fd_ofileflags
,
3239 oldnfiles
* sizeof(*fdp
->fd_ofileflags
));
3240 (void) memset(&newofileflags
[oldnfiles
], 0,
3241 (numfiles
- oldnfiles
) *
3242 sizeof(*fdp
->fd_ofileflags
));
3243 ofiles
= fdp
->fd_ofiles
;
3244 fdp
->fd_ofiles
= newofiles
;
3245 fdp
->fd_ofileflags
= newofileflags
;
3246 fdp
->fd_nfiles
= numfiles
;
3247 FREE_ZONE(ofiles
, oldnfiles
* OFILESIZE
, M_OFILETABL
);
3256 * Description: Check to see whether n user file descriptors are available
3259 * Parameters: p Process to check in
3260 * n The number of fd's desired
3265 * Locks: Assumes proc_fdlock for process is held by the caller
3267 * Notes: The answer only remains valid so long as the proc_fdlock is
3268 * held by the caller.
3271 fdavail(proc_t p
, int n
)
3273 struct filedesc
*fdp
= p
->p_fd
;
3274 struct fileproc
**fpp
;
3278 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
3279 if ((i
= lim
- fdp
->fd_nfiles
) > 0 && (n
-= i
) <= 0)
3281 fpp
= &fdp
->fd_ofiles
[fdp
->fd_freefile
];
3282 flags
= &fdp
->fd_ofileflags
[fdp
->fd_freefile
];
3283 for (i
= fdp
->fd_nfiles
- fdp
->fd_freefile
; --i
>= 0; fpp
++, flags
++)
3284 if (*fpp
== NULL
&& !(*flags
& UF_RESERVED
) && --n
<= 0)
3293 * Description: Legacy KPI wrapper function for _fdrelse
3295 * Parameters: p Process in which fd lives
3300 * Locks: Assumes proc_fdlock for process is held by the caller
3303 fdrelse(proc_t p
, int fd
)
3312 * Description: Get the fileproc pointer for the given fd from the per process
3313 * open file table without taking an explicit reference on it.
3315 * Parameters: p Process containing fd
3316 * fd fd to obtain fileproc for
3317 * resultfp Pointer to pointer return area
3319 * Returns: 0 Success
3323 * *resultfp (modified) Pointer to fileproc pointer
3325 * Locks: Assumes proc_fdlock for process is held by the caller
3327 * Notes: Because there is no reference explicitly taken, the returned
3328 * fileproc pointer is only valid so long as the proc_fdlock
3329 * remains held by the caller.
3332 fdgetf_noref(proc_t p
, int fd
, struct fileproc
**resultfp
)
3334 struct filedesc
*fdp
= p
->p_fd
;
3335 struct fileproc
*fp
;
3337 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3338 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3339 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3351 * Description: Get fileproc and vnode pointer for a given fd from the per
3352 * process open file table of the specified process, and if
3353 * successful, increment the f_iocount
3355 * Parameters: p Process in which fd lives
3356 * fd fd to get information for
3357 * resultfp Pointer to result fileproc
3358 * pointer area, or 0 if none
3359 * resultvp Pointer to result vnode pointer
3360 * area, or 0 if none
3362 * Returns: 0 Success
3363 * EBADF Bad file descriptor
3364 * ENOTSUP fd does not refer to a vnode
3367 * *resultfp (modified) Fileproc pointer
3368 * *resultvp (modified) vnode pointer
3370 * Notes: The resultfp and resultvp fields are optional, and may be
3371 * independently specified as NULL to skip returning information
3373 * Locks: Internally takes and releases proc_fdlock
3376 fp_getfvp(proc_t p
, int fd
, struct fileproc
**resultfp
, struct vnode
**resultvp
)
3378 struct filedesc
*fdp
= p
->p_fd
;
3379 struct fileproc
*fp
;
3381 proc_fdlock_spin(p
);
3382 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3383 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3384 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3388 if (fp
->f_type
!= DTYPE_VNODE
) {
3397 *resultvp
= (struct vnode
*)fp
->f_data
;
3407 * Description: Get fileproc, vnode pointer, and vid for a given fd from the
3408 * per process open file table of the specified process, and if
3409 * successful, increment the f_iocount
3411 * Parameters: p Process in which fd lives
3412 * fd fd to get information for
3413 * resultfp Pointer to result fileproc
3414 * pointer area, or 0 if none
3415 * resultvp Pointer to result vnode pointer
3416 * area, or 0 if none
3417 * vidp Pointer to resuld vid area
3419 * Returns: 0 Success
3420 * EBADF Bad file descriptor
3421 * ENOTSUP fd does not refer to a vnode
3424 * *resultfp (modified) Fileproc pointer
3425 * *resultvp (modified) vnode pointer
3428 * Notes: The resultfp and resultvp fields are optional, and may be
3429 * independently specified as NULL to skip returning information
3431 * Locks: Internally takes and releases proc_fdlock
3434 fp_getfvpandvid(proc_t p
, int fd
, struct fileproc
**resultfp
,
3435 struct vnode
**resultvp
, uint32_t *vidp
)
3437 struct filedesc
*fdp
= p
->p_fd
;
3438 struct fileproc
*fp
;
3440 proc_fdlock_spin(p
);
3441 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3442 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3443 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3447 if (fp
->f_type
!= DTYPE_VNODE
) {
3456 *resultvp
= (struct vnode
*)fp
->f_data
;
3458 *vidp
= (uint32_t)vnode_vid((struct vnode
*)fp
->f_data
);
3468 * Description: Get fileproc and socket pointer for a given fd from the
3469 * per process open file table of the specified process, and if
3470 * successful, increment the f_iocount
3472 * Parameters: p Process in which fd lives
3473 * fd fd to get information for
3474 * resultfp Pointer to result fileproc
3475 * pointer area, or 0 if none
3476 * results Pointer to result socket
3477 * pointer area, or 0 if none
3479 * Returns: EBADF The file descriptor is invalid
3480 * EOPNOTSUPP The file descriptor is not a socket
3484 * *resultfp (modified) Fileproc pointer
3485 * *results (modified) socket pointer
3487 * Notes: EOPNOTSUPP should probably be ENOTSOCK; this function is only
3488 * ever called from accept1().
3491 fp_getfsock(proc_t p
, int fd
, struct fileproc
**resultfp
,
3492 struct socket
**results
)
3494 struct filedesc
*fdp
= p
->p_fd
;
3495 struct fileproc
*fp
;
3497 proc_fdlock_spin(p
);
3498 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3499 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3500 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3504 if (fp
->f_type
!= DTYPE_SOCKET
) {
3513 *results
= (struct socket
*)fp
->f_data
;
3523 * Description: Get fileproc and kqueue pointer for a given fd from the
3524 * per process open file table of the specified process, and if
3525 * successful, increment the f_iocount
3527 * Parameters: p Process in which fd lives
3528 * fd fd to get information for
3529 * resultfp Pointer to result fileproc
3530 * pointer area, or 0 if none
3531 * resultkq Pointer to result kqueue
3532 * pointer area, or 0 if none
3534 * Returns: EBADF The file descriptor is invalid
3535 * EBADF The file descriptor is not a socket
3539 * *resultfp (modified) Fileproc pointer
3540 * *resultkq (modified) kqueue pointer
3542 * Notes: The second EBADF should probably be something else to make
3543 * the error condition distinct.
3546 fp_getfkq(proc_t p
, int fd
, struct fileproc
**resultfp
,
3547 struct kqueue
**resultkq
)
3549 struct filedesc
*fdp
= p
->p_fd
;
3550 struct fileproc
*fp
;
3552 proc_fdlock_spin(p
);
3553 if ( fd
< 0 || fd
>= fdp
->fd_nfiles
||
3554 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3555 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3559 if (fp
->f_type
!= DTYPE_KQUEUE
) {
3568 *resultkq
= (struct kqueue
*)fp
->f_data
;
3578 * Description: Get fileproc and POSIX shared memory pointer for a given fd
3579 * from the per process open file table of the specified process
3580 * and if successful, increment the f_iocount
3582 * Parameters: p Process in which fd lives
3583 * fd fd to get information for
3584 * resultfp Pointer to result fileproc
3585 * pointer area, or 0 if none
3586 * resultpshm Pointer to result POSIX
3587 * shared memory pointer
3588 * pointer area, or 0 if none
3590 * Returns: EBADF The file descriptor is invalid
3591 * EBADF The file descriptor is not a POSIX
3592 * shared memory area
3596 * *resultfp (modified) Fileproc pointer
3597 * *resultpshm (modified) POSIX shared memory pointer
3599 * Notes: The second EBADF should probably be something else to make
3600 * the error condition distinct.
3603 fp_getfpshm(proc_t p
, int fd
, struct fileproc
**resultfp
,
3604 struct pshmnode
**resultpshm
)
3606 struct filedesc
*fdp
= p
->p_fd
;
3607 struct fileproc
*fp
;
3609 proc_fdlock_spin(p
);
3610 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3611 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3612 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3616 if (fp
->f_type
!= DTYPE_PSXSHM
) {
3626 *resultpshm
= (struct pshmnode
*)fp
->f_data
;
3636 * Description: Get fileproc and POSIX semaphore pointer for a given fd from
3637 * the per process open file table of the specified process
3638 * and if successful, increment the f_iocount
3640 * Parameters: p Process in which fd lives
3641 * fd fd to get information for
3642 * resultfp Pointer to result fileproc
3643 * pointer area, or 0 if none
3644 * resultpsem Pointer to result POSIX
3645 * semaphore pointer area, or
3648 * Returns: EBADF The file descriptor is invalid
3649 * EBADF The file descriptor is not a POSIX
3654 * *resultfp (modified) Fileproc pointer
3655 * *resultpsem (modified) POSIX semaphore pointer
3657 * Notes: The second EBADF should probably be something else to make
3658 * the error condition distinct.
3660 * In order to support unnamed POSIX semaphores, the named
3661 * POSIX semaphores will have to move out of the per-process
3662 * open filetable, and into a global table that is shared with
3663 * unnamed POSIX semaphores, since unnamed POSIX semaphores
3664 * are typically used by declaring instances in shared memory,
3665 * and there's no other way to do this without changing the
3666 * underlying type, which would introduce binary compatibility
3670 fp_getfpsem(proc_t p
, int fd
, struct fileproc
**resultfp
,
3671 struct psemnode
**resultpsem
)
3673 struct filedesc
*fdp
= p
->p_fd
;
3674 struct fileproc
*fp
;
3676 proc_fdlock_spin(p
);
3677 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3678 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3679 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3683 if (fp
->f_type
!= DTYPE_PSXSEM
) {
3692 *resultpsem
= (struct psemnode
*)fp
->f_data
;
3702 * Description: Get fileproc and pipe pointer for a given fd from the
3703 * per process open file table of the specified process
3704 * and if successful, increment the f_iocount
3706 * Parameters: p Process in which fd lives
3707 * fd fd to get information for
3708 * resultfp Pointer to result fileproc
3709 * pointer area, or 0 if none
3710 * resultpipe Pointer to result pipe
3711 * pointer area, or 0 if none
3713 * Returns: EBADF The file descriptor is invalid
3714 * EBADF The file descriptor is not a socket
3718 * *resultfp (modified) Fileproc pointer
3719 * *resultpipe (modified) pipe pointer
3721 * Notes: The second EBADF should probably be something else to make
3722 * the error condition distinct.
3725 fp_getfpipe(proc_t p
, int fd
, struct fileproc
**resultfp
,
3726 struct pipe
**resultpipe
)
3728 struct filedesc
*fdp
= p
->p_fd
;
3729 struct fileproc
*fp
;
3731 proc_fdlock_spin(p
);
3732 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3733 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3734 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3738 if (fp
->f_type
!= DTYPE_PIPE
) {
3747 *resultpipe
= (struct pipe
*)fp
->f_data
;
3756 * Description: Get fileproc pointer for a given fd from the per process
3757 * open file table of the specified process and if successful,
3758 * increment the f_iocount
3760 * Parameters: p Process in which fd lives
3761 * fd fd to get information for
3762 * resultfp Pointer to result fileproc
3763 * pointer area, or 0 if none
3764 * locked !0 if the caller holds the
3765 * proc_fdlock, 0 otherwise
3767 * Returns: 0 Success
3768 * EBADF Bad file descriptor
3771 * *resultfp (modified) Fileproc pointer
3773 * Locks: If the argument 'locked' is non-zero, then the caller is
3774 * expected to have taken and held the proc_fdlock; if it is
3775 * zero, than this routine internally takes and drops this lock.
3778 fp_lookup(proc_t p
, int fd
, struct fileproc
**resultfp
, int locked
)
3780 struct filedesc
*fdp
= p
->p_fd
;
3781 struct fileproc
*fp
;
3784 proc_fdlock_spin(p
);
3785 if (fd
< 0 || fdp
== NULL
|| fd
>= fdp
->fd_nfiles
||
3786 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3787 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3806 * Description: Swap the fileproc pointer for a given fd with a new
3807 * fileproc pointer in the per-process open file table of
3808 * the specified process. The fdlock must be held at entry.
3810 * Parameters: p Process containing the fd
3811 * fd The fd of interest
3812 * nfp Pointer to the newfp
3814 * Returns: 0 Success
3815 * EBADF Bad file descriptor
3817 * EKEEPLOOKING f_iocount changed while lock was dropped.
3820 fp_tryswap(proc_t p
, int fd
, struct fileproc
*nfp
)
3822 struct fileproc
*fp
;
3825 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
3827 if (0 != (error
= fp_lookup(p
, fd
, &fp
, 1)))
3830 * At this point, our caller (change_guardedfd_np) has
3831 * one f_iocount reference, and we just took another
3832 * one to begin the replacement.
3834 if (fp
->f_iocount
< 2) {
3835 panic("f_iocount too small %d", fp
->f_iocount
);
3836 } else if (2 == fp
->f_iocount
) {
3838 /* Copy the contents of *fp, preserving the "type" of *nfp */
3840 nfp
->f_flags
= (nfp
->f_flags
& FP_TYPEMASK
) |
3841 (fp
->f_flags
& ~FP_TYPEMASK
);
3842 nfp
->f_iocount
= fp
->f_iocount
;
3843 nfp
->f_fglob
= fp
->f_fglob
;
3844 nfp
->f_waddr
= fp
->f_waddr
;
3846 p
->p_fd
->fd_ofiles
[fd
] = nfp
;
3847 (void) fp_drop(p
, fd
, nfp
, 1);
3850 * Wait for all other active references to evaporate.
3852 p
->p_fpdrainwait
= 1;
3853 error
= msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
,
3854 PRIBIO
| PCATCH
, "tryswap fpdrain", NULL
);
3857 * Return an "internal" errno to trigger a full
3858 * reevaluation of the change-guard attempt.
3860 error
= EKEEPLOOKING
;
3861 printf("%s: lookup collision fd %d\n", __func__
, fd
);
3863 (void) fp_drop(p
, fd
, fp
, 1);
3872 * Description: Set the FP_WRITTEN flag on the fileproc and drop the I/O
3873 * reference previously taken by calling fp_lookup et. al.
3875 * Parameters: p Process in which the fd lives
3876 * fd fd associated with the fileproc
3877 * fp fileproc on which to set the
3878 * flag and drop the reference
3880 * Returns: 0 Success
3881 * fp_drop:EBADF Bad file descriptor
3883 * Locks: This function internally takes and drops the proc_fdlock for
3884 * the supplied process
3886 * Notes: The fileproc must correspond to the fd in the supplied proc
3889 fp_drop_written(proc_t p
, int fd
, struct fileproc
*fp
)
3893 proc_fdlock_spin(p
);
3895 fp
->f_flags
|= FP_WRITTEN
;
3897 error
= fp_drop(p
, fd
, fp
, 1);
3908 * Description: Set the FP_WAITEVENT flag on the fileproc and drop the I/O
3909 * reference previously taken by calling fp_lookup et. al.
3911 * Parameters: p Process in which the fd lives
3912 * fd fd associated with the fileproc
3913 * fp fileproc on which to set the
3914 * flag and drop the reference
3916 * Returns: 0 Success
3917 * fp_drop:EBADF Bad file descriptor
3919 * Locks: This function internally takes and drops the proc_fdlock for
3920 * the supplied process
3922 * Notes: The fileproc must correspond to the fd in the supplied proc
3925 fp_drop_event(proc_t p
, int fd
, struct fileproc
*fp
)
3929 proc_fdlock_spin(p
);
3931 fp
->f_flags
|= FP_WAITEVENT
;
3933 error
= fp_drop(p
, fd
, fp
, 1);
3944 * Description: Drop the I/O reference previously taken by calling fp_lookup
3947 * Parameters: p Process in which the fd lives
3948 * fd fd associated with the fileproc
3949 * fp fileproc on which to set the
3950 * flag and drop the reference
3951 * locked flag to internally take and
3952 * drop proc_fdlock if it is not
3953 * already held by the caller
3955 * Returns: 0 Success
3956 * EBADF Bad file descriptor
3958 * Locks: This function internally takes and drops the proc_fdlock for
3959 * the supplied process if 'locked' is non-zero, and assumes that
3960 * the caller already holds this lock if 'locked' is non-zero.
3962 * Notes: The fileproc must correspond to the fd in the supplied proc
3965 fp_drop(proc_t p
, int fd
, struct fileproc
*fp
, int locked
)
3967 struct filedesc
*fdp
= p
->p_fd
;
3971 proc_fdlock_spin(p
);
3972 if ((fp
== FILEPROC_NULL
) && (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3973 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3974 ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
3975 !(fdp
->fd_ofileflags
[fd
] & UF_CLOSING
)))) {
3982 if (fp
->f_iocount
== 0) {
3983 if (fp
->f_flags
& FP_SELCONFLICT
)
3984 fp
->f_flags
&= ~FP_SELCONFLICT
;
3986 if (p
->p_fpdrainwait
) {
3987 p
->p_fpdrainwait
= 0;
3994 wakeup(&p
->p_fpdrainwait
);
4003 * Description: Given an fd, look it up in the current process's per process
4004 * open file table, and return its internal vnode pointer.
4006 * Parameters: fd fd to obtain vnode from
4007 * vpp pointer to vnode return area
4009 * Returns: 0 Success
4010 * EINVAL The fd does not refer to a
4011 * vnode fileproc entry
4012 * fp_lookup:EBADF Bad file descriptor
4015 * *vpp (modified) Returned vnode pointer
4017 * Locks: This function internally takes and drops the proc_fdlock for
4018 * the current process
4020 * Notes: If successful, this function increments the f_iocount on the
4021 * fd's corresponding fileproc.
4023 * The fileproc referenced is not returned; because of this, care
4024 * must be taken to not drop the last reference (e.g. by closing
4025 * the file). This is inherently unsafe, since the reference may
4026 * not be recoverable from the vnode, if there is a subsequent
4027 * close that destroys the associate fileproc. The caller should
4028 * therefore retain their own reference on the fileproc so that
4029 * the f_iocount can be dropped subsequently. Failure to do this
4030 * can result in the returned pointer immediately becoming invalid
4031 * following the call.
4033 * Use of this function is discouraged.
4036 file_vnode(int fd
, struct vnode
**vpp
)
4038 proc_t p
= current_proc();
4039 struct fileproc
*fp
;
4042 proc_fdlock_spin(p
);
4043 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4047 if (fp
->f_type
!= DTYPE_VNODE
) {
4048 fp_drop(p
, fd
, fp
,1);
4053 *vpp
= (struct vnode
*)fp
->f_data
;
4061 * file_vnode_withvid
4063 * Description: Given an fd, look it up in the current process's per process
4064 * open file table, and return its internal vnode pointer.
4066 * Parameters: fd fd to obtain vnode from
4067 * vpp pointer to vnode return area
4068 * vidp pointer to vid of the returned vnode
4070 * Returns: 0 Success
4071 * EINVAL The fd does not refer to a
4072 * vnode fileproc entry
4073 * fp_lookup:EBADF Bad file descriptor
4076 * *vpp (modified) Returned vnode pointer
4078 * Locks: This function internally takes and drops the proc_fdlock for
4079 * the current process
4081 * Notes: If successful, this function increments the f_iocount on the
4082 * fd's corresponding fileproc.
4084 * The fileproc referenced is not returned; because of this, care
4085 * must be taken to not drop the last reference (e.g. by closing
4086 * the file). This is inherently unsafe, since the reference may
4087 * not be recoverable from the vnode, if there is a subsequent
4088 * close that destroys the associate fileproc. The caller should
4089 * therefore retain their own reference on the fileproc so that
4090 * the f_iocount can be dropped subsequently. Failure to do this
4091 * can result in the returned pointer immediately becoming invalid
4092 * following the call.
4094 * Use of this function is discouraged.
4097 file_vnode_withvid(int fd
, struct vnode
**vpp
, uint32_t * vidp
)
4099 proc_t p
= current_proc();
4100 struct fileproc
*fp
;
4104 proc_fdlock_spin(p
);
4105 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4109 if (fp
->f_type
!= DTYPE_VNODE
) {
4110 fp_drop(p
, fd
, fp
,1);
4114 vp
= (struct vnode
*)fp
->f_data
;
4118 if ((vidp
!= NULL
) && (vp
!= NULLVP
))
4119 *vidp
= (uint32_t)vp
->v_id
;
4130 * Description: Given an fd, look it up in the current process's per process
4131 * open file table, and return its internal socket pointer.
4133 * Parameters: fd fd to obtain vnode from
4134 * sp pointer to socket return area
4136 * Returns: 0 Success
4137 * ENOTSOCK Not a socket
4138 * fp_lookup:EBADF Bad file descriptor
4141 * *sp (modified) Returned socket pointer
4143 * Locks: This function internally takes and drops the proc_fdlock for
4144 * the current process
4146 * Notes: If successful, this function increments the f_iocount on the
4147 * fd's corresponding fileproc.
4149 * The fileproc referenced is not returned; because of this, care
4150 * must be taken to not drop the last reference (e.g. by closing
4151 * the file). This is inherently unsafe, since the reference may
4152 * not be recoverable from the socket, if there is a subsequent
4153 * close that destroys the associate fileproc. The caller should
4154 * therefore retain their own reference on the fileproc so that
4155 * the f_iocount can be dropped subsequently. Failure to do this
4156 * can result in the returned pointer immediately becoming invalid
4157 * following the call.
4159 * Use of this function is discouraged.
4162 file_socket(int fd
, struct socket
**sp
)
4164 proc_t p
= current_proc();
4165 struct fileproc
*fp
;
4168 proc_fdlock_spin(p
);
4169 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4173 if (fp
->f_type
!= DTYPE_SOCKET
) {
4174 fp_drop(p
, fd
, fp
,1);
4178 *sp
= (struct socket
*)fp
->f_data
;
4188 * Description: Given an fd, look it up in the current process's per process
4189 * open file table, and return its fileproc's flags field.
4191 * Parameters: fd fd whose flags are to be
4193 * flags pointer to flags data area
4195 * Returns: 0 Success
4196 * ENOTSOCK Not a socket
4197 * fp_lookup:EBADF Bad file descriptor
4200 * *flags (modified) Returned flags field
4202 * Locks: This function internally takes and drops the proc_fdlock for
4203 * the current process
4205 * Notes: This function will internally increment and decrement the
4206 * f_iocount of the fileproc as part of its operation.
4209 file_flags(int fd
, int *flags
)
4212 proc_t p
= current_proc();
4213 struct fileproc
*fp
;
4216 proc_fdlock_spin(p
);
4217 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4221 *flags
= (int)fp
->f_flag
;
4222 fp_drop(p
, fd
, fp
,1);
4232 * Description: Drop an iocount reference on an fd, and wake up any waiters
4233 * for draining (i.e. blocked in fileproc_drain() called during
4234 * the last attempt to close a file).
4236 * Parameters: fd fd on which an ioreference is
4239 * Returns: 0 Success
4240 * EBADF Bad file descriptor
4242 * Description: Given an fd, look it up in the current process's per process
4243 * open file table, and drop it's fileproc's f_iocount by one
4245 * Notes: This is intended as a corresponding operation to the functions
4246 * file_vnode() and file_socket() operations.
4248 * Technically, the close reference is supposed to be protected
4249 * by a fileproc_drain(), however, a drain will only block if
4250 * the fd refers to a character device, and that device has had
4251 * preparefileread() called on it. If it refers to something
4252 * other than a character device, then the drain will occur and
4253 * block each close attempt, rather than merely the last close.
4255 * Since it's possible for an fd that refers to a character
4256 * device to have an intermediate close followed by an open to
4257 * cause a different file to correspond to that descriptor,
4258 * unless there was a cautionary reference taken on the fileproc,
4259 * this is an inherently unsafe function. This happens in the
4260 * case where multiple fd's in a process refer to the same
4261 * character device (e.g. stdin/out/err pointing to a tty, etc.).
4263 * Use of this function is discouraged.
4268 struct fileproc
*fp
;
4269 proc_t p
= current_proc();
4272 proc_fdlock_spin(p
);
4273 if (fd
< 0 || fd
>= p
->p_fd
->fd_nfiles
||
4274 (fp
= p
->p_fd
->fd_ofiles
[fd
]) == NULL
||
4275 ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
4276 !(p
->p_fd
->fd_ofileflags
[fd
] & UF_CLOSING
))) {
4282 if (fp
->f_iocount
== 0) {
4283 if (fp
->f_flags
& FP_SELCONFLICT
)
4284 fp
->f_flags
&= ~FP_SELCONFLICT
;
4286 if (p
->p_fpdrainwait
) {
4287 p
->p_fpdrainwait
= 0;
4294 wakeup(&p
->p_fpdrainwait
);
4299 static int falloc_withalloc_locked(proc_t
, struct fileproc
**, int *,
4300 vfs_context_t
, struct fileproc
* (*)(void *), void *, int);
4305 * Description: Allocate an entry in the per process open file table and
4306 * return the corresponding fileproc and fd.
4308 * Parameters: p The process in whose open file
4309 * table the fd is to be allocated
4310 * resultfp Pointer to fileproc pointer
4312 * resultfd Pointer to fd return area
4315 * Returns: 0 Success
4316 * falloc:ENFILE Too many open files in system
4317 * falloc:EMFILE Too many open files in process
4318 * falloc:ENOMEM M_FILEPROC or M_FILEGLOB zone
4322 * *resultfd (modified) Returned fileproc pointer
4323 * *resultfd (modified) Returned fd
4325 * Locks: This function takes and drops the proc_fdlock; if this lock
4326 * is already held, use falloc_locked() instead.
4328 * Notes: This function takes separate process and context arguments
4329 * solely to support kern_exec.c; otherwise, it would take
4330 * neither, and expect falloc_locked() to use the
4331 * vfs_context_current() routine internally.
4334 falloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
)
4336 return (falloc_withalloc(p
, resultfp
, resultfd
, ctx
,
4337 fileproc_alloc_init
, NULL
));
4341 * Like falloc, but including the fileproc allocator and create-args
4344 falloc_withalloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4345 vfs_context_t ctx
, fp_allocfn_t fp_zalloc
, void *arg
)
4350 error
= falloc_withalloc_locked(p
,
4351 resultfp
, resultfd
, ctx
, fp_zalloc
, arg
, 1);
4358 * "uninitialized" ops -- ensure fg->fg_ops->fo_type always exists
4360 static const struct fileops uninitops
;
4365 * Create a new open file structure and allocate
4366 * a file descriptor for the process that refers to it.
4368 * Returns: 0 Success
4370 * Description: Allocate an entry in the per process open file table and
4371 * return the corresponding fileproc and fd.
4373 * Parameters: p The process in whose open file
4374 * table the fd is to be allocated
4375 * resultfp Pointer to fileproc pointer
4377 * resultfd Pointer to fd return area
4379 * locked Flag to indicate whether the
4380 * caller holds proc_fdlock
4382 * Returns: 0 Success
4383 * ENFILE Too many open files in system
4384 * fdalloc:EMFILE Too many open files in process
4385 * ENOMEM M_FILEPROC or M_FILEGLOB zone
4390 * *resultfd (modified) Returned fileproc pointer
4391 * *resultfd (modified) Returned fd
4393 * Locks: If the parameter 'locked' is zero, this function takes and
4394 * drops the proc_fdlock; if non-zero, the caller must hold the
4397 * Notes: If you intend to use a non-zero 'locked' parameter, use the
4398 * utility function falloc() instead.
4400 * This function takes separate process and context arguments
4401 * solely to support kern_exec.c; otherwise, it would take
4402 * neither, and use the vfs_context_current() routine internally.
4405 falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4406 vfs_context_t ctx
, int locked
)
4408 return (falloc_withalloc_locked(p
, resultfp
, resultfd
, ctx
,
4409 fileproc_alloc_init
, NULL
, locked
));
4413 falloc_withalloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4414 vfs_context_t ctx
, fp_allocfn_t fp_zalloc
, void *crarg
,
4417 struct fileproc
*fp
;
4418 struct fileglob
*fg
;
4423 if ( (error
= fdalloc(p
, 0, &nfd
)) ) {
4428 if (nfiles
>= maxfiles
) {
4435 error
= mac_file_check_create(proc_ucred(p
));
4444 * Allocate a new file descriptor.
4445 * If the process has file descriptor zero open, add to the list
4446 * of open files at that point, otherwise put it at the front of
4447 * the list of open files.
4451 fp
= (*fp_zalloc
)(crarg
);
4457 MALLOC_ZONE(fg
, struct fileglob
*, sizeof(struct fileglob
), M_FILEGLOB
, M_WAITOK
);
4464 bzero(fg
, sizeof(struct fileglob
));
4465 lck_mtx_init(&fg
->fg_lock
, file_lck_grp
, file_lck_attr
);
4469 fg
->fg_ops
= &uninitops
;
4472 mac_file_label_init(fg
);
4475 kauth_cred_ref(ctx
->vc_ucred
);
4479 fp
->f_cred
= ctx
->vc_ucred
;
4482 mac_file_label_associate(fp
->f_cred
, fg
);
4485 OSAddAtomic(1, &nfiles
);
4487 p
->p_fd
->fd_ofiles
[nfd
] = fp
;
4504 * Description: Free a file structure; drop the global open file count, and
4505 * drop the credential reference, if the fileglob has one, and
4506 * destroy the instance mutex before freeing
4508 * Parameters: fg Pointer to fileglob to be
4514 fg_free(struct fileglob
*fg
)
4516 OSAddAtomic(-1, &nfiles
);
4518 if (fg
->fg_vn_data
) {
4519 fg_vn_data_free(fg
->fg_vn_data
);
4520 fg
->fg_vn_data
= NULL
;
4523 if (IS_VALID_CRED(fg
->fg_cred
)) {
4524 kauth_cred_unref(&fg
->fg_cred
);
4526 lck_mtx_destroy(&fg
->fg_lock
, file_lck_grp
);
4529 mac_file_label_destroy(fg
);
4531 FREE_ZONE(fg
, sizeof *fg
, M_FILEGLOB
);
4538 * Description: Perform close-on-exec processing for all files in a process
4539 * that are either marked as close-on-exec, or which were in the
4540 * process of being opened at the time of the execve
4542 * Also handles the case (via posix_spawn()) where -all-
4543 * files except those marked with "inherit" as treated as
4546 * Parameters: p Pointer to process calling
4551 * Locks: This function internally takes and drops proc_fdlock()
4555 fdexec(proc_t p
, short flags
)
4557 struct filedesc
*fdp
= p
->p_fd
;
4559 boolean_t cloexec_default
= (flags
& POSIX_SPAWN_CLOEXEC_DEFAULT
) != 0;
4562 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
4564 struct fileproc
*fp
= fdp
->fd_ofiles
[i
];
4565 char *flagp
= &fdp
->fd_ofileflags
[i
];
4567 if (fp
&& cloexec_default
) {
4569 * Reverse the usual semantics of file descriptor
4570 * inheritance - all of them should be closed
4571 * except files marked explicitly as "inherit" and
4572 * not marked close-on-exec.
4574 if ((*flagp
& (UF_EXCLOSE
|UF_INHERIT
)) != UF_INHERIT
)
4575 *flagp
|= UF_EXCLOSE
;
4576 *flagp
&= ~UF_INHERIT
;
4580 ((*flagp
& (UF_RESERVED
|UF_EXCLOSE
)) == UF_EXCLOSE
)
4582 || (fp
&& mac_file_check_inherit(proc_ucred(p
), fp
->f_fglob
))
4585 if (i
< fdp
->fd_knlistsize
)
4586 knote_fdclose(p
, i
);
4587 procfdtbl_clearfd(p
, i
);
4588 if (i
== fdp
->fd_lastfile
&& i
> 0)
4590 if (i
< fdp
->fd_freefile
)
4591 fdp
->fd_freefile
= i
;
4594 * Wait for any third party viewers (e.g., lsof)
4595 * to release their references to this fileproc.
4597 while (fp
->f_iocount
> 0) {
4598 p
->p_fpdrainwait
= 1;
4599 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
,
4603 closef_locked(fp
, fp
->f_fglob
, p
);
4615 * Description: Copy a filedesc structure. This is normally used as part of
4616 * forkproc() when forking a new process, to copy the per process
4617 * open file table over to the new process.
4619 * Parameters: p Process whose open file table
4620 * is to be copied (parent)
4621 * uth_cdir Per thread current working
4622 * cirectory, or NULL
4624 * Returns: NULL Copy failed
4625 * !NULL Pointer to new struct filedesc
4627 * Locks: This function internally takes and drops proc_fdlock()
4629 * Notes: Files are copied directly, ignoring the new resource limits
4630 * for the process that's being copied into. Since the descriptor
4631 * references are just additional references, this does not count
4632 * against the number of open files on the system.
4634 * The struct filedesc includes the current working directory,
4635 * and the current root directory, if the process is chroot'ed.
4637 * If the exec was called by a thread using a per thread current
4638 * working directory, we inherit the working directory from the
4639 * thread making the call, rather than from the process.
4641 * In the case of a failure to obtain a reference, for most cases,
4642 * the file entry will be silently dropped. There's an exception
4643 * for the case of a chroot dir, since a failure to to obtain a
4644 * reference there would constitute an "escape" from the chroot
4645 * environment, which must not be allowed. In that case, we will
4646 * deny the execve() operation, rather than allowing the escape.
4649 fdcopy(proc_t p
, vnode_t uth_cdir
)
4651 struct filedesc
*newfdp
, *fdp
= p
->p_fd
;
4653 struct fileproc
*ofp
, *fp
;
4656 MALLOC_ZONE(newfdp
, struct filedesc
*,
4657 sizeof(*newfdp
), M_FILEDESC
, M_WAITOK
);
4664 * the FD_CHROOT flag will be inherited via this copy
4666 (void) memcpy(newfdp
, fdp
, sizeof(*newfdp
));
4669 * If we are running with per-thread current working directories,
4670 * inherit the new current working directory from the current thread
4671 * instead, before we take our references.
4673 if (uth_cdir
!= NULLVP
)
4674 newfdp
->fd_cdir
= uth_cdir
;
4677 * For both fd_cdir and fd_rdir make sure we get
4678 * a valid reference... if we can't, than set
4679 * set the pointer(s) to NULL in the child... this
4680 * will keep us from using a non-referenced vp
4681 * and allows us to do the vnode_rele only on
4682 * a properly referenced vp
4684 if ( (v_dir
= newfdp
->fd_cdir
) ) {
4685 if (vnode_getwithref(v_dir
) == 0) {
4686 if ( (vnode_ref(v_dir
)) )
4687 newfdp
->fd_cdir
= NULL
;
4690 newfdp
->fd_cdir
= NULL
;
4692 if (newfdp
->fd_cdir
== NULL
&& fdp
->fd_cdir
) {
4694 * we couldn't get a new reference on
4695 * the current working directory being
4696 * inherited... we might as well drop
4697 * our reference from the parent also
4698 * since the vnode has gone DEAD making
4699 * it useless... by dropping it we'll
4700 * be that much closer to recycling it
4702 vnode_rele(fdp
->fd_cdir
);
4703 fdp
->fd_cdir
= NULL
;
4706 if ( (v_dir
= newfdp
->fd_rdir
) ) {
4707 if (vnode_getwithref(v_dir
) == 0) {
4708 if ( (vnode_ref(v_dir
)) )
4709 newfdp
->fd_rdir
= NULL
;
4712 newfdp
->fd_rdir
= NULL
;
4715 /* Coming from a chroot environment and unable to get a reference... */
4716 if (newfdp
->fd_rdir
== NULL
&& fdp
->fd_rdir
) {
4718 * We couldn't get a new reference on
4719 * the chroot directory being
4720 * inherited... this is fatal, since
4721 * otherwise it would constitute an
4722 * escape from a chroot environment by
4725 if (newfdp
->fd_cdir
)
4726 vnode_rele(newfdp
->fd_cdir
);
4727 FREE_ZONE(newfdp
, sizeof *newfdp
, M_FILEDESC
);
4732 * If the number of open files fits in the internal arrays
4733 * of the open file structure, use them, otherwise allocate
4734 * additional memory for the number of descriptors currently
4737 if (newfdp
->fd_lastfile
< NDFILE
)
4741 * Compute the smallest multiple of NDEXTENT needed
4742 * for the file descriptors currently in use,
4743 * allowing the table to shrink.
4745 i
= newfdp
->fd_nfiles
;
4746 while (i
> 2 * NDEXTENT
&& i
> newfdp
->fd_lastfile
* 2)
4751 MALLOC_ZONE(newfdp
->fd_ofiles
, struct fileproc
**,
4752 i
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
4753 if (newfdp
->fd_ofiles
== NULL
) {
4754 if (newfdp
->fd_cdir
)
4755 vnode_rele(newfdp
->fd_cdir
);
4756 if (newfdp
->fd_rdir
)
4757 vnode_rele(newfdp
->fd_rdir
);
4759 FREE_ZONE(newfdp
, sizeof(*newfdp
), M_FILEDESC
);
4762 (void) memset(newfdp
->fd_ofiles
, 0, i
* OFILESIZE
);
4765 newfdp
->fd_ofileflags
= (char *) &newfdp
->fd_ofiles
[i
];
4766 newfdp
->fd_nfiles
= i
;
4768 if (fdp
->fd_nfiles
> 0) {
4769 struct fileproc
**fpp
;
4772 (void) memcpy(newfdp
->fd_ofiles
, fdp
->fd_ofiles
,
4773 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofiles
));
4774 (void) memcpy(newfdp
->fd_ofileflags
, fdp
->fd_ofileflags
,
4775 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofileflags
));
4778 * kq descriptors cannot be copied.
4780 if (newfdp
->fd_knlistsize
!= -1) {
4781 fpp
= &newfdp
->fd_ofiles
[newfdp
->fd_lastfile
];
4782 flags
= &newfdp
->fd_ofileflags
[newfdp
->fd_lastfile
];
4783 for (i
= newfdp
->fd_lastfile
;
4784 i
>= 0; i
--, fpp
--, flags
--) {
4785 if (*flags
& UF_RESERVED
)
4786 continue; /* (removed below) */
4787 if (*fpp
!= NULL
&& (*fpp
)->f_type
== DTYPE_KQUEUE
) {
4790 if (i
< newfdp
->fd_freefile
)
4791 newfdp
->fd_freefile
= i
;
4793 if (*fpp
== NULL
&& i
== newfdp
->fd_lastfile
&& i
> 0)
4794 newfdp
->fd_lastfile
--;
4796 newfdp
->fd_knlist
= NULL
;
4797 newfdp
->fd_knlistsize
= -1;
4798 newfdp
->fd_knhash
= NULL
;
4799 newfdp
->fd_knhashmask
= 0;
4801 fpp
= newfdp
->fd_ofiles
;
4802 flags
= newfdp
->fd_ofileflags
;
4804 for (i
= newfdp
->fd_lastfile
+ 1; --i
>= 0; fpp
++, flags
++)
4805 if ((ofp
= *fpp
) != NULL
&&
4806 0 == (*flags
& (UF_FORKCLOSE
|UF_RESERVED
))) {
4808 if (FILEPROC_TYPE(ofp
) != FTYPE_SIMPLE
)
4809 panic("complex fileproc");
4811 fp
= fileproc_alloc_init(NULL
);
4814 * XXX no room to copy, unable to
4815 * XXX safely unwind state at present
4820 (ofp
->f_flags
& ~FP_TYPEMASK
);
4821 fp
->f_fglob
= ofp
->f_fglob
;
4826 if (i
< newfdp
->fd_freefile
)
4827 newfdp
->fd_freefile
= i
;
4841 * Description: Release a filedesc (per process open file table) structure;
4842 * this is done on process exit(), or from forkproc_free() if
4843 * the fork fails for some reason subsequent to a successful
4846 * Parameters: p Pointer to process going away
4850 * Locks: This function internally takes and drops proc_fdlock()
4855 struct filedesc
*fdp
;
4856 struct fileproc
*fp
;
4861 if (p
== kernproc
|| NULL
== (fdp
= p
->p_fd
)) {
4866 extern struct filedesc filedesc0
;
4868 if (&filedesc0
== fdp
)
4871 if (fdp
->fd_nfiles
> 0 && fdp
->fd_ofiles
) {
4872 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
4873 if ((fp
= fdp
->fd_ofiles
[i
]) != NULL
) {
4875 if (fdp
->fd_ofileflags
[i
] & UF_RESERVED
)
4876 panic("fdfree: found fp with UF_RESERVED");
4878 procfdtbl_reservefd(p
, i
);
4880 if (i
< fdp
->fd_knlistsize
)
4881 knote_fdclose(p
, i
);
4882 if (fp
->f_flags
& FP_WAITEVENT
)
4883 (void)waitevent_close(p
, fp
);
4884 (void) closef_locked(fp
, fp
->f_fglob
, p
);
4888 FREE_ZONE(fdp
->fd_ofiles
, fdp
->fd_nfiles
* OFILESIZE
, M_OFILETABL
);
4889 fdp
->fd_ofiles
= NULL
;
4896 vnode_rele(fdp
->fd_cdir
);
4898 vnode_rele(fdp
->fd_rdir
);
4900 proc_fdlock_spin(p
);
4905 FREE(fdp
->fd_knlist
, M_KQUEUE
);
4907 FREE(fdp
->fd_knhash
, M_KQUEUE
);
4909 FREE_ZONE(fdp
, sizeof(*fdp
), M_FILEDESC
);
4915 * Description: Internal form of closef; called with proc_fdlock held
4917 * Parameters: fp Pointer to fileproc for fd
4918 * fg Pointer to fileglob for fd
4919 * p Pointer to proc structure
4921 * Returns: 0 Success
4922 * closef_finish:??? Anything returnable by a per-fileops
4925 * Note: Decrements reference count on file structure; if this was the
4926 * last reference, then closef_finish() is called
4928 * p and fp are allowed to be NULL when closing a file that was
4929 * being passed in a message (but only if we are called when this
4930 * is NOT the last reference).
4933 closef_locked(struct fileproc
*fp
, struct fileglob
*fg
, proc_t p
)
4937 struct vfs_context context
;
4944 /* Set up context with cred stashed in fg */
4945 if (p
== current_proc())
4946 context
.vc_thread
= current_thread();
4948 context
.vc_thread
= NULL
;
4949 context
.vc_ucred
= fg
->fg_cred
;
4952 * POSIX record locking dictates that any close releases ALL
4953 * locks owned by this process. This is handled by setting
4954 * a flag in the unlock to free ONLY locks obeying POSIX
4955 * semantics, and not to free BSD-style file locks.
4956 * If the descriptor was in a message, POSIX-style locks
4957 * aren't passed with the descriptor.
4959 if (p
&& (p
->p_ladvflag
& P_LADVLOCK
) &&
4960 DTYPE_VNODE
== FILEGLOB_DTYPE(fg
)) {
4963 lf
.l_whence
= SEEK_SET
;
4966 lf
.l_type
= F_UNLCK
;
4967 vp
= (struct vnode
*)fg
->fg_data
;
4969 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
4970 (void) VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &lf
, F_POSIX
, &context
, NULL
);
4971 (void)vnode_put(vp
);
4975 lck_mtx_lock_spin(&fg
->fg_lock
);
4978 if (fg
->fg_count
> 0) {
4979 lck_mtx_unlock(&fg
->fg_lock
);
4983 if (fg
->fg_count
!= 0)
4984 panic("fg %p: being freed with bad fg_count (%d)", fg
, fg
->fg_count
);
4987 if (fp
&& (fp
->f_flags
& FP_WRITTEN
))
4988 fg
->fg_flag
|= FWASWRITTEN
;
4990 fg
->fg_lflags
|= FG_TERM
;
4991 lck_mtx_unlock(&fg
->fg_lock
);
4996 /* Since we ensure that fg->fg_ops is always initialized,
4997 * it is safe to invoke fo_close on the fg */
4998 error
= fo_close(fg
, &context
);
5012 * Description: Drain out pending I/O operations
5014 * Parameters: p Process closing this file
5015 * fp fileproc struct for the open
5016 * instance on the file
5020 * Locks: Assumes the caller holds the proc_fdlock
5022 * Notes: For character devices, this occurs on the last close of the
5023 * device; for all other file descriptors, this occurs on each
5024 * close to prevent fd's from being closed out from under
5025 * operations currently in progress and blocked
5027 * See Also: file_vnode(), file_socket(), file_drop(), and the cautions
5028 * regarding their use and interaction with this function.
5031 fileproc_drain(proc_t p
, struct fileproc
* fp
)
5033 struct vfs_context context
;
5035 context
.vc_thread
= proc_thread(p
); /* XXX */
5036 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
5038 fp
->f_iocount
-- ; /* (the one the close holds) */
5040 while (fp
->f_iocount
) {
5042 lck_mtx_convert_spin(&p
->p_fdmlock
);
5044 if (fp
->f_fglob
->fg_ops
->fo_drain
) {
5045 (*fp
->f_fglob
->fg_ops
->fo_drain
)(fp
, &context
);
5047 if ((fp
->f_flags
& FP_INSELECT
) == FP_INSELECT
) {
5048 if (wait_queue_wakeup_all((wait_queue_t
)fp
->f_waddr
, NULL
, THREAD_INTERRUPTED
) == KERN_INVALID_ARGUMENT
)
5049 panic("bad wait queue for wait_queue_wakeup_all %p", fp
->f_waddr
);
5051 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
) {
5052 if (wait_queue_wakeup_all(&select_conflict_queue
, NULL
, THREAD_INTERRUPTED
) == KERN_INVALID_ARGUMENT
)
5053 panic("bad select_conflict_queue");
5055 p
->p_fpdrainwait
= 1;
5057 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
, "fpdrain", NULL
);
5061 if ((fp
->f_flags
& FP_INSELECT
) != 0)
5062 panic("FP_INSELECT set on drained fp");
5064 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
)
5065 fp
->f_flags
&= ~FP_SELCONFLICT
;
5072 * Description: Release the fd and free the fileproc associated with the fd
5073 * in the per process open file table of the specified process;
5074 * these values must correspond.
5076 * Parameters: p Process containing fd
5077 * fd fd to be released
5078 * fp fileproc to be freed
5080 * Returns: 0 Success
5082 * Notes: XXX function should be void - no one interprets the returns
5086 fp_free(proc_t p
, int fd
, struct fileproc
* fp
)
5088 proc_fdlock_spin(p
);
5092 fg_free(fp
->f_fglob
);
5101 * Description: Apply an advisory lock on a file descriptor.
5103 * Parameters: p Process making request
5104 * uap->fd fd on which the lock is to be
5106 * uap->how (Un)Lock bits, including type
5107 * retval Pointer to the call return area
5109 * Returns: 0 Success
5110 * fp_getfvp:EBADF Bad file descriptor
5111 * fp_getfvp:ENOTSUP fd does not refer to a vnode
5112 * vnode_getwithref:???
5116 * *retval (modified) Size of dtable
5118 * Notes: Just attempt to get a record lock of the requested type on
5119 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
5122 flock(proc_t p
, struct flock_args
*uap
, __unused
int32_t *retval
)
5126 struct fileproc
*fp
;
5129 vfs_context_t ctx
= vfs_context_current();
5132 AUDIT_ARG(fd
, uap
->fd
);
5133 if ( (error
= fp_getfvp(p
, fd
, &fp
, &vp
)) ) {
5136 if ( (error
= vnode_getwithref(vp
)) ) {
5139 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
5141 lf
.l_whence
= SEEK_SET
;
5144 if (how
& LOCK_UN
) {
5145 lf
.l_type
= F_UNLCK
;
5146 fp
->f_flag
&= ~FHASLOCK
;
5147 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_UNLCK
, &lf
, F_FLOCK
, ctx
, NULL
);
5151 lf
.l_type
= F_WRLCK
;
5152 else if (how
& LOCK_SH
)
5153 lf
.l_type
= F_RDLCK
;
5159 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
, F_SETLK
, &lf
);
5163 fp
->f_flag
|= FHASLOCK
;
5164 if (how
& LOCK_NB
) {
5165 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, F_FLOCK
, ctx
, NULL
);
5168 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, F_FLOCK
|F_WAIT
, ctx
, NULL
);
5170 (void)vnode_put(vp
);
5172 fp_drop(p
, fd
, fp
, 0);
5180 * Description: Obtain a Mach send right for a given file descriptor.
5182 * Parameters: p Process calling fileport
5183 * uap->fd The fd to reference
5184 * uap->portnamep User address at which to place port name.
5186 * Returns: 0 Success.
5187 * EBADF Bad file descriptor.
5188 * EINVAL File descriptor had type that cannot be sent, misc. other errors.
5189 * EFAULT Address at which to store port name is not valid.
5190 * EAGAIN Resource shortage.
5193 * On success, name of send right is stored at user-specified address.
5196 fileport_makeport(proc_t p
, struct fileport_makeport_args
*uap
,
5197 __unused
int *retval
)
5201 user_addr_t user_portaddr
= uap
->portnamep
;
5202 struct fileproc
*fp
= FILEPROC_NULL
;
5203 struct fileglob
*fg
= NULL
;
5204 ipc_port_t fileport
;
5205 mach_port_name_t name
= MACH_PORT_NULL
;
5207 err
= fp_lookup(p
, fd
, &fp
, 0);
5212 if (!filetype_issendable(fp
->f_type
)) {
5217 if (FP_ISGUARDED(fp
, GUARD_FILEPORT
)) {
5219 err
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_FILEPORT
);
5224 /* Dropped when port is deallocated */
5228 /* Allocate and initialize a port */
5229 fileport
= fileport_alloc(fg
);
5230 if (fileport
== IPC_PORT_NULL
) {
5236 /* Add an entry. Deallocates port on failure. */
5237 name
= ipc_port_copyout_send(fileport
, get_task_ipcspace(p
->task
));
5238 if (!MACH_PORT_VALID(name
)) {
5243 err
= copyout(&name
, user_portaddr
, sizeof(mach_port_name_t
));
5248 /* Tag the fileglob for debugging purposes */
5249 lck_mtx_lock_spin(&fg
->fg_lock
);
5250 fg
->fg_lflags
|= FG_PORTMADE
;
5251 lck_mtx_unlock(&fg
->fg_lock
);
5253 fp_drop(p
, fd
, fp
, 0);
5258 if (MACH_PORT_VALID(name
)) {
5259 /* Don't care if another thread races us to deallocate the entry */
5260 (void) mach_port_deallocate(get_task_ipcspace(p
->task
), name
);
5263 if (fp
!= FILEPROC_NULL
) {
5264 fp_drop(p
, fd
, fp
, 0);
5271 fileport_releasefg(struct fileglob
*fg
)
5273 (void)closef_locked(NULL
, fg
, PROC_NULL
);
5282 * Description: Obtain the file descriptor for a given Mach send right.
5284 * Parameters: p Process calling fileport
5285 * uap->port Name of send right to file port.
5287 * Returns: 0 Success
5288 * EINVAL Invalid Mach port name, or port is not for a file.
5290 * fdalloc:ENOMEM Unable to allocate fileproc or extend file table.
5293 * *retval (modified) The new descriptor
5296 fileport_makefd(proc_t p
, struct fileport_makefd_args
*uap
, int32_t *retval
)
5298 struct fileglob
*fg
;
5299 struct fileproc
*fp
= FILEPROC_NULL
;
5300 ipc_port_t port
= IPC_PORT_NULL
;
5301 mach_port_name_t send
= uap
->port
;
5306 res
= ipc_object_copyin(get_task_ipcspace(p
->task
),
5307 send
, MACH_MSG_TYPE_COPY_SEND
, &port
);
5309 if (res
!= KERN_SUCCESS
) {
5314 fg
= fileport_port_to_fileglob(port
);
5320 fp
= fileproc_alloc_init(NULL
);
5321 if (fp
== FILEPROC_NULL
) {
5330 err
= fdalloc(p
, 0, &fd
);
5335 *fdflags(p
, fd
) |= UF_EXCLOSE
;
5337 procfdtbl_releasefd(p
, fd
, fp
);
5343 if ((fp
!= NULL
) && (0 != err
)) {
5347 if (IPC_PORT_NULL
!= port
) {
5348 ipc_port_release_send(port
);
5358 * Description: Duplicate the specified descriptor to a free descriptor;
5359 * this is the second half of fdopen(), above.
5361 * Parameters: fdp filedesc pointer to fill in
5363 * dfd fd to dup from
5364 * mode mode to set on new fd
5365 * error command code
5367 * Returns: 0 Success
5368 * EBADF Source fd is bad
5369 * EACCES Requested mode not allowed
5370 * !0 'error', if not ENODEV or
5373 * Notes: XXX This is not thread safe; see fdopen() above
5376 dupfdopen(struct filedesc
*fdp
, int indx
, int dfd
, int flags
, int error
)
5378 struct fileproc
*wfp
;
5379 struct fileproc
*fp
;
5383 proc_t p
= current_proc();
5386 * If the to-be-dup'd fd number is greater than the allowed number
5387 * of file descriptors, or the fd to be dup'd has already been
5388 * closed, reject. Note, check for new == old is necessary as
5389 * falloc could allocate an already closed to-be-dup'd descriptor
5390 * as the new descriptor.
5394 fp
= fdp
->fd_ofiles
[indx
];
5395 if (dfd
< 0 || dfd
>= fdp
->fd_nfiles
||
5396 (wfp
= fdp
->fd_ofiles
[dfd
]) == NULL
|| wfp
== fp
||
5397 (fdp
->fd_ofileflags
[dfd
] & UF_RESERVED
)) {
5403 myerror
= mac_file_check_dup(proc_ucred(p
), wfp
->f_fglob
, dfd
);
5410 * There are two cases of interest here.
5412 * For ENODEV simply dup (dfd) to file descriptor
5413 * (indx) and return.
5415 * For ENXIO steal away the file structure from (dfd) and
5416 * store it in (indx). (dfd) is effectively closed by
5419 * Any other error code is just returned.
5423 if (FP_ISGUARDED(wfp
, GUARD_DUP
)) {
5424 int err
= fp_guard_exception(p
,
5425 dfd
, wfp
, kGUARD_EXC_DUP
);
5431 * Check that the mode the file is being opened for is a
5432 * subset of the mode of the existing descriptor.
5434 if (((flags
& (FREAD
|FWRITE
)) | wfp
->f_flag
) != wfp
->f_flag
) {
5438 if (indx
> fdp
->fd_lastfile
)
5439 fdp
->fd_lastfile
= indx
;
5443 fg_free(fp
->f_fglob
);
5444 fp
->f_fglob
= wfp
->f_fglob
;
5446 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
] |
5447 (flags
& O_CLOEXEC
) ? UF_EXCLOSE
: 0;
5463 * Description: Add a reference to a fileglob by fileproc
5465 * Parameters: fp fileproc containing fileglob
5470 * Notes: XXX Should use OSAddAtomic?
5473 fg_ref(struct fileproc
* fp
)
5475 struct fileglob
*fg
;
5479 lck_mtx_lock_spin(&fg
->fg_lock
);
5482 if ((fp
->f_flags
& ~((unsigned int)FP_VALID_FLAGS
)) != 0)
5483 panic("fg_ref: invalid bits on fp %p", fp
);
5485 if (fg
->fg_count
== 0)
5486 panic("fg_ref: adding fgcount to zeroed fg: fp %p fg %p",
5490 lck_mtx_unlock(&fg
->fg_lock
);
5497 * Description: Remove a reference to a fileglob by fileproc
5499 * Parameters: fp fileproc containing fileglob
5504 * Notes: XXX Should use OSAddAtomic?
5507 fg_drop(struct fileproc
* fp
)
5509 struct fileglob
*fg
;
5512 lck_mtx_lock_spin(&fg
->fg_lock
);
5514 lck_mtx_unlock(&fg
->fg_lock
);
5521 * Description: Insert fileglob onto message queue
5523 * Parameters: fg Fileglob pointer to insert
5527 * Locks: Takes and drops fg_lock, potentially many times
5530 fg_insertuipc(struct fileglob
* fg
)
5534 lck_mtx_lock_spin(&fg
->fg_lock
);
5536 while (fg
->fg_lflags
& FG_RMMSGQ
) {
5537 lck_mtx_convert_spin(&fg
->fg_lock
);
5539 fg
->fg_lflags
|= FG_WRMMSGQ
;
5540 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_insertuipc", NULL
);
5545 if (fg
->fg_msgcount
== 1) {
5546 fg
->fg_lflags
|= FG_INSMSGQ
;
5549 lck_mtx_unlock(&fg
->fg_lock
);
5552 lck_mtx_lock_spin(uipc_lock
);
5554 LIST_INSERT_HEAD(&fmsghead
, fg
, f_msglist
);
5555 lck_mtx_unlock(uipc_lock
);
5556 lck_mtx_lock(&fg
->fg_lock
);
5557 fg
->fg_lflags
&= ~FG_INSMSGQ
;
5558 if (fg
->fg_lflags
& FG_WINSMSGQ
) {
5559 fg
->fg_lflags
&= ~FG_WINSMSGQ
;
5560 wakeup(&fg
->fg_lflags
);
5562 lck_mtx_unlock(&fg
->fg_lock
);
5571 * Description: Remove fileglob from message queue
5573 * Parameters: fg Fileglob pointer to remove
5577 * Locks: Takes and drops fg_lock, potentially many times
5580 fg_removeuipc(struct fileglob
* fg
)
5584 lck_mtx_lock_spin(&fg
->fg_lock
);
5585 while (fg
->fg_lflags
& FG_INSMSGQ
) {
5586 lck_mtx_convert_spin(&fg
->fg_lock
);
5588 fg
->fg_lflags
|= FG_WINSMSGQ
;
5589 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_removeuipc", NULL
);
5592 if (fg
->fg_msgcount
== 0) {
5593 fg
->fg_lflags
|= FG_RMMSGQ
;
5596 lck_mtx_unlock(&fg
->fg_lock
);
5599 lck_mtx_lock_spin(uipc_lock
);
5601 LIST_REMOVE(fg
, f_msglist
);
5602 lck_mtx_unlock(uipc_lock
);
5603 lck_mtx_lock(&fg
->fg_lock
);
5604 fg
->fg_lflags
&= ~FG_RMMSGQ
;
5605 if (fg
->fg_lflags
& FG_WRMMSGQ
) {
5606 fg
->fg_lflags
&= ~FG_WRMMSGQ
;
5607 wakeup(&fg
->fg_lflags
);
5609 lck_mtx_unlock(&fg
->fg_lock
);
5612 #endif /* SOCKETS */
5617 * Description: Generic fileops read indirected through the fileops pointer
5618 * in the fileproc structure
5620 * Parameters: fp fileproc structure pointer
5621 * uio user I/O structure pointer
5623 * ctx VFS context for operation
5625 * Returns: 0 Success
5626 * !0 Errno from read
5629 fo_read(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
5631 return ((*fp
->f_ops
->fo_read
)(fp
, uio
, flags
, ctx
));
5638 * Description: Generic fileops write indirected through the fileops pointer
5639 * in the fileproc structure
5641 * Parameters: fp fileproc structure pointer
5642 * uio user I/O structure pointer
5644 * ctx VFS context for operation
5646 * Returns: 0 Success
5647 * !0 Errno from write
5650 fo_write(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
5652 return((*fp
->f_ops
->fo_write
)(fp
, uio
, flags
, ctx
));
5659 * Description: Generic fileops ioctl indirected through the fileops pointer
5660 * in the fileproc structure
5662 * Parameters: fp fileproc structure pointer
5664 * data pointer to internalized copy
5665 * of user space ioctl command
5666 * parameter data in kernel space
5667 * ctx VFS context for operation
5669 * Returns: 0 Success
5670 * !0 Errno from ioctl
5672 * Locks: The caller is assumed to have held the proc_fdlock; this
5673 * function releases and reacquires this lock. If the caller
5674 * accesses data protected by this lock prior to calling this
5675 * function, it will need to revalidate/reacquire any cached
5676 * protected data obtained prior to the call.
5679 fo_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
, vfs_context_t ctx
)
5683 proc_fdunlock(vfs_context_proc(ctx
));
5684 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, com
, data
, ctx
);
5685 proc_fdlock(vfs_context_proc(ctx
));
5693 * Description: Generic fileops select indirected through the fileops pointer
5694 * in the fileproc structure
5696 * Parameters: fp fileproc structure pointer
5697 * which select which
5698 * wql pointer to wait queue list
5699 * ctx VFS context for operation
5701 * Returns: 0 Success
5702 * !0 Errno from select
5705 fo_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
5707 return((*fp
->f_ops
->fo_select
)(fp
, which
, wql
, ctx
));
5714 * Description: Generic fileops close indirected through the fileops pointer
5715 * in the fileproc structure
5717 * Parameters: fp fileproc structure pointer for
5719 * ctx VFS context for operation
5721 * Returns: 0 Success
5722 * !0 Errno from close
5725 fo_close(struct fileglob
*fg
, vfs_context_t ctx
)
5727 return((*fg
->fg_ops
->fo_close
)(fg
, ctx
));
5734 * Description: Generic fileops kqueue filter indirected through the fileops
5735 * pointer in the fileproc structure
5737 * Parameters: fp fileproc structure pointer
5738 * kn pointer to knote to filter on
5739 * ctx VFS context for operation
5741 * Returns: 0 Success
5742 * !0 Errno from kqueue filter
5745 fo_kqfilter(struct fileproc
*fp
, struct knote
*kn
, vfs_context_t ctx
)
5747 return ((*fp
->f_ops
->fo_kqfilter
)(fp
, kn
, ctx
));
5751 * The ability to send a file descriptor to another
5752 * process is opt-in by file type.
5755 filetype_issendable(file_type_t fdtype
)
5764 /* DTYPE_KQUEUE, DTYPE_FSEVENTS, DTYPE_PSXSEM */
5771 fileproc_alloc_init(__unused
void *arg
)
5773 struct fileproc
*fp
;
5775 MALLOC_ZONE(fp
, struct fileproc
*, sizeof (*fp
), M_FILEPROC
, M_WAITOK
);
5777 bzero(fp
, sizeof (*fp
));
5783 fileproc_free(struct fileproc
*fp
)
5785 switch (FILEPROC_TYPE(fp
)) {
5787 FREE_ZONE(fp
, sizeof (*fp
), M_FILEPROC
);
5790 guarded_fileproc_free(fp
);
5793 panic("%s: corrupt fp %p flags %x", __func__
, fp
, fp
->f_flags
);