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
, struct nameidata
*, 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 struct nameidata nd
;
1583 user_addr_t pathname
;
1585 /* Check if this isn't a valid file descriptor */
1586 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1587 (fp
->f_flag
& FREAD
) == 0) {
1591 vp
= (struct vnode
*)fp
->f_data
;
1594 if (vnode_getwithref(vp
)) {
1599 /* Only valid for directories */
1600 if (vp
->v_type
!= VDIR
) {
1606 /* Get flags, mode and pathname arguments. */
1607 if (IS_64BIT_PROCESS(p
)) {
1608 pathname
= (user_addr_t
)argp
;
1610 pathname
= CAST_USER_ADDR_T(argp
);
1613 /* Start the lookup relative to the file descriptor's vnode. */
1614 NDINIT(&nd
, DELETE
, OP_UNLINK
, USEDVP
| AUDITVNPATH1
, UIO_USERSPACE
,
1615 pathname
, &context
);
1618 error
= unlink1(&context
, &nd
, 0);
1628 struct user_fsignatures fs
;
1630 vm_offset_t kernel_blob_addr
;
1631 vm_size_t kernel_blob_size
;
1633 if (fp
->f_type
!= DTYPE_VNODE
) {
1637 vp
= (struct vnode
*)fp
->f_data
;
1639 error
= vnode_getwithref(vp
);
1643 if (IS_64BIT_PROCESS(p
)) {
1644 error
= copyin(argp
, &fs
, sizeof (fs
));
1646 struct user32_fsignatures fs32
;
1648 error
= copyin(argp
, &fs32
, sizeof (fs32
));
1649 fs
.fs_file_start
= fs32
.fs_file_start
;
1650 fs
.fs_blob_start
= CAST_USER_ADDR_T(fs32
.fs_blob_start
);
1651 fs
.fs_blob_size
= fs32
.fs_blob_size
;
1659 if(ubc_cs_blob_get(vp
, CPU_TYPE_ANY
, fs
.fs_file_start
))
1665 * An arbitrary limit, to prevent someone from mapping in a 20GB blob. This should cover
1666 * our use cases for the immediate future, but note that at the time of this commit, some
1667 * platforms are nearing 2MB blob sizes (with a prior soft limit of 2.5MB).
1669 * We should consider how we can manage this more effectively; the above means that some
1670 * platforms are using megabytes of memory for signing data; it merely hasn't crossed the
1671 * threshold considered ridiculous at the time of this change.
1673 #define CS_MAX_BLOB_SIZE (10ULL * 1024ULL * 1024ULL)
1674 if (fs
.fs_blob_size
> CS_MAX_BLOB_SIZE
) {
1680 kernel_blob_size
= CAST_DOWN(vm_size_t
, fs
.fs_blob_size
);
1681 kr
= ubc_cs_blob_allocate(&kernel_blob_addr
, &kernel_blob_size
);
1682 if (kr
!= KERN_SUCCESS
) {
1688 if(uap
->cmd
== F_ADDSIGS
) {
1689 error
= copyin(fs
.fs_blob_start
,
1690 (void *) kernel_blob_addr
,
1692 } else /* F_ADDFILESIGS */ {
1693 error
= vn_rdwr(UIO_READ
,
1695 (caddr_t
) kernel_blob_addr
,
1697 fs
.fs_file_start
+ fs
.fs_blob_start
,
1706 ubc_cs_blob_deallocate(kernel_blob_addr
,
1712 error
= ubc_cs_blob_add(
1714 CPU_TYPE_ANY
, /* not for a specific architecture */
1719 ubc_cs_blob_deallocate(kernel_blob_addr
,
1722 /* ubc_blob_add() has consumed "kernel_blob_addr" */
1723 #if CHECK_CS_VALIDATION_BITMAP
1724 ubc_cs_validation_bitmap_allocate( vp
);
1728 (void) vnode_put(vp
);
1732 #ifdef SECURE_KERNEL
1734 #else /* !SECURE_KERNEL */
1737 if (fp
->f_type
!= DTYPE_VNODE
) {
1741 vp
= (struct vnode
*)fp
->f_data
;
1743 error
= vnode_getwithref(vp
);
1747 error
= copyin(argp
, &offsetMacho
, sizeof(offsetMacho
));
1749 (void)vnode_put(vp
);
1754 error
= mac_vnode_find_sigs(p
, vp
, offsetMacho
);
1759 (void)vnode_put(vp
);
1762 #endif /* SECURE_KERNEL */
1766 case F_GETPROTECTIONCLASS
: {
1769 if (fp
->f_type
!= DTYPE_VNODE
) {
1773 vp
= (struct vnode
*)fp
->f_data
;
1777 if (vnode_getwithref(vp
)) {
1782 error
= cp_vnode_getclass (vp
, &class);
1791 case F_SETPROTECTIONCLASS
: {
1792 /* tmp must be a valid PROTECTION_CLASS_* */
1793 tmp
= CAST_DOWN_EXPLICIT(uint32_t, uap
->arg
);
1795 if (fp
->f_type
!= DTYPE_VNODE
) {
1799 vp
= (struct vnode
*)fp
->f_data
;
1803 if (vnode_getwithref(vp
)) {
1808 /* Only go forward if you have write access */
1809 vfs_context_t ctx
= vfs_context_current();
1810 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
1815 error
= cp_vnode_setclass (vp
, tmp
);
1820 case F_TRANSCODEKEY
: {
1822 char *backup_keyp
= NULL
;
1823 unsigned backup_key_len
= CP_MAX_WRAPPEDKEYSIZE
;
1825 if (fp
->f_type
!= DTYPE_VNODE
) {
1830 vp
= (struct vnode
*)fp
->f_data
;
1833 if (vnode_getwithref(vp
)) {
1838 MALLOC(backup_keyp
, char *, backup_key_len
, M_TEMP
, M_WAITOK
);
1839 if (backup_keyp
== NULL
) {
1844 error
= cp_vnode_transcode (vp
, backup_keyp
, &backup_key_len
);
1848 error
= copyout((caddr_t
)backup_keyp
, argp
, backup_key_len
);
1849 *retval
= backup_key_len
;
1852 FREE(backup_keyp
, M_TEMP
);
1857 case F_GETPROTECTIONLEVEL
: {
1858 uint32_t cp_version
= 0;
1860 if (fp
->f_type
!= DTYPE_VNODE
) {
1865 vp
= (struct vnode
*) fp
->f_data
;
1868 if (vnode_getwithref(vp
)) {
1874 * if cp_get_major_vers fails, error will be set to proper errno
1875 * and cp_version will still be 0.
1878 error
= cp_get_root_major_vers (vp
, &cp_version
);
1879 *retval
= cp_version
;
1885 case F_GETDEFAULTPROTLEVEL
: {
1886 uint32_t cp_default
= 0;
1888 if (fp
->f_type
!= DTYPE_VNODE
) {
1893 vp
= (struct vnode
*) fp
->f_data
;
1896 if (vnode_getwithref(vp
)) {
1902 * if cp_get_major_vers fails, error will be set to proper errno
1903 * and cp_version will still be 0.
1906 error
= cp_get_default_level(vp
, &cp_default
);
1907 *retval
= cp_default
;
1914 #endif /* CONFIG_PROTECT */
1916 case F_MOVEDATAEXTENTS
: {
1917 struct fileproc
*fp2
= NULL
;
1918 struct vnode
*src_vp
= NULLVP
;
1919 struct vnode
*dst_vp
= NULLVP
;
1920 /* We need to grab the 2nd FD out of the argments before moving on. */
1921 int fd2
= CAST_DOWN_EXPLICIT(int32_t, uap
->arg
);
1923 if (fp
->f_type
!= DTYPE_VNODE
) {
1928 /* For now, special case HFS+ only, since this is SPI. */
1929 src_vp
= (struct vnode
*)fp
->f_data
;
1930 if (src_vp
->v_tag
!= VT_HFS
) {
1936 * Get the references before we start acquiring iocounts on the vnodes,
1937 * while we still hold the proc fd lock
1939 if ( (error
= fp_lookup(p
, fd2
, &fp2
, 1)) ) {
1943 if (fp2
->f_type
!= DTYPE_VNODE
) {
1944 fp_drop(p
, fd2
, fp2
, 1);
1948 dst_vp
= (struct vnode
*)fp2
->f_data
;
1949 if (dst_vp
->v_tag
!= VT_HFS
) {
1950 fp_drop(p
, fd2
, fp2
, 1);
1956 /* Re-do MAC checks against the new FD, pass in a fake argument */
1957 error
= mac_file_check_fcntl(proc_ucred(p
), fp2
->f_fglob
, uap
->cmd
, 0);
1959 fp_drop(p
, fd2
, fp2
, 1);
1963 /* Audit the 2nd FD */
1968 if (vnode_getwithref(src_vp
)) {
1969 fp_drop(p
, fd2
, fp2
, 0);
1973 if (vnode_getwithref(dst_vp
)) {
1975 fp_drop(p
, fd2
, fp2
, 0);
1981 * Basic asserts; validate they are not the same and that
1982 * both live on the same filesystem.
1984 if (dst_vp
== src_vp
) {
1987 fp_drop (p
, fd2
, fp2
, 0);
1992 if (dst_vp
->v_mount
!= src_vp
->v_mount
) {
1995 fp_drop (p
, fd2
, fp2
, 0);
2000 /* Now we have a legit pair of FDs. Go to work */
2002 /* Now check for write access to the target files */
2003 if(vnode_authorize(src_vp
, NULLVP
,
2004 (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), &context
) != 0) {
2007 fp_drop(p
, fd2
, fp2
, 0);
2012 if(vnode_authorize(dst_vp
, NULLVP
,
2013 (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), &context
) != 0) {
2016 fp_drop(p
, fd2
, fp2
, 0);
2021 /* Verify that both vps point to files and not directories */
2022 if ( !vnode_isreg(src_vp
) || !vnode_isreg(dst_vp
)) {
2026 fp_drop (p
, fd2
, fp2
, 0);
2031 * The exchangedata syscall handler passes in 0 for the flags to VNOP_EXCHANGE.
2032 * We'll pass in our special bit indicating that the new behavior is expected
2035 error
= VNOP_EXCHANGE(src_vp
, dst_vp
, FSOPT_EXCHANGE_DATA_ONLY
, &context
);
2039 fp_drop(p
, fd2
, fp2
, 0);
2044 * SPI for making a file compressed.
2046 case F_MAKECOMPRESSED
: {
2047 uint32_t gcounter
= CAST_DOWN_EXPLICIT(uint32_t, uap
->arg
);
2049 if (fp
->f_type
!= DTYPE_VNODE
) {
2054 vp
= (struct vnode
*) fp
->f_data
;
2058 if (vnode_getwithref(vp
)) {
2064 if ((vnode_isreg(vp
) == 0) && (vnode_islnk(vp
) == 0)) {
2070 /* invoke ioctl to pass off to FS */
2071 /* Only go forward if you have write access */
2072 vfs_context_t ctx
= vfs_context_current();
2073 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2079 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)&gcounter
, 0, &context
);
2086 * SPI (private) for indicating to a filesystem that subsequent writes to
2087 * the open FD will written to the Fastflow.
2089 case F_SET_GREEDY_MODE
:
2090 /* intentionally drop through to the same handler as F_SETSTATIC.
2091 * both fcntls should pass the argument and their selector into VNOP_IOCTL.
2095 * SPI (private) for indicating to a filesystem that subsequent writes to
2096 * the open FD will represent static content.
2098 case F_SETSTATICCONTENT
: {
2099 caddr_t ioctl_arg
= NULL
;
2102 ioctl_arg
= (caddr_t
) 1;
2105 if (fp
->f_type
!= DTYPE_VNODE
) {
2109 vp
= (struct vnode
*)fp
->f_data
;
2112 error
= vnode_getwithref(vp
);
2118 /* Only go forward if you have write access */
2119 vfs_context_t ctx
= vfs_context_current();
2120 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2126 error
= VNOP_IOCTL(vp
, uap
->cmd
, ioctl_arg
, 0, &context
);
2127 (void)vnode_put(vp
);
2133 * SPI (private) for indicating to the lower level storage driver that the
2134 * subsequent writes should be of a particular IO type (burst, greedy, static),
2135 * or other flavors that may be necessary.
2142 /* extract 32 bits of flags from userland */
2143 param_ptr
= (caddr_t
) uap
->arg
;
2144 param
= (uint32_t) param_ptr
;
2147 /* If no argument is specified, error out */
2153 * Validate the different types of flags that can be specified:
2154 * all of them are mutually exclusive for now.
2157 case F_IOTYPE_ISOCHRONOUS
:
2166 if (fp
->f_type
!= DTYPE_VNODE
) {
2170 vp
= (struct vnode
*)fp
->f_data
;
2173 error
= vnode_getwithref(vp
);
2179 /* Only go forward if you have write access */
2180 vfs_context_t ctx
= vfs_context_current();
2181 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2187 error
= VNOP_IOCTL(vp
, uap
->cmd
, param_ptr
, 0, &context
);
2188 (void)vnode_put(vp
);
2195 * Extract the CodeDirectory of the vnode associated with
2196 * the file descriptor and copy it back to user space
2198 case F_GETCODEDIR
: {
2199 struct user_fcodeblobs args
;
2201 if (fp
->f_type
!= DTYPE_VNODE
) {
2206 vp
= (struct vnode
*)fp
->f_data
;
2209 if ((fp
->f_flag
& FREAD
) == 0) {
2214 if (IS_64BIT_PROCESS(p
)) {
2215 struct user64_fcodeblobs args64
;
2217 error
= copyin(argp
, &args64
, sizeof(args64
));
2221 args
.f_cd_hash
= args64
.f_cd_hash
;
2222 args
.f_hash_size
= args64
.f_hash_size
;
2223 args
.f_cd_buffer
= args64
.f_cd_buffer
;
2224 args
.f_cd_size
= args64
.f_cd_size
;
2225 args
.f_out_size
= args64
.f_out_size
;
2226 args
.f_arch
= args64
.f_arch
;
2228 struct user32_fcodeblobs args32
;
2230 error
= copyin(argp
, &args32
, sizeof(args32
));
2234 args
.f_cd_hash
= CAST_USER_ADDR_T(args32
.f_cd_hash
);
2235 args
.f_hash_size
= args32
.f_hash_size
;
2236 args
.f_cd_buffer
= CAST_USER_ADDR_T(args32
.f_cd_buffer
);
2237 args
.f_cd_size
= args32
.f_cd_size
;
2238 args
.f_out_size
= CAST_USER_ADDR_T(args32
.f_out_size
);
2239 args
.f_arch
= args32
.f_arch
;
2242 if (vp
->v_ubcinfo
== NULL
) {
2247 struct cs_blob
*t_blob
= vp
->v_ubcinfo
->cs_blobs
;
2250 * This call fails if there is no cs_blob corresponding to the
2251 * vnode, or if there are multiple cs_blobs present, and the caller
2252 * did not specify which cpu_type they want the cs_blob for
2254 if (t_blob
== NULL
) {
2255 error
= ENOENT
; /* there is no codesigning blob for this process */
2257 } else if (args
.f_arch
== 0 && t_blob
->csb_next
!= NULL
) {
2258 error
= ENOENT
; /* too many architectures and none specified */
2262 /* If the user specified an architecture, find the right blob */
2263 if (args
.f_arch
!= 0) {
2265 if (t_blob
->csb_cpu_type
== args
.f_arch
)
2267 t_blob
= t_blob
->csb_next
;
2269 /* The cpu_type the user requested could not be found */
2270 if (t_blob
== NULL
) {
2276 const CS_SuperBlob
*super_blob
= (void *)t_blob
->csb_mem_kaddr
;
2277 const CS_CodeDirectory
*cd
= findCodeDirectory(super_blob
,
2278 (char *) super_blob
,
2279 (char *) super_blob
+ t_blob
->csb_mem_size
);
2285 uint64_t buffer_size
= ntohl(cd
->length
);
2287 if (buffer_size
> UINT_MAX
) {
2292 error
= copyout(&buffer_size
, args
.f_out_size
, sizeof(unsigned int));
2296 if (sizeof(t_blob
->csb_sha1
) > args
.f_hash_size
||
2297 buffer_size
> args
.f_cd_size
) {
2302 error
= copyout(t_blob
->csb_sha1
, args
.f_cd_hash
, sizeof(t_blob
->csb_sha1
));
2305 error
= copyout(cd
, args
.f_cd_buffer
, buffer_size
);
2313 * Set the vnode pointed to by 'fd'
2314 * and tag it as the (potentially future) backing store
2315 * for another filesystem
2317 case F_SETBACKINGSTORE
: {
2318 if (fp
->f_type
!= DTYPE_VNODE
) {
2323 vp
= (struct vnode
*)fp
->f_data
;
2325 if (vp
->v_tag
!= VT_HFS
) {
2331 if (vnode_getwithref(vp
)) {
2336 /* only proceed if you have write access */
2337 vfs_context_t ctx
= vfs_context_current();
2338 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
2345 /* If arg != 0, set, otherwise unset */
2347 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
)1, 0, &context
);
2350 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
2358 * like F_GETPATH, but special semantics for
2359 * the mobile time machine handler.
2361 case F_GETPATH_MTMINFO
: {
2365 if (fp
->f_type
!= DTYPE_VNODE
) {
2369 vp
= (struct vnode
*)fp
->f_data
;
2372 pathlen
= MAXPATHLEN
;
2373 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
2374 if (pathbufp
== NULL
) {
2378 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
2379 int backingstore
= 0;
2381 /* Check for error from vn_getpath before moving on */
2382 if ((error
= vn_getpath(vp
, pathbufp
, &pathlen
)) == 0) {
2383 if (vp
->v_tag
== VT_HFS
) {
2384 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
) &backingstore
, 0, &context
);
2386 (void)vnode_put(vp
);
2389 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
2393 * If the copyout was successful, now check to ensure
2394 * that this vnode is not a BACKINGSTORE vnode. mtmd
2395 * wants the path regardless.
2402 (void)vnode_put(vp
);
2404 FREE(pathbufp
, M_TEMP
);
2410 * This is an fcntl() that we d not recognize at this level;
2411 * if this is a vnode, we send it down into the VNOP_IOCTL
2412 * for this vnode; this can include special devices, and will
2413 * effectively overload fcntl() to send ioctl()'s.
2415 if((uap
->cmd
& IOC_VOID
) && (uap
->cmd
& IOC_INOUT
)){
2420 /* Catch any now-invalid fcntl() selectors */
2422 case F_MARKDEPENDENCY
:
2429 if (fp
->f_type
!= DTYPE_VNODE
) {
2433 vp
= (struct vnode
*)fp
->f_data
;
2436 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
2437 #define STK_PARAMS 128
2438 char stkbuf
[STK_PARAMS
];
2442 * For this to work properly, we have to copy in the
2443 * ioctl() cmd argument if there is one; we must also
2444 * check that a command parameter, if present, does
2445 * not exceed the maximum command length dictated by
2446 * the number of bits we have available in the command
2447 * to represent a structure length. Finally, we have
2448 * to copy the results back out, if it is that type of
2451 size
= IOCPARM_LEN(uap
->cmd
);
2452 if (size
> IOCPARM_MAX
) {
2453 (void)vnode_put(vp
);
2459 if (size
> sizeof (stkbuf
)) {
2460 if ((memp
= (caddr_t
)kalloc(size
)) == 0) {
2461 (void)vnode_put(vp
);
2470 if (uap
->cmd
& IOC_IN
) {
2473 error
= copyin(argp
, data
, size
);
2475 (void)vnode_put(vp
);
2481 /* Bzero the section beyond that which was needed */
2482 if (size
<= sizeof(stkbuf
)) {
2483 bzero ( (((uint8_t*)data
) + size
), (sizeof(stkbuf
) - size
));
2488 *(user_addr_t
*)data
= argp
;
2490 *(uint32_t *)data
= (uint32_t)argp
;
2493 } else if ((uap
->cmd
& IOC_OUT
) && size
) {
2495 * Zero the buffer so the user always
2496 * gets back something deterministic.
2499 } else if (uap
->cmd
& IOC_VOID
) {
2501 *(user_addr_t
*)data
= argp
;
2503 *(uint32_t *)data
= (uint32_t)argp
;
2507 error
= VNOP_IOCTL(vp
, uap
->cmd
, CAST_DOWN(caddr_t
, data
), 0, &context
);
2509 (void)vnode_put(vp
);
2511 /* Copy any output data to user */
2512 if (error
== 0 && (uap
->cmd
& IOC_OUT
) && size
)
2513 error
= copyout(data
, argp
, size
);
2521 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
2522 fp_drop(p
, fd
, fp
, 0);
2525 fp_drop(p
, fd
, fp
, 1);
2534 * Description: Common code for dup, dup2, and fcntl(F_DUPFD).
2536 * Parameters: p Process performing the dup
2538 * new The fd to dup it to
2539 * fd_flags Flags to augment the new fd
2540 * retval Pointer to the call return area
2542 * Returns: 0 Success
2547 * *retval (modified) The new descriptor
2549 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
2552 * Notes: This function may drop and reacquire this lock; it is unsafe
2553 * for a caller to assume that other state protected by the lock
2554 * has not been subsequently changed out from under it.
2558 struct filedesc
*fdp
, int old
, int new, int fd_flags
, int32_t *retval
)
2560 struct fileproc
*nfp
;
2561 struct fileproc
*ofp
;
2567 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2569 if ((ofp
= fdp
->fd_ofiles
[old
]) == NULL
||
2570 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
)) {
2577 error
= mac_file_check_dup(proc_ucred(p
), ofp
->f_fglob
, new);
2587 nfp
= fileproc_alloc_init(NULL
);
2597 nfp
->f_fglob
= ofp
->f_fglob
;
2600 if (fdp
->fd_ofiles
[new] != 0)
2601 panic("finishdup: overwriting fd_ofiles with new %d", new);
2602 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0)
2603 panic("finishdup: unreserved fileflags with new %d", new);
2606 if (new > fdp
->fd_lastfile
)
2607 fdp
->fd_lastfile
= new;
2608 *fdflags(p
, new) |= fd_flags
;
2609 procfdtbl_releasefd(p
, new, nfp
);
2618 * Description: The implementation of the close(2) system call
2620 * Parameters: p Process in whose per process file table
2621 * the close is to occur
2622 * uap->fd fd to be closed
2625 * Returns: 0 Success
2626 * fp_lookup:EBADF Bad file descriptor
2627 * fp_guard_exception:??? Guarded file descriptor
2628 * close_internal:EBADF
2629 * close_internal:??? Anything returnable by a per-fileops
2633 close(proc_t p
, struct close_args
*uap
, int32_t *retval
)
2635 __pthread_testcancel(1);
2636 return(close_nocancel(p
, (struct close_nocancel_args
*)uap
, retval
));
2641 close_nocancel(proc_t p
, struct close_nocancel_args
*uap
, __unused
int32_t *retval
)
2643 struct fileproc
*fp
;
2647 AUDIT_SYSCLOSE(p
, fd
);
2651 if ( (error
= fp_lookup(p
,fd
,&fp
, 1)) ) {
2656 if (FP_ISGUARDED(fp
, GUARD_CLOSE
)) {
2657 error
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_CLOSE
);
2658 (void) fp_drop(p
, fd
, fp
, 1);
2663 error
= close_internal_locked(p
, fd
, fp
, 0);
2672 * close_internal_locked
2674 * Close a file descriptor.
2676 * Parameters: p Process in whose per process file table
2677 * the close is to occur
2678 * fd fd to be closed
2679 * fp fileproc associated with the fd
2681 * Returns: 0 Success
2682 * EBADF fd already in close wait state
2683 * closef_locked:??? Anything returnable by a per-fileops
2686 * Locks: Assumes proc_fdlock for process is held by the caller and returns
2689 * Notes: This function may drop and reacquire this lock; it is unsafe
2690 * for a caller to assume that other state protected by the lock
2691 * has not been subsequently changed out from under it.
2694 close_internal_locked(proc_t p
, int fd
, struct fileproc
*fp
, int flags
)
2696 struct filedesc
*fdp
= p
->p_fd
;
2698 int resvfd
= flags
& FD_DUP2RESV
;
2702 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2705 /* Keep people from using the filedesc while we are closing it */
2706 procfdtbl_markclosefd(p
, fd
);
2709 if ((fp
->f_flags
& FP_CLOSING
) == FP_CLOSING
) {
2710 panic("close_internal_locked: being called on already closing fd");
2715 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
2716 panic("close_internal: unreserved fileflags with fd %d", fd
);
2719 fp
->f_flags
|= FP_CLOSING
;
2721 if ( (fp
->f_flags
& FP_AIOISSUED
) || kauth_authorize_fileop_has_listeners() ) {
2725 if ( (fp
->f_type
== DTYPE_VNODE
) && kauth_authorize_fileop_has_listeners() ) {
2727 * call out to allow 3rd party notification of close.
2728 * Ignore result of kauth_authorize_fileop call.
2730 if (vnode_getwithref((vnode_t
)fp
->f_data
) == 0) {
2731 u_int fileop_flags
= 0;
2732 if ((fp
->f_flags
& FP_WRITTEN
) != 0)
2733 fileop_flags
|= KAUTH_FILEOP_CLOSE_MODIFIED
;
2734 kauth_authorize_fileop(fp
->f_fglob
->fg_cred
, KAUTH_FILEOP_CLOSE
,
2735 (uintptr_t)fp
->f_data
, (uintptr_t)fileop_flags
);
2736 vnode_put((vnode_t
)fp
->f_data
);
2739 if (fp
->f_flags
& FP_AIOISSUED
)
2741 * cancel all async IO requests that can be cancelled.
2743 _aio_close( p
, fd
);
2748 if (fd
< fdp
->fd_knlistsize
)
2749 knote_fdclose(p
, fd
);
2751 if (fp
->f_flags
& FP_WAITEVENT
)
2752 (void)waitevent_close(p
, fp
);
2754 fileproc_drain(p
, fp
);
2759 procfdtbl_reservefd(p
, fd
);
2762 error
= closef_locked(fp
, fp
->f_fglob
, p
);
2763 if ((fp
->f_flags
& FP_WAITCLOSE
) == FP_WAITCLOSE
)
2764 wakeup(&fp
->f_flags
);
2765 fp
->f_flags
&= ~(FP_WAITCLOSE
| FP_CLOSING
);
2775 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
2776 panic("close with reserved fd returns with freed fd:%d: proc: %p", fd
, p
);
2787 * Description: Return status information about a file descriptor.
2789 * Parameters: p The process doing the fstat
2791 * ub The user stat buffer
2792 * xsecurity The user extended security
2793 * buffer, or 0 if none
2794 * xsecurity_size The size of xsecurity, or 0
2796 * isstat64 Flag to indicate 64 bit version
2797 * for inode size, etc.
2799 * Returns: 0 Success
2802 * fp_lookup:EBADF Bad file descriptor
2803 * vnode_getwithref:???
2805 * vnode_getwithref:???
2812 * Notes: Internal implementation for all other fstat() related
2815 * XXX switch on node type is bogus; need a stat in struct
2816 * XXX fileops instead.
2819 fstat1(proc_t p
, int fd
, user_addr_t ub
, user_addr_t xsecurity
, user_addr_t xsecurity_size
, int isstat64
)
2821 struct fileproc
*fp
;
2827 struct user64_stat user64_sb
;
2828 struct user32_stat user32_sb
;
2829 struct user64_stat64 user64_sb64
;
2830 struct user32_stat64 user32_sb64
;
2835 kauth_filesec_t fsec
;
2836 user_size_t xsecurity_bufsize
;
2837 vfs_context_t ctx
= vfs_context_current();
2843 if ((error
= fp_lookup(p
, fd
, &fp
, 0)) != 0) {
2848 fsec
= KAUTH_FILESEC_NONE
;
2850 sbptr
= (void *)&source
;
2855 if ((error
= vnode_getwithref((vnode_t
)data
)) == 0) {
2857 * If the caller has the file open, and is not
2858 * requesting extended security information, we are
2859 * going to let them get the basic stat information.
2861 if (xsecurity
== USER_ADDR_NULL
) {
2862 error
= vn_stat_noauth((vnode_t
)data
, sbptr
, NULL
, isstat64
, ctx
);
2864 error
= vn_stat((vnode_t
)data
, sbptr
, &fsec
, isstat64
, ctx
);
2867 AUDIT_ARG(vnpath
, (struct vnode
*)data
, ARG_VNODE1
);
2868 (void)vnode_put((vnode_t
)data
);
2874 error
= soo_stat((struct socket
*)data
, sbptr
, isstat64
);
2876 #endif /* SOCKETS */
2879 error
= pipe_stat((void *)data
, sbptr
, isstat64
);
2883 error
= pshm_stat((void *)data
, sbptr
, isstat64
);
2887 error
= kqueue_stat((void *)data
, sbptr
, isstat64
, p
);
2897 if (isstat64
!= 0) {
2898 source
.sb64
.st_lspare
= 0;
2899 source
.sb64
.st_qspare
[0] = 0LL;
2900 source
.sb64
.st_qspare
[1] = 0LL;
2902 if (IS_64BIT_PROCESS(current_proc())) {
2903 munge_user64_stat64(&source
.sb64
, &dest
.user64_sb64
);
2904 my_size
= sizeof(dest
.user64_sb64
);
2905 sbp
= (caddr_t
)&dest
.user64_sb64
;
2907 munge_user32_stat64(&source
.sb64
, &dest
.user32_sb64
);
2908 my_size
= sizeof(dest
.user32_sb64
);
2909 sbp
= (caddr_t
)&dest
.user32_sb64
;
2912 source
.sb
.st_lspare
= 0;
2913 source
.sb
.st_qspare
[0] = 0LL;
2914 source
.sb
.st_qspare
[1] = 0LL;
2915 if (IS_64BIT_PROCESS(current_proc())) {
2916 munge_user64_stat(&source
.sb
, &dest
.user64_sb
);
2917 my_size
= sizeof(dest
.user64_sb
);
2918 sbp
= (caddr_t
)&dest
.user64_sb
;
2920 munge_user32_stat(&source
.sb
, &dest
.user32_sb
);
2921 my_size
= sizeof(dest
.user32_sb
);
2922 sbp
= (caddr_t
)&dest
.user32_sb
;
2926 error
= copyout(sbp
, ub
, my_size
);
2929 /* caller wants extended security information? */
2930 if (xsecurity
!= USER_ADDR_NULL
) {
2932 /* did we get any? */
2933 if (fsec
== KAUTH_FILESEC_NONE
) {
2934 if (susize(xsecurity_size
, 0) != 0) {
2939 /* find the user buffer size */
2940 xsecurity_bufsize
= fusize(xsecurity_size
);
2942 /* copy out the actual data size */
2943 if (susize(xsecurity_size
, KAUTH_FILESEC_COPYSIZE(fsec
)) != 0) {
2948 /* if the caller supplied enough room, copy out to it */
2949 if (xsecurity_bufsize
>= KAUTH_FILESEC_COPYSIZE(fsec
))
2950 error
= copyout(fsec
, xsecurity
, KAUTH_FILESEC_COPYSIZE(fsec
));
2954 fp_drop(p
, fd
, fp
, 0);
2956 kauth_filesec_free(fsec
);
2964 * Description: Extended version of fstat supporting returning extended
2965 * security information
2967 * Parameters: p The process doing the fstat
2968 * uap->fd The fd to stat
2969 * uap->ub The user stat buffer
2970 * uap->xsecurity The user extended security
2971 * buffer, or 0 if none
2972 * uap->xsecurity_size The size of xsecurity, or 0
2974 * Returns: 0 Success
2975 * !0 Errno (see fstat1)
2978 fstat_extended(proc_t p
, struct fstat_extended_args
*uap
, __unused
int32_t *retval
)
2980 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 0));
2987 * Description: Get file status for the file associated with fd
2989 * Parameters: p The process doing the fstat
2990 * uap->fd The fd to stat
2991 * uap->ub The user stat buffer
2993 * Returns: 0 Success
2994 * !0 Errno (see fstat1)
2997 fstat(proc_t p
, register struct fstat_args
*uap
, __unused
int32_t *retval
)
2999 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 0));
3006 * Description: Extended version of fstat64 supporting returning extended
3007 * security information
3009 * Parameters: p The process doing the fstat
3010 * uap->fd The fd to stat
3011 * uap->ub The user stat buffer
3012 * uap->xsecurity The user extended security
3013 * buffer, or 0 if none
3014 * uap->xsecurity_size The size of xsecurity, or 0
3016 * Returns: 0 Success
3017 * !0 Errno (see fstat1)
3020 fstat64_extended(proc_t p
, struct fstat64_extended_args
*uap
, __unused
int32_t *retval
)
3022 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 1));
3029 * Description: Get 64 bit version of the file status for the file associated
3032 * Parameters: p The process doing the fstat
3033 * uap->fd The fd to stat
3034 * uap->ub The user stat buffer
3036 * Returns: 0 Success
3037 * !0 Errno (see fstat1)
3040 fstat64(proc_t p
, register struct fstat64_args
*uap
, __unused
int32_t *retval
)
3042 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 1));
3049 * Description: Return pathconf information about a file descriptor.
3051 * Parameters: p Process making the request
3052 * uap->fd fd to get information about
3053 * uap->name Name of information desired
3054 * retval Pointer to the call return area
3056 * Returns: 0 Success
3058 * fp_lookup:EBADF Bad file descriptor
3059 * vnode_getwithref:???
3063 * *retval (modified) Returned information (numeric)
3066 fpathconf(proc_t p
, struct fpathconf_args
*uap
, int32_t *retval
)
3069 struct fileproc
*fp
;
3076 AUDIT_ARG(fd
, uap
->fd
);
3077 if ( (error
= fp_lookup(p
, fd
, &fp
, 0)) )
3085 if (uap
->name
!= _PC_PIPE_BUF
) {
3094 if (uap
->name
!= _PC_PIPE_BUF
) {
3103 vp
= (struct vnode
*)data
;
3105 if ( (error
= vnode_getwithref(vp
)) == 0) {
3106 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
3108 error
= vn_pathconf(vp
, uap
->name
, retval
, vfs_context_current());
3110 (void)vnode_put(vp
);
3121 fp_drop(p
, fd
, fp
, 0);
3126 * Statistics counter for the number of times a process calling fdalloc()
3127 * has resulted in an expansion of the per process open file table.
3129 * XXX This would likely be of more use if it were per process
3137 * Description: Allocate a file descriptor for the process.
3139 * Parameters: p Process to allocate the fd in
3140 * want The fd we would prefer to get
3141 * result Pointer to fd we got
3143 * Returns: 0 Success
3148 * *result (modified) The fd which was allocated
3151 fdalloc(proc_t p
, int want
, int *result
)
3153 struct filedesc
*fdp
= p
->p_fd
;
3155 int lim
, last
, numfiles
, oldnfiles
;
3156 struct fileproc
**newofiles
, **ofiles
;
3157 char *newofileflags
;
3160 * Search for a free descriptor starting at the higher
3161 * of want or fd_freefile. If that fails, consider
3162 * expanding the ofile array.
3165 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
3168 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
3170 last
= min(fdp
->fd_nfiles
, lim
);
3171 if ((i
= want
) < fdp
->fd_freefile
)
3172 i
= fdp
->fd_freefile
;
3173 for (; i
< last
; i
++) {
3174 if (fdp
->fd_ofiles
[i
] == NULL
&& !(fdp
->fd_ofileflags
[i
] & UF_RESERVED
)) {
3175 procfdtbl_reservefd(p
, i
);
3176 if (i
> fdp
->fd_lastfile
)
3177 fdp
->fd_lastfile
= i
;
3178 if (want
<= fdp
->fd_freefile
)
3179 fdp
->fd_freefile
= i
;
3186 * No space in current array. Expand?
3188 if (fdp
->fd_nfiles
>= lim
)
3190 if (fdp
->fd_nfiles
< NDEXTENT
)
3191 numfiles
= NDEXTENT
;
3193 numfiles
= 2 * fdp
->fd_nfiles
;
3198 MALLOC_ZONE(newofiles
, struct fileproc
**,
3199 numfiles
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
3201 if (newofiles
== NULL
) {
3204 if (fdp
->fd_nfiles
>= numfiles
) {
3205 FREE_ZONE(newofiles
, numfiles
* OFILESIZE
, M_OFILETABL
);
3208 newofileflags
= (char *) &newofiles
[numfiles
];
3210 * Copy the existing ofile and ofileflags arrays
3211 * and zero the new portion of each array.
3213 oldnfiles
= fdp
->fd_nfiles
;
3214 (void) memcpy(newofiles
, fdp
->fd_ofiles
,
3215 oldnfiles
* sizeof(*fdp
->fd_ofiles
));
3216 (void) memset(&newofiles
[oldnfiles
], 0,
3217 (numfiles
- oldnfiles
) * sizeof(*fdp
->fd_ofiles
));
3219 (void) memcpy(newofileflags
, fdp
->fd_ofileflags
,
3220 oldnfiles
* sizeof(*fdp
->fd_ofileflags
));
3221 (void) memset(&newofileflags
[oldnfiles
], 0,
3222 (numfiles
- oldnfiles
) *
3223 sizeof(*fdp
->fd_ofileflags
));
3224 ofiles
= fdp
->fd_ofiles
;
3225 fdp
->fd_ofiles
= newofiles
;
3226 fdp
->fd_ofileflags
= newofileflags
;
3227 fdp
->fd_nfiles
= numfiles
;
3228 FREE_ZONE(ofiles
, oldnfiles
* OFILESIZE
, M_OFILETABL
);
3237 * Description: Check to see whether n user file descriptors are available
3240 * Parameters: p Process to check in
3241 * n The number of fd's desired
3246 * Locks: Assumes proc_fdlock for process is held by the caller
3248 * Notes: The answer only remains valid so long as the proc_fdlock is
3249 * held by the caller.
3252 fdavail(proc_t p
, int n
)
3254 struct filedesc
*fdp
= p
->p_fd
;
3255 struct fileproc
**fpp
;
3259 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
3260 if ((i
= lim
- fdp
->fd_nfiles
) > 0 && (n
-= i
) <= 0)
3262 fpp
= &fdp
->fd_ofiles
[fdp
->fd_freefile
];
3263 flags
= &fdp
->fd_ofileflags
[fdp
->fd_freefile
];
3264 for (i
= fdp
->fd_nfiles
- fdp
->fd_freefile
; --i
>= 0; fpp
++, flags
++)
3265 if (*fpp
== NULL
&& !(*flags
& UF_RESERVED
) && --n
<= 0)
3274 * Description: Legacy KPI wrapper function for _fdrelse
3276 * Parameters: p Process in which fd lives
3281 * Locks: Assumes proc_fdlock for process is held by the caller
3284 fdrelse(proc_t p
, int fd
)
3293 * Description: Get the fileproc pointer for the given fd from the per process
3294 * open file table without taking an explicit reference on it.
3296 * Parameters: p Process containing fd
3297 * fd fd to obtain fileproc for
3298 * resultfp Pointer to pointer return area
3300 * Returns: 0 Success
3304 * *resultfp (modified) Pointer to fileproc pointer
3306 * Locks: Assumes proc_fdlock for process is held by the caller
3308 * Notes: Because there is no reference explicitly taken, the returned
3309 * fileproc pointer is only valid so long as the proc_fdlock
3310 * remains held by the caller.
3313 fdgetf_noref(proc_t p
, int fd
, struct fileproc
**resultfp
)
3315 struct filedesc
*fdp
= p
->p_fd
;
3316 struct fileproc
*fp
;
3318 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3319 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3320 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3332 * Description: Get fileproc and vnode pointer for a given fd from the per
3333 * process open file table of the specified process, and if
3334 * successful, increment the f_iocount
3336 * Parameters: p Process in which fd lives
3337 * fd fd to get information for
3338 * resultfp Pointer to result fileproc
3339 * pointer area, or 0 if none
3340 * resultvp Pointer to result vnode pointer
3341 * area, or 0 if none
3343 * Returns: 0 Success
3344 * EBADF Bad file descriptor
3345 * ENOTSUP fd does not refer to a vnode
3348 * *resultfp (modified) Fileproc pointer
3349 * *resultvp (modified) vnode pointer
3351 * Notes: The resultfp and resultvp fields are optional, and may be
3352 * independently specified as NULL to skip returning information
3354 * Locks: Internally takes and releases proc_fdlock
3357 fp_getfvp(proc_t p
, int fd
, struct fileproc
**resultfp
, struct vnode
**resultvp
)
3359 struct filedesc
*fdp
= p
->p_fd
;
3360 struct fileproc
*fp
;
3362 proc_fdlock_spin(p
);
3363 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3364 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3365 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3369 if (fp
->f_type
!= DTYPE_VNODE
) {
3378 *resultvp
= (struct vnode
*)fp
->f_data
;
3388 * Description: Get fileproc, vnode pointer, and vid for a given fd from the
3389 * per process open file table of the specified process, and if
3390 * successful, increment the f_iocount
3392 * Parameters: p Process in which fd lives
3393 * fd fd to get information for
3394 * resultfp Pointer to result fileproc
3395 * pointer area, or 0 if none
3396 * resultvp Pointer to result vnode pointer
3397 * area, or 0 if none
3398 * vidp Pointer to resuld vid area
3400 * Returns: 0 Success
3401 * EBADF Bad file descriptor
3402 * ENOTSUP fd does not refer to a vnode
3405 * *resultfp (modified) Fileproc pointer
3406 * *resultvp (modified) vnode pointer
3409 * Notes: The resultfp and resultvp fields are optional, and may be
3410 * independently specified as NULL to skip returning information
3412 * Locks: Internally takes and releases proc_fdlock
3415 fp_getfvpandvid(proc_t p
, int fd
, struct fileproc
**resultfp
,
3416 struct vnode
**resultvp
, uint32_t *vidp
)
3418 struct filedesc
*fdp
= p
->p_fd
;
3419 struct fileproc
*fp
;
3421 proc_fdlock_spin(p
);
3422 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3423 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3424 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3428 if (fp
->f_type
!= DTYPE_VNODE
) {
3437 *resultvp
= (struct vnode
*)fp
->f_data
;
3439 *vidp
= (uint32_t)vnode_vid((struct vnode
*)fp
->f_data
);
3449 * Description: Get fileproc and socket pointer for a given fd from the
3450 * per process open file table of the specified process, and if
3451 * successful, increment the f_iocount
3453 * Parameters: p Process in which fd lives
3454 * fd fd to get information for
3455 * resultfp Pointer to result fileproc
3456 * pointer area, or 0 if none
3457 * results Pointer to result socket
3458 * pointer area, or 0 if none
3460 * Returns: EBADF The file descriptor is invalid
3461 * EOPNOTSUPP The file descriptor is not a socket
3465 * *resultfp (modified) Fileproc pointer
3466 * *results (modified) socket pointer
3468 * Notes: EOPNOTSUPP should probably be ENOTSOCK; this function is only
3469 * ever called from accept1().
3472 fp_getfsock(proc_t p
, int fd
, struct fileproc
**resultfp
,
3473 struct socket
**results
)
3475 struct filedesc
*fdp
= p
->p_fd
;
3476 struct fileproc
*fp
;
3478 proc_fdlock_spin(p
);
3479 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3480 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3481 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3485 if (fp
->f_type
!= DTYPE_SOCKET
) {
3494 *results
= (struct socket
*)fp
->f_data
;
3504 * Description: Get fileproc and kqueue pointer for a given fd from the
3505 * per process open file table of the specified process, and if
3506 * successful, increment the f_iocount
3508 * Parameters: p Process in which fd lives
3509 * fd fd to get information for
3510 * resultfp Pointer to result fileproc
3511 * pointer area, or 0 if none
3512 * resultkq Pointer to result kqueue
3513 * pointer area, or 0 if none
3515 * Returns: EBADF The file descriptor is invalid
3516 * EBADF The file descriptor is not a socket
3520 * *resultfp (modified) Fileproc pointer
3521 * *resultkq (modified) kqueue pointer
3523 * Notes: The second EBADF should probably be something else to make
3524 * the error condition distinct.
3527 fp_getfkq(proc_t p
, int fd
, struct fileproc
**resultfp
,
3528 struct kqueue
**resultkq
)
3530 struct filedesc
*fdp
= p
->p_fd
;
3531 struct fileproc
*fp
;
3533 proc_fdlock_spin(p
);
3534 if ( fd
< 0 || fd
>= fdp
->fd_nfiles
||
3535 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3536 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3540 if (fp
->f_type
!= DTYPE_KQUEUE
) {
3549 *resultkq
= (struct kqueue
*)fp
->f_data
;
3559 * Description: Get fileproc and POSIX shared memory pointer for a given fd
3560 * from the per process open file table of the specified process
3561 * and if successful, increment the f_iocount
3563 * Parameters: p Process in which fd lives
3564 * fd fd to get information for
3565 * resultfp Pointer to result fileproc
3566 * pointer area, or 0 if none
3567 * resultpshm Pointer to result POSIX
3568 * shared memory pointer
3569 * pointer area, or 0 if none
3571 * Returns: EBADF The file descriptor is invalid
3572 * EBADF The file descriptor is not a POSIX
3573 * shared memory area
3577 * *resultfp (modified) Fileproc pointer
3578 * *resultpshm (modified) POSIX shared memory pointer
3580 * Notes: The second EBADF should probably be something else to make
3581 * the error condition distinct.
3584 fp_getfpshm(proc_t p
, int fd
, struct fileproc
**resultfp
,
3585 struct pshmnode
**resultpshm
)
3587 struct filedesc
*fdp
= p
->p_fd
;
3588 struct fileproc
*fp
;
3590 proc_fdlock_spin(p
);
3591 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3592 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3593 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3597 if (fp
->f_type
!= DTYPE_PSXSHM
) {
3607 *resultpshm
= (struct pshmnode
*)fp
->f_data
;
3617 * Description: Get fileproc and POSIX semaphore pointer for a given fd from
3618 * the per process open file table of the specified process
3619 * and if successful, increment the f_iocount
3621 * Parameters: p Process in which fd lives
3622 * fd fd to get information for
3623 * resultfp Pointer to result fileproc
3624 * pointer area, or 0 if none
3625 * resultpsem Pointer to result POSIX
3626 * semaphore pointer area, or
3629 * Returns: EBADF The file descriptor is invalid
3630 * EBADF The file descriptor is not a POSIX
3635 * *resultfp (modified) Fileproc pointer
3636 * *resultpsem (modified) POSIX semaphore pointer
3638 * Notes: The second EBADF should probably be something else to make
3639 * the error condition distinct.
3641 * In order to support unnamed POSIX semaphores, the named
3642 * POSIX semaphores will have to move out of the per-process
3643 * open filetable, and into a global table that is shared with
3644 * unnamed POSIX semaphores, since unnamed POSIX semaphores
3645 * are typically used by declaring instances in shared memory,
3646 * and there's no other way to do this without changing the
3647 * underlying type, which would introduce binary compatibility
3651 fp_getfpsem(proc_t p
, int fd
, struct fileproc
**resultfp
,
3652 struct psemnode
**resultpsem
)
3654 struct filedesc
*fdp
= p
->p_fd
;
3655 struct fileproc
*fp
;
3657 proc_fdlock_spin(p
);
3658 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3659 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3660 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3664 if (fp
->f_type
!= DTYPE_PSXSEM
) {
3673 *resultpsem
= (struct psemnode
*)fp
->f_data
;
3683 * Description: Get fileproc and pipe pointer for a given fd from the
3684 * per process open file table of the specified process
3685 * and if successful, increment the f_iocount
3687 * Parameters: p Process in which fd lives
3688 * fd fd to get information for
3689 * resultfp Pointer to result fileproc
3690 * pointer area, or 0 if none
3691 * resultpipe Pointer to result pipe
3692 * pointer area, or 0 if none
3694 * Returns: EBADF The file descriptor is invalid
3695 * EBADF The file descriptor is not a socket
3699 * *resultfp (modified) Fileproc pointer
3700 * *resultpipe (modified) pipe pointer
3702 * Notes: The second EBADF should probably be something else to make
3703 * the error condition distinct.
3706 fp_getfpipe(proc_t p
, int fd
, struct fileproc
**resultfp
,
3707 struct pipe
**resultpipe
)
3709 struct filedesc
*fdp
= p
->p_fd
;
3710 struct fileproc
*fp
;
3712 proc_fdlock_spin(p
);
3713 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3714 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3715 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3719 if (fp
->f_type
!= DTYPE_PIPE
) {
3728 *resultpipe
= (struct pipe
*)fp
->f_data
;
3737 * Description: Get fileproc pointer for a given fd from the per process
3738 * open file table of the specified process and if successful,
3739 * increment the f_iocount
3741 * Parameters: p Process in which fd lives
3742 * fd fd to get information for
3743 * resultfp Pointer to result fileproc
3744 * pointer area, or 0 if none
3745 * locked !0 if the caller holds the
3746 * proc_fdlock, 0 otherwise
3748 * Returns: 0 Success
3749 * EBADF Bad file descriptor
3752 * *resultfp (modified) Fileproc pointer
3754 * Locks: If the argument 'locked' is non-zero, then the caller is
3755 * expected to have taken and held the proc_fdlock; if it is
3756 * zero, than this routine internally takes and drops this lock.
3759 fp_lookup(proc_t p
, int fd
, struct fileproc
**resultfp
, int locked
)
3761 struct filedesc
*fdp
= p
->p_fd
;
3762 struct fileproc
*fp
;
3765 proc_fdlock_spin(p
);
3766 if (fd
< 0 || fdp
== NULL
|| fd
>= fdp
->fd_nfiles
||
3767 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3768 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3787 * Description: Swap the fileproc pointer for a given fd with a new
3788 * fileproc pointer in the per-process open file table of
3789 * the specified process. The fdlock must be held at entry.
3791 * Parameters: p Process containing the fd
3792 * fd The fd of interest
3793 * nfp Pointer to the newfp
3795 * Returns: 0 Success
3796 * EBADF Bad file descriptor
3798 * EKEEPLOOKING f_iocount changed while lock was dropped.
3801 fp_tryswap(proc_t p
, int fd
, struct fileproc
*nfp
)
3803 struct fileproc
*fp
;
3806 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
3808 if (0 != (error
= fp_lookup(p
, fd
, &fp
, 1)))
3811 * At this point, our caller (change_guardedfd_np) has
3812 * one f_iocount reference, and we just took another
3813 * one to begin the replacement.
3815 if (fp
->f_iocount
< 2) {
3816 panic("f_iocount too small %d", fp
->f_iocount
);
3817 } else if (2 == fp
->f_iocount
) {
3819 /* Copy the contents of *fp, preserving the "type" of *nfp */
3821 nfp
->f_flags
= (nfp
->f_flags
& FP_TYPEMASK
) |
3822 (fp
->f_flags
& ~FP_TYPEMASK
);
3823 nfp
->f_iocount
= fp
->f_iocount
;
3824 nfp
->f_fglob
= fp
->f_fglob
;
3825 nfp
->f_waddr
= fp
->f_waddr
;
3827 p
->p_fd
->fd_ofiles
[fd
] = nfp
;
3828 (void) fp_drop(p
, fd
, nfp
, 1);
3831 * Wait for all other active references to evaporate.
3833 p
->p_fpdrainwait
= 1;
3834 error
= msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
,
3835 PRIBIO
| PCATCH
, "tryswap fpdrain", NULL
);
3838 * Return an "internal" errno to trigger a full
3839 * reevaluation of the change-guard attempt.
3841 error
= EKEEPLOOKING
;
3842 printf("%s: lookup collision fd %d\n", __func__
, fd
);
3844 (void) fp_drop(p
, fd
, fp
, 1);
3853 * Description: Set the FP_WRITTEN flag on the fileproc and drop the I/O
3854 * reference previously taken by calling fp_lookup et. al.
3856 * Parameters: p Process in which the fd lives
3857 * fd fd associated with the fileproc
3858 * fp fileproc on which to set the
3859 * flag and drop the reference
3861 * Returns: 0 Success
3862 * fp_drop:EBADF Bad file descriptor
3864 * Locks: This function internally takes and drops the proc_fdlock for
3865 * the supplied process
3867 * Notes: The fileproc must correspond to the fd in the supplied proc
3870 fp_drop_written(proc_t p
, int fd
, struct fileproc
*fp
)
3874 proc_fdlock_spin(p
);
3876 fp
->f_flags
|= FP_WRITTEN
;
3878 error
= fp_drop(p
, fd
, fp
, 1);
3889 * Description: Set the FP_WAITEVENT flag on the fileproc and drop the I/O
3890 * reference previously taken by calling fp_lookup et. al.
3892 * Parameters: p Process in which the fd lives
3893 * fd fd associated with the fileproc
3894 * fp fileproc on which to set the
3895 * flag and drop the reference
3897 * Returns: 0 Success
3898 * fp_drop:EBADF Bad file descriptor
3900 * Locks: This function internally takes and drops the proc_fdlock for
3901 * the supplied process
3903 * Notes: The fileproc must correspond to the fd in the supplied proc
3906 fp_drop_event(proc_t p
, int fd
, struct fileproc
*fp
)
3910 proc_fdlock_spin(p
);
3912 fp
->f_flags
|= FP_WAITEVENT
;
3914 error
= fp_drop(p
, fd
, fp
, 1);
3925 * Description: Drop the I/O reference previously taken by calling fp_lookup
3928 * Parameters: p Process in which the fd lives
3929 * fd fd associated with the fileproc
3930 * fp fileproc on which to set the
3931 * flag and drop the reference
3932 * locked flag to internally take and
3933 * drop proc_fdlock if it is not
3934 * already held by the caller
3936 * Returns: 0 Success
3937 * EBADF Bad file descriptor
3939 * Locks: This function internally takes and drops the proc_fdlock for
3940 * the supplied process if 'locked' is non-zero, and assumes that
3941 * the caller already holds this lock if 'locked' is non-zero.
3943 * Notes: The fileproc must correspond to the fd in the supplied proc
3946 fp_drop(proc_t p
, int fd
, struct fileproc
*fp
, int locked
)
3948 struct filedesc
*fdp
= p
->p_fd
;
3952 proc_fdlock_spin(p
);
3953 if ((fp
== FILEPROC_NULL
) && (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3954 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3955 ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
3956 !(fdp
->fd_ofileflags
[fd
] & UF_CLOSING
)))) {
3963 if (fp
->f_iocount
== 0) {
3964 if (fp
->f_flags
& FP_SELCONFLICT
)
3965 fp
->f_flags
&= ~FP_SELCONFLICT
;
3967 if (p
->p_fpdrainwait
) {
3968 p
->p_fpdrainwait
= 0;
3975 wakeup(&p
->p_fpdrainwait
);
3984 * Description: Given an fd, look it up in the current process's per process
3985 * open file table, and return its internal vnode pointer.
3987 * Parameters: fd fd to obtain vnode from
3988 * vpp pointer to vnode return area
3990 * Returns: 0 Success
3991 * EINVAL The fd does not refer to a
3992 * vnode fileproc entry
3993 * fp_lookup:EBADF Bad file descriptor
3996 * *vpp (modified) Returned vnode pointer
3998 * Locks: This function internally takes and drops the proc_fdlock for
3999 * the current process
4001 * Notes: If successful, this function increments the f_iocount on the
4002 * fd's corresponding fileproc.
4004 * The fileproc referenced is not returned; because of this, care
4005 * must be taken to not drop the last reference (e.g. by closing
4006 * the file). This is inherently unsafe, since the reference may
4007 * not be recoverable from the vnode, if there is a subsequent
4008 * close that destroys the associate fileproc. The caller should
4009 * therefore retain their own reference on the fileproc so that
4010 * the f_iocount can be dropped subsequently. Failure to do this
4011 * can result in the returned pointer immediately becoming invalid
4012 * following the call.
4014 * Use of this function is discouraged.
4017 file_vnode(int fd
, struct vnode
**vpp
)
4019 proc_t p
= current_proc();
4020 struct fileproc
*fp
;
4023 proc_fdlock_spin(p
);
4024 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4028 if (fp
->f_type
!= DTYPE_VNODE
) {
4029 fp_drop(p
, fd
, fp
,1);
4034 *vpp
= (struct vnode
*)fp
->f_data
;
4042 * file_vnode_withvid
4044 * Description: Given an fd, look it up in the current process's per process
4045 * open file table, and return its internal vnode pointer.
4047 * Parameters: fd fd to obtain vnode from
4048 * vpp pointer to vnode return area
4049 * vidp pointer to vid of the returned vnode
4051 * Returns: 0 Success
4052 * EINVAL The fd does not refer to a
4053 * vnode fileproc entry
4054 * fp_lookup:EBADF Bad file descriptor
4057 * *vpp (modified) Returned vnode pointer
4059 * Locks: This function internally takes and drops the proc_fdlock for
4060 * the current process
4062 * Notes: If successful, this function increments the f_iocount on the
4063 * fd's corresponding fileproc.
4065 * The fileproc referenced is not returned; because of this, care
4066 * must be taken to not drop the last reference (e.g. by closing
4067 * the file). This is inherently unsafe, since the reference may
4068 * not be recoverable from the vnode, if there is a subsequent
4069 * close that destroys the associate fileproc. The caller should
4070 * therefore retain their own reference on the fileproc so that
4071 * the f_iocount can be dropped subsequently. Failure to do this
4072 * can result in the returned pointer immediately becoming invalid
4073 * following the call.
4075 * Use of this function is discouraged.
4078 file_vnode_withvid(int fd
, struct vnode
**vpp
, uint32_t * vidp
)
4080 proc_t p
= current_proc();
4081 struct fileproc
*fp
;
4085 proc_fdlock_spin(p
);
4086 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4090 if (fp
->f_type
!= DTYPE_VNODE
) {
4091 fp_drop(p
, fd
, fp
,1);
4095 vp
= (struct vnode
*)fp
->f_data
;
4099 if ((vidp
!= NULL
) && (vp
!= NULLVP
))
4100 *vidp
= (uint32_t)vp
->v_id
;
4111 * Description: Given an fd, look it up in the current process's per process
4112 * open file table, and return its internal socket pointer.
4114 * Parameters: fd fd to obtain vnode from
4115 * sp pointer to socket return area
4117 * Returns: 0 Success
4118 * ENOTSOCK Not a socket
4119 * fp_lookup:EBADF Bad file descriptor
4122 * *sp (modified) Returned socket pointer
4124 * Locks: This function internally takes and drops the proc_fdlock for
4125 * the current process
4127 * Notes: If successful, this function increments the f_iocount on the
4128 * fd's corresponding fileproc.
4130 * The fileproc referenced is not returned; because of this, care
4131 * must be taken to not drop the last reference (e.g. by closing
4132 * the file). This is inherently unsafe, since the reference may
4133 * not be recoverable from the socket, if there is a subsequent
4134 * close that destroys the associate fileproc. The caller should
4135 * therefore retain their own reference on the fileproc so that
4136 * the f_iocount can be dropped subsequently. Failure to do this
4137 * can result in the returned pointer immediately becoming invalid
4138 * following the call.
4140 * Use of this function is discouraged.
4143 file_socket(int fd
, struct socket
**sp
)
4145 proc_t p
= current_proc();
4146 struct fileproc
*fp
;
4149 proc_fdlock_spin(p
);
4150 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4154 if (fp
->f_type
!= DTYPE_SOCKET
) {
4155 fp_drop(p
, fd
, fp
,1);
4159 *sp
= (struct socket
*)fp
->f_data
;
4169 * Description: Given an fd, look it up in the current process's per process
4170 * open file table, and return its fileproc's flags field.
4172 * Parameters: fd fd whose flags are to be
4174 * flags pointer to flags data area
4176 * Returns: 0 Success
4177 * ENOTSOCK Not a socket
4178 * fp_lookup:EBADF Bad file descriptor
4181 * *flags (modified) Returned flags field
4183 * Locks: This function internally takes and drops the proc_fdlock for
4184 * the current process
4186 * Notes: This function will internally increment and decrement the
4187 * f_iocount of the fileproc as part of its operation.
4190 file_flags(int fd
, int *flags
)
4193 proc_t p
= current_proc();
4194 struct fileproc
*fp
;
4197 proc_fdlock_spin(p
);
4198 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
4202 *flags
= (int)fp
->f_flag
;
4203 fp_drop(p
, fd
, fp
,1);
4213 * Description: Drop an iocount reference on an fd, and wake up any waiters
4214 * for draining (i.e. blocked in fileproc_drain() called during
4215 * the last attempt to close a file).
4217 * Parameters: fd fd on which an ioreference is
4220 * Returns: 0 Success
4221 * EBADF Bad file descriptor
4223 * Description: Given an fd, look it up in the current process's per process
4224 * open file table, and drop it's fileproc's f_iocount by one
4226 * Notes: This is intended as a corresponding operation to the functions
4227 * file_vnode() and file_socket() operations.
4229 * Technically, the close reference is supposed to be protected
4230 * by a fileproc_drain(), however, a drain will only block if
4231 * the fd refers to a character device, and that device has had
4232 * preparefileread() called on it. If it refers to something
4233 * other than a character device, then the drain will occur and
4234 * block each close attempt, rather than merely the last close.
4236 * Since it's possible for an fd that refers to a character
4237 * device to have an intermediate close followed by an open to
4238 * cause a different file to correspond to that descriptor,
4239 * unless there was a cautionary reference taken on the fileproc,
4240 * this is an inherently unsafe function. This happens in the
4241 * case where multiple fd's in a process refer to the same
4242 * character device (e.g. stdin/out/err pointing to a tty, etc.).
4244 * Use of this function is discouraged.
4249 struct fileproc
*fp
;
4250 proc_t p
= current_proc();
4253 proc_fdlock_spin(p
);
4254 if (fd
< 0 || fd
>= p
->p_fd
->fd_nfiles
||
4255 (fp
= p
->p_fd
->fd_ofiles
[fd
]) == NULL
||
4256 ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
4257 !(p
->p_fd
->fd_ofileflags
[fd
] & UF_CLOSING
))) {
4263 if (fp
->f_iocount
== 0) {
4264 if (fp
->f_flags
& FP_SELCONFLICT
)
4265 fp
->f_flags
&= ~FP_SELCONFLICT
;
4267 if (p
->p_fpdrainwait
) {
4268 p
->p_fpdrainwait
= 0;
4275 wakeup(&p
->p_fpdrainwait
);
4280 static int falloc_withalloc_locked(proc_t
, struct fileproc
**, int *,
4281 vfs_context_t
, struct fileproc
* (*)(void *), void *, int);
4286 * Description: Allocate an entry in the per process open file table and
4287 * return the corresponding fileproc and fd.
4289 * Parameters: p The process in whose open file
4290 * table the fd is to be allocated
4291 * resultfp Pointer to fileproc pointer
4293 * resultfd Pointer to fd return area
4296 * Returns: 0 Success
4297 * falloc:ENFILE Too many open files in system
4298 * falloc:EMFILE Too many open files in process
4299 * falloc:ENOMEM M_FILEPROC or M_FILEGLOB zone
4303 * *resultfd (modified) Returned fileproc pointer
4304 * *resultfd (modified) Returned fd
4306 * Locks: This function takes and drops the proc_fdlock; if this lock
4307 * is already held, use falloc_locked() instead.
4309 * Notes: This function takes separate process and context arguments
4310 * solely to support kern_exec.c; otherwise, it would take
4311 * neither, and expect falloc_locked() to use the
4312 * vfs_context_current() routine internally.
4315 falloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
)
4317 return (falloc_withalloc(p
, resultfp
, resultfd
, ctx
,
4318 fileproc_alloc_init
, NULL
));
4322 * Like falloc, but including the fileproc allocator and create-args
4325 falloc_withalloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4326 vfs_context_t ctx
, fp_allocfn_t fp_zalloc
, void *arg
)
4331 error
= falloc_withalloc_locked(p
,
4332 resultfp
, resultfd
, ctx
, fp_zalloc
, arg
, 1);
4339 * "uninitialized" ops -- ensure fg->fg_ops->fo_type always exists
4341 static const struct fileops uninitops
;
4346 * Create a new open file structure and allocate
4347 * a file descriptor for the process that refers to it.
4349 * Returns: 0 Success
4351 * Description: Allocate an entry in the per process open file table and
4352 * return the corresponding fileproc and fd.
4354 * Parameters: p The process in whose open file
4355 * table the fd is to be allocated
4356 * resultfp Pointer to fileproc pointer
4358 * resultfd Pointer to fd return area
4360 * locked Flag to indicate whether the
4361 * caller holds proc_fdlock
4363 * Returns: 0 Success
4364 * ENFILE Too many open files in system
4365 * fdalloc:EMFILE Too many open files in process
4366 * ENOMEM M_FILEPROC or M_FILEGLOB zone
4371 * *resultfd (modified) Returned fileproc pointer
4372 * *resultfd (modified) Returned fd
4374 * Locks: If the parameter 'locked' is zero, this function takes and
4375 * drops the proc_fdlock; if non-zero, the caller must hold the
4378 * Notes: If you intend to use a non-zero 'locked' parameter, use the
4379 * utility function falloc() instead.
4381 * This function takes separate process and context arguments
4382 * solely to support kern_exec.c; otherwise, it would take
4383 * neither, and use the vfs_context_current() routine internally.
4386 falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4387 vfs_context_t ctx
, int locked
)
4389 return (falloc_withalloc_locked(p
, resultfp
, resultfd
, ctx
,
4390 fileproc_alloc_init
, NULL
, locked
));
4394 falloc_withalloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
4395 vfs_context_t ctx
, fp_allocfn_t fp_zalloc
, void *crarg
,
4398 struct fileproc
*fp
;
4399 struct fileglob
*fg
;
4404 if ( (error
= fdalloc(p
, 0, &nfd
)) ) {
4409 if (nfiles
>= maxfiles
) {
4416 error
= mac_file_check_create(proc_ucred(p
));
4425 * Allocate a new file descriptor.
4426 * If the process has file descriptor zero open, add to the list
4427 * of open files at that point, otherwise put it at the front of
4428 * the list of open files.
4432 fp
= (*fp_zalloc
)(crarg
);
4438 MALLOC_ZONE(fg
, struct fileglob
*, sizeof(struct fileglob
), M_FILEGLOB
, M_WAITOK
);
4445 bzero(fg
, sizeof(struct fileglob
));
4446 lck_mtx_init(&fg
->fg_lock
, file_lck_grp
, file_lck_attr
);
4450 fg
->fg_ops
= &uninitops
;
4453 mac_file_label_init(fg
);
4456 kauth_cred_ref(ctx
->vc_ucred
);
4460 fp
->f_cred
= ctx
->vc_ucred
;
4463 mac_file_label_associate(fp
->f_cred
, fg
);
4466 OSAddAtomic(1, &nfiles
);
4468 p
->p_fd
->fd_ofiles
[nfd
] = fp
;
4485 * Description: Free a file structure; drop the global open file count, and
4486 * drop the credential reference, if the fileglob has one, and
4487 * destroy the instance mutex before freeing
4489 * Parameters: fg Pointer to fileglob to be
4495 fg_free(struct fileglob
*fg
)
4497 OSAddAtomic(-1, &nfiles
);
4499 if (fg
->fg_vn_data
) {
4500 fg_vn_data_free(fg
->fg_vn_data
);
4501 fg
->fg_vn_data
= NULL
;
4504 if (IS_VALID_CRED(fg
->fg_cred
)) {
4505 kauth_cred_unref(&fg
->fg_cred
);
4507 lck_mtx_destroy(&fg
->fg_lock
, file_lck_grp
);
4510 mac_file_label_destroy(fg
);
4512 FREE_ZONE(fg
, sizeof *fg
, M_FILEGLOB
);
4519 * Description: Perform close-on-exec processing for all files in a process
4520 * that are either marked as close-on-exec, or which were in the
4521 * process of being opened at the time of the execve
4523 * Also handles the case (via posix_spawn()) where -all-
4524 * files except those marked with "inherit" as treated as
4527 * Parameters: p Pointer to process calling
4532 * Locks: This function internally takes and drops proc_fdlock()
4536 fdexec(proc_t p
, short flags
)
4538 struct filedesc
*fdp
= p
->p_fd
;
4540 boolean_t cloexec_default
= (flags
& POSIX_SPAWN_CLOEXEC_DEFAULT
) != 0;
4543 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
4545 struct fileproc
*fp
= fdp
->fd_ofiles
[i
];
4546 char *flagp
= &fdp
->fd_ofileflags
[i
];
4548 if (fp
&& cloexec_default
) {
4550 * Reverse the usual semantics of file descriptor
4551 * inheritance - all of them should be closed
4552 * except files marked explicitly as "inherit" and
4553 * not marked close-on-exec.
4555 if ((*flagp
& (UF_EXCLOSE
|UF_INHERIT
)) != UF_INHERIT
)
4556 *flagp
|= UF_EXCLOSE
;
4557 *flagp
&= ~UF_INHERIT
;
4561 ((*flagp
& (UF_RESERVED
|UF_EXCLOSE
)) == UF_EXCLOSE
)
4563 || (fp
&& mac_file_check_inherit(proc_ucred(p
), fp
->f_fglob
))
4566 if (i
< fdp
->fd_knlistsize
)
4567 knote_fdclose(p
, i
);
4568 procfdtbl_clearfd(p
, i
);
4569 if (i
== fdp
->fd_lastfile
&& i
> 0)
4571 if (i
< fdp
->fd_freefile
)
4572 fdp
->fd_freefile
= i
;
4575 * Wait for any third party viewers (e.g., lsof)
4576 * to release their references to this fileproc.
4578 while (fp
->f_iocount
> 0) {
4579 p
->p_fpdrainwait
= 1;
4580 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
,
4584 closef_locked(fp
, fp
->f_fglob
, p
);
4596 * Description: Copy a filedesc structure. This is normally used as part of
4597 * forkproc() when forking a new process, to copy the per process
4598 * open file table over to the new process.
4600 * Parameters: p Process whose open file table
4601 * is to be copied (parent)
4602 * uth_cdir Per thread current working
4603 * cirectory, or NULL
4605 * Returns: NULL Copy failed
4606 * !NULL Pointer to new struct filedesc
4608 * Locks: This function internally takes and drops proc_fdlock()
4610 * Notes: Files are copied directly, ignoring the new resource limits
4611 * for the process that's being copied into. Since the descriptor
4612 * references are just additional references, this does not count
4613 * against the number of open files on the system.
4615 * The struct filedesc includes the current working directory,
4616 * and the current root directory, if the process is chroot'ed.
4618 * If the exec was called by a thread using a per thread current
4619 * working directory, we inherit the working directory from the
4620 * thread making the call, rather than from the process.
4622 * In the case of a failure to obtain a reference, for most cases,
4623 * the file entry will be silently dropped. There's an exception
4624 * for the case of a chroot dir, since a failure to to obtain a
4625 * reference there would constitute an "escape" from the chroot
4626 * environment, which must not be allowed. In that case, we will
4627 * deny the execve() operation, rather than allowing the escape.
4630 fdcopy(proc_t p
, vnode_t uth_cdir
)
4632 struct filedesc
*newfdp
, *fdp
= p
->p_fd
;
4634 struct fileproc
*ofp
, *fp
;
4637 MALLOC_ZONE(newfdp
, struct filedesc
*,
4638 sizeof(*newfdp
), M_FILEDESC
, M_WAITOK
);
4645 * the FD_CHROOT flag will be inherited via this copy
4647 (void) memcpy(newfdp
, fdp
, sizeof(*newfdp
));
4650 * If we are running with per-thread current working directories,
4651 * inherit the new current working directory from the current thread
4652 * instead, before we take our references.
4654 if (uth_cdir
!= NULLVP
)
4655 newfdp
->fd_cdir
= uth_cdir
;
4658 * For both fd_cdir and fd_rdir make sure we get
4659 * a valid reference... if we can't, than set
4660 * set the pointer(s) to NULL in the child... this
4661 * will keep us from using a non-referenced vp
4662 * and allows us to do the vnode_rele only on
4663 * a properly referenced vp
4665 if ( (v_dir
= newfdp
->fd_cdir
) ) {
4666 if (vnode_getwithref(v_dir
) == 0) {
4667 if ( (vnode_ref(v_dir
)) )
4668 newfdp
->fd_cdir
= NULL
;
4671 newfdp
->fd_cdir
= NULL
;
4673 if (newfdp
->fd_cdir
== NULL
&& fdp
->fd_cdir
) {
4675 * we couldn't get a new reference on
4676 * the current working directory being
4677 * inherited... we might as well drop
4678 * our reference from the parent also
4679 * since the vnode has gone DEAD making
4680 * it useless... by dropping it we'll
4681 * be that much closer to recycling it
4683 vnode_rele(fdp
->fd_cdir
);
4684 fdp
->fd_cdir
= NULL
;
4687 if ( (v_dir
= newfdp
->fd_rdir
) ) {
4688 if (vnode_getwithref(v_dir
) == 0) {
4689 if ( (vnode_ref(v_dir
)) )
4690 newfdp
->fd_rdir
= NULL
;
4693 newfdp
->fd_rdir
= NULL
;
4696 /* Coming from a chroot environment and unable to get a reference... */
4697 if (newfdp
->fd_rdir
== NULL
&& fdp
->fd_rdir
) {
4699 * We couldn't get a new reference on
4700 * the chroot directory being
4701 * inherited... this is fatal, since
4702 * otherwise it would constitute an
4703 * escape from a chroot environment by
4706 if (newfdp
->fd_cdir
)
4707 vnode_rele(newfdp
->fd_cdir
);
4708 FREE_ZONE(newfdp
, sizeof *newfdp
, M_FILEDESC
);
4713 * If the number of open files fits in the internal arrays
4714 * of the open file structure, use them, otherwise allocate
4715 * additional memory for the number of descriptors currently
4718 if (newfdp
->fd_lastfile
< NDFILE
)
4722 * Compute the smallest multiple of NDEXTENT needed
4723 * for the file descriptors currently in use,
4724 * allowing the table to shrink.
4726 i
= newfdp
->fd_nfiles
;
4727 while (i
> 2 * NDEXTENT
&& i
> newfdp
->fd_lastfile
* 2)
4732 MALLOC_ZONE(newfdp
->fd_ofiles
, struct fileproc
**,
4733 i
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
4734 if (newfdp
->fd_ofiles
== NULL
) {
4735 if (newfdp
->fd_cdir
)
4736 vnode_rele(newfdp
->fd_cdir
);
4737 if (newfdp
->fd_rdir
)
4738 vnode_rele(newfdp
->fd_rdir
);
4740 FREE_ZONE(newfdp
, sizeof(*newfdp
), M_FILEDESC
);
4743 (void) memset(newfdp
->fd_ofiles
, 0, i
* OFILESIZE
);
4746 newfdp
->fd_ofileflags
= (char *) &newfdp
->fd_ofiles
[i
];
4747 newfdp
->fd_nfiles
= i
;
4749 if (fdp
->fd_nfiles
> 0) {
4750 struct fileproc
**fpp
;
4753 (void) memcpy(newfdp
->fd_ofiles
, fdp
->fd_ofiles
,
4754 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofiles
));
4755 (void) memcpy(newfdp
->fd_ofileflags
, fdp
->fd_ofileflags
,
4756 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofileflags
));
4759 * kq descriptors cannot be copied.
4761 if (newfdp
->fd_knlistsize
!= -1) {
4762 fpp
= &newfdp
->fd_ofiles
[newfdp
->fd_lastfile
];
4763 flags
= &newfdp
->fd_ofileflags
[newfdp
->fd_lastfile
];
4764 for (i
= newfdp
->fd_lastfile
;
4765 i
>= 0; i
--, fpp
--, flags
--) {
4766 if (*flags
& UF_RESERVED
)
4767 continue; /* (removed below) */
4768 if (*fpp
!= NULL
&& (*fpp
)->f_type
== DTYPE_KQUEUE
) {
4771 if (i
< newfdp
->fd_freefile
)
4772 newfdp
->fd_freefile
= i
;
4774 if (*fpp
== NULL
&& i
== newfdp
->fd_lastfile
&& i
> 0)
4775 newfdp
->fd_lastfile
--;
4777 newfdp
->fd_knlist
= NULL
;
4778 newfdp
->fd_knlistsize
= -1;
4779 newfdp
->fd_knhash
= NULL
;
4780 newfdp
->fd_knhashmask
= 0;
4782 fpp
= newfdp
->fd_ofiles
;
4783 flags
= newfdp
->fd_ofileflags
;
4785 for (i
= newfdp
->fd_lastfile
+ 1; --i
>= 0; fpp
++, flags
++)
4786 if ((ofp
= *fpp
) != NULL
&&
4787 0 == (*flags
& (UF_FORKCLOSE
|UF_RESERVED
))) {
4789 if (FILEPROC_TYPE(ofp
) != FTYPE_SIMPLE
)
4790 panic("complex fileproc");
4792 fp
= fileproc_alloc_init(NULL
);
4795 * XXX no room to copy, unable to
4796 * XXX safely unwind state at present
4801 (ofp
->f_flags
& ~FP_TYPEMASK
);
4802 fp
->f_fglob
= ofp
->f_fglob
;
4807 if (i
< newfdp
->fd_freefile
)
4808 newfdp
->fd_freefile
= i
;
4822 * Description: Release a filedesc (per process open file table) structure;
4823 * this is done on process exit(), or from forkproc_free() if
4824 * the fork fails for some reason subsequent to a successful
4827 * Parameters: p Pointer to process going away
4831 * Locks: This function internally takes and drops proc_fdlock()
4836 struct filedesc
*fdp
;
4837 struct fileproc
*fp
;
4842 if (p
== kernproc
|| NULL
== (fdp
= p
->p_fd
)) {
4847 extern struct filedesc filedesc0
;
4849 if (&filedesc0
== fdp
)
4852 if (fdp
->fd_nfiles
> 0 && fdp
->fd_ofiles
) {
4853 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
4854 if ((fp
= fdp
->fd_ofiles
[i
]) != NULL
) {
4856 if (fdp
->fd_ofileflags
[i
] & UF_RESERVED
)
4857 panic("fdfree: found fp with UF_RESERVED");
4859 procfdtbl_reservefd(p
, i
);
4861 if (i
< fdp
->fd_knlistsize
)
4862 knote_fdclose(p
, i
);
4863 if (fp
->f_flags
& FP_WAITEVENT
)
4864 (void)waitevent_close(p
, fp
);
4865 (void) closef_locked(fp
, fp
->f_fglob
, p
);
4869 FREE_ZONE(fdp
->fd_ofiles
, fdp
->fd_nfiles
* OFILESIZE
, M_OFILETABL
);
4870 fdp
->fd_ofiles
= NULL
;
4877 vnode_rele(fdp
->fd_cdir
);
4879 vnode_rele(fdp
->fd_rdir
);
4881 proc_fdlock_spin(p
);
4886 FREE(fdp
->fd_knlist
, M_KQUEUE
);
4888 FREE(fdp
->fd_knhash
, M_KQUEUE
);
4890 FREE_ZONE(fdp
, sizeof(*fdp
), M_FILEDESC
);
4896 * Description: Internal form of closef; called with proc_fdlock held
4898 * Parameters: fp Pointer to fileproc for fd
4899 * fg Pointer to fileglob for fd
4900 * p Pointer to proc structure
4902 * Returns: 0 Success
4903 * closef_finish:??? Anything returnable by a per-fileops
4906 * Note: Decrements reference count on file structure; if this was the
4907 * last reference, then closef_finish() is called
4909 * p and fp are allowed to be NULL when closing a file that was
4910 * being passed in a message (but only if we are called when this
4911 * is NOT the last reference).
4914 closef_locked(struct fileproc
*fp
, struct fileglob
*fg
, proc_t p
)
4918 struct vfs_context context
;
4925 /* Set up context with cred stashed in fg */
4926 if (p
== current_proc())
4927 context
.vc_thread
= current_thread();
4929 context
.vc_thread
= NULL
;
4930 context
.vc_ucred
= fg
->fg_cred
;
4933 * POSIX record locking dictates that any close releases ALL
4934 * locks owned by this process. This is handled by setting
4935 * a flag in the unlock to free ONLY locks obeying POSIX
4936 * semantics, and not to free BSD-style file locks.
4937 * If the descriptor was in a message, POSIX-style locks
4938 * aren't passed with the descriptor.
4940 if (p
&& (p
->p_ladvflag
& P_LADVLOCK
) &&
4941 DTYPE_VNODE
== FILEGLOB_DTYPE(fg
)) {
4944 lf
.l_whence
= SEEK_SET
;
4947 lf
.l_type
= F_UNLCK
;
4948 vp
= (struct vnode
*)fg
->fg_data
;
4950 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
4951 (void) VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &lf
, F_POSIX
, &context
, NULL
);
4952 (void)vnode_put(vp
);
4956 lck_mtx_lock_spin(&fg
->fg_lock
);
4959 if (fg
->fg_count
> 0) {
4960 lck_mtx_unlock(&fg
->fg_lock
);
4964 if (fg
->fg_count
!= 0)
4965 panic("fg %p: being freed with bad fg_count (%d)", fg
, fg
->fg_count
);
4968 if (fp
&& (fp
->f_flags
& FP_WRITTEN
))
4969 fg
->fg_flag
|= FWASWRITTEN
;
4971 fg
->fg_lflags
|= FG_TERM
;
4972 lck_mtx_unlock(&fg
->fg_lock
);
4977 /* Since we ensure that fg->fg_ops is always initialized,
4978 * it is safe to invoke fo_close on the fg */
4979 error
= fo_close(fg
, &context
);
4993 * Description: Drain out pending I/O operations
4995 * Parameters: p Process closing this file
4996 * fp fileproc struct for the open
4997 * instance on the file
5001 * Locks: Assumes the caller holds the proc_fdlock
5003 * Notes: For character devices, this occurs on the last close of the
5004 * device; for all other file descriptors, this occurs on each
5005 * close to prevent fd's from being closed out from under
5006 * operations currently in progress and blocked
5008 * See Also: file_vnode(), file_socket(), file_drop(), and the cautions
5009 * regarding their use and interaction with this function.
5012 fileproc_drain(proc_t p
, struct fileproc
* fp
)
5014 struct vfs_context context
;
5016 context
.vc_thread
= proc_thread(p
); /* XXX */
5017 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
5019 fp
->f_iocount
-- ; /* (the one the close holds) */
5021 while (fp
->f_iocount
) {
5023 lck_mtx_convert_spin(&p
->p_fdmlock
);
5025 if (fp
->f_fglob
->fg_ops
->fo_drain
) {
5026 (*fp
->f_fglob
->fg_ops
->fo_drain
)(fp
, &context
);
5028 if ((fp
->f_flags
& FP_INSELECT
) == FP_INSELECT
) {
5029 if (wait_queue_wakeup_all((wait_queue_t
)fp
->f_waddr
, NULL
, THREAD_INTERRUPTED
) == KERN_INVALID_ARGUMENT
)
5030 panic("bad wait queue for wait_queue_wakeup_all %p", fp
->f_waddr
);
5032 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
) {
5033 if (wait_queue_wakeup_all(&select_conflict_queue
, NULL
, THREAD_INTERRUPTED
) == KERN_INVALID_ARGUMENT
)
5034 panic("bad select_conflict_queue");
5036 p
->p_fpdrainwait
= 1;
5038 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
, "fpdrain", NULL
);
5042 if ((fp
->f_flags
& FP_INSELECT
) != 0)
5043 panic("FP_INSELECT set on drained fp");
5045 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
)
5046 fp
->f_flags
&= ~FP_SELCONFLICT
;
5053 * Description: Release the fd and free the fileproc associated with the fd
5054 * in the per process open file table of the specified process;
5055 * these values must correspond.
5057 * Parameters: p Process containing fd
5058 * fd fd to be released
5059 * fp fileproc to be freed
5061 * Returns: 0 Success
5063 * Notes: XXX function should be void - no one interprets the returns
5067 fp_free(proc_t p
, int fd
, struct fileproc
* fp
)
5069 proc_fdlock_spin(p
);
5073 fg_free(fp
->f_fglob
);
5082 * Description: Apply an advisory lock on a file descriptor.
5084 * Parameters: p Process making request
5085 * uap->fd fd on which the lock is to be
5087 * uap->how (Un)Lock bits, including type
5088 * retval Pointer to the call return area
5090 * Returns: 0 Success
5091 * fp_getfvp:EBADF Bad file descriptor
5092 * fp_getfvp:ENOTSUP fd does not refer to a vnode
5093 * vnode_getwithref:???
5097 * *retval (modified) Size of dtable
5099 * Notes: Just attempt to get a record lock of the requested type on
5100 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
5103 flock(proc_t p
, struct flock_args
*uap
, __unused
int32_t *retval
)
5107 struct fileproc
*fp
;
5110 vfs_context_t ctx
= vfs_context_current();
5113 AUDIT_ARG(fd
, uap
->fd
);
5114 if ( (error
= fp_getfvp(p
, fd
, &fp
, &vp
)) ) {
5117 if ( (error
= vnode_getwithref(vp
)) ) {
5120 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
5122 lf
.l_whence
= SEEK_SET
;
5125 if (how
& LOCK_UN
) {
5126 lf
.l_type
= F_UNLCK
;
5127 fp
->f_flag
&= ~FHASLOCK
;
5128 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_UNLCK
, &lf
, F_FLOCK
, ctx
, NULL
);
5132 lf
.l_type
= F_WRLCK
;
5133 else if (how
& LOCK_SH
)
5134 lf
.l_type
= F_RDLCK
;
5140 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
, F_SETLK
, &lf
);
5144 fp
->f_flag
|= FHASLOCK
;
5145 if (how
& LOCK_NB
) {
5146 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, F_FLOCK
, ctx
, NULL
);
5149 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, F_FLOCK
|F_WAIT
, ctx
, NULL
);
5151 (void)vnode_put(vp
);
5153 fp_drop(p
, fd
, fp
, 0);
5161 * Description: Obtain a Mach send right for a given file descriptor.
5163 * Parameters: p Process calling fileport
5164 * uap->fd The fd to reference
5165 * uap->portnamep User address at which to place port name.
5167 * Returns: 0 Success.
5168 * EBADF Bad file descriptor.
5169 * EINVAL File descriptor had type that cannot be sent, misc. other errors.
5170 * EFAULT Address at which to store port name is not valid.
5171 * EAGAIN Resource shortage.
5174 * On success, name of send right is stored at user-specified address.
5177 fileport_makeport(proc_t p
, struct fileport_makeport_args
*uap
,
5178 __unused
int *retval
)
5182 user_addr_t user_portaddr
= uap
->portnamep
;
5183 struct fileproc
*fp
= FILEPROC_NULL
;
5184 struct fileglob
*fg
= NULL
;
5185 ipc_port_t fileport
;
5186 mach_port_name_t name
= MACH_PORT_NULL
;
5188 err
= fp_lookup(p
, fd
, &fp
, 0);
5193 if (!filetype_issendable(fp
->f_type
)) {
5198 if (FP_ISGUARDED(fp
, GUARD_FILEPORT
)) {
5200 err
= fp_guard_exception(p
, fd
, fp
, kGUARD_EXC_FILEPORT
);
5205 /* Dropped when port is deallocated */
5209 /* Allocate and initialize a port */
5210 fileport
= fileport_alloc(fg
);
5211 if (fileport
== IPC_PORT_NULL
) {
5217 /* Add an entry. Deallocates port on failure. */
5218 name
= ipc_port_copyout_send(fileport
, get_task_ipcspace(p
->task
));
5219 if (!MACH_PORT_VALID(name
)) {
5224 err
= copyout(&name
, user_portaddr
, sizeof(mach_port_name_t
));
5229 /* Tag the fileglob for debugging purposes */
5230 lck_mtx_lock_spin(&fg
->fg_lock
);
5231 fg
->fg_lflags
|= FG_PORTMADE
;
5232 lck_mtx_unlock(&fg
->fg_lock
);
5234 fp_drop(p
, fd
, fp
, 0);
5239 if (MACH_PORT_VALID(name
)) {
5240 /* Don't care if another thread races us to deallocate the entry */
5241 (void) mach_port_deallocate(get_task_ipcspace(p
->task
), name
);
5244 if (fp
!= FILEPROC_NULL
) {
5245 fp_drop(p
, fd
, fp
, 0);
5252 fileport_releasefg(struct fileglob
*fg
)
5254 (void)closef_locked(NULL
, fg
, PROC_NULL
);
5263 * Description: Obtain the file descriptor for a given Mach send right.
5265 * Parameters: p Process calling fileport
5266 * uap->port Name of send right to file port.
5268 * Returns: 0 Success
5269 * EINVAL Invalid Mach port name, or port is not for a file.
5271 * fdalloc:ENOMEM Unable to allocate fileproc or extend file table.
5274 * *retval (modified) The new descriptor
5277 fileport_makefd(proc_t p
, struct fileport_makefd_args
*uap
, int32_t *retval
)
5279 struct fileglob
*fg
;
5280 struct fileproc
*fp
= FILEPROC_NULL
;
5281 ipc_port_t port
= IPC_PORT_NULL
;
5282 mach_port_name_t send
= uap
->port
;
5287 res
= ipc_object_copyin(get_task_ipcspace(p
->task
),
5288 send
, MACH_MSG_TYPE_COPY_SEND
, &port
);
5290 if (res
!= KERN_SUCCESS
) {
5295 fg
= fileport_port_to_fileglob(port
);
5301 fp
= fileproc_alloc_init(NULL
);
5302 if (fp
== FILEPROC_NULL
) {
5311 err
= fdalloc(p
, 0, &fd
);
5316 *fdflags(p
, fd
) |= UF_EXCLOSE
;
5318 procfdtbl_releasefd(p
, fd
, fp
);
5324 if ((fp
!= NULL
) && (0 != err
)) {
5328 if (IPC_PORT_NULL
!= port
) {
5329 ipc_port_release_send(port
);
5339 * Description: Duplicate the specified descriptor to a free descriptor;
5340 * this is the second half of fdopen(), above.
5342 * Parameters: fdp filedesc pointer to fill in
5344 * dfd fd to dup from
5345 * mode mode to set on new fd
5346 * error command code
5348 * Returns: 0 Success
5349 * EBADF Source fd is bad
5350 * EACCES Requested mode not allowed
5351 * !0 'error', if not ENODEV or
5354 * Notes: XXX This is not thread safe; see fdopen() above
5357 dupfdopen(struct filedesc
*fdp
, int indx
, int dfd
, int flags
, int error
)
5359 struct fileproc
*wfp
;
5360 struct fileproc
*fp
;
5364 proc_t p
= current_proc();
5367 * If the to-be-dup'd fd number is greater than the allowed number
5368 * of file descriptors, or the fd to be dup'd has already been
5369 * closed, reject. Note, check for new == old is necessary as
5370 * falloc could allocate an already closed to-be-dup'd descriptor
5371 * as the new descriptor.
5375 fp
= fdp
->fd_ofiles
[indx
];
5376 if (dfd
< 0 || dfd
>= fdp
->fd_nfiles
||
5377 (wfp
= fdp
->fd_ofiles
[dfd
]) == NULL
|| wfp
== fp
||
5378 (fdp
->fd_ofileflags
[dfd
] & UF_RESERVED
)) {
5384 myerror
= mac_file_check_dup(proc_ucred(p
), wfp
->f_fglob
, dfd
);
5391 * There are two cases of interest here.
5393 * For ENODEV simply dup (dfd) to file descriptor
5394 * (indx) and return.
5396 * For ENXIO steal away the file structure from (dfd) and
5397 * store it in (indx). (dfd) is effectively closed by
5400 * Any other error code is just returned.
5404 if (FP_ISGUARDED(wfp
, GUARD_DUP
)) {
5405 int err
= fp_guard_exception(p
,
5406 dfd
, wfp
, kGUARD_EXC_DUP
);
5412 * Check that the mode the file is being opened for is a
5413 * subset of the mode of the existing descriptor.
5415 if (((flags
& (FREAD
|FWRITE
)) | wfp
->f_flag
) != wfp
->f_flag
) {
5419 if (indx
> fdp
->fd_lastfile
)
5420 fdp
->fd_lastfile
= indx
;
5424 fg_free(fp
->f_fglob
);
5425 fp
->f_fglob
= wfp
->f_fglob
;
5427 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
] |
5428 (flags
& O_CLOEXEC
) ? UF_EXCLOSE
: 0;
5444 * Description: Add a reference to a fileglob by fileproc
5446 * Parameters: fp fileproc containing fileglob
5451 * Notes: XXX Should use OSAddAtomic?
5454 fg_ref(struct fileproc
* fp
)
5456 struct fileglob
*fg
;
5460 lck_mtx_lock_spin(&fg
->fg_lock
);
5463 if ((fp
->f_flags
& ~((unsigned int)FP_VALID_FLAGS
)) != 0)
5464 panic("fg_ref: invalid bits on fp %p", fp
);
5466 if (fg
->fg_count
== 0)
5467 panic("fg_ref: adding fgcount to zeroed fg: fp %p fg %p",
5471 lck_mtx_unlock(&fg
->fg_lock
);
5478 * Description: Remove a reference to a fileglob by fileproc
5480 * Parameters: fp fileproc containing fileglob
5485 * Notes: XXX Should use OSAddAtomic?
5488 fg_drop(struct fileproc
* fp
)
5490 struct fileglob
*fg
;
5493 lck_mtx_lock_spin(&fg
->fg_lock
);
5495 lck_mtx_unlock(&fg
->fg_lock
);
5502 * Description: Insert fileglob onto message queue
5504 * Parameters: fg Fileglob pointer to insert
5508 * Locks: Takes and drops fg_lock, potentially many times
5511 fg_insertuipc(struct fileglob
* fg
)
5515 lck_mtx_lock_spin(&fg
->fg_lock
);
5517 while (fg
->fg_lflags
& FG_RMMSGQ
) {
5518 lck_mtx_convert_spin(&fg
->fg_lock
);
5520 fg
->fg_lflags
|= FG_WRMMSGQ
;
5521 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_insertuipc", NULL
);
5526 if (fg
->fg_msgcount
== 1) {
5527 fg
->fg_lflags
|= FG_INSMSGQ
;
5530 lck_mtx_unlock(&fg
->fg_lock
);
5533 lck_mtx_lock_spin(uipc_lock
);
5535 LIST_INSERT_HEAD(&fmsghead
, fg
, f_msglist
);
5536 lck_mtx_unlock(uipc_lock
);
5537 lck_mtx_lock(&fg
->fg_lock
);
5538 fg
->fg_lflags
&= ~FG_INSMSGQ
;
5539 if (fg
->fg_lflags
& FG_WINSMSGQ
) {
5540 fg
->fg_lflags
&= ~FG_WINSMSGQ
;
5541 wakeup(&fg
->fg_lflags
);
5543 lck_mtx_unlock(&fg
->fg_lock
);
5552 * Description: Remove fileglob from message queue
5554 * Parameters: fg Fileglob pointer to remove
5558 * Locks: Takes and drops fg_lock, potentially many times
5561 fg_removeuipc(struct fileglob
* fg
)
5565 lck_mtx_lock_spin(&fg
->fg_lock
);
5566 while (fg
->fg_lflags
& FG_INSMSGQ
) {
5567 lck_mtx_convert_spin(&fg
->fg_lock
);
5569 fg
->fg_lflags
|= FG_WINSMSGQ
;
5570 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_removeuipc", NULL
);
5573 if (fg
->fg_msgcount
== 0) {
5574 fg
->fg_lflags
|= FG_RMMSGQ
;
5577 lck_mtx_unlock(&fg
->fg_lock
);
5580 lck_mtx_lock_spin(uipc_lock
);
5582 LIST_REMOVE(fg
, f_msglist
);
5583 lck_mtx_unlock(uipc_lock
);
5584 lck_mtx_lock(&fg
->fg_lock
);
5585 fg
->fg_lflags
&= ~FG_RMMSGQ
;
5586 if (fg
->fg_lflags
& FG_WRMMSGQ
) {
5587 fg
->fg_lflags
&= ~FG_WRMMSGQ
;
5588 wakeup(&fg
->fg_lflags
);
5590 lck_mtx_unlock(&fg
->fg_lock
);
5593 #endif /* SOCKETS */
5598 * Description: Generic fileops read indirected through the fileops pointer
5599 * in the fileproc structure
5601 * Parameters: fp fileproc structure pointer
5602 * uio user I/O structure pointer
5604 * ctx VFS context for operation
5606 * Returns: 0 Success
5607 * !0 Errno from read
5610 fo_read(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
5612 return ((*fp
->f_ops
->fo_read
)(fp
, uio
, flags
, ctx
));
5619 * Description: Generic fileops write indirected through the fileops pointer
5620 * in the fileproc structure
5622 * Parameters: fp fileproc structure pointer
5623 * uio user I/O structure pointer
5625 * ctx VFS context for operation
5627 * Returns: 0 Success
5628 * !0 Errno from write
5631 fo_write(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
5633 return((*fp
->f_ops
->fo_write
)(fp
, uio
, flags
, ctx
));
5640 * Description: Generic fileops ioctl indirected through the fileops pointer
5641 * in the fileproc structure
5643 * Parameters: fp fileproc structure pointer
5645 * data pointer to internalized copy
5646 * of user space ioctl command
5647 * parameter data in kernel space
5648 * ctx VFS context for operation
5650 * Returns: 0 Success
5651 * !0 Errno from ioctl
5653 * Locks: The caller is assumed to have held the proc_fdlock; this
5654 * function releases and reacquires this lock. If the caller
5655 * accesses data protected by this lock prior to calling this
5656 * function, it will need to revalidate/reacquire any cached
5657 * protected data obtained prior to the call.
5660 fo_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
, vfs_context_t ctx
)
5664 proc_fdunlock(vfs_context_proc(ctx
));
5665 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, com
, data
, ctx
);
5666 proc_fdlock(vfs_context_proc(ctx
));
5674 * Description: Generic fileops select indirected through the fileops pointer
5675 * in the fileproc structure
5677 * Parameters: fp fileproc structure pointer
5678 * which select which
5679 * wql pointer to wait queue list
5680 * ctx VFS context for operation
5682 * Returns: 0 Success
5683 * !0 Errno from select
5686 fo_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
5688 return((*fp
->f_ops
->fo_select
)(fp
, which
, wql
, ctx
));
5695 * Description: Generic fileops close indirected through the fileops pointer
5696 * in the fileproc structure
5698 * Parameters: fp fileproc structure pointer for
5700 * ctx VFS context for operation
5702 * Returns: 0 Success
5703 * !0 Errno from close
5706 fo_close(struct fileglob
*fg
, vfs_context_t ctx
)
5708 return((*fg
->fg_ops
->fo_close
)(fg
, ctx
));
5715 * Description: Generic fileops kqueue filter indirected through the fileops
5716 * pointer in the fileproc structure
5718 * Parameters: fp fileproc structure pointer
5719 * kn pointer to knote to filter on
5720 * ctx VFS context for operation
5722 * Returns: 0 Success
5723 * !0 Errno from kqueue filter
5726 fo_kqfilter(struct fileproc
*fp
, struct knote
*kn
, vfs_context_t ctx
)
5728 return ((*fp
->f_ops
->fo_kqfilter
)(fp
, kn
, ctx
));
5732 * The ability to send a file descriptor to another
5733 * process is opt-in by file type.
5736 filetype_issendable(file_type_t fdtype
)
5745 /* DTYPE_KQUEUE, DTYPE_FSEVENTS, DTYPE_PSXSEM */
5752 fileproc_alloc_init(__unused
void *arg
)
5754 struct fileproc
*fp
;
5756 MALLOC_ZONE(fp
, struct fileproc
*, sizeof (*fp
), M_FILEPROC
, M_WAITOK
);
5758 bzero(fp
, sizeof (*fp
));
5764 fileproc_free(struct fileproc
*fp
)
5766 switch (FILEPROC_TYPE(fp
)) {
5768 FREE_ZONE(fp
, sizeof (*fp
), M_FILEPROC
);
5771 guarded_fileproc_free(fp
);
5774 panic("%s: corrupt fp %p flags %x", __func__
, fp
, fp
->f_flags
);