2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* Copyright (c) 1995, 1997 Apple Computer, Inc. All Rights Reserved */
25 * Copyright (c) 1982, 1986, 1989, 1991, 1993
26 * The Regents of the University of California. All rights reserved.
27 * (c) UNIX System Laboratories, Inc.
28 * All or some portions of this file are derived from material licensed
29 * to the University of California by American Telephone and Telegraph
30 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
31 * the permission of UNIX System Laboratories, Inc.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)kern_descrip.c 8.8 (Berkeley) 2/14/95
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/filedesc.h>
67 #include <sys/kernel.h>
68 #include <sys/vnode_internal.h>
69 #include <sys/proc_internal.h>
70 #include <sys/kauth.h>
71 #include <sys/file_internal.h>
72 #include <sys/socket.h>
73 #include <sys/socketvar.h>
75 #include <sys/ioctl.h>
76 #include <sys/fcntl.h>
77 #include <sys/malloc.h>
79 #include <sys/syslog.h>
80 #include <sys/unistd.h>
81 #include <sys/resourcevar.h>
82 #include <sys/aio_kern.h>
84 #include <kern/lock.h>
86 #include <bsm/audit_kernel.h>
88 #include <sys/mount_internal.h>
89 #include <sys/kdebug.h>
90 #include <sys/sysproto.h>
92 #include <kern/kern_types.h>
93 #include <kern/kalloc.h>
94 #include <libkern/OSAtomic.h>
99 int fdopen(dev_t dev
, int mode
, int type
, struct proc
*p
);
100 int ogetdtablesize(struct proc
*p
, void *uap
, register_t
*retval
);
101 int finishdup(struct proc
* p
, struct filedesc
*fdp
, int old
, int new, register_t
*retval
);
103 int closef(struct fileglob
*fg
, struct proc
*p
);
104 int falloc_locked(struct proc
*p
, struct fileproc
**resultfp
, int *resultfd
, int locked
);
105 void fddrop(struct proc
*p
, int fd
);
106 int fdgetf_noref(struct proc
*p
, int fd
, struct fileproc
**resultfp
);
107 void fg_drop(struct fileproc
* fp
);
108 void fg_free(struct fileglob
*fg
);
109 void fg_ref(struct fileproc
* fp
);
110 int fp_getfpshm(struct proc
*p
, int fd
, struct fileproc
**resultfp
, struct pshmnode
**resultpshm
);
112 static int closef_finish(struct fileproc
*fp
, struct fileglob
*fg
, struct proc
*p
);
114 extern void file_lock_init(void);
115 extern int is_suser(void);
116 extern int kqueue_stat(struct fileproc
*fp
, struct stat
*st
, struct proc
*p
);
117 extern int soo_stat(struct socket
*so
, struct stat
*ub
);
118 extern int vn_path_package_check(vnode_t vp
, char *path
, int pathlen
, int *component
);
120 extern kauth_scope_t kauth_scope_fileop
;
122 #define f_flag f_fglob->fg_flag
123 #define f_type f_fglob->fg_type
124 #define f_msgcount f_fglob->fg_msgcount
125 #define f_cred f_fglob->fg_cred
126 #define f_ops f_fglob->fg_ops
127 #define f_offset f_fglob->fg_offset
128 #define f_data f_fglob->fg_data
130 * Descriptor management.
132 struct filelist filehead
; /* head of list of open files */
133 struct fmsglist fmsghead
; /* head of list of open files */
134 struct fmsglist fmsg_ithead
; /* head of list of open files */
135 int nfiles
; /* actual number of open files */
138 lck_grp_attr_t
* file_lck_grp_attr
;
139 lck_grp_t
* file_lck_grp
;
140 lck_attr_t
* file_lck_attr
;
142 lck_mtx_t
* uipc_lock
;
143 lck_mtx_t
* file_iterate_lcok
;
144 lck_mtx_t
* file_flist_lock
;
151 /* allocate file lock group attribute and group */
152 file_lck_grp_attr
= lck_grp_attr_alloc_init();
153 lck_grp_attr_setstat(file_lck_grp_attr
);
155 file_lck_grp
= lck_grp_alloc_init("file", file_lck_grp_attr
);
157 /* Allocate file lock attribute */
158 file_lck_attr
= lck_attr_alloc_init();
159 //lck_attr_setdebug(file_lck_attr);
161 uipc_lock
= lck_mtx_alloc_init(file_lck_grp
, file_lck_attr
);
162 file_iterate_lcok
= lck_mtx_alloc_init(file_lck_grp
, file_lck_attr
);
163 file_flist_lock
= lck_mtx_alloc_init(file_lck_grp
, file_lck_attr
);
171 proc_fdlock(struct proc
*p
)
173 lck_mtx_lock(&p
->p_fdmlock
);
177 proc_fdunlock(struct proc
*p
)
179 lck_mtx_unlock(&p
->p_fdmlock
);
183 * System calls on descriptors.
187 getdtablesize(struct proc
*p
, __unused
struct getdtablesize_args
*uap
, register_t
*retval
)
190 *retval
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
197 ogetdtablesize(struct proc
*p
, __unused
void *uap
, register_t
*retval
)
200 *retval
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, NOFILE
);
206 static __inline__
void
207 _fdrelse(struct filedesc
*fdp
, int fd
)
209 if (fd
< fdp
->fd_freefile
)
210 fdp
->fd_freefile
= fd
;
212 if (fd
> fdp
->fd_lastfile
)
213 panic("fdrelse: fd_lastfile inconsistent");
215 fdp
->fd_ofiles
[fd
] = NULL
;
216 fdp
->fd_ofileflags
[fd
] = 0;
218 while ((fd
= fdp
->fd_lastfile
) > 0 &&
219 fdp
->fd_ofiles
[fd
] == NULL
&&
220 !(fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
225 * Duplicate a file descriptor.
231 struct dup_args
*uap
;
234 register struct filedesc
*fdp
= p
->p_fd
;
235 register int old
= uap
->fd
;
240 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
244 if ( (error
= fdalloc(p
, 0, &new)) ) {
245 fp_drop(p
, old
, fp
, 1);
249 error
= finishdup(p
, fdp
, old
, new, retval
);
250 fp_drop(p
, old
, fp
, 1);
257 * Duplicate a file descriptor to a particular value.
263 struct dup2_args
*uap
;
266 register struct filedesc
*fdp
= p
->p_fd
;
267 register int old
= uap
->from
, new = uap
->to
;
273 if ( (error
= fp_lookup(p
, old
, &fp
, 1)) ) {
278 new >= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
280 fp_drop(p
, old
, fp
, 1);
285 fp_drop(p
, old
, fp
, 1);
290 if (new < 0 || new >= fdp
->fd_nfiles
) {
291 if ( (error
= fdalloc(p
, new, &i
)) ) {
292 fp_drop(p
, old
, fp
, 1);
301 struct fileproc
**fpp
;
304 flags
= fdp
->fd_ofileflags
[new];
305 if ((flags
& (UF_RESERVED
| UF_CLOSING
)) == UF_RESERVED
) {
306 fp_drop(p
, old
, fp
, 1);
312 * dup2() must succeed even if the close has an error.
314 if (*(fpp
= &fdp
->fd_ofiles
[new])) {
315 struct fileproc
*nfp
= *fpp
;
317 close_internal(p
, new, nfp
, (CLOSEINT_LOCKED
| CLOSEINT_WAITONCLOSE
| CLOSEINT_NOFDRELSE
| CLOSEINT_NOFDNOREF
));
321 error
= finishdup(p
, fdp
, old
, new, retval
);
322 fp_drop(p
, old
, fp
, 1);
329 * The file control system call.
332 fcntl(p
, uap
, retval
)
334 struct fcntl_args
*uap
;
338 struct filedesc
*fdp
= p
->p_fd
;
342 int i
, tmp
, error
, error2
, flg
= F_POSIX
;
344 struct vfs_context context
;
348 int devBlockSize
= 0;
352 AUDIT_ARG(fd
, uap
->fd
);
353 AUDIT_ARG(cmd
, uap
->cmd
);
356 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
361 context
.vc_ucred
= fp
->f_cred
;
362 if (proc_is64bit(p
)) {
366 /* since the arg parameter is defined as a long but may be either
367 * a long or a pointer we must take care to handle sign extension
368 * issues. Our sys call munger will sign extend a long when we are
369 * called from a 32-bit process. Since we can never have an address
370 * greater than 32-bits from a 32-bit process we lop off the top
371 * 32-bits to avoid getting the wrong address
373 argp
= CAST_USER_ADDR_T(uap
->arg
);
376 pop
= &fdp
->fd_ofileflags
[fd
];
381 newmin
= CAST_DOWN(int, uap
->arg
);
382 if ((u_int
)newmin
>= p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
||
383 newmin
>= maxfiles
) {
387 if ( (error
= fdalloc(p
, newmin
, &i
)) )
389 error
= finishdup(p
, fdp
, fd
, i
, retval
);
393 *retval
= (*pop
& UF_EXCLOSE
)? 1 : 0;
398 *pop
= (*pop
&~ UF_EXCLOSE
) |
399 (uap
->arg
& 1)? UF_EXCLOSE
: 0;
404 *retval
= OFLAGS(fp
->f_flag
);
409 fp
->f_flag
&= ~FCNTLFLAGS
;
410 tmp
= CAST_DOWN(int, uap
->arg
);
411 fp
->f_flag
|= FFLAGS(tmp
) & FCNTLFLAGS
;
412 tmp
= fp
->f_flag
& FNONBLOCK
;
413 error
= fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, p
);
416 tmp
= fp
->f_flag
& FASYNC
;
417 error
= fo_ioctl(fp
, FIOASYNC
, (caddr_t
)&tmp
, p
);
420 fp
->f_flag
&= ~FNONBLOCK
;
422 (void)fo_ioctl(fp
, FIONBIO
, (caddr_t
)&tmp
, p
);
426 if (fp
->f_type
== DTYPE_SOCKET
) {
427 *retval
= ((struct socket
*)fp
->f_data
)->so_pgid
;
431 error
= fo_ioctl(fp
, (int)TIOCGPGRP
, (caddr_t
)retval
, p
);
436 tmp
= CAST_DOWN(pid_t
, uap
->arg
);
437 if (fp
->f_type
== DTYPE_SOCKET
) {
438 ((struct socket
*)fp
->f_data
)->so_pgid
= tmp
;
442 if (fp
->f_type
== DTYPE_PIPE
) {
443 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, p
);
450 struct proc
*p1
= pfind(tmp
);
455 tmp
= (int)p1
->p_pgrp
->pg_id
;
457 error
= fo_ioctl(fp
, (int)TIOCSPGRP
, (caddr_t
)&tmp
, p
);
462 /* Fall into F_SETLK */
465 if (fp
->f_type
!= DTYPE_VNODE
) {
469 vp
= (struct vnode
*)fp
->f_data
;
472 offset
= fp
->f_offset
;
475 /* Copy in the lock structure */
476 error
= copyin(argp
, (caddr_t
)&fl
, sizeof (fl
));
480 if ( (error
= vnode_getwithref(vp
)) ) {
483 if (fl
.l_whence
== SEEK_CUR
)
484 fl
.l_start
+= offset
;
489 if ((fflag
& FREAD
) == 0) {
494 OSBitOrAtomic(P_LADVLOCK
, &p
->p_ladvflag
);
495 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_SETLK
, &fl
, flg
, &context
);
500 if ((fflag
& FWRITE
) == 0) {
505 OSBitOrAtomic(P_LADVLOCK
, &p
->p_ladvflag
);
506 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_SETLK
, &fl
, flg
, &context
);
511 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &fl
,
523 if (fp
->f_type
!= DTYPE_VNODE
) {
527 vp
= (struct vnode
*)fp
->f_data
;
529 offset
= fp
->f_offset
;
532 /* Copy in the lock structure */
533 error
= copyin(argp
, (caddr_t
)&fl
, sizeof (fl
));
537 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
538 if (fl
.l_whence
== SEEK_CUR
)
539 fl
.l_start
+= offset
;
541 error
= VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_GETLK
, &fl
, F_POSIX
, &context
);
546 error
= copyout((caddr_t
)&fl
, argp
, sizeof (fl
));
550 case F_PREALLOCATE
: {
551 fstore_t alloc_struct
; /* structure for allocate command */
552 u_int32_t alloc_flags
= 0;
554 if (fp
->f_type
!= DTYPE_VNODE
) {
559 vp
= (struct vnode
*)fp
->f_data
;
562 /* make sure that we have write permission */
563 if ((fp
->f_flag
& FWRITE
) == 0) {
568 error
= copyin(argp
, (caddr_t
)&alloc_struct
, sizeof (alloc_struct
));
572 /* now set the space allocated to 0 */
573 alloc_struct
.fst_bytesalloc
= 0;
576 * Do some simple parameter checking
579 /* set up the flags */
581 alloc_flags
|= PREALLOCATE
;
583 if (alloc_struct
.fst_flags
& F_ALLOCATECONTIG
)
584 alloc_flags
|= ALLOCATECONTIG
;
586 if (alloc_struct
.fst_flags
& F_ALLOCATEALL
)
587 alloc_flags
|= ALLOCATEALL
;
590 * Do any position mode specific stuff. The only
591 * position mode supported now is PEOFPOSMODE
594 switch (alloc_struct
.fst_posmode
) {
597 if (alloc_struct
.fst_offset
!= 0) {
602 alloc_flags
|= ALLOCATEFROMPEOF
;
606 if (alloc_struct
.fst_offset
<= 0) {
611 alloc_flags
|= ALLOCATEFROMVOL
;
619 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
621 * call allocate to get the space
623 error
= VNOP_ALLOCATE(vp
,alloc_struct
.fst_length
,alloc_flags
,
624 &alloc_struct
.fst_bytesalloc
, alloc_struct
.fst_offset
,
628 error2
= copyout((caddr_t
)&alloc_struct
, argp
, sizeof (alloc_struct
));
637 if (fp
->f_type
!= DTYPE_VNODE
) {
643 error
= copyin(argp
, (caddr_t
)&offset
, sizeof (off_t
));
648 * Make sure that we are root. Growing a file
649 * without zero filling the data is a security hole
650 * root would have access anyway so we'll allow it
657 vp
= (struct vnode
*)fp
->f_data
;
659 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
663 error
= vnode_setsize(vp
, offset
, IO_NOZEROFILL
, &context
);
670 if (fp
->f_type
!= DTYPE_VNODE
) {
674 vp
= (struct vnode
*)fp
->f_data
;
677 if ( (error
= vnode_getwithref(vp
)) == 0) {
679 vnode_clearnoreadahead(vp
);
681 vnode_setnoreadahead(vp
);
688 if (fp
->f_type
!= DTYPE_VNODE
) {
692 vp
= (struct vnode
*)fp
->f_data
;
695 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
697 vnode_setnocache(vp
);
699 vnode_clearnocache(vp
);
705 case F_GLOBAL_NOCACHE
:
706 if (fp
->f_type
!= DTYPE_VNODE
) {
710 vp
= (struct vnode
*)fp
->f_data
;
713 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
715 *retval
= vnode_isnocache(vp
);
718 vnode_setnocache(vp
);
720 vnode_clearnocache(vp
);
727 struct radvisory ra_struct
;
729 if (fp
->f_type
!= DTYPE_VNODE
) {
733 vp
= (struct vnode
*)fp
->f_data
;
736 if ( (error
= copyin(argp
, (caddr_t
)&ra_struct
, sizeof (ra_struct
))) )
738 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
739 error
= VNOP_IOCTL(vp
, F_RDADVISE
, (caddr_t
)&ra_struct
, 0, &context
);
746 case F_READBOOTSTRAP
:
747 case F_WRITEBOOTSTRAP
: {
748 fbootstraptransfer_t fbt_struct
;
749 user_fbootstraptransfer_t user_fbt_struct
;
751 caddr_t boot_structp
;
753 if (fp
->f_type
!= DTYPE_VNODE
) {
757 vp
= (struct vnode
*)fp
->f_data
;
760 if (IS_64BIT_PROCESS(p
)) {
761 sizeof_struct
= sizeof(user_fbt_struct
);
762 boot_structp
= (caddr_t
) &user_fbt_struct
;
765 sizeof_struct
= sizeof(fbt_struct
);
766 boot_structp
= (caddr_t
) &fbt_struct
;
768 error
= copyin(argp
, boot_structp
, sizeof_struct
);
771 if ( (error
= vnode_getwithref(vp
)) ) {
774 if (uap
->cmd
== F_WRITEBOOTSTRAP
) {
776 * Make sure that we are root. Updating the
777 * bootstrap on a disk could be a security hole
785 if (strcmp(vnode_mount(vp
)->mnt_vfsstat
.f_fstypename
, "hfs") != 0) {
789 * call vnop_ioctl to handle the I/O
791 error
= VNOP_IOCTL(vp
, uap
->cmd
, boot_structp
, 0, &context
);
797 struct log2phys l2p_struct
; /* structure for allocate command */
799 if (fp
->f_type
!= DTYPE_VNODE
) {
803 vp
= (struct vnode
*)fp
->f_data
;
805 if ( (error
= vnode_getwithref(vp
)) ) {
808 error
= VNOP_OFFTOBLK(vp
, fp
->f_offset
, &lbn
);
813 error
= VNOP_BLKTOOFF(vp
, lbn
, &offset
);
818 devBlockSize
= vfs_devblocksize(vnode_mount(vp
));
820 error
= VNOP_BLOCKMAP(vp
, offset
, devBlockSize
, &bn
, NULL
, NULL
, 0, &context
);
825 l2p_struct
.l2p_flags
= 0; /* for now */
826 l2p_struct
.l2p_contigbytes
= 0; /* for now */
827 l2p_struct
.l2p_devoffset
= bn
* devBlockSize
;
828 l2p_struct
.l2p_devoffset
+= fp
->f_offset
- offset
;
829 error
= copyout((caddr_t
)&l2p_struct
, argp
, sizeof (l2p_struct
));
837 if (fp
->f_type
!= DTYPE_VNODE
) {
841 vp
= (struct vnode
*)fp
->f_data
;
844 pathlen
= MAXPATHLEN
;
845 MALLOC(pathbufp
, char *, pathlen
, M_TEMP
, M_WAITOK
);
846 if (pathbufp
== NULL
) {
850 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
851 error
= vn_getpath(vp
, pathbufp
, &pathlen
);
855 error
= copyout((caddr_t
)pathbufp
, argp
, pathlen
);
857 FREE(pathbufp
, M_TEMP
);
861 case F_PATHPKG_CHECK
: {
865 if (fp
->f_type
!= DTYPE_VNODE
) {
869 vp
= (struct vnode
*)fp
->f_data
;
872 pathlen
= MAXPATHLEN
;
873 pathbufp
= kalloc(MAXPATHLEN
);
875 if ( (error
= copyinstr(argp
, pathbufp
, MAXPATHLEN
, &pathlen
)) == 0 ) {
876 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
877 error
= vn_path_package_check(vp
, pathbufp
, pathlen
, retval
);
882 kfree(pathbufp
, MAXPATHLEN
);
886 case F_CHKCLEAN
: // used by regression tests to see if all dirty pages got cleaned by fsync()
887 case F_FULLFSYNC
: // fsync + flush the journal + DKIOCSYNCHRONIZECACHE
888 case F_FREEZE_FS
: // freeze all other fs operations for the fs of this fd
889 case F_THAW_FS
: { // thaw all frozen fs operations for the fs of this fd
890 if (fp
->f_type
!= DTYPE_VNODE
) {
894 vp
= (struct vnode
*)fp
->f_data
;
897 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
898 error
= VNOP_IOCTL(vp
, uap
->cmd
, (caddr_t
)NULL
, 0, &context
);
906 if (uap
->cmd
< FCNTL_FS_SPECIFIC_BASE
) {
911 // if it's a fs-specific fcntl() then just pass it through
913 if (fp
->f_type
!= DTYPE_VNODE
) {
917 vp
= (struct vnode
*)fp
->f_data
;
920 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
921 error
= VNOP_IOCTL(vp
, uap
->cmd
, CAST_DOWN(caddr_t
, argp
), 0, &context
);
930 AUDIT_ARG(vnpath_withref
, vp
, ARG_VNODE1
);
931 fp_drop(p
, fd
, fp
, 0);
934 fp_drop(p
, fd
, fp
, 1);
940 * Common code for dup, dup2, and fcntl(F_DUPFD).
943 finishdup(struct proc
* p
, struct filedesc
*fdp
, int old
, int new, register_t
*retval
)
945 struct fileproc
*nfp
;
946 struct fileproc
*ofp
;
948 if ((ofp
= fdp
->fd_ofiles
[old
]) == NULL
||
949 (fdp
->fd_ofileflags
[old
] & UF_RESERVED
)) {
956 MALLOC_ZONE(nfp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
957 bzero(nfp
, sizeof(struct fileproc
));
960 nfp
->f_flags
= ofp
->f_flags
;
961 nfp
->f_fglob
= ofp
->f_fglob
;
964 fdp
->fd_ofiles
[new] = nfp
;
965 fdp
->fd_ofileflags
[new] = fdp
->fd_ofileflags
[old
] &~ UF_EXCLOSE
;
966 if (new > fdp
->fd_lastfile
)
967 fdp
->fd_lastfile
= new;
974 close(struct proc
*p
, struct close_args
*uap
, __unused register_t
*retval
)
980 AUDIT_SYSCLOSE(p
, fd
);
984 if ( (error
= fp_lookup(p
,fd
,&fp
, 1)) ) {
989 error
= close_internal(p
, fd
, fp
, CLOSEINT_LOCKED
| CLOSEINT_WAITONCLOSE
);
998 * Close a file descriptor.
1001 close_internal(struct proc
*p
, int fd
, struct fileproc
*fp
, int flags
)
1003 struct filedesc
*fdp
= p
->p_fd
;
1005 int locked
= flags
& CLOSEINT_LOCKED
;
1006 int waitonclose
= flags
& CLOSEINT_WAITONCLOSE
;
1007 int norelse
= flags
& CLOSEINT_NOFDRELSE
;
1008 int nofdref
= flags
& CLOSEINT_NOFDNOREF
;
1009 int slpstate
= PRIBIO
;
1014 /* Keep people from using the filedesc while we are closing it */
1015 fdp
->fd_ofileflags
[fd
] |= UF_RESERVED
;
1017 fdp
->fd_ofileflags
[fd
] |= UF_CLOSING
;
1020 if ((waitonclose
&& ((fp
->f_flags
& FP_CLOSING
) == FP_CLOSING
))) {
1022 fp_drop(p
, fd
, fp
, 1);
1023 fp
->f_flags
|= FP_WAITCLOSE
;
1026 msleep(&fp
->f_flags
, &p
->p_fdmlock
, slpstate
, "close wait",0) ;
1030 fp
->f_flags
|= FP_CLOSING
;
1034 if ( (fp
->f_flags
& FP_AIOISSUED
) || kauth_authorize_fileop_has_listeners() ) {
1038 if ( (fp
->f_type
== DTYPE_VNODE
) && kauth_authorize_fileop_has_listeners() ) {
1040 * call out to allow 3rd party notification of close.
1041 * Ignore result of kauth_authorize_fileop call.
1043 if (vnode_getwithref((vnode_t
)fp
->f_data
) == 0) {
1044 u_int fileop_flags
= 0;
1045 if ((fp
->f_flags
& FP_WRITTEN
) != 0)
1046 fileop_flags
|= KAUTH_FILEOP_CLOSE_MODIFIED
;
1047 kauth_authorize_fileop(fp
->f_fglob
->fg_cred
, KAUTH_FILEOP_CLOSE
,
1048 (uintptr_t)fp
->f_data
, (uintptr_t)fileop_flags
);
1049 vnode_put((vnode_t
)fp
->f_data
);
1052 if (fp
->f_flags
& FP_AIOISSUED
)
1054 * cancel all async IO requests that can be cancelled.
1056 _aio_close( p
, fd
);
1061 if (fd
< fdp
->fd_knlistsize
)
1062 knote_fdclose(p
, fd
);
1064 if (fp
->f_flags
& FP_WAITEVENT
)
1065 (void)waitevent_close(p
, fp
);
1067 if ((fp
->f_flags
& FP_INCHRREAD
) == 0)
1068 fileproc_drain(p
, fp
);
1071 error
= closef_locked(fp
, fp
->f_fglob
, p
);
1072 if ((fp
->f_flags
& FP_WAITCLOSE
) == FP_WAITCLOSE
)
1073 wakeup(&fp
->f_flags
);
1074 fp
->f_flags
&= ~(FP_WAITCLOSE
| FP_CLOSING
);
1079 FREE_ZONE(fp
, sizeof *fp
, M_FILEPROC
);
1084 * Return status information about a file descriptor.
1086 * XXX switch on node type is bogus; need a stat in struct fileops instead.
1089 fstat1(struct proc
*p
, int fd
, user_addr_t ub
, user_addr_t xsecurity
, user_addr_t xsecurity_size
)
1091 struct fileproc
*fp
;
1093 struct user_stat user_sb
;
1098 kauth_filesec_t fsec
;
1099 ssize_t xsecurity_bufsize
;
1101 struct vfs_context context
;
1106 if ((error
= fp_lookup(p
, fd
, &fp
, 0)) != 0)
1110 fsec
= KAUTH_FILESEC_NONE
;
1115 context
.vc_proc
= current_proc();
1116 context
.vc_ucred
= kauth_cred_get();
1117 if ((error
= vnode_getwithref((vnode_t
)data
)) == 0) {
1119 * If the caller has the file open, and is not requesting extended security,
1120 * we are going to let them get the basic stat information.
1122 if (xsecurity
== USER_ADDR_NULL
) {
1123 error
= vn_stat_noauth((vnode_t
)data
, &sb
, NULL
, &context
);
1125 error
= vn_stat((vnode_t
)data
, &sb
, &fsec
, &context
);
1128 AUDIT_ARG(vnpath
, (struct vnode
*)data
, ARG_VNODE1
);
1129 (void)vnode_put((vnode_t
)data
);
1134 error
= soo_stat((struct socket
*)data
, &sb
);
1138 error
= pipe_stat((void *)data
, &sb
);
1142 error
= pshm_stat((void *)data
, &sb
);
1146 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1147 error
= kqueue_stat(fp
, &sb
, p
);
1148 thread_funnel_set(kernel_flock
, funnel_state
);
1155 /* Zap spare fields */
1157 sb
.st_qspare
[0] = 0LL;
1158 sb
.st_qspare
[1] = 0LL;
1161 if (IS_64BIT_PROCESS(current_proc())) {
1162 munge_stat(&sb
, &user_sb
);
1163 my_size
= sizeof(user_sb
);
1164 sbp
= (caddr_t
)&user_sb
;
1167 my_size
= sizeof(sb
);
1170 error
= copyout(sbp
, ub
, my_size
);
1173 /* caller wants extended security information? */
1174 if (xsecurity
!= USER_ADDR_NULL
) {
1176 /* did we get any? */
1177 if (fsec
== KAUTH_FILESEC_NONE
) {
1178 if (susize(xsecurity_size
, 0) != 0) {
1183 /* find the user buffer size */
1184 xsecurity_bufsize
= fusize(xsecurity_size
);
1186 /* copy out the actual data size */
1187 if (susize(xsecurity_size
, KAUTH_FILESEC_COPYSIZE(fsec
)) != 0) {
1192 /* if the caller supplied enough room, copy out to it */
1193 if (xsecurity_bufsize
>= KAUTH_FILESEC_COPYSIZE(fsec
))
1194 error
= copyout(fsec
, xsecurity
, KAUTH_FILESEC_COPYSIZE(fsec
));
1198 fp_drop(p
, fd
, fp
, 0);
1200 kauth_filesec_free(fsec
);
1205 fstat_extended(struct proc
*p
, struct fstat_extended_args
*uap
, __unused register_t
*retval
)
1207 return(fstat1(p
, uap
->fd
, uap
->ub
, uap
->xsecurity
, uap
->xsecurity_size
));
1211 fstat(struct proc
*p
, register struct fstat_args
*uap
, __unused register_t
*retval
)
1213 return(fstat1(p
, uap
->fd
, uap
->ub
, 0, 0));
1217 * Return pathconf information about a file descriptor.
1220 fpathconf(p
, uap
, retval
)
1222 register struct fpathconf_args
*uap
;
1226 struct fileproc
*fp
;
1228 struct vfs_context context
;
1234 AUDIT_ARG(fd
, uap
->fd
);
1235 if ( (error
= fp_lookup(p
, fd
, &fp
, 0)) )
1243 if (uap
->name
!= _PC_PIPE_BUF
) {
1257 vp
= (struct vnode
*)data
;
1259 if ( (error
= vnode_getwithref(vp
)) == 0) {
1260 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
1262 context
.vc_proc
= p
;
1263 context
.vc_ucred
= kauth_cred_get();
1265 error
= vn_pathconf(vp
, uap
->name
, retval
, &context
);
1267 (void)vnode_put(vp
);
1277 panic("fpathconf (unrecognized - %d)", type
);
1281 fp_drop(p
, fd
, fp
, 0);
1286 * Allocate a file descriptor for the process.
1291 fdalloc(p
, want
, result
)
1296 register struct filedesc
*fdp
= p
->p_fd
;
1298 int lim
, last
, numfiles
, oldnfiles
;
1299 struct fileproc
**newofiles
, **ofiles
;
1300 char *newofileflags
, *ofileflags
;
1303 * Search for a free descriptor starting at the higher
1304 * of want or fd_freefile. If that fails, consider
1305 * expanding the ofile array.
1307 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
1309 last
= min(fdp
->fd_nfiles
, lim
);
1310 if ((i
= want
) < fdp
->fd_freefile
)
1311 i
= fdp
->fd_freefile
;
1312 ofiles
= &fdp
->fd_ofiles
[i
];
1313 ofileflags
= &fdp
->fd_ofileflags
[i
];
1314 for (; i
< last
; i
++) {
1315 if (*ofiles
== NULL
&& !(*ofileflags
& UF_RESERVED
)) {
1316 *ofileflags
= UF_RESERVED
;
1317 if (i
> fdp
->fd_lastfile
)
1318 fdp
->fd_lastfile
= i
;
1319 if (want
<= fdp
->fd_freefile
)
1320 fdp
->fd_freefile
= i
;
1324 ofiles
++; ofileflags
++;
1328 * No space in current array. Expand?
1330 if (fdp
->fd_nfiles
>= lim
)
1332 if (fdp
->fd_nfiles
< NDEXTENT
)
1333 numfiles
= NDEXTENT
;
1335 numfiles
= 2 * fdp
->fd_nfiles
;
1340 MALLOC_ZONE(newofiles
, struct fileproc
**,
1341 numfiles
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
1343 if (newofiles
== NULL
) {
1346 if (fdp
->fd_nfiles
>= numfiles
) {
1347 FREE_ZONE(newofiles
, numfiles
* OFILESIZE
, M_OFILETABL
);
1350 newofileflags
= (char *) &newofiles
[numfiles
];
1352 * Copy the existing ofile and ofileflags arrays
1353 * and zero the new portion of each array.
1355 oldnfiles
= fdp
->fd_nfiles
;
1356 (void) memcpy(newofiles
, fdp
->fd_ofiles
,
1357 oldnfiles
* sizeof *fdp
->fd_ofiles
);
1358 (void) memset(&newofiles
[oldnfiles
], 0,
1359 (numfiles
- oldnfiles
) * sizeof *fdp
->fd_ofiles
);
1361 (void) memcpy(newofileflags
, fdp
->fd_ofileflags
,
1362 oldnfiles
* sizeof *fdp
->fd_ofileflags
);
1363 (void) memset(&newofileflags
[oldnfiles
], 0,
1364 (numfiles
- oldnfiles
) *
1365 sizeof *fdp
->fd_ofileflags
);
1366 ofiles
= fdp
->fd_ofiles
;
1367 fdp
->fd_ofiles
= newofiles
;
1368 fdp
->fd_ofileflags
= newofileflags
;
1369 fdp
->fd_nfiles
= numfiles
;
1370 FREE_ZONE(ofiles
, oldnfiles
* OFILESIZE
, M_OFILETABL
);
1376 * Check to see whether n user file descriptors
1377 * are available to the process p.
1384 struct filedesc
*fdp
= p
->p_fd
;
1385 struct fileproc
**fpp
;
1389 lim
= min((int)p
->p_rlimit
[RLIMIT_NOFILE
].rlim_cur
, maxfiles
);
1390 if ((i
= lim
- fdp
->fd_nfiles
) > 0 && (n
-= i
) <= 0)
1392 fpp
= &fdp
->fd_ofiles
[fdp
->fd_freefile
];
1393 flags
= &fdp
->fd_ofileflags
[fdp
->fd_freefile
];
1394 for (i
= fdp
->fd_nfiles
- fdp
->fd_freefile
; --i
>= 0; fpp
++, flags
++)
1395 if (*fpp
== NULL
&& !(*flags
& UF_RESERVED
) && --n
<= 0)
1405 _fdrelse(p
->p_fd
, fd
);
1413 struct filedesc
*fdp
= p
->p_fd
;
1414 struct fileproc
*fp
;
1416 if (fd
< fdp
->fd_freefile
)
1417 fdp
->fd_freefile
= fd
;
1419 if (fd
> fdp
->fd_lastfile
)
1420 panic("fdrelse: fd_lastfile inconsistent");
1422 fp
= fdp
->fd_ofiles
[fd
];
1423 fdp
->fd_ofiles
[fd
] = NULL
;
1424 fdp
->fd_ofileflags
[fd
] = 0;
1426 while ((fd
= fdp
->fd_lastfile
) > 0 &&
1427 fdp
->fd_ofiles
[fd
] == NULL
&&
1428 !(fdp
->fd_ofileflags
[fd
] & UF_RESERVED
))
1430 FREE_ZONE(fp
, sizeof *fp
, M_FILEPROC
);
1435 fdgetf_noref(p
, fd
, resultfp
)
1438 struct fileproc
**resultfp
;
1440 struct filedesc
*fdp
= p
->p_fd
;
1441 struct fileproc
*fp
;
1443 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
1444 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
1445 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1454 /* should be called only when proc_fdlock is held */
1456 fp_setflags(proc_t p
, struct fileproc
* fp
, int flags
)
1459 fp
->f_flags
|= flags
;
1464 fp_clearflags(proc_t p
, struct fileproc
* fp
, int flags
)
1469 fp
->f_flags
&= ~flags
;
1474 fp_getfvp(p
, fd
, resultfp
, resultvp
)
1477 struct fileproc
**resultfp
;
1478 struct vnode
**resultvp
;
1480 struct filedesc
*fdp
= p
->p_fd
;
1481 struct fileproc
*fp
;
1484 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
1485 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
1486 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1490 if (fp
->f_type
!= DTYPE_VNODE
) {
1499 *resultvp
= (struct vnode
*)fp
->f_data
;
1507 * Returns: EBADF The file descriptor is invalid
1508 * EOPNOTSUPP The file descriptor is not a socket
1511 * Notes: EOPNOTSUPP should probably be ENOTSOCK; this function is only
1512 * ever called from accept1().
1515 fp_getfsock(p
, fd
, resultfp
, results
)
1518 struct fileproc
**resultfp
;
1519 struct socket
**results
;
1521 struct filedesc
*fdp
= p
->p_fd
;
1522 struct fileproc
*fp
;
1525 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
1526 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
1527 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1531 if (fp
->f_type
!= DTYPE_SOCKET
) {
1540 *results
= (struct socket
*)fp
->f_data
;
1548 fp_getfkq(p
, fd
, resultfp
, resultkq
)
1551 struct fileproc
**resultfp
;
1552 struct kqueue
**resultkq
;
1554 struct filedesc
*fdp
= p
->p_fd
;
1555 struct fileproc
*fp
;
1558 if ( fd
< 0 || fd
>= fdp
->fd_nfiles
||
1559 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
1560 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1564 if (fp
->f_type
!= DTYPE_KQUEUE
) {
1573 *resultkq
= (struct kqueue
*)fp
->f_data
;
1580 fp_getfpshm(p
, fd
, resultfp
, resultpshm
)
1583 struct fileproc
**resultfp
;
1584 struct pshmnode
**resultpshm
;
1586 struct filedesc
*fdp
= p
->p_fd
;
1587 struct fileproc
*fp
;
1590 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
1591 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
1592 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1596 if (fp
->f_type
!= DTYPE_PSXSHM
) {
1606 *resultpshm
= (struct pshmnode
*)fp
->f_data
;
1614 fp_getfpsem(p
, fd
, resultfp
, resultpsem
)
1617 struct fileproc
**resultfp
;
1618 struct psemnode
**resultpsem
;
1620 struct filedesc
*fdp
= p
->p_fd
;
1621 struct fileproc
*fp
;
1624 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
1625 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
1626 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1630 if (fp
->f_type
!= DTYPE_PSXSEM
) {
1639 *resultpsem
= (struct psemnode
*)fp
->f_data
;
1645 fp_lookup(p
, fd
, resultfp
, locked
)
1648 struct fileproc
**resultfp
;
1651 struct filedesc
*fdp
= p
->p_fd
;
1652 struct fileproc
*fp
;
1656 if (fd
< 0 || fd
>= fdp
->fd_nfiles
||
1657 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
1658 (fdp
->fd_ofileflags
[fd
] & UF_RESERVED
)) {
1674 fp_drop_written(proc_t p
, int fd
, struct fileproc
*fp
)
1680 fp
->f_flags
|= FP_WRITTEN
;
1682 error
= fp_drop(p
, fd
, fp
, 1);
1691 fp_drop_event(proc_t p
, int fd
, struct fileproc
*fp
)
1697 fp
->f_flags
|= FP_WAITEVENT
;
1699 error
= fp_drop(p
, fd
, fp
, 1);
1707 fp_drop(p
, fd
, fp
, locked
)
1710 struct fileproc
*fp
;
1713 struct filedesc
*fdp
= p
->p_fd
;
1717 if ((fp
== FILEPROC_NULL
) && (fd
< 0 || fd
>= fdp
->fd_nfiles
||
1718 (fp
= fdp
->fd_ofiles
[fd
]) == NULL
||
1719 ((fdp
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
1720 !(fdp
->fd_ofileflags
[fd
] & UF_CLOSING
)))) {
1727 if (p
->p_fpdrainwait
&& fp
->f_iocount
== 0) {
1728 p
->p_fpdrainwait
= 0;
1729 wakeup(&p
->p_fpdrainwait
);
1738 file_vnode(int fd
, struct vnode
**vpp
)
1740 struct proc
* p
= current_proc();
1741 struct fileproc
*fp
;
1745 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
1749 if (fp
->f_type
!= DTYPE_VNODE
) {
1750 fp_drop(p
, fd
, fp
,1);
1754 *vpp
= (struct vnode
*)fp
->f_data
;
1762 file_socket(int fd
, struct socket
**sp
)
1764 struct proc
* p
= current_proc();
1765 struct fileproc
*fp
;
1769 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
1773 if (fp
->f_type
!= DTYPE_SOCKET
) {
1774 fp_drop(p
, fd
, fp
,1);
1778 *sp
= (struct socket
*)fp
->f_data
;
1785 file_flags(int fd
, int * flags
)
1788 struct proc
* p
= current_proc();
1789 struct fileproc
*fp
;
1793 if ( (error
= fp_lookup(p
, fd
, &fp
, 1)) ) {
1797 *flags
= (int)fp
->f_flag
;
1798 fp_drop(p
, fd
, fp
,1);
1808 struct fileproc
*fp
;
1809 struct proc
*p
= current_proc();
1812 if (fd
< 0 || fd
>= p
->p_fd
->fd_nfiles
||
1813 (fp
= p
->p_fd
->fd_ofiles
[fd
]) == NULL
||
1814 ((p
->p_fd
->fd_ofileflags
[fd
] & UF_RESERVED
) &&
1815 !(p
->p_fd
->fd_ofileflags
[fd
] & UF_CLOSING
))) {
1821 if (p
->p_fpdrainwait
&& fp
->f_iocount
== 0) {
1822 p
->p_fpdrainwait
= 0;
1823 wakeup(&p
->p_fpdrainwait
);
1832 falloc(p
, resultfp
, resultfd
)
1834 struct fileproc
**resultfp
;
1840 error
= falloc_locked(p
, resultfp
, resultfd
, 1);
1846 * Create a new open file structure and allocate
1847 * a file decriptor for the process that refers to it.
1850 falloc_locked(p
, resultfp
, resultfd
, locked
)
1852 struct fileproc
**resultfp
;
1856 struct fileproc
*fp
, *fq
;
1857 struct fileglob
*fg
;
1862 if ( (error
= fdalloc(p
, 0, &nfd
)) ) {
1867 if (nfiles
>= maxfiles
) {
1874 * Allocate a new file descriptor.
1875 * If the process has file descriptor zero open, add to the list
1876 * of open files at that point, otherwise put it at the front of
1877 * the list of open files.
1881 MALLOC_ZONE(fp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
1882 MALLOC_ZONE(fg
, struct fileglob
*, sizeof(struct fileglob
), M_FILEGLOB
, M_WAITOK
);
1883 bzero(fp
, sizeof(struct fileproc
));
1884 bzero(fg
, sizeof(struct fileglob
));
1885 lck_mtx_init(&fg
->fg_lock
, file_lck_grp
, file_lck_attr
);
1893 fp
->f_cred
= kauth_cred_proc_ref(p
);
1895 lck_mtx_lock(file_flist_lock
);
1899 if ( (fq
= p
->p_fd
->fd_ofiles
[0]) ) {
1900 LIST_INSERT_AFTER(fq
->f_fglob
, fg
, f_list
);
1902 LIST_INSERT_HEAD(&filehead
, fg
, f_list
);
1904 lck_mtx_unlock(file_flist_lock
);
1906 p
->p_fd
->fd_ofiles
[nfd
] = fp
;
1920 * Free a file structure.
1924 struct fileglob
*fg
;
1928 lck_mtx_lock(file_flist_lock
);
1929 LIST_REMOVE(fg
, f_list
);
1931 lck_mtx_unlock(file_flist_lock
);
1934 if (cred
!= NOCRED
) {
1935 fg
->fg_cred
= NOCRED
;
1936 kauth_cred_rele(cred
);
1938 lck_mtx_destroy(&fg
->fg_lock
, file_lck_grp
);
1940 FREE_ZONE(fg
, sizeof *fg
, M_FILEGLOB
);
1947 struct filedesc
*fdp
= p
->p_fd
;
1948 int i
= fdp
->fd_lastfile
;
1949 struct fileproc
**fpp
= &fdp
->fd_ofiles
[i
];
1950 char *flags
= &fdp
->fd_ofileflags
[i
];
1953 funnel_state
= thread_funnel_set(kernel_flock
, FALSE
);
1957 if ((*flags
& (UF_RESERVED
|UF_EXCLOSE
)) == UF_EXCLOSE
) {
1958 struct fileproc
*fp
= *fpp
;
1960 if (i
< fdp
->fd_knlistsize
)
1961 knote_fdclose(p
, i
);
1963 *fpp
= NULL
; *flags
= 0;
1964 if (i
== fdp
->fd_lastfile
&& i
> 0)
1966 closef_locked(fp
, fp
->f_fglob
, p
);
1967 FREE_ZONE(fp
, sizeof *fp
, M_FILEPROC
);
1970 i
--; fpp
--; flags
--;
1973 thread_funnel_set(kernel_flock
, funnel_state
);
1977 * Copy a filedesc structure.
1983 struct filedesc
*newfdp
, *fdp
= p
->p_fd
;
1985 struct fileproc
*ofp
, *fp
;
1988 MALLOC_ZONE(newfdp
, struct filedesc
*,
1989 sizeof *newfdp
, M_FILEDESC
, M_WAITOK
);
1996 * the FD_CHROOT flag will be inherited via this copy
1998 (void) memcpy(newfdp
, fdp
, sizeof *newfdp
);
2001 * for both fd_cdir and fd_rdir make sure we get
2002 * a valid reference... if we can't, than set
2003 * set the pointer(s) to NULL in the child... this
2004 * will keep us from using a non-referenced vp
2005 * and allows us to do the vnode_rele only on
2006 * a properly referenced vp
2008 if ( (v_dir
= newfdp
->fd_cdir
) ) {
2009 if (vnode_getwithref(v_dir
) == 0) {
2010 if ( (vnode_ref(v_dir
)) )
2011 newfdp
->fd_cdir
= NULL
;
2014 newfdp
->fd_cdir
= NULL
;
2016 if (newfdp
->fd_cdir
== NULL
&& fdp
->fd_cdir
) {
2018 * we couldn't get a new reference on
2019 * the current working directory being
2020 * inherited... we might as well drop
2021 * our reference from the parent also
2022 * since the vnode has gone DEAD making
2023 * it useless... by dropping it we'll
2024 * be that much closer to recyling it
2026 vnode_rele(fdp
->fd_cdir
);
2027 fdp
->fd_cdir
= NULL
;
2030 if ( (v_dir
= newfdp
->fd_rdir
) ) {
2031 if (vnode_getwithref(v_dir
) == 0) {
2032 if ( (vnode_ref(v_dir
)) )
2033 newfdp
->fd_rdir
= NULL
;
2036 newfdp
->fd_rdir
= NULL
;
2038 if (newfdp
->fd_rdir
== NULL
&& fdp
->fd_rdir
) {
2040 * we couldn't get a new reference on
2041 * the root directory being
2042 * inherited... we might as well drop
2043 * our reference from the parent also
2044 * since the vnode has gone DEAD making
2045 * it useless... by dropping it we'll
2046 * be that much closer to recyling it
2048 vnode_rele(fdp
->fd_rdir
);
2049 fdp
->fd_rdir
= NULL
;
2051 newfdp
->fd_refcnt
= 1;
2054 * If the number of open files fits in the internal arrays
2055 * of the open file structure, use them, otherwise allocate
2056 * additional memory for the number of descriptors currently
2059 if (newfdp
->fd_lastfile
< NDFILE
)
2063 * Compute the smallest multiple of NDEXTENT needed
2064 * for the file descriptors currently in use,
2065 * allowing the table to shrink.
2067 i
= newfdp
->fd_nfiles
;
2068 while (i
> 2 * NDEXTENT
&& i
> newfdp
->fd_lastfile
* 2)
2073 MALLOC_ZONE(newfdp
->fd_ofiles
, struct fileproc
**,
2074 i
* OFILESIZE
, M_OFILETABL
, M_WAITOK
);
2075 if (newfdp
->fd_ofiles
== NULL
) {
2076 if (newfdp
->fd_cdir
)
2077 vnode_rele(newfdp
->fd_cdir
);
2078 if (newfdp
->fd_rdir
)
2079 vnode_rele(newfdp
->fd_rdir
);
2081 FREE_ZONE(newfdp
, sizeof *newfdp
, M_FILEDESC
);
2086 newfdp
->fd_ofileflags
= (char *) &newfdp
->fd_ofiles
[i
];
2087 newfdp
->fd_nfiles
= i
;
2089 if (fdp
->fd_nfiles
> 0) {
2090 struct fileproc
**fpp
;
2093 (void) memcpy(newfdp
->fd_ofiles
, fdp
->fd_ofiles
,
2094 i
* sizeof *fdp
->fd_ofiles
);
2095 (void) memcpy(newfdp
->fd_ofileflags
, fdp
->fd_ofileflags
,
2096 i
* sizeof *fdp
->fd_ofileflags
);
2099 * kq descriptors cannot be copied.
2101 if (newfdp
->fd_knlistsize
!= -1) {
2102 fpp
= &newfdp
->fd_ofiles
[newfdp
->fd_lastfile
];
2103 for (i
= newfdp
->fd_lastfile
; i
>= 0; i
--, fpp
--) {
2104 if (*fpp
!= NULL
&& (*fpp
)->f_type
== DTYPE_KQUEUE
) {
2106 if (i
< newfdp
->fd_freefile
)
2107 newfdp
->fd_freefile
= i
;
2109 if (*fpp
== NULL
&& i
== newfdp
->fd_lastfile
&& i
> 0)
2110 newfdp
->fd_lastfile
--;
2112 newfdp
->fd_knlist
= NULL
;
2113 newfdp
->fd_knlistsize
= -1;
2114 newfdp
->fd_knhash
= NULL
;
2115 newfdp
->fd_knhashmask
= 0;
2117 fpp
= newfdp
->fd_ofiles
;
2118 flags
= newfdp
->fd_ofileflags
;
2120 for (i
= newfdp
->fd_lastfile
; i
-- >= 0; fpp
++, flags
++)
2121 if ((ofp
= *fpp
) != NULL
&& !(*flags
& UF_RESERVED
)) {
2122 MALLOC_ZONE(fp
, struct fileproc
*, sizeof(struct fileproc
), M_FILEPROC
, M_WAITOK
);
2123 bzero(fp
, sizeof(struct fileproc
));
2124 fp
->f_flags
= ofp
->f_flags
;
2125 //fp->f_iocount = ofp->f_iocount;
2127 fp
->f_fglob
= ofp
->f_fglob
;
2135 (void) memset(newfdp
->fd_ofiles
, 0, i
* OFILESIZE
);
2142 * Release a filedesc structure.
2148 struct filedesc
*fdp
;
2149 struct fileproc
*fp
;
2154 /* Certain daemons might not have file descriptors */
2157 if ((fdp
== NULL
) || (--fdp
->fd_refcnt
> 0)) {
2161 if (fdp
->fd_refcnt
== 0xffff)
2162 panic("fdfree: bad fd_refcnt");
2164 /* Last reference: the structure can't change out from under us */
2166 if (fdp
->fd_nfiles
> 0 && fdp
->fd_ofiles
) {
2167 for (i
= fdp
->fd_lastfile
; i
>= 0; i
--) {
2168 if ((fp
= fdp
->fd_ofiles
[i
]) != NULL
) {
2170 if (fdp
->fd_ofileflags
[i
] & UF_RESERVED
)
2171 panic("fdfree: found fp with UF_RESERVED\n");
2173 /* closef drops the iocount ... */
2174 if ((fp
->f_flags
& FP_INCHRREAD
) != 0)
2176 fdp
->fd_ofiles
[i
] = NULL
;
2177 fdp
->fd_ofileflags
[i
] |= UF_RESERVED
;
2179 if (i
< fdp
->fd_knlistsize
)
2180 knote_fdclose(p
, i
);
2181 if (fp
->f_flags
& FP_WAITEVENT
)
2182 (void)waitevent_close(p
, fp
);
2183 (void) closef_locked(fp
, fp
->f_fglob
, p
);
2184 FREE_ZONE(fp
, sizeof *fp
, M_FILEPROC
);
2187 FREE_ZONE(fdp
->fd_ofiles
, fdp
->fd_nfiles
* OFILESIZE
, M_OFILETABL
);
2188 fdp
->fd_ofiles
= NULL
;
2195 vnode_rele(fdp
->fd_cdir
);
2197 vnode_rele(fdp
->fd_rdir
);
2204 FREE(fdp
->fd_knlist
, M_KQUEUE
);
2206 FREE(fdp
->fd_knhash
, M_KQUEUE
);
2208 FREE_ZONE(fdp
, sizeof *fdp
, M_FILEDESC
);
2212 closef_finish(fp
, fg
, p
)
2213 struct fileproc
*fp
;
2214 struct fileglob
*fg
;
2220 struct vfs_context context
;
2222 if ((fg
->fg_flag
& FHASLOCK
) && fg
->fg_type
== DTYPE_VNODE
) {
2223 lf
.l_whence
= SEEK_SET
;
2226 lf
.l_type
= F_UNLCK
;
2227 vp
= (struct vnode
*)fg
->fg_data
;
2228 context
.vc_proc
= p
;
2229 context
.vc_ucred
= fg
->fg_cred
;
2231 (void) VNOP_ADVLOCK(vp
, (caddr_t
)fg
, F_UNLCK
, &lf
, F_FLOCK
, &context
);
2234 error
= fo_close(fg
, p
);
2238 if (((fp
!= (struct fileproc
*)0) && ((fp
->f_flags
& FP_INCHRREAD
) != 0))) {
2240 if ( ((fp
->f_flags
& FP_INCHRREAD
) != 0) ) {
2241 fileproc_drain(p
, fp
);
2252 struct fileglob
*fg
;
2258 error
= closef_locked((struct fileproc
*)0, fg
, p
);
2264 * Internal form of close.
2265 * Decrement reference count on file structure.
2266 * Note: p may be NULL when closing a file
2267 * that was being passed in a message.
2270 closef_locked(fp
, fg
, p
)
2271 struct fileproc
*fp
;
2272 struct fileglob
*fg
;
2277 struct vfs_context context
;
2284 * POSIX record locking dictates that any close releases ALL
2285 * locks owned by this process. This is handled by setting
2286 * a flag in the unlock to free ONLY locks obeying POSIX
2287 * semantics, and not to free BSD-style file locks.
2288 * If the descriptor was in a message, POSIX-style locks
2289 * aren't passed with the descriptor.
2291 if (p
&& (p
->p_ladvflag
& P_LADVLOCK
) && fg
->fg_type
== DTYPE_VNODE
) {
2294 lf
.l_whence
= SEEK_SET
;
2297 lf
.l_type
= F_UNLCK
;
2298 vp
= (struct vnode
*)fg
->fg_data
;
2300 if ( (error
= vnode_getwithref(vp
)) == 0 ) {
2301 context
.vc_proc
= p
;
2302 context
.vc_ucred
= fg
->fg_cred
;
2303 (void) VNOP_ADVLOCK(vp
, (caddr_t
)p
, F_UNLCK
, &lf
, F_POSIX
, &context
);
2305 (void)vnode_put(vp
);
2309 lck_mtx_lock(&fg
->fg_lock
);
2312 if (fg
->fg_count
> 0) {
2313 lck_mtx_unlock(&fg
->fg_lock
);
2316 if (fg
->fg_count
!= 0)
2317 panic("fg: being freed with bad fg_count (%d)", fg
, fg
->fg_count
);
2319 if (fp
&& (fp
->f_flags
& FP_WRITTEN
))
2320 fg
->fg_flag
|= FWASWRITTEN
;
2322 fg
->fg_lflags
|= FG_TERM
;
2323 lck_mtx_unlock(&fg
->fg_lock
);
2326 error
= closef_finish(fp
, fg
, p
);
2335 fileproc_drain(struct proc
*p
, struct fileproc
* fp
)
2337 fp
->f_iocount
-- ; /* (the one the close holds) */
2339 while (fp
->f_iocount
) {
2340 if (((fp
->f_flags
& FP_INSELECT
)== FP_INSELECT
)) {
2341 wait_queue_wakeup_all((wait_queue_t
)fp
->f_waddr
, &selwait
, THREAD_INTERRUPTED
);
2343 if (fp
->f_fglob
->fg_ops
->fo_drain
) {
2344 (*fp
->f_fglob
->fg_ops
->fo_drain
)(fp
, p
);
2347 p
->p_fpdrainwait
= 1;
2349 msleep(&p
->p_fpdrainwait
, &p
->p_fdmlock
, PRIBIO
, "fpdrain",0);
2351 //panic("successful wait after drain\n");
2356 fp_free(struct proc
* p
, int fd
, struct fileproc
* fp
)
2362 fg_free(fp
->f_fglob
);
2363 FREE_ZONE(fp
, sizeof *fp
, M_FILEPROC
);
2368 * Apply an advisory lock on a file descriptor.
2370 * Just attempt to get a record lock of the requested type on
2371 * the entire file (l_whence = SEEK_SET, l_start = 0, l_len = 0).
2374 flock(struct proc
*p
, register struct flock_args
*uap
, __unused register_t
*retval
)
2378 struct fileproc
*fp
;
2381 struct vfs_context context
;
2384 AUDIT_ARG(fd
, uap
->fd
);
2385 if ( (error
= fp_getfvp(p
, fd
, &fp
, &vp
)) ) {
2388 if ( (error
= vnode_getwithref(vp
)) ) {
2391 AUDIT_ARG(vnpath
, vp
, ARG_VNODE1
);
2393 context
.vc_proc
= p
;
2394 context
.vc_ucred
= fp
->f_cred
;
2396 lf
.l_whence
= SEEK_SET
;
2399 if (how
& LOCK_UN
) {
2400 lf
.l_type
= F_UNLCK
;
2401 fp
->f_flag
&= ~FHASLOCK
;
2402 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_UNLCK
, &lf
, F_FLOCK
, &context
);
2406 lf
.l_type
= F_WRLCK
;
2407 else if (how
& LOCK_SH
)
2408 lf
.l_type
= F_RDLCK
;
2413 fp
->f_flag
|= FHASLOCK
;
2414 if (how
& LOCK_NB
) {
2415 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, F_FLOCK
, &context
);
2418 error
= VNOP_ADVLOCK(vp
, (caddr_t
)fp
->f_fglob
, F_SETLK
, &lf
, F_FLOCK
|F_WAIT
, &context
);
2420 (void)vnode_put(vp
);
2422 fp_drop(p
, fd
, fp
, 0);
2428 * File Descriptor pseudo-device driver (/dev/fd/).
2430 * Opening minor device N dup()s the file (if any) connected to file
2431 * descriptor N belonging to the calling process. Note that this driver
2432 * consists of only the ``open()'' routine, because all subsequent
2433 * references to this file will be direct to the other driver.
2436 fdopen(dev_t dev
, __unused
int mode
, __unused
int type
, struct proc
*p
)
2440 * XXX Kludge: set curproc->p_dupfd to contain the value of the
2441 * the file descriptor being sought for duplication. The error
2442 * return ensures that the vnode for this device will be released
2443 * by vn_open. Open will detect this special error and take the
2444 * actions in dupfdopen below. Other callers of vn_open or vnop_open
2445 * will simply report the error.
2447 p
->p_dupfd
= minor(dev
);
2452 * Duplicate the specified descriptor to a free descriptor.
2455 dupfdopen(fdp
, indx
, dfd
, mode
, error
)
2456 register struct filedesc
*fdp
;
2457 register int indx
, dfd
;
2461 struct fileproc
*wfp
;
2462 struct fileproc
*fp
;
2463 struct proc
* p
= current_proc();
2466 * If the to-be-dup'd fd number is greater than the allowed number
2467 * of file descriptors, or the fd to be dup'd has already been
2468 * closed, reject. Note, check for new == old is necessary as
2469 * falloc could allocate an already closed to-be-dup'd descriptor
2470 * as the new descriptor.
2474 fp
= fdp
->fd_ofiles
[indx
];
2475 if (dfd
< 0 || dfd
>= fdp
->fd_nfiles
||
2476 (wfp
= fdp
->fd_ofiles
[dfd
]) == NULL
|| wfp
== fp
||
2477 (fdp
->fd_ofileflags
[dfd
] & UF_RESERVED
)) {
2483 * There are two cases of interest here.
2485 * For ENODEV simply dup (dfd) to file descriptor
2486 * (indx) and return.
2488 * For ENXIO steal away the file structure from (dfd) and
2489 * store it in (indx). (dfd) is effectively closed by
2492 * Any other error code is just returned.
2497 * Check that the mode the file is being opened for is a
2498 * subset of the mode of the existing descriptor.
2500 if (((mode
& (FREAD
|FWRITE
)) | wfp
->f_flag
) != wfp
->f_flag
) {
2504 if (indx
> fdp
->fd_lastfile
)
2505 fdp
->fd_lastfile
= indx
;
2509 fg_free(fp
->f_fglob
);
2510 fp
->f_fglob
= wfp
->f_fglob
;
2512 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
];
2519 * Steal away the file pointer from dfd, and stuff it into indx.
2521 if (indx
> fdp
->fd_lastfile
)
2522 fdp
->fd_lastfile
= indx
;
2525 fg_free(fp
->f_fglob
);
2526 fp
->f_fglob
= wfp
->f_fglob
;
2528 fdp
->fd_ofileflags
[indx
] = fdp
->fd_ofileflags
[dfd
];
2533 FREE_ZONE(wfp
, sizeof *fp
, M_FILEPROC
);
2545 fg_ref(struct fileproc
* fp
)
2547 struct fileglob
*fg
;
2551 lck_mtx_lock(&fg
->fg_lock
);
2553 lck_mtx_unlock(&fg
->fg_lock
);
2557 fg_drop(struct fileproc
* fp
)
2559 struct fileglob
*fg
;
2562 lck_mtx_lock(&fg
->fg_lock
);
2564 lck_mtx_unlock(&fg
->fg_lock
);
2569 fg_insertuipc(struct fileglob
* fg
)
2573 lck_mtx_lock(&fg
->fg_lock
);
2575 while (fg
->fg_lflags
& FG_RMMSGQ
) {
2576 fg
->fg_lflags
|= FG_WRMMSGQ
;
2577 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_insertuipc", 0);
2582 if (fg
->fg_msgcount
== 1) {
2583 fg
->fg_lflags
|= FG_INSMSGQ
;
2586 lck_mtx_unlock(&fg
->fg_lock
);
2589 lck_mtx_lock(uipc_lock
);
2590 LIST_INSERT_HEAD(&fmsghead
, fg
, f_msglist
);
2591 lck_mtx_unlock(uipc_lock
);
2592 lck_mtx_lock(&fg
->fg_lock
);
2593 fg
->fg_lflags
&= ~FG_INSMSGQ
;
2594 if (fg
->fg_lflags
& FG_WINSMSGQ
) {
2595 fg
->fg_lflags
&= ~FG_WINSMSGQ
;
2596 wakeup(&fg
->fg_lflags
);
2598 lck_mtx_unlock(&fg
->fg_lock
);
2604 fg_removeuipc(struct fileglob
* fg
)
2608 lck_mtx_lock(&fg
->fg_lock
);
2609 while (fg
->fg_lflags
& FG_INSMSGQ
) {
2610 fg
->fg_lflags
|= FG_WINSMSGQ
;
2611 msleep(&fg
->fg_lflags
, &fg
->fg_lock
, 0, "fg_removeuipc", 0);
2614 if (fg
->fg_msgcount
== 0) {
2615 fg
->fg_lflags
|= FG_RMMSGQ
;
2618 lck_mtx_unlock(&fg
->fg_lock
);
2621 lck_mtx_lock(uipc_lock
);
2622 LIST_REMOVE(fg
, f_msglist
);
2623 lck_mtx_unlock(uipc_lock
);
2624 lck_mtx_lock(&fg
->fg_lock
);
2625 fg
->fg_lflags
&= ~FG_RMMSGQ
;
2626 if (fg
->fg_lflags
& FG_WRMMSGQ
) {
2627 fg
->fg_lflags
&= ~FG_WRMMSGQ
;
2628 wakeup(&fg
->fg_lflags
);
2630 lck_mtx_unlock(&fg
->fg_lock
);
2636 fo_read(struct fileproc
*fp
, struct uio
*uio
, kauth_cred_t cred
, int flags
, struct proc
*p
)
2638 return ((*fp
->f_ops
->fo_read
)(fp
, uio
, cred
, flags
, p
));
2642 fo_write(struct fileproc
*fp
, struct uio
*uio
, kauth_cred_t cred
, int flags
, struct proc
*p
)
2644 return((*fp
->f_ops
->fo_write
)(fp
, uio
, cred
, flags
, p
));
2648 fo_ioctl(struct fileproc
*fp
, u_long com
, caddr_t data
, struct proc
*p
)
2653 error
= (*fp
->f_ops
->fo_ioctl
)(fp
, com
, data
, p
);
2659 fo_select(struct fileproc
*fp
, int which
, void *wql
, struct proc
*p
)
2661 return((*fp
->f_ops
->fo_select
)(fp
, which
, wql
, p
));
2665 fo_close(struct fileglob
*fg
, struct proc
*p
)
2667 return((*fg
->fg_ops
->fo_close
)(fg
, p
));
2671 fo_kqfilter(struct fileproc
*fp
, struct knote
*kn
, struct proc
*p
)
2673 return ((*fp
->f_ops
->fo_kqfilter
)(fp
, kn
, p
));