2 * Copyright (c) 2000-2007 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/socket.h>
84 #include <sys/socketvar.h>
86 #include <sys/ioctl.h>
87 #include <sys/fcntl.h>
88 #include <sys/malloc.h>
90 #include <sys/syslog.h>
91 #include <sys/unistd.h>
92 #include <sys/resourcevar.h>
93 #include <sys/aio_kern.h>
95 #include <kern/lock.h>
96 #include <sys/uio_internal.h>
98 #include <security/audit/audit.h>
100 #include <sys/mount_internal.h>
101 #include <sys/kdebug.h>
102 #include <sys/sysproto.h>
103 #include <sys/pipe.h>
104 #include <kern/kern_types.h>
105 #include <kern/kalloc.h>
106 #include <libkern/OSAtomic.h>
108 #include <sys/ubc_internal.h>
110 #include <kern/ipc_misc.h>
111 #include <vm/vm_protos.h>
113 #include <mach/mach_port.h>
115 kern_return_t
ipc_object_copyin(ipc_space_t
, mach_port_name_t
,
116 mach_msg_type_name_t
, ipc_port_t
*);
117 void ipc_port_release_send(ipc_port_t
);
122 int fdopen(dev_t dev
, int mode
, int type
, proc_t p
);
123 int finishdup(proc_t p
, struct filedesc
*fdp
, int old
, int new, int32_t *retval
);
125 int falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
, int locked
);
126 void fg_drop(struct fileproc
* fp
);
127 void fg_free(struct fileglob
*fg
);
128 void fg_ref(struct fileproc
* fp
);
130 void fileport_releasefg(struct fileglob
*fg
);
131 #endif /* CONFIG_EMBEDDED */
133 /* flags for close_internal_locked */
134 #define FD_DUP2RESV 1
135 static int close_internal_locked(struct proc
*p
, int fd
, struct fileproc
*fp
, int flags
);
137 static int closef_finish(struct fileproc
*fp
, struct fileglob
*fg
, proc_t p
, vfs_context_t ctx
);
139 /* We don't want these exported */
141 int open1(vfs_context_t
, struct nameidata
*, int, struct vnode_attr
*, int32_t *);
144 int unlink1(vfs_context_t
, struct nameidata
*, int);
146 static void _fdrelse(struct proc
* p
, int fd
);
149 extern void file_lock_init(void) __attribute__((section("__TEXT, initcode")));
150 extern int kqueue_stat(struct fileproc
*fp
, void *ub
, int isstat4
, proc_t p
);
152 extern int soo_stat(struct socket
*so
, void *ub
, int isstat64
);
155 extern kauth_scope_t kauth_scope_fileop
;
159 #define f_flag f_fglob->fg_flag
160 #define f_type f_fglob->fg_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
167 * Descriptor management.
169 struct filelist filehead
; /* head of list of open files */
170 struct fmsglist fmsghead
; /* head of list of open files */
171 struct fmsglist fmsg_ithead
; /* head of list of open files */
172 int nfiles
; /* actual number of open files */
175 lck_grp_attr_t
* file_lck_grp_attr
;
176 lck_grp_t
* file_lck_grp
;
177 lck_attr_t
* file_lck_attr
;
179 lck_mtx_t
* uipc_lock
;
180 lck_mtx_t
* file_flist_lock
;
186 * Description: Initialize the file lock group and the uipc and flist locks
192 * Notes: Called at system startup from bsd_init().
197 /* allocate file lock group attribute and group */
198 file_lck_grp_attr
= lck_grp_attr_alloc_init();
200 file_lck_grp
= lck_grp_alloc_init("file", file_lck_grp_attr
);
202 /* Allocate file lock attribute */
203 file_lck_attr
= lck_attr_alloc_init();
205 uipc_lock
= lck_mtx_alloc_init(file_lck_grp
, file_lck_attr
);
206 file_flist_lock
= lck_mtx_alloc_init(file_lck_grp
, file_lck_attr
);
211 * proc_fdlock, proc_fdlock_spin
213 * Description: Lock to control access to the per process struct fileproc
214 * and struct filedesc
216 * Parameters: p Process to take the lock on
220 * Notes: The lock is initialized in forkproc() and destroyed in
221 * reap_child_process().
224 proc_fdlock(proc_t p
)
226 lck_mtx_lock(&p
->p_fdmlock
);
230 proc_fdlock_spin(proc_t p
)
232 lck_mtx_lock_spin(&p
->p_fdmlock
);
236 proc_fdlock_assert(proc_t p
, int assertflags
)
238 lck_mtx_assert(&p
->p_fdmlock
, assertflags
);
245 * Description: Unlock the lock previously locked by a call to proc_fdlock()
247 * Parameters: p Process to drop the lock on
252 proc_fdunlock(proc_t p
)
254 lck_mtx_unlock(&p
->p_fdmlock
);
259 * System calls on descriptors.
266 * Description: Returns the per process maximum size of the descriptor table
268 * Parameters: p Process being queried
269 * retval Pointer to the call return area
274 * *retval (modified) Size of dtable
277 getdtablesize(proc_t p
, __unused
struct getdtablesize_args
*uap
, int32_t *retval
)
280 *retval
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
288 procfdtbl_reservefd(struct proc
* p
, int fd
)
290 p
->p_fd
->fd_ofiles
[fd
] = NULL
;
291 p
->p_fd
->fd_ofileflags
[fd
] |= UF_RESERVED
;
295 procfdtbl_markclosefd(struct proc
* p
, int fd
)
297 p
->p_fd
->fd_ofileflags
[fd
] |= (UF_RESERVED
| UF_CLOSING
);
301 procfdtbl_releasefd(struct proc
* p
, int fd
, struct fileproc
* fp
)
304 p
->p_fd
->fd_ofiles
[fd
] = fp
;
305 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESERVED
;
306 if ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
) == UF_RESVWAIT
) {
307 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESVWAIT
;
313 procfdtbl_waitfd(struct proc
* p
, int fd
)
315 p
->p_fd
->fd_ofileflags
[fd
] |= UF_RESVWAIT
;
316 msleep(&p
->p_fd
, &p
->p_fdmlock
, PRIBIO
, "ftbl_waitfd", NULL
);
321 procfdtbl_clearfd(struct proc
* p
, int fd
)
325 waiting
= (p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
);
326 p
->p_fd
->fd_ofiles
[fd
] = NULL
;
327 p
->p_fd
->fd_ofileflags
[fd
] = 0;
328 if ( waiting
== UF_RESVWAIT
) {
336 * Description: Inline utility function to free an fd in a filedesc
338 * Parameters: fdp Pointer to filedesc fd lies in
340 * reserv fd should be reserved
344 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
348 _fdrelse(struct proc
* p
, int fd
)
350 struct filedesc
*fdp
= p
->p_fd
;
353 if (fd
< fdp
->fd_freefile
)
354 fdp
->fd_freefile
= fd
;
356 if (fd
> fdp
->fd_lastfile
)
357 panic("fdrelse: fd_lastfile inconsistent");
359 procfdtbl_clearfd(p
, fd
);
361 while ((nfd
= fdp
->fd_lastfile
) > 0 &&
362 fdp
->fd_ofiles
[nfd
] == NULL
&&
363 !(fdp
->fd_ofileflags
[nfd
] & UF_RESERVED
))
385 char uio_buf
[ UIO_SIZEOF(1) ];
386 struct vfs_context context
= *(vfs_context_current());
390 error
= fp_lookup(p
, fd
, &fp
, 0);
394 if (fp
->f_type
!= DTYPE_VNODE
&& fp
->f_type
!= DTYPE_PIPE
&& fp
->f_type
!= DTYPE_SOCKET
) {
398 if (rw
== UIO_WRITE
&& !(fp
->f_flag
& FWRITE
)) {
403 if (rw
== UIO_READ
&& !(fp
->f_flag
& FREAD
)) {
408 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
410 if (UIO_SEG_IS_USER_SPACE(segflg
))
411 spacetype
= proc_is64bit(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
413 spacetype
= UIO_SYSSPACE
;
415 auio
= uio_createwithbuffer(1, offset
, spacetype
, rw
, &uio_buf
[0], sizeof(uio_buf
));
417 uio_addiov(auio
, base
, len
);
419 if ( !(io_flg
& IO_APPEND
))
423 error
= fo_write(fp
, auio
, flags
, &context
);
425 error
= fo_read(fp
, auio
, flags
, &context
);
428 *aresid
= uio_resid(auio
);
430 if (uio_resid(auio
) && error
== 0)
434 if (rw
== UIO_WRITE
&& error
== 0)
435 fp_drop_written(p
, fd
, fp
);
437 fp_drop(p
, fd
, fp
, 0);
447 * Description: Duplicate a file descriptor.
449 * Parameters: p Process performing the dup
450 * uap->fd The fd to dup
451 * retval Pointer to the call return area
457 * *retval (modified) The new descriptor
460 dup(proc_t p
, struct dup_args
*uap
, int32_t *retval
)
462 struct filedesc
*fdp
= p
->p_fd
;
468 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
472 if ( (error
= fdalloc(p
, 0, &new)) ) {
473 fp_drop(p
, old
, fp
, 1);
477 error
= finishdup(p
, fdp
, old
, new, retval
);
478 fp_drop(p
, old
, fp
, 1);
488 * Description: Duplicate a file descriptor to a particular value.
490 * Parameters: p Process performing the dup
491 * uap->fd The fd to dup
492 * uap->to The fd to dup it to
493 * retval Pointer to the call return area
499 * *retval (modified) The new descriptor
502 dup2(proc_t p
, struct dup2_args
*uap
, int32_t *retval
)
504 struct filedesc
*fdp
= p
->p_fd
;
505 int old
= uap
->from
, new = uap
->to
;
507 struct fileproc
*fp
, *nfp
;
512 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
517 (rlim_t
)new >= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
519 fp_drop(p
, old
, fp
, 1);
524 fp_drop(p
, old
, fp
, 1);
529 if (new < 0 || new >= fdp
->fd_nfiles
) {
530 if ( (error
= fdalloc(p
, new, &i
)) ) {
531 fp_drop(p
, old
, fp
, 1);
541 while ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == UF_RESERVED
) {
542 fp_drop(p
, old
, fp
, 1);
543 procfdtbl_waitfd(p
, new);
545 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
550 if ((fdp
->fd_ofiles
[new] != NULL
) && ((error
= fp_lookup(p
, new, &nfp
, 1)) == 0)) {
551 fp_drop(p
, old
, fp
, 1);
552 (void)close_internal_locked(p
, new, nfp
, FD_DUP2RESV
);
554 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
556 procfdtbl_clearfd(p
, new);
560 if (fdp
->fd_ofiles
[new] != NULL
)
561 panic("dup2: unable to get ref on a fileproc %d\n", new);
563 procfdtbl_reservefd(p
, new);
567 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
572 if (fdp
->fd_ofiles
[new] != 0)
573 panic("dup2-1: overwriting fd_ofiles with new %d\n", new);
574 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0)
575 panic("dup2-1: unreserved fileflags with new %d\n", new);
577 error
= finishdup(p
, fdp
, old
, new, retval
);
578 fp_drop(p
, old
, fp
, 1);
588 * Description: The file control system call.
590 * Parameters: p Process performing the fcntl
591 * uap->fd The fd to operate against
592 * uap->cmd The command to perform
593 * uap->arg Pointer to the command argument
594 * retval Pointer to the call return area
597 * !0 Errno (see fcntl_nocancel)
600 * *retval (modified) fcntl return value (if any)
602 * Notes: This system call differs from fcntl_nocancel() in that it
603 * tests for cancellation prior to performing a potentially
604 * blocking operation.
607 fcntl(proc_t p
, struct fcntl_args
*uap
, int32_t *retval
)
609 __pthread_testcancel(1);
610 return(fcntl_nocancel(p
, (struct fcntl_nocancel_args
*)uap
, retval
));
617 * Description: A non-cancel-testing file control system call.
619 * Parameters: p Process performing the fcntl
620 * uap->fd The fd to operate against
621 * uap->cmd The command to perform
622 * uap->arg Pointer to the command argument
623 * retval Pointer to the call return area
627 * fp_lookup:EBADF Bad file descriptor
639 * vnode_getwithref:???
646 * vnode_getwithref:???
653 * vnode_getwithref:???
655 * [F_SETSIZE,F_RDADVISE]
658 * vnode_getwithref:???
659 * [F_RDAHEAD,F_NOCACHE]
661 * vnode_getwithref:???
665 * *retval (modified) fcntl return value (if any)
668 fcntl_nocancel(proc_t p
, struct fcntl_nocancel_args
*uap
, int32_t *retval
)
671 struct filedesc
*fdp
= p
->p_fd
;
674 struct vnode
*vp
= NULLVP
; /* for AUDIT_ARG() at end */
675 int i
, tmp
, error
, error2
, flg
= F_POSIX
;
677 struct vfs_context context
;
681 int devBlockSize
= 0;
686 AUDIT_ARG(fd
, uap
->fd
);
687 AUDIT_ARG(cmd
, uap
->cmd
);
690 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
694 context
.vc_thread
= current_thread();
695 context
.vc_ucred
= fp
->f_cred
;
697 is64bit
= proc_is64bit(p
);
703 * Since the arg parameter is defined as a long but may be
704 * either a long or a pointer we must take care to handle
705 * sign extension issues. Our sys call munger will sign
706 * extend a long when we are called from a 32-bit process.
707 * Since we can never have an address greater than 32-bits
708 * from a 32-bit process we lop off the top 32-bits to avoid
709 * getting the wrong address
711 argp
= CAST_USER_ADDR_T((uint32_t)uap
->arg
);
714 pop
= &fdp
->fd_ofileflags
[fd
];
717 error
= mac_file_check_fcntl(proc_ucred(p
), fp
->f_fglob
, uap
->cmd
,
726 newmin
= CAST_DOWN_EXPLICIT(int, uap
->arg
); /* arg is an int, so we won't lose bits */
727 AUDIT_ARG(value32
, newmin
);
728 if ((u_int
)newmin
>= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
729 newmin
>= maxfiles
) {
733 if ( (error
= fdalloc(p
, newmin
, &i
)) )
735 error
= finishdup(p
, fdp
, fd
, i
, retval
);
739 *retval
= (*pop
& UF_EXCLOSE
)? 1 : 0;
744 AUDIT_ARG(value32
, uap
->arg
);
745 *pop
= (*pop
&~ UF_EXCLOSE
) |
746 (uap
->arg
& 1)? UF_EXCLOSE
: 0;
751 *retval
= OFLAGS(fp
->f_flag
);
756 fp
->f_flag
&= ~FCNTLFLAGS
;
757 tmp
= CAST_DOWN_EXPLICIT(int, uap
->arg
); /* arg is an int, so we won't lose bits */
758 AUDIT_ARG(value32
, tmp
);
759 fp
->f_flag
|= FFLAGS(tmp
) & FCNTLFLAGS
;
760 tmp
= fp
->f_flag
& FNONBLOCK
;
761 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
764 tmp
= fp
->f_flag
& FASYNC
;
765 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, &context
);
768 fp
->f_flag
&= ~FNONBLOCK
;
770 (void)fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
774 if (fp
->f_type
== DTYPE_SOCKET
) {
775 *retval
= ((struct socket
*)fp
->f_data
)->so_pgid
;
779 error
= fo_ioctl(fp
, (int)TIOCGPGRP
, (caddr_t
)retval
, &context
);
784 tmp
= CAST_DOWN_EXPLICIT(pid_t
, uap
->arg
); /* arg is an int, so we won't lose bits */
785 AUDIT_ARG(value32
, tmp
);
786 if (fp
->f_type
== DTYPE_SOCKET
) {
787 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
791 if (fp
->f_type
== DTYPE_PIPE
) {
792 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
799 proc_t p1
= proc_find(tmp
);
804 tmp
= (int)p1
->p_pgrpid
;
807 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
812 /* Fall into F_SETLK */
815 if (fp
->f_type
!= DTYPE_VNODE
) {
819 vp
= (struct vnode
*)fp
->f_data
;
822 offset
= fp
->f_offset
;
825 /* Copy in the lock structure */
826 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
831 if ((fl
.l_whence
== SEEK_CUR
) && (fl
.l_start
+ offset
< fl
.l_start
)) {
836 if ( (error
= vnode_getwithref(vp
)) ) {
839 if (fl
.l_whence
== SEEK_CUR
)
840 fl
.l_start
+= offset
;
843 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
853 if ((fflag
& FREAD
) == 0) {
858 // XXX UInt32 unsafe for LP64 kernel
859 OSBitOrAtomic(P_LADVLOCK
, &p
->p_ladvflag
);
860 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_SETLK
, &fl
, flg
, &context
);
865 if ((fflag
& FWRITE
) == 0) {
870 // XXX UInt32 unsafe for LP64 kernel
871 OSBitOrAtomic(P_LADVLOCK
, &p
->p_ladvflag
);
872 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_SETLK
, &fl
, flg
, &context
);
877 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &fl
,
889 if (fp
->f_type
!= DTYPE_VNODE
) {
893 vp
= (struct vnode
*)fp
->f_data
;
895 offset
= fp
->f_offset
;
898 /* Copy in the lock structure */
899 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
903 /* Check starting byte and ending byte for EOVERFLOW in SEEK_CUR */
904 /* and ending byte for EOVERFLOW in SEEK_SET */
905 if (((fl
.l_whence
== SEEK_CUR
) &&
906 ((fl
.l_start
+ offset
< fl
.l_start
) ||
907 ((fl
.l_len
> 0) && (fl
.l_start
+offset
+ fl
.l_len
- 1 < fl
.l_start
+offset
)))) ||
908 ((fl
.l_whence
== SEEK_SET
) && (fl
.l_len
> 0) && (fl
.l_start
+ fl
.l_len
- 1 < fl
.l_start
)))
910 /* lf_advlock doesn't check start/end for F_GETLK if file has no locks */
915 if ((fl
.l_whence
== SEEK_SET
) && (fl
.l_start
< 0)) {
930 switch (fl
.l_whence
) {
940 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
941 if (fl
.l_whence
== SEEK_CUR
)
942 fl
.l_start
+= offset
;
945 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
949 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_GETLK
, &fl
, F_POSIX
, &context
);
954 error
= copyout((caddr_t
)&fl
, argp
, sizeof(fl
));
958 case F_PREALLOCATE
: {
959 fstore_t alloc_struct
; /* structure for allocate command */
960 u_int32_t alloc_flags
= 0;
962 if (fp
->f_type
!= DTYPE_VNODE
) {
967 vp
= (struct vnode
*)fp
->f_data
;
970 /* make sure that we have write permission */
971 if ((fp
->f_flag
& FWRITE
) == 0) {
976 error
= copyin(argp
, (caddr_t
)&alloc_struct
, sizeof(alloc_struct
));
980 /* now set the space allocated to 0 */
981 alloc_struct
.fst_bytesalloc
= 0;
984 * Do some simple parameter checking
987 /* set up the flags */
989 alloc_flags
|= PREALLOCATE
;
991 if (alloc_struct
.fst_flags
& F_ALLOCATECONTIG
)
992 alloc_flags
|= ALLOCATECONTIG
;
994 if (alloc_struct
.fst_flags
& F_ALLOCATEALL
)
995 alloc_flags
|= ALLOCATEALL
;
998 * Do any position mode specific stuff. The only
999 * position mode supported now is PEOFPOSMODE
1002 switch (alloc_struct
.fst_posmode
) {
1005 if (alloc_struct
.fst_offset
!= 0) {
1010 alloc_flags
|= ALLOCATEFROMPEOF
;
1014 if (alloc_struct
.fst_offset
<= 0) {
1019 alloc_flags
|= ALLOCATEFROMVOL
;
1027 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1029 * call allocate to get the space
1031 error
= VNOP_ALLOCATE(vp
,alloc_struct
.fst_length
,alloc_flags
,
1032 &alloc_struct
.fst_bytesalloc
, alloc_struct
.fst_offset
,
1034 (void)vnode_put(vp
);
1036 error2
= copyout((caddr_t
)&alloc_struct
, argp
, sizeof(alloc_struct
));
1045 if (fp
->f_type
!= DTYPE_VNODE
) {
1049 vp
= (struct vnode
*)fp
->f_data
;
1052 error
= copyin(argp
, (caddr_t
)&offset
, sizeof (off_t
));
1055 AUDIT_ARG(value64
, offset
);
1057 error
= vnode_getwithref(vp
);
1062 error
= mac_vnode_check_truncate(&context
,
1063 fp
->f_fglob
->fg_cred
, vp
);
1065 (void)vnode_put(vp
);
1070 * Make sure that we are root. Growing a file
1071 * without zero filling the data is a security hole
1072 * root would have access anyway so we'll allow it
1080 error
= vnode_setsize(vp
, offset
, IO_NOZEROFILL
,
1084 (void)vnode_put(vp
);
1088 if (fp
->f_type
!= DTYPE_VNODE
) {
1093 fp
->f_fglob
->fg_flag
&= ~FNORDAHEAD
;
1095 fp
->f_fglob
->fg_flag
|= FNORDAHEAD
;
1100 if (fp
->f_type
!= DTYPE_VNODE
) {
1105 fp
->f_fglob
->fg_flag
|= FNOCACHE
;
1107 fp
->f_fglob
->fg_flag
&= ~FNOCACHE
;
1111 case F_GLOBAL_NOCACHE
:
1112 if (fp
->f_type
!= DTYPE_VNODE
) {
1116 vp
= (struct vnode
*)fp
->f_data
;
1119 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1121 *retval
= vnode_isnocache(vp
);
1124 vnode_setnocache(vp
);
1126 vnode_clearnocache(vp
);
1128 (void)vnode_put(vp
);
1132 case F_CHECK_OPENEVT
:
1133 if (fp
->f_type
!= DTYPE_VNODE
) {
1137 vp
= (struct vnode
*)fp
->f_data
;
1140 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1142 *retval
= vnode_is_openevt(vp
);
1145 vnode_set_openevt(vp
);
1147 vnode_clear_openevt(vp
);
1149 (void)vnode_put(vp
);
1154 struct radvisory ra_struct
;
1156 if (fp
->f_type
!= DTYPE_VNODE
) {
1160 vp
= (struct vnode
*)fp
->f_data
;
1163 if ( (error
= copyin(argp
, (caddr_t
)&ra_struct
, sizeof(ra_struct
))) )
1165 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1166 error
= VNOP_IOCTL(vp
, F_RDADVISE
, (caddr_t
)&ra_struct
, 0, &context
);
1168 (void)vnode_put(vp
);
1173 case F_READBOOTSTRAP
:
1174 case F_WRITEBOOTSTRAP
: {
1175 user32_fbootstraptransfer_t user32_fbt_struct
;
1176 user_fbootstraptransfer_t user_fbt_struct
;
1178 caddr_t boot_structp
;
1180 if (fp
->f_type
!= DTYPE_VNODE
) {
1184 vp
= (struct vnode
*)fp
->f_data
;
1187 if (IS_64BIT_PROCESS(p
)) {
1188 sizeof_struct
= sizeof(user_fbt_struct
);
1189 boot_structp
= (caddr_t
) &user_fbt_struct
;
1192 sizeof_struct
= sizeof(user32_fbt_struct
);
1193 boot_structp
= (caddr_t
) &user32_fbt_struct
;
1195 error
= copyin(argp
, boot_structp
, sizeof_struct
);
1198 if ( (error
= vnode_getwithref(vp
)) ) {
1201 if (uap
->cmd
== F_WRITEBOOTSTRAP
) {
1203 * Make sure that we are root. Updating the
1204 * bootstrap on a disk could be a security hole
1207 (void)vnode_put(vp
);
1212 if (strncmp(vnode_mount(vp
)->mnt_vfsstat
.f_fstypename
, "hfs",
1213 sizeof(vnode_mount(vp
)->mnt_vfsstat
.f_fstypename
)) != 0) {
1217 * call vnop_ioctl to handle the I/O
1219 error
= VNOP_IOCTL(vp
, uap
->cmd
, boot_structp
, 0, &context
);
1221 (void)vnode_put(vp
);
1225 struct log2phys l2p_struct
; /* structure for allocate command */
1227 if (fp
->f_type
!= DTYPE_VNODE
) {
1231 vp
= (struct vnode
*)fp
->f_data
;
1233 if ( (error
= vnode_getwithref(vp
)) ) {
1236 error
= VNOP_OFFTOBLK(vp
, fp
->f_offset
, &lbn
);
1238 (void)vnode_put(vp
);
1241 error
= VNOP_BLKTOOFF(vp
, lbn
, &offset
);
1243 (void)vnode_put(vp
);
1246 devBlockSize
= vfs_devblocksize(vnode_mount(vp
));
1248 error
= VNOP_BLOCKMAP(vp
, offset
, devBlockSize
, &bn
, NULL
, NULL
, 0, &context
);
1250 (void)vnode_put(vp
);
1253 l2p_struct
.l2p_flags
= 0; /* for now */
1254 l2p_struct
.l2p_contigbytes
= 0; /* for now */
1255 l2p_struct
.l2p_devoffset
= bn
* devBlockSize
;
1256 l2p_struct
.l2p_devoffset
+= fp
->f_offset
- offset
;
1257 error
= copyout((caddr_t
)&l2p_struct
, argp
, sizeof(l2p_struct
));
1265 if (fp
->f_type
!= DTYPE_VNODE
) {
1269 vp
= (struct vnode
*)fp
->f_data
;
1272 pathlen
= MAXPATHLEN
;
1273 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
1274 if (pathbufp
== NULL
) {
1278 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1279 error
= vn_getpath(vp
, pathbufp
, &pathlen
);
1280 (void)vnode_put(vp
);
1283 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
1285 FREE(pathbufp
, M_TEMP
);
1289 case F_PATHPKG_CHECK
: {
1293 if (fp
->f_type
!= DTYPE_VNODE
) {
1297 vp
= (struct vnode
*)fp
->f_data
;
1300 pathlen
= MAXPATHLEN
;
1301 pathbufp
= kalloc(MAXPATHLEN
);
1303 if ( (error
= copyinstr(argp
, pathbufp
, MAXPATHLEN
, &pathlen
)) == 0 ) {
1304 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1305 AUDIT_ARG(text
, pathbufp
);
1306 error
= vn_path_package_check(vp
, pathbufp
, pathlen
, retval
);
1308 (void)vnode_put(vp
);
1311 kfree(pathbufp
, MAXPATHLEN
);
1315 case F_CHKCLEAN
: // used by regression tests to see if all dirty pages got cleaned by fsync()
1316 case F_FULLFSYNC
: // fsync + flush the journal + DKIOCSYNCHRONIZECACHE
1317 case F_FREEZE_FS
: // freeze all other fs operations for the fs of this fd
1318 case F_THAW_FS
: { // thaw all frozen fs operations for the fs of this fd
1319 if (fp
->f_type
!= DTYPE_VNODE
) {
1323 vp
= (struct vnode
*)fp
->f_data
;
1326 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1327 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
1329 (void)vnode_put(vp
);
1335 * SPI (private) for opening a file starting from a dir fd
1338 struct user_fopenfrom fopen
;
1339 struct vnode_attr va
;
1340 struct nameidata nd
;
1343 /* Check if this isn't a valid file descriptor */
1344 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1345 (fp
->f_flag
& FREAD
) == 0) {
1349 vp
= (struct vnode
*)fp
->f_data
;
1352 if (vnode_getwithref(vp
)) {
1357 /* Only valid for directories */
1358 if (vp
->v_type
!= VDIR
) {
1364 /* Get flags, mode and pathname arguments. */
1365 if (IS_64BIT_PROCESS(p
)) {
1366 error
= copyin(argp
, &fopen
, sizeof(fopen
));
1368 struct user32_fopenfrom fopen32
;
1370 error
= copyin(argp
, &fopen32
, sizeof(fopen32
));
1371 fopen
.o_flags
= fopen32
.o_flags
;
1372 fopen
.o_mode
= fopen32
.o_mode
;
1373 fopen
.o_pathname
= CAST_USER_ADDR_T(fopen32
.o_pathname
);
1379 AUDIT_ARG(fflags
, fopen
.o_flags
);
1380 AUDIT_ARG(mode
, fopen
.o_mode
);
1382 /* Mask off all but regular access permissions */
1383 cmode
= ((fopen
.o_mode
&~ fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
1384 VATTR_SET(&va
, va_mode
, cmode
& ACCESSPERMS
);
1386 /* Start the lookup relative to the file descriptor's vnode. */
1387 NDINIT(&nd
, LOOKUP
, USEDVP
| FOLLOW
| AUDITVNPATH1
, UIO_USERSPACE
,
1388 fopen
.o_pathname
, &context
);
1391 error
= open1(&context
, &nd
, fopen
.o_flags
, &va
, retval
);
1397 * SPI (private) for unlinking a file starting from a dir fd
1399 case F_UNLINKFROM
: {
1400 struct nameidata nd
;
1401 user_addr_t pathname
;
1403 /* Check if this isn't a valid file descriptor */
1404 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1405 (fp
->f_flag
& FREAD
) == 0) {
1409 vp
= (struct vnode
*)fp
->f_data
;
1412 if (vnode_getwithref(vp
)) {
1417 /* Only valid for directories */
1418 if (vp
->v_type
!= VDIR
) {
1424 /* Get flags, mode and pathname arguments. */
1425 if (IS_64BIT_PROCESS(p
)) {
1426 pathname
= (user_addr_t
)argp
;
1428 pathname
= CAST_USER_ADDR_T(argp
);
1431 /* Start the lookup relative to the file descriptor's vnode. */
1432 NDINIT(&nd
, DELETE
, USEDVP
| AUDITVNPATH1
, UIO_USERSPACE
, pathname
, &context
);
1435 error
= unlink1(&context
, &nd
, 0);
1445 struct user_fsignatures fs
;
1447 vm_offset_t kernel_blob_addr
;
1448 vm_size_t kernel_blob_size
;
1450 if (fp
->f_type
!= DTYPE_VNODE
) {
1454 vp
= (struct vnode
*)fp
->f_data
;
1456 error
= vnode_getwithref(vp
);
1460 if (IS_64BIT_PROCESS(p
)) {
1461 error
= copyin(argp
, &fs
, sizeof (fs
));
1463 struct user32_fsignatures fs32
;
1465 error
= copyin(argp
, &fs32
, sizeof (fs32
));
1466 fs
.fs_file_start
= fs32
.fs_file_start
;
1467 fs
.fs_blob_start
= CAST_USER_ADDR_T(fs32
.fs_blob_start
);
1468 fs
.fs_blob_size
= fs32
.fs_blob_size
;
1476 if(ubc_cs_blob_get(vp
, CPU_TYPE_ANY
, fs
.fs_file_start
))
1480 printf("CODE SIGNING: resident blob offered for: %s\n", vp->v_name);
1486 #define CS_MAX_BLOB_SIZE (1ULL * 1024 * 1024) /* XXX ? */
1487 if (fs
.fs_blob_size
> CS_MAX_BLOB_SIZE
) {
1493 kernel_blob_size
= CAST_DOWN(vm_size_t
, fs
.fs_blob_size
);
1494 kr
= ubc_cs_blob_allocate(&kernel_blob_addr
, &kernel_blob_size
);
1495 if (kr
!= KERN_SUCCESS
) {
1501 if(uap
->cmd
== F_ADDSIGS
) {
1502 error
= copyin(fs
.fs_blob_start
,
1503 (void *) kernel_blob_addr
,
1505 } else /* F_ADDFILESIGS */ {
1506 error
= vn_rdwr(UIO_READ
,
1508 (caddr_t
) kernel_blob_addr
,
1510 fs
.fs_file_start
+ fs
.fs_blob_start
,
1519 ubc_cs_blob_deallocate(kernel_blob_addr
,
1525 error
= ubc_cs_blob_add(
1527 CPU_TYPE_ANY
, /* not for a specific architecture */
1532 ubc_cs_blob_deallocate(kernel_blob_addr
,
1535 /* ubc_blob_add() has consumed "kernel_blob_addr" */
1538 (void) vnode_put(vp
);
1542 case F_MARKDEPENDENCY
: {
1543 struct vnode
*root_vp
;
1544 struct vnode_attr va
;
1545 vfs_context_t ctx
= vfs_context_current();
1548 if ((current_proc()->p_flag
& P_DEPENDENCY_CAPABLE
) == 0) {
1553 if (fp
->f_type
!= DTYPE_VNODE
) {
1558 vp
= (struct vnode
*)fp
->f_data
;
1561 if (vnode_getwithref(vp
)) {
1566 // the passed in vnode must be the root dir of the file system
1567 if (VFS_ROOT(vp
->v_mount
, &root_vp
, ctx
) != 0 || vp
!= root_vp
) {
1574 // get the owner of the root dir
1576 VATTR_WANTED(&va
, va_uid
);
1577 if (vnode_getattr(vp
, &va
, ctx
) != 0) {
1583 // and last, check that the caller is the super user or
1584 // the owner of the mount point
1585 cred
= vfs_context_ucred(ctx
);
1586 if (!is_suser() && va
.va_uid
!= kauth_cred_getuid(cred
)) {
1592 // if all those checks pass then we can mark the dependency
1593 vfs_markdependency(vp
->v_mount
);
1601 case F_GETPROTECTIONCLASS
: {
1602 // stub to make the API work
1603 printf("Reached F_GETPROTECTIONCLASS, returning without action\n");
1608 case F_SETPROTECTIONCLASS
: {
1609 // stub to make the API work
1610 printf("Reached F_SETPROTECTIONCLASS, returning without action\n");
1618 * This is an fcntl() that we d not recognize at this level;
1619 * if this is a vnode, we send it down into the VNOP_IOCTL
1620 * for this vnode; this can include special devices, and will
1621 * effectively overload fcntl() to send ioctl()'s.
1623 if((uap
->cmd
& IOC_VOID
) && (uap
->cmd
& IOC_INOUT
)){
1628 if (fp
->f_type
!= DTYPE_VNODE
) {
1632 vp
= (struct vnode
*)fp
->f_data
;
1635 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1636 #define STK_PARAMS 128
1637 char stkbuf
[STK_PARAMS
];
1641 * For this to work properly, we have to copy in the
1642 * ioctl() cmd argument if there is one; we must also
1643 * check that a command parameter, if present, does
1644 * not exceed the maximum command length dictated by
1645 * the number of bits we have available in the command
1646 * to represent a structure length. Finally, we have
1647 * to copy the results back out, if it is that type of
1650 size
= IOCPARM_LEN(uap
->cmd
);
1651 if (size
> IOCPARM_MAX
) {
1652 (void)vnode_put(vp
);
1658 if (size
> sizeof (stkbuf
)) {
1659 if ((memp
= (caddr_t
)kalloc(size
)) == 0) {
1660 (void)vnode_put(vp
);
1669 if (uap
->cmd
& IOC_IN
) {
1672 error
= copyin(argp
, data
, size
);
1674 (void)vnode_put(vp
);
1682 *(user_addr_t
*)data
= argp
;
1684 *(uint32_t *)data
= (uint32_t)argp
;
1687 } else if ((uap
->cmd
& IOC_OUT
) && size
) {
1689 * Zero the buffer so the user always
1690 * gets back something deterministic.
1693 } else if (uap
->cmd
& IOC_VOID
) {
1695 *(user_addr_t
*)data
= argp
;
1697 *(uint32_t *)data
= (uint32_t)argp
;
1701 error
= VNOP_IOCTL(vp
, uap
->cmd
, CAST_DOWN(caddr_t
, data
), 0, &context
);
1703 (void)vnode_put(vp
);
1705 /* Copy any output data to user */
1706 if (error
== 0 && (uap
->cmd
& IOC_OUT
) && size
)
1707 error
= copyout(data
, argp
, size
);
1715 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
1716 fp_drop(p
, fd
, fp
, 0);
1719 fp_drop(p
, fd
, fp
, 1);
1728 * Description: Common code for dup, dup2, and fcntl(F_DUPFD).
1730 * Parameters: p Process performing the dup
1732 * new The fd to dup it to
1733 * retval Pointer to the call return area
1735 * Returns: 0 Success
1740 * *retval (modified) The new descriptor
1742 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
1745 * Notes: This function may drop and reacquire this lock; it is unsafe
1746 * for a caller to assume that other state protected by the lock
1747 * has not been subsequently changes out from under it.
1750 finishdup(proc_t p
, struct filedesc
*fdp
, int old
, int new, int32_t *retval
)
1752 struct fileproc
*nfp
;
1753 struct fileproc
*ofp
;
1759 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
1762 if ((ofp
= fdp
->fd_ofiles
[old
]) == NULL
||
1763 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
)) {
1770 error
= mac_file_check_dup(proc_ucred(p
), ofp
->f_fglob
, new);
1780 MALLOC_ZONE(nfp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
1781 /* Failure check follows proc_fdlock() due to handling requirements */
1791 bzero(nfp
, sizeof(struct fileproc
));
1794 nfp
->f_fglob
= ofp
->f_fglob
;
1798 if (fdp
->fd_ofiles
[new] != 0)
1799 panic("finishdup: overwriting fd_ofiles with new %d\n", new);
1800 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0)
1801 panic("finishdup: unreserved fileflags with new %d\n", new);
1804 if (new > fdp
->fd_lastfile
)
1805 fdp
->fd_lastfile
= new;
1806 procfdtbl_releasefd(p
, new, nfp
);
1815 * Description: The implementation of the close(2) system call
1817 * Parameters: p Process in whose per process file table
1818 * the close is to occur
1819 * uap->fd fd to be closed
1822 * Returns: 0 Success
1823 * fp_lookup:EBADF Bad file descriptor
1824 * close_internal:EBADF
1825 * close_internal:??? Anything returnable by a per-fileops
1829 close(proc_t p
, struct close_args
*uap
, int32_t *retval
)
1831 __pthread_testcancel(1);
1832 return(close_nocancel(p
, (struct close_nocancel_args
*)uap
, retval
));
1837 close_nocancel(proc_t p
, struct close_nocancel_args
*uap
, __unused
int32_t *retval
)
1839 struct fileproc
*fp
;
1843 AUDIT_SYSCLOSE(p
, fd
);
1847 if ( (error
= fp_lookup(p
,fd
,&fp
, 1)) ) {
1852 error
= close_internal_locked(p
, fd
, fp
, 0);
1861 * close_internal_locked
1863 * Close a file descriptor.
1865 * Parameters: p Process in whose per process file table
1866 * the close is to occur
1867 * fd fd to be closed
1868 * fp fileproc associated with the fd
1870 * Returns: 0 Success
1871 * EBADF fd already in close wait state
1872 * closef_locked:??? Anything returnable by a per-fileops
1875 * Locks: Assumes proc_fdlock for process is held by the caller and returns
1878 * Notes: This function may drop and reacquire this lock; it is unsafe
1879 * for a caller to assume that other state protected by the lock
1880 * has not been subsequently changes out from under it, if the
1881 * caller made the call with the lock held.
1884 close_internal_locked(proc_t p
, int fd
, struct fileproc
*fp
, int flags
)
1886 struct filedesc
*fdp
= p
->p_fd
;
1888 int resvfd
= flags
& FD_DUP2RESV
;
1892 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
1895 /* Keep people from using the filedesc while we are closing it */
1896 procfdtbl_markclosefd(p
, fd
);
1899 if ((fp
->f_flags
& FP_CLOSING
) == FP_CLOSING
) {
1900 panic("close_internal_locked: being called on already closing fd\n");
1905 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
1906 panic("close_internal: unreserved fileflags with fd %d\n", fd
);
1909 fp
->f_flags
|= FP_CLOSING
;
1911 if ( (fp
->f_flags
& FP_AIOISSUED
) || kauth_authorize_fileop_has_listeners() ) {
1915 if ( (fp
->f_type
== DTYPE_VNODE
) && kauth_authorize_fileop_has_listeners() ) {
1917 * call out to allow 3rd party notification of close.
1918 * Ignore result of kauth_authorize_fileop call.
1920 if (vnode_getwithref((vnode_t
)fp
->f_data
) == 0) {
1921 u_int fileop_flags
= 0;
1922 if ((fp
->f_flags
& FP_WRITTEN
) != 0)
1923 fileop_flags
|= KAUTH_FILEOP_CLOSE_MODIFIED
;
1924 kauth_authorize_fileop(fp
->f_fglob
->fg_cred
, KAUTH_FILEOP_CLOSE
,
1925 (uintptr_t)fp
->f_data
, (uintptr_t)fileop_flags
);
1926 vnode_put((vnode_t
)fp
->f_data
);
1929 if (fp
->f_flags
& FP_AIOISSUED
)
1931 * cancel all async IO requests that can be cancelled.
1933 _aio_close( p
, fd
);
1938 if (fd
< fdp
->fd_knlistsize
)
1939 knote_fdclose(p
, fd
);
1941 if (fp
->f_flags
& FP_WAITEVENT
)
1942 (void)waitevent_close(p
, fp
);
1944 if ((fp
->f_flags
& FP_INCHRREAD
) == 0)
1945 fileproc_drain(p
, fp
);
1950 error
= closef_locked(fp
, fp
->f_fglob
, p
);
1951 if ((fp
->f_flags
& FP_WAITCLOSE
) == FP_WAITCLOSE
)
1952 wakeup(&fp
->f_flags
);
1953 fp
->f_flags
&= ~(FP_WAITCLOSE
| FP_CLOSING
);
1957 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
1963 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
1964 panic("close with reserved fd returns with freed fd:%d: proc: %x\n", fd
, (unsigned int)p
);
1975 * Description: Return status information about a file descriptor.
1977 * Parameters: p The process doing the fstat
1979 * ub The user stat buffer
1980 * xsecurity The user extended security
1981 * buffer, or 0 if none
1982 * xsecurity_size The size of xsecurity, or 0
1984 * isstat64 Flag to indicate 64 bit version
1985 * for inode size, etc.
1987 * Returns: 0 Success
1990 * fp_lookup:EBADF Bad file descriptor
1991 * vnode_getwithref:???
1993 * vnode_getwithref:???
2000 * Notes: Internal implementation for all other fstat() related
2003 * XXX switch on node type is bogus; need a stat in struct
2004 * XXX fileops instead.
2007 fstat1(proc_t p
, int fd
, user_addr_t ub
, user_addr_t xsecurity
, user_addr_t xsecurity_size
, int isstat64
)
2009 struct fileproc
*fp
;
2015 struct user64_stat user64_sb
;
2016 struct user32_stat user32_sb
;
2017 struct user64_stat64 user64_sb64
;
2018 struct user32_stat64 user32_sb64
;
2024 kauth_filesec_t fsec
;
2025 user_size_t xsecurity_bufsize
;
2026 vfs_context_t ctx
= vfs_context_current();
2032 if ((error
= fp_lookup(p
, fd
, &fp
, 0)) != 0) {
2037 fsec
= KAUTH_FILESEC_NONE
;
2039 sbptr
= (void *)&source
;
2044 if ((error
= vnode_getwithref((vnode_t
)data
)) == 0) {
2046 * If the caller has the file open, and is not
2047 * requesting extended security information, we are
2048 * going to let them get the basic stat information.
2050 if (xsecurity
== USER_ADDR_NULL
) {
2051 error
= vn_stat_noauth((vnode_t
)data
, sbptr
, NULL
, isstat64
, ctx
);
2053 error
= vn_stat((vnode_t
)data
, sbptr
, &fsec
, isstat64
, ctx
);
2056 AUDIT_ARG(vnpath
, (struct vnode
*)data
, ARG_VNODE1
);
2057 (void)vnode_put((vnode_t
)data
);
2063 error
= soo_stat((struct socket
*)data
, sbptr
, isstat64
);
2065 #endif /* SOCKETS */
2068 error
= pipe_stat((void *)data
, sbptr
, isstat64
);
2072 error
= pshm_stat((void *)data
, sbptr
, isstat64
);
2076 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2077 error
= kqueue_stat(fp
, sbptr
, isstat64
, p
);
2078 thread_funnel_set(kernel_flock
, funnel_state
);
2088 if (isstat64
!= 0) {
2089 source
.sb64
.st_lspare
= 0;
2090 source
.sb64
.st_qspare
[0] = 0LL;
2091 source
.sb64
.st_qspare
[1] = 0LL;
2093 if (IS_64BIT_PROCESS(current_proc())) {
2094 munge_user64_stat64(&source
.sb64
, &dest
.user64_sb64
);
2095 my_size
= sizeof(dest
.user64_sb64
);
2096 sbp
= (caddr_t
)&dest
.user64_sb64
;
2098 munge_user32_stat64(&source
.sb64
, &dest
.user32_sb64
);
2099 my_size
= sizeof(dest
.user32_sb64
);
2100 sbp
= (caddr_t
)&dest
.user32_sb64
;
2103 source
.sb
.st_lspare
= 0;
2104 source
.sb
.st_qspare
[0] = 0LL;
2105 source
.sb
.st_qspare
[1] = 0LL;
2106 if (IS_64BIT_PROCESS(current_proc())) {
2107 munge_user64_stat(&source
.sb
, &dest
.user64_sb
);
2108 my_size
= sizeof(dest
.user64_sb
);
2109 sbp
= (caddr_t
)&dest
.user64_sb
;
2111 munge_user32_stat(&source
.sb
, &dest
.user32_sb
);
2112 my_size
= sizeof(dest
.user32_sb
);
2113 sbp
= (caddr_t
)&dest
.user32_sb
;
2117 error
= copyout(sbp
, ub
, my_size
);
2120 /* caller wants extended security information? */
2121 if (xsecurity
!= USER_ADDR_NULL
) {
2123 /* did we get any? */
2124 if (fsec
== KAUTH_FILESEC_NONE
) {
2125 if (susize(xsecurity_size
, 0) != 0) {
2130 /* find the user buffer size */
2131 xsecurity_bufsize
= fusize(xsecurity_size
);
2133 /* copy out the actual data size */
2134 if (susize(xsecurity_size
, KAUTH_FILESEC_COPYSIZE(fsec
)) != 0) {
2139 /* if the caller supplied enough room, copy out to it */
2140 if (xsecurity_bufsize
>= KAUTH_FILESEC_COPYSIZE(fsec
))
2141 error
= copyout(fsec
, xsecurity
, KAUTH_FILESEC_COPYSIZE(fsec
));
2145 fp_drop(p
, fd
, fp
, 0);
2147 kauth_filesec_free(fsec
);
2155 * Description: Extended version of fstat supporting returning extended
2156 * security information
2158 * Parameters: p The process doing the fstat
2159 * uap->fd The fd to stat
2160 * uap->ub The user stat buffer
2161 * uap->xsecurity The user extended security
2162 * buffer, or 0 if none
2163 * uap->xsecurity_size The size of xsecurity, or 0
2165 * Returns: 0 Success
2166 * !0 Errno (see fstat1)
2169 fstat_extended(proc_t p
, struct fstat_extended_args
*uap
, __unused
int32_t *retval
)
2171 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 0));
2178 * Description: Get file status for the file associated with fd
2180 * Parameters: p The process doing the fstat
2181 * uap->fd The fd to stat
2182 * uap->ub The user stat buffer
2184 * Returns: 0 Success
2185 * !0 Errno (see fstat1)
2188 fstat(proc_t p
, register struct fstat_args
*uap
, __unused
int32_t *retval
)
2190 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 0));
2197 * Description: Extended version of fstat64 supporting returning extended
2198 * security information
2200 * Parameters: p The process doing the fstat
2201 * uap->fd The fd to stat
2202 * uap->ub The user stat buffer
2203 * uap->xsecurity The user extended security
2204 * buffer, or 0 if none
2205 * uap->xsecurity_size The size of xsecurity, or 0
2207 * Returns: 0 Success
2208 * !0 Errno (see fstat1)
2211 fstat64_extended(proc_t p
, struct fstat64_extended_args
*uap
, __unused
int32_t *retval
)
2213 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 1));
2220 * Description: Get 64 bit version of the file status for the file associated
2223 * Parameters: p The process doing the fstat
2224 * uap->fd The fd to stat
2225 * uap->ub The user stat buffer
2227 * Returns: 0 Success
2228 * !0 Errno (see fstat1)
2231 fstat64(proc_t p
, register struct fstat64_args
*uap
, __unused
int32_t *retval
)
2233 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 1));
2240 * Description: Return pathconf information about a file descriptor.
2242 * Parameters: p Process making the request
2243 * uap->fd fd to get information about
2244 * uap->name Name of information desired
2245 * retval Pointer to the call return area
2247 * Returns: 0 Success
2249 * fp_lookup:EBADF Bad file descriptor
2250 * vnode_getwithref:???
2254 * *retval (modified) Returned information (numeric)
2257 fpathconf(proc_t p
, struct fpathconf_args
*uap
, int32_t *retval
)
2260 struct fileproc
*fp
;
2267 AUDIT_ARG(fd
, uap
->fd
);
2268 if ( (error
= fp_lookup(p
, fd
, &fp
, 0)) )
2276 if (uap
->name
!= _PC_PIPE_BUF
) {
2285 if (uap
->name
!= _PC_PIPE_BUF
) {
2294 vp
= (struct vnode
*)data
;
2296 if ( (error
= vnode_getwithref(vp
)) == 0) {
2297 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
2299 error
= vn_pathconf(vp
, uap
->name
, retval
, vfs_context_current());
2301 (void)vnode_put(vp
);
2308 case DTYPE_FSEVENTS
:
2315 fp_drop(p
, fd
, fp
, 0);
2320 * Statistics counter for the number of times a process calling fdalloc()
2321 * has resulted in an expansion of the per process open file table.
2323 * XXX This would likely be of more use if it were per process
2331 * Description: Allocate a file descriptor for the process.
2333 * Parameters: p Process to allocate the fd in
2334 * want The fd we would prefer to get
2335 * result Pointer to fd we got
2337 * Returns: 0 Success
2342 * *result (modified) The fd which was allocated
2345 fdalloc(proc_t p
, int want
, int *result
)
2347 struct filedesc
*fdp
= p
->p_fd
;
2349 int lim
, last
, numfiles
, oldnfiles
;
2350 struct fileproc
**newofiles
, **ofiles
;
2351 char *newofileflags
;
2354 * Search for a free descriptor starting at the higher
2355 * of want or fd_freefile. If that fails, consider
2356 * expanding the ofile array.
2359 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2362 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
2364 last
= min(fdp
->fd_nfiles
, lim
);
2365 if ((i
= want
) < fdp
->fd_freefile
)
2366 i
= fdp
->fd_freefile
;
2367 for (; i
< last
; i
++) {
2368 if (fdp
->fd_ofiles
[i
] == NULL
&& !(fdp
->fd_ofileflags
[i
] & UF_RESERVED
)) {
2369 procfdtbl_reservefd(p
, i
);
2370 if (i
> fdp
->fd_lastfile
)
2371 fdp
->fd_lastfile
= i
;
2372 if (want
<= fdp
->fd_freefile
)
2373 fdp
->fd_freefile
= i
;
2380 * No space in current array. Expand?
2382 if (fdp
->fd_nfiles
>= lim
)
2384 if (fdp
->fd_nfiles
< NDEXTENT
)
2385 numfiles
= NDEXTENT
;
2387 numfiles
= 2 * fdp
->fd_nfiles
;
2392 MALLOC_ZONE(newofiles
, struct fileproc
**,
2393 numfiles
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
2395 if (newofiles
== NULL
) {
2398 if (fdp
->fd_nfiles
>= numfiles
) {
2399 FREE_ZONE(newofiles
, numfiles
* OFILESIZE
, M_OFILETABL
);
2402 newofileflags
= (char *) &newofiles
[numfiles
];
2404 * Copy the existing ofile and ofileflags arrays
2405 * and zero the new portion of each array.
2407 oldnfiles
= fdp
->fd_nfiles
;
2408 (void) memcpy(newofiles
, fdp
->fd_ofiles
,
2409 oldnfiles
* sizeof(*fdp
->fd_ofiles
));
2410 (void) memset(&newofiles
[oldnfiles
], 0,
2411 (numfiles
- oldnfiles
) * sizeof(*fdp
->fd_ofiles
));
2413 (void) memcpy(newofileflags
, fdp
->fd_ofileflags
,
2414 oldnfiles
* sizeof(*fdp
->fd_ofileflags
));
2415 (void) memset(&newofileflags
[oldnfiles
], 0,
2416 (numfiles
- oldnfiles
) *
2417 sizeof(*fdp
->fd_ofileflags
));
2418 ofiles
= fdp
->fd_ofiles
;
2419 fdp
->fd_ofiles
= newofiles
;
2420 fdp
->fd_ofileflags
= newofileflags
;
2421 fdp
->fd_nfiles
= numfiles
;
2422 FREE_ZONE(ofiles
, oldnfiles
* OFILESIZE
, M_OFILETABL
);
2431 * Description: Check to see whether n user file descriptors are available
2434 * Parameters: p Process to check in
2435 * n The number of fd's desired
2440 * Locks: Assumes proc_fdlock for process is held by the caller
2442 * Notes: The answer only remains valid so long as the proc_fdlock is
2443 * held by the caller.
2446 fdavail(proc_t p
, int n
)
2448 struct filedesc
*fdp
= p
->p_fd
;
2449 struct fileproc
**fpp
;
2453 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
2454 if ((i
= lim
- fdp
->fd_nfiles
) > 0 && (n
-= i
) <= 0)
2456 fpp
= &fdp
->fd_ofiles
[fdp
->fd_freefile
];
2457 flags
= &fdp
->fd_ofileflags
[fdp
->fd_freefile
];
2458 for (i
= fdp
->fd_nfiles
- fdp
->fd_freefile
; --i
>= 0; fpp
++, flags
++)
2459 if (*fpp
== NULL
&& !(*flags
& UF_RESERVED
) && --n
<= 0)
2468 * Description: Legacy KPI wrapper function for _fdrelse
2470 * Parameters: p Process in which fd lives
2475 * Locks: Assumes proc_fdlock for process is held by the caller
2478 fdrelse(proc_t p
, int fd
)
2487 * Description: Get the fileproc pointer for the given fd from the per process
2488 * open file table without taking an explicit reference on it.
2490 * Parameters: p Process containing fd
2491 * fd fd to obtain fileproc for
2492 * resultfp Pointer to pointer return area
2494 * Returns: 0 Success
2498 * *resultfp (modified) Pointer to fileproc pointer
2500 * Locks: Assumes proc_fdlock for process is held by the caller
2502 * Notes: Because there is no reference explicitly taken, the returned
2503 * fileproc pointer is only valid so long as the proc_fdlock
2504 * remains held by the caller.
2507 fdgetf_noref(proc_t p
, int fd
, struct fileproc
**resultfp
)
2509 struct filedesc
*fdp
= p
->p_fd
;
2510 struct fileproc
*fp
;
2512 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2513 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2514 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2526 * Description: Get fileproc and vnode pointer for a given fd from the per
2527 * process open file table of the specified process, and if
2528 * successful, increment the f_iocount
2530 * Parameters: p Process in which fd lives
2531 * fd fd to get information for
2532 * resultfp Pointer to result fileproc
2533 * pointer area, or 0 if none
2534 * resultvp Pointer to result vnode pointer
2535 * area, or 0 if none
2537 * Returns: 0 Success
2538 * EBADF Bad file descriptor
2539 * ENOTSUP fd does not refer to a vnode
2542 * *resultfp (modified) Fileproc pointer
2543 * *resultvp (modified) vnode pointer
2545 * Notes: The resultfp and resultvp fields are optional, and may be
2546 * independently specified as NULL to skip returning information
2548 * Locks: Internally takes and releases proc_fdlock
2551 fp_getfvp(proc_t p
, int fd
, struct fileproc
**resultfp
, struct vnode
**resultvp
)
2553 struct filedesc
*fdp
= p
->p_fd
;
2554 struct fileproc
*fp
;
2556 proc_fdlock_spin(p
);
2557 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2558 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2559 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2563 if (fp
->f_type
!= DTYPE_VNODE
) {
2572 *resultvp
= (struct vnode
*)fp
->f_data
;
2582 * Description: Get fileproc, vnode pointer, and vid for a given fd from the
2583 * per process open file table of the specified process, and if
2584 * successful, increment the f_iocount
2586 * Parameters: p Process in which fd lives
2587 * fd fd to get information for
2588 * resultfp Pointer to result fileproc
2589 * pointer area, or 0 if none
2590 * resultvp Pointer to result vnode pointer
2591 * area, or 0 if none
2592 * vidp Pointer to resuld vid area
2594 * Returns: 0 Success
2595 * EBADF Bad file descriptor
2596 * ENOTSUP fd does not refer to a vnode
2599 * *resultfp (modified) Fileproc pointer
2600 * *resultvp (modified) vnode pointer
2603 * Notes: The resultfp and resultvp fields are optional, and may be
2604 * independently specified as NULL to skip returning information
2606 * Locks: Internally takes and releases proc_fdlock
2609 fp_getfvpandvid(proc_t p
, int fd
, struct fileproc
**resultfp
,
2610 struct vnode
**resultvp
, uint32_t *vidp
)
2612 struct filedesc
*fdp
= p
->p_fd
;
2613 struct fileproc
*fp
;
2615 proc_fdlock_spin(p
);
2616 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2617 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2618 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2622 if (fp
->f_type
!= DTYPE_VNODE
) {
2631 *resultvp
= (struct vnode
*)fp
->f_data
;
2633 *vidp
= (uint32_t)vnode_vid((struct vnode
*)fp
->f_data
);
2643 * Description: Get fileproc and socket pointer for a given fd from the
2644 * per process open file table of the specified process, and if
2645 * successful, increment the f_iocount
2647 * Parameters: p Process in which fd lives
2648 * fd fd to get information for
2649 * resultfp Pointer to result fileproc
2650 * pointer area, or 0 if none
2651 * results Pointer to result socket
2652 * pointer area, or 0 if none
2654 * Returns: EBADF The file descriptor is invalid
2655 * EOPNOTSUPP The file descriptor is not a socket
2659 * *resultfp (modified) Fileproc pointer
2660 * *results (modified) socket pointer
2662 * Notes: EOPNOTSUPP should probably be ENOTSOCK; this function is only
2663 * ever called from accept1().
2666 fp_getfsock(proc_t p
, int fd
, struct fileproc
**resultfp
,
2667 struct socket
**results
)
2669 struct filedesc
*fdp
= p
->p_fd
;
2670 struct fileproc
*fp
;
2672 proc_fdlock_spin(p
);
2673 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2674 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2675 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2679 if (fp
->f_type
!= DTYPE_SOCKET
) {
2688 *results
= (struct socket
*)fp
->f_data
;
2698 * Description: Get fileproc and kqueue pointer for a given fd from the
2699 * per process open file table of the specified process, and if
2700 * successful, increment the f_iocount
2702 * Parameters: p Process in which fd lives
2703 * fd fd to get information for
2704 * resultfp Pointer to result fileproc
2705 * pointer area, or 0 if none
2706 * resultkq Pointer to result kqueue
2707 * pointer area, or 0 if none
2709 * Returns: EBADF The file descriptor is invalid
2710 * EBADF The file descriptor is not a socket
2714 * *resultfp (modified) Fileproc pointer
2715 * *resultkq (modified) kqueue pointer
2717 * Notes: The second EBADF should probably be something else to make
2718 * the error condition distinct.
2721 fp_getfkq(proc_t p
, int fd
, struct fileproc
**resultfp
,
2722 struct kqueue
**resultkq
)
2724 struct filedesc
*fdp
= p
->p_fd
;
2725 struct fileproc
*fp
;
2727 proc_fdlock_spin(p
);
2728 if ( fd
< 0 || fd
>= fdp
->fd_nfiles
||
2729 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2730 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2734 if (fp
->f_type
!= DTYPE_KQUEUE
) {
2743 *resultkq
= (struct kqueue
*)fp
->f_data
;
2753 * Description: Get fileproc and POSIX shared memory pointer for a given fd
2754 * from the per process open file table of the specified process
2755 * and if successful, increment the f_iocount
2757 * Parameters: p Process in which fd lives
2758 * fd fd to get information for
2759 * resultfp Pointer to result fileproc
2760 * pointer area, or 0 if none
2761 * resultpshm Pointer to result POSIX
2762 * shared memory pointer
2763 * pointer area, or 0 if none
2765 * Returns: EBADF The file descriptor is invalid
2766 * EBADF The file descriptor is not a POSIX
2767 * shared memory area
2771 * *resultfp (modified) Fileproc pointer
2772 * *resultpshm (modified) POSIX shared memory pointer
2774 * Notes: The second EBADF should probably be something else to make
2775 * the error condition distinct.
2778 fp_getfpshm(proc_t p
, int fd
, struct fileproc
**resultfp
,
2779 struct pshmnode
**resultpshm
)
2781 struct filedesc
*fdp
= p
->p_fd
;
2782 struct fileproc
*fp
;
2784 proc_fdlock_spin(p
);
2785 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2786 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2787 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2791 if (fp
->f_type
!= DTYPE_PSXSHM
) {
2801 *resultpshm
= (struct pshmnode
*)fp
->f_data
;
2811 * Description: Get fileproc and POSIX semaphore pointer for a given fd from
2812 * the per process open file table of the specified process
2813 * and if successful, increment the f_iocount
2815 * Parameters: p Process in which fd lives
2816 * fd fd to get information for
2817 * resultfp Pointer to result fileproc
2818 * pointer area, or 0 if none
2819 * resultpsem Pointer to result POSIX
2820 * semaphore pointer area, or
2823 * Returns: EBADF The file descriptor is invalid
2824 * EBADF The file descriptor is not a POSIX
2829 * *resultfp (modified) Fileproc pointer
2830 * *resultpsem (modified) POSIX semaphore pointer
2832 * Notes: The second EBADF should probably be something else to make
2833 * the error condition distinct.
2835 * In order to support unnamed POSIX semaphores, the named
2836 * POSIX semaphores will have to move out of the per-process
2837 * open filetable, and into a global table that is shared with
2838 * unnamed POSIX semaphores, since unnamed POSIX semaphores
2839 * are typically used by declaring instances in shared memory,
2840 * and there's no other way to do this without changing the
2841 * underlying type, which would introduce binary compatibility
2845 fp_getfpsem(proc_t p
, int fd
, struct fileproc
**resultfp
,
2846 struct psemnode
**resultpsem
)
2848 struct filedesc
*fdp
= p
->p_fd
;
2849 struct fileproc
*fp
;
2851 proc_fdlock_spin(p
);
2852 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2853 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2854 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2858 if (fp
->f_type
!= DTYPE_PSXSEM
) {
2867 *resultpsem
= (struct psemnode
*)fp
->f_data
;
2877 * Description: Get fileproc and pipe pointer for a given fd from the
2878 * per process open file table of the specified process
2879 * and if successful, increment the f_iocount
2881 * Parameters: p Process in which fd lives
2882 * fd fd to get information for
2883 * resultfp Pointer to result fileproc
2884 * pointer area, or 0 if none
2885 * resultpipe Pointer to result pipe
2886 * pointer area, or 0 if none
2888 * Returns: EBADF The file descriptor is invalid
2889 * EBADF The file descriptor is not a socket
2893 * *resultfp (modified) Fileproc pointer
2894 * *resultpipe (modified) pipe pointer
2896 * Notes: The second EBADF should probably be something else to make
2897 * the error condition distinct.
2900 fp_getfpipe(proc_t p
, int fd
, struct fileproc
**resultfp
,
2901 struct pipe
**resultpipe
)
2903 struct filedesc
*fdp
= p
->p_fd
;
2904 struct fileproc
*fp
;
2906 proc_fdlock_spin(p
);
2907 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2908 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2909 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2913 if (fp
->f_type
!= DTYPE_PIPE
) {
2922 *resultpipe
= (struct pipe
*)fp
->f_data
;
2929 #define DTYPE_ATALK -1 /* XXX This does not belong here */
2935 * Description: Get fileproc and atalk pointer for a given fd from the
2936 * per process open file table of the specified process
2937 * and if successful, increment the f_iocount
2939 * Parameters: p Process in which fd lives
2940 * fd fd to get information for
2941 * resultfp Pointer to result fileproc
2942 * pointer area, or 0 if none
2943 * resultatalk Pointer to result atalk
2944 * pointer area, or 0 if none
2945 * Returns: EBADF The file descriptor is invalid
2946 * EBADF The file descriptor is not a socket
2950 * *resultfp (modified) Fileproc pointer
2951 * *resultatalk (modified) atalk pointer
2953 * Notes: The second EBADF should probably be something else to make
2954 * the error condition distinct.
2956 * XXX This code is specific to AppleTalk protocol support, and
2957 * XXX should be conditionally compiled
2960 fp_getfatalk(proc_t p
, int fd
, struct fileproc
**resultfp
,
2961 struct atalk
**resultatalk
)
2963 struct filedesc
*fdp
= p
->p_fd
;
2964 struct fileproc
*fp
;
2966 proc_fdlock_spin(p
);
2967 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2968 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2969 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2973 if (fp
->f_type
!= (DTYPE_ATALK
+1)) {
2982 *resultatalk
= (struct atalk
*)fp
->f_data
;
2993 * Description: Get fileproc pointer for a given fd from the per process
2994 * open file table of the specified process and if successful,
2995 * increment the f_iocount
2997 * Parameters: p Process in which fd lives
2998 * fd fd to get information for
2999 * resultfp Pointer to result fileproc
3000 * pointer area, or 0 if none
3001 * locked !0 if the caller holds the
3002 * proc_fdlock, 0 otherwise
3004 * Returns: 0 Success
3005 * EBADF Bad file descriptor
3008 * *resultfp (modified) Fileproc pointer
3010 * Locks: If the argument 'locked' is non-zero, then the caller is
3011 * expected to have taken and held the proc_fdlock; if it is
3012 * zero, than this routine internally takes and drops this lock.
3015 fp_lookup(proc_t p
, int fd
, struct fileproc
**resultfp
, int locked
)
3017 struct filedesc
*fdp
= p
->p_fd
;
3018 struct fileproc
*fp
;
3021 proc_fdlock_spin(p
);
3022 if (fd
< 0 || fdp
== NULL
|| fd
>= fdp
->fd_nfiles
||
3023 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3024 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3043 * Description: Set the FP_WRITTEN flag on the fileproc and drop the I/O
3044 * reference previously taken by calling fp_lookup et. al.
3046 * Parameters: p Process in which the fd lives
3047 * fd fd associated with the fileproc
3048 * fp fileproc on which to set the
3049 * flag and drop the reference
3051 * Returns: 0 Success
3052 * fp_drop:EBADF Bad file descriptor
3054 * Locks: This function internally takes and drops the proc_fdlock for
3055 * the supplied process
3057 * Notes: The fileproc must correspond to the fd in the supplied proc
3060 fp_drop_written(proc_t p
, int fd
, struct fileproc
*fp
)
3064 proc_fdlock_spin(p
);
3066 fp
->f_flags
|= FP_WRITTEN
;
3068 error
= fp_drop(p
, fd
, fp
, 1);
3079 * Description: Set the FP_WAITEVENT flag on the fileproc and drop the I/O
3080 * reference previously taken by calling fp_lookup et. al.
3082 * Parameters: p Process in which the fd lives
3083 * fd fd associated with the fileproc
3084 * fp fileproc on which to set the
3085 * flag and drop the reference
3087 * Returns: 0 Success
3088 * fp_drop:EBADF Bad file descriptor
3090 * Locks: This function internally takes and drops the proc_fdlock for
3091 * the supplied process
3093 * Notes: The fileproc must correspond to the fd in the supplied proc
3096 fp_drop_event(proc_t p
, int fd
, struct fileproc
*fp
)
3100 proc_fdlock_spin(p
);
3102 fp
->f_flags
|= FP_WAITEVENT
;
3104 error
= fp_drop(p
, fd
, fp
, 1);
3115 * Description: Drop the I/O reference previously taken by calling fp_lookup
3118 * Parameters: p Process in which the fd lives
3119 * fd fd associated with the fileproc
3120 * fp fileproc on which to set the
3121 * flag and drop the reference
3122 * locked flag to internally take and
3123 * drop proc_fdlock if it is not
3124 * already held by the caller
3126 * Returns: 0 Success
3127 * EBADF Bad file descriptor
3129 * Locks: This function internally takes and drops the proc_fdlock for
3130 * the supplied process if 'locked' is non-zero, and assumes that
3131 * the caller already holds this lock if 'locked' is non-zero.
3133 * Notes: The fileproc must correspond to the fd in the supplied proc
3136 fp_drop(proc_t p
, int fd
, struct fileproc
*fp
, int locked
)
3138 struct filedesc
*fdp
= p
->p_fd
;
3142 proc_fdlock_spin(p
);
3143 if ((fp
== FILEPROC_NULL
) && (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3144 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3145 ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
3146 !(fdp
->fd_ofileflags
[fd
] & UF_CLOSING
)))) {
3153 if (p
->p_fpdrainwait
&& fp
->f_iocount
== 0) {
3154 p
->p_fpdrainwait
= 0;
3160 wakeup(&p
->p_fpdrainwait
);
3169 * Description: Given an fd, look it up in the current process's per process
3170 * open file table, and return its internal vnode pointer.
3172 * Parameters: fd fd to obtain vnode from
3173 * vpp pointer to vnode return area
3175 * Returns: 0 Success
3176 * EINVAL The fd does not refer to a
3177 * vnode fileproc entry
3178 * fp_lookup:EBADF Bad file descriptor
3181 * *vpp (modified) Returned vnode pointer
3183 * Locks: This function internally takes and drops the proc_fdlock for
3184 * the current process
3186 * Notes: If successful, this function increments the f_iocount on the
3187 * fd's corresponding fileproc.
3189 * The fileproc referenced is not returned; because of this, care
3190 * must be taken to not drop the last reference (e.g. by closing
3191 * the file). This is inhernely unsafe, since the reference may
3192 * not be recoverable from the vnode, if there is a subsequent
3193 * close that destroys the associate fileproc. The caller should
3194 * therefore retain their own reference on the fileproc so that
3195 * the f_iocount can be dropped subsequently. Failure to do this
3196 * can result in the returned pointer immediately becoming invalid
3197 * following the call.
3199 * Use of this function is discouraged.
3202 file_vnode(int fd
, struct vnode
**vpp
)
3204 proc_t p
= current_proc();
3205 struct fileproc
*fp
;
3208 proc_fdlock_spin(p
);
3209 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
3213 if (fp
->f_type
!= DTYPE_VNODE
) {
3214 fp_drop(p
, fd
, fp
,1);
3219 *vpp
= (struct vnode
*)fp
->f_data
;
3227 * file_vnode_withvid
3229 * Description: Given an fd, look it up in the current process's per process
3230 * open file table, and return its internal vnode pointer.
3232 * Parameters: fd fd to obtain vnode from
3233 * vpp pointer to vnode return area
3234 * vidp pointer to vid of the returned vnode
3236 * Returns: 0 Success
3237 * EINVAL The fd does not refer to a
3238 * vnode fileproc entry
3239 * fp_lookup:EBADF Bad file descriptor
3242 * *vpp (modified) Returned vnode pointer
3244 * Locks: This function internally takes and drops the proc_fdlock for
3245 * the current process
3247 * Notes: If successful, this function increments the f_iocount on the
3248 * fd's corresponding fileproc.
3250 * The fileproc referenced is not returned; because of this, care
3251 * must be taken to not drop the last reference (e.g. by closing
3252 * the file). This is inhernely unsafe, since the reference may
3253 * not be recoverable from the vnode, if there is a subsequent
3254 * close that destroys the associate fileproc. The caller should
3255 * therefore retain their own reference on the fileproc so that
3256 * the f_iocount can be dropped subsequently. Failure to do this
3257 * can result in the returned pointer immediately becoming invalid
3258 * following the call.
3260 * Use of this function is discouraged.
3263 file_vnode_withvid(int fd
, struct vnode
**vpp
, uint32_t * vidp
)
3265 proc_t p
= current_proc();
3266 struct fileproc
*fp
;
3270 proc_fdlock_spin(p
);
3271 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
3275 if (fp
->f_type
!= DTYPE_VNODE
) {
3276 fp_drop(p
, fd
, fp
,1);
3280 vp
= (struct vnode
*)fp
->f_data
;
3284 if ((vidp
!= NULL
) && (vp
!= NULLVP
))
3285 *vidp
= (uint32_t)vp
->v_id
;
3296 * Description: Given an fd, look it up in the current process's per process
3297 * open file table, and return its internal socket pointer.
3299 * Parameters: fd fd to obtain vnode from
3300 * sp pointer to socket return area
3302 * Returns: 0 Success
3303 * ENOTSOCK Not a socket
3304 * fp_lookup:EBADF Bad file descriptor
3307 * *sp (modified) Returned socket pointer
3309 * Locks: This function internally takes and drops the proc_fdlock for
3310 * the current process
3312 * Notes: If successful, this function increments the f_iocount on the
3313 * fd's corresponding fileproc.
3315 * The fileproc referenced is not returned; because of this, care
3316 * must be taken to not drop the last reference (e.g. by closing
3317 * the file). This is inhernely unsafe, since the reference may
3318 * not be recoverable from the socket, if there is a subsequent
3319 * close that destroys the associate fileproc. The caller should
3320 * therefore retain their own reference on the fileproc so that
3321 * the f_iocount can be dropped subsequently. Failure to do this
3322 * can result in the returned pointer immediately becoming invalid
3323 * following the call.
3325 * Use of this function is discouraged.
3328 file_socket(int fd
, struct socket
**sp
)
3330 proc_t p
= current_proc();
3331 struct fileproc
*fp
;
3334 proc_fdlock_spin(p
);
3335 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
3339 if (fp
->f_type
!= DTYPE_SOCKET
) {
3340 fp_drop(p
, fd
, fp
,1);
3344 *sp
= (struct socket
*)fp
->f_data
;
3354 * Description: Given an fd, look it up in the current process's per process
3355 * open file table, and return its fileproc's flags field.
3357 * Parameters: fd fd whose flags are to be
3359 * flags pointer to flags data area
3361 * Returns: 0 Success
3362 * ENOTSOCK Not a socket
3363 * fp_lookup:EBADF Bad file descriptor
3366 * *flags (modified) Returned flags field
3368 * Locks: This function internally takes and drops the proc_fdlock for
3369 * the current process
3371 * Notes: This function will internally increment and decrement the
3372 * f_iocount of the fileproc as part of its operation.
3375 file_flags(int fd
, int *flags
)
3378 proc_t p
= current_proc();
3379 struct fileproc
*fp
;
3382 proc_fdlock_spin(p
);
3383 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
3387 *flags
= (int)fp
->f_flag
;
3388 fp_drop(p
, fd
, fp
,1);
3398 * Description: Drop an iocount reference on an fd, and wake up any waiters
3399 * for draining (i.e. blocked in fileproc_drain() called during
3400 * the last attempt to close a file).
3402 * Parameters: fd fd on which an ioreference is
3405 * Returns: 0 Success
3406 * EBADF Bad file descriptor
3408 * Description: Given an fd, look it up in the current process's per process
3409 * open file table, and drop it's fileproc's f_iocount by one
3411 * Notes: This is intended as a corresponding operation to the functions
3412 * file_vnode() and file_socket() operations.
3414 * Technically, the close reference is supposed to be protected
3415 * by a fileproc_drain(), however, a drain will only block if
3416 * the fd refers to a character device, and that device has had
3417 * preparefileread() called on it. If it refers to something
3418 * other than a character device, then the drain will occur and
3419 * block each close attempt, rather than merely the last close.
3421 * Since it's possible for an fd that refers to a character
3422 * device to have an intermediate close followed by an open to
3423 * cause a different file to correspond to that descriptor,
3424 * unless there was a cautionary reference taken on the fileproc,
3425 * this is an inherently unsafe function. This happens in the
3426 * case where multiple fd's in a process refer to the same
3427 * character device (e.g. stdin/out/err pointing to a tty, etc.).
3429 * Use of this function is discouraged.
3434 struct fileproc
*fp
;
3435 proc_t p
= current_proc();
3438 proc_fdlock_spin(p
);
3439 if (fd
< 0 || fd
>= p
->p_fd
->fd_nfiles
||
3440 (fp
= p
->p_fd
->fd_ofiles
[fd
]) == NULL
||
3441 ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
3442 !(p
->p_fd
->fd_ofileflags
[fd
] & UF_CLOSING
))) {
3448 if (p
->p_fpdrainwait
&& fp
->f_iocount
== 0) {
3449 p
->p_fpdrainwait
= 0;
3455 wakeup(&p
->p_fpdrainwait
);
3463 * Description: Allocate an entry in the per process open file table and
3464 * return the corresponding fileproc and fd.
3466 * Parameters: p The process in whose open file
3467 * table the fd is to be allocated
3468 * resultfp Pointer to fileproc pointer
3470 * resultfd Pointer to fd return area
3473 * Returns: 0 Success
3474 * falloc:ENFILE Too many open files in system
3475 * falloc:EMFILE Too many open files in process
3476 * falloc:ENOMEM M_FILEPROC or M_FILEGLOB zone
3480 * *resultfd (modified) Returned fileproc pointer
3481 * *resultfd (modified) Returned fd
3483 * Locks: This function takes and drops the proc_fdlock; if this lock
3484 * is alread held, use falloc_locked() instead.
3486 * Notes: This function takes separate process and context arguments
3487 * solely to support kern_exec.c; otherwise, it would take
3488 * neither, and expect falloc_locked() to use the
3489 * vfs_context_current() routine internally.
3492 falloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
)
3497 error
= falloc_locked(p
, resultfp
, resultfd
, ctx
, 1);
3507 * Create a new open file structure and allocate
3508 * a file decriptor for the process that refers to it.
3510 * Returns: 0 Success
3512 * Description: Allocate an entry in the per process open file table and
3513 * return the corresponding fileproc and fd.
3515 * Parameters: p The process in whose open file
3516 * table the fd is to be allocated
3517 * resultfp Pointer to fileproc pointer
3519 * resultfd Pointer to fd return area
3521 * locked Flag to indicate whether the
3522 * caller holds proc_fdlock
3524 * Returns: 0 Success
3525 * ENFILE Too many open files in system
3526 * fdalloc:EMFILE Too many open files in process
3527 * ENOMEM M_FILEPROC or M_FILEGLOB zone
3532 * *resultfd (modified) Returned fileproc pointer
3533 * *resultfd (modified) Returned fd
3535 * Locks: If the parameter 'locked' is zero, this function takes and
3536 * drops the proc_fdlock; if non-zero, the caller must hold the
3539 * Notes: If you intend to use a non-zero 'locked' parameter, use the
3540 * utility function falloc() instead.
3542 * This function takes separate process and context arguments
3543 * solely to support kern_exec.c; otherwise, it would take
3544 * neither, and use the vfs_context_current() routine internally.
3547 falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
3548 vfs_context_t ctx
, int locked
)
3550 struct fileproc
*fp
, *fq
;
3551 struct fileglob
*fg
;
3556 if ( (error
= fdalloc(p
, 0, &nfd
)) ) {
3561 if (nfiles
>= maxfiles
) {
3568 error
= mac_file_check_create(proc_ucred(p
));
3577 * Allocate a new file descriptor.
3578 * If the process has file descriptor zero open, add to the list
3579 * of open files at that point, otherwise put it at the front of
3580 * the list of open files.
3584 MALLOC_ZONE(fp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
3590 MALLOC_ZONE(fg
, struct fileglob
*, sizeof(struct fileglob
), M_FILEGLOB
, M_WAITOK
);
3592 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
3597 bzero(fp
, sizeof(struct fileproc
));
3598 bzero(fg
, sizeof(struct fileglob
));
3599 lck_mtx_init(&fg
->fg_lock
, file_lck_grp
, file_lck_attr
);
3605 mac_file_label_init(fg
);
3608 kauth_cred_ref(ctx
->vc_ucred
);
3612 fp
->f_cred
= ctx
->vc_ucred
;
3615 mac_file_label_associate(fp
->f_cred
, fg
);
3618 lck_mtx_lock_spin(file_flist_lock
);
3622 if ( (fq
= p
->p_fd
->fd_ofiles
[0]) ) {
3623 LIST_INSERT_AFTER(fq
->f_fglob
, fg
, f_list
);
3625 LIST_INSERT_HEAD(&filehead
, fg
, f_list
);
3627 lck_mtx_unlock(file_flist_lock
);
3629 p
->p_fd
->fd_ofiles
[nfd
] = fp
;
3646 * Description: Free a file structure; drop the global open file count, and
3647 * drop the credential reference, if the fileglob has one, and
3648 * destroy the instance mutex before freeing
3650 * Parameters: fg Pointer to fileglob to be
3656 fg_free(struct fileglob
*fg
)
3658 lck_mtx_lock_spin(file_flist_lock
);
3659 LIST_REMOVE(fg
, f_list
);
3661 lck_mtx_unlock(file_flist_lock
);
3663 if (IS_VALID_CRED(fg
->fg_cred
)) {
3664 kauth_cred_unref(&fg
->fg_cred
);
3666 lck_mtx_destroy(&fg
->fg_lock
, file_lck_grp
);
3669 mac_file_label_destroy(fg
);
3671 FREE_ZONE(fg
, sizeof *fg
, M_FILEGLOB
);
3678 * Description: Perform close-on-exec processing for all files in a process
3679 * that are either marked as close-on-exec, or which were in the
3680 * process of being opened at the time of the execve
3682 * Parameters: p Pointer to process calling
3687 * Locks: This function internally takes and drops proc_fdlock()
3689 * Notes: This function drops and retakes the kernel funnel; this is
3690 * inherently unsafe, since another thread may have the
3693 * XXX: We should likely reverse the lock and funnel drop/acquire
3694 * order to avoid the small race window; it's also possible that
3695 * if the program doing the exec has an outstanding listen socket
3696 * and a network connection is completed asyncrhonously that we
3697 * will end up with a "ghost" socket reference in the new process.
3699 * This needs reworking to make it safe to remove the funnel from
3700 * the execve and posix_spawn system calls.
3705 struct filedesc
*fdp
= p
->p_fd
;
3707 struct fileproc
*fp
;
3710 i
= fdp
->fd_lastfile
;
3714 fp
= fdp
->fd_ofiles
[i
];
3716 ((fdp
->fd_ofileflags
[i
] & (UF_RESERVED
|UF_EXCLOSE
)) == UF_EXCLOSE
)
3718 || (fp
&& mac_file_check_inherit(proc_ucred(p
), fp
->f_fglob
))
3721 if (i
< fdp
->fd_knlistsize
)
3722 knote_fdclose(p
, i
);
3723 procfdtbl_clearfd(p
, i
);
3724 if (i
== fdp
->fd_lastfile
&& i
> 0)
3726 if (i
< fdp
->fd_freefile
)
3727 fdp
->fd_freefile
= i
;
3728 closef_locked(fp
, fp
->f_fglob
, p
);
3729 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
3740 * Description: Copy a filedesc structure. This is normally used as part of
3741 * forkproc() when forking a new process, to copy the per process
3742 * open file table over to the new process.
3744 * Parameters: p Process whose open file table
3745 * is to be copied (parent)
3746 * uth_cdir Per thread current working
3747 * cirectory, or NULL
3749 * Returns: NULL Copy failed
3750 * !NULL Pointer to new struct filedesc
3752 * Locks: This function internally takes and drops proc_fdlock()
3754 * Notes: Files are copied directly, ignoring the new resource limits
3755 * for the process that's being copied into. Since the descriptor
3756 * references are just additional references, this does not count
3757 * against the number of open files on the system.
3759 * The struct filedesc includes the current working directory,
3760 * and the current root directory, if the process is chroot'ed.
3762 * If the exec was called by a thread using a per thread current
3763 * working directory, we inherit the working directory from the
3764 * thread making the call, rather than from the process.
3766 * In the case of a failure to obtain a reference, for most cases,
3767 * the file entry will be silently droppped. There's an exception
3768 * for the case of a chroot dir, since a failure to to obtain a
3769 * reference there would constitute an "escape" from the chroot
3770 * environment, which must not be allowed. In that case, we will
3771 * deny the execve() operation, rather than allowing the escape.
3774 fdcopy(proc_t p
, vnode_t uth_cdir
)
3776 struct filedesc
*newfdp
, *fdp
= p
->p_fd
;
3778 struct fileproc
*ofp
, *fp
;
3781 MALLOC_ZONE(newfdp
, struct filedesc
*,
3782 sizeof(*newfdp
), M_FILEDESC
, M_WAITOK
);
3789 * the FD_CHROOT flag will be inherited via this copy
3791 (void) memcpy(newfdp
, fdp
, sizeof(*newfdp
));
3794 * If we are running with per-thread current working directories,
3795 * inherit the new current working directory from the current thread
3796 * instead, before we take our references.
3798 if (uth_cdir
!= NULLVP
)
3799 newfdp
->fd_cdir
= uth_cdir
;
3802 * For both fd_cdir and fd_rdir make sure we get
3803 * a valid reference... if we can't, than set
3804 * set the pointer(s) to NULL in the child... this
3805 * will keep us from using a non-referenced vp
3806 * and allows us to do the vnode_rele only on
3807 * a properly referenced vp
3809 if ( (v_dir
= newfdp
->fd_cdir
) ) {
3810 if (vnode_getwithref(v_dir
) == 0) {
3811 if ( (vnode_ref(v_dir
)) )
3812 newfdp
->fd_cdir
= NULL
;
3815 newfdp
->fd_cdir
= NULL
;
3817 if (newfdp
->fd_cdir
== NULL
&& fdp
->fd_cdir
) {
3819 * we couldn't get a new reference on
3820 * the current working directory being
3821 * inherited... we might as well drop
3822 * our reference from the parent also
3823 * since the vnode has gone DEAD making
3824 * it useless... by dropping it we'll
3825 * be that much closer to recyling it
3827 vnode_rele(fdp
->fd_cdir
);
3828 fdp
->fd_cdir
= NULL
;
3831 if ( (v_dir
= newfdp
->fd_rdir
) ) {
3832 if (vnode_getwithref(v_dir
) == 0) {
3833 if ( (vnode_ref(v_dir
)) )
3834 newfdp
->fd_rdir
= NULL
;
3837 newfdp
->fd_rdir
= NULL
;
3840 /* Coming from a chroot environment and unable to get a reference... */
3841 if (newfdp
->fd_rdir
== NULL
&& fdp
->fd_rdir
) {
3843 * We couldn't get a new reference on
3844 * the chroot directory being
3845 * inherited... this is fatal, since
3846 * otherwise it would constitute an
3847 * escape from a chroot environment by
3850 if (newfdp
->fd_cdir
)
3851 vnode_rele(newfdp
->fd_cdir
);
3852 FREE_ZONE(newfdp
, sizeof *newfdp
, M_FILEDESC
);
3855 newfdp
->fd_refcnt
= 1;
3858 * If the number of open files fits in the internal arrays
3859 * of the open file structure, use them, otherwise allocate
3860 * additional memory for the number of descriptors currently
3863 if (newfdp
->fd_lastfile
< NDFILE
)
3867 * Compute the smallest multiple of NDEXTENT needed
3868 * for the file descriptors currently in use,
3869 * allowing the table to shrink.
3871 i
= newfdp
->fd_nfiles
;
3872 while (i
> 2 * NDEXTENT
&& i
> newfdp
->fd_lastfile
* 2)
3877 MALLOC_ZONE(newfdp
->fd_ofiles
, struct fileproc
**,
3878 i
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
3879 if (newfdp
->fd_ofiles
== NULL
) {
3880 if (newfdp
->fd_cdir
)
3881 vnode_rele(newfdp
->fd_cdir
);
3882 if (newfdp
->fd_rdir
)
3883 vnode_rele(newfdp
->fd_rdir
);
3885 FREE_ZONE(newfdp
, sizeof(*newfdp
), M_FILEDESC
);
3888 (void) memset(newfdp
->fd_ofiles
, 0, i
* OFILESIZE
);
3891 newfdp
->fd_ofileflags
= (char *) &newfdp
->fd_ofiles
[i
];
3892 newfdp
->fd_nfiles
= i
;
3894 if (fdp
->fd_nfiles
> 0) {
3895 struct fileproc
**fpp
;
3898 (void) memcpy(newfdp
->fd_ofiles
, fdp
->fd_ofiles
,
3899 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofiles
));
3900 (void) memcpy(newfdp
->fd_ofileflags
, fdp
->fd_ofileflags
,
3901 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofileflags
));
3904 * kq descriptors cannot be copied.
3906 if (newfdp
->fd_knlistsize
!= -1) {
3907 fpp
= &newfdp
->fd_ofiles
[newfdp
->fd_lastfile
];
3908 for (i
= newfdp
->fd_lastfile
; i
>= 0; i
--, fpp
--) {
3909 if (*fpp
!= NULL
&& (*fpp
)->f_type
== DTYPE_KQUEUE
) {
3911 newfdp
->fd_ofileflags
[i
] = 0;
3912 if (i
< newfdp
->fd_freefile
)
3913 newfdp
->fd_freefile
= i
;
3915 if (*fpp
== NULL
&& i
== newfdp
->fd_lastfile
&& i
> 0)
3916 newfdp
->fd_lastfile
--;
3918 newfdp
->fd_knlist
= NULL
;
3919 newfdp
->fd_knlistsize
= -1;
3920 newfdp
->fd_knhash
= NULL
;
3921 newfdp
->fd_knhashmask
= 0;
3923 fpp
= newfdp
->fd_ofiles
;
3924 flags
= newfdp
->fd_ofileflags
;
3926 for (i
= newfdp
->fd_lastfile
+ 1; --i
>= 0; fpp
++, flags
++)
3927 if ((ofp
= *fpp
) != NULL
&& !(*flags
& UF_RESERVED
)) {
3928 MALLOC_ZONE(fp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
3931 * XXX no room to copy, unable to
3932 * XXX safely unwind state at present
3936 bzero(fp
, sizeof(struct fileproc
));
3937 fp
->f_flags
= ofp
->f_flags
;
3938 //fp->f_iocount = ofp->f_iocount;
3940 fp
->f_fglob
= ofp
->f_fglob
;
3945 if (i
< newfdp
->fd_freefile
)
3946 newfdp
->fd_freefile
= i
;
3960 * Description: Release a filedesc (per process open file table) structure;
3961 * this is done on process exit(), or from forkproc_free() if
3962 * the fork fails for some reason subsequent to a successful
3965 * Parameters: p Pointer to process going away
3969 * Locks: This function internally takes and drops proc_fdlock()
3974 struct filedesc
*fdp
;
3975 struct fileproc
*fp
;
3980 /* Certain daemons might not have file descriptors */
3983 if ((fdp
== NULL
) || (--fdp
->fd_refcnt
> 0)) {
3987 if (fdp
->fd_refcnt
== 0xffff)
3988 panic("fdfree: bad fd_refcnt");
3990 /* Last reference: the structure can't change out from under us */
3992 if (fdp
->fd_nfiles
> 0 && fdp
->fd_ofiles
) {
3993 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
3994 if ((fp
= fdp
->fd_ofiles
[i
]) != NULL
) {
3996 if (fdp
->fd_ofileflags
[i
] & UF_RESERVED
)
3997 panic("fdfree: found fp with UF_RESERVED\n");
3999 /* closef drops the iocount ... */
4000 if ((fp
->f_flags
& FP_INCHRREAD
) != 0)
4002 procfdtbl_reservefd(p
, i
);
4004 if (i
< fdp
->fd_knlistsize
)
4005 knote_fdclose(p
, i
);
4006 if (fp
->f_flags
& FP_WAITEVENT
)
4007 (void)waitevent_close(p
, fp
);
4008 (void) closef_locked(fp
, fp
->f_fglob
, p
);
4009 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
4012 FREE_ZONE(fdp
->fd_ofiles
, fdp
->fd_nfiles
* OFILESIZE
, M_OFILETABL
);
4013 fdp
->fd_ofiles
= NULL
;
4020 vnode_rele(fdp
->fd_cdir
);
4022 vnode_rele(fdp
->fd_rdir
);
4024 proc_fdlock_spin(p
);
4029 FREE(fdp
->fd_knlist
, M_KQUEUE
);
4031 FREE(fdp
->fd_knhash
, M_KQUEUE
);
4033 FREE_ZONE(fdp
, sizeof(*fdp
), M_FILEDESC
);
4040 * Description: Called on last open instance for a fileglob for a file being
4043 * Parameters: fp Pointer to fileproc for fd
4044 * fg Pointer to fileglob for fd
4045 * p Pointer to proc structure
4047 * Returns: 0 Success
4048 * <fo_close>:??? Anything returnable by a per-fileops
4051 * Note: fp can only be non-NULL if p is also non-NULL. If p is NULL,
4052 * then fg must eith be locked (FHASLOCK) or must not have a
4053 * type of DTYPE_VNODE.
4055 * On return, the fg is freed.
4057 * This function may block draining output to a character
4058 * device on last close of that device.
4061 closef_finish(struct fileproc
*fp
, struct fileglob
*fg
, proc_t p
, vfs_context_t ctx
)
4066 /* fg_ops completed initialization? */
4068 error
= fo_close(fg
, ctx
);
4072 /* if fp is non-NULL, drain it out */
4073 if (((fp
!= (struct fileproc
*)0) && ((fp
->f_flags
& FP_INCHRREAD
) != 0))) {
4074 proc_fdlock_spin(p
);
4075 if ( ((fp
->f_flags
& FP_INCHRREAD
) != 0) ) {
4076 fileproc_drain(p
, fp
);
4088 * Description: Internal form of closef; called with proc_fdlock held
4090 * Parameters: fp Pointer to fileproc for fd
4091 * fg Pointer to fileglob for fd
4092 * p Pointer to proc structure
4094 * Returns: 0 Success
4095 * closef_finish:??? Anything returnable by a per-fileops
4098 * Note: Decrements reference count on file structure; if this was the
4099 * last reference, then closef_finish() is called
4101 * p and fp are allowed to be NULL when closing a file that was
4102 * being passed in a message (but only if we are called when this
4103 * is NOT the last reference).
4106 closef_locked(struct fileproc
*fp
, struct fileglob
*fg
, proc_t p
)
4110 struct vfs_context context
;
4117 /* Set up context with cred stashed in fg */
4118 if (p
== current_proc())
4119 context
.vc_thread
= current_thread();
4121 context
.vc_thread
= NULL
;
4122 context
.vc_ucred
= fg
->fg_cred
;
4125 * POSIX record locking dictates that any close releases ALL
4126 * locks owned by this process. This is handled by setting
4127 * a flag in the unlock to free ONLY locks obeying POSIX
4128 * semantics, and not to free BSD-style file locks.
4129 * If the descriptor was in a message, POSIX-style locks
4130 * aren't passed with the descriptor.
4132 if (p
&& (p
->p_ladvflag
& P_LADVLOCK
) && fg
->fg_type
== DTYPE_VNODE
) {
4135 lf
.l_whence
= SEEK_SET
;
4138 lf
.l_type
= F_UNLCK
;
4139 vp
= (struct vnode
*)fg
->fg_data
;
4141 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
4142 (void) VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &lf
, F_POSIX
, &context
);
4143 (void)vnode_put(vp
);
4147 lck_mtx_lock_spin(&fg
->fg_lock
);
4150 if (fg
->fg_count
> 0) {
4151 lck_mtx_unlock(&fg
->fg_lock
);
4155 if (fg
->fg_count
!= 0)
4156 panic("fg %p: being freed with bad fg_count (%d)", fg
, fg
->fg_count
);
4159 if (fp
&& (fp
->f_flags
& FP_WRITTEN
))
4160 fg
->fg_flag
|= FWASWRITTEN
;
4162 fg
->fg_lflags
|= FG_TERM
;
4163 lck_mtx_unlock(&fg
->fg_lock
);
4167 error
= closef_finish(fp
, fg
, p
, &context
);
4178 * Description: Drain out pending I/O operations
4180 * Parameters: p Process closing this file
4181 * fp fileproc struct for the open
4182 * instance on the file
4186 * Locks: Assumes the caller holds the proc_fdlock
4188 * Notes: For character devices, this occurs on the last close of the
4189 * device; for all other file descriptos, this occurs on each
4190 * close to prevent fd's from being closed out from under
4191 * operations currently in progress and blocked
4193 * See Also: file_vnode(), file_socket(), file_drop(), and the cautions
4194 * regarding their use and interaction with this function.
4197 fileproc_drain(proc_t p
, struct fileproc
* fp
)
4199 struct vfs_context context
;
4201 context
.vc_thread
= proc_thread(p
); /* XXX */
4202 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
4204 fp
->f_iocount
-- ; /* (the one the close holds) */
4206 while (fp
->f_iocount
) {
4208 lck_mtx_convert_spin(&p
->p_fdmlock
);
4210 if (fp
->f_fglob
->fg_ops
->fo_drain
) {
4211 (*fp
->f_fglob
->fg_ops
->fo_drain
)(fp
, &context
);
4213 if (((fp
->f_flags
& FP_INSELECT
)== FP_INSELECT
)) {
4214 wait_queue_wakeup_all((wait_queue_t
)fp
->f_waddr
, NULL
, THREAD_INTERRUPTED
);
4216 p
->p_fpdrainwait
= 1;
4218 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
, "fpdrain", NULL
);
4227 * Description: Release the fd and free the fileproc associated with the fd
4228 * in the per process open file table of the specified process;
4229 * these values must correspond.
4231 * Parameters: p Process containing fd
4232 * fd fd to be released
4233 * fp fileproc to be freed
4235 * Returns: 0 Success
4237 * Notes: XXX function should be void - no one interprets the returns
4241 fp_free(proc_t p
, int fd
, struct fileproc
* fp
)
4243 proc_fdlock_spin(p
);
4247 fg_free(fp
->f_fglob
);
4248 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
4256 * Description: Apply an advisory lock on a file descriptor.
4258 * Parameters: p Process making request
4259 * uap->fd fd on which the lock is to be
4261 * uap->how (Un)Lock bits, including type
4262 * retval Pointer to the call return area
4264 * Returns: 0 Success
4265 * fp_getfvp:EBADF Bad file descriptor
4266 * fp_getfvp:ENOTSUP fd does not refer to a vnode
4267 * vnode_getwithref:???
4271 * *retval (modified) Size of dtable
4273 * Notes: Just attempt to get a record lock of the requested type on
4274 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
4277 flock(proc_t p
, struct flock_args
*uap
, __unused
int32_t *retval
)
4281 struct fileproc
*fp
;
4284 vfs_context_t ctx
= vfs_context_current();
4287 AUDIT_ARG(fd
, uap
->fd
);
4288 if ( (error
= fp_getfvp(p
, fd
, &fp
, &vp
)) ) {
4291 if ( (error
= vnode_getwithref(vp
)) ) {
4294 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
4296 lf
.l_whence
= SEEK_SET
;
4299 if (how
& LOCK_UN
) {
4300 lf
.l_type
= F_UNLCK
;
4301 fp
->f_flag
&= ~FHASLOCK
;
4302 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_UNLCK
, &lf
, F_FLOCK
, ctx
);
4306 lf
.l_type
= F_WRLCK
;
4307 else if (how
& LOCK_SH
)
4308 lf
.l_type
= F_RDLCK
;
4314 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
, F_SETLK
, &lf
);
4318 fp
->f_flag
|= FHASLOCK
;
4319 if (how
& LOCK_NB
) {
4320 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, F_FLOCK
, ctx
);
4323 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, F_FLOCK
|F_WAIT
, ctx
);
4325 (void)vnode_put(vp
);
4327 fp_drop(p
, fd
, fp
, 0);
4336 * Description: Obtain a Mach send right for a given file descriptor.
4338 * Parameters: p Process calling fileport
4339 * uap->fd The fd to reference
4340 * uap->portnamep User address at which to place port name.
4342 * Returns: 0 Success.
4343 * EBADF Bad file descriptor.
4344 * EINVAL File descriptor had type that cannot be sent, misc. other errors.
4345 * EFAULT Address at which to store port name is not valid.
4346 * EAGAIN Resource shortage.
4349 * On success, name of send right is stored at user-specified address.
4352 fileport_makeport(proc_t p
, struct fileport_makeport_args
*uap
,
4353 __unused
int *retval
)
4357 user_addr_t user_portaddr
= uap
->portnamep
;
4358 struct fileproc
*fp
= FILEPROC_NULL
;
4359 struct fileglob
*fg
= NULL
;
4360 ipc_port_t fileport
;
4361 mach_port_name_t name
= MACH_PORT_NULL
;
4363 err
= fp_lookup(p
, fd
, &fp
, 0);
4368 if (!filetype_issendable(fp
->f_type
)) {
4373 /* Dropped when port is deallocated */
4377 /* Allocate and initialize a port */
4378 fileport
= fileport_alloc(fg
);
4379 if (fileport
== IPC_PORT_NULL
) {
4385 /* Add an entry. Deallocates port on failure. */
4386 name
= ipc_port_copyout_send(fileport
, get_task_ipcspace(p
->task
));
4387 if (!MACH_PORT_VALID(name
)) {
4392 err
= copyout(&name
, user_portaddr
, sizeof(mach_port_name_t
));
4397 /* Tag the fileglob for debugging purposes */
4398 lck_mtx_lock_spin(&fg
->fg_lock
);
4399 fg
->fg_lflags
|= FG_PORTMADE
;
4400 lck_mtx_unlock(&fg
->fg_lock
);
4402 fp_drop(p
, fd
, fp
, 0);
4407 if (MACH_PORT_VALID(name
)) {
4408 /* Don't care if another thread races us to deallocate the entry */
4409 (void) mach_port_deallocate(get_task_ipcspace(p
->task
), name
);
4412 if (fp
!= FILEPROC_NULL
) {
4413 fp_drop(p
, fd
, fp
, 0);
4420 fileport_releasefg(struct fileglob
*fg
)
4422 (void)closef_locked(NULL
, fg
, PROC_NULL
);
4431 * Description: Obtain the file descriptor for a given Mach send right.
4433 * Parameters: p Process calling fileport
4434 * uap->port Name of send right to file port.
4436 * Returns: 0 Success
4437 * EINVAL Invalid Mach port name, or port is not for a file.
4439 * fdalloc:ENOMEM Unable to allocate fileproc or extend file table.
4442 * *retval (modified) The new descriptor
4445 fileport_makefd(proc_t p
, struct fileport_makefd_args
*uap
, int32_t *retval
)
4447 struct fileglob
*fg
;
4448 struct fileproc
*fp
= FILEPROC_NULL
;
4449 ipc_port_t port
= IPC_PORT_NULL
;
4450 mach_port_name_t send
= uap
->port
;
4455 res
= ipc_object_copyin(get_task_ipcspace(p
->task
),
4456 send
, MACH_MSG_TYPE_COPY_SEND
, &port
);
4458 if (res
!= KERN_SUCCESS
) {
4463 fg
= fileport_port_to_fileglob(port
);
4469 MALLOC_ZONE(fp
, struct fileproc
*, sizeof(*fp
), M_FILEPROC
, M_WAITOK
);
4470 if (fp
== FILEPROC_NULL
) {
4475 bzero(fp
, sizeof(*fp
));
4481 err
= fdalloc(p
, 0, &fd
);
4487 procfdtbl_releasefd(p
, fd
, fp
);
4493 if ((fp
!= NULL
) && (0 != err
)) {
4494 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
4497 if (IPC_PORT_NULL
!= port
) {
4498 ipc_port_release_send(port
);
4503 #endif /* CONFIG_EMBEDDED */
4509 * Description: Duplicate the specified descriptor to a free descriptor;
4510 * this is the second half of fdopen(), above.
4512 * Parameters: fdp filedesc pointer to fill in
4514 * dfd fd to dup from
4515 * mode mode to set on new fd
4516 * error command code
4518 * Returns: 0 Success
4519 * EBADF Source fd is bad
4520 * EACCES Requested mode not allowed
4521 * !0 'error', if not ENODEV or
4524 * Notes: XXX This is not thread safe; see fdopen() above
4527 dupfdopen(struct filedesc
*fdp
, int indx
, int dfd
, int mode
, int error
)
4529 struct fileproc
*wfp
;
4530 struct fileproc
*fp
;
4534 proc_t p
= current_proc();
4537 * If the to-be-dup'd fd number is greater than the allowed number
4538 * of file descriptors, or the fd to be dup'd has already been
4539 * closed, reject. Note, check for new == old is necessary as
4540 * falloc could allocate an already closed to-be-dup'd descriptor
4541 * as the new descriptor.
4545 fp
= fdp
->fd_ofiles
[indx
];
4546 if (dfd
< 0 || dfd
>= fdp
->fd_nfiles
||
4547 (wfp
= fdp
->fd_ofiles
[dfd
]) == NULL
|| wfp
== fp
||
4548 (fdp
->fd_ofileflags
[dfd
] & UF_RESERVED
)) {
4554 myerror
= mac_file_check_dup(proc_ucred(p
), wfp
->f_fglob
, dfd
);
4561 * There are two cases of interest here.
4563 * For ENODEV simply dup (dfd) to file descriptor
4564 * (indx) and return.
4566 * For ENXIO steal away the file structure from (dfd) and
4567 * store it in (indx). (dfd) is effectively closed by
4570 * Any other error code is just returned.
4575 * Check that the mode the file is being opened for is a
4576 * subset of the mode of the existing descriptor.
4578 if (((mode
& (FREAD
|FWRITE
)) | wfp
->f_flag
) != wfp
->f_flag
) {
4582 if (indx
> fdp
->fd_lastfile
)
4583 fdp
->fd_lastfile
= indx
;
4587 fg_free(fp
->f_fglob
);
4588 fp
->f_fglob
= wfp
->f_fglob
;
4590 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
];
4606 * Description: Add a reference to a fileglob by fileproc
4608 * Parameters: fp fileproc containing fileglob
4613 * Notes: XXX Should use OSAddAtomic?
4616 fg_ref(struct fileproc
* fp
)
4618 struct fileglob
*fg
;
4622 lck_mtx_lock_spin(&fg
->fg_lock
);
4625 if ((fp
->f_flags
& ~((unsigned int)FP_VALID_FLAGS
)) != 0)
4626 panic("fg_ref: invalid bits on fp%x\n", (unsigned int)fp
);
4628 if (fg
->fg_count
== 0)
4629 panic("fg_ref: adding fgcount to zeroed fg :fp %x, fg%x\n ", (unsigned int)fp
, (unsigned int)fg
);
4632 lck_mtx_unlock(&fg
->fg_lock
);
4639 * Description: Remove a reference to a fileglob by fileproc
4641 * Parameters: fp fileproc containing fileglob
4646 * Notes: XXX Should use OSAddAtomic?
4649 fg_drop(struct fileproc
* fp
)
4651 struct fileglob
*fg
;
4654 lck_mtx_lock_spin(&fg
->fg_lock
);
4656 lck_mtx_unlock(&fg
->fg_lock
);
4663 * Description: Insert fileglob onto message queue
4665 * Parameters: fg Fileglob pointer to insert
4669 * Locks: Takes and drops fg_lock, potentially many times
4672 fg_insertuipc(struct fileglob
* fg
)
4676 lck_mtx_lock_spin(&fg
->fg_lock
);
4678 while (fg
->fg_lflags
& FG_RMMSGQ
) {
4679 lck_mtx_convert_spin(&fg
->fg_lock
);
4681 fg
->fg_lflags
|= FG_WRMMSGQ
;
4682 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_insertuipc", NULL
);
4687 if (fg
->fg_msgcount
== 1) {
4688 fg
->fg_lflags
|= FG_INSMSGQ
;
4691 lck_mtx_unlock(&fg
->fg_lock
);
4694 lck_mtx_lock_spin(uipc_lock
);
4696 LIST_INSERT_HEAD(&fmsghead
, fg
, f_msglist
);
4697 lck_mtx_unlock(uipc_lock
);
4698 lck_mtx_lock(&fg
->fg_lock
);
4699 fg
->fg_lflags
&= ~FG_INSMSGQ
;
4700 if (fg
->fg_lflags
& FG_WINSMSGQ
) {
4701 fg
->fg_lflags
&= ~FG_WINSMSGQ
;
4702 wakeup(&fg
->fg_lflags
);
4704 lck_mtx_unlock(&fg
->fg_lock
);
4713 * Description: Remove fileglob from message queue
4715 * Parameters: fg Fileglob pointer to remove
4719 * Locks: Takes and drops fg_lock, potentially many times
4722 fg_removeuipc(struct fileglob
* fg
)
4726 lck_mtx_lock_spin(&fg
->fg_lock
);
4727 while (fg
->fg_lflags
& FG_INSMSGQ
) {
4728 lck_mtx_convert_spin(&fg
->fg_lock
);
4730 fg
->fg_lflags
|= FG_WINSMSGQ
;
4731 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_removeuipc", NULL
);
4734 if (fg
->fg_msgcount
== 0) {
4735 fg
->fg_lflags
|= FG_RMMSGQ
;
4738 lck_mtx_unlock(&fg
->fg_lock
);
4741 lck_mtx_lock_spin(uipc_lock
);
4743 LIST_REMOVE(fg
, f_msglist
);
4744 lck_mtx_unlock(uipc_lock
);
4745 lck_mtx_lock(&fg
->fg_lock
);
4746 fg
->fg_lflags
&= ~FG_RMMSGQ
;
4747 if (fg
->fg_lflags
& FG_WRMMSGQ
) {
4748 fg
->fg_lflags
&= ~FG_WRMMSGQ
;
4749 wakeup(&fg
->fg_lflags
);
4751 lck_mtx_unlock(&fg
->fg_lock
);
4759 * Description: Generic fileops read indirected through the fileops pointer
4760 * in the fileproc structure
4762 * Parameters: fp fileproc structure pointer
4763 * uio user I/O structure pointer
4765 * ctx VFS context for operation
4767 * Returns: 0 Success
4768 * !0 Errno from read
4771 fo_read(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
4773 return ((*fp
->f_ops
->fo_read
)(fp
, uio
, flags
, ctx
));
4780 * Description: Generic fileops write indirected through the fileops pointer
4781 * in the fileproc structure
4783 * Parameters: fp fileproc structure pointer
4784 * uio user I/O structure pointer
4786 * ctx VFS context for operation
4788 * Returns: 0 Success
4789 * !0 Errno from write
4792 fo_write(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
4794 return((*fp
->f_ops
->fo_write
)(fp
, uio
, flags
, ctx
));
4801 * Description: Generic fileops ioctl indirected through the fileops pointer
4802 * in the fileproc structure
4804 * Parameters: fp fileproc structure pointer
4806 * data pointer to internalized copy
4807 * of user space ioctl command
4808 * parameter data in kernel space
4809 * ctx VFS context for operation
4811 * Returns: 0 Success
4812 * !0 Errno from ioctl
4814 * Locks: The caller is assumed to have held the proc_fdlock; this
4815 * function releases and reacquires this lock. If the caller
4816 * accesses data protected by this lock prior to calling this
4817 * function, it will need to revalidate/reacquire any cached
4818 * protected data obtained prior to the call.
4821 fo_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
, vfs_context_t ctx
)
4825 proc_fdunlock(vfs_context_proc(ctx
));
4826 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, com
, data
, ctx
);
4827 proc_fdlock(vfs_context_proc(ctx
));
4835 * Description: Generic fileops select indirected through the fileops pointer
4836 * in the fileproc structure
4838 * Parameters: fp fileproc structure pointer
4839 * which select which
4840 * wql pointer to wait queue list
4841 * ctx VFS context for operation
4843 * Returns: 0 Success
4844 * !0 Errno from select
4847 fo_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
4849 return((*fp
->f_ops
->fo_select
)(fp
, which
, wql
, ctx
));
4856 * Description: Generic fileops close indirected through the fileops pointer
4857 * in the fileproc structure
4859 * Parameters: fp fileproc structure pointer for
4861 * ctx VFS context for operation
4863 * Returns: 0 Success
4864 * !0 Errno from close
4867 fo_close(struct fileglob
*fg
, vfs_context_t ctx
)
4869 return((*fg
->fg_ops
->fo_close
)(fg
, ctx
));
4876 * Description: Generic fileops kqueue filter indirected through the fileops
4877 * pointer in the fileproc structure
4879 * Parameters: fp fileproc structure pointer
4880 * kn pointer to knote to filter on
4881 * ctx VFS context for operation
4883 * Returns: 0 Success
4884 * !0 Errno from kqueue filter
4887 fo_kqfilter(struct fileproc
*fp
, struct knote
*kn
, vfs_context_t ctx
)
4889 return ((*fp
->f_ops
->fo_kqfilter
)(fp
, kn
, ctx
));
4893 * The ability to send a file descriptor to another
4894 * process is opt-in by file type.
4897 filetype_issendable(file_type_t fdtype
)
4906 /* DTYPE_KQUEUE, DTYPE_FSEVENTS, DTYPE_PSXSEM */