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>
113 int fdopen(dev_t dev
, int mode
, int type
, proc_t p
);
114 int finishdup(proc_t p
, struct filedesc
*fdp
, int old
, int new, int32_t *retval
);
116 int falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
, int locked
);
117 void fg_drop(struct fileproc
* fp
);
118 void fg_free(struct fileglob
*fg
);
119 void fg_ref(struct fileproc
* fp
);
121 /* flags for close_internal_locked */
122 #define FD_DUP2RESV 1
123 static int close_internal_locked(struct proc
*p
, int fd
, struct fileproc
*fp
, int flags
);
125 static int closef_finish(struct fileproc
*fp
, struct fileglob
*fg
, proc_t p
, vfs_context_t ctx
);
127 /* We don't want these exported */
129 int open1(vfs_context_t
, struct nameidata
*, int, struct vnode_attr
*, int32_t *);
132 int unlink1(vfs_context_t
, struct nameidata
*, int);
134 static void _fdrelse(struct proc
* p
, int fd
);
137 extern void file_lock_init(void) __attribute__((section("__TEXT, initcode")));
138 extern int kqueue_stat(struct fileproc
*fp
, void *ub
, int isstat4
, proc_t p
);
140 extern int soo_stat(struct socket
*so
, void *ub
, int isstat64
);
143 extern kauth_scope_t kauth_scope_fileop
;
147 #define f_flag f_fglob->fg_flag
148 #define f_type f_fglob->fg_type
149 #define f_msgcount f_fglob->fg_msgcount
150 #define f_cred f_fglob->fg_cred
151 #define f_ops f_fglob->fg_ops
152 #define f_offset f_fglob->fg_offset
153 #define f_data f_fglob->fg_data
155 * Descriptor management.
157 struct filelist filehead
; /* head of list of open files */
158 struct fmsglist fmsghead
; /* head of list of open files */
159 struct fmsglist fmsg_ithead
; /* head of list of open files */
160 int nfiles
; /* actual number of open files */
163 lck_grp_attr_t
* file_lck_grp_attr
;
164 lck_grp_t
* file_lck_grp
;
165 lck_attr_t
* file_lck_attr
;
167 lck_mtx_t
* uipc_lock
;
168 lck_mtx_t
* file_flist_lock
;
174 * Description: Initialize the file lock group and the uipc and flist locks
180 * Notes: Called at system startup from bsd_init().
185 /* allocate file lock group attribute and group */
186 file_lck_grp_attr
= lck_grp_attr_alloc_init();
188 file_lck_grp
= lck_grp_alloc_init("file", file_lck_grp_attr
);
190 /* Allocate file lock attribute */
191 file_lck_attr
= lck_attr_alloc_init();
193 uipc_lock
= lck_mtx_alloc_init(file_lck_grp
, file_lck_attr
);
194 file_flist_lock
= lck_mtx_alloc_init(file_lck_grp
, file_lck_attr
);
199 * proc_fdlock, proc_fdlock_spin
201 * Description: Lock to control access to the per process struct fileproc
202 * and struct filedesc
204 * Parameters: p Process to take the lock on
208 * Notes: The lock is initialized in forkproc() and destroyed in
209 * reap_child_process().
212 proc_fdlock(proc_t p
)
214 lck_mtx_lock(&p
->p_fdmlock
);
218 proc_fdlock_spin(proc_t p
)
220 lck_mtx_lock_spin(&p
->p_fdmlock
);
224 proc_fdlock_assert(proc_t p
, int assertflags
)
226 lck_mtx_assert(&p
->p_fdmlock
, assertflags
);
233 * Description: Unlock the lock previously locked by a call to proc_fdlock()
235 * Parameters: p Process to drop the lock on
240 proc_fdunlock(proc_t p
)
242 lck_mtx_unlock(&p
->p_fdmlock
);
247 * System calls on descriptors.
254 * Description: Returns the per process maximum size of the descriptor table
256 * Parameters: p Process being queried
257 * retval Pointer to the call return area
262 * *retval (modified) Size of dtable
265 getdtablesize(proc_t p
, __unused
struct getdtablesize_args
*uap
, int32_t *retval
)
268 *retval
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
276 procfdtbl_reservefd(struct proc
* p
, int fd
)
278 p
->p_fd
->fd_ofiles
[fd
] = NULL
;
279 p
->p_fd
->fd_ofileflags
[fd
] |= UF_RESERVED
;
283 procfdtbl_markclosefd(struct proc
* p
, int fd
)
285 p
->p_fd
->fd_ofileflags
[fd
] |= (UF_RESERVED
| UF_CLOSING
);
289 procfdtbl_releasefd(struct proc
* p
, int fd
, struct fileproc
* fp
)
292 p
->p_fd
->fd_ofiles
[fd
] = fp
;
293 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESERVED
;
294 if ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
) == UF_RESVWAIT
) {
295 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESVWAIT
;
301 procfdtbl_waitfd(struct proc
* p
, int fd
)
303 p
->p_fd
->fd_ofileflags
[fd
] |= UF_RESVWAIT
;
304 msleep(&p
->p_fd
, &p
->p_fdmlock
, PRIBIO
, "ftbl_waitfd", NULL
);
309 procfdtbl_clearfd(struct proc
* p
, int fd
)
313 waiting
= (p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
);
314 p
->p_fd
->fd_ofiles
[fd
] = NULL
;
315 p
->p_fd
->fd_ofileflags
[fd
] = 0;
316 if ( waiting
== UF_RESVWAIT
) {
324 * Description: Inline utility function to free an fd in a filedesc
326 * Parameters: fdp Pointer to filedesc fd lies in
328 * reserv fd should be reserved
332 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
336 _fdrelse(struct proc
* p
, int fd
)
338 struct filedesc
*fdp
= p
->p_fd
;
341 if (fd
< fdp
->fd_freefile
)
342 fdp
->fd_freefile
= fd
;
344 if (fd
> fdp
->fd_lastfile
)
345 panic("fdrelse: fd_lastfile inconsistent");
347 procfdtbl_clearfd(p
, fd
);
349 while ((nfd
= fdp
->fd_lastfile
) > 0 &&
350 fdp
->fd_ofiles
[nfd
] == NULL
&&
351 !(fdp
->fd_ofileflags
[nfd
] & UF_RESERVED
))
373 char uio_buf
[ UIO_SIZEOF(1) ];
374 struct vfs_context context
= *(vfs_context_current());
378 error
= fp_lookup(p
, fd
, &fp
, 0);
382 if (fp
->f_type
!= DTYPE_VNODE
&& fp
->f_type
!= DTYPE_PIPE
&& fp
->f_type
!= DTYPE_SOCKET
) {
386 if (rw
== UIO_WRITE
&& !(fp
->f_flag
& FWRITE
)) {
391 if (rw
== UIO_READ
&& !(fp
->f_flag
& FREAD
)) {
396 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
398 if (UIO_SEG_IS_USER_SPACE(segflg
))
399 spacetype
= proc_is64bit(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
401 spacetype
= UIO_SYSSPACE
;
403 auio
= uio_createwithbuffer(1, offset
, spacetype
, rw
, &uio_buf
[0], sizeof(uio_buf
));
405 uio_addiov(auio
, base
, len
);
407 if ( !(io_flg
& IO_APPEND
))
411 error
= fo_write(fp
, auio
, flags
, &context
);
413 error
= fo_read(fp
, auio
, flags
, &context
);
416 *aresid
= uio_resid(auio
);
418 if (uio_resid(auio
) && error
== 0)
422 if (rw
== UIO_WRITE
&& error
== 0)
423 fp_drop_written(p
, fd
, fp
);
425 fp_drop(p
, fd
, fp
, 0);
435 * Description: Duplicate a file descriptor.
437 * Parameters: p Process performing the dup
438 * uap->fd The fd to dup
439 * retval Pointer to the call return area
445 * *retval (modified) The new descriptor
448 dup(proc_t p
, struct dup_args
*uap
, int32_t *retval
)
450 struct filedesc
*fdp
= p
->p_fd
;
456 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
460 if ( (error
= fdalloc(p
, 0, &new)) ) {
461 fp_drop(p
, old
, fp
, 1);
465 error
= finishdup(p
, fdp
, old
, new, retval
);
466 fp_drop(p
, old
, fp
, 1);
476 * Description: Duplicate a file descriptor to a particular value.
478 * Parameters: p Process performing the dup
479 * uap->fd The fd to dup
480 * uap->to The fd to dup it to
481 * retval Pointer to the call return area
487 * *retval (modified) The new descriptor
490 dup2(proc_t p
, struct dup2_args
*uap
, int32_t *retval
)
492 struct filedesc
*fdp
= p
->p_fd
;
493 int old
= uap
->from
, new = uap
->to
;
495 struct fileproc
*fp
, *nfp
;
500 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
505 (rlim_t
)new >= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
507 fp_drop(p
, old
, fp
, 1);
512 fp_drop(p
, old
, fp
, 1);
517 if (new < 0 || new >= fdp
->fd_nfiles
) {
518 if ( (error
= fdalloc(p
, new, &i
)) ) {
519 fp_drop(p
, old
, fp
, 1);
529 while ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == UF_RESERVED
) {
530 fp_drop(p
, old
, fp
, 1);
531 procfdtbl_waitfd(p
, new);
533 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
538 if ((fdp
->fd_ofiles
[new] != NULL
) && ((error
= fp_lookup(p
, new, &nfp
, 1)) == 0)) {
539 fp_drop(p
, old
, fp
, 1);
540 (void)close_internal_locked(p
, new, nfp
, FD_DUP2RESV
);
542 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
544 procfdtbl_clearfd(p
, new);
548 if (fdp
->fd_ofiles
[new] != NULL
)
549 panic("dup2: unable to get ref on a fileproc %d\n", new);
551 procfdtbl_reservefd(p
, new);
555 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
560 if (fdp
->fd_ofiles
[new] != 0)
561 panic("dup2-1: overwriting fd_ofiles with new %d\n", new);
562 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0)
563 panic("dup2-1: unreserved fileflags with new %d\n", new);
565 error
= finishdup(p
, fdp
, old
, new, retval
);
566 fp_drop(p
, old
, fp
, 1);
576 * Description: The file control system call.
578 * Parameters: p Process performing the fcntl
579 * uap->fd The fd to operate against
580 * uap->cmd The command to perform
581 * uap->arg Pointer to the command argument
582 * retval Pointer to the call return area
585 * !0 Errno (see fcntl_nocancel)
588 * *retval (modified) fcntl return value (if any)
590 * Notes: This system call differs from fcntl_nocancel() in that it
591 * tests for cancellation prior to performing a potentially
592 * blocking operation.
595 fcntl(proc_t p
, struct fcntl_args
*uap
, int32_t *retval
)
597 __pthread_testcancel(1);
598 return(fcntl_nocancel(p
, (struct fcntl_nocancel_args
*)uap
, retval
));
605 * Description: A non-cancel-testing file control system call.
607 * Parameters: p Process performing the fcntl
608 * uap->fd The fd to operate against
609 * uap->cmd The command to perform
610 * uap->arg Pointer to the command argument
611 * retval Pointer to the call return area
615 * fp_lookup:EBADF Bad file descriptor
627 * vnode_getwithref:???
634 * vnode_getwithref:???
641 * vnode_getwithref:???
643 * [F_SETSIZE,F_RDADVISE]
646 * vnode_getwithref:???
647 * [F_RDAHEAD,F_NOCACHE]
649 * vnode_getwithref:???
653 * *retval (modified) fcntl return value (if any)
656 fcntl_nocancel(proc_t p
, struct fcntl_nocancel_args
*uap
, int32_t *retval
)
659 struct filedesc
*fdp
= p
->p_fd
;
662 struct vnode
*vp
= NULLVP
; /* for AUDIT_ARG() at end */
663 int i
, tmp
, error
, error2
, flg
= F_POSIX
;
665 struct vfs_context context
;
669 int devBlockSize
= 0;
674 AUDIT_ARG(fd
, uap
->fd
);
675 AUDIT_ARG(cmd
, uap
->cmd
);
678 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
682 context
.vc_thread
= current_thread();
683 context
.vc_ucred
= fp
->f_cred
;
685 is64bit
= proc_is64bit(p
);
691 * Since the arg parameter is defined as a long but may be
692 * either a long or a pointer we must take care to handle
693 * sign extension issues. Our sys call munger will sign
694 * extend a long when we are called from a 32-bit process.
695 * Since we can never have an address greater than 32-bits
696 * from a 32-bit process we lop off the top 32-bits to avoid
697 * getting the wrong address
699 argp
= CAST_USER_ADDR_T((uint32_t)uap
->arg
);
702 pop
= &fdp
->fd_ofileflags
[fd
];
705 error
= mac_file_check_fcntl(proc_ucred(p
), fp
->f_fglob
, uap
->cmd
,
714 newmin
= CAST_DOWN_EXPLICIT(int, uap
->arg
); /* arg is an int, so we won't lose bits */
715 AUDIT_ARG(value32
, newmin
);
716 if ((u_int
)newmin
>= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
717 newmin
>= maxfiles
) {
721 if ( (error
= fdalloc(p
, newmin
, &i
)) )
723 error
= finishdup(p
, fdp
, fd
, i
, retval
);
727 *retval
= (*pop
& UF_EXCLOSE
)? 1 : 0;
732 AUDIT_ARG(value32
, uap
->arg
);
733 *pop
= (*pop
&~ UF_EXCLOSE
) |
734 (uap
->arg
& 1)? UF_EXCLOSE
: 0;
739 *retval
= OFLAGS(fp
->f_flag
);
744 fp
->f_flag
&= ~FCNTLFLAGS
;
745 tmp
= CAST_DOWN_EXPLICIT(int, uap
->arg
); /* arg is an int, so we won't lose bits */
746 AUDIT_ARG(value32
, tmp
);
747 fp
->f_flag
|= FFLAGS(tmp
) & FCNTLFLAGS
;
748 tmp
= fp
->f_flag
& FNONBLOCK
;
749 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
752 tmp
= fp
->f_flag
& FASYNC
;
753 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, &context
);
756 fp
->f_flag
&= ~FNONBLOCK
;
758 (void)fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
762 if (fp
->f_type
== DTYPE_SOCKET
) {
763 *retval
= ((struct socket
*)fp
->f_data
)->so_pgid
;
767 error
= fo_ioctl(fp
, (int)TIOCGPGRP
, (caddr_t
)retval
, &context
);
772 tmp
= CAST_DOWN_EXPLICIT(pid_t
, uap
->arg
); /* arg is an int, so we won't lose bits */
773 AUDIT_ARG(value32
, tmp
);
774 if (fp
->f_type
== DTYPE_SOCKET
) {
775 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
779 if (fp
->f_type
== DTYPE_PIPE
) {
780 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
787 proc_t p1
= proc_find(tmp
);
792 tmp
= (int)p1
->p_pgrpid
;
795 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
800 /* Fall into F_SETLK */
803 if (fp
->f_type
!= DTYPE_VNODE
) {
807 vp
= (struct vnode
*)fp
->f_data
;
810 offset
= fp
->f_offset
;
813 /* Copy in the lock structure */
814 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
819 if ((fl
.l_whence
== SEEK_CUR
) && (fl
.l_start
+ offset
< fl
.l_start
)) {
824 if ( (error
= vnode_getwithref(vp
)) ) {
827 if (fl
.l_whence
== SEEK_CUR
)
828 fl
.l_start
+= offset
;
831 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
841 if ((fflag
& FREAD
) == 0) {
846 // XXX UInt32 unsafe for LP64 kernel
847 OSBitOrAtomic(P_LADVLOCK
, &p
->p_ladvflag
);
848 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_SETLK
, &fl
, flg
, &context
);
853 if ((fflag
& FWRITE
) == 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 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &fl
,
877 if (fp
->f_type
!= DTYPE_VNODE
) {
881 vp
= (struct vnode
*)fp
->f_data
;
883 offset
= fp
->f_offset
;
886 /* Copy in the lock structure */
887 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
891 /* Check starting byte and ending byte for EOVERFLOW in SEEK_CUR */
892 /* and ending byte for EOVERFLOW in SEEK_SET */
893 if (((fl
.l_whence
== SEEK_CUR
) &&
894 ((fl
.l_start
+ offset
< fl
.l_start
) ||
895 ((fl
.l_len
> 0) && (fl
.l_start
+offset
+ fl
.l_len
- 1 < fl
.l_start
+offset
)))) ||
896 ((fl
.l_whence
== SEEK_SET
) && (fl
.l_len
> 0) && (fl
.l_start
+ fl
.l_len
- 1 < fl
.l_start
)))
898 /* lf_advlock doesn't check start/end for F_GETLK if file has no locks */
903 if ((fl
.l_whence
== SEEK_SET
) && (fl
.l_start
< 0)) {
918 switch (fl
.l_whence
) {
928 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
929 if (fl
.l_whence
== SEEK_CUR
)
930 fl
.l_start
+= offset
;
933 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
937 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_GETLK
, &fl
, F_POSIX
, &context
);
942 error
= copyout((caddr_t
)&fl
, argp
, sizeof(fl
));
946 case F_PREALLOCATE
: {
947 fstore_t alloc_struct
; /* structure for allocate command */
948 u_int32_t alloc_flags
= 0;
950 if (fp
->f_type
!= DTYPE_VNODE
) {
955 vp
= (struct vnode
*)fp
->f_data
;
958 /* make sure that we have write permission */
959 if ((fp
->f_flag
& FWRITE
) == 0) {
964 error
= copyin(argp
, (caddr_t
)&alloc_struct
, sizeof(alloc_struct
));
968 /* now set the space allocated to 0 */
969 alloc_struct
.fst_bytesalloc
= 0;
972 * Do some simple parameter checking
975 /* set up the flags */
977 alloc_flags
|= PREALLOCATE
;
979 if (alloc_struct
.fst_flags
& F_ALLOCATECONTIG
)
980 alloc_flags
|= ALLOCATECONTIG
;
982 if (alloc_struct
.fst_flags
& F_ALLOCATEALL
)
983 alloc_flags
|= ALLOCATEALL
;
986 * Do any position mode specific stuff. The only
987 * position mode supported now is PEOFPOSMODE
990 switch (alloc_struct
.fst_posmode
) {
993 if (alloc_struct
.fst_offset
!= 0) {
998 alloc_flags
|= ALLOCATEFROMPEOF
;
1002 if (alloc_struct
.fst_offset
<= 0) {
1007 alloc_flags
|= ALLOCATEFROMVOL
;
1015 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1017 * call allocate to get the space
1019 error
= VNOP_ALLOCATE(vp
,alloc_struct
.fst_length
,alloc_flags
,
1020 &alloc_struct
.fst_bytesalloc
, alloc_struct
.fst_offset
,
1022 (void)vnode_put(vp
);
1024 error2
= copyout((caddr_t
)&alloc_struct
, argp
, sizeof(alloc_struct
));
1033 if (fp
->f_type
!= DTYPE_VNODE
) {
1037 vp
= (struct vnode
*)fp
->f_data
;
1040 error
= copyin(argp
, (caddr_t
)&offset
, sizeof (off_t
));
1043 AUDIT_ARG(value64
, offset
);
1045 error
= vnode_getwithref(vp
);
1050 error
= mac_vnode_check_truncate(&context
,
1051 fp
->f_fglob
->fg_cred
, vp
);
1053 (void)vnode_put(vp
);
1058 * Make sure that we are root. Growing a file
1059 * without zero filling the data is a security hole
1060 * root would have access anyway so we'll allow it
1068 error
= vnode_setsize(vp
, offset
, IO_NOZEROFILL
,
1072 (void)vnode_put(vp
);
1076 if (fp
->f_type
!= DTYPE_VNODE
) {
1081 fp
->f_fglob
->fg_flag
&= ~FNORDAHEAD
;
1083 fp
->f_fglob
->fg_flag
|= FNORDAHEAD
;
1088 if (fp
->f_type
!= DTYPE_VNODE
) {
1093 fp
->f_fglob
->fg_flag
|= FNOCACHE
;
1095 fp
->f_fglob
->fg_flag
&= ~FNOCACHE
;
1099 case F_GLOBAL_NOCACHE
:
1100 if (fp
->f_type
!= DTYPE_VNODE
) {
1104 vp
= (struct vnode
*)fp
->f_data
;
1107 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1109 *retval
= vnode_isnocache(vp
);
1112 vnode_setnocache(vp
);
1114 vnode_clearnocache(vp
);
1116 (void)vnode_put(vp
);
1120 case F_CHECK_OPENEVT
:
1121 if (fp
->f_type
!= DTYPE_VNODE
) {
1125 vp
= (struct vnode
*)fp
->f_data
;
1128 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1130 *retval
= vnode_is_openevt(vp
);
1133 vnode_set_openevt(vp
);
1135 vnode_clear_openevt(vp
);
1137 (void)vnode_put(vp
);
1142 struct radvisory ra_struct
;
1144 if (fp
->f_type
!= DTYPE_VNODE
) {
1148 vp
= (struct vnode
*)fp
->f_data
;
1151 if ( (error
= copyin(argp
, (caddr_t
)&ra_struct
, sizeof(ra_struct
))) )
1153 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1154 error
= VNOP_IOCTL(vp
, F_RDADVISE
, (caddr_t
)&ra_struct
, 0, &context
);
1156 (void)vnode_put(vp
);
1161 case F_READBOOTSTRAP
:
1162 case F_WRITEBOOTSTRAP
: {
1163 user32_fbootstraptransfer_t user32_fbt_struct
;
1164 user_fbootstraptransfer_t user_fbt_struct
;
1166 caddr_t boot_structp
;
1168 if (fp
->f_type
!= DTYPE_VNODE
) {
1172 vp
= (struct vnode
*)fp
->f_data
;
1175 if (IS_64BIT_PROCESS(p
)) {
1176 sizeof_struct
= sizeof(user_fbt_struct
);
1177 boot_structp
= (caddr_t
) &user_fbt_struct
;
1180 sizeof_struct
= sizeof(user32_fbt_struct
);
1181 boot_structp
= (caddr_t
) &user32_fbt_struct
;
1183 error
= copyin(argp
, boot_structp
, sizeof_struct
);
1186 if ( (error
= vnode_getwithref(vp
)) ) {
1189 if (uap
->cmd
== F_WRITEBOOTSTRAP
) {
1191 * Make sure that we are root. Updating the
1192 * bootstrap on a disk could be a security hole
1195 (void)vnode_put(vp
);
1200 if (strncmp(vnode_mount(vp
)->mnt_vfsstat
.f_fstypename
, "hfs",
1201 sizeof(vnode_mount(vp
)->mnt_vfsstat
.f_fstypename
)) != 0) {
1205 * call vnop_ioctl to handle the I/O
1207 error
= VNOP_IOCTL(vp
, uap
->cmd
, boot_structp
, 0, &context
);
1209 (void)vnode_put(vp
);
1213 struct log2phys l2p_struct
; /* structure for allocate command */
1215 if (fp
->f_type
!= DTYPE_VNODE
) {
1219 vp
= (struct vnode
*)fp
->f_data
;
1221 if ( (error
= vnode_getwithref(vp
)) ) {
1224 error
= VNOP_OFFTOBLK(vp
, fp
->f_offset
, &lbn
);
1226 (void)vnode_put(vp
);
1229 error
= VNOP_BLKTOOFF(vp
, lbn
, &offset
);
1231 (void)vnode_put(vp
);
1234 devBlockSize
= vfs_devblocksize(vnode_mount(vp
));
1236 error
= VNOP_BLOCKMAP(vp
, offset
, devBlockSize
, &bn
, NULL
, NULL
, 0, &context
);
1238 (void)vnode_put(vp
);
1241 l2p_struct
.l2p_flags
= 0; /* for now */
1242 l2p_struct
.l2p_contigbytes
= 0; /* for now */
1243 l2p_struct
.l2p_devoffset
= bn
* devBlockSize
;
1244 l2p_struct
.l2p_devoffset
+= fp
->f_offset
- offset
;
1245 error
= copyout((caddr_t
)&l2p_struct
, argp
, sizeof(l2p_struct
));
1253 if (fp
->f_type
!= DTYPE_VNODE
) {
1257 vp
= (struct vnode
*)fp
->f_data
;
1260 pathlen
= MAXPATHLEN
;
1261 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
1262 if (pathbufp
== NULL
) {
1266 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1267 error
= vn_getpath(vp
, pathbufp
, &pathlen
);
1268 (void)vnode_put(vp
);
1271 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
1273 FREE(pathbufp
, M_TEMP
);
1277 case F_PATHPKG_CHECK
: {
1281 if (fp
->f_type
!= DTYPE_VNODE
) {
1285 vp
= (struct vnode
*)fp
->f_data
;
1288 pathlen
= MAXPATHLEN
;
1289 pathbufp
= kalloc(MAXPATHLEN
);
1291 if ( (error
= copyinstr(argp
, pathbufp
, MAXPATHLEN
, &pathlen
)) == 0 ) {
1292 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1293 AUDIT_ARG(text
, pathbufp
);
1294 error
= vn_path_package_check(vp
, pathbufp
, pathlen
, retval
);
1296 (void)vnode_put(vp
);
1299 kfree(pathbufp
, MAXPATHLEN
);
1303 case F_CHKCLEAN
: // used by regression tests to see if all dirty pages got cleaned by fsync()
1304 case F_FULLFSYNC
: // fsync + flush the journal + DKIOCSYNCHRONIZECACHE
1305 case F_FREEZE_FS
: // freeze all other fs operations for the fs of this fd
1306 case F_THAW_FS
: { // thaw all frozen fs operations for the fs of this fd
1307 if (fp
->f_type
!= DTYPE_VNODE
) {
1311 vp
= (struct vnode
*)fp
->f_data
;
1314 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1315 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
1317 (void)vnode_put(vp
);
1323 * SPI (private) for opening a file starting from a dir fd
1326 struct user_fopenfrom fopen
;
1327 struct vnode_attr va
;
1328 struct nameidata nd
;
1331 /* Check if this isn't a valid file descriptor */
1332 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1333 (fp
->f_flag
& FREAD
) == 0) {
1337 vp
= (struct vnode
*)fp
->f_data
;
1340 if (vnode_getwithref(vp
)) {
1345 /* Only valid for directories */
1346 if (vp
->v_type
!= VDIR
) {
1352 /* Get flags, mode and pathname arguments. */
1353 if (IS_64BIT_PROCESS(p
)) {
1354 error
= copyin(argp
, &fopen
, sizeof(fopen
));
1356 struct user32_fopenfrom fopen32
;
1358 error
= copyin(argp
, &fopen32
, sizeof(fopen32
));
1359 fopen
.o_flags
= fopen32
.o_flags
;
1360 fopen
.o_mode
= fopen32
.o_mode
;
1361 fopen
.o_pathname
= CAST_USER_ADDR_T(fopen32
.o_pathname
);
1367 AUDIT_ARG(fflags
, fopen
.o_flags
);
1368 AUDIT_ARG(mode
, fopen
.o_mode
);
1370 /* Mask off all but regular access permissions */
1371 cmode
= ((fopen
.o_mode
&~ fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
1372 VATTR_SET(&va
, va_mode
, cmode
& ACCESSPERMS
);
1374 /* Start the lookup relative to the file descriptor's vnode. */
1375 NDINIT(&nd
, LOOKUP
, USEDVP
| FOLLOW
| AUDITVNPATH1
, UIO_USERSPACE
,
1376 fopen
.o_pathname
, &context
);
1379 error
= open1(&context
, &nd
, fopen
.o_flags
, &va
, retval
);
1385 * SPI (private) for unlinking a file starting from a dir fd
1387 case F_UNLINKFROM
: {
1388 struct nameidata nd
;
1389 user_addr_t pathname
;
1391 /* Check if this isn't a valid file descriptor */
1392 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1393 (fp
->f_flag
& FREAD
) == 0) {
1397 vp
= (struct vnode
*)fp
->f_data
;
1400 if (vnode_getwithref(vp
)) {
1405 /* Only valid for directories */
1406 if (vp
->v_type
!= VDIR
) {
1412 /* Get flags, mode and pathname arguments. */
1413 if (IS_64BIT_PROCESS(p
)) {
1414 pathname
= (user_addr_t
)argp
;
1416 pathname
= CAST_USER_ADDR_T(argp
);
1419 /* Start the lookup relative to the file descriptor's vnode. */
1420 NDINIT(&nd
, DELETE
, USEDVP
| AUDITVNPATH1
, UIO_USERSPACE
, pathname
, &context
);
1423 error
= unlink1(&context
, &nd
, 0);
1433 struct user_fsignatures fs
;
1435 vm_offset_t kernel_blob_addr
;
1436 vm_size_t kernel_blob_size
;
1438 if (fp
->f_type
!= DTYPE_VNODE
) {
1442 vp
= (struct vnode
*)fp
->f_data
;
1444 error
= vnode_getwithref(vp
);
1448 if (IS_64BIT_PROCESS(p
)) {
1449 error
= copyin(argp
, &fs
, sizeof (fs
));
1451 struct user32_fsignatures fs32
;
1453 error
= copyin(argp
, &fs32
, sizeof (fs32
));
1454 fs
.fs_file_start
= fs32
.fs_file_start
;
1455 fs
.fs_blob_start
= CAST_USER_ADDR_T(fs32
.fs_blob_start
);
1456 fs
.fs_blob_size
= fs32
.fs_blob_size
;
1464 if(ubc_cs_blob_get(vp
, CPU_TYPE_ANY
, fs
.fs_file_start
))
1468 printf("CODE SIGNING: resident blob offered for: %s\n", vp->v_name);
1474 #define CS_MAX_BLOB_SIZE (1ULL * 1024 * 1024) /* XXX ? */
1475 if (fs
.fs_blob_size
> CS_MAX_BLOB_SIZE
) {
1481 kernel_blob_size
= CAST_DOWN(vm_size_t
, fs
.fs_blob_size
);
1482 kr
= ubc_cs_blob_allocate(&kernel_blob_addr
, &kernel_blob_size
);
1483 if (kr
!= KERN_SUCCESS
) {
1489 if(uap
->cmd
== F_ADDSIGS
) {
1490 error
= copyin(fs
.fs_blob_start
,
1491 (void *) kernel_blob_addr
,
1493 } else /* F_ADDFILESIGS */ {
1494 error
= vn_rdwr(UIO_READ
,
1496 (caddr_t
) kernel_blob_addr
,
1498 fs
.fs_file_start
+ fs
.fs_blob_start
,
1507 ubc_cs_blob_deallocate(kernel_blob_addr
,
1513 error
= ubc_cs_blob_add(
1515 CPU_TYPE_ANY
, /* not for a specific architecture */
1520 ubc_cs_blob_deallocate(kernel_blob_addr
,
1523 /* ubc_blob_add() has consumed "kernel_blob_addr" */
1526 (void) vnode_put(vp
);
1530 case F_MARKDEPENDENCY
: {
1531 struct vnode
*root_vp
;
1532 struct vnode_attr va
;
1533 vfs_context_t ctx
= vfs_context_current();
1536 if ((current_proc()->p_flag
& P_DEPENDENCY_CAPABLE
) == 0) {
1541 if (fp
->f_type
!= DTYPE_VNODE
) {
1546 vp
= (struct vnode
*)fp
->f_data
;
1549 if (vnode_getwithref(vp
)) {
1554 // the passed in vnode must be the root dir of the file system
1555 if (VFS_ROOT(vp
->v_mount
, &root_vp
, ctx
) != 0 || vp
!= root_vp
) {
1562 // get the owner of the root dir
1564 VATTR_WANTED(&va
, va_uid
);
1565 if (vnode_getattr(vp
, &va
, ctx
) != 0) {
1571 // and last, check that the caller is the super user or
1572 // the owner of the mount point
1573 cred
= vfs_context_ucred(ctx
);
1574 if (!is_suser() && va
.va_uid
!= kauth_cred_getuid(cred
)) {
1580 // if all those checks pass then we can mark the dependency
1581 vfs_markdependency(vp
->v_mount
);
1591 * This is an fcntl() that we d not recognize at this level;
1592 * if this is a vnode, we send it down into the VNOP_IOCTL
1593 * for this vnode; this can include special devices, and will
1594 * effectively overload fcntl() to send ioctl()'s.
1596 if((uap
->cmd
& IOC_VOID
) && (uap
->cmd
& IOC_INOUT
)){
1601 if (fp
->f_type
!= DTYPE_VNODE
) {
1605 vp
= (struct vnode
*)fp
->f_data
;
1608 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1609 #define STK_PARAMS 128
1610 char stkbuf
[STK_PARAMS
];
1614 * For this to work properly, we have to copy in the
1615 * ioctl() cmd argument if there is one; we must also
1616 * check that a command parameter, if present, does
1617 * not exceed the maximum command length dictated by
1618 * the number of bits we have available in the command
1619 * to represent a structure length. Finally, we have
1620 * to copy the results back out, if it is that type of
1623 size
= IOCPARM_LEN(uap
->cmd
);
1624 if (size
> IOCPARM_MAX
) {
1625 (void)vnode_put(vp
);
1631 if (size
> sizeof (stkbuf
)) {
1632 if ((memp
= (caddr_t
)kalloc(size
)) == 0) {
1633 (void)vnode_put(vp
);
1642 if (uap
->cmd
& IOC_IN
) {
1645 error
= copyin(argp
, data
, size
);
1647 (void)vnode_put(vp
);
1655 *(user_addr_t
*)data
= argp
;
1657 *(uint32_t *)data
= (uint32_t)argp
;
1660 } else if ((uap
->cmd
& IOC_OUT
) && size
) {
1662 * Zero the buffer so the user always
1663 * gets back something deterministic.
1666 } else if (uap
->cmd
& IOC_VOID
) {
1668 *(user_addr_t
*)data
= argp
;
1670 *(uint32_t *)data
= (uint32_t)argp
;
1674 error
= VNOP_IOCTL(vp
, uap
->cmd
, CAST_DOWN(caddr_t
, data
), 0, &context
);
1676 (void)vnode_put(vp
);
1678 /* Copy any output data to user */
1679 if (error
== 0 && (uap
->cmd
& IOC_OUT
) && size
)
1680 error
= copyout(data
, argp
, size
);
1688 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
1689 fp_drop(p
, fd
, fp
, 0);
1692 fp_drop(p
, fd
, fp
, 1);
1701 * Description: Common code for dup, dup2, and fcntl(F_DUPFD).
1703 * Parameters: p Process performing the dup
1705 * new The fd to dup it to
1706 * retval Pointer to the call return area
1708 * Returns: 0 Success
1713 * *retval (modified) The new descriptor
1715 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
1718 * Notes: This function may drop and reacquire this lock; it is unsafe
1719 * for a caller to assume that other state protected by the lock
1720 * has not been subsequently changes out from under it.
1723 finishdup(proc_t p
, struct filedesc
*fdp
, int old
, int new, int32_t *retval
)
1725 struct fileproc
*nfp
;
1726 struct fileproc
*ofp
;
1732 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
1735 if ((ofp
= fdp
->fd_ofiles
[old
]) == NULL
||
1736 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
)) {
1743 error
= mac_file_check_dup(proc_ucred(p
), ofp
->f_fglob
, new);
1753 MALLOC_ZONE(nfp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
1754 /* Failure check follows proc_fdlock() due to handling requirements */
1764 bzero(nfp
, sizeof(struct fileproc
));
1767 nfp
->f_fglob
= ofp
->f_fglob
;
1771 if (fdp
->fd_ofiles
[new] != 0)
1772 panic("finishdup: overwriting fd_ofiles with new %d\n", new);
1773 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0)
1774 panic("finishdup: unreserved fileflags with new %d\n", new);
1777 if (new > fdp
->fd_lastfile
)
1778 fdp
->fd_lastfile
= new;
1779 procfdtbl_releasefd(p
, new, nfp
);
1788 * Description: The implementation of the close(2) system call
1790 * Parameters: p Process in whose per process file table
1791 * the close is to occur
1792 * uap->fd fd to be closed
1795 * Returns: 0 Success
1796 * fp_lookup:EBADF Bad file descriptor
1797 * close_internal:EBADF
1798 * close_internal:??? Anything returnable by a per-fileops
1802 close(proc_t p
, struct close_args
*uap
, int32_t *retval
)
1804 __pthread_testcancel(1);
1805 return(close_nocancel(p
, (struct close_nocancel_args
*)uap
, retval
));
1810 close_nocancel(proc_t p
, struct close_nocancel_args
*uap
, __unused
int32_t *retval
)
1812 struct fileproc
*fp
;
1816 AUDIT_SYSCLOSE(p
, fd
);
1820 if ( (error
= fp_lookup(p
,fd
,&fp
, 1)) ) {
1825 error
= close_internal_locked(p
, fd
, fp
, 0);
1834 * close_internal_locked
1836 * Close a file descriptor.
1838 * Parameters: p Process in whose per process file table
1839 * the close is to occur
1840 * fd fd to be closed
1841 * fp fileproc associated with the fd
1843 * Returns: 0 Success
1844 * EBADF fd already in close wait state
1845 * closef_locked:??? Anything returnable by a per-fileops
1848 * Locks: Assumes proc_fdlock for process is held by the caller and returns
1851 * Notes: This function may drop and reacquire this lock; it is unsafe
1852 * for a caller to assume that other state protected by the lock
1853 * has not been subsequently changes out from under it, if the
1854 * caller made the call with the lock held.
1857 close_internal_locked(proc_t p
, int fd
, struct fileproc
*fp
, int flags
)
1859 struct filedesc
*fdp
= p
->p_fd
;
1861 int resvfd
= flags
& FD_DUP2RESV
;
1865 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
1868 /* Keep people from using the filedesc while we are closing it */
1869 procfdtbl_markclosefd(p
, fd
);
1872 if ((fp
->f_flags
& FP_CLOSING
) == FP_CLOSING
) {
1873 panic("close_internal_locked: being called on already closing fd\n");
1878 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
1879 panic("close_internal: unreserved fileflags with fd %d\n", fd
);
1882 fp
->f_flags
|= FP_CLOSING
;
1884 if ( (fp
->f_flags
& FP_AIOISSUED
) || kauth_authorize_fileop_has_listeners() ) {
1888 if ( (fp
->f_type
== DTYPE_VNODE
) && kauth_authorize_fileop_has_listeners() ) {
1890 * call out to allow 3rd party notification of close.
1891 * Ignore result of kauth_authorize_fileop call.
1893 if (vnode_getwithref((vnode_t
)fp
->f_data
) == 0) {
1894 u_int fileop_flags
= 0;
1895 if ((fp
->f_flags
& FP_WRITTEN
) != 0)
1896 fileop_flags
|= KAUTH_FILEOP_CLOSE_MODIFIED
;
1897 kauth_authorize_fileop(fp
->f_fglob
->fg_cred
, KAUTH_FILEOP_CLOSE
,
1898 (uintptr_t)fp
->f_data
, (uintptr_t)fileop_flags
);
1899 vnode_put((vnode_t
)fp
->f_data
);
1902 if (fp
->f_flags
& FP_AIOISSUED
)
1904 * cancel all async IO requests that can be cancelled.
1906 _aio_close( p
, fd
);
1911 if (fd
< fdp
->fd_knlistsize
)
1912 knote_fdclose(p
, fd
);
1914 if (fp
->f_flags
& FP_WAITEVENT
)
1915 (void)waitevent_close(p
, fp
);
1917 if ((fp
->f_flags
& FP_INCHRREAD
) == 0)
1918 fileproc_drain(p
, fp
);
1923 error
= closef_locked(fp
, fp
->f_fglob
, p
);
1924 if ((fp
->f_flags
& FP_WAITCLOSE
) == FP_WAITCLOSE
)
1925 wakeup(&fp
->f_flags
);
1926 fp
->f_flags
&= ~(FP_WAITCLOSE
| FP_CLOSING
);
1930 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
1936 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
1937 panic("close with reserved fd returns with freed fd:%d: proc: %x\n", fd
, (unsigned int)p
);
1948 * Description: Return status information about a file descriptor.
1950 * Parameters: p The process doing the fstat
1952 * ub The user stat buffer
1953 * xsecurity The user extended security
1954 * buffer, or 0 if none
1955 * xsecurity_size The size of xsecurity, or 0
1957 * isstat64 Flag to indicate 64 bit version
1958 * for inode size, etc.
1960 * Returns: 0 Success
1963 * fp_lookup:EBADF Bad file descriptor
1964 * vnode_getwithref:???
1966 * vnode_getwithref:???
1973 * Notes: Internal implementation for all other fstat() related
1976 * XXX switch on node type is bogus; need a stat in struct
1977 * XXX fileops instead.
1980 fstat1(proc_t p
, int fd
, user_addr_t ub
, user_addr_t xsecurity
, user_addr_t xsecurity_size
, int isstat64
)
1982 struct fileproc
*fp
;
1988 struct user64_stat user64_sb
;
1989 struct user32_stat user32_sb
;
1990 struct user64_stat64 user64_sb64
;
1991 struct user32_stat64 user32_sb64
;
1997 kauth_filesec_t fsec
;
1998 user_size_t xsecurity_bufsize
;
1999 vfs_context_t ctx
= vfs_context_current();
2005 if ((error
= fp_lookup(p
, fd
, &fp
, 0)) != 0) {
2010 fsec
= KAUTH_FILESEC_NONE
;
2012 sbptr
= (void *)&source
;
2017 if ((error
= vnode_getwithref((vnode_t
)data
)) == 0) {
2019 * If the caller has the file open, and is not
2020 * requesting extended security information, we are
2021 * going to let them get the basic stat information.
2023 if (xsecurity
== USER_ADDR_NULL
) {
2024 error
= vn_stat_noauth((vnode_t
)data
, sbptr
, NULL
, isstat64
, ctx
);
2026 error
= vn_stat((vnode_t
)data
, sbptr
, &fsec
, isstat64
, ctx
);
2029 AUDIT_ARG(vnpath
, (struct vnode
*)data
, ARG_VNODE1
);
2030 (void)vnode_put((vnode_t
)data
);
2036 error
= soo_stat((struct socket
*)data
, sbptr
, isstat64
);
2038 #endif /* SOCKETS */
2041 error
= pipe_stat((void *)data
, sbptr
, isstat64
);
2045 error
= pshm_stat((void *)data
, sbptr
, isstat64
);
2049 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2050 error
= kqueue_stat(fp
, sbptr
, isstat64
, p
);
2051 thread_funnel_set(kernel_flock
, funnel_state
);
2061 if (isstat64
!= 0) {
2062 source
.sb64
.st_lspare
= 0;
2063 source
.sb64
.st_qspare
[0] = 0LL;
2064 source
.sb64
.st_qspare
[1] = 0LL;
2066 if (IS_64BIT_PROCESS(current_proc())) {
2067 munge_user64_stat64(&source
.sb64
, &dest
.user64_sb64
);
2068 my_size
= sizeof(dest
.user64_sb64
);
2069 sbp
= (caddr_t
)&dest
.user64_sb64
;
2071 munge_user32_stat64(&source
.sb64
, &dest
.user32_sb64
);
2072 my_size
= sizeof(dest
.user32_sb64
);
2073 sbp
= (caddr_t
)&dest
.user32_sb64
;
2076 source
.sb
.st_lspare
= 0;
2077 source
.sb
.st_qspare
[0] = 0LL;
2078 source
.sb
.st_qspare
[1] = 0LL;
2079 if (IS_64BIT_PROCESS(current_proc())) {
2080 munge_user64_stat(&source
.sb
, &dest
.user64_sb
);
2081 my_size
= sizeof(dest
.user64_sb
);
2082 sbp
= (caddr_t
)&dest
.user64_sb
;
2084 munge_user32_stat(&source
.sb
, &dest
.user32_sb
);
2085 my_size
= sizeof(dest
.user32_sb
);
2086 sbp
= (caddr_t
)&dest
.user32_sb
;
2090 error
= copyout(sbp
, ub
, my_size
);
2093 /* caller wants extended security information? */
2094 if (xsecurity
!= USER_ADDR_NULL
) {
2096 /* did we get any? */
2097 if (fsec
== KAUTH_FILESEC_NONE
) {
2098 if (susize(xsecurity_size
, 0) != 0) {
2103 /* find the user buffer size */
2104 xsecurity_bufsize
= fusize(xsecurity_size
);
2106 /* copy out the actual data size */
2107 if (susize(xsecurity_size
, KAUTH_FILESEC_COPYSIZE(fsec
)) != 0) {
2112 /* if the caller supplied enough room, copy out to it */
2113 if (xsecurity_bufsize
>= KAUTH_FILESEC_COPYSIZE(fsec
))
2114 error
= copyout(fsec
, xsecurity
, KAUTH_FILESEC_COPYSIZE(fsec
));
2118 fp_drop(p
, fd
, fp
, 0);
2120 kauth_filesec_free(fsec
);
2128 * Description: Extended version of fstat supporting returning extended
2129 * security information
2131 * Parameters: p The process doing the fstat
2132 * uap->fd The fd to stat
2133 * uap->ub The user stat buffer
2134 * uap->xsecurity The user extended security
2135 * buffer, or 0 if none
2136 * uap->xsecurity_size The size of xsecurity, or 0
2138 * Returns: 0 Success
2139 * !0 Errno (see fstat1)
2142 fstat_extended(proc_t p
, struct fstat_extended_args
*uap
, __unused
int32_t *retval
)
2144 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 0));
2151 * Description: Get file status for the file associated with fd
2153 * Parameters: p The process doing the fstat
2154 * uap->fd The fd to stat
2155 * uap->ub The user stat buffer
2157 * Returns: 0 Success
2158 * !0 Errno (see fstat1)
2161 fstat(proc_t p
, register struct fstat_args
*uap
, __unused
int32_t *retval
)
2163 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 0));
2170 * Description: Extended version of fstat64 supporting returning extended
2171 * security information
2173 * Parameters: p The process doing the fstat
2174 * uap->fd The fd to stat
2175 * uap->ub The user stat buffer
2176 * uap->xsecurity The user extended security
2177 * buffer, or 0 if none
2178 * uap->xsecurity_size The size of xsecurity, or 0
2180 * Returns: 0 Success
2181 * !0 Errno (see fstat1)
2184 fstat64_extended(proc_t p
, struct fstat64_extended_args
*uap
, __unused
int32_t *retval
)
2186 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 1));
2193 * Description: Get 64 bit version of the file status for the file associated
2196 * Parameters: p The process doing the fstat
2197 * uap->fd The fd to stat
2198 * uap->ub The user stat buffer
2200 * Returns: 0 Success
2201 * !0 Errno (see fstat1)
2204 fstat64(proc_t p
, register struct fstat64_args
*uap
, __unused
int32_t *retval
)
2206 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 1));
2213 * Description: Return pathconf information about a file descriptor.
2215 * Parameters: p Process making the request
2216 * uap->fd fd to get information about
2217 * uap->name Name of information desired
2218 * retval Pointer to the call return area
2220 * Returns: 0 Success
2222 * fp_lookup:EBADF Bad file descriptor
2223 * vnode_getwithref:???
2227 * *retval (modified) Returned information (numeric)
2230 fpathconf(proc_t p
, struct fpathconf_args
*uap
, int32_t *retval
)
2233 struct fileproc
*fp
;
2240 AUDIT_ARG(fd
, uap
->fd
);
2241 if ( (error
= fp_lookup(p
, fd
, &fp
, 0)) )
2249 if (uap
->name
!= _PC_PIPE_BUF
) {
2258 if (uap
->name
!= _PC_PIPE_BUF
) {
2267 vp
= (struct vnode
*)data
;
2269 if ( (error
= vnode_getwithref(vp
)) == 0) {
2270 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
2272 error
= vn_pathconf(vp
, uap
->name
, retval
, vfs_context_current());
2274 (void)vnode_put(vp
);
2281 case DTYPE_FSEVENTS
:
2288 fp_drop(p
, fd
, fp
, 0);
2293 * Statistics counter for the number of times a process calling fdalloc()
2294 * has resulted in an expansion of the per process open file table.
2296 * XXX This would likely be of more use if it were per process
2304 * Description: Allocate a file descriptor for the process.
2306 * Parameters: p Process to allocate the fd in
2307 * want The fd we would prefer to get
2308 * result Pointer to fd we got
2310 * Returns: 0 Success
2315 * *result (modified) The fd which was allocated
2318 fdalloc(proc_t p
, int want
, int *result
)
2320 struct filedesc
*fdp
= p
->p_fd
;
2322 int lim
, last
, numfiles
, oldnfiles
;
2323 struct fileproc
**newofiles
, **ofiles
;
2324 char *newofileflags
;
2327 * Search for a free descriptor starting at the higher
2328 * of want or fd_freefile. If that fails, consider
2329 * expanding the ofile array.
2332 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2335 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
2337 last
= min(fdp
->fd_nfiles
, lim
);
2338 if ((i
= want
) < fdp
->fd_freefile
)
2339 i
= fdp
->fd_freefile
;
2340 for (; i
< last
; i
++) {
2341 if (fdp
->fd_ofiles
[i
] == NULL
&& !(fdp
->fd_ofileflags
[i
] & UF_RESERVED
)) {
2342 procfdtbl_reservefd(p
, i
);
2343 if (i
> fdp
->fd_lastfile
)
2344 fdp
->fd_lastfile
= i
;
2345 if (want
<= fdp
->fd_freefile
)
2346 fdp
->fd_freefile
= i
;
2353 * No space in current array. Expand?
2355 if (fdp
->fd_nfiles
>= lim
)
2357 if (fdp
->fd_nfiles
< NDEXTENT
)
2358 numfiles
= NDEXTENT
;
2360 numfiles
= 2 * fdp
->fd_nfiles
;
2365 MALLOC_ZONE(newofiles
, struct fileproc
**,
2366 numfiles
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
2368 if (newofiles
== NULL
) {
2371 if (fdp
->fd_nfiles
>= numfiles
) {
2372 FREE_ZONE(newofiles
, numfiles
* OFILESIZE
, M_OFILETABL
);
2375 newofileflags
= (char *) &newofiles
[numfiles
];
2377 * Copy the existing ofile and ofileflags arrays
2378 * and zero the new portion of each array.
2380 oldnfiles
= fdp
->fd_nfiles
;
2381 (void) memcpy(newofiles
, fdp
->fd_ofiles
,
2382 oldnfiles
* sizeof(*fdp
->fd_ofiles
));
2383 (void) memset(&newofiles
[oldnfiles
], 0,
2384 (numfiles
- oldnfiles
) * sizeof(*fdp
->fd_ofiles
));
2386 (void) memcpy(newofileflags
, fdp
->fd_ofileflags
,
2387 oldnfiles
* sizeof(*fdp
->fd_ofileflags
));
2388 (void) memset(&newofileflags
[oldnfiles
], 0,
2389 (numfiles
- oldnfiles
) *
2390 sizeof(*fdp
->fd_ofileflags
));
2391 ofiles
= fdp
->fd_ofiles
;
2392 fdp
->fd_ofiles
= newofiles
;
2393 fdp
->fd_ofileflags
= newofileflags
;
2394 fdp
->fd_nfiles
= numfiles
;
2395 FREE_ZONE(ofiles
, oldnfiles
* OFILESIZE
, M_OFILETABL
);
2404 * Description: Check to see whether n user file descriptors are available
2407 * Parameters: p Process to check in
2408 * n The number of fd's desired
2413 * Locks: Assumes proc_fdlock for process is held by the caller
2415 * Notes: The answer only remains valid so long as the proc_fdlock is
2416 * held by the caller.
2419 fdavail(proc_t p
, int n
)
2421 struct filedesc
*fdp
= p
->p_fd
;
2422 struct fileproc
**fpp
;
2426 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
2427 if ((i
= lim
- fdp
->fd_nfiles
) > 0 && (n
-= i
) <= 0)
2429 fpp
= &fdp
->fd_ofiles
[fdp
->fd_freefile
];
2430 flags
= &fdp
->fd_ofileflags
[fdp
->fd_freefile
];
2431 for (i
= fdp
->fd_nfiles
- fdp
->fd_freefile
; --i
>= 0; fpp
++, flags
++)
2432 if (*fpp
== NULL
&& !(*flags
& UF_RESERVED
) && --n
<= 0)
2441 * Description: Legacy KPI wrapper function for _fdrelse
2443 * Parameters: p Process in which fd lives
2448 * Locks: Assumes proc_fdlock for process is held by the caller
2451 fdrelse(proc_t p
, int fd
)
2460 * Description: Get the fileproc pointer for the given fd from the per process
2461 * open file table without taking an explicit reference on it.
2463 * Parameters: p Process containing fd
2464 * fd fd to obtain fileproc for
2465 * resultfp Pointer to pointer return area
2467 * Returns: 0 Success
2471 * *resultfp (modified) Pointer to fileproc pointer
2473 * Locks: Assumes proc_fdlock for process is held by the caller
2475 * Notes: Because there is no reference explicitly taken, the returned
2476 * fileproc pointer is only valid so long as the proc_fdlock
2477 * remains held by the caller.
2480 fdgetf_noref(proc_t p
, int fd
, struct fileproc
**resultfp
)
2482 struct filedesc
*fdp
= p
->p_fd
;
2483 struct fileproc
*fp
;
2485 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2486 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2487 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2499 * Description: Get fileproc and vnode pointer for a given fd from the per
2500 * process open file table of the specified process, and if
2501 * successful, increment the f_iocount
2503 * Parameters: p Process in which fd lives
2504 * fd fd to get information for
2505 * resultfp Pointer to result fileproc
2506 * pointer area, or 0 if none
2507 * resultvp Pointer to result vnode pointer
2508 * area, or 0 if none
2510 * Returns: 0 Success
2511 * EBADF Bad file descriptor
2512 * ENOTSUP fd does not refer to a vnode
2515 * *resultfp (modified) Fileproc pointer
2516 * *resultvp (modified) vnode pointer
2518 * Notes: The resultfp and resultvp fields are optional, and may be
2519 * independently specified as NULL to skip returning information
2521 * Locks: Internally takes and releases proc_fdlock
2524 fp_getfvp(proc_t p
, int fd
, struct fileproc
**resultfp
, struct vnode
**resultvp
)
2526 struct filedesc
*fdp
= p
->p_fd
;
2527 struct fileproc
*fp
;
2529 proc_fdlock_spin(p
);
2530 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2531 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2532 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2536 if (fp
->f_type
!= DTYPE_VNODE
) {
2545 *resultvp
= (struct vnode
*)fp
->f_data
;
2555 * Description: Get fileproc, vnode pointer, and vid for a given fd from the
2556 * per process open file table of the specified process, and if
2557 * successful, increment the f_iocount
2559 * Parameters: p Process in which fd lives
2560 * fd fd to get information for
2561 * resultfp Pointer to result fileproc
2562 * pointer area, or 0 if none
2563 * resultvp Pointer to result vnode pointer
2564 * area, or 0 if none
2565 * vidp Pointer to resuld vid area
2567 * Returns: 0 Success
2568 * EBADF Bad file descriptor
2569 * ENOTSUP fd does not refer to a vnode
2572 * *resultfp (modified) Fileproc pointer
2573 * *resultvp (modified) vnode pointer
2576 * Notes: The resultfp and resultvp fields are optional, and may be
2577 * independently specified as NULL to skip returning information
2579 * Locks: Internally takes and releases proc_fdlock
2582 fp_getfvpandvid(proc_t p
, int fd
, struct fileproc
**resultfp
,
2583 struct vnode
**resultvp
, uint32_t *vidp
)
2585 struct filedesc
*fdp
= p
->p_fd
;
2586 struct fileproc
*fp
;
2588 proc_fdlock_spin(p
);
2589 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2590 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2591 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2595 if (fp
->f_type
!= DTYPE_VNODE
) {
2604 *resultvp
= (struct vnode
*)fp
->f_data
;
2606 *vidp
= (uint32_t)vnode_vid((struct vnode
*)fp
->f_data
);
2616 * Description: Get fileproc and socket pointer for a given fd from the
2617 * per process open file table of the specified process, and if
2618 * successful, increment the f_iocount
2620 * Parameters: p Process in which fd lives
2621 * fd fd to get information for
2622 * resultfp Pointer to result fileproc
2623 * pointer area, or 0 if none
2624 * results Pointer to result socket
2625 * pointer area, or 0 if none
2627 * Returns: EBADF The file descriptor is invalid
2628 * EOPNOTSUPP The file descriptor is not a socket
2632 * *resultfp (modified) Fileproc pointer
2633 * *results (modified) socket pointer
2635 * Notes: EOPNOTSUPP should probably be ENOTSOCK; this function is only
2636 * ever called from accept1().
2639 fp_getfsock(proc_t p
, int fd
, struct fileproc
**resultfp
,
2640 struct socket
**results
)
2642 struct filedesc
*fdp
= p
->p_fd
;
2643 struct fileproc
*fp
;
2645 proc_fdlock_spin(p
);
2646 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2647 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2648 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2652 if (fp
->f_type
!= DTYPE_SOCKET
) {
2661 *results
= (struct socket
*)fp
->f_data
;
2671 * Description: Get fileproc and kqueue pointer for a given fd from the
2672 * per process open file table of the specified process, and if
2673 * successful, increment the f_iocount
2675 * Parameters: p Process in which fd lives
2676 * fd fd to get information for
2677 * resultfp Pointer to result fileproc
2678 * pointer area, or 0 if none
2679 * resultkq Pointer to result kqueue
2680 * pointer area, or 0 if none
2682 * Returns: EBADF The file descriptor is invalid
2683 * EBADF The file descriptor is not a socket
2687 * *resultfp (modified) Fileproc pointer
2688 * *resultkq (modified) kqueue pointer
2690 * Notes: The second EBADF should probably be something else to make
2691 * the error condition distinct.
2694 fp_getfkq(proc_t p
, int fd
, struct fileproc
**resultfp
,
2695 struct kqueue
**resultkq
)
2697 struct filedesc
*fdp
= p
->p_fd
;
2698 struct fileproc
*fp
;
2700 proc_fdlock_spin(p
);
2701 if ( fd
< 0 || fd
>= fdp
->fd_nfiles
||
2702 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2703 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2707 if (fp
->f_type
!= DTYPE_KQUEUE
) {
2716 *resultkq
= (struct kqueue
*)fp
->f_data
;
2726 * Description: Get fileproc and POSIX shared memory pointer for a given fd
2727 * from the per process open file table of the specified process
2728 * and if successful, increment the f_iocount
2730 * Parameters: p Process in which fd lives
2731 * fd fd to get information for
2732 * resultfp Pointer to result fileproc
2733 * pointer area, or 0 if none
2734 * resultpshm Pointer to result POSIX
2735 * shared memory pointer
2736 * pointer area, or 0 if none
2738 * Returns: EBADF The file descriptor is invalid
2739 * EBADF The file descriptor is not a POSIX
2740 * shared memory area
2744 * *resultfp (modified) Fileproc pointer
2745 * *resultpshm (modified) POSIX shared memory pointer
2747 * Notes: The second EBADF should probably be something else to make
2748 * the error condition distinct.
2751 fp_getfpshm(proc_t p
, int fd
, struct fileproc
**resultfp
,
2752 struct pshmnode
**resultpshm
)
2754 struct filedesc
*fdp
= p
->p_fd
;
2755 struct fileproc
*fp
;
2757 proc_fdlock_spin(p
);
2758 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2759 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2760 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2764 if (fp
->f_type
!= DTYPE_PSXSHM
) {
2774 *resultpshm
= (struct pshmnode
*)fp
->f_data
;
2784 * Description: Get fileproc and POSIX semaphore pointer for a given fd from
2785 * the per process open file table of the specified process
2786 * and if successful, increment the f_iocount
2788 * Parameters: p Process in which fd lives
2789 * fd fd to get information for
2790 * resultfp Pointer to result fileproc
2791 * pointer area, or 0 if none
2792 * resultpsem Pointer to result POSIX
2793 * semaphore pointer area, or
2796 * Returns: EBADF The file descriptor is invalid
2797 * EBADF The file descriptor is not a POSIX
2802 * *resultfp (modified) Fileproc pointer
2803 * *resultpsem (modified) POSIX semaphore pointer
2805 * Notes: The second EBADF should probably be something else to make
2806 * the error condition distinct.
2808 * In order to support unnamed POSIX semaphores, the named
2809 * POSIX semaphores will have to move out of the per-process
2810 * open filetable, and into a global table that is shared with
2811 * unnamed POSIX semaphores, since unnamed POSIX semaphores
2812 * are typically used by declaring instances in shared memory,
2813 * and there's no other way to do this without changing the
2814 * underlying type, which would introduce binary compatibility
2818 fp_getfpsem(proc_t p
, int fd
, struct fileproc
**resultfp
,
2819 struct psemnode
**resultpsem
)
2821 struct filedesc
*fdp
= p
->p_fd
;
2822 struct fileproc
*fp
;
2824 proc_fdlock_spin(p
);
2825 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2826 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2827 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2831 if (fp
->f_type
!= DTYPE_PSXSEM
) {
2840 *resultpsem
= (struct psemnode
*)fp
->f_data
;
2850 * Description: Get fileproc and pipe pointer for a given fd from the
2851 * per process open file table of the specified process
2852 * and if successful, increment the f_iocount
2854 * Parameters: p Process in which fd lives
2855 * fd fd to get information for
2856 * resultfp Pointer to result fileproc
2857 * pointer area, or 0 if none
2858 * resultpipe Pointer to result pipe
2859 * pointer area, or 0 if none
2861 * Returns: EBADF The file descriptor is invalid
2862 * EBADF The file descriptor is not a socket
2866 * *resultfp (modified) Fileproc pointer
2867 * *resultpipe (modified) pipe pointer
2869 * Notes: The second EBADF should probably be something else to make
2870 * the error condition distinct.
2873 fp_getfpipe(proc_t p
, int fd
, struct fileproc
**resultfp
,
2874 struct pipe
**resultpipe
)
2876 struct filedesc
*fdp
= p
->p_fd
;
2877 struct fileproc
*fp
;
2879 proc_fdlock_spin(p
);
2880 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2881 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2882 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2886 if (fp
->f_type
!= DTYPE_PIPE
) {
2895 *resultpipe
= (struct pipe
*)fp
->f_data
;
2902 #define DTYPE_ATALK -1 /* XXX This does not belong here */
2908 * Description: Get fileproc and atalk pointer for a given fd from the
2909 * per process open file table of the specified process
2910 * and if successful, increment the f_iocount
2912 * Parameters: p Process in which fd lives
2913 * fd fd to get information for
2914 * resultfp Pointer to result fileproc
2915 * pointer area, or 0 if none
2916 * resultatalk Pointer to result atalk
2917 * pointer area, or 0 if none
2918 * Returns: EBADF The file descriptor is invalid
2919 * EBADF The file descriptor is not a socket
2923 * *resultfp (modified) Fileproc pointer
2924 * *resultatalk (modified) atalk pointer
2926 * Notes: The second EBADF should probably be something else to make
2927 * the error condition distinct.
2929 * XXX This code is specific to AppleTalk protocol support, and
2930 * XXX should be conditionally compiled
2933 fp_getfatalk(proc_t p
, int fd
, struct fileproc
**resultfp
,
2934 struct atalk
**resultatalk
)
2936 struct filedesc
*fdp
= p
->p_fd
;
2937 struct fileproc
*fp
;
2939 proc_fdlock_spin(p
);
2940 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2941 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2942 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2946 if (fp
->f_type
!= (DTYPE_ATALK
+1)) {
2955 *resultatalk
= (struct atalk
*)fp
->f_data
;
2966 * Description: Get fileproc pointer for a given fd from the per process
2967 * open file table of the specified process and if successful,
2968 * increment the f_iocount
2970 * Parameters: p Process in which fd lives
2971 * fd fd to get information for
2972 * resultfp Pointer to result fileproc
2973 * pointer area, or 0 if none
2974 * locked !0 if the caller holds the
2975 * proc_fdlock, 0 otherwise
2977 * Returns: 0 Success
2978 * EBADF Bad file descriptor
2981 * *resultfp (modified) Fileproc pointer
2983 * Locks: If the argument 'locked' is non-zero, then the caller is
2984 * expected to have taken and held the proc_fdlock; if it is
2985 * zero, than this routine internally takes and drops this lock.
2988 fp_lookup(proc_t p
, int fd
, struct fileproc
**resultfp
, int locked
)
2990 struct filedesc
*fdp
= p
->p_fd
;
2991 struct fileproc
*fp
;
2994 proc_fdlock_spin(p
);
2995 if (fd
< 0 || fdp
== NULL
|| fd
>= fdp
->fd_nfiles
||
2996 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2997 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3016 * Description: Set the FP_WRITTEN flag on the fileproc and drop the I/O
3017 * reference previously taken by calling fp_lookup et. al.
3019 * Parameters: p Process in which the fd lives
3020 * fd fd associated with the fileproc
3021 * fp fileproc on which to set the
3022 * flag and drop the reference
3024 * Returns: 0 Success
3025 * fp_drop:EBADF Bad file descriptor
3027 * Locks: This function internally takes and drops the proc_fdlock for
3028 * the supplied process
3030 * Notes: The fileproc must correspond to the fd in the supplied proc
3033 fp_drop_written(proc_t p
, int fd
, struct fileproc
*fp
)
3037 proc_fdlock_spin(p
);
3039 fp
->f_flags
|= FP_WRITTEN
;
3041 error
= fp_drop(p
, fd
, fp
, 1);
3052 * Description: Set the FP_WAITEVENT flag on the fileproc and drop the I/O
3053 * reference previously taken by calling fp_lookup et. al.
3055 * Parameters: p Process in which the fd lives
3056 * fd fd associated with the fileproc
3057 * fp fileproc on which to set the
3058 * flag and drop the reference
3060 * Returns: 0 Success
3061 * fp_drop:EBADF Bad file descriptor
3063 * Locks: This function internally takes and drops the proc_fdlock for
3064 * the supplied process
3066 * Notes: The fileproc must correspond to the fd in the supplied proc
3069 fp_drop_event(proc_t p
, int fd
, struct fileproc
*fp
)
3073 proc_fdlock_spin(p
);
3075 fp
->f_flags
|= FP_WAITEVENT
;
3077 error
= fp_drop(p
, fd
, fp
, 1);
3088 * Description: Drop the I/O reference previously taken by calling fp_lookup
3091 * Parameters: p Process in which the fd lives
3092 * fd fd associated with the fileproc
3093 * fp fileproc on which to set the
3094 * flag and drop the reference
3095 * locked flag to internally take and
3096 * drop proc_fdlock if it is not
3097 * already held by the caller
3099 * Returns: 0 Success
3100 * EBADF Bad file descriptor
3102 * Locks: This function internally takes and drops the proc_fdlock for
3103 * the supplied process if 'locked' is non-zero, and assumes that
3104 * the caller already holds this lock if 'locked' is non-zero.
3106 * Notes: The fileproc must correspond to the fd in the supplied proc
3109 fp_drop(proc_t p
, int fd
, struct fileproc
*fp
, int locked
)
3111 struct filedesc
*fdp
= p
->p_fd
;
3115 proc_fdlock_spin(p
);
3116 if ((fp
== FILEPROC_NULL
) && (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3117 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3118 ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
3119 !(fdp
->fd_ofileflags
[fd
] & UF_CLOSING
)))) {
3126 if (p
->p_fpdrainwait
&& fp
->f_iocount
== 0) {
3127 p
->p_fpdrainwait
= 0;
3133 wakeup(&p
->p_fpdrainwait
);
3142 * Description: Given an fd, look it up in the current process's per process
3143 * open file table, and return its internal vnode pointer.
3145 * Parameters: fd fd to obtain vnode from
3146 * vpp pointer to vnode return area
3148 * Returns: 0 Success
3149 * EINVAL The fd does not refer to a
3150 * vnode fileproc entry
3151 * fp_lookup:EBADF Bad file descriptor
3154 * *vpp (modified) Returned vnode pointer
3156 * Locks: This function internally takes and drops the proc_fdlock for
3157 * the current process
3159 * Notes: If successful, this function increments the f_iocount on the
3160 * fd's corresponding fileproc.
3162 * The fileproc referenced is not returned; because of this, care
3163 * must be taken to not drop the last reference (e.g. by closing
3164 * the file). This is inhernely unsafe, since the reference may
3165 * not be recoverable from the vnode, if there is a subsequent
3166 * close that destroys the associate fileproc. The caller should
3167 * therefore retain their own reference on the fileproc so that
3168 * the f_iocount can be dropped subsequently. Failure to do this
3169 * can result in the returned pointer immediately becoming invalid
3170 * following the call.
3172 * Use of this function is discouraged.
3175 file_vnode(int fd
, struct vnode
**vpp
)
3177 proc_t p
= current_proc();
3178 struct fileproc
*fp
;
3181 proc_fdlock_spin(p
);
3182 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
3186 if (fp
->f_type
!= DTYPE_VNODE
) {
3187 fp_drop(p
, fd
, fp
,1);
3192 *vpp
= (struct vnode
*)fp
->f_data
;
3200 * file_vnode_withvid
3202 * Description: Given an fd, look it up in the current process's per process
3203 * open file table, and return its internal vnode pointer.
3205 * Parameters: fd fd to obtain vnode from
3206 * vpp pointer to vnode return area
3207 * vidp pointer to vid of the returned vnode
3209 * Returns: 0 Success
3210 * EINVAL The fd does not refer to a
3211 * vnode fileproc entry
3212 * fp_lookup:EBADF Bad file descriptor
3215 * *vpp (modified) Returned vnode pointer
3217 * Locks: This function internally takes and drops the proc_fdlock for
3218 * the current process
3220 * Notes: If successful, this function increments the f_iocount on the
3221 * fd's corresponding fileproc.
3223 * The fileproc referenced is not returned; because of this, care
3224 * must be taken to not drop the last reference (e.g. by closing
3225 * the file). This is inhernely unsafe, since the reference may
3226 * not be recoverable from the vnode, if there is a subsequent
3227 * close that destroys the associate fileproc. The caller should
3228 * therefore retain their own reference on the fileproc so that
3229 * the f_iocount can be dropped subsequently. Failure to do this
3230 * can result in the returned pointer immediately becoming invalid
3231 * following the call.
3233 * Use of this function is discouraged.
3236 file_vnode_withvid(int fd
, struct vnode
**vpp
, uint32_t * vidp
)
3238 proc_t p
= current_proc();
3239 struct fileproc
*fp
;
3243 proc_fdlock_spin(p
);
3244 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
3248 if (fp
->f_type
!= DTYPE_VNODE
) {
3249 fp_drop(p
, fd
, fp
,1);
3253 vp
= (struct vnode
*)fp
->f_data
;
3257 if ((vidp
!= NULL
) && (vp
!= NULLVP
))
3258 *vidp
= (uint32_t)vp
->v_id
;
3269 * Description: Given an fd, look it up in the current process's per process
3270 * open file table, and return its internal socket pointer.
3272 * Parameters: fd fd to obtain vnode from
3273 * sp pointer to socket return area
3275 * Returns: 0 Success
3276 * ENOTSOCK Not a socket
3277 * fp_lookup:EBADF Bad file descriptor
3280 * *sp (modified) Returned socket pointer
3282 * Locks: This function internally takes and drops the proc_fdlock for
3283 * the current process
3285 * Notes: If successful, this function increments the f_iocount on the
3286 * fd's corresponding fileproc.
3288 * The fileproc referenced is not returned; because of this, care
3289 * must be taken to not drop the last reference (e.g. by closing
3290 * the file). This is inhernely unsafe, since the reference may
3291 * not be recoverable from the socket, if there is a subsequent
3292 * close that destroys the associate fileproc. The caller should
3293 * therefore retain their own reference on the fileproc so that
3294 * the f_iocount can be dropped subsequently. Failure to do this
3295 * can result in the returned pointer immediately becoming invalid
3296 * following the call.
3298 * Use of this function is discouraged.
3301 file_socket(int fd
, struct socket
**sp
)
3303 proc_t p
= current_proc();
3304 struct fileproc
*fp
;
3307 proc_fdlock_spin(p
);
3308 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
3312 if (fp
->f_type
!= DTYPE_SOCKET
) {
3313 fp_drop(p
, fd
, fp
,1);
3317 *sp
= (struct socket
*)fp
->f_data
;
3327 * Description: Given an fd, look it up in the current process's per process
3328 * open file table, and return its fileproc's flags field.
3330 * Parameters: fd fd whose flags are to be
3332 * flags pointer to flags data area
3334 * Returns: 0 Success
3335 * ENOTSOCK Not a socket
3336 * fp_lookup:EBADF Bad file descriptor
3339 * *flags (modified) Returned flags field
3341 * Locks: This function internally takes and drops the proc_fdlock for
3342 * the current process
3344 * Notes: This function will internally increment and decrement the
3345 * f_iocount of the fileproc as part of its operation.
3348 file_flags(int fd
, int *flags
)
3351 proc_t p
= current_proc();
3352 struct fileproc
*fp
;
3355 proc_fdlock_spin(p
);
3356 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
3360 *flags
= (int)fp
->f_flag
;
3361 fp_drop(p
, fd
, fp
,1);
3371 * Description: Drop an iocount reference on an fd, and wake up any waiters
3372 * for draining (i.e. blocked in fileproc_drain() called during
3373 * the last attempt to close a file).
3375 * Parameters: fd fd on which an ioreference is
3378 * Returns: 0 Success
3379 * EBADF Bad file descriptor
3381 * Description: Given an fd, look it up in the current process's per process
3382 * open file table, and drop it's fileproc's f_iocount by one
3384 * Notes: This is intended as a corresponding operation to the functions
3385 * file_vnode() and file_socket() operations.
3387 * Technically, the close reference is supposed to be protected
3388 * by a fileproc_drain(), however, a drain will only block if
3389 * the fd refers to a character device, and that device has had
3390 * preparefileread() called on it. If it refers to something
3391 * other than a character device, then the drain will occur and
3392 * block each close attempt, rather than merely the last close.
3394 * Since it's possible for an fd that refers to a character
3395 * device to have an intermediate close followed by an open to
3396 * cause a different file to correspond to that descriptor,
3397 * unless there was a cautionary reference taken on the fileproc,
3398 * this is an inherently unsafe function. This happens in the
3399 * case where multiple fd's in a process refer to the same
3400 * character device (e.g. stdin/out/err pointing to a tty, etc.).
3402 * Use of this function is discouraged.
3407 struct fileproc
*fp
;
3408 proc_t p
= current_proc();
3411 proc_fdlock_spin(p
);
3412 if (fd
< 0 || fd
>= p
->p_fd
->fd_nfiles
||
3413 (fp
= p
->p_fd
->fd_ofiles
[fd
]) == NULL
||
3414 ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
3415 !(p
->p_fd
->fd_ofileflags
[fd
] & UF_CLOSING
))) {
3421 if (p
->p_fpdrainwait
&& fp
->f_iocount
== 0) {
3422 p
->p_fpdrainwait
= 0;
3428 wakeup(&p
->p_fpdrainwait
);
3436 * Description: Allocate an entry in the per process open file table and
3437 * return the corresponding fileproc and fd.
3439 * Parameters: p The process in whose open file
3440 * table the fd is to be allocated
3441 * resultfp Pointer to fileproc pointer
3443 * resultfd Pointer to fd return area
3446 * Returns: 0 Success
3447 * falloc:ENFILE Too many open files in system
3448 * falloc:EMFILE Too many open files in process
3449 * falloc:ENOMEM M_FILEPROC or M_FILEGLOB zone
3453 * *resultfd (modified) Returned fileproc pointer
3454 * *resultfd (modified) Returned fd
3456 * Locks: This function takes and drops the proc_fdlock; if this lock
3457 * is alread held, use falloc_locked() instead.
3459 * Notes: This function takes separate process and context arguments
3460 * solely to support kern_exec.c; otherwise, it would take
3461 * neither, and expect falloc_locked() to use the
3462 * vfs_context_current() routine internally.
3465 falloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
)
3470 error
= falloc_locked(p
, resultfp
, resultfd
, ctx
, 1);
3480 * Create a new open file structure and allocate
3481 * a file decriptor for the process that refers to it.
3483 * Returns: 0 Success
3485 * Description: Allocate an entry in the per process open file table and
3486 * return the corresponding fileproc and fd.
3488 * Parameters: p The process in whose open file
3489 * table the fd is to be allocated
3490 * resultfp Pointer to fileproc pointer
3492 * resultfd Pointer to fd return area
3494 * locked Flag to indicate whether the
3495 * caller holds proc_fdlock
3497 * Returns: 0 Success
3498 * ENFILE Too many open files in system
3499 * fdalloc:EMFILE Too many open files in process
3500 * ENOMEM M_FILEPROC or M_FILEGLOB zone
3505 * *resultfd (modified) Returned fileproc pointer
3506 * *resultfd (modified) Returned fd
3508 * Locks: If the parameter 'locked' is zero, this function takes and
3509 * drops the proc_fdlock; if non-zero, the caller must hold the
3512 * Notes: If you intend to use a non-zero 'locked' parameter, use the
3513 * utility function falloc() instead.
3515 * This function takes separate process and context arguments
3516 * solely to support kern_exec.c; otherwise, it would take
3517 * neither, and use the vfs_context_current() routine internally.
3520 falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
3521 vfs_context_t ctx
, int locked
)
3523 struct fileproc
*fp
, *fq
;
3524 struct fileglob
*fg
;
3529 if ( (error
= fdalloc(p
, 0, &nfd
)) ) {
3534 if (nfiles
>= maxfiles
) {
3541 error
= mac_file_check_create(proc_ucred(p
));
3550 * Allocate a new file descriptor.
3551 * If the process has file descriptor zero open, add to the list
3552 * of open files at that point, otherwise put it at the front of
3553 * the list of open files.
3557 MALLOC_ZONE(fp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
3563 MALLOC_ZONE(fg
, struct fileglob
*, sizeof(struct fileglob
), M_FILEGLOB
, M_WAITOK
);
3565 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
3570 bzero(fp
, sizeof(struct fileproc
));
3571 bzero(fg
, sizeof(struct fileglob
));
3572 lck_mtx_init(&fg
->fg_lock
, file_lck_grp
, file_lck_attr
);
3578 mac_file_label_init(fg
);
3581 kauth_cred_ref(ctx
->vc_ucred
);
3585 fp
->f_cred
= ctx
->vc_ucred
;
3588 mac_file_label_associate(fp
->f_cred
, fg
);
3591 lck_mtx_lock_spin(file_flist_lock
);
3595 if ( (fq
= p
->p_fd
->fd_ofiles
[0]) ) {
3596 LIST_INSERT_AFTER(fq
->f_fglob
, fg
, f_list
);
3598 LIST_INSERT_HEAD(&filehead
, fg
, f_list
);
3600 lck_mtx_unlock(file_flist_lock
);
3602 p
->p_fd
->fd_ofiles
[nfd
] = fp
;
3619 * Description: Free a file structure; drop the global open file count, and
3620 * drop the credential reference, if the fileglob has one, and
3621 * destroy the instance mutex before freeing
3623 * Parameters: fg Pointer to fileglob to be
3629 fg_free(struct fileglob
*fg
)
3631 lck_mtx_lock_spin(file_flist_lock
);
3632 LIST_REMOVE(fg
, f_list
);
3634 lck_mtx_unlock(file_flist_lock
);
3636 if (IS_VALID_CRED(fg
->fg_cred
)) {
3637 kauth_cred_unref(&fg
->fg_cred
);
3639 lck_mtx_destroy(&fg
->fg_lock
, file_lck_grp
);
3642 mac_file_label_destroy(fg
);
3644 FREE_ZONE(fg
, sizeof *fg
, M_FILEGLOB
);
3651 * Description: Perform close-on-exec processing for all files in a process
3652 * that are either marked as close-on-exec, or which were in the
3653 * process of being opened at the time of the execve
3655 * Parameters: p Pointer to process calling
3660 * Locks: This function internally takes and drops proc_fdlock()
3662 * Notes: This function drops and retakes the kernel funnel; this is
3663 * inherently unsafe, since another thread may have the
3666 * XXX: We should likely reverse the lock and funnel drop/acquire
3667 * order to avoid the small race window; it's also possible that
3668 * if the program doing the exec has an outstanding listen socket
3669 * and a network connection is completed asyncrhonously that we
3670 * will end up with a "ghost" socket reference in the new process.
3672 * This needs reworking to make it safe to remove the funnel from
3673 * the execve and posix_spawn system calls.
3678 struct filedesc
*fdp
= p
->p_fd
;
3680 struct fileproc
*fp
;
3683 i
= fdp
->fd_lastfile
;
3687 fp
= fdp
->fd_ofiles
[i
];
3689 ((fdp
->fd_ofileflags
[i
] & (UF_RESERVED
|UF_EXCLOSE
)) == UF_EXCLOSE
)
3691 || (fp
&& mac_file_check_inherit(proc_ucred(p
), fp
->f_fglob
))
3694 if (i
< fdp
->fd_knlistsize
)
3695 knote_fdclose(p
, i
);
3696 procfdtbl_clearfd(p
, i
);
3697 if (i
== fdp
->fd_lastfile
&& i
> 0)
3699 if (i
< fdp
->fd_freefile
)
3700 fdp
->fd_freefile
= i
;
3701 closef_locked(fp
, fp
->f_fglob
, p
);
3702 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
3713 * Description: Copy a filedesc structure. This is normally used as part of
3714 * forkproc() when forking a new process, to copy the per process
3715 * open file table over to the new process.
3717 * Parameters: p Process whose open file table
3718 * is to be copied (parent)
3719 * uth_cdir Per thread current working
3720 * cirectory, or NULL
3722 * Returns: NULL Copy failed
3723 * !NULL Pointer to new struct filedesc
3725 * Locks: This function internally takes and drops proc_fdlock()
3727 * Notes: Files are copied directly, ignoring the new resource limits
3728 * for the process that's being copied into. Since the descriptor
3729 * references are just additional references, this does not count
3730 * against the number of open files on the system.
3732 * The struct filedesc includes the current working directory,
3733 * and the current root directory, if the process is chroot'ed.
3735 * If the exec was called by a thread using a per thread current
3736 * working directory, we inherit the working directory from the
3737 * thread making the call, rather than from the process.
3739 * In the case of a failure to obtain a reference, for most cases,
3740 * the file entry will be silently droppped. There's an exception
3741 * for the case of a chroot dir, since a failure to to obtain a
3742 * reference there would constitute an "escape" from the chroot
3743 * environment, which must not be allowed. In that case, we will
3744 * deny the execve() operation, rather than allowing the escape.
3747 fdcopy(proc_t p
, vnode_t uth_cdir
)
3749 struct filedesc
*newfdp
, *fdp
= p
->p_fd
;
3751 struct fileproc
*ofp
, *fp
;
3754 MALLOC_ZONE(newfdp
, struct filedesc
*,
3755 sizeof(*newfdp
), M_FILEDESC
, M_WAITOK
);
3762 * the FD_CHROOT flag will be inherited via this copy
3764 (void) memcpy(newfdp
, fdp
, sizeof(*newfdp
));
3767 * If we are running with per-thread current working directories,
3768 * inherit the new current working directory from the current thread
3769 * instead, before we take our references.
3771 if (uth_cdir
!= NULLVP
)
3772 newfdp
->fd_cdir
= uth_cdir
;
3775 * For both fd_cdir and fd_rdir make sure we get
3776 * a valid reference... if we can't, than set
3777 * set the pointer(s) to NULL in the child... this
3778 * will keep us from using a non-referenced vp
3779 * and allows us to do the vnode_rele only on
3780 * a properly referenced vp
3782 if ( (v_dir
= newfdp
->fd_cdir
) ) {
3783 if (vnode_getwithref(v_dir
) == 0) {
3784 if ( (vnode_ref(v_dir
)) )
3785 newfdp
->fd_cdir
= NULL
;
3788 newfdp
->fd_cdir
= NULL
;
3790 if (newfdp
->fd_cdir
== NULL
&& fdp
->fd_cdir
) {
3792 * we couldn't get a new reference on
3793 * the current working directory being
3794 * inherited... we might as well drop
3795 * our reference from the parent also
3796 * since the vnode has gone DEAD making
3797 * it useless... by dropping it we'll
3798 * be that much closer to recyling it
3800 vnode_rele(fdp
->fd_cdir
);
3801 fdp
->fd_cdir
= NULL
;
3804 if ( (v_dir
= newfdp
->fd_rdir
) ) {
3805 if (vnode_getwithref(v_dir
) == 0) {
3806 if ( (vnode_ref(v_dir
)) )
3807 newfdp
->fd_rdir
= NULL
;
3810 newfdp
->fd_rdir
= NULL
;
3813 /* Coming from a chroot environment and unable to get a reference... */
3814 if (newfdp
->fd_rdir
== NULL
&& fdp
->fd_rdir
) {
3816 * We couldn't get a new reference on
3817 * the chroot directory being
3818 * inherited... this is fatal, since
3819 * otherwise it would constitute an
3820 * escape from a chroot environment by
3823 if (newfdp
->fd_cdir
)
3824 vnode_rele(newfdp
->fd_cdir
);
3825 FREE_ZONE(newfdp
, sizeof *newfdp
, M_FILEDESC
);
3828 newfdp
->fd_refcnt
= 1;
3831 * If the number of open files fits in the internal arrays
3832 * of the open file structure, use them, otherwise allocate
3833 * additional memory for the number of descriptors currently
3836 if (newfdp
->fd_lastfile
< NDFILE
)
3840 * Compute the smallest multiple of NDEXTENT needed
3841 * for the file descriptors currently in use,
3842 * allowing the table to shrink.
3844 i
= newfdp
->fd_nfiles
;
3845 while (i
> 2 * NDEXTENT
&& i
> newfdp
->fd_lastfile
* 2)
3850 MALLOC_ZONE(newfdp
->fd_ofiles
, struct fileproc
**,
3851 i
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
3852 if (newfdp
->fd_ofiles
== NULL
) {
3853 if (newfdp
->fd_cdir
)
3854 vnode_rele(newfdp
->fd_cdir
);
3855 if (newfdp
->fd_rdir
)
3856 vnode_rele(newfdp
->fd_rdir
);
3858 FREE_ZONE(newfdp
, sizeof(*newfdp
), M_FILEDESC
);
3861 (void) memset(newfdp
->fd_ofiles
, 0, i
* OFILESIZE
);
3864 newfdp
->fd_ofileflags
= (char *) &newfdp
->fd_ofiles
[i
];
3865 newfdp
->fd_nfiles
= i
;
3867 if (fdp
->fd_nfiles
> 0) {
3868 struct fileproc
**fpp
;
3871 (void) memcpy(newfdp
->fd_ofiles
, fdp
->fd_ofiles
,
3872 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofiles
));
3873 (void) memcpy(newfdp
->fd_ofileflags
, fdp
->fd_ofileflags
,
3874 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofileflags
));
3877 * kq descriptors cannot be copied.
3879 if (newfdp
->fd_knlistsize
!= -1) {
3880 fpp
= &newfdp
->fd_ofiles
[newfdp
->fd_lastfile
];
3881 for (i
= newfdp
->fd_lastfile
; i
>= 0; i
--, fpp
--) {
3882 if (*fpp
!= NULL
&& (*fpp
)->f_type
== DTYPE_KQUEUE
) {
3884 newfdp
->fd_ofileflags
[i
] = 0;
3885 if (i
< newfdp
->fd_freefile
)
3886 newfdp
->fd_freefile
= i
;
3888 if (*fpp
== NULL
&& i
== newfdp
->fd_lastfile
&& i
> 0)
3889 newfdp
->fd_lastfile
--;
3891 newfdp
->fd_knlist
= NULL
;
3892 newfdp
->fd_knlistsize
= -1;
3893 newfdp
->fd_knhash
= NULL
;
3894 newfdp
->fd_knhashmask
= 0;
3896 fpp
= newfdp
->fd_ofiles
;
3897 flags
= newfdp
->fd_ofileflags
;
3899 for (i
= newfdp
->fd_lastfile
+ 1; --i
>= 0; fpp
++, flags
++)
3900 if ((ofp
= *fpp
) != NULL
&& !(*flags
& UF_RESERVED
)) {
3901 MALLOC_ZONE(fp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
3904 * XXX no room to copy, unable to
3905 * XXX safely unwind state at present
3909 bzero(fp
, sizeof(struct fileproc
));
3910 fp
->f_flags
= ofp
->f_flags
;
3911 //fp->f_iocount = ofp->f_iocount;
3913 fp
->f_fglob
= ofp
->f_fglob
;
3918 if (i
< newfdp
->fd_freefile
)
3919 newfdp
->fd_freefile
= i
;
3933 * Description: Release a filedesc (per process open file table) structure;
3934 * this is done on process exit(), or from forkproc_free() if
3935 * the fork fails for some reason subsequent to a successful
3938 * Parameters: p Pointer to process going away
3942 * Locks: This function internally takes and drops proc_fdlock()
3947 struct filedesc
*fdp
;
3948 struct fileproc
*fp
;
3953 /* Certain daemons might not have file descriptors */
3956 if ((fdp
== NULL
) || (--fdp
->fd_refcnt
> 0)) {
3960 if (fdp
->fd_refcnt
== 0xffff)
3961 panic("fdfree: bad fd_refcnt");
3963 /* Last reference: the structure can't change out from under us */
3965 if (fdp
->fd_nfiles
> 0 && fdp
->fd_ofiles
) {
3966 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
3967 if ((fp
= fdp
->fd_ofiles
[i
]) != NULL
) {
3969 if (fdp
->fd_ofileflags
[i
] & UF_RESERVED
)
3970 panic("fdfree: found fp with UF_RESERVED\n");
3972 /* closef drops the iocount ... */
3973 if ((fp
->f_flags
& FP_INCHRREAD
) != 0)
3975 procfdtbl_reservefd(p
, i
);
3977 if (i
< fdp
->fd_knlistsize
)
3978 knote_fdclose(p
, i
);
3979 if (fp
->f_flags
& FP_WAITEVENT
)
3980 (void)waitevent_close(p
, fp
);
3981 (void) closef_locked(fp
, fp
->f_fglob
, p
);
3982 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
3985 FREE_ZONE(fdp
->fd_ofiles
, fdp
->fd_nfiles
* OFILESIZE
, M_OFILETABL
);
3986 fdp
->fd_ofiles
= NULL
;
3993 vnode_rele(fdp
->fd_cdir
);
3995 vnode_rele(fdp
->fd_rdir
);
3997 proc_fdlock_spin(p
);
4002 FREE(fdp
->fd_knlist
, M_KQUEUE
);
4004 FREE(fdp
->fd_knhash
, M_KQUEUE
);
4006 FREE_ZONE(fdp
, sizeof(*fdp
), M_FILEDESC
);
4013 * Description: Called on last open instance for a fileglob for a file being
4016 * Parameters: fp Pointer to fileproc for fd
4017 * fg Pointer to fileglob for fd
4018 * p Pointer to proc structure
4020 * Returns: 0 Success
4021 * <fo_close>:??? Anything returnable by a per-fileops
4024 * Note: fp can only be non-NULL if p is also non-NULL. If p is NULL,
4025 * then fg must eith be locked (FHASLOCK) or must not have a
4026 * type of DTYPE_VNODE.
4028 * On return, the fg is freed.
4030 * This function may block draining output to a character
4031 * device on last close of that device.
4034 closef_finish(struct fileproc
*fp
, struct fileglob
*fg
, proc_t p
, vfs_context_t ctx
)
4039 /* fg_ops completed initialization? */
4041 error
= fo_close(fg
, ctx
);
4045 /* if fp is non-NULL, drain it out */
4046 if (((fp
!= (struct fileproc
*)0) && ((fp
->f_flags
& FP_INCHRREAD
) != 0))) {
4047 proc_fdlock_spin(p
);
4048 if ( ((fp
->f_flags
& FP_INCHRREAD
) != 0) ) {
4049 fileproc_drain(p
, fp
);
4061 * Description: Internal form of closef; called with proc_fdlock held
4063 * Parameters: fp Pointer to fileproc for fd
4064 * fg Pointer to fileglob for fd
4065 * p Pointer to proc structure
4067 * Returns: 0 Success
4068 * closef_finish:??? Anything returnable by a per-fileops
4071 * Note: Decrements reference count on file structure; if this was the
4072 * last reference, then closef_finish() is called
4074 * p and fp are allowed to be NULL when closing a file that was
4075 * being passed in a message (but only if we are called when this
4076 * is NOT the last reference).
4079 closef_locked(struct fileproc
*fp
, struct fileglob
*fg
, proc_t p
)
4083 struct vfs_context context
;
4090 /* Set up context with cred stashed in fg */
4091 if (p
== current_proc())
4092 context
.vc_thread
= current_thread();
4094 context
.vc_thread
= NULL
;
4095 context
.vc_ucred
= fg
->fg_cred
;
4098 * POSIX record locking dictates that any close releases ALL
4099 * locks owned by this process. This is handled by setting
4100 * a flag in the unlock to free ONLY locks obeying POSIX
4101 * semantics, and not to free BSD-style file locks.
4102 * If the descriptor was in a message, POSIX-style locks
4103 * aren't passed with the descriptor.
4105 if (p
&& (p
->p_ladvflag
& P_LADVLOCK
) && fg
->fg_type
== DTYPE_VNODE
) {
4108 lf
.l_whence
= SEEK_SET
;
4111 lf
.l_type
= F_UNLCK
;
4112 vp
= (struct vnode
*)fg
->fg_data
;
4114 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
4115 (void) VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &lf
, F_POSIX
, &context
);
4116 (void)vnode_put(vp
);
4120 lck_mtx_lock_spin(&fg
->fg_lock
);
4123 if (fg
->fg_count
> 0) {
4124 lck_mtx_unlock(&fg
->fg_lock
);
4128 if (fg
->fg_count
!= 0)
4129 panic("fg %p: being freed with bad fg_count (%d)", fg
, fg
->fg_count
);
4132 if (fp
&& (fp
->f_flags
& FP_WRITTEN
))
4133 fg
->fg_flag
|= FWASWRITTEN
;
4135 fg
->fg_lflags
|= FG_TERM
;
4136 lck_mtx_unlock(&fg
->fg_lock
);
4140 error
= closef_finish(fp
, fg
, p
, &context
);
4151 * Description: Drain out pending I/O operations
4153 * Parameters: p Process closing this file
4154 * fp fileproc struct for the open
4155 * instance on the file
4159 * Locks: Assumes the caller holds the proc_fdlock
4161 * Notes: For character devices, this occurs on the last close of the
4162 * device; for all other file descriptos, this occurs on each
4163 * close to prevent fd's from being closed out from under
4164 * operations currently in progress and blocked
4166 * See Also: file_vnode(), file_socket(), file_drop(), and the cautions
4167 * regarding their use and interaction with this function.
4170 fileproc_drain(proc_t p
, struct fileproc
* fp
)
4172 struct vfs_context context
;
4174 context
.vc_thread
= proc_thread(p
); /* XXX */
4175 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
4177 fp
->f_iocount
-- ; /* (the one the close holds) */
4179 while (fp
->f_iocount
) {
4181 lck_mtx_convert_spin(&p
->p_fdmlock
);
4183 if (fp
->f_fglob
->fg_ops
->fo_drain
) {
4184 (*fp
->f_fglob
->fg_ops
->fo_drain
)(fp
, &context
);
4186 if (((fp
->f_flags
& FP_INSELECT
)== FP_INSELECT
)) {
4187 wait_queue_wakeup_all((wait_queue_t
)fp
->f_waddr
, NULL
, THREAD_INTERRUPTED
);
4189 p
->p_fpdrainwait
= 1;
4191 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
, "fpdrain", NULL
);
4200 * Description: Release the fd and free the fileproc associated with the fd
4201 * in the per process open file table of the specified process;
4202 * these values must correspond.
4204 * Parameters: p Process containing fd
4205 * fd fd to be released
4206 * fp fileproc to be freed
4208 * Returns: 0 Success
4210 * Notes: XXX function should be void - no one interprets the returns
4214 fp_free(proc_t p
, int fd
, struct fileproc
* fp
)
4216 proc_fdlock_spin(p
);
4220 fg_free(fp
->f_fglob
);
4221 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
4229 * Description: Apply an advisory lock on a file descriptor.
4231 * Parameters: p Process making request
4232 * uap->fd fd on which the lock is to be
4234 * uap->how (Un)Lock bits, including type
4235 * retval Pointer to the call return area
4237 * Returns: 0 Success
4238 * fp_getfvp:EBADF Bad file descriptor
4239 * fp_getfvp:ENOTSUP fd does not refer to a vnode
4240 * vnode_getwithref:???
4244 * *retval (modified) Size of dtable
4246 * Notes: Just attempt to get a record lock of the requested type on
4247 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
4250 flock(proc_t p
, struct flock_args
*uap
, __unused
int32_t *retval
)
4254 struct fileproc
*fp
;
4257 vfs_context_t ctx
= vfs_context_current();
4260 AUDIT_ARG(fd
, uap
->fd
);
4261 if ( (error
= fp_getfvp(p
, fd
, &fp
, &vp
)) ) {
4264 if ( (error
= vnode_getwithref(vp
)) ) {
4267 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
4269 lf
.l_whence
= SEEK_SET
;
4272 if (how
& LOCK_UN
) {
4273 lf
.l_type
= F_UNLCK
;
4274 fp
->f_flag
&= ~FHASLOCK
;
4275 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_UNLCK
, &lf
, F_FLOCK
, ctx
);
4279 lf
.l_type
= F_WRLCK
;
4280 else if (how
& LOCK_SH
)
4281 lf
.l_type
= F_RDLCK
;
4287 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
, F_SETLK
, &lf
);
4291 fp
->f_flag
|= FHASLOCK
;
4292 if (how
& LOCK_NB
) {
4293 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, F_FLOCK
, ctx
);
4296 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, F_FLOCK
|F_WAIT
, ctx
);
4298 (void)vnode_put(vp
);
4300 fp_drop(p
, fd
, fp
, 0);
4308 * Description: Duplicate the specified descriptor to a free descriptor;
4309 * this is the second half of fdopen(), above.
4311 * Parameters: fdp filedesc pointer to fill in
4313 * dfd fd to dup from
4314 * mode mode to set on new fd
4315 * error command code
4317 * Returns: 0 Success
4318 * EBADF Source fd is bad
4319 * EACCES Requested mode not allowed
4320 * !0 'error', if not ENODEV or
4323 * Notes: XXX This is not thread safe; see fdopen() above
4326 dupfdopen(struct filedesc
*fdp
, int indx
, int dfd
, int mode
, int error
)
4328 struct fileproc
*wfp
;
4329 struct fileproc
*fp
;
4333 proc_t p
= current_proc();
4336 * If the to-be-dup'd fd number is greater than the allowed number
4337 * of file descriptors, or the fd to be dup'd has already been
4338 * closed, reject. Note, check for new == old is necessary as
4339 * falloc could allocate an already closed to-be-dup'd descriptor
4340 * as the new descriptor.
4344 fp
= fdp
->fd_ofiles
[indx
];
4345 if (dfd
< 0 || dfd
>= fdp
->fd_nfiles
||
4346 (wfp
= fdp
->fd_ofiles
[dfd
]) == NULL
|| wfp
== fp
||
4347 (fdp
->fd_ofileflags
[dfd
] & UF_RESERVED
)) {
4353 myerror
= mac_file_check_dup(proc_ucred(p
), wfp
->f_fglob
, dfd
);
4360 * There are two cases of interest here.
4362 * For ENODEV simply dup (dfd) to file descriptor
4363 * (indx) and return.
4365 * For ENXIO steal away the file structure from (dfd) and
4366 * store it in (indx). (dfd) is effectively closed by
4369 * Any other error code is just returned.
4374 * Check that the mode the file is being opened for is a
4375 * subset of the mode of the existing descriptor.
4377 if (((mode
& (FREAD
|FWRITE
)) | wfp
->f_flag
) != wfp
->f_flag
) {
4381 if (indx
> fdp
->fd_lastfile
)
4382 fdp
->fd_lastfile
= indx
;
4386 fg_free(fp
->f_fglob
);
4387 fp
->f_fglob
= wfp
->f_fglob
;
4389 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
];
4405 * Description: Add a reference to a fileglob by fileproc
4407 * Parameters: fp fileproc containing fileglob
4412 * Notes: XXX Should use OSAddAtomic?
4415 fg_ref(struct fileproc
* fp
)
4417 struct fileglob
*fg
;
4421 lck_mtx_lock_spin(&fg
->fg_lock
);
4424 if ((fp
->f_flags
& ~((unsigned int)FP_VALID_FLAGS
)) != 0)
4425 panic("fg_ref: invalid bits on fp%x\n", (unsigned int)fp
);
4427 if (fg
->fg_count
== 0)
4428 panic("fg_ref: adding fgcount to zeroed fg :fp %x, fg%x\n ", (unsigned int)fp
, (unsigned int)fg
);
4431 lck_mtx_unlock(&fg
->fg_lock
);
4438 * Description: Remove a reference to a fileglob by fileproc
4440 * Parameters: fp fileproc containing fileglob
4445 * Notes: XXX Should use OSAddAtomic?
4448 fg_drop(struct fileproc
* fp
)
4450 struct fileglob
*fg
;
4453 lck_mtx_lock_spin(&fg
->fg_lock
);
4455 lck_mtx_unlock(&fg
->fg_lock
);
4462 * Description: Insert fileglob onto message queue
4464 * Parameters: fg Fileglob pointer to insert
4468 * Locks: Takes and drops fg_lock, potentially many times
4471 fg_insertuipc(struct fileglob
* fg
)
4475 lck_mtx_lock_spin(&fg
->fg_lock
);
4477 while (fg
->fg_lflags
& FG_RMMSGQ
) {
4478 lck_mtx_convert_spin(&fg
->fg_lock
);
4480 fg
->fg_lflags
|= FG_WRMMSGQ
;
4481 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_insertuipc", NULL
);
4486 if (fg
->fg_msgcount
== 1) {
4487 fg
->fg_lflags
|= FG_INSMSGQ
;
4490 lck_mtx_unlock(&fg
->fg_lock
);
4493 lck_mtx_lock_spin(uipc_lock
);
4495 LIST_INSERT_HEAD(&fmsghead
, fg
, f_msglist
);
4496 lck_mtx_unlock(uipc_lock
);
4497 lck_mtx_lock(&fg
->fg_lock
);
4498 fg
->fg_lflags
&= ~FG_INSMSGQ
;
4499 if (fg
->fg_lflags
& FG_WINSMSGQ
) {
4500 fg
->fg_lflags
&= ~FG_WINSMSGQ
;
4501 wakeup(&fg
->fg_lflags
);
4503 lck_mtx_unlock(&fg
->fg_lock
);
4512 * Description: Remove fileglob from message queue
4514 * Parameters: fg Fileglob pointer to remove
4518 * Locks: Takes and drops fg_lock, potentially many times
4521 fg_removeuipc(struct fileglob
* fg
)
4525 lck_mtx_lock_spin(&fg
->fg_lock
);
4526 while (fg
->fg_lflags
& FG_INSMSGQ
) {
4527 lck_mtx_convert_spin(&fg
->fg_lock
);
4529 fg
->fg_lflags
|= FG_WINSMSGQ
;
4530 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_removeuipc", NULL
);
4533 if (fg
->fg_msgcount
== 0) {
4534 fg
->fg_lflags
|= FG_RMMSGQ
;
4537 lck_mtx_unlock(&fg
->fg_lock
);
4540 lck_mtx_lock_spin(uipc_lock
);
4542 LIST_REMOVE(fg
, f_msglist
);
4543 lck_mtx_unlock(uipc_lock
);
4544 lck_mtx_lock(&fg
->fg_lock
);
4545 fg
->fg_lflags
&= ~FG_RMMSGQ
;
4546 if (fg
->fg_lflags
& FG_WRMMSGQ
) {
4547 fg
->fg_lflags
&= ~FG_WRMMSGQ
;
4548 wakeup(&fg
->fg_lflags
);
4550 lck_mtx_unlock(&fg
->fg_lock
);
4558 * Description: Generic fileops read indirected through the fileops pointer
4559 * in the fileproc structure
4561 * Parameters: fp fileproc structure pointer
4562 * uio user I/O structure pointer
4564 * ctx VFS context for operation
4566 * Returns: 0 Success
4567 * !0 Errno from read
4570 fo_read(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
4572 return ((*fp
->f_ops
->fo_read
)(fp
, uio
, flags
, ctx
));
4579 * Description: Generic fileops write indirected through the fileops pointer
4580 * in the fileproc structure
4582 * Parameters: fp fileproc structure pointer
4583 * uio user I/O structure pointer
4585 * ctx VFS context for operation
4587 * Returns: 0 Success
4588 * !0 Errno from write
4591 fo_write(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
4593 return((*fp
->f_ops
->fo_write
)(fp
, uio
, flags
, ctx
));
4600 * Description: Generic fileops ioctl indirected through the fileops pointer
4601 * in the fileproc structure
4603 * Parameters: fp fileproc structure pointer
4605 * data pointer to internalized copy
4606 * of user space ioctl command
4607 * parameter data in kernel space
4608 * ctx VFS context for operation
4610 * Returns: 0 Success
4611 * !0 Errno from ioctl
4613 * Locks: The caller is assumed to have held the proc_fdlock; this
4614 * function releases and reacquires this lock. If the caller
4615 * accesses data protected by this lock prior to calling this
4616 * function, it will need to revalidate/reacquire any cached
4617 * protected data obtained prior to the call.
4620 fo_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
, vfs_context_t ctx
)
4624 proc_fdunlock(vfs_context_proc(ctx
));
4625 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, com
, data
, ctx
);
4626 proc_fdlock(vfs_context_proc(ctx
));
4634 * Description: Generic fileops select indirected through the fileops pointer
4635 * in the fileproc structure
4637 * Parameters: fp fileproc structure pointer
4638 * which select which
4639 * wql pointer to wait queue list
4640 * ctx VFS context for operation
4642 * Returns: 0 Success
4643 * !0 Errno from select
4646 fo_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
4648 return((*fp
->f_ops
->fo_select
)(fp
, which
, wql
, ctx
));
4655 * Description: Generic fileops close indirected through the fileops pointer
4656 * in the fileproc structure
4658 * Parameters: fp fileproc structure pointer for
4660 * ctx VFS context for operation
4662 * Returns: 0 Success
4663 * !0 Errno from close
4666 fo_close(struct fileglob
*fg
, vfs_context_t ctx
)
4668 return((*fg
->fg_ops
->fo_close
)(fg
, ctx
));
4675 * Description: Generic fileops kqueue filter indirected through the fileops
4676 * pointer in the fileproc structure
4678 * Parameters: fp fileproc structure pointer
4679 * kn pointer to knote to filter on
4680 * ctx VFS context for operation
4682 * Returns: 0 Success
4683 * !0 Errno from kqueue filter
4686 fo_kqfilter(struct fileproc
*fp
, struct knote
*kn
, vfs_context_t ctx
)
4688 return ((*fp
->f_ops
->fo_kqfilter
)(fp
, kn
, ctx
));
4692 * The ability to send a file descriptor to another
4693 * process is opt-in by file type.
4696 filetype_issendable(file_type_t fdtype
)
4705 /* DTYPE_KQUEUE, DTYPE_FSEVENTS, DTYPE_PSXSEM */