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>
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
;
145 #define f_flag f_fglob->fg_flag
146 #define f_type f_fglob->fg_type
147 #define f_msgcount f_fglob->fg_msgcount
148 #define f_cred f_fglob->fg_cred
149 #define f_ops f_fglob->fg_ops
150 #define f_offset f_fglob->fg_offset
151 #define f_data f_fglob->fg_data
153 * Descriptor management.
155 struct filelist filehead
; /* head of list of open files */
156 struct fmsglist fmsghead
; /* head of list of open files */
157 struct fmsglist fmsg_ithead
; /* head of list of open files */
158 int nfiles
; /* actual number of open files */
161 lck_grp_attr_t
* file_lck_grp_attr
;
162 lck_grp_t
* file_lck_grp
;
163 lck_attr_t
* file_lck_attr
;
165 lck_mtx_t
* uipc_lock
;
166 lck_mtx_t
* file_flist_lock
;
172 * Description: Initialize the file lock group and the uipc and flist locks
178 * Notes: Called at system startup from bsd_init().
183 /* allocate file lock group attribute and group */
184 file_lck_grp_attr
= lck_grp_attr_alloc_init();
186 file_lck_grp
= lck_grp_alloc_init("file", file_lck_grp_attr
);
188 /* Allocate file lock attribute */
189 file_lck_attr
= lck_attr_alloc_init();
191 uipc_lock
= lck_mtx_alloc_init(file_lck_grp
, file_lck_attr
);
192 file_flist_lock
= lck_mtx_alloc_init(file_lck_grp
, file_lck_attr
);
197 * proc_fdlock, proc_fdlock_spin
199 * Description: Lock to control access to the per process struct fileproc
200 * and struct filedesc
202 * Parameters: p Process to take the lock on
206 * Notes: The lock is initialized in forkproc() and destroyed in
207 * reap_child_process().
210 proc_fdlock(proc_t p
)
212 lck_mtx_lock(&p
->p_fdmlock
);
216 proc_fdlock_spin(proc_t p
)
218 lck_mtx_lock_spin(&p
->p_fdmlock
);
222 proc_fdlock_assert(proc_t p
, int assertflags
)
224 lck_mtx_assert(&p
->p_fdmlock
, assertflags
);
231 * Description: Unlock the lock previously locked by a call to proc_fdlock()
233 * Parameters: p Process to drop the lock on
238 proc_fdunlock(proc_t p
)
240 lck_mtx_unlock(&p
->p_fdmlock
);
245 * System calls on descriptors.
252 * Description: Returns the per process maximum size of the descriptor table
254 * Parameters: p Process being queried
255 * retval Pointer to the call return area
260 * *retval (modified) Size of dtable
263 getdtablesize(proc_t p
, __unused
struct getdtablesize_args
*uap
, register_t
*retval
)
266 *retval
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
274 procfdtbl_reservefd(struct proc
* p
, int fd
)
276 p
->p_fd
->fd_ofiles
[fd
] = NULL
;
277 p
->p_fd
->fd_ofileflags
[fd
] |= UF_RESERVED
;
281 procfdtbl_markclosefd(struct proc
* p
, int fd
)
283 p
->p_fd
->fd_ofileflags
[fd
] |= (UF_RESERVED
| UF_CLOSING
);
287 procfdtbl_releasefd(struct proc
* p
, int fd
, struct fileproc
* fp
)
290 p
->p_fd
->fd_ofiles
[fd
] = fp
;
291 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESERVED
;
292 if ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
) == UF_RESVWAIT
) {
293 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESVWAIT
;
299 procfdtbl_waitfd(struct proc
* p
, int fd
)
301 p
->p_fd
->fd_ofileflags
[fd
] |= UF_RESVWAIT
;
302 msleep(&p
->p_fd
, &p
->p_fdmlock
, PRIBIO
, "ftbl_waitfd", NULL
);
307 procfdtbl_clearfd(struct proc
* p
, int fd
)
311 waiting
= (p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
);
312 p
->p_fd
->fd_ofiles
[fd
] = NULL
;
313 p
->p_fd
->fd_ofileflags
[fd
] = 0;
314 if ( waiting
== UF_RESVWAIT
) {
322 * Description: Inline utility function to free an fd in a filedesc
324 * Parameters: fdp Pointer to filedesc fd lies in
326 * reserv fd should be reserved
330 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
334 _fdrelse(struct proc
* p
, int fd
)
336 struct filedesc
*fdp
= p
->p_fd
;
339 if (fd
< fdp
->fd_freefile
)
340 fdp
->fd_freefile
= fd
;
342 if (fd
> fdp
->fd_lastfile
)
343 panic("fdrelse: fd_lastfile inconsistent");
345 procfdtbl_clearfd(p
, fd
);
347 while ((nfd
= fdp
->fd_lastfile
) > 0 &&
348 fdp
->fd_ofiles
[nfd
] == NULL
&&
349 !(fdp
->fd_ofileflags
[nfd
] & UF_RESERVED
))
357 * Description: Duplicate a file descriptor.
359 * Parameters: p Process performing the dup
360 * uap->fd The fd to dup
361 * retval Pointer to the call return area
367 * *retval (modified) The new descriptor
370 dup(proc_t p
, struct dup_args
*uap
, register_t
*retval
)
372 struct filedesc
*fdp
= p
->p_fd
;
378 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
382 if ( (error
= fdalloc(p
, 0, &new)) ) {
383 fp_drop(p
, old
, fp
, 1);
387 error
= finishdup(p
, fdp
, old
, new, retval
);
388 fp_drop(p
, old
, fp
, 1);
398 * Description: Duplicate a file descriptor to a particular value.
400 * Parameters: p Process performing the dup
401 * uap->fd The fd to dup
402 * uap->to The fd to dup it to
403 * retval Pointer to the call return area
409 * *retval (modified) The new descriptor
412 dup2(proc_t p
, struct dup2_args
*uap
, register_t
*retval
)
414 struct filedesc
*fdp
= p
->p_fd
;
415 int old
= uap
->from
, new = uap
->to
;
417 struct fileproc
*fp
, *nfp
;
422 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
427 (rlim_t
)new >= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
429 fp_drop(p
, old
, fp
, 1);
434 fp_drop(p
, old
, fp
, 1);
439 if (new < 0 || new >= fdp
->fd_nfiles
) {
440 if ( (error
= fdalloc(p
, new, &i
)) ) {
441 fp_drop(p
, old
, fp
, 1);
451 while ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == UF_RESERVED
) {
452 fp_drop(p
, old
, fp
, 1);
453 procfdtbl_waitfd(p
, new);
455 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
460 if ((fdp
->fd_ofiles
[new] != NULL
) && ((error
= fp_lookup(p
, new, &nfp
, 1)) == 0)) {
461 fp_drop(p
, old
, fp
, 1);
462 (void)close_internal_locked(p
, new, nfp
, FD_DUP2RESV
);
464 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
466 procfdtbl_clearfd(p
, new);
470 if (fdp
->fd_ofiles
[new] != NULL
)
471 panic("dup2: unable to get ref on a fileproc %d\n", new);
473 procfdtbl_reservefd(p
, new);
477 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
482 if (fdp
->fd_ofiles
[new] != 0)
483 panic("dup2-1: overwriting fd_ofiles with new %d\n", new);
484 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0)
485 panic("dup2-1: unreserved fileflags with new %d\n", new);
487 error
= finishdup(p
, fdp
, old
, new, retval
);
488 fp_drop(p
, old
, fp
, 1);
498 * Description: The file control system call.
500 * Parameters: p Process performing the fcntl
501 * uap->fd The fd to operate against
502 * uap->cmd The command to perform
503 * uap->arg Pointer to the command argument
504 * retval Pointer to the call return area
507 * !0 Errno (see fcntl_nocancel)
510 * *retval (modified) fcntl return value (if any)
512 * Notes: This system call differs from fcntl_nocancel() in that it
513 * tests for cancellation prior to performing a potentially
514 * blocking operation.
517 fcntl(proc_t p
, struct fcntl_args
*uap
, register_t
*retval
)
519 __pthread_testcancel(1);
520 return(fcntl_nocancel(p
, (struct fcntl_nocancel_args
*)uap
, retval
));
527 * Description: A non-cancel-testing file control system call.
529 * Parameters: p Process performing the fcntl
530 * uap->fd The fd to operate against
531 * uap->cmd The command to perform
532 * uap->arg Pointer to the command argument
533 * retval Pointer to the call return area
537 * fp_lookup:EBADF Bad file descriptor
549 * vnode_getwithref:???
556 * vnode_getwithref:???
563 * vnode_getwithref:???
565 * [F_SETSIZE,F_RDADVISE]
568 * vnode_getwithref:???
569 * [F_RDAHEAD,F_NOCACHE]
571 * vnode_getwithref:???
575 * *retval (modified) fcntl return value (if any)
578 fcntl_nocancel(proc_t p
, struct fcntl_nocancel_args
*uap
, register_t
*retval
)
581 struct filedesc
*fdp
= p
->p_fd
;
584 struct vnode
*vp
= NULLVP
; /* for AUDIT_ARG() at end */
585 int i
, tmp
, error
, error2
, flg
= F_POSIX
;
587 struct vfs_context context
;
591 int devBlockSize
= 0;
595 AUDIT_ARG(fd
, uap
->fd
);
596 AUDIT_ARG(cmd
, uap
->cmd
);
599 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
603 context
.vc_thread
= current_thread();
604 context
.vc_ucred
= fp
->f_cred
;
605 if (proc_is64bit(p
)) {
610 * Since the arg parameter is defined as a long but may be
611 * either a long or a pointer we must take care to handle
612 * sign extension issues. Our sys call munger will sign
613 * extend a long when we are called from a 32-bit process.
614 * Since we can never have an address greater than 32-bits
615 * from a 32-bit process we lop off the top 32-bits to avoid
616 * getting the wrong address
618 argp
= CAST_USER_ADDR_T(uap
->arg
);
621 pop
= &fdp
->fd_ofileflags
[fd
];
624 error
= mac_file_check_fcntl(proc_ucred(p
), fp
->f_fglob
, uap
->cmd
,
633 newmin
= CAST_DOWN(int, uap
->arg
);
634 if ((u_int
)newmin
>= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
635 newmin
>= maxfiles
) {
639 if ( (error
= fdalloc(p
, newmin
, &i
)) )
641 error
= finishdup(p
, fdp
, fd
, i
, retval
);
645 *retval
= (*pop
& UF_EXCLOSE
)? 1 : 0;
650 *pop
= (*pop
&~ UF_EXCLOSE
) |
651 (uap
->arg
& 1)? UF_EXCLOSE
: 0;
656 *retval
= OFLAGS(fp
->f_flag
);
661 fp
->f_flag
&= ~FCNTLFLAGS
;
662 tmp
= CAST_DOWN(int, uap
->arg
);
663 fp
->f_flag
|= FFLAGS(tmp
) & FCNTLFLAGS
;
664 tmp
= fp
->f_flag
& FNONBLOCK
;
665 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
668 tmp
= fp
->f_flag
& FASYNC
;
669 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, &context
);
672 fp
->f_flag
&= ~FNONBLOCK
;
674 (void)fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
678 if (fp
->f_type
== DTYPE_SOCKET
) {
679 *retval
= ((struct socket
*)fp
->f_data
)->so_pgid
;
683 error
= fo_ioctl(fp
, (int)TIOCGPGRP
, (caddr_t
)retval
, &context
);
688 tmp
= CAST_DOWN(pid_t
, uap
->arg
);
689 if (fp
->f_type
== DTYPE_SOCKET
) {
690 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
694 if (fp
->f_type
== DTYPE_PIPE
) {
695 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
702 proc_t p1
= proc_find(tmp
);
707 tmp
= (int)p1
->p_pgrpid
;
710 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
715 /* Fall into F_SETLK */
718 if (fp
->f_type
!= DTYPE_VNODE
) {
722 vp
= (struct vnode
*)fp
->f_data
;
725 offset
= fp
->f_offset
;
728 /* Copy in the lock structure */
729 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
734 if ((fl
.l_whence
== SEEK_CUR
) && (fl
.l_start
+ offset
< fl
.l_start
)) {
739 if ( (error
= vnode_getwithref(vp
)) ) {
742 if (fl
.l_whence
== SEEK_CUR
)
743 fl
.l_start
+= offset
;
746 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
756 if ((fflag
& FREAD
) == 0) {
761 // XXX UInt32 unsafe for LP64 kernel
762 OSBitOrAtomic(P_LADVLOCK
, (UInt32
*)&p
->p_ladvflag
);
763 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_SETLK
, &fl
, flg
, &context
);
768 if ((fflag
& FWRITE
) == 0) {
773 // XXX UInt32 unsafe for LP64 kernel
774 OSBitOrAtomic(P_LADVLOCK
, (UInt32
*)&p
->p_ladvflag
);
775 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_SETLK
, &fl
, flg
, &context
);
780 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &fl
,
792 if (fp
->f_type
!= DTYPE_VNODE
) {
796 vp
= (struct vnode
*)fp
->f_data
;
798 offset
= fp
->f_offset
;
801 /* Copy in the lock structure */
802 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
806 /* Check starting byte and ending byte for EOVERFLOW in SEEK_CUR */
807 /* and ending byte for EOVERFLOW in SEEK_SET */
808 if (((fl
.l_whence
== SEEK_CUR
) &&
809 ((fl
.l_start
+ offset
< fl
.l_start
) ||
810 ((fl
.l_len
> 0) && (fl
.l_start
+offset
+ fl
.l_len
- 1 < fl
.l_start
+offset
)))) ||
811 ((fl
.l_whence
== SEEK_SET
) && (fl
.l_len
> 0) && (fl
.l_start
+ fl
.l_len
- 1 < fl
.l_start
)))
813 /* lf_advlock doesn't check start/end for F_GETLK if file has no locks */
818 if ((fl
.l_whence
== SEEK_SET
) && (fl
.l_start
< 0)) {
833 switch (fl
.l_whence
) {
843 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
844 if (fl
.l_whence
== SEEK_CUR
)
845 fl
.l_start
+= offset
;
848 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
852 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_GETLK
, &fl
, F_POSIX
, &context
);
857 error
= copyout((caddr_t
)&fl
, argp
, sizeof(fl
));
861 case F_PREALLOCATE
: {
862 fstore_t alloc_struct
; /* structure for allocate command */
863 u_int32_t alloc_flags
= 0;
865 if (fp
->f_type
!= DTYPE_VNODE
) {
870 vp
= (struct vnode
*)fp
->f_data
;
873 /* make sure that we have write permission */
874 if ((fp
->f_flag
& FWRITE
) == 0) {
879 error
= copyin(argp
, (caddr_t
)&alloc_struct
, sizeof(alloc_struct
));
883 /* now set the space allocated to 0 */
884 alloc_struct
.fst_bytesalloc
= 0;
887 * Do some simple parameter checking
890 /* set up the flags */
892 alloc_flags
|= PREALLOCATE
;
894 if (alloc_struct
.fst_flags
& F_ALLOCATECONTIG
)
895 alloc_flags
|= ALLOCATECONTIG
;
897 if (alloc_struct
.fst_flags
& F_ALLOCATEALL
)
898 alloc_flags
|= ALLOCATEALL
;
901 * Do any position mode specific stuff. The only
902 * position mode supported now is PEOFPOSMODE
905 switch (alloc_struct
.fst_posmode
) {
908 if (alloc_struct
.fst_offset
!= 0) {
913 alloc_flags
|= ALLOCATEFROMPEOF
;
917 if (alloc_struct
.fst_offset
<= 0) {
922 alloc_flags
|= ALLOCATEFROMVOL
;
930 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
932 * call allocate to get the space
934 error
= VNOP_ALLOCATE(vp
,alloc_struct
.fst_length
,alloc_flags
,
935 &alloc_struct
.fst_bytesalloc
, alloc_struct
.fst_offset
,
939 error2
= copyout((caddr_t
)&alloc_struct
, argp
, sizeof(alloc_struct
));
948 if (fp
->f_type
!= DTYPE_VNODE
) {
952 vp
= (struct vnode
*)fp
->f_data
;
955 error
= copyin(argp
, (caddr_t
)&offset
, sizeof (off_t
));
959 error
= vnode_getwithref(vp
);
964 error
= mac_vnode_check_truncate(&context
,
965 fp
->f_fglob
->fg_cred
, vp
);
972 * Make sure that we are root. Growing a file
973 * without zero filling the data is a security hole
974 * root would have access anyway so we'll allow it
982 error
= vnode_setsize(vp
, offset
, IO_NOZEROFILL
,
990 if (fp
->f_type
!= DTYPE_VNODE
) {
995 fp
->f_fglob
->fg_flag
&= ~FNORDAHEAD
;
997 fp
->f_fglob
->fg_flag
|= FNORDAHEAD
;
1002 if (fp
->f_type
!= DTYPE_VNODE
) {
1007 fp
->f_fglob
->fg_flag
|= FNOCACHE
;
1009 fp
->f_fglob
->fg_flag
&= ~FNOCACHE
;
1013 case F_GLOBAL_NOCACHE
:
1014 if (fp
->f_type
!= DTYPE_VNODE
) {
1018 vp
= (struct vnode
*)fp
->f_data
;
1021 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1023 *retval
= vnode_isnocache(vp
);
1026 vnode_setnocache(vp
);
1028 vnode_clearnocache(vp
);
1030 (void)vnode_put(vp
);
1034 case F_CHECK_OPENEVT
:
1035 if (fp
->f_type
!= DTYPE_VNODE
) {
1039 vp
= (struct vnode
*)fp
->f_data
;
1042 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1044 *retval
= vnode_is_openevt(vp
);
1047 vnode_set_openevt(vp
);
1049 vnode_clear_openevt(vp
);
1051 (void)vnode_put(vp
);
1056 struct radvisory ra_struct
;
1058 if (fp
->f_type
!= DTYPE_VNODE
) {
1062 vp
= (struct vnode
*)fp
->f_data
;
1065 if ( (error
= copyin(argp
, (caddr_t
)&ra_struct
, sizeof(ra_struct
))) )
1067 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1068 error
= VNOP_IOCTL(vp
, F_RDADVISE
, (caddr_t
)&ra_struct
, 0, &context
);
1070 (void)vnode_put(vp
);
1075 case F_READBOOTSTRAP
:
1076 case F_WRITEBOOTSTRAP
: {
1077 fbootstraptransfer_t fbt_struct
;
1078 user_fbootstraptransfer_t user_fbt_struct
;
1080 caddr_t boot_structp
;
1082 if (fp
->f_type
!= DTYPE_VNODE
) {
1086 vp
= (struct vnode
*)fp
->f_data
;
1089 if (IS_64BIT_PROCESS(p
)) {
1090 sizeof_struct
= sizeof(user_fbt_struct
);
1091 boot_structp
= (caddr_t
) &user_fbt_struct
;
1094 sizeof_struct
= sizeof(fbt_struct
);
1095 boot_structp
= (caddr_t
) &fbt_struct
;
1097 error
= copyin(argp
, boot_structp
, sizeof_struct
);
1100 if ( (error
= vnode_getwithref(vp
)) ) {
1103 if (uap
->cmd
== F_WRITEBOOTSTRAP
) {
1105 * Make sure that we are root. Updating the
1106 * bootstrap on a disk could be a security hole
1109 (void)vnode_put(vp
);
1114 if (strncmp(vnode_mount(vp
)->mnt_vfsstat
.f_fstypename
, "hfs",
1115 sizeof(vnode_mount(vp
)->mnt_vfsstat
.f_fstypename
)) != 0) {
1119 * call vnop_ioctl to handle the I/O
1121 error
= VNOP_IOCTL(vp
, uap
->cmd
, boot_structp
, 0, &context
);
1123 (void)vnode_put(vp
);
1127 struct log2phys l2p_struct
; /* structure for allocate command */
1129 if (fp
->f_type
!= DTYPE_VNODE
) {
1133 vp
= (struct vnode
*)fp
->f_data
;
1135 if ( (error
= vnode_getwithref(vp
)) ) {
1138 error
= VNOP_OFFTOBLK(vp
, fp
->f_offset
, &lbn
);
1140 (void)vnode_put(vp
);
1143 error
= VNOP_BLKTOOFF(vp
, lbn
, &offset
);
1145 (void)vnode_put(vp
);
1148 devBlockSize
= vfs_devblocksize(vnode_mount(vp
));
1150 error
= VNOP_BLOCKMAP(vp
, offset
, devBlockSize
, &bn
, NULL
, NULL
, 0, &context
);
1152 (void)vnode_put(vp
);
1155 l2p_struct
.l2p_flags
= 0; /* for now */
1156 l2p_struct
.l2p_contigbytes
= 0; /* for now */
1157 l2p_struct
.l2p_devoffset
= bn
* devBlockSize
;
1158 l2p_struct
.l2p_devoffset
+= fp
->f_offset
- offset
;
1159 error
= copyout((caddr_t
)&l2p_struct
, argp
, sizeof(l2p_struct
));
1167 if (fp
->f_type
!= DTYPE_VNODE
) {
1171 vp
= (struct vnode
*)fp
->f_data
;
1174 pathlen
= MAXPATHLEN
;
1175 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
1176 if (pathbufp
== NULL
) {
1180 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1181 error
= vn_getpath(vp
, pathbufp
, &pathlen
);
1182 (void)vnode_put(vp
);
1185 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
1187 FREE(pathbufp
, M_TEMP
);
1191 case F_PATHPKG_CHECK
: {
1195 if (fp
->f_type
!= DTYPE_VNODE
) {
1199 vp
= (struct vnode
*)fp
->f_data
;
1202 pathlen
= MAXPATHLEN
;
1203 pathbufp
= kalloc(MAXPATHLEN
);
1205 if ( (error
= copyinstr(argp
, pathbufp
, MAXPATHLEN
, &pathlen
)) == 0 ) {
1206 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1207 error
= vn_path_package_check(vp
, pathbufp
, pathlen
, retval
);
1209 (void)vnode_put(vp
);
1212 kfree(pathbufp
, MAXPATHLEN
);
1216 case F_CHKCLEAN
: // used by regression tests to see if all dirty pages got cleaned by fsync()
1217 case F_FULLFSYNC
: // fsync + flush the journal + DKIOCSYNCHRONIZECACHE
1218 case F_FREEZE_FS
: // freeze all other fs operations for the fs of this fd
1219 case F_THAW_FS
: { // thaw all frozen fs operations for the fs of this fd
1220 if (fp
->f_type
!= DTYPE_VNODE
) {
1224 vp
= (struct vnode
*)fp
->f_data
;
1227 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1228 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
1230 (void)vnode_put(vp
);
1236 * SPI (private) for opening a file starting from a dir fd
1239 struct user_fopenfrom fopen
;
1240 struct vnode_attr va
;
1241 struct nameidata nd
;
1244 /* Check if this isn't a valid file descriptor */
1245 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1246 (fp
->f_flag
& FREAD
) == 0) {
1250 vp
= (struct vnode
*)fp
->f_data
;
1253 if (vnode_getwithref(vp
)) {
1258 /* Only valid for directories */
1259 if (vp
->v_type
!= VDIR
) {
1265 /* Get flags, mode and pathname arguments. */
1266 if (IS_64BIT_PROCESS(p
)) {
1267 error
= copyin(argp
, &fopen
, sizeof(fopen
));
1269 struct fopenfrom fopen32
;
1271 error
= copyin(argp
, &fopen32
, sizeof(fopen32
));
1272 fopen
.o_flags
= fopen32
.o_flags
;
1273 fopen
.o_mode
= fopen32
.o_mode
;
1274 fopen
.o_pathname
= CAST_USER_ADDR_T(fopen32
.o_pathname
);
1281 /* Mask off all but regular access permissions */
1282 cmode
= ((fopen
.o_mode
&~ fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
1283 VATTR_SET(&va
, va_mode
, cmode
& ACCESSPERMS
);
1285 /* Start the lookup relative to the file descriptor's vnode. */
1286 NDINIT(&nd
, LOOKUP
, USEDVP
| FOLLOW
| AUDITVNPATH1
, UIO_USERSPACE
,
1287 fopen
.o_pathname
, &context
);
1290 error
= open1(&context
, &nd
, fopen
.o_flags
, &va
, retval
);
1296 * SPI (private) for unlinking a file starting from a dir fd
1298 case F_UNLINKFROM
: {
1299 struct nameidata nd
;
1300 user_addr_t pathname
;
1302 /* Check if this isn't a valid file descriptor */
1303 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1304 (fp
->f_flag
& FREAD
) == 0) {
1308 vp
= (struct vnode
*)fp
->f_data
;
1311 if (vnode_getwithref(vp
)) {
1316 /* Only valid for directories */
1317 if (vp
->v_type
!= VDIR
) {
1323 /* Get flags, mode and pathname arguments. */
1324 if (IS_64BIT_PROCESS(p
)) {
1325 pathname
= (user_addr_t
)argp
;
1327 pathname
= CAST_USER_ADDR_T(argp
);
1330 /* Start the lookup relative to the file descriptor's vnode. */
1331 NDINIT(&nd
, DELETE
, USEDVP
| AUDITVNPATH1
, UIO_USERSPACE
, pathname
, &context
);
1334 error
= unlink1(&context
, &nd
, 0);
1342 struct user_fsignatures fs
;
1344 vm_address_t kernel_blob_addr
;
1345 vm_size_t kernel_blob_size
;
1347 if (fp
->f_type
!= DTYPE_VNODE
) {
1351 vp
= (struct vnode
*)fp
->f_data
;
1353 error
= vnode_getwithref(vp
);
1357 if (IS_64BIT_PROCESS(p
)) {
1358 error
= copyin(argp
, &fs
, sizeof (fs
));
1360 struct fsignatures fs32
;
1362 error
= copyin(argp
, &fs32
, sizeof (fs32
));
1363 fs
.fs_file_start
= fs32
.fs_file_start
;
1364 fs
.fs_blob_start
= CAST_USER_ADDR_T(fs32
.fs_blob_start
);
1365 fs
.fs_blob_size
= fs32
.fs_blob_size
;
1373 #define CS_MAX_BLOB_SIZE (1ULL * 1024 * 1024) /* XXX ? */
1374 if (fs
.fs_blob_size
> CS_MAX_BLOB_SIZE
) {
1380 kernel_blob_size
= CAST_DOWN(vm_size_t
, fs
.fs_blob_size
);
1381 kr
= kmem_alloc(kernel_map
,
1384 if (kr
!= KERN_SUCCESS
) {
1390 error
= copyin(fs
.fs_blob_start
,
1391 (void *) kernel_blob_addr
,
1394 kmem_free(kernel_map
,
1401 error
= ubc_cs_blob_add(
1403 CPU_TYPE_ANY
, /* not for a specific architecture */
1408 kmem_free(kernel_map
,
1412 /* ubc_blob_add() was consumed "kernel_blob_addr" */
1415 (void) vnode_put(vp
);
1419 case F_MARKDEPENDENCY
: {
1420 struct vnode
*root_vp
;
1421 struct vnode_attr va
;
1422 vfs_context_t ctx
= vfs_context_current();
1425 if ((current_proc()->p_flag
& P_DEPENDENCY_CAPABLE
) == 0) {
1430 if (fp
->f_type
!= DTYPE_VNODE
) {
1435 vp
= (struct vnode
*)fp
->f_data
;
1438 if (vnode_getwithref(vp
)) {
1443 // the passed in vnode must be the root dir of the file system
1444 if (VFS_ROOT(vp
->v_mount
, &root_vp
, ctx
) != 0 || vp
!= root_vp
) {
1451 // get the owner of the root dir
1453 VATTR_WANTED(&va
, va_uid
);
1454 if (vnode_getattr(vp
, &va
, ctx
) != 0) {
1460 // and last, check that the caller is the super user or
1461 // the owner of the mount point
1462 cred
= vfs_context_ucred(ctx
);
1463 if (!is_suser() && va
.va_uid
!= kauth_cred_getuid(cred
)) {
1469 // if all those checks pass then we can mark the dependency
1470 vfs_markdependency(vp
->v_mount
);
1479 if (uap
->cmd
< FCNTL_FS_SPECIFIC_BASE
) {
1484 // if it's a fs-specific fcntl() then just pass it through
1486 if (fp
->f_type
!= DTYPE_VNODE
) {
1490 vp
= (struct vnode
*)fp
->f_data
;
1493 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1494 error
= VNOP_IOCTL(vp
, uap
->cmd
, CAST_DOWN(caddr_t
, argp
), 0, &context
);
1496 (void)vnode_put(vp
);
1503 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
1504 fp_drop(p
, fd
, fp
, 0);
1507 fp_drop(p
, fd
, fp
, 1);
1516 * Description: Common code for dup, dup2, and fcntl(F_DUPFD).
1518 * Parameters: p Process performing the dup
1520 * new The fd to dup it to
1521 * retval Pointer to the call return area
1523 * Returns: 0 Success
1528 * *retval (modified) The new descriptor
1530 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
1533 * Notes: This function may drop and reacquire this lock; it is unsafe
1534 * for a caller to assume that other state protected by the lock
1535 * has not been subsequently changes out from under it.
1538 finishdup(proc_t p
, struct filedesc
*fdp
, int old
, int new, register_t
*retval
)
1540 struct fileproc
*nfp
;
1541 struct fileproc
*ofp
;
1547 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
1550 if ((ofp
= fdp
->fd_ofiles
[old
]) == NULL
||
1551 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
)) {
1558 error
= mac_file_check_dup(proc_ucred(p
), ofp
->f_fglob
, new);
1568 MALLOC_ZONE(nfp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
1569 /* Failure check follows proc_fdlock() due to handling requirements */
1579 bzero(nfp
, sizeof(struct fileproc
));
1582 nfp
->f_fglob
= ofp
->f_fglob
;
1586 if (fdp
->fd_ofiles
[new] != 0)
1587 panic("finishdup: overwriting fd_ofiles with new %d\n", new);
1588 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0)
1589 panic("finishdup: unreserved fileflags with new %d\n", new);
1592 if (new > fdp
->fd_lastfile
)
1593 fdp
->fd_lastfile
= new;
1594 procfdtbl_releasefd(p
, new, nfp
);
1603 * Description: The implementation of the close(2) system call
1605 * Parameters: p Process in whose per process file table
1606 * the close is to occur
1607 * uap->fd fd to be closed
1610 * Returns: 0 Success
1611 * fp_lookup:EBADF Bad file descriptor
1612 * close_internal:EBADF
1613 * close_internal:??? Anything returnable by a per-fileops
1617 close(proc_t p
, struct close_args
*uap
, register_t
*retval
)
1619 __pthread_testcancel(1);
1620 return(close_nocancel(p
, (struct close_nocancel_args
*)uap
, retval
));
1625 close_nocancel(proc_t p
, struct close_nocancel_args
*uap
, __unused register_t
*retval
)
1627 struct fileproc
*fp
;
1631 AUDIT_SYSCLOSE(p
, fd
);
1635 if ( (error
= fp_lookup(p
,fd
,&fp
, 1)) ) {
1640 error
= close_internal_locked(p
, fd
, fp
, 0);
1649 * close_internal_locked
1651 * Close a file descriptor.
1653 * Parameters: p Process in whose per process file table
1654 * the close is to occur
1655 * fd fd to be closed
1656 * fp fileproc associated with the fd
1658 * Returns: 0 Success
1659 * EBADF fd already in close wait state
1660 * closef_locked:??? Anything returnable by a per-fileops
1663 * Locks: Assumes proc_fdlock for process is held by the caller and returns
1666 * Notes: This function may drop and reacquire this lock; it is unsafe
1667 * for a caller to assume that other state protected by the lock
1668 * has not been subsequently changes out from under it, if the
1669 * caller made the call with the lock held.
1672 close_internal_locked(proc_t p
, int fd
, struct fileproc
*fp
, int flags
)
1674 struct filedesc
*fdp
= p
->p_fd
;
1676 int resvfd
= flags
& FD_DUP2RESV
;
1680 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
1683 /* Keep people from using the filedesc while we are closing it */
1684 procfdtbl_markclosefd(p
, fd
);
1687 if ((fp
->f_flags
& FP_CLOSING
) == FP_CLOSING
) {
1688 panic("close_internal_locked: being called on already closing fd\n");
1693 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
1694 panic("close_internal: unreserved fileflags with fd %d\n", fd
);
1697 fp
->f_flags
|= FP_CLOSING
;
1699 if ( (fp
->f_flags
& FP_AIOISSUED
) || kauth_authorize_fileop_has_listeners() ) {
1703 if ( (fp
->f_type
== DTYPE_VNODE
) && kauth_authorize_fileop_has_listeners() ) {
1705 * call out to allow 3rd party notification of close.
1706 * Ignore result of kauth_authorize_fileop call.
1708 if (vnode_getwithref((vnode_t
)fp
->f_data
) == 0) {
1709 u_int fileop_flags
= 0;
1710 if ((fp
->f_flags
& FP_WRITTEN
) != 0)
1711 fileop_flags
|= KAUTH_FILEOP_CLOSE_MODIFIED
;
1712 kauth_authorize_fileop(fp
->f_fglob
->fg_cred
, KAUTH_FILEOP_CLOSE
,
1713 (uintptr_t)fp
->f_data
, (uintptr_t)fileop_flags
);
1714 vnode_put((vnode_t
)fp
->f_data
);
1717 if (fp
->f_flags
& FP_AIOISSUED
)
1719 * cancel all async IO requests that can be cancelled.
1721 _aio_close( p
, fd
);
1726 if (fd
< fdp
->fd_knlistsize
)
1727 knote_fdclose(p
, fd
);
1729 if (fp
->f_flags
& FP_WAITEVENT
)
1730 (void)waitevent_close(p
, fp
);
1732 if ((fp
->f_flags
& FP_INCHRREAD
) == 0)
1733 fileproc_drain(p
, fp
);
1738 error
= closef_locked(fp
, fp
->f_fglob
, p
);
1739 if ((fp
->f_flags
& FP_WAITCLOSE
) == FP_WAITCLOSE
)
1740 wakeup(&fp
->f_flags
);
1741 fp
->f_flags
&= ~(FP_WAITCLOSE
| FP_CLOSING
);
1745 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
1751 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
1752 panic("close with reserved fd returns with freed fd:%d: proc: %x\n", fd
, (unsigned int)p
);
1763 * Description: Return status information about a file descriptor.
1765 * Parameters: p The process doing the fstat
1767 * ub The user stat buffer
1768 * xsecurity The user extended security
1769 * buffer, or 0 if none
1770 * xsecurity_size The size of xsecurity, or 0
1772 * isstat64 Flag to indicate 64 bit version
1773 * for inode size, etc.
1775 * Returns: 0 Success
1778 * fp_lookup:EBADF Bad file descriptor
1779 * vnode_getwithref:???
1781 * vnode_getwithref:???
1788 * Notes: Internal implementation for all other fstat() related
1791 * XXX switch on node type is bogus; need a stat in struct
1792 * XXX fileops instead.
1795 fstat1(proc_t p
, int fd
, user_addr_t ub
, user_addr_t xsecurity
, user_addr_t xsecurity_size
, int isstat64
)
1797 struct fileproc
*fp
;
1800 struct user_stat user_sb
;
1801 struct user_stat64 user_sb64
;
1806 kauth_filesec_t fsec
;
1807 user_size_t xsecurity_bufsize
;
1808 vfs_context_t ctx
= vfs_context_current();
1814 if ((error
= fp_lookup(p
, fd
, &fp
, 0)) != 0) {
1819 fsec
= KAUTH_FILESEC_NONE
;
1821 sbptr
= (isstat64
!= 0) ? (void *)&sb64
: (void *)&sb
;
1826 if ((error
= vnode_getwithref((vnode_t
)data
)) == 0) {
1828 * If the caller has the file open, and is not
1829 * requesting extended security information, we are
1830 * going to let them get the basic stat information.
1832 if (xsecurity
== USER_ADDR_NULL
) {
1833 error
= vn_stat_noauth((vnode_t
)data
, sbptr
, NULL
, isstat64
, ctx
);
1835 error
= vn_stat((vnode_t
)data
, sbptr
, &fsec
, isstat64
, ctx
);
1838 AUDIT_ARG(vnpath
, (struct vnode
*)data
, ARG_VNODE1
);
1839 (void)vnode_put((vnode_t
)data
);
1845 error
= soo_stat((struct socket
*)data
, sbptr
, isstat64
);
1847 #endif /* SOCKETS */
1850 error
= pipe_stat((void *)data
, sbptr
, isstat64
);
1854 error
= pshm_stat((void *)data
, sbptr
, isstat64
);
1858 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1859 error
= kqueue_stat(fp
, sbptr
, isstat64
, p
);
1860 thread_funnel_set(kernel_flock
, funnel_state
);
1870 if (isstat64
!= 0) {
1872 sb64
.st_qspare
[0] = 0LL;
1873 sb64
.st_qspare
[1] = 0LL;
1874 if (IS_64BIT_PROCESS(current_proc())) {
1875 munge_stat64(&sb64
, &user_sb64
);
1876 my_size
= sizeof(user_sb64
);
1877 sbp
= (caddr_t
)&user_sb64
;
1879 my_size
= sizeof(sb64
);
1880 sbp
= (caddr_t
)&sb64
;
1884 sb
.st_qspare
[0] = 0LL;
1885 sb
.st_qspare
[1] = 0LL;
1886 if (IS_64BIT_PROCESS(current_proc())) {
1887 munge_stat(&sb
, &user_sb
);
1888 my_size
= sizeof(user_sb
);
1889 sbp
= (caddr_t
)&user_sb
;
1891 my_size
= sizeof(sb
);
1896 error
= copyout(sbp
, ub
, my_size
);
1899 /* caller wants extended security information? */
1900 if (xsecurity
!= USER_ADDR_NULL
) {
1902 /* did we get any? */
1903 if (fsec
== KAUTH_FILESEC_NONE
) {
1904 if (susize(xsecurity_size
, 0) != 0) {
1909 /* find the user buffer size */
1910 xsecurity_bufsize
= fusize(xsecurity_size
);
1912 /* copy out the actual data size */
1913 if (susize(xsecurity_size
, KAUTH_FILESEC_COPYSIZE(fsec
)) != 0) {
1918 /* if the caller supplied enough room, copy out to it */
1919 if (xsecurity_bufsize
>= KAUTH_FILESEC_COPYSIZE(fsec
))
1920 error
= copyout(fsec
, xsecurity
, KAUTH_FILESEC_COPYSIZE(fsec
));
1924 fp_drop(p
, fd
, fp
, 0);
1926 kauth_filesec_free(fsec
);
1934 * Description: Extended version of fstat supporting returning extended
1935 * security information
1937 * Parameters: p The process doing the fstat
1938 * uap->fd The fd to stat
1939 * uap->ub The user stat buffer
1940 * uap->xsecurity The user extended security
1941 * buffer, or 0 if none
1942 * uap->xsecurity_size The size of xsecurity, or 0
1944 * Returns: 0 Success
1945 * !0 Errno (see fstat1)
1948 fstat_extended(proc_t p
, struct fstat_extended_args
*uap
, __unused register_t
*retval
)
1950 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 0));
1957 * Description: Get file status for the file associated with fd
1959 * Parameters: p The process doing the fstat
1960 * uap->fd The fd to stat
1961 * uap->ub The user stat buffer
1963 * Returns: 0 Success
1964 * !0 Errno (see fstat1)
1967 fstat(proc_t p
, register struct fstat_args
*uap
, __unused register_t
*retval
)
1969 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 0));
1976 * Description: Extended version of fstat64 supporting returning extended
1977 * security information
1979 * Parameters: p The process doing the fstat
1980 * uap->fd The fd to stat
1981 * uap->ub The user stat buffer
1982 * uap->xsecurity The user extended security
1983 * buffer, or 0 if none
1984 * uap->xsecurity_size The size of xsecurity, or 0
1986 * Returns: 0 Success
1987 * !0 Errno (see fstat1)
1990 fstat64_extended(proc_t p
, struct fstat64_extended_args
*uap
, __unused register_t
*retval
)
1992 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 1));
1999 * Description: Get 64 bit version of the file status for the file associated
2002 * Parameters: p The process doing the fstat
2003 * uap->fd The fd to stat
2004 * uap->ub The user stat buffer
2006 * Returns: 0 Success
2007 * !0 Errno (see fstat1)
2010 fstat64(proc_t p
, register struct fstat64_args
*uap
, __unused register_t
*retval
)
2012 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 1));
2019 * Description: Return pathconf information about a file descriptor.
2021 * Parameters: p Process making the request
2022 * uap->fd fd to get information about
2023 * uap->name Name of information desired
2024 * retval Pointer to the call return area
2026 * Returns: 0 Success
2028 * fp_lookup:EBADF Bad file descriptor
2029 * vnode_getwithref:???
2033 * *retval (modified) Returned information (numeric)
2036 fpathconf(proc_t p
, struct fpathconf_args
*uap
, register_t
*retval
)
2039 struct fileproc
*fp
;
2046 AUDIT_ARG(fd
, uap
->fd
);
2047 if ( (error
= fp_lookup(p
, fd
, &fp
, 0)) )
2055 if (uap
->name
!= _PC_PIPE_BUF
) {
2069 vp
= (struct vnode
*)data
;
2071 if ( (error
= vnode_getwithref(vp
)) == 0) {
2072 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
2074 error
= vn_pathconf(vp
, uap
->name
, retval
, vfs_context_current());
2076 (void)vnode_put(vp
);
2083 case DTYPE_FSEVENTS
:
2090 fp_drop(p
, fd
, fp
, 0);
2095 * Statistics counter for the number of times a process calling fdalloc()
2096 * has resulted in an expansion of the per process open file table.
2098 * XXX This would likely be of more use if it were per process
2106 * Description: Allocate a file descriptor for the process.
2108 * Parameters: p Process to allocate the fd in
2109 * want The fd we would prefer to get
2110 * result Pointer to fd we got
2112 * Returns: 0 Success
2117 * *result (modified) The fd which was allocated
2120 fdalloc(proc_t p
, int want
, int *result
)
2122 struct filedesc
*fdp
= p
->p_fd
;
2124 int lim
, last
, numfiles
, oldnfiles
;
2125 struct fileproc
**newofiles
, **ofiles
;
2126 char *newofileflags
;
2129 * Search for a free descriptor starting at the higher
2130 * of want or fd_freefile. If that fails, consider
2131 * expanding the ofile array.
2134 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2137 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
2139 last
= min(fdp
->fd_nfiles
, lim
);
2140 if ((i
= want
) < fdp
->fd_freefile
)
2141 i
= fdp
->fd_freefile
;
2142 for (; i
< last
; i
++) {
2143 if (fdp
->fd_ofiles
[i
] == NULL
&& !(fdp
->fd_ofileflags
[i
] & UF_RESERVED
)) {
2144 procfdtbl_reservefd(p
, i
);
2145 if (i
> fdp
->fd_lastfile
)
2146 fdp
->fd_lastfile
= i
;
2147 if (want
<= fdp
->fd_freefile
)
2148 fdp
->fd_freefile
= i
;
2155 * No space in current array. Expand?
2157 if (fdp
->fd_nfiles
>= lim
)
2159 if (fdp
->fd_nfiles
< NDEXTENT
)
2160 numfiles
= NDEXTENT
;
2162 numfiles
= 2 * fdp
->fd_nfiles
;
2167 MALLOC_ZONE(newofiles
, struct fileproc
**,
2168 numfiles
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
2170 if (newofiles
== NULL
) {
2173 if (fdp
->fd_nfiles
>= numfiles
) {
2174 FREE_ZONE(newofiles
, numfiles
* OFILESIZE
, M_OFILETABL
);
2177 newofileflags
= (char *) &newofiles
[numfiles
];
2179 * Copy the existing ofile and ofileflags arrays
2180 * and zero the new portion of each array.
2182 oldnfiles
= fdp
->fd_nfiles
;
2183 (void) memcpy(newofiles
, fdp
->fd_ofiles
,
2184 oldnfiles
* sizeof(*fdp
->fd_ofiles
));
2185 (void) memset(&newofiles
[oldnfiles
], 0,
2186 (numfiles
- oldnfiles
) * sizeof(*fdp
->fd_ofiles
));
2188 (void) memcpy(newofileflags
, fdp
->fd_ofileflags
,
2189 oldnfiles
* sizeof(*fdp
->fd_ofileflags
));
2190 (void) memset(&newofileflags
[oldnfiles
], 0,
2191 (numfiles
- oldnfiles
) *
2192 sizeof(*fdp
->fd_ofileflags
));
2193 ofiles
= fdp
->fd_ofiles
;
2194 fdp
->fd_ofiles
= newofiles
;
2195 fdp
->fd_ofileflags
= newofileflags
;
2196 fdp
->fd_nfiles
= numfiles
;
2197 FREE_ZONE(ofiles
, oldnfiles
* OFILESIZE
, M_OFILETABL
);
2206 * Description: Check to see whether n user file descriptors are available
2209 * Parameters: p Process to check in
2210 * n The number of fd's desired
2215 * Locks: Assumes proc_fdlock for process is held by the caller
2217 * Notes: The answer only remains valid so long as the proc_fdlock is
2218 * held by the caller.
2221 fdavail(proc_t p
, int n
)
2223 struct filedesc
*fdp
= p
->p_fd
;
2224 struct fileproc
**fpp
;
2228 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
2229 if ((i
= lim
- fdp
->fd_nfiles
) > 0 && (n
-= i
) <= 0)
2231 fpp
= &fdp
->fd_ofiles
[fdp
->fd_freefile
];
2232 flags
= &fdp
->fd_ofileflags
[fdp
->fd_freefile
];
2233 for (i
= fdp
->fd_nfiles
- fdp
->fd_freefile
; --i
>= 0; fpp
++, flags
++)
2234 if (*fpp
== NULL
&& !(*flags
& UF_RESERVED
) && --n
<= 0)
2243 * Description: Legacy KPI wrapper function for _fdrelse
2245 * Parameters: p Process in which fd lives
2250 * Locks: Assumes proc_fdlock for process is held by the caller
2253 fdrelse(proc_t p
, int fd
)
2262 * Description: Get the fileproc pointer for the given fd from the per process
2263 * open file table without taking an explicit reference on it.
2265 * Parameters: p Process containing fd
2266 * fd fd to obtain fileproc for
2267 * resultfp Pointer to pointer return area
2269 * Returns: 0 Success
2273 * *resultfp (modified) Pointer to fileproc pointer
2275 * Locks: Assumes proc_fdlock for process is held by the caller
2277 * Notes: Because there is no reference explicitly taken, the returned
2278 * fileproc pointer is only valid so long as the proc_fdlock
2279 * remains held by the caller.
2282 fdgetf_noref(proc_t p
, int fd
, struct fileproc
**resultfp
)
2284 struct filedesc
*fdp
= p
->p_fd
;
2285 struct fileproc
*fp
;
2287 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2288 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2289 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2301 * Description: Get fileproc and vnode pointer for a given fd from the per
2302 * process open file table of the specified process, and if
2303 * successful, increment the f_iocount
2305 * Parameters: p Process in which fd lives
2306 * fd fd to get information for
2307 * resultfp Pointer to result fileproc
2308 * pointer area, or 0 if none
2309 * resultvp Pointer to result vnode pointer
2310 * area, or 0 if none
2312 * Returns: 0 Success
2313 * EBADF Bad file descriptor
2314 * ENOTSUP fd does not refer to a vnode
2317 * *resultfp (modified) Fileproc pointer
2318 * *resultvp (modified) vnode pointer
2320 * Notes: The resultfp and resultvp fields are optional, and may be
2321 * independently specified as NULL to skip returning information
2323 * Locks: Internally takes and releases proc_fdlock
2326 fp_getfvp(proc_t p
, int fd
, struct fileproc
**resultfp
, struct vnode
**resultvp
)
2328 struct filedesc
*fdp
= p
->p_fd
;
2329 struct fileproc
*fp
;
2331 proc_fdlock_spin(p
);
2332 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2333 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2334 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2338 if (fp
->f_type
!= DTYPE_VNODE
) {
2347 *resultvp
= (struct vnode
*)fp
->f_data
;
2357 * Description: Get fileproc, vnode pointer, and vid for a given fd from the
2358 * per process open file table of the specified process, and if
2359 * successful, increment the f_iocount
2361 * Parameters: p Process in which fd lives
2362 * fd fd to get information for
2363 * resultfp Pointer to result fileproc
2364 * pointer area, or 0 if none
2365 * resultvp Pointer to result vnode pointer
2366 * area, or 0 if none
2367 * vidp Pointer to resuld vid area
2369 * Returns: 0 Success
2370 * EBADF Bad file descriptor
2371 * ENOTSUP fd does not refer to a vnode
2374 * *resultfp (modified) Fileproc pointer
2375 * *resultvp (modified) vnode pointer
2378 * Notes: The resultfp and resultvp fields are optional, and may be
2379 * independently specified as NULL to skip returning information
2381 * Locks: Internally takes and releases proc_fdlock
2384 fp_getfvpandvid(proc_t p
, int fd
, struct fileproc
**resultfp
,
2385 struct vnode
**resultvp
, uint32_t *vidp
)
2387 struct filedesc
*fdp
= p
->p_fd
;
2388 struct fileproc
*fp
;
2390 proc_fdlock_spin(p
);
2391 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2392 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2393 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2397 if (fp
->f_type
!= DTYPE_VNODE
) {
2406 *resultvp
= (struct vnode
*)fp
->f_data
;
2408 *vidp
= (uint32_t)vnode_vid((struct vnode
*)fp
->f_data
);
2418 * Description: Get fileproc and socket pointer for a given fd from the
2419 * per process open file table of the specified process, and if
2420 * successful, increment the f_iocount
2422 * Parameters: p Process in which fd lives
2423 * fd fd to get information for
2424 * resultfp Pointer to result fileproc
2425 * pointer area, or 0 if none
2426 * results Pointer to result socket
2427 * pointer area, or 0 if none
2429 * Returns: EBADF The file descriptor is invalid
2430 * EOPNOTSUPP The file descriptor is not a socket
2434 * *resultfp (modified) Fileproc pointer
2435 * *results (modified) socket pointer
2437 * Notes: EOPNOTSUPP should probably be ENOTSOCK; this function is only
2438 * ever called from accept1().
2441 fp_getfsock(proc_t p
, int fd
, struct fileproc
**resultfp
,
2442 struct socket
**results
)
2444 struct filedesc
*fdp
= p
->p_fd
;
2445 struct fileproc
*fp
;
2447 proc_fdlock_spin(p
);
2448 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2449 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2450 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2454 if (fp
->f_type
!= DTYPE_SOCKET
) {
2463 *results
= (struct socket
*)fp
->f_data
;
2473 * Description: Get fileproc and kqueue pointer for a given fd from the
2474 * per process open file table of the specified process, and if
2475 * successful, increment the f_iocount
2477 * Parameters: p Process in which fd lives
2478 * fd fd to get information for
2479 * resultfp Pointer to result fileproc
2480 * pointer area, or 0 if none
2481 * resultkq Pointer to result kqueue
2482 * pointer area, or 0 if none
2484 * Returns: EBADF The file descriptor is invalid
2485 * EBADF The file descriptor is not a socket
2489 * *resultfp (modified) Fileproc pointer
2490 * *resultkq (modified) kqueue pointer
2492 * Notes: The second EBADF should probably be something else to make
2493 * the error condition distinct.
2496 fp_getfkq(proc_t p
, int fd
, struct fileproc
**resultfp
,
2497 struct kqueue
**resultkq
)
2499 struct filedesc
*fdp
= p
->p_fd
;
2500 struct fileproc
*fp
;
2502 proc_fdlock_spin(p
);
2503 if ( fd
< 0 || fd
>= fdp
->fd_nfiles
||
2504 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2505 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2509 if (fp
->f_type
!= DTYPE_KQUEUE
) {
2518 *resultkq
= (struct kqueue
*)fp
->f_data
;
2528 * Description: Get fileproc and POSIX shared memory pointer for a given fd
2529 * from the per process open file table of the specified process
2530 * and if successful, increment the f_iocount
2532 * Parameters: p Process in which fd lives
2533 * fd fd to get information for
2534 * resultfp Pointer to result fileproc
2535 * pointer area, or 0 if none
2536 * resultpshm Pointer to result POSIX
2537 * shared memory pointer
2538 * pointer area, or 0 if none
2540 * Returns: EBADF The file descriptor is invalid
2541 * EBADF The file descriptor is not a POSIX
2542 * shared memory area
2546 * *resultfp (modified) Fileproc pointer
2547 * *resultpshm (modified) POSIX shared memory pointer
2549 * Notes: The second EBADF should probably be something else to make
2550 * the error condition distinct.
2553 fp_getfpshm(proc_t p
, int fd
, struct fileproc
**resultfp
,
2554 struct pshmnode
**resultpshm
)
2556 struct filedesc
*fdp
= p
->p_fd
;
2557 struct fileproc
*fp
;
2559 proc_fdlock_spin(p
);
2560 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2561 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2562 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2566 if (fp
->f_type
!= DTYPE_PSXSHM
) {
2576 *resultpshm
= (struct pshmnode
*)fp
->f_data
;
2586 * Description: Get fileproc and POSIX semaphore pointer for a given fd from
2587 * the per process open file table of the specified process
2588 * and if successful, increment the f_iocount
2590 * Parameters: p Process in which fd lives
2591 * fd fd to get information for
2592 * resultfp Pointer to result fileproc
2593 * pointer area, or 0 if none
2594 * resultpsem Pointer to result POSIX
2595 * semaphore pointer area, or
2598 * Returns: EBADF The file descriptor is invalid
2599 * EBADF The file descriptor is not a POSIX
2604 * *resultfp (modified) Fileproc pointer
2605 * *resultpsem (modified) POSIX semaphore pointer
2607 * Notes: The second EBADF should probably be something else to make
2608 * the error condition distinct.
2610 * In order to support unnamed POSIX semaphores, the named
2611 * POSIX semaphores will have to move out of the per-process
2612 * open filetable, and into a global table that is shared with
2613 * unnamed POSIX semaphores, since unnamed POSIX semaphores
2614 * are typically used by declaring instances in shared memory,
2615 * and there's no other way to do this without changing the
2616 * underlying type, which would introduce binary compatibility
2620 fp_getfpsem(proc_t p
, int fd
, struct fileproc
**resultfp
,
2621 struct psemnode
**resultpsem
)
2623 struct filedesc
*fdp
= p
->p_fd
;
2624 struct fileproc
*fp
;
2626 proc_fdlock_spin(p
);
2627 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2628 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2629 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2633 if (fp
->f_type
!= DTYPE_PSXSEM
) {
2642 *resultpsem
= (struct psemnode
*)fp
->f_data
;
2652 * Description: Get fileproc and pipe pointer for a given fd from the
2653 * per process open file table of the specified process
2654 * and if successful, increment the f_iocount
2656 * Parameters: p Process in which fd lives
2657 * fd fd to get information for
2658 * resultfp Pointer to result fileproc
2659 * pointer area, or 0 if none
2660 * resultpipe Pointer to result pipe
2661 * pointer area, or 0 if none
2663 * Returns: EBADF The file descriptor is invalid
2664 * EBADF The file descriptor is not a socket
2668 * *resultfp (modified) Fileproc pointer
2669 * *resultpipe (modified) pipe pointer
2671 * Notes: The second EBADF should probably be something else to make
2672 * the error condition distinct.
2675 fp_getfpipe(proc_t p
, int fd
, struct fileproc
**resultfp
,
2676 struct pipe
**resultpipe
)
2678 struct filedesc
*fdp
= p
->p_fd
;
2679 struct fileproc
*fp
;
2681 proc_fdlock_spin(p
);
2682 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2683 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2684 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2688 if (fp
->f_type
!= DTYPE_PIPE
) {
2697 *resultpipe
= (struct pipe
*)fp
->f_data
;
2704 #define DTYPE_ATALK -1 /* XXX This does not belong here */
2710 * Description: Get fileproc and atalk pointer for a given fd from the
2711 * per process open file table of the specified process
2712 * and if successful, increment the f_iocount
2714 * Parameters: p Process in which fd lives
2715 * fd fd to get information for
2716 * resultfp Pointer to result fileproc
2717 * pointer area, or 0 if none
2718 * resultatalk Pointer to result atalk
2719 * pointer area, or 0 if none
2720 * Returns: EBADF The file descriptor is invalid
2721 * EBADF The file descriptor is not a socket
2725 * *resultfp (modified) Fileproc pointer
2726 * *resultatalk (modified) atalk pointer
2728 * Notes: The second EBADF should probably be something else to make
2729 * the error condition distinct.
2731 * XXX This code is specific to AppleTalk protocol support, and
2732 * XXX should be conditionally compiled
2735 fp_getfatalk(proc_t p
, int fd
, struct fileproc
**resultfp
,
2736 struct atalk
**resultatalk
)
2738 struct filedesc
*fdp
= p
->p_fd
;
2739 struct fileproc
*fp
;
2741 proc_fdlock_spin(p
);
2742 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2743 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2744 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2748 if (fp
->f_type
!= (DTYPE_ATALK
+1)) {
2757 *resultatalk
= (struct atalk
*)fp
->f_data
;
2767 * Description: Get fileproc pointer for a given fd from the per process
2768 * open file table of the specified process and if successful,
2769 * increment the f_iocount
2771 * Parameters: p Process in which fd lives
2772 * fd fd to get information for
2773 * resultfp Pointer to result fileproc
2774 * pointer area, or 0 if none
2775 * locked !0 if the caller holds the
2776 * proc_fdlock, 0 otherwise
2778 * Returns: 0 Success
2779 * EBADF Bad file descriptor
2782 * *resultfp (modified) Fileproc pointer
2784 * Locks: If the argument 'locked' is non-zero, then the caller is
2785 * expected to have taken and held the proc_fdlock; if it is
2786 * zero, than this routine internally takes and drops this lock.
2789 fp_lookup(proc_t p
, int fd
, struct fileproc
**resultfp
, int locked
)
2791 struct filedesc
*fdp
= p
->p_fd
;
2792 struct fileproc
*fp
;
2795 proc_fdlock_spin(p
);
2796 if (fd
< 0 || fdp
== NULL
|| fd
>= fdp
->fd_nfiles
||
2797 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2798 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2817 * Description: Set the FP_WRITTEN flag on the fileproc and drop the I/O
2818 * reference previously taken by calling fp_lookup et. al.
2820 * Parameters: p Process in which the fd lives
2821 * fd fd associated with the fileproc
2822 * fp fileproc on which to set the
2823 * flag and drop the reference
2825 * Returns: 0 Success
2826 * fp_drop:EBADF Bad file descriptor
2828 * Locks: This function internally takes and drops the proc_fdlock for
2829 * the supplied process
2831 * Notes: The fileproc must correspond to the fd in the supplied proc
2834 fp_drop_written(proc_t p
, int fd
, struct fileproc
*fp
)
2838 proc_fdlock_spin(p
);
2840 fp
->f_flags
|= FP_WRITTEN
;
2842 error
= fp_drop(p
, fd
, fp
, 1);
2853 * Description: Set the FP_WAITEVENT flag on the fileproc and drop the I/O
2854 * reference previously taken by calling fp_lookup et. al.
2856 * Parameters: p Process in which the fd lives
2857 * fd fd associated with the fileproc
2858 * fp fileproc on which to set the
2859 * flag and drop the reference
2861 * Returns: 0 Success
2862 * fp_drop:EBADF Bad file descriptor
2864 * Locks: This function internally takes and drops the proc_fdlock for
2865 * the supplied process
2867 * Notes: The fileproc must correspond to the fd in the supplied proc
2870 fp_drop_event(proc_t p
, int fd
, struct fileproc
*fp
)
2874 proc_fdlock_spin(p
);
2876 fp
->f_flags
|= FP_WAITEVENT
;
2878 error
= fp_drop(p
, fd
, fp
, 1);
2889 * Description: Drop the I/O reference previously taken by calling fp_lookup
2892 * Parameters: p Process in which the fd lives
2893 * fd fd associated with the fileproc
2894 * fp fileproc on which to set the
2895 * flag and drop the reference
2896 * locked flag to internally take and
2897 * drop proc_fdlock if it is not
2898 * already held by the caller
2900 * Returns: 0 Success
2901 * EBADF Bad file descriptor
2903 * Locks: This function internally takes and drops the proc_fdlock for
2904 * the supplied process if 'locked' is non-zero, and assumes that
2905 * the caller already holds this lock if 'locked' is non-zero.
2907 * Notes: The fileproc must correspond to the fd in the supplied proc
2910 fp_drop(proc_t p
, int fd
, struct fileproc
*fp
, int locked
)
2912 struct filedesc
*fdp
= p
->p_fd
;
2916 proc_fdlock_spin(p
);
2917 if ((fp
== FILEPROC_NULL
) && (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2918 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2919 ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
2920 !(fdp
->fd_ofileflags
[fd
] & UF_CLOSING
)))) {
2927 if (p
->p_fpdrainwait
&& fp
->f_iocount
== 0) {
2928 p
->p_fpdrainwait
= 0;
2934 wakeup(&p
->p_fpdrainwait
);
2943 * Description: Given an fd, look it up in the current process's per process
2944 * open file table, and return its internal vnode pointer.
2946 * Parameters: fd fd to obtain vnode from
2947 * vpp pointer to vnode return area
2949 * Returns: 0 Success
2950 * EINVAL The fd does not refer to a
2951 * vnode fileproc entry
2952 * fp_lookup:EBADF Bad file descriptor
2955 * *vpp (modified) Returned vnode pointer
2957 * Locks: This function internally takes and drops the proc_fdlock for
2958 * the current process
2960 * Notes: If successful, this function increments the f_iocount on the
2961 * fd's corresponding fileproc.
2963 * The fileproc referenced is not returned; because of this, care
2964 * must be taken to not drop the last reference (e.g. by closing
2965 * the file). This is inhernely unsafe, since the reference may
2966 * not be recoverable from the vnode, if there is a subsequent
2967 * close that destroys the associate fileproc. The caller should
2968 * therefore retain their own reference on the fileproc so that
2969 * the f_iocount can be dropped subsequently. Failure to do this
2970 * can result in the returned pointer immediately becoming invalid
2971 * following the call.
2973 * Use of this function is discouraged.
2976 file_vnode(int fd
, struct vnode
**vpp
)
2978 proc_t p
= current_proc();
2979 struct fileproc
*fp
;
2982 proc_fdlock_spin(p
);
2983 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
2987 if (fp
->f_type
!= DTYPE_VNODE
) {
2988 fp_drop(p
, fd
, fp
,1);
2992 *vpp
= (struct vnode
*)fp
->f_data
;
3002 * Description: Given an fd, look it up in the current process's per process
3003 * open file table, and return its internal socket pointer.
3005 * Parameters: fd fd to obtain vnode from
3006 * sp pointer to socket return area
3008 * Returns: 0 Success
3009 * ENOTSOCK Not a socket
3010 * fp_lookup:EBADF Bad file descriptor
3013 * *sp (modified) Returned socket pointer
3015 * Locks: This function internally takes and drops the proc_fdlock for
3016 * the current process
3018 * Notes: If successful, this function increments the f_iocount on the
3019 * fd's corresponding fileproc.
3021 * The fileproc referenced is not returned; because of this, care
3022 * must be taken to not drop the last reference (e.g. by closing
3023 * the file). This is inhernely unsafe, since the reference may
3024 * not be recoverable from the socket, if there is a subsequent
3025 * close that destroys the associate fileproc. The caller should
3026 * therefore retain their own reference on the fileproc so that
3027 * the f_iocount can be dropped subsequently. Failure to do this
3028 * can result in the returned pointer immediately becoming invalid
3029 * following the call.
3031 * Use of this function is discouraged.
3034 file_socket(int fd
, struct socket
**sp
)
3036 proc_t p
= current_proc();
3037 struct fileproc
*fp
;
3040 proc_fdlock_spin(p
);
3041 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
3045 if (fp
->f_type
!= DTYPE_SOCKET
) {
3046 fp_drop(p
, fd
, fp
,1);
3050 *sp
= (struct socket
*)fp
->f_data
;
3060 * Description: Given an fd, look it up in the current process's per process
3061 * open file table, and return its fileproc's flags field.
3063 * Parameters: fd fd whose flags are to be
3065 * flags pointer to flags data area
3067 * Returns: 0 Success
3068 * ENOTSOCK Not a socket
3069 * fp_lookup:EBADF Bad file descriptor
3072 * *flags (modified) Returned flags field
3074 * Locks: This function internally takes and drops the proc_fdlock for
3075 * the current process
3077 * Notes: This function will internally increment and decrement the
3078 * f_iocount of the fileproc as part of its operation.
3081 file_flags(int fd
, int *flags
)
3084 proc_t p
= current_proc();
3085 struct fileproc
*fp
;
3088 proc_fdlock_spin(p
);
3089 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
3093 *flags
= (int)fp
->f_flag
;
3094 fp_drop(p
, fd
, fp
,1);
3104 * Description: Drop an iocount reference on an fd, and wake up any waiters
3105 * for draining (i.e. blocked in fileproc_drain() called during
3106 * the last attempt to close a file).
3108 * Parameters: fd fd on which an ioreference is
3111 * Returns: 0 Success
3112 * EBADF Bad file descriptor
3114 * Description: Given an fd, look it up in the current process's per process
3115 * open file table, and drop it's fileproc's f_iocount by one
3117 * Notes: This is intended as a corresponding operation to the functions
3118 * file_vnode() and file_socket() operations.
3120 * Technically, the close reference is supposed to be protected
3121 * by a fileproc_drain(), however, a drain will only block if
3122 * the fd refers to a character device, and that device has had
3123 * preparefileread() called on it. If it refers to something
3124 * other than a character device, then the drain will occur and
3125 * block each close attempt, rather than merely the last close.
3127 * Since it's possible for an fd that refers to a character
3128 * device to have an intermediate close followed by an open to
3129 * cause a different file to correspond to that descriptor,
3130 * unless there was a cautionary reference taken on the fileproc,
3131 * this is an inherently unsafe function. This happens in the
3132 * case where multiple fd's in a process refer to the same
3133 * character device (e.g. stdin/out/err pointing to a tty, etc.).
3135 * Use of this function is discouraged.
3140 struct fileproc
*fp
;
3141 proc_t p
= current_proc();
3144 proc_fdlock_spin(p
);
3145 if (fd
< 0 || fd
>= p
->p_fd
->fd_nfiles
||
3146 (fp
= p
->p_fd
->fd_ofiles
[fd
]) == NULL
||
3147 ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
3148 !(p
->p_fd
->fd_ofileflags
[fd
] & UF_CLOSING
))) {
3154 if (p
->p_fpdrainwait
&& fp
->f_iocount
== 0) {
3155 p
->p_fpdrainwait
= 0;
3161 wakeup(&p
->p_fpdrainwait
);
3169 * Description: Allocate an entry in the per process open file table and
3170 * return the corresponding fileproc and fd.
3172 * Parameters: p The process in whose open file
3173 * table the fd is to be allocated
3174 * resultfp Pointer to fileproc pointer
3176 * resultfd Pointer to fd return area
3179 * Returns: 0 Success
3180 * falloc:ENFILE Too many open files in system
3181 * falloc:EMFILE Too many open files in process
3182 * falloc:ENOMEM M_FILEPROC or M_FILEGLOB zone
3186 * *resultfd (modified) Returned fileproc pointer
3187 * *resultfd (modified) Returned fd
3189 * Locks: This function takes and drops the proc_fdlock; if this lock
3190 * is alread held, use falloc_locked() instead.
3192 * Notes: This function takes separate process and context arguments
3193 * solely to support kern_exec.c; otherwise, it would take
3194 * neither, and expect falloc_locked() to use the
3195 * vfs_context_current() routine internally.
3198 falloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
)
3203 error
= falloc_locked(p
, resultfp
, resultfd
, ctx
, 1);
3213 * Create a new open file structure and allocate
3214 * a file decriptor for the process that refers to it.
3216 * Returns: 0 Success
3218 * Description: Allocate an entry in the per process open file table and
3219 * return the corresponding fileproc and fd.
3221 * Parameters: p The process in whose open file
3222 * table the fd is to be allocated
3223 * resultfp Pointer to fileproc pointer
3225 * resultfd Pointer to fd return area
3227 * locked Flag to indicate whether the
3228 * caller holds proc_fdlock
3230 * Returns: 0 Success
3231 * ENFILE Too many open files in system
3232 * fdalloc:EMFILE Too many open files in process
3233 * ENOMEM M_FILEPROC or M_FILEGLOB zone
3238 * *resultfd (modified) Returned fileproc pointer
3239 * *resultfd (modified) Returned fd
3241 * Locks: If the parameter 'locked' is zero, this function takes and
3242 * drops the proc_fdlock; if non-zero, the caller must hold the
3245 * Notes: If you intend to use a non-zero 'locked' parameter, use the
3246 * utility function falloc() instead.
3248 * This function takes separate process and context arguments
3249 * solely to support kern_exec.c; otherwise, it would take
3250 * neither, and use the vfs_context_current() routine internally.
3253 falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
3254 vfs_context_t ctx
, int locked
)
3256 struct fileproc
*fp
, *fq
;
3257 struct fileglob
*fg
;
3262 if ( (error
= fdalloc(p
, 0, &nfd
)) ) {
3267 if (nfiles
>= maxfiles
) {
3274 error
= mac_file_check_create(proc_ucred(p
));
3283 * Allocate a new file descriptor.
3284 * If the process has file descriptor zero open, add to the list
3285 * of open files at that point, otherwise put it at the front of
3286 * the list of open files.
3290 MALLOC_ZONE(fp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
3296 MALLOC_ZONE(fg
, struct fileglob
*, sizeof(struct fileglob
), M_FILEGLOB
, M_WAITOK
);
3298 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
3303 bzero(fp
, sizeof(struct fileproc
));
3304 bzero(fg
, sizeof(struct fileglob
));
3305 lck_mtx_init(&fg
->fg_lock
, file_lck_grp
, file_lck_attr
);
3311 mac_file_label_init(fg
);
3314 kauth_cred_ref(ctx
->vc_ucred
);
3318 fp
->f_cred
= ctx
->vc_ucred
;
3321 mac_file_label_associate(fp
->f_cred
, fg
);
3324 lck_mtx_lock_spin(file_flist_lock
);
3328 if ( (fq
= p
->p_fd
->fd_ofiles
[0]) ) {
3329 LIST_INSERT_AFTER(fq
->f_fglob
, fg
, f_list
);
3331 LIST_INSERT_HEAD(&filehead
, fg
, f_list
);
3333 lck_mtx_unlock(file_flist_lock
);
3335 p
->p_fd
->fd_ofiles
[nfd
] = fp
;
3352 * Description: Free a file structure; drop the global open file count, and
3353 * drop the credential reference, if the fileglob has one, and
3354 * destroy the instance mutex before freeing
3356 * Parameters: fg Pointer to fileglob to be
3362 fg_free(struct fileglob
*fg
)
3364 lck_mtx_lock_spin(file_flist_lock
);
3365 LIST_REMOVE(fg
, f_list
);
3367 lck_mtx_unlock(file_flist_lock
);
3369 if (IS_VALID_CRED(fg
->fg_cred
)) {
3370 kauth_cred_unref(&fg
->fg_cred
);
3372 lck_mtx_destroy(&fg
->fg_lock
, file_lck_grp
);
3375 mac_file_label_destroy(fg
);
3377 FREE_ZONE(fg
, sizeof *fg
, M_FILEGLOB
);
3384 * Description: Perform close-on-exec processing for all files in a process
3385 * that are either marked as close-on-exec, or which were in the
3386 * process of being opened at the time of the execve
3388 * Parameters: p Pointer to process calling
3393 * Locks: This function internally takes and drops proc_fdlock()
3395 * Notes: This function drops and retakes the kernel funnel; this is
3396 * inherently unsafe, since another thread may have the
3399 * XXX: We should likely reverse the lock and funnel drop/acquire
3400 * order to avoid the small race window; it's also possible that
3401 * if the program doing the exec has an outstanding listen socket
3402 * and a network connection is completed asyncrhonously that we
3403 * will end up with a "ghost" socket reference in the new process.
3405 * This needs reworking to make it safe to remove the funnel from
3406 * the execve and posix_spawn system calls.
3411 struct filedesc
*fdp
= p
->p_fd
;
3412 int i
= fdp
->fd_lastfile
;
3413 struct fileproc
*fp
;
3418 fp
= fdp
->fd_ofiles
[i
];
3420 ((fdp
->fd_ofileflags
[i
] & (UF_RESERVED
|UF_EXCLOSE
)) == UF_EXCLOSE
)
3422 || (fp
&& mac_file_check_inherit(proc_ucred(p
), fp
->f_fglob
))
3425 if (i
< fdp
->fd_knlistsize
)
3426 knote_fdclose(p
, i
);
3427 procfdtbl_clearfd(p
, i
);
3428 if (i
== fdp
->fd_lastfile
&& i
> 0)
3430 if (i
< fdp
->fd_freefile
)
3431 fdp
->fd_freefile
= i
;
3432 closef_locked(fp
, fp
->f_fglob
, p
);
3433 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
3444 * Description: Copy a filedesc structure. This is normally used as part of
3445 * forkproc() when forking a new process, to copy the per process
3446 * open file table over to the new process.
3448 * Parameters: p Process whose open file table
3449 * is to be copied (parent)
3450 * uth_cdir Per thread current working
3451 * cirectory, or NULL
3453 * Returns: NULL Copy failed
3454 * !NULL Pointer to new struct filedesc
3456 * Locks: This function internally takes and drops proc_fdlock()
3458 * Notes: Files are copied directly, ignoring the new resource limits
3459 * for the process that's being copied into. Since the descriptor
3460 * references are just additional references, this does not count
3461 * against the number of open files on the system.
3463 * The struct filedesc includes the current working directory,
3464 * and the current root directory, if the process is chroot'ed.
3466 * If the exec was called by a thread using a per thread current
3467 * working directory, we inherit the working directory from the
3468 * thread making the call, rather than from the process.
3470 * In the case of a failure to obtain a reference, for most cases,
3471 * the file entry will be silently droppped. There's an exception
3472 * for the case of a chroot dir, since a failure to to obtain a
3473 * reference there would constitute an "escape" from the chroot
3474 * environment, which must not be allowed. In that case, we will
3475 * deny the execve() operation, rather than allowing the escape.
3478 fdcopy(proc_t p
, vnode_t uth_cdir
)
3480 struct filedesc
*newfdp
, *fdp
= p
->p_fd
;
3482 struct fileproc
*ofp
, *fp
;
3485 MALLOC_ZONE(newfdp
, struct filedesc
*,
3486 sizeof(*newfdp
), M_FILEDESC
, M_WAITOK
);
3493 * the FD_CHROOT flag will be inherited via this copy
3495 (void) memcpy(newfdp
, fdp
, sizeof(*newfdp
));
3498 * If we are running with per-thread current working directories,
3499 * inherit the new current working directory from the current thread
3500 * instead, before we take our references.
3502 if (uth_cdir
!= NULLVP
)
3503 newfdp
->fd_cdir
= uth_cdir
;
3506 * For both fd_cdir and fd_rdir make sure we get
3507 * a valid reference... if we can't, than set
3508 * set the pointer(s) to NULL in the child... this
3509 * will keep us from using a non-referenced vp
3510 * and allows us to do the vnode_rele only on
3511 * a properly referenced vp
3513 if ( (v_dir
= newfdp
->fd_cdir
) ) {
3514 if (vnode_getwithref(v_dir
) == 0) {
3515 if ( (vnode_ref(v_dir
)) )
3516 newfdp
->fd_cdir
= NULL
;
3519 newfdp
->fd_cdir
= NULL
;
3521 if (newfdp
->fd_cdir
== NULL
&& fdp
->fd_cdir
) {
3523 * we couldn't get a new reference on
3524 * the current working directory being
3525 * inherited... we might as well drop
3526 * our reference from the parent also
3527 * since the vnode has gone DEAD making
3528 * it useless... by dropping it we'll
3529 * be that much closer to recyling it
3531 vnode_rele(fdp
->fd_cdir
);
3532 fdp
->fd_cdir
= NULL
;
3535 if ( (v_dir
= newfdp
->fd_rdir
) ) {
3536 if (vnode_getwithref(v_dir
) == 0) {
3537 if ( (vnode_ref(v_dir
)) )
3538 newfdp
->fd_rdir
= NULL
;
3541 newfdp
->fd_rdir
= NULL
;
3544 /* Coming from a chroot environment and unable to get a reference... */
3545 if (newfdp
->fd_rdir
== NULL
&& fdp
->fd_rdir
) {
3547 * We couldn't get a new reference on
3548 * the chroot directory being
3549 * inherited... this is fatal, since
3550 * otherwise it would constitute an
3551 * escape from a chroot environment by
3554 if (newfdp
->fd_cdir
)
3555 vnode_rele(newfdp
->fd_cdir
);
3556 FREE_ZONE(newfdp
, sizeof *newfdp
, M_FILEDESC
);
3559 newfdp
->fd_refcnt
= 1;
3562 * If the number of open files fits in the internal arrays
3563 * of the open file structure, use them, otherwise allocate
3564 * additional memory for the number of descriptors currently
3567 if (newfdp
->fd_lastfile
< NDFILE
)
3571 * Compute the smallest multiple of NDEXTENT needed
3572 * for the file descriptors currently in use,
3573 * allowing the table to shrink.
3575 i
= newfdp
->fd_nfiles
;
3576 while (i
> 2 * NDEXTENT
&& i
> newfdp
->fd_lastfile
* 2)
3581 MALLOC_ZONE(newfdp
->fd_ofiles
, struct fileproc
**,
3582 i
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
3583 if (newfdp
->fd_ofiles
== NULL
) {
3584 if (newfdp
->fd_cdir
)
3585 vnode_rele(newfdp
->fd_cdir
);
3586 if (newfdp
->fd_rdir
)
3587 vnode_rele(newfdp
->fd_rdir
);
3589 FREE_ZONE(newfdp
, sizeof(*newfdp
), M_FILEDESC
);
3592 (void) memset(newfdp
->fd_ofiles
, 0, i
* OFILESIZE
);
3595 newfdp
->fd_ofileflags
= (char *) &newfdp
->fd_ofiles
[i
];
3596 newfdp
->fd_nfiles
= i
;
3598 if (fdp
->fd_nfiles
> 0) {
3599 struct fileproc
**fpp
;
3602 (void) memcpy(newfdp
->fd_ofiles
, fdp
->fd_ofiles
,
3603 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofiles
));
3604 (void) memcpy(newfdp
->fd_ofileflags
, fdp
->fd_ofileflags
,
3605 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofileflags
));
3608 * kq descriptors cannot be copied.
3610 if (newfdp
->fd_knlistsize
!= -1) {
3611 fpp
= &newfdp
->fd_ofiles
[newfdp
->fd_lastfile
];
3612 for (i
= newfdp
->fd_lastfile
; i
>= 0; i
--, fpp
--) {
3613 if (*fpp
!= NULL
&& (*fpp
)->f_type
== DTYPE_KQUEUE
) {
3615 if (i
< newfdp
->fd_freefile
)
3616 newfdp
->fd_freefile
= i
;
3618 if (*fpp
== NULL
&& i
== newfdp
->fd_lastfile
&& i
> 0)
3619 newfdp
->fd_lastfile
--;
3621 newfdp
->fd_knlist
= NULL
;
3622 newfdp
->fd_knlistsize
= -1;
3623 newfdp
->fd_knhash
= NULL
;
3624 newfdp
->fd_knhashmask
= 0;
3626 fpp
= newfdp
->fd_ofiles
;
3627 flags
= newfdp
->fd_ofileflags
;
3629 for (i
= newfdp
->fd_lastfile
+ 1; --i
>= 0; fpp
++, flags
++)
3630 if ((ofp
= *fpp
) != NULL
&& !(*flags
& UF_RESERVED
)) {
3631 MALLOC_ZONE(fp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
3634 * XXX no room to copy, unable to
3635 * XXX safely unwind state at present
3639 bzero(fp
, sizeof(struct fileproc
));
3640 fp
->f_flags
= ofp
->f_flags
;
3641 //fp->f_iocount = ofp->f_iocount;
3643 fp
->f_fglob
= ofp
->f_fglob
;
3648 if (i
< newfdp
->fd_freefile
)
3649 newfdp
->fd_freefile
= i
;
3663 * Description: Release a filedesc (per process open file table) structure;
3664 * this is done on process exit(), or from forkproc_free() if
3665 * the fork fails for some reason subsequent to a successful
3668 * Parameters: p Pointer to process going away
3672 * Locks: This function internally takes and drops proc_fdlock()
3677 struct filedesc
*fdp
;
3678 struct fileproc
*fp
;
3683 /* Certain daemons might not have file descriptors */
3686 if ((fdp
== NULL
) || (--fdp
->fd_refcnt
> 0)) {
3690 if (fdp
->fd_refcnt
== 0xffff)
3691 panic("fdfree: bad fd_refcnt");
3693 /* Last reference: the structure can't change out from under us */
3695 if (fdp
->fd_nfiles
> 0 && fdp
->fd_ofiles
) {
3696 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
3697 if ((fp
= fdp
->fd_ofiles
[i
]) != NULL
) {
3699 if (fdp
->fd_ofileflags
[i
] & UF_RESERVED
)
3700 panic("fdfree: found fp with UF_RESERVED\n");
3702 /* closef drops the iocount ... */
3703 if ((fp
->f_flags
& FP_INCHRREAD
) != 0)
3705 procfdtbl_reservefd(p
, i
);
3707 if (i
< fdp
->fd_knlistsize
)
3708 knote_fdclose(p
, i
);
3709 if (fp
->f_flags
& FP_WAITEVENT
)
3710 (void)waitevent_close(p
, fp
);
3711 (void) closef_locked(fp
, fp
->f_fglob
, p
);
3712 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
3715 FREE_ZONE(fdp
->fd_ofiles
, fdp
->fd_nfiles
* OFILESIZE
, M_OFILETABL
);
3716 fdp
->fd_ofiles
= NULL
;
3723 vnode_rele(fdp
->fd_cdir
);
3725 vnode_rele(fdp
->fd_rdir
);
3727 proc_fdlock_spin(p
);
3732 FREE(fdp
->fd_knlist
, M_KQUEUE
);
3734 FREE(fdp
->fd_knhash
, M_KQUEUE
);
3736 FREE_ZONE(fdp
, sizeof(*fdp
), M_FILEDESC
);
3743 * Description: Called on last open instance for a fileglob for a file being
3746 * Parameters: fp Pointer to fileproc for fd
3747 * fg Pointer to fileglob for fd
3748 * p Pointer to proc structure
3750 * Returns: 0 Success
3751 * <fo_close>:??? Anything returnable by a per-fileops
3754 * Note: fp can only be non-NULL if p is also non-NULL. If p is NULL,
3755 * then fg must eith be locked (FHASLOCK) or must not have a
3756 * type of DTYPE_VNODE.
3758 * On return, the fg is freed.
3760 * This function may block draining output to a character
3761 * device on last close of that device.
3764 closef_finish(struct fileproc
*fp
, struct fileglob
*fg
, proc_t p
, vfs_context_t ctx
)
3769 /* fg_ops completed initialization? */
3771 error
= fo_close(fg
, ctx
);
3775 /* if fp is non-NULL, drain it out */
3776 if (((fp
!= (struct fileproc
*)0) && ((fp
->f_flags
& FP_INCHRREAD
) != 0))) {
3777 proc_fdlock_spin(p
);
3778 if ( ((fp
->f_flags
& FP_INCHRREAD
) != 0) ) {
3779 fileproc_drain(p
, fp
);
3791 * Description: Internal form of closef; called with proc_fdlock held
3793 * Parameters: fp Pointer to fileproc for fd
3794 * fg Pointer to fileglob for fd
3795 * p Pointer to proc structure
3797 * Returns: 0 Success
3798 * closef_finish:??? Anything returnable by a per-fileops
3801 * Note: Decrements reference count on file structure; if this was the
3802 * last reference, then closef_finish() is called
3804 * p and fp are allowed to be NULL when closing a file that was
3805 * being passed in a message (but only if we are called when this
3806 * is NOT the last reference).
3809 closef_locked(struct fileproc
*fp
, struct fileglob
*fg
, proc_t p
)
3813 struct vfs_context context
;
3820 /* Set up context with cred stashed in fg */
3821 if (p
== current_proc())
3822 context
.vc_thread
= current_thread();
3824 context
.vc_thread
= NULL
;
3825 context
.vc_ucred
= fg
->fg_cred
;
3828 * POSIX record locking dictates that any close releases ALL
3829 * locks owned by this process. This is handled by setting
3830 * a flag in the unlock to free ONLY locks obeying POSIX
3831 * semantics, and not to free BSD-style file locks.
3832 * If the descriptor was in a message, POSIX-style locks
3833 * aren't passed with the descriptor.
3835 if (p
&& (p
->p_ladvflag
& P_LADVLOCK
) && fg
->fg_type
== DTYPE_VNODE
) {
3838 lf
.l_whence
= SEEK_SET
;
3841 lf
.l_type
= F_UNLCK
;
3842 vp
= (struct vnode
*)fg
->fg_data
;
3844 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
3845 (void) VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &lf
, F_POSIX
, &context
);
3846 (void)vnode_put(vp
);
3850 lck_mtx_lock_spin(&fg
->fg_lock
);
3853 if (fg
->fg_count
> 0) {
3854 lck_mtx_unlock(&fg
->fg_lock
);
3858 if (fg
->fg_count
!= 0)
3859 panic("fg %p: being freed with bad fg_count (%d)", fg
, fg
->fg_count
);
3862 if (fp
&& (fp
->f_flags
& FP_WRITTEN
))
3863 fg
->fg_flag
|= FWASWRITTEN
;
3865 fg
->fg_lflags
|= FG_TERM
;
3866 lck_mtx_unlock(&fg
->fg_lock
);
3869 error
= closef_finish(fp
, fg
, p
, &context
);
3876 /* sleep address to permit wakeup of select by fileproc_drain() */
3883 * Description: Drain out pending I/O operations
3885 * Parameters: p Process closing this file
3886 * fp fileproc struct for the open
3887 * instance on the file
3891 * Locks: Assumes the caller holds the proc_fdlock
3893 * Notes: For character devices, this occurs on the last close of the
3894 * device; for all other file descriptos, this occurs on each
3895 * close to prevent fd's from being closed out from under
3896 * operations currently in progress and blocked
3898 * See Also: file_vnode(), file_socket(), file_drop(), and the cautions
3899 * regarding their use and interaction with this function.
3902 fileproc_drain(proc_t p
, struct fileproc
* fp
)
3904 struct vfs_context context
;
3906 context
.vc_thread
= proc_thread(p
); /* XXX */
3907 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
3909 fp
->f_iocount
-- ; /* (the one the close holds) */
3911 while (fp
->f_iocount
) {
3913 lck_mtx_convert_spin(&p
->p_fdmlock
);
3915 if (((fp
->f_flags
& FP_INSELECT
)== FP_INSELECT
)) {
3916 wait_queue_wakeup_all((wait_queue_t
)fp
->f_waddr
, &selwait
, THREAD_INTERRUPTED
);
3918 if (fp
->f_fglob
->fg_ops
->fo_drain
) {
3919 (*fp
->f_fglob
->fg_ops
->fo_drain
)(fp
, &context
);
3922 p
->p_fpdrainwait
= 1;
3924 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
, "fpdrain", NULL
);
3933 * Description: Release the fd and free the fileproc associated with the fd
3934 * in the per process open file table of the specified process;
3935 * these values must correspond.
3937 * Parameters: p Process containing fd
3938 * fd fd to be released
3939 * fp fileproc to be freed
3941 * Returns: 0 Success
3943 * Notes: XXX function should be void - no one interprets the returns
3947 fp_free(proc_t p
, int fd
, struct fileproc
* fp
)
3949 proc_fdlock_spin(p
);
3953 fg_free(fp
->f_fglob
);
3954 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
3962 * Description: Apply an advisory lock on a file descriptor.
3964 * Parameters: p Process making request
3965 * uap->fd fd on which the lock is to be
3967 * uap->how (Un)Lock bits, including type
3968 * retval Pointer to the call return area
3970 * Returns: 0 Success
3971 * fp_getfvp:EBADF Bad file descriptor
3972 * fp_getfvp:ENOTSUP fd does not refer to a vnode
3973 * vnode_getwithref:???
3977 * *retval (modified) Size of dtable
3979 * Notes: Just attempt to get a record lock of the requested type on
3980 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
3983 flock(proc_t p
, struct flock_args
*uap
, __unused register_t
*retval
)
3987 struct fileproc
*fp
;
3990 vfs_context_t ctx
= vfs_context_current();
3993 AUDIT_ARG(fd
, uap
->fd
);
3994 if ( (error
= fp_getfvp(p
, fd
, &fp
, &vp
)) ) {
3997 if ( (error
= vnode_getwithref(vp
)) ) {
4000 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
4002 lf
.l_whence
= SEEK_SET
;
4005 if (how
& LOCK_UN
) {
4006 lf
.l_type
= F_UNLCK
;
4007 fp
->f_flag
&= ~FHASLOCK
;
4008 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_UNLCK
, &lf
, F_FLOCK
, ctx
);
4012 lf
.l_type
= F_WRLCK
;
4013 else if (how
& LOCK_SH
)
4014 lf
.l_type
= F_RDLCK
;
4020 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
, F_SETLK
, &lf
);
4024 fp
->f_flag
|= FHASLOCK
;
4025 if (how
& LOCK_NB
) {
4026 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, F_FLOCK
, ctx
);
4029 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, F_FLOCK
|F_WAIT
, ctx
);
4031 (void)vnode_put(vp
);
4033 fp_drop(p
, fd
, fp
, 0);
4041 * Description: Duplicate the specified descriptor to a free descriptor;
4042 * this is the second half of fdopen(), above.
4044 * Parameters: fdp filedesc pointer to fill in
4046 * dfd fd to dup from
4047 * mode mode to set on new fd
4048 * error command code
4050 * Returns: 0 Success
4051 * EBADF Source fd is bad
4052 * EACCES Requested mode not allowed
4053 * !0 'error', if not ENODEV or
4056 * Notes: XXX This is not thread safe; see fdopen() above
4059 dupfdopen(struct filedesc
*fdp
, int indx
, int dfd
, int mode
, int error
)
4061 struct fileproc
*wfp
;
4062 struct fileproc
*fp
;
4066 proc_t p
= current_proc();
4069 * If the to-be-dup'd fd number is greater than the allowed number
4070 * of file descriptors, or the fd to be dup'd has already been
4071 * closed, reject. Note, check for new == old is necessary as
4072 * falloc could allocate an already closed to-be-dup'd descriptor
4073 * as the new descriptor.
4077 fp
= fdp
->fd_ofiles
[indx
];
4078 if (dfd
< 0 || dfd
>= fdp
->fd_nfiles
||
4079 (wfp
= fdp
->fd_ofiles
[dfd
]) == NULL
|| wfp
== fp
||
4080 (fdp
->fd_ofileflags
[dfd
] & UF_RESERVED
)) {
4086 myerror
= mac_file_check_dup(proc_ucred(p
), wfp
->f_fglob
, dfd
);
4093 * There are two cases of interest here.
4095 * For ENODEV simply dup (dfd) to file descriptor
4096 * (indx) and return.
4098 * For ENXIO steal away the file structure from (dfd) and
4099 * store it in (indx). (dfd) is effectively closed by
4102 * Any other error code is just returned.
4107 * Check that the mode the file is being opened for is a
4108 * subset of the mode of the existing descriptor.
4110 if (((mode
& (FREAD
|FWRITE
)) | wfp
->f_flag
) != wfp
->f_flag
) {
4114 if (indx
> fdp
->fd_lastfile
)
4115 fdp
->fd_lastfile
= indx
;
4119 fg_free(fp
->f_fglob
);
4120 fp
->f_fglob
= wfp
->f_fglob
;
4122 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
];
4138 * Description: Add a reference to a fileglob by fileproc
4140 * Parameters: fp fileproc containing fileglob
4145 * Notes: XXX Should use OSAddAtomic?
4148 fg_ref(struct fileproc
* fp
)
4150 struct fileglob
*fg
;
4154 lck_mtx_lock_spin(&fg
->fg_lock
);
4157 if ((fp
->f_flags
& ~((unsigned int)FP_VALID_FLAGS
)) != 0)
4158 panic("fg_ref: invalid bits on fp%x\n", (unsigned int)fp
);
4160 if (fg
->fg_count
== 0)
4161 panic("fg_ref: adding fgcount to zeroed fg :fp %x, fg%x\n ", (unsigned int)fp
, (unsigned int)fg
);
4164 lck_mtx_unlock(&fg
->fg_lock
);
4171 * Description: Remove a reference to a fileglob by fileproc
4173 * Parameters: fp fileproc containing fileglob
4178 * Notes: XXX Should use OSAddAtomic?
4181 fg_drop(struct fileproc
* fp
)
4183 struct fileglob
*fg
;
4186 lck_mtx_lock_spin(&fg
->fg_lock
);
4188 lck_mtx_unlock(&fg
->fg_lock
);
4195 * Description: Insert fileglob onto message queue
4197 * Parameters: fg Fileglob pointer to insert
4201 * Locks: Takes and drops fg_lock, potentially many times
4204 fg_insertuipc(struct fileglob
* fg
)
4208 lck_mtx_lock_spin(&fg
->fg_lock
);
4210 while (fg
->fg_lflags
& FG_RMMSGQ
) {
4211 lck_mtx_convert_spin(&fg
->fg_lock
);
4213 fg
->fg_lflags
|= FG_WRMMSGQ
;
4214 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_insertuipc", NULL
);
4219 if (fg
->fg_msgcount
== 1) {
4220 fg
->fg_lflags
|= FG_INSMSGQ
;
4223 lck_mtx_unlock(&fg
->fg_lock
);
4226 lck_mtx_lock_spin(uipc_lock
);
4228 LIST_INSERT_HEAD(&fmsghead
, fg
, f_msglist
);
4229 lck_mtx_unlock(uipc_lock
);
4230 lck_mtx_lock(&fg
->fg_lock
);
4231 fg
->fg_lflags
&= ~FG_INSMSGQ
;
4232 if (fg
->fg_lflags
& FG_WINSMSGQ
) {
4233 fg
->fg_lflags
&= ~FG_WINSMSGQ
;
4234 wakeup(&fg
->fg_lflags
);
4236 lck_mtx_unlock(&fg
->fg_lock
);
4245 * Description: Remove fileglob from message queue
4247 * Parameters: fg Fileglob pointer to remove
4251 * Locks: Takes and drops fg_lock, potentially many times
4254 fg_removeuipc(struct fileglob
* fg
)
4258 lck_mtx_lock_spin(&fg
->fg_lock
);
4259 while (fg
->fg_lflags
& FG_INSMSGQ
) {
4260 lck_mtx_convert_spin(&fg
->fg_lock
);
4262 fg
->fg_lflags
|= FG_WINSMSGQ
;
4263 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_removeuipc", NULL
);
4266 if (fg
->fg_msgcount
== 0) {
4267 fg
->fg_lflags
|= FG_RMMSGQ
;
4270 lck_mtx_unlock(&fg
->fg_lock
);
4273 lck_mtx_lock_spin(uipc_lock
);
4275 LIST_REMOVE(fg
, f_msglist
);
4276 lck_mtx_unlock(uipc_lock
);
4277 lck_mtx_lock(&fg
->fg_lock
);
4278 fg
->fg_lflags
&= ~FG_RMMSGQ
;
4279 if (fg
->fg_lflags
& FG_WRMMSGQ
) {
4280 fg
->fg_lflags
&= ~FG_WRMMSGQ
;
4281 wakeup(&fg
->fg_lflags
);
4283 lck_mtx_unlock(&fg
->fg_lock
);
4291 * Description: Generic fileops read indirected through the fileops pointer
4292 * in the fileproc structure
4294 * Parameters: fp fileproc structure pointer
4295 * uio user I/O structure pointer
4297 * ctx VFS context for operation
4299 * Returns: 0 Success
4300 * !0 Errno from read
4303 fo_read(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
4305 return ((*fp
->f_ops
->fo_read
)(fp
, uio
, flags
, ctx
));
4312 * Description: Generic fileops write indirected through the fileops pointer
4313 * in the fileproc structure
4315 * Parameters: fp fileproc structure pointer
4316 * uio user I/O structure pointer
4318 * ctx VFS context for operation
4320 * Returns: 0 Success
4321 * !0 Errno from write
4324 fo_write(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
4326 return((*fp
->f_ops
->fo_write
)(fp
, uio
, flags
, ctx
));
4333 * Description: Generic fileops ioctl indirected through the fileops pointer
4334 * in the fileproc structure
4336 * Parameters: fp fileproc structure pointer
4338 * data pointer to internalized copy
4339 * of user space ioctl command
4340 * parameter data in kernel space
4341 * ctx VFS context for operation
4343 * Returns: 0 Success
4344 * !0 Errno from ioctl
4346 * Locks: The caller is assumed to have held the proc_fdlock; this
4347 * function releases and reacquires this lock. If the caller
4348 * accesses data protected by this lock prior to calling this
4349 * function, it will need to revalidate/reacquire any cached
4350 * protected data obtained prior to the call.
4353 fo_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
, vfs_context_t ctx
)
4357 proc_fdunlock(vfs_context_proc(ctx
));
4358 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, com
, data
, ctx
);
4359 proc_fdlock(vfs_context_proc(ctx
));
4367 * Description: Generic fileops select indirected through the fileops pointer
4368 * in the fileproc structure
4370 * Parameters: fp fileproc structure pointer
4371 * which select which
4372 * wql pointer to wait queue list
4373 * ctx VFS context for operation
4375 * Returns: 0 Success
4376 * !0 Errno from select
4379 fo_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
4381 return((*fp
->f_ops
->fo_select
)(fp
, which
, wql
, ctx
));
4388 * Description: Generic fileops close indirected through the fileops pointer
4389 * in the fileproc structure
4391 * Parameters: fp fileproc structure pointer for
4393 * ctx VFS context for operation
4395 * Returns: 0 Success
4396 * !0 Errno from close
4399 fo_close(struct fileglob
*fg
, vfs_context_t ctx
)
4401 return((*fg
->fg_ops
->fo_close
)(fg
, ctx
));
4408 * Description: Generic fileops kqueue filter indirected through the fileops
4409 * pointer in the fileproc structure
4411 * Parameters: fp fileproc structure pointer
4412 * kn pointer to knote to filter on
4413 * ctx VFS context for operation
4415 * Returns: 0 Success
4416 * !0 Errno from kqueue filter
4419 fo_kqfilter(struct fileproc
*fp
, struct knote
*kn
, vfs_context_t ctx
)
4421 return ((*fp
->f_ops
->fo_kqfilter
)(fp
, kn
, ctx
));