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>
97 #include <bsm/audit_kernel.h>
99 #include <sys/mount_internal.h>
100 #include <sys/kdebug.h>
101 #include <sys/sysproto.h>
102 #include <sys/pipe.h>
103 #include <kern/kern_types.h>
104 #include <kern/kalloc.h>
105 #include <libkern/OSAtomic.h>
107 #include <sys/ubc_internal.h>
112 int fdopen(dev_t dev
, int mode
, int type
, proc_t p
);
113 int finishdup(proc_t p
, struct filedesc
*fdp
, int old
, int new, register_t
*retval
);
115 int falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
, int locked
);
116 int fdgetf_noref(proc_t p
, int fd
, struct fileproc
**resultfp
);
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
*, register_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
, register_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
))
359 * Description: Duplicate a file descriptor.
361 * Parameters: p Process performing the dup
362 * uap->fd The fd to dup
363 * retval Pointer to the call return area
369 * *retval (modified) The new descriptor
372 dup(proc_t p
, struct dup_args
*uap
, register_t
*retval
)
374 struct filedesc
*fdp
= p
->p_fd
;
380 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
384 if ( (error
= fdalloc(p
, 0, &new)) ) {
385 fp_drop(p
, old
, fp
, 1);
389 error
= finishdup(p
, fdp
, old
, new, retval
);
390 fp_drop(p
, old
, fp
, 1);
400 * Description: Duplicate a file descriptor to a particular value.
402 * Parameters: p Process performing the dup
403 * uap->fd The fd to dup
404 * uap->to The fd to dup it to
405 * retval Pointer to the call return area
411 * *retval (modified) The new descriptor
414 dup2(proc_t p
, struct dup2_args
*uap
, register_t
*retval
)
416 struct filedesc
*fdp
= p
->p_fd
;
417 int old
= uap
->from
, new = uap
->to
;
419 struct fileproc
*fp
, *nfp
;
424 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
429 (rlim_t
)new >= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
431 fp_drop(p
, old
, fp
, 1);
436 fp_drop(p
, old
, fp
, 1);
441 if (new < 0 || new >= fdp
->fd_nfiles
) {
442 if ( (error
= fdalloc(p
, new, &i
)) ) {
443 fp_drop(p
, old
, fp
, 1);
453 while ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == UF_RESERVED
) {
454 fp_drop(p
, old
, fp
, 1);
455 procfdtbl_waitfd(p
, new);
457 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
462 if ((fdp
->fd_ofiles
[new] != NULL
) && ((error
= fp_lookup(p
, new, &nfp
, 1)) == 0)) {
463 fp_drop(p
, old
, fp
, 1);
464 (void)close_internal_locked(p
, new, nfp
, FD_DUP2RESV
);
466 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
468 procfdtbl_clearfd(p
, new);
472 if (fdp
->fd_ofiles
[new] != NULL
)
473 panic("dup2: unable to get ref on a fileproc %d\n", new);
475 procfdtbl_reservefd(p
, new);
479 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
484 if (fdp
->fd_ofiles
[new] != 0)
485 panic("dup2-1: overwriting fd_ofiles with new %d\n", new);
486 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0)
487 panic("dup2-1: unreserved fileflags with new %d\n", new);
489 error
= finishdup(p
, fdp
, old
, new, retval
);
490 fp_drop(p
, old
, fp
, 1);
500 * Description: The file control system call.
502 * Parameters: p Process performing the fcntl
503 * uap->fd The fd to operate against
504 * uap->cmd The command to perform
505 * uap->arg Pointer to the command argument
506 * retval Pointer to the call return area
509 * !0 Errno (see fcntl_nocancel)
512 * *retval (modified) fcntl return value (if any)
514 * Notes: This system call differs from fcntl_nocancel() in that it
515 * tests for cancellation prior to performing a potentially
516 * blocking operation.
519 fcntl(proc_t p
, struct fcntl_args
*uap
, register_t
*retval
)
521 __pthread_testcancel(1);
522 return(fcntl_nocancel(p
, (struct fcntl_nocancel_args
*)uap
, retval
));
529 * Description: A non-cancel-testing file control system call.
531 * Parameters: p Process performing the fcntl
532 * uap->fd The fd to operate against
533 * uap->cmd The command to perform
534 * uap->arg Pointer to the command argument
535 * retval Pointer to the call return area
539 * fp_lookup:EBADF Bad file descriptor
551 * vnode_getwithref:???
558 * vnode_getwithref:???
565 * vnode_getwithref:???
567 * [F_SETSIZE,F_RDADVISE]
570 * vnode_getwithref:???
571 * [F_RDAHEAD,F_NOCACHE]
573 * vnode_getwithref:???
577 * *retval (modified) fcntl return value (if any)
580 fcntl_nocancel(proc_t p
, struct fcntl_nocancel_args
*uap
, register_t
*retval
)
583 struct filedesc
*fdp
= p
->p_fd
;
586 struct vnode
*vp
= NULLVP
; /* for AUDIT_ARG() at end */
587 int i
, tmp
, error
, error2
, flg
= F_POSIX
;
589 struct vfs_context context
;
593 int devBlockSize
= 0;
597 AUDIT_ARG(fd
, uap
->fd
);
598 AUDIT_ARG(cmd
, uap
->cmd
);
601 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
605 context
.vc_thread
= current_thread();
606 context
.vc_ucred
= fp
->f_cred
;
607 if (proc_is64bit(p
)) {
612 * Since the arg parameter is defined as a long but may be
613 * either a long or a pointer we must take care to handle
614 * sign extension issues. Our sys call munger will sign
615 * extend a long when we are called from a 32-bit process.
616 * Since we can never have an address greater than 32-bits
617 * from a 32-bit process we lop off the top 32-bits to avoid
618 * getting the wrong address
620 argp
= CAST_USER_ADDR_T(uap
->arg
);
623 pop
= &fdp
->fd_ofileflags
[fd
];
626 error
= mac_file_check_fcntl(proc_ucred(p
), fp
->f_fglob
, uap
->cmd
,
635 newmin
= CAST_DOWN(int, uap
->arg
);
636 if ((u_int
)newmin
>= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
637 newmin
>= maxfiles
) {
641 if ( (error
= fdalloc(p
, newmin
, &i
)) )
643 error
= finishdup(p
, fdp
, fd
, i
, retval
);
647 *retval
= (*pop
& UF_EXCLOSE
)? 1 : 0;
652 *pop
= (*pop
&~ UF_EXCLOSE
) |
653 (uap
->arg
& 1)? UF_EXCLOSE
: 0;
658 *retval
= OFLAGS(fp
->f_flag
);
663 fp
->f_flag
&= ~FCNTLFLAGS
;
664 tmp
= CAST_DOWN(int, uap
->arg
);
665 fp
->f_flag
|= FFLAGS(tmp
) & FCNTLFLAGS
;
666 tmp
= fp
->f_flag
& FNONBLOCK
;
667 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
670 tmp
= fp
->f_flag
& FASYNC
;
671 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, &context
);
674 fp
->f_flag
&= ~FNONBLOCK
;
676 (void)fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
680 if (fp
->f_type
== DTYPE_SOCKET
) {
681 *retval
= ((struct socket
*)fp
->f_data
)->so_pgid
;
685 error
= fo_ioctl(fp
, (int)TIOCGPGRP
, (caddr_t
)retval
, &context
);
690 tmp
= CAST_DOWN(pid_t
, uap
->arg
);
691 if (fp
->f_type
== DTYPE_SOCKET
) {
692 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
696 if (fp
->f_type
== DTYPE_PIPE
) {
697 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
704 proc_t p1
= proc_find(tmp
);
709 tmp
= (int)p1
->p_pgrpid
;
712 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
717 /* Fall into F_SETLK */
720 if (fp
->f_type
!= DTYPE_VNODE
) {
724 vp
= (struct vnode
*)fp
->f_data
;
727 offset
= fp
->f_offset
;
730 /* Copy in the lock structure */
731 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
736 if ((fl
.l_whence
== SEEK_CUR
) && (fl
.l_start
+ offset
< fl
.l_start
)) {
741 if ( (error
= vnode_getwithref(vp
)) ) {
744 if (fl
.l_whence
== SEEK_CUR
)
745 fl
.l_start
+= offset
;
748 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
758 if ((fflag
& FREAD
) == 0) {
763 // XXX UInt32 unsafe for LP64 kernel
764 OSBitOrAtomic(P_LADVLOCK
, (UInt32
*)&p
->p_ladvflag
);
765 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_SETLK
, &fl
, flg
, &context
);
770 if ((fflag
& FWRITE
) == 0) {
775 // XXX UInt32 unsafe for LP64 kernel
776 OSBitOrAtomic(P_LADVLOCK
, (UInt32
*)&p
->p_ladvflag
);
777 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_SETLK
, &fl
, flg
, &context
);
782 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &fl
,
794 if (fp
->f_type
!= DTYPE_VNODE
) {
798 vp
= (struct vnode
*)fp
->f_data
;
800 offset
= fp
->f_offset
;
803 /* Copy in the lock structure */
804 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
808 /* Check starting byte and ending byte for EOVERFLOW in SEEK_CUR */
809 /* and ending byte for EOVERFLOW in SEEK_SET */
810 if (((fl
.l_whence
== SEEK_CUR
) &&
811 ((fl
.l_start
+ offset
< fl
.l_start
) ||
812 ((fl
.l_len
> 0) && (fl
.l_start
+offset
+ fl
.l_len
- 1 < fl
.l_start
+offset
)))) ||
813 ((fl
.l_whence
== SEEK_SET
) && (fl
.l_len
> 0) && (fl
.l_start
+ fl
.l_len
- 1 < fl
.l_start
)))
815 /* lf_advlock doesn't check start/end for F_GETLK if file has no locks */
820 if ((fl
.l_whence
== SEEK_SET
) && (fl
.l_start
< 0)) {
835 switch (fl
.l_whence
) {
845 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
846 if (fl
.l_whence
== SEEK_CUR
)
847 fl
.l_start
+= offset
;
850 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
854 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_GETLK
, &fl
, F_POSIX
, &context
);
859 error
= copyout((caddr_t
)&fl
, argp
, sizeof(fl
));
863 case F_PREALLOCATE
: {
864 fstore_t alloc_struct
; /* structure for allocate command */
865 u_int32_t alloc_flags
= 0;
867 if (fp
->f_type
!= DTYPE_VNODE
) {
872 vp
= (struct vnode
*)fp
->f_data
;
875 /* make sure that we have write permission */
876 if ((fp
->f_flag
& FWRITE
) == 0) {
881 error
= copyin(argp
, (caddr_t
)&alloc_struct
, sizeof(alloc_struct
));
885 /* now set the space allocated to 0 */
886 alloc_struct
.fst_bytesalloc
= 0;
889 * Do some simple parameter checking
892 /* set up the flags */
894 alloc_flags
|= PREALLOCATE
;
896 if (alloc_struct
.fst_flags
& F_ALLOCATECONTIG
)
897 alloc_flags
|= ALLOCATECONTIG
;
899 if (alloc_struct
.fst_flags
& F_ALLOCATEALL
)
900 alloc_flags
|= ALLOCATEALL
;
903 * Do any position mode specific stuff. The only
904 * position mode supported now is PEOFPOSMODE
907 switch (alloc_struct
.fst_posmode
) {
910 if (alloc_struct
.fst_offset
!= 0) {
915 alloc_flags
|= ALLOCATEFROMPEOF
;
919 if (alloc_struct
.fst_offset
<= 0) {
924 alloc_flags
|= ALLOCATEFROMVOL
;
932 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
934 * call allocate to get the space
936 error
= VNOP_ALLOCATE(vp
,alloc_struct
.fst_length
,alloc_flags
,
937 &alloc_struct
.fst_bytesalloc
, alloc_struct
.fst_offset
,
941 error2
= copyout((caddr_t
)&alloc_struct
, argp
, sizeof(alloc_struct
));
950 if (fp
->f_type
!= DTYPE_VNODE
) {
954 vp
= (struct vnode
*)fp
->f_data
;
957 error
= copyin(argp
, (caddr_t
)&offset
, sizeof (off_t
));
961 error
= vnode_getwithref(vp
);
966 error
= mac_vnode_check_truncate(&context
,
967 fp
->f_fglob
->fg_cred
, vp
);
974 * Make sure that we are root. Growing a file
975 * without zero filling the data is a security hole
976 * root would have access anyway so we'll allow it
984 error
= vnode_setsize(vp
, offset
, IO_NOZEROFILL
,
992 if (fp
->f_type
!= DTYPE_VNODE
) {
997 fp
->f_fglob
->fg_flag
&= ~FNORDAHEAD
;
999 fp
->f_fglob
->fg_flag
|= FNORDAHEAD
;
1004 if (fp
->f_type
!= DTYPE_VNODE
) {
1009 fp
->f_fglob
->fg_flag
|= FNOCACHE
;
1011 fp
->f_fglob
->fg_flag
&= ~FNOCACHE
;
1015 case F_GLOBAL_NOCACHE
:
1016 if (fp
->f_type
!= DTYPE_VNODE
) {
1020 vp
= (struct vnode
*)fp
->f_data
;
1023 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1025 *retval
= vnode_isnocache(vp
);
1028 vnode_setnocache(vp
);
1030 vnode_clearnocache(vp
);
1032 (void)vnode_put(vp
);
1036 case F_CHECK_OPENEVT
:
1037 if (fp
->f_type
!= DTYPE_VNODE
) {
1041 vp
= (struct vnode
*)fp
->f_data
;
1044 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1046 *retval
= vnode_is_openevt(vp
);
1049 vnode_set_openevt(vp
);
1051 vnode_clear_openevt(vp
);
1053 (void)vnode_put(vp
);
1058 struct radvisory ra_struct
;
1060 if (fp
->f_type
!= DTYPE_VNODE
) {
1064 vp
= (struct vnode
*)fp
->f_data
;
1067 if ( (error
= copyin(argp
, (caddr_t
)&ra_struct
, sizeof(ra_struct
))) )
1069 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1070 error
= VNOP_IOCTL(vp
, F_RDADVISE
, (caddr_t
)&ra_struct
, 0, &context
);
1072 (void)vnode_put(vp
);
1077 case F_READBOOTSTRAP
:
1078 case F_WRITEBOOTSTRAP
: {
1079 fbootstraptransfer_t fbt_struct
;
1080 user_fbootstraptransfer_t user_fbt_struct
;
1082 caddr_t boot_structp
;
1084 if (fp
->f_type
!= DTYPE_VNODE
) {
1088 vp
= (struct vnode
*)fp
->f_data
;
1091 if (IS_64BIT_PROCESS(p
)) {
1092 sizeof_struct
= sizeof(user_fbt_struct
);
1093 boot_structp
= (caddr_t
) &user_fbt_struct
;
1096 sizeof_struct
= sizeof(fbt_struct
);
1097 boot_structp
= (caddr_t
) &fbt_struct
;
1099 error
= copyin(argp
, boot_structp
, sizeof_struct
);
1102 if ( (error
= vnode_getwithref(vp
)) ) {
1105 if (uap
->cmd
== F_WRITEBOOTSTRAP
) {
1107 * Make sure that we are root. Updating the
1108 * bootstrap on a disk could be a security hole
1111 (void)vnode_put(vp
);
1116 if (strncmp(vnode_mount(vp
)->mnt_vfsstat
.f_fstypename
, "hfs",
1117 sizeof(vnode_mount(vp
)->mnt_vfsstat
.f_fstypename
)) != 0) {
1121 * call vnop_ioctl to handle the I/O
1123 error
= VNOP_IOCTL(vp
, uap
->cmd
, boot_structp
, 0, &context
);
1125 (void)vnode_put(vp
);
1129 struct log2phys l2p_struct
; /* structure for allocate command */
1131 if (fp
->f_type
!= DTYPE_VNODE
) {
1135 vp
= (struct vnode
*)fp
->f_data
;
1137 if ( (error
= vnode_getwithref(vp
)) ) {
1140 error
= VNOP_OFFTOBLK(vp
, fp
->f_offset
, &lbn
);
1142 (void)vnode_put(vp
);
1145 error
= VNOP_BLKTOOFF(vp
, lbn
, &offset
);
1147 (void)vnode_put(vp
);
1150 devBlockSize
= vfs_devblocksize(vnode_mount(vp
));
1152 error
= VNOP_BLOCKMAP(vp
, offset
, devBlockSize
, &bn
, NULL
, NULL
, 0, &context
);
1154 (void)vnode_put(vp
);
1157 l2p_struct
.l2p_flags
= 0; /* for now */
1158 l2p_struct
.l2p_contigbytes
= 0; /* for now */
1159 l2p_struct
.l2p_devoffset
= bn
* devBlockSize
;
1160 l2p_struct
.l2p_devoffset
+= fp
->f_offset
- offset
;
1161 error
= copyout((caddr_t
)&l2p_struct
, argp
, sizeof(l2p_struct
));
1169 if (fp
->f_type
!= DTYPE_VNODE
) {
1173 vp
= (struct vnode
*)fp
->f_data
;
1176 pathlen
= MAXPATHLEN
;
1177 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
1178 if (pathbufp
== NULL
) {
1182 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1183 error
= vn_getpath(vp
, pathbufp
, &pathlen
);
1184 (void)vnode_put(vp
);
1187 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
1189 FREE(pathbufp
, M_TEMP
);
1193 case F_PATHPKG_CHECK
: {
1197 if (fp
->f_type
!= DTYPE_VNODE
) {
1201 vp
= (struct vnode
*)fp
->f_data
;
1204 pathlen
= MAXPATHLEN
;
1205 pathbufp
= kalloc(MAXPATHLEN
);
1207 if ( (error
= copyinstr(argp
, pathbufp
, MAXPATHLEN
, &pathlen
)) == 0 ) {
1208 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1209 error
= vn_path_package_check(vp
, pathbufp
, pathlen
, retval
);
1211 (void)vnode_put(vp
);
1214 kfree(pathbufp
, MAXPATHLEN
);
1218 case F_CHKCLEAN
: // used by regression tests to see if all dirty pages got cleaned by fsync()
1219 case F_FULLFSYNC
: // fsync + flush the journal + DKIOCSYNCHRONIZECACHE
1220 case F_FREEZE_FS
: // freeze all other fs operations for the fs of this fd
1221 case F_THAW_FS
: { // thaw all frozen fs operations for the fs of this fd
1222 if (fp
->f_type
!= DTYPE_VNODE
) {
1226 vp
= (struct vnode
*)fp
->f_data
;
1229 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1230 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
1232 (void)vnode_put(vp
);
1238 * SPI (private) for opening a file starting from a dir fd
1241 struct user_fopenfrom fopen
;
1242 struct vnode_attr va
;
1243 struct nameidata nd
;
1246 /* Check if this isn't a valid file descriptor */
1247 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1248 (fp
->f_flag
& FREAD
) == 0) {
1252 vp
= (struct vnode
*)fp
->f_data
;
1255 if (vnode_getwithref(vp
)) {
1260 /* Only valid for directories */
1261 if (vp
->v_type
!= VDIR
) {
1267 /* Get flags, mode and pathname arguments. */
1268 if (IS_64BIT_PROCESS(p
)) {
1269 error
= copyin(argp
, &fopen
, sizeof(fopen
));
1271 struct fopenfrom fopen32
;
1273 error
= copyin(argp
, &fopen32
, sizeof(fopen32
));
1274 fopen
.o_flags
= fopen32
.o_flags
;
1275 fopen
.o_mode
= fopen32
.o_mode
;
1276 fopen
.o_pathname
= CAST_USER_ADDR_T(fopen32
.o_pathname
);
1283 /* Mask off all but regular access permissions */
1284 cmode
= ((fopen
.o_mode
&~ fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
1285 VATTR_SET(&va
, va_mode
, cmode
& ACCESSPERMS
);
1287 /* Start the lookup relative to the file descriptor's vnode. */
1288 NDINIT(&nd
, LOOKUP
, USEDVP
| FOLLOW
| AUDITVNPATH1
, UIO_USERSPACE
,
1289 fopen
.o_pathname
, &context
);
1292 error
= open1(&context
, &nd
, fopen
.o_flags
, &va
, retval
);
1298 * SPI (private) for unlinking a file starting from a dir fd
1300 case F_UNLINKFROM
: {
1301 struct nameidata nd
;
1302 user_addr_t pathname
;
1304 /* Check if this isn't a valid file descriptor */
1305 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1306 (fp
->f_flag
& FREAD
) == 0) {
1310 vp
= (struct vnode
*)fp
->f_data
;
1313 if (vnode_getwithref(vp
)) {
1318 /* Only valid for directories */
1319 if (vp
->v_type
!= VDIR
) {
1325 /* Get flags, mode and pathname arguments. */
1326 if (IS_64BIT_PROCESS(p
)) {
1327 pathname
= (user_addr_t
)argp
;
1329 pathname
= CAST_USER_ADDR_T(argp
);
1332 /* Start the lookup relative to the file descriptor's vnode. */
1333 NDINIT(&nd
, DELETE
, USEDVP
| AUDITVNPATH1
, UIO_USERSPACE
, pathname
, &context
);
1336 error
= unlink1(&context
, &nd
, 0);
1344 struct user_fsignatures fs
;
1346 vm_address_t kernel_blob_addr
;
1347 vm_size_t kernel_blob_size
;
1349 if (fp
->f_type
!= DTYPE_VNODE
) {
1353 vp
= (struct vnode
*)fp
->f_data
;
1355 error
= vnode_getwithref(vp
);
1359 if (IS_64BIT_PROCESS(p
)) {
1360 error
= copyin(argp
, &fs
, sizeof (fs
));
1362 struct fsignatures fs32
;
1364 error
= copyin(argp
, &fs32
, sizeof (fs32
));
1365 fs
.fs_file_start
= fs32
.fs_file_start
;
1366 fs
.fs_blob_start
= CAST_USER_ADDR_T(fs32
.fs_blob_start
);
1367 fs
.fs_blob_size
= fs32
.fs_blob_size
;
1375 if(ubc_cs_blob_get(vp
, CPU_TYPE_ANY
, fs
.fs_file_start
))
1378 printf("CODE SIGNING: resident blob offered for: %s\n", vp
->v_name
);
1383 #define CS_MAX_BLOB_SIZE (1ULL * 1024 * 1024) /* XXX ? */
1384 if (fs
.fs_blob_size
> CS_MAX_BLOB_SIZE
) {
1390 kernel_blob_size
= CAST_DOWN(vm_size_t
, fs
.fs_blob_size
);
1391 kr
= ubc_cs_blob_allocate(&kernel_blob_addr
, &kernel_blob_size
);
1392 if (kr
!= KERN_SUCCESS
) {
1398 error
= copyin(fs
.fs_blob_start
,
1399 (void *) kernel_blob_addr
,
1402 ubc_cs_blob_deallocate(kernel_blob_addr
,
1408 error
= ubc_cs_blob_add(
1410 CPU_TYPE_ANY
, /* not for a specific architecture */
1415 ubc_cs_blob_deallocate(kernel_blob_addr
,
1418 /* ubc_blob_add() has consumed "kernel_blob_addr" */
1421 (void) vnode_put(vp
);
1425 case F_MARKDEPENDENCY
: {
1426 struct vnode
*root_vp
;
1427 struct vnode_attr va
;
1428 vfs_context_t ctx
= vfs_context_current();
1431 if ((current_proc()->p_flag
& P_DEPENDENCY_CAPABLE
) == 0) {
1436 if (fp
->f_type
!= DTYPE_VNODE
) {
1441 vp
= (struct vnode
*)fp
->f_data
;
1444 if (vnode_getwithref(vp
)) {
1449 // the passed in vnode must be the root dir of the file system
1450 if (VFS_ROOT(vp
->v_mount
, &root_vp
, ctx
) != 0 || vp
!= root_vp
) {
1457 // get the owner of the root dir
1459 VATTR_WANTED(&va
, va_uid
);
1460 if (vnode_getattr(vp
, &va
, ctx
) != 0) {
1466 // and last, check that the caller is the super user or
1467 // the owner of the mount point
1468 cred
= vfs_context_ucred(ctx
);
1469 if (!is_suser() && va
.va_uid
!= kauth_cred_getuid(cred
)) {
1475 // if all those checks pass then we can mark the dependency
1476 vfs_markdependency(vp
->v_mount
);
1485 if (uap
->cmd
< FCNTL_FS_SPECIFIC_BASE
) {
1490 // if it's a fs-specific fcntl() then just pass it through
1492 if (fp
->f_type
!= DTYPE_VNODE
) {
1496 vp
= (struct vnode
*)fp
->f_data
;
1499 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1500 error
= VNOP_IOCTL(vp
, uap
->cmd
, CAST_DOWN(caddr_t
, argp
), 0, &context
);
1502 (void)vnode_put(vp
);
1509 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
1510 fp_drop(p
, fd
, fp
, 0);
1513 fp_drop(p
, fd
, fp
, 1);
1522 * Description: Common code for dup, dup2, and fcntl(F_DUPFD).
1524 * Parameters: p Process performing the dup
1526 * new The fd to dup it to
1527 * retval Pointer to the call return area
1529 * Returns: 0 Success
1534 * *retval (modified) The new descriptor
1536 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
1539 * Notes: This function may drop and reacquire this lock; it is unsafe
1540 * for a caller to assume that other state protected by the lock
1541 * has not been subsequently changes out from under it.
1544 finishdup(proc_t p
, struct filedesc
*fdp
, int old
, int new, register_t
*retval
)
1546 struct fileproc
*nfp
;
1547 struct fileproc
*ofp
;
1553 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
1556 if ((ofp
= fdp
->fd_ofiles
[old
]) == NULL
||
1557 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
)) {
1564 error
= mac_file_check_dup(proc_ucred(p
), ofp
->f_fglob
, new);
1574 MALLOC_ZONE(nfp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
1575 /* Failure check follows proc_fdlock() due to handling requirements */
1585 bzero(nfp
, sizeof(struct fileproc
));
1588 nfp
->f_fglob
= ofp
->f_fglob
;
1592 if (fdp
->fd_ofiles
[new] != 0)
1593 panic("finishdup: overwriting fd_ofiles with new %d\n", new);
1594 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0)
1595 panic("finishdup: unreserved fileflags with new %d\n", new);
1598 if (new > fdp
->fd_lastfile
)
1599 fdp
->fd_lastfile
= new;
1600 procfdtbl_releasefd(p
, new, nfp
);
1609 * Description: The implementation of the close(2) system call
1611 * Parameters: p Process in whose per process file table
1612 * the close is to occur
1613 * uap->fd fd to be closed
1616 * Returns: 0 Success
1617 * fp_lookup:EBADF Bad file descriptor
1618 * close_internal:EBADF
1619 * close_internal:??? Anything returnable by a per-fileops
1623 close(proc_t p
, struct close_args
*uap
, register_t
*retval
)
1625 __pthread_testcancel(1);
1626 return(close_nocancel(p
, (struct close_nocancel_args
*)uap
, retval
));
1631 close_nocancel(proc_t p
, struct close_nocancel_args
*uap
, __unused register_t
*retval
)
1633 struct fileproc
*fp
;
1637 AUDIT_SYSCLOSE(p
, fd
);
1641 if ( (error
= fp_lookup(p
,fd
,&fp
, 1)) ) {
1646 error
= close_internal_locked(p
, fd
, fp
, 0);
1655 * close_internal_locked
1657 * Close a file descriptor.
1659 * Parameters: p Process in whose per process file table
1660 * the close is to occur
1661 * fd fd to be closed
1662 * fp fileproc associated with the fd
1664 * Returns: 0 Success
1665 * EBADF fd already in close wait state
1666 * closef_locked:??? Anything returnable by a per-fileops
1669 * Locks: Assumes proc_fdlock for process is held by the caller and returns
1672 * Notes: This function may drop and reacquire this lock; it is unsafe
1673 * for a caller to assume that other state protected by the lock
1674 * has not been subsequently changes out from under it, if the
1675 * caller made the call with the lock held.
1678 close_internal_locked(proc_t p
, int fd
, struct fileproc
*fp
, int flags
)
1680 struct filedesc
*fdp
= p
->p_fd
;
1682 int resvfd
= flags
& FD_DUP2RESV
;
1686 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
1689 /* Keep people from using the filedesc while we are closing it */
1690 procfdtbl_markclosefd(p
, fd
);
1693 if ((fp
->f_flags
& FP_CLOSING
) == FP_CLOSING
) {
1694 panic("close_internal_locked: being called on already closing fd\n");
1699 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
1700 panic("close_internal: unreserved fileflags with fd %d\n", fd
);
1703 fp
->f_flags
|= FP_CLOSING
;
1705 if ( (fp
->f_flags
& FP_AIOISSUED
) || kauth_authorize_fileop_has_listeners() ) {
1709 if ( (fp
->f_type
== DTYPE_VNODE
) && kauth_authorize_fileop_has_listeners() ) {
1711 * call out to allow 3rd party notification of close.
1712 * Ignore result of kauth_authorize_fileop call.
1714 if (vnode_getwithref((vnode_t
)fp
->f_data
) == 0) {
1715 u_int fileop_flags
= 0;
1716 if ((fp
->f_flags
& FP_WRITTEN
) != 0)
1717 fileop_flags
|= KAUTH_FILEOP_CLOSE_MODIFIED
;
1718 kauth_authorize_fileop(fp
->f_fglob
->fg_cred
, KAUTH_FILEOP_CLOSE
,
1719 (uintptr_t)fp
->f_data
, (uintptr_t)fileop_flags
);
1720 vnode_put((vnode_t
)fp
->f_data
);
1723 if (fp
->f_flags
& FP_AIOISSUED
)
1725 * cancel all async IO requests that can be cancelled.
1727 _aio_close( p
, fd
);
1732 if (fd
< fdp
->fd_knlistsize
)
1733 knote_fdclose(p
, fd
);
1735 if (fp
->f_flags
& FP_WAITEVENT
)
1736 (void)waitevent_close(p
, fp
);
1738 if ((fp
->f_flags
& FP_INCHRREAD
) == 0)
1739 fileproc_drain(p
, fp
);
1744 error
= closef_locked(fp
, fp
->f_fglob
, p
);
1745 if ((fp
->f_flags
& FP_WAITCLOSE
) == FP_WAITCLOSE
)
1746 wakeup(&fp
->f_flags
);
1747 fp
->f_flags
&= ~(FP_WAITCLOSE
| FP_CLOSING
);
1751 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
1757 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
1758 panic("close with reserved fd returns with freed fd:%d: proc: %x\n", fd
, (unsigned int)p
);
1769 * Description: Return status information about a file descriptor.
1771 * Parameters: p The process doing the fstat
1773 * ub The user stat buffer
1774 * xsecurity The user extended security
1775 * buffer, or 0 if none
1776 * xsecurity_size The size of xsecurity, or 0
1778 * isstat64 Flag to indicate 64 bit version
1779 * for inode size, etc.
1781 * Returns: 0 Success
1784 * fp_lookup:EBADF Bad file descriptor
1785 * vnode_getwithref:???
1787 * vnode_getwithref:???
1794 * Notes: Internal implementation for all other fstat() related
1797 * XXX switch on node type is bogus; need a stat in struct
1798 * XXX fileops instead.
1801 fstat1(proc_t p
, int fd
, user_addr_t ub
, user_addr_t xsecurity
, user_addr_t xsecurity_size
, int isstat64
)
1803 struct fileproc
*fp
;
1806 struct user_stat user_sb
;
1807 struct user_stat64 user_sb64
;
1812 kauth_filesec_t fsec
;
1813 user_size_t xsecurity_bufsize
;
1814 vfs_context_t ctx
= vfs_context_current();
1820 if ((error
= fp_lookup(p
, fd
, &fp
, 0)) != 0) {
1825 fsec
= KAUTH_FILESEC_NONE
;
1827 sbptr
= (isstat64
!= 0) ? (void *)&sb64
: (void *)&sb
;
1832 if ((error
= vnode_getwithref((vnode_t
)data
)) == 0) {
1834 * If the caller has the file open, and is not
1835 * requesting extended security information, we are
1836 * going to let them get the basic stat information.
1838 if (xsecurity
== USER_ADDR_NULL
) {
1839 error
= vn_stat_noauth((vnode_t
)data
, sbptr
, NULL
, isstat64
, ctx
);
1841 error
= vn_stat((vnode_t
)data
, sbptr
, &fsec
, isstat64
, ctx
);
1844 AUDIT_ARG(vnpath
, (struct vnode
*)data
, ARG_VNODE1
);
1845 (void)vnode_put((vnode_t
)data
);
1851 error
= soo_stat((struct socket
*)data
, sbptr
, isstat64
);
1853 #endif /* SOCKETS */
1856 error
= pipe_stat((void *)data
, sbptr
, isstat64
);
1860 error
= pshm_stat((void *)data
, sbptr
, isstat64
);
1864 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1865 error
= kqueue_stat(fp
, sbptr
, isstat64
, p
);
1866 thread_funnel_set(kernel_flock
, funnel_state
);
1876 if (isstat64
!= 0) {
1878 sb64
.st_qspare
[0] = 0LL;
1879 sb64
.st_qspare
[1] = 0LL;
1880 if (IS_64BIT_PROCESS(current_proc())) {
1881 munge_stat64(&sb64
, &user_sb64
);
1882 my_size
= sizeof(user_sb64
);
1883 sbp
= (caddr_t
)&user_sb64
;
1885 my_size
= sizeof(sb64
);
1886 sbp
= (caddr_t
)&sb64
;
1890 sb
.st_qspare
[0] = 0LL;
1891 sb
.st_qspare
[1] = 0LL;
1892 if (IS_64BIT_PROCESS(current_proc())) {
1893 munge_stat(&sb
, &user_sb
);
1894 my_size
= sizeof(user_sb
);
1895 sbp
= (caddr_t
)&user_sb
;
1897 my_size
= sizeof(sb
);
1902 error
= copyout(sbp
, ub
, my_size
);
1905 /* caller wants extended security information? */
1906 if (xsecurity
!= USER_ADDR_NULL
) {
1908 /* did we get any? */
1909 if (fsec
== KAUTH_FILESEC_NONE
) {
1910 if (susize(xsecurity_size
, 0) != 0) {
1915 /* find the user buffer size */
1916 xsecurity_bufsize
= fusize(xsecurity_size
);
1918 /* copy out the actual data size */
1919 if (susize(xsecurity_size
, KAUTH_FILESEC_COPYSIZE(fsec
)) != 0) {
1924 /* if the caller supplied enough room, copy out to it */
1925 if (xsecurity_bufsize
>= KAUTH_FILESEC_COPYSIZE(fsec
))
1926 error
= copyout(fsec
, xsecurity
, KAUTH_FILESEC_COPYSIZE(fsec
));
1930 fp_drop(p
, fd
, fp
, 0);
1932 kauth_filesec_free(fsec
);
1940 * Description: Extended version of fstat supporting returning extended
1941 * security information
1943 * Parameters: p The process doing the fstat
1944 * uap->fd The fd to stat
1945 * uap->ub The user stat buffer
1946 * uap->xsecurity The user extended security
1947 * buffer, or 0 if none
1948 * uap->xsecurity_size The size of xsecurity, or 0
1950 * Returns: 0 Success
1951 * !0 Errno (see fstat1)
1954 fstat_extended(proc_t p
, struct fstat_extended_args
*uap
, __unused register_t
*retval
)
1956 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 0));
1963 * Description: Get file status for the file associated with fd
1965 * Parameters: p The process doing the fstat
1966 * uap->fd The fd to stat
1967 * uap->ub The user stat buffer
1969 * Returns: 0 Success
1970 * !0 Errno (see fstat1)
1973 fstat(proc_t p
, register struct fstat_args
*uap
, __unused register_t
*retval
)
1975 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 0));
1982 * Description: Extended version of fstat64 supporting returning extended
1983 * security information
1985 * Parameters: p The process doing the fstat
1986 * uap->fd The fd to stat
1987 * uap->ub The user stat buffer
1988 * uap->xsecurity The user extended security
1989 * buffer, or 0 if none
1990 * uap->xsecurity_size The size of xsecurity, or 0
1992 * Returns: 0 Success
1993 * !0 Errno (see fstat1)
1996 fstat64_extended(proc_t p
, struct fstat64_extended_args
*uap
, __unused register_t
*retval
)
1998 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 1));
2005 * Description: Get 64 bit version of the file status for the file associated
2008 * Parameters: p The process doing the fstat
2009 * uap->fd The fd to stat
2010 * uap->ub The user stat buffer
2012 * Returns: 0 Success
2013 * !0 Errno (see fstat1)
2016 fstat64(proc_t p
, register struct fstat64_args
*uap
, __unused register_t
*retval
)
2018 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 1));
2025 * Description: Return pathconf information about a file descriptor.
2027 * Parameters: p Process making the request
2028 * uap->fd fd to get information about
2029 * uap->name Name of information desired
2030 * retval Pointer to the call return area
2032 * Returns: 0 Success
2034 * fp_lookup:EBADF Bad file descriptor
2035 * vnode_getwithref:???
2039 * *retval (modified) Returned information (numeric)
2042 fpathconf(proc_t p
, struct fpathconf_args
*uap
, register_t
*retval
)
2045 struct fileproc
*fp
;
2052 AUDIT_ARG(fd
, uap
->fd
);
2053 if ( (error
= fp_lookup(p
, fd
, &fp
, 0)) )
2061 if (uap
->name
!= _PC_PIPE_BUF
) {
2075 vp
= (struct vnode
*)data
;
2077 if ( (error
= vnode_getwithref(vp
)) == 0) {
2078 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
2080 error
= vn_pathconf(vp
, uap
->name
, retval
, vfs_context_current());
2082 (void)vnode_put(vp
);
2089 case DTYPE_FSEVENTS
:
2096 fp_drop(p
, fd
, fp
, 0);
2101 * Statistics counter for the number of times a process calling fdalloc()
2102 * has resulted in an expansion of the per process open file table.
2104 * XXX This would likely be of more use if it were per process
2112 * Description: Allocate a file descriptor for the process.
2114 * Parameters: p Process to allocate the fd in
2115 * want The fd we would prefer to get
2116 * result Pointer to fd we got
2118 * Returns: 0 Success
2123 * *result (modified) The fd which was allocated
2126 fdalloc(proc_t p
, int want
, int *result
)
2128 struct filedesc
*fdp
= p
->p_fd
;
2130 int lim
, last
, numfiles
, oldnfiles
;
2131 struct fileproc
**newofiles
, **ofiles
;
2132 char *newofileflags
;
2135 * Search for a free descriptor starting at the higher
2136 * of want or fd_freefile. If that fails, consider
2137 * expanding the ofile array.
2140 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2143 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
2145 last
= min(fdp
->fd_nfiles
, lim
);
2146 if ((i
= want
) < fdp
->fd_freefile
)
2147 i
= fdp
->fd_freefile
;
2148 for (; i
< last
; i
++) {
2149 if (fdp
->fd_ofiles
[i
] == NULL
&& !(fdp
->fd_ofileflags
[i
] & UF_RESERVED
)) {
2150 procfdtbl_reservefd(p
, i
);
2151 if (i
> fdp
->fd_lastfile
)
2152 fdp
->fd_lastfile
= i
;
2153 if (want
<= fdp
->fd_freefile
)
2154 fdp
->fd_freefile
= i
;
2161 * No space in current array. Expand?
2163 if (fdp
->fd_nfiles
>= lim
)
2165 if (fdp
->fd_nfiles
< NDEXTENT
)
2166 numfiles
= NDEXTENT
;
2168 numfiles
= 2 * fdp
->fd_nfiles
;
2173 MALLOC_ZONE(newofiles
, struct fileproc
**,
2174 numfiles
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
2176 if (newofiles
== NULL
) {
2179 if (fdp
->fd_nfiles
>= numfiles
) {
2180 FREE_ZONE(newofiles
, numfiles
* OFILESIZE
, M_OFILETABL
);
2183 newofileflags
= (char *) &newofiles
[numfiles
];
2185 * Copy the existing ofile and ofileflags arrays
2186 * and zero the new portion of each array.
2188 oldnfiles
= fdp
->fd_nfiles
;
2189 (void) memcpy(newofiles
, fdp
->fd_ofiles
,
2190 oldnfiles
* sizeof(*fdp
->fd_ofiles
));
2191 (void) memset(&newofiles
[oldnfiles
], 0,
2192 (numfiles
- oldnfiles
) * sizeof(*fdp
->fd_ofiles
));
2194 (void) memcpy(newofileflags
, fdp
->fd_ofileflags
,
2195 oldnfiles
* sizeof(*fdp
->fd_ofileflags
));
2196 (void) memset(&newofileflags
[oldnfiles
], 0,
2197 (numfiles
- oldnfiles
) *
2198 sizeof(*fdp
->fd_ofileflags
));
2199 ofiles
= fdp
->fd_ofiles
;
2200 fdp
->fd_ofiles
= newofiles
;
2201 fdp
->fd_ofileflags
= newofileflags
;
2202 fdp
->fd_nfiles
= numfiles
;
2203 FREE_ZONE(ofiles
, oldnfiles
* OFILESIZE
, M_OFILETABL
);
2212 * Description: Check to see whether n user file descriptors are available
2215 * Parameters: p Process to check in
2216 * n The number of fd's desired
2221 * Locks: Assumes proc_fdlock for process is held by the caller
2223 * Notes: The answer only remains valid so long as the proc_fdlock is
2224 * held by the caller.
2227 fdavail(proc_t p
, int n
)
2229 struct filedesc
*fdp
= p
->p_fd
;
2230 struct fileproc
**fpp
;
2234 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
2235 if ((i
= lim
- fdp
->fd_nfiles
) > 0 && (n
-= i
) <= 0)
2237 fpp
= &fdp
->fd_ofiles
[fdp
->fd_freefile
];
2238 flags
= &fdp
->fd_ofileflags
[fdp
->fd_freefile
];
2239 for (i
= fdp
->fd_nfiles
- fdp
->fd_freefile
; --i
>= 0; fpp
++, flags
++)
2240 if (*fpp
== NULL
&& !(*flags
& UF_RESERVED
) && --n
<= 0)
2249 * Description: Legacy KPI wrapper function for _fdrelse
2251 * Parameters: p Process in which fd lives
2256 * Locks: Assumes proc_fdlock for process is held by the caller
2259 fdrelse(proc_t p
, int fd
)
2268 * Description: Get the fileproc pointer for the given fd from the per process
2269 * open file table without taking an explicit reference on it.
2271 * Parameters: p Process containing fd
2272 * fd fd to obtain fileproc for
2273 * resultfp Pointer to pointer return area
2275 * Returns: 0 Success
2279 * *resultfp (modified) Pointer to fileproc pointer
2281 * Locks: Assumes proc_fdlock for process is held by the caller
2283 * Notes: Because there is no reference explicitly taken, the returned
2284 * fileproc pointer is only valid so long as the proc_fdlock
2285 * remains held by the caller.
2288 fdgetf_noref(proc_t p
, int fd
, struct fileproc
**resultfp
)
2290 struct filedesc
*fdp
= p
->p_fd
;
2291 struct fileproc
*fp
;
2293 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2294 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2295 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2307 * Description: Get fileproc and vnode pointer for a given fd from the per
2308 * process open file table of the specified process, and if
2309 * successful, increment the f_iocount
2311 * Parameters: p Process in which fd lives
2312 * fd fd to get information for
2313 * resultfp Pointer to result fileproc
2314 * pointer area, or 0 if none
2315 * resultvp Pointer to result vnode pointer
2316 * area, or 0 if none
2318 * Returns: 0 Success
2319 * EBADF Bad file descriptor
2320 * ENOTSUP fd does not refer to a vnode
2323 * *resultfp (modified) Fileproc pointer
2324 * *resultvp (modified) vnode pointer
2326 * Notes: The resultfp and resultvp fields are optional, and may be
2327 * independently specified as NULL to skip returning information
2329 * Locks: Internally takes and releases proc_fdlock
2332 fp_getfvp(proc_t p
, int fd
, struct fileproc
**resultfp
, struct vnode
**resultvp
)
2334 struct filedesc
*fdp
= p
->p_fd
;
2335 struct fileproc
*fp
;
2337 proc_fdlock_spin(p
);
2338 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2339 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2340 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2344 if (fp
->f_type
!= DTYPE_VNODE
) {
2353 *resultvp
= (struct vnode
*)fp
->f_data
;
2363 * Description: Get fileproc, vnode pointer, and vid for a given fd from the
2364 * per process open file table of the specified process, and if
2365 * successful, increment the f_iocount
2367 * Parameters: p Process in which fd lives
2368 * fd fd to get information for
2369 * resultfp Pointer to result fileproc
2370 * pointer area, or 0 if none
2371 * resultvp Pointer to result vnode pointer
2372 * area, or 0 if none
2373 * vidp Pointer to resuld vid area
2375 * Returns: 0 Success
2376 * EBADF Bad file descriptor
2377 * ENOTSUP fd does not refer to a vnode
2380 * *resultfp (modified) Fileproc pointer
2381 * *resultvp (modified) vnode pointer
2384 * Notes: The resultfp and resultvp fields are optional, and may be
2385 * independently specified as NULL to skip returning information
2387 * Locks: Internally takes and releases proc_fdlock
2390 fp_getfvpandvid(proc_t p
, int fd
, struct fileproc
**resultfp
,
2391 struct vnode
**resultvp
, uint32_t *vidp
)
2393 struct filedesc
*fdp
= p
->p_fd
;
2394 struct fileproc
*fp
;
2396 proc_fdlock_spin(p
);
2397 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2398 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2399 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2403 if (fp
->f_type
!= DTYPE_VNODE
) {
2412 *resultvp
= (struct vnode
*)fp
->f_data
;
2414 *vidp
= (uint32_t)vnode_vid((struct vnode
*)fp
->f_data
);
2424 * Description: Get fileproc and socket pointer for a given fd from the
2425 * per process open file table of the specified process, and if
2426 * successful, increment the f_iocount
2428 * Parameters: p Process in which fd lives
2429 * fd fd to get information for
2430 * resultfp Pointer to result fileproc
2431 * pointer area, or 0 if none
2432 * results Pointer to result socket
2433 * pointer area, or 0 if none
2435 * Returns: EBADF The file descriptor is invalid
2436 * EOPNOTSUPP The file descriptor is not a socket
2440 * *resultfp (modified) Fileproc pointer
2441 * *results (modified) socket pointer
2443 * Notes: EOPNOTSUPP should probably be ENOTSOCK; this function is only
2444 * ever called from accept1().
2447 fp_getfsock(proc_t p
, int fd
, struct fileproc
**resultfp
,
2448 struct socket
**results
)
2450 struct filedesc
*fdp
= p
->p_fd
;
2451 struct fileproc
*fp
;
2453 proc_fdlock_spin(p
);
2454 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2455 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2456 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2460 if (fp
->f_type
!= DTYPE_SOCKET
) {
2469 *results
= (struct socket
*)fp
->f_data
;
2479 * Description: Get fileproc and kqueue pointer for a given fd from the
2480 * per process open file table of the specified process, and if
2481 * successful, increment the f_iocount
2483 * Parameters: p Process in which fd lives
2484 * fd fd to get information for
2485 * resultfp Pointer to result fileproc
2486 * pointer area, or 0 if none
2487 * resultkq Pointer to result kqueue
2488 * pointer area, or 0 if none
2490 * Returns: EBADF The file descriptor is invalid
2491 * EBADF The file descriptor is not a socket
2495 * *resultfp (modified) Fileproc pointer
2496 * *resultkq (modified) kqueue pointer
2498 * Notes: The second EBADF should probably be something else to make
2499 * the error condition distinct.
2502 fp_getfkq(proc_t p
, int fd
, struct fileproc
**resultfp
,
2503 struct kqueue
**resultkq
)
2505 struct filedesc
*fdp
= p
->p_fd
;
2506 struct fileproc
*fp
;
2508 proc_fdlock_spin(p
);
2509 if ( fd
< 0 || fd
>= fdp
->fd_nfiles
||
2510 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2511 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2515 if (fp
->f_type
!= DTYPE_KQUEUE
) {
2524 *resultkq
= (struct kqueue
*)fp
->f_data
;
2534 * Description: Get fileproc and POSIX shared memory pointer for a given fd
2535 * from the per process open file table of the specified process
2536 * and if successful, increment the f_iocount
2538 * Parameters: p Process in which fd lives
2539 * fd fd to get information for
2540 * resultfp Pointer to result fileproc
2541 * pointer area, or 0 if none
2542 * resultpshm Pointer to result POSIX
2543 * shared memory pointer
2544 * pointer area, or 0 if none
2546 * Returns: EBADF The file descriptor is invalid
2547 * EBADF The file descriptor is not a POSIX
2548 * shared memory area
2552 * *resultfp (modified) Fileproc pointer
2553 * *resultpshm (modified) POSIX shared memory pointer
2555 * Notes: The second EBADF should probably be something else to make
2556 * the error condition distinct.
2559 fp_getfpshm(proc_t p
, int fd
, struct fileproc
**resultfp
,
2560 struct pshmnode
**resultpshm
)
2562 struct filedesc
*fdp
= p
->p_fd
;
2563 struct fileproc
*fp
;
2565 proc_fdlock_spin(p
);
2566 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2567 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2568 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2572 if (fp
->f_type
!= DTYPE_PSXSHM
) {
2582 *resultpshm
= (struct pshmnode
*)fp
->f_data
;
2592 * Description: Get fileproc and POSIX semaphore pointer for a given fd from
2593 * the per process open file table of the specified process
2594 * and if successful, increment the f_iocount
2596 * Parameters: p Process in which fd lives
2597 * fd fd to get information for
2598 * resultfp Pointer to result fileproc
2599 * pointer area, or 0 if none
2600 * resultpsem Pointer to result POSIX
2601 * semaphore pointer area, or
2604 * Returns: EBADF The file descriptor is invalid
2605 * EBADF The file descriptor is not a POSIX
2610 * *resultfp (modified) Fileproc pointer
2611 * *resultpsem (modified) POSIX semaphore pointer
2613 * Notes: The second EBADF should probably be something else to make
2614 * the error condition distinct.
2616 * In order to support unnamed POSIX semaphores, the named
2617 * POSIX semaphores will have to move out of the per-process
2618 * open filetable, and into a global table that is shared with
2619 * unnamed POSIX semaphores, since unnamed POSIX semaphores
2620 * are typically used by declaring instances in shared memory,
2621 * and there's no other way to do this without changing the
2622 * underlying type, which would introduce binary compatibility
2626 fp_getfpsem(proc_t p
, int fd
, struct fileproc
**resultfp
,
2627 struct psemnode
**resultpsem
)
2629 struct filedesc
*fdp
= p
->p_fd
;
2630 struct fileproc
*fp
;
2632 proc_fdlock_spin(p
);
2633 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2634 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2635 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2639 if (fp
->f_type
!= DTYPE_PSXSEM
) {
2648 *resultpsem
= (struct psemnode
*)fp
->f_data
;
2658 * Description: Get fileproc and pipe pointer for a given fd from the
2659 * per process open file table of the specified process
2660 * and if successful, increment the f_iocount
2662 * Parameters: p Process in which fd lives
2663 * fd fd to get information for
2664 * resultfp Pointer to result fileproc
2665 * pointer area, or 0 if none
2666 * resultpipe Pointer to result pipe
2667 * pointer area, or 0 if none
2669 * Returns: EBADF The file descriptor is invalid
2670 * EBADF The file descriptor is not a socket
2674 * *resultfp (modified) Fileproc pointer
2675 * *resultpipe (modified) pipe pointer
2677 * Notes: The second EBADF should probably be something else to make
2678 * the error condition distinct.
2681 fp_getfpipe(proc_t p
, int fd
, struct fileproc
**resultfp
,
2682 struct pipe
**resultpipe
)
2684 struct filedesc
*fdp
= p
->p_fd
;
2685 struct fileproc
*fp
;
2687 proc_fdlock_spin(p
);
2688 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2689 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2690 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2694 if (fp
->f_type
!= DTYPE_PIPE
) {
2703 *resultpipe
= (struct pipe
*)fp
->f_data
;
2710 #define DTYPE_ATALK -1 /* XXX This does not belong here */
2716 * Description: Get fileproc and atalk pointer for a given fd from the
2717 * per process open file table of the specified process
2718 * and if successful, increment the f_iocount
2720 * Parameters: p Process in which fd lives
2721 * fd fd to get information for
2722 * resultfp Pointer to result fileproc
2723 * pointer area, or 0 if none
2724 * resultatalk Pointer to result atalk
2725 * pointer area, or 0 if none
2726 * Returns: EBADF The file descriptor is invalid
2727 * EBADF The file descriptor is not a socket
2731 * *resultfp (modified) Fileproc pointer
2732 * *resultatalk (modified) atalk pointer
2734 * Notes: The second EBADF should probably be something else to make
2735 * the error condition distinct.
2737 * XXX This code is specific to AppleTalk protocol support, and
2738 * XXX should be conditionally compiled
2741 fp_getfatalk(proc_t p
, int fd
, struct fileproc
**resultfp
,
2742 struct atalk
**resultatalk
)
2744 struct filedesc
*fdp
= p
->p_fd
;
2745 struct fileproc
*fp
;
2747 proc_fdlock_spin(p
);
2748 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2749 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2750 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2754 if (fp
->f_type
!= (DTYPE_ATALK
+1)) {
2763 *resultatalk
= (struct atalk
*)fp
->f_data
;
2773 * Description: Get fileproc pointer for a given fd from the per process
2774 * open file table of the specified process and if successful,
2775 * increment the f_iocount
2777 * Parameters: p Process in which fd lives
2778 * fd fd to get information for
2779 * resultfp Pointer to result fileproc
2780 * pointer area, or 0 if none
2781 * locked !0 if the caller holds the
2782 * proc_fdlock, 0 otherwise
2784 * Returns: 0 Success
2785 * EBADF Bad file descriptor
2788 * *resultfp (modified) Fileproc pointer
2790 * Locks: If the argument 'locked' is non-zero, then the caller is
2791 * expected to have taken and held the proc_fdlock; if it is
2792 * zero, than this routine internally takes and drops this lock.
2795 fp_lookup(proc_t p
, int fd
, struct fileproc
**resultfp
, int locked
)
2797 struct filedesc
*fdp
= p
->p_fd
;
2798 struct fileproc
*fp
;
2801 proc_fdlock_spin(p
);
2802 if (fd
< 0 || fdp
== NULL
|| fd
>= fdp
->fd_nfiles
||
2803 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2804 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2823 * Description: Set the FP_WRITTEN flag on the fileproc and drop the I/O
2824 * reference previously taken by calling fp_lookup et. al.
2826 * Parameters: p Process in which the fd lives
2827 * fd fd associated with the fileproc
2828 * fp fileproc on which to set the
2829 * flag and drop the reference
2831 * Returns: 0 Success
2832 * fp_drop:EBADF Bad file descriptor
2834 * Locks: This function internally takes and drops the proc_fdlock for
2835 * the supplied process
2837 * Notes: The fileproc must correspond to the fd in the supplied proc
2840 fp_drop_written(proc_t p
, int fd
, struct fileproc
*fp
)
2844 proc_fdlock_spin(p
);
2846 fp
->f_flags
|= FP_WRITTEN
;
2848 error
= fp_drop(p
, fd
, fp
, 1);
2859 * Description: Set the FP_WAITEVENT flag on the fileproc and drop the I/O
2860 * reference previously taken by calling fp_lookup et. al.
2862 * Parameters: p Process in which the fd lives
2863 * fd fd associated with the fileproc
2864 * fp fileproc on which to set the
2865 * flag and drop the reference
2867 * Returns: 0 Success
2868 * fp_drop:EBADF Bad file descriptor
2870 * Locks: This function internally takes and drops the proc_fdlock for
2871 * the supplied process
2873 * Notes: The fileproc must correspond to the fd in the supplied proc
2876 fp_drop_event(proc_t p
, int fd
, struct fileproc
*fp
)
2880 proc_fdlock_spin(p
);
2882 fp
->f_flags
|= FP_WAITEVENT
;
2884 error
= fp_drop(p
, fd
, fp
, 1);
2895 * Description: Drop the I/O reference previously taken by calling fp_lookup
2898 * Parameters: p Process in which the fd lives
2899 * fd fd associated with the fileproc
2900 * fp fileproc on which to set the
2901 * flag and drop the reference
2902 * locked flag to internally take and
2903 * drop proc_fdlock if it is not
2904 * already held by the caller
2906 * Returns: 0 Success
2907 * EBADF Bad file descriptor
2909 * Locks: This function internally takes and drops the proc_fdlock for
2910 * the supplied process if 'locked' is non-zero, and assumes that
2911 * the caller already holds this lock if 'locked' is non-zero.
2913 * Notes: The fileproc must correspond to the fd in the supplied proc
2916 fp_drop(proc_t p
, int fd
, struct fileproc
*fp
, int locked
)
2918 struct filedesc
*fdp
= p
->p_fd
;
2922 proc_fdlock_spin(p
);
2923 if ((fp
== FILEPROC_NULL
) && (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2924 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2925 ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
2926 !(fdp
->fd_ofileflags
[fd
] & UF_CLOSING
)))) {
2933 if (p
->p_fpdrainwait
&& fp
->f_iocount
== 0) {
2934 p
->p_fpdrainwait
= 0;
2940 wakeup(&p
->p_fpdrainwait
);
2949 * Description: Given an fd, look it up in the current process's per process
2950 * open file table, and return its internal vnode pointer.
2952 * Parameters: fd fd to obtain vnode from
2953 * vpp pointer to vnode return area
2955 * Returns: 0 Success
2956 * EINVAL The fd does not refer to a
2957 * vnode fileproc entry
2958 * fp_lookup:EBADF Bad file descriptor
2961 * *vpp (modified) Returned vnode pointer
2963 * Locks: This function internally takes and drops the proc_fdlock for
2964 * the current process
2966 * Notes: If successful, this function increments the f_iocount on the
2967 * fd's corresponding fileproc.
2969 * The fileproc referenced is not returned; because of this, care
2970 * must be taken to not drop the last reference (e.g. by closing
2971 * the file). This is inhernely unsafe, since the reference may
2972 * not be recoverable from the vnode, if there is a subsequent
2973 * close that destroys the associate fileproc. The caller should
2974 * therefore retain their own reference on the fileproc so that
2975 * the f_iocount can be dropped subsequently. Failure to do this
2976 * can result in the returned pointer immediately becoming invalid
2977 * following the call.
2979 * Use of this function is discouraged.
2982 file_vnode(int fd
, struct vnode
**vpp
)
2984 proc_t p
= current_proc();
2985 struct fileproc
*fp
;
2988 proc_fdlock_spin(p
);
2989 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
2993 if (fp
->f_type
!= DTYPE_VNODE
) {
2994 fp_drop(p
, fd
, fp
,1);
2998 *vpp
= (struct vnode
*)fp
->f_data
;
3008 * Description: Given an fd, look it up in the current process's per process
3009 * open file table, and return its internal socket pointer.
3011 * Parameters: fd fd to obtain vnode from
3012 * sp pointer to socket return area
3014 * Returns: 0 Success
3015 * ENOTSOCK Not a socket
3016 * fp_lookup:EBADF Bad file descriptor
3019 * *sp (modified) Returned socket pointer
3021 * Locks: This function internally takes and drops the proc_fdlock for
3022 * the current process
3024 * Notes: If successful, this function increments the f_iocount on the
3025 * fd's corresponding fileproc.
3027 * The fileproc referenced is not returned; because of this, care
3028 * must be taken to not drop the last reference (e.g. by closing
3029 * the file). This is inhernely unsafe, since the reference may
3030 * not be recoverable from the socket, if there is a subsequent
3031 * close that destroys the associate fileproc. The caller should
3032 * therefore retain their own reference on the fileproc so that
3033 * the f_iocount can be dropped subsequently. Failure to do this
3034 * can result in the returned pointer immediately becoming invalid
3035 * following the call.
3037 * Use of this function is discouraged.
3040 file_socket(int fd
, struct socket
**sp
)
3042 proc_t p
= current_proc();
3043 struct fileproc
*fp
;
3046 proc_fdlock_spin(p
);
3047 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
3051 if (fp
->f_type
!= DTYPE_SOCKET
) {
3052 fp_drop(p
, fd
, fp
,1);
3056 *sp
= (struct socket
*)fp
->f_data
;
3066 * Description: Given an fd, look it up in the current process's per process
3067 * open file table, and return its fileproc's flags field.
3069 * Parameters: fd fd whose flags are to be
3071 * flags pointer to flags data area
3073 * Returns: 0 Success
3074 * ENOTSOCK Not a socket
3075 * fp_lookup:EBADF Bad file descriptor
3078 * *flags (modified) Returned flags field
3080 * Locks: This function internally takes and drops the proc_fdlock for
3081 * the current process
3083 * Notes: This function will internally increment and decrement the
3084 * f_iocount of the fileproc as part of its operation.
3087 file_flags(int fd
, int *flags
)
3090 proc_t p
= current_proc();
3091 struct fileproc
*fp
;
3094 proc_fdlock_spin(p
);
3095 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
3099 *flags
= (int)fp
->f_flag
;
3100 fp_drop(p
, fd
, fp
,1);
3110 * Description: Drop an iocount reference on an fd, and wake up any waiters
3111 * for draining (i.e. blocked in fileproc_drain() called during
3112 * the last attempt to close a file).
3114 * Parameters: fd fd on which an ioreference is
3117 * Returns: 0 Success
3118 * EBADF Bad file descriptor
3120 * Description: Given an fd, look it up in the current process's per process
3121 * open file table, and drop it's fileproc's f_iocount by one
3123 * Notes: This is intended as a corresponding operation to the functions
3124 * file_vnode() and file_socket() operations.
3126 * Technically, the close reference is supposed to be protected
3127 * by a fileproc_drain(), however, a drain will only block if
3128 * the fd refers to a character device, and that device has had
3129 * preparefileread() called on it. If it refers to something
3130 * other than a character device, then the drain will occur and
3131 * block each close attempt, rather than merely the last close.
3133 * Since it's possible for an fd that refers to a character
3134 * device to have an intermediate close followed by an open to
3135 * cause a different file to correspond to that descriptor,
3136 * unless there was a cautionary reference taken on the fileproc,
3137 * this is an inherently unsafe function. This happens in the
3138 * case where multiple fd's in a process refer to the same
3139 * character device (e.g. stdin/out/err pointing to a tty, etc.).
3141 * Use of this function is discouraged.
3146 struct fileproc
*fp
;
3147 proc_t p
= current_proc();
3150 proc_fdlock_spin(p
);
3151 if (fd
< 0 || fd
>= p
->p_fd
->fd_nfiles
||
3152 (fp
= p
->p_fd
->fd_ofiles
[fd
]) == NULL
||
3153 ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
3154 !(p
->p_fd
->fd_ofileflags
[fd
] & UF_CLOSING
))) {
3160 if (p
->p_fpdrainwait
&& fp
->f_iocount
== 0) {
3161 p
->p_fpdrainwait
= 0;
3167 wakeup(&p
->p_fpdrainwait
);
3175 * Description: Allocate an entry in the per process open file table and
3176 * return the corresponding fileproc and fd.
3178 * Parameters: p The process in whose open file
3179 * table the fd is to be allocated
3180 * resultfp Pointer to fileproc pointer
3182 * resultfd Pointer to fd return area
3185 * Returns: 0 Success
3186 * falloc:ENFILE Too many open files in system
3187 * falloc:EMFILE Too many open files in process
3188 * falloc:ENOMEM M_FILEPROC or M_FILEGLOB zone
3192 * *resultfd (modified) Returned fileproc pointer
3193 * *resultfd (modified) Returned fd
3195 * Locks: This function takes and drops the proc_fdlock; if this lock
3196 * is alread held, use falloc_locked() instead.
3198 * Notes: This function takes separate process and context arguments
3199 * solely to support kern_exec.c; otherwise, it would take
3200 * neither, and expect falloc_locked() to use the
3201 * vfs_context_current() routine internally.
3204 falloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
)
3209 error
= falloc_locked(p
, resultfp
, resultfd
, ctx
, 1);
3219 * Create a new open file structure and allocate
3220 * a file decriptor for the process that refers to it.
3222 * Returns: 0 Success
3224 * Description: Allocate an entry in the per process open file table and
3225 * return the corresponding fileproc and fd.
3227 * Parameters: p The process in whose open file
3228 * table the fd is to be allocated
3229 * resultfp Pointer to fileproc pointer
3231 * resultfd Pointer to fd return area
3233 * locked Flag to indicate whether the
3234 * caller holds proc_fdlock
3236 * Returns: 0 Success
3237 * ENFILE Too many open files in system
3238 * fdalloc:EMFILE Too many open files in process
3239 * ENOMEM M_FILEPROC or M_FILEGLOB zone
3244 * *resultfd (modified) Returned fileproc pointer
3245 * *resultfd (modified) Returned fd
3247 * Locks: If the parameter 'locked' is zero, this function takes and
3248 * drops the proc_fdlock; if non-zero, the caller must hold the
3251 * Notes: If you intend to use a non-zero 'locked' parameter, use the
3252 * utility function falloc() instead.
3254 * This function takes separate process and context arguments
3255 * solely to support kern_exec.c; otherwise, it would take
3256 * neither, and use the vfs_context_current() routine internally.
3259 falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
3260 vfs_context_t ctx
, int locked
)
3262 struct fileproc
*fp
, *fq
;
3263 struct fileglob
*fg
;
3268 if ( (error
= fdalloc(p
, 0, &nfd
)) ) {
3273 if (nfiles
>= maxfiles
) {
3280 error
= mac_file_check_create(proc_ucred(p
));
3289 * Allocate a new file descriptor.
3290 * If the process has file descriptor zero open, add to the list
3291 * of open files at that point, otherwise put it at the front of
3292 * the list of open files.
3296 MALLOC_ZONE(fp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
3302 MALLOC_ZONE(fg
, struct fileglob
*, sizeof(struct fileglob
), M_FILEGLOB
, M_WAITOK
);
3304 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
3309 bzero(fp
, sizeof(struct fileproc
));
3310 bzero(fg
, sizeof(struct fileglob
));
3311 lck_mtx_init(&fg
->fg_lock
, file_lck_grp
, file_lck_attr
);
3317 mac_file_label_init(fg
);
3320 kauth_cred_ref(ctx
->vc_ucred
);
3324 fp
->f_cred
= ctx
->vc_ucred
;
3327 mac_file_label_associate(fp
->f_cred
, fg
);
3330 lck_mtx_lock_spin(file_flist_lock
);
3334 if ( (fq
= p
->p_fd
->fd_ofiles
[0]) ) {
3335 LIST_INSERT_AFTER(fq
->f_fglob
, fg
, f_list
);
3337 LIST_INSERT_HEAD(&filehead
, fg
, f_list
);
3339 lck_mtx_unlock(file_flist_lock
);
3341 p
->p_fd
->fd_ofiles
[nfd
] = fp
;
3358 * Description: Free a file structure; drop the global open file count, and
3359 * drop the credential reference, if the fileglob has one, and
3360 * destroy the instance mutex before freeing
3362 * Parameters: fg Pointer to fileglob to be
3368 fg_free(struct fileglob
*fg
)
3370 lck_mtx_lock_spin(file_flist_lock
);
3371 LIST_REMOVE(fg
, f_list
);
3373 lck_mtx_unlock(file_flist_lock
);
3375 if (IS_VALID_CRED(fg
->fg_cred
)) {
3376 kauth_cred_unref(&fg
->fg_cred
);
3378 lck_mtx_destroy(&fg
->fg_lock
, file_lck_grp
);
3381 mac_file_label_destroy(fg
);
3383 FREE_ZONE(fg
, sizeof *fg
, M_FILEGLOB
);
3390 * Description: Perform close-on-exec processing for all files in a process
3391 * that are either marked as close-on-exec, or which were in the
3392 * process of being opened at the time of the execve
3394 * Parameters: p Pointer to process calling
3399 * Locks: This function internally takes and drops proc_fdlock()
3401 * Notes: This function drops and retakes the kernel funnel; this is
3402 * inherently unsafe, since another thread may have the
3405 * XXX: We should likely reverse the lock and funnel drop/acquire
3406 * order to avoid the small race window; it's also possible that
3407 * if the program doing the exec has an outstanding listen socket
3408 * and a network connection is completed asyncrhonously that we
3409 * will end up with a "ghost" socket reference in the new process.
3411 * This needs reworking to make it safe to remove the funnel from
3412 * the execve and posix_spawn system calls.
3417 struct filedesc
*fdp
= p
->p_fd
;
3418 int i
= fdp
->fd_lastfile
;
3419 struct fileproc
*fp
;
3424 fp
= fdp
->fd_ofiles
[i
];
3426 ((fdp
->fd_ofileflags
[i
] & (UF_RESERVED
|UF_EXCLOSE
)) == UF_EXCLOSE
)
3428 || (fp
&& mac_file_check_inherit(proc_ucred(p
), fp
->f_fglob
))
3431 if (i
< fdp
->fd_knlistsize
)
3432 knote_fdclose(p
, i
);
3433 procfdtbl_clearfd(p
, i
);
3434 if (i
== fdp
->fd_lastfile
&& i
> 0)
3436 if (i
< fdp
->fd_freefile
)
3437 fdp
->fd_freefile
= i
;
3438 closef_locked(fp
, fp
->f_fglob
, p
);
3439 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
3450 * Description: Copy a filedesc structure. This is normally used as part of
3451 * forkproc() when forking a new process, to copy the per process
3452 * open file table over to the new process.
3454 * Parameters: p Process whose open file table
3455 * is to be copied (parent)
3456 * uth_cdir Per thread current working
3457 * cirectory, or NULL
3459 * Returns: NULL Copy failed
3460 * !NULL Pointer to new struct filedesc
3462 * Locks: This function internally takes and drops proc_fdlock()
3464 * Notes: Files are copied directly, ignoring the new resource limits
3465 * for the process that's being copied into. Since the descriptor
3466 * references are just additional references, this does not count
3467 * against the number of open files on the system.
3469 * The struct filedesc includes the current working directory,
3470 * and the current root directory, if the process is chroot'ed.
3472 * If the exec was called by a thread using a per thread current
3473 * working directory, we inherit the working directory from the
3474 * thread making the call, rather than from the process.
3476 * In the case of a failure to obtain a reference, for most cases,
3477 * the file entry will be silently droppped. There's an exception
3478 * for the case of a chroot dir, since a failure to to obtain a
3479 * reference there would constitute an "escape" from the chroot
3480 * environment, which must not be allowed. In that case, we will
3481 * deny the execve() operation, rather than allowing the escape.
3484 fdcopy(proc_t p
, vnode_t uth_cdir
)
3486 struct filedesc
*newfdp
, *fdp
= p
->p_fd
;
3488 struct fileproc
*ofp
, *fp
;
3491 MALLOC_ZONE(newfdp
, struct filedesc
*,
3492 sizeof(*newfdp
), M_FILEDESC
, M_WAITOK
);
3499 * the FD_CHROOT flag will be inherited via this copy
3501 (void) memcpy(newfdp
, fdp
, sizeof(*newfdp
));
3504 * If we are running with per-thread current working directories,
3505 * inherit the new current working directory from the current thread
3506 * instead, before we take our references.
3508 if (uth_cdir
!= NULLVP
)
3509 newfdp
->fd_cdir
= uth_cdir
;
3512 * For both fd_cdir and fd_rdir make sure we get
3513 * a valid reference... if we can't, than set
3514 * set the pointer(s) to NULL in the child... this
3515 * will keep us from using a non-referenced vp
3516 * and allows us to do the vnode_rele only on
3517 * a properly referenced vp
3519 if ( (v_dir
= newfdp
->fd_cdir
) ) {
3520 if (vnode_getwithref(v_dir
) == 0) {
3521 if ( (vnode_ref(v_dir
)) )
3522 newfdp
->fd_cdir
= NULL
;
3525 newfdp
->fd_cdir
= NULL
;
3527 if (newfdp
->fd_cdir
== NULL
&& fdp
->fd_cdir
) {
3529 * we couldn't get a new reference on
3530 * the current working directory being
3531 * inherited... we might as well drop
3532 * our reference from the parent also
3533 * since the vnode has gone DEAD making
3534 * it useless... by dropping it we'll
3535 * be that much closer to recyling it
3537 vnode_rele(fdp
->fd_cdir
);
3538 fdp
->fd_cdir
= NULL
;
3541 if ( (v_dir
= newfdp
->fd_rdir
) ) {
3542 if (vnode_getwithref(v_dir
) == 0) {
3543 if ( (vnode_ref(v_dir
)) )
3544 newfdp
->fd_rdir
= NULL
;
3547 newfdp
->fd_rdir
= NULL
;
3550 /* Coming from a chroot environment and unable to get a reference... */
3551 if (newfdp
->fd_rdir
== NULL
&& fdp
->fd_rdir
) {
3553 * We couldn't get a new reference on
3554 * the chroot directory being
3555 * inherited... this is fatal, since
3556 * otherwise it would constitute an
3557 * escape from a chroot environment by
3560 if (newfdp
->fd_cdir
)
3561 vnode_rele(newfdp
->fd_cdir
);
3562 FREE_ZONE(newfdp
, sizeof *newfdp
, M_FILEDESC
);
3565 newfdp
->fd_refcnt
= 1;
3568 * If the number of open files fits in the internal arrays
3569 * of the open file structure, use them, otherwise allocate
3570 * additional memory for the number of descriptors currently
3573 if (newfdp
->fd_lastfile
< NDFILE
)
3577 * Compute the smallest multiple of NDEXTENT needed
3578 * for the file descriptors currently in use,
3579 * allowing the table to shrink.
3581 i
= newfdp
->fd_nfiles
;
3582 while (i
> 2 * NDEXTENT
&& i
> newfdp
->fd_lastfile
* 2)
3587 MALLOC_ZONE(newfdp
->fd_ofiles
, struct fileproc
**,
3588 i
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
3589 if (newfdp
->fd_ofiles
== NULL
) {
3590 if (newfdp
->fd_cdir
)
3591 vnode_rele(newfdp
->fd_cdir
);
3592 if (newfdp
->fd_rdir
)
3593 vnode_rele(newfdp
->fd_rdir
);
3595 FREE_ZONE(newfdp
, sizeof(*newfdp
), M_FILEDESC
);
3598 (void) memset(newfdp
->fd_ofiles
, 0, i
* OFILESIZE
);
3601 newfdp
->fd_ofileflags
= (char *) &newfdp
->fd_ofiles
[i
];
3602 newfdp
->fd_nfiles
= i
;
3604 if (fdp
->fd_nfiles
> 0) {
3605 struct fileproc
**fpp
;
3608 (void) memcpy(newfdp
->fd_ofiles
, fdp
->fd_ofiles
,
3609 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofiles
));
3610 (void) memcpy(newfdp
->fd_ofileflags
, fdp
->fd_ofileflags
,
3611 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofileflags
));
3614 * kq descriptors cannot be copied.
3616 if (newfdp
->fd_knlistsize
!= -1) {
3617 fpp
= &newfdp
->fd_ofiles
[newfdp
->fd_lastfile
];
3618 for (i
= newfdp
->fd_lastfile
; i
>= 0; i
--, fpp
--) {
3619 if (*fpp
!= NULL
&& (*fpp
)->f_type
== DTYPE_KQUEUE
) {
3621 if (i
< newfdp
->fd_freefile
)
3622 newfdp
->fd_freefile
= i
;
3624 if (*fpp
== NULL
&& i
== newfdp
->fd_lastfile
&& i
> 0)
3625 newfdp
->fd_lastfile
--;
3627 newfdp
->fd_knlist
= NULL
;
3628 newfdp
->fd_knlistsize
= -1;
3629 newfdp
->fd_knhash
= NULL
;
3630 newfdp
->fd_knhashmask
= 0;
3632 fpp
= newfdp
->fd_ofiles
;
3633 flags
= newfdp
->fd_ofileflags
;
3635 for (i
= newfdp
->fd_lastfile
+ 1; --i
>= 0; fpp
++, flags
++)
3636 if ((ofp
= *fpp
) != NULL
&& !(*flags
& UF_RESERVED
)) {
3637 MALLOC_ZONE(fp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
3640 * XXX no room to copy, unable to
3641 * XXX safely unwind state at present
3645 bzero(fp
, sizeof(struct fileproc
));
3646 fp
->f_flags
= ofp
->f_flags
;
3647 //fp->f_iocount = ofp->f_iocount;
3649 fp
->f_fglob
= ofp
->f_fglob
;
3654 if (i
< newfdp
->fd_freefile
)
3655 newfdp
->fd_freefile
= i
;
3669 * Description: Release a filedesc (per process open file table) structure;
3670 * this is done on process exit(), or from forkproc_free() if
3671 * the fork fails for some reason subsequent to a successful
3674 * Parameters: p Pointer to process going away
3678 * Locks: This function internally takes and drops proc_fdlock()
3683 struct filedesc
*fdp
;
3684 struct fileproc
*fp
;
3689 /* Certain daemons might not have file descriptors */
3692 if ((fdp
== NULL
) || (--fdp
->fd_refcnt
> 0)) {
3696 if (fdp
->fd_refcnt
== 0xffff)
3697 panic("fdfree: bad fd_refcnt");
3699 /* Last reference: the structure can't change out from under us */
3701 if (fdp
->fd_nfiles
> 0 && fdp
->fd_ofiles
) {
3702 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
3703 if ((fp
= fdp
->fd_ofiles
[i
]) != NULL
) {
3705 if (fdp
->fd_ofileflags
[i
] & UF_RESERVED
)
3706 panic("fdfree: found fp with UF_RESERVED\n");
3708 /* closef drops the iocount ... */
3709 if ((fp
->f_flags
& FP_INCHRREAD
) != 0)
3711 procfdtbl_reservefd(p
, i
);
3713 if (i
< fdp
->fd_knlistsize
)
3714 knote_fdclose(p
, i
);
3715 if (fp
->f_flags
& FP_WAITEVENT
)
3716 (void)waitevent_close(p
, fp
);
3717 (void) closef_locked(fp
, fp
->f_fglob
, p
);
3718 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
3721 FREE_ZONE(fdp
->fd_ofiles
, fdp
->fd_nfiles
* OFILESIZE
, M_OFILETABL
);
3722 fdp
->fd_ofiles
= NULL
;
3729 vnode_rele(fdp
->fd_cdir
);
3731 vnode_rele(fdp
->fd_rdir
);
3733 proc_fdlock_spin(p
);
3738 FREE(fdp
->fd_knlist
, M_KQUEUE
);
3740 FREE(fdp
->fd_knhash
, M_KQUEUE
);
3742 FREE_ZONE(fdp
, sizeof(*fdp
), M_FILEDESC
);
3749 * Description: Called on last open instance for a fileglob for a file being
3752 * Parameters: fp Pointer to fileproc for fd
3753 * fg Pointer to fileglob for fd
3754 * p Pointer to proc structure
3756 * Returns: 0 Success
3757 * <fo_close>:??? Anything returnable by a per-fileops
3760 * Note: fp can only be non-NULL if p is also non-NULL. If p is NULL,
3761 * then fg must eith be locked (FHASLOCK) or must not have a
3762 * type of DTYPE_VNODE.
3764 * On return, the fg is freed.
3766 * This function may block draining output to a character
3767 * device on last close of that device.
3770 closef_finish(struct fileproc
*fp
, struct fileglob
*fg
, proc_t p
, vfs_context_t ctx
)
3775 /* fg_ops completed initialization? */
3777 error
= fo_close(fg
, ctx
);
3781 /* if fp is non-NULL, drain it out */
3782 if (((fp
!= (struct fileproc
*)0) && ((fp
->f_flags
& FP_INCHRREAD
) != 0))) {
3783 proc_fdlock_spin(p
);
3784 if ( ((fp
->f_flags
& FP_INCHRREAD
) != 0) ) {
3785 fileproc_drain(p
, fp
);
3797 * Description: Internal form of closef; called with proc_fdlock held
3799 * Parameters: fp Pointer to fileproc for fd
3800 * fg Pointer to fileglob for fd
3801 * p Pointer to proc structure
3803 * Returns: 0 Success
3804 * closef_finish:??? Anything returnable by a per-fileops
3807 * Note: Decrements reference count on file structure; if this was the
3808 * last reference, then closef_finish() is called
3810 * p and fp are allowed to be NULL when closing a file that was
3811 * being passed in a message (but only if we are called when this
3812 * is NOT the last reference).
3815 closef_locked(struct fileproc
*fp
, struct fileglob
*fg
, proc_t p
)
3819 struct vfs_context context
;
3826 /* Set up context with cred stashed in fg */
3827 if (p
== current_proc())
3828 context
.vc_thread
= current_thread();
3830 context
.vc_thread
= NULL
;
3831 context
.vc_ucred
= fg
->fg_cred
;
3834 * POSIX record locking dictates that any close releases ALL
3835 * locks owned by this process. This is handled by setting
3836 * a flag in the unlock to free ONLY locks obeying POSIX
3837 * semantics, and not to free BSD-style file locks.
3838 * If the descriptor was in a message, POSIX-style locks
3839 * aren't passed with the descriptor.
3841 if (p
&& (p
->p_ladvflag
& P_LADVLOCK
) && fg
->fg_type
== DTYPE_VNODE
) {
3844 lf
.l_whence
= SEEK_SET
;
3847 lf
.l_type
= F_UNLCK
;
3848 vp
= (struct vnode
*)fg
->fg_data
;
3850 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
3851 (void) VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &lf
, F_POSIX
, &context
);
3852 (void)vnode_put(vp
);
3856 lck_mtx_lock_spin(&fg
->fg_lock
);
3859 if (fg
->fg_count
> 0) {
3860 lck_mtx_unlock(&fg
->fg_lock
);
3864 if (fg
->fg_count
!= 0)
3865 panic("fg %p: being freed with bad fg_count (%d)", fg
, fg
->fg_count
);
3868 if (fp
&& (fp
->f_flags
& FP_WRITTEN
))
3869 fg
->fg_flag
|= FWASWRITTEN
;
3871 fg
->fg_lflags
|= FG_TERM
;
3872 lck_mtx_unlock(&fg
->fg_lock
);
3875 error
= closef_finish(fp
, fg
, p
, &context
);
3882 /* sleep address to permit wakeup of select by fileproc_drain() */
3889 * Description: Drain out pending I/O operations
3891 * Parameters: p Process closing this file
3892 * fp fileproc struct for the open
3893 * instance on the file
3897 * Locks: Assumes the caller holds the proc_fdlock
3899 * Notes: For character devices, this occurs on the last close of the
3900 * device; for all other file descriptos, this occurs on each
3901 * close to prevent fd's from being closed out from under
3902 * operations currently in progress and blocked
3904 * See Also: file_vnode(), file_socket(), file_drop(), and the cautions
3905 * regarding their use and interaction with this function.
3908 fileproc_drain(proc_t p
, struct fileproc
* fp
)
3910 struct vfs_context context
;
3912 context
.vc_thread
= proc_thread(p
); /* XXX */
3913 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
3915 fp
->f_iocount
-- ; /* (the one the close holds) */
3917 while (fp
->f_iocount
) {
3919 lck_mtx_convert_spin(&p
->p_fdmlock
);
3921 if (fp
->f_fglob
->fg_ops
->fo_drain
) {
3922 (*fp
->f_fglob
->fg_ops
->fo_drain
)(fp
, &context
);
3924 if (((fp
->f_flags
& FP_INSELECT
)== FP_INSELECT
)) {
3925 wait_queue_wakeup_all((wait_queue_t
)fp
->f_waddr
, &selwait
, THREAD_INTERRUPTED
);
3927 p
->p_fpdrainwait
= 1;
3929 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
, "fpdrain", NULL
);
3938 * Description: Release the fd and free the fileproc associated with the fd
3939 * in the per process open file table of the specified process;
3940 * these values must correspond.
3942 * Parameters: p Process containing fd
3943 * fd fd to be released
3944 * fp fileproc to be freed
3946 * Returns: 0 Success
3948 * Notes: XXX function should be void - no one interprets the returns
3952 fp_free(proc_t p
, int fd
, struct fileproc
* fp
)
3954 proc_fdlock_spin(p
);
3958 fg_free(fp
->f_fglob
);
3959 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
3967 * Description: Apply an advisory lock on a file descriptor.
3969 * Parameters: p Process making request
3970 * uap->fd fd on which the lock is to be
3972 * uap->how (Un)Lock bits, including type
3973 * retval Pointer to the call return area
3975 * Returns: 0 Success
3976 * fp_getfvp:EBADF Bad file descriptor
3977 * fp_getfvp:ENOTSUP fd does not refer to a vnode
3978 * vnode_getwithref:???
3982 * *retval (modified) Size of dtable
3984 * Notes: Just attempt to get a record lock of the requested type on
3985 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
3988 flock(proc_t p
, struct flock_args
*uap
, __unused register_t
*retval
)
3992 struct fileproc
*fp
;
3995 vfs_context_t ctx
= vfs_context_current();
3998 AUDIT_ARG(fd
, uap
->fd
);
3999 if ( (error
= fp_getfvp(p
, fd
, &fp
, &vp
)) ) {
4002 if ( (error
= vnode_getwithref(vp
)) ) {
4005 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
4007 lf
.l_whence
= SEEK_SET
;
4010 if (how
& LOCK_UN
) {
4011 lf
.l_type
= F_UNLCK
;
4012 fp
->f_flag
&= ~FHASLOCK
;
4013 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_UNLCK
, &lf
, F_FLOCK
, ctx
);
4017 lf
.l_type
= F_WRLCK
;
4018 else if (how
& LOCK_SH
)
4019 lf
.l_type
= F_RDLCK
;
4025 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
, F_SETLK
, &lf
);
4029 fp
->f_flag
|= FHASLOCK
;
4030 if (how
& LOCK_NB
) {
4031 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, F_FLOCK
, ctx
);
4034 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, F_FLOCK
|F_WAIT
, ctx
);
4036 (void)vnode_put(vp
);
4038 fp_drop(p
, fd
, fp
, 0);
4046 * Description: Duplicate the specified descriptor to a free descriptor;
4047 * this is the second half of fdopen(), above.
4049 * Parameters: fdp filedesc pointer to fill in
4051 * dfd fd to dup from
4052 * mode mode to set on new fd
4053 * error command code
4055 * Returns: 0 Success
4056 * EBADF Source fd is bad
4057 * EACCES Requested mode not allowed
4058 * !0 'error', if not ENODEV or
4061 * Notes: XXX This is not thread safe; see fdopen() above
4064 dupfdopen(struct filedesc
*fdp
, int indx
, int dfd
, int mode
, int error
)
4066 struct fileproc
*wfp
;
4067 struct fileproc
*fp
;
4071 proc_t p
= current_proc();
4074 * If the to-be-dup'd fd number is greater than the allowed number
4075 * of file descriptors, or the fd to be dup'd has already been
4076 * closed, reject. Note, check for new == old is necessary as
4077 * falloc could allocate an already closed to-be-dup'd descriptor
4078 * as the new descriptor.
4082 fp
= fdp
->fd_ofiles
[indx
];
4083 if (dfd
< 0 || dfd
>= fdp
->fd_nfiles
||
4084 (wfp
= fdp
->fd_ofiles
[dfd
]) == NULL
|| wfp
== fp
||
4085 (fdp
->fd_ofileflags
[dfd
] & UF_RESERVED
)) {
4091 myerror
= mac_file_check_dup(proc_ucred(p
), wfp
->f_fglob
, dfd
);
4098 * There are two cases of interest here.
4100 * For ENODEV simply dup (dfd) to file descriptor
4101 * (indx) and return.
4103 * For ENXIO steal away the file structure from (dfd) and
4104 * store it in (indx). (dfd) is effectively closed by
4107 * Any other error code is just returned.
4112 * Check that the mode the file is being opened for is a
4113 * subset of the mode of the existing descriptor.
4115 if (((mode
& (FREAD
|FWRITE
)) | wfp
->f_flag
) != wfp
->f_flag
) {
4119 if (indx
> fdp
->fd_lastfile
)
4120 fdp
->fd_lastfile
= indx
;
4124 fg_free(fp
->f_fglob
);
4125 fp
->f_fglob
= wfp
->f_fglob
;
4127 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
];
4143 * Description: Add a reference to a fileglob by fileproc
4145 * Parameters: fp fileproc containing fileglob
4150 * Notes: XXX Should use OSAddAtomic?
4153 fg_ref(struct fileproc
* fp
)
4155 struct fileglob
*fg
;
4159 lck_mtx_lock_spin(&fg
->fg_lock
);
4162 if ((fp
->f_flags
& ~((unsigned int)FP_VALID_FLAGS
)) != 0)
4163 panic("fg_ref: invalid bits on fp%x\n", (unsigned int)fp
);
4165 if (fg
->fg_count
== 0)
4166 panic("fg_ref: adding fgcount to zeroed fg :fp %x, fg%x\n ", (unsigned int)fp
, (unsigned int)fg
);
4169 lck_mtx_unlock(&fg
->fg_lock
);
4176 * Description: Remove a reference to a fileglob by fileproc
4178 * Parameters: fp fileproc containing fileglob
4183 * Notes: XXX Should use OSAddAtomic?
4186 fg_drop(struct fileproc
* fp
)
4188 struct fileglob
*fg
;
4191 lck_mtx_lock_spin(&fg
->fg_lock
);
4193 lck_mtx_unlock(&fg
->fg_lock
);
4200 * Description: Insert fileglob onto message queue
4202 * Parameters: fg Fileglob pointer to insert
4206 * Locks: Takes and drops fg_lock, potentially many times
4209 fg_insertuipc(struct fileglob
* fg
)
4213 lck_mtx_lock_spin(&fg
->fg_lock
);
4215 while (fg
->fg_lflags
& FG_RMMSGQ
) {
4216 lck_mtx_convert_spin(&fg
->fg_lock
);
4218 fg
->fg_lflags
|= FG_WRMMSGQ
;
4219 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_insertuipc", NULL
);
4224 if (fg
->fg_msgcount
== 1) {
4225 fg
->fg_lflags
|= FG_INSMSGQ
;
4228 lck_mtx_unlock(&fg
->fg_lock
);
4231 lck_mtx_lock_spin(uipc_lock
);
4233 LIST_INSERT_HEAD(&fmsghead
, fg
, f_msglist
);
4234 lck_mtx_unlock(uipc_lock
);
4235 lck_mtx_lock(&fg
->fg_lock
);
4236 fg
->fg_lflags
&= ~FG_INSMSGQ
;
4237 if (fg
->fg_lflags
& FG_WINSMSGQ
) {
4238 fg
->fg_lflags
&= ~FG_WINSMSGQ
;
4239 wakeup(&fg
->fg_lflags
);
4241 lck_mtx_unlock(&fg
->fg_lock
);
4250 * Description: Remove fileglob from message queue
4252 * Parameters: fg Fileglob pointer to remove
4256 * Locks: Takes and drops fg_lock, potentially many times
4259 fg_removeuipc(struct fileglob
* fg
)
4263 lck_mtx_lock_spin(&fg
->fg_lock
);
4264 while (fg
->fg_lflags
& FG_INSMSGQ
) {
4265 lck_mtx_convert_spin(&fg
->fg_lock
);
4267 fg
->fg_lflags
|= FG_WINSMSGQ
;
4268 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_removeuipc", NULL
);
4271 if (fg
->fg_msgcount
== 0) {
4272 fg
->fg_lflags
|= FG_RMMSGQ
;
4275 lck_mtx_unlock(&fg
->fg_lock
);
4278 lck_mtx_lock_spin(uipc_lock
);
4280 LIST_REMOVE(fg
, f_msglist
);
4281 lck_mtx_unlock(uipc_lock
);
4282 lck_mtx_lock(&fg
->fg_lock
);
4283 fg
->fg_lflags
&= ~FG_RMMSGQ
;
4284 if (fg
->fg_lflags
& FG_WRMMSGQ
) {
4285 fg
->fg_lflags
&= ~FG_WRMMSGQ
;
4286 wakeup(&fg
->fg_lflags
);
4288 lck_mtx_unlock(&fg
->fg_lock
);
4296 * Description: Generic fileops read indirected through the fileops pointer
4297 * in the fileproc structure
4299 * Parameters: fp fileproc structure pointer
4300 * uio user I/O structure pointer
4302 * ctx VFS context for operation
4304 * Returns: 0 Success
4305 * !0 Errno from read
4308 fo_read(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
4310 return ((*fp
->f_ops
->fo_read
)(fp
, uio
, flags
, ctx
));
4317 * Description: Generic fileops write indirected through the fileops pointer
4318 * in the fileproc structure
4320 * Parameters: fp fileproc structure pointer
4321 * uio user I/O structure pointer
4323 * ctx VFS context for operation
4325 * Returns: 0 Success
4326 * !0 Errno from write
4329 fo_write(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
4331 return((*fp
->f_ops
->fo_write
)(fp
, uio
, flags
, ctx
));
4338 * Description: Generic fileops ioctl indirected through the fileops pointer
4339 * in the fileproc structure
4341 * Parameters: fp fileproc structure pointer
4343 * data pointer to internalized copy
4344 * of user space ioctl command
4345 * parameter data in kernel space
4346 * ctx VFS context for operation
4348 * Returns: 0 Success
4349 * !0 Errno from ioctl
4351 * Locks: The caller is assumed to have held the proc_fdlock; this
4352 * function releases and reacquires this lock. If the caller
4353 * accesses data protected by this lock prior to calling this
4354 * function, it will need to revalidate/reacquire any cached
4355 * protected data obtained prior to the call.
4358 fo_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
, vfs_context_t ctx
)
4362 proc_fdunlock(vfs_context_proc(ctx
));
4363 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, com
, data
, ctx
);
4364 proc_fdlock(vfs_context_proc(ctx
));
4372 * Description: Generic fileops select indirected through the fileops pointer
4373 * in the fileproc structure
4375 * Parameters: fp fileproc structure pointer
4376 * which select which
4377 * wql pointer to wait queue list
4378 * ctx VFS context for operation
4380 * Returns: 0 Success
4381 * !0 Errno from select
4384 fo_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
4386 return((*fp
->f_ops
->fo_select
)(fp
, which
, wql
, ctx
));
4393 * Description: Generic fileops close indirected through the fileops pointer
4394 * in the fileproc structure
4396 * Parameters: fp fileproc structure pointer for
4398 * ctx VFS context for operation
4400 * Returns: 0 Success
4401 * !0 Errno from close
4404 fo_close(struct fileglob
*fg
, vfs_context_t ctx
)
4406 return((*fg
->fg_ops
->fo_close
)(fg
, ctx
));
4413 * Description: Generic fileops kqueue filter indirected through the fileops
4414 * pointer in the fileproc structure
4416 * Parameters: fp fileproc structure pointer
4417 * kn pointer to knote to filter on
4418 * ctx VFS context for operation
4420 * Returns: 0 Success
4421 * !0 Errno from kqueue filter
4424 fo_kqfilter(struct fileproc
*fp
, struct knote
*kn
, vfs_context_t ctx
)
4426 return ((*fp
->f_ops
->fo_kqfilter
)(fp
, kn
, ctx
));