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 fmsglist fmsghead
; /* head of list of open files */
177 struct fmsglist fmsg_ithead
; /* head of list of open files */
178 int nfiles
; /* actual number of open files */
181 lck_grp_attr_t
* file_lck_grp_attr
;
182 lck_grp_t
* file_lck_grp
;
183 lck_attr_t
* file_lck_attr
;
185 lck_mtx_t
* uipc_lock
;
191 * Description: Initialize the file lock group and the uipc and flist locks
197 * Notes: Called at system startup from bsd_init().
202 /* allocate file lock group attribute and group */
203 file_lck_grp_attr
= lck_grp_attr_alloc_init();
205 file_lck_grp
= lck_grp_alloc_init("file", file_lck_grp_attr
);
207 /* Allocate file lock attribute */
208 file_lck_attr
= lck_attr_alloc_init();
210 uipc_lock
= lck_mtx_alloc_init(file_lck_grp
, file_lck_attr
);
215 * proc_fdlock, proc_fdlock_spin
217 * Description: Lock to control access to the per process struct fileproc
218 * and struct filedesc
220 * Parameters: p Process to take the lock on
224 * Notes: The lock is initialized in forkproc() and destroyed in
225 * reap_child_process().
228 proc_fdlock(proc_t p
)
230 lck_mtx_lock(&p
->p_fdmlock
);
234 proc_fdlock_spin(proc_t p
)
236 lck_mtx_lock_spin(&p
->p_fdmlock
);
240 proc_fdlock_assert(proc_t p
, int assertflags
)
242 lck_mtx_assert(&p
->p_fdmlock
, assertflags
);
249 * Description: Unlock the lock previously locked by a call to proc_fdlock()
251 * Parameters: p Process to drop the lock on
256 proc_fdunlock(proc_t p
)
258 lck_mtx_unlock(&p
->p_fdmlock
);
263 * System calls on descriptors.
270 * Description: Returns the per process maximum size of the descriptor table
272 * Parameters: p Process being queried
273 * retval Pointer to the call return area
278 * *retval (modified) Size of dtable
281 getdtablesize(proc_t p
, __unused
struct getdtablesize_args
*uap
, int32_t *retval
)
284 *retval
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
292 procfdtbl_reservefd(struct proc
* p
, int fd
)
294 p
->p_fd
->fd_ofiles
[fd
] = NULL
;
295 p
->p_fd
->fd_ofileflags
[fd
] |= UF_RESERVED
;
299 procfdtbl_markclosefd(struct proc
* p
, int fd
)
301 p
->p_fd
->fd_ofileflags
[fd
] |= (UF_RESERVED
| UF_CLOSING
);
305 procfdtbl_releasefd(struct proc
* p
, int fd
, struct fileproc
* fp
)
308 p
->p_fd
->fd_ofiles
[fd
] = fp
;
309 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESERVED
;
310 if ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
) == UF_RESVWAIT
) {
311 p
->p_fd
->fd_ofileflags
[fd
] &= ~UF_RESVWAIT
;
317 procfdtbl_waitfd(struct proc
* p
, int fd
)
319 p
->p_fd
->fd_ofileflags
[fd
] |= UF_RESVWAIT
;
320 msleep(&p
->p_fd
, &p
->p_fdmlock
, PRIBIO
, "ftbl_waitfd", NULL
);
325 procfdtbl_clearfd(struct proc
* p
, int fd
)
329 waiting
= (p
->p_fd
->fd_ofileflags
[fd
] & UF_RESVWAIT
);
330 p
->p_fd
->fd_ofiles
[fd
] = NULL
;
331 p
->p_fd
->fd_ofileflags
[fd
] = 0;
332 if ( waiting
== UF_RESVWAIT
) {
340 * Description: Inline utility function to free an fd in a filedesc
342 * Parameters: fdp Pointer to filedesc fd lies in
344 * reserv fd should be reserved
348 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
352 _fdrelse(struct proc
* p
, int fd
)
354 struct filedesc
*fdp
= p
->p_fd
;
357 if (fd
< fdp
->fd_freefile
)
358 fdp
->fd_freefile
= fd
;
360 if (fd
> fdp
->fd_lastfile
)
361 panic("fdrelse: fd_lastfile inconsistent");
363 procfdtbl_clearfd(p
, fd
);
365 while ((nfd
= fdp
->fd_lastfile
) > 0 &&
366 fdp
->fd_ofiles
[nfd
] == NULL
&&
367 !(fdp
->fd_ofileflags
[nfd
] & UF_RESERVED
))
389 char uio_buf
[ UIO_SIZEOF(1) ];
390 struct vfs_context context
= *(vfs_context_current());
394 error
= fp_lookup(p
, fd
, &fp
, 0);
398 if (fp
->f_type
!= DTYPE_VNODE
&& fp
->f_type
!= DTYPE_PIPE
&& fp
->f_type
!= DTYPE_SOCKET
) {
402 if (rw
== UIO_WRITE
&& !(fp
->f_flag
& FWRITE
)) {
407 if (rw
== UIO_READ
&& !(fp
->f_flag
& FREAD
)) {
412 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
414 if (UIO_SEG_IS_USER_SPACE(segflg
))
415 spacetype
= proc_is64bit(p
) ? UIO_USERSPACE64
: UIO_USERSPACE32
;
417 spacetype
= UIO_SYSSPACE
;
419 auio
= uio_createwithbuffer(1, offset
, spacetype
, rw
, &uio_buf
[0], sizeof(uio_buf
));
421 uio_addiov(auio
, base
, len
);
423 if ( !(io_flg
& IO_APPEND
))
427 error
= fo_write(fp
, auio
, flags
, &context
);
429 error
= fo_read(fp
, auio
, flags
, &context
);
432 *aresid
= uio_resid(auio
);
434 if (uio_resid(auio
) && error
== 0)
438 if (rw
== UIO_WRITE
&& error
== 0)
439 fp_drop_written(p
, fd
, fp
);
441 fp_drop(p
, fd
, fp
, 0);
451 * Description: Duplicate a file descriptor.
453 * Parameters: p Process performing the dup
454 * uap->fd The fd to dup
455 * retval Pointer to the call return area
461 * *retval (modified) The new descriptor
464 dup(proc_t p
, struct dup_args
*uap
, int32_t *retval
)
466 struct filedesc
*fdp
= p
->p_fd
;
472 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
476 if ( (error
= fdalloc(p
, 0, &new)) ) {
477 fp_drop(p
, old
, fp
, 1);
481 error
= finishdup(p
, fdp
, old
, new, 0, retval
);
482 fp_drop(p
, old
, fp
, 1);
491 * Description: Duplicate a file descriptor to a particular value.
493 * Parameters: p Process performing the dup
494 * uap->from The fd to dup
495 * uap->to The fd to dup it to
496 * retval Pointer to the call return area
502 * *retval (modified) The new descriptor
505 dup2(proc_t p
, struct dup2_args
*uap
, int32_t *retval
)
507 struct filedesc
*fdp
= p
->p_fd
;
508 int old
= uap
->from
, new = uap
->to
;
510 struct fileproc
*fp
, *nfp
;
515 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
520 (rlim_t
)new >= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
522 fp_drop(p
, old
, fp
, 1);
527 fp_drop(p
, old
, fp
, 1);
532 if (new < 0 || new >= fdp
->fd_nfiles
) {
533 if ( (error
= fdalloc(p
, new, &i
)) ) {
534 fp_drop(p
, old
, fp
, 1);
544 while ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == UF_RESERVED
) {
545 fp_drop(p
, old
, fp
, 1);
546 procfdtbl_waitfd(p
, new);
548 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
553 if ((fdp
->fd_ofiles
[new] != NULL
) &&
554 ((error
= fp_lookup(p
, new, &nfp
, 1)) == 0)) {
555 fp_drop(p
, old
, fp
, 1);
556 (void)close_internal_locked(p
, new, nfp
, FD_DUP2RESV
);
558 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
560 procfdtbl_clearfd(p
, new);
564 if (fdp
->fd_ofiles
[new] != NULL
)
565 panic("dup2: no ref on fileproc %d", new);
567 procfdtbl_reservefd(p
, new);
571 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
576 if (fdp
->fd_ofiles
[new] != 0)
577 panic("dup2: overwriting fd_ofiles with new %d", new);
578 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0)
579 panic("dup2: unreserved fileflags with new %d", new);
581 error
= finishdup(p
, fdp
, old
, new, 0, retval
);
582 fp_drop(p
, old
, fp
, 1);
592 * Description: The file control system call.
594 * Parameters: p Process performing the fcntl
595 * uap->fd The fd to operate against
596 * uap->cmd The command to perform
597 * uap->arg Pointer to the command argument
598 * retval Pointer to the call return area
601 * !0 Errno (see fcntl_nocancel)
604 * *retval (modified) fcntl return value (if any)
606 * Notes: This system call differs from fcntl_nocancel() in that it
607 * tests for cancellation prior to performing a potentially
608 * blocking operation.
611 fcntl(proc_t p
, struct fcntl_args
*uap
, int32_t *retval
)
613 __pthread_testcancel(1);
614 return(fcntl_nocancel(p
, (struct fcntl_nocancel_args
*)uap
, retval
));
621 * Description: A non-cancel-testing file control system call.
623 * Parameters: p Process performing the fcntl
624 * uap->fd The fd to operate against
625 * uap->cmd The command to perform
626 * uap->arg Pointer to the command argument
627 * retval Pointer to the call return area
631 * fp_lookup:EBADF Bad file descriptor
643 * vnode_getwithref:???
650 * vnode_getwithref:???
657 * vnode_getwithref:???
659 * [F_SETSIZE,F_RDADVISE]
662 * vnode_getwithref:???
663 * [F_RDAHEAD,F_NOCACHE]
665 * vnode_getwithref:???
669 * *retval (modified) fcntl return value (if any)
672 fcntl_nocancel(proc_t p
, struct fcntl_nocancel_args
*uap
, int32_t *retval
)
675 struct filedesc
*fdp
= p
->p_fd
;
678 struct vnode
*vp
= NULLVP
; /* for AUDIT_ARG() at end */
679 int i
, tmp
, error
, error2
, flg
= F_POSIX
;
681 struct vfs_context context
;
689 AUDIT_ARG(fd
, uap
->fd
);
690 AUDIT_ARG(cmd
, uap
->cmd
);
693 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
697 context
.vc_thread
= current_thread();
698 context
.vc_ucred
= fp
->f_cred
;
700 is64bit
= proc_is64bit(p
);
706 * Since the arg parameter is defined as a long but may be
707 * either a long or a pointer we must take care to handle
708 * sign extension issues. Our sys call munger will sign
709 * extend a long when we are called from a 32-bit process.
710 * Since we can never have an address greater than 32-bits
711 * from a 32-bit process we lop off the top 32-bits to avoid
712 * getting the wrong address
714 argp
= CAST_USER_ADDR_T((uint32_t)uap
->arg
);
717 pop
= &fdp
->fd_ofileflags
[fd
];
720 error
= mac_file_check_fcntl(proc_ucred(p
), fp
->f_fglob
, uap
->cmd
,
729 case F_DUPFD_CLOEXEC
:
730 newmin
= CAST_DOWN_EXPLICIT(int, uap
->arg
); /* arg is an int, so we won't lose bits */
731 AUDIT_ARG(value32
, newmin
);
732 if ((u_int
)newmin
>= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
733 newmin
>= maxfiles
) {
737 if ( (error
= fdalloc(p
, newmin
, &i
)) )
739 error
= finishdup(p
, fdp
, fd
, i
,
740 uap
->cmd
== F_DUPFD_CLOEXEC
? UF_EXCLOSE
: 0, retval
);
744 *retval
= (*pop
& UF_EXCLOSE
)? 1 : 0;
749 AUDIT_ARG(value32
, uap
->arg
);
750 *pop
= (*pop
&~ UF_EXCLOSE
) |
751 (uap
->arg
& 1)? UF_EXCLOSE
: 0;
756 *retval
= OFLAGS(fp
->f_flag
);
761 fp
->f_flag
&= ~FCNTLFLAGS
;
762 tmp
= CAST_DOWN_EXPLICIT(int, uap
->arg
); /* arg is an int, so we won't lose bits */
763 AUDIT_ARG(value32
, tmp
);
764 fp
->f_flag
|= FFLAGS(tmp
) & FCNTLFLAGS
;
765 tmp
= fp
->f_flag
& FNONBLOCK
;
766 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
769 tmp
= fp
->f_flag
& FASYNC
;
770 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, &context
);
773 fp
->f_flag
&= ~FNONBLOCK
;
775 (void)fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, &context
);
779 if (fp
->f_type
== DTYPE_SOCKET
) {
780 *retval
= ((struct socket
*)fp
->f_data
)->so_pgid
;
784 error
= fo_ioctl(fp
, (int)TIOCGPGRP
, (caddr_t
)retval
, &context
);
789 tmp
= CAST_DOWN_EXPLICIT(pid_t
, uap
->arg
); /* arg is an int, so we won't lose bits */
790 AUDIT_ARG(value32
, tmp
);
791 if (fp
->f_type
== DTYPE_SOCKET
) {
792 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
796 if (fp
->f_type
== DTYPE_PIPE
) {
797 error
= fo_ioctl(fp
, TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
804 proc_t p1
= proc_find(tmp
);
809 tmp
= (int)p1
->p_pgrpid
;
812 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, &context
);
816 tmp
= CAST_DOWN_EXPLICIT(int, uap
->arg
);
817 if (fp
->f_type
== DTYPE_SOCKET
) {
818 error
= sock_setsockopt((struct socket
*)fp
->f_data
,
819 SOL_SOCKET
, SO_NOSIGPIPE
, &tmp
, sizeof (tmp
));
821 struct fileglob
*fg
= fp
->f_fglob
;
823 lck_mtx_lock_spin(&fg
->fg_lock
);
825 fg
->fg_lflags
|= FG_NOSIGPIPE
;
827 fg
->fg_lflags
&= FG_NOSIGPIPE
;
828 lck_mtx_unlock(&fg
->fg_lock
);
834 if (fp
->f_type
== DTYPE_SOCKET
) {
835 int retsize
= sizeof (*retval
);
836 error
= sock_getsockopt((struct socket
*)fp
->f_data
,
837 SOL_SOCKET
, SO_NOSIGPIPE
, retval
, &retsize
);
839 *retval
= (fp
->f_fglob
->fg_lflags
& FG_NOSIGPIPE
) ?
847 /* Fall into F_SETLK */
850 if (fp
->f_type
!= DTYPE_VNODE
) {
854 vp
= (struct vnode
*)fp
->f_data
;
857 offset
= fp
->f_offset
;
860 /* Copy in the lock structure */
861 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
866 volatile off_t affected_lock_area_set
= 0;
867 affected_lock_area_set
= fl
.l_start
+ offset
;
868 if ((fl
.l_whence
== SEEK_CUR
) && (affected_lock_area_set
< fl
.l_start
)) {
873 if ( (error
= vnode_getwithref(vp
)) ) {
876 if (fl
.l_whence
== SEEK_CUR
)
877 fl
.l_start
+= offset
;
880 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
890 if ((fflag
& FREAD
) == 0) {
895 // XXX UInt32 unsafe for LP64 kernel
896 OSBitOrAtomic(P_LADVLOCK
, &p
->p_ladvflag
);
897 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_SETLK
, &fl
, flg
, &context
);
902 if ((fflag
& FWRITE
) == 0) {
907 // XXX UInt32 unsafe for LP64 kernel
908 OSBitOrAtomic(P_LADVLOCK
, &p
->p_ladvflag
);
909 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_SETLK
, &fl
, flg
, &context
);
914 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &fl
,
929 if (fp
->f_type
!= DTYPE_VNODE
) {
933 vp
= (struct vnode
*)fp
->f_data
;
935 offset
= fp
->f_offset
;
938 /* Copy in the lock structure */
939 error
= copyin(argp
, (caddr_t
)&fl
, sizeof(fl
));
943 volatile off_t affected_lock_area_end
= 0;
944 affected_lock_area_end
= fl
.l_start
+ offset
;
945 /* Check starting byte and ending byte for EOVERFLOW in SEEK_CUR */
946 /* and ending byte for EOVERFLOW in SEEK_SET */
947 if (((fl
.l_whence
== SEEK_CUR
) &&
948 ((affected_lock_area_end
< fl
.l_start
) ||
949 ((fl
.l_len
> 0) && (affected_lock_area_end
+ fl
.l_len
- 1 < affected_lock_area_end
)))) ||
950 ((fl
.l_whence
== SEEK_SET
) && (fl
.l_len
> 0) && (fl
.l_start
+ fl
.l_len
- 1 < fl
.l_start
)))
952 /* lf_advlock doesn't check start/end for F_GETLK if file has no locks */
957 if ((fl
.l_whence
== SEEK_SET
) && (fl
.l_start
< 0)) {
972 switch (fl
.l_whence
) {
982 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
983 if (fl
.l_whence
== SEEK_CUR
)
984 fl
.l_start
+= offset
;
987 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
,
991 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, uap
->cmd
, &fl
, F_POSIX
, &context
);
996 error
= copyout((caddr_t
)&fl
, argp
, sizeof(fl
));
1000 case F_PREALLOCATE
: {
1001 fstore_t alloc_struct
; /* structure for allocate command */
1002 u_int32_t alloc_flags
= 0;
1004 if (fp
->f_type
!= DTYPE_VNODE
) {
1009 vp
= (struct vnode
*)fp
->f_data
;
1012 /* make sure that we have write permission */
1013 if ((fp
->f_flag
& FWRITE
) == 0) {
1018 error
= copyin(argp
, (caddr_t
)&alloc_struct
, sizeof(alloc_struct
));
1022 /* now set the space allocated to 0 */
1023 alloc_struct
.fst_bytesalloc
= 0;
1026 * Do some simple parameter checking
1029 /* set up the flags */
1031 alloc_flags
|= PREALLOCATE
;
1033 if (alloc_struct
.fst_flags
& F_ALLOCATECONTIG
)
1034 alloc_flags
|= ALLOCATECONTIG
;
1036 if (alloc_struct
.fst_flags
& F_ALLOCATEALL
)
1037 alloc_flags
|= ALLOCATEALL
;
1040 * Do any position mode specific stuff. The only
1041 * position mode supported now is PEOFPOSMODE
1044 switch (alloc_struct
.fst_posmode
) {
1047 if (alloc_struct
.fst_offset
!= 0) {
1052 alloc_flags
|= ALLOCATEFROMPEOF
;
1056 if (alloc_struct
.fst_offset
<= 0) {
1061 alloc_flags
|= ALLOCATEFROMVOL
;
1069 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1071 * call allocate to get the space
1073 error
= VNOP_ALLOCATE(vp
,alloc_struct
.fst_length
,alloc_flags
,
1074 &alloc_struct
.fst_bytesalloc
, alloc_struct
.fst_offset
,
1076 (void)vnode_put(vp
);
1078 error2
= copyout((caddr_t
)&alloc_struct
, argp
, sizeof(alloc_struct
));
1087 if (fp
->f_type
!= DTYPE_VNODE
) {
1091 vp
= (struct vnode
*)fp
->f_data
;
1094 error
= copyin(argp
, (caddr_t
)&offset
, sizeof (off_t
));
1097 AUDIT_ARG(value64
, offset
);
1099 error
= vnode_getwithref(vp
);
1104 error
= mac_vnode_check_truncate(&context
,
1105 fp
->f_fglob
->fg_cred
, vp
);
1107 (void)vnode_put(vp
);
1112 * Make sure that we are root. Growing a file
1113 * without zero filling the data is a security hole
1114 * root would have access anyway so we'll allow it
1122 error
= vnode_setsize(vp
, offset
, IO_NOZEROFILL
,
1126 (void)vnode_put(vp
);
1130 if (fp
->f_type
!= DTYPE_VNODE
) {
1135 fp
->f_fglob
->fg_flag
&= ~FNORDAHEAD
;
1137 fp
->f_fglob
->fg_flag
|= FNORDAHEAD
;
1142 if (fp
->f_type
!= DTYPE_VNODE
) {
1147 fp
->f_fglob
->fg_flag
|= FNOCACHE
;
1149 fp
->f_fglob
->fg_flag
&= ~FNOCACHE
;
1154 if (fp
->f_type
!= DTYPE_VNODE
) {
1159 fp
->f_fglob
->fg_flag
|= FNODIRECT
;
1161 fp
->f_fglob
->fg_flag
&= ~FNODIRECT
;
1165 case F_SINGLE_WRITER
:
1166 if (fp
->f_type
!= DTYPE_VNODE
) {
1171 fp
->f_fglob
->fg_flag
|= FSINGLE_WRITER
;
1173 fp
->f_fglob
->fg_flag
&= ~FSINGLE_WRITER
;
1177 case F_GLOBAL_NOCACHE
:
1178 if (fp
->f_type
!= DTYPE_VNODE
) {
1182 vp
= (struct vnode
*)fp
->f_data
;
1185 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1187 *retval
= vnode_isnocache(vp
);
1190 vnode_setnocache(vp
);
1192 vnode_clearnocache(vp
);
1194 (void)vnode_put(vp
);
1198 case F_CHECK_OPENEVT
:
1199 if (fp
->f_type
!= DTYPE_VNODE
) {
1203 vp
= (struct vnode
*)fp
->f_data
;
1206 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1208 *retval
= vnode_is_openevt(vp
);
1211 vnode_set_openevt(vp
);
1213 vnode_clear_openevt(vp
);
1215 (void)vnode_put(vp
);
1220 struct radvisory ra_struct
;
1222 if (fp
->f_type
!= DTYPE_VNODE
) {
1226 vp
= (struct vnode
*)fp
->f_data
;
1229 if ( (error
= copyin(argp
, (caddr_t
)&ra_struct
, sizeof(ra_struct
))) )
1231 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1232 error
= VNOP_IOCTL(vp
, F_RDADVISE
, (caddr_t
)&ra_struct
, 0, &context
);
1234 (void)vnode_put(vp
);
1241 if (fp
->f_type
!= DTYPE_VNODE
) {
1245 vp
= (struct vnode
*)fp
->f_data
;
1248 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1249 error
= cluster_push(vp
, 0);
1251 (void)vnode_put(vp
);
1256 case F_LOG2PHYS_EXT
: {
1257 struct log2phys l2p_struct
; /* structure for allocate command */
1260 off_t file_offset
= 0;
1264 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1265 error
= copyin(argp
, (caddr_t
)&l2p_struct
, sizeof(l2p_struct
));
1268 file_offset
= l2p_struct
.l2p_devoffset
;
1270 file_offset
= fp
->f_offset
;
1272 if (fp
->f_type
!= DTYPE_VNODE
) {
1276 vp
= (struct vnode
*)fp
->f_data
;
1278 if ( (error
= vnode_getwithref(vp
)) ) {
1281 error
= VNOP_OFFTOBLK(vp
, file_offset
, &lbn
);
1283 (void)vnode_put(vp
);
1286 error
= VNOP_BLKTOOFF(vp
, lbn
, &offset
);
1288 (void)vnode_put(vp
);
1291 devBlockSize
= vfs_devblocksize(vnode_mount(vp
));
1292 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1293 a_size
= l2p_struct
.l2p_contigbytes
;
1295 a_size
= devBlockSize
;
1298 error
= VNOP_BLOCKMAP(vp
, offset
, a_size
, &bn
, &run
, NULL
, 0, &context
);
1300 (void)vnode_put(vp
);
1303 l2p_struct
.l2p_flags
= 0; /* for now */
1304 if (uap
->cmd
== F_LOG2PHYS_EXT
) {
1305 l2p_struct
.l2p_contigbytes
= run
- (file_offset
- offset
);
1307 l2p_struct
.l2p_contigbytes
= 0; /* for now */
1309 l2p_struct
.l2p_devoffset
= bn
* devBlockSize
;
1310 l2p_struct
.l2p_devoffset
+= file_offset
- offset
;
1311 error
= copyout((caddr_t
)&l2p_struct
, argp
, sizeof(l2p_struct
));
1319 if (fp
->f_type
!= DTYPE_VNODE
) {
1323 vp
= (struct vnode
*)fp
->f_data
;
1326 pathlen
= MAXPATHLEN
;
1327 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
1328 if (pathbufp
== NULL
) {
1332 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1333 error
= vn_getpath(vp
, pathbufp
, &pathlen
);
1334 (void)vnode_put(vp
);
1337 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
1339 FREE(pathbufp
, M_TEMP
);
1343 case F_PATHPKG_CHECK
: {
1347 if (fp
->f_type
!= DTYPE_VNODE
) {
1351 vp
= (struct vnode
*)fp
->f_data
;
1354 pathlen
= MAXPATHLEN
;
1355 pathbufp
= kalloc(MAXPATHLEN
);
1357 if ( (error
= copyinstr(argp
, pathbufp
, MAXPATHLEN
, &pathlen
)) == 0 ) {
1358 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1359 AUDIT_ARG(text
, pathbufp
);
1360 error
= vn_path_package_check(vp
, pathbufp
, pathlen
, retval
);
1362 (void)vnode_put(vp
);
1365 kfree(pathbufp
, MAXPATHLEN
);
1369 case F_CHKCLEAN
: // used by regression tests to see if all dirty pages got cleaned by fsync()
1370 case F_FULLFSYNC
: // fsync + flush the journal + DKIOCSYNCHRONIZECACHE
1371 case F_FREEZE_FS
: // freeze all other fs operations for the fs of this fd
1372 case F_THAW_FS
: { // thaw all frozen fs operations for the fs of this fd
1373 if (fp
->f_type
!= DTYPE_VNODE
) {
1377 vp
= (struct vnode
*)fp
->f_data
;
1380 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1381 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
1383 (void)vnode_put(vp
);
1389 * SPI (private) for opening a file starting from a dir fd
1392 struct user_fopenfrom fopen
;
1393 struct vnode_attr va
;
1394 struct nameidata nd
;
1397 /* Check if this isn't a valid file descriptor */
1398 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1399 (fp
->f_flag
& FREAD
) == 0) {
1403 vp
= (struct vnode
*)fp
->f_data
;
1406 if (vnode_getwithref(vp
)) {
1411 /* Only valid for directories */
1412 if (vp
->v_type
!= VDIR
) {
1418 /* Get flags, mode and pathname arguments. */
1419 if (IS_64BIT_PROCESS(p
)) {
1420 error
= copyin(argp
, &fopen
, sizeof(fopen
));
1422 struct user32_fopenfrom fopen32
;
1424 error
= copyin(argp
, &fopen32
, sizeof(fopen32
));
1425 fopen
.o_flags
= fopen32
.o_flags
;
1426 fopen
.o_mode
= fopen32
.o_mode
;
1427 fopen
.o_pathname
= CAST_USER_ADDR_T(fopen32
.o_pathname
);
1433 AUDIT_ARG(fflags
, fopen
.o_flags
);
1434 AUDIT_ARG(mode
, fopen
.o_mode
);
1436 /* Mask off all but regular access permissions */
1437 cmode
= ((fopen
.o_mode
&~ fdp
->fd_cmask
) & ALLPERMS
) & ~S_ISTXT
;
1438 VATTR_SET(&va
, va_mode
, cmode
& ACCESSPERMS
);
1440 /* Start the lookup relative to the file descriptor's vnode. */
1441 NDINIT(&nd
, LOOKUP
, OP_OPEN
, USEDVP
| FOLLOW
| AUDITVNPATH1
, UIO_USERSPACE
,
1442 fopen
.o_pathname
, &context
);
1445 error
= open1(&context
, &nd
, fopen
.o_flags
, &va
, retval
);
1451 * SPI (private) for unlinking a file starting from a dir fd
1453 case F_UNLINKFROM
: {
1454 struct nameidata nd
;
1455 user_addr_t pathname
;
1457 /* Check if this isn't a valid file descriptor */
1458 if ((fp
->f_type
!= DTYPE_VNODE
) ||
1459 (fp
->f_flag
& FREAD
) == 0) {
1463 vp
= (struct vnode
*)fp
->f_data
;
1466 if (vnode_getwithref(vp
)) {
1471 /* Only valid for directories */
1472 if (vp
->v_type
!= VDIR
) {
1478 /* Get flags, mode and pathname arguments. */
1479 if (IS_64BIT_PROCESS(p
)) {
1480 pathname
= (user_addr_t
)argp
;
1482 pathname
= CAST_USER_ADDR_T(argp
);
1485 /* Start the lookup relative to the file descriptor's vnode. */
1486 NDINIT(&nd
, DELETE
, OP_UNLINK
, USEDVP
| AUDITVNPATH1
, UIO_USERSPACE
,
1487 pathname
, &context
);
1490 error
= unlink1(&context
, &nd
, 0);
1500 struct user_fsignatures fs
;
1502 vm_offset_t kernel_blob_addr
;
1503 vm_size_t kernel_blob_size
;
1505 if (fp
->f_type
!= DTYPE_VNODE
) {
1509 vp
= (struct vnode
*)fp
->f_data
;
1511 error
= vnode_getwithref(vp
);
1515 if (IS_64BIT_PROCESS(p
)) {
1516 error
= copyin(argp
, &fs
, sizeof (fs
));
1518 struct user32_fsignatures fs32
;
1520 error
= copyin(argp
, &fs32
, sizeof (fs32
));
1521 fs
.fs_file_start
= fs32
.fs_file_start
;
1522 fs
.fs_blob_start
= CAST_USER_ADDR_T(fs32
.fs_blob_start
);
1523 fs
.fs_blob_size
= fs32
.fs_blob_size
;
1531 if(ubc_cs_blob_get(vp
, CPU_TYPE_ANY
, fs
.fs_file_start
))
1535 printf("CODE SIGNING: resident blob offered for: %s\n", vp->v_name);
1541 #define CS_MAX_BLOB_SIZE (1280ULL * 1024) /* max shared cache file XXX ? */
1542 if (fs
.fs_blob_size
> CS_MAX_BLOB_SIZE
) {
1548 kernel_blob_size
= CAST_DOWN(vm_size_t
, fs
.fs_blob_size
);
1549 kr
= ubc_cs_blob_allocate(&kernel_blob_addr
, &kernel_blob_size
);
1550 if (kr
!= KERN_SUCCESS
) {
1556 if(uap
->cmd
== F_ADDSIGS
) {
1557 error
= copyin(fs
.fs_blob_start
,
1558 (void *) kernel_blob_addr
,
1560 } else /* F_ADDFILESIGS */ {
1561 error
= vn_rdwr(UIO_READ
,
1563 (caddr_t
) kernel_blob_addr
,
1565 fs
.fs_file_start
+ fs
.fs_blob_start
,
1574 ubc_cs_blob_deallocate(kernel_blob_addr
,
1580 error
= ubc_cs_blob_add(
1582 CPU_TYPE_ANY
, /* not for a specific architecture */
1587 ubc_cs_blob_deallocate(kernel_blob_addr
,
1590 /* ubc_blob_add() has consumed "kernel_blob_addr" */
1591 #if CHECK_CS_VALIDATION_BITMAP
1592 ubc_cs_validation_bitmap_allocate( vp
);
1596 (void) vnode_put(vp
);
1600 case F_MARKDEPENDENCY
: {
1601 struct vnode_attr va
;
1602 vfs_context_t ctx
= vfs_context_current();
1605 if ((current_proc()->p_flag
& P_DEPENDENCY_CAPABLE
) == 0) {
1610 if (fp
->f_type
!= DTYPE_VNODE
) {
1615 vp
= (struct vnode
*)fp
->f_data
;
1618 if (vnode_getwithref(vp
)) {
1623 if (!vnode_isvroot(vp
)) {
1629 // get the owner of the root dir
1631 VATTR_WANTED(&va
, va_uid
);
1632 if (vnode_getattr(vp
, &va
, ctx
) != 0) {
1638 // and last, check that the caller is the super user or
1639 // the owner of the mount point
1640 cred
= vfs_context_ucred(ctx
);
1641 if (!is_suser() && va
.va_uid
!= kauth_cred_getuid(cred
)) {
1647 // if all those checks pass then we can mark the dependency
1648 vfs_markdependency(vp
->v_mount
);
1657 case F_GETPROTECTIONCLASS
: {
1660 if (fp
->f_type
!= DTYPE_VNODE
) {
1664 vp
= (struct vnode
*)fp
->f_data
;
1668 if (vnode_getwithref(vp
)) {
1673 error
= cp_vnode_getclass (vp
, &class);
1682 case F_SETPROTECTIONCLASS
: {
1683 /* tmp must be a valid PROTECTION_CLASS_* */
1684 tmp
= CAST_DOWN_EXPLICIT(uint32_t, uap
->arg
);
1686 if (fp
->f_type
!= DTYPE_VNODE
) {
1690 vp
= (struct vnode
*)fp
->f_data
;
1694 if (vnode_getwithref(vp
)) {
1699 /* Only go forward if you have write access */
1700 vfs_context_t ctx
= vfs_context_current();
1701 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
1706 error
= cp_vnode_setclass (vp
, tmp
);
1711 case F_TRANSCODEKEY
: {
1713 if (fp
->f_type
!= DTYPE_VNODE
) {
1718 vp
= (struct vnode
*)fp
->f_data
;
1721 if (vnode_getwithref(vp
)) {
1726 error
= cp_vnode_transcode (vp
);
1731 case F_GETPROTECTIONLEVEL
: {
1732 uint32_t cp_version
= 0;
1734 if (fp
->f_type
!= DTYPE_VNODE
) {
1739 vp
= (struct vnode
*) fp
->f_data
;
1742 if (vnode_getwithref(vp
)) {
1748 * if cp_get_major_vers fails, error will be set to proper errno
1749 * and cp_version will still be 0.
1752 error
= cp_get_root_major_vers (vp
, &cp_version
);
1753 *retval
= cp_version
;
1759 #endif /* CONFIG_PROTECT */
1761 case F_MOVEDATAEXTENTS
: {
1762 struct fileproc
*fp2
= NULL
;
1763 struct vnode
*src_vp
= NULLVP
;
1764 struct vnode
*dst_vp
= NULLVP
;
1765 /* We need to grab the 2nd FD out of the argments before moving on. */
1766 int fd2
= CAST_DOWN_EXPLICIT(int32_t, uap
->arg
);
1768 if (fp
->f_type
!= DTYPE_VNODE
) {
1773 /* For now, special case HFS+ only, since this is SPI. */
1774 src_vp
= (struct vnode
*)fp
->f_data
;
1775 if (src_vp
->v_tag
!= VT_HFS
) {
1781 * Get the references before we start acquiring iocounts on the vnodes,
1782 * while we still hold the proc fd lock
1784 if ( (error
= fp_lookup(p
, fd2
, &fp2
, 1)) ) {
1788 if (fp2
->f_type
!= DTYPE_VNODE
) {
1789 fp_drop(p
, fd2
, fp2
, 1);
1793 dst_vp
= (struct vnode
*)fp2
->f_data
;
1794 if (dst_vp
->v_tag
!= VT_HFS
) {
1795 fp_drop(p
, fd2
, fp2
, 1);
1801 /* Re-do MAC checks against the new FD, pass in a fake argument */
1802 error
= mac_file_check_fcntl(proc_ucred(p
), fp2
->f_fglob
, uap
->cmd
, 0);
1804 fp_drop(p
, fd2
, fp2
, 1);
1808 /* Audit the 2nd FD */
1813 if (vnode_getwithref(src_vp
)) {
1814 fp_drop(p
, fd2
, fp2
, 0);
1818 if (vnode_getwithref(dst_vp
)) {
1820 fp_drop(p
, fd2
, fp2
, 0);
1826 * Basic asserts; validate they are not the same and that
1827 * both live on the same filesystem.
1829 if (dst_vp
== src_vp
) {
1832 fp_drop (p
, fd2
, fp2
, 0);
1837 if (dst_vp
->v_mount
!= src_vp
->v_mount
) {
1840 fp_drop (p
, fd2
, fp2
, 0);
1845 /* Now we have a legit pair of FDs. Go to work */
1847 /* Now check for write access to the target files */
1848 if(vnode_authorize(src_vp
, NULLVP
,
1849 (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), &context
) != 0) {
1852 fp_drop(p
, fd2
, fp2
, 0);
1857 if(vnode_authorize(dst_vp
, NULLVP
,
1858 (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), &context
) != 0) {
1861 fp_drop(p
, fd2
, fp2
, 0);
1866 /* Verify that both vps point to files and not directories */
1867 if ( !vnode_isreg(src_vp
) || !vnode_isreg(dst_vp
)) {
1871 fp_drop (p
, fd2
, fp2
, 0);
1876 * The exchangedata syscall handler passes in 0 for the flags to VNOP_EXCHANGE.
1877 * We'll pass in our special bit indicating that the new behavior is expected
1880 error
= VNOP_EXCHANGE(src_vp
, dst_vp
, FSOPT_EXCHANGE_DATA_ONLY
, &context
);
1884 fp_drop(p
, fd2
, fp2
, 0);
1890 * SPI (private) for indicating to a filesystem that subsequent writes to
1891 * the open FD will represent static content.
1893 case F_SETSTATICCONTENT
: {
1894 caddr_t ioctl_arg
= NULL
;
1897 ioctl_arg
= (caddr_t
) 1;
1900 if (fp
->f_type
!= DTYPE_VNODE
) {
1904 vp
= (struct vnode
*)fp
->f_data
;
1907 error
= vnode_getwithref(vp
);
1913 /* Only go forward if you have write access */
1914 vfs_context_t ctx
= vfs_context_current();
1915 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
1921 error
= VNOP_IOCTL(vp
, uap
->cmd
, ioctl_arg
, 0, &context
);
1922 (void)vnode_put(vp
);
1928 * Set the vnode pointed to by 'fd'
1929 * and tag it as the (potentially future) backing store
1930 * for another filesystem
1932 case F_SETBACKINGSTORE
: {
1933 if (fp
->f_type
!= DTYPE_VNODE
) {
1938 vp
= (struct vnode
*)fp
->f_data
;
1940 if (vp
->v_tag
!= VT_HFS
) {
1946 if (vnode_getwithref(vp
)) {
1951 /* only proceed if you have write access */
1952 vfs_context_t ctx
= vfs_context_current();
1953 if(vnode_authorize(vp
, NULLVP
, (KAUTH_VNODE_ACCESS
| KAUTH_VNODE_WRITE_DATA
), ctx
) != 0) {
1960 /* If arg != 0, set, otherwise unset */
1962 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
)1, 0, &context
);
1965 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
1973 * like F_GETPATH, but special semantics for
1974 * the mobile time machine handler.
1976 case F_GETPATH_MTMINFO
: {
1980 if (fp
->f_type
!= DTYPE_VNODE
) {
1984 vp
= (struct vnode
*)fp
->f_data
;
1987 pathlen
= MAXPATHLEN
;
1988 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
1989 if (pathbufp
== NULL
) {
1993 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
1994 int backingstore
= 0;
1996 /* Check for error from vn_getpath before moving on */
1997 if ((error
= vn_getpath(vp
, pathbufp
, &pathlen
)) == 0) {
1998 if (vp
->v_tag
== VT_HFS
) {
1999 error
= VNOP_IOCTL (vp
, uap
->cmd
, (caddr_t
) &backingstore
, 0, &context
);
2001 (void)vnode_put(vp
);
2004 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
2008 * If the copyout was successful, now check to ensure
2009 * that this vnode is not a BACKINGSTORE vnode. mtmd
2010 * wants the path regardless.
2017 (void)vnode_put(vp
);
2019 FREE(pathbufp
, M_TEMP
);
2025 * This is an fcntl() that we d not recognize at this level;
2026 * if this is a vnode, we send it down into the VNOP_IOCTL
2027 * for this vnode; this can include special devices, and will
2028 * effectively overload fcntl() to send ioctl()'s.
2030 if((uap
->cmd
& IOC_VOID
) && (uap
->cmd
& IOC_INOUT
)){
2035 if (fp
->f_type
!= DTYPE_VNODE
) {
2039 vp
= (struct vnode
*)fp
->f_data
;
2042 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
2043 #define STK_PARAMS 128
2044 char stkbuf
[STK_PARAMS
];
2048 * For this to work properly, we have to copy in the
2049 * ioctl() cmd argument if there is one; we must also
2050 * check that a command parameter, if present, does
2051 * not exceed the maximum command length dictated by
2052 * the number of bits we have available in the command
2053 * to represent a structure length. Finally, we have
2054 * to copy the results back out, if it is that type of
2057 size
= IOCPARM_LEN(uap
->cmd
);
2058 if (size
> IOCPARM_MAX
) {
2059 (void)vnode_put(vp
);
2065 if (size
> sizeof (stkbuf
)) {
2066 if ((memp
= (caddr_t
)kalloc(size
)) == 0) {
2067 (void)vnode_put(vp
);
2076 if (uap
->cmd
& IOC_IN
) {
2079 error
= copyin(argp
, data
, size
);
2081 (void)vnode_put(vp
);
2089 *(user_addr_t
*)data
= argp
;
2091 *(uint32_t *)data
= (uint32_t)argp
;
2094 } else if ((uap
->cmd
& IOC_OUT
) && size
) {
2096 * Zero the buffer so the user always
2097 * gets back something deterministic.
2100 } else if (uap
->cmd
& IOC_VOID
) {
2102 *(user_addr_t
*)data
= argp
;
2104 *(uint32_t *)data
= (uint32_t)argp
;
2108 error
= VNOP_IOCTL(vp
, uap
->cmd
, CAST_DOWN(caddr_t
, data
), 0, &context
);
2110 (void)vnode_put(vp
);
2112 /* Copy any output data to user */
2113 if (error
== 0 && (uap
->cmd
& IOC_OUT
) && size
)
2114 error
= copyout(data
, argp
, size
);
2122 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
2123 fp_drop(p
, fd
, fp
, 0);
2126 fp_drop(p
, fd
, fp
, 1);
2135 * Description: Common code for dup, dup2, and fcntl(F_DUPFD).
2137 * Parameters: p Process performing the dup
2139 * new The fd to dup it to
2140 * fd_flags Flags to augment the new fd
2141 * retval Pointer to the call return area
2143 * Returns: 0 Success
2148 * *retval (modified) The new descriptor
2150 * Locks: Assumes proc_fdlock for process pointing to fdp is held by
2153 * Notes: This function may drop and reacquire this lock; it is unsafe
2154 * for a caller to assume that other state protected by the lock
2155 * has not been subsequently changed out from under it.
2159 struct filedesc
*fdp
, int old
, int new, int fd_flags
, int32_t *retval
)
2161 struct fileproc
*nfp
;
2162 struct fileproc
*ofp
;
2168 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2170 if ((ofp
= fdp
->fd_ofiles
[old
]) == NULL
||
2171 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
)) {
2178 error
= mac_file_check_dup(proc_ucred(p
), ofp
->f_fglob
, new);
2188 MALLOC_ZONE(nfp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
2189 /* Failure check follows proc_fdlock() due to handling requirements */
2199 bzero(nfp
, sizeof(struct fileproc
));
2202 nfp
->f_fglob
= ofp
->f_fglob
;
2206 if (fdp
->fd_ofiles
[new] != 0)
2207 panic("finishdup: overwriting fd_ofiles with new %d", new);
2208 if ((fdp
->fd_ofileflags
[new] & UF_RESERVED
) == 0)
2209 panic("finishdup: unreserved fileflags with new %d", new);
2212 if (new > fdp
->fd_lastfile
)
2213 fdp
->fd_lastfile
= new;
2214 *fdflags(p
, new) |= fd_flags
;
2215 procfdtbl_releasefd(p
, new, nfp
);
2224 * Description: The implementation of the close(2) system call
2226 * Parameters: p Process in whose per process file table
2227 * the close is to occur
2228 * uap->fd fd to be closed
2231 * Returns: 0 Success
2232 * fp_lookup:EBADF Bad file descriptor
2233 * close_internal:EBADF
2234 * close_internal:??? Anything returnable by a per-fileops
2238 close(proc_t p
, struct close_args
*uap
, int32_t *retval
)
2240 __pthread_testcancel(1);
2241 return(close_nocancel(p
, (struct close_nocancel_args
*)uap
, retval
));
2246 close_nocancel(proc_t p
, struct close_nocancel_args
*uap
, __unused
int32_t *retval
)
2248 struct fileproc
*fp
;
2252 AUDIT_SYSCLOSE(p
, fd
);
2256 if ( (error
= fp_lookup(p
,fd
,&fp
, 1)) ) {
2261 error
= close_internal_locked(p
, fd
, fp
, 0);
2270 * close_internal_locked
2272 * Close a file descriptor.
2274 * Parameters: p Process in whose per process file table
2275 * the close is to occur
2276 * fd fd to be closed
2277 * fp fileproc associated with the fd
2279 * Returns: 0 Success
2280 * EBADF fd already in close wait state
2281 * closef_locked:??? Anything returnable by a per-fileops
2284 * Locks: Assumes proc_fdlock for process is held by the caller and returns
2287 * Notes: This function may drop and reacquire this lock; it is unsafe
2288 * for a caller to assume that other state protected by the lock
2289 * has not been subsequently changes out from under it, if the
2290 * caller made the call with the lock held.
2293 close_internal_locked(proc_t p
, int fd
, struct fileproc
*fp
, int flags
)
2295 struct filedesc
*fdp
= p
->p_fd
;
2297 int resvfd
= flags
& FD_DUP2RESV
;
2301 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2304 /* Keep people from using the filedesc while we are closing it */
2305 procfdtbl_markclosefd(p
, fd
);
2308 if ((fp
->f_flags
& FP_CLOSING
) == FP_CLOSING
) {
2309 panic("close_internal_locked: being called on already closing fd");
2314 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
2315 panic("close_internal: unreserved fileflags with fd %d", fd
);
2318 fp
->f_flags
|= FP_CLOSING
;
2320 if ( (fp
->f_flags
& FP_AIOISSUED
) || kauth_authorize_fileop_has_listeners() ) {
2324 if ( (fp
->f_type
== DTYPE_VNODE
) && kauth_authorize_fileop_has_listeners() ) {
2326 * call out to allow 3rd party notification of close.
2327 * Ignore result of kauth_authorize_fileop call.
2329 if (vnode_getwithref((vnode_t
)fp
->f_data
) == 0) {
2330 u_int fileop_flags
= 0;
2331 if ((fp
->f_flags
& FP_WRITTEN
) != 0)
2332 fileop_flags
|= KAUTH_FILEOP_CLOSE_MODIFIED
;
2333 kauth_authorize_fileop(fp
->f_fglob
->fg_cred
, KAUTH_FILEOP_CLOSE
,
2334 (uintptr_t)fp
->f_data
, (uintptr_t)fileop_flags
);
2335 vnode_put((vnode_t
)fp
->f_data
);
2338 if (fp
->f_flags
& FP_AIOISSUED
)
2340 * cancel all async IO requests that can be cancelled.
2342 _aio_close( p
, fd
);
2347 if (fd
< fdp
->fd_knlistsize
)
2348 knote_fdclose(p
, fd
);
2350 if (fp
->f_flags
& FP_WAITEVENT
)
2351 (void)waitevent_close(p
, fp
);
2353 if ((fp
->f_flags
& FP_INCHRREAD
) == 0)
2354 fileproc_drain(p
, fp
);
2359 procfdtbl_reservefd(p
, fd
);
2362 error
= closef_locked(fp
, fp
->f_fglob
, p
);
2363 if ((fp
->f_flags
& FP_WAITCLOSE
) == FP_WAITCLOSE
)
2364 wakeup(&fp
->f_flags
);
2365 fp
->f_flags
&= ~(FP_WAITCLOSE
| FP_CLOSING
);
2369 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
2375 if ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) == 0)
2376 panic("close with reserved fd returns with freed fd:%d: proc: %p", fd
, p
);
2387 * Description: Return status information about a file descriptor.
2389 * Parameters: p The process doing the fstat
2391 * ub The user stat buffer
2392 * xsecurity The user extended security
2393 * buffer, or 0 if none
2394 * xsecurity_size The size of xsecurity, or 0
2396 * isstat64 Flag to indicate 64 bit version
2397 * for inode size, etc.
2399 * Returns: 0 Success
2402 * fp_lookup:EBADF Bad file descriptor
2403 * vnode_getwithref:???
2405 * vnode_getwithref:???
2412 * Notes: Internal implementation for all other fstat() related
2415 * XXX switch on node type is bogus; need a stat in struct
2416 * XXX fileops instead.
2419 fstat1(proc_t p
, int fd
, user_addr_t ub
, user_addr_t xsecurity
, user_addr_t xsecurity_size
, int isstat64
)
2421 struct fileproc
*fp
;
2427 struct user64_stat user64_sb
;
2428 struct user32_stat user32_sb
;
2429 struct user64_stat64 user64_sb64
;
2430 struct user32_stat64 user32_sb64
;
2436 kauth_filesec_t fsec
;
2437 user_size_t xsecurity_bufsize
;
2438 vfs_context_t ctx
= vfs_context_current();
2444 if ((error
= fp_lookup(p
, fd
, &fp
, 0)) != 0) {
2449 fsec
= KAUTH_FILESEC_NONE
;
2451 sbptr
= (void *)&source
;
2456 if ((error
= vnode_getwithref((vnode_t
)data
)) == 0) {
2458 * If the caller has the file open, and is not
2459 * requesting extended security information, we are
2460 * going to let them get the basic stat information.
2462 if (xsecurity
== USER_ADDR_NULL
) {
2463 error
= vn_stat_noauth((vnode_t
)data
, sbptr
, NULL
, isstat64
, ctx
);
2465 error
= vn_stat((vnode_t
)data
, sbptr
, &fsec
, isstat64
, ctx
);
2468 AUDIT_ARG(vnpath
, (struct vnode
*)data
, ARG_VNODE1
);
2469 (void)vnode_put((vnode_t
)data
);
2475 error
= soo_stat((struct socket
*)data
, sbptr
, isstat64
);
2477 #endif /* SOCKETS */
2480 error
= pipe_stat((void *)data
, sbptr
, isstat64
);
2484 error
= pshm_stat((void *)data
, sbptr
, isstat64
);
2488 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
2489 error
= kqueue_stat(fp
, sbptr
, isstat64
, p
);
2490 thread_funnel_set(kernel_flock
, funnel_state
);
2500 if (isstat64
!= 0) {
2501 source
.sb64
.st_lspare
= 0;
2502 source
.sb64
.st_qspare
[0] = 0LL;
2503 source
.sb64
.st_qspare
[1] = 0LL;
2505 if (IS_64BIT_PROCESS(current_proc())) {
2506 munge_user64_stat64(&source
.sb64
, &dest
.user64_sb64
);
2507 my_size
= sizeof(dest
.user64_sb64
);
2508 sbp
= (caddr_t
)&dest
.user64_sb64
;
2510 munge_user32_stat64(&source
.sb64
, &dest
.user32_sb64
);
2511 my_size
= sizeof(dest
.user32_sb64
);
2512 sbp
= (caddr_t
)&dest
.user32_sb64
;
2515 source
.sb
.st_lspare
= 0;
2516 source
.sb
.st_qspare
[0] = 0LL;
2517 source
.sb
.st_qspare
[1] = 0LL;
2518 if (IS_64BIT_PROCESS(current_proc())) {
2519 munge_user64_stat(&source
.sb
, &dest
.user64_sb
);
2520 my_size
= sizeof(dest
.user64_sb
);
2521 sbp
= (caddr_t
)&dest
.user64_sb
;
2523 munge_user32_stat(&source
.sb
, &dest
.user32_sb
);
2524 my_size
= sizeof(dest
.user32_sb
);
2525 sbp
= (caddr_t
)&dest
.user32_sb
;
2529 error
= copyout(sbp
, ub
, my_size
);
2532 /* caller wants extended security information? */
2533 if (xsecurity
!= USER_ADDR_NULL
) {
2535 /* did we get any? */
2536 if (fsec
== KAUTH_FILESEC_NONE
) {
2537 if (susize(xsecurity_size
, 0) != 0) {
2542 /* find the user buffer size */
2543 xsecurity_bufsize
= fusize(xsecurity_size
);
2545 /* copy out the actual data size */
2546 if (susize(xsecurity_size
, KAUTH_FILESEC_COPYSIZE(fsec
)) != 0) {
2551 /* if the caller supplied enough room, copy out to it */
2552 if (xsecurity_bufsize
>= KAUTH_FILESEC_COPYSIZE(fsec
))
2553 error
= copyout(fsec
, xsecurity
, KAUTH_FILESEC_COPYSIZE(fsec
));
2557 fp_drop(p
, fd
, fp
, 0);
2559 kauth_filesec_free(fsec
);
2567 * Description: Extended version of fstat supporting returning extended
2568 * security information
2570 * Parameters: p The process doing the fstat
2571 * uap->fd The fd to stat
2572 * uap->ub The user stat buffer
2573 * uap->xsecurity The user extended security
2574 * buffer, or 0 if none
2575 * uap->xsecurity_size The size of xsecurity, or 0
2577 * Returns: 0 Success
2578 * !0 Errno (see fstat1)
2581 fstat_extended(proc_t p
, struct fstat_extended_args
*uap
, __unused
int32_t *retval
)
2583 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 0));
2590 * Description: Get file status for the file associated with fd
2592 * Parameters: p The process doing the fstat
2593 * uap->fd The fd to stat
2594 * uap->ub The user stat buffer
2596 * Returns: 0 Success
2597 * !0 Errno (see fstat1)
2600 fstat(proc_t p
, register struct fstat_args
*uap
, __unused
int32_t *retval
)
2602 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 0));
2609 * Description: Extended version of fstat64 supporting returning extended
2610 * security information
2612 * Parameters: p The process doing the fstat
2613 * uap->fd The fd to stat
2614 * uap->ub The user stat buffer
2615 * uap->xsecurity The user extended security
2616 * buffer, or 0 if none
2617 * uap->xsecurity_size The size of xsecurity, or 0
2619 * Returns: 0 Success
2620 * !0 Errno (see fstat1)
2623 fstat64_extended(proc_t p
, struct fstat64_extended_args
*uap
, __unused
int32_t *retval
)
2625 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
, 1));
2632 * Description: Get 64 bit version of the file status for the file associated
2635 * Parameters: p The process doing the fstat
2636 * uap->fd The fd to stat
2637 * uap->ub The user stat buffer
2639 * Returns: 0 Success
2640 * !0 Errno (see fstat1)
2643 fstat64(proc_t p
, register struct fstat64_args
*uap
, __unused
int32_t *retval
)
2645 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0, 1));
2652 * Description: Return pathconf information about a file descriptor.
2654 * Parameters: p Process making the request
2655 * uap->fd fd to get information about
2656 * uap->name Name of information desired
2657 * retval Pointer to the call return area
2659 * Returns: 0 Success
2661 * fp_lookup:EBADF Bad file descriptor
2662 * vnode_getwithref:???
2666 * *retval (modified) Returned information (numeric)
2669 fpathconf(proc_t p
, struct fpathconf_args
*uap
, int32_t *retval
)
2672 struct fileproc
*fp
;
2679 AUDIT_ARG(fd
, uap
->fd
);
2680 if ( (error
= fp_lookup(p
, fd
, &fp
, 0)) )
2688 if (uap
->name
!= _PC_PIPE_BUF
) {
2697 if (uap
->name
!= _PC_PIPE_BUF
) {
2706 vp
= (struct vnode
*)data
;
2708 if ( (error
= vnode_getwithref(vp
)) == 0) {
2709 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
2711 error
= vn_pathconf(vp
, uap
->name
, retval
, vfs_context_current());
2713 (void)vnode_put(vp
);
2720 case DTYPE_FSEVENTS
:
2727 fp_drop(p
, fd
, fp
, 0);
2732 * Statistics counter for the number of times a process calling fdalloc()
2733 * has resulted in an expansion of the per process open file table.
2735 * XXX This would likely be of more use if it were per process
2743 * Description: Allocate a file descriptor for the process.
2745 * Parameters: p Process to allocate the fd in
2746 * want The fd we would prefer to get
2747 * result Pointer to fd we got
2749 * Returns: 0 Success
2754 * *result (modified) The fd which was allocated
2757 fdalloc(proc_t p
, int want
, int *result
)
2759 struct filedesc
*fdp
= p
->p_fd
;
2761 int lim
, last
, numfiles
, oldnfiles
;
2762 struct fileproc
**newofiles
, **ofiles
;
2763 char *newofileflags
;
2766 * Search for a free descriptor starting at the higher
2767 * of want or fd_freefile. If that fails, consider
2768 * expanding the ofile array.
2771 proc_fdlock_assert(p
, LCK_MTX_ASSERT_OWNED
);
2774 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
2776 last
= min(fdp
->fd_nfiles
, lim
);
2777 if ((i
= want
) < fdp
->fd_freefile
)
2778 i
= fdp
->fd_freefile
;
2779 for (; i
< last
; i
++) {
2780 if (fdp
->fd_ofiles
[i
] == NULL
&& !(fdp
->fd_ofileflags
[i
] & UF_RESERVED
)) {
2781 procfdtbl_reservefd(p
, i
);
2782 if (i
> fdp
->fd_lastfile
)
2783 fdp
->fd_lastfile
= i
;
2784 if (want
<= fdp
->fd_freefile
)
2785 fdp
->fd_freefile
= i
;
2792 * No space in current array. Expand?
2794 if (fdp
->fd_nfiles
>= lim
)
2796 if (fdp
->fd_nfiles
< NDEXTENT
)
2797 numfiles
= NDEXTENT
;
2799 numfiles
= 2 * fdp
->fd_nfiles
;
2804 MALLOC_ZONE(newofiles
, struct fileproc
**,
2805 numfiles
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
2807 if (newofiles
== NULL
) {
2810 if (fdp
->fd_nfiles
>= numfiles
) {
2811 FREE_ZONE(newofiles
, numfiles
* OFILESIZE
, M_OFILETABL
);
2814 newofileflags
= (char *) &newofiles
[numfiles
];
2816 * Copy the existing ofile and ofileflags arrays
2817 * and zero the new portion of each array.
2819 oldnfiles
= fdp
->fd_nfiles
;
2820 (void) memcpy(newofiles
, fdp
->fd_ofiles
,
2821 oldnfiles
* sizeof(*fdp
->fd_ofiles
));
2822 (void) memset(&newofiles
[oldnfiles
], 0,
2823 (numfiles
- oldnfiles
) * sizeof(*fdp
->fd_ofiles
));
2825 (void) memcpy(newofileflags
, fdp
->fd_ofileflags
,
2826 oldnfiles
* sizeof(*fdp
->fd_ofileflags
));
2827 (void) memset(&newofileflags
[oldnfiles
], 0,
2828 (numfiles
- oldnfiles
) *
2829 sizeof(*fdp
->fd_ofileflags
));
2830 ofiles
= fdp
->fd_ofiles
;
2831 fdp
->fd_ofiles
= newofiles
;
2832 fdp
->fd_ofileflags
= newofileflags
;
2833 fdp
->fd_nfiles
= numfiles
;
2834 FREE_ZONE(ofiles
, oldnfiles
* OFILESIZE
, M_OFILETABL
);
2843 * Description: Check to see whether n user file descriptors are available
2846 * Parameters: p Process to check in
2847 * n The number of fd's desired
2852 * Locks: Assumes proc_fdlock for process is held by the caller
2854 * Notes: The answer only remains valid so long as the proc_fdlock is
2855 * held by the caller.
2858 fdavail(proc_t p
, int n
)
2860 struct filedesc
*fdp
= p
->p_fd
;
2861 struct fileproc
**fpp
;
2865 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
2866 if ((i
= lim
- fdp
->fd_nfiles
) > 0 && (n
-= i
) <= 0)
2868 fpp
= &fdp
->fd_ofiles
[fdp
->fd_freefile
];
2869 flags
= &fdp
->fd_ofileflags
[fdp
->fd_freefile
];
2870 for (i
= fdp
->fd_nfiles
- fdp
->fd_freefile
; --i
>= 0; fpp
++, flags
++)
2871 if (*fpp
== NULL
&& !(*flags
& UF_RESERVED
) && --n
<= 0)
2880 * Description: Legacy KPI wrapper function for _fdrelse
2882 * Parameters: p Process in which fd lives
2887 * Locks: Assumes proc_fdlock for process is held by the caller
2890 fdrelse(proc_t p
, int fd
)
2899 * Description: Get the fileproc pointer for the given fd from the per process
2900 * open file table without taking an explicit reference on it.
2902 * Parameters: p Process containing fd
2903 * fd fd to obtain fileproc for
2904 * resultfp Pointer to pointer return area
2906 * Returns: 0 Success
2910 * *resultfp (modified) Pointer to fileproc pointer
2912 * Locks: Assumes proc_fdlock for process is held by the caller
2914 * Notes: Because there is no reference explicitly taken, the returned
2915 * fileproc pointer is only valid so long as the proc_fdlock
2916 * remains held by the caller.
2919 fdgetf_noref(proc_t p
, int fd
, struct fileproc
**resultfp
)
2921 struct filedesc
*fdp
= p
->p_fd
;
2922 struct fileproc
*fp
;
2924 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2925 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2926 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2938 * Description: Get fileproc and vnode pointer for a given fd from the per
2939 * process open file table of the specified process, and if
2940 * successful, increment the f_iocount
2942 * Parameters: p Process in which fd lives
2943 * fd fd to get information for
2944 * resultfp Pointer to result fileproc
2945 * pointer area, or 0 if none
2946 * resultvp Pointer to result vnode pointer
2947 * area, or 0 if none
2949 * Returns: 0 Success
2950 * EBADF Bad file descriptor
2951 * ENOTSUP fd does not refer to a vnode
2954 * *resultfp (modified) Fileproc pointer
2955 * *resultvp (modified) vnode pointer
2957 * Notes: The resultfp and resultvp fields are optional, and may be
2958 * independently specified as NULL to skip returning information
2960 * Locks: Internally takes and releases proc_fdlock
2963 fp_getfvp(proc_t p
, int fd
, struct fileproc
**resultfp
, struct vnode
**resultvp
)
2965 struct filedesc
*fdp
= p
->p_fd
;
2966 struct fileproc
*fp
;
2968 proc_fdlock_spin(p
);
2969 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
2970 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
2971 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
2975 if (fp
->f_type
!= DTYPE_VNODE
) {
2984 *resultvp
= (struct vnode
*)fp
->f_data
;
2994 * Description: Get fileproc, vnode pointer, and vid for a given fd from the
2995 * per process open file table of the specified process, and if
2996 * successful, increment the f_iocount
2998 * Parameters: p Process in which fd lives
2999 * fd fd to get information for
3000 * resultfp Pointer to result fileproc
3001 * pointer area, or 0 if none
3002 * resultvp Pointer to result vnode pointer
3003 * area, or 0 if none
3004 * vidp Pointer to resuld vid area
3006 * Returns: 0 Success
3007 * EBADF Bad file descriptor
3008 * ENOTSUP fd does not refer to a vnode
3011 * *resultfp (modified) Fileproc pointer
3012 * *resultvp (modified) vnode pointer
3015 * Notes: The resultfp and resultvp fields are optional, and may be
3016 * independently specified as NULL to skip returning information
3018 * Locks: Internally takes and releases proc_fdlock
3021 fp_getfvpandvid(proc_t p
, int fd
, struct fileproc
**resultfp
,
3022 struct vnode
**resultvp
, uint32_t *vidp
)
3024 struct filedesc
*fdp
= p
->p_fd
;
3025 struct fileproc
*fp
;
3027 proc_fdlock_spin(p
);
3028 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3029 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3030 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3034 if (fp
->f_type
!= DTYPE_VNODE
) {
3043 *resultvp
= (struct vnode
*)fp
->f_data
;
3045 *vidp
= (uint32_t)vnode_vid((struct vnode
*)fp
->f_data
);
3055 * Description: Get fileproc and socket pointer for a given fd from the
3056 * per process open file table of the specified process, and if
3057 * successful, increment the f_iocount
3059 * Parameters: p Process in which fd lives
3060 * fd fd to get information for
3061 * resultfp Pointer to result fileproc
3062 * pointer area, or 0 if none
3063 * results Pointer to result socket
3064 * pointer area, or 0 if none
3066 * Returns: EBADF The file descriptor is invalid
3067 * EOPNOTSUPP The file descriptor is not a socket
3071 * *resultfp (modified) Fileproc pointer
3072 * *results (modified) socket pointer
3074 * Notes: EOPNOTSUPP should probably be ENOTSOCK; this function is only
3075 * ever called from accept1().
3078 fp_getfsock(proc_t p
, int fd
, struct fileproc
**resultfp
,
3079 struct socket
**results
)
3081 struct filedesc
*fdp
= p
->p_fd
;
3082 struct fileproc
*fp
;
3084 proc_fdlock_spin(p
);
3085 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3086 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3087 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3091 if (fp
->f_type
!= DTYPE_SOCKET
) {
3100 *results
= (struct socket
*)fp
->f_data
;
3110 * Description: Get fileproc and kqueue pointer for a given fd from the
3111 * per process open file table of the specified process, and if
3112 * successful, increment the f_iocount
3114 * Parameters: p Process in which fd lives
3115 * fd fd to get information for
3116 * resultfp Pointer to result fileproc
3117 * pointer area, or 0 if none
3118 * resultkq Pointer to result kqueue
3119 * pointer area, or 0 if none
3121 * Returns: EBADF The file descriptor is invalid
3122 * EBADF The file descriptor is not a socket
3126 * *resultfp (modified) Fileproc pointer
3127 * *resultkq (modified) kqueue pointer
3129 * Notes: The second EBADF should probably be something else to make
3130 * the error condition distinct.
3133 fp_getfkq(proc_t p
, int fd
, struct fileproc
**resultfp
,
3134 struct kqueue
**resultkq
)
3136 struct filedesc
*fdp
= p
->p_fd
;
3137 struct fileproc
*fp
;
3139 proc_fdlock_spin(p
);
3140 if ( fd
< 0 || fd
>= fdp
->fd_nfiles
||
3141 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3142 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3146 if (fp
->f_type
!= DTYPE_KQUEUE
) {
3155 *resultkq
= (struct kqueue
*)fp
->f_data
;
3165 * Description: Get fileproc and POSIX shared memory pointer for a given fd
3166 * from the per process open file table of the specified process
3167 * and if successful, increment the f_iocount
3169 * Parameters: p Process in which fd lives
3170 * fd fd to get information for
3171 * resultfp Pointer to result fileproc
3172 * pointer area, or 0 if none
3173 * resultpshm Pointer to result POSIX
3174 * shared memory pointer
3175 * pointer area, or 0 if none
3177 * Returns: EBADF The file descriptor is invalid
3178 * EBADF The file descriptor is not a POSIX
3179 * shared memory area
3183 * *resultfp (modified) Fileproc pointer
3184 * *resultpshm (modified) POSIX shared memory pointer
3186 * Notes: The second EBADF should probably be something else to make
3187 * the error condition distinct.
3190 fp_getfpshm(proc_t p
, int fd
, struct fileproc
**resultfp
,
3191 struct pshmnode
**resultpshm
)
3193 struct filedesc
*fdp
= p
->p_fd
;
3194 struct fileproc
*fp
;
3196 proc_fdlock_spin(p
);
3197 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3198 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3199 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3203 if (fp
->f_type
!= DTYPE_PSXSHM
) {
3213 *resultpshm
= (struct pshmnode
*)fp
->f_data
;
3223 * Description: Get fileproc and POSIX semaphore pointer for a given fd from
3224 * the per process open file table of the specified process
3225 * and if successful, increment the f_iocount
3227 * Parameters: p Process in which fd lives
3228 * fd fd to get information for
3229 * resultfp Pointer to result fileproc
3230 * pointer area, or 0 if none
3231 * resultpsem Pointer to result POSIX
3232 * semaphore pointer area, or
3235 * Returns: EBADF The file descriptor is invalid
3236 * EBADF The file descriptor is not a POSIX
3241 * *resultfp (modified) Fileproc pointer
3242 * *resultpsem (modified) POSIX semaphore pointer
3244 * Notes: The second EBADF should probably be something else to make
3245 * the error condition distinct.
3247 * In order to support unnamed POSIX semaphores, the named
3248 * POSIX semaphores will have to move out of the per-process
3249 * open filetable, and into a global table that is shared with
3250 * unnamed POSIX semaphores, since unnamed POSIX semaphores
3251 * are typically used by declaring instances in shared memory,
3252 * and there's no other way to do this without changing the
3253 * underlying type, which would introduce binary compatibility
3257 fp_getfpsem(proc_t p
, int fd
, struct fileproc
**resultfp
,
3258 struct psemnode
**resultpsem
)
3260 struct filedesc
*fdp
= p
->p_fd
;
3261 struct fileproc
*fp
;
3263 proc_fdlock_spin(p
);
3264 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3265 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3266 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3270 if (fp
->f_type
!= DTYPE_PSXSEM
) {
3279 *resultpsem
= (struct psemnode
*)fp
->f_data
;
3289 * Description: Get fileproc and pipe pointer for a given fd from the
3290 * per process open file table of the specified process
3291 * and if successful, increment the f_iocount
3293 * Parameters: p Process in which fd lives
3294 * fd fd to get information for
3295 * resultfp Pointer to result fileproc
3296 * pointer area, or 0 if none
3297 * resultpipe Pointer to result pipe
3298 * pointer area, or 0 if none
3300 * Returns: EBADF The file descriptor is invalid
3301 * EBADF The file descriptor is not a socket
3305 * *resultfp (modified) Fileproc pointer
3306 * *resultpipe (modified) pipe pointer
3308 * Notes: The second EBADF should probably be something else to make
3309 * the error condition distinct.
3312 fp_getfpipe(proc_t p
, int fd
, struct fileproc
**resultfp
,
3313 struct pipe
**resultpipe
)
3315 struct filedesc
*fdp
= p
->p_fd
;
3316 struct fileproc
*fp
;
3318 proc_fdlock_spin(p
);
3319 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3320 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3321 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3325 if (fp
->f_type
!= DTYPE_PIPE
) {
3334 *resultpipe
= (struct pipe
*)fp
->f_data
;
3341 #define DTYPE_ATALK -1 /* XXX This does not belong here */
3347 * Description: Get fileproc and atalk pointer for a given fd from the
3348 * per process open file table of the specified process
3349 * and if successful, increment the f_iocount
3351 * Parameters: p Process in which fd lives
3352 * fd fd to get information for
3353 * resultfp Pointer to result fileproc
3354 * pointer area, or 0 if none
3355 * resultatalk Pointer to result atalk
3356 * pointer area, or 0 if none
3357 * Returns: EBADF The file descriptor is invalid
3358 * EBADF The file descriptor is not a socket
3362 * *resultfp (modified) Fileproc pointer
3363 * *resultatalk (modified) atalk pointer
3365 * Notes: The second EBADF should probably be something else to make
3366 * the error condition distinct.
3368 * XXX This code is specific to AppleTalk protocol support, and
3369 * XXX should be conditionally compiled
3372 fp_getfatalk(proc_t p
, int fd
, struct fileproc
**resultfp
,
3373 struct atalk
**resultatalk
)
3375 struct filedesc
*fdp
= p
->p_fd
;
3376 struct fileproc
*fp
;
3378 proc_fdlock_spin(p
);
3379 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3380 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3381 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3385 if (fp
->f_type
!= (DTYPE_ATALK
+1)) {
3394 *resultatalk
= (struct atalk
*)fp
->f_data
;
3405 * Description: Get fileproc pointer for a given fd from the per process
3406 * open file table of the specified process and if successful,
3407 * increment the f_iocount
3409 * Parameters: p Process in which fd lives
3410 * fd fd to get information for
3411 * resultfp Pointer to result fileproc
3412 * pointer area, or 0 if none
3413 * locked !0 if the caller holds the
3414 * proc_fdlock, 0 otherwise
3416 * Returns: 0 Success
3417 * EBADF Bad file descriptor
3420 * *resultfp (modified) Fileproc pointer
3422 * Locks: If the argument 'locked' is non-zero, then the caller is
3423 * expected to have taken and held the proc_fdlock; if it is
3424 * zero, than this routine internally takes and drops this lock.
3427 fp_lookup(proc_t p
, int fd
, struct fileproc
**resultfp
, int locked
)
3429 struct filedesc
*fdp
= p
->p_fd
;
3430 struct fileproc
*fp
;
3433 proc_fdlock_spin(p
);
3434 if (fd
< 0 || fdp
== NULL
|| fd
>= fdp
->fd_nfiles
||
3435 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3436 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
3455 * Description: Set the FP_WRITTEN flag on the fileproc and drop the I/O
3456 * reference previously taken by calling fp_lookup et. al.
3458 * Parameters: p Process in which the fd lives
3459 * fd fd associated with the fileproc
3460 * fp fileproc on which to set the
3461 * flag and drop the reference
3463 * Returns: 0 Success
3464 * fp_drop:EBADF Bad file descriptor
3466 * Locks: This function internally takes and drops the proc_fdlock for
3467 * the supplied process
3469 * Notes: The fileproc must correspond to the fd in the supplied proc
3472 fp_drop_written(proc_t p
, int fd
, struct fileproc
*fp
)
3476 proc_fdlock_spin(p
);
3478 fp
->f_flags
|= FP_WRITTEN
;
3480 error
= fp_drop(p
, fd
, fp
, 1);
3491 * Description: Set the FP_WAITEVENT flag on the fileproc and drop the I/O
3492 * reference previously taken by calling fp_lookup et. al.
3494 * Parameters: p Process in which the fd lives
3495 * fd fd associated with the fileproc
3496 * fp fileproc on which to set the
3497 * flag and drop the reference
3499 * Returns: 0 Success
3500 * fp_drop:EBADF Bad file descriptor
3502 * Locks: This function internally takes and drops the proc_fdlock for
3503 * the supplied process
3505 * Notes: The fileproc must correspond to the fd in the supplied proc
3508 fp_drop_event(proc_t p
, int fd
, struct fileproc
*fp
)
3512 proc_fdlock_spin(p
);
3514 fp
->f_flags
|= FP_WAITEVENT
;
3516 error
= fp_drop(p
, fd
, fp
, 1);
3527 * Description: Drop the I/O reference previously taken by calling fp_lookup
3530 * Parameters: p Process in which the fd lives
3531 * fd fd associated with the fileproc
3532 * fp fileproc on which to set the
3533 * flag and drop the reference
3534 * locked flag to internally take and
3535 * drop proc_fdlock if it is not
3536 * already held by the caller
3538 * Returns: 0 Success
3539 * EBADF Bad file descriptor
3541 * Locks: This function internally takes and drops the proc_fdlock for
3542 * the supplied process if 'locked' is non-zero, and assumes that
3543 * the caller already holds this lock if 'locked' is non-zero.
3545 * Notes: The fileproc must correspond to the fd in the supplied proc
3548 fp_drop(proc_t p
, int fd
, struct fileproc
*fp
, int locked
)
3550 struct filedesc
*fdp
= p
->p_fd
;
3554 proc_fdlock_spin(p
);
3555 if ((fp
== FILEPROC_NULL
) && (fd
< 0 || fd
>= fdp
->fd_nfiles
||
3556 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
3557 ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
3558 !(fdp
->fd_ofileflags
[fd
] & UF_CLOSING
)))) {
3565 if (fp
->f_iocount
== 0) {
3566 if (fp
->f_flags
& FP_SELCONFLICT
)
3567 fp
->f_flags
&= ~FP_SELCONFLICT
;
3569 if (p
->p_fpdrainwait
) {
3570 p
->p_fpdrainwait
= 0;
3577 wakeup(&p
->p_fpdrainwait
);
3586 * Description: Given an fd, look it up in the current process's per process
3587 * open file table, and return its internal vnode pointer.
3589 * Parameters: fd fd to obtain vnode from
3590 * vpp pointer to vnode return area
3592 * Returns: 0 Success
3593 * EINVAL The fd does not refer to a
3594 * vnode fileproc entry
3595 * fp_lookup:EBADF Bad file descriptor
3598 * *vpp (modified) Returned vnode pointer
3600 * Locks: This function internally takes and drops the proc_fdlock for
3601 * the current process
3603 * Notes: If successful, this function increments the f_iocount on the
3604 * fd's corresponding fileproc.
3606 * The fileproc referenced is not returned; because of this, care
3607 * must be taken to not drop the last reference (e.g. by closing
3608 * the file). This is inherently unsafe, since the reference may
3609 * not be recoverable from the vnode, if there is a subsequent
3610 * close that destroys the associate fileproc. The caller should
3611 * therefore retain their own reference on the fileproc so that
3612 * the f_iocount can be dropped subsequently. Failure to do this
3613 * can result in the returned pointer immediately becoming invalid
3614 * following the call.
3616 * Use of this function is discouraged.
3619 file_vnode(int fd
, struct vnode
**vpp
)
3621 proc_t p
= current_proc();
3622 struct fileproc
*fp
;
3625 proc_fdlock_spin(p
);
3626 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
3630 if (fp
->f_type
!= DTYPE_VNODE
) {
3631 fp_drop(p
, fd
, fp
,1);
3636 *vpp
= (struct vnode
*)fp
->f_data
;
3644 * file_vnode_withvid
3646 * Description: Given an fd, look it up in the current process's per process
3647 * open file table, and return its internal vnode pointer.
3649 * Parameters: fd fd to obtain vnode from
3650 * vpp pointer to vnode return area
3651 * vidp pointer to vid of the returned vnode
3653 * Returns: 0 Success
3654 * EINVAL The fd does not refer to a
3655 * vnode fileproc entry
3656 * fp_lookup:EBADF Bad file descriptor
3659 * *vpp (modified) Returned vnode pointer
3661 * Locks: This function internally takes and drops the proc_fdlock for
3662 * the current process
3664 * Notes: If successful, this function increments the f_iocount on the
3665 * fd's corresponding fileproc.
3667 * The fileproc referenced is not returned; because of this, care
3668 * must be taken to not drop the last reference (e.g. by closing
3669 * the file). This is inherently unsafe, since the reference may
3670 * not be recoverable from the vnode, if there is a subsequent
3671 * close that destroys the associate fileproc. The caller should
3672 * therefore retain their own reference on the fileproc so that
3673 * the f_iocount can be dropped subsequently. Failure to do this
3674 * can result in the returned pointer immediately becoming invalid
3675 * following the call.
3677 * Use of this function is discouraged.
3680 file_vnode_withvid(int fd
, struct vnode
**vpp
, uint32_t * vidp
)
3682 proc_t p
= current_proc();
3683 struct fileproc
*fp
;
3687 proc_fdlock_spin(p
);
3688 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
3692 if (fp
->f_type
!= DTYPE_VNODE
) {
3693 fp_drop(p
, fd
, fp
,1);
3697 vp
= (struct vnode
*)fp
->f_data
;
3701 if ((vidp
!= NULL
) && (vp
!= NULLVP
))
3702 *vidp
= (uint32_t)vp
->v_id
;
3713 * Description: Given an fd, look it up in the current process's per process
3714 * open file table, and return its internal socket pointer.
3716 * Parameters: fd fd to obtain vnode from
3717 * sp pointer to socket return area
3719 * Returns: 0 Success
3720 * ENOTSOCK Not a socket
3721 * fp_lookup:EBADF Bad file descriptor
3724 * *sp (modified) Returned socket pointer
3726 * Locks: This function internally takes and drops the proc_fdlock for
3727 * the current process
3729 * Notes: If successful, this function increments the f_iocount on the
3730 * fd's corresponding fileproc.
3732 * The fileproc referenced is not returned; because of this, care
3733 * must be taken to not drop the last reference (e.g. by closing
3734 * the file). This is inherently unsafe, since the reference may
3735 * not be recoverable from the socket, if there is a subsequent
3736 * close that destroys the associate fileproc. The caller should
3737 * therefore retain their own reference on the fileproc so that
3738 * the f_iocount can be dropped subsequently. Failure to do this
3739 * can result in the returned pointer immediately becoming invalid
3740 * following the call.
3742 * Use of this function is discouraged.
3745 file_socket(int fd
, struct socket
**sp
)
3747 proc_t p
= current_proc();
3748 struct fileproc
*fp
;
3751 proc_fdlock_spin(p
);
3752 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
3756 if (fp
->f_type
!= DTYPE_SOCKET
) {
3757 fp_drop(p
, fd
, fp
,1);
3761 *sp
= (struct socket
*)fp
->f_data
;
3771 * Description: Given an fd, look it up in the current process's per process
3772 * open file table, and return its fileproc's flags field.
3774 * Parameters: fd fd whose flags are to be
3776 * flags pointer to flags data area
3778 * Returns: 0 Success
3779 * ENOTSOCK Not a socket
3780 * fp_lookup:EBADF Bad file descriptor
3783 * *flags (modified) Returned flags field
3785 * Locks: This function internally takes and drops the proc_fdlock for
3786 * the current process
3788 * Notes: This function will internally increment and decrement the
3789 * f_iocount of the fileproc as part of its operation.
3792 file_flags(int fd
, int *flags
)
3795 proc_t p
= current_proc();
3796 struct fileproc
*fp
;
3799 proc_fdlock_spin(p
);
3800 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
3804 *flags
= (int)fp
->f_flag
;
3805 fp_drop(p
, fd
, fp
,1);
3815 * Description: Drop an iocount reference on an fd, and wake up any waiters
3816 * for draining (i.e. blocked in fileproc_drain() called during
3817 * the last attempt to close a file).
3819 * Parameters: fd fd on which an ioreference is
3822 * Returns: 0 Success
3823 * EBADF Bad file descriptor
3825 * Description: Given an fd, look it up in the current process's per process
3826 * open file table, and drop it's fileproc's f_iocount by one
3828 * Notes: This is intended as a corresponding operation to the functions
3829 * file_vnode() and file_socket() operations.
3831 * Technically, the close reference is supposed to be protected
3832 * by a fileproc_drain(), however, a drain will only block if
3833 * the fd refers to a character device, and that device has had
3834 * preparefileread() called on it. If it refers to something
3835 * other than a character device, then the drain will occur and
3836 * block each close attempt, rather than merely the last close.
3838 * Since it's possible for an fd that refers to a character
3839 * device to have an intermediate close followed by an open to
3840 * cause a different file to correspond to that descriptor,
3841 * unless there was a cautionary reference taken on the fileproc,
3842 * this is an inherently unsafe function. This happens in the
3843 * case where multiple fd's in a process refer to the same
3844 * character device (e.g. stdin/out/err pointing to a tty, etc.).
3846 * Use of this function is discouraged.
3851 struct fileproc
*fp
;
3852 proc_t p
= current_proc();
3855 proc_fdlock_spin(p
);
3856 if (fd
< 0 || fd
>= p
->p_fd
->fd_nfiles
||
3857 (fp
= p
->p_fd
->fd_ofiles
[fd
]) == NULL
||
3858 ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
3859 !(p
->p_fd
->fd_ofileflags
[fd
] & UF_CLOSING
))) {
3865 if (fp
->f_iocount
== 0) {
3866 if (fp
->f_flags
& FP_SELCONFLICT
)
3867 fp
->f_flags
&= ~FP_SELCONFLICT
;
3869 if (p
->p_fpdrainwait
) {
3870 p
->p_fpdrainwait
= 0;
3877 wakeup(&p
->p_fpdrainwait
);
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
3895 * Returns: 0 Success
3896 * falloc:ENFILE Too many open files in system
3897 * falloc:EMFILE Too many open files in process
3898 * falloc:ENOMEM M_FILEPROC or M_FILEGLOB zone
3902 * *resultfd (modified) Returned fileproc pointer
3903 * *resultfd (modified) Returned fd
3905 * Locks: This function takes and drops the proc_fdlock; if this lock
3906 * is already held, use falloc_locked() instead.
3908 * Notes: This function takes separate process and context arguments
3909 * solely to support kern_exec.c; otherwise, it would take
3910 * neither, and expect falloc_locked() to use the
3911 * vfs_context_current() routine internally.
3914 falloc(proc_t p
, struct fileproc
**resultfp
, int *resultfd
, vfs_context_t ctx
)
3919 error
= falloc_locked(p
, resultfp
, resultfd
, ctx
, 1);
3929 * Create a new open file structure and allocate
3930 * a file descriptor for the process that refers to it.
3932 * Returns: 0 Success
3934 * Description: Allocate an entry in the per process open file table and
3935 * return the corresponding fileproc and fd.
3937 * Parameters: p The process in whose open file
3938 * table the fd is to be allocated
3939 * resultfp Pointer to fileproc pointer
3941 * resultfd Pointer to fd return area
3943 * locked Flag to indicate whether the
3944 * caller holds proc_fdlock
3946 * Returns: 0 Success
3947 * ENFILE Too many open files in system
3948 * fdalloc:EMFILE Too many open files in process
3949 * ENOMEM M_FILEPROC or M_FILEGLOB zone
3954 * *resultfd (modified) Returned fileproc pointer
3955 * *resultfd (modified) Returned fd
3957 * Locks: If the parameter 'locked' is zero, this function takes and
3958 * drops the proc_fdlock; if non-zero, the caller must hold the
3961 * Notes: If you intend to use a non-zero 'locked' parameter, use the
3962 * utility function falloc() instead.
3964 * This function takes separate process and context arguments
3965 * solely to support kern_exec.c; otherwise, it would take
3966 * neither, and use the vfs_context_current() routine internally.
3969 falloc_locked(proc_t p
, struct fileproc
**resultfp
, int *resultfd
,
3970 vfs_context_t ctx
, int locked
)
3972 struct fileproc
*fp
;
3973 struct fileglob
*fg
;
3978 if ( (error
= fdalloc(p
, 0, &nfd
)) ) {
3983 if (nfiles
>= maxfiles
) {
3990 error
= mac_file_check_create(proc_ucred(p
));
3999 * Allocate a new file descriptor.
4000 * If the process has file descriptor zero open, add to the list
4001 * of open files at that point, otherwise put it at the front of
4002 * the list of open files.
4006 MALLOC_ZONE(fp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
4012 MALLOC_ZONE(fg
, struct fileglob
*, sizeof(struct fileglob
), M_FILEGLOB
, M_WAITOK
);
4014 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
4019 bzero(fp
, sizeof(struct fileproc
));
4020 bzero(fg
, sizeof(struct fileglob
));
4021 lck_mtx_init(&fg
->fg_lock
, file_lck_grp
, file_lck_attr
);
4027 mac_file_label_init(fg
);
4030 kauth_cred_ref(ctx
->vc_ucred
);
4034 fp
->f_cred
= ctx
->vc_ucred
;
4037 mac_file_label_associate(fp
->f_cred
, fg
);
4040 OSAddAtomic(1, &nfiles
);
4042 p
->p_fd
->fd_ofiles
[nfd
] = fp
;
4059 * Description: Free a file structure; drop the global open file count, and
4060 * drop the credential reference, if the fileglob has one, and
4061 * destroy the instance mutex before freeing
4063 * Parameters: fg Pointer to fileglob to be
4069 fg_free(struct fileglob
*fg
)
4071 OSAddAtomic(-1, &nfiles
);
4073 if (IS_VALID_CRED(fg
->fg_cred
)) {
4074 kauth_cred_unref(&fg
->fg_cred
);
4076 lck_mtx_destroy(&fg
->fg_lock
, file_lck_grp
);
4079 mac_file_label_destroy(fg
);
4081 FREE_ZONE(fg
, sizeof *fg
, M_FILEGLOB
);
4088 * Description: Perform close-on-exec processing for all files in a process
4089 * that are either marked as close-on-exec, or which were in the
4090 * process of being opened at the time of the execve
4092 * Also handles the case (via posix_spawn()) where -all-
4093 * files except those marked with "inherit" as treated as
4096 * Parameters: p Pointer to process calling
4101 * Locks: This function internally takes and drops proc_fdlock()
4103 * Notes: This function drops and retakes the kernel funnel; this is
4104 * inherently unsafe, since another thread may have the
4107 * XXX: We should likely reverse the lock and funnel drop/acquire
4108 * order to avoid the small race window; it's also possible that
4109 * if the program doing the exec has an outstanding listen socket
4110 * and a network connection is completed asynchronously that we
4111 * will end up with a "ghost" socket reference in the new process.
4113 * This needs reworking to make it safe to remove the funnel from
4114 * the execve and posix_spawn system calls.
4117 fdexec(proc_t p
, short flags
)
4119 struct filedesc
*fdp
= p
->p_fd
;
4121 boolean_t cloexec_default
= (flags
& POSIX_SPAWN_CLOEXEC_DEFAULT
) != 0;
4124 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
4126 struct fileproc
*fp
= fdp
->fd_ofiles
[i
];
4127 char *flagp
= &fdp
->fd_ofileflags
[i
];
4129 if (fp
&& cloexec_default
) {
4131 * Reverse the usual semantics of file descriptor
4132 * inheritance - all of them should be closed
4133 * except files marked explicitly as "inherit" and
4134 * not marked close-on-exec.
4136 if ((*flagp
& (UF_EXCLOSE
|UF_INHERIT
)) != UF_INHERIT
)
4137 *flagp
|= UF_EXCLOSE
;
4138 *flagp
&= ~UF_INHERIT
;
4142 ((*flagp
& (UF_RESERVED
|UF_EXCLOSE
)) == UF_EXCLOSE
)
4144 || (fp
&& mac_file_check_inherit(proc_ucred(p
), fp
->f_fglob
))
4147 if (i
< fdp
->fd_knlistsize
)
4148 knote_fdclose(p
, i
);
4149 procfdtbl_clearfd(p
, i
);
4150 if (i
== fdp
->fd_lastfile
&& i
> 0)
4152 if (i
< fdp
->fd_freefile
)
4153 fdp
->fd_freefile
= i
;
4156 * Wait for any third party viewers (e.g., lsof)
4157 * to release their references to this fileproc.
4159 while (fp
->f_iocount
> 0) {
4160 p
->p_fpdrainwait
= 1;
4161 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
,
4165 closef_locked(fp
, fp
->f_fglob
, p
);
4167 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
4177 * Description: Copy a filedesc structure. This is normally used as part of
4178 * forkproc() when forking a new process, to copy the per process
4179 * open file table over to the new process.
4181 * Parameters: p Process whose open file table
4182 * is to be copied (parent)
4183 * uth_cdir Per thread current working
4184 * cirectory, or NULL
4186 * Returns: NULL Copy failed
4187 * !NULL Pointer to new struct filedesc
4189 * Locks: This function internally takes and drops proc_fdlock()
4191 * Notes: Files are copied directly, ignoring the new resource limits
4192 * for the process that's being copied into. Since the descriptor
4193 * references are just additional references, this does not count
4194 * against the number of open files on the system.
4196 * The struct filedesc includes the current working directory,
4197 * and the current root directory, if the process is chroot'ed.
4199 * If the exec was called by a thread using a per thread current
4200 * working directory, we inherit the working directory from the
4201 * thread making the call, rather than from the process.
4203 * In the case of a failure to obtain a reference, for most cases,
4204 * the file entry will be silently dropped. There's an exception
4205 * for the case of a chroot dir, since a failure to to obtain a
4206 * reference there would constitute an "escape" from the chroot
4207 * environment, which must not be allowed. In that case, we will
4208 * deny the execve() operation, rather than allowing the escape.
4211 fdcopy(proc_t p
, vnode_t uth_cdir
)
4213 struct filedesc
*newfdp
, *fdp
= p
->p_fd
;
4215 struct fileproc
*ofp
, *fp
;
4218 MALLOC_ZONE(newfdp
, struct filedesc
*,
4219 sizeof(*newfdp
), M_FILEDESC
, M_WAITOK
);
4226 * the FD_CHROOT flag will be inherited via this copy
4228 (void) memcpy(newfdp
, fdp
, sizeof(*newfdp
));
4231 * If we are running with per-thread current working directories,
4232 * inherit the new current working directory from the current thread
4233 * instead, before we take our references.
4235 if (uth_cdir
!= NULLVP
)
4236 newfdp
->fd_cdir
= uth_cdir
;
4239 * For both fd_cdir and fd_rdir make sure we get
4240 * a valid reference... if we can't, than set
4241 * set the pointer(s) to NULL in the child... this
4242 * will keep us from using a non-referenced vp
4243 * and allows us to do the vnode_rele only on
4244 * a properly referenced vp
4246 if ( (v_dir
= newfdp
->fd_cdir
) ) {
4247 if (vnode_getwithref(v_dir
) == 0) {
4248 if ( (vnode_ref(v_dir
)) )
4249 newfdp
->fd_cdir
= NULL
;
4252 newfdp
->fd_cdir
= NULL
;
4254 if (newfdp
->fd_cdir
== NULL
&& fdp
->fd_cdir
) {
4256 * we couldn't get a new reference on
4257 * the current working directory being
4258 * inherited... we might as well drop
4259 * our reference from the parent also
4260 * since the vnode has gone DEAD making
4261 * it useless... by dropping it we'll
4262 * be that much closer to recycling it
4264 vnode_rele(fdp
->fd_cdir
);
4265 fdp
->fd_cdir
= NULL
;
4268 if ( (v_dir
= newfdp
->fd_rdir
) ) {
4269 if (vnode_getwithref(v_dir
) == 0) {
4270 if ( (vnode_ref(v_dir
)) )
4271 newfdp
->fd_rdir
= NULL
;
4274 newfdp
->fd_rdir
= NULL
;
4277 /* Coming from a chroot environment and unable to get a reference... */
4278 if (newfdp
->fd_rdir
== NULL
&& fdp
->fd_rdir
) {
4280 * We couldn't get a new reference on
4281 * the chroot directory being
4282 * inherited... this is fatal, since
4283 * otherwise it would constitute an
4284 * escape from a chroot environment by
4287 if (newfdp
->fd_cdir
)
4288 vnode_rele(newfdp
->fd_cdir
);
4289 FREE_ZONE(newfdp
, sizeof *newfdp
, M_FILEDESC
);
4292 newfdp
->fd_refcnt
= 1;
4295 * If the number of open files fits in the internal arrays
4296 * of the open file structure, use them, otherwise allocate
4297 * additional memory for the number of descriptors currently
4300 if (newfdp
->fd_lastfile
< NDFILE
)
4304 * Compute the smallest multiple of NDEXTENT needed
4305 * for the file descriptors currently in use,
4306 * allowing the table to shrink.
4308 i
= newfdp
->fd_nfiles
;
4309 while (i
> 2 * NDEXTENT
&& i
> newfdp
->fd_lastfile
* 2)
4314 MALLOC_ZONE(newfdp
->fd_ofiles
, struct fileproc
**,
4315 i
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
4316 if (newfdp
->fd_ofiles
== NULL
) {
4317 if (newfdp
->fd_cdir
)
4318 vnode_rele(newfdp
->fd_cdir
);
4319 if (newfdp
->fd_rdir
)
4320 vnode_rele(newfdp
->fd_rdir
);
4322 FREE_ZONE(newfdp
, sizeof(*newfdp
), M_FILEDESC
);
4325 (void) memset(newfdp
->fd_ofiles
, 0, i
* OFILESIZE
);
4328 newfdp
->fd_ofileflags
= (char *) &newfdp
->fd_ofiles
[i
];
4329 newfdp
->fd_nfiles
= i
;
4331 if (fdp
->fd_nfiles
> 0) {
4332 struct fileproc
**fpp
;
4335 (void) memcpy(newfdp
->fd_ofiles
, fdp
->fd_ofiles
,
4336 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofiles
));
4337 (void) memcpy(newfdp
->fd_ofileflags
, fdp
->fd_ofileflags
,
4338 (newfdp
->fd_lastfile
+ 1) * sizeof(*fdp
->fd_ofileflags
));
4341 * kq descriptors cannot be copied.
4343 if (newfdp
->fd_knlistsize
!= -1) {
4344 fpp
= &newfdp
->fd_ofiles
[newfdp
->fd_lastfile
];
4345 for (i
= newfdp
->fd_lastfile
; i
>= 0; i
--, fpp
--) {
4346 if (*fpp
!= NULL
&& (*fpp
)->f_type
== DTYPE_KQUEUE
) {
4348 newfdp
->fd_ofileflags
[i
] = 0;
4349 if (i
< newfdp
->fd_freefile
)
4350 newfdp
->fd_freefile
= i
;
4352 if (*fpp
== NULL
&& i
== newfdp
->fd_lastfile
&& i
> 0)
4353 newfdp
->fd_lastfile
--;
4355 newfdp
->fd_knlist
= NULL
;
4356 newfdp
->fd_knlistsize
= -1;
4357 newfdp
->fd_knhash
= NULL
;
4358 newfdp
->fd_knhashmask
= 0;
4360 fpp
= newfdp
->fd_ofiles
;
4361 flags
= newfdp
->fd_ofileflags
;
4363 for (i
= newfdp
->fd_lastfile
+ 1; --i
>= 0; fpp
++, flags
++)
4364 if ((ofp
= *fpp
) != NULL
&& !(*flags
& UF_RESERVED
)) {
4365 MALLOC_ZONE(fp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
4368 * XXX no room to copy, unable to
4369 * XXX safely unwind state at present
4373 bzero(fp
, sizeof(struct fileproc
));
4374 fp
->f_flags
= ofp
->f_flags
;
4375 //fp->f_iocount = ofp->f_iocount;
4377 fp
->f_fglob
= ofp
->f_fglob
;
4382 if (i
< newfdp
->fd_freefile
)
4383 newfdp
->fd_freefile
= i
;
4397 * Description: Release a filedesc (per process open file table) structure;
4398 * this is done on process exit(), or from forkproc_free() if
4399 * the fork fails for some reason subsequent to a successful
4402 * Parameters: p Pointer to process going away
4406 * Locks: This function internally takes and drops proc_fdlock()
4411 struct filedesc
*fdp
;
4412 struct fileproc
*fp
;
4417 /* Certain daemons might not have file descriptors */
4420 if ((fdp
== NULL
) || (--fdp
->fd_refcnt
> 0)) {
4424 if (fdp
->fd_refcnt
== 0xffff)
4425 panic("fdfree: bad fd_refcnt");
4427 /* Last reference: the structure can't change out from under us */
4429 if (fdp
->fd_nfiles
> 0 && fdp
->fd_ofiles
) {
4430 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
4431 if ((fp
= fdp
->fd_ofiles
[i
]) != NULL
) {
4433 if (fdp
->fd_ofileflags
[i
] & UF_RESERVED
)
4434 panic("fdfree: found fp with UF_RESERVED");
4436 /* closef drops the iocount ... */
4437 if ((fp
->f_flags
& FP_INCHRREAD
) != 0)
4439 procfdtbl_reservefd(p
, i
);
4441 if (i
< fdp
->fd_knlistsize
)
4442 knote_fdclose(p
, i
);
4443 if (fp
->f_flags
& FP_WAITEVENT
)
4444 (void)waitevent_close(p
, fp
);
4445 (void) closef_locked(fp
, fp
->f_fglob
, p
);
4446 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
4449 FREE_ZONE(fdp
->fd_ofiles
, fdp
->fd_nfiles
* OFILESIZE
, M_OFILETABL
);
4450 fdp
->fd_ofiles
= NULL
;
4457 vnode_rele(fdp
->fd_cdir
);
4459 vnode_rele(fdp
->fd_rdir
);
4461 proc_fdlock_spin(p
);
4466 FREE(fdp
->fd_knlist
, M_KQUEUE
);
4468 FREE(fdp
->fd_knhash
, M_KQUEUE
);
4470 FREE_ZONE(fdp
, sizeof(*fdp
), M_FILEDESC
);
4477 * Description: Called on last open instance for a fileglob for a file being
4480 * Parameters: fp Pointer to fileproc for fd
4481 * fg Pointer to fileglob for fd
4482 * p Pointer to proc structure
4484 * Returns: 0 Success
4485 * <fo_close>:??? Anything returnable by a per-fileops
4488 * Note: fp can only be non-NULL if p is also non-NULL. If p is NULL,
4489 * then fg must eith be locked (FHASLOCK) or must not have a
4490 * type of DTYPE_VNODE.
4492 * On return, the fg is freed.
4494 * This function may block draining output to a character
4495 * device on last close of that device.
4498 closef_finish(struct fileproc
*fp
, struct fileglob
*fg
, proc_t p
, vfs_context_t ctx
)
4503 /* fg_ops completed initialization? */
4505 error
= fo_close(fg
, ctx
);
4509 /* if fp is non-NULL, drain it out */
4510 if (((fp
!= (struct fileproc
*)0) && ((fp
->f_flags
& FP_INCHRREAD
) != 0))) {
4511 proc_fdlock_spin(p
);
4512 if ( ((fp
->f_flags
& FP_INCHRREAD
) != 0) ) {
4513 fileproc_drain(p
, fp
);
4525 * Description: Internal form of closef; called with proc_fdlock held
4527 * Parameters: fp Pointer to fileproc for fd
4528 * fg Pointer to fileglob for fd
4529 * p Pointer to proc structure
4531 * Returns: 0 Success
4532 * closef_finish:??? Anything returnable by a per-fileops
4535 * Note: Decrements reference count on file structure; if this was the
4536 * last reference, then closef_finish() is called
4538 * p and fp are allowed to be NULL when closing a file that was
4539 * being passed in a message (but only if we are called when this
4540 * is NOT the last reference).
4543 closef_locked(struct fileproc
*fp
, struct fileglob
*fg
, proc_t p
)
4547 struct vfs_context context
;
4554 /* Set up context with cred stashed in fg */
4555 if (p
== current_proc())
4556 context
.vc_thread
= current_thread();
4558 context
.vc_thread
= NULL
;
4559 context
.vc_ucred
= fg
->fg_cred
;
4562 * POSIX record locking dictates that any close releases ALL
4563 * locks owned by this process. This is handled by setting
4564 * a flag in the unlock to free ONLY locks obeying POSIX
4565 * semantics, and not to free BSD-style file locks.
4566 * If the descriptor was in a message, POSIX-style locks
4567 * aren't passed with the descriptor.
4569 if (p
&& (p
->p_ladvflag
& P_LADVLOCK
) && fg
->fg_type
== DTYPE_VNODE
) {
4572 lf
.l_whence
= SEEK_SET
;
4575 lf
.l_type
= F_UNLCK
;
4576 vp
= (struct vnode
*)fg
->fg_data
;
4578 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
4579 (void) VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &lf
, F_POSIX
, &context
);
4580 (void)vnode_put(vp
);
4584 lck_mtx_lock_spin(&fg
->fg_lock
);
4587 if (fg
->fg_count
> 0) {
4588 lck_mtx_unlock(&fg
->fg_lock
);
4592 if (fg
->fg_count
!= 0)
4593 panic("fg %p: being freed with bad fg_count (%d)", fg
, fg
->fg_count
);
4596 if (fp
&& (fp
->f_flags
& FP_WRITTEN
))
4597 fg
->fg_flag
|= FWASWRITTEN
;
4599 fg
->fg_lflags
|= FG_TERM
;
4600 lck_mtx_unlock(&fg
->fg_lock
);
4604 error
= closef_finish(fp
, fg
, p
, &context
);
4615 * Description: Drain out pending I/O operations
4617 * Parameters: p Process closing this file
4618 * fp fileproc struct for the open
4619 * instance on the file
4623 * Locks: Assumes the caller holds the proc_fdlock
4625 * Notes: For character devices, this occurs on the last close of the
4626 * device; for all other file descriptors, this occurs on each
4627 * close to prevent fd's from being closed out from under
4628 * operations currently in progress and blocked
4630 * See Also: file_vnode(), file_socket(), file_drop(), and the cautions
4631 * regarding their use and interaction with this function.
4634 fileproc_drain(proc_t p
, struct fileproc
* fp
)
4636 struct vfs_context context
;
4638 context
.vc_thread
= proc_thread(p
); /* XXX */
4639 context
.vc_ucred
= fp
->f_fglob
->fg_cred
;
4641 fp
->f_iocount
-- ; /* (the one the close holds) */
4643 while (fp
->f_iocount
) {
4645 lck_mtx_convert_spin(&p
->p_fdmlock
);
4647 if (fp
->f_fglob
->fg_ops
->fo_drain
) {
4648 (*fp
->f_fglob
->fg_ops
->fo_drain
)(fp
, &context
);
4650 if ((fp
->f_flags
& FP_INSELECT
) == FP_INSELECT
) {
4651 if (wait_queue_wakeup_all((wait_queue_t
)fp
->f_waddr
, NULL
, THREAD_INTERRUPTED
) == KERN_INVALID_ARGUMENT
)
4652 panic("bad wait queue for wait_queue_wakeup_all %p", fp
->f_waddr
);
4654 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
) {
4655 if (wait_queue_wakeup_all(&select_conflict_queue
, NULL
, THREAD_INTERRUPTED
) == KERN_INVALID_ARGUMENT
)
4656 panic("bad select_conflict_queue");
4658 p
->p_fpdrainwait
= 1;
4660 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
, "fpdrain", NULL
);
4664 if ((fp
->f_flags
& FP_INSELECT
) != 0)
4665 panic("FP_INSELECT set on drained fp");
4667 if ((fp
->f_flags
& FP_SELCONFLICT
) == FP_SELCONFLICT
)
4668 fp
->f_flags
&= ~FP_SELCONFLICT
;
4675 * Description: Release the fd and free the fileproc associated with the fd
4676 * in the per process open file table of the specified process;
4677 * these values must correspond.
4679 * Parameters: p Process containing fd
4680 * fd fd to be released
4681 * fp fileproc to be freed
4683 * Returns: 0 Success
4685 * Notes: XXX function should be void - no one interprets the returns
4689 fp_free(proc_t p
, int fd
, struct fileproc
* fp
)
4691 proc_fdlock_spin(p
);
4695 fg_free(fp
->f_fglob
);
4696 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
4704 * Description: Apply an advisory lock on a file descriptor.
4706 * Parameters: p Process making request
4707 * uap->fd fd on which the lock is to be
4709 * uap->how (Un)Lock bits, including type
4710 * retval Pointer to the call return area
4712 * Returns: 0 Success
4713 * fp_getfvp:EBADF Bad file descriptor
4714 * fp_getfvp:ENOTSUP fd does not refer to a vnode
4715 * vnode_getwithref:???
4719 * *retval (modified) Size of dtable
4721 * Notes: Just attempt to get a record lock of the requested type on
4722 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
4725 flock(proc_t p
, struct flock_args
*uap
, __unused
int32_t *retval
)
4729 struct fileproc
*fp
;
4732 vfs_context_t ctx
= vfs_context_current();
4735 AUDIT_ARG(fd
, uap
->fd
);
4736 if ( (error
= fp_getfvp(p
, fd
, &fp
, &vp
)) ) {
4739 if ( (error
= vnode_getwithref(vp
)) ) {
4742 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
4744 lf
.l_whence
= SEEK_SET
;
4747 if (how
& LOCK_UN
) {
4748 lf
.l_type
= F_UNLCK
;
4749 fp
->f_flag
&= ~FHASLOCK
;
4750 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_UNLCK
, &lf
, F_FLOCK
, ctx
);
4754 lf
.l_type
= F_WRLCK
;
4755 else if (how
& LOCK_SH
)
4756 lf
.l_type
= F_RDLCK
;
4762 error
= mac_file_check_lock(proc_ucred(p
), fp
->f_fglob
, F_SETLK
, &lf
);
4766 fp
->f_flag
|= FHASLOCK
;
4767 if (how
& LOCK_NB
) {
4768 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, F_FLOCK
, ctx
);
4771 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, F_FLOCK
|F_WAIT
, ctx
);
4773 (void)vnode_put(vp
);
4775 fp_drop(p
, fd
, fp
, 0);
4783 * Description: Obtain a Mach send right for a given file descriptor.
4785 * Parameters: p Process calling fileport
4786 * uap->fd The fd to reference
4787 * uap->portnamep User address at which to place port name.
4789 * Returns: 0 Success.
4790 * EBADF Bad file descriptor.
4791 * EINVAL File descriptor had type that cannot be sent, misc. other errors.
4792 * EFAULT Address at which to store port name is not valid.
4793 * EAGAIN Resource shortage.
4796 * On success, name of send right is stored at user-specified address.
4799 fileport_makeport(proc_t p
, struct fileport_makeport_args
*uap
,
4800 __unused
int *retval
)
4804 user_addr_t user_portaddr
= uap
->portnamep
;
4805 struct fileproc
*fp
= FILEPROC_NULL
;
4806 struct fileglob
*fg
= NULL
;
4807 ipc_port_t fileport
;
4808 mach_port_name_t name
= MACH_PORT_NULL
;
4810 err
= fp_lookup(p
, fd
, &fp
, 0);
4815 if (!filetype_issendable(fp
->f_type
)) {
4820 /* Dropped when port is deallocated */
4824 /* Allocate and initialize a port */
4825 fileport
= fileport_alloc(fg
);
4826 if (fileport
== IPC_PORT_NULL
) {
4832 /* Add an entry. Deallocates port on failure. */
4833 name
= ipc_port_copyout_send(fileport
, get_task_ipcspace(p
->task
));
4834 if (!MACH_PORT_VALID(name
)) {
4839 err
= copyout(&name
, user_portaddr
, sizeof(mach_port_name_t
));
4844 /* Tag the fileglob for debugging purposes */
4845 lck_mtx_lock_spin(&fg
->fg_lock
);
4846 fg
->fg_lflags
|= FG_PORTMADE
;
4847 lck_mtx_unlock(&fg
->fg_lock
);
4849 fp_drop(p
, fd
, fp
, 0);
4854 if (MACH_PORT_VALID(name
)) {
4855 /* Don't care if another thread races us to deallocate the entry */
4856 (void) mach_port_deallocate(get_task_ipcspace(p
->task
), name
);
4859 if (fp
!= FILEPROC_NULL
) {
4860 fp_drop(p
, fd
, fp
, 0);
4867 fileport_releasefg(struct fileglob
*fg
)
4869 (void)closef_locked(NULL
, fg
, PROC_NULL
);
4878 * Description: Obtain the file descriptor for a given Mach send right.
4880 * Parameters: p Process calling fileport
4881 * uap->port Name of send right to file port.
4883 * Returns: 0 Success
4884 * EINVAL Invalid Mach port name, or port is not for a file.
4886 * fdalloc:ENOMEM Unable to allocate fileproc or extend file table.
4889 * *retval (modified) The new descriptor
4892 fileport_makefd(proc_t p
, struct fileport_makefd_args
*uap
, int32_t *retval
)
4894 struct fileglob
*fg
;
4895 struct fileproc
*fp
= FILEPROC_NULL
;
4896 ipc_port_t port
= IPC_PORT_NULL
;
4897 mach_port_name_t send
= uap
->port
;
4902 res
= ipc_object_copyin(get_task_ipcspace(p
->task
),
4903 send
, MACH_MSG_TYPE_COPY_SEND
, &port
);
4905 if (res
!= KERN_SUCCESS
) {
4910 fg
= fileport_port_to_fileglob(port
);
4916 MALLOC_ZONE(fp
, struct fileproc
*, sizeof(*fp
), M_FILEPROC
, M_WAITOK
);
4917 if (fp
== FILEPROC_NULL
) {
4922 bzero(fp
, sizeof(*fp
));
4928 err
= fdalloc(p
, 0, &fd
);
4933 *fdflags(p
, fd
) |= UF_EXCLOSE
;
4935 procfdtbl_releasefd(p
, fd
, fp
);
4941 if ((fp
!= NULL
) && (0 != err
)) {
4942 FREE_ZONE(fp
, sizeof(*fp
), M_FILEPROC
);
4945 if (IPC_PORT_NULL
!= port
) {
4946 ipc_port_release_send(port
);
4956 * Description: Duplicate the specified descriptor to a free descriptor;
4957 * this is the second half of fdopen(), above.
4959 * Parameters: fdp filedesc pointer to fill in
4961 * dfd fd to dup from
4962 * mode mode to set on new fd
4963 * error command code
4965 * Returns: 0 Success
4966 * EBADF Source fd is bad
4967 * EACCES Requested mode not allowed
4968 * !0 'error', if not ENODEV or
4971 * Notes: XXX This is not thread safe; see fdopen() above
4974 dupfdopen(struct filedesc
*fdp
, int indx
, int dfd
, int flags
, int error
)
4976 struct fileproc
*wfp
;
4977 struct fileproc
*fp
;
4981 proc_t p
= current_proc();
4984 * If the to-be-dup'd fd number is greater than the allowed number
4985 * of file descriptors, or the fd to be dup'd has already been
4986 * closed, reject. Note, check for new == old is necessary as
4987 * falloc could allocate an already closed to-be-dup'd descriptor
4988 * as the new descriptor.
4992 fp
= fdp
->fd_ofiles
[indx
];
4993 if (dfd
< 0 || dfd
>= fdp
->fd_nfiles
||
4994 (wfp
= fdp
->fd_ofiles
[dfd
]) == NULL
|| wfp
== fp
||
4995 (fdp
->fd_ofileflags
[dfd
] & UF_RESERVED
)) {
5001 myerror
= mac_file_check_dup(proc_ucred(p
), wfp
->f_fglob
, dfd
);
5008 * There are two cases of interest here.
5010 * For ENODEV simply dup (dfd) to file descriptor
5011 * (indx) and return.
5013 * For ENXIO steal away the file structure from (dfd) and
5014 * store it in (indx). (dfd) is effectively closed by
5017 * Any other error code is just returned.
5022 * Check that the mode the file is being opened for is a
5023 * subset of the mode of the existing descriptor.
5025 if (((flags
& (FREAD
|FWRITE
)) | wfp
->f_flag
) != wfp
->f_flag
) {
5029 if (indx
> fdp
->fd_lastfile
)
5030 fdp
->fd_lastfile
= indx
;
5034 fg_free(fp
->f_fglob
);
5035 fp
->f_fglob
= wfp
->f_fglob
;
5037 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
] |
5038 (flags
& O_CLOEXEC
) ? UF_EXCLOSE
: 0;
5054 * Description: Add a reference to a fileglob by fileproc
5056 * Parameters: fp fileproc containing fileglob
5061 * Notes: XXX Should use OSAddAtomic?
5064 fg_ref(struct fileproc
* fp
)
5066 struct fileglob
*fg
;
5070 lck_mtx_lock_spin(&fg
->fg_lock
);
5073 if ((fp
->f_flags
& ~((unsigned int)FP_VALID_FLAGS
)) != 0)
5074 panic("fg_ref: invalid bits on fp %p", fp
);
5076 if (fg
->fg_count
== 0)
5077 panic("fg_ref: adding fgcount to zeroed fg: fp %p fg %p",
5081 lck_mtx_unlock(&fg
->fg_lock
);
5088 * Description: Remove a reference to a fileglob by fileproc
5090 * Parameters: fp fileproc containing fileglob
5095 * Notes: XXX Should use OSAddAtomic?
5098 fg_drop(struct fileproc
* fp
)
5100 struct fileglob
*fg
;
5103 lck_mtx_lock_spin(&fg
->fg_lock
);
5105 lck_mtx_unlock(&fg
->fg_lock
);
5112 * Description: Insert fileglob onto message queue
5114 * Parameters: fg Fileglob pointer to insert
5118 * Locks: Takes and drops fg_lock, potentially many times
5121 fg_insertuipc(struct fileglob
* fg
)
5125 lck_mtx_lock_spin(&fg
->fg_lock
);
5127 while (fg
->fg_lflags
& FG_RMMSGQ
) {
5128 lck_mtx_convert_spin(&fg
->fg_lock
);
5130 fg
->fg_lflags
|= FG_WRMMSGQ
;
5131 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_insertuipc", NULL
);
5136 if (fg
->fg_msgcount
== 1) {
5137 fg
->fg_lflags
|= FG_INSMSGQ
;
5140 lck_mtx_unlock(&fg
->fg_lock
);
5143 lck_mtx_lock_spin(uipc_lock
);
5145 LIST_INSERT_HEAD(&fmsghead
, fg
, f_msglist
);
5146 lck_mtx_unlock(uipc_lock
);
5147 lck_mtx_lock(&fg
->fg_lock
);
5148 fg
->fg_lflags
&= ~FG_INSMSGQ
;
5149 if (fg
->fg_lflags
& FG_WINSMSGQ
) {
5150 fg
->fg_lflags
&= ~FG_WINSMSGQ
;
5151 wakeup(&fg
->fg_lflags
);
5153 lck_mtx_unlock(&fg
->fg_lock
);
5162 * Description: Remove fileglob from message queue
5164 * Parameters: fg Fileglob pointer to remove
5168 * Locks: Takes and drops fg_lock, potentially many times
5171 fg_removeuipc(struct fileglob
* fg
)
5175 lck_mtx_lock_spin(&fg
->fg_lock
);
5176 while (fg
->fg_lflags
& FG_INSMSGQ
) {
5177 lck_mtx_convert_spin(&fg
->fg_lock
);
5179 fg
->fg_lflags
|= FG_WINSMSGQ
;
5180 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_removeuipc", NULL
);
5183 if (fg
->fg_msgcount
== 0) {
5184 fg
->fg_lflags
|= FG_RMMSGQ
;
5187 lck_mtx_unlock(&fg
->fg_lock
);
5190 lck_mtx_lock_spin(uipc_lock
);
5192 LIST_REMOVE(fg
, f_msglist
);
5193 lck_mtx_unlock(uipc_lock
);
5194 lck_mtx_lock(&fg
->fg_lock
);
5195 fg
->fg_lflags
&= ~FG_RMMSGQ
;
5196 if (fg
->fg_lflags
& FG_WRMMSGQ
) {
5197 fg
->fg_lflags
&= ~FG_WRMMSGQ
;
5198 wakeup(&fg
->fg_lflags
);
5200 lck_mtx_unlock(&fg
->fg_lock
);
5208 * Description: Generic fileops read indirected through the fileops pointer
5209 * in the fileproc structure
5211 * Parameters: fp fileproc structure pointer
5212 * uio user I/O structure pointer
5214 * ctx VFS context for operation
5216 * Returns: 0 Success
5217 * !0 Errno from read
5220 fo_read(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
5222 return ((*fp
->f_ops
->fo_read
)(fp
, uio
, flags
, ctx
));
5229 * Description: Generic fileops write indirected through the fileops pointer
5230 * in the fileproc structure
5232 * Parameters: fp fileproc structure pointer
5233 * uio user I/O structure pointer
5235 * ctx VFS context for operation
5237 * Returns: 0 Success
5238 * !0 Errno from write
5241 fo_write(struct fileproc
*fp
, struct uio
*uio
, int flags
, vfs_context_t ctx
)
5243 return((*fp
->f_ops
->fo_write
)(fp
, uio
, flags
, ctx
));
5250 * Description: Generic fileops ioctl indirected through the fileops pointer
5251 * in the fileproc structure
5253 * Parameters: fp fileproc structure pointer
5255 * data pointer to internalized copy
5256 * of user space ioctl command
5257 * parameter data in kernel space
5258 * ctx VFS context for operation
5260 * Returns: 0 Success
5261 * !0 Errno from ioctl
5263 * Locks: The caller is assumed to have held the proc_fdlock; this
5264 * function releases and reacquires this lock. If the caller
5265 * accesses data protected by this lock prior to calling this
5266 * function, it will need to revalidate/reacquire any cached
5267 * protected data obtained prior to the call.
5270 fo_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
, vfs_context_t ctx
)
5274 proc_fdunlock(vfs_context_proc(ctx
));
5275 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, com
, data
, ctx
);
5276 proc_fdlock(vfs_context_proc(ctx
));
5284 * Description: Generic fileops select indirected through the fileops pointer
5285 * in the fileproc structure
5287 * Parameters: fp fileproc structure pointer
5288 * which select which
5289 * wql pointer to wait queue list
5290 * ctx VFS context for operation
5292 * Returns: 0 Success
5293 * !0 Errno from select
5296 fo_select(struct fileproc
*fp
, int which
, void *wql
, vfs_context_t ctx
)
5298 return((*fp
->f_ops
->fo_select
)(fp
, which
, wql
, ctx
));
5305 * Description: Generic fileops close indirected through the fileops pointer
5306 * in the fileproc structure
5308 * Parameters: fp fileproc structure pointer for
5310 * ctx VFS context for operation
5312 * Returns: 0 Success
5313 * !0 Errno from close
5316 fo_close(struct fileglob
*fg
, vfs_context_t ctx
)
5318 return((*fg
->fg_ops
->fo_close
)(fg
, ctx
));
5325 * Description: Generic fileops kqueue filter indirected through the fileops
5326 * pointer in the fileproc structure
5328 * Parameters: fp fileproc structure pointer
5329 * kn pointer to knote to filter on
5330 * ctx VFS context for operation
5332 * Returns: 0 Success
5333 * !0 Errno from kqueue filter
5336 fo_kqfilter(struct fileproc
*fp
, struct knote
*kn
, vfs_context_t ctx
)
5338 return ((*fp
->f_ops
->fo_kqfilter
)(fp
, kn
, ctx
));
5342 * The ability to send a file descriptor to another
5343 * process is opt-in by file type.
5346 filetype_issendable(file_type_t fdtype
)
5355 /* DTYPE_KQUEUE, DTYPE_FSEVENTS, DTYPE_PSXSEM */