2 * Copyright (c) 2000-2010 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1982, 1986, 1989, 1991, 1993
31 * The Regents of the University of California. All rights reserved.
32 * (c) UNIX System Laboratories, Inc.
33 * All or some portions of this file are derived from material licensed
34 * to the University of California by American Telephone and Telegraph
35 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
36 * the permission of UNIX System Laboratories, Inc.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgement:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)kern_descrip.c 8.8 (Berkeley) 2/14/95
69 * NOTICE: This file was modified by SPARTA, Inc. in 2006 to introduce
70 * support for mandatory and extensible security protections. This notice
71 * is included in support of clause 2.2 (b) of the Apple Public License,
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/filedesc.h>
78 #include <sys/kernel.h>
79 #include <sys/vnode_internal.h>
80 #include <sys/proc_internal.h>
81 #include <sys/kauth.h>
82 #include <sys/file_internal.h>
83 #include <sys/socket.h>
84 #include <sys/socketvar.h>
86 #include <sys/ioctl.h>
87 #include <sys/fcntl.h>
88 #include <sys/malloc.h>
90 #include <sys/syslog.h>
91 #include <sys/unistd.h>
92 #include <sys/resourcevar.h>
93 #include <sys/aio_kern.h>
95 #include <kern/lock.h>
96 #include <sys/uio_internal.h>
98 #include <security/audit/audit.h>
100 #include <sys/mount_internal.h>
101 #include <sys/kdebug.h>
102 #include <sys/sysproto.h>
103 #include <sys/pipe.h>
104 #include <sys/spawn.h>
105 #include <kern/kern_types.h>
106 #include <kern/kalloc.h>
107 #include <libkern/OSAtomic.h>
109 #include <sys/ubc_internal.h>
111 #include <kern/ipc_misc.h>
112 #include <vm/vm_protos.h>
114 #include <mach/mach_port.h>
117 #include <sys/cprotect.h>
121 kern_return_t
ipc_object_copyin(ipc_space_t
, mach_port_name_t
,
122 mach_msg_type_name_t
, ipc_port_t
*);
123 void ipc_port_release_send(ipc_port_t
);
128 static int finishdup(proc_t p
,
129 struct filedesc
*fdp
, int old
, int new, int flags
, int32_t *retval
);
131 int falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
, int locked
);
132 void fg_drop(struct fileproc
* fp
);
133 void fg_free(struct fileglob
*fg
);
134 void fg_ref(struct fileproc
* fp
);
135 void fileport_releasefg(struct fileglob
*fg
);
137 /* flags for close_internal_locked */
138 #define FD_DUP2RESV 1
139 static int close_internal_locked(struct proc
*p
, int fd
, struct fileproc
*fp
, int flags
);
141 static int closef_finish(struct fileproc
*fp
, struct fileglob
*fg
, proc_t p
, vfs_context_t ctx
);
143 /* We don't want these exported */
145 int open1(vfs_context_t
, struct nameidata
*, int, struct vnode_attr
*, int32_t *);
148 int unlink1(vfs_context_t
, struct nameidata
*, int);
150 static void _fdrelse(struct proc
* p
, int fd
);
153 extern void file_lock_init(void) __attribute__((section("__TEXT, initcode")));
154 extern int kqueue_stat(struct fileproc
*fp
, void *ub
, int isstat4
, proc_t p
);
156 extern int soo_stat(struct socket
*so
, void *ub
, int isstat64
);
159 extern kauth_scope_t kauth_scope_fileop
;
163 /* Conflict wait queue for when selects collide (opaque type) */
164 extern struct wait_queue select_conflict_queue
;
166 #define f_flag f_fglob->fg_flag
167 #define f_type f_fglob->fg_type
168 #define f_msgcount f_fglob->fg_msgcount
169 #define f_cred f_fglob->fg_cred
170 #define f_ops f_fglob->fg_ops
171 #define f_offset f_fglob->fg_offset
172 #define f_data f_fglob->fg_data
174 * Descriptor management.
176 struct filelist filehead
; /* head of list of open files */
177 struct fmsglist fmsghead
; /* head of list of open files */
178 struct fmsglist fmsg_ithead
; /* head of list of open files */
179 int nfiles
; /* actual number of open files */
182 lck_grp_attr_t
* file_lck_grp_attr
;
183 lck_grp_t
* file_lck_grp
;
184 lck_attr_t
* file_lck_attr
;
186 lck_mtx_t
* uipc_lock
;
187 lck_mtx_t
* file_flist_lock
;
193 * Description: Initialize the file lock group and the uipc and flist locks
199 * Notes: Called at system startup from bsd_init().
204 /* allocate file lock group attribute and group */
205 file_lck_grp_attr
= lck_grp_attr_alloc_init();
207 file_lck_grp
= lck_grp_alloc_init("file", file_lck_grp_attr
);
209 /* Allocate file lock attribute */
210 file_lck_attr
= lck_attr_alloc_init();
212 uipc_lock
= lck_mtx_alloc_init(file_lck_grp
, file_lck_attr
);
213 file_flist_lock
= lck_mtx_alloc_init(file_lck_grp
, file_lck_attr
);
218 * proc_fdlock, proc_fdlock_spin
220 * Description: Lock to control access to the per process struct fileproc
221 * and struct filedesc
223 * Parameters: p Process to take the lock on
227 * Notes: The lock is initialized in forkproc() and destroyed in
228 * reap_child_process().
231 proc_fdlock(proc_t p
)
233 lck_mtx_lock(&p
->p_fdmlock
);
237 proc_fdlock_spin(proc_t p
)
239 lck_mtx_lock_spin(&p
->p_fdmlock
);
243 proc_fdlock_assert(proc_t p
, int assertflags
)
245 lck_mtx_assert(&p
->p_fdmlock
, assertflags
);
252 * Description: Unlock the lock previously locked by a call to proc_fdlock()
254 * Parameters: p Process to drop the lock on
259 proc_fdunlock(proc_t p
)
261 lck_mtx_unlock(&p
->p_fdmlock
);
266 * System calls on descriptors.
273 * Description: Returns the per process maximum size of the descriptor table
275 * Parameters: p Process being queried
276 * retval Pointer to the call return area
281 * *retval (modified) Size of dtable
284 getdtablesize(proc_t p
, __unused
struct getdtablesize_args
*uap
, int32_t *retval
)
287 *retval
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
295 procfdtbl_reservefd(struct proc
* p
, int fd
)
297 p
->p_fd
->fd_ofiles
[fd
] = NULL
;
298 p
->p_fd
->fd_ofileflags
[fd
] |= UF_RESERVED
;
302 procfdtbl_markclosefd(struct proc
* p
, int fd
)
304 p
->p_fd
->fd_ofileflags
[fd
] |= (UF_RESERVED
| UF_CLOSING
);
308 procfdtbl_releasefd(struct proc
* p
, int fd
, struct fileproc
* fp
)
311 p
->p_fd
->fd_ofiles
[fd
] = fp
;
312 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESERVED
;
313 if ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
) == UF_RESVWAIT
) {
314 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESVWAIT
;
320 procfdtbl_waitfd(struct proc
* p
, int fd
)
322 p
->p_fd
->fd_ofileflags
[fd
] |= UF_RESVWAIT
;
323 msleep(&p
->p_fd
, &p
->p_fdmlock
, PRIBIO
, "ftbl_waitfd", NULL
);
328 procfdtbl_clearfd(struct proc
* p
, int fd
)
332 waiting
= (p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
);
333 p
->p_fd
->fd_ofiles
[fd
] = NULL
;
334 p
->p_fd
->fd_ofileflags
[fd
] = 0;
335 if ( waiting
== UF_RESVWAIT
) {
343 * Description: Inline utility function to free an fd in a filedesc
345 * Parameters: fdp Pointer to filedesc fd lies in
347 * reserv fd should be reserved
351 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
355 _fdrelse(struct proc
* p
, int fd
)
357 struct filedesc
*fdp
= p
->p_fd
;
360 if (fd
< fdp
->fd_freefile
)
361 fdp
->fd_freefile
= fd
;
363 if (fd
> fdp
->fd_lastfile
)
364 panic("fdrelse: fd_lastfile inconsistent");
366 procfdtbl_clearfd(p
, fd
);
368 while ((nfd
= fdp
->fd_lastfile
) > 0 &&
369 fdp
->fd_ofiles
[nfd
] == NULL
&&
370 !(fdp
->fd_ofileflags
[nfd
] & UF_RESERVED
))
392 char uio_buf
[ UIO_SIZEOF(1) ];
393 struct vfs_context context
= *(vfs_context_current());
397 error
= fp_lookup(p
, fd
, &fp
, 0);
401 if (fp
->f_type
!= DTYPE_VNODE
&& fp
->f_type
!= DTYPE_PIPE
&& fp
->f_type
!= DTYPE_SOCKET
) {
405 if (rw
== UIO_WRITE
&& !(fp
->f_flag
& FWRITE
)) {
410 if (rw
== UIO_READ
&& !(fp
->f_flag
& FREAD
)) {
415 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
417 if (UIO_SEG_IS_USER_SPACE(segflg
))
418 spacetype
= proc_is64bit(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
420 spacetype
= UIO_SYSSPACE
;
422 auio
= uio_createwithbuffer(1, offset
, spacetype
, rw
, &uio_buf
[0], sizeof(uio_buf
));
424 uio_addiov(auio
, base
, len
);
426 if ( !(io_flg
& IO_APPEND
))
430 error
= fo_write(fp
, auio
, flags
, &context
);
432 error
= fo_read(fp
, auio
, flags
, &context
);
435 *aresid
= uio_resid(auio
);
437 if (uio_resid(auio
) && error
== 0)
441 if (rw
== UIO_WRITE
&& error
== 0)
442 fp_drop_written(p
, fd
, fp
);
444 fp_drop(p
, fd
, fp
, 0);
454 * Description: Duplicate a file descriptor.
456 * Parameters: p Process performing the dup
457 * uap->fd The fd to dup
458 * retval Pointer to the call return area
464 * *retval (modified) The new descriptor
467 dup(proc_t p
, struct dup_args
*uap
, int32_t *retval
)
469 struct filedesc
*fdp
= p
->p_fd
;
475 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
479 if ( (error
= fdalloc(p
, 0, &new)) ) {
480 fp_drop(p
, old
, fp
, 1);
484 error
= finishdup(p
, fdp
, old
, new, 0, retval
);
485 fp_drop(p
, old
, fp
, 1);
494 * Description: Duplicate a file descriptor to a particular value.
496 * Parameters: p Process performing the dup
497 * uap->from The fd to dup
498 * uap->to The fd to dup it to
499 * retval Pointer to the call return area
505 * *retval (modified) The new descriptor
508 dup2(proc_t p
, struct dup2_args
*uap
, int32_t *retval
)
510 struct filedesc
*fdp
= p
->p_fd
;
511 int old
= uap
->from
, new = uap
->to
;
513 struct fileproc
*fp
, *nfp
;
518 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
523 (rlim_t
)new >= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
525 fp_drop(p
, old
, fp
, 1);
530 fp_drop(p
, old
, fp
, 1);
535 if (new < 0 || new >= fdp
->fd_nfiles
) {
536 if ( (error
= fdalloc(p
, new, &i
)) ) {
537 fp_drop(p
, old
, fp
, 1);
547 while ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == UF_RESERVED
) {
548 fp_drop(p
, old
, fp
, 1);
549 procfdtbl_waitfd(p
, new);
551 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
556 if ((fdp
->fd_ofiles
[new] != NULL
) &&
557 ((error
= fp_lookup(p
, new, &nfp
, 1)) == 0)) {
558 fp_drop(p
, old
, fp
, 1);
559 (void)close_internal_locked(p
, new, nfp
, FD_DUP2RESV
);
561 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
563 procfdtbl_clearfd(p
, new);
567 if (fdp
->fd_ofiles
[new] != NULL
)
568 panic("dup2: no ref on fileproc %d", new);
570 procfdtbl_reservefd(p
, new);
574 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
579 if (fdp
->fd_ofiles
[new] != 0)
580 panic("dup2: overwriting fd_ofiles with new %d", new);
581 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0)
582 panic("dup2: unreserved fileflags with new %d", new);
584 error
= finishdup(p
, fdp
, old
, new, 0, retval
);
585 fp_drop(p
, old
, fp
, 1);
595 * Description: The file control system call.
597 * Parameters: p Process performing the fcntl
598 * uap->fd The fd to operate against
599 * uap->cmd The command to perform
600 * uap->arg Pointer to the command argument
601 * retval Pointer to the call return area
604 * !0 Errno (see fcntl_nocancel)
607 * *retval (modified) fcntl return value (if any)
609 * Notes: This system call differs from fcntl_nocancel() in that it
610 * tests for cancellation prior to performing a potentially
611 * blocking operation.
614 fcntl(proc_t p
, struct fcntl_args
*uap
, int32_t *retval
)
616 __pthread_testcancel(1);
617 return(fcntl_nocancel(p
, (struct fcntl_nocancel_args
*)uap
, retval
));
624 * Description: A non-cancel-testing file control system call.
626 * Parameters: p Process performing the fcntl
627 * uap->fd The fd to operate against
628 * uap->cmd The command to perform
629 * uap->arg Pointer to the command argument
630 * retval Pointer to the call return area
634 * fp_lookup:EBADF Bad file descriptor
646 * vnode_getwithref:???
653 * vnode_getwithref:???
660 * vnode_getwithref:???
662 * [F_SETSIZE,F_RDADVISE]
665 * vnode_getwithref:???
666 * [F_RDAHEAD,F_NOCACHE]
668 * vnode_getwithref:???
672 * *retval (modified) fcntl return value (if any)
675 fcntl_nocancel(proc_t p
, struct fcntl_nocancel_args
*uap
, int32_t *retval
)
678 struct filedesc
*fdp
= p
->p_fd
;
681 struct vnode
*vp
= NULLVP
; /* for AUDIT_ARG() at end */
682 int i
, tmp
, error
, error2
, flg
= F_POSIX
;
684 struct vfs_context context
;
692 AUDIT_ARG(fd
, uap
->fd
);
693 AUDIT_ARG(cmd
, uap
->cmd
);
696 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
700 context
.vc_thread
= current_thread();
701 context
.vc_ucred
= fp
->f_cred
;
703 is64bit
= proc_is64bit(p
);
709 * Since the arg parameter is defined as a long but may be
710 * either a long or a pointer we must take care to handle
711 * sign extension issues. Our sys call munger will sign
712 * extend a long when we are called from a 32-bit process.
713 * Since we can never have an address greater than 32-bits
714 * from a 32-bit process we lop off the top 32-bits to avoid
715 * getting the wrong address
717 argp
= CAST_USER_ADDR_T((uint32_t)uap
->arg
);
720 pop
= &fdp
->fd_ofileflags
[fd
];
723 error
= mac_file_check_fcntl(proc_ucred(p
), fp
->f_fglob
, uap
->cmd
,
732 case F_DUPFD_CLOEXEC
:
733 newmin
= CAST_DOWN_EXPLICIT(int, uap
->arg
); /* arg is an int, so we won't lose bits */
734 AUDIT_ARG(value32
, newmin
);
735 if ((u_int
)newmin
>= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
736 newmin
>= maxfiles
) {
740 if ( (error
= fdalloc(p
, newmin
, &i
)) )
742 error
= finishdup(p
, fdp
, fd
, i
,
743 uap
->cmd
== F_DUPFD_CLOEXEC
? UF_EXCLOSE
: 0, retval
);
747 *retval
= (*pop
& UF_EXCLOSE
)? 1 : 0;
752 AUDIT_ARG(value32
, uap
->arg
);
753 *pop
= (*pop
&~ UF_EXCLOSE
) |
754 (uap
->arg
& 1)? UF_EXCLOSE
: 0;
759 *retval
= OFLAGS(fp
->f_flag
);
764 fp
->f_flag
&= ~FCNTLFLAGS
;
765 tmp
= CAST_DOWN_EXPLICIT(int, uap
->arg
); /* arg is an int, so we won't lose bits */
766 AUDIT_ARG(value32
, tmp
);
767 fp
->f_flag
|= FFLAGS(tmp
) & FCNTLFLAGS
;
768 tmp
= fp
->f_flag
& FNONBLOCK
;
769 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
772 tmp
= fp
->f_flag
& FASYNC
;
773 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, &context
);
776 fp
->f_flag
&= ~FNONBLOCK
;
778 (void)fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
782 if (fp
->f_type
== DTYPE_SOCKET
) {
783 *retval
= ((struct socket
*)fp
->f_data
)->so_pgid
;
787 error
= fo_ioctl(fp
, (int)TIOCGPGRP
, (caddr_t
)retval
, &context
);
792 tmp
= CAST_DOWN_EXPLICIT(pid_t
, uap
->arg
); /* arg is an int, so we won't lose bits */
793 AUDIT_ARG(value32
, tmp
);
794 if (fp
->f_type
== DTYPE_SOCKET
) {
795 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
799 if (fp
->f_type
== DTYPE_PIPE
) {
800 error
= fo_ioctl(fp
, TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
807 proc_t p1
= proc_find(tmp
);
812 tmp
= (int)p1
->p_pgrpid
;
815 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
819 tmp
= CAST_DOWN_EXPLICIT(int, uap
->arg
);
820 if (fp
->f_type
== DTYPE_SOCKET
) {
821 error
= sock_setsockopt((struct socket
*)fp
->f_data
,
822 SOL_SOCKET
, SO_NOSIGPIPE
, &tmp
, sizeof (tmp
));
824 struct fileglob
*fg
= fp
->f_fglob
;
826 lck_mtx_lock_spin(&fg
->fg_lock
);
828 fg
->fg_lflags
|= FG_NOSIGPIPE
;
830 fg
->fg_lflags
&= FG_NOSIGPIPE
;
831 lck_mtx_unlock(&fg
->fg_lock
);
837 if (fp
->f_type
== DTYPE_SOCKET
) {
838 int retsize
= sizeof (*retval
);
839 error
= sock_getsockopt((struct socket
*)fp
->f_data
,
840 SOL_SOCKET
, SO_NOSIGPIPE
, retval
, &retsize
);
842 *retval
= (fp
->f_fglob
->fg_lflags
& FG_NOSIGPIPE
) ?
850 /* Fall into F_SETLK */
853 if (fp
->f_type
!= DTYPE_VNODE
) {
857 vp
= (struct vnode
*)fp
->f_data
;
860 offset
= fp
->f_offset
;
863 /* Copy in the lock structure */
864 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
869 if ((fl
.l_whence
== SEEK_CUR
) && (fl
.l_start
+ offset
< fl
.l_start
)) {
874 if ( (error
= vnode_getwithref(vp
)) ) {
877 if (fl
.l_whence
== SEEK_CUR
)
878 fl
.l_start
+= offset
;
881 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
891 if ((fflag
& FREAD
) == 0) {
896 // XXX UInt32 unsafe for LP64 kernel
897 OSBitOrAtomic(P_LADVLOCK
, &p
->p_ladvflag
);
898 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_SETLK
, &fl
, flg
, &context
);
903 if ((fflag
& FWRITE
) == 0) {
908 // XXX UInt32 unsafe for LP64 kernel
909 OSBitOrAtomic(P_LADVLOCK
, &p
->p_ladvflag
);
910 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_SETLK
, &fl
, flg
, &context
);
915 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &fl
,
930 if (fp
->f_type
!= DTYPE_VNODE
) {
934 vp
= (struct vnode
*)fp
->f_data
;
936 offset
= fp
->f_offset
;
939 /* Copy in the lock structure */
940 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
944 /* Check starting byte and ending byte for EOVERFLOW in SEEK_CUR */
945 /* and ending byte for EOVERFLOW in SEEK_SET */
946 if (((fl
.l_whence
== SEEK_CUR
) &&
947 ((fl
.l_start
+ offset
< fl
.l_start
) ||
948 ((fl
.l_len
> 0) && (fl
.l_start
+offset
+ fl
.l_len
- 1 < fl
.l_start
+offset
)))) ||
949 ((fl
.l_whence
== SEEK_SET
) && (fl
.l_len
> 0) && (fl
.l_start
+ fl
.l_len
- 1 < fl
.l_start
)))
951 /* lf_advlock doesn't check start/end for F_GETLK if file has no locks */
956 if ((fl
.l_whence
== SEEK_SET
) && (fl
.l_start
< 0)) {
971 switch (fl
.l_whence
) {
981 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
982 if (fl
.l_whence
== SEEK_CUR
)
983 fl
.l_start
+= offset
;
986 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
990 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, uap
->cmd
, &fl
, F_POSIX
, &context
);
995 error
= copyout((caddr_t
)&fl
, argp
, sizeof(fl
));
999 case F_PREALLOCATE
: {
1000 fstore_t alloc_struct
; /* structure for allocate command */
1001 u_int32_t alloc_flags
= 0;
1003 if (fp
->f_type
!= DTYPE_VNODE
) {
1008 vp
= (struct vnode
*)fp
->f_data
;
1011 /* make sure that we have write permission */
1012 if ((fp
->f_flag
& FWRITE
) == 0) {
1017 error
= copyin(argp
, (caddr_t
)&alloc_struct
, sizeof(alloc_struct
));
1021 /* now set the space allocated to 0 */
1022 alloc_struct
.fst_bytesalloc
= 0;
1025 * Do some simple parameter checking
1028 /* set up the flags */
1030 alloc_flags
|= PREALLOCATE
;
1032 if (alloc_struct
.fst_flags
& F_ALLOCATECONTIG
)
1033 alloc_flags
|= ALLOCATECONTIG
;
1035 if (alloc_struct
.fst_flags
& F_ALLOCATEALL
)
1036 alloc_flags
|= ALLOCATEALL
;
1039 * Do any position mode specific stuff. The only
1040 * position mode supported now is PEOFPOSMODE
1043 switch (alloc_struct
.fst_posmode
) {
1046 if (alloc_struct
.fst_offset
!= 0) {
1051 alloc_flags
|= ALLOCATEFROMPEOF
;
1055 if (alloc_struct
.fst_offset
<= 0) {
1060 alloc_flags
|= ALLOCATEFROMVOL
;
1068 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1070 * call allocate to get the space
1072 error
= VNOP_ALLOCATE(vp
,alloc_struct
.fst_length
,alloc_flags
,
1073 &alloc_struct
.fst_bytesalloc
, alloc_struct
.fst_offset
,
1075 (void)vnode_put(vp
);
1077 error2
= copyout((caddr_t
)&alloc_struct
, argp
, sizeof(alloc_struct
));
1086 if (fp
->f_type
!= DTYPE_VNODE
) {
1090 vp
= (struct vnode
*)fp
->f_data
;
1093 error
= copyin(argp
, (caddr_t
)&offset
, sizeof (off_t
));
1096 AUDIT_ARG(value64
, offset
);
1098 error
= vnode_getwithref(vp
);
1103 error
= mac_vnode_check_truncate(&context
,
1104 fp
->f_fglob
->fg_cred
, vp
);
1106 (void)vnode_put(vp
);
1111 * Make sure that we are root. Growing a file
1112 * without zero filling the data is a security hole
1113 * root would have access anyway so we'll allow it
1121 error
= vnode_setsize(vp
, offset
, IO_NOZEROFILL
,
1125 (void)vnode_put(vp
);
1129 if (fp
->f_type
!= DTYPE_VNODE
) {
1134 fp
->f_fglob
->fg_flag
&= ~FNORDAHEAD
;
1136 fp
->f_fglob
->fg_flag
|= FNORDAHEAD
;
1141 if (fp
->f_type
!= DTYPE_VNODE
) {
1146 fp
->f_fglob
->fg_flag
|= FNOCACHE
;
1148 fp
->f_fglob
->fg_flag
&= ~FNOCACHE
;
1153 if (fp
->f_type
!= DTYPE_VNODE
) {
1158 fp
->f_fglob
->fg_flag
|= FNODIRECT
;
1160 fp
->f_fglob
->fg_flag
&= ~FNODIRECT
;
1164 case F_GLOBAL_NOCACHE
:
1165 if (fp
->f_type
!= DTYPE_VNODE
) {
1169 vp
= (struct vnode
*)fp
->f_data
;
1172 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1174 *retval
= vnode_isnocache(vp
);
1177 vnode_setnocache(vp
);
1179 vnode_clearnocache(vp
);
1181 (void)vnode_put(vp
);
1185 case F_CHECK_OPENEVT
:
1186 if (fp
->f_type
!= DTYPE_VNODE
) {
1190 vp
= (struct vnode
*)fp
->f_data
;
1193 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1195 *retval
= vnode_is_openevt(vp
);
1198 vnode_set_openevt(vp
);
1200 vnode_clear_openevt(vp
);
1202 (void)vnode_put(vp
);
1207 struct radvisory ra_struct
;
1209 if (fp
->f_type
!= DTYPE_VNODE
) {
1213 vp
= (struct vnode
*)fp
->f_data
;
1216 if ( (error
= copyin(argp
, (caddr_t
)&ra_struct
, sizeof(ra_struct
))) )
1218 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1219 error
= VNOP_IOCTL(vp
, F_RDADVISE
, (caddr_t
)&ra_struct
, 0, &context
);
1221 (void)vnode_put(vp
);
1228 if (fp
->f_type
!= DTYPE_VNODE
) {
1232 vp
= (struct vnode
*)fp
->f_data
;
1235 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1236 error
= cluster_push(vp
, 0);
1238 (void)vnode_put(vp
);
1243 case F_READBOOTSTRAP
:
1244 case F_WRITEBOOTSTRAP
: {
1245 user32_fbootstraptransfer_t user32_fbt_struct
;
1246 user_fbootstraptransfer_t user_fbt_struct
;
1248 caddr_t boot_structp
;
1250 if (fp
->f_type
!= DTYPE_VNODE
) {
1254 vp
= (struct vnode
*)fp
->f_data
;
1257 if (IS_64BIT_PROCESS(p
)) {
1258 sizeof_struct
= sizeof(user_fbt_struct
);
1259 boot_structp
= (caddr_t
) &user_fbt_struct
;
1262 sizeof_struct
= sizeof(user32_fbt_struct
);
1263 boot_structp
= (caddr_t
) &user32_fbt_struct
;
1265 error
= copyin(argp
, boot_structp
, sizeof_struct
);
1268 if ( (error
= vnode_getwithref(vp
)) ) {
1271 if (uap
->cmd
== F_WRITEBOOTSTRAP
) {
1273 * Make sure that we are root. Updating the
1274 * bootstrap on a disk could be a security hole
1277 (void)vnode_put(vp
);
1282 if (strncmp(vnode_mount(vp
)->mnt_vfsstat
.f_fstypename
, "hfs",
1283 sizeof(vnode_mount(vp
)->mnt_vfsstat
.f_fstypename
)) != 0) {
1287 * call vnop_ioctl to handle the I/O
1289 error
= VNOP_IOCTL(vp
, uap
->cmd
, boot_structp
, 0, &context
);
1291 (void)vnode_put(vp
);
1295 case F_LOG2PHYS_EXT
: {
1296 struct log2phys l2p_struct
; /* structure for allocate command */
1299 off_t file_offset
= 0;
1303 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1304 error
= copyin(argp
, (caddr_t
)&l2p_struct
, sizeof(l2p_struct
));
1307 file_offset
= l2p_struct
.l2p_devoffset
;
1309 file_offset
= fp
->f_offset
;
1311 if (fp
->f_type
!= DTYPE_VNODE
) {
1315 vp
= (struct vnode
*)fp
->f_data
;
1317 if ( (error
= vnode_getwithref(vp
)) ) {
1320 error
= VNOP_OFFTOBLK(vp
, file_offset
, &lbn
);
1322 (void)vnode_put(vp
);
1325 error
= VNOP_BLKTOOFF(vp
, lbn
, &offset
);
1327 (void)vnode_put(vp
);
1330 devBlockSize
= vfs_devblocksize(vnode_mount(vp
));
1331 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1332 a_size
= l2p_struct
.l2p_contigbytes
;
1334 a_size
= devBlockSize
;
1337 error
= VNOP_BLOCKMAP(vp
, offset
, a_size
, &bn
, &run
, NULL
, 0, &context
);
1339 (void)vnode_put(vp
);
1342 l2p_struct
.l2p_flags
= 0; /* for now */
1343 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1344 l2p_struct
.l2p_contigbytes
= run
- (file_offset
- offset
);
1346 l2p_struct
.l2p_contigbytes
= 0; /* for now */
1348 l2p_struct
.l2p_devoffset
= bn
* devBlockSize
;
1349 l2p_struct
.l2p_devoffset
+= file_offset
- offset
;
1350 error
= copyout((caddr_t
)&l2p_struct
, argp
, sizeof(l2p_struct
));
1358 if (fp
->f_type
!= DTYPE_VNODE
) {
1362 vp
= (struct vnode
*)fp
->f_data
;
1365 pathlen
= MAXPATHLEN
;
1366 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
1367 if (pathbufp
== NULL
) {
1371 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1372 error
= vn_getpath(vp
, pathbufp
, &pathlen
);
1373 (void)vnode_put(vp
);
1376 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
1378 FREE(pathbufp
, M_TEMP
);
1382 case F_PATHPKG_CHECK
: {
1386 if (fp
->f_type
!= DTYPE_VNODE
) {
1390 vp
= (struct vnode
*)fp
->f_data
;
1393 pathlen
= MAXPATHLEN
;
1394 pathbufp
= kalloc(MAXPATHLEN
);
1396 if ( (error
= copyinstr(argp
, pathbufp
, MAXPATHLEN
, &pathlen
)) == 0 ) {
1397 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1398 AUDIT_ARG(text
, pathbufp
);
1399 error
= vn_path_package_check(vp
, pathbufp
, pathlen
, retval
);
1401 (void)vnode_put(vp
);
1404 kfree(pathbufp
, MAXPATHLEN
);
1408 case F_CHKCLEAN
: // used by regression tests to see if all dirty pages got cleaned by fsync()
1409 case F_FULLFSYNC
: // fsync + flush the journal + DKIOCSYNCHRONIZECACHE
1410 case F_FREEZE_FS
: // freeze all other fs operations for the fs of this fd
1411 case F_THAW_FS
: { // thaw all frozen fs operations for the fs of this fd
1412 if (fp
->f_type
!= DTYPE_VNODE
) {
1416 vp
= (struct vnode
*)fp
->f_data
;
1419 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1420 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
1422 (void)vnode_put(vp
);
1428 * SPI (private) for opening a file starting from a dir fd
1431 struct user_fopenfrom fopen
;
1432 struct vnode_attr va
;
1433 struct nameidata nd
;
1436 /* Check if this isn't a valid file descriptor */
1437 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1438 (fp
->f_flag
& FREAD
) == 0) {
1442 vp
= (struct vnode
*)fp
->f_data
;
1445 if (vnode_getwithref(vp
)) {
1450 /* Only valid for directories */
1451 if (vp
->v_type
!= VDIR
) {
1457 /* Get flags, mode and pathname arguments. */
1458 if (IS_64BIT_PROCESS(p
)) {
1459 error
= copyin(argp
, &fopen
, sizeof(fopen
));
1461 struct user32_fopenfrom fopen32
;
1463 error
= copyin(argp
, &fopen32
, sizeof(fopen32
));
1464 fopen
.o_flags
= fopen32
.o_flags
;
1465 fopen
.o_mode
= fopen32
.o_mode
;
1466 fopen
.o_pathname
= CAST_USER_ADDR_T(fopen32
.o_pathname
);
1472 AUDIT_ARG(fflags
, fopen
.o_flags
);
1473 AUDIT_ARG(mode
, fopen
.o_mode
);
1475 /* Mask off all but regular access permissions */
1476 cmode
= ((fopen
.o_mode
&~ fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
1477 VATTR_SET(&va
, va_mode
, cmode
& ACCESSPERMS
);
1479 /* Start the lookup relative to the file descriptor's vnode. */
1480 NDINIT(&nd
, LOOKUP
, OP_OPEN
, USEDVP
| FOLLOW
| AUDITVNPATH1
, UIO_USERSPACE
,
1481 fopen
.o_pathname
, &context
);
1484 error
= open1(&context
, &nd
, fopen
.o_flags
, &va
, retval
);
1490 * SPI (private) for unlinking a file starting from a dir fd
1492 case F_UNLINKFROM
: {
1493 struct nameidata nd
;
1494 user_addr_t pathname
;
1496 /* Check if this isn't a valid file descriptor */
1497 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1498 (fp
->f_flag
& FREAD
) == 0) {
1502 vp
= (struct vnode
*)fp
->f_data
;
1505 if (vnode_getwithref(vp
)) {
1510 /* Only valid for directories */
1511 if (vp
->v_type
!= VDIR
) {
1517 /* Get flags, mode and pathname arguments. */
1518 if (IS_64BIT_PROCESS(p
)) {
1519 pathname
= (user_addr_t
)argp
;
1521 pathname
= CAST_USER_ADDR_T(argp
);
1524 /* Start the lookup relative to the file descriptor's vnode. */
1525 NDINIT(&nd
, DELETE
, OP_UNLINK
, USEDVP
| AUDITVNPATH1
, UIO_USERSPACE
,
1526 pathname
, &context
);
1529 error
= unlink1(&context
, &nd
, 0);
1539 struct user_fsignatures fs
;
1541 vm_offset_t kernel_blob_addr
;
1542 vm_size_t kernel_blob_size
;
1544 if (fp
->f_type
!= DTYPE_VNODE
) {
1548 vp
= (struct vnode
*)fp
->f_data
;
1550 error
= vnode_getwithref(vp
);
1554 if (IS_64BIT_PROCESS(p
)) {
1555 error
= copyin(argp
, &fs
, sizeof (fs
));
1557 struct user32_fsignatures fs32
;
1559 error
= copyin(argp
, &fs32
, sizeof (fs32
));
1560 fs
.fs_file_start
= fs32
.fs_file_start
;
1561 fs
.fs_blob_start
= CAST_USER_ADDR_T(fs32
.fs_blob_start
);
1562 fs
.fs_blob_size
= fs32
.fs_blob_size
;
1570 if(ubc_cs_blob_get(vp
, CPU_TYPE_ANY
, fs
.fs_file_start
))
1574 printf("CODE SIGNING: resident blob offered for: %s\n", vp->v_name);
1580 #define CS_MAX_BLOB_SIZE (1ULL * 1024 * 1024) /* XXX ? */
1581 if (fs
.fs_blob_size
> CS_MAX_BLOB_SIZE
) {
1587 kernel_blob_size
= CAST_DOWN(vm_size_t
, fs
.fs_blob_size
);
1588 kr
= ubc_cs_blob_allocate(&kernel_blob_addr
, &kernel_blob_size
);
1589 if (kr
!= KERN_SUCCESS
) {
1595 if(uap
->cmd
== F_ADDSIGS
) {
1596 error
= copyin(fs
.fs_blob_start
,
1597 (void *) kernel_blob_addr
,
1599 } else /* F_ADDFILESIGS */ {
1600 error
= vn_rdwr(UIO_READ
,
1602 (caddr_t
) kernel_blob_addr
,
1604 fs
.fs_file_start
+ fs
.fs_blob_start
,
1613 ubc_cs_blob_deallocate(kernel_blob_addr
,
1619 error
= ubc_cs_blob_add(
1621 CPU_TYPE_ANY
, /* not for a specific architecture */
1626 ubc_cs_blob_deallocate(kernel_blob_addr
,
1629 /* ubc_blob_add() has consumed "kernel_blob_addr" */
1630 #if CHECK_CS_VALIDATION_BITMAP
1631 ubc_cs_validation_bitmap_allocate( vp
);
1635 (void) vnode_put(vp
);
1639 case F_MARKDEPENDENCY
: {
1640 struct vnode_attr va
;
1641 vfs_context_t ctx
= vfs_context_current();
1644 if ((current_proc()->p_flag
& P_DEPENDENCY_CAPABLE
) == 0) {
1649 if (fp
->f_type
!= DTYPE_VNODE
) {
1654 vp
= (struct vnode
*)fp
->f_data
;
1657 if (vnode_getwithref(vp
)) {
1662 if (!vnode_isvroot(vp
)) {
1668 // get the owner of the root dir
1670 VATTR_WANTED(&va
, va_uid
);
1671 if (vnode_getattr(vp
, &va
, ctx
) != 0) {
1677 // and last, check that the caller is the super user or
1678 // the owner of the mount point
1679 cred
= vfs_context_ucred(ctx
);
1680 if (!is_suser() && va
.va_uid
!= kauth_cred_getuid(cred
)) {
1686 // if all those checks pass then we can mark the dependency
1687 vfs_markdependency(vp
->v_mount
);
1695 #ifdef CONFIG_PROTECT
1696 case F_GETPROTECTIONCLASS
: {
1699 if (fp
->f_type
!= DTYPE_VNODE
) {
1703 vp
= (struct vnode
*)fp
->f_data
;
1707 if (vnode_getwithref(vp
)) {
1712 error
= cp_vnode_getclass (vp
, &class);
1721 case F_SETPROTECTIONCLASS
: {
1722 /* tmp must be a valid PROTECTION_CLASS_* */
1723 tmp
= CAST_DOWN_EXPLICIT(uint32_t, uap
->arg
);
1725 if (fp
->f_type
!= DTYPE_VNODE
) {
1729 vp
= (struct vnode
*)fp
->f_data
;
1733 if (vnode_getwithref(vp
)) {
1738 /* Only go forward if you have write access */
1739 vfs_context_t ctx
= vfs_context_current();
1740 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
1745 error
= cp_vnode_setclass (vp
, tmp
);
1749 #endif /* CONFIG_PROTECT */
1751 case F_MOVEDATAEXTENTS
: {
1752 struct fileproc
*fp2
= NULL
;
1753 struct vnode
*src_vp
= NULLVP
;
1754 struct vnode
*dst_vp
= NULLVP
;
1755 /* We need to grab the 2nd FD out of the argments before moving on. */
1756 int fd2
= CAST_DOWN_EXPLICIT(int32_t, uap
->arg
);
1758 if (fp
->f_type
!= DTYPE_VNODE
) {
1762 vp
= src_vp
= (struct vnode
*)fp
->f_data
;
1764 /* For now, special case HFS+ only, since this is SPI. */
1765 if (src_vp
->v_tag
!= VT_HFS
) {
1770 /* We're still holding the proc FD lock */
1771 if ( (error
= fp_lookup(p
, fd2
, &fp2
, 1)) ) {
1775 if (fp2
->f_type
!= DTYPE_VNODE
) {
1776 fp_drop(p
, fd2
, fp2
, 1);
1780 dst_vp
= (struct vnode
*)fp2
->f_data
;
1782 /* For now, special case HFS+ only, since this is SPI. */
1783 if (dst_vp
->v_tag
!= VT_HFS
) {
1784 fp_drop(p
, fd2
, fp2
, 1);
1790 /* Re-do MAC checks against the new FD, pass in a fake argument */
1791 error
= mac_file_check_fcntl(proc_ucred(p
), fp2
->f_fglob
, uap
->cmd
, 0);
1793 fp_drop(p
, fd2
, fp2
, 1);
1797 /* Audit the 2nd FD */
1802 /* Proc lock dropped; now we have a legit pair of FDs. Go to work */
1804 if (vnode_getwithref(src_vp
)) {
1805 fp_drop(p
, fd2
, fp2
, 0);
1809 if (vnode_getwithref(dst_vp
)) {
1811 fp_drop(p
, fd2
, fp2
, 0);
1817 * Basic asserts; validate they are not the same and that
1818 * both live on the same filesystem.
1821 if (dst_vp
== src_vp
) {
1824 fp_drop (p
, fd2
, fp2
, 0);
1829 if (dst_vp
->v_mount
!= src_vp
->v_mount
) {
1832 fp_drop (p
, fd2
, fp2
, 0);
1837 /* Now check for write access to the target files */
1838 if(vnode_authorize(src_vp
, NULLVP
,
1839 (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), &context
) != 0) {
1842 fp_drop(p
, fd2
, fp2
, 0);
1847 if(vnode_authorize(dst_vp
, NULLVP
,
1848 (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), &context
) != 0) {
1851 fp_drop(p
, fd2
, fp2
, 0);
1856 /* Verify that both vps point to files and not directories */
1857 if (!vnode_isreg(src_vp
) || !vnode_isreg(dst_vp
)) {
1860 fp_drop(p
, fd2
, fp2
, 0);
1866 * The exchangedata syscall handler passes in 0 for the flags to VNOP_EXCHANGE.
1867 * We'll pass in our special bit indicating that the new behavior is expected
1870 error
= VNOP_EXCHANGE(src_vp
, dst_vp
, FSOPT_EXCHANGE_DATA_ONLY
, &context
);
1874 fp_drop(p
, fd2
, fp2
, 0);
1879 * Set the vnode pointed to by 'fd'
1880 * and tag it as the (potentially future) backing store
1881 * for another filesystem
1883 case F_SETBACKINGSTORE
: {
1884 if (fp
->f_type
!= DTYPE_VNODE
) {
1888 vp
= (struct vnode
*)fp
->f_data
;
1890 if (vp
->v_tag
!= VT_HFS
) {
1897 if (vnode_getwithref(vp
)) {
1902 /* only proceed if you have write access */
1903 vfs_context_t ctx
= vfs_context_current();
1904 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
1911 /* If arg != 0, set, otherwise unset */
1913 error
= hfs_set_backingstore (vp
, 1);
1916 error
= hfs_set_backingstore (vp
, 0);
1918 /* Success. explicitly set error to 0. */
1926 * like F_GETPATH, but special semantics for
1927 * the mobile time machine handler.
1929 case F_GETPATH_MTMINFO
: {
1933 if (fp
->f_type
!= DTYPE_VNODE
) {
1937 vp
= (struct vnode
*)fp
->f_data
;
1940 pathlen
= MAXPATHLEN
;
1941 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
1942 if (pathbufp
== NULL
) {
1946 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1947 int backingstore
= 0;
1949 /* Check for error from vn_getpath before moving on */
1950 if ((error
= vn_getpath(vp
, pathbufp
, &pathlen
)) == 0) {
1951 if (vp
->v_tag
== VT_HFS
) {
1952 error
= hfs_is_backingstore (vp
, &backingstore
);
1954 (void)vnode_put(vp
);
1957 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
1961 * If the copyout was successful, now check to ensure
1962 * that this vnode is not a BACKINGSTORE vnode. mtmd
1963 * wants the path regardless.
1970 (void)vnode_put(vp
);
1972 FREE(pathbufp
, M_TEMP
);
1979 * This is an fcntl() that we d not recognize at this level;
1980 * if this is a vnode, we send it down into the VNOP_IOCTL
1981 * for this vnode; this can include special devices, and will
1982 * effectively overload fcntl() to send ioctl()'s.
1984 if((uap
->cmd
& IOC_VOID
) && (uap
->cmd
& IOC_INOUT
)){
1989 if (fp
->f_type
!= DTYPE_VNODE
) {
1993 vp
= (struct vnode
*)fp
->f_data
;
1996 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1997 #define STK_PARAMS 128
1998 char stkbuf
[STK_PARAMS
];
2002 * For this to work properly, we have to copy in the
2003 * ioctl() cmd argument if there is one; we must also
2004 * check that a command parameter, if present, does
2005 * not exceed the maximum command length dictated by
2006 * the number of bits we have available in the command
2007 * to represent a structure length. Finally, we have
2008 * to copy the results back out, if it is that type of
2011 size
= IOCPARM_LEN(uap
->cmd
);
2012 if (size
> IOCPARM_MAX
) {
2013 (void)vnode_put(vp
);
2019 if (size
> sizeof (stkbuf
)) {
2020 if ((memp
= (caddr_t
)kalloc(size
)) == 0) {
2021 (void)vnode_put(vp
);
2030 if (uap
->cmd
& IOC_IN
) {
2033 error
= copyin(argp
, data
, size
);
2035 (void)vnode_put(vp
);
2043 *(user_addr_t
*)data
= argp
;
2045 *(uint32_t *)data
= (uint32_t)argp
;
2048 } else if ((uap
->cmd
& IOC_OUT
) && size
) {
2050 * Zero the buffer so the user always
2051 * gets back something deterministic.
2054 } else if (uap
->cmd
& IOC_VOID
) {
2056 *(user_addr_t
*)data
= argp
;
2058 *(uint32_t *)data
= (uint32_t)argp
;
2062 error
= VNOP_IOCTL(vp
, uap
->cmd
, CAST_DOWN(caddr_t
, data
), 0, &context
);
2064 (void)vnode_put(vp
);
2066 /* Copy any output data to user */
2067 if (error
== 0 && (uap
->cmd
& IOC_OUT
) && size
)
2068 error
= copyout(data
, argp
, size
);
2076 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
2077 fp_drop(p
, fd
, fp
, 0);
2080 fp_drop(p
, fd
, fp
, 1);
2089 * Description: Common code for dup, dup2, and fcntl(F_DUPFD).
2091 * Parameters: p Process performing the dup
2093 * new The fd to dup it to
2094 * fd_flags Flags to augment the new fd
2095 * retval Pointer to the call return area
2097 * Returns: 0 Success
2102 * *retval (modified) The new descriptor
2104 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
2107 * Notes: This function may drop and reacquire this lock; it is unsafe
2108 * for a caller to assume that other state protected by the lock
2109 * has not been subsequently changed out from under it.
2113 struct filedesc
*fdp
, int old
, int new, int fd_flags
, int32_t *retval
)
2115 struct fileproc
*nfp
;
2116 struct fileproc
*ofp
;
2122 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2124 if ((ofp
= fdp
->fd_ofiles
[old
]) == NULL
||
2125 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
)) {
2132 error
= mac_file_check_dup(proc_ucred(p
), ofp
->f_fglob
, new);
2142 MALLOC_ZONE(nfp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
2143 /* Failure check follows proc_fdlock() due to handling requirements */
2153 bzero(nfp
, sizeof(struct fileproc
));
2156 nfp
->f_fglob
= ofp
->f_fglob
;
2160 if (fdp
->fd_ofiles
[new] != 0)
2161 panic("finishdup: overwriting fd_ofiles with new %d", new);
2162 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0)
2163 panic("finishdup: unreserved fileflags with new %d", new);
2166 if (new > fdp
->fd_lastfile
)
2167 fdp
->fd_lastfile
= new;
2168 *fdflags(p
, new) |= fd_flags
;
2169 procfdtbl_releasefd(p
, new, nfp
);
2178 * Description: The implementation of the close(2) system call
2180 * Parameters: p Process in whose per process file table
2181 * the close is to occur
2182 * uap->fd fd to be closed
2185 * Returns: 0 Success
2186 * fp_lookup:EBADF Bad file descriptor
2187 * close_internal:EBADF
2188 * close_internal:??? Anything returnable by a per-fileops
2192 close(proc_t p
, struct close_args
*uap
, int32_t *retval
)
2194 __pthread_testcancel(1);
2195 return(close_nocancel(p
, (struct close_nocancel_args
*)uap
, retval
));
2200 close_nocancel(proc_t p
, struct close_nocancel_args
*uap
, __unused
int32_t *retval
)
2202 struct fileproc
*fp
;
2206 AUDIT_SYSCLOSE(p
, fd
);
2210 if ( (error
= fp_lookup(p
,fd
,&fp
, 1)) ) {
2215 error
= close_internal_locked(p
, fd
, fp
, 0);
2224 * close_internal_locked
2226 * Close a file descriptor.
2228 * Parameters: p Process in whose per process file table
2229 * the close is to occur
2230 * fd fd to be closed
2231 * fp fileproc associated with the fd
2233 * Returns: 0 Success
2234 * EBADF fd already in close wait state
2235 * closef_locked:??? Anything returnable by a per-fileops
2238 * Locks: Assumes proc_fdlock for process is held by the caller and returns
2241 * Notes: This function may drop and reacquire this lock; it is unsafe
2242 * for a caller to assume that other state protected by the lock
2243 * has not been subsequently changes out from under it, if the
2244 * caller made the call with the lock held.
2247 close_internal_locked(proc_t p
, int fd
, struct fileproc
*fp
, int flags
)
2249 struct filedesc
*fdp
= p
->p_fd
;
2251 int resvfd
= flags
& FD_DUP2RESV
;
2255 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2258 /* Keep people from using the filedesc while we are closing it */
2259 procfdtbl_markclosefd(p
, fd
);
2262 if ((fp
->f_flags
& FP_CLOSING
) == FP_CLOSING
) {
2263 panic("close_internal_locked: being called on already closing fd");
2268 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
2269 panic("close_internal: unreserved fileflags with fd %d", fd
);
2272 fp
->f_flags
|= FP_CLOSING
;
2274 if ( (fp
->f_flags
& FP_AIOISSUED
) || kauth_authorize_fileop_has_listeners() ) {
2278 if ( (fp
->f_type
== DTYPE_VNODE
) && kauth_authorize_fileop_has_listeners() ) {
2280 * call out to allow 3rd party notification of close.
2281 * Ignore result of kauth_authorize_fileop call.
2283 if (vnode_getwithref((vnode_t
)fp
->f_data
) == 0) {
2284 u_int fileop_flags
= 0;
2285 if ((fp
->f_flags
& FP_WRITTEN
) != 0)
2286 fileop_flags
|= KAUTH_FILEOP_CLOSE_MODIFIED
;
2287 kauth_authorize_fileop(fp
->f_fglob
->fg_cred
, KAUTH_FILEOP_CLOSE
,
2288 (uintptr_t)fp
->f_data
, (uintptr_t)fileop_flags
);
2289 vnode_put((vnode_t
)fp
->f_data
);
2292 if (fp
->f_flags
& FP_AIOISSUED
)
2294 * cancel all async IO requests that can be cancelled.
2296 _aio_close( p
, fd
);
2301 if (fd
< fdp
->fd_knlistsize
)
2302 knote_fdclose(p
, fd
);
2304 if (fp
->f_flags
& FP_WAITEVENT
)
2305 (void)waitevent_close(p
, fp
);
2307 if ((fp
->f_flags
& FP_INCHRREAD
) == 0)
2308 fileproc_drain(p
, fp
);
2313 error
= closef_locked(fp
, fp
->f_fglob
, p
);
2314 if ((fp
->f_flags
& FP_WAITCLOSE
) == FP_WAITCLOSE
)
2315 wakeup(&fp
->f_flags
);
2316 fp
->f_flags
&= ~(FP_WAITCLOSE
| FP_CLOSING
);
2320 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
2326 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
2327 panic("close with reserved fd returns with freed fd:%d: proc: %p", fd
, p
);
2338 * Description: Return status information about a file descriptor.
2340 * Parameters: p The process doing the fstat
2342 * ub The user stat buffer
2343 * xsecurity The user extended security
2344 * buffer, or 0 if none
2345 * xsecurity_size The size of xsecurity, or 0
2347 * isstat64 Flag to indicate 64 bit version
2348 * for inode size, etc.
2350 * Returns: 0 Success
2353 * fp_lookup:EBADF Bad file descriptor
2354 * vnode_getwithref:???
2356 * vnode_getwithref:???
2363 * Notes: Internal implementation for all other fstat() related
2366 * XXX switch on node type is bogus; need a stat in struct
2367 * XXX fileops instead.
2370 fstat1(proc_t p
, int fd
, user_addr_t ub
, user_addr_t xsecurity
, user_addr_t xsecurity_size
, int isstat64
)
2372 struct fileproc
*fp
;
2378 struct user64_stat user64_sb
;
2379 struct user32_stat user32_sb
;
2380 struct user64_stat64 user64_sb64
;
2381 struct user32_stat64 user32_sb64
;
2387 kauth_filesec_t fsec
;
2388 user_size_t xsecurity_bufsize
;
2389 vfs_context_t ctx
= vfs_context_current();
2395 if ((error
= fp_lookup(p
, fd
, &fp
, 0)) != 0) {
2400 fsec
= KAUTH_FILESEC_NONE
;
2402 sbptr
= (void *)&source
;
2407 if ((error
= vnode_getwithref((vnode_t
)data
)) == 0) {
2409 * If the caller has the file open, and is not
2410 * requesting extended security information, we are
2411 * going to let them get the basic stat information.
2413 if (xsecurity
== USER_ADDR_NULL
) {
2414 error
= vn_stat_noauth((vnode_t
)data
, sbptr
, NULL
, isstat64
, ctx
);
2416 error
= vn_stat((vnode_t
)data
, sbptr
, &fsec
, isstat64
, ctx
);
2419 AUDIT_ARG(vnpath
, (struct vnode
*)data
, ARG_VNODE1
);
2420 (void)vnode_put((vnode_t
)data
);
2426 error
= soo_stat((struct socket
*)data
, sbptr
, isstat64
);
2428 #endif /* SOCKETS */
2431 error
= pipe_stat((void *)data
, sbptr
, isstat64
);
2435 error
= pshm_stat((void *)data
, sbptr
, isstat64
);
2439 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2440 error
= kqueue_stat(fp
, sbptr
, isstat64
, p
);
2441 thread_funnel_set(kernel_flock
, funnel_state
);
2451 if (isstat64
!= 0) {
2452 source
.sb64
.st_lspare
= 0;
2453 source
.sb64
.st_qspare
[0] = 0LL;
2454 source
.sb64
.st_qspare
[1] = 0LL;
2456 if (IS_64BIT_PROCESS(current_proc())) {
2457 munge_user64_stat64(&source
.sb64
, &dest
.user64_sb64
);
2458 my_size
= sizeof(dest
.user64_sb64
);
2459 sbp
= (caddr_t
)&dest
.user64_sb64
;
2461 munge_user32_stat64(&source
.sb64
, &dest
.user32_sb64
);
2462 my_size
= sizeof(dest
.user32_sb64
);
2463 sbp
= (caddr_t
)&dest
.user32_sb64
;
2466 source
.sb
.st_lspare
= 0;
2467 source
.sb
.st_qspare
[0] = 0LL;
2468 source
.sb
.st_qspare
[1] = 0LL;
2469 if (IS_64BIT_PROCESS(current_proc())) {
2470 munge_user64_stat(&source
.sb
, &dest
.user64_sb
);
2471 my_size
= sizeof(dest
.user64_sb
);
2472 sbp
= (caddr_t
)&dest
.user64_sb
;
2474 munge_user32_stat(&source
.sb
, &dest
.user32_sb
);
2475 my_size
= sizeof(dest
.user32_sb
);
2476 sbp
= (caddr_t
)&dest
.user32_sb
;
2480 error
= copyout(sbp
, ub
, my_size
);
2483 /* caller wants extended security information? */
2484 if (xsecurity
!= USER_ADDR_NULL
) {
2486 /* did we get any? */
2487 if (fsec
== KAUTH_FILESEC_NONE
) {
2488 if (susize(xsecurity_size
, 0) != 0) {
2493 /* find the user buffer size */
2494 xsecurity_bufsize
= fusize(xsecurity_size
);
2496 /* copy out the actual data size */
2497 if (susize(xsecurity_size
, KAUTH_FILESEC_COPYSIZE(fsec
)) != 0) {
2502 /* if the caller supplied enough room, copy out to it */
2503 if (xsecurity_bufsize
>= KAUTH_FILESEC_COPYSIZE(fsec
))
2504 error
= copyout(fsec
, xsecurity
, KAUTH_FILESEC_COPYSIZE(fsec
));
2508 fp_drop(p
, fd
, fp
, 0);
2510 kauth_filesec_free(fsec
);
2518 * Description: Extended version of fstat supporting returning extended
2519 * security information
2521 * Parameters: p The process doing the fstat
2522 * uap->fd The fd to stat
2523 * uap->ub The user stat buffer
2524 * uap->xsecurity The user extended security
2525 * buffer, or 0 if none
2526 * uap->xsecurity_size The size of xsecurity, or 0
2528 * Returns: 0 Success
2529 * !0 Errno (see fstat1)
2532 fstat_extended(proc_t p
, struct fstat_extended_args
*uap
, __unused
int32_t *retval
)
2534 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 0));
2541 * Description: Get file status for the file associated with fd
2543 * Parameters: p The process doing the fstat
2544 * uap->fd The fd to stat
2545 * uap->ub The user stat buffer
2547 * Returns: 0 Success
2548 * !0 Errno (see fstat1)
2551 fstat(proc_t p
, register struct fstat_args
*uap
, __unused
int32_t *retval
)
2553 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 0));
2560 * Description: Extended version of fstat64 supporting returning extended
2561 * security information
2563 * Parameters: p The process doing the fstat
2564 * uap->fd The fd to stat
2565 * uap->ub The user stat buffer
2566 * uap->xsecurity The user extended security
2567 * buffer, or 0 if none
2568 * uap->xsecurity_size The size of xsecurity, or 0
2570 * Returns: 0 Success
2571 * !0 Errno (see fstat1)
2574 fstat64_extended(proc_t p
, struct fstat64_extended_args
*uap
, __unused
int32_t *retval
)
2576 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 1));
2583 * Description: Get 64 bit version of the file status for the file associated
2586 * Parameters: p The process doing the fstat
2587 * uap->fd The fd to stat
2588 * uap->ub The user stat buffer
2590 * Returns: 0 Success
2591 * !0 Errno (see fstat1)
2594 fstat64(proc_t p
, register struct fstat64_args
*uap
, __unused
int32_t *retval
)
2596 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 1));
2603 * Description: Return pathconf information about a file descriptor.
2605 * Parameters: p Process making the request
2606 * uap->fd fd to get information about
2607 * uap->name Name of information desired
2608 * retval Pointer to the call return area
2610 * Returns: 0 Success
2612 * fp_lookup:EBADF Bad file descriptor
2613 * vnode_getwithref:???
2617 * *retval (modified) Returned information (numeric)
2620 fpathconf(proc_t p
, struct fpathconf_args
*uap
, int32_t *retval
)
2623 struct fileproc
*fp
;
2630 AUDIT_ARG(fd
, uap
->fd
);
2631 if ( (error
= fp_lookup(p
, fd
, &fp
, 0)) )
2639 if (uap
->name
!= _PC_PIPE_BUF
) {
2648 if (uap
->name
!= _PC_PIPE_BUF
) {
2657 vp
= (struct vnode
*)data
;
2659 if ( (error
= vnode_getwithref(vp
)) == 0) {
2660 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
2662 error
= vn_pathconf(vp
, uap
->name
, retval
, vfs_context_current());
2664 (void)vnode_put(vp
);
2671 case DTYPE_FSEVENTS
:
2678 fp_drop(p
, fd
, fp
, 0);
2683 * Statistics counter for the number of times a process calling fdalloc()
2684 * has resulted in an expansion of the per process open file table.
2686 * XXX This would likely be of more use if it were per process
2694 * Description: Allocate a file descriptor for the process.
2696 * Parameters: p Process to allocate the fd in
2697 * want The fd we would prefer to get
2698 * result Pointer to fd we got
2700 * Returns: 0 Success
2705 * *result (modified) The fd which was allocated
2708 fdalloc(proc_t p
, int want
, int *result
)
2710 struct filedesc
*fdp
= p
->p_fd
;
2712 int lim
, last
, numfiles
, oldnfiles
;
2713 struct fileproc
**newofiles
, **ofiles
;
2714 char *newofileflags
;
2717 * Search for a free descriptor starting at the higher
2718 * of want or fd_freefile. If that fails, consider
2719 * expanding the ofile array.
2722 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2725 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
2727 last
= min(fdp
->fd_nfiles
, lim
);
2728 if ((i
= want
) < fdp
->fd_freefile
)
2729 i
= fdp
->fd_freefile
;
2730 for (; i
< last
; i
++) {
2731 if (fdp
->fd_ofiles
[i
] == NULL
&& !(fdp
->fd_ofileflags
[i
] & UF_RESERVED
)) {
2732 procfdtbl_reservefd(p
, i
);
2733 if (i
> fdp
->fd_lastfile
)
2734 fdp
->fd_lastfile
= i
;
2735 if (want
<= fdp
->fd_freefile
)
2736 fdp
->fd_freefile
= i
;
2743 * No space in current array. Expand?
2745 if (fdp
->fd_nfiles
>= lim
)
2747 if (fdp
->fd_nfiles
< NDEXTENT
)
2748 numfiles
= NDEXTENT
;
2750 numfiles
= 2 * fdp
->fd_nfiles
;
2755 MALLOC_ZONE(newofiles
, struct fileproc
**,
2756 numfiles
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
2758 if (newofiles
== NULL
) {
2761 if (fdp
->fd_nfiles
>= numfiles
) {
2762 FREE_ZONE(newofiles
, numfiles
* OFILESIZE
, M_OFILETABL
);
2765 newofileflags
= (char *) &newofiles
[numfiles
];
2767 * Copy the existing ofile and ofileflags arrays
2768 * and zero the new portion of each array.
2770 oldnfiles
= fdp
->fd_nfiles
;
2771 (void) memcpy(newofiles
, fdp
->fd_ofiles
,
2772 oldnfiles
* sizeof(*fdp
->fd_ofiles
));
2773 (void) memset(&newofiles
[oldnfiles
], 0,
2774 (numfiles
- oldnfiles
) * sizeof(*fdp
->fd_ofiles
));
2776 (void) memcpy(newofileflags
, fdp
->fd_ofileflags
,
2777 oldnfiles
* sizeof(*fdp
->fd_ofileflags
));
2778 (void) memset(&newofileflags
[oldnfiles
], 0,
2779 (numfiles
- oldnfiles
) *
2780 sizeof(*fdp
->fd_ofileflags
));
2781 ofiles
= fdp
->fd_ofiles
;
2782 fdp
->fd_ofiles
= newofiles
;
2783 fdp
->fd_ofileflags
= newofileflags
;
2784 fdp
->fd_nfiles
= numfiles
;
2785 FREE_ZONE(ofiles
, oldnfiles
* OFILESIZE
, M_OFILETABL
);
2794 * Description: Check to see whether n user file descriptors are available
2797 * Parameters: p Process to check in
2798 * n The number of fd's desired
2803 * Locks: Assumes proc_fdlock for process is held by the caller
2805 * Notes: The answer only remains valid so long as the proc_fdlock is
2806 * held by the caller.
2809 fdavail(proc_t p
, int n
)
2811 struct filedesc
*fdp
= p
->p_fd
;
2812 struct fileproc
**fpp
;
2816 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
2817 if ((i
= lim
- fdp
->fd_nfiles
) > 0 && (n
-= i
) <= 0)
2819 fpp
= &fdp
->fd_ofiles
[fdp
->fd_freefile
];
2820 flags
= &fdp
->fd_ofileflags
[fdp
->fd_freefile
];
2821 for (i
= fdp
->fd_nfiles
- fdp
->fd_freefile
; --i
>= 0; fpp
++, flags
++)
2822 if (*fpp
== NULL
&& !(*flags
& UF_RESERVED
) && --n
<= 0)
2831 * Description: Legacy KPI wrapper function for _fdrelse
2833 * Parameters: p Process in which fd lives
2838 * Locks: Assumes proc_fdlock for process is held by the caller
2841 fdrelse(proc_t p
, int fd
)
2850 * Description: Get the fileproc pointer for the given fd from the per process
2851 * open file table without taking an explicit reference on it.
2853 * Parameters: p Process containing fd
2854 * fd fd to obtain fileproc for
2855 * resultfp Pointer to pointer return area
2857 * Returns: 0 Success
2861 * *resultfp (modified) Pointer to fileproc pointer
2863 * Locks: Assumes proc_fdlock for process is held by the caller
2865 * Notes: Because there is no reference explicitly taken, the returned
2866 * fileproc pointer is only valid so long as the proc_fdlock
2867 * remains held by the caller.
2870 fdgetf_noref(proc_t p
, int fd
, struct fileproc
**resultfp
)
2872 struct filedesc
*fdp
= p
->p_fd
;
2873 struct fileproc
*fp
;
2875 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2876 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2877 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2889 * Description: Get fileproc and vnode pointer for a given fd from the per
2890 * process open file table of the specified process, and if
2891 * successful, increment the f_iocount
2893 * Parameters: p Process in which fd lives
2894 * fd fd to get information for
2895 * resultfp Pointer to result fileproc
2896 * pointer area, or 0 if none
2897 * resultvp Pointer to result vnode pointer
2898 * area, or 0 if none
2900 * Returns: 0 Success
2901 * EBADF Bad file descriptor
2902 * ENOTSUP fd does not refer to a vnode
2905 * *resultfp (modified) Fileproc pointer
2906 * *resultvp (modified) vnode pointer
2908 * Notes: The resultfp and resultvp fields are optional, and may be
2909 * independently specified as NULL to skip returning information
2911 * Locks: Internally takes and releases proc_fdlock
2914 fp_getfvp(proc_t p
, int fd
, struct fileproc
**resultfp
, struct vnode
**resultvp
)
2916 struct filedesc
*fdp
= p
->p_fd
;
2917 struct fileproc
*fp
;
2919 proc_fdlock_spin(p
);
2920 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2921 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2922 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2926 if (fp
->f_type
!= DTYPE_VNODE
) {
2935 *resultvp
= (struct vnode
*)fp
->f_data
;
2945 * Description: Get fileproc, vnode pointer, and vid for a given fd from the
2946 * per process open file table of the specified process, and if
2947 * successful, increment the f_iocount
2949 * Parameters: p Process in which fd lives
2950 * fd fd to get information for
2951 * resultfp Pointer to result fileproc
2952 * pointer area, or 0 if none
2953 * resultvp Pointer to result vnode pointer
2954 * area, or 0 if none
2955 * vidp Pointer to resuld vid area
2957 * Returns: 0 Success
2958 * EBADF Bad file descriptor
2959 * ENOTSUP fd does not refer to a vnode
2962 * *resultfp (modified) Fileproc pointer
2963 * *resultvp (modified) vnode pointer
2966 * Notes: The resultfp and resultvp fields are optional, and may be
2967 * independently specified as NULL to skip returning information
2969 * Locks: Internally takes and releases proc_fdlock
2972 fp_getfvpandvid(proc_t p
, int fd
, struct fileproc
**resultfp
,
2973 struct vnode
**resultvp
, uint32_t *vidp
)
2975 struct filedesc
*fdp
= p
->p_fd
;
2976 struct fileproc
*fp
;
2978 proc_fdlock_spin(p
);
2979 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2980 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2981 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2985 if (fp
->f_type
!= DTYPE_VNODE
) {
2994 *resultvp
= (struct vnode
*)fp
->f_data
;
2996 *vidp
= (uint32_t)vnode_vid((struct vnode
*)fp
->f_data
);
3006 * Description: Get fileproc and socket pointer for a given fd from the
3007 * per process open file table of the specified process, and if
3008 * successful, increment the f_iocount
3010 * Parameters: p Process in which fd lives
3011 * fd fd to get information for
3012 * resultfp Pointer to result fileproc
3013 * pointer area, or 0 if none
3014 * results Pointer to result socket
3015 * pointer area, or 0 if none
3017 * Returns: EBADF The file descriptor is invalid
3018 * EOPNOTSUPP The file descriptor is not a socket
3022 * *resultfp (modified) Fileproc pointer
3023 * *results (modified) socket pointer
3025 * Notes: EOPNOTSUPP should probably be ENOTSOCK; this function is only
3026 * ever called from accept1().
3029 fp_getfsock(proc_t p
, int fd
, struct fileproc
**resultfp
,
3030 struct socket
**results
)
3032 struct filedesc
*fdp
= p
->p_fd
;
3033 struct fileproc
*fp
;
3035 proc_fdlock_spin(p
);
3036 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3037 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3038 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3042 if (fp
->f_type
!= DTYPE_SOCKET
) {
3051 *results
= (struct socket
*)fp
->f_data
;
3061 * Description: Get fileproc and kqueue pointer for a given fd from the
3062 * per process open file table of the specified process, and if
3063 * successful, increment the f_iocount
3065 * Parameters: p Process in which fd lives
3066 * fd fd to get information for
3067 * resultfp Pointer to result fileproc
3068 * pointer area, or 0 if none
3069 * resultkq Pointer to result kqueue
3070 * pointer area, or 0 if none
3072 * Returns: EBADF The file descriptor is invalid
3073 * EBADF The file descriptor is not a socket
3077 * *resultfp (modified) Fileproc pointer
3078 * *resultkq (modified) kqueue pointer
3080 * Notes: The second EBADF should probably be something else to make
3081 * the error condition distinct.
3084 fp_getfkq(proc_t p
, int fd
, struct fileproc
**resultfp
,
3085 struct kqueue
**resultkq
)
3087 struct filedesc
*fdp
= p
->p_fd
;
3088 struct fileproc
*fp
;
3090 proc_fdlock_spin(p
);
3091 if ( fd
< 0 || fd
>= fdp
->fd_nfiles
||
3092 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3093 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3097 if (fp
->f_type
!= DTYPE_KQUEUE
) {
3106 *resultkq
= (struct kqueue
*)fp
->f_data
;
3116 * Description: Get fileproc and POSIX shared memory pointer for a given fd
3117 * from the per process open file table of the specified process
3118 * and if successful, increment the f_iocount
3120 * Parameters: p Process in which fd lives
3121 * fd fd to get information for
3122 * resultfp Pointer to result fileproc
3123 * pointer area, or 0 if none
3124 * resultpshm Pointer to result POSIX
3125 * shared memory pointer
3126 * pointer area, or 0 if none
3128 * Returns: EBADF The file descriptor is invalid
3129 * EBADF The file descriptor is not a POSIX
3130 * shared memory area
3134 * *resultfp (modified) Fileproc pointer
3135 * *resultpshm (modified) POSIX shared memory pointer
3137 * Notes: The second EBADF should probably be something else to make
3138 * the error condition distinct.
3141 fp_getfpshm(proc_t p
, int fd
, struct fileproc
**resultfp
,
3142 struct pshmnode
**resultpshm
)
3144 struct filedesc
*fdp
= p
->p_fd
;
3145 struct fileproc
*fp
;
3147 proc_fdlock_spin(p
);
3148 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3149 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3150 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3154 if (fp
->f_type
!= DTYPE_PSXSHM
) {
3164 *resultpshm
= (struct pshmnode
*)fp
->f_data
;
3174 * Description: Get fileproc and POSIX semaphore pointer for a given fd from
3175 * the per process open file table of the specified process
3176 * and if successful, increment the f_iocount
3178 * Parameters: p Process in which fd lives
3179 * fd fd to get information for
3180 * resultfp Pointer to result fileproc
3181 * pointer area, or 0 if none
3182 * resultpsem Pointer to result POSIX
3183 * semaphore pointer area, or
3186 * Returns: EBADF The file descriptor is invalid
3187 * EBADF The file descriptor is not a POSIX
3192 * *resultfp (modified) Fileproc pointer
3193 * *resultpsem (modified) POSIX semaphore pointer
3195 * Notes: The second EBADF should probably be something else to make
3196 * the error condition distinct.
3198 * In order to support unnamed POSIX semaphores, the named
3199 * POSIX semaphores will have to move out of the per-process
3200 * open filetable, and into a global table that is shared with
3201 * unnamed POSIX semaphores, since unnamed POSIX semaphores
3202 * are typically used by declaring instances in shared memory,
3203 * and there's no other way to do this without changing the
3204 * underlying type, which would introduce binary compatibility
3208 fp_getfpsem(proc_t p
, int fd
, struct fileproc
**resultfp
,
3209 struct psemnode
**resultpsem
)
3211 struct filedesc
*fdp
= p
->p_fd
;
3212 struct fileproc
*fp
;
3214 proc_fdlock_spin(p
);
3215 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3216 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3217 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3221 if (fp
->f_type
!= DTYPE_PSXSEM
) {
3230 *resultpsem
= (struct psemnode
*)fp
->f_data
;
3240 * Description: Get fileproc and pipe pointer for a given fd from the
3241 * per process open file table of the specified process
3242 * and if successful, increment the f_iocount
3244 * Parameters: p Process in which fd lives
3245 * fd fd to get information for
3246 * resultfp Pointer to result fileproc
3247 * pointer area, or 0 if none
3248 * resultpipe Pointer to result pipe
3249 * pointer area, or 0 if none
3251 * Returns: EBADF The file descriptor is invalid
3252 * EBADF The file descriptor is not a socket
3256 * *resultfp (modified) Fileproc pointer
3257 * *resultpipe (modified) pipe pointer
3259 * Notes: The second EBADF should probably be something else to make
3260 * the error condition distinct.
3263 fp_getfpipe(proc_t p
, int fd
, struct fileproc
**resultfp
,
3264 struct pipe
**resultpipe
)
3266 struct filedesc
*fdp
= p
->p_fd
;
3267 struct fileproc
*fp
;
3269 proc_fdlock_spin(p
);
3270 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3271 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3272 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3276 if (fp
->f_type
!= DTYPE_PIPE
) {
3285 *resultpipe
= (struct pipe
*)fp
->f_data
;
3292 #define DTYPE_ATALK -1 /* XXX This does not belong here */
3298 * Description: Get fileproc and atalk pointer for a given fd from the
3299 * per process open file table of the specified process
3300 * and if successful, increment the f_iocount
3302 * Parameters: p Process in which fd lives
3303 * fd fd to get information for
3304 * resultfp Pointer to result fileproc
3305 * pointer area, or 0 if none
3306 * resultatalk Pointer to result atalk
3307 * pointer area, or 0 if none
3308 * Returns: EBADF The file descriptor is invalid
3309 * EBADF The file descriptor is not a socket
3313 * *resultfp (modified) Fileproc pointer
3314 * *resultatalk (modified) atalk pointer
3316 * Notes: The second EBADF should probably be something else to make
3317 * the error condition distinct.
3319 * XXX This code is specific to AppleTalk protocol support, and
3320 * XXX should be conditionally compiled
3323 fp_getfatalk(proc_t p
, int fd
, struct fileproc
**resultfp
,
3324 struct atalk
**resultatalk
)
3326 struct filedesc
*fdp
= p
->p_fd
;
3327 struct fileproc
*fp
;
3329 proc_fdlock_spin(p
);
3330 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3331 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3332 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3336 if (fp
->f_type
!= (DTYPE_ATALK
+1)) {
3345 *resultatalk
= (struct atalk
*)fp
->f_data
;
3356 * Description: Get fileproc pointer for a given fd from the per process
3357 * open file table of the specified process and if successful,
3358 * increment the f_iocount
3360 * Parameters: p Process in which fd lives
3361 * fd fd to get information for
3362 * resultfp Pointer to result fileproc
3363 * pointer area, or 0 if none
3364 * locked !0 if the caller holds the
3365 * proc_fdlock, 0 otherwise
3367 * Returns: 0 Success
3368 * EBADF Bad file descriptor
3371 * *resultfp (modified) Fileproc pointer
3373 * Locks: If the argument 'locked' is non-zero, then the caller is
3374 * expected to have taken and held the proc_fdlock; if it is
3375 * zero, than this routine internally takes and drops this lock.
3378 fp_lookup(proc_t p
, int fd
, struct fileproc
**resultfp
, int locked
)
3380 struct filedesc
*fdp
= p
->p_fd
;
3381 struct fileproc
*fp
;
3384 proc_fdlock_spin(p
);
3385 if (fd
< 0 || fdp
== NULL
|| fd
>= fdp
->fd_nfiles
||
3386 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3387 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3406 * Description: Set the FP_WRITTEN flag on the fileproc and drop the I/O
3407 * reference previously taken by calling fp_lookup et. al.
3409 * Parameters: p Process in which the fd lives
3410 * fd fd associated with the fileproc
3411 * fp fileproc on which to set the
3412 * flag and drop the reference
3414 * Returns: 0 Success
3415 * fp_drop:EBADF Bad file descriptor
3417 * Locks: This function internally takes and drops the proc_fdlock for
3418 * the supplied process
3420 * Notes: The fileproc must correspond to the fd in the supplied proc
3423 fp_drop_written(proc_t p
, int fd
, struct fileproc
*fp
)
3427 proc_fdlock_spin(p
);
3429 fp
->f_flags
|= FP_WRITTEN
;
3431 error
= fp_drop(p
, fd
, fp
, 1);
3442 * Description: Set the FP_WAITEVENT flag on the fileproc and drop the I/O
3443 * reference previously taken by calling fp_lookup et. al.
3445 * Parameters: p Process in which the fd lives
3446 * fd fd associated with the fileproc
3447 * fp fileproc on which to set the
3448 * flag and drop the reference
3450 * Returns: 0 Success
3451 * fp_drop:EBADF Bad file descriptor
3453 * Locks: This function internally takes and drops the proc_fdlock for
3454 * the supplied process
3456 * Notes: The fileproc must correspond to the fd in the supplied proc
3459 fp_drop_event(proc_t p
, int fd
, struct fileproc
*fp
)
3463 proc_fdlock_spin(p
);
3465 fp
->f_flags
|= FP_WAITEVENT
;
3467 error
= fp_drop(p
, fd
, fp
, 1);
3478 * Description: Drop the I/O reference previously taken by calling fp_lookup
3481 * Parameters: p Process in which the fd lives
3482 * fd fd associated with the fileproc
3483 * fp fileproc on which to set the
3484 * flag and drop the reference
3485 * locked flag to internally take and
3486 * drop proc_fdlock if it is not
3487 * already held by the caller
3489 * Returns: 0 Success
3490 * EBADF Bad file descriptor
3492 * Locks: This function internally takes and drops the proc_fdlock for
3493 * the supplied process if 'locked' is non-zero, and assumes that
3494 * the caller already holds this lock if 'locked' is non-zero.
3496 * Notes: The fileproc must correspond to the fd in the supplied proc
3499 fp_drop(proc_t p
, int fd
, struct fileproc
*fp
, int locked
)
3501 struct filedesc
*fdp
= p
->p_fd
;
3505 proc_fdlock_spin(p
);
3506 if ((fp
== FILEPROC_NULL
) && (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3507 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3508 ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
3509 !(fdp
->fd_ofileflags
[fd
] & UF_CLOSING
)))) {
3516 if (fp
->f_iocount
== 0) {
3517 if (fp
->f_flags
& FP_SELCONFLICT
)
3518 fp
->f_flags
&= ~FP_SELCONFLICT
;
3520 if (p
->p_fpdrainwait
) {
3521 p
->p_fpdrainwait
= 0;
3528 wakeup(&p
->p_fpdrainwait
);
3537 * Description: Given an fd, look it up in the current process's per process
3538 * open file table, and return its internal vnode pointer.
3540 * Parameters: fd fd to obtain vnode from
3541 * vpp pointer to vnode return area
3543 * Returns: 0 Success
3544 * EINVAL The fd does not refer to a
3545 * vnode fileproc entry
3546 * fp_lookup:EBADF Bad file descriptor
3549 * *vpp (modified) Returned vnode pointer
3551 * Locks: This function internally takes and drops the proc_fdlock for
3552 * the current process
3554 * Notes: If successful, this function increments the f_iocount on the
3555 * fd's corresponding fileproc.
3557 * The fileproc referenced is not returned; because of this, care
3558 * must be taken to not drop the last reference (e.g. by closing
3559 * the file). This is inherently unsafe, since the reference may
3560 * not be recoverable from the vnode, if there is a subsequent
3561 * close that destroys the associate fileproc. The caller should
3562 * therefore retain their own reference on the fileproc so that
3563 * the f_iocount can be dropped subsequently. Failure to do this
3564 * can result in the returned pointer immediately becoming invalid
3565 * following the call.
3567 * Use of this function is discouraged.
3570 file_vnode(int fd
, struct vnode
**vpp
)
3572 proc_t p
= current_proc();
3573 struct fileproc
*fp
;
3576 proc_fdlock_spin(p
);
3577 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
3581 if (fp
->f_type
!= DTYPE_VNODE
) {
3582 fp_drop(p
, fd
, fp
,1);
3587 *vpp
= (struct vnode
*)fp
->f_data
;
3595 * file_vnode_withvid
3597 * Description: Given an fd, look it up in the current process's per process
3598 * open file table, and return its internal vnode pointer.
3600 * Parameters: fd fd to obtain vnode from
3601 * vpp pointer to vnode return area
3602 * vidp pointer to vid of the returned vnode
3604 * Returns: 0 Success
3605 * EINVAL The fd does not refer to a
3606 * vnode fileproc entry
3607 * fp_lookup:EBADF Bad file descriptor
3610 * *vpp (modified) Returned vnode pointer
3612 * Locks: This function internally takes and drops the proc_fdlock for
3613 * the current process
3615 * Notes: If successful, this function increments the f_iocount on the
3616 * fd's corresponding fileproc.
3618 * The fileproc referenced is not returned; because of this, care
3619 * must be taken to not drop the last reference (e.g. by closing
3620 * the file). This is inherently unsafe, since the reference may
3621 * not be recoverable from the vnode, if there is a subsequent
3622 * close that destroys the associate fileproc. The caller should
3623 * therefore retain their own reference on the fileproc so that
3624 * the f_iocount can be dropped subsequently. Failure to do this
3625 * can result in the returned pointer immediately becoming invalid
3626 * following the call.
3628 * Use of this function is discouraged.
3631 file_vnode_withvid(int fd
, struct vnode
**vpp
, uint32_t * vidp
)
3633 proc_t p
= current_proc();
3634 struct fileproc
*fp
;
3638 proc_fdlock_spin(p
);
3639 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
3643 if (fp
->f_type
!= DTYPE_VNODE
) {
3644 fp_drop(p
, fd
, fp
,1);
3648 vp
= (struct vnode
*)fp
->f_data
;
3652 if ((vidp
!= NULL
) && (vp
!= NULLVP
))
3653 *vidp
= (uint32_t)vp
->v_id
;
3664 * Description: Given an fd, look it up in the current process's per process
3665 * open file table, and return its internal socket pointer.
3667 * Parameters: fd fd to obtain vnode from
3668 * sp pointer to socket return area
3670 * Returns: 0 Success
3671 * ENOTSOCK Not a socket
3672 * fp_lookup:EBADF Bad file descriptor
3675 * *sp (modified) Returned socket pointer
3677 * Locks: This function internally takes and drops the proc_fdlock for
3678 * the current process
3680 * Notes: If successful, this function increments the f_iocount on the
3681 * fd's corresponding fileproc.
3683 * The fileproc referenced is not returned; because of this, care
3684 * must be taken to not drop the last reference (e.g. by closing
3685 * the file). This is inherently unsafe, since the reference may
3686 * not be recoverable from the socket, if there is a subsequent
3687 * close that destroys the associate fileproc. The caller should
3688 * therefore retain their own reference on the fileproc so that
3689 * the f_iocount can be dropped subsequently. Failure to do this
3690 * can result in the returned pointer immediately becoming invalid
3691 * following the call.
3693 * Use of this function is discouraged.
3696 file_socket(int fd
, struct socket
**sp
)
3698 proc_t p
= current_proc();
3699 struct fileproc
*fp
;
3702 proc_fdlock_spin(p
);
3703 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
3707 if (fp
->f_type
!= DTYPE_SOCKET
) {
3708 fp_drop(p
, fd
, fp
,1);
3712 *sp
= (struct socket
*)fp
->f_data
;
3722 * Description: Given an fd, look it up in the current process's per process
3723 * open file table, and return its fileproc's flags field.
3725 * Parameters: fd fd whose flags are to be
3727 * flags pointer to flags data area
3729 * Returns: 0 Success
3730 * ENOTSOCK Not a socket
3731 * fp_lookup:EBADF Bad file descriptor
3734 * *flags (modified) Returned flags field
3736 * Locks: This function internally takes and drops the proc_fdlock for
3737 * the current process
3739 * Notes: This function will internally increment and decrement the
3740 * f_iocount of the fileproc as part of its operation.
3743 file_flags(int fd
, int *flags
)
3746 proc_t p
= current_proc();
3747 struct fileproc
*fp
;
3750 proc_fdlock_spin(p
);
3751 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
3755 *flags
= (int)fp
->f_flag
;
3756 fp_drop(p
, fd
, fp
,1);
3766 * Description: Drop an iocount reference on an fd, and wake up any waiters
3767 * for draining (i.e. blocked in fileproc_drain() called during
3768 * the last attempt to close a file).
3770 * Parameters: fd fd on which an ioreference is
3773 * Returns: 0 Success
3774 * EBADF Bad file descriptor
3776 * Description: Given an fd, look it up in the current process's per process
3777 * open file table, and drop it's fileproc's f_iocount by one
3779 * Notes: This is intended as a corresponding operation to the functions
3780 * file_vnode() and file_socket() operations.
3782 * Technically, the close reference is supposed to be protected
3783 * by a fileproc_drain(), however, a drain will only block if
3784 * the fd refers to a character device, and that device has had
3785 * preparefileread() called on it. If it refers to something
3786 * other than a character device, then the drain will occur and
3787 * block each close attempt, rather than merely the last close.
3789 * Since it's possible for an fd that refers to a character
3790 * device to have an intermediate close followed by an open to
3791 * cause a different file to correspond to that descriptor,
3792 * unless there was a cautionary reference taken on the fileproc,
3793 * this is an inherently unsafe function. This happens in the
3794 * case where multiple fd's in a process refer to the same
3795 * character device (e.g. stdin/out/err pointing to a tty, etc.).
3797 * Use of this function is discouraged.
3802 struct fileproc
*fp
;
3803 proc_t p
= current_proc();
3806 proc_fdlock_spin(p
);
3807 if (fd
< 0 || fd
>= p
->p_fd
->fd_nfiles
||
3808 (fp
= p
->p_fd
->fd_ofiles
[fd
]) == NULL
||
3809 ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
3810 !(p
->p_fd
->fd_ofileflags
[fd
] & UF_CLOSING
))) {
3816 if (fp
->f_iocount
== 0) {
3817 if (fp
->f_flags
& FP_SELCONFLICT
)
3818 fp
->f_flags
&= ~FP_SELCONFLICT
;
3820 if (p
->p_fpdrainwait
) {
3821 p
->p_fpdrainwait
= 0;
3828 wakeup(&p
->p_fpdrainwait
);
3836 * Description: Allocate an entry in the per process open file table and
3837 * return the corresponding fileproc and fd.
3839 * Parameters: p The process in whose open file
3840 * table the fd is to be allocated
3841 * resultfp Pointer to fileproc pointer
3843 * resultfd Pointer to fd return area
3846 * Returns: 0 Success
3847 * falloc:ENFILE Too many open files in system
3848 * falloc:EMFILE Too many open files in process
3849 * falloc:ENOMEM M_FILEPROC or M_FILEGLOB zone
3853 * *resultfd (modified) Returned fileproc pointer
3854 * *resultfd (modified) Returned fd
3856 * Locks: This function takes and drops the proc_fdlock; if this lock
3857 * is already held, use falloc_locked() instead.
3859 * Notes: This function takes separate process and context arguments
3860 * solely to support kern_exec.c; otherwise, it would take
3861 * neither, and expect falloc_locked() to use the
3862 * vfs_context_current() routine internally.
3865 falloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
)
3870 error
= falloc_locked(p
, resultfp
, resultfd
, ctx
, 1);
3880 * Create a new open file structure and allocate
3881 * a file descriptor for the process that refers to it.
3883 * Returns: 0 Success
3885 * Description: Allocate an entry in the per process open file table and
3886 * return the corresponding fileproc and fd.
3888 * Parameters: p The process in whose open file
3889 * table the fd is to be allocated
3890 * resultfp Pointer to fileproc pointer
3892 * resultfd Pointer to fd return area
3894 * locked Flag to indicate whether the
3895 * caller holds proc_fdlock
3897 * Returns: 0 Success
3898 * ENFILE Too many open files in system
3899 * fdalloc:EMFILE Too many open files in process
3900 * ENOMEM M_FILEPROC or M_FILEGLOB zone
3905 * *resultfd (modified) Returned fileproc pointer
3906 * *resultfd (modified) Returned fd
3908 * Locks: If the parameter 'locked' is zero, this function takes and
3909 * drops the proc_fdlock; if non-zero, the caller must hold the
3912 * Notes: If you intend to use a non-zero 'locked' parameter, use the
3913 * utility function falloc() instead.
3915 * This function takes separate process and context arguments
3916 * solely to support kern_exec.c; otherwise, it would take
3917 * neither, and use the vfs_context_current() routine internally.
3920 falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
3921 vfs_context_t ctx
, int locked
)
3923 struct fileproc
*fp
, *fq
;
3924 struct fileglob
*fg
;
3929 if ( (error
= fdalloc(p
, 0, &nfd
)) ) {
3934 if (nfiles
>= maxfiles
) {
3941 error
= mac_file_check_create(proc_ucred(p
));
3950 * Allocate a new file descriptor.
3951 * If the process has file descriptor zero open, add to the list
3952 * of open files at that point, otherwise put it at the front of
3953 * the list of open files.
3957 MALLOC_ZONE(fp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
3963 MALLOC_ZONE(fg
, struct fileglob
*, sizeof(struct fileglob
), M_FILEGLOB
, M_WAITOK
);
3965 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
3970 bzero(fp
, sizeof(struct fileproc
));
3971 bzero(fg
, sizeof(struct fileglob
));
3972 lck_mtx_init(&fg
->fg_lock
, file_lck_grp
, file_lck_attr
);
3978 mac_file_label_init(fg
);
3981 kauth_cred_ref(ctx
->vc_ucred
);
3985 fp
->f_cred
= ctx
->vc_ucred
;
3988 mac_file_label_associate(fp
->f_cred
, fg
);
3991 lck_mtx_lock_spin(file_flist_lock
);
3995 if ( (fq
= p
->p_fd
->fd_ofiles
[0]) ) {
3996 LIST_INSERT_AFTER(fq
->f_fglob
, fg
, f_list
);
3998 LIST_INSERT_HEAD(&filehead
, fg
, f_list
);
4000 lck_mtx_unlock(file_flist_lock
);
4002 p
->p_fd
->fd_ofiles
[nfd
] = fp
;
4019 * Description: Free a file structure; drop the global open file count, and
4020 * drop the credential reference, if the fileglob has one, and
4021 * destroy the instance mutex before freeing
4023 * Parameters: fg Pointer to fileglob to be
4029 fg_free(struct fileglob
*fg
)
4031 lck_mtx_lock_spin(file_flist_lock
);
4032 LIST_REMOVE(fg
, f_list
);
4034 lck_mtx_unlock(file_flist_lock
);
4036 if (IS_VALID_CRED(fg
->fg_cred
)) {
4037 kauth_cred_unref(&fg
->fg_cred
);
4039 lck_mtx_destroy(&fg
->fg_lock
, file_lck_grp
);
4042 mac_file_label_destroy(fg
);
4044 FREE_ZONE(fg
, sizeof *fg
, M_FILEGLOB
);
4051 * Description: Perform close-on-exec processing for all files in a process
4052 * that are either marked as close-on-exec, or which were in the
4053 * process of being opened at the time of the execve
4055 * Also handles the case (via posix_spawn()) where -all-
4056 * files except those marked with "inherit" as treated as
4059 * Parameters: p Pointer to process calling
4064 * Locks: This function internally takes and drops proc_fdlock()
4066 * Notes: This function drops and retakes the kernel funnel; this is
4067 * inherently unsafe, since another thread may have the
4070 * XXX: We should likely reverse the lock and funnel drop/acquire
4071 * order to avoid the small race window; it's also possible that
4072 * if the program doing the exec has an outstanding listen socket
4073 * and a network connection is completed asynchronously that we
4074 * will end up with a "ghost" socket reference in the new process.
4076 * This needs reworking to make it safe to remove the funnel from
4077 * the execve and posix_spawn system calls.
4080 fdexec(proc_t p
, short flags
)
4082 struct filedesc
*fdp
= p
->p_fd
;
4084 boolean_t cloexec_default
= (flags
& POSIX_SPAWN_CLOEXEC_DEFAULT
) != 0;
4087 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
4089 struct fileproc
*fp
= fdp
->fd_ofiles
[i
];
4090 char *flagp
= &fdp
->fd_ofileflags
[i
];
4092 if (cloexec_default
) {
4094 * Reverse the usual semantics of file descriptor
4095 * inheritance - all of them should be closed
4096 * except files marked explicitly as "inherit" and
4097 * not marked close-on-exec.
4099 if ((*flagp
& (UF_EXCLOSE
|UF_INHERIT
)) != UF_INHERIT
)
4100 *flagp
|= UF_EXCLOSE
;
4101 *flagp
&= ~UF_INHERIT
;
4105 ((*flagp
& (UF_RESERVED
|UF_EXCLOSE
)) == UF_EXCLOSE
)
4107 || (fp
&& mac_file_check_inherit(proc_ucred(p
), fp
->f_fglob
))
4110 if (i
< fdp
->fd_knlistsize
)
4111 knote_fdclose(p
, i
);
4112 procfdtbl_clearfd(p
, i
);
4113 if (i
== fdp
->fd_lastfile
&& i
> 0)
4115 if (i
< fdp
->fd_freefile
)
4116 fdp
->fd_freefile
= i
;
4119 * Wait for any third party viewers (e.g., lsof)
4120 * to release their references to this fileproc.
4122 while (fp
->f_iocount
> 0) {
4123 p
->p_fpdrainwait
= 1;
4124 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
,
4128 closef_locked(fp
, fp
->f_fglob
, p
);
4130 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
4140 * Description: Copy a filedesc structure. This is normally used as part of
4141 * forkproc() when forking a new process, to copy the per process
4142 * open file table over to the new process.
4144 * Parameters: p Process whose open file table
4145 * is to be copied (parent)
4146 * uth_cdir Per thread current working
4147 * cirectory, or NULL
4149 * Returns: NULL Copy failed
4150 * !NULL Pointer to new struct filedesc
4152 * Locks: This function internally takes and drops proc_fdlock()
4154 * Notes: Files are copied directly, ignoring the new resource limits
4155 * for the process that's being copied into. Since the descriptor
4156 * references are just additional references, this does not count
4157 * against the number of open files on the system.
4159 * The struct filedesc includes the current working directory,
4160 * and the current root directory, if the process is chroot'ed.
4162 * If the exec was called by a thread using a per thread current
4163 * working directory, we inherit the working directory from the
4164 * thread making the call, rather than from the process.
4166 * In the case of a failure to obtain a reference, for most cases,
4167 * the file entry will be silently dropped. There's an exception
4168 * for the case of a chroot dir, since a failure to to obtain a
4169 * reference there would constitute an "escape" from the chroot
4170 * environment, which must not be allowed. In that case, we will
4171 * deny the execve() operation, rather than allowing the escape.
4174 fdcopy(proc_t p
, vnode_t uth_cdir
)
4176 struct filedesc
*newfdp
, *fdp
= p
->p_fd
;
4178 struct fileproc
*ofp
, *fp
;
4181 MALLOC_ZONE(newfdp
, struct filedesc
*,
4182 sizeof(*newfdp
), M_FILEDESC
, M_WAITOK
);
4189 * the FD_CHROOT flag will be inherited via this copy
4191 (void) memcpy(newfdp
, fdp
, sizeof(*newfdp
));
4194 * If we are running with per-thread current working directories,
4195 * inherit the new current working directory from the current thread
4196 * instead, before we take our references.
4198 if (uth_cdir
!= NULLVP
)
4199 newfdp
->fd_cdir
= uth_cdir
;
4202 * For both fd_cdir and fd_rdir make sure we get
4203 * a valid reference... if we can't, than set
4204 * set the pointer(s) to NULL in the child... this
4205 * will keep us from using a non-referenced vp
4206 * and allows us to do the vnode_rele only on
4207 * a properly referenced vp
4209 if ( (v_dir
= newfdp
->fd_cdir
) ) {
4210 if (vnode_getwithref(v_dir
) == 0) {
4211 if ( (vnode_ref(v_dir
)) )
4212 newfdp
->fd_cdir
= NULL
;
4215 newfdp
->fd_cdir
= NULL
;
4217 if (newfdp
->fd_cdir
== NULL
&& fdp
->fd_cdir
) {
4219 * we couldn't get a new reference on
4220 * the current working directory being
4221 * inherited... we might as well drop
4222 * our reference from the parent also
4223 * since the vnode has gone DEAD making
4224 * it useless... by dropping it we'll
4225 * be that much closer to recycling it
4227 vnode_rele(fdp
->fd_cdir
);
4228 fdp
->fd_cdir
= NULL
;
4231 if ( (v_dir
= newfdp
->fd_rdir
) ) {
4232 if (vnode_getwithref(v_dir
) == 0) {
4233 if ( (vnode_ref(v_dir
)) )
4234 newfdp
->fd_rdir
= NULL
;
4237 newfdp
->fd_rdir
= NULL
;
4240 /* Coming from a chroot environment and unable to get a reference... */
4241 if (newfdp
->fd_rdir
== NULL
&& fdp
->fd_rdir
) {
4243 * We couldn't get a new reference on
4244 * the chroot directory being
4245 * inherited... this is fatal, since
4246 * otherwise it would constitute an
4247 * escape from a chroot environment by
4250 if (newfdp
->fd_cdir
)
4251 vnode_rele(newfdp
->fd_cdir
);
4252 FREE_ZONE(newfdp
, sizeof *newfdp
, M_FILEDESC
);
4255 newfdp
->fd_refcnt
= 1;
4258 * If the number of open files fits in the internal arrays
4259 * of the open file structure, use them, otherwise allocate
4260 * additional memory for the number of descriptors currently
4263 if (newfdp
->fd_lastfile
< NDFILE
)
4267 * Compute the smallest multiple of NDEXTENT needed
4268 * for the file descriptors currently in use,
4269 * allowing the table to shrink.
4271 i
= newfdp
->fd_nfiles
;
4272 while (i
> 2 * NDEXTENT
&& i
> newfdp
->fd_lastfile
* 2)
4277 MALLOC_ZONE(newfdp
->fd_ofiles
, struct fileproc
**,
4278 i
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
4279 if (newfdp
->fd_ofiles
== NULL
) {
4280 if (newfdp
->fd_cdir
)
4281 vnode_rele(newfdp
->fd_cdir
);
4282 if (newfdp
->fd_rdir
)
4283 vnode_rele(newfdp
->fd_rdir
);
4285 FREE_ZONE(newfdp
, sizeof(*newfdp
), M_FILEDESC
);
4288 (void) memset(newfdp
->fd_ofiles
, 0, i
* OFILESIZE
);
4291 newfdp
->fd_ofileflags
= (char *) &newfdp
->fd_ofiles
[i
];
4292 newfdp
->fd_nfiles
= i
;
4294 if (fdp
->fd_nfiles
> 0) {
4295 struct fileproc
**fpp
;
4298 (void) memcpy(newfdp
->fd_ofiles
, fdp
->fd_ofiles
,
4299 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofiles
));
4300 (void) memcpy(newfdp
->fd_ofileflags
, fdp
->fd_ofileflags
,
4301 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofileflags
));
4304 * kq descriptors cannot be copied.
4306 if (newfdp
->fd_knlistsize
!= -1) {
4307 fpp
= &newfdp
->fd_ofiles
[newfdp
->fd_lastfile
];
4308 for (i
= newfdp
->fd_lastfile
; i
>= 0; i
--, fpp
--) {
4309 if (*fpp
!= NULL
&& (*fpp
)->f_type
== DTYPE_KQUEUE
) {
4311 newfdp
->fd_ofileflags
[i
] = 0;
4312 if (i
< newfdp
->fd_freefile
)
4313 newfdp
->fd_freefile
= i
;
4315 if (*fpp
== NULL
&& i
== newfdp
->fd_lastfile
&& i
> 0)
4316 newfdp
->fd_lastfile
--;
4318 newfdp
->fd_knlist
= NULL
;
4319 newfdp
->fd_knlistsize
= -1;
4320 newfdp
->fd_knhash
= NULL
;
4321 newfdp
->fd_knhashmask
= 0;
4323 fpp
= newfdp
->fd_ofiles
;
4324 flags
= newfdp
->fd_ofileflags
;
4326 for (i
= newfdp
->fd_lastfile
+ 1; --i
>= 0; fpp
++, flags
++)
4327 if ((ofp
= *fpp
) != NULL
&& !(*flags
& UF_RESERVED
)) {
4328 MALLOC_ZONE(fp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
4331 * XXX no room to copy, unable to
4332 * XXX safely unwind state at present
4336 bzero(fp
, sizeof(struct fileproc
));
4337 fp
->f_flags
= ofp
->f_flags
;
4338 //fp->f_iocount = ofp->f_iocount;
4340 fp
->f_fglob
= ofp
->f_fglob
;
4345 if (i
< newfdp
->fd_freefile
)
4346 newfdp
->fd_freefile
= i
;
4360 * Description: Release a filedesc (per process open file table) structure;
4361 * this is done on process exit(), or from forkproc_free() if
4362 * the fork fails for some reason subsequent to a successful
4365 * Parameters: p Pointer to process going away
4369 * Locks: This function internally takes and drops proc_fdlock()
4374 struct filedesc
*fdp
;
4375 struct fileproc
*fp
;
4380 /* Certain daemons might not have file descriptors */
4383 if ((fdp
== NULL
) || (--fdp
->fd_refcnt
> 0)) {
4387 if (fdp
->fd_refcnt
== 0xffff)
4388 panic("fdfree: bad fd_refcnt");
4390 /* Last reference: the structure can't change out from under us */
4392 if (fdp
->fd_nfiles
> 0 && fdp
->fd_ofiles
) {
4393 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
4394 if ((fp
= fdp
->fd_ofiles
[i
]) != NULL
) {
4396 if (fdp
->fd_ofileflags
[i
] & UF_RESERVED
)
4397 panic("fdfree: found fp with UF_RESERVED");
4399 /* closef drops the iocount ... */
4400 if ((fp
->f_flags
& FP_INCHRREAD
) != 0)
4402 procfdtbl_reservefd(p
, i
);
4404 if (i
< fdp
->fd_knlistsize
)
4405 knote_fdclose(p
, i
);
4406 if (fp
->f_flags
& FP_WAITEVENT
)
4407 (void)waitevent_close(p
, fp
);
4408 (void) closef_locked(fp
, fp
->f_fglob
, p
);
4409 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
4412 FREE_ZONE(fdp
->fd_ofiles
, fdp
->fd_nfiles
* OFILESIZE
, M_OFILETABL
);
4413 fdp
->fd_ofiles
= NULL
;
4420 vnode_rele(fdp
->fd_cdir
);
4422 vnode_rele(fdp
->fd_rdir
);
4424 proc_fdlock_spin(p
);
4429 FREE(fdp
->fd_knlist
, M_KQUEUE
);
4431 FREE(fdp
->fd_knhash
, M_KQUEUE
);
4433 FREE_ZONE(fdp
, sizeof(*fdp
), M_FILEDESC
);
4440 * Description: Called on last open instance for a fileglob for a file being
4443 * Parameters: fp Pointer to fileproc for fd
4444 * fg Pointer to fileglob for fd
4445 * p Pointer to proc structure
4447 * Returns: 0 Success
4448 * <fo_close>:??? Anything returnable by a per-fileops
4451 * Note: fp can only be non-NULL if p is also non-NULL. If p is NULL,
4452 * then fg must eith be locked (FHASLOCK) or must not have a
4453 * type of DTYPE_VNODE.
4455 * On return, the fg is freed.
4457 * This function may block draining output to a character
4458 * device on last close of that device.
4461 closef_finish(struct fileproc
*fp
, struct fileglob
*fg
, proc_t p
, vfs_context_t ctx
)
4466 /* fg_ops completed initialization? */
4468 error
= fo_close(fg
, ctx
);
4472 /* if fp is non-NULL, drain it out */
4473 if (((fp
!= (struct fileproc
*)0) && ((fp
->f_flags
& FP_INCHRREAD
) != 0))) {
4474 proc_fdlock_spin(p
);
4475 if ( ((fp
->f_flags
& FP_INCHRREAD
) != 0) ) {
4476 fileproc_drain(p
, fp
);
4488 * Description: Internal form of closef; called with proc_fdlock held
4490 * Parameters: fp Pointer to fileproc for fd
4491 * fg Pointer to fileglob for fd
4492 * p Pointer to proc structure
4494 * Returns: 0 Success
4495 * closef_finish:??? Anything returnable by a per-fileops
4498 * Note: Decrements reference count on file structure; if this was the
4499 * last reference, then closef_finish() is called
4501 * p and fp are allowed to be NULL when closing a file that was
4502 * being passed in a message (but only if we are called when this
4503 * is NOT the last reference).
4506 closef_locked(struct fileproc
*fp
, struct fileglob
*fg
, proc_t p
)
4510 struct vfs_context context
;
4517 /* Set up context with cred stashed in fg */
4518 if (p
== current_proc())
4519 context
.vc_thread
= current_thread();
4521 context
.vc_thread
= NULL
;
4522 context
.vc_ucred
= fg
->fg_cred
;
4525 * POSIX record locking dictates that any close releases ALL
4526 * locks owned by this process. This is handled by setting
4527 * a flag in the unlock to free ONLY locks obeying POSIX
4528 * semantics, and not to free BSD-style file locks.
4529 * If the descriptor was in a message, POSIX-style locks
4530 * aren't passed with the descriptor.
4532 if (p
&& (p
->p_ladvflag
& P_LADVLOCK
) && fg
->fg_type
== DTYPE_VNODE
) {
4535 lf
.l_whence
= SEEK_SET
;
4538 lf
.l_type
= F_UNLCK
;
4539 vp
= (struct vnode
*)fg
->fg_data
;
4541 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
4542 (void) VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &lf
, F_POSIX
, &context
);
4543 (void)vnode_put(vp
);
4547 lck_mtx_lock_spin(&fg
->fg_lock
);
4550 if (fg
->fg_count
> 0) {
4551 lck_mtx_unlock(&fg
->fg_lock
);
4555 if (fg
->fg_count
!= 0)
4556 panic("fg %p: being freed with bad fg_count (%d)", fg
, fg
->fg_count
);
4559 if (fp
&& (fp
->f_flags
& FP_WRITTEN
))
4560 fg
->fg_flag
|= FWASWRITTEN
;
4562 fg
->fg_lflags
|= FG_TERM
;
4563 lck_mtx_unlock(&fg
->fg_lock
);
4567 error
= closef_finish(fp
, fg
, p
, &context
);
4578 * Description: Drain out pending I/O operations
4580 * Parameters: p Process closing this file
4581 * fp fileproc struct for the open
4582 * instance on the file
4586 * Locks: Assumes the caller holds the proc_fdlock
4588 * Notes: For character devices, this occurs on the last close of the
4589 * device; for all other file descriptors, this occurs on each
4590 * close to prevent fd's from being closed out from under
4591 * operations currently in progress and blocked
4593 * See Also: file_vnode(), file_socket(), file_drop(), and the cautions
4594 * regarding their use and interaction with this function.
4597 fileproc_drain(proc_t p
, struct fileproc
* fp
)
4599 struct vfs_context context
;
4601 context
.vc_thread
= proc_thread(p
); /* XXX */
4602 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
4604 fp
->f_iocount
-- ; /* (the one the close holds) */
4606 while (fp
->f_iocount
) {
4608 lck_mtx_convert_spin(&p
->p_fdmlock
);
4610 if (fp
->f_fglob
->fg_ops
->fo_drain
) {
4611 (*fp
->f_fglob
->fg_ops
->fo_drain
)(fp
, &context
);
4613 if ((fp
->f_flags
& FP_INSELECT
) == FP_INSELECT
) {
4614 if (wait_queue_wakeup_all((wait_queue_t
)fp
->f_waddr
, NULL
, THREAD_INTERRUPTED
) == KERN_INVALID_ARGUMENT
)
4615 panic("bad wait queue for wait_queue_wakeup_all %p", fp
->f_waddr
);
4617 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
) {
4618 if (wait_queue_wakeup_all(&select_conflict_queue
, NULL
, THREAD_INTERRUPTED
) == KERN_INVALID_ARGUMENT
)
4619 panic("bad select_conflict_queue");
4621 p
->p_fpdrainwait
= 1;
4623 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
, "fpdrain", NULL
);
4627 if ((fp
->f_flags
& FP_INSELECT
) != 0)
4628 panic("FP_INSELECT set on drained fp");
4630 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
)
4631 fp
->f_flags
&= ~FP_SELCONFLICT
;
4638 * Description: Release the fd and free the fileproc associated with the fd
4639 * in the per process open file table of the specified process;
4640 * these values must correspond.
4642 * Parameters: p Process containing fd
4643 * fd fd to be released
4644 * fp fileproc to be freed
4646 * Returns: 0 Success
4648 * Notes: XXX function should be void - no one interprets the returns
4652 fp_free(proc_t p
, int fd
, struct fileproc
* fp
)
4654 proc_fdlock_spin(p
);
4658 fg_free(fp
->f_fglob
);
4659 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
4667 * Description: Apply an advisory lock on a file descriptor.
4669 * Parameters: p Process making request
4670 * uap->fd fd on which the lock is to be
4672 * uap->how (Un)Lock bits, including type
4673 * retval Pointer to the call return area
4675 * Returns: 0 Success
4676 * fp_getfvp:EBADF Bad file descriptor
4677 * fp_getfvp:ENOTSUP fd does not refer to a vnode
4678 * vnode_getwithref:???
4682 * *retval (modified) Size of dtable
4684 * Notes: Just attempt to get a record lock of the requested type on
4685 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
4688 flock(proc_t p
, struct flock_args
*uap
, __unused
int32_t *retval
)
4692 struct fileproc
*fp
;
4695 vfs_context_t ctx
= vfs_context_current();
4698 AUDIT_ARG(fd
, uap
->fd
);
4699 if ( (error
= fp_getfvp(p
, fd
, &fp
, &vp
)) ) {
4702 if ( (error
= vnode_getwithref(vp
)) ) {
4705 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
4707 lf
.l_whence
= SEEK_SET
;
4710 if (how
& LOCK_UN
) {
4711 lf
.l_type
= F_UNLCK
;
4712 fp
->f_flag
&= ~FHASLOCK
;
4713 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_UNLCK
, &lf
, F_FLOCK
, ctx
);
4717 lf
.l_type
= F_WRLCK
;
4718 else if (how
& LOCK_SH
)
4719 lf
.l_type
= F_RDLCK
;
4725 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
, F_SETLK
, &lf
);
4729 fp
->f_flag
|= FHASLOCK
;
4730 if (how
& LOCK_NB
) {
4731 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, F_FLOCK
, ctx
);
4734 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, F_FLOCK
|F_WAIT
, ctx
);
4736 (void)vnode_put(vp
);
4738 fp_drop(p
, fd
, fp
, 0);
4746 * Description: Obtain a Mach send right for a given file descriptor.
4748 * Parameters: p Process calling fileport
4749 * uap->fd The fd to reference
4750 * uap->portnamep User address at which to place port name.
4752 * Returns: 0 Success.
4753 * EBADF Bad file descriptor.
4754 * EINVAL File descriptor had type that cannot be sent, misc. other errors.
4755 * EFAULT Address at which to store port name is not valid.
4756 * EAGAIN Resource shortage.
4759 * On success, name of send right is stored at user-specified address.
4762 fileport_makeport(proc_t p
, struct fileport_makeport_args
*uap
,
4763 __unused
int *retval
)
4767 user_addr_t user_portaddr
= uap
->portnamep
;
4768 struct fileproc
*fp
= FILEPROC_NULL
;
4769 struct fileglob
*fg
= NULL
;
4770 ipc_port_t fileport
;
4771 mach_port_name_t name
= MACH_PORT_NULL
;
4773 err
= fp_lookup(p
, fd
, &fp
, 0);
4778 if (!filetype_issendable(fp
->f_type
)) {
4783 /* Dropped when port is deallocated */
4787 /* Allocate and initialize a port */
4788 fileport
= fileport_alloc(fg
);
4789 if (fileport
== IPC_PORT_NULL
) {
4795 /* Add an entry. Deallocates port on failure. */
4796 name
= ipc_port_copyout_send(fileport
, get_task_ipcspace(p
->task
));
4797 if (!MACH_PORT_VALID(name
)) {
4802 err
= copyout(&name
, user_portaddr
, sizeof(mach_port_name_t
));
4807 /* Tag the fileglob for debugging purposes */
4808 lck_mtx_lock_spin(&fg
->fg_lock
);
4809 fg
->fg_lflags
|= FG_PORTMADE
;
4810 lck_mtx_unlock(&fg
->fg_lock
);
4812 fp_drop(p
, fd
, fp
, 0);
4817 if (MACH_PORT_VALID(name
)) {
4818 /* Don't care if another thread races us to deallocate the entry */
4819 (void) mach_port_deallocate(get_task_ipcspace(p
->task
), name
);
4822 if (fp
!= FILEPROC_NULL
) {
4823 fp_drop(p
, fd
, fp
, 0);
4830 fileport_releasefg(struct fileglob
*fg
)
4832 (void)closef_locked(NULL
, fg
, PROC_NULL
);
4841 * Description: Obtain the file descriptor for a given Mach send right.
4843 * Parameters: p Process calling fileport
4844 * uap->port Name of send right to file port.
4846 * Returns: 0 Success
4847 * EINVAL Invalid Mach port name, or port is not for a file.
4849 * fdalloc:ENOMEM Unable to allocate fileproc or extend file table.
4852 * *retval (modified) The new descriptor
4855 fileport_makefd(proc_t p
, struct fileport_makefd_args
*uap
, int32_t *retval
)
4857 struct fileglob
*fg
;
4858 struct fileproc
*fp
= FILEPROC_NULL
;
4859 ipc_port_t port
= IPC_PORT_NULL
;
4860 mach_port_name_t send
= uap
->port
;
4865 res
= ipc_object_copyin(get_task_ipcspace(p
->task
),
4866 send
, MACH_MSG_TYPE_COPY_SEND
, &port
);
4868 if (res
!= KERN_SUCCESS
) {
4873 fg
= fileport_port_to_fileglob(port
);
4879 MALLOC_ZONE(fp
, struct fileproc
*, sizeof(*fp
), M_FILEPROC
, M_WAITOK
);
4880 if (fp
== FILEPROC_NULL
) {
4885 bzero(fp
, sizeof(*fp
));
4891 err
= fdalloc(p
, 0, &fd
);
4896 *fdflags(p
, fd
) |= UF_EXCLOSE
;
4898 procfdtbl_releasefd(p
, fd
, fp
);
4904 if ((fp
!= NULL
) && (0 != err
)) {
4905 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
4908 if (IPC_PORT_NULL
!= port
) {
4909 ipc_port_release_send(port
);
4919 * Description: Duplicate the specified descriptor to a free descriptor;
4920 * this is the second half of fdopen(), above.
4922 * Parameters: fdp filedesc pointer to fill in
4924 * dfd fd to dup from
4925 * mode mode to set on new fd
4926 * error command code
4928 * Returns: 0 Success
4929 * EBADF Source fd is bad
4930 * EACCES Requested mode not allowed
4931 * !0 'error', if not ENODEV or
4934 * Notes: XXX This is not thread safe; see fdopen() above
4937 dupfdopen(struct filedesc
*fdp
, int indx
, int dfd
, int flags
, int error
)
4939 struct fileproc
*wfp
;
4940 struct fileproc
*fp
;
4944 proc_t p
= current_proc();
4947 * If the to-be-dup'd fd number is greater than the allowed number
4948 * of file descriptors, or the fd to be dup'd has already been
4949 * closed, reject. Note, check for new == old is necessary as
4950 * falloc could allocate an already closed to-be-dup'd descriptor
4951 * as the new descriptor.
4955 fp
= fdp
->fd_ofiles
[indx
];
4956 if (dfd
< 0 || dfd
>= fdp
->fd_nfiles
||
4957 (wfp
= fdp
->fd_ofiles
[dfd
]) == NULL
|| wfp
== fp
||
4958 (fdp
->fd_ofileflags
[dfd
] & UF_RESERVED
)) {
4964 myerror
= mac_file_check_dup(proc_ucred(p
), wfp
->f_fglob
, dfd
);
4971 * There are two cases of interest here.
4973 * For ENODEV simply dup (dfd) to file descriptor
4974 * (indx) and return.
4976 * For ENXIO steal away the file structure from (dfd) and
4977 * store it in (indx). (dfd) is effectively closed by
4980 * Any other error code is just returned.
4985 * Check that the mode the file is being opened for is a
4986 * subset of the mode of the existing descriptor.
4988 if (((flags
& (FREAD
|FWRITE
)) | wfp
->f_flag
) != wfp
->f_flag
) {
4992 if (indx
> fdp
->fd_lastfile
)
4993 fdp
->fd_lastfile
= indx
;
4997 fg_free(fp
->f_fglob
);
4998 fp
->f_fglob
= wfp
->f_fglob
;
5000 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
] |
5001 (flags
& O_CLOEXEC
) ? UF_EXCLOSE
: 0;
5017 * Description: Add a reference to a fileglob by fileproc
5019 * Parameters: fp fileproc containing fileglob
5024 * Notes: XXX Should use OSAddAtomic?
5027 fg_ref(struct fileproc
* fp
)
5029 struct fileglob
*fg
;
5033 lck_mtx_lock_spin(&fg
->fg_lock
);
5036 if ((fp
->f_flags
& ~((unsigned int)FP_VALID_FLAGS
)) != 0)
5037 panic("fg_ref: invalid bits on fp %p", fp
);
5039 if (fg
->fg_count
== 0)
5040 panic("fg_ref: adding fgcount to zeroed fg: fp %p fg %p",
5044 lck_mtx_unlock(&fg
->fg_lock
);
5051 * Description: Remove a reference to a fileglob by fileproc
5053 * Parameters: fp fileproc containing fileglob
5058 * Notes: XXX Should use OSAddAtomic?
5061 fg_drop(struct fileproc
* fp
)
5063 struct fileglob
*fg
;
5066 lck_mtx_lock_spin(&fg
->fg_lock
);
5068 lck_mtx_unlock(&fg
->fg_lock
);
5075 * Description: Insert fileglob onto message queue
5077 * Parameters: fg Fileglob pointer to insert
5081 * Locks: Takes and drops fg_lock, potentially many times
5084 fg_insertuipc(struct fileglob
* fg
)
5088 lck_mtx_lock_spin(&fg
->fg_lock
);
5090 while (fg
->fg_lflags
& FG_RMMSGQ
) {
5091 lck_mtx_convert_spin(&fg
->fg_lock
);
5093 fg
->fg_lflags
|= FG_WRMMSGQ
;
5094 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_insertuipc", NULL
);
5099 if (fg
->fg_msgcount
== 1) {
5100 fg
->fg_lflags
|= FG_INSMSGQ
;
5103 lck_mtx_unlock(&fg
->fg_lock
);
5106 lck_mtx_lock_spin(uipc_lock
);
5108 LIST_INSERT_HEAD(&fmsghead
, fg
, f_msglist
);
5109 lck_mtx_unlock(uipc_lock
);
5110 lck_mtx_lock(&fg
->fg_lock
);
5111 fg
->fg_lflags
&= ~FG_INSMSGQ
;
5112 if (fg
->fg_lflags
& FG_WINSMSGQ
) {
5113 fg
->fg_lflags
&= ~FG_WINSMSGQ
;
5114 wakeup(&fg
->fg_lflags
);
5116 lck_mtx_unlock(&fg
->fg_lock
);
5125 * Description: Remove fileglob from message queue
5127 * Parameters: fg Fileglob pointer to remove
5131 * Locks: Takes and drops fg_lock, potentially many times
5134 fg_removeuipc(struct fileglob
* fg
)
5138 lck_mtx_lock_spin(&fg
->fg_lock
);
5139 while (fg
->fg_lflags
& FG_INSMSGQ
) {
5140 lck_mtx_convert_spin(&fg
->fg_lock
);
5142 fg
->fg_lflags
|= FG_WINSMSGQ
;
5143 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_removeuipc", NULL
);
5146 if (fg
->fg_msgcount
== 0) {
5147 fg
->fg_lflags
|= FG_RMMSGQ
;
5150 lck_mtx_unlock(&fg
->fg_lock
);
5153 lck_mtx_lock_spin(uipc_lock
);
5155 LIST_REMOVE(fg
, f_msglist
);
5156 lck_mtx_unlock(uipc_lock
);
5157 lck_mtx_lock(&fg
->fg_lock
);
5158 fg
->fg_lflags
&= ~FG_RMMSGQ
;
5159 if (fg
->fg_lflags
& FG_WRMMSGQ
) {
5160 fg
->fg_lflags
&= ~FG_WRMMSGQ
;
5161 wakeup(&fg
->fg_lflags
);
5163 lck_mtx_unlock(&fg
->fg_lock
);
5171 * Description: Generic fileops read indirected through the fileops pointer
5172 * in the fileproc structure
5174 * Parameters: fp fileproc structure pointer
5175 * uio user I/O structure pointer
5177 * ctx VFS context for operation
5179 * Returns: 0 Success
5180 * !0 Errno from read
5183 fo_read(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
5185 return ((*fp
->f_ops
->fo_read
)(fp
, uio
, flags
, ctx
));
5192 * Description: Generic fileops write indirected through the fileops pointer
5193 * in the fileproc structure
5195 * Parameters: fp fileproc structure pointer
5196 * uio user I/O structure pointer
5198 * ctx VFS context for operation
5200 * Returns: 0 Success
5201 * !0 Errno from write
5204 fo_write(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
5206 return((*fp
->f_ops
->fo_write
)(fp
, uio
, flags
, ctx
));
5213 * Description: Generic fileops ioctl indirected through the fileops pointer
5214 * in the fileproc structure
5216 * Parameters: fp fileproc structure pointer
5218 * data pointer to internalized copy
5219 * of user space ioctl command
5220 * parameter data in kernel space
5221 * ctx VFS context for operation
5223 * Returns: 0 Success
5224 * !0 Errno from ioctl
5226 * Locks: The caller is assumed to have held the proc_fdlock; this
5227 * function releases and reacquires this lock. If the caller
5228 * accesses data protected by this lock prior to calling this
5229 * function, it will need to revalidate/reacquire any cached
5230 * protected data obtained prior to the call.
5233 fo_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
, vfs_context_t ctx
)
5237 proc_fdunlock(vfs_context_proc(ctx
));
5238 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, com
, data
, ctx
);
5239 proc_fdlock(vfs_context_proc(ctx
));
5247 * Description: Generic fileops select indirected through the fileops pointer
5248 * in the fileproc structure
5250 * Parameters: fp fileproc structure pointer
5251 * which select which
5252 * wql pointer to wait queue list
5253 * ctx VFS context for operation
5255 * Returns: 0 Success
5256 * !0 Errno from select
5259 fo_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
5261 return((*fp
->f_ops
->fo_select
)(fp
, which
, wql
, ctx
));
5268 * Description: Generic fileops close indirected through the fileops pointer
5269 * in the fileproc structure
5271 * Parameters: fp fileproc structure pointer for
5273 * ctx VFS context for operation
5275 * Returns: 0 Success
5276 * !0 Errno from close
5279 fo_close(struct fileglob
*fg
, vfs_context_t ctx
)
5281 return((*fg
->fg_ops
->fo_close
)(fg
, ctx
));
5288 * Description: Generic fileops kqueue filter indirected through the fileops
5289 * pointer in the fileproc structure
5291 * Parameters: fp fileproc structure pointer
5292 * kn pointer to knote to filter on
5293 * ctx VFS context for operation
5295 * Returns: 0 Success
5296 * !0 Errno from kqueue filter
5299 fo_kqfilter(struct fileproc
*fp
, struct knote
*kn
, vfs_context_t ctx
)
5301 return ((*fp
->f_ops
->fo_kqfilter
)(fp
, kn
, ctx
));
5305 * The ability to send a file descriptor to another
5306 * process is opt-in by file type.
5309 filetype_issendable(file_type_t fdtype
)
5318 /* DTYPE_KQUEUE, DTYPE_FSEVENTS, DTYPE_PSXSEM */